From f1381ad535b97f3d37652b534c77c6ea61c29276 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Tue, 7 Feb 2023 18:04:53 -0800 Subject: [PATCH] refactor: use EthHash for event topics This ensures they're always 32 bytes and padded, as required. --- chain/types/ethtypes/eth_types.go | 2 +- chain/types/ethtypes/eth_types_test.go | 2 +- itests/eth_filter_test.go | 344 +++++++++++++------------ itests/kit/solidity.go | 18 +- node/impl/full/eth.go | 14 +- 5 files changed, 200 insertions(+), 180 deletions(-) diff --git a/chain/types/ethtypes/eth_types.go b/chain/types/ethtypes/eth_types.go index ea083f6c1..6bc1639a0 100644 --- a/chain/types/ethtypes/eth_types.go +++ b/chain/types/ethtypes/eth_types.go @@ -626,7 +626,7 @@ type EthLog struct { Data EthBytes `json:"data"` // List of topics associated with the event log. - Topics []EthBytes `json:"topics"` + Topics []EthHash `json:"topics"` // Following fields are derived from the transaction containing the log diff --git a/chain/types/ethtypes/eth_types_test.go b/chain/types/ethtypes/eth_types_test.go index e42d2bdc3..488562cf0 100644 --- a/chain/types/ethtypes/eth_types_test.go +++ b/chain/types/ethtypes/eth_types_test.go @@ -216,7 +216,7 @@ func TestEthFilterResultMarshalJSON(t *testing.T) { TransactionHash: hash1, BlockHash: hash2, BlockNumber: 53, - Topics: []EthBytes{hash1[:]}, + Topics: []EthHash{hash1}, Data: EthBytes(hash1[:]), Address: addr, } diff --git a/itests/eth_filter_test.go b/itests/eth_filter_test.go index 59e0e6337..ed21c188a 100644 --- a/itests/eth_filter_test.go +++ b/itests/eth_filter_test.go @@ -285,31 +285,31 @@ func TestEthNewFilterDefaultSpec(t *testing.T) { expected := []ExpectedEthLog{ { Address: ethContractAddr, - Topics: []ethtypes.EthBytes{ - paddedEthBytes([]byte{0x11, 0x11}), - paddedEthBytes([]byte{0x22, 0x22}), - paddedEthBytes([]byte{0x33, 0x33}), - paddedEthBytes([]byte{0x44, 0x44}), + Topics: []ethtypes.EthHash{ + paddedEthHash([]byte{0x11, 0x11}), + paddedEthHash([]byte{0x22, 0x22}), + paddedEthHash([]byte{0x33, 0x33}), + paddedEthHash([]byte{0x44, 0x44}), }, Data: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}, }, { Address: ethContractAddr, - Topics: []ethtypes.EthBytes{ - paddedEthBytes([]byte{0x11, 0x11}), - paddedEthBytes([]byte{0x22, 0x22}), - paddedEthBytes([]byte{0x33, 0x33}), - paddedEthBytes([]byte{0x44, 0x44}), + Topics: []ethtypes.EthHash{ + paddedEthHash([]byte{0x11, 0x11}), + paddedEthHash([]byte{0x22, 0x22}), + paddedEthHash([]byte{0x33, 0x33}), + paddedEthHash([]byte{0x44, 0x44}), }, Data: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}, }, { Address: ethContractAddr, - Topics: []ethtypes.EthBytes{ - paddedEthBytes([]byte{0x11, 0x11}), - paddedEthBytes([]byte{0x22, 0x22}), - paddedEthBytes([]byte{0x33, 0x33}), - paddedEthBytes([]byte{0x44, 0x44}), + Topics: []ethtypes.EthHash{ + paddedEthHash([]byte{0x11, 0x11}), + paddedEthHash([]byte{0x22, 0x22}), + paddedEthHash([]byte{0x33, 0x33}), + paddedEthHash([]byte{0x44, 0x44}), }, Data: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}, }, @@ -344,11 +344,11 @@ func TestEthGetLogsBasic(t *testing.T) { expected := []ExpectedEthLog{ { Address: ethContractAddr, - Topics: []ethtypes.EthBytes{ - paddedEthBytes([]byte{0x11, 0x11}), - paddedEthBytes([]byte{0x22, 0x22}), - paddedEthBytes([]byte{0x33, 0x33}), - paddedEthBytes([]byte{0x44, 0x44}), + Topics: []ethtypes.EthHash{ + paddedEthHash([]byte{0x11, 0x11}), + paddedEthHash([]byte{0x22, 0x22}), + paddedEthHash([]byte{0x33, 0x33}), + paddedEthHash([]byte{0x44, 0x44}), }, Data: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}, }, @@ -409,11 +409,11 @@ func TestEthSubscribeLogsNoTopicSpec(t *testing.T) { for i := range expected { expected[i] = ExpectedEthLog{ Address: ethContractAddr, - Topics: []ethtypes.EthBytes{ - paddedEthBytes([]byte{0x11, 0x11}), - paddedEthBytes([]byte{0x22, 0x22}), - paddedEthBytes([]byte{0x33, 0x33}), - paddedEthBytes([]byte{0x44, 0x44}), + Topics: []ethtypes.EthHash{ + paddedEthHash([]byte{0x11, 0x11}), + paddedEthHash([]byte{0x22, 0x22}), + paddedEthHash([]byte{0x33, 0x33}), + paddedEthHash([]byte{0x44, 0x44}), }, Data: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}, } @@ -696,10 +696,10 @@ func TestEthGetLogsWithBlockRanges(t *testing.T) { distinctHeights[m.ts.Height()] = true expectedByHeight[m.ts.Height()] = append(expectedByHeight[m.ts.Height()], ExpectedEthLog{ Address: addr, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], - paddedUint64(args[0]), - paddedUint64(args[1]), + uint64EthHash(args[0]), + uint64EthHash(args[1]), }, Data: paddedUint64(args[2]), }) @@ -762,7 +762,7 @@ func TestEthGetLogsWithBlockRanges(t *testing.T) { require.True(len(partition3.expected) > 0, "partition should have events") // these are the topics we selected for partitioning earlier - topics := []ethtypes.EthHash{paddedEthHash(kit.EventMatrixContract.Ev["EventTwoIndexedWithData"])} + topics := []ethtypes.EthHash{kit.EventMatrixContract.Ev["EventTwoIndexedWithData"]} union := func(lists ...[]ExpectedEthLog) []ExpectedEthLog { ret := []ExpectedEthLog{} @@ -950,30 +950,30 @@ func TestEthNewFilterMergesHistoricWithRealtime(t *testing.T) { expected := []ExpectedEthLog{ { Address: ethContractAddr, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventOneData"], }, Data: paddedUint64(1), }, { Address: ethContractAddr, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventOneIndexed"], - paddedUint64(2), + uint64EthHash(2), }, }, { Address: ethContractAddr, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventOneData"], }, Data: paddedUint64(3), }, { Address: ethContractAddr, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventOneIndexed"], - paddedUint64(4), + uint64EthHash(4), }, }, } @@ -1364,19 +1364,19 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock return []filterTestCase{ { name: "find all EventZeroData events", - spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic1OneOf(paddedEthHash(kit.EventMatrixContract.Ev["EventZeroData"])).Filter(), + spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic1OneOf(kit.EventMatrixContract.Ev["EventZeroData"]).Filter(), expected: []ExpectedEthLog{ { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventZeroData"], }, Data: nil, }, { Address: contract2, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventZeroData"], }, Data: nil, @@ -1385,19 +1385,19 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock }, { name: "find all EventOneData events", - spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic1OneOf(paddedEthHash(kit.EventMatrixContract.Ev["EventOneData"])).Filter(), + spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic1OneOf(kit.EventMatrixContract.Ev["EventOneData"]).Filter(), expected: []ExpectedEthLog{ { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventOneData"], }, Data: packUint64Values(23), }, { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventOneData"], }, Data: packUint64Values(44), @@ -1406,12 +1406,12 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock }, { name: "find all EventTwoData events", - spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic1OneOf(paddedEthHash(kit.EventMatrixContract.Ev["EventTwoData"])).Filter(), + spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic1OneOf(kit.EventMatrixContract.Ev["EventTwoData"]).Filter(), expected: []ExpectedEthLog{ { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventTwoData"], }, Data: packUint64Values(555, 666), @@ -1420,12 +1420,12 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock }, { name: "find all EventThreeData events", - spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic1OneOf(paddedEthHash(kit.EventMatrixContract.Ev["EventThreeData"])).Filter(), + spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic1OneOf(kit.EventMatrixContract.Ev["EventThreeData"]).Filter(), expected: []ExpectedEthLog{ { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventThreeData"], }, Data: packUint64Values(1, 2, 3), @@ -1434,14 +1434,14 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock }, { name: "find all EventOneIndexed events", - spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic1OneOf(paddedEthHash(kit.EventMatrixContract.Ev["EventOneIndexed"])).Filter(), + spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic1OneOf(kit.EventMatrixContract.Ev["EventOneIndexed"]).Filter(), expected: []ExpectedEthLog{ { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventOneIndexed"], - paddedUint64(44), + uint64EthHash(44), }, Data: nil, }, @@ -1449,24 +1449,24 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock }, { name: "find all EventTwoIndexed events", - spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic1OneOf(paddedEthHash(kit.EventMatrixContract.Ev["EventTwoIndexed"])).Filter(), + spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic1OneOf(kit.EventMatrixContract.Ev["EventTwoIndexed"]).Filter(), expected: []ExpectedEthLog{ { Address: contract2, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventTwoIndexed"], - paddedUint64(44), - paddedUint64(19), + uint64EthHash(44), + uint64EthHash(19), }, Data: nil, }, { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventTwoIndexed"], - paddedUint64(40), - paddedUint64(20), + uint64EthHash(40), + uint64EthHash(20), }, Data: nil, }, @@ -1474,16 +1474,16 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock }, { name: "find all EventThreeIndexed events", - spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic1OneOf(paddedEthHash(kit.EventMatrixContract.Ev["EventThreeIndexed"])).Filter(), + spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic1OneOf(kit.EventMatrixContract.Ev["EventThreeIndexed"]).Filter(), expected: []ExpectedEthLog{ { Address: contract2, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventThreeIndexed"], - paddedUint64(44), - paddedUint64(27), - paddedUint64(19), + uint64EthHash(44), + uint64EthHash(27), + uint64EthHash(19), }, Data: nil, }, @@ -1491,30 +1491,30 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock }, { name: "find all EventOneIndexedWithData events", - spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic1OneOf(paddedEthHash(kit.EventMatrixContract.Ev["EventOneIndexedWithData"])).Filter(), + spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic1OneOf(kit.EventMatrixContract.Ev["EventOneIndexedWithData"]).Filter(), expected: []ExpectedEthLog{ { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventOneIndexedWithData"], - paddedUint64(44), + uint64EthHash(44), }, Data: paddedUint64(19), }, { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventOneIndexedWithData"], - paddedUint64(46), + uint64EthHash(46), }, Data: paddedUint64(12), }, { Address: contract2, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventOneIndexedWithData"], - paddedUint64(50), + uint64EthHash(50), }, Data: paddedUint64(9), }, @@ -1522,33 +1522,33 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock }, { name: "find all EventTwoIndexedWithData events", - spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic1OneOf(paddedEthHash(kit.EventMatrixContract.Ev["EventTwoIndexedWithData"])).Filter(), + spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic1OneOf(kit.EventMatrixContract.Ev["EventTwoIndexedWithData"]).Filter(), expected: []ExpectedEthLog{ { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], - paddedUint64(44), - paddedUint64(27), + uint64EthHash(44), + uint64EthHash(27), }, Data: paddedUint64(19), }, { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], - paddedUint64(46), - paddedUint64(27), + uint64EthHash(46), + uint64EthHash(27), }, Data: paddedUint64(19), }, { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], - paddedUint64(46), - paddedUint64(14), + uint64EthHash(46), + uint64EthHash(14), }, Data: paddedUint64(19), }, @@ -1556,16 +1556,16 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock }, { name: "find all EventThreeIndexedWithData events", - spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic1OneOf(paddedEthHash(kit.EventMatrixContract.Ev["EventThreeIndexedWithData"])).Filter(), + spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic1OneOf(kit.EventMatrixContract.Ev["EventThreeIndexedWithData"]).Filter(), expected: []ExpectedEthLog{ { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventThreeIndexedWithData"], - paddedUint64(44), - paddedUint64(27), - paddedUint64(19), + uint64EthHash(44), + uint64EthHash(27), + uint64EthHash(19), }, Data: paddedUint64(12), }, @@ -1574,60 +1574,60 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock { name: "find all events with topic2 of 44", - spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic2OneOf(paddedEthHash(paddedUint64(44))).Filter(), + spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic2OneOf(uint64EthHash(44)).Filter(), expected: []ExpectedEthLog{ { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventOneIndexed"], - paddedUint64(44), + uint64EthHash(44), }, Data: nil, }, { Address: contract2, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventTwoIndexed"], - paddedUint64(44), - paddedUint64(19), + uint64EthHash(44), + uint64EthHash(19), }, Data: nil, }, { Address: contract2, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventThreeIndexed"], - paddedUint64(44), - paddedUint64(27), - paddedUint64(19), + uint64EthHash(44), + uint64EthHash(27), + uint64EthHash(19), }, Data: nil, }, { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventOneIndexedWithData"], - paddedUint64(44), + uint64EthHash(44), }, Data: paddedUint64(19), }, { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], - paddedUint64(44), - paddedUint64(27), + uint64EthHash(44), + uint64EthHash(27), }, Data: paddedUint64(19), }, { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventThreeIndexedWithData"], - paddedUint64(44), - paddedUint64(27), - paddedUint64(19), + uint64EthHash(44), + uint64EthHash(27), + uint64EthHash(19), }, Data: paddedUint64(12), }, @@ -1635,32 +1635,32 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock }, { name: "find all events with topic2 of 46", - spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic2OneOf(paddedEthHash(paddedUint64(46))).Filter(), + spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic2OneOf(uint64EthHash(46)).Filter(), expected: []ExpectedEthLog{ { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventOneIndexedWithData"], - paddedUint64(46), + uint64EthHash(46), }, Data: paddedUint64(12), }, { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], - paddedUint64(46), - paddedUint64(27), + uint64EthHash(46), + uint64EthHash(27), }, Data: paddedUint64(19), }, { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], - paddedUint64(46), - paddedUint64(14), + uint64EthHash(46), + uint64EthHash(14), }, Data: paddedUint64(19), }, @@ -1668,14 +1668,14 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock }, { name: "find all events with topic2 of 50", - spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic2OneOf(paddedEthHash(paddedUint64(50))).Filter(), + spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic2OneOf(uint64EthHash(50)).Filter(), expected: []ExpectedEthLog{ { Address: contract2, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventOneIndexedWithData"], - paddedUint64(50), + uint64EthHash(50), }, Data: paddedUint64(9), }, @@ -1683,40 +1683,40 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock }, { name: "find all events with topic2 of 46 or 50", - spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic2OneOf(paddedEthHash(paddedUint64(46)), paddedEthHash(paddedUint64(50))).Filter(), + spec: kit.NewEthFilterBuilder().FromBlock(fromBlock).Topic2OneOf(uint64EthHash(46), uint64EthHash(50)).Filter(), expected: []ExpectedEthLog{ { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventOneIndexedWithData"], - paddedUint64(46), + uint64EthHash(46), }, Data: paddedUint64(12), }, { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], - paddedUint64(46), - paddedUint64(27), + uint64EthHash(46), + uint64EthHash(27), }, Data: paddedUint64(19), }, { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], - paddedUint64(46), - paddedUint64(14), + uint64EthHash(46), + uint64EthHash(14), }, Data: paddedUint64(19), }, { Address: contract2, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventOneIndexedWithData"], - paddedUint64(50), + uint64EthHash(50), }, Data: paddedUint64(9), }, @@ -1727,26 +1727,26 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock name: "find all events with topic1 of EventTwoIndexedWithData and topic3 of 27", spec: kit.NewEthFilterBuilder(). FromBlockEpoch(0). - Topic1OneOf(paddedEthHash(kit.EventMatrixContract.Ev["EventTwoIndexedWithData"])). - Topic3OneOf(paddedEthHash(paddedUint64(27))). + Topic1OneOf(kit.EventMatrixContract.Ev["EventTwoIndexedWithData"]). + Topic3OneOf(uint64EthHash(27)). Filter(), expected: []ExpectedEthLog{ { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], - paddedUint64(44), - paddedUint64(27), + uint64EthHash(44), + uint64EthHash(27), }, Data: paddedUint64(19), }, { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], - paddedUint64(46), - paddedUint64(27), + uint64EthHash(46), + uint64EthHash(27), }, Data: paddedUint64(19), }, @@ -1757,25 +1757,25 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock name: "find all events with topic1 of EventTwoIndexedWithData or EventOneIndexed and topic2 of 44", spec: kit.NewEthFilterBuilder(). FromBlockEpoch(0). - Topic1OneOf(paddedEthHash(kit.EventMatrixContract.Ev["EventTwoIndexedWithData"]), paddedEthHash(kit.EventMatrixContract.Ev["EventOneIndexed"])). - Topic2OneOf(paddedEthHash(paddedUint64(44))). + Topic1OneOf((kit.EventMatrixContract.Ev["EventTwoIndexedWithData"]), kit.EventMatrixContract.Ev["EventOneIndexed"]). + Topic2OneOf(uint64EthHash(44)). Filter(), expected: []ExpectedEthLog{ { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], - paddedUint64(44), - paddedUint64(27), + uint64EthHash(44), + uint64EthHash(27), }, Data: paddedUint64(19), }, { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventOneIndexed"], - paddedUint64(44), + uint64EthHash(44), }, Data: nil, }, @@ -1794,35 +1794,35 @@ func getAddressFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlo expected: []ExpectedEthLog{ { Address: contract2, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventZeroData"], }, Data: nil, }, { Address: contract2, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventThreeIndexed"], - paddedUint64(44), - paddedUint64(27), - paddedUint64(19), + uint64EthHash(44), + uint64EthHash(27), + uint64EthHash(19), }, Data: nil, }, { Address: contract2, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventTwoIndexed"], - paddedUint64(44), - paddedUint64(19), + uint64EthHash(44), + uint64EthHash(19), }, Data: nil, }, { Address: contract2, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventOneIndexedWithData"], - paddedUint64(50), + uint64EthHash(50), }, Data: paddedUint64(9), }, @@ -1836,20 +1836,20 @@ func getAddressFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlo expected: []ExpectedEthLog{ { Address: contract2, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventThreeIndexed"], - paddedUint64(44), - paddedUint64(27), - paddedUint64(19), + uint64EthHash(44), + uint64EthHash(27), + uint64EthHash(19), }, Data: nil, }, { Address: contract2, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventTwoIndexed"], - paddedUint64(44), - paddedUint64(19), + uint64EthHash(44), + uint64EthHash(19), }, Data: nil, }, @@ -1861,31 +1861,31 @@ func getAddressFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlo spec: kit.NewEthFilterBuilder(). FromBlockEpoch(0). AddressOneOf(contract1, contract2). - Topic1OneOf(paddedEthHash(kit.EventMatrixContract.Ev["EventOneIndexedWithData"])). + Topic1OneOf(kit.EventMatrixContract.Ev["EventOneIndexedWithData"]). Filter(), expected: []ExpectedEthLog{ { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventOneIndexedWithData"], - paddedUint64(44), + uint64EthHash(44), }, Data: paddedUint64(19), }, { Address: contract1, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventOneIndexedWithData"], - paddedUint64(46), + uint64EthHash(46), }, Data: paddedUint64(12), }, { Address: contract2, - Topics: []ethtypes.EthBytes{ + Topics: []ethtypes.EthHash{ kit.EventMatrixContract.Ev["EventOneIndexedWithData"], - paddedUint64(50), + uint64EthHash(50), }, Data: paddedUint64(9), }, @@ -1906,7 +1906,7 @@ type ExpectedEthLog struct { Address ethtypes.EthAddress `json:"address"` // List of topics associated with the event log. - Topics []ethtypes.EthBytes `json:"topics"` + Topics []ethtypes.EthHash `json:"topics"` // Data is the value of the event log, excluding topics Data ethtypes.EthBytes `json:"data"` @@ -1918,10 +1918,10 @@ func AssertEthLogs(t *testing.T, actual []*ethtypes.EthLog, expected []ExpectedE t.Logf("got %d ethlogs, wanted %d", len(actual), len(expected)) - formatTopics := func(topics []ethtypes.EthBytes) string { + formatTopics := func(topics []ethtypes.EthHash) string { ss := make([]string, len(topics)) for i := range topics { - ss[i] = fmt.Sprintf("%d:%x", i, topics[i]) + ss[i] = fmt.Sprintf("%d:%s", i, topics[i]) } return strings.Join(ss, ",") } @@ -1958,7 +1958,7 @@ func AssertEthLogs(t *testing.T, actual []*ethtypes.EthLog, expected []ExpectedE } for j := range elog.Topics { - if !bytes.Equal(elog.Topics[j], want.Topics[j]) { + if elog.Topics[j] != want.Topics[j] { continue LoopExpected } } @@ -2132,7 +2132,7 @@ func ParseEthLog(in map[string]interface{}) (*ethtypes.EthLog, error) { if err != nil { return nil, xerrors.Errorf("%s: %w", k, err) } - el.Topics = append(el.Topics, topic) + el.Topics = append(el.Topics, paddedEthHash(topic)) continue } @@ -2145,7 +2145,7 @@ func ParseEthLog(in map[string]interface{}) (*ethtypes.EthLog, error) { if err != nil { return nil, xerrors.Errorf("%s: %w", k, err) } - el.Topics = append(el.Topics, topic) + el.Topics = append(el.Topics, paddedEthHash(topic)) } } } @@ -2169,6 +2169,12 @@ func paddedUint64(v uint64) ethtypes.EthBytes { return buf } +func uint64EthHash(v uint64) ethtypes.EthHash { + var buf ethtypes.EthHash + binary.BigEndian.PutUint64(buf[24:], v) + return buf +} + func paddedEthHash(orig []byte) ethtypes.EthHash { if len(orig) > 32 { panic("exceeds EthHash length") diff --git a/itests/kit/solidity.go b/itests/kit/solidity.go index ea9d452f4..6999bd91b 100644 --- a/itests/kit/solidity.go +++ b/itests/kit/solidity.go @@ -2,12 +2,16 @@ package kit import ( "golang.org/x/crypto/sha3" + + "github.com/filecoin-project/lotus/chain/types/ethtypes" ) -func EthTopicHash(sig string) []byte { +func EthTopicHash(sig string) ethtypes.EthHash { hasher := sha3.NewLegacyKeccak256() hasher.Write([]byte(sig)) - return hasher.Sum(nil) + var hash ethtypes.EthHash + copy(hash[:], hasher.Sum(nil)) + return hash } func EthFunctionHash(sig string) []byte { @@ -18,9 +22,9 @@ func EthFunctionHash(sig string) []byte { // SolidityContractDef holds information about one of the test contracts type SolidityContractDef struct { - Filename string // filename of the hex of the contract, e.g. contracts/EventMatrix.hex - Fn map[string][]byte // mapping of function names to 32-bit selector - Ev map[string][]byte // mapping of event names to 256-bit signature hashes + Filename string // filename of the hex of the contract, e.g. contracts/EventMatrix.hex + Fn map[string][]byte // mapping of function names to 32-bit selector + Ev map[string]ethtypes.EthHash // mapping of event names to 256-bit signature hashes } var EventMatrixContract = SolidityContractDef{ @@ -38,7 +42,7 @@ var EventMatrixContract = SolidityContractDef{ "logEventTwoIndexedWithData": EthFunctionHash("logEventTwoIndexedWithData(uint256,uint256,uint256)"), "logEventThreeIndexedWithData": EthFunctionHash("logEventThreeIndexedWithData(uint256,uint256,uint256,uint256)"), }, - Ev: map[string][]byte{ + Ev: map[string]ethtypes.EthHash{ "EventZeroData": EthTopicHash("EventZeroData()"), "EventOneData": EthTopicHash("EventOneData(uint256)"), "EventTwoData": EthTopicHash("EventTwoData(uint256,uint256)"), @@ -60,5 +64,5 @@ var EventsContract = SolidityContractDef{ "log_zero_nodata": {0x00, 0x00, 0x00, 0x01}, "log_four_data": {0x00, 0x00, 0x00, 0x02}, }, - Ev: map[string][]byte{}, + Ev: map[string]ethtypes.EthHash{}, } diff --git a/node/impl/full/eth.go b/node/impl/full/eth.go index bfbd6dbda..59147323a 100644 --- a/node/impl/full/eth.go +++ b/node/impl/full/eth.go @@ -1376,7 +1376,12 @@ func ethFilterResultFromEvents(evs []*filter.CollectedEvent, sa StateAPI) (*etht continue } if entry.Key == ethtypes.EthTopic1 || entry.Key == ethtypes.EthTopic2 || entry.Key == ethtypes.EthTopic3 || entry.Key == ethtypes.EthTopic4 { - log.Topics = append(log.Topics, entry.Value) + if len(entry.Value) != 32 { + continue + } + var value ethtypes.EthHash + copy(value[:], entry.Value) + log.Topics = append(log.Topics, value) } else { log.Data = entry.Value } @@ -1913,8 +1918,13 @@ func newEthTxReceipt(ctx context.Context, tx ethtypes.EthTx, lookup *api.MsgLook continue } if entry.Key == ethtypes.EthTopic1 || entry.Key == ethtypes.EthTopic2 || entry.Key == ethtypes.EthTopic3 || entry.Key == ethtypes.EthTopic4 { + if len(entry.Value) != 32 { + continue + } + var value ethtypes.EthHash + copy(value[:], entry.Value) + l.Topics = append(l.Topics, value) ethtypes.EthBloomSet(receipt.LogsBloom, entry.Value) - l.Topics = append(l.Topics, entry.Value) } else { l.Data = entry.Value }