Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(store/v2): remove cosmos-db depdency from store #19229

Merged
merged 12 commits into from
Jan 29, 2024
6 changes: 3 additions & 3 deletions store/commitment/iavl/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package iavl
import (
"fmt"

dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/iavl"
ics23 "github.com/cosmos/ics23/go"

log "cosmossdk.io/log"
"cosmossdk.io/store/v2/commitment"
dbm "cosmossdk.io/store/v2/db"
)

var _ commitment.Tree = (*IavlTree)(nil)
Expand All @@ -19,8 +19,8 @@ type IavlTree struct {
}

// NewIavlTree creates a new IavlTree instance.
func NewIavlTree(db dbm.DB, logger log.Logger, cfg *Config) *IavlTree {
tree := iavl.NewMutableTree(db, cfg.CacheSize, cfg.SkipFastStorageUpgrade, logger)
func NewIavlTree(db dbm.RawDB, logger log.Logger, cfg *Config) *IavlTree {
tree := iavl.NewMutableTree(dbm.NewWrapper(db), cfg.CacheSize, cfg.SkipFastStorageUpgrade, logger)
return &IavlTree{
tree: tree,
}
Expand Down
4 changes: 2 additions & 2 deletions store/commitment/iavl/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package iavl
import (
"testing"

dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"cosmossdk.io/log"
"cosmossdk.io/store/v2/commitment"
dbm "cosmossdk.io/store/v2/db"
)

func TestCommitterSuite(t *testing.T) {
s := &commitment.CommitStoreTestSuite{
NewStore: func(db dbm.DB, storeKeys []string, logger log.Logger) (*commitment.CommitStore, error) {
NewStore: func(db dbm.RawDB, storeKeys []string, logger log.Logger) (*commitment.CommitStore, error) {
multiTrees := make(map[string]commitment.Tree)
cfg := DefaultConfig()
for _, storeKey := range storeKeys {
Expand Down
47 changes: 24 additions & 23 deletions store/commitment/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"io"
"math"

dbm "github.com/cosmos/cosmos-db"
protoio "github.com/cosmos/gogoproto/io"

"cosmossdk.io/log"
"cosmossdk.io/store/v2"
dbm "cosmossdk.io/store/v2/db"
"cosmossdk.io/store/v2/internal/encoding"
"cosmossdk.io/store/v2/proof"
"cosmossdk.io/store/v2/snapshots"
snapshotstypes "cosmossdk.io/store/v2/snapshots/types"
)
Expand All @@ -23,7 +24,7 @@ const (
)

var (
_ store.Committer = (*CommitStore)(nil)
_ dbm.Committer = (*CommitStore)(nil)
_ snapshots.CommitSnapshotter = (*CommitStore)(nil)
)

Expand All @@ -34,12 +35,12 @@ var (
// and trees.
type CommitStore struct {
logger log.Logger
db dbm.DB
db dbm.RawDB
multiTrees map[string]Tree
}

// NewCommitStore creates a new CommitStore instance.
func NewCommitStore(multiTrees map[string]Tree, db dbm.DB, logger log.Logger) (*CommitStore, error) {
func NewCommitStore(multiTrees map[string]Tree, db dbm.RawDB, logger log.Logger) (*CommitStore, error) {
return &CommitStore{
logger: logger,
db: db,
Expand Down Expand Up @@ -67,19 +68,19 @@ func (c *CommitStore) WriteBatch(cs *store.Changeset) error {
return nil
}

func (c *CommitStore) WorkingCommitInfo(version uint64) *store.CommitInfo {
storeInfos := make([]store.StoreInfo, 0, len(c.multiTrees))
func (c *CommitStore) WorkingCommitInfo(version uint64) *proof.CommitInfo {
storeInfos := make([]proof.StoreInfo, 0, len(c.multiTrees))
for storeKey, tree := range c.multiTrees {
storeInfos = append(storeInfos, store.StoreInfo{
storeInfos = append(storeInfos, proof.StoreInfo{
Name: storeKey,
CommitID: store.CommitID{
CommitID: proof.CommitID{
Version: version,
Hash: tree.WorkingHash(),
},
})
}

return &store.CommitInfo{
return &proof.CommitInfo{
Version: version,
StoreInfos: storeInfos,
}
Expand Down Expand Up @@ -129,15 +130,15 @@ func (c *CommitStore) LoadVersion(targetVersion uint64) error {

// If the target version is greater than the latest version, it is the snapshot
// restore case, we should create a new commit info for the target version.
var cInfo *store.CommitInfo
var cInfo *proof.CommitInfo
if targetVersion > latestVersion {
cInfo = c.WorkingCommitInfo(targetVersion)
}

return c.flushCommitInfo(targetVersion, cInfo)
}

func (c *CommitStore) GetCommitInfo(version uint64) (*store.CommitInfo, error) {
func (c *CommitStore) GetCommitInfo(version uint64) (*proof.CommitInfo, error) {
key := []byte(fmt.Sprintf(commitInfoKeyFmt, version))
value, err := c.db.Get(key)
if err != nil {
Expand All @@ -147,15 +148,15 @@ func (c *CommitStore) GetCommitInfo(version uint64) (*store.CommitInfo, error) {
return nil, nil
}

cInfo := &store.CommitInfo{}
cInfo := &proof.CommitInfo{}
if err := cInfo.Unmarshal(value); err != nil {
return nil, err
}

return cInfo, nil
}

func (c *CommitStore) flushCommitInfo(version uint64, cInfo *store.CommitInfo) error {
func (c *CommitStore) flushCommitInfo(version uint64, cInfo *proof.CommitInfo) error {
batch := c.db.NewBatch()
if cInfo != nil {
cInfoKey := []byte(fmt.Sprintf(commitInfoKeyFmt, version))
Expand All @@ -180,14 +181,14 @@ func (c *CommitStore) flushCommitInfo(version uint64, cInfo *store.CommitInfo) e
return batch.WriteSync()
}

func (c *CommitStore) Commit(version uint64) (*store.CommitInfo, error) {
storeInfos := make([]store.StoreInfo, 0, len(c.multiTrees))
func (c *CommitStore) Commit(version uint64) (*proof.CommitInfo, error) {
storeInfos := make([]proof.StoreInfo, 0, len(c.multiTrees))

for storeKey, tree := range c.multiTrees {
// If a commit event execution is interrupted, a new iavl store's version
// will be larger than the RMS's metadata, when the block is replayed, we
// should avoid committing that iavl store again.
var commitID store.CommitID
var commitID proof.CommitID
if tree.GetLatestVersion() >= version {
commitID.Version = version
commitID.Hash = tree.Hash()
Expand All @@ -196,18 +197,18 @@ func (c *CommitStore) Commit(version uint64) (*store.CommitInfo, error) {
if err != nil {
return nil, err
}
commitID = store.CommitID{
commitID = proof.CommitID{
Version: version,
Hash: hash,
}
}
storeInfos = append(storeInfos, store.StoreInfo{
storeInfos = append(storeInfos, proof.StoreInfo{
Name: storeKey,
CommitID: commitID,
})
}

cInfo := &store.CommitInfo{
cInfo := &proof.CommitInfo{
Version: version,
StoreInfos: storeInfos,
}
Expand All @@ -229,13 +230,13 @@ func (c *CommitStore) SetInitialVersion(version uint64) error {
return nil
}

func (c *CommitStore) GetProof(storeKey string, version uint64, key []byte) ([]store.CommitmentOp, error) {
func (c *CommitStore) GetProof(storeKey string, version uint64, key []byte) ([]proof.CommitmentOp, error) {
tree, ok := c.multiTrees[storeKey]
if !ok {
return nil, fmt.Errorf("store %s not found", storeKey)
}

proof, err := tree.GetProof(version, key)
iProof, err := tree.GetProof(version, key)
if err != nil {
return nil, err
}
Expand All @@ -246,13 +247,13 @@ func (c *CommitStore) GetProof(storeKey string, version uint64, key []byte) ([]s
if cInfo == nil {
return nil, fmt.Errorf("commit info not found for version %d", version)
}
commitOp := store.NewIAVLCommitmentOp(key, proof)
commitOp := proof.NewIAVLCommitmentOp(key, iProof)
_, storeCommitmentOp, err := cInfo.GetStoreProof(storeKey)
if err != nil {
return nil, err
}

return []store.CommitmentOp{commitOp, *storeCommitmentOp}, nil
return []proof.CommitmentOp{commitOp, *storeCommitmentOp}, nil
}

func (c *CommitStore) Get(storeKey string, version uint64, key []byte) ([]byte, error) {
Expand Down
4 changes: 2 additions & 2 deletions store/commitment/store_test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"io"
"sync"

dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/suite"

"cosmossdk.io/log"
"cosmossdk.io/store/v2"
dbm "cosmossdk.io/store/v2/db"
"cosmossdk.io/store/v2/snapshots"
snapshotstypes "cosmossdk.io/store/v2/snapshots/types"
)
Expand All @@ -23,7 +23,7 @@ const (
type CommitStoreTestSuite struct {
suite.Suite

NewStore func(db dbm.DB, storeKeys []string, logger log.Logger) (*CommitStore, error)
NewStore func(db dbm.RawDB, storeKeys []string, logger log.Logger) (*CommitStore, error)
}

func (s *CommitStoreTestSuite) TestSnapshotter() {
Expand Down
2 changes: 1 addition & 1 deletion store/batch.go → store/db/batch.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package store
package db

// Batch is a write-only database that commits changes to the underlying database
// when Write is called. A batch cannot be used concurrently.
Expand Down
16 changes: 9 additions & 7 deletions store/database.go → store/db/database.go
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package store
package db

import (
"io"

corestore "cosmossdk.io/core/store"
"cosmossdk.io/store/v2"
"cosmossdk.io/store/v2/proof"
)

// Reader wraps the Has and Get method of a backing data store.
Expand Down Expand Up @@ -53,7 +55,7 @@ type VersionedDatabase interface {
Iterator(storeKey string, version uint64, start, end []byte) (corestore.Iterator, error)
ReverseIterator(storeKey string, version uint64, start, end []byte) (corestore.Iterator, error)

ApplyChangeset(version uint64, cs *Changeset) error
ApplyChangeset(version uint64, cs *store.Changeset) error

// Prune attempts to prune all versions up to and including the provided
// version argument. The operation should be idempotent. An error should be
Expand All @@ -68,10 +70,10 @@ type VersionedDatabase interface {
// Committer defines an API for committing state.
type Committer interface {
// WriteBatch writes a batch of key-value pairs to the tree.
WriteBatch(cs *Changeset) error
WriteBatch(cs *store.Changeset) error

// WorkingCommitInfo returns the CommitInfo for the working tree.
WorkingCommitInfo(version uint64) *CommitInfo
WorkingCommitInfo(version uint64) *proof.CommitInfo

// GetLatestVersion returns the latest version.
GetLatestVersion() (uint64, error)
Expand All @@ -80,10 +82,10 @@ type Committer interface {
LoadVersion(targetVersion uint64) error

// Commit commits the working tree to the database.
Commit(version uint64) (*CommitInfo, error)
Commit(version uint64) (*proof.CommitInfo, error)

// GetProof returns the proof of existence or non-existence for the given key.
GetProof(storeKey string, version uint64, key []byte) ([]CommitmentOp, error)
GetProof(storeKey string, version uint64, key []byte) ([]proof.CommitmentOp, error)

// Get returns the value for the given key at the given version.
//
Expand All @@ -95,7 +97,7 @@ type Committer interface {
SetInitialVersion(version uint64) error

// GetCommitInfo returns the CommitInfo for the given version.
GetCommitInfo(version uint64) (*CommitInfo, error)
GetCommitInfo(version uint64) (*proof.CommitInfo, error)

// Prune attempts to prune all versions up to and including the provided
// version argument. The operation should be idempotent. An error should be
Expand Down
Loading
Loading