Skip to content

Commit 967c710

Browse files
committed
go/oasis-node/cmd/storage: Factor out db providers
Observe that migrate command now opens runtime light history with hasLocalStorage=true. This is intentional, to be consistent with the other callsite. When doing migration or offline pruning the node clearly needs to have local storage. In practice this is not relevant as it only affects behaviour of light history when the storage committee worker is running. Furthermore, once we move pruning and (re)-indexing of the runtime light history this half of the history.History api can be removed, including this variable.
1 parent 3182a76 commit 967c710

2 files changed

Lines changed: 92 additions & 51 deletions

File tree

go/oasis-node/cmd/storage/dbs.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package storage
2+
3+
import (
4+
"fmt"
5+
"path/filepath"
6+
7+
cmtCfg "github.com/cometbft/cometbft/config"
8+
"github.com/cometbft/cometbft/state"
9+
"github.com/cometbft/cometbft/store"
10+
11+
"github.com/oasisprotocol/oasis-core/go/common"
12+
"github.com/oasisprotocol/oasis-core/go/config"
13+
"github.com/oasisprotocol/oasis-core/go/consensus/cometbft/abci"
14+
cmtCommon "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/common"
15+
cmtDB "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/db"
16+
runtimeConfig "github.com/oasisprotocol/oasis-core/go/runtime/config"
17+
"github.com/oasisprotocol/oasis-core/go/runtime/history"
18+
"github.com/oasisprotocol/oasis-core/go/storage/api"
19+
)
20+
21+
func openConsensusNodeDB(dataDir string) (api.NodeDB, func(), error) {
22+
ldb, ndb, _, err := abci.InitStateStorage(
23+
&abci.ApplicationConfig{
24+
DataDir: filepath.Join(dataDir, cmtCommon.StateDir),
25+
StorageBackend: config.GlobalConfig.Storage.Backend,
26+
MemoryOnlyStorage: false,
27+
ReadOnlyStorage: false,
28+
DisableCheckpointer: true,
29+
},
30+
)
31+
if err != nil {
32+
return nil, nil, fmt.Errorf("failed to initialize ABCI storage backend: %w", err)
33+
}
34+
35+
// Close and Cleanup both only close NodeDB. Still closing both explicitly,
36+
// to prevent resource leaks if things change in the future.
37+
close := func() {
38+
ndb.Close()
39+
ldb.Cleanup()
40+
}
41+
42+
return ndb, close, nil
43+
}
44+
45+
func openConsensusBlockstore(dataDir string) (*store.BlockStore, error) {
46+
cmtConfig := cmtCfg.DefaultConfig()
47+
cmtConfig.SetRoot(filepath.Join(dataDir, cmtCommon.StateDir))
48+
49+
dbProvider, err := cmtDB.Provider()
50+
if err != nil {
51+
return nil, fmt.Errorf("failed to obtain db provider: %w", err)
52+
}
53+
54+
blockstoreDB, err := cmtDB.OpenBlockstoreDB(dbProvider, cmtConfig)
55+
if err != nil {
56+
return nil, fmt.Errorf("failed to open blockstore: %w", err)
57+
}
58+
blockstore := store.NewBlockStore(blockstoreDB)
59+
60+
return blockstore, nil
61+
}
62+
63+
func openConsensusStatestore(dataDir string) (state.Store, error) {
64+
cmtConfig := cmtCfg.DefaultConfig()
65+
cmtConfig.SetRoot(filepath.Join(dataDir, cmtCommon.StateDir))
66+
67+
dbProvider, err := cmtDB.Provider()
68+
if err != nil {
69+
return nil, fmt.Errorf("failed to obtain db provider: %w", err)
70+
}
71+
stateDB, err := cmtDB.OpenStateDB(dbProvider, cmtConfig)
72+
if err != nil {
73+
return nil, fmt.Errorf("failed to open state db: %w", err)
74+
}
75+
return cmtDB.OpenStateStore(stateDB), nil
76+
}
77+
78+
func openRuntimeLightHistory(dataDir string, rt common.Namespace) (history.History, error) {
79+
rtDir := runtimeConfig.GetRuntimeStateDir(dataDir, rt)
80+
history, err := history.New(rt, rtDir, history.NewNonePrunerFactory(), true)
81+
if err != nil {
82+
return nil, fmt.Errorf("failed to open new light history: %w", err)
83+
}
84+
return history, nil
85+
}

