refactor: use EthHash for event topics

This ensures they're always 32 bytes and padded, as required.
This commit is contained in:
Steven Allen 2023-02-07 18:04:53 -08:00
parent 1588dd3dbe
commit f1381ad535
5 changed files with 200 additions and 180 deletions

View File

@ -626,7 +626,7 @@ type EthLog struct {
Data EthBytes `json:"data"` Data EthBytes `json:"data"`
// List of topics associated with the event log. // 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 // Following fields are derived from the transaction containing the log

View File

@ -216,7 +216,7 @@ func TestEthFilterResultMarshalJSON(t *testing.T) {
TransactionHash: hash1, TransactionHash: hash1,
BlockHash: hash2, BlockHash: hash2,
BlockNumber: 53, BlockNumber: 53,
Topics: []EthBytes{hash1[:]}, Topics: []EthHash{hash1},
Data: EthBytes(hash1[:]), Data: EthBytes(hash1[:]),
Address: addr, Address: addr,
} }

View File

@ -285,31 +285,31 @@ func TestEthNewFilterDefaultSpec(t *testing.T) {
expected := []ExpectedEthLog{ expected := []ExpectedEthLog{
{ {
Address: ethContractAddr, Address: ethContractAddr,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
paddedEthBytes([]byte{0x11, 0x11}), paddedEthHash([]byte{0x11, 0x11}),
paddedEthBytes([]byte{0x22, 0x22}), paddedEthHash([]byte{0x22, 0x22}),
paddedEthBytes([]byte{0x33, 0x33}), paddedEthHash([]byte{0x33, 0x33}),
paddedEthBytes([]byte{0x44, 0x44}), paddedEthHash([]byte{0x44, 0x44}),
}, },
Data: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}, Data: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88},
}, },
{ {
Address: ethContractAddr, Address: ethContractAddr,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
paddedEthBytes([]byte{0x11, 0x11}), paddedEthHash([]byte{0x11, 0x11}),
paddedEthBytes([]byte{0x22, 0x22}), paddedEthHash([]byte{0x22, 0x22}),
paddedEthBytes([]byte{0x33, 0x33}), paddedEthHash([]byte{0x33, 0x33}),
paddedEthBytes([]byte{0x44, 0x44}), paddedEthHash([]byte{0x44, 0x44}),
}, },
Data: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}, Data: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88},
}, },
{ {
Address: ethContractAddr, Address: ethContractAddr,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
paddedEthBytes([]byte{0x11, 0x11}), paddedEthHash([]byte{0x11, 0x11}),
paddedEthBytes([]byte{0x22, 0x22}), paddedEthHash([]byte{0x22, 0x22}),
paddedEthBytes([]byte{0x33, 0x33}), paddedEthHash([]byte{0x33, 0x33}),
paddedEthBytes([]byte{0x44, 0x44}), paddedEthHash([]byte{0x44, 0x44}),
}, },
Data: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}, Data: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88},
}, },
@ -344,11 +344,11 @@ func TestEthGetLogsBasic(t *testing.T) {
expected := []ExpectedEthLog{ expected := []ExpectedEthLog{
{ {
Address: ethContractAddr, Address: ethContractAddr,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
paddedEthBytes([]byte{0x11, 0x11}), paddedEthHash([]byte{0x11, 0x11}),
paddedEthBytes([]byte{0x22, 0x22}), paddedEthHash([]byte{0x22, 0x22}),
paddedEthBytes([]byte{0x33, 0x33}), paddedEthHash([]byte{0x33, 0x33}),
paddedEthBytes([]byte{0x44, 0x44}), paddedEthHash([]byte{0x44, 0x44}),
}, },
Data: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}, Data: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88},
}, },
@ -409,11 +409,11 @@ func TestEthSubscribeLogsNoTopicSpec(t *testing.T) {
for i := range expected { for i := range expected {
expected[i] = ExpectedEthLog{ expected[i] = ExpectedEthLog{
Address: ethContractAddr, Address: ethContractAddr,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
paddedEthBytes([]byte{0x11, 0x11}), paddedEthHash([]byte{0x11, 0x11}),
paddedEthBytes([]byte{0x22, 0x22}), paddedEthHash([]byte{0x22, 0x22}),
paddedEthBytes([]byte{0x33, 0x33}), paddedEthHash([]byte{0x33, 0x33}),
paddedEthBytes([]byte{0x44, 0x44}), paddedEthHash([]byte{0x44, 0x44}),
}, },
Data: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}, Data: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88},
} }
@ -696,10 +696,10 @@ func TestEthGetLogsWithBlockRanges(t *testing.T) {
distinctHeights[m.ts.Height()] = true distinctHeights[m.ts.Height()] = true
expectedByHeight[m.ts.Height()] = append(expectedByHeight[m.ts.Height()], ExpectedEthLog{ expectedByHeight[m.ts.Height()] = append(expectedByHeight[m.ts.Height()], ExpectedEthLog{
Address: addr, Address: addr,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], kit.EventMatrixContract.Ev["EventTwoIndexedWithData"],
paddedUint64(args[0]), uint64EthHash(args[0]),
paddedUint64(args[1]), uint64EthHash(args[1]),
}, },
Data: paddedUint64(args[2]), Data: paddedUint64(args[2]),
}) })
@ -762,7 +762,7 @@ func TestEthGetLogsWithBlockRanges(t *testing.T) {
require.True(len(partition3.expected) > 0, "partition should have events") require.True(len(partition3.expected) > 0, "partition should have events")
// these are the topics we selected for partitioning earlier // 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 { union := func(lists ...[]ExpectedEthLog) []ExpectedEthLog {
ret := []ExpectedEthLog{} ret := []ExpectedEthLog{}
@ -950,30 +950,30 @@ func TestEthNewFilterMergesHistoricWithRealtime(t *testing.T) {
expected := []ExpectedEthLog{ expected := []ExpectedEthLog{
{ {
Address: ethContractAddr, Address: ethContractAddr,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventOneData"], kit.EventMatrixContract.Ev["EventOneData"],
}, },
Data: paddedUint64(1), Data: paddedUint64(1),
}, },
{ {
Address: ethContractAddr, Address: ethContractAddr,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventOneIndexed"], kit.EventMatrixContract.Ev["EventOneIndexed"],
paddedUint64(2), uint64EthHash(2),
}, },
}, },
{ {
Address: ethContractAddr, Address: ethContractAddr,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventOneData"], kit.EventMatrixContract.Ev["EventOneData"],
}, },
Data: paddedUint64(3), Data: paddedUint64(3),
}, },
{ {
Address: ethContractAddr, Address: ethContractAddr,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventOneIndexed"], kit.EventMatrixContract.Ev["EventOneIndexed"],
paddedUint64(4), uint64EthHash(4),
}, },
}, },
} }
@ -1364,19 +1364,19 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock
return []filterTestCase{ return []filterTestCase{
{ {
name: "find all EventZeroData events", 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{ expected: []ExpectedEthLog{
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventZeroData"], kit.EventMatrixContract.Ev["EventZeroData"],
}, },
Data: nil, Data: nil,
}, },
{ {
Address: contract2, Address: contract2,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventZeroData"], kit.EventMatrixContract.Ev["EventZeroData"],
}, },
Data: nil, Data: nil,
@ -1385,19 +1385,19 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock
}, },
{ {
name: "find all EventOneData events", 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{ expected: []ExpectedEthLog{
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventOneData"], kit.EventMatrixContract.Ev["EventOneData"],
}, },
Data: packUint64Values(23), Data: packUint64Values(23),
}, },
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventOneData"], kit.EventMatrixContract.Ev["EventOneData"],
}, },
Data: packUint64Values(44), Data: packUint64Values(44),
@ -1406,12 +1406,12 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock
}, },
{ {
name: "find all EventTwoData events", 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{ expected: []ExpectedEthLog{
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventTwoData"], kit.EventMatrixContract.Ev["EventTwoData"],
}, },
Data: packUint64Values(555, 666), Data: packUint64Values(555, 666),
@ -1420,12 +1420,12 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock
}, },
{ {
name: "find all EventThreeData events", 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{ expected: []ExpectedEthLog{
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventThreeData"], kit.EventMatrixContract.Ev["EventThreeData"],
}, },
Data: packUint64Values(1, 2, 3), Data: packUint64Values(1, 2, 3),
@ -1434,14 +1434,14 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock
}, },
{ {
name: "find all EventOneIndexed events", 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{ expected: []ExpectedEthLog{
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventOneIndexed"], kit.EventMatrixContract.Ev["EventOneIndexed"],
paddedUint64(44), uint64EthHash(44),
}, },
Data: nil, Data: nil,
}, },
@ -1449,24 +1449,24 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock
}, },
{ {
name: "find all EventTwoIndexed events", 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{ expected: []ExpectedEthLog{
{ {
Address: contract2, Address: contract2,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventTwoIndexed"], kit.EventMatrixContract.Ev["EventTwoIndexed"],
paddedUint64(44), uint64EthHash(44),
paddedUint64(19), uint64EthHash(19),
}, },
Data: nil, Data: nil,
}, },
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventTwoIndexed"], kit.EventMatrixContract.Ev["EventTwoIndexed"],
paddedUint64(40), uint64EthHash(40),
paddedUint64(20), uint64EthHash(20),
}, },
Data: nil, Data: nil,
}, },
@ -1474,16 +1474,16 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock
}, },
{ {
name: "find all EventThreeIndexed events", 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{ expected: []ExpectedEthLog{
{ {
Address: contract2, Address: contract2,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventThreeIndexed"], kit.EventMatrixContract.Ev["EventThreeIndexed"],
paddedUint64(44), uint64EthHash(44),
paddedUint64(27), uint64EthHash(27),
paddedUint64(19), uint64EthHash(19),
}, },
Data: nil, Data: nil,
}, },
@ -1491,30 +1491,30 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock
}, },
{ {
name: "find all EventOneIndexedWithData events", 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{ expected: []ExpectedEthLog{
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventOneIndexedWithData"], kit.EventMatrixContract.Ev["EventOneIndexedWithData"],
paddedUint64(44), uint64EthHash(44),
}, },
Data: paddedUint64(19), Data: paddedUint64(19),
}, },
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventOneIndexedWithData"], kit.EventMatrixContract.Ev["EventOneIndexedWithData"],
paddedUint64(46), uint64EthHash(46),
}, },
Data: paddedUint64(12), Data: paddedUint64(12),
}, },
{ {
Address: contract2, Address: contract2,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventOneIndexedWithData"], kit.EventMatrixContract.Ev["EventOneIndexedWithData"],
paddedUint64(50), uint64EthHash(50),
}, },
Data: paddedUint64(9), Data: paddedUint64(9),
}, },
@ -1522,33 +1522,33 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock
}, },
{ {
name: "find all EventTwoIndexedWithData events", 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{ expected: []ExpectedEthLog{
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], kit.EventMatrixContract.Ev["EventTwoIndexedWithData"],
paddedUint64(44), uint64EthHash(44),
paddedUint64(27), uint64EthHash(27),
}, },
Data: paddedUint64(19), Data: paddedUint64(19),
}, },
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], kit.EventMatrixContract.Ev["EventTwoIndexedWithData"],
paddedUint64(46), uint64EthHash(46),
paddedUint64(27), uint64EthHash(27),
}, },
Data: paddedUint64(19), Data: paddedUint64(19),
}, },
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], kit.EventMatrixContract.Ev["EventTwoIndexedWithData"],
paddedUint64(46), uint64EthHash(46),
paddedUint64(14), uint64EthHash(14),
}, },
Data: paddedUint64(19), Data: paddedUint64(19),
}, },
@ -1556,16 +1556,16 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock
}, },
{ {
name: "find all EventThreeIndexedWithData events", 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{ expected: []ExpectedEthLog{
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventThreeIndexedWithData"], kit.EventMatrixContract.Ev["EventThreeIndexedWithData"],
paddedUint64(44), uint64EthHash(44),
paddedUint64(27), uint64EthHash(27),
paddedUint64(19), uint64EthHash(19),
}, },
Data: paddedUint64(12), Data: paddedUint64(12),
}, },
@ -1574,60 +1574,60 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock
{ {
name: "find all events with topic2 of 44", 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{ expected: []ExpectedEthLog{
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventOneIndexed"], kit.EventMatrixContract.Ev["EventOneIndexed"],
paddedUint64(44), uint64EthHash(44),
}, },
Data: nil, Data: nil,
}, },
{ {
Address: contract2, Address: contract2,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventTwoIndexed"], kit.EventMatrixContract.Ev["EventTwoIndexed"],
paddedUint64(44), uint64EthHash(44),
paddedUint64(19), uint64EthHash(19),
}, },
Data: nil, Data: nil,
}, },
{ {
Address: contract2, Address: contract2,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventThreeIndexed"], kit.EventMatrixContract.Ev["EventThreeIndexed"],
paddedUint64(44), uint64EthHash(44),
paddedUint64(27), uint64EthHash(27),
paddedUint64(19), uint64EthHash(19),
}, },
Data: nil, Data: nil,
}, },
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventOneIndexedWithData"], kit.EventMatrixContract.Ev["EventOneIndexedWithData"],
paddedUint64(44), uint64EthHash(44),
}, },
Data: paddedUint64(19), Data: paddedUint64(19),
}, },
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], kit.EventMatrixContract.Ev["EventTwoIndexedWithData"],
paddedUint64(44), uint64EthHash(44),
paddedUint64(27), uint64EthHash(27),
}, },
Data: paddedUint64(19), Data: paddedUint64(19),
}, },
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventThreeIndexedWithData"], kit.EventMatrixContract.Ev["EventThreeIndexedWithData"],
paddedUint64(44), uint64EthHash(44),
paddedUint64(27), uint64EthHash(27),
paddedUint64(19), uint64EthHash(19),
}, },
Data: paddedUint64(12), Data: paddedUint64(12),
}, },
@ -1635,32 +1635,32 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock
}, },
{ {
name: "find all events with topic2 of 46", 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{ expected: []ExpectedEthLog{
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventOneIndexedWithData"], kit.EventMatrixContract.Ev["EventOneIndexedWithData"],
paddedUint64(46), uint64EthHash(46),
}, },
Data: paddedUint64(12), Data: paddedUint64(12),
}, },
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], kit.EventMatrixContract.Ev["EventTwoIndexedWithData"],
paddedUint64(46), uint64EthHash(46),
paddedUint64(27), uint64EthHash(27),
}, },
Data: paddedUint64(19), Data: paddedUint64(19),
}, },
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], kit.EventMatrixContract.Ev["EventTwoIndexedWithData"],
paddedUint64(46), uint64EthHash(46),
paddedUint64(14), uint64EthHash(14),
}, },
Data: paddedUint64(19), Data: paddedUint64(19),
}, },
@ -1668,14 +1668,14 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock
}, },
{ {
name: "find all events with topic2 of 50", 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{ expected: []ExpectedEthLog{
{ {
Address: contract2, Address: contract2,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventOneIndexedWithData"], kit.EventMatrixContract.Ev["EventOneIndexedWithData"],
paddedUint64(50), uint64EthHash(50),
}, },
Data: paddedUint64(9), Data: paddedUint64(9),
}, },
@ -1683,40 +1683,40 @@ func getTopicFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlock
}, },
{ {
name: "find all events with topic2 of 46 or 50", 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{ expected: []ExpectedEthLog{
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventOneIndexedWithData"], kit.EventMatrixContract.Ev["EventOneIndexedWithData"],
paddedUint64(46), uint64EthHash(46),
}, },
Data: paddedUint64(12), Data: paddedUint64(12),
}, },
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], kit.EventMatrixContract.Ev["EventTwoIndexedWithData"],
paddedUint64(46), uint64EthHash(46),
paddedUint64(27), uint64EthHash(27),
}, },
Data: paddedUint64(19), Data: paddedUint64(19),
}, },
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], kit.EventMatrixContract.Ev["EventTwoIndexedWithData"],
paddedUint64(46), uint64EthHash(46),
paddedUint64(14), uint64EthHash(14),
}, },
Data: paddedUint64(19), Data: paddedUint64(19),
}, },
{ {
Address: contract2, Address: contract2,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventOneIndexedWithData"], kit.EventMatrixContract.Ev["EventOneIndexedWithData"],
paddedUint64(50), uint64EthHash(50),
}, },
Data: paddedUint64(9), 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", name: "find all events with topic1 of EventTwoIndexedWithData and topic3 of 27",
spec: kit.NewEthFilterBuilder(). spec: kit.NewEthFilterBuilder().
FromBlockEpoch(0). FromBlockEpoch(0).
Topic1OneOf(paddedEthHash(kit.EventMatrixContract.Ev["EventTwoIndexedWithData"])). Topic1OneOf(kit.EventMatrixContract.Ev["EventTwoIndexedWithData"]).
Topic3OneOf(paddedEthHash(paddedUint64(27))). Topic3OneOf(uint64EthHash(27)).
Filter(), Filter(),
expected: []ExpectedEthLog{ expected: []ExpectedEthLog{
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], kit.EventMatrixContract.Ev["EventTwoIndexedWithData"],
paddedUint64(44), uint64EthHash(44),
paddedUint64(27), uint64EthHash(27),
}, },
Data: paddedUint64(19), Data: paddedUint64(19),
}, },
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], kit.EventMatrixContract.Ev["EventTwoIndexedWithData"],
paddedUint64(46), uint64EthHash(46),
paddedUint64(27), uint64EthHash(27),
}, },
Data: paddedUint64(19), 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", name: "find all events with topic1 of EventTwoIndexedWithData or EventOneIndexed and topic2 of 44",
spec: kit.NewEthFilterBuilder(). spec: kit.NewEthFilterBuilder().
FromBlockEpoch(0). FromBlockEpoch(0).
Topic1OneOf(paddedEthHash(kit.EventMatrixContract.Ev["EventTwoIndexedWithData"]), paddedEthHash(kit.EventMatrixContract.Ev["EventOneIndexed"])). Topic1OneOf((kit.EventMatrixContract.Ev["EventTwoIndexedWithData"]), kit.EventMatrixContract.Ev["EventOneIndexed"]).
Topic2OneOf(paddedEthHash(paddedUint64(44))). Topic2OneOf(uint64EthHash(44)).
Filter(), Filter(),
expected: []ExpectedEthLog{ expected: []ExpectedEthLog{
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventTwoIndexedWithData"], kit.EventMatrixContract.Ev["EventTwoIndexedWithData"],
paddedUint64(44), uint64EthHash(44),
paddedUint64(27), uint64EthHash(27),
}, },
Data: paddedUint64(19), Data: paddedUint64(19),
}, },
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventOneIndexed"], kit.EventMatrixContract.Ev["EventOneIndexed"],
paddedUint64(44), uint64EthHash(44),
}, },
Data: nil, Data: nil,
}, },
@ -1794,35 +1794,35 @@ func getAddressFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlo
expected: []ExpectedEthLog{ expected: []ExpectedEthLog{
{ {
Address: contract2, Address: contract2,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventZeroData"], kit.EventMatrixContract.Ev["EventZeroData"],
}, },
Data: nil, Data: nil,
}, },
{ {
Address: contract2, Address: contract2,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventThreeIndexed"], kit.EventMatrixContract.Ev["EventThreeIndexed"],
paddedUint64(44), uint64EthHash(44),
paddedUint64(27), uint64EthHash(27),
paddedUint64(19), uint64EthHash(19),
}, },
Data: nil, Data: nil,
}, },
{ {
Address: contract2, Address: contract2,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventTwoIndexed"], kit.EventMatrixContract.Ev["EventTwoIndexed"],
paddedUint64(44), uint64EthHash(44),
paddedUint64(19), uint64EthHash(19),
}, },
Data: nil, Data: nil,
}, },
{ {
Address: contract2, Address: contract2,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventOneIndexedWithData"], kit.EventMatrixContract.Ev["EventOneIndexedWithData"],
paddedUint64(50), uint64EthHash(50),
}, },
Data: paddedUint64(9), Data: paddedUint64(9),
}, },
@ -1836,20 +1836,20 @@ func getAddressFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlo
expected: []ExpectedEthLog{ expected: []ExpectedEthLog{
{ {
Address: contract2, Address: contract2,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventThreeIndexed"], kit.EventMatrixContract.Ev["EventThreeIndexed"],
paddedUint64(44), uint64EthHash(44),
paddedUint64(27), uint64EthHash(27),
paddedUint64(19), uint64EthHash(19),
}, },
Data: nil, Data: nil,
}, },
{ {
Address: contract2, Address: contract2,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventTwoIndexed"], kit.EventMatrixContract.Ev["EventTwoIndexed"],
paddedUint64(44), uint64EthHash(44),
paddedUint64(19), uint64EthHash(19),
}, },
Data: nil, Data: nil,
}, },
@ -1861,31 +1861,31 @@ func getAddressFilterTestCases(contract1, contract2 ethtypes.EthAddress, fromBlo
spec: kit.NewEthFilterBuilder(). spec: kit.NewEthFilterBuilder().
FromBlockEpoch(0). FromBlockEpoch(0).
AddressOneOf(contract1, contract2). AddressOneOf(contract1, contract2).
Topic1OneOf(paddedEthHash(kit.EventMatrixContract.Ev["EventOneIndexedWithData"])). Topic1OneOf(kit.EventMatrixContract.Ev["EventOneIndexedWithData"]).
Filter(), Filter(),
expected: []ExpectedEthLog{ expected: []ExpectedEthLog{
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventOneIndexedWithData"], kit.EventMatrixContract.Ev["EventOneIndexedWithData"],
paddedUint64(44), uint64EthHash(44),
}, },
Data: paddedUint64(19), Data: paddedUint64(19),
}, },
{ {
Address: contract1, Address: contract1,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventOneIndexedWithData"], kit.EventMatrixContract.Ev["EventOneIndexedWithData"],
paddedUint64(46), uint64EthHash(46),
}, },
Data: paddedUint64(12), Data: paddedUint64(12),
}, },
{ {
Address: contract2, Address: contract2,
Topics: []ethtypes.EthBytes{ Topics: []ethtypes.EthHash{
kit.EventMatrixContract.Ev["EventOneIndexedWithData"], kit.EventMatrixContract.Ev["EventOneIndexedWithData"],
paddedUint64(50), uint64EthHash(50),
}, },
Data: paddedUint64(9), Data: paddedUint64(9),
}, },
@ -1906,7 +1906,7 @@ type ExpectedEthLog struct {
Address ethtypes.EthAddress `json:"address"` Address ethtypes.EthAddress `json:"address"`
// List of topics associated with the event log. // 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 is the value of the event log, excluding topics
Data ethtypes.EthBytes `json:"data"` 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)) 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)) ss := make([]string, len(topics))
for i := range 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, ",") return strings.Join(ss, ",")
} }
@ -1958,7 +1958,7 @@ func AssertEthLogs(t *testing.T, actual []*ethtypes.EthLog, expected []ExpectedE
} }
for j := range elog.Topics { for j := range elog.Topics {
if !bytes.Equal(elog.Topics[j], want.Topics[j]) { if elog.Topics[j] != want.Topics[j] {
continue LoopExpected continue LoopExpected
} }
} }
@ -2132,7 +2132,7 @@ func ParseEthLog(in map[string]interface{}) (*ethtypes.EthLog, error) {
if err != nil { if err != nil {
return nil, xerrors.Errorf("%s: %w", k, err) return nil, xerrors.Errorf("%s: %w", k, err)
} }
el.Topics = append(el.Topics, topic) el.Topics = append(el.Topics, paddedEthHash(topic))
continue continue
} }
@ -2145,7 +2145,7 @@ func ParseEthLog(in map[string]interface{}) (*ethtypes.EthLog, error) {
if err != nil { if err != nil {
return nil, xerrors.Errorf("%s: %w", k, err) 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 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 { func paddedEthHash(orig []byte) ethtypes.EthHash {
if len(orig) > 32 { if len(orig) > 32 {
panic("exceeds EthHash length") panic("exceeds EthHash length")

View File

@ -2,12 +2,16 @@ package kit
import ( import (
"golang.org/x/crypto/sha3" "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 := sha3.NewLegacyKeccak256()
hasher.Write([]byte(sig)) hasher.Write([]byte(sig))
return hasher.Sum(nil) var hash ethtypes.EthHash
copy(hash[:], hasher.Sum(nil))
return hash
} }
func EthFunctionHash(sig string) []byte { func EthFunctionHash(sig string) []byte {
@ -20,7 +24,7 @@ func EthFunctionHash(sig string) []byte {
type SolidityContractDef struct { type SolidityContractDef struct {
Filename string // filename of the hex of the contract, e.g. contracts/EventMatrix.hex 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 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 Ev map[string]ethtypes.EthHash // mapping of event names to 256-bit signature hashes
} }
var EventMatrixContract = SolidityContractDef{ var EventMatrixContract = SolidityContractDef{
@ -38,7 +42,7 @@ var EventMatrixContract = SolidityContractDef{
"logEventTwoIndexedWithData": EthFunctionHash("logEventTwoIndexedWithData(uint256,uint256,uint256)"), "logEventTwoIndexedWithData": EthFunctionHash("logEventTwoIndexedWithData(uint256,uint256,uint256)"),
"logEventThreeIndexedWithData": EthFunctionHash("logEventThreeIndexedWithData(uint256,uint256,uint256,uint256)"), "logEventThreeIndexedWithData": EthFunctionHash("logEventThreeIndexedWithData(uint256,uint256,uint256,uint256)"),
}, },
Ev: map[string][]byte{ Ev: map[string]ethtypes.EthHash{
"EventZeroData": EthTopicHash("EventZeroData()"), "EventZeroData": EthTopicHash("EventZeroData()"),
"EventOneData": EthTopicHash("EventOneData(uint256)"), "EventOneData": EthTopicHash("EventOneData(uint256)"),
"EventTwoData": EthTopicHash("EventTwoData(uint256,uint256)"), "EventTwoData": EthTopicHash("EventTwoData(uint256,uint256)"),
@ -60,5 +64,5 @@ var EventsContract = SolidityContractDef{
"log_zero_nodata": {0x00, 0x00, 0x00, 0x01}, "log_zero_nodata": {0x00, 0x00, 0x00, 0x01},
"log_four_data": {0x00, 0x00, 0x00, 0x02}, "log_four_data": {0x00, 0x00, 0x00, 0x02},
}, },
Ev: map[string][]byte{}, Ev: map[string]ethtypes.EthHash{},
} }

View File

@ -1376,7 +1376,12 @@ func ethFilterResultFromEvents(evs []*filter.CollectedEvent, sa StateAPI) (*etht
continue continue
} }
if entry.Key == ethtypes.EthTopic1 || entry.Key == ethtypes.EthTopic2 || entry.Key == ethtypes.EthTopic3 || entry.Key == ethtypes.EthTopic4 { 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 { } else {
log.Data = entry.Value log.Data = entry.Value
} }
@ -1913,8 +1918,13 @@ func newEthTxReceipt(ctx context.Context, tx ethtypes.EthTx, lookup *api.MsgLook
continue continue
} }
if entry.Key == ethtypes.EthTopic1 || entry.Key == ethtypes.EthTopic2 || entry.Key == ethtypes.EthTopic3 || entry.Key == ethtypes.EthTopic4 { 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) ethtypes.EthBloomSet(receipt.LogsBloom, entry.Value)
l.Topics = append(l.Topics, entry.Value)
} else { } else {
l.Data = entry.Value l.Data = entry.Value
} }