forked from ledgerwatch/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
beacon_block_reader.go
203 lines (172 loc) · 5.24 KB
/
beacon_block_reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package freezeblocks
import (
"bytes"
"context"
"fmt"
"sync"
libcommon "github.com/nebojsa94/erigon/erigon-lib/common"
"github.com/nebojsa94/erigon/erigon-lib/kv"
"github.com/nebojsa94/erigon/cl/clparams"
"github.com/nebojsa94/erigon/cl/cltypes"
"github.com/nebojsa94/erigon/cl/persistence"
"github.com/nebojsa94/erigon/cl/persistence/beacon_indicies"
"github.com/nebojsa94/erigon/cl/persistence/format/snapshot_format"
"github.com/pierrec/lz4"
)
var buffersPool = sync.Pool{
New: func() interface{} { return &bytes.Buffer{} },
}
var lz4ReaderPool = sync.Pool{
New: func() interface{} {
return lz4.NewReader(nil)
},
}
type BeaconSnapshotReader interface {
// ReadBlock reads the block at the given slot.
// If the block is not present, it returns nil.
ReadBlockBySlot(ctx context.Context, tx kv.Tx, slot uint64) (*cltypes.SignedBeaconBlock, error)
ReadBlockByRoot(ctx context.Context, tx kv.Tx, blockRoot libcommon.Hash) (*cltypes.SignedBeaconBlock, error)
ReadHeaderByRoot(ctx context.Context, tx kv.Tx, blockRoot libcommon.Hash) (*cltypes.SignedBeaconBlockHeader, error)
FrozenSlots() uint64
}
type beaconSnapshotReader struct {
sn *CaplinSnapshots
eth1Getter snapshot_format.ExecutionBlockReaderByNumber
beaconDB persistence.BlockSource
cfg *clparams.BeaconChainConfig
}
func NewBeaconSnapshotReader(snapshots *CaplinSnapshots, eth1Getter snapshot_format.ExecutionBlockReaderByNumber, beaconDB persistence.BlockSource, cfg *clparams.BeaconChainConfig) BeaconSnapshotReader {
return &beaconSnapshotReader{sn: snapshots, eth1Getter: eth1Getter, cfg: cfg, beaconDB: beaconDB}
}
func (r *beaconSnapshotReader) FrozenSlots() uint64 {
return r.sn.BlocksAvailable()
}
func (r *beaconSnapshotReader) ReadBlockBySlot(ctx context.Context, tx kv.Tx, slot uint64) (*cltypes.SignedBeaconBlock, error) {
view := r.sn.View()
defer view.Close()
var buf []byte
if slot > r.sn.BlocksAvailable() {
data, err := r.beaconDB.GetBlock(ctx, tx, slot)
return data.Data, err
}
if r.eth1Getter == nil {
return nil, nil
}
seg, ok := view.BeaconBlocksSegment(slot)
if !ok {
return nil, nil
}
if seg.idxSlot == nil {
return nil, nil
}
if slot < seg.idxSlot.BaseDataID() {
return nil, fmt.Errorf("slot %d is before the base data id %d", slot, seg.idxSlot.BaseDataID())
}
blockOffset := seg.idxSlot.OrdinalLookup(slot - seg.idxSlot.BaseDataID())
gg := seg.seg.MakeGetter()
gg.Reset(blockOffset)
if !gg.HasNext() {
return nil, nil
}
buf = buf[:0]
buf, _ = gg.Next(buf)
if len(buf) == 0 {
return nil, nil
}
// Decompress this thing
buffer := buffersPool.Get().(*bytes.Buffer)
defer buffersPool.Put(buffer)
buffer.Reset()
buffer.Write(buf)
lzReader := lz4ReaderPool.Get().(*lz4.Reader)
defer lz4ReaderPool.Put(lzReader)
lzReader.Reset(buffer)
// Use pooled buffers and readers to avoid allocations.
return snapshot_format.ReadBlockFromSnapshot(lzReader, r.eth1Getter, r.cfg)
}
func (r *beaconSnapshotReader) ReadBlockByRoot(ctx context.Context, tx kv.Tx, root libcommon.Hash) (*cltypes.SignedBeaconBlock, error) {
view := r.sn.View()
defer view.Close()
slot, err := beacon_indicies.ReadBlockSlotByBlockRoot(tx, root)
if err != nil {
return nil, err
}
if slot == nil {
return nil, nil
}
var buf []byte
if *slot > r.sn.BlocksAvailable() {
data, err := r.beaconDB.GetBlock(ctx, tx, *slot)
return data.Data, err
}
if r.eth1Getter == nil {
return nil, nil
}
// Find canonical block
canonicalBlockRoot, err := beacon_indicies.ReadCanonicalBlockRoot(tx, *slot)
if err != nil {
return nil, err
}
// root non-canonical? BAD
if canonicalBlockRoot != root {
return nil, nil
}
seg, ok := view.BeaconBlocksSegment(*slot)
if !ok {
return nil, nil
}
if seg.idxSlot == nil {
return nil, nil
}
if *slot < seg.idxSlot.BaseDataID() {
return nil, fmt.Errorf("slot %d is before the base data id %d", slot, seg.idxSlot.BaseDataID())
}
blockOffset := seg.idxSlot.OrdinalLookup(*slot - seg.idxSlot.BaseDataID())
gg := seg.seg.MakeGetter()
gg.Reset(blockOffset)
if !gg.HasNext() {
return nil, nil
}
buf = buf[:0]
buf, _ = gg.Next(buf)
if len(buf) == 0 {
return nil, nil
}
// Decompress this thing
buffer := buffersPool.Get().(*bytes.Buffer)
defer buffersPool.Put(buffer)
buffer.Reset()
buffer.Write(buf)
lzReader := lz4ReaderPool.Get().(*lz4.Reader)
defer lz4ReaderPool.Put(lzReader)
lzReader.Reset(buffer)
// Use pooled buffers and readers to avoid allocations.
return snapshot_format.ReadBlockFromSnapshot(lzReader, r.eth1Getter, r.cfg)
}
func (r *beaconSnapshotReader) ReadHeaderByRoot(ctx context.Context, tx kv.Tx, root libcommon.Hash) (*cltypes.SignedBeaconBlockHeader, error) {
view := r.sn.View()
defer view.Close()
slot, err := beacon_indicies.ReadBlockSlotByBlockRoot(tx, root)
if err != nil {
return nil, err
}
if slot == nil {
return nil, nil
}
if *slot > r.sn.BlocksAvailable() {
h, _, err := beacon_indicies.ReadSignedHeaderByBlockRoot(ctx, tx, root)
return h, err
}
// Find canonical block
canonicalBlockRoot, err := beacon_indicies.ReadCanonicalBlockRoot(tx, *slot)
if err != nil {
return nil, err
}
// root non-canonical? BAD
if canonicalBlockRoot != root {
return nil, nil
}
h, _, _, err := r.sn.ReadHeader(*slot)
// Use pooled buffers and readers to avoid allocations.
return h, err
}