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

types: add kv type #6897

Merged
merged 10 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from 9 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
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{
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@odeke-em can you check this? when I ran make proto-gen it forced me to make these changes

{
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this needs a new package to itself, but I guess it's okay...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i can change, let me know what you prefer


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