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

refactor: remove store kv dependency #14144

Merged
merged 22 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved

// Pairs defines a repeated slice of Pair objects.
message Pairs {
Expand Down
18 changes: 14 additions & 4 deletions simapp/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ 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"
"github.com/cosmos/cosmos-sdk/types/kv"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
Expand Down Expand Up @@ -218,11 +218,21 @@ func TestAppImportExport(t *testing.T) {
storeA := ctxA.KVStore(skp.A)
storeB := ctxB.KVStore(skp.B)

failedKVAs, failedKVBs := sdk.DiffKVStores(storeA, storeB, skp.Prefixes)
failedKVAs, failedKVBs := storetypes.DiffKVStores(storeA, storeB, skp.Prefixes)
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
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))

kva := kv.Pairs{}
kvb := kv.Pairs{}
for i, a := range failedKVAs {
kva.Pairs[i] = kv.Pair(a)
}
for i, a := range failedKVBs {
kvb.Pairs[i] = kv.Pair(a)
}

require.Equal(t, 0, len(failedKVAs), simtestutil.GetSimulationLog(skp.A.Name(), app.SimulationManager().StoreDecoders, kva.Pairs, kvb.Pairs))
}
}

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
2 changes: 1 addition & 1 deletion store/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sort"
"strings"

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

// KVStorePrefixIterator iterates over all the keys with a certain prefix in ascending order
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
}
6 changes: 0 additions & 6 deletions types/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ func KVStoreReversePrefixIteratorPaginated(kvs KVStore, prefix []byte, page, lim
return types.KVStoreReversePrefixIteratorPaginated(kvs, prefix, page, limit)
}

// 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) {
return types.DiffKVStores(a, b, prefixesToSkip)
}

// assertNoCommonPrefix will panic if there are two keys: k1 and k2 in keys, such that
// k1 is a prefix of k2
func assertNoPrefix(keys []string) {
Expand Down