Skip to content
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

[Flow EVM] fix tx hash bug for direct calls (deposit, withdraw) #5454

Merged
merged 4 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading