From 77de1067e2c886dc3233f4b1ce40448462cfa605 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 20 Nov 2024 14:34:58 +0100 Subject: [PATCH 1/3] feat(core/coretesting): make memDB satisfy db.Db interface --- core/testing/memdb.go | 33 ++++++++++++++++++++++++++++++++- tests/integration/type_check.go | 14 ++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tests/integration/type_check.go diff --git a/core/testing/memdb.go b/core/testing/memdb.go index 1a1cfe89cbc2..f9b60a49bf51 100644 --- a/core/testing/memdb.go +++ b/core/testing/memdb.go @@ -258,7 +258,9 @@ func (mi *memIterator) assertValid() { } } -var _ store.KVStoreWithBatch = (*MemDB)(nil) +var ( + _ store.KVStoreWithBatch = (*MemDB)(nil) +) // MemDB is a simple in-memory key-value store with Batch support. type MemDB struct { @@ -291,12 +293,20 @@ func (bt *MemDB) Set(key, value []byte) error { return bt.kv.Set(key, value) } +func (bt *MemDB) SetSync(key, value []byte) error { + return bt.Set(key, value) +} + func (bt *MemDB) Delete(key []byte) error { bt.mtx.Lock() defer bt.mtx.Unlock() return bt.kv.Delete(key) } +func (bt *MemDB) DeleteSync(key []byte) error { + return bt.Delete(key) +} + func (bt *MemDB) Iterator(start, end []byte) (store.Iterator, error) { return bt.kv.Iterator(start, end) } @@ -305,6 +315,27 @@ func (bt *MemDB) ReverseIterator(start, end []byte) (store.Iterator, error) { return bt.kv.ReverseIterator(start, end) } +func (db *MemDB) Print() error { + db.mtx.RLock() + defer db.mtx.RUnlock() + + db.kv.tree.Ascend(item{}, func(i item) bool { + fmt.Printf("[%X]:\t[%X]\n", i.key, i.value) + return true + }) + return nil +} + +func (db *MemDB) Stats() map[string]string { + db.mtx.RLock() + defer db.mtx.RUnlock() + + stats := make(map[string]string) + stats["database.type"] = "memDB" + stats["database.size"] = fmt.Sprintf("%d", db.kv.tree.Len()) + return stats +} + // Close closes the MemDB, releasing any resources held. func (db *MemDB) Close() error { return nil diff --git a/tests/integration/type_check.go b/tests/integration/type_check.go new file mode 100644 index 000000000000..9885981eb2a4 --- /dev/null +++ b/tests/integration/type_check.go @@ -0,0 +1,14 @@ +package integration + +import ( + coretesting "cosmossdk.io/core/testing" + + db "github.com/cosmos/cosmos-db" +) + +// This file contains a list of type checks that are used to ensure that implementations +// matches the interface. We do not do those type checks directly in the components to +// avoid to bring in more dependencies than needed. +var ( + _ db.DB = (*coretesting.MemDB)(nil) +) From fbd4e03a40a19068a775ddb7a026d30d77561c7a Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 20 Nov 2024 14:35:42 +0100 Subject: [PATCH 2/3] whitespace --- core/testing/memdb.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/core/testing/memdb.go b/core/testing/memdb.go index f9b60a49bf51..526a6d45ef8d 100644 --- a/core/testing/memdb.go +++ b/core/testing/memdb.go @@ -258,9 +258,7 @@ func (mi *memIterator) assertValid() { } } -var ( - _ store.KVStoreWithBatch = (*MemDB)(nil) -) +var _ store.KVStoreWithBatch = (*MemDB)(nil) // MemDB is a simple in-memory key-value store with Batch support. type MemDB struct { From 5aee0182a0843dcecb750109527b3b87b9cba666 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 20 Nov 2024 15:08:16 +0100 Subject: [PATCH 3/3] go mod tidy --- tests/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/go.mod b/tests/go.mod index ab2af286d8f9..c847222250fc 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -50,6 +50,7 @@ require ( cosmossdk.io/x/slashing v0.0.0-00010101000000-000000000000 cosmossdk.io/x/staking v0.0.0-20240226161501-23359a0b6d91 github.com/cometbft/cometbft/api v1.0.0-rc.1 + github.com/cosmos/cosmos-db v1.0.3-0.20240911104526-ddc3f09bfc22 github.com/google/go-cmp v0.6.0 github.com/google/gofuzz v1.2.0 github.com/jhump/protoreflect v1.17.0 @@ -103,7 +104,6 @@ require ( github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect github.com/cometbft/cometbft-db v0.15.0 // indirect github.com/cosmos/btcutil v1.0.5 // indirect - github.com/cosmos/cosmos-db v1.0.3-0.20240911104526-ddc3f09bfc22 // indirect github.com/cosmos/crypto v0.1.2 // indirect github.com/cosmos/go-bip39 v1.0.0 // indirect github.com/cosmos/gogogateway v1.2.0 // indirect