From aa3b30661318546edf12f96b02507d26cdf074b1 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Fri, 16 Feb 2024 16:20:56 +0100 Subject: [PATCH 1/4] add block hash change test --- fvm/evm/types/block_test.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 fvm/evm/types/block_test.go diff --git a/fvm/evm/types/block_test.go b/fvm/evm/types/block_test.go new file mode 100644 index 00000000000..848660a6513 --- /dev/null +++ b/fvm/evm/types/block_test.go @@ -0,0 +1,33 @@ +package types + +import ( + "math/big" + "testing" + + gethCommon "github.com/ethereum/go-ethereum/common" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_BlockHash(t *testing.T) { + b := Block{ + ParentBlockHash: gethCommon.HexToHash("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), + Height: 1, + TotalSupply: big.NewInt(1000), + ReceiptRoot: gethCommon.Hash{0x2, 0x3, 0x4}, + TransactionHashes: []gethCommon.Hash{ + gethCommon.HexToHash("0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"), + }, + } + + h1, err := b.Hash() + require.NoError(t, err) + + b.Height = 2 + + h2, err := b.Hash() + require.NoError(t, err) + + // hashes should not equal if any data is changed + assert.NotEqual(t, h1, h2) +} From 27693bef1dfc4d10516e40535cad090587feef28 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Fri, 16 Feb 2024 16:21:41 +0100 Subject: [PATCH 2/4] check tx hash equals block tx hash --- fvm/evm/handler/handler_test.go | 38 +++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/fvm/evm/handler/handler_test.go b/fvm/evm/handler/handler_test.go index 38647f832b8..8b82b07f8e0 100644 --- a/fvm/evm/handler/handler_test.go +++ b/fvm/evm/handler/handler_test.go @@ -8,19 +8,16 @@ import ( "strings" "testing" - "github.com/onflow/cadence" - - jsoncdc "github.com/onflow/cadence/encoding/json" - gethCommon "github.com/ethereum/go-ethereum/common" gethTypes "github.com/ethereum/go-ethereum/core/types" gethParams "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" + "github.com/onflow/cadence" + jsoncdc "github.com/onflow/cadence/encoding/json" + "github.com/onflow/cadence/runtime/common" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/onflow/cadence/runtime/common" - "github.com/onflow/flow-go/fvm/errors" "github.com/onflow/flow-go/fvm/evm/emulator" "github.com/onflow/flow-go/fvm/evm/handler" @@ -120,7 +117,34 @@ func TestHandler_TransactionRun(t *testing.T) { // check block event event = events[1] assert.Equal(t, event.Type, types.EventTypeBlockExecuted) - _, err = jsoncdc.Decode(nil, event.Payload) + ev, err = jsoncdc.Decode(nil, event.Payload) + require.NoError(t, err) + + // make sure block transaction list references the above transaction id + cadenceEvent, ok = ev.(cadence.Event) + require.True(t, ok) + + // calculate tx id to match it + var evmTx gethTypes.Transaction + err = evmTx.UnmarshalBinary(tx) + require.NoError(t, err) + + fmt.Println(cadenceEvent.String()) + js, _ := evmTx.MarshalJSON() + fmt.Println(evmTx.Hash(), string(js)) + + for j, f := range cadenceEvent.GetFields() { + if f.Identifier == "transactionHashes" { + txsRaw := cadenceEvent.GetFieldValues()[j] + txs, ok := txsRaw.(cadence.Array) + require.True(t, ok) + // we know there's only one tx for now in block + eventTxID := txs.Values[0].ToGoValue().(string) + // make sure the transaction id included in the block transaction list is the same as tx sumbmitted + assert.Equal(t, evmTx.Hash().String(), eventTxID) + } + } + require.NoError(t, err) }) }) From feacf4a5c855304b2d819b9fa1dd4b21ab52dbcb Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Fri, 16 Feb 2024 16:25:14 +0100 Subject: [PATCH 3/4] remove unneeded log --- fvm/evm/handler/handler_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/fvm/evm/handler/handler_test.go b/fvm/evm/handler/handler_test.go index 8b82b07f8e0..eed3736939c 100644 --- a/fvm/evm/handler/handler_test.go +++ b/fvm/evm/handler/handler_test.go @@ -129,10 +129,6 @@ func TestHandler_TransactionRun(t *testing.T) { err = evmTx.UnmarshalBinary(tx) require.NoError(t, err) - fmt.Println(cadenceEvent.String()) - js, _ := evmTx.MarshalJSON() - fmt.Println(evmTx.Hash(), string(js)) - for j, f := range cadenceEvent.GetFields() { if f.Identifier == "transactionHashes" { txsRaw := cadenceEvent.GetFieldValues()[j] From 3f83ea3b4fc3234acdbf71684da50ef4922926c8 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Fri, 16 Feb 2024 16:25:25 +0100 Subject: [PATCH 4/4] fix block hash calculation --- fvm/evm/types/block.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fvm/evm/types/block.go b/fvm/evm/types/block.go index 71d78ce5999..d88d5f5903d 100644 --- a/fvm/evm/types/block.go +++ b/fvm/evm/types/block.go @@ -5,6 +5,7 @@ import ( gethCommon "github.com/ethereum/go-ethereum/common" gethTypes "github.com/ethereum/go-ethereum/core/types" + gethCrypto "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/rlp" ) @@ -35,7 +36,7 @@ func (b *Block) ToBytes() ([]byte, error) { // Hash returns the hash of the block func (b *Block) Hash() (gethCommon.Hash, error) { data, err := b.ToBytes() - return gethCommon.BytesToHash(data), err + return gethCrypto.Keccak256Hash(data), err } // AppendTxHash appends a transaction hash to the list of transaction hashes of the block