stake cleanup, functionality for total prevote power

This commit is contained in:
rigelrozanski 2018-05-03 13:38:02 -04:00
parent aeabdcae5d
commit 750cbc53ec
19 changed files with 167 additions and 184 deletions

View File

@ -2,6 +2,11 @@
## 0.18.0 (TBD) ## 0.18.0 (TBD)
BREAKING CHANGES
* Queries against the store must be prefixed with the path "/store"
* RecentValidator store now take pubkey instead of address, is sorted like Tendermint by pk's address
FEATURES FEATURES
* [x/auth] Added ability to change pubkey to auth module * [x/auth] Added ability to change pubkey to auth module
@ -53,7 +58,6 @@ BREAKING CHANGES
* gaiad init now requires use of `--name` flag * gaiad init now requires use of `--name` flag
* Removed Get from Msg interface * Removed Get from Msg interface
* types/rational now extends big.Rat * types/rational now extends big.Rat
* Queries against the store must be prefixed with the path "/store"
FEATURES: FEATURES:

View File

@ -234,9 +234,9 @@ func (app *BaseApp) initFromStore(mainKey sdk.StoreKey) error {
// NewContext returns a new Context with the correct store, the given header, and nil txBytes. // NewContext returns a new Context with the correct store, the given header, and nil txBytes.
func (app *BaseApp) NewContext(isCheckTx bool, header abci.Header) sdk.Context { func (app *BaseApp) NewContext(isCheckTx bool, header abci.Header) sdk.Context {
if isCheckTx { if isCheckTx {
return sdk.NewContext(app.checkState.ms, header, true, nil, app.Logger) return sdk.NewContext(app.checkState.ms, header, true, nil, app.Logger, nil)
} }
return sdk.NewContext(app.deliverState.ms, header, false, nil, app.Logger) return sdk.NewContext(app.deliverState.ms, header, false, nil, app.Logger, nil)
} }
type state struct { type state struct {
@ -252,7 +252,7 @@ func (app *BaseApp) setCheckState(header abci.Header) {
ms := app.cms.CacheMultiStore() ms := app.cms.CacheMultiStore()
app.checkState = &state{ app.checkState = &state{
ms: ms, ms: ms,
ctx: sdk.NewContext(ms, header, true, nil, app.Logger), ctx: sdk.NewContext(ms, header, true, nil, app.Logger, nil),
} }
} }
@ -260,7 +260,7 @@ func (app *BaseApp) setDeliverState(header abci.Header) {
ms := app.cms.CacheMultiStore() ms := app.cms.CacheMultiStore()
app.deliverState = &state{ app.deliverState = &state{
ms: ms, ms: ms,
ctx: sdk.NewContext(ms, header, false, nil, app.Logger), ctx: sdk.NewContext(ms, header, false, nil, app.Logger, nil),
} }
} }

View File

@ -30,7 +30,7 @@ func TestCoolKeeper(t *testing.T) {
auth.RegisterBaseAccount(cdc) auth.RegisterBaseAccount(cdc)
am := auth.NewAccountMapper(cdc, capKey, &auth.BaseAccount{}) am := auth.NewAccountMapper(cdc, capKey, &auth.BaseAccount{})
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, nil) ctx := sdk.NewContext(ms, abci.Header{}, false, nil, nil, nil)
ck := bank.NewKeeper(am) ck := bank.NewKeeper(am)
keeper := NewKeeper(capKey, ck, DefaultCodespace) keeper := NewKeeper(capKey, ck, DefaultCodespace)

View File

@ -20,7 +20,7 @@ func TestPowHandler(t *testing.T) {
auth.RegisterBaseAccount(cdc) auth.RegisterBaseAccount(cdc)
am := auth.NewAccountMapper(cdc, capKey, &auth.BaseAccount{}) am := auth.NewAccountMapper(cdc, capKey, &auth.BaseAccount{})
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger()) ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger(), nil)
config := NewConfig("pow", int64(1)) config := NewConfig("pow", int64(1))
ck := bank.NewKeeper(am) ck := bank.NewKeeper(am)
keeper := NewKeeper(capKey, config, ck, DefaultCodespace) keeper := NewKeeper(capKey, config, ck, DefaultCodespace)

View File

@ -33,7 +33,7 @@ func TestPowKeeperGetSet(t *testing.T) {
auth.RegisterBaseAccount(cdc) auth.RegisterBaseAccount(cdc)
am := auth.NewAccountMapper(cdc, capKey, &auth.BaseAccount{}) am := auth.NewAccountMapper(cdc, capKey, &auth.BaseAccount{})
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger()) ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger(), nil)
config := NewConfig("pow", int64(1)) config := NewConfig("pow", int64(1))
ck := bank.NewKeeper(am) ck := bank.NewKeeper(am)
keeper := NewKeeper(capKey, config, ck, DefaultCodespace) keeper := NewKeeper(capKey, config, ck, DefaultCodespace)

View File

