Merge pull request #1067 from cosmos/sunny/subspace-iterator-2
SubspaceIterator as helper function
This commit is contained in:
commit
f36eb2209e
|
@ -13,6 +13,7 @@ BREAKING CHANGES
|
|||
* [x/auth] got rid of AccountMapper interface (in favor of the struct already in auth module)
|
||||
* [x/auth] removed the FeeHandler function from the AnteHandler, Replaced with FeeKeeper
|
||||
* [x/auth] Removed GetSignatures() from Tx interface (as different Tx styles might use something different than StdSignature)
|
||||
* [store] Removed SubspaceIterator and ReverseSubspaceIterator from KVStore interface and replaced them with helper functions in /types
|
||||
|
||||
BUG FIXES
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
"sort"
|
||||
"sync"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
)
|
||||
|
||||
|
@ -134,16 +133,6 @@ func (ci *cacheKVStore) ReverseIterator(start, end []byte) Iterator {
|
|||
return ci.iterator(start, end, false)
|
||||
}
|
||||
|
||||
// Implements KVStore.
|
||||
func (ci *cacheKVStore) SubspaceIterator(prefix []byte) Iterator {
|
||||
return ci.iterator(prefix, sdk.PrefixEndBytes(prefix), true)
|
||||
}
|
||||
|
||||
// Implements KVStore.
|
||||
func (ci *cacheKVStore) ReverseSubspaceIterator(prefix []byte) Iterator {
|
||||
return ci.iterator(prefix, sdk.PrefixEndBytes(prefix), false)
|
||||
}
|
||||
|
||||
func (ci *cacheKVStore) iterator(start, end []byte, ascending bool) Iterator {
|
||||
var parent, cache Iterator
|
||||
if ascending {
|
||||
|
|
|
@ -19,13 +19,5 @@ func (dsa dbStoreAdapter) CacheWrap() CacheWrap {
|
|||
return NewCacheKVStore(dsa)
|
||||
}
|
||||
|
||||
func (dsa dbStoreAdapter) SubspaceIterator(prefix []byte) Iterator {
|
||||
return dsa.Iterator(prefix, sdk.PrefixEndBytes(prefix))
|
||||
}
|
||||
|
||||
func (dsa dbStoreAdapter) ReverseSubspaceIterator(prefix []byte) Iterator {
|
||||
return dsa.ReverseIterator(prefix, sdk.PrefixEndBytes(prefix))
|
||||
}
|
||||
|
||||
// dbm.DB implements KVStore so we can CacheKVStore it.
|
||||
var _ KVStore = dbStoreAdapter{dbm.DB(nil)}
|
||||
|
|
|
@ -75,16 +75,6 @@ func (gi *gasKVStore) ReverseIterator(start, end []byte) sdk.Iterator {
|
|||
return gi.iterator(start, end, false)
|
||||
}
|
||||
|
||||
// Implements KVStore.
|
||||
func (gi *gasKVStore) SubspaceIterator(prefix []byte) sdk.Iterator {
|
||||
return gi.iterator(prefix, sdk.PrefixEndBytes(prefix), true)
|
||||
}
|
||||
|
||||
// Implements KVStore.
|
||||
func (gi *gasKVStore) ReverseSubspaceIterator(prefix []byte) sdk.Iterator {
|
||||
return gi.iterator(prefix, sdk.PrefixEndBytes(prefix), false)
|
||||
}
|
||||
|
||||
// Implements KVStore.
|
||||
func (gi *gasKVStore) CacheWrap() sdk.CacheWrap {
|
||||
panic("you cannot CacheWrap a GasKVStore")
|
||||
|
|
|
@ -125,16 +125,6 @@ func (st *iavlStore) ReverseIterator(start, end []byte) Iterator {
|
|||
return newIAVLIterator(st.tree.Tree(), start, end, false)
|
||||
}
|
||||
|
||||
// Implements KVStore.
|
||||
func (st *iavlStore) SubspaceIterator(prefix []byte) Iterator {
|
||||
return st.Iterator(prefix, sdk.PrefixEndBytes(prefix))
|
||||
}
|
||||
|
||||
// Implements KVStore.
|
||||
func (st *iavlStore) ReverseSubspaceIterator(prefix []byte) Iterator {
|
||||
return st.ReverseIterator(prefix, sdk.PrefixEndBytes(prefix))
|
||||
}
|
||||
|
||||
// Query implements ABCI interface, allows queries
|
||||
//
|
||||
// by default we will return from (latest height -1),
|
||||
|
@ -180,7 +170,7 @@ func (st *iavlStore) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
|
|||
subspace := req.Data
|
||||
res.Key = subspace
|
||||
var KVs []KVPair
|
||||
iterator := st.SubspaceIterator(subspace)
|
||||
iterator := sdk.KVStorePrefixIterator(st, subspace)
|
||||
for ; iterator.Valid(); iterator.Next() {
|
||||
KVs = append(KVs, KVPair{iterator.Key(), iterator.Value()})
|
||||
}
|
||||
|
|
|
@ -157,7 +157,7 @@ func TestIAVLSubspaceIterator(t *testing.T) {
|
|||
|
||||
i := 0
|
||||
|
||||
iter := iavlStore.SubspaceIterator([]byte("test"))
|
||||
iter := sdk.KVStorePrefixIterator(iavlStore, []byte("test"))
|
||||
expected := []string{"test1", "test2", "test3"}
|
||||
for i = 0; iter.Valid(); iter.Next() {
|
||||
expectedKey := expected[i]
|
||||
|
@ -168,7 +168,7 @@ func TestIAVLSubspaceIterator(t *testing.T) {
|
|||
}
|
||||
assert.Equal(t, len(expected), i)
|
||||
|
||||
iter = iavlStore.SubspaceIterator([]byte{byte(55), byte(255), byte(255)})
|
||||
iter = sdk.KVStorePrefixIterator(iavlStore, []byte{byte(55), byte(255), byte(255)})
|
||||
expected2 := [][]byte{
|
||||
[]byte{byte(55), byte(255), byte(255), byte(0)},
|
||||
[]byte{byte(55), byte(255), byte(255), byte(1)},
|
||||
|
@ -183,7 +183,7 @@ func TestIAVLSubspaceIterator(t *testing.T) {
|
|||
}
|
||||
assert.Equal(t, len(expected), i)
|
||||
|
||||
iter = iavlStore.SubspaceIterator([]byte{byte(255), byte(255)})
|
||||
iter = sdk.KVStorePrefixIterator(iavlStore, []byte{byte(255), byte(255)})
|
||||
expected2 = [][]byte{
|
||||
[]byte{byte(255), byte(255), byte(0)},
|
||||
[]byte{byte(255), byte(255), byte(1)},
|
||||
|
@ -216,7 +216,7 @@ func TestIAVLReverseSubspaceIterator(t *testing.T) {
|
|||
|
||||
i := 0
|
||||
|
||||
iter := iavlStore.ReverseSubspaceIterator([]byte("test"))
|
||||
iter := sdk.KVStoreReversePrefixIterator(iavlStore, []byte("test"))
|
||||
expected := []string{"test3", "test2", "test1"}
|
||||
for i = 0; iter.Valid(); iter.Next() {
|
||||
expectedKey := expected[i]
|
||||
|
@ -227,7 +227,7 @@ func TestIAVLReverseSubspaceIterator(t *testing.T) {
|
|||
}
|
||||
assert.Equal(t, len(expected), i)
|
||||
|
||||
iter = iavlStore.ReverseSubspaceIterator([]byte{byte(55), byte(255), byte(255)})
|
||||
iter = sdk.KVStoreReversePrefixIterator(iavlStore, []byte{byte(55), byte(255), byte(255)})
|
||||
expected2 := [][]byte{
|
||||
[]byte{byte(55), byte(255), byte(255), byte(255)},
|
||||
[]byte{byte(55), byte(255), byte(255), byte(1)},
|
||||
|
@ -242,7 +242,7 @@ func TestIAVLReverseSubspaceIterator(t *testing.T) {
|
|||
}
|
||||
assert.Equal(t, len(expected), i)
|
||||
|
||||
iter = iavlStore.ReverseSubspaceIterator([]byte{byte(255), byte(255)})
|
||||
iter = sdk.KVStoreReversePrefixIterator(iavlStore, []byte{byte(255), byte(255)})
|
||||
expected2 = [][]byte{
|
||||
[]byte{byte(255), byte(255), byte(255)},
|
||||
[]byte{byte(255), byte(255), byte(1)},
|
||||
|
|
|
@ -84,7 +84,7 @@ type CommitMultiStore interface {
|
|||
LoadVersion(ver int64) error
|
||||
}
|
||||
|
||||
//----------------------------------------
|
||||
//---------subsp-------------------------------
|
||||
// KVStore
|
||||
|
||||
// KVStore is a simple interface to get/set data
|
||||
|
@ -113,25 +113,26 @@ type KVStore interface {
|
|||
// CONTRACT: No writes may happen within a domain while an iterator exists over it.
|
||||
ReverseIterator(start, end []byte) Iterator
|
||||
|
||||
// Iterator over all the keys with a certain prefix in ascending order.
|
||||
// CONTRACT: No writes may happen within a domain while an iterator exists over it.
|
||||
SubspaceIterator(prefix []byte) Iterator
|
||||
|
||||
// Iterator over all the keys with a certain prefix in descending order.
|
||||
// CONTRACT: No writes may happen within a domain while an iterator exists over it.
|
||||
ReverseSubspaceIterator(prefix []byte) Iterator
|
||||
|
||||
// TODO Not yet implemented.
|
||||
// CreateSubKVStore(key *storeKey) (KVStore, error)
|
||||
|
||||
// TODO Not yet implemented.
|
||||
// GetSubKVStore(key *storeKey) KVStore
|
||||
|
||||
}
|
||||
|
||||
// Alias iterator to db's Iterator for convenience.
|
||||
type Iterator = dbm.Iterator
|
||||
|
||||
// Iterator over all the keys with a certain prefix in ascending order
|
||||
func KVStorePrefixIterator(kvs KVStore, prefix []byte) Iterator {
|
||||
return kvs.Iterator(prefix, PrefixEndBytes(prefix))
|
||||
}
|
||||
|
||||
// Iterator over all the keys with a certain prefix in descending order.
|
||||
func KVStoreReversePrefixIterator(kvs KVStore, prefix []byte) Iterator {
|
||||
return kvs.ReverseIterator(prefix, PrefixEndBytes(prefix))
|
||||
}
|
||||
|
||||
// CacheKVStore cache-wraps a KVStore. After calling .Write() on
|
||||
// the CacheKVStore, all previously created CacheKVStores on the
|
||||
// object expire.
|
||||
|
|
|
@ -58,7 +58,7 @@ func (k Keeper) setValidator(ctx sdk.Context, validator Validator) {
|
|||
// Get the set of all validators with no limits, used during genesis dump
|
||||
func (k Keeper) getAllValidators(ctx sdk.Context) (validators Validators) {
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
iterator := store.SubspaceIterator(ValidatorsKey)
|
||||
iterator := sdk.KVStorePrefixIterator(store, ValidatorsKey)
|
||||
|
||||
i := 0
|
||||
for ; ; i++ {
|
||||
|
@ -78,7 +78,7 @@ func (k Keeper) getAllValidators(ctx sdk.Context) (validators Validators) {
|
|||
// Get the set of all validators, retrieve a maxRetrieve number of records
|
||||
func (k Keeper) GetValidators(ctx sdk.Context, maxRetrieve int16) (validators Validators) {
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
iterator := store.SubspaceIterator(ValidatorsKey)
|
||||
iterator := sdk.KVStorePrefixIterator(store, ValidatorsKey)
|
||||
|
||||
validators = make([]Validator, maxRetrieve)
|
||||
i := 0
|
||||
|
@ -106,7 +106,7 @@ func (k Keeper) GetValidatorsBonded(ctx sdk.Context) (validators []Validator) {
|
|||
maxValidators := k.GetParams(ctx).MaxValidators
|
||||
validators = make([]Validator, maxValidators)
|
||||
|
||||
iterator := store.SubspaceIterator(ValidatorsBondedKey)
|
||||
iterator := sdk.KVStorePrefixIterator(store, ValidatorsBondedKey)
|
||||
i := 0
|
||||
for ; iterator.Valid(); iterator.Next() {
|
||||
|
||||
|
@ -132,7 +132,7 @@ func (k Keeper) GetValidatorsByPower(ctx sdk.Context) []Validator {
|
|||
store := ctx.KVStore(k.storeKey)
|
||||
maxValidators := k.GetParams(ctx).MaxValidators
|
||||
validators := make([]Validator, maxValidators)
|
||||
iterator := store.ReverseSubspaceIterator(ValidatorsByPowerKey) // largest to smallest
|
||||
iterator := sdk.KVStoreReversePrefixIterator(store, ValidatorsByPowerKey) // largest to smallest
|
||||
i := 0
|
||||
for {
|
||||
if !iterator.Valid() || i > int(maxValidators-1) {
|
||||
|
@ -160,7 +160,7 @@ func (k Keeper) GetValidatorsByPower(ctx sdk.Context) []Validator {
|
|||
func (k Keeper) getTendermintUpdates(ctx sdk.Context) (updates []abci.Validator) {
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
|
||||
iterator := store.SubspaceIterator(TendermintUpdatesKey) //smallest to largest
|
||||
iterator := sdk.KVStorePrefixIterator(store, TendermintUpdatesKey) //smallest to largest
|
||||
for ; iterator.Valid(); iterator.Next() {
|
||||
valBytes := iterator.Value()
|
||||
var val abci.Validator
|
||||
|
@ -176,7 +176,7 @@ func (k Keeper) clearTendermintUpdates(ctx sdk.Context) {
|
|||
store := ctx.KVStore(k.storeKey)
|
||||
|
||||
// delete subspace
|
||||
iterator := store.SubspaceIterator(TendermintUpdatesKey)
|
||||
iterator := sdk.KVStorePrefixIterator(store, TendermintUpdatesKey)
|
||||
for ; iterator.Valid(); iterator.Next() {
|
||||
store.Delete(iterator.Key())
|
||||
}
|
||||
|
@ -278,7 +278,7 @@ func (k Keeper) updateBondedValidators(ctx sdk.Context, store sdk.KVStore,
|
|||
|
||||
// add the actual validator power sorted store
|
||||
maxValidators := k.GetParams(ctx).MaxValidators
|
||||
iterator := store.ReverseSubspaceIterator(ValidatorsByPowerKey) // largest to smallest
|
||||
iterator := sdk.KVStoreReversePrefixIterator(store, ValidatorsByPowerKey) // largest to smallest
|
||||
bondedValidatorsCount := 0
|
||||
var validator Validator
|
||||
for {
|
||||
|
@ -343,7 +343,7 @@ func (k Keeper) updateBondedValidators(ctx sdk.Context, store sdk.KVStore,
|
|||
func (k Keeper) updateBondedValidatorsFull(ctx sdk.Context, store sdk.KVStore) {
|
||||
// clear the current validators store, add to the ToKickOut temp store
|
||||
toKickOut := make(map[string]byte)
|
||||
iterator := store.SubspaceIterator(ValidatorsBondedKey)
|
||||
iterator := sdk.KVStorePrefixIterator(store, ValidatorsBondedKey)
|
||||
for ; iterator.Valid(); iterator.Next() {
|
||||
ownerAddr := iterator.Value()
|
||||
toKickOut[string(ownerAddr)] = 0 // set anything
|
||||
|
@ -352,7 +352,7 @@ func (k Keeper) updateBondedValidatorsFull(ctx sdk.Context, store sdk.KVStore) {
|
|||
|
||||
// add the actual validator power sorted store
|
||||
maxValidators := k.GetParams(ctx).MaxValidators
|
||||
iterator = store.ReverseSubspaceIterator(ValidatorsByPowerKey) // largest to smallest
|
||||
iterator = sdk.KVStoreReversePrefixIterator(store, ValidatorsByPowerKey) // largest to smallest
|
||||
bondedValidatorsCount := 0
|
||||
var validator Validator
|
||||
for {
|
||||
|
@ -502,7 +502,7 @@ func (k Keeper) GetDelegation(ctx sdk.Context,
|
|||
// load all delegations used during genesis dump
|
||||
func (k Keeper) getAllDelegations(ctx sdk.Context) (delegations []Delegation) {
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
iterator := store.SubspaceIterator(DelegationKey)
|
||||
iterator := sdk.KVStorePrefixIterator(store, DelegationKey)
|
||||
|
||||
i := 0
|
||||
for ; ; i++ {
|
||||
|
@ -523,7 +523,7 @@ func (k Keeper) getAllDelegations(ctx sdk.Context) (delegations []Delegation) {
|
|||
func (k Keeper) GetDelegations(ctx sdk.Context, delegator sdk.Address, maxRetrieve int16) (bonds []Delegation) {
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
delegatorPrefixKey := GetDelegationsKey(delegator, k.cdc)
|
||||
iterator := store.SubspaceIterator(delegatorPrefixKey) //smallest to largest
|
||||
iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) //smallest to largest
|
||||
|
||||
bonds = make([]Delegation, maxRetrieve)
|
||||
i := 0
|
||||
|
@ -665,7 +665,7 @@ var _ sdk.ValidatorSet = Keeper{}
|
|||
// iterate through the active validator set and perform the provided function
|
||||
func (k Keeper) IterateValidators(ctx sdk.Context, fn func(index int64, validator sdk.Validator) (stop bool)) {
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
iterator := store.SubspaceIterator(ValidatorsKey)
|
||||
iterator := sdk.KVStorePrefixIterator(store, ValidatorsKey)
|
||||
i := int64(0)
|
||||
for ; iterator.Valid(); iterator.Next() {
|
||||
bz := iterator.Value()
|
||||
|
@ -683,7 +683,7 @@ func (k Keeper) IterateValidators(ctx sdk.Context, fn func(index int64, validato
|
|||
// iterate through the active validator set and perform the provided function
|
||||
func (k Keeper) IterateValidatorsBonded(ctx sdk.Context, fn func(index int64, validator sdk.Validator) (stop bool)) {
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
iterator := store.SubspaceIterator(ValidatorsBondedKey)
|
||||
iterator := sdk.KVStorePrefixIterator(store, ValidatorsBondedKey)
|
||||
i := int64(0)
|
||||
for ; iterator.Valid(); iterator.Next() {
|
||||
address := iterator.Value()
|
||||
|
@ -735,7 +735,7 @@ func (k Keeper) Delegation(ctx sdk.Context, addrDel sdk.Address, addrVal sdk.Add
|
|||
func (k Keeper) IterateDelegators(ctx sdk.Context, delAddr sdk.Address, fn func(index int64, delegation sdk.Delegation) (stop bool)) {
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
key := GetDelegationsKey(delAddr, k.cdc)
|
||||
iterator := store.SubspaceIterator(key)
|
||||
iterator := sdk.KVStorePrefixIterator(store, key)
|
||||
i := int64(0)
|
||||
for ; iterator.Valid(); iterator.Next() {
|
||||
bz := iterator.Value()
|
||||
|
|
Loading…
Reference in New Issue