Skip to content

Commit

Permalink
Merge pull request onflow#5432 from onflow/ramtin/flow-evm-disable-ba…
Browse files Browse the repository at this point in the history
…se-fee

[Flow EVM] Add test for tx with zero fee or dynamic fees
  • Loading branch information
ramtinms authored Feb 24, 2024
2 parents e1e2ddd + 75e1537 commit 9ca5a55
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
5 changes: 5 additions & 0 deletions fvm/evm/emulator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,15 @@ var DefaultChainConfig = &gethParams.ChainConfig{
PragueTime: &zero, // already on Prague
}

// Default config supports the dynamic fee structure (EIP-1559)
// so it accepts both legacy transactions with a fixed gas price
// and dynamic transactions with tip and cap.
// Yet default config keeps the base fee to zero (no automatic adjustment)
func defaultConfig() *Config {
return &Config{
ChainConfig: DefaultChainConfig,
EVMConfig: gethVM.Config{
// setting this flag let us we force the base fee to zero (coinbase will collect)
NoBaseFee: true,
},
TxContext: &gethVM.TxContext{
Expand Down
48 changes: 47 additions & 1 deletion fvm/evm/emulator/emulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,10 @@ func TestContractInteraction(t *testing.T) {
gethCommon.Big1, // gas fee

)
_, err = blk.RunTransaction(tx)
res, err := blk.RunTransaction(tx)
require.NoError(t, err)
require.NoError(t, res.VMError)
require.Greater(t, res.GasConsumed, uint64(0))

// check the balance of coinbase
RunWithNewReadOnlyBlockView(t, env, func(blk2 types.ReadOnlyBlockView) {
Expand All @@ -249,6 +251,50 @@ func TestContractInteraction(t *testing.T) {
})
})
})

t.Run("test runing transactions with dynamic fees (happy case)", func(t *testing.T) {
account := testutils.GetTestEOAAccount(t, testutils.EOATestAccount1KeyHex)
fAddr := account.Address()
RunWithNewEmulator(t, backend, rootAddr, func(env *emulator.Emulator) {
RunWithNewBlockView(t, env, func(blk types.BlockView) {
_, err := blk.DirectCall(types.NewDepositCall(fAddr, amount, account.Nonce()))
require.NoError(t, err)
})
})
account.SetNonce(account.Nonce() + 1)

RunWithNewEmulator(t, backend, rootAddr, func(env *emulator.Emulator) {
ctx := types.NewDefaultBlockContext(blockNumber.Uint64())
ctx.GasFeeCollector = types.NewAddressFromString("coinbase")
coinbaseOrgBalance := gethCommon.Big1
// small amount of money to create account
RunWithNewBlockView(t, env, func(blk types.BlockView) {
_, err := blk.DirectCall(types.NewDepositCall(ctx.GasFeeCollector, coinbaseOrgBalance, 1))
require.NoError(t, err)
})

blk, err := env.NewBlockView(ctx)
require.NoError(t, err)
tx := account.SignTx(
t,
gethTypes.NewTx(&gethTypes.DynamicFeeTx{
Nonce: account.Nonce(),
GasTipCap: big.NewInt(2),
GasFeeCap: big.NewInt(3),
Gas: gethParams.TxGas,
To: &gethCommon.Address{},
Value: big.NewInt(1),
}),
)
account.SetNonce(account.Nonce() + 1)

res, err := blk.RunTransaction(tx)
require.NoError(t, err)
require.NoError(t, res.VMError)
require.Greater(t, res.GasConsumed, uint64(0))
})
})

t.Run("test sending transactions (invalid nonce)", func(t *testing.T) {
account := testutils.GetTestEOAAccount(t, testutils.EOATestAccount1KeyHex)
fAddr := account.Address()
Expand Down
8 changes: 6 additions & 2 deletions fvm/evm/types/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const (
DeployCallSubType = byte(4)
ContractCallSubType = byte(5)

// Note that these gas values might need to change if we
// change the transaction (e.g. add accesslist),
// then it has to be updated to use Intrinsic function
// to calculate the minimum gas needed to run the transaction.
DepositCallGasLimit = gethParams.TxGas
WithdrawCallGasLimit = gethParams.TxGas

Expand Down Expand Up @@ -71,8 +75,8 @@ func (dc *DirectCall) Message() *gethCore.Message {
Nonce: dc.Nonce,
GasLimit: dc.GasLimit,
GasPrice: big.NewInt(0), // price is set to zero fo direct calls
GasTipCap: big.NewInt(1), // also known as maxPriorityFeePerGas (in GWei)
GasFeeCap: big.NewInt(2), // also known as maxFeePerGas (in GWei)
GasTipCap: big.NewInt(0), // also known as maxPriorityFeePerGas (in GWei)
GasFeeCap: big.NewInt(0), // also known as maxFeePerGas (in GWei)
// AccessList: tx.AccessList(), // TODO revisit this value, the cost matter but performance might
SkipAccountChecks: true, // this would let us not set the nonce
}
Expand Down

0 comments on commit 9ca5a55

Please sign in to comment.