diff --git a/consensus/consensus.go b/consensus/consensus.go index 10144c910..33f6ee5a1 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -74,6 +74,7 @@ type Consensus struct { // blockNum: the next blockNumber that FBFT is going to agree on, // should be equal to the blockNumber of next block blockNum uint64 + epoch uint64 // Blockhash - 32 byte blockHash [32]byte // Block to run consensus on diff --git a/consensus/consensus_service.go b/consensus/consensus_service.go index 2fabb42b3..76e5d9c44 100644 --- a/consensus/consensus_service.go +++ b/consensus/consensus_service.go @@ -82,6 +82,7 @@ func (consensus *Consensus) UpdatePublicKeys(pubKeys, allowlist []bls_cosi.Publi func (consensus *Consensus) updatePublicKeys(pubKeys, allowlist []bls_cosi.PublicKeyWrapper) int64 { consensus.Decider.UpdateParticipants(pubKeys, allowlist) + consensus.pubKeyLock.Unlock() consensus.getLogger().Info().Msg("My Committee updated") for i := range pubKeys { consensus.getLogger().Info(). @@ -89,15 +90,22 @@ func (consensus *Consensus) updatePublicKeys(pubKeys, allowlist []bls_cosi.Publi Str("BLSPubKey", pubKeys[i].Bytes.Hex()). Msg("Member") } - - allKeys := consensus.Decider.Participants() - if len(allKeys) != 0 { - consensus.LeaderPubKey = &allKeys[0] - consensus.getLogger().Info(). - Str("info", consensus.LeaderPubKey.Bytes.Hex()).Msg("My Leader") + if consensus.Blockchain.Config().IsLeaderRotation(consensus.GetCurEpoch()) { + consensus.updateLeader() } else { - consensus.getLogger().Error(). - Msg("[UpdatePublicKeys] Participants is empty") + consensus.pubKeyLock.Lock() + allKeys := consensus.Decider.Participants() + consensus.pubKeyLock.Unlock() + if len(allKeys) != 0 { + consensus.pubKeyLock.Lock() + consensus.LeaderPubKey = &allKeys[0] + consensus.pubKeyLock.Unlock() + consensus.getLogger().Info(). + Str("info", consensus.LeaderPubKey.Bytes.Hex()).Msg("My Leader") + } else { + consensus.getLogger().Error(). + Msg("[UpdatePublicKeys] Participants is empty") + } } // reset states after update public keys // TODO: incorporate bitmaps in the decider, so their state can't be inconsistent. @@ -658,3 +666,13 @@ func (consensus *Consensus) getLogger() *zerolog.Logger { Logger() return &logger } + +func UpdatePublicKeyDefault(consensus *Consensus) { + if allKeys := consensus.Decider.Participants(); len(allKeys) > 0 { + consensus.LeaderPubKey = &allKeys[0] + } +} + +func UpdatePublicKeyRotate(consensus *Consensus) { + //consensus +} diff --git a/consensus/consensus_v2.go b/consensus/consensus_v2.go index fea7b7dfe..653ae72d6 100644 --- a/consensus/consensus_v2.go +++ b/consensus/consensus_v2.go @@ -778,7 +778,7 @@ func (consensus *Consensus) getEpochFirstBlockViewID(epoch *big.Int) (uint64, er if epoch.Uint64() == 0 { return 0, nil } - epochBlock := consensus.Blockchain.GetBlockByNumber(epoch.Uint64() - 1) + epochBlock := consensus.Blockchain.GetBlockByNumber(shard.Schedule.EpochLastBlock(epoch.Uint64() - 1)) if epochBlock == nil { return 0, errors.Errorf("block not found for number %d", epoch.Uint64()-1) }