Skip to content

Commit

Permalink
review updates; flip set to delete in KVStorePair, updated proto-docs…
Browse files Browse the repository at this point in the history
… from running 'make proto-gen'
  • Loading branch information
i-norden committed Mar 24, 2021
1 parent 472c66d commit 26cc072
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 62 deletions.
14 changes: 7 additions & 7 deletions docs/architecture/adr-038-state-listening.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type WriteListener interface {
// if value is nil then it was deleted
// storeKey indicates the source KVStore, to facilitate using the the same WriteListener across separate KVStores
// set bool indicates if it was a set; true: set, false: delete
OnWrite(storeKey types.StoreKey, set bool, key []byte, value []byte) error
OnWrite(storeKey StoreKey, key []byte, value []byte, delete bool) error
}
```

Expand Down Expand Up @@ -72,10 +72,10 @@ func NewStoreKVPairWriteListener(w io.Writer, m codec.BinaryMarshaler) *StoreKVP
}

// OnWrite satisfies the WriteListener interface by writing length-prefixed protobuf encoded StoreKVPairs
func (wl *StoreKVPairWriteListener) OnWrite(storeKey types.StoreKey, set bool, key []byte, value []byte) error {
func (wl *StoreKVPairWriteListener) OnWrite(storeKey types.StoreKey, key []byte, value []byte, delete bool) error error {
kvPair := new(types.StoreKVPair)
kvPair.StoreKey = storeKey.Name()
kvPair.Set = set
kvPair.Delete = Delete
kvPair.Key = key
kvPair.Value = value
by, err := wl.marshaller.MarshalBinaryLengthPrefixed(kvPair)
Expand Down Expand Up @@ -115,20 +115,20 @@ func NewStore(parent types.KVStore, psk types.StoreKey, listeners []types.WriteL
func (s *Store) Set(key []byte, value []byte) {
types.AssertValidKey(key)
s.parent.Set(key, value)
s.onWrite(true, key, value)
s.onWrite(false, key, value)
}

// Delete implements the KVStore interface. It traces a write operation and
// delegates the Delete call to the parent KVStore.
func (s *Store) Delete(key []byte) {
s.parent.Delete(key)
s.onWrite(false, key, nil)
s.onWrite(true, key, nil)
}

// onWrite writes a KVStore operation to all of the WriteListeners
func (s *Store) onWrite(set bool, key, value []byte) {
func (s *Store) onWrite(delete bool, key, value []byte) {
for _, l := range s.listeners {
if err := l.OnWrite(s.parentStoreKey, set, key, value); err != nil {
if err := l.OnWrite(s.parentStoreKey, key, value, delete); err != nil {
// log error
}
}
Expand Down
56 changes: 38 additions & 18 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
- [Output](#cosmos.bank.v1beta1.Output)
- [Params](#cosmos.bank.v1beta1.Params)
- [SendEnabled](#cosmos.bank.v1beta1.SendEnabled)
- [Supply](#cosmos.bank.v1beta1.Supply)

- [cosmos/bank/v1beta1/genesis.proto](#cosmos/bank/v1beta1/genesis.proto)
- [Balance](#cosmos.bank.v1beta1.Balance)
Expand Down Expand Up @@ -133,6 +132,9 @@
- [CommitInfo](#cosmos.base.store.v1beta1.CommitInfo)
- [StoreInfo](#cosmos.base.store.v1beta1.StoreInfo)

- [cosmos/base/store/v1beta1/listening.proto](#cosmos/base/store/v1beta1/listening.proto)
- [StoreKVPair](#cosmos.base.store.v1beta1.StoreKVPair)

- [cosmos/base/store/v1beta1/snapshot.proto](#cosmos/base/store/v1beta1/snapshot.proto)
- [SnapshotIAVLItem](#cosmos.base.store.v1beta1.SnapshotIAVLItem)
- [SnapshotItem](#cosmos.base.store.v1beta1.SnapshotItem)
Expand Down Expand Up @@ -1563,23 +1565,6 @@ sendable).




<a name="cosmos.bank.v1beta1.Supply"></a>

### Supply
Supply represents a struct that passively keeps track of the total supply
amounts in the network.
This message is deprecated now that supply is indexed by denom.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `total` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | repeated | |





<!-- end messages -->

<!-- end enums -->
Expand Down Expand Up @@ -2200,6 +2185,41 @@ between a store name and the commit ID.



<!-- end messages -->

<!-- end enums -->

<!-- end HasExtensions -->

<!-- end services -->



<a name="cosmos/base/store/v1beta1/listening.proto"></a>
<p align="right"><a href="#top">Top</a></p>

## cosmos/base/store/v1beta1/listening.proto



<a name="cosmos.base.store.v1beta1.StoreKVPair"></a>

### StoreKVPair
StoreKVPair is a KVStore KVPair used for listening to state changes (Sets and Deletes)
It optionally includes the StoreKey for the originating KVStore and a Boolean flag to distinguish between Sets and Deletes


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `store_key` | [string](#string) | | the store key for the KVStore this pair originates from |
| `delete` | [bool](#bool) | | true indicates a delete operation, false indicates a set operation |
| `key` | [bytes](#bytes) | | |
| `value` | [bytes](#bytes) | | |





<!-- end messages -->

<!-- end enums -->
Expand Down
2 changes: 1 addition & 1 deletion proto/cosmos/base/store/v1beta1/listening.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ option go_package = "github.com/cosmos/cosmos-sdk/store/types";
// It optionally includes the StoreKey for the originating KVStore and a Boolean flag to distinguish between Sets and Deletes
message StoreKVPair {
string store_key = 1; // the store key for the KVStore this pair originates from
bool set = 2; // true indicates a set operation, false indicates a delete operation
bool delete = 2; // true indicates a delete operation, false indicates a set operation
bytes key = 3;
bytes value = 4;
}
8 changes: 4 additions & 4 deletions store/listenkv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ func (s *Store) Get(key []byte) []byte {
func (s *Store) Set(key []byte, value []byte) {
types.AssertValidKey(key)
s.parent.Set(key, value)
s.onWrite(true, key, value)
s.onWrite(false, key, value)
}

// Delete implements the KVStore interface. It traces a write operation and
// delegates the Delete call to the parent KVStore.
func (s *Store) Delete(key []byte) {
s.parent.Delete(key)
s.onWrite(false, key, nil)
s.onWrite(true, key, nil)
}

// Has implements the KVStore interface. It delegates the Has call to the
Expand Down Expand Up @@ -148,8 +148,8 @@ func (s *Store) CacheWrapWithListeners(_ types.StoreKey, _ []types.WriteListener
}

// onWrite writes a KVStore operation to all of the WriteListeners
func (s *Store) onWrite(set bool, key, value []byte) {
func (s *Store) onWrite(delete bool, key, value []byte) {
for _, l := range s.listeners {
l.OnWrite(s.parentStoreKey, set, key, value)
l.OnWrite(s.parentStoreKey, key, value, delete)
}
}
8 changes: 4 additions & 4 deletions store/listenkv/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestListenKVStoreSet(t *testing.T) {
Key: kvPairs[0].Key,
Value: kvPairs[0].Value,
StoreKey: testStoreKey.Name(),
Set: true,
Delete: false,
},
},
{
Expand All @@ -99,7 +99,7 @@ func TestListenKVStoreSet(t *testing.T) {
Key: kvPairs[1].Key,
Value: kvPairs[1].Value,
StoreKey: testStoreKey.Name(),
Set: true,
Delete: false,
},
},
{
Expand All @@ -109,7 +109,7 @@ func TestListenKVStoreSet(t *testing.T) {
Key: kvPairs[2].Key,
Value: kvPairs[2].Value,
StoreKey: testStoreKey.Name(),
Set: true,
Delete: false,
},
},
}
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestListenKVStoreDelete(t *testing.T) {
Key: kvPairs[0].Key,
Value: nil,
StoreKey: testStoreKey.Name(),
Set: false,
Delete: true,
},
},
}
Expand Down
8 changes: 4 additions & 4 deletions store/rootmulti/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ func TestGetListenWrappedKVStore(t *testing.T) {
Key: testKey1,
Value: testValue1,
StoreKey: testStoreKey1.Name(),
Set: true,
Delete: false,
})
require.Nil(t, err)
kvPairSet1Bytes := buf.Bytes()
Expand All @@ -744,7 +744,7 @@ func TestGetListenWrappedKVStore(t *testing.T) {
Key: testKey1,
Value: nil,
StoreKey: testStoreKey1.Name(),
Set: false,
Delete: true,
})
require.Nil(t, err)
kvPairDelete1Bytes := buf.Bytes()
Expand All @@ -759,7 +759,7 @@ func TestGetListenWrappedKVStore(t *testing.T) {
Key: testKey2,
Value: testValue2,
StoreKey: testStoreKey2.Name(),
Set: true,
Delete: false,
})
kvPairSet2Bytes := buf.Bytes()
buf.Reset()
Expand All @@ -770,7 +770,7 @@ func TestGetListenWrappedKVStore(t *testing.T) {
Key: testKey2,
Value: nil,
StoreKey: testStoreKey2.Name(),
Set: false,
Delete: true,
})
kvPairDelete2Bytes := buf.Bytes()
buf.Reset()
Expand Down
8 changes: 4 additions & 4 deletions store/types/listening.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
type WriteListener interface {
// if value is nil then it was deleted
// storeKey indicates the source KVStore, to facilitate using the the same WriteListener across separate KVStores
// set bool indicates if it was a set; true: set, false: delete
OnWrite(storeKey StoreKey, set bool, key []byte, value []byte) error
// delete bool indicates if it was a delete; true: delete, false: set
OnWrite(storeKey StoreKey, key []byte, value []byte, delete bool) error
}

// StoreKVPairWriteListener is used to configure listening to a KVStore by writing out length-prefixed
Expand All @@ -30,10 +30,10 @@ func NewStoreKVPairWriteListener(w io.Writer, m codec.BinaryMarshaler) *StoreKVP
}

// OnWrite satisfies the WriteListener interface by writing length-prefixed protobuf encoded StoreKVPairs
func (wl *StoreKVPairWriteListener) OnWrite(storeKey StoreKey, set bool, key []byte, value []byte) error {
func (wl *StoreKVPairWriteListener) OnWrite(storeKey StoreKey, key []byte, value []byte, delete bool) error {
kvPair := new(StoreKVPair)
kvPair.StoreKey = storeKey.Name()
kvPair.Set = set
kvPair.Delete = delete
kvPair.Key = key
kvPair.Value = value
by, err := wl.marshaller.MarshalBinaryLengthPrefixed(kvPair)
Expand Down
36 changes: 18 additions & 18 deletions store/types/listening.pb.go

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

22 changes: 20 additions & 2 deletions store/types/listening_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func TestOnWrite(t *testing.T) {
testStoreKey := NewKVStoreKey("test_key")
testKey := []byte("testing123")
testValue := []byte("testing321")
err := wl.OnWrite(testStoreKey, true, testKey, testValue)

// test set
err := wl.OnWrite(testStoreKey, testKey, testValue, false)
require.Nil(t, err)

outputBytes := testWriter.Bytes()
Expand All @@ -40,7 +42,23 @@ func TestOnWrite(t *testing.T) {
Key: testKey,
Value: testValue,
StoreKey: testStoreKey.Name(),
Set: true,
Delete: false,
}
testMarshaller.UnmarshalBinaryLengthPrefixed(outputBytes, outputKVPair)
require.EqualValues(t, expectedOutputKVPair, outputKVPair)
testWriter.Reset()

// test delete
err = wl.OnWrite(testStoreKey, testKey, testValue, true)
require.Nil(t, err)

outputBytes = testWriter.Bytes()
outputKVPair = new(StoreKVPair)
expectedOutputKVPair = &StoreKVPair{
Key: testKey,
Value: testValue,
StoreKey: testStoreKey.Name(),
Delete: true,
}
testMarshaller.UnmarshalBinaryLengthPrefixed(outputBytes, outputKVPair)
require.EqualValues(t, expectedOutputKVPair, outputKVPair)
Expand Down

0 comments on commit 26cc072

Please sign in to comment.