fix: eth: strict event parsing
We now enforce the following rules: 1. No duplicate topics or data. 2. Topics must have 32 byte keys. 3. Topics may not be skipped. (e.g., no t1 & t3 without a t2). 4. Raw codecs. We _don't_ require that topics/data be emitted in any specific order. We _skip_ events with unknown keys. We _drop_ events that violate the above rules.
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
package full
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/filecoin-project/lotus/chain/types"
|
||||
"github.com/filecoin-project/lotus/chain/types/ethtypes"
|
||||
"github.com/ipfs/go-cid"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
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{})
|
||||
}
|
||||
Reference in New Issue
Block a user