@ -33,7 +33,7 @@ func setupMultiStore() (sdk.MultiStore, *sdk.KVStoreKey, *sdk.KVStoreKey) {
func TestKeeperGetSet(t *testing.T) { func TestKeeperGetSet(t *testing.T) {
ms, _, capKey := setupMultiStore() ms, _, capKey := setupMultiStore()
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger()) ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger(), nil)
stakeKeeper := NewKeeper(capKey, bank.NewKeeper(nil), DefaultCodespace) stakeKeeper := NewKeeper(capKey, bank.NewKeeper(nil), DefaultCodespace)
addr := sdk.Address([]byte("some-address")) addr := sdk.Address([]byte("some-address"))
@ -60,7 +60,7 @@ func TestBonding(t *testing.T) {
cdc := wire.NewCodec() cdc := wire.NewCodec()
auth.RegisterBaseAccount(cdc) auth.RegisterBaseAccount(cdc)
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger()) ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger(), nil)
accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{}) accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{})
coinKeeper := bank.NewKeeper(accountMapper) coinKeeper := bank.NewKeeper(accountMapper)

View File

@ -30,7 +30,9 @@ type Context struct {
} }
// create a new context // create a new context
func NewContext(ms MultiStore, header abci.Header, isCheckTx bool, txBytes []byte, logger log.Logger) Context { func NewContext(ms MultiStore, header abci.Header, isCheckTx bool,
txBytes []byte, logger log.Logger, absentValidators []int32) Context {
c := Context{ c := Context{
Context: context.Background(), Context: context.Background(),
pst: newThePast(), pst: newThePast(),
@ -44,6 +46,7 @@ func NewContext(ms MultiStore, header abci.Header, isCheckTx bool, txBytes []byt
c = c.WithTxBytes(txBytes) c = c.WithTxBytes(txBytes)
c = c.WithLogger(logger) c = c.WithLogger(logger)
c = c.WithGasMeter(NewInfiniteGasMeter()) c = c.WithGasMeter(NewInfiniteGasMeter())
c = c.WithAbsentValidators(absentValidators)
return c return c
} }

View File

@ -43,7 +43,7 @@ func (l MockLogger) With(kvs ...interface{}) log.Logger {
func TestContextGetOpShouldNeverPanic(t *testing.T) { func TestContextGetOpShouldNeverPanic(t *testing.T) {
var ms types.MultiStore var ms types.MultiStore
ctx := types.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger()) ctx := types.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger(), nil)
indices := []int64{ indices := []int64{
-10, 1, 0, 10, 20, -10, 1, 0, 10, 20,
} }
@ -58,7 +58,7 @@ func defaultContext(key types.StoreKey) types.Context {
cms := store.NewCommitMultiStore(db) cms := store.NewCommitMultiStore(db)
cms.MountStoreWithDB(key, types.StoreTypeIAVL, db) cms.MountStoreWithDB(key, types.StoreTypeIAVL, db)
cms.LoadLatestVersion() cms.LoadLatestVersion()
ctx := types.NewContext(cms, abci.Header{}, false, nil, log.NewNopLogger()) ctx := types.NewContext(cms, abci.Header{}, false, nil, log.NewNopLogger(), nil)
return ctx return ctx
} }

View File

@ -25,7 +25,7 @@ func defaultComponents(key sdk.StoreKey) (sdk.Context, *wire.Codec) {
cms := store.NewCommitMultiStore(db) cms := store.NewCommitMultiStore(db)
cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db) cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db)
cms.LoadLatestVersion() cms.LoadLatestVersion()
ctx := sdk.NewContext(cms, abci.Header{}, false, nil, log.NewNopLogger()) ctx := sdk.NewContext(cms, abci.Header{}, false, nil, log.NewNopLogger(), nil)
cdc := wire.NewCodec() cdc := wire.NewCodec()
return ctx, cdc return ctx, cdc
} }

View File

@ -74,7 +74,7 @@ func TestAnteHandlerSigErrors(t *testing.T) {
RegisterBaseAccount(cdc) RegisterBaseAccount(cdc)
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{}) mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
anteHandler := NewAnteHandler(mapper, BurnFeeHandler) anteHandler := NewAnteHandler(mapper, BurnFeeHandler)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger()) ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger(), nil)
// keys and addresses // keys and addresses
priv1, addr1 := privAndAddr() priv1, addr1 := privAndAddr()
@ -115,7 +115,7 @@ func TestAnteHandlerSequences(t *testing.T) {
RegisterBaseAccount(cdc) RegisterBaseAccount(cdc)
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{}) mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
anteHandler := NewAnteHandler(mapper, BurnFeeHandler) anteHandler := NewAnteHandler(mapper, BurnFeeHandler)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger()) ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger(), nil)
// keys and addresses // keys and addresses
priv1, addr1 := privAndAddr() priv1, addr1 := privAndAddr()
@ -181,7 +181,7 @@ func TestAnteHandlerFees(t *testing.T) {
RegisterBaseAccount(cdc) RegisterBaseAccount(cdc)
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{}) mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
anteHandler := NewAnteHandler(mapper, BurnFeeHandler) anteHandler := NewAnteHandler(mapper, BurnFeeHandler)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger()) ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger(), nil)
// keys and addresses // keys and addresses
priv1, addr1 := privAndAddr() priv1, addr1 := privAndAddr()
@ -218,7 +218,7 @@ func TestAnteHandlerBadSignBytes(t *testing.T) {
RegisterBaseAccount(cdc) RegisterBaseAccount(cdc)
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{}) mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
anteHandler := NewAnteHandler(mapper, BurnFeeHandler) anteHandler := NewAnteHandler(mapper, BurnFeeHandler)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger()) ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger(), nil)
// keys and addresses // keys and addresses
priv1, addr1 := privAndAddr() priv1, addr1 := privAndAddr()
@ -293,7 +293,7 @@ func TestAnteHandlerSetPubKey(t *testing.T) {
RegisterBaseAccount(cdc) RegisterBaseAccount(cdc)
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{}) mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
anteHandler := NewAnteHandler(mapper, BurnFeeHandler) anteHandler := NewAnteHandler(mapper, BurnFeeHandler)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger()) ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger(), nil)
// keys and addresses // keys and addresses
priv1, addr1 := privAndAddr() priv1, addr1 := privAndAddr()

