Merge pull request #10215 from filecoin-project/steb/txhash-topic

feat: eth: harden event processing
This commit is contained in:
Aayush Rajasekaran 2023-02-10 15:16:34 -05:00 committed by GitHub
commit 1996694a2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 368 additions and 219 deletions

Binary file not shown.

Binary file not shown.

View File

@ -25,13 +25,6 @@ import (
"github.com/filecoin-project/lotus/lib/must"
)
var (
EthTopic1 = "t1"
EthTopic2 = "t2"
EthTopic3 = "t3"
EthTopic4 = "t4"
)
var ErrInvalidAddress = errors.New("invalid Filecoin Eth address")
type EthUint64 uint64
@ -626,7 +619,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

View File

@ -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,
}

View File

@ -2794,7 +2794,7 @@ Response:
"address": "0x5cbeecf99d3fdb3f25e309cc264f240bb0664031",
"data": "0x07",
"topics": [
"0x07"
"0x37690cfec6c1bf4c3b9288c7a5d783e98731e90b0a4c177c2a374c7a9427355e"
],
"removed": true,
"logIndex": "0x5",

View File

@ -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))
}
}
}
@ -2153,22 +2153,18 @@ func ParseEthLog(in map[string]interface{}) (*ethtypes.EthLog, error) {
return el, err
}
func paddedEthBytes(orig []byte) ethtypes.EthBytes {
needed := 32 - len(orig)
if needed <= 0 {
return orig
}
ret := make([]byte, 32)
copy(ret[needed:], orig)
return ret
}
func paddedUint64(v uint64) ethtypes.EthBytes {
buf := make([]byte, 32)
binary.BigEndian.PutUint64(buf[24:], v)
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")

View File

@ -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{},
}

View File

