Skip to content

Commit

Permalink
refactor: remove store kv dependency (#14144)
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle authored Dec 7, 2022
1 parent cdae85f commit f3be418
Show file tree
Hide file tree
Showing 15 changed files with 262 additions and 276 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
syntax = "proto3";
package cosmos.base.kv.v1beta1;
package cosmos.base.store.internal.kv.v1beta1;

import "gogoproto/gogo.proto";

option go_package = "github.com/cosmos/cosmos-sdk/types/kv";
option go_package = "github.com/cosmos/cosmos-sdk/store/internal/kv";

// This is duplicated from the base kv directory to avoid a circular dependency with the cosmos-sdk

// Pairs defines a repeated slice of Pair objects.
message Pairs {
Expand Down
3 changes: 2 additions & 1 deletion simapp/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/server"
storetypes "github.com/cosmos/cosmos-sdk/store/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
Expand Down Expand Up @@ -222,6 +222,7 @@ func TestAppImportExport(t *testing.T) {
require.Equal(t, len(failedKVAs), len(failedKVBs), "unequal sets of key-values to compare")

fmt.Printf("compared %d different key/value pairs between %s and %s\n", len(failedKVAs), skp.A, skp.B)

require.Equal(t, 0, len(failedKVAs), simtestutil.GetSimulationLog(skp.A.Name(), app.SimulationManager().StoreDecoders, failedKVAs, failedKVBs))
}
}
Expand Down
2 changes: 1 addition & 1 deletion store/cachekv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/internal/conv"
"github.com/cosmos/cosmos-sdk/store/internal/kv"
"github.com/cosmos/cosmos-sdk/store/tracekv"
"github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/types/kv"
)

// cValue represents a cached value.
Expand Down
2 changes: 1 addition & 1 deletion store/iavl/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (

sdkerrors "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/store/cachekv"
"github.com/cosmos/cosmos-sdk/store/internal/kv"
pruningtypes "github.com/cosmos/cosmos-sdk/store/pruning/types"
"github.com/cosmos/cosmos-sdk/store/tracekv"
"github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/telemetry"
"github.com/cosmos/cosmos-sdk/types/kv"
)

