-
Notifications
You must be signed in to change notification settings - Fork 324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[evm] refactor evm functions parameters #3959
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,7 @@ type ( | |
genesis genesis.Blockchain | ||
featureCtx protocol.FeatureCtx | ||
actionCtx protocol.ActionCtx | ||
helperCtx HelperContext | ||
} | ||
) | ||
|
||
|
@@ -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 | ||
|
@@ -198,6 +200,7 @@ func newParams( | |
g.Blockchain, | ||
featureCtx, | ||
actionCtx, | ||
helperCtx, | ||
}, nil | ||
} | ||
|
||
|
@@ -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() | ||
|
@@ -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 | ||
|
@@ -279,6 +280,7 @@ func ExecuteContract( | |
sharedGasFee, totalGasFee *big.Int | ||
) | ||
if ps.featureCtx.SharedGasWithDapp && sgd != nil { | ||
// TODO: sgd is whether nil should be checked in processSGD | ||
receiver, sharedGas, err = processSGD(ctx, sm, execution, consumedGas, sgd) | ||
if err != nil { | ||
return nil, nil, errors.Wrap(err, "failed to process Sharing of Gas-fee with DApps") | ||
|
@@ -289,7 +291,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 | ||
} | ||
|
@@ -610,7 +612,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() | ||
|
@@ -638,14 +639,18 @@ func SimulateExecution( | |
) | ||
|
||
ctx = protocol.WithFeatureCtx(ctx) | ||
// TODO: move the logic out of SimulateExecution | ||
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, | ||
}) | ||
Comment on lines
+643
to
+650
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is preferred to be done in this pr since putting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will open another PR to address it, to avoid causing too many code conflicts in the upcoming PRs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add TODO |
||
return ExecuteContract( | ||
ctx, | ||
sm, | ||
ex, | ||
getBlockHash, | ||
func(context.Context, protocol.StateManager, address.Address, *big.Int, *big.Int) (*action.TransactionLog, error) { | ||
return nil, nil | ||
}, | ||
nil, | ||
Comment on lines
-646
to
-649
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrong implementation. |
||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ( | ||
|
@@ -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 | ||
|
@@ -291,7 +294,11 @@ func getContractReaderForGenesisStates(ctx context.Context, sm protocol.StateMan | |
return nil, err | ||
} | ||
|
||
res, _, err := evm.SimulateExecution(ctx, sm, addr, ex, getBlockHash) | ||
// TODO: move to the caller, then getBlockHash param can be removed | ||
ctx = evm.WithHelperCtx(ctx, evm.HelperContext{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WithHelperCtx could be called by the caller, then There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added a TODO |
||
GetBlockHash: getBlockHash, | ||
}) | ||
res, _, err := evm.SimulateExecution(ctx, sm, addr, ex) | ||
|
||
return res, err | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
Comment on lines
-143
to
-149
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a TODO |
||
) | ||
if err != nil { | ||
return err | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()) | ||
} | ||
|
@@ -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 | ||
} | ||
|
@@ -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 | ||
|
@@ -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 | ||
} | ||
|
||
|
@@ -1731,3 +1732,11 @@ 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) { | ||
// TODO: add depositGas | ||
ctx = evm.WithHelperCtx(ctx, evm.HelperContext{ | ||
GetBlockHash: getBlockHash, | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a |
||
return core.sf.SimulateExecution(ctx, addr, exec) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -617,7 +618,11 @@ func (builder *Builder) registerRollDPoSProtocol() error { | |
return nil, err | ||
} | ||
|
||
data, _, err := factory.SimulateExecution(ctx, addr, ex, dao.GetBlockHash) | ||
// TODO: add depositGas | ||
ctx = evm.WithHelperCtx(ctx, evm.HelperContext{ | ||
GetBlockHash: dao.GetBlockHash, | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
data, _, err := factory.SimulateExecution(ctx, addr, ex) | ||
|
||
return data, err | ||
}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change L277-287:
after modifying
func processSGD(ctx, sm, execution, consumeGas, helperCtx)