@ -1358,6 +1358,67 @@ type filterTipSetCollector interface {
TakeCollectedTipSets(context.Context) []types.TipSetKey
}
func ethLogFromEvent(entries []types.EventEntry) (data []byte, topics []ethtypes.EthHash, ok bool) {
var (
topicsFound [4]bool
topicsFoundCount int
dataFound bool
)
for _, entry := range entries {
// Drop events with non-raw topics to avoid mistakes.
if entry.Codec != cid.Raw {
log.Warnw("did not expect an event entry with a non-raw codec", "codec", entry.Codec, "key", entry.Key)
return nil, nil, false
}
// Check if the key is t1..t4
if len(entry.Key) == 2 && "t1" <= entry.Key && entry.Key <= "t4" {
// '1' - '1' == 0, etc.
idx := int(entry.Key[1] - '1')
// Drop events with mis-sized topics.
if len(entry.Value) != 32 {
log.Warnw("got an EVM event topic with an invalid size", "key", entry.Key, "size", len(entry.Value))
return nil, nil, false
}
// Drop events with duplicate topics.
if topicsFound[idx] {
log.Warnw("got a duplicate EVM event topic", "key", entry.Key)
return nil, nil, false
}
topicsFound[idx] = true
topicsFoundCount++
// Extend the topics array
for len(topics) <= idx {
topics = append(topics, ethtypes.EthHash{})
}
copy(topics[idx][:], entry.Value)
} else if entry.Key == "d" {
// Drop events with duplicate data fields.
if dataFound {
log.Warnw("got duplicate EVM event data")
return nil, nil, false
}
dataFound = true
data = entry.Value
} else {
// Skip entries we don't understand (makes it easier to extend things).
// But we warn for now because we don't expect them.
log.Warnw("unexpected event entry", "key", entry.Key)
}
}
// Drop events with skipped topics.
if len(topics) != topicsFoundCount {
log.Warnw("EVM event topic length mismatch", "expected", len(topics), "actual", topicsFoundCount)
return nil, nil, false
}
return data, topics, true
}
func ethFilterResultFromEvents(evs []*filter.CollectedEvent, sa StateAPI) (*ethtypes.EthFilterResult, error) {
res := &ethtypes.EthFilterResult{}
for _, ev := range evs {
@ -1367,19 +1428,14 @@ func ethFilterResultFromEvents(evs []*filter.CollectedEvent, sa StateAPI) (*etht
TransactionIndex: ethtypes.EthUint64(ev.MsgIdx),
BlockNumber: ethtypes.EthUint64(ev.Height),
}
var (
err error
ok bool
)
var err error
for _, entry := range ev.Entries {
// Skip all events that aren't "raw" data.
if entry.Codec != cid.Raw {
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)
} else {
log.Data = entry.Value
}
log.Data, log.Topics, ok = ethLogFromEvent(ev.Entries)
if !ok {
continue
}
log.Address, err = ethtypes.EthAddressFromFilecoinAddress(ev.EmitterAddr)
@ -1907,18 +1963,16 @@ func newEthTxReceipt(ctx context.Context, tx ethtypes.EthTx, lookup *api.MsgLook
BlockNumber: blockNumber,
}
for _, entry := range evt.Entries {
// Ignore any non-raw values/keys.
if entry.Codec != cid.Raw {
continue
}
if entry.Key == ethtypes.EthTopic1 || entry.Key == ethtypes.EthTopic2 || entry.Key == ethtypes.EthTopic3 || entry.Key == ethtypes.EthTopic4 {
ethtypes.EthBloomSet(receipt.LogsBloom, entry.Value)
l.Topics = append(l.Topics, entry.Value)
} else {
l.Data = entry.Value
}
data, topics, ok := ethLogFromEvent(evt.Entries)
if !ok {
// not an eth event.
continue
}
for _, topic := range topics {
ethtypes.EthBloomSet(receipt.LogsBloom, topic[:])
}
l.Data = data
l.Topics = topics
addr, err := address.NewIDAddress(uint64(evt.Emitter))
if err != nil {

102
node/impl/full/eth_test.go Normal file
View File

@ -0,0 +1,102 @@
package full
import (
"testing"
"github.com/ipfs/go-cid"
"github.com/stretchr/testify/require"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/types/ethtypes"
)
func TestEthLogFromEvent(t *testing.T) {
// basic empty
data, topics, ok := ethLogFromEvent(nil)
require.True(t, ok)
require.Nil(t, data)
require.Nil(t, topics)
// basic topic
data, topics, ok = ethLogFromEvent([]types.EventEntry{{
Flags: 0,
Key: "t1",
Codec: cid.Raw,
Value: make([]byte, 32),
}})
require.True(t, ok)
require.Nil(t, data)
require.Len(t, topics, 1)
require.Equal(t, topics[0], ethtypes.EthHash{})
// basic topic with data
data, topics, ok = ethLogFromEvent([]types.EventEntry{{
Flags: 0,
Key: "t1",
Codec: cid.Raw,
Value: make([]byte, 32),
}, {
Flags: 0,
Key: "d",
Codec: cid.Raw,
Value: []byte{0x0},
}})
require.True(t, ok)
require.Equal(t, data, []byte{0x0})
require.Len(t, topics, 1)
require.Equal(t, topics[0], ethtypes.EthHash{})
// skip topic
_, _, ok = ethLogFromEvent([]types.EventEntry{{
Flags: 0,
Key: "t2",
Codec: cid.Raw,
Value: make([]byte, 32),
}})
require.False(t, ok)
// duplicate topic
_, _, ok = ethLogFromEvent([]types.EventEntry{{
Flags: 0,
Key: "t1",
Codec: cid.Raw,
Value: make([]byte, 32),
}, {
Flags: 0,
Key: "t1",
Codec: cid.Raw,
Value: make([]byte, 32),
}})
require.False(t, ok)
// duplicate data
_, _, ok = ethLogFromEvent([]types.EventEntry{{
Flags: 0,
Key: "d",
Codec: cid.Raw,
Value: make([]byte, 32),
}, {
Flags: 0,
Key: "d",
Codec: cid.Raw,
Value: make([]byte, 32),
}})
require.False(t, ok)
// unknown key is fine
data, topics, ok = ethLogFromEvent([]types.EventEntry{{
Flags: 0,
Key: "t5",
Codec: cid.Raw,
Value: make([]byte, 32),
}, {
Flags: 0,
Key: "t1",
Codec: cid.Raw,
Value: make([]byte, 32),
}})
require.True(t, ok)
require.Nil(t, data)
require.Len(t, topics, 1)
require.Equal(t, topics[0], ethtypes.EthHash{})
}