Skip to content

Commit

Permalink
Merge pull request #5454 from onflow/ramtin/flow-evm-fix-tx-hash-issue
Browse files Browse the repository at this point in the history
[Flow EVM] fix tx hash bug for direct calls (deposit, withdraw)
  • Loading branch information
ramtinms authored Feb 26, 2024
2 parents f64ed9e + 05e4140 commit c4831ee
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
22 changes: 17 additions & 5 deletions fvm/evm/emulator/emulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ func (bl *BlockView) DirectCall(call *types.DirectCall) (*types.Result, error) {
}
switch call.SubType {
case types.DepositCallSubType:
return proc.mintTo(call.To, call.Value)
return proc.mintTo(call.To, call.Value, txHash)
case types.WithdrawCallSubType:
return proc.withdrawFrom(call.From, call.Value)
return proc.withdrawFrom(call.From, call.Value, txHash)
case types.DeployCallSubType:
if !call.EmptyToField() {
return proc.deployAt(call.From, call.To, call.Data, call.GasLimit, call.Value)
return proc.deployAt(call.From, call.To, call.Data, call.GasLimit, call.Value, txHash)
}
fallthrough
default:
Expand Down Expand Up @@ -199,11 +199,16 @@ func (proc *procedure) commit() error {
return nil
}

func (proc *procedure) mintTo(address types.Address, amount *big.Int) (*types.Result, error) {
func (proc *procedure) mintTo(
address types.Address,
amount *big.Int,
txHash gethCommon.Hash,
) (*types.Result, error) {
addr := address.ToCommon()
res := &types.Result{
GasConsumed: proc.config.DirectCallBaseGasUsage,
TxType: types.DirectCallTxType,
TxHash: txHash,
}

// create account if not exist
Expand All @@ -218,12 +223,17 @@ func (proc *procedure) mintTo(address types.Address, amount *big.Int) (*types.Re
return res, proc.commit()
}

func (proc *procedure) withdrawFrom(address types.Address, amount *big.Int) (*types.Result, error) {
func (proc *procedure) withdrawFrom(
address types.Address,
amount *big.Int,
txHash gethCommon.Hash,
) (*types.Result, error) {

addr := address.ToCommon()
res := &types.Result{
GasConsumed: proc.config.DirectCallBaseGasUsage,
TxType: types.DirectCallTxType,
TxHash: txHash,
}

// check if account exists
Expand Down Expand Up @@ -264,13 +274,15 @@ func (proc *procedure) deployAt(
data types.Code,
gasLimit uint64,
value *big.Int,
txHash gethCommon.Hash,
) (*types.Result, error) {
if value.Sign() < 0 {
return nil, types.ErrInvalidBalance
}

res := &types.Result{
TxType: types.DirectCallTxType,
TxHash: txHash,
}
addr := to.ToCommon()

Expand Down
30 changes: 20 additions & 10 deletions fvm/evm/emulator/emulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ func TestNativeTokenBridging(t *testing.T) {
t.Run("mint tokens to the first account", func(t *testing.T) {
RunWithNewEmulator(t, backend, rootAddr, func(env *emulator.Emulator) {
RunWithNewBlockView(t, env, func(blk types.BlockView) {
res, err := blk.DirectCall(types.NewDepositCall(testAccount, originalBalance, nonce))
call := types.NewDepositCall(testAccount, originalBalance, nonce)
res, err := blk.DirectCall(call)
require.NoError(t, err)
require.Equal(t, defaultCtx.DirectCallBaseGasUsage, res.GasConsumed)
expectedHash, err := call.Hash()
require.NoError(t, err)
require.Equal(t, expectedHash, res.TxHash)
nonce += 1
})
})
Expand All @@ -66,9 +70,13 @@ func TestNativeTokenBridging(t *testing.T) {
})
RunWithNewEmulator(t, backend, rootAddr, func(env *emulator.Emulator) {
RunWithNewBlockView(t, env, func(blk types.BlockView) {
res, err := blk.DirectCall(types.NewWithdrawCall(testAccount, amount, nonce))
call := types.NewWithdrawCall(testAccount, amount, nonce)
res, err := blk.DirectCall(call)
require.NoError(t, err)
require.Equal(t, defaultCtx.DirectCallBaseGasUsage, res.GasConsumed)
expectedHash, err := call.Hash()
require.NoError(t, err)
require.Equal(t, expectedHash, res.TxHash)
nonce += 1
})
})
Expand Down Expand Up @@ -111,16 +119,18 @@ func TestContractInteraction(t *testing.T) {
t.Run("deploy contract", func(t *testing.T) {
RunWithNewEmulator(t, backend, rootAddr, func(env *emulator.Emulator) {
RunWithNewBlockView(t, env, func(blk types.BlockView) {
res, err := blk.DirectCall(
types.NewDeployCall(
testAccount,
testContract.ByteCode,
math.MaxUint64,
amountToBeTransfered,
nonce),
)
call := types.NewDeployCall(
testAccount,
testContract.ByteCode,
math.MaxUint64,
amountToBeTransfered,
nonce)
res, err := blk.DirectCall(call)
require.NoError(t, err)
contractAddr = res.DeployedContractAddress
expectedHash, err := call.Hash()
require.NoError(t, err)
require.Equal(t, expectedHash, res.TxHash)
nonce += 1
})
RunWithNewReadOnlyBlockView(t, env, func(blk types.ReadOnlyBlockView) {
Expand Down
10 changes: 10 additions & 0 deletions fvm/evm/types/call.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"fmt"
"math/big"

gethCommon "github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -51,6 +52,15 @@ type DirectCall struct {
Nonce uint64
}

// DirectCallFromEncoded constructs a DirectCall from encoded data
func DirectCallFromEncoded(encoded []byte) (*DirectCall, error) {
if encoded[0] != DirectCallTxType {
return nil, fmt.Errorf("tx type mismatch")
}
dc := &DirectCall{}
return dc, rlp.DecodeBytes(encoded[1:], dc)
}

// Encode encodes the direct call it also adds the type
// as the very first byte, similar to how evm encodes types.
func (dc *DirectCall) Encode() ([]byte, error) {
Expand Down

0 comments on commit c4831ee

Please sign in to comment.