|
|
@ -5,6 +5,7 @@ import ( |
|
|
|
"context" |
|
|
|
"context" |
|
|
|
"encoding/hex" |
|
|
|
"encoding/hex" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
|
|
|
|
"math/big" |
|
|
|
"sync/atomic" |
|
|
|
"sync/atomic" |
|
|
|
"time" |
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
@ -691,16 +692,25 @@ func (consensus *Consensus) SetupForNewConsensus(blk *types.Block, committedMsg |
|
|
|
atomic.StoreUint64(&consensus.blockNum, blk.NumberU64()+1) |
|
|
|
atomic.StoreUint64(&consensus.blockNum, blk.NumberU64()+1) |
|
|
|
curBlockViewID := consensus.SetCurBlockViewID(committedMsg.ViewID + 1) |
|
|
|
curBlockViewID := consensus.SetCurBlockViewID(committedMsg.ViewID + 1) |
|
|
|
prev := consensus.GetLeaderPubKey() |
|
|
|
prev := consensus.GetLeaderPubKey() |
|
|
|
idx := consensus.SetLeaderIndex(func(i int) int { |
|
|
|
if consensus.Blockchain.Config().IsLeaderRotation(blk.Epoch()) { |
|
|
|
if curBlockViewID%3 == 0 { |
|
|
|
epochBlockViewID, err := consensus.getEpochFirstBlockViewID(blk.Epoch()) |
|
|
|
return i + 1 |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
consensus.getLogger().Error().Err(err).Msgf("[SetupForNewConsensus] Failed to get epoch block viewID for epoch %d", blk.Epoch().Uint64()) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if epochBlockViewID > curBlockViewID { |
|
|
|
|
|
|
|
consensus.getLogger().Error().Msg("[SetupForNewConsensus] Epoch block viewID is greater than current block viewID") |
|
|
|
|
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
return i |
|
|
|
|
|
|
|
}) |
|
|
|
diff := curBlockViewID - epochBlockViewID |
|
|
|
|
|
|
|
|
|
|
|
pps := consensus.Decider.Participants() |
|
|
|
pps := consensus.Decider.Participants() |
|
|
|
|
|
|
|
idx := (int(diff) / 3) % len(pps) |
|
|
|
consensus.pubKeyLock.Lock() |
|
|
|
consensus.pubKeyLock.Lock() |
|
|
|
consensus.LeaderPubKey = &pps[idx%len(pps)] |
|
|
|
fmt.Println("(int(diff)/3)%len(pps) == ", idx) |
|
|
|
fmt.Printf("SetupForNewConsensus :%d idx: %d future v%d new: %s prev: %s %q\n", utils.GetPort(), idx, curBlockViewID, consensus.LeaderPubKey.Bytes.Hex(), prev.Bytes.Hex(), consensus.isLeader()) |
|
|
|
consensus.LeaderPubKey = &pps[idx] |
|
|
|
|
|
|
|
fmt.Printf("SetupForNewConsensus :%d idx: %d future v%d new: %s prev: %s %v\n", utils.GetPort(), idx, curBlockViewID, consensus.LeaderPubKey.Bytes.Hex(), prev.Bytes.Hex(), consensus.isLeader()) |
|
|
|
consensus.pubKeyLock.Unlock() |
|
|
|
consensus.pubKeyLock.Unlock() |
|
|
|
if consensus.IsLeader() && !consensus.GetLeaderPubKey().Object.IsEqual(prev.Object) { |
|
|
|
if consensus.IsLeader() && !consensus.GetLeaderPubKey().Object.IsEqual(prev.Object) { |
|
|
|
// leader changed
|
|
|
|
// leader changed
|
|
|
@ -710,8 +720,14 @@ func (consensus *Consensus) SetupForNewConsensus(blk *types.Block, committedMsg |
|
|
|
consensus.ReadySignal <- SyncProposal |
|
|
|
consensus.ReadySignal <- SyncProposal |
|
|
|
|
|
|
|
|
|
|
|
}() |
|
|
|
}() |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
fmt.Printf("SetupForNewConsensus0 :%d future v%d new: %s prev: %s %v\n", utils.GetPort(), curBlockViewID, consensus.LeaderPubKey.Bytes.Hex(), prev.Bytes.Hex(), consensus.isLeader()) |
|
|
|
|
|
|
|
consensus.pubKeyLock.Lock() |
|
|
|
|
|
|
|
consensus.LeaderPubKey = committedMsg.SenderPubkeys[0] |
|
|
|
|
|
|
|
consensus.pubKeyLock.Unlock() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Update consensus keys at last so the change of leader status doesn't mess up normal flow
|
|
|
|
// Update consensus keys at last so the change of leader status doesn't mess up normal flow
|
|
|
|
if blk.IsLastBlockInEpoch() { |
|
|
|
if blk.IsLastBlockInEpoch() { |
|
|
|
consensus.SetMode(consensus.UpdateConsensusInformation()) |
|
|
|
consensus.SetMode(consensus.UpdateConsensusInformation()) |
|
|
@ -720,6 +736,17 @@ func (consensus *Consensus) SetupForNewConsensus(blk *types.Block, committedMsg |
|
|
|
consensus.ResetState() |
|
|
|
consensus.ResetState() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (consensus *Consensus) getEpochFirstBlockViewID(epoch *big.Int) (uint64, error) { |
|
|
|
|
|
|
|
if epoch.Uint64() == 0 { |
|
|
|
|
|
|
|
return 0, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
epochBlock := consensus.Blockchain.GetBlockByNumber(epoch.Uint64() - 1) |
|
|
|
|
|
|
|
if epochBlock == nil { |
|
|
|
|
|
|
|
return 0, errors.Errorf("block not found for number %d", epoch.Uint64()-1) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return epochBlock.Header().ViewID().Uint64() + 1, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (consensus *Consensus) postCatchup(initBN uint64) { |
|
|
|
func (consensus *Consensus) postCatchup(initBN uint64) { |
|
|
|
if initBN < consensus.BlockNum() { |
|
|
|
if initBN < consensus.BlockNum() { |
|
|
|
consensus.getLogger().Info(). |
|
|
|
consensus.getLogger().Info(). |
|
|
|