go/oasis-node/cmd/storage/storage.go

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,21 @@ import (
1212
"strings"
1313
"time"
1414

15-
cmtconfig "github.com/cometbft/cometbft/config"
16-
cmtBlockstore "github.com/cometbft/cometbft/store"
1715
badgerDB "github.com/dgraph-io/badger/v4"
1816
"github.com/spf13/cobra"
1917

2018
"github.com/oasisprotocol/oasis-core/go/common"
2119
"github.com/oasisprotocol/oasis-core/go/common/crypto/hash"
2220
"github.com/oasisprotocol/oasis-core/go/common/logging"
2321
"github.com/oasisprotocol/oasis-core/go/config"
24-
"github.com/oasisprotocol/oasis-core/go/consensus/cometbft/abci"
25-
cmtCommon "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/common"
2622
cmtConfig "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/config"
27-
cmtDB "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/db"
2823
cmtDBProvider "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/db/badger"
2924
cmdCommon "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common"
3025
roothash "github.com/oasisprotocol/oasis-core/go/roothash/api"
3126
"github.com/oasisprotocol/oasis-core/go/runtime/bundle"
3227
runtimeConfig "github.com/oasisprotocol/oasis-core/go/runtime/config"
3328
"github.com/oasisprotocol/oasis-core/go/runtime/history"
3429
"github.com/oasisprotocol/oasis-core/go/runtime/registry"
35-
"github.com/oasisprotocol/oasis-core/go/storage/api"
3630
db "github.com/oasisprotocol/oasis-core/go/storage/mkvs/db/api"
3731
"github.com/oasisprotocol/oasis-core/go/storage/mkvs/db/badger"
3832
"github.com/oasisprotocol/oasis-core/go/storage/mkvs/node"
@@ -195,9 +189,7 @@ func doMigrate(_ *cobra.Command, args []string) error {
195189
}
196190
err := func() error {
197191
runtimeDir := runtimeConfig.GetRuntimeStateDir(dataDir, rt)
198-
199-
prunerFactory := history.NewNonePrunerFactory()
200-
history, err := history.New(rt, runtimeDir, prunerFactory, false)
192+
history, err := openRuntimeLightHistory(dataDir, rt)
201193
if err != nil {
202194
return fmt.Errorf("error creating history provider: %w", err)
203195
}
@@ -417,30 +409,6 @@ func compactConsensusNodeDB(dataDir string) error {
417409
return ndb.Compact()
418410
}
419411

420-
func openConsensusNodeDB(dataDir string) (api.NodeDB, func(), error) {
421-
ldb, ndb, _, err := abci.InitStateStorage(
422-
&abci.ApplicationConfig{
423-
DataDir: filepath.Join(dataDir, cmtCommon.StateDir),
424-
StorageBackend: config.GlobalConfig.Storage.Backend,
425-
MemoryOnlyStorage: false,
426-
ReadOnlyStorage: false,
427-
DisableCheckpointer: true,
428-
},
429-
)
430-
if err != nil {
431-
return nil, nil, fmt.Errorf("failed to initialize ABCI storage backend: %w", err)
432-
}
433-
434-
// Close and Cleanup both only close NodeDB. Still closing both explicitly,
435-
// to prevent resource leaks if things change in the future.
436-
close := func() {
437-
ndb.Close()
438-
ldb.Cleanup()
439-
}
440-
441-
return ndb, close, nil
442-
}
443-
444412
func doPrune(_ *cobra.Command, args []string) error {
445413
if err := cmdCommon.Init(); err != nil {
446414
cmdCommon.EarlyLogAndExit(err)
@@ -554,11 +522,9 @@ func pruneConsensusNodeDB(ndb db.NodeDB, retainHeight uint64) error {
554522
// In case of no configured runtimes it returns max int64.
555523
func minReindexedHeight(dataDir string, runtimes []common.Namespace) (int64, error) {
556524
fetchLastReindexedHeight := func(runtimeID common.Namespace) (int64, error) {
557-
rtDir := runtimeConfig.GetRuntimeStateDir(dataDir, runtimeID)
558-
559-
history, err := history.New(runtimeID, rtDir, history.NewNonePrunerFactory(), true)
525+
history, err := openRuntimeLightHistory(dataDir, runtimeID)
560526
if err != nil {
561-
return 0, fmt.Errorf("failed to open new light history: %w", err)
527+
return 0, fmt.Errorf("failed to open runtime light history: %w", err)
562528
}
563529
defer history.Close()
564530

@@ -586,19 +552,10 @@ func minReindexedHeight(dataDir string, runtimes []common.Namespace) (int64, err
586552
}
587553

588554
func pruneCometDBs(dataDir string, retainHeight int64) error {
589-
cmtConfig := cmtconfig.DefaultConfig()
590-
cmtConfig.SetRoot(filepath.Join(dataDir, cmtCommon.StateDir))
591-
592-
dbProvider, err := cmtDB.Provider()
593-
if err != nil {
594-
return fmt.Errorf("failed to obtain db provider: %w", err)
595-
}
596-
597-
blockstoreDB, err := cmtDB.OpenBlockstoreDB(dbProvider, cmtConfig)
555+
blockstore, err := openConsensusBlockstore(dataDir)
598556
if err != nil {
599-
return fmt.Errorf("failed to open blockstore: %w", err)
557+
return fmt.Errorf("failed to open consensus blockstore: %w", err)
600558
}
601-
blockstore := cmtBlockstore.NewBlockStore(blockstoreDB)
602559
defer blockstore.Close()
603560

604561
// Mimic the upstream pruning logic from CometBFT
@@ -623,11 +580,10 @@ func pruneCometDBs(dataDir string, retainHeight int64) error {
623580
}
624581
logger.Info("blockstore pruning finished", "pruned", n)
625582

626-
stateDB, err := cmtDB.OpenStateDB(dbProvider, cmtConfig)
583+
state, err := openConsensusStatestore(dataDir)
627584
if err != nil {
628-
return fmt.Errorf("failed to open state db: %w", err)
585+
return fmt.Errorf("failed to open consensus state store: %w", err)
629586
}
630-
state := cmtDB.OpenStateStore(stateDB)
631587
defer state.Close()
632588

633589
logger.Info("pruning consensus states", "base", base, "retain_height", retainHeight)

0 commit comments

Comments
 (0)