Skip to content

Commit

Permalink
refactor evm function parameters by HelperContext
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed Nov 3, 2023
1 parent 9715764 commit 535abf8
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 43 deletions.
33 changes: 33 additions & 0 deletions action/protocol/execution/evm/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package evm

import (
"context"

"github.com/iotexproject/iotex-core/pkg/log"
)

type (
helperContextKey struct{}

// HelperContext is the context for EVM helper
HelperContext struct {
GetBlockHash GetBlockHash
DepositGasFunc DepositGasWithSGD
// TODO: sgd should be moved into depositGasFunc
Sgd SGDRegistry
}
)

// WithHelperCtx returns a new context with helper context
func WithHelperCtx(ctx context.Context, hctx HelperContext) context.Context {
return context.WithValue(ctx, helperContextKey{}, hctx)
}

// mustGetHelperCtx returns the helper context from the context
func mustGetHelperCtx(ctx context.Context) HelperContext {
hc, ok := ctx.Value(helperContextKey{}).(HelperContext)
if !ok {
log.S().Panic("Miss evm helper context")
}
return hc
}
27 changes: 15 additions & 12 deletions action/protocol/execution/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ type (
genesis genesis.Blockchain
featureCtx protocol.FeatureCtx
actionCtx protocol.ActionCtx
helperCtx HelperContext
}
)

