Skip to content

Commit 4a8eaeb

Browse files
committed
core/filtermaps: implement log indexer as core.Indexer
1 parent 8ce710a commit 4a8eaeb

33 files changed

+3730
-4002
lines changed

common/range.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ type Range[T uint32 | uint64] struct {
2727

2828
// NewRange creates a new range based of first element and number of elements.
2929
func NewRange[T uint32 | uint64](first, count T) Range[T] {
30-
return Range[T]{first, first + count}
30+
afterLast := first + count
31+
if afterLast < first {
32+
panic("range overflow")
33+
}
34+
return Range[T]{first, afterLast}
3135
}
3236

3337
// First returns the first element of the range.
@@ -97,6 +101,12 @@ func (r Range[T]) Intersection(q Range[T]) Range[T] {
97101

98102
// Union returns the union of two ranges. Panics for gapped ranges.
99103
func (r Range[T]) Union(q Range[T]) Range[T] {
104+
if r.IsEmpty() {
105+
return q
106+
}
107+
if q.IsEmpty() {
108+
return r
109+
}
100110
if max(r.first, q.first) > min(r.afterLast, q.afterLast) {
101111
panic("cannot create union; gap between ranges")
102112
}

core/filtermaps/chain_view.go

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ type blockchain interface {
3232
GetRawReceipts(hash common.Hash, number uint64) types.Receipts
3333
}
3434

35-
// ChainView represents an immutable view of a chain with a block id and a set
35+
// ChainView represents an immutable view of a chain with a block hash and a set
3636
// of receipts associated to each block number and a block hash associated with
3737
// all block numbers except the head block. This is because in the future
3838
// ChainView might represent a view where the head block is currently being
39-
// created. Block id is a unique identifier that can also be calculated for the
40-
// head block.
39+
// created.
4140
// Note that the view's head does not have to be the current canonical head
4241
// of the underlying blockchain, it should only possess the block headers
4342
// and receipts up until the expected chain view head.
@@ -79,21 +78,6 @@ func (cv *ChainView) BlockHash(number uint64) common.Hash {
7978
return cv.blockHash(number)
8079
}
8180

82-
// BlockId returns the unique block id belonging to the given block number.
83-
// Note that it is currently equal to the block hash. In the future it might
84-
// be a different id for future blocks if the log index root becomes part of
85-
// consensus and therefore rendering the index with the new head will happen
86-
// before the hash of that new head is available.
87-
func (cv *ChainView) BlockId(number uint64) common.Hash {
88-
cv.lock.Lock()
89-
defer cv.lock.Unlock()
90-
91-
if number > cv.headNumber {
92-
panic("invalid block number")
93-
}
94-
return cv.blockHash(number)
95-
}
96-
9781
// Header returns the block header at the given block number.
9882
func (cv *ChainView) Header(number uint64) *types.Header {
9983
return cv.chain.GetHeader(cv.BlockHash(number), number)
@@ -128,7 +112,7 @@ func (cv *ChainView) SharedRange(cv2 *ChainView) common.Range[uint64] {
128112
return common.Range[uint64]{}
129113
}
130114
sharedLen := min(cv.headNumber, cv2.headNumber) + 1
131-
for sharedLen > 0 && cv.BlockId(sharedLen-1) != cv2.BlockId(sharedLen-1) {
115+
for sharedLen > 0 && cv.BlockHash(sharedLen-1) != cv2.BlockHash(sharedLen-1) {
132116
sharedLen--
133117
}
134118
return common.NewRange(0, sharedLen)
@@ -139,7 +123,7 @@ func equalViews(cv1, cv2 *ChainView) bool {
139123
if cv1 == nil || cv2 == nil {
140124
return false
141125
}
142-
return cv1.headNumber == cv2.headNumber && cv1.BlockId(cv1.headNumber) == cv2.BlockId(cv2.headNumber)
126+
return cv1.headNumber == cv2.headNumber && cv1.BlockHash(cv1.headNumber) == cv2.BlockHash(cv2.headNumber)
143127
}
144128

145129
// matchViews returns true if the two chain views are equivalent up until the
@@ -152,12 +136,7 @@ func matchViews(cv1, cv2 *ChainView, number uint64) bool {
152136
if cv1.headNumber < number || cv2.headNumber < number {
153137
return false
154138
}
155-
var h1, h2 common.Hash
156-
if number == cv1.headNumber || number == cv2.headNumber {
157-
h1, h2 = cv1.BlockId(number), cv2.BlockId(number)
158-
} else {
159-
h1, h2 = cv1.BlockHash(number), cv2.BlockHash(number)
160-
}
139+
h1, h2 := cv1.BlockHash(number), cv2.BlockHash(number)
161140
return h1 == h2 && h1 != common.Hash{}
162141
}
163142

core/filtermaps/checkpoints.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type checkpointList []epochCheckpoint
3333
// be initialized at the epoch boundary.
3434
type epochCheckpoint struct {
3535
BlockNumber uint64 // block that generated the last log value of the given epoch
36-
BlockId common.Hash
36+
BlockHash common.Hash
3737
FirstIndex uint64 // first log value index of the given block
3838
}
3939

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
[
2-
{"blockNumber": 814410, "blockId": "0x6c38f0d4ff2c23ae187f581cebb0963c0ec2dbf051b289de1582c369c98a3244", "firstIndex": 67107349},
3-
{"blockNumber": 914268, "blockId": "0xeff161573a11eb6e2ab930674a5245b2c5ffc5e9e63093503344ec6fa60578a3", "firstIndex": 134216064},
4-
{"blockNumber": 1048866, "blockId": "0xebd3b95415dad9ab7f2b1d25e48c791e299063c09427bbde54217029c3e215ad", "firstIndex": 201325914},
5-
{"blockNumber": 1144433, "blockId": "0x0c895438ef12a2c835b4372c209e40a499ba2b18ed0e658846813784de391392", "firstIndex": 268434935},
6-
{"blockNumber": 1230406, "blockId": "0xd3512e7241efc9853e46b1ad565403d302f62457b6dd84917c31ff375fabf487", "firstIndex": 335544096},
7-
{"blockNumber": 1309104, "blockId": "0xcf108c6e002e7a5657bf7587adde03edf5f20d03e95b66a194eb8080daaf4f97", "firstIndex": 402653143},
8-
{"blockNumber": 1380499, "blockId": "0x984b0f8b6bf06f1240bbca8c1df9bef3880bedafd5b5c2a55cbc2f64c9efb974", "firstIndex": 469759822},
9-
{"blockNumber": 1476950, "blockId": "0x8323b528bb8d80a96b172de92452be0f45078a9ca311cb4be6675f089e25a426", "firstIndex": 536870335},
10-
{"blockNumber": 1533506, "blockId": "0x737dc416070aa3b522a0c2ab813a70c1820f062c718f27597394f309f0004106", "firstIndex": 603979618},
11-
{"blockNumber": 1613764, "blockId": "0x9d6a5a505afdda86b1ebcc2b10cb687a1f89c2e2b74315945a3ddc6a0b3c9cd6", "firstIndex": 671088253},
12-
{"blockNumber": 1719073, "blockId": "0x17bf6a51d9c55a908c3e9e652f80b2aa464b049c697abf3bd5a537e461aff0b9", "firstIndex": 738197366},
13-
{"blockNumber": 1973133, "blockId": "0x14b288d70e688de3334d09283670301aacfed21c193da8a56fd63767f8177705", "firstIndex": 805305624},
14-
{"blockNumber": 2274761, "blockId": "0xf02790b273b04219e001d2fcc93e8e47708cb228364e96ad754bc8050d08a9aa", "firstIndex": 872414871},
15-
{"blockNumber": 2530470, "blockId": "0x157ea98b1a7e66dd3f045da8e25219800671f9a000211d3d94006efd33d89a80", "firstIndex": 939523234},
16-
{"blockNumber": 2781706, "blockId": "0xa723663a584e280700d2302eba03d1b3b6549333db70c6152576d33e2911f594", "firstIndex": 1006632936},
17-
{"blockNumber": 3101669, "blockId": "0xa6e66a18fed64d33178c7088f273c404be597662c34f6e6884cf2e24f3ea4ace", "firstIndex": 1073741353},
18-
{"blockNumber": 3221725, "blockId": "0xe771f897dece48b1583cc1d1d10de8015da57407eb1fdf239fdbe46eaab85143", "firstIndex": 1140850137},
19-
{"blockNumber": 3357164, "blockId": "0x6252d0aa54c79623b0680069c88d7b5c47983f0d5c4845b6c811b8d9b5e8ff3c", "firstIndex": 1207959453},
20-
{"blockNumber": 3447019, "blockId": "0xeb7d585e1e063f3cc05ed399fbf6c2df63c271f62f030acb804e9fb1e74b6dc1", "firstIndex": 1275067542},
21-
{"blockNumber": 3546397, "blockId": "0xdabdef7defa4281180a57c5af121877b82274f15ccf074ea0096146f4c246df2", "firstIndex": 1342176778},
22-
{"blockNumber": 3867885, "blockId": "0x8be069dd7a3e2ffb869ee164d11b28555233d2510b134ab9d5484fdae55d2225", "firstIndex": 1409285539},
23-
{"blockNumber": 3935446, "blockId": "0xc91a61bc215bbcccc3020c62e5c8153162df0d8bcc59813d74671b2d24903ed7", "firstIndex": 1476394742},
24-
{"blockNumber": 3989508, "blockId": "0xc85dec36a767e44237842ef51915944c2a49780c8c394a3aa6cfb013c99cf58b", "firstIndex": 1543503452},
25-
{"blockNumber": 4057078, "blockId": "0xccdb79f6705629cb6ab1667a1244938f60911236549143fcff23a3989213e67e", "firstIndex": 1610612030},
26-
{"blockNumber": 4126499, "blockId": "0x92f2ef21fc911e87e81e38373d5f2915587b9648a0ab3cf4fcfe3e5aaffe7b85", "firstIndex": 1677720416},
27-
{"blockNumber": 4239335, "blockId": "0x64fbd22965eb583a584552b7edb9b7ce26fb6aad247c1063d0d5a4d11cbcc58c", "firstIndex": 1744830176}
2+
{"blockNumber": 814410, "blockHash": "0x6c38f0d4ff2c23ae187f581cebb0963c0ec2dbf051b289de1582c369c98a3244", "firstIndex": 67107349},
3+
{"blockNumber": 914268, "blockHash": "0xeff161573a11eb6e2ab930674a5245b2c5ffc5e9e63093503344ec6fa60578a3", "firstIndex": 134216064},
4+
{"blockNumber": 1048866, "blockHash": "0xebd3b95415dad9ab7f2b1d25e48c791e299063c09427bbde54217029c3e215ad", "firstIndex": 201325914},
5+
{"blockNumber": 1144433, "blockHash": "0x0c895438ef12a2c835b4372c209e40a499ba2b18ed0e658846813784de391392", "firstIndex": 268434935},
6+
{"blockNumber": 1230406, "blockHash": "0xd3512e7241efc9853e46b1ad565403d302f62457b6dd84917c31ff375fabf487", "firstIndex": 335544096},
7+
{"blockNumber": 1309104, "blockHash": "0xcf108c6e002e7a5657bf7587adde03edf5f20d03e95b66a194eb8080daaf4f97", "firstIndex": 402653143},
8+
{"blockNumber": 1380499, "blockHash": "0x984b0f8b6bf06f1240bbca8c1df9bef3880bedafd5b5c2a55cbc2f64c9efb974", "firstIndex": 469759822},
9+
{"blockNumber": 1476950, "blockHash": "0x8323b528bb8d80a96b172de92452be0f45078a9ca311cb4be6675f089e25a426", "firstIndex": 536870335},
10+
{"blockNumber": 1533506, "blockHash": "0x737dc416070aa3b522a0c2ab813a70c1820f062c718f27597394f309f0004106", "firstIndex": 603979618},
11+
{"blockNumber": 1613764, "blockHash": "0x9d6a5a505afdda86b1ebcc2b10cb687a1f89c2e2b74315945a3ddc6a0b3c9cd6", "firstIndex": 671088253},
12+
{"blockNumber": 1719073, "blockHash": "0x17bf6a51d9c55a908c3e9e652f80b2aa464b049c697abf3bd5a537e461aff0b9", "firstIndex": 738197366},
13+
{"blockNumber": 1973133, "blockHash": "0x14b288d70e688de3334d09283670301aacfed21c193da8a56fd63767f8177705", "firstIndex": 805305624},
14+
{"blockNumber": 2274761, "blockHash": "0xf02790b273b04219e001d2fcc93e8e47708cb228364e96ad754bc8050d08a9aa", "firstIndex": 872414871},
15+
{"blockNumber": 2530470, "blockHash": "0x157ea98b1a7e66dd3f045da8e25219800671f9a000211d3d94006efd33d89a80", "firstIndex": 939523234},
16+
{"blockNumber": 2781706, "blockHash": "0xa723663a584e280700d2302eba03d1b3b6549333db70c6152576d33e2911f594", "firstIndex": 1006632936},
17+
{"blockNumber": 3101669, "blockHash": "0xa6e66a18fed64d33178c7088f273c404be597662c34f6e6884cf2e24f3ea4ace", "firstIndex": 1073741353},
18+
{"blockNumber": 3221725, "blockHash": "0xe771f897dece48b1583cc1d1d10de8015da57407eb1fdf239fdbe46eaab85143", "firstIndex": 1140850137},
19+
{"blockNumber": 3357164, "blockHash": "0x6252d0aa54c79623b0680069c88d7b5c47983f0d5c4845b6c811b8d9b5e8ff3c", "firstIndex": 1207959453},
20+
{"blockNumber": 3447019, "blockHash": "0xeb7d585e1e063f3cc05ed399fbf6c2df63c271f62f030acb804e9fb1e74b6dc1", "firstIndex": 1275067542},
21+
{"blockNumber": 3546397, "blockHash": "0xdabdef7defa4281180a57c5af121877b82274f15ccf074ea0096146f4c246df2", "firstIndex": 1342176778},
22+
{"blockNumber": 3867885, "blockHash": "0x8be069dd7a3e2ffb869ee164d11b28555233d2510b134ab9d5484fdae55d2225", "firstIndex": 1409285539},
23+
{"blockNumber": 3935446, "blockHash": "0xc91a61bc215bbcccc3020c62e5c8153162df0d8bcc59813d74671b2d24903ed7", "firstIndex": 1476394742},
24+
{"blockNumber": 3989508, "blockHash": "0xc85dec36a767e44237842ef51915944c2a49780c8c394a3aa6cfb013c99cf58b", "firstIndex": 1543503452},
25+
{"blockNumber": 4057078, "blockHash": "0xccdb79f6705629cb6ab1667a1244938f60911236549143fcff23a3989213e67e", "firstIndex": 1610612030},
26+
{"blockNumber": 4126499, "blockHash": "0x92f2ef21fc911e87e81e38373d5f2915587b9648a0ab3cf4fcfe3e5aaffe7b85", "firstIndex": 1677720416},
27+
{"blockNumber": 4239335, "blockHash": "0x64fbd22965eb583a584552b7edb9b7ce26fb6aad247c1063d0d5a4d11cbcc58c", "firstIndex": 1744830176}
2828
]

0 commit comments

Comments
 (0)