Skip to content

Commit

Permalink
types: add kv type (cosmos#6897)
Browse files Browse the repository at this point in the history
* add kv type

* add changelog entry

* fix build

* replace sdkkv with kv

* revert change

* fix some tests

* proto-gen

* fix tests

Co-authored-by: Aleksandr Bezobchuk <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 30, 2020
1 parent c7ad21d commit 617b822
Show file tree
Hide file tree
Showing 46 changed files with 1,317 additions and 925 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ pagination.
* (baseapp) [\#6053](https://github.com/cosmos/cosmos-sdk/pull/6053) Customizable panic recovery handling added for `app.runTx()` method (as proposed in the [ADR 22](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-022-custom-panic-handling.md)). Adds ability for developers to register custom panic handlers extending standard ones.
* (store) [\#6481](https://github.com/cosmos/cosmos-sdk/pull/6481) Move `SimpleProofsFromMap` from Tendermint into the SDK.
* (store) [\#6719](https://github.com/cosmos/cosmos-sdk/6754) Add validity checks to stores for nil and empty keys.
* (types) \#6897 Add KV type from tendermint to `types` directory.

## [v0.39.0] - 2020-07-20

Expand Down
14 changes: 7 additions & 7 deletions codec/unknownproto/unknown_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestRejectUnknownFieldsRepeated(t *testing.T) {
F: &testdata.TestVersion2{
A: &testdata.TestVersion2{
B: &testdata.TestVersion2{
H: []*testdata.TestVersion2{
H: []*testdata.TestVersion1{
{
X: 0x01,
},
Expand All @@ -44,7 +44,7 @@ func TestRejectUnknownFieldsRepeated(t *testing.T) {
F: &testdata.TestVersion2{
A: &testdata.TestVersion2{
B: &testdata.TestVersion2{
H: []*testdata.TestVersion2{
H: []*testdata.TestVersion1{
{
X: 0x02,
},
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestRejectUnknownFieldsRepeated(t *testing.T) {
F: &testdata.TestVersion2{
A: &testdata.TestVersion2{
B: &testdata.TestVersion2{
H: []*testdata.TestVersion2{
H: []*testdata.TestVersion1{
{
X: 0x01,
},
Expand All @@ -99,7 +99,7 @@ func TestRejectUnknownFieldsRepeated(t *testing.T) {
F: &testdata.TestVersion2{
A: &testdata.TestVersion2{
B: &testdata.TestVersion2{
H: []*testdata.TestVersion2{
H: []*testdata.TestVersion1{
{
X: 0x02,
},
Expand Down Expand Up @@ -309,13 +309,13 @@ func TestRejectUnknownFieldsNested(t *testing.T) {
Sum: &testdata.TestVersion2_E{
E: 100,
},
H: []*testdata.TestVersion2{
H: []*testdata.TestVersion1{
{X: 999},
{X: -55},
{
X: 102,
Sum: &testdata.TestVersion2_F{
F: &testdata.TestVersion2{
Sum: &testdata.TestVersion1_F{
F: &testdata.TestVersion1{
X: 4,
},
},
Expand Down
10 changes: 10 additions & 0 deletions proto/cosmos/kv/kv.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";
package cosmos.kv;

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

// Key-Value Pair
message Pair {
bytes key = 1;
bytes value = 2;
}
12 changes: 5 additions & 7 deletions proto/ibc/transfer/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ package ibc.transfer;
option go_package = "github.com/cosmos/cosmos-sdk/x/ibc-transfer/types";

import "gogoproto/gogo.proto";
import "cosmos/cosmos.proto";
import "ibc/transfer/transfer.proto";

// GenesisState is currently only used to ensure that the InitGenesis gets run
// by the module manager
message GenesisState{
string port_id = 1 [
(gogoproto.customname) = "PortID",
(gogoproto.moretags) = "yaml:\"port_id\""
];
message GenesisState {
string port_id = 1 [
(gogoproto.customname) = "PortID",
(gogoproto.moretags) = "yaml:\"port_id\""
];
}
4 changes: 2 additions & 2 deletions simapp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"fmt"
"io/ioutil"

tmkv "github.com/tendermint/tendermint/libs/kv"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/kv"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
)
Expand Down Expand Up @@ -109,7 +109,7 @@ func PrintStats(db dbm.DB) {

// GetSimulationLog unmarshals the KVPair's Value to the corresponding type based on the
// each's module store key and the prefix bytes of the KVPair's key.
func GetSimulationLog(storeName string, sdr sdk.StoreDecoderRegistry, kvAs, kvBs []tmkv.Pair) (log string) {
func GetSimulationLog(storeName string, sdr sdk.StoreDecoderRegistry, kvAs, kvBs []kv.Pair) (log string) {
for i := 0; i < len(kvAs); i++ {
if len(kvAs[i].Value) == 0 && len(kvBs[i].Value) == 0 {
// skip if the value doesn't have any bytes
Expand Down
15 changes: 7 additions & 8 deletions simapp/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,38 @@ import (
"fmt"
"testing"

"github.com/cosmos/cosmos-sdk/std"

"github.com/stretchr/testify/require"
tmkv "github.com/tendermint/tendermint/libs/kv"

"github.com/cosmos/cosmos-sdk/std"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/kv"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

func TestGetSimulationLog(t *testing.T) {
cdc := std.MakeCodec(ModuleBasics)

decoders := make(sdk.StoreDecoderRegistry)
decoders[authtypes.StoreKey] = func(kvAs, kvBs tmkv.Pair) string { return "10" }
decoders[authtypes.StoreKey] = func(kvAs, kvBs kv.Pair) string { return "10" }

tests := []struct {
store string
kvPairs []tmkv.Pair
kvPairs []kv.Pair
expectedLog string
}{
{
"Empty",
[]tmkv.Pair{{}},
[]kv.Pair{{}},
"",
},
{
authtypes.StoreKey,
[]tmkv.Pair{{Key: authtypes.GlobalAccountNumberKey, Value: cdc.MustMarshalBinaryBare(uint64(10))}},
[]kv.Pair{{Key: authtypes.GlobalAccountNumberKey, Value: cdc.MustMarshalBinaryBare(uint64(10))}},
"10",
},
{
"OtherStore",
[]tmkv.Pair{{Key: []byte("key"), Value: []byte("value")}},
[]kv.Pair{{Key: []byte("key"), Value: []byte("value")}},
fmt.Sprintf("store A %X => %X\nstore B %X => %X\n", []byte("key"), []byte("value"), []byte("key"), []byte("value")),
},
}
Expand Down
8 changes: 4 additions & 4 deletions store/cachekv/memiterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"container/list"
"errors"

tmkv "github.com/tendermint/tendermint/libs/kv"
"github.com/cosmos/cosmos-sdk/types/kv"
dbm "github.com/tendermint/tm-db"
)

Expand All @@ -13,17 +13,17 @@ import (
// Implements Iterator.
type memIterator struct {
start, end []byte
items []*tmkv.Pair
items []*kv.Pair
ascending bool
}

func newMemIterator(start, end []byte, items *list.List, ascending bool) *memIterator {
itemsInDomain := make([]*tmkv.Pair, 0)
itemsInDomain := make([]*kv.Pair, 0)

var entered bool

for e := items.Front(); e != nil; e = e.Next() {
item := e.Value.(*tmkv.Pair)
item := e.Value.(*kv.Pair)
if !dbm.IsKeyInDomain(item.Key, start, end) {
if entered {
break
Expand Down
8 changes: 4 additions & 4 deletions store/cachekv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"sync"
"time"

tmkv "github.com/tendermint/tendermint/libs/kv"
dbm "github.com/tendermint/tm-db"

"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"
)

// If value is nil but deleted is false, it means the parent doesn't have the
Expand Down Expand Up @@ -181,13 +181,13 @@ func (store *Store) iterator(start, end []byte, ascending bool) types.Iterator {

// Constructs a slice of dirty items, to use w/ memIterator.
func (store *Store) dirtyItems(start, end []byte) {
unsorted := make([]*tmkv.Pair, 0)
unsorted := make([]*kv.Pair, 0)

for key := range store.unsortedCache {
cacheValue := store.cache[key]

if dbm.IsKeyInDomain([]byte(key), start, end) {
unsorted = append(unsorted, &tmkv.Pair{Key: []byte(key), Value: cacheValue.value})
unsorted = append(unsorted, &kv.Pair{Key: []byte(key), Value: cacheValue.value})

delete(store.unsortedCache, key)
}
Expand All @@ -199,7 +199,7 @@ func (store *Store) dirtyItems(start, end []byte) {

for e := store.sortedCache.Front(); e != nil && len(unsorted) != 0; {
uitem := unsorted[0]
sitem := e.Value.(*tmkv.Pair)
sitem := e.Value.(*kv.Pair)
comp := bytes.Compare(uitem.Key, sitem.Key)

switch comp {
Expand Down
13 changes: 6 additions & 7 deletions store/firstlast.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,27 @@ package store
import (
"bytes"

tmkv "github.com/tendermint/tendermint/libs/kv"

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

// Gets the first item.
func First(st KVStore, start, end []byte) (kv tmkv.Pair, ok bool) {
func First(st KVStore, start, end []byte) (kv sdkkv.Pair, ok bool) {
iter := st.Iterator(start, end)
if !iter.Valid() {
return kv, false
}
defer iter.Close()

return tmkv.Pair{Key: iter.Key(), Value: iter.Value()}, true
return sdkkv.Pair{Key: iter.Key(), Value: iter.Value()}, true
}

// Gets the last item. `end` is exclusive.
func Last(st KVStore, start, end []byte) (kv tmkv.Pair, ok bool) {
func Last(st KVStore, start, end []byte) (kv sdkkv.Pair, ok bool) {
iter := st.ReverseIterator(end, start)
if !iter.Valid() {
if v := st.Get(start); v != nil {
return tmkv.Pair{Key: sdk.CopyBytes(start), Value: sdk.CopyBytes(v)}, true
return sdkkv.Pair{Key: sdk.CopyBytes(start), Value: sdk.CopyBytes(v)}, true
}
return kv, false
}
Expand All @@ -38,5 +37,5 @@ func Last(st KVStore, start, end []byte) (kv tmkv.Pair, ok bool) {
}
}

return tmkv.Pair{Key: iter.Key(), Value: iter.Value()}, true
return sdkkv.Pair{Key: iter.Key(), Value: iter.Value()}, true
}
8 changes: 4 additions & 4 deletions store/iavl/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/tendermint/iavl"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/merkle"
tmkv "github.com/tendermint/tendermint/libs/kv"
dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/store/cachekv"
Expand All @@ -20,6 +19,7 @@ import (
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/kv"
)

const (
Expand Down Expand Up @@ -333,7 +333,7 @@ type iavlIterator struct {
tree *iavl.ImmutableTree

// Channel to push iteration values.
iterCh chan tmkv.Pair
iterCh chan kv.Pair

// Close this to release goroutine.
quitCh chan struct{}
Expand All @@ -359,7 +359,7 @@ func newIAVLIterator(tree *iavl.ImmutableTree, start, end []byte, ascending bool
start: sdk.CopyBytes(start),
end: sdk.CopyBytes(end),
ascending: ascending,
iterCh: make(chan tmkv.Pair), // Set capacity > 0?
iterCh: make(chan kv.Pair), // Set capacity > 0?
quitCh: make(chan struct{}),
initCh: make(chan struct{}),
}
Expand All @@ -376,7 +376,7 @@ func (iter *iavlIterator) iterateRoutine() {
select {
case <-iter.quitCh:
return true // done with iteration.
case iter.iterCh <- tmkv.Pair{Key: key, Value: value}:
case iter.iterCh <- kv.Pair{Key: key, Value: value}:
return false // yay.
}
},
Expand Down
2 changes: 1 addition & 1 deletion store/rootmulti/internal/maps/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (

"github.com/tendermint/tendermint/crypto/merkle"
"github.com/tendermint/tendermint/crypto/tmhash"
"github.com/tendermint/tendermint/libs/kv"

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

// merkleMap defines a merkle-ized tree from a map. Leave values are treated as
Expand Down
5 changes: 3 additions & 2 deletions store/types/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"io"

abci "github.com/tendermint/tendermint/abci/types"
tmkv "github.com/tendermint/tendermint/libs/kv"
dbm "github.com/tendermint/tm-db"

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

type Store interface {
Expand Down Expand Up @@ -381,7 +382,7 @@ func (key *MemoryStoreKey) String() string {
//----------------------------------------

// key-value result for iterator queries
type KVPair tmkv.Pair
type KVPair kv.Pair

//----------------------------------------

Expand Down
10 changes: 5 additions & 5 deletions store/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package types
import (
"bytes"

tmkv "github.com/tendermint/tendermint/libs/kv"
"github.com/cosmos/cosmos-sdk/types/kv"
)

// Iterator over all the keys with a certain prefix in ascending order
Expand All @@ -18,7 +18,7 @@ func KVStoreReversePrefixIterator(kvs KVStore, prefix []byte) Iterator {

// 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 []tmkv.Pair) {
func DiffKVStores(a KVStore, b KVStore, prefixesToSkip [][]byte) (kvAs, kvBs []kv.Pair) {
iterA := a.Iterator(nil, nil)

defer iterA.Close()
Expand All @@ -32,15 +32,15 @@ func DiffKVStores(a KVStore, b KVStore, prefixesToSkip [][]byte) (kvAs, kvBs []t
return kvAs, kvBs
}

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

iterA.Next()
}

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

iterB.Next()
}
Expand Down
Loading

0 comments on commit 617b822

Please sign in to comment.