Expand All @@ -105,15 +106,16 @@ func newParams(
ctx context.Context,
execution *action.Execution,
stateDB *StateDBAdapter,
getBlockHash GetBlockHash,
) (*Params, error) {
var (
actionCtx = protocol.MustGetActionCtx(ctx)
blkCtx = protocol.MustGetBlockCtx(ctx)
featureCtx = protocol.MustGetFeatureCtx(ctx)
g = genesis.MustExtractGenesisContext(ctx)
helperCtx = mustGetHelperCtx(ctx)
evmNetworkID = protocol.MustGetBlockchainCtx(ctx).EvmNetworkID
executorAddr = common.BytesToAddress(actionCtx.Caller.Bytes())
getBlockHash = helperCtx.GetBlockHash

vmConfig vm.Config
contractAddrPointer *common.Address
Expand Down Expand Up @@ -198,6 +200,7 @@ func newParams(
g.Blockchain,
featureCtx,
actionCtx,
helperCtx,
}, nil
}

Expand All @@ -224,9 +227,6 @@ func ExecuteContract(
ctx context.Context,
sm protocol.StateManager,
execution *action.Execution,
getBlockHash GetBlockHash,
depositGasFunc DepositGasWithSGD,
sgd SGDRegistry,
) ([]byte, *action.Receipt, error) {
ctx, span := tracer.NewSpan(ctx, "evm.ExecuteContract")
defer span.End()
Expand All @@ -235,10 +235,11 @@ func ExecuteContract(
if err != nil {
return nil, nil, err
}
ps, err := newParams(ctx, execution, stateDB, getBlockHash)
ps, err := newParams(ctx, execution, stateDB)
if err != nil {
return nil, nil, err
}
sgd := ps.helperCtx.Sgd
retval, depositGas, remainingGas, contractAddress, statusCode, err := executeInEVM(ps, stateDB)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -289,7 +290,7 @@ func ExecuteContract(
sharedGasFee.Mul(sharedGasFee, ps.txCtx.GasPrice)
}
totalGasFee = new(big.Int).Mul(new(big.Int).SetUint64(consumedGas), ps.txCtx.GasPrice)
depositLog, err = depositGasFunc(ctx, sm, receiver, totalGasFee, sharedGasFee)
depositLog, err = ps.helperCtx.DepositGasFunc(ctx, sm, receiver, totalGasFee, sharedGasFee)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -610,7 +611,6 @@ func SimulateExecution(
sm protocol.StateManager,
caller address.Address,
ex *action.Execution,
getBlockHash GetBlockHash,
) ([]byte, *action.Receipt, error) {
ctx, span := tracer.NewSpan(ctx, "evm.SimulateExecution")
defer span.End()
Expand Down Expand Up @@ -638,14 +638,17 @@ func SimulateExecution(
)

ctx = protocol.WithFeatureCtx(ctx)
helperCtx := mustGetHelperCtx(ctx)
ctx = WithHelperCtx(ctx, HelperContext{
GetBlockHash: helperCtx.GetBlockHash,
DepositGasFunc: func(context.Context, protocol.StateManager, address.Address, *big.Int, *big.Int) (*action.TransactionLog, error) {
return nil, nil
},
Sgd: nil,
})
return ExecuteContract(
ctx,
sm,
ex,
getBlockHash,
func(context.Context, protocol.StateManager, address.Address, *big.Int, *big.Int) (*action.TransactionLog, error) {
return nil, nil
},
nil,
)
}
11 changes: 8 additions & 3 deletions action/protocol/execution/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ package execution
import (
"context"

"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/pkg/errors"
"go.uber.org/zap"

"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/action/protocol/execution/evm"
Expand Down Expand Up @@ -66,7 +66,12 @@ func (p *Protocol) Handle(ctx context.Context, act action.Action, sm protocol.St
if !ok {
return nil, nil
}
_, receipt, err := evm.ExecuteContract(ctx, sm, exec, p.getBlockHash, p.depositGas, p.sgdRegistry)
ctx = evm.WithHelperCtx(ctx, evm.HelperContext{
GetBlockHash: p.getBlockHash,
DepositGasFunc: p.depositGas,
Sgd: p.sgdRegistry,
})
_, receipt, err := evm.ExecuteContract(ctx, sm, exec)

if err != nil {
return nil, errors.Wrap(err, "failed to execute contract")
Expand Down
28 changes: 17 additions & 11 deletions action/protocol/poll/consortium.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
"go.uber.org/zap"

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/action/protocol/execution/evm"
"github.com/iotexproject/iotex-core/action/protocol/rolldpos"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/state"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
"go.uber.org/zap"
)

var (
Expand Down Expand Up @@ -124,19 +125,21 @@ func (cc *consortiumCommittee) CreateGenesisStates(ctx context.Context, sm proto
}
ctx = protocol.WithActionCtx(ctx, actionCtx)
ctx = protocol.WithBlockCtx(ctx, blkCtx)
ctx = evm.WithHelperCtx(ctx, evm.HelperContext{
GetBlockHash: func(height uint64) (hash.Hash256, error) {
return hash.ZeroHash256, nil
},
DepositGasFunc: func(context.Context, protocol.StateManager, address.Address, *big.Int, *big.Int) (*action.TransactionLog, error) {
return nil, nil
},
Sgd: nil,
})

// deploy consortiumCommittee contract
_, receipt, err := evm.ExecuteContract(
ctx,
sm,
execution,
func(height uint64) (hash.Hash256, error) {
return hash.ZeroHash256, nil
},
func(context.Context, protocol.StateManager, address.Address, *big.Int, *big.Int) (*action.TransactionLog, error) {
return nil, nil
},
nil,
)
if err != nil {
return err
Expand Down Expand Up @@ -291,7 +294,10 @@ func getContractReaderForGenesisStates(ctx context.Context, sm protocol.StateMan
return nil, err
}

res, _, err := evm.SimulateExecution(ctx, sm, addr, ex, getBlockHash)
ctx = evm.WithHelperCtx(ctx, evm.HelperContext{
GetBlockHash: getBlockHash,
})
res, _, err := evm.SimulateExecution(ctx, sm, addr, ex)

return res, err
}
Expand Down
16 changes: 9 additions & 7 deletions action/protocol/poll/staking_committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,20 @@ func (sc *stakingCommittee) CreateGenesisStates(ctx context.Context, sm protocol
}
ctx = protocol.WithActionCtx(ctx, actionCtx)
ctx = protocol.WithBlockCtx(ctx, blkCtx)
ctx = evm.WithHelperCtx(ctx, evm.HelperContext{
GetBlockHash: func(height uint64) (hash.Hash256, error) {
return hash.ZeroHash256, nil
},
DepositGasFunc: func(context.Context, protocol.StateManager, address.Address, *big.Int, *big.Int) (*action.TransactionLog, error) {
return nil, nil
},
Sgd: nil,
})
// deploy native staking contract
_, receipt, err := evm.ExecuteContract(
ctx,
sm,
execution,
func(height uint64) (hash.Hash256, error) {
return hash.ZeroHash256, nil
},
func(context.Context, protocol.StateManager, address.Address, *big.Int, *big.Int) (*action.TransactionLog, error) {
return nil, nil
},
nil,
)
if err != nil {
return err
Expand Down
16 changes: 12 additions & 4 deletions api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ func (core *coreService) ReadContract(ctx context.Context, callerAddr address.Ad
}
sc.SetGasPrice(big.NewInt(0)) // ReadContract() is read-only, use 0 to prevent insufficient gas

retval, receipt, err := core.sf.SimulateExecution(ctx, callerAddr, sc, core.dao.GetBlockHash)
retval, receipt, err := core.simulateExecution(ctx, callerAddr, sc, core.dao.GetBlockHash)
if err != nil {
return "", nil, status.Error(codes.Internal, err.Error())
}
Expand Down Expand Up @@ -1479,7 +1479,8 @@ func (core *coreService) isGasLimitEnough(
if err != nil {
return false, nil, err
}
_, receipt, err := core.sf.SimulateExecution(ctx, caller, sc, core.dao.GetBlockHash)

_, receipt, err := core.simulateExecution(ctx, caller, sc, core.dao.GetBlockHash)
if err != nil {
return false, nil, err
}
Expand Down Expand Up @@ -1628,7 +1629,7 @@ func (core *coreService) SimulateExecution(ctx context.Context, addr address.Add
return nil, nil, err
}
exec.SetGasLimit(core.bc.Genesis().BlockGasLimit)
return core.sf.SimulateExecution(ctx, addr, exec, core.dao.GetBlockHash)
return core.simulateExecution(ctx, addr, exec, core.dao.GetBlockHash)
}

// SyncingProgress returns the syncing status of node
Expand Down Expand Up @@ -1715,7 +1716,7 @@ func (core *coreService) TraceCall(ctx context.Context,
getblockHash := func(height uint64) (hash.Hash256, error) {
return blkHash, nil
}
retval, receipt, err := core.sf.SimulateExecution(ctx, callerAddr, exec, getblockHash)
retval, receipt, err := core.simulateExecution(ctx, callerAddr, exec, getblockHash)
return retval, receipt, traces, err
}

Expand All @@ -1731,3 +1732,10 @@ func (core *coreService) Track(ctx context.Context, start time.Time, method stri
Success: success,
}, size)
}

func (core *coreService) simulateExecution(ctx context.Context, addr address.Address, exec *action.Execution, getBlockHash evm.GetBlockHash) ([]byte, *action.Receipt, error) {
ctx = evm.WithHelperCtx(ctx, evm.HelperContext{
GetBlockHash: getBlockHash,
})
return core.sf.SimulateExecution(ctx, addr, exec)
}
6 changes: 5 additions & 1 deletion chainservice/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/iotexproject/iotex-core/action/protocol/account"
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
"github.com/iotexproject/iotex-core/action/protocol/execution"
"github.com/iotexproject/iotex-core/action/protocol/execution/evm"
"github.com/iotexproject/iotex-core/action/protocol/poll"
"github.com/iotexproject/iotex-core/action/protocol/rewarding"
"github.com/iotexproject/iotex-core/action/protocol/rolldpos"
Expand Down Expand Up @@ -617,7 +618,10 @@ func (builder *Builder) registerRollDPoSProtocol() error {
return nil, err
}

data, _, err := factory.SimulateExecution(ctx, addr, ex, dao.GetBlockHash)
ctx = evm.WithHelperCtx(ctx, evm.HelperContext{
GetBlockHash: dao.GetBlockHash,
})
data, _, err := factory.SimulateExecution(ctx, addr, ex)

return data, err
},
Expand Down
5 changes: 2 additions & 3 deletions state/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ type (
Validate(context.Context, *block.Block) error
// NewBlockBuilder creates block builder
NewBlockBuilder(context.Context, actpool.ActPool, func(action.Envelope) (action.SealedEnvelope, error)) (*block.Builder, error)
SimulateExecution(context.Context, address.Address, *action.Execution, evm.GetBlockHash) ([]byte, *action.Receipt, error)
SimulateExecution(context.Context, address.Address, *action.Execution) ([]byte, *action.Receipt, error)
ReadContractStorage(context.Context, address.Address, []byte) ([]byte, error)
PutBlock(context.Context, *block.Block) error
DeleteTipBlock(context.Context, *block.Block) error
Expand Down Expand Up @@ -387,7 +387,6 @@ func (sf *factory) SimulateExecution(
ctx context.Context,
caller address.Address,
ex *action.Execution,
getBlockHash evm.GetBlockHash,
) ([]byte, *action.Receipt, error) {
ctx, span := tracer.NewSpan(ctx, "factory.SimulateExecution")
defer span.End()
Expand All @@ -399,7 +398,7 @@ func (sf *factory) SimulateExecution(
return nil, nil, errors.Wrap(err, "failed to obtain working set from state factory")
}

return evm.SimulateExecution(ctx, ws, caller, ex, getBlockHash)
return evm.SimulateExecution(ctx, ws, caller, ex)
}

// ReadContractStorage reads contract's storage
Expand Down
3 changes: 1 addition & 2 deletions state/factory/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ func (sdb *stateDB) SimulateExecution(
ctx context.Context,
caller address.Address,
ex *action.Execution,
getBlockHash evm.GetBlockHash,
) ([]byte, *action.Receipt, error) {
ctx, span := tracer.NewSpan(ctx, "stateDB.SimulateExecution")
defer span.End()
Expand All @@ -280,7 +279,7 @@ func (sdb *stateDB) SimulateExecution(
return nil, nil, err
}

return evm.SimulateExecution(ctx, ws, caller, ex, getBlockHash)
return evm.SimulateExecution(ctx, ws, caller, ex)
}

// ReadContractStorage reads contract's storage
Expand Down

0 comments on commit 535abf8

Please sign in to comment.