aboutsummaryrefslogtreecommitdiffstats
path: root/core/agreement-mgr.go
diff options
context:
space:
mode:
Diffstat (limited to 'core/agreement-mgr.go')
-rw-r--r--core/agreement-mgr.go107
1 files changed, 77 insertions, 30 deletions
diff --git a/core/agreement-mgr.go b/core/agreement-mgr.go
index 3e0851d..8cb4c2e 100644
--- a/core/agreement-mgr.go
+++ b/core/agreement-mgr.go
@@ -26,6 +26,7 @@ import (
"github.com/dexon-foundation/dexon-consensus/common"
"github.com/dexon-foundation/dexon-consensus/core/types"
+ typesDKG "github.com/dexon-foundation/dexon-consensus/core/types/dkg"
"github.com/dexon-foundation/dexon-consensus/core/utils"
)
@@ -34,6 +35,7 @@ var (
ErrPreviousRoundIsNotFinished = errors.New("previous round is not finished")
ErrRoundOutOfRange = errors.New("round out of range")
ErrInvalidBlock = errors.New("invalid block")
+ ErrNoValidLeader = errors.New("no valid leader")
)
const maxResultCache = 100
@@ -89,7 +91,8 @@ func newAgreementMgrConfig(prev agreementMgrConfig, config *types.Config,
type baRoundSetting struct {
round uint64
- notarySet map[types.NodeID]struct{}
+ dkgSet map[types.NodeID]struct{}
+ threshold int
ticker Ticker
crs common.Hash
}
@@ -173,6 +176,19 @@ func (mgr *agreementMgr) run() {
}()
}
+func (mgr *agreementMgr) calcLeader(
+ dkgSet map[types.NodeID]struct{},
+ crs common.Hash, pos types.Position) (
+ types.NodeID, error) {
+ nodeSet := types.NewNodeSetFromMap(dkgSet)
+ leader := nodeSet.GetSubSet(1, types.NewNodeLeaderTarget(
+ crs, pos.Height))
+ for nID := range leader {
+ return nID, nil
+ }
+ return types.NodeID{}, ErrNoValidLeader
+}
+
func (mgr *agreementMgr) config(round uint64) *agreementMgrConfig {
mgr.lock.RLock()
defer mgr.lock.RUnlock()
@@ -274,10 +290,6 @@ func (mgr *agreementMgr) processAgreementResult(
}
} else if result.Position.Newer(aID) {
mgr.logger.Info("Fast syncing BA", "position", result.Position)
- nIDs, err := mgr.cache.GetNotarySet(result.Position.Round)
- if err != nil {
- return err
- }
if result.Position.Round < DKGDelayRound {
mgr.logger.Debug("Calling Network.PullBlocks for fast syncing BA",
"hash", result.BlockHash)
@@ -288,13 +300,19 @@ func (mgr *agreementMgr) processAgreementResult(
}
}
}
- mgr.logger.Debug("Calling Governance.CRS", "round", result.Position.Round)
- crs := utils.GetCRSWithPanic(mgr.gov, result.Position.Round, mgr.logger)
- leader, err := mgr.cache.GetLeaderNode(result.Position)
+ setting := mgr.generateSetting(result.Position.Round)
+ if setting == nil {
+ mgr.logger.Warn("unable to get setting", "round",
+ result.Position.Round)
+ return ErrConfigurationNotReady
+ }
+ leader, err := mgr.calcLeader(setting.dkgSet, setting.crs, result.Position)
if err != nil {
return err
}
- mgr.baModule.restart(nIDs, result.Position, leader, crs)
+ mgr.baModule.restart(
+ setting.dkgSet, setting.threshold,
+ result.Position, leader, setting.crs)
if result.Position.Round >= DKGDelayRound {
return mgr.baModule.processAgreementResult(result)
}
@@ -322,58 +340,87 @@ func (mgr *agreementMgr) stop() {
mgr.waitGroup.Wait()
}
+func (mgr *agreementMgr) generateSetting(round uint64) *baRoundSetting {
+ curConfig := mgr.config(round)
+ if curConfig == nil {
+ return nil
+ }
+ var dkgSet map[types.NodeID]struct{}
+ if round >= DKGDelayRound {
+ _, qualidifed, err := typesDKG.CalcQualifyNodes(
+ mgr.gov.DKGMasterPublicKeys(round),
+ mgr.gov.DKGComplaints(round),
+ utils.GetDKGThreshold(mgr.gov.Configuration(round)),
+ )
+ if err != nil {
+ mgr.logger.Error("Failed to get gpk", "round", round, "error", err)
+ return nil
+ }
+ dkgSet = qualidifed
+ }
+ if len(dkgSet) == 0 {
+ var err error
+ dkgSet, err = mgr.cache.GetNotarySet(round)
+ if err != nil {
+ mgr.logger.Error("Failed to get notarySet", "round", round)
+ return nil
+ }
+ }
+ return &baRoundSetting{
+ crs: curConfig.crs,
+ dkgSet: dkgSet,
+ round: round,
+ threshold: utils.GetBAThreshold(&types.Config{
+ NotarySetSize: curConfig.notarySetSize}),
+ }
+}
+
func (mgr *agreementMgr) runBA(initRound uint64) {
// These are round based variables.
var (
currentRound uint64
nextRound = initRound
curConfig = mgr.config(initRound)
- setting = baRoundSetting{}
+ setting = &baRoundSetting{}
tickDuration time.Duration
+ ticker Ticker
)
// Check if this routine needs to awake in this round and prepare essential
// variables when yes.
- checkRound := func() (isNotary bool) {
+ checkRound := func() (isDKG bool) {
defer func() {
currentRound = nextRound
nextRound++
}()
// Wait until the configuartion for next round is ready.
for {
- if curConfig = mgr.config(nextRound); curConfig != nil {
+ if setting = mgr.generateSetting(nextRound); setting != nil {
break
} else {
mgr.logger.Debug("Round is not ready", "round", nextRound)
time.Sleep(1 * time.Second)
}
}
- // Check if this node in notary set of this chain in this round.
- notarySet, err := mgr.cache.GetNotarySet(nextRound)
- if err != nil {
- panic(err)
- }
- setting.crs = curConfig.crs
- setting.notarySet = notarySet
- setting.round = nextRound
- _, isNotary = setting.notarySet[mgr.ID]
- if isNotary {
- mgr.logger.Info("Selected as notary set",
+ _, isDKG = setting.dkgSet[mgr.ID]
+ if isDKG {
+ mgr.logger.Info("Selected as dkg set",
"ID", mgr.ID,
"round", nextRound)
} else {
- mgr.logger.Info("Not selected as notary set",
+ mgr.logger.Info("Not selected as dkg set",
"ID", mgr.ID,
"round", nextRound)
}
// Setup ticker
if tickDuration != curConfig.lambdaBA {
- if setting.ticker != nil {
- setting.ticker.Stop()
+ if ticker != nil {
+ ticker.Stop()
}
- setting.ticker = newTicker(mgr.gov, nextRound, TickerBA)
+ ticker = newTicker(mgr.gov, nextRound, TickerBA)
tickDuration = curConfig.lambdaBA
}
+ setting.ticker = ticker
return
}
Loop:
@@ -403,7 +450,7 @@ Loop:
mgr.recv.npks = nil
mgr.recv.psigSigner = nil
}
- if err := mgr.baRoutineForOneRound(&setting); err != nil {
+ if err := mgr.baRoutineForOneRound(setting); err != nil {
mgr.logger.Error("BA routine failed",
"error", err,
"nodeID", mgr.ID)
@@ -473,13 +520,13 @@ func (mgr *agreementMgr) baRoutineForOneRound(
}
oldPos = nextPos
var leader types.NodeID
- leader, err = mgr.cache.GetLeaderNode(nextPos)
+ leader, err = mgr.calcLeader(setting.dkgSet, setting.crs, nextPos)
if err != nil {
return
}
time.Sleep(nextTime.Sub(time.Now()))
setting.ticker.Restart()
- agr.restart(setting.notarySet, nextPos, leader, setting.crs)
+ agr.restart(setting.dkgSet, setting.threshold, nextPos, leader, setting.crs)
return
}
Loop: