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 the issue with the hash calculation of the direct calls #6118

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 10 additions & 0 deletions fvm/evm/types/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func (dc *DirectCall) Encode() ([]byte, error) {
func (dc *DirectCall) Hash() (gethCommon.Hash, error) {
// we use geth transaction hash calculation since direct call hash is included in the
// block transaction hashes, and thus observed as any other transaction
// We construct this Legacy tx type so the external 3rd party tools
// don't have to support a new type for the purpose of hash computation
return dc.Transaction().Hash(), nil
}

Expand All @@ -98,13 +100,21 @@ func (dc *DirectCall) Message() *gethCore.Message {

// Transaction constructs a geth.Transaction from the direct call
func (dc *DirectCall) Transaction() *gethTypes.Transaction {
// Since a direct call doesn't have a valid siganture
// and we need to somehow include the From feild for the purpose
// of hash calculation. we define the canonical format by
// using the FROM bytes to set the bytes for the R part of the tx (big endian),
// S captures the subtype of transaction and V is set to DirectCallTxType (255).
return gethTypes.NewTx(&gethTypes.LegacyTx{
GasPrice: big.NewInt(0),
Gas: dc.GasLimit,
To: dc.to(),
Value: dc.Value,
Data: dc.Data,
Nonce: dc.Nonce,
R: new(big.Int).SetBytes(dc.From.Bytes()),
S: new(big.Int).SetBytes([]byte{dc.SubType}),
V: new(big.Int).SetBytes([]byte{DirectCallTxType}),
})
}

Expand Down
35 changes: 34 additions & 1 deletion fvm/evm/types/call_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package types

import (
"bytes"
"io"
"math/big"
"testing"

gethTypes "github.com/onflow/go-ethereum/core/types"
"github.com/onflow/go-ethereum/rlp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -22,7 +26,31 @@ func TestDirectCall(t *testing.T) {
t.Run("calculate hash", func(t *testing.T) {
h, err := dc.Hash()
require.NoError(t, err)
assert.Equal(t, "0xe28ff08eca95608646d765e3007b3710f7f2a8ac5e297431da1962c33487e7b6", h.Hex())
assert.Equal(t, "0xed76124cc3c59f13e1113f5c380e2a67dab9bf616afc645073d2491fe3aecb62", h.Hex())

// the hash should stay the same after RLP encoding and decoding
var b bytes.Buffer
writer := io.Writer(&b)
err = dc.Transaction().EncodeRLP(writer)
require.NoError(t, err)

reconstructedTx := &gethTypes.Transaction{}
err = reconstructedTx.DecodeRLP(rlp.NewStream(io.Reader(&b), 1000))
require.NoError(t, err)

h = reconstructedTx.Hash()
assert.Equal(t, "0xed76124cc3c59f13e1113f5c380e2a67dab9bf616afc645073d2491fe3aecb62", h.Hex())
})

t.Run("same content except `from` should result in different hashes", func(t *testing.T) {
h, err := dc.Hash()
require.NoError(t, err)

dc.From = Address{0x4, 0x5}
h2, err := dc.Hash()
require.NoError(t, err)

assert.NotEqual(t, h2.Hex(), h.Hex())
})

t.Run("construct transaction", func(t *testing.T) {
Expand All @@ -35,5 +63,10 @@ func TestDirectCall(t *testing.T) {
assert.Equal(t, dc.GasLimit, tx.Gas())
assert.Equal(t, dc.Data, tx.Data())
assert.Equal(t, uint64(0), tx.Nonce()) // no nonce exists for direct call

v, r, s := tx.RawSignatureValues()
require.Equal(t, dc.From.Bytes(), r.Bytes())
require.Equal(t, []byte{dc.SubType}, s.Bytes())
require.Equal(t, []byte{DirectCallTxType}, v.Bytes())
})
}
Loading