View File

@ -13,7 +13,7 @@ import (
func TestContextWithSigners(t *testing.T) { func TestContextWithSigners(t *testing.T) {
ms, _ := setupMultiStore() ms, _ := setupMultiStore()
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger()) ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger(), nil)
_, _, addr1 := keyPubAddr() _, _, addr1 := keyPubAddr()
_, _, addr2 := keyPubAddr() _, _, addr2 := keyPubAddr()

View File

@ -29,7 +29,7 @@ func TestAccountMapperGetSet(t *testing.T) {
RegisterBaseAccount(cdc) RegisterBaseAccount(cdc)
// make context and mapper // make context and mapper
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger()) ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger(), nil)
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{}) mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
addr := sdk.Address([]byte("some-address")) addr := sdk.Address([]byte("some-address"))

View File

@ -31,7 +31,7 @@ func TestKeeper(t *testing.T) {
cdc := wire.NewCodec() cdc := wire.NewCodec()
auth.RegisterBaseAccount(cdc) auth.RegisterBaseAccount(cdc)
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger()) ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger(), nil)
accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{}) accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{})
coinKeeper := NewKeeper(accountMapper) coinKeeper := NewKeeper(accountMapper)
@ -116,7 +116,7 @@ func TestSendKeeper(t *testing.T) {
cdc := wire.NewCodec() cdc := wire.NewCodec()
auth.RegisterBaseAccount(cdc) auth.RegisterBaseAccount(cdc)
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger()) ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger(), nil)
accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{}) accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{})
coinKeeper := NewKeeper(accountMapper) coinKeeper := NewKeeper(accountMapper)
sendKeeper := NewSendKeeper(accountMapper) sendKeeper := NewSendKeeper(accountMapper)
@ -185,7 +185,7 @@ func TestViewKeeper(t *testing.T) {
cdc := wire.NewCodec() cdc := wire.NewCodec()
auth.RegisterBaseAccount(cdc) auth.RegisterBaseAccount(cdc)
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger()) ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger(), nil)
accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{}) accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{})
coinKeeper := NewKeeper(accountMapper) coinKeeper := NewKeeper(accountMapper)
viewKeeper := NewViewKeeper(accountMapper) viewKeeper := NewViewKeeper(accountMapper)

View File

@ -24,7 +24,7 @@ func defaultContext(key sdk.StoreKey) sdk.Context {
cms := store.NewCommitMultiStore(db) cms := store.NewCommitMultiStore(db)
cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db) cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db)
cms.LoadLatestVersion() cms.LoadLatestVersion()
ctx := sdk.NewContext(cms, abci.Header{}, false, nil, log.NewNopLogger()) ctx := sdk.NewContext(cms, abci.Header{}, false, nil, log.NewNopLogger(), nil)
return ctx return ctx
} }

View File

