Merge pull request #1067 from cosmos/sunny/subspace-iterator-2

SubspaceIterator as helper function
This commit is contained in:
Rigel 2018-05-27 05:17:09 -04:00 committed by GitHub
commit f36eb2209e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 33 additions and 70 deletions

View File

@ -13,6 +13,7 @@ BREAKING CHANGES
* [x/auth] got rid of AccountMapper interface (in favor of the struct already in auth module) * [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 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) * [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 BUG FIXES

View File

@ -5,7 +5,6 @@ import (
"sort" "sort"
"sync" "sync"
sdk "github.com/cosmos/cosmos-sdk/types"
cmn "github.com/tendermint/tmlibs/common" cmn "github.com/tendermint/tmlibs/common"
) )
@ -134,16 +133,6 @@ func (ci *cacheKVStore) ReverseIterator(start, end []byte) Iterator {
return ci.iterator(start, end, false) 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 { func (ci *cacheKVStore) iterator(start, end []byte, ascending bool) Iterator {
var parent, cache Iterator var parent, cache Iterator
if ascending { if ascending {

View File

@ -19,13 +19,5 @@ func (dsa dbStoreAdapter) CacheWrap() CacheWrap {
return NewCacheKVStore(dsa) 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. // dbm.DB implements KVStore so we can CacheKVStore it.
var _ KVStore = dbStoreAdapter{dbm.DB(nil)} var _ KVStore = dbStoreAdapter{dbm.DB(nil)}

View File

@ -75,16 +75,6 @@ func (gi *gasKVStore) ReverseIterator(start, end []byte) sdk.Iterator {
return gi.iterator(start, end, false) 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. // Implements KVStore.
func (gi *gasKVStore) CacheWrap() sdk.CacheWrap { func (gi *gasKVStore) CacheWrap() sdk.CacheWrap {
panic("you cannot CacheWrap a GasKVStore") panic("you cannot CacheWrap a GasKVStore")

View File

@ -125,16 +125,6 @@ func (st *iavlStore) ReverseIterator(start, end []byte) Iterator {
return newIAVLIterator(st.tree.Tree(), start, end, false) 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 // Query implements ABCI interface, allows queries
// //
// by default we will return from (latest height -1), // 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 subspace := req.Data
res.Key = subspace res.Key = subspace
var KVs []KVPair var KVs []KVPair
iterator := st.SubspaceIterator(subspace) iterator := sdk.KVStorePrefixIterator(st, subspace)
for ; iterator.Valid(); iterator.Next() { for ; iterator.Valid(); iterator.Next() {
KVs = append(KVs, KVPair{iterator.Key(), iterator.Value()}) KVs = append(KVs, KVPair{iterator.Key(), iterator.Value()})
} }

View File

@ -157,7 +157,7 @@ func TestIAVLSubspaceIterator(t *testing.T) {
i := 0 i := 0
iter := iavlStore.SubspaceIterator([]byte("test")) iter := sdk.KVStorePrefixIterator(iavlStore, []byte("test"))
expected := []string{"test1", "test2", "test3"} expected := []string{"test1", "test2", "test3"}
for i = 0; iter.Valid(); iter.Next() { for i = 0; iter.Valid(); iter.Next() {
expectedKey := expected[i] expectedKey := expected[i]
@ -168,7 +168,7 @@ func TestIAVLSubspaceIterator(t *testing.T) {
} }
assert.Equal(t, len(expected), i) 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{ expected2 := [][]byte{
[]byte{byte(55), byte(255), byte(255), byte(0)}, []byte{byte(55), byte(255), byte(255), byte(0)},
[]byte{byte(55), byte(255), byte(255), byte(1)}, []byte{byte(55), byte(255), byte(255), byte(1)},
@ -183,7 +183,7 @@ func TestIAVLSubspaceIterator(t *testing.T) {
} }
assert.Equal(t, len(expected), i) 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{ expected2 = [][]byte{
[]byte{byte(255), byte(255), byte(0)}, []byte{byte(255), byte(255), byte(0)},
[]byte{byte(255), byte(255), byte(1)}, []byte{byte(255), byte(255), byte(1)},
@ -216,7 +216,7 @@ func TestIAVLReverseSubspaceIterator(t *testing.T) {
i := 0 i := 0
iter := iavlStore.ReverseSubspaceIterator([]byte("test")) iter := sdk.KVStoreReversePrefixIterator(iavlStore, []byte("test"))
expected := []string{"test3", "test2", "test1"} expected := []string{"test3", "test2", "test1"}
for i = 0; iter.Valid(); iter.Next() { for i = 0; iter.Valid(); iter.Next() {
expectedKey := expected[i] expectedKey := expected[i]
@ -227,7 +227,7 @@ func TestIAVLReverseSubspaceIterator(t *testing.T) {
} }
assert.Equal(t, len(expected), i) 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{ expected2 := [][]byte{
[]byte{byte(55), byte(255), byte(255), byte(255)}, []byte{byte(55), byte(255), byte(255), byte(255)},
[]byte{byte(55), byte(255), byte(255), byte(1)}, []byte{byte(55), byte(255), byte(255), byte(1)},
@ -242,7 +242,7 @@ func TestIAVLReverseSubspaceIterator(t *testing.T) {
} }
assert.Equal(t, len(expected), i) 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{ expected2 = [][]byte{
[]byte{byte(255), byte(255), byte(255)}, []byte{byte(255), byte(255), byte(255)},
[]byte{byte(255), byte(255), byte(1)}, []byte{byte(255), byte(255), byte(1)},

View File

@ -84,7 +84,7 @@ type CommitMultiStore interface {
LoadVersion(ver int64) error LoadVersion(ver int64) error
} }
//---------------------------------------- //---------subsp-------------------------------
// KVStore // KVStore
// KVStore is a simple interface to get/set data // 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. // CONTRACT: No writes may happen within a domain while an iterator exists over it.
ReverseIterator(start, end []byte) Iterator 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. // TODO Not yet implemented.
// CreateSubKVStore(key *storeKey) (KVStore, error) // CreateSubKVStore(key *storeKey) (KVStore, error)
// TODO Not yet implemented. // TODO Not yet implemented.
// GetSubKVStore(key *storeKey) KVStore // GetSubKVStore(key *storeKey) KVStore
} }
// Alias iterator to db's Iterator for convenience. // Alias iterator to db's Iterator for convenience.
type Iterator = dbm.Iterator 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 // CacheKVStore cache-wraps a KVStore. After calling .Write() on
// the CacheKVStore, all previously created CacheKVStores on the // the CacheKVStore, all previously created CacheKVStores on the
// object expire. // object expire.

View File

@ -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 // Get the set of all validators with no limits, used during genesis dump
func (k Keeper) getAllValidators(ctx sdk.Context) (validators Validators) { func (k Keeper) getAllValidators(ctx sdk.Context) (validators Validators) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
iterator := store.SubspaceIterator(ValidatorsKey) iterator := sdk.KVStorePrefixIterator(store, ValidatorsKey)
i := 0 i := 0
for ; ; i++ { 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 // Get the set of all validators, retrieve a maxRetrieve number of records
func (k Keeper) GetValidators(ctx sdk.Context, maxRetrieve int16) (validators Validators) { func (k Keeper) GetValidators(ctx sdk.Context, maxRetrieve int16) (validators Validators) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
iterator := store.SubspaceIterator(ValidatorsKey) iterator := sdk.KVStorePrefixIterator(store, ValidatorsKey)
validators = make([]Validator, maxRetrieve) validators = make([]Validator, maxRetrieve)
i := 0 i := 0
@ -106,7 +106,7 @@ func (k Keeper) GetValidatorsBonded(ctx sdk.Context) (validators []Validator) {
maxValidators := k.GetParams(ctx).MaxValidators maxValidators := k.GetParams(ctx).MaxValidators
validators = make([]Validator, maxValidators) validators = make([]Validator, maxValidators)
iterator := store.SubspaceIterator(ValidatorsBondedKey) iterator := sdk.KVStorePrefixIterator(store, ValidatorsBondedKey)
i := 0 i := 0
for ; iterator.Valid(); iterator.Next() { for ; iterator.Valid(); iterator.Next() {
@ -132,7 +132,7 @@ func (k Keeper) GetValidatorsByPower(ctx sdk.Context) []Validator {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
maxValidators := k.GetParams(ctx).MaxValidators maxValidators := k.GetParams(ctx).MaxValidators
validators := make([]Validator, maxValidators) validators := make([]Validator, maxValidators)
iterator := store.ReverseSubspaceIterator(ValidatorsByPowerKey) // largest to smallest iterator := sdk.KVStoreReversePrefixIterator(store, ValidatorsByPowerKey) // largest to smallest
i := 0 i := 0
for { for {
if !iterator.Valid() || i > int(maxValidators-1) { 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) { func (k Keeper) getTendermintUpdates(ctx sdk.Context) (updates []abci.Validator) {
store := ctx.KVStore(k.storeKey) 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() { for ; iterator.Valid(); iterator.Next() {
valBytes := iterator.Value() valBytes := iterator.Value()
var val abci.Validator var val abci.Validator
@ -176,7 +176,7 @@ func (k Keeper) clearTendermintUpdates(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
// delete subspace // delete subspace
iterator := store.SubspaceIterator(TendermintUpdatesKey) iterator := sdk.KVStorePrefixIterator(store, TendermintUpdatesKey)
for ; iterator.Valid(); iterator.Next() { for ; iterator.Valid(); iterator.Next() {
store.Delete(iterator.Key()) 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 // add the actual validator power sorted store
maxValidators := k.GetParams(ctx).MaxValidators maxValidators := k.GetParams(ctx).MaxValidators
iterator := store.ReverseSubspaceIterator(ValidatorsByPowerKey) // largest to smallest iterator := sdk.KVStoreReversePrefixIterator(store, ValidatorsByPowerKey) // largest to smallest
bondedValidatorsCount := 0 bondedValidatorsCount := 0
var validator Validator var validator Validator
for { 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) { func (k Keeper) updateBondedValidatorsFull(ctx sdk.Context, store sdk.KVStore) {
// clear the current validators store, add to the ToKickOut temp store // clear the current validators store, add to the ToKickOut temp store
toKickOut := make(map[string]byte) toKickOut := make(map[string]byte)
iterator := store.SubspaceIterator(ValidatorsBondedKey) iterator := sdk.KVStorePrefixIterator(store, ValidatorsBondedKey)
for ; iterator.Valid(); iterator.Next() { for ; iterator.Valid(); iterator.Next() {
ownerAddr := iterator.Value() ownerAddr := iterator.Value()
toKickOut[string(ownerAddr)] = 0 // set anything 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 // add the actual validator power sorted store
maxValidators := k.GetParams(ctx).MaxValidators maxValidators := k.GetParams(ctx).MaxValidators
iterator = store.ReverseSubspaceIterator(ValidatorsByPowerKey) // largest to smallest iterator = sdk.KVStoreReversePrefixIterator(store, ValidatorsByPowerKey) // largest to smallest
bondedValidatorsCount := 0 bondedValidatorsCount := 0
var validator Validator var validator Validator
for { for {
@ -502,7 +502,7 @@ func (k Keeper) GetDelegation(ctx sdk.Context,
// load all delegations used during genesis dump // load all delegations used during genesis dump
func (k Keeper) getAllDelegations(ctx sdk.Context) (delegations []Delegation) { func (k Keeper) getAllDelegations(ctx sdk.Context) (delegations []Delegation) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
iterator := store.SubspaceIterator(DelegationKey) iterator := sdk.KVStorePrefixIterator(store, DelegationKey)
i := 0 i := 0
for ; ; i++ { 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) { func (k Keeper) GetDelegations(ctx sdk.Context, delegator sdk.Address, maxRetrieve int16) (bonds []Delegation) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
delegatorPrefixKey := GetDelegationsKey(delegator, k.cdc) delegatorPrefixKey := GetDelegationsKey(delegator, k.cdc)
iterator := store.SubspaceIterator(delegatorPrefixKey) //smallest to largest iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) //smallest to largest
bonds = make([]Delegation, maxRetrieve) bonds = make([]Delegation, maxRetrieve)
i := 0 i := 0
@ -665,7 +665,7 @@ var _ sdk.ValidatorSet = Keeper{}
// iterate through the active validator set and perform the provided function // 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)) { func (k Keeper) IterateValidators(ctx sdk.Context, fn func(index int64, validator sdk.Validator) (stop bool)) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
iterator := store.SubspaceIterator(ValidatorsKey) iterator := sdk.KVStorePrefixIterator(store, ValidatorsKey)
i := int64(0) i := int64(0)
for ; iterator.Valid(); iterator.Next() { for ; iterator.Valid(); iterator.Next() {
bz := iterator.Value() 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 // 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)) { func (k Keeper) IterateValidatorsBonded(ctx sdk.Context, fn func(index int64, validator sdk.Validator) (stop bool)) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
iterator := store.SubspaceIterator(ValidatorsBondedKey) iterator := sdk.KVStorePrefixIterator(store, ValidatorsBondedKey)
i := int64(0) i := int64(0)
for ; iterator.Valid(); iterator.Next() { for ; iterator.Valid(); iterator.Next() {
address := iterator.Value() 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)) { func (k Keeper) IterateDelegators(ctx sdk.Context, delAddr sdk.Address, fn func(index int64, delegation sdk.Delegation) (stop bool)) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
key := GetDelegationsKey(delAddr, k.cdc) key := GetDelegationsKey(delAddr, k.cdc)
iterator := store.SubspaceIterator(key) iterator := sdk.KVStorePrefixIterator(store, key)
i := int64(0) i := int64(0)
for ; iterator.Valid(); iterator.Next() { for ; iterator.Valid(); iterator.Next() {
bz := iterator.Value() bz := iterator.Value()