Skip to content

Commit

Permalink
store: Remove Amino (cosmos#6984)
Browse files Browse the repository at this point in the history
* Update kv pair to proto

* updates

* fix LastCommitID

* lint++

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
alexanderbez and mergify[bot] authored Aug 11, 2020
1 parent 69cd552 commit 0f44d1a
Show file tree
Hide file tree
Showing 33 changed files with 1,442 additions and 391 deletions.
9 changes: 8 additions & 1 deletion proto/cosmos/kv/kv.proto
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
syntax = "proto3";
package cosmos.kv;

import "gogoproto/gogo.proto";

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

// Key-Value Pair
// Pairs defines a repeated slice of Pair objects.
message Pairs {
repeated Pair pairs = 1 [(gogoproto.nullable) = false];
}

// Pair defines a key/value bytes tuple.
message Pair {
bytes key = 1;
bytes value = 2;
Expand Down
29 changes: 29 additions & 0 deletions proto/cosmos/store/commit_info.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
syntax = "proto3";
package cosmos.store;

import "gogoproto/gogo.proto";

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

// CommitInfo defines commit information used by the multi-store when committing
// a version/height.
message CommitInfo {
int64 version = 1;
repeated StoreInfo store_infos = 2 [(gogoproto.nullable) = false];
}

// StoreInfo defines store-specific commit information. It contains a reference
// between a store name and the commit ID.
message StoreInfo {
string name = 1;
CommitID commit_id = 2 [(gogoproto.nullable) = false, (gogoproto.customname) = "CommitID"];
}

// CommitID defines the committment information when a specific store is
// committed.
message CommitID {
option (gogoproto.goproto_stringer) = false;

int64 version = 1;
bytes hash = 2;
}
15 changes: 11 additions & 4 deletions store/iavl/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,18 +270,25 @@ func (st *Store) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
res.Proof = getProofFromTree(mtree, req.Data, res.Value != nil)

case "/subspace":
var KVs []types.KVPair
pairs := kv.Pairs{
Pairs: make([]kv.Pair, 0),
}

subspace := req.Data
res.Key = subspace

iterator := types.KVStorePrefixIterator(st, subspace)
for ; iterator.Valid(); iterator.Next() {
KVs = append(KVs, types.KVPair{Key: iterator.Key(), Value: iterator.Value()})
pairs.Pairs = append(pairs.Pairs, kv.Pair{Key: iterator.Key(), Value: iterator.Value()})
}

iterator.Close()
res.Value = cdc.MustMarshalBinaryBare(KVs)

bz, err := pairs.Marshal()
if err != nil {
panic(fmt.Errorf("failed to marshal KV pairs: %w", err))
}

res.Value = bz

default:
return sdkerrors.QueryResult(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unexpected query path: %v", req.Path))
Expand Down
31 changes: 21 additions & 10 deletions store/iavl/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
dbm "github.com/tendermint/tm-db"

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

var (
Expand Down Expand Up @@ -408,18 +409,28 @@ func TestIAVLStoreQuery(t *testing.T) {
v3 := []byte("val3")

ksub := []byte("key")
KVs0 := []types.KVPair{}
KVs1 := []types.KVPair{
{Key: k1, Value: v1},
{Key: k2, Value: v2},
KVs0 := kv.Pairs{}
KVs1 := kv.Pairs{
Pairs: []kv.Pair{
{Key: k1, Value: v1},
{Key: k2, Value: v2},
},
}
KVs2 := []types.KVPair{
{Key: k1, Value: v3},
{Key: k2, Value: v2},
KVs2 := kv.Pairs{
Pairs: []kv.Pair{
{Key: k1, Value: v3},
{Key: k2, Value: v2},
},
}
valExpSubEmpty := cdc.MustMarshalBinaryBare(KVs0)
valExpSub1 := cdc.MustMarshalBinaryBare(KVs1)
valExpSub2 := cdc.MustMarshalBinaryBare(KVs2)

valExpSubEmpty, err := KVs0.Marshal()
require.NoError(t, err)

valExpSub1, err := KVs1.Marshal()
require.NoError(t, err)

valExpSub2, err := KVs2.Marshal()
require.NoError(t, err)

cid := iavlStore.Commit()
ver := cid.Version
Expand Down
7 changes: 0 additions & 7 deletions store/iavl/wire.go

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/tendermint/tendermint/crypto/merkle"
"github.com/tendermint/tendermint/crypto/tmhash"

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

Expand All @@ -19,7 +18,7 @@ type merkleMap struct {

func newMerkleMap() *merkleMap {
return &merkleMap{
kvs: nil,
kvs: kv.Pairs{},
sorted: false,
}
}
Expand All @@ -29,15 +28,15 @@ func newMerkleMap() *merkleMap {
// of kv.Pairs. Whenever called, the MerkleMap must be resorted.
func (sm *merkleMap) set(key string, value []byte) {
byteKey := []byte(key)
types.AssertValidKey(byteKey)
assertValidKey(byteKey)

sm.sorted = false

// The value is hashed, so you can check for equality with a cached value (say)
// and make a determination to fetch or not.
vhash := tmhash.Sum(value)

sm.kvs = append(sm.kvs, kv.Pair{
sm.kvs.Pairs = append(sm.kvs.Pairs, kv.Pair{
Key: byteKey,
Value: vhash,
})
Expand All @@ -61,8 +60,8 @@ func (sm *merkleMap) sort() {
// hashKVPairs hashes a kvPair and creates a merkle tree where the leaves are
// byte slices.
func hashKVPairs(kvs kv.Pairs) []byte {
kvsH := make([][]byte, len(kvs))
for i, kvp := range kvs {
kvsH := make([][]byte, len(kvs.Pairs))
for i, kvp := range kvs.Pairs {
kvsH[i] = KVPair(kvp).Bytes()
}

Expand All @@ -81,7 +80,7 @@ type simpleMap struct {

func newSimpleMap() *simpleMap {
return &simpleMap{
Kvs: nil,
Kvs: kv.Pairs{},
sorted: false,
}
}
Expand All @@ -90,15 +89,15 @@ func newSimpleMap() *simpleMap {
// and then appends it to SimpleMap's kv pairs.
func (sm *simpleMap) Set(key string, value []byte) {
byteKey := []byte(key)
types.AssertValidKey(byteKey)
assertValidKey(byteKey)
sm.sorted = false

// The value is hashed, so you can
// check for equality with a cached value (say)
// and make a determination to fetch or not.
vhash := tmhash.Sum(value)

sm.Kvs = append(sm.Kvs, kv.Pair{
sm.Kvs.Pairs = append(sm.Kvs.Pairs, kv.Pair{
Key: byteKey,
Value: vhash,
})
Expand All @@ -123,8 +122,11 @@ func (sm *simpleMap) Sort() {
// NOTE these contain the hashed key and value.
func (sm *simpleMap) KVPairs() kv.Pairs {
sm.Sort()
kvs := make(kv.Pairs, len(sm.Kvs))
copy(kvs, sm.Kvs)
kvs := kv.Pairs{
Pairs: make([]kv.Pair, len(sm.Kvs.Pairs)),
}

copy(kvs.Pairs, sm.Kvs.Pairs)
return kvs
}

Expand Down Expand Up @@ -188,18 +190,25 @@ func SimpleProofsFromMap(m map[string][]byte) ([]byte, map[string]*merkle.Simple

sm.Sort()
kvs := sm.Kvs
kvsBytes := make([][]byte, len(kvs))
for i, kvp := range kvs {
kvsBytes := make([][]byte, len(kvs.Pairs))
for i, kvp := range kvs.Pairs {
kvsBytes[i] = KVPair(kvp).Bytes()
}

rootHash, proofList := merkle.SimpleProofsFromByteSlices(kvsBytes)
proofs := make(map[string]*merkle.SimpleProof)
keys := make([]string, len(proofList))
for i, kvp := range kvs {

for i, kvp := range kvs.Pairs {
proofs[string(kvp.Key)] = proofList[i]
keys[i] = string(kvp.Key)
}

return rootHash, proofs, keys
}

func assertValidKey(key []byte) {
if len(key) == 0 {
panic("key is nil")
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

ics23 "github.com/confio/ics23/go"

sdkmaps "github.com/cosmos/cosmos-sdk/store/rootmulti/internal/maps"
sdkmaps "github.com/cosmos/cosmos-sdk/store/internal/maps"
)

// TendermintSpec constrains the format from ics23-tendermint (crypto/merkle SimpleProof)
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 0f44d1a

Please sign in to comment.