@ -7,6 +7,7 @@ import (
"github.com/cosmos/cosmos-sdk/wire" "github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/bank" "github.com/cosmos/cosmos-sdk/x/bank"
abci "github.com/tendermint/abci/types" abci "github.com/tendermint/abci/types"
crypto "github.com/tendermint/go-crypto"
) )
// keeper of the staking store // keeper of the staking store
@ -41,20 +42,14 @@ func (k Keeper) getCounter(ctx sdk.Context) int16 {
return 0 return 0
} }
var counter int16 var counter int16
err := k.cdc.UnmarshalBinary(b, &counter) k.cdc.MustUnmarshalBinary(b, &counter)
if err != nil {
panic(err)
}
return counter return counter
} }
// set the current in-block validator operation counter // set the current in-block validator operation counter
func (k Keeper) setCounter(ctx sdk.Context, counter int16) { func (k Keeper) setCounter(ctx sdk.Context, counter int16) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
bz, err := k.cdc.MarshalBinary(counter) bz := k.cdc.MustMarshalBinary(counter)
if err != nil {
panic(err)
}
store.Set(CounterKey, bz) store.Set(CounterKey, bz)
} }
@ -67,10 +62,7 @@ func (k Keeper) GetCandidate(ctx sdk.Context, addr sdk.Address) (candidate Candi
if b == nil { if b == nil {
return candidate, false return candidate, false
} }
err := k.cdc.UnmarshalBinary(b, &candidate) k.cdc.MustUnmarshalBinary(b, &candidate)
if err != nil {
panic(err)
}
return candidate, true return candidate, true
} }
@ -88,10 +80,7 @@ func (k Keeper) GetCandidates(ctx sdk.Context, maxRetrieve int16) (candidates Ca
} }
bz := iterator.Value() bz := iterator.Value()
var candidate Candidate var candidate Candidate
err := k.cdc.UnmarshalBinary(bz, &candidate) k.cdc.MustUnmarshalBinary(bz, &candidate)
if err != nil {
panic(err)
}
candidates[i] = candidate candidates[i] = candidate
iterator.Next() iterator.Next()
} }
@ -112,11 +101,8 @@ func (k Keeper) setCandidate(ctx sdk.Context, candidate Candidate) {
} }
// marshal the candidate record and add to the state // marshal the candidate record and add to the state
bz, err := k.cdc.MarshalBinary(candidate) bz := k.cdc.MustMarshalBinary(candidate)
if err != nil { store.Set(GetCandidateKey(address), bz)
panic(err)
}
store.Set(GetCandidateKey(candidate.Address), bz)
// if the voting power is the same no need to update any of the other indexes // if the voting power is the same no need to update any of the other indexes
if oldFound && oldCandidate.Assets.Equal(candidate.Assets) { if oldFound && oldCandidate.Assets.Equal(candidate.Assets) {
@ -127,7 +113,7 @@ func (k Keeper) setCandidate(ctx sdk.Context, candidate Candidate) {
// update the list ordered by voting power // update the list ordered by voting power
if oldFound { if oldFound {
if !k.isNewValidator(ctx, store, candidate.Address) { if !k.isNewValidator(ctx, store, address) {
updateHeight = true updateHeight = true
} }
// else already in the validator set - retain the old validator height and counter // else already in the validator set - retain the old validator height and counter
@ -145,37 +131,28 @@ func (k Keeper) setCandidate(ctx sdk.Context, candidate Candidate) {
} }
// update the candidate record // update the candidate record
bz, err = k.cdc.MarshalBinary(candidate) bz = k.cdc.MustMarshalBinary(candidate)
if err != nil { store.Set(GetCandidateKey(address), bz)
panic(err)
}
store.Set(GetCandidateKey(candidate.Address), bz)
// marshal the new validator record // marshal the new validator record
validator := candidate.validator() validator := candidate.validator()
bz, err = k.cdc.MarshalBinary(validator) bz = k.cdc.MustMarshalBinary(validator)
if err != nil {
panic(err)
}
store.Set(GetValidatorKey(address, validator.Power, validator.Height, validator.Counter, k.cdc), bz) store.Set(GetValidatorKey(address, validator.Power, validator.Height, validator.Counter, k.cdc), bz)
// add to the validators to update list if is already a validator // add to the validators to update list if is already a validator
// or is a new validator // or is a new validator
setAcc := false setAcc := false
if store.Get(GetRecentValidatorKey(address)) != nil { if store.Get(GetRecentValidatorKey(candidate.PubKey)) != nil {
setAcc = true setAcc = true
// want to check in the else statement because inefficient // want to check in the else statement because inefficient
} else if k.isNewValidator(ctx, store, address) { } else if k.isNewValidator(ctx, store, address) {
setAcc = true setAcc = true
} }
if setAcc { if setAcc {
bz, err = k.cdc.MarshalBinary(validator.abciValidator(k.cdc)) bz = k.cdc.MustMarshalBinary(validator.abciValidator(k.cdc))
if err != nil { store.Set(GetAccUpdateValidatorKey(address), bz)
panic(err)
}
store.Set(GetAccUpdateValidatorKey(validator.Address), bz)
} }
@ -197,15 +174,12 @@ func (k Keeper) removeCandidate(ctx sdk.Context, address sdk.Address) {
// delete from recent and power weighted validator groups if the validator // delete from recent and power weighted validator groups if the validator
// exists and add validator with zero power to the validator updates // exists and add validator with zero power to the validator updates
if store.Get(GetRecentValidatorKey(address)) == nil { if store.Get(GetRecentValidatorKey(candidate.PubKey)) == nil {
return return
} }
bz, err := k.cdc.MarshalBinary(candidate.validator().abciValidatorZero(k.cdc)) bz := k.cdc.MustMarshalBinary(candidate.validator().abciValidatorZero(k.cdc))
if err != nil {
panic(err)
}
store.Set(GetAccUpdateValidatorKey(address), bz) store.Set(GetAccUpdateValidatorKey(address), bz)
store.Delete(GetRecentValidatorKey(address)) store.Delete(GetRecentValidatorKey(candidate.PubKey))
} }
//___________________________________________________________________________ //___________________________________________________________________________
@ -222,7 +196,11 @@ func (k Keeper) GetValidators(ctx sdk.Context) (validators []Validator) {
// clear the recent validators store, add to the ToKickOut Temp store // clear the recent validators store, add to the ToKickOut Temp store
iterator := store.SubspaceIterator(RecentValidatorsKey) iterator := store.SubspaceIterator(RecentValidatorsKey)
for ; iterator.Valid(); iterator.Next() { for ; iterator.Valid(); iterator.Next() {
addr := AddrFromKey(iterator.Key())
bz := iterator.Value()
var validator Validator
k.cdc.MustUnmarshalBinary(bz, &validator)
addr := validator.Address
// iterator.Value is the validator object // iterator.Value is the validator object
store.Set(GetToKickOutValidatorKey(addr), iterator.Value()) store.Set(GetToKickOutValidatorKey(addr), iterator.Value())
@ -242,17 +220,14 @@ func (k Keeper) GetValidators(ctx sdk.Context) (validators []Validator) {
} }
bz := iterator.Value() bz := iterator.Value()
var validator Validator var validator Validator
err := k.cdc.UnmarshalBinary(bz, &validator) k.cdc.MustUnmarshalBinary(bz, &validator)
if err != nil {
panic(err)
}
validators[i] = validator validators[i] = validator
// remove from ToKickOut group // remove from ToKickOut group
store.Delete(GetToKickOutValidatorKey(validator.Address)) store.Delete(GetToKickOutValidatorKey(validator.Address))
// also add to the recent validators group // also add to the recent validators group
store.Set(GetRecentValidatorKey(validator.Address), bz) store.Set(GetRecentValidatorKey(validator.PubKey), bz)
iterator.Next() iterator.Next()
} }
@ -266,14 +241,8 @@ func (k Keeper) GetValidators(ctx sdk.Context) (validators []Validator) {
// get the zero abci validator from the ToKickOut iterator value // get the zero abci validator from the ToKickOut iterator value
bz := iterator.Value() bz := iterator.Value()
var validator Validator var validator Validator
err := k.cdc.UnmarshalBinary(bz, &validator) k.cdc.MustUnmarshalBinary(bz, &validator)
if err != nil { bz = k.cdc.MustMarshalBinary(validator.abciValidatorZero(k.cdc))
panic(err)
}
bz, err = k.cdc.MarshalBinary(validator.abciValidatorZero(k.cdc))
if err != nil {
panic(err)
}
store.Set(GetAccUpdateValidatorKey(addr), bz) store.Set(GetAccUpdateValidatorKey(addr), bz)
store.Delete(key) store.Delete(key)
@ -297,10 +266,7 @@ func (k Keeper) isNewValidator(ctx sdk.Context, store sdk.KVStore, address sdk.A
} }
bz := iterator.Value() bz := iterator.Value()
var val Validator var val Validator
err := k.cdc.UnmarshalBinary(bz, &val) k.cdc.MustUnmarshalBinary(bz, &val)
if err != nil {
panic(err)
}
if bytes.Equal(val.Address, address) { if bytes.Equal(val.Address, address) {
return true return true
} }
@ -311,14 +277,53 @@ func (k Keeper) isNewValidator(ctx sdk.Context, store sdk.KVStore, address sdk.A
} }
// Is the address provided a part of the most recently saved validator group? // Is the address provided a part of the most recently saved validator group?
func (k Keeper) IsRecentValidator(ctx sdk.Context, address sdk.Address) bool { func (k Keeper) IsRecentValidator(ctx sdk.Context, pk crypto.PubKey) bool {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
if store.Get(GetRecentValidatorKey(address)) == nil { if store.Get(GetRecentValidatorKey(pk)) == nil {
return false return false
} }
return true return true
} }
// Is the power of non-absent prevotes
func (k Keeper) GetTotalPrecommitVotingPower(ctx sdk.Context) sdk.Rat {
store := ctx.KVStore(k.storeKey)
// get absent prevote indexes
absents := ctx.AbsentValidators()
TotalPower := sdk.ZeroRat()
i := int32(0)
iterator := store.SubspaceIterator(RecentValidatorsKey)
for ; iterator.Valid(); iterator.Next() {
skip := false
for j, absentIndex := range absents {
if absentIndex > i {
break
}
// if non-voting validator found, skip adding its power
if absentIndex == i {
absents = append(absents[:j], absents[j+1:]...) // won't need again
skip = true
break
}
}
if skip {
break
}
bz := iterator.Value()
var validator Validator
k.cdc.MustUnmarshalBinary(bz, &validator)
TotalPower = TotalPower.Add(validator.Power)
i++
}
iterator.Close()
return TotalPower
}
//_________________________________________________________________________ //_________________________________________________________________________
// Accumulated updates to the validator set // Accumulated updates to the validator set
@ -330,10 +335,7 @@ func (k Keeper) getAccUpdateValidators(ctx sdk.Context) (updates []abci.Validato
for ; iterator.Valid(); iterator.Next() { for ; iterator.Valid(); iterator.Next() {
valBytes := iterator.Value() valBytes := iterator.Value()
var val abci.Validator var val abci.Validator
err := k.cdc.UnmarshalBinary(valBytes, &val) k.cdc.MustUnmarshalBinary(valBytes, &val)
if err != nil {
panic(err)
}
updates = append(updates, val) updates = append(updates, val)
} }
iterator.Close() iterator.Close()
@ -364,10 +366,7 @@ func (k Keeper) GetDelegatorBond(ctx sdk.Context,
return bond, false return bond, false
} }
err := k.cdc.UnmarshalBinary(delegatorBytes, &bond) k.cdc.MustUnmarshalBinary(delegatorBytes, &bond)
if err != nil {
panic(err)
}
return bond, true return bond, true
} }
@ -385,10 +384,7 @@ func (k Keeper) getBonds(ctx sdk.Context, maxRetrieve int16) (bonds []DelegatorB
} }
bondBytes := iterator.Value() bondBytes := iterator.Value()
var bond DelegatorBond var bond DelegatorBond
err := k.cdc.UnmarshalBinary(bondBytes, &bond) k.cdc.MustUnmarshalBinary(bondBytes, &bond)
if err != nil {
panic(err)
}
bonds[i] = bond bonds[i] = bond
iterator.Next() iterator.Next()
} }
@ -410,10 +406,7 @@ func (k Keeper) GetDelegatorBonds(ctx sdk.Context, delegator sdk.Address, maxRet
} }
bondBytes := iterator.Value() bondBytes := iterator.Value()
var bond DelegatorBond var bond DelegatorBond
err := k.cdc.UnmarshalBinary(bondBytes, &bond) k.cdc.MustUnmarshalBinary(bondBytes, &bond)
if err != nil {
panic(err)
}
bonds[i] = bond bonds[i] = bond
iterator.Next() iterator.Next()
} }
@ -422,10 +415,7 @@ func (k Keeper) GetDelegatorBonds(ctx sdk.Context, delegator sdk.Address, maxRet
func (k Keeper) setDelegatorBond(ctx sdk.Context, bond DelegatorBond) { func (k Keeper) setDelegatorBond(ctx sdk.Context, bond DelegatorBond) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
b, err := k.cdc.MarshalBinary(bond) b := k.cdc.MustMarshalBinary(bond)
if err != nil {
panic(err)
}
store.Set(GetDelegatorBondKey(bond.DelegatorAddr, bond.CandidateAddr, k.cdc), b) store.Set(GetDelegatorBondKey(bond.DelegatorAddr, bond.CandidateAddr, k.cdc), b)
} }
@ -448,18 +438,12 @@ func (k Keeper) GetParams(ctx sdk.Context) (params Params) {
panic("Stored params should not have been nil") panic("Stored params should not have been nil")
} }
err := k.cdc.UnmarshalBinary(b, &params) k.cdc.MustUnmarshalBinary(b, &params)
if err != nil {
panic(err)
}
return return
} }
func (k Keeper) setParams(ctx sdk.Context, params Params) { func (k Keeper) setParams(ctx sdk.Context, params Params) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
b, err := k.cdc.MarshalBinary(params) b := k.cdc.MustMarshalBinary(params)
if err != nil {
panic(err)
}
store.Set(ParamKey, b) store.Set(ParamKey, b)
k.params = Params{} // clear the cache k.params = Params{} // clear the cache
} }
@ -477,19 +461,13 @@ func (k Keeper) GetPool(ctx sdk.Context) (pool Pool) {
if b == nil { if b == nil {
panic("Stored pool should not have been nil") panic("Stored pool should not have been nil")
} }
err := k.cdc.UnmarshalBinary(b, &pool) k.cdc.MustUnmarshalBinary(b, &pool)
if err != nil {
panic(err) // This error should never occur big problem if does
}
return return
} }
func (k Keeper) setPool(ctx sdk.Context, p Pool) { func (k Keeper) setPool(ctx sdk.Context, p Pool) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
b, err := k.cdc.MarshalBinary(p) b := k.cdc.MustMarshalBinary(p)
if err != nil {
panic(err)
}
store.Set(PoolKey, b) store.Set(PoolKey, b)
k.pool = Pool{} //clear the cache k.pool = Pool{} //clear the cache
} }

