Skip to content

Commit

Permalink
Merge branch 'master' into flow-evm-events-ccf-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
franklywatson authored Feb 17, 2024
2 parents 22f7c23 + 07e3dab commit e5e5bd1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
34 changes: 27 additions & 7 deletions fvm/evm/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@ import (
"strings"
"testing"

"github.com/onflow/cadence"

jsoncdc "github.com/onflow/cadence/encoding/json"

gethCommon "github.com/ethereum/go-ethereum/common"
gethCore "github.com/ethereum/go-ethereum/core"
gethTypes "github.com/ethereum/go-ethereum/core/types"
gethVM "github.com/ethereum/go-ethereum/core/vm"
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"
Expand Down Expand Up @@ -120,7 +117,30 @@ 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)

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)
})
})
Expand Down
3 changes: 2 additions & 1 deletion fvm/evm/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions fvm/evm/types/block_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit e5e5bd1

Please sign in to comment.