remove privValIndex; Stale->AppHashIsStale

This commit is contained in:
Ethan Buchman
2016-12-02 00:12:06 -05:00
parent 3e7c7f51fa
commit 0fe53dc5cf
4 changed files with 24 additions and 48 deletions

View File

@@ -43,7 +43,7 @@ func (s *State) ExecBlock(eventCache types.Fireable, proxyAppConn proxy.AppConnC
}
// update the validator set
s.valAddedOrRemoved, err = updateValidators(nextValSet, changedValidators)
err = updateValidators(nextValSet, changedValidators)
if err != nil {
log.Warn("Error changing validator set", "error", err)
// TODO: err or carry on?
@@ -135,22 +135,20 @@ func execBlockOnProxyApp(eventCache types.Fireable, proxyAppConn proxy.AppConnCo
return changedValidators, nil
}
func updateValidators(validators *types.ValidatorSet, changedValidators []*tmsp.Validator) (bool, error) {
func updateValidators(validators *types.ValidatorSet, changedValidators []*tmsp.Validator) error {
// TODO: prevent change of 1/3+ at once
var addedOrRemoved bool
for _, v := range changedValidators {
pubkey, err := crypto.PubKeyFromBytes(v.PubKey) // NOTE: expects go-wire encoded pubkey
if err != nil {
return false, err
return err
}
address := pubkey.Address()
power := int64(v.Power)
// mind the overflow from uint64
if power < 0 {
return false, errors.New(Fmt("Power (%d) overflows int64", v.Power))
return errors.New(Fmt("Power (%d) overflows int64", v.Power))
}
_, val := validators.GetByAddress(address)
@@ -158,26 +156,24 @@ func updateValidators(validators *types.ValidatorSet, changedValidators []*tmsp.
// add val
added := validators.Add(types.NewValidator(pubkey, power))
if !added {
return false, errors.New(Fmt("Failed to add new validator %X with voting power %d", address, power))
return errors.New(Fmt("Failed to add new validator %X with voting power %d", address, power))
}
addedOrRemoved = true
} else if v.Power == 0 {
// remove val
_, removed := validators.Remove(address)
if !removed {
return false, errors.New(Fmt("Failed to remove validator %X)"))
return errors.New(Fmt("Failed to remove validator %X)"))
}
addedOrRemoved = true
} else {
// update val
val.VotingPower = power
updated := validators.Update(val)
if !updated {
return false, errors.New(Fmt("Failed to update validator %X with voting power %d", address, power))
return errors.New(Fmt("Failed to update validator %X with voting power %d", address, power))
}
}
}
return addedOrRemoved, nil
return nil
}
// return a bit array of validators that signed the last commit
@@ -363,8 +359,8 @@ func (h *Handshaker) ReplayBlocks(appHash []byte, appBlockHeight int, appConnCon
// if we crashed between Commit and SaveState,
// the state's app hash is stale
// otherwise we're synced
if h.state.Stale {
h.state.Stale = false
if h.state.AppHashIsStale {
h.state.AppHashIsStale = false
h.state.AppHash = appHash
}
return nil

View File

@@ -38,10 +38,8 @@ type State struct {
// AppHash is updated after Commit;
// it's stale after ExecBlock and before Commit
Stale bool
AppHash []byte
valAddedOrRemoved bool // true if a validator was added or removed
AppHashIsStale bool
AppHash []byte
}
func LoadState(db dbm.DB) *State {
@@ -62,6 +60,9 @@ func LoadState(db dbm.DB) *State {
}
func (s *State) Copy() *State {
if s.AppHashIsStale {
PanicSanity(Fmt("App hash is stale: %v", s))
}
return &State{
db: s.db,
GenesisDoc: s.GenesisDoc,
@@ -71,7 +72,7 @@ func (s *State) Copy() *State {
LastBlockTime: s.LastBlockTime,
Validators: s.Validators.Copy(),
LastValidators: s.LastValidators.Copy(),
Stale: s.Stale, // XXX: but really state shouldnt be copied while its stale
AppHashIsStale: false,
AppHash: s.AppHash,
}
}
@@ -96,7 +97,7 @@ func (s *State) Bytes() []byte {
}
// Mutate state variables to match block and validators
// Since we don't have the new AppHash yet, we set s.Stale=true
// Since we don't have the new AppHash yet, we set s.AppHashIsStale=true
func (s *State) SetBlockAndValidators(header *types.Header, blockPartsHeader types.PartSetHeader, prevValSet, nextValSet *types.ValidatorSet) {
s.LastBlockHeight = header.Height
s.LastBlockID = types.BlockID{header.Hash(), blockPartsHeader}
@@ -104,11 +105,7 @@ func (s *State) SetBlockAndValidators(header *types.Header, blockPartsHeader typ
s.Validators = nextValSet
s.LastValidators = prevValSet
s.Stale = true
}
func (s *State) ValidatorAddedOrRemoved() bool {
return s.valAddedOrRemoved
s.AppHashIsStale = true
}
func (s *State) GetValidators() (*types.ValidatorSet, *types.ValidatorSet) {