Skip to content

Commit e5dfc75

Browse files
authored
feat: L1Reader (#1099)
* port changes from #1013 * port changes from #1068 * go.mod tidy * fix compile error * fix goimports * fix log * address review comments * upgrade golang.org/x/net to 0.23.0 * port changes from #1018 * fix tests and linter errors * address review comments
1 parent ac8164f commit e5dfc75

18 files changed

+1204
-211
lines changed

common/heapmap.go

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package common
2+
3+
type HeapMap[K comparable, T Comparable[T]] struct {
4+
h *Heap[T]
5+
m *ShrinkingMap[K, *HeapElement[T]]
6+
keyFromElement func(T) K
7+
}
8+
9+
func NewHeapMap[K comparable, T Comparable[T]](keyFromElement func(T) K) *HeapMap[K, T] {
10+
return &HeapMap[K, T]{
11+
h: NewHeap[T](),
12+
m: NewShrinkingMap[K, *HeapElement[T]](1000),
13+
keyFromElement: keyFromElement,
14+
}
15+
}
16+
17+
func (hm *HeapMap[K, T]) Len() int {
18+
return hm.h.Len()
19+
}
20+
21+
func (hm *HeapMap[K, T]) Push(element T) bool {
22+
k := hm.keyFromElement(element)
23+
24+
if hm.m.Has(k) {
25+
return false
26+
}
27+
28+
heapElement := hm.h.Push(element)
29+
hm.m.Set(k, heapElement)
30+
31+
return true
32+
}
33+
34+
func (hm *HeapMap[K, T]) Pop() T {
35+
element := hm.h.Pop()
36+
k := hm.keyFromElement(element.Value())
37+
hm.m.Delete(k)
38+
39+
return element.Value()
40+
}
41+
42+
func (hm *HeapMap[K, T]) Peek() T {
43+
return hm.h.Peek().Value()
44+
}
45+
46+
func (hm *HeapMap[K, T]) RemoveByElement(element T) bool {
47+
key := hm.keyFromElement(element)
48+
heapElement, exists := hm.m.Get(key)
49+
if !exists {
50+
return false
51+
}
52+
53+
hm.h.Remove(heapElement)
54+
hm.m.Delete(key)
55+
56+
return true
57+
}
58+
59+
func (hm *HeapMap[K, T]) RemoveByKey(key K) bool {
60+
heapElement, exists := hm.m.Get(key)
61+
if !exists {
62+
return false
63+
}
64+
65+
hm.h.Remove(heapElement)
66+
hm.m.Delete(key)
67+
68+
return true
69+
}
70+
71+
func (hm *HeapMap[K, T]) Clear() {
72+
hm.h.Clear()
73+
hm.m = NewShrinkingMap[K, *HeapElement[T]](1000)
74+
}
75+
76+
func (hm *HeapMap[K, T]) Keys() []K {
77+
return hm.m.Keys()
78+
}
79+
80+
func (hm *HeapMap[K, T]) Elements() []T {
81+
var elements []T
82+
for _, element := range hm.m.Values() {
83+
elements = append(elements, element.Value())
84+
}
85+
return elements
86+
}
87+
88+
func (hm *HeapMap[K, T]) Has(element T) bool {
89+
return hm.m.Has(hm.keyFromElement(element))
90+
}

common/shrinkingmap.go

+16
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ func (s *ShrinkingMap[K, V]) Delete(key K) (deleted bool) {
4747
return true
4848
}
4949

50+
func (s *ShrinkingMap[K, V]) Keys() []K {
51+
var keys []K
52+
for k := range s.m {
53+
keys = append(keys, k)
54+
}
55+
return keys
56+
}
57+
58+
func (s *ShrinkingMap[K, V]) Values() []V {
59+
var values []V
60+
for _, v := range s.m {
61+
values = append(values, v)
62+
}
63+
return values
64+
}
65+
5066
func (s *ShrinkingMap[K, V]) Size() (size int) {
5167
return len(s.m)
5268
}

eth/backend.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import (
5757
"github.com/scroll-tech/go-ethereum/rlp"
5858
"github.com/scroll-tech/go-ethereum/rollup/ccc"
5959
"github.com/scroll-tech/go-ethereum/rollup/da_syncer"
60+
"github.com/scroll-tech/go-ethereum/rollup/l1"
6061
"github.com/scroll-tech/go-ethereum/rollup/rollup_sync_service"
6162
"github.com/scroll-tech/go-ethereum/rollup/sync_service"
6263
"github.com/scroll-tech/go-ethereum/rpc"
@@ -109,7 +110,7 @@ type Ethereum struct {
109110

110111
// New creates a new Ethereum object (including the
111112
// initialisation of the common Ethereum object)
112-
func New(stack *node.Node, config *ethconfig.Config, l1Client sync_service.EthClient) (*Ethereum, error) {
113+
func New(stack *node.Node, config *ethconfig.Config, l1Client l1.Client) (*Ethereum, error) {
113114
// Ensure configuration values are compatible and sane
114115
if config.SyncMode == downloader.LightSync {
115116
return nil, errors.New("can't run eth.Ethereum in light sync mode, use les.LightEthereum")

rollup/da_syncer/blob_client/beacon_node_client.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ import (
1212

1313
"github.com/scroll-tech/go-ethereum/common"
1414
"github.com/scroll-tech/go-ethereum/crypto/kzg4844"
15-
"github.com/scroll-tech/go-ethereum/rollup/rollup_sync_service"
1615
)
1716

1817
type BeaconNodeClient struct {
1918
apiEndpoint string
20-
l1Client *rollup_sync_service.L1Client
2119
genesisTime uint64
2220
secondsPerSlot uint64
2321
}
@@ -28,7 +26,7 @@ var (
2826
beaconNodeBlobEndpoint = "/eth/v1/beacon/blob_sidecars"
2927
)
3028

31-
func NewBeaconNodeClient(apiEndpoint string, l1Client *rollup_sync_service.L1Client) (*BeaconNodeClient, error) {
29+
func NewBeaconNodeClient(apiEndpoint string) (*BeaconNodeClient, error) {
3230
// get genesis time
3331
genesisPath, err := url.JoinPath(apiEndpoint, beaconNodeGenesisEndpoint)
3432
if err != nil {
@@ -94,19 +92,13 @@ func NewBeaconNodeClient(apiEndpoint string, l1Client *rollup_sync_service.L1Cli
9492

9593
return &BeaconNodeClient{
9694
apiEndpoint: apiEndpoint,
97-
l1Client: l1Client,
9895
genesisTime: genesisTime,
9996
secondsPerSlot: secondsPerSlot,
10097
}, nil
10198
}
10299

103-
func (c *BeaconNodeClient) GetBlobByVersionedHashAndBlockNumber(ctx context.Context, versionedHash common.Hash, blockNumber uint64) (*kzg4844.Blob, error) {
104-
// get block timestamp to calculate slot
105-
header, err := c.l1Client.GetHeaderByNumber(blockNumber)
106-
if err != nil {
107-
return nil, fmt.Errorf("failed to get header by number, err: %w", err)
108-
}
109-
slot := (header.Time - c.genesisTime) / c.secondsPerSlot
100+
func (c *BeaconNodeClient) GetBlobByVersionedHashAndBlockTime(ctx context.Context, versionedHash common.Hash, blockTime uint64) (*kzg4844.Blob, error) {
101+
slot := (blockTime - c.genesisTime) / c.secondsPerSlot
110102

111103
// get blob sidecar for slot
112104
blobSidecarPath, err := url.JoinPath(c.apiEndpoint, beaconNodeBlobEndpoint, fmt.Sprintf("%d", slot))
@@ -156,7 +148,7 @@ func (c *BeaconNodeClient) GetBlobByVersionedHashAndBlockNumber(ctx context.Cont
156148
}
157149
}
158150

159-
return nil, fmt.Errorf("missing blob %v in slot %d, block number %d", versionedHash, slot, blockNumber)
151+
return nil, fmt.Errorf("missing blob %v in slot %d", versionedHash, slot)
160152
}
161153

162154
type GenesisResp struct {

rollup/da_syncer/blob_client/blob_client.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const (
1717
)
1818

1919
type BlobClient interface {
20-
GetBlobByVersionedHashAndBlockNumber(ctx context.Context, versionedHash common.Hash, blockNumber uint64) (*kzg4844.Blob, error)
20+
GetBlobByVersionedHashAndBlockTime(ctx context.Context, versionedHash common.Hash, blockTime uint64) (*kzg4844.Blob, error)
2121
}
2222

2323
type BlobClients struct {
@@ -32,13 +32,13 @@ func NewBlobClients(blobClients ...BlobClient) *BlobClients {
3232
}
3333
}
3434

35-
func (c *BlobClients) GetBlobByVersionedHashAndBlockNumber(ctx context.Context, versionedHash common.Hash, blockNumber uint64) (*kzg4844.Blob, error) {
35+
func (c *BlobClients) GetBlobByVersionedHashAndBlockTime(ctx context.Context, versionedHash common.Hash, blockTime uint64) (*kzg4844.Blob, error) {
3636
if len(c.list) == 0 {
3737
return nil, fmt.Errorf("BlobClients.GetBlobByVersionedHash: list of BlobClients is empty")
3838
}
3939

4040
for i := 0; i < len(c.list); i++ {
41-
blob, err := c.list[c.curPos].GetBlobByVersionedHashAndBlockNumber(ctx, versionedHash, blockNumber)
41+
blob, err := c.list[c.curPos].GetBlobByVersionedHashAndBlockTime(ctx, versionedHash, blockTime)
4242
if err == nil {
4343
return blob, nil
4444
}

rollup/da_syncer/blob_client/blob_scan_client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func NewBlobScanClient(apiEndpoint string) *BlobScanClient {
2626
}
2727
}
2828

29-
func (c *BlobScanClient) GetBlobByVersionedHashAndBlockNumber(ctx context.Context, versionedHash common.Hash, blockNumber uint64) (*kzg4844.Blob, error) {
29+
func (c *BlobScanClient) GetBlobByVersionedHashAndBlockTime(ctx context.Context, versionedHash common.Hash, blockTime uint64) (*kzg4844.Blob, error) {
3030
// blobscan api docs https://api.blobscan.com/#/blobs/blob-getByBlobId
3131
path, err := url.JoinPath(c.apiEndpoint, versionedHash.String())
3232
if err != nil {

rollup/da_syncer/blob_client/block_native_client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func NewBlockNativeClient(apiEndpoint string) *BlockNativeClient {
2424
}
2525
}
2626

27-
func (c *BlockNativeClient) GetBlobByVersionedHashAndBlockNumber(ctx context.Context, versionedHash common.Hash, blockNumber uint64) (*kzg4844.Blob, error) {
27+
func (c *BlockNativeClient) GetBlobByVersionedHashAndBlockTime(ctx context.Context, versionedHash common.Hash, blockTime uint64) (*kzg4844.Blob, error) {
2828
// blocknative api docs https://docs.blocknative.com/blocknative-data-archive/blob-archive
2929
path, err := url.JoinPath(c.apiEndpoint, versionedHash.String())
3030
if err != nil {

0 commit comments

Comments
 (0)