From 5bec47df315a1024f9ef4f147906569a7e6af53b Mon Sep 17 00:00:00 2001 From: envestcc Date: Wed, 1 Nov 2023 22:00:17 +0800 Subject: [PATCH 1/4] use Params struct as main params in evm --- action/protocol/execution/evm/evm.go | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/action/protocol/execution/evm/evm.go b/action/protocol/execution/evm/evm.go index 8b2852cd97..cc73151c0d 100644 --- a/action/protocol/execution/evm/evm.go +++ b/action/protocol/execution/evm/evm.go @@ -92,6 +92,9 @@ type ( gas uint64 data []byte accessList types.AccessList + evmConfig vm.Config + chainConfig *params.ChainConfig + blkCtx protocol.BlockCtx } ) @@ -105,6 +108,7 @@ func newParams( actionCtx := protocol.MustGetActionCtx(ctx) blkCtx := protocol.MustGetBlockCtx(ctx) featureCtx := protocol.MustGetFeatureCtx(ctx) + g := genesis.MustExtractGenesisContext(ctx) executorAddr := common.BytesToAddress(actionCtx.Caller.Bytes()) var contractAddrPointer *common.Address if dest := execution.Contract(); dest != action.EmptyAddress { @@ -162,6 +166,12 @@ func newParams( Difficulty: new(big.Int).SetUint64(uint64(50)), BaseFee: new(big.Int), } + evmNetworkID := protocol.MustGetBlockchainCtx(ctx).EvmNetworkID + vmConfig := vm.Config{} + if vmCfg, ok := protocol.GetVMConfigCtx(ctx); ok { + vmConfig = vmCfg + } + chainConfig := getChainConfig(g.Blockchain, blkCtx.BlockHeight, evmNetworkID) return &Params{ context, @@ -170,13 +180,16 @@ func newParams( GasPrice: execution.GasPrice(), }, execution.Nonce(), - protocol.MustGetBlockchainCtx(ctx).EvmNetworkID, + evmNetworkID, actionCtx.Caller.String(), execution.Amount(), contractAddrPointer, gasLimit, execution.Data(), execution.AccessList(), + vmConfig, + chainConfig, + blkCtx, }, nil } @@ -221,7 +234,7 @@ func ExecuteContract( if err != nil { return nil, nil, err } - retval, depositGas, remainingGas, contractAddress, statusCode, err := executeInEVM(ctx, ps, stateDB, g.Blockchain, blkCtx.GasLimit, blkCtx.BlockHeight) + retval, depositGas, remainingGas, contractAddress, statusCode, err := executeInEVM(ctx, ps, stateDB, g.Blockchain) if err != nil { return nil, nil, err } @@ -403,20 +416,19 @@ func getChainConfig(g genesis.Blockchain, height uint64, id uint32) *params.Chai } // Error in executeInEVM is a consensus issue -func executeInEVM(ctx context.Context, evmParams *Params, stateDB *StateDBAdapter, g genesis.Blockchain, gasLimit uint64, blockHeight uint64) ([]byte, uint64, uint64, string, iotextypes.ReceiptStatus, error) { +func executeInEVM(ctx context.Context, evmParams *Params, stateDB *StateDBAdapter, g genesis.Blockchain) ([]byte, uint64, uint64, string, iotextypes.ReceiptStatus, error) { + gasLimit := evmParams.blkCtx.GasLimit + blockHeight := evmParams.blkCtx.BlockHeight remainingGas := evmParams.gas if err := securityDeposit(evmParams, stateDB, gasLimit); err != nil { log.L().Warn("unexpected error: not enough security deposit", zap.Error(err)) return nil, 0, 0, action.EmptyAddress, iotextypes.ReceiptStatus_Failure, err } var ( - config vm.Config accessList types.AccessList ) - if vmCfg, ok := protocol.GetVMConfigCtx(ctx); ok { - config = vmCfg - } - chainConfig := getChainConfig(g, blockHeight, evmParams.evmNetworkID) + config := evmParams.evmConfig + chainConfig := evmParams.chainConfig evm := vm.NewEVM(evmParams.context, evmParams.txCtx, stateDB, chainConfig, config) if g.IsOkhotsk(blockHeight) { accessList = evmParams.accessList From 2c60dbe3731696237db96465b09541ebde22176e Mon Sep 17 00:00:00 2001 From: envestcc Date: Wed, 1 Nov 2023 22:30:57 +0800 Subject: [PATCH 2/4] move to Params struct --- action/protocol/execution/evm/evm.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/action/protocol/execution/evm/evm.go b/action/protocol/execution/evm/evm.go index cc73151c0d..13f6446ee1 100644 --- a/action/protocol/execution/evm/evm.go +++ b/action/protocol/execution/evm/evm.go @@ -95,6 +95,9 @@ type ( evmConfig vm.Config chainConfig *params.ChainConfig blkCtx protocol.BlockCtx + genesis genesis.Blockchain + featureCtx protocol.FeatureCtx + actionCtx protocol.ActionCtx } ) @@ -190,6 +193,9 @@ func newParams( vmConfig, chainConfig, blkCtx, + g.Blockchain, + featureCtx, + actionCtx, }, nil } @@ -222,10 +228,7 @@ func ExecuteContract( ) ([]byte, *action.Receipt, error) { ctx, span := tracer.NewSpan(ctx, "evm.ExecuteContract") defer span.End() - actionCtx := protocol.MustGetActionCtx(ctx) - blkCtx := protocol.MustGetBlockCtx(ctx) - g := genesis.MustExtractGenesisContext(ctx) - featureCtx := protocol.MustGetFeatureCtx(ctx) + stateDB, err := prepareStateDB(ctx, sm) if err != nil { return nil, nil, err @@ -234,7 +237,10 @@ func ExecuteContract( if err != nil { return nil, nil, err } - retval, depositGas, remainingGas, contractAddress, statusCode, err := executeInEVM(ctx, ps, stateDB, g.Blockchain) + actionCtx := ps.actionCtx + blkCtx := ps.blkCtx + featureCtx := ps.featureCtx + retval, depositGas, remainingGas, contractAddress, statusCode, err := executeInEVM(ps, stateDB) if err != nil { return nil, nil, err } @@ -416,9 +422,10 @@ func getChainConfig(g genesis.Blockchain, height uint64, id uint32) *params.Chai } // Error in executeInEVM is a consensus issue -func executeInEVM(ctx context.Context, evmParams *Params, stateDB *StateDBAdapter, g genesis.Blockchain) ([]byte, uint64, uint64, string, iotextypes.ReceiptStatus, error) { +func executeInEVM(evmParams *Params, stateDB *StateDBAdapter) ([]byte, uint64, uint64, string, iotextypes.ReceiptStatus, error) { gasLimit := evmParams.blkCtx.GasLimit blockHeight := evmParams.blkCtx.BlockHeight + g := evmParams.genesis remainingGas := evmParams.gas if err := securityDeposit(evmParams, stateDB, gasLimit); err != nil { log.L().Warn("unexpected error: not enough security deposit", zap.Error(err)) @@ -493,7 +500,7 @@ func executeInEVM(ctx context.Context, evmParams *Params, stateDB *StateDBAdapte // the tx is reverted. After Okhotsk height, it is fixed inside RevertToSnapshot() var ( deltaRefundByDynamicGas = evm.DeltaRefundByDynamicGas - featureCtx = protocol.MustGetFeatureCtx(ctx) + featureCtx = evmParams.featureCtx ) if !featureCtx.CorrectGasRefund && deltaRefundByDynamicGas != 0 { if deltaRefundByDynamicGas > 0 { From 0a8efd363566559117f60a88cc7b4d7837f785f6 Mon Sep 17 00:00:00 2001 From: envestcc Date: Fri, 3 Nov 2023 10:14:57 +0800 Subject: [PATCH 3/4] format --- action/protocol/execution/evm/evm.go | 51 ++++++++++++++-------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/action/protocol/execution/evm/evm.go b/action/protocol/execution/evm/evm.go index 13f6446ee1..659239649f 100644 --- a/action/protocol/execution/evm/evm.go +++ b/action/protocol/execution/evm/evm.go @@ -108,12 +108,18 @@ func newParams( stateDB *StateDBAdapter, getBlockHash GetBlockHash, ) (*Params, error) { - actionCtx := protocol.MustGetActionCtx(ctx) - blkCtx := protocol.MustGetBlockCtx(ctx) - featureCtx := protocol.MustGetFeatureCtx(ctx) - g := genesis.MustExtractGenesisContext(ctx) + var ( + actionCtx = protocol.MustGetActionCtx(ctx) + blkCtx = protocol.MustGetBlockCtx(ctx) + featureCtx = protocol.MustGetFeatureCtx(ctx) + g = genesis.MustExtractGenesisContext(ctx) + evmNetworkID = protocol.MustGetBlockchainCtx(ctx).EvmNetworkID + + vmConfig vm.Config + contractAddrPointer *common.Address + getHashFn vm.GetHashFunc + ) executorAddr := common.BytesToAddress(actionCtx.Caller.Bytes()) - var contractAddrPointer *common.Address if dest := execution.Contract(); dest != action.EmptyAddress { contract, err := address.FromString(execution.Contract()) if err != nil { @@ -129,7 +135,6 @@ func newParams( gasLimit = _preAleutianActionGasLimit } - var getHashFn vm.GetHashFunc switch { case featureCtx.CorrectGetHashFn: getHashFn = func(n uint64) common.Hash { @@ -169,8 +174,6 @@ func newParams( Difficulty: new(big.Int).SetUint64(uint64(50)), BaseFee: new(big.Int), } - evmNetworkID := protocol.MustGetBlockchainCtx(ctx).EvmNetworkID - vmConfig := vm.Config{} if vmCfg, ok := protocol.GetVMConfigCtx(ctx); ok { vmConfig = vmCfg } @@ -237,17 +240,14 @@ func ExecuteContract( if err != nil { return nil, nil, err } - actionCtx := ps.actionCtx - blkCtx := ps.blkCtx - featureCtx := ps.featureCtx retval, depositGas, remainingGas, contractAddress, statusCode, err := executeInEVM(ps, stateDB) if err != nil { return nil, nil, err } receipt := &action.Receipt{ GasConsumed: ps.gas - remainingGas, - BlockHeight: blkCtx.BlockHeight, - ActionHash: actionCtx.ActionHash, + BlockHeight: ps.blkCtx.BlockHeight, + ActionHash: ps.actionCtx.ActionHash, ContractAddress: contractAddress, } @@ -256,7 +256,7 @@ func ExecuteContract( depositLog, burnLog *action.TransactionLog consumedGas = depositGas - remainingGas ) - if featureCtx.FixDoubleChargeGas { + if ps.featureCtx.FixDoubleChargeGas { // Refund all deposit and, actual gas fee will be subtracted when depositing gas fee to the rewarding protocol stateDB.AddBalance(ps.txCtx.Origin, big.NewInt(0).Mul(big.NewInt(0).SetUint64(depositGas), ps.txCtx.GasPrice)) } else { @@ -267,7 +267,7 @@ func ExecuteContract( if consumedGas > 0 { burnLog = &action.TransactionLog{ Type: iotextypes.TransactionLogType_GAS_FEE, - Sender: actionCtx.Caller.String(), + Sender: ps.actionCtx.Caller.String(), Recipient: "", // burned Amount: new(big.Int).Mul(new(big.Int).SetUint64(consumedGas), ps.txCtx.GasPrice), } @@ -279,7 +279,7 @@ func ExecuteContract( sharedGas uint64 sharedGasFee, totalGasFee *big.Int ) - if featureCtx.SharedGasWithDapp && sgd != nil { + if ps.featureCtx.SharedGasWithDapp && sgd != nil { 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") @@ -301,12 +301,12 @@ func ExecuteContract( } receipt.AddLogs(stateDB.Logs()...).AddTransactionLogs(depositLog, burnLog) if receipt.Status == uint64(iotextypes.ReceiptStatus_Success) || - featureCtx.AddOutOfGasToTransactionLog && receipt.Status == uint64(iotextypes.ReceiptStatus_ErrCodeStoreOutOfGas) { + ps.featureCtx.AddOutOfGasToTransactionLog && receipt.Status == uint64(iotextypes.ReceiptStatus_ErrCodeStoreOutOfGas) { receipt.AddTransactionLogs(stateDB.TransactionLogs()...) } stateDB.clear() - if featureCtx.SetRevertMessageToReceipt && receipt.Status == uint64(iotextypes.ReceiptStatus_ErrExecutionReverted) && retval != nil && bytes.Equal(retval[:4], _revertSelector) { + if ps.featureCtx.SetRevertMessageToReceipt && receipt.Status == uint64(iotextypes.ReceiptStatus_ErrExecutionReverted) && retval != nil && bytes.Equal(retval[:4], _revertSelector) { // in case of the execution revert error, parse the retVal and add to receipt data := retval[4:] msgLength := byteutil.BytesToUint64BigEndian(data[56:64]) @@ -423,10 +423,13 @@ func getChainConfig(g genesis.Blockchain, height uint64, id uint32) *params.Chai // Error in executeInEVM is a consensus issue func executeInEVM(evmParams *Params, stateDB *StateDBAdapter) ([]byte, uint64, uint64, string, iotextypes.ReceiptStatus, error) { - gasLimit := evmParams.blkCtx.GasLimit - blockHeight := evmParams.blkCtx.BlockHeight - g := evmParams.genesis - remainingGas := evmParams.gas + var ( + gasLimit = evmParams.blkCtx.GasLimit + blockHeight = evmParams.blkCtx.BlockHeight + g = evmParams.genesis + remainingGas = evmParams.gas + chainConfig = evmParams.chainConfig + ) if err := securityDeposit(evmParams, stateDB, gasLimit); err != nil { log.L().Warn("unexpected error: not enough security deposit", zap.Error(err)) return nil, 0, 0, action.EmptyAddress, iotextypes.ReceiptStatus_Failure, err @@ -434,9 +437,7 @@ func executeInEVM(evmParams *Params, stateDB *StateDBAdapter) ([]byte, uint64, u var ( accessList types.AccessList ) - config := evmParams.evmConfig - chainConfig := evmParams.chainConfig - evm := vm.NewEVM(evmParams.context, evmParams.txCtx, stateDB, chainConfig, config) + evm := vm.NewEVM(evmParams.context, evmParams.txCtx, stateDB, chainConfig, evmParams.evmConfig) if g.IsOkhotsk(blockHeight) { accessList = evmParams.accessList } From 66d0e8576525d9b46970c5d1e182c241304fe72f Mon Sep 17 00:00:00 2001 From: envestcc Date: Fri, 3 Nov 2023 11:04:07 +0800 Subject: [PATCH 4/4] format --- action/protocol/execution/evm/evm.go | 35 ++++++++++++++-------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/action/protocol/execution/evm/evm.go b/action/protocol/execution/evm/evm.go index 659239649f..9b0433805b 100644 --- a/action/protocol/execution/evm/evm.go +++ b/action/protocol/execution/evm/evm.go @@ -82,22 +82,21 @@ func MakeTransfer(db vm.StateDB, fromHash, toHash common.Address, amount *big.In type ( // Params is the context and parameters Params struct { - context vm.BlockContext - txCtx vm.TxContext - nonce uint64 - evmNetworkID uint32 - executorRawAddress string - amount *big.Int - contract *common.Address - gas uint64 - data []byte - accessList types.AccessList - evmConfig vm.Config - chainConfig *params.ChainConfig - blkCtx protocol.BlockCtx - genesis genesis.Blockchain - featureCtx protocol.FeatureCtx - actionCtx protocol.ActionCtx + context vm.BlockContext + txCtx vm.TxContext + nonce uint64 + evmNetworkID uint32 + amount *big.Int + contract *common.Address + gas uint64 + data []byte + accessList types.AccessList + evmConfig vm.Config + chainConfig *params.ChainConfig + blkCtx protocol.BlockCtx + genesis genesis.Blockchain + featureCtx protocol.FeatureCtx + actionCtx protocol.ActionCtx } ) @@ -114,12 +113,13 @@ func newParams( featureCtx = protocol.MustGetFeatureCtx(ctx) g = genesis.MustExtractGenesisContext(ctx) evmNetworkID = protocol.MustGetBlockchainCtx(ctx).EvmNetworkID + executorAddr = common.BytesToAddress(actionCtx.Caller.Bytes()) vmConfig vm.Config contractAddrPointer *common.Address getHashFn vm.GetHashFunc ) - executorAddr := common.BytesToAddress(actionCtx.Caller.Bytes()) + if dest := execution.Contract(); dest != action.EmptyAddress { contract, err := address.FromString(execution.Contract()) if err != nil { @@ -187,7 +187,6 @@ func newParams( }, execution.Nonce(), evmNetworkID, - actionCtx.Caller.String(), execution.Amount(), contractAddrPointer, gasLimit,