View File

@ -5,6 +5,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire" "github.com/cosmos/cosmos-sdk/wire"
crypto "github.com/tendermint/go-crypto"
) )
// TODO remove some of these prefixes once have working multistore // TODO remove some of these prefixes once have working multistore
@ -18,11 +19,8 @@ var (
ValidatorsKey = []byte{0x03} // prefix for each key to a validator ValidatorsKey = []byte{0x03} // prefix for each key to a validator
AccUpdateValidatorsKey = []byte{0x04} // prefix for each key to a validator which is being updated AccUpdateValidatorsKey = []byte{0x04} // prefix for each key to a validator which is being updated
RecentValidatorsKey = []byte{0x05} // prefix for each key to the last updated validator group RecentValidatorsKey = []byte{0x05} // prefix for each key to the last updated validator group
ToKickOutValidatorsKey = []byte{0x06} // prefix for each key to the last updated validator group ToKickOutValidatorsKey = []byte{0x06} // prefix for each key to the last updated validator group
DelegatorBondKeyPrefix = []byte{0x07} // prefix for each key to a delegator's bond DelegatorBondKeyPrefix = []byte{0x07} // prefix for each key to a delegator's bond
CounterKey = []byte{0x08} // key for block-local tx index CounterKey = []byte{0x08} // key for block-local tx index
) )
@ -48,12 +46,13 @@ func GetAccUpdateValidatorKey(addr sdk.Address) []byte {
return append(AccUpdateValidatorsKey, addr.Bytes()...) return append(AccUpdateValidatorsKey, addr.Bytes()...)
} }
// get the key for the accumulated update validators // get the key for the recent validator group, ordered like tendermint
func GetRecentValidatorKey(addr sdk.Address) []byte { func GetRecentValidatorKey(pk crypto.PubKey) []byte {
addr := pk.Address()
return append(RecentValidatorsKey, addr.Bytes()...) return append(RecentValidatorsKey, addr.Bytes()...)
} }
// reverse operation of GetRecentValidatorKey // remove the prefix byte from a key
func AddrFromKey(key []byte) sdk.Address { func AddrFromKey(key []byte) sdk.Address {
return key[1:] return key[1:]
} }