const (
Expand Down
5 changes: 2 additions & 3 deletions store/iavl/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import (
"fmt"
"testing"

"github.com/cosmos/cosmos-sdk/store/cachekv"

"github.com/cosmos/iavl"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/store/cachekv"
"github.com/cosmos/cosmos-sdk/store/internal/kv"
"github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/types/kv"
)

var (
Expand Down
17 changes: 17 additions & 0 deletions store/internal/kv/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package kv

import "fmt"

// AssertKeyAtLeastLength panics when store key length is less than the given length.
func AssertKeyAtLeastLength(bz []byte, length int) {
if len(bz) < length {
panic(fmt.Sprintf("expected key of length at least %d, got %d", length, len(bz)))
}
}

// AssertKeyLength panics when store key length is not equal to the given length.
func AssertKeyLength(bz []byte, length int) {
if len(bz) != length {
panic(fmt.Sprintf("unexpected key length; got: %d, expected: %d", len(bz), length))
}
}
28 changes: 28 additions & 0 deletions store/internal/kv/kv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package kv

import (
"bytes"
"sort"
)

func (kvs Pairs) Len() int { return len(kvs.Pairs) }
func (kvs Pairs) Less(i, j int) bool {
switch bytes.Compare(kvs.Pairs[i].Key, kvs.Pairs[j].Key) {
case -1:
return true

case 0:
return bytes.Compare(kvs.Pairs[i].Value, kvs.Pairs[j].Value) < 0

case 1:
return false

default:
panic("invalid comparison result")
}
}

func (kvs Pairs) Swap(i, j int) { kvs.Pairs[i], kvs.Pairs[j] = kvs.Pairs[j], kvs.Pairs[i] }

// Sort invokes sort.Sort on kvs.
func (kvs Pairs) Sort() { sort.Sort(kvs) }
47 changes: 25 additions & 22 deletions types/kv/kv.pb.go → store/internal/kv/kv.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion store/internal/maps/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/tendermint/tendermint/crypto/tmhash"
tmcrypto "github.com/tendermint/tendermint/proto/tendermint/crypto"

"github.com/cosmos/cosmos-sdk/types/kv"
"github.com/cosmos/cosmos-sdk/store/internal/kv"
)

// merkleMap defines a merkle-ized tree from a map. Leave values are treated as
Expand Down
59 changes: 0 additions & 59 deletions store/types/utils.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package types

import (
"bytes"
"fmt"
"sort"
"strings"

"github.com/cosmos/cosmos-sdk/types/kv"
)

// KVStorePrefixIterator iterates over all the keys with a certain prefix in ascending order
Expand All @@ -19,62 +16,6 @@ func KVStoreReversePrefixIterator(kvs KVStore, prefix []byte) Iterator {
return kvs.ReverseIterator(prefix, PrefixEndBytes(prefix))
}

// DiffKVStores compares two KVstores and returns all the key/value pairs
// that differ from one another. It also skips value comparison for a set of provided prefixes.
func DiffKVStores(a KVStore, b KVStore, prefixesToSkip [][]byte) (kvAs, kvBs []kv.Pair) {
iterA := a.Iterator(nil, nil)

defer iterA.Close()

iterB := b.Iterator(nil, nil)

defer iterB.Close()

for {
if !iterA.Valid() && !iterB.Valid() {
return kvAs, kvBs
}

var kvA, kvB kv.Pair
if iterA.Valid() {
kvA = kv.Pair{Key: iterA.Key(), Value: iterA.Value()}

iterA.Next()
}

if iterB.Valid() {
kvB = kv.Pair{Key: iterB.Key(), Value: iterB.Value()}
}

compareValue := true

for _, prefix := range prefixesToSkip {
// Skip value comparison if we matched a prefix
if bytes.HasPrefix(kvA.Key, prefix) {
compareValue = false
break
}
}

if !compareValue {
// We're skipping this key due to an exclusion prefix. If it's present in B, iterate past it. If it's
// absent don't iterate.
if bytes.Equal(kvA.Key, kvB.Key) {
iterB.Next()
}
continue
}

// always iterate B when comparing
iterB.Next()

if !bytes.Equal(kvA.Key, kvB.Key) || !bytes.Equal(kvA.Value, kvB.Value) {
kvAs = append(kvAs, kvA)
kvBs = append(kvBs, kvB)
}
}
}

// PrefixEndBytes returns the []byte that would end a
// range query for all []byte with a certain prefix
// Deals with last byte of prefix being FF without overflowing
Expand Down
47 changes: 0 additions & 47 deletions store/types/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,53 +24,6 @@ func initTestStores(t *testing.T) (types.KVStore, types.KVStore) {
return ms.GetKVStore(key1), ms.GetKVStore(key2)
}

func TestDiffKVStores(t *testing.T) {
t.Parallel()
store1, store2 := initTestStores(t)
// Two equal stores
k1, v1 := []byte("k1"), []byte("v1")
store1.Set(k1, v1)
store2.Set(k1, v1)

kvAs, kvBs := types.DiffKVStores(store1, store2, nil)
require.Equal(t, 0, len(kvAs))
require.Equal(t, len(kvAs), len(kvBs))

// delete k1 from store2, which is now empty
store2.Delete(k1)
kvAs, kvBs = types.DiffKVStores(store1, store2, nil)
require.Equal(t, 1, len(kvAs))
require.Equal(t, len(kvAs), len(kvBs))

// set k1 in store2, different value than what store1 holds for k1
v2 := []byte("v2")
store2.Set(k1, v2)
kvAs, kvBs = types.DiffKVStores(store1, store2, nil)
require.Equal(t, 1, len(kvAs))
require.Equal(t, len(kvAs), len(kvBs))

// add k2 to store2
k2 := []byte("k2")
store2.Set(k2, v2)
kvAs, kvBs = types.DiffKVStores(store1, store2, nil)
require.Equal(t, 2, len(kvAs))
require.Equal(t, len(kvAs), len(kvBs))

// Reset stores
store1.Delete(k1)
store2.Delete(k1)
store2.Delete(k2)

// Same keys, different value. Comparisons will be nil as prefixes are skipped.
prefix := []byte("prefix:")
k1Prefixed := append(prefix, k1...)
store1.Set(k1Prefixed, v1)
store2.Set(k1Prefixed, v2)
kvAs, kvBs = types.DiffKVStores(store1, store2, [][]byte{prefix})
require.Equal(t, 0, len(kvAs))
require.Equal(t, len(kvAs), len(kvBs))
}

func TestPrefixEndBytes(t *testing.T) {
t.Parallel()
bs1 := []byte{0x23, 0xA5, 0x06}
Expand Down
30 changes: 6 additions & 24 deletions types/kv/kv.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
package kv

import (
"bytes"
"sort"
)

func (kvs Pairs) Len() int { return len(kvs.Pairs) }
func (kvs Pairs) Less(i, j int) bool {
switch bytes.Compare(kvs.Pairs[i].Key, kvs.Pairs[j].Key) {
case -1:
return true

case 0:
return bytes.Compare(kvs.Pairs[i].Value, kvs.Pairs[j].Value) < 0

case 1:
return false

default:
panic("invalid comparison result")
}
type Pair struct {
Key []byte
Value []byte
}

func (kvs Pairs) Swap(i, j int) { kvs.Pairs[i], kvs.Pairs[j] = kvs.Pairs[j], kvs.Pairs[i] }

// Sort invokes sort.Sort on kvs.
func (kvs Pairs) Sort() { sort.Sort(kvs) }
type Pairs struct {
Pairs []Pair
}
Loading

0 comments on commit f3be418

Please sign in to comment.