Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Nov 5, 2024
1 parent b365980 commit 6147086
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 25 deletions.
16 changes: 4 additions & 12 deletions state/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ func (sf *factory) Validate(ctx context.Context, blk *block.Block) error {
func (sf *factory) AtHeight(h uint64) (protocol.ArchiveStateSimulator, error) {
sf.mutex.RLock()
defer sf.mutex.RUnlock()
if h > sf.currentChainHeight {
return nil, errors.Errorf("query height %d is higher than tip height %d", h, sf.currentChainHeight)
}
if h == sf.currentChainHeight {
return sf, nil
}
Expand Down Expand Up @@ -413,16 +416,7 @@ func (sf *factory) SimulateExecution(
if err != nil {
return nil, nil, errors.Wrap(err, "failed to obtain working set from state factory")
}
cfg := &protocol.SimulateOptionConfig{}
for _, opt := range opts {
opt(cfg)
}
if cfg.PreOpt != nil {
if err := cfg.PreOpt(ws); err != nil {
return nil, nil, err
}
}
return evm.SimulateExecution(ctx, ws, caller, elp)
return simulateExecution(ctx, ws, caller, elp, opts...)
}

// ReadContractStorage reads contract's storage
Expand All @@ -438,9 +432,7 @@ func (sf *factory) ReadContractStorage(ctx context.Context, contract address.Add

// PutBlock persists all changes in RunActions() into the DB
func (sf *factory) PutBlock(ctx context.Context, blk *block.Block) error {
sf.mutex.Lock()
timer := sf.timerFactory.NewTimer("Commit")
sf.mutex.Unlock()
defer timer.End()
producer := blk.PublicKey().Address()
if producer == nil {
Expand Down
2 changes: 2 additions & 0 deletions state/factory/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,8 @@ func testHistoryState(sf Factory, t *testing.T, statetx, archive bool) {
_, err = sf.AtHeight(0)
require.Equal(t, ErrNotSupported, errors.Cause(err))
} else {
_, err = sf.AtHeight(10)
require.Contains(t, err.Error(), "query height 10 is higher than tip height 1")
sr, err := sf.AtHeight(0)
if !archive {
require.Equal(t, ErrNoArchiveData, errors.Cause(err))
Expand Down
40 changes: 27 additions & 13 deletions state/factory/factory_withheight.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import (

"github.com/pkg/errors"

"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/v2/action"
"github.com/iotexproject/iotex-core/v2/action/protocol"
"github.com/iotexproject/iotex-core/v2/action/protocol/execution/evm"
"github.com/iotexproject/iotex-core/v2/pkg/tracer"
"github.com/iotexproject/iotex-core/v2/state"
)

Expand All @@ -21,25 +25,17 @@ type factoryWithHeight struct {
}

func (sf *factoryWithHeight) State(s interface{}, opts ...protocol.StateOption) (uint64, error) {
sf.mutex.RLock()
defer sf.mutex.RUnlock()
cfg, err := processOptions(opts...)
if err != nil {
return 0, err
}
if cfg.Keys != nil {
return 0, errors.Wrap(ErrNotSupported, "Read state with keys option has not been implemented yet")
}
if sf.height > sf.currentChainHeight {
return 0, errors.Errorf("query height %d is higher than tip height %d", sf.height, sf.currentChainHeight)
}
return sf.height, sf.stateAtHeight(sf.height, cfg.Namespace, cfg.Key, s)
}

func (sf *factoryWithHeight) stateAtHeight(height uint64, ns string, key []byte, s interface{}) error {
if !sf.saveHistory {
return ErrNoArchiveData
}
tlt, err := newTwoLayerTrie(ArchiveTrieNamespace, sf.dao, fmt.Sprintf("%s-%d", ArchiveTrieRootKey, height), false)
if err != nil {
return errors.Wrapf(err, "failed to generate trie for %d", height)
Expand All @@ -57,11 +53,6 @@ func (sf *factoryWithHeight) stateAtHeight(height uint64, ns string, key []byte,
}

func (sf *factoryWithHeight) States(opts ...protocol.StateOption) (uint64, state.Iterator, error) {
sf.mutex.RLock()
defer sf.mutex.RUnlock()
if sf.height > sf.currentChainHeight {
return 0, nil, errors.Errorf("query height %d is higher than tip height %d", sf.height, sf.currentChainHeight)
}
cfg, err := processOptions(opts...)
if err != nil {
return 0, nil, err
Expand All @@ -87,3 +78,26 @@ func (sf *factoryWithHeight) States(opts ...protocol.StateOption) (uint64, state
}
return sf.height, iter, err
}

func (sf *factoryWithHeight) SimulateExecution(
ctx context.Context,
caller address.Address,
elp action.Envelope,
opts ...protocol.SimulateOption,
) ([]byte, *action.Receipt, error) {
ctx, span := tracer.NewSpan(ctx, "factoryWithHeight.SimulateExecution")
defer span.End()
ws, err := sf.newWorkingSet(ctx, sf.height)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to obtain working set from state factory")
}
return simulateExecution(ctx, ws, caller, elp)
}

func (sf *factoryWithHeight) ReadContractStorage(ctx context.Context, contract address.Address, key []byte) ([]byte, error) {
ws, err := sf.newWorkingSet(ctx, sf.height)
if err != nil {
return nil, errors.Wrap(err, "failed to generate working set from state factory")
}
return evm.ReadContractStorage(ctx, ws, contract, key)
}
21 changes: 21 additions & 0 deletions state/factory/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"github.com/iotexproject/go-pkgs/bloom"
"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/pkg/errors"

"github.com/iotexproject/iotex-core/v2/action"
"github.com/iotexproject/iotex-core/v2/action/protocol"
"github.com/iotexproject/iotex-core/v2/action/protocol/execution/evm"
"github.com/iotexproject/iotex-core/v2/blockchain/block"
"github.com/iotexproject/iotex-core/v2/blockchain/genesis"
"github.com/iotexproject/iotex-core/v2/db"
Expand Down Expand Up @@ -223,3 +225,22 @@ func newTwoLayerTrie(ns string, dao db.KVStore, rootKey string, create bool) (tr
}
return mptrie.NewTwoLayerTrie(dbForTrie, rootKey), nil
}

func simulateExecution(
ctx context.Context,
ws *workingSet,
caller address.Address,
elp action.Envelope,
opts ...protocol.SimulateOption,
) ([]byte, *action.Receipt, error) {
cfg := &protocol.SimulateOptionConfig{}
for _, opt := range opts {
opt(cfg)
}
if cfg.PreOpt != nil {
if err := cfg.PreOpt(ws); err != nil {
return nil, nil, err
}
}
return evm.SimulateExecution(ctx, ws, caller, elp)
}

0 comments on commit 6147086

Please sign in to comment.