Skip to content

Commit

Permalink
Revert "refactor: remove cosmos-db as a dep (backport #955) (#968)"
Browse files Browse the repository at this point in the history
This reverts commit 984d49b.
  • Loading branch information
cool-develope committed Jul 31, 2024
1 parent 984d49b commit f07f9d8
Show file tree
Hide file tree
Showing 41 changed files with 727 additions and 1,678 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
- [#961](https://github.com/cosmos/iavl/pull/961) Add new `GetLatestVersion` API to get the latest version.
- [#965](https://github.com/cosmos/iavl/pull/965) Use expected interface for expected IAVL `Logger`.
- [#970](https://github.com/cosmos/iavl/pull/970) Close the pruning process when the nodeDB is closed.
- [#968](https://github.com/cosmos/iavl/pull/968) Get rid of `cosmos-db` deps completely.

## v1.2.0 May 13, 2024

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.59.1
golangci_version=v1.55.2

lint:
@echo "--> Running linter"
Expand Down
2 changes: 1 addition & 1 deletion basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func TestIterateRange(t *testing.T) {
}
// test traversing the whole node works... in order
viewed := []string{}
tree.Iterate(func(key []byte, _ []byte) bool {
tree.Iterate(func(key []byte, value []byte) bool {
viewed = append(viewed, string(key))
return false
})
Expand Down
7 changes: 3 additions & 4 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package iavl
import (
"sync"

corestore "cosmossdk.io/core/store"
dbm "github.com/cosmos/iavl/db"
)

Expand All @@ -12,13 +11,13 @@ import (
// as soon as the configurable limit is reached.
type BatchWithFlusher struct {
mtx sync.Mutex
db dbm.DB // This is only used to create new batch
batch corestore.Batch // Batched writing buffer.
db dbm.DB // This is only used to create new batch
batch dbm.Batch // Batched writing buffer.

flushThreshold int // The threshold to flush the batch to disk.
}

var _ corestore.Batch = (*BatchWithFlusher)(nil)
var _ dbm.Batch = (*BatchWithFlusher)(nil)

// NewBatchWithFlusher returns new BatchWithFlusher wrapping the passed in batch
func NewBatchWithFlusher(db dbm.DB, flushThreshold int) *BatchWithFlusher {
Expand Down
12 changes: 9 additions & 3 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@ func makeKey(n uint16) []byte {
}

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

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

func testBatchWithFlusher(t *testing.T) {
func testBatchWithFlusher(t *testing.T, backend string) {
name := fmt.Sprintf("test_%x", randstr(12))
dir := t.TempDir()
db, err := dbm.NewGoLevelDB(name, dir)
db, err := dbm.NewDB(name, backend, dir)
require.NoError(t, err)
defer cleanupDBDir(dir, name)

Expand Down
17 changes: 14 additions & 3 deletions benchmarks/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
mrand "math/rand"
"os"
"runtime"
"strings"
"testing"

corestore "cosmossdk.io/core/store"
"github.com/stretchr/testify/require"

"github.com/cosmos/iavl"
Expand Down Expand Up @@ -146,7 +146,7 @@ func runIterationSlow(b *testing.B, t *iavl.MutableTree, expectedSize int) {
}
}

func iterate(b *testing.B, itr corestore.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 @@ -329,6 +329,9 @@ 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 == "rocksdb" {
_ = os.Mkdir(dirName, 0o755)
}

defer func() {
err := os.RemoveAll(dirName)
Expand All @@ -343,8 +346,16 @@ func runBenchmarks(b *testing.B, benchmarks []benchmark) {
err error
)
if bb.dbType != "nodb" {
d, err = dbm.NewGoLevelDB("test", dirName)
d, err = dbm.NewDB("test", bb.dbType, dirName)

if err != nil {
if strings.Contains(err.Error(), "unknown db_backend") {
// As an exception to run benchmarks: if the error is about cleveldb, or rocksdb,
// it requires a tag "cleveldb" to link the database at runtime, so instead just
// log the error instead of failing.
b.Logf("%+v\n", err)
continue
}
require.NoError(b, err)
}
defer d.Close()
Expand Down
14 changes: 8 additions & 6 deletions benchmarks/cosmos-exim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"time"

tmdb "github.com/cosmos/cosmos-db"

"github.com/cosmos/iavl"
idbm "github.com/cosmos/iavl/db"
)
Expand Down Expand Up @@ -86,11 +88,11 @@ func run(dbPath string) error {

// runExport runs an export benchmark and returns a map of store names/export nodes
func runExport(dbPath string) (int64, map[string][]*iavl.ExportNode, error) {
ldb, err := idbm.NewGoLevelDB("application", dbPath)
ldb, err := tmdb.NewDB("application", tmdb.GoLevelDBBackend, dbPath)
if err != nil {
return 0, nil, err
}
tree := iavl.NewMutableTree(idbm.NewPrefixDB(ldb, []byte("s/k:main/")), 0, false, iavl.NewNopLogger())
tree := iavl.NewMutableTree(idbm.NewWrapper(tmdb.NewPrefixDB(ldb, []byte("s/k:main/"))), 0, false, iavl.NewNopLogger())
version, err := tree.LoadVersion(0)
if err != nil {
return 0, nil, err
Expand All @@ -101,8 +103,8 @@ func runExport(dbPath string) (int64, map[string][]*iavl.ExportNode, error) {

totalStats := Stats{}
for _, name := range stores {
db := idbm.NewPrefixDB(ldb, []byte("s/k:"+name+"/"))
tree := iavl.NewMutableTree(db, 0, false, iavl.NewNopLogger())
db := tmdb.NewPrefixDB(ldb, []byte("s/k:"+name+"/"))
tree := iavl.NewMutableTree(idbm.NewWrapper(db), 0, false, iavl.NewNopLogger())

stats := Stats{}
export := make([]*iavl.ExportNode, 0, 100000)
Expand Down Expand Up @@ -163,11 +165,11 @@ func runImport(version int64, exports map[string][]*iavl.ExportNode) error {
start := time.Now()
stats := Stats{}

newDB, err := idbm.NewGoLevelDB(name, tempdir)
newDB, err := tmdb.NewDB(name, tmdb.GoLevelDBBackend, tempdir)
if err != nil {
return err
}
newTree := iavl.NewMutableTree(newDB, 0, false, iavl.NewNopLogger())
newTree := iavl.NewMutableTree(idbm.NewWrapper(newDB), 0, false, iavl.NewNopLogger())
importer, err := newTree.Import(version)
if err != nil {
return err
Expand Down
53 changes: 53 additions & 0 deletions benchmarks/hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package benchmarks

import (
"crypto"
"fmt"
"hash"
"testing"

"github.com/cosmos/iavl"
"github.com/stretchr/testify/require"

_ "crypto/sha256"

_ "golang.org/x/crypto/ripemd160" // nolint: staticcheck // need to test ripemd160
_ "golang.org/x/crypto/sha3"
)

func BenchmarkHash(b *testing.B) {
fmt.Printf("%s\n", iavl.GetVersionInfo())
hashers := []struct {
name string
size int
hash hash.Hash
}{
{"ripemd160", 64, crypto.RIPEMD160.New()},
{"ripemd160", 512, crypto.RIPEMD160.New()},
{"sha2-256", 64, crypto.SHA256.New()},
{"sha2-256", 512, crypto.SHA256.New()},
{"sha3-256", 64, crypto.SHA3_256.New()},
{"sha3-256", 512, crypto.SHA3_256.New()},
}

for _, h := range hashers {
prefix := fmt.Sprintf("%s-%d", h.name, h.size)
hasher := h
b.Run(prefix, func(sub *testing.B) {
benchHasher(sub, hasher.hash, hasher.size)
})
}
}

func benchHasher(b *testing.B, hash hash.Hash, size int) {
// create all random bytes before to avoid timing this
inputs := randBytes(b.N + size + 1)

for i := 0; i < b.N; i++ {
hash.Reset()
// grab a slice of size bytes from random string
_, err := hash.Write(inputs[i : i+size])
require.NoError(b, err)
hash.Sum(nil)
}
}
53 changes: 0 additions & 53 deletions cmd/go.mod

This file was deleted.

Loading

0 comments on commit f07f9d8

Please sign in to comment.