Skip to content

Commit

Permalink
Merge 66d0e85 into 32cbd3c
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed Nov 3, 2023
2 parents 32cbd3c + 66d0e85 commit 10f0aa3
Showing 1 changed file with 58 additions and 39 deletions.
97 changes: 58 additions & 39 deletions action/protocol/execution/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +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
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
}
)

Expand All @@ -102,11 +107,19 @@ func newParams(
stateDB *StateDBAdapter,
getBlockHash GetBlockHash,
) (*Params, error) {
actionCtx := protocol.MustGetActionCtx(ctx)
blkCtx := protocol.MustGetBlockCtx(ctx)
featureCtx := protocol.MustGetFeatureCtx(ctx)
executorAddr := common.BytesToAddress(actionCtx.Caller.Bytes())
var contractAddrPointer *common.Address
var (
actionCtx = protocol.MustGetActionCtx(ctx)
blkCtx = protocol.MustGetBlockCtx(ctx)
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
)

if dest := execution.Contract(); dest != action.EmptyAddress {
contract, err := address.FromString(execution.Contract())
if err != nil {
Expand All @@ -122,7 +135,6 @@ func newParams(
gasLimit = _preAleutianActionGasLimit
}

var getHashFn vm.GetHashFunc
switch {
case featureCtx.CorrectGetHashFn:
getHashFn = func(n uint64) common.Hash {
Expand Down Expand Up @@ -162,6 +174,10 @@ func newParams(
Difficulty: new(big.Int).SetUint64(uint64(50)),
BaseFee: new(big.Int),
}
if vmCfg, ok := protocol.GetVMConfigCtx(ctx); ok {
vmConfig = vmCfg

Check warning on line 178 in action/protocol/execution/evm/evm.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/execution/evm/evm.go#L178

Added line #L178 was not covered by tests
}
chainConfig := getChainConfig(g.Blockchain, blkCtx.BlockHeight, evmNetworkID)

return &Params{
context,
Expand All @@ -170,13 +186,18 @@ func newParams(
GasPrice: execution.GasPrice(),
},
execution.Nonce(),
protocol.MustGetBlockchainCtx(ctx).EvmNetworkID,
actionCtx.Caller.String(),
evmNetworkID,
execution.Amount(),
contractAddrPointer,
gasLimit,
execution.Data(),
execution.AccessList(),
vmConfig,
chainConfig,
blkCtx,
g.Blockchain,
featureCtx,
actionCtx,
}, nil
}

Expand Down Expand Up @@ -209,10 +230,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
Expand All @@ -221,14 +239,14 @@ 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(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,

Check warning on line 249 in action/protocol/execution/evm/evm.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/execution/evm/evm.go#L248-L249

Added lines #L248 - L249 were not covered by tests
ContractAddress: contractAddress,
}

Expand All @@ -237,7 +255,7 @@ func ExecuteContract(
depositLog, burnLog *action.TransactionLog
consumedGas = depositGas - remainingGas
)
if featureCtx.FixDoubleChargeGas {
if ps.featureCtx.FixDoubleChargeGas {

Check warning on line 258 in action/protocol/execution/evm/evm.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/execution/evm/evm.go#L254-L258

Added lines #L254 - L258 were not covered by tests
// 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 {
Expand All @@ -248,7 +266,7 @@ func ExecuteContract(
if consumedGas > 0 {

Check warning on line 266 in action/protocol/execution/evm/evm.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/execution/evm/evm.go#L266

Added line #L266 was not covered by tests
burnLog = &action.TransactionLog{
Type: iotextypes.TransactionLogType_GAS_FEE,
Sender: actionCtx.Caller.String(),
Sender: ps.actionCtx.Caller.String(),

Check warning on line 269 in action/protocol/execution/evm/evm.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/execution/evm/evm.go#L269

Added line #L269 was not covered by tests
Recipient: "", // burned
Amount: new(big.Int).Mul(new(big.Int).SetUint64(consumedGas), ps.txCtx.GasPrice),

Check warning on line 271 in action/protocol/execution/evm/evm.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/execution/evm/evm.go#L271

Added line #L271 was not covered by tests
}
Expand All @@ -260,7 +278,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 {

Check warning on line 283 in action/protocol/execution/evm/evm.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/execution/evm/evm.go#L275-L283

Added lines #L275 - L283 were not covered by tests
return nil, nil, errors.Wrap(err, "failed to process Sharing of Gas-fee with DApps")
Expand All @@ -282,12 +300,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) {

Check warning on line 303 in action/protocol/execution/evm/evm.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/execution/evm/evm.go#L303

Added line #L303 was not covered by tests
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) {

Check warning on line 308 in action/protocol/execution/evm/evm.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/execution/evm/evm.go#L308

Added line #L308 was not covered by tests
// in case of the execution revert error, parse the retVal and add to receipt
data := retval[4:]
msgLength := byteutil.BytesToUint64BigEndian(data[56:64])
Expand Down Expand Up @@ -403,21 +421,22 @@ 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) {
remainingGas := evmParams.gas
func executeInEVM(evmParams *Params, stateDB *StateDBAdapter) ([]byte, uint64, uint64, string, iotextypes.ReceiptStatus, error) {
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
}
var (
config vm.Config
accessList types.AccessList
)
if vmCfg, ok := protocol.GetVMConfigCtx(ctx); ok {
config = vmCfg
}
chainConfig := getChainConfig(g, blockHeight, evmParams.evmNetworkID)
evm := vm.NewEVM(evmParams.context, evmParams.txCtx, stateDB, chainConfig, config)
evm := vm.NewEVM(evmParams.context, evmParams.txCtx, stateDB, chainConfig, evmParams.evmConfig)

Check warning on line 439 in action/protocol/execution/evm/evm.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/execution/evm/evm.go#L439

Added line #L439 was not covered by tests
if g.IsOkhotsk(blockHeight) {
accessList = evmParams.accessList
}
Expand Down Expand Up @@ -481,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

Check warning on line 503 in action/protocol/execution/evm/evm.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/execution/evm/evm.go#L503

Added line #L503 was not covered by tests
)
if !featureCtx.CorrectGasRefund && deltaRefundByDynamicGas != 0 {
if deltaRefundByDynamicGas > 0 {
Expand Down

0 comments on commit 10f0aa3

Please sign in to comment.