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 52fbe83
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 19 deletions.
13 changes: 1 addition & 12 deletions state/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,16 +413,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 +429,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
43 changes: 36 additions & 7 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,17 +25,18 @@ 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)
sf.mutex.RLock()
tipHeight := sf.currentChainHeight
sf.mutex.RUnlock()
if sf.height > tipHeight {
return 0, errors.Errorf("query height %d is higher than tip height %d", sf.height, tipHeight)
}
return sf.height, sf.stateAtHeight(sf.height, cfg.Namespace, cfg.Key, s)
}
Expand All @@ -58,9 +63,10 @@ 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)
tipHeight := sf.currentChainHeight
sf.mutex.RUnlock()
if sf.height > tipHeight {
return 0, nil, errors.Errorf("query height %d is higher than tip height %d", sf.height, tipHeight)
}
cfg, err := processOptions(opts...)
if err != nil {
Expand All @@ -87,3 +93,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 52fbe83

Please sign in to comment.