Ordering fixes, testcases

This commit is contained in:
Christopher Goes 2018-04-09 13:12:08 +02:00
parent 7fa634e772
commit 90d88114d9
No known key found for this signature in database
GPG Key ID: E828D98232D328D3
1 changed files with 20 additions and 12 deletions

View File

@ -80,9 +80,6 @@ func (k Keeper) setCandidate(ctx sdk.Context, candidate Candidate) {
// retreive the old candidate record
oldCandidate, oldFound := k.GetCandidate(ctx, address)
// update the validator block height (will only get written if stake has changed)
candidate.ValidatorHeight = ctx.BlockHeight()
// marshal the candidate record and add to the state
bz, err := k.cdc.MarshalBinary(candidate)
if err != nil {
@ -90,6 +87,26 @@ func (k Keeper) setCandidate(ctx sdk.Context, candidate Candidate) {
}
store.Set(GetCandidateKey(candidate.Address), bz)
// if the voting power is the same no need to update any of the other indexes
if oldFound && oldCandidate.Assets.Equal(candidate.Assets) {
return
}
// update the list ordered by voting power
if oldFound {
store.Delete(GetValidatorKey(address, oldCandidate.Assets, oldCandidate.ValidatorHeight, k.cdc))
}
// update the validator block height
candidate.ValidatorHeight = ctx.BlockHeight()
// update the candidate record
bz, err = k.cdc.MarshalBinary(candidate)
if err != nil {
panic(err)
}
store.Set(GetCandidateKey(candidate.Address), bz)
// marshal the new validator record
validator := candidate.validator()
bz, err = k.cdc.MarshalBinary(validator)
@ -97,15 +114,6 @@ func (k Keeper) setCandidate(ctx sdk.Context, candidate Candidate) {
panic(err)
}
// if the voting power is the same no need to update any of the other indexes
if oldFound && oldCandidate.Assets.Equal(candidate.Assets) {
return
}
// update the list ordered by voting power
if oldFound {
store.Delete(GetValidatorKey(address, oldCandidate.Assets, oldCandidate.ValidatorHeight, k.cdc))
}
store.Set(GetValidatorKey(address, validator.Power, validator.Height, k.cdc), bz)
// add to the validators to update list if is already a validator