@@ -4,23 +4,25 @@ import (
44 "context"
55 "fmt"
66
7+ "github.com/sirupsen/logrus"
8+
79 "github.com/ethpandaops/dora/blockdb/pebble"
810 "github.com/ethpandaops/dora/blockdb/s3"
11+ "github.com/ethpandaops/dora/blockdb/tiered"
912 "github.com/ethpandaops/dora/blockdb/types"
1013 dtypes "github.com/ethpandaops/dora/types"
1114)
1215
13- // BlockDb wraps the underlying storage engine for both beacon block data
14- // and execution data.
16+ // BlockDb is the main wrapper for block database operations.
1517type BlockDb struct {
1618 engine types.BlockDbEngine
1719 execEngine types.ExecDataEngine // nil if engine doesn't support exec data
1820}
1921
20- // GlobalBlockDb is the global singleton BlockDb instance.
22+ // GlobalBlockDb is the global block database instance.
2123var GlobalBlockDb * BlockDb
2224
23- // InitWithPebble initializes the global BlockDb with a Pebble backend .
25+ // InitWithPebble initializes the block database with Pebble (local) storage .
2426func InitWithPebble (config dtypes.PebbleBlockDBConfig ) error {
2527 engine , err := pebble .NewPebbleEngine (config )
2628 if err != nil {
@@ -41,7 +43,7 @@ func InitWithPebble(config dtypes.PebbleBlockDBConfig) error {
4143 return nil
4244}
4345
44- // InitWithS3 initializes the global BlockDb with an S3 backend .
46+ // InitWithS3 initializes the block database with S3 (remote) storage .
4547func InitWithS3 (config dtypes.S3BlockDBConfig ) error {
4648 engine , err := s3 .NewS3Engine (config )
4749 if err != nil {
@@ -62,6 +64,27 @@ func InitWithS3(config dtypes.S3BlockDBConfig) error {
6264 return nil
6365}
6466
67+ // InitWithTiered initializes the block database with tiered storage (Pebble cache + S3 backend).
68+ func InitWithTiered (config dtypes.TieredBlockDBConfig , logger logrus.FieldLogger ) error {
69+ engine , err := tiered .NewTieredEngine (config , logger )
70+ if err != nil {
71+ return err
72+ }
73+
74+ db := & BlockDb {
75+ engine : engine ,
76+ }
77+
78+ // Check if tiered engine supports exec data
79+ if execEngine , ok := engine .(types.ExecDataEngine ); ok {
80+ db .execEngine = execEngine
81+ }
82+
83+ GlobalBlockDb = db
84+
85+ return nil
86+ }
87+
6588// GetEngine returns the underlying storage engine.
6689func (db * BlockDb ) GetEngine () types.BlockDbEngine {
6790 return db .engine
@@ -71,22 +94,59 @@ func (db *BlockDb) Close() error {
7194 return db .engine .Close ()
7295}
7396
74- func (db * BlockDb ) GetBlock (ctx context.Context , slot uint64 , root []byte , parseBlock func (uint64 , []byte ) (interface {}, error )) (* types.BlockData , error ) {
75- return db .engine .GetBlock (ctx , slot , root , parseBlock )
76- }
77-
78- func (db * BlockDb ) AddBlock (ctx context.Context , slot uint64 , root []byte , header_ver uint64 , header_data []byte , body_ver uint64 , body_data []byte ) (bool , error ) {
97+ // GetBlock retrieves block data with selective loading based on flags.
98+ func (db * BlockDb ) GetBlock (
99+ ctx context.Context ,
100+ slot uint64 ,
101+ root []byte ,
102+ flags types.BlockDataFlags ,
103+ parseBlock func (uint64 , []byte ) (any , error ),
104+ parsePayload func (uint64 , []byte ) (any , error ),
105+ ) (* types.BlockData , error ) {
106+ return db .engine .GetBlock (ctx , slot , root , flags , parseBlock , parsePayload )
107+ }
108+
109+ // GetStoredComponents returns which components exist for a block.
110+ func (db * BlockDb ) GetStoredComponents (ctx context.Context , slot uint64 , root []byte ) (types.BlockDataFlags , error ) {
111+ return db .engine .GetStoredComponents (ctx , slot , root )
112+ }
113+
114+ // AddBlock stores block data. Returns (added, updated, error).
115+ func (db * BlockDb ) AddBlock (
116+ ctx context.Context ,
117+ slot uint64 ,
118+ root []byte ,
119+ headerVer uint64 ,
120+ headerData []byte ,
121+ bodyVer uint64 ,
122+ bodyData []byte ,
123+ payloadVer uint64 ,
124+ payloadData []byte ,
125+ balVer uint64 ,
126+ balData []byte ,
127+ ) (bool , bool , error ) {
79128 return db .engine .AddBlock (ctx , slot , root , func () (* types.BlockData , error ) {
80129 return & types.BlockData {
81- HeaderVersion : header_ver ,
82- HeaderData : header_data ,
83- BodyVersion : body_ver ,
84- BodyData : body_data ,
130+ HeaderVersion : headerVer ,
131+ HeaderData : headerData ,
132+ BodyVersion : bodyVer ,
133+ BodyData : bodyData ,
134+ PayloadVersion : payloadVer ,
135+ PayloadData : payloadData ,
136+ BalVersion : balVer ,
137+ BalData : balData ,
85138 }, nil
86139 })
87140}
88141
89- func (db * BlockDb ) AddBlockWithCallback (ctx context.Context , slot uint64 , root []byte , dataCb func () (* types.BlockData , error )) (bool , error ) {
142+ // AddBlockWithCallback stores block data using a callback for deferred data loading.
143+ // Returns (added, updated, error).
144+ func (db * BlockDb ) AddBlockWithCallback (
145+ ctx context.Context ,
146+ slot uint64 ,
147+ root []byte ,
148+ dataCb func () (* types.BlockData , error ),
149+ ) (bool , bool , error ) {
90150 return db .engine .AddBlock (ctx , slot , root , dataCb )
91151}
92152
0 commit comments