Skip to content

Commit

Permalink
feat: decouple cosmos-db (backport #874) (#887)
Browse files Browse the repository at this point in the history
Co-authored-by: cool-developer <51834436+cool-develope@users.noreply.github.com>
Co-authored-by: Cool Developer <cool199966@outlook.com>
  • Loading branch information
3 people authored Feb 21, 2024
1 parent 7bc76b0 commit e019f74
Show file tree
Hide file tree
Showing 31 changed files with 856 additions and 262 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ linters:
disable-all: true
enable:
- bodyclose
- depguard
- dogsled
- errcheck
- exportloopref
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ format:

# look into .golangci.yml for enabling / disabling linters
golangci_lint_cmd=golangci-lint
golangci_version=v1.51.2
golangci_version=v1.55.2

lint:
@echo "--> Running linter"
Expand Down
11 changes: 6 additions & 5 deletions basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"testing"

"cosmossdk.io/log"
db "github.com/cosmos/cosmos-db"
iavlrand "github.com/cosmos/iavl/internal/rand"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

dbm "github.com/cosmos/iavl/db"
iavlrand "github.com/cosmos/iavl/internal/rand"
)

func TestBasic(t *testing.T) {
Expand Down Expand Up @@ -236,7 +237,7 @@ func TestUnit(t *testing.T) {
expectRemove(t11, 3, "((1 2) (4 5))")
}

func TestRemove(t *testing.T) {
func TestRemove(_ *testing.T) {
keyLen, dataLen := 16, 40

size := 10000
Expand Down Expand Up @@ -432,7 +433,7 @@ func TestIterateRange(t *testing.T) {
}

func TestPersistence(t *testing.T) {
db := db.NewMemDB()
db := dbm.NewMemDB()

// Create some random key value pairs
records := make(map[string]string)
Expand Down Expand Up @@ -495,7 +496,7 @@ func TestProof(t *testing.T) {
}

func TestTreeProof(t *testing.T) {
db := db.NewMemDB()
db := dbm.NewMemDB()
tree := NewMutableTree(db, 100, false, log.NewNopLogger())
hash := tree.Hash()
assert.Equal(t, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", hex.EncodeToString(hash))
Expand Down
2 changes: 1 addition & 1 deletion batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package iavl
import (
"sync"

dbm "github.com/cosmos/cosmos-db"
dbm "github.com/cosmos/iavl/db"
)

// BatchWithFlusher is a wrapper
Expand Down
9 changes: 5 additions & 4 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"path/filepath"
"testing"

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

dbm "github.com/cosmos/iavl/db"
)

func cleanupDBDir(dir, name string) {
Expand All @@ -27,16 +28,16 @@ func makeKey(n uint16) []byte {
}

func TestBatchWithFlusher(t *testing.T) {
testedBackends := []dbm.BackendType{
dbm.GoLevelDBBackend,
testedBackends := []string{
"goleveldb",
}

for _, backend := range testedBackends {
testBatchWithFlusher(t, backend)
}
}

func testBatchWithFlusher(t *testing.T, backend dbm.BackendType) {
func testBatchWithFlusher(t *testing.T, backend string) {
name := fmt.Sprintf("test_%x", randstr(12))
dir := t.TempDir()
db, err := dbm.NewDB(name, backend, dir)
Expand Down
16 changes: 8 additions & 8 deletions benchmarks/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"cosmossdk.io/log"
"github.com/stretchr/testify/require"

db "github.com/cosmos/cosmos-db"
"github.com/cosmos/iavl"
dbm "github.com/cosmos/iavl/db"
)

const historySize = 20
Expand All @@ -26,7 +26,7 @@ func randBytes(length int) []byte {
return key
}

func prepareTree(b *testing.B, db db.DB, size, keyLen, dataLen int) (*iavl.MutableTree, [][]byte) {
func prepareTree(b *testing.B, db dbm.DB, size, keyLen, dataLen int) (*iavl.MutableTree, [][]byte) {
t := iavl.NewMutableTree(db, size, false, log.NewNopLogger())
keys := make([][]byte, size)

Expand Down Expand Up @@ -147,7 +147,7 @@ func runIterationSlow(b *testing.B, t *iavl.MutableTree, expectedSize int) {
}
}

func iterate(b *testing.B, itr db.Iterator, expectedSize int) {
func iterate(b *testing.B, itr dbm.Iterator, expectedSize int) {
b.StartTimer()
keyValuePairs := make([][][]byte, 0, expectedSize)
for i := 0; i < expectedSize && itr.Valid(); i++ {
Expand Down Expand Up @@ -259,7 +259,7 @@ func BenchmarkRandomBytes(b *testing.B) {
}

type benchmark struct {
dbType db.BackendType
dbType string
initSize, blockSize int
keyLen, dataLen int
}
Expand Down Expand Up @@ -330,7 +330,7 @@ func runBenchmarks(b *testing.B, benchmarks []benchmark) {

// prepare a dir for the db and cleanup afterwards
dirName := fmt.Sprintf("./%s-db", prefix)
if bb.dbType == db.RocksDBBackend {
if bb.dbType == "rocksdb" {
_ = os.Mkdir(dirName, 0o755)
}

Expand All @@ -343,11 +343,11 @@ func runBenchmarks(b *testing.B, benchmarks []benchmark) {

// note that "" leads to nil backing db!
var (
d db.DB
d dbm.DB
err error
)
if bb.dbType != "nodb" {
d, err = db.NewDB("test", bb.dbType, dirName)
d, err = dbm.NewDB("test", bb.dbType, dirName)

if err != nil {
if strings.Contains(err.Error(), "unknown db_backend") {
Expand Down Expand Up @@ -376,7 +376,7 @@ func memUseMB() float64 {
return mb
}

func runSuite(b *testing.B, d db.DB, initSize, blockSize, keyLen, dataLen int) {
func runSuite(b *testing.B, d dbm.DB, initSize, blockSize, keyLen, dataLen int) {
// measure mem usage
runtime.GC()
init := memUseMB()
Expand Down
8 changes: 5 additions & 3 deletions benchmarks/cosmos-exim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (

"cosmossdk.io/log"
tmdb "github.com/cosmos/cosmos-db"

"github.com/cosmos/iavl"
idbm "github.com/cosmos/iavl/db"
)

// stores is the list of stores in the CosmosHub database
Expand Down Expand Up @@ -91,7 +93,7 @@ func runExport(dbPath string) (int64, map[string][]*iavl.ExportNode, error) {
if err != nil {
return 0, nil, err
}
tree := iavl.NewMutableTree(tmdb.NewPrefixDB(ldb, []byte("s/k:main/")), 0, false, log.NewNopLogger())
tree := iavl.NewMutableTree(idbm.NewWrapper(tmdb.NewPrefixDB(ldb, []byte("s/k:main/"))), 0, false, log.NewNopLogger())
version, err := tree.LoadVersion(0)
if err != nil {
return 0, nil, err
Expand All @@ -103,7 +105,7 @@ func runExport(dbPath string) (int64, map[string][]*iavl.ExportNode, error) {
totalStats := Stats{}
for _, name := range stores {
db := tmdb.NewPrefixDB(ldb, []byte("s/k:"+name+"/"))
tree := iavl.NewMutableTree(db, 0, false, log.NewNopLogger())
tree := iavl.NewMutableTree(idbm.NewWrapper(db), 0, false, log.NewNopLogger())

stats := Stats{}
export := make([]*iavl.ExportNode, 0, 100000)
Expand Down Expand Up @@ -168,7 +170,7 @@ func runImport(version int64, exports map[string][]*iavl.ExportNode) error {
if err != nil {
return err
}
newTree := iavl.NewMutableTree(newDB, 0, false, log.NewNopLogger())
newTree := iavl.NewMutableTree(idbm.NewWrapper(newDB), 0, false, log.NewNopLogger())
importer, err := newTree.Import(version)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion cmd/iaviewer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
dbm "github.com/cosmos/cosmos-db"

"github.com/cosmos/iavl"
idbm "github.com/cosmos/iavl/db"
)

// TODO: make this configurable?
Expand Down Expand Up @@ -122,7 +123,7 @@ func ReadTree(dir string, version int, prefix []byte) (*iavl.MutableTree, error)
db = dbm.NewPrefixDB(db, prefix)
}

tree := iavl.NewMutableTree(db, DefaultCacheSize, false, log.NewLogger(os.Stdout))
tree := iavl.NewMutableTree(idbm.NewWrapper(db), DefaultCacheSize, false, log.NewLogger(os.Stdout))
ver, err := tree.LoadVersion(int64(version))
fmt.Printf("Got version: %d\n", ver)
return tree, err
Expand Down
Loading

0 comments on commit e019f74

Please sign in to comment.