feat: replace the cosmos-db.DB interface with core/store interface (#21450)

This commit is contained in:
cool-developer
2024-08-30 21:25:25 +00:00
committed by GitHub
parent f843f1a478
commit ce4fb98cb8
119 changed files with 553 additions and 550 deletions
+6 -7
View File
@@ -6,8 +6,7 @@ import (
"sort"
"sync"
dbm "github.com/cosmos/cosmos-db"
corestore "cosmossdk.io/core/store"
"cosmossdk.io/store/pruning/types"
storetypes "cosmossdk.io/store/types"
)
@@ -16,7 +15,7 @@ import (
// determining when to prune old heights of the store
// based on the strategy described by the pruning options.
type Manager struct {
db dbm.DB
db corestore.KVStoreWithBatch
logger storetypes.Logger
opts types.PruningOptions
snapshotInterval uint64
@@ -46,7 +45,7 @@ var pruneSnapshotHeightsKey = []byte("s/prunesnapshotheights")
// The returned manager uses a pruning strategy of "nothing" which
// keeps all heights. Users of the Manager may change the strategy
// by calling SetOptions.
func NewManager(db dbm.DB, logger storetypes.Logger) *Manager {
func NewManager(db corestore.KVStoreWithBatch, logger storetypes.Logger) *Manager {
return &Manager{
db: db,
logger: logger,
@@ -89,7 +88,7 @@ func (m *Manager) HandleSnapshotHeight(height int64) {
m.pruneSnapshotHeights = m.pruneSnapshotHeights[k-1:]
// flush the updates to disk so that they are not lost if crash happens.
if err := m.db.SetSync(pruneSnapshotHeightsKey, int64SliceToBytes(m.pruneSnapshotHeights)); err != nil {
if err := m.db.Set(pruneSnapshotHeightsKey, int64SliceToBytes(m.pruneSnapshotHeights)); err != nil {
panic(err)
}
}
@@ -137,7 +136,7 @@ func (m *Manager) GetPruningHeight(height int64) int64 {
}
// LoadSnapshotHeights loads the snapshot heights from the database as a crash recovery.
func (m *Manager) LoadSnapshotHeights(db dbm.DB) error {
func (m *Manager) LoadSnapshotHeights(db corestore.KVStoreWithBatch) error {
if m.opts.GetPruningStrategy() == types.PruningNothing {
return nil
}
@@ -156,7 +155,7 @@ func (m *Manager) LoadSnapshotHeights(db dbm.DB) error {
return nil
}
func loadPruningSnapshotHeights(db dbm.DB) ([]int64, error) {
func loadPruningSnapshotHeights(db corestore.KVStoreWithBatch) ([]int64, error) {
bz, err := db.Get(pruneSnapshotHeightsKey)
if err != nil {
return nil, fmt.Errorf("failed to get post-snapshot pruned heights: %w", err)
+1 -1
View File
@@ -202,7 +202,7 @@ func TestHandleSnapshotHeight_DbErr_Panic(t *testing.T) {
// Setup
dbMock := mock.NewMockDB(ctrl)
dbMock.EXPECT().SetSync(gomock.Any(), gomock.Any()).Return(errors.New(dbErr)).Times(1)
dbMock.EXPECT().Set(gomock.Any(), gomock.Any()).Return(errors.New(dbErr)).Times(1)
manager := pruning.NewManager(dbMock, log.NewNopLogger())
manager.SetOptions(types.NewPruningOptions(types.PruningEverything))