View File

@ -31,12 +31,9 @@ func TestCandidate(t *testing.T) {
var candidates [3]Candidate var candidates [3]Candidate
amts := []int64{9, 8, 7} amts := []int64{9, 8, 7}
for i, amt := range amts { for i, amt := range amts {
candidates[i] = Candidate{ candidates[i] = NewCandidate(addrVals[i], pks[i], Description{})
Address: addrVals[i], candidates[i].Assets = sdk.NewRat(amt)
PubKey: pks[i], candidates[i].Liabilities = sdk.NewRat(amt)
Assets: sdk.NewRat(amt),
Liabilities: sdk.NewRat(amt),
}
} }
// check the empty keeper first // check the empty keeper first
@ -92,12 +89,9 @@ func TestBond(t *testing.T) {
amts := []int64{9, 8, 7} amts := []int64{9, 8, 7}
var candidates [3]Candidate var candidates [3]Candidate
for i, amt := range amts { for i, amt := range amts {
candidates[i] = Candidate{ candidates[i] = NewCandidate(addrVals[i], pks[i], Description{})
Address: addrVals[i], candidates[i].Assets = sdk.NewRat(amt)
PubKey: pks[i], candidates[i].Liabilities = sdk.NewRat(amt)
Assets: sdk.NewRat(amt),
Liabilities: sdk.NewRat(amt),
}
} }
// first add a candidates[0] to delegate too // first add a candidates[0] to delegate too
@ -194,15 +188,10 @@ func TestGetValidators(t *testing.T) {
n := len(amts) n := len(amts)
var candidates [5]Candidate var candidates [5]Candidate
for i, amt := range amts { for i, amt := range amts {
c := Candidate{ candidates[i] = NewCandidate(addrs[i], pks[i], Description{})
Status: Unbonded, candidates[i].Assets = sdk.NewRat(amt)
PubKey: pks[i], candidates[i].Liabilities = sdk.NewRat(amt)
Address: addrs[i], keeper.setCandidate(ctx, candidates[i])
Assets: sdk.NewRat(amt),
Liabilities: sdk.NewRat(amt),
}
keeper.setCandidate(ctx, c)
candidates[i] = c
} }
// first make sure everything as normal is ordered // first make sure everything as normal is ordered
@ -383,15 +372,10 @@ func TestClearAccUpdateValidators(t *testing.T) {
amts := []int64{100, 400, 200} amts := []int64{100, 400, 200}
candidates := make([]Candidate, len(amts)) candidates := make([]Candidate, len(amts))
for i, amt := range amts { for i, amt := range amts {
c := Candidate{ candidates[i] = NewCandidate(addrs[i], pks[i], Description{})
Status: Unbonded, candidates[i].Assets = sdk.NewRat(amt)
PubKey: pks[i], candidates[i].Liabilities = sdk.NewRat(amt)
Address: addrs[i], keeper.setCandidate(ctx, candidates[i])
Assets: sdk.NewRat(amt),
Liabilities: sdk.NewRat(amt),
}
candidates[i] = c
keeper.setCandidate(ctx, c)
} }
acc := keeper.getAccUpdateValidators(ctx) acc := keeper.getAccUpdateValidators(ctx)
@ -416,12 +400,9 @@ func TestGetAccUpdateValidators(t *testing.T) {
amts := []int64{10, 11, 12, 13, 1} amts := []int64{10, 11, 12, 13, 1}
var candidatesIn [5]Candidate var candidatesIn [5]Candidate
for i, amt := range amts { for i, amt := range amts {
candidatesIn[i] = Candidate{ candidatesIn[i] = NewCandidate(addrs[i], pks[i], Description{})
Address: addrs[i], candidatesIn[i].Assets = sdk.NewRat(amt)
PubKey: pks[i], candidatesIn[i].Liabilities = sdk.NewRat(amt)
Assets: sdk.NewRat(amt),
Liabilities: sdk.NewRat(amt),
}
} }
// test from nothing to something // test from nothing to something
@ -620,12 +601,9 @@ func TestIsRecentValidator(t *testing.T) {
amts := []int64{9, 8, 7, 10, 6} amts := []int64{9, 8, 7, 10, 6}
var candidatesIn [5]Candidate var candidatesIn [5]Candidate
for i, amt := range amts { for i, amt := range amts {
candidatesIn[i] = Candidate{ candidatesIn[i] = NewCandidate(addrVals[i], pks[i], Description{})
Address: addrVals[i], candidatesIn[i].Assets = sdk.NewRat(amt)
PubKey: pks[i], candidatesIn[i].Liabilities = sdk.NewRat(amt)
Assets: sdk.NewRat(amt),
Liabilities: sdk.NewRat(amt),
}
} }
// test that an empty validator set doesn't have any validators // test that an empty validator set doesn't have any validators
@ -643,17 +621,38 @@ func TestIsRecentValidator(t *testing.T) {
assert.True(t, c1ValWithCounter.equal(validators[1])) assert.True(t, c1ValWithCounter.equal(validators[1]))
// test a basic retrieve of something that should be a recent validator // test a basic retrieve of something that should be a recent validator
assert.True(t, keeper.IsRecentValidator(ctx, candidatesIn[0].Address)) assert.True(t, keeper.IsRecentValidator(ctx, candidatesIn[0].PubKey))
assert.True(t, keeper.IsRecentValidator(ctx, candidatesIn[1].Address)) assert.True(t, keeper.IsRecentValidator(ctx, candidatesIn[1].PubKey))
// test a basic retrieve of something that should not be a recent validator // test a basic retrieve of something that should not be a recent validator
assert.False(t, keeper.IsRecentValidator(ctx, candidatesIn[2].Address)) assert.False(t, keeper.IsRecentValidator(ctx, candidatesIn[2].PubKey))
// remove that validator, but don't retrieve the recent validator group // remove that validator, but don't retrieve the recent validator group
keeper.removeCandidate(ctx, candidatesIn[0].Address) keeper.removeCandidate(ctx, candidatesIn[0].Address)
// test that removed validator is not considered a recent validator // test that removed validator is not considered a recent validator
assert.False(t, keeper.IsRecentValidator(ctx, candidatesIn[0].Address)) assert.False(t, keeper.IsRecentValidator(ctx, candidatesIn[0].PubKey))
}
// test if is a validator from the last update
func TestGetTotalPrecommitVotingPower(t *testing.T) {
ctx, _, keeper := createTestInput(t, false, 0)
// set absent validators to be the 1st and 3rd record sorted by pubKey address
ctx = ctx.WithAbsentValidators([]int32{1, 3})
amts := []int64{9, 8, 7, 10, 6}
var candidatesIn [5]Candidate
for i, amt := range amts {
candidatesIn[i] = NewCandidate(addrVals[i], pks[i], Description{})
candidatesIn[i].Assets = sdk.NewRat(amt)
candidatesIn[i].Liabilities = sdk.NewRat(amt)
keeper.setCandidate(ctx, candidatesIn[i])
}
// test that an empty validator set doesn't have any validators
validators := keeper.GetValidators(ctx)
assert.Equal(t, 5, len(validators))
} }
func TestParams(t *testing.T) { func TestParams(t *testing.T) {

View File

@ -92,7 +92,7 @@ func createTestInput(t *testing.T, isCheckTx bool, initCoins int64) (sdk.Context
err := ms.LoadLatestVersion() err := ms.LoadLatestVersion()
require.Nil(t, err) require.Nil(t, err)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "foochainid"}, isCheckTx, nil, log.NewNopLogger()) ctx := sdk.NewContext(ms, abci.Header{ChainID: "foochainid"}, isCheckTx, nil, log.NewNopLogger(), nil)
cdc := makeTestCodec() cdc := makeTestCodec()
accountMapper := auth.NewAccountMapper( accountMapper := auth.NewAccountMapper(
cdc, // amino codec cdc, // amino codec

View File

@ -111,10 +111,10 @@ func initialPool() Pool {
InflationLastTime: 0, InflationLastTime: 0,
Inflation: sdk.NewRat(7, 100), Inflation: sdk.NewRat(7, 100),
DateLastCommissionReset: 0, DateLastCommissionReset: 0,
ReservePool: sdk.Coins{}, ReservePool: sdk.Coins(nil),
FeePool: sdk.Coins{}, FeePool: sdk.Coins(nil),
SumFeesReceived: sdk.Coins{}, SumFeesReceived: sdk.Coins(nil),
RecentFee: sdk.Coins{}, RecentFee: sdk.Coins(nil),
Adjustment: sdk.ZeroRat(), Adjustment: sdk.ZeroRat(),
} }
} }
@ -184,7 +184,7 @@ func (c Candidate) equal(c2 Candidate) bool {
c.Liabilities.Equal(c2.Liabilities) && c.Liabilities.Equal(c2.Liabilities) &&
c.Description == c2.Description && c.Description == c2.Description &&
c.ValidatorBondHeight == c2.ValidatorBondHeight && c.ValidatorBondHeight == c2.ValidatorBondHeight &&
c.ValidatorBondCounter == c2.ValidatorBondCounter && //c.ValidatorBondCounter == c2.ValidatorBondCounter && // counter is always changing
c.ProposerRewardPool.IsEqual(c2.ProposerRewardPool) && c.ProposerRewardPool.IsEqual(c2.ProposerRewardPool) &&
c.Commission.Equal(c2.Commission) && c.Commission.Equal(c2.Commission) &&
c.CommissionMax.Equal(c2.CommissionMax) && c.CommissionMax.Equal(c2.CommissionMax) &&