consensus check is forked

pull/4351/head
frozen 2 years ago committed by Casey Gardiner
parent 4d7e167bee
commit 0e1a1667e2
  1. 6
      api/service/explorer/service.go
  2. 2
      consensus/consensus.go
  3. 14
      consensus/consensus_service.go
  4. 43
      consensus/consensus_v2.go
  5. 10
      internal/params/config.go

@ -223,9 +223,9 @@ func (s *Service) GetBlocks(w http.ResponseWriter, r *http.Request) {
for i := cur; i > 0; i-- { for i := cur; i > 0; i-- {
block := s.blockchain.GetBlockByNumber(i) block := s.blockchain.GetBlockByNumber(i)
w.Write([]byte(fmt.Sprintf("%d ", i))) w.Write([]byte(fmt.Sprintf("#%d ", i)))
w.Write([]byte(fmt.Sprintf("%s ", block.Header().ViewID().String()))) w.Write([]byte(fmt.Sprintf("v%s ", block.Header().ViewID().String())))
w.Write([]byte(fmt.Sprintf("%s ", block.Header().Coinbase().Hash().Hex()))) w.Write([]byte(fmt.Sprintf("e%d ", block.Header().Epoch().Uint64())))
w.Write([]byte(fmt.Sprintf("%s\n", block.Header().Coinbase().Hex()))) w.Write([]byte(fmt.Sprintf("%s\n", block.Header().Coinbase().Hex())))
} }
} }

@ -73,8 +73,6 @@ type Consensus struct {
priKey multibls.PrivateKeys priKey multibls.PrivateKeys
// the publickey of leader // the publickey of leader
LeaderPubKey *bls.PublicKeyWrapper LeaderPubKey *bls.PublicKeyWrapper
// index of leader in the list of validators.
LeaderIndex int
// blockNum: the next blockNumber that FBFT is going to agree on, // blockNum: the next blockNumber that FBFT is going to agree on,
// should be equal to the blockNumber of next block // should be equal to the blockNumber of next block
blockNum uint64 blockNum uint64

@ -474,20 +474,6 @@ func (consensus *Consensus) SetCurBlockViewID(viewID uint64) uint64 {
return consensus.current.SetCurBlockViewID(viewID) return consensus.current.SetCurBlockViewID(viewID)
} }
// SetLeaderIndex set the leader index.
func (consensus *Consensus) SetLeaderIndex(f func(int) int) (current int) {
consensus.pubKeyLock.Lock()
defer consensus.pubKeyLock.Unlock()
consensus.LeaderIndex = f(consensus.LeaderIndex)
return consensus.LeaderIndex
}
func (consensus *Consensus) GetLeaderIndex() int {
consensus.pubKeyLock.Lock()
defer consensus.pubKeyLock.Unlock()
return consensus.LeaderIndex
}
// SetViewChangingID set the current view change ID // SetViewChangingID set the current view change ID
func (consensus *Consensus) SetViewChangingID(viewID uint64) { func (consensus *Consensus) SetViewChangingID(viewID uint64) {
consensus.current.SetViewChangingID(viewID) consensus.current.SetViewChangingID(viewID)

@ -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().

@ -262,6 +262,7 @@ var (
CrossShardXferPrecompileEpoch: big.NewInt(1), CrossShardXferPrecompileEpoch: big.NewInt(1),
AllowlistEpoch: EpochTBD, AllowlistEpoch: EpochTBD,
FeeCollectEpoch: big.NewInt(5), FeeCollectEpoch: big.NewInt(5),
LeaderRotationEpoch: big.NewInt(1),
} }
// AllProtocolChanges ... // AllProtocolChanges ...
@ -301,6 +302,7 @@ var (
big.NewInt(0), // SlotsLimitedEpoch big.NewInt(0), // SlotsLimitedEpoch
big.NewInt(1), // CrossShardXferPrecompileEpoch big.NewInt(1), // CrossShardXferPrecompileEpoch
big.NewInt(0), // AllowlistEpoch big.NewInt(0), // AllowlistEpoch
big.NewInt(1), // LeaderRotationEpoch
big.NewInt(0), // FeeCollectEpoch big.NewInt(0), // FeeCollectEpoch
} }
@ -341,6 +343,8 @@ var (
big.NewInt(0), // SlotsLimitedEpoch big.NewInt(0), // SlotsLimitedEpoch
big.NewInt(1), // CrossShardXferPrecompileEpoch big.NewInt(1), // CrossShardXferPrecompileEpoch
big.NewInt(0), // AllowlistEpoch big.NewInt(0), // AllowlistEpoch
// TODO place correct epoch number
big.NewInt(1), // LeaderRotationEpoch
big.NewInt(0), // FeeCollectEpoch big.NewInt(0), // FeeCollectEpoch
} }
@ -481,6 +485,8 @@ type ChainConfig struct {
// AllowlistEpoch is the first epoch to support allowlist of HIP18 // AllowlistEpoch is the first epoch to support allowlist of HIP18
AllowlistEpoch *big.Int AllowlistEpoch *big.Int
LeaderRotationEpoch *big.Int `json:"leader-rotation-epoch,omitempty"`
// FeeCollectEpoch is the first epoch that enables txn fees to be collected into the community-managed account. // FeeCollectEpoch is the first epoch that enables txn fees to be collected into the community-managed account.
// It should >= StakingEpoch. // It should >= StakingEpoch.
// Before StakingEpoch, txn fees are paid to miner/leader. // Before StakingEpoch, txn fees are paid to miner/leader.
@ -687,6 +693,10 @@ func (c *ChainConfig) IsAllowlistEpoch(epoch *big.Int) bool {
return isForked(c.AllowlistEpoch, epoch) return isForked(c.AllowlistEpoch, epoch)
} }
func (c *ChainConfig) IsLeaderRotation(epoch *big.Int) bool {
return isForked(c.LeaderRotationEpoch, epoch)
}
// IsFeeCollectEpoch determines whether Txn Fees will be collected into the community-managed account. // IsFeeCollectEpoch determines whether Txn Fees will be collected into the community-managed account.
func (c *ChainConfig) IsFeeCollectEpoch(epoch *big.Int) bool { func (c *ChainConfig) IsFeeCollectEpoch(epoch *big.Int) bool {
return isForked(c.FeeCollectEpoch, epoch) return isForked(c.FeeCollectEpoch, epoch)

Loading…
Cancel
Save