|
2 | 2 | package db |
3 | 3 |
|
4 | 4 | import ( |
| 5 | + "fmt" |
| 6 | + |
5 | 7 | dbm "github.com/cometbft/cometbft-db" |
| 8 | + cmtconfig "github.com/cometbft/cometbft/config" |
6 | 9 | "github.com/cometbft/cometbft/node" |
| 10 | + cmtnode "github.com/cometbft/cometbft/node" |
| 11 | + "github.com/cometbft/cometbft/state" |
7 | 12 |
|
8 | 13 | "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/db/badger" |
9 | 14 | ) |
10 | 15 |
|
11 | | -// GetBackendName returns the currently configured CometBFT database backend. |
12 | | -func GetBackendName() string { |
| 16 | +// BackendName returns the currently configured CometBFT database backend. |
| 17 | +func BackendName() string { |
13 | 18 | return badger.BackendName |
14 | 19 | } |
15 | 20 |
|
16 | | -// GetProvider returns the currently configured CometBFT DBProvider. |
17 | | -func GetProvider() (node.DBProvider, error) { |
| 21 | +// Provider returns the currently configured CometBFT DBProvider. |
| 22 | +func Provider() (node.DBProvider, error) { |
18 | 23 | return badger.DBProvider, nil |
19 | 24 | } |
20 | 25 |
|
21 | 26 | // New constructs a new CometBFT DB with the configured backend. |
22 | 27 | func New(fn string, noSuffix bool) (dbm.DB, error) { |
23 | 28 | return badger.New(fn, noSuffix) |
24 | 29 | } |
| 30 | + |
| 31 | +// OpenBlockstoreDB opens a CometBFT managed blockstore DB. |
| 32 | +// |
| 33 | +// This function is a hack as CometBFT does not expose a way to access the underlying databases. |
| 34 | +func OpenBlockstoreDB(provider cmtnode.DBProvider, cfg *cmtconfig.Config) (dbm.DB, error) { |
| 35 | + // NOTE: DBContext uses a full CometBFT config but the only thing that is actually used |
| 36 | + // is the data dir field. |
| 37 | + db, err := provider(&cmtnode.DBContext{ID: "blockstore", Config: cfg}) |
| 38 | + if err != nil { |
| 39 | + return nil, fmt.Errorf("failed to open blockstore: %w", err) |
| 40 | + } |
| 41 | + |
| 42 | + return db, nil |
| 43 | +} |
| 44 | + |
| 45 | +// OpenStateDB opens a CometBFT managed state DB. |
| 46 | +// |
| 47 | +// This function is a hack as CometBFT does not expose a way to access the underlying databases. |
| 48 | +func OpenStateDB(provider cmtnode.DBProvider, cfg *cmtconfig.Config) (dbm.DB, error) { |
| 49 | + // NOTE: DBContext uses a full CometBFT config but the only thing that is actually used |
| 50 | + // is the data dir field. |
| 51 | + db, err := provider(&cmtnode.DBContext{ID: "state", Config: cfg}) |
| 52 | + if err != nil { |
| 53 | + return nil, fmt.Errorf("failed to open state db: %w", err) |
| 54 | + } |
| 55 | + |
| 56 | + return db, nil |
| 57 | +} |
| 58 | + |
| 59 | +// OpenStateStore constructs a new state store using default options. |
| 60 | +func OpenStateStore(stateDB dbm.DB) state.Store { |
| 61 | + return state.NewStore(stateDB, state.StoreOptions{}) |
| 62 | +} |
0 commit comments