retrieve leaderKey/viewID informatin before join consensus; fall back to ignoreViewID check if failed to retrieve leaderPubKey

pull/1180/head
chao 5 years ago committed by chaosma
parent 215ae81b07
commit 978e8c4a3c
  1. 48
      consensus/consensus_service.go
  2. 3
      consensus/consensus_v2.go

@ -603,3 +603,51 @@ func (consensus *Consensus) getLogger() log.Logger {
logger := consensus.logger(utils.GetLogInstance())
return logger
}
// retrieve corresponding blsPublicKey from Coinbase Address
func (consensus *Consensus) getLeaderPubKeyFromCoinbase(header *types.Header) (*bls.PublicKey, error) {
shardState, err := consensus.ChainReader.ReadShardState(header.Epoch)
if err != nil {
return nil, ctxerror.New("cannot read shard state",
"epoch", header.Epoch,
"coinbaseAddr", header.Coinbase,
).WithCause(err)
}
committee := shardState.FindCommitteeByID(header.ShardID)
if committee == nil {
return nil, ctxerror.New("cannot find shard in the shard state",
"blockNum", header.Number,
"shardID", header.ShardID,
"coinbaseAddr", header.Coinbase,
)
}
committerKey := new(bls.PublicKey)
for _, member := range committee.NodeList {
if member.EcdsaAddress == header.Coinbase {
err := member.BlsPublicKey.ToLibBLSPublicKey(committerKey)
if err != nil {
return nil, ctxerror.New("cannot convert BLS public key",
"blsPublicKey", member.BlsPublicKey,
"coinbaseAddr", header.Coinbase).WithCause(err)
}
return committerKey, nil
}
}
return nil, ctxerror.New("cannot find corresponding BLS Public Key", "coinbaseAddr", header.Coinbase)
}
// update consensus information before join consensus after state syncing
func (consensus *Consensus) updateConsensusInformation() {
header := consensus.ChainReader.CurrentHeader()
consensus.SetBlockNum(header.Number.Uint64() + 1)
consensus.SetViewID(header.ViewID.Uint64() + 1)
leaderPubKey, err := consensus.getLeaderPubKeyFromCoinbase(header)
if err != nil || leaderPubKey == nil {
consensus.getLogger().Debug("[SYNC] Unable to get leaderPubKey from coinbase", "error", err)
consensus.ignoreViewIDCheck = true
} else {
consensus.getLogger().Debug("[SYNC] Most Recent LeaderPubKey Updated Based on BlockChain", "leaderPubKey", leaderPubKey.SerializeToHexStr())
consensus.LeaderPubKey = leaderPubKey
}
}

@ -871,9 +871,8 @@ func (consensus *Consensus) Start(blockChannel chan *types.Block, stopChan chan
}
}
case <-consensus.syncReadyChan:
consensus.SetBlockNum(consensus.ChainReader.CurrentHeader().Number.Uint64() + 1)
consensus.updateConsensusInformation()
consensus.getLogger().Info("Node is in sync")
consensus.ignoreViewIDCheck = true
case <-consensus.syncNotReadyChan:
consensus.SetBlockNum(consensus.ChainReader.CurrentHeader().Number.Uint64() + 1)

Loading…
Cancel
Save