all, state: unexpose GenesisDoc, ChainID fields make them accessor methods

Fixes #671

Unexpose GenesisDoc and ChainID fields to avoid them being
serialized to the DB on every block write/state.Save()

A GenesisDoc can now be alternatively written to the state's
database, by serializing its JSON as a value of key "genesis-doc".

There are now accessors and a setter for these attributes:
- state.GenesisDoc() (*types.GenesisDoc, error)
- state.ChainID() (string, error)
- state.SetGenesisDoc(*types.GenesisDoc)

This is a breaking change since it changes how the state's
serialization and requires that if loading the GenesisDoc entirely
from the database, you'll need to set its value in the database
as the GenesisDoc's JSON marshaled bytes.
This commit is contained in:
Emmanuel Odeke
2017-09-22 18:17:21 -06:00
committed by Anton Kaliaev
parent a1e0f0ba95
commit 7939d62ef0
7 changed files with 229 additions and 28 deletions

View File

@ -389,8 +389,12 @@ func (cs *ConsensusState) reconstructLastCommit(state *sm.State) {
if state.LastBlockHeight == 0 {
return
}
chainID, err := cs.state.ChainID()
if err != nil {
cmn.PanicCrisis(cmn.Fmt("Failed to retrieve ChainID: %v", err))
}
seenCommit := cs.blockStore.LoadSeenCommit(state.LastBlockHeight)
lastPrecommits := types.NewVoteSet(cs.state.ChainID, state.LastBlockHeight, seenCommit.Round(), types.VoteTypePrecommit, state.LastValidators)
lastPrecommits := types.NewVoteSet(chainID, state.LastBlockHeight, seenCommit.Round(), types.VoteTypePrecommit, state.LastValidators)
for _, precommit := range seenCommit.Precommits {
if precommit == nil {
continue
@ -720,7 +724,11 @@ func (cs *ConsensusState) proposalHeartbeat(height, round int) {
ValidatorAddress: addr,
ValidatorIndex: valIndex,
}
cs.privValidator.SignHeartbeat(cs.state.ChainID, heartbeat)
chainID, err := cs.state.ChainID()
if err != nil {
return
}
cs.privValidator.SignHeartbeat(chainID, heartbeat)
heartbeatEvent := types.EventDataProposalHeartbeat{heartbeat}
types.FireEventProposalHeartbeat(cs.evsw, heartbeatEvent)
counter += 1
@ -797,8 +805,11 @@ func (cs *ConsensusState) defaultDecideProposal(height, round int) {
// Make proposal
polRound, polBlockID := cs.Votes.POLInfo()
proposal := types.NewProposal(height, round, blockParts.Header(), polRound, polBlockID)
err := cs.privValidator.SignProposal(cs.state.ChainID, proposal)
if err == nil {
chainID, err := cs.state.ChainID()
if err != nil {
return
}
if err := cs.privValidator.SignProposal(chainID, proposal); err == nil {
// Set fields
/* fields set by setProposal and addBlockPart
cs.Proposal = proposal
@ -857,8 +868,12 @@ func (cs *ConsensusState) createProposalBlock() (block *types.Block, blockParts
// Mempool validated transactions
txs := cs.mempool.Reap(cs.config.MaxBlockSizeTxs)
return types.MakeBlock(cs.Height, cs.state.ChainID, txs, commit,
chainID, err := cs.state.ChainID()
if err != nil {
cs.Logger.Error("chainID err", err)
return
}
return types.MakeBlock(cs.Height, chainID, txs, commit,
cs.state.LastBlockID, cs.state.Validators.Hash(),
cs.state.AppHash, cs.state.Params().BlockPartSizeBytes)
}
@ -1263,8 +1278,13 @@ func (cs *ConsensusState) defaultSetProposal(proposal *types.Proposal) error {
return ErrInvalidProposalPOLRound
}
chainID, err := cs.state.ChainID()
if err != nil {
return err
}
// Verify signature
if !cs.Validators.GetProposer().PubKey.VerifyBytes(types.SignBytes(cs.state.ChainID, proposal), proposal.Signature) {
if !cs.Validators.GetProposer().PubKey.VerifyBytes(types.SignBytes(chainID, proposal), proposal.Signature) {
return ErrInvalidProposalSignature
}
@ -1449,6 +1469,10 @@ func (cs *ConsensusState) addVote(vote *types.Vote, peerKey string) (added bool,
}
func (cs *ConsensusState) signVote(type_ byte, hash []byte, header types.PartSetHeader) (*types.Vote, error) {
chainID, err := cs.state.ChainID()
if err != nil {
return nil, err
}
addr := cs.privValidator.GetAddress()
valIndex, _ := cs.Validators.GetByAddress(addr)
vote := &types.Vote{
@ -1459,7 +1483,7 @@ func (cs *ConsensusState) signVote(type_ byte, hash []byte, header types.PartSet
Type: type_,
BlockID: types.BlockID{hash, header},
}
err := cs.privValidator.SignVote(cs.state.ChainID, vote)
err = cs.privValidator.SignVote(chainID, vote)
return vote, err
}