aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMission Liao <mission.liao@dexon.org>2018-11-02 16:05:25 +0800
committerGitHub <noreply@github.com>2018-11-02 16:05:25 +0800
commit513bfa84c18c53d1d9d7a02b68945a758d825362 (patch)
treec616b379a975724ffdab6984c0a13cb4d173abf7
parent5170bff5f5332fd7782f300cb4a1d63f3cd3664c (diff)
downloaddexon-consensus-513bfa84c18c53d1d9d7a02b68945a758d825362.tar
dexon-consensus-513bfa84c18c53d1d9d7a02b68945a758d825362.tar.gz
dexon-consensus-513bfa84c18c53d1d9d7a02b68945a758d825362.tar.bz2
dexon-consensus-513bfa84c18c53d1d9d7a02b68945a758d825362.tar.lz
dexon-consensus-513bfa84c18c53d1d9d7a02b68945a758d825362.tar.xz
dexon-consensus-513bfa84c18c53d1d9d7a02b68945a758d825362.tar.zst
dexon-consensus-513bfa84c18c53d1d9d7a02b68945a758d825362.zip
core: reset CRS in leader selector by round (#286)
-rw-r--r--core/agreement-state_test.go5
-rw-r--r--core/agreement.go7
-rw-r--r--core/agreement_test.go5
-rw-r--r--core/consensus.go24
-rw-r--r--core/consensus_test.go2
-rw-r--r--core/leader-selector.go13
-rw-r--r--core/leader-selector_test.go6
7 files changed, 28 insertions, 34 deletions
diff --git a/core/agreement-state_test.go b/core/agreement-state_test.go
index 5844c6d..7bb12be 100644
--- a/core/agreement-state_test.go
+++ b/core/agreement-state_test.go
@@ -97,7 +97,7 @@ func (s *AgreementStateTestSuite) SetupTest() {
}
func (s *AgreementStateTestSuite) newAgreement(numNode int) *agreement {
- leader := newLeaderSelector(common.NewRandomHash(), func(*types.Block) bool {
+ leader := newLeaderSelector(func(*types.Block) bool {
return true
})
notarySet := make(map[types.NodeID]struct{})
@@ -115,11 +115,10 @@ func (s *AgreementStateTestSuite) newAgreement(numNode int) *agreement {
s: s,
leader: leader,
},
- notarySet,
leader,
s.auths[s.ID],
)
- agreement.restart(notarySet, types.Position{})
+ agreement.restart(notarySet, types.Position{}, common.NewRandomHash())
return agreement
}
diff --git a/core/agreement.go b/core/agreement.go
index 8741baf..d6875bc 100644
--- a/core/agreement.go
+++ b/core/agreement.go
@@ -118,7 +118,6 @@ type agreement struct {
func newAgreement(
ID types.NodeID,
recv agreementReceiver,
- notarySet map[types.NodeID]struct{},
leader *leaderSelector,
authModule *Authenticator) *agreement {
agreement := &agreement{
@@ -137,7 +136,7 @@ func newAgreement(
// restart the agreement
func (a *agreement) restart(
- notarySet map[types.NodeID]struct{}, aID types.Position) {
+ notarySet map[types.NodeID]struct{}, aID types.Position, crs common.Hash) {
func() {
a.lock.Lock()
@@ -151,7 +150,7 @@ func (a *agreement) restart(
a.data.period = 1
a.data.blocks = make(map[types.NodeID]*types.Block)
a.data.requiredVote = len(notarySet)/3*2 + 1
- a.data.leader.restart()
+ a.data.leader.restart(crs)
a.data.lockValue = nullBlockHash
a.data.lockRound = 1
a.fastForward = make(chan uint64, 1)
@@ -213,7 +212,7 @@ func (a *agreement) restart(
func (a *agreement) stop() {
a.restart(make(map[types.NodeID]struct{}), types.Position{
ChainID: math.MaxUint32,
- })
+ }, common.Hash{})
}
func isStop(aID types.Position) bool {
diff --git a/core/agreement_test.go b/core/agreement_test.go
index dd9be31..74687ca 100644
--- a/core/agreement_test.go
+++ b/core/agreement_test.go
@@ -93,7 +93,7 @@ func (s *AgreementTestSuite) SetupTest() {
}
func (s *AgreementTestSuite) newAgreement(numNotarySet int) *agreement {
- leader := newLeaderSelector(common.NewRandomHash(), func(*types.Block) bool {
+ leader := newLeaderSelector(func(*types.Block) bool {
return true
})
agreementIdx := len(s.agreement)
@@ -112,11 +112,10 @@ func (s *AgreementTestSuite) newAgreement(numNotarySet int) *agreement {
s: s,
agreementIndex: agreementIdx,
},
- notarySet,
leader,
s.auths[s.ID],
)
- agreement.restart(notarySet, types.Position{})
+ agreement.restart(notarySet, types.Position{}, common.NewRandomHash())
s.agreement = append(s.agreement, agreement)
return agreement
}
diff --git a/core/consensus.go b/core/consensus.go
index 0371e60..29d4aa2 100644
--- a/core/consensus.go
+++ b/core/consensus.go
@@ -315,12 +315,6 @@ func NewConsensus(
config := gov.Configuration(round)
nodeSetCache := NewNodeSetCache(gov)
logger.Debug("Calling Governance.CRS", "round", round)
- crs := gov.CRS(round)
- // Setup acking by information returned from Governace.
- nodes, err := nodeSetCache.GetNodeSet(round)
- if err != nil {
- panic(err)
- }
// Setup auth module.
authModule := NewAuthenticator(prv)
// Check if the application implement Debug interface.
@@ -385,8 +379,7 @@ func NewConsensus(
agreementModule := newAgreement(
con.ID,
recv,
- nodes.IDs,
- newLeaderSelector(crs, validLeader),
+ newLeaderSelector(validLeader),
con.authModule,
)
// Hacky way to make agreement module self contained.
@@ -448,6 +441,7 @@ func (con *Consensus) runBA(chainID uint32, tick <-chan struct{}) {
recv := con.receivers[chainID]
recv.restartNotary <- true
nIDs := make(map[types.NodeID]struct{})
+ crs := common.Hash{}
// Reset ticker
<-tick
BALoop:
@@ -466,16 +460,17 @@ BALoop:
if err != nil {
panic(err)
}
+ con.logger.Debug("Calling Governance.CRS", "round", recv.round)
+ crs = con.gov.CRS(recv.round)
con.logger.Debug("Calling Governance.Configuration",
"round", recv.round)
- con.logger.Debug("Calling Governance.CRS", "round", recv.round)
nIDs = nodes.GetSubSet(
int(con.gov.Configuration(recv.round).NotarySetSize),
- types.NewNotarySetTarget(con.gov.CRS(recv.round), chainID))
+ types.NewNotarySetTarget(crs, chainID))
}
nextPos := con.lattice.NextPosition(chainID)
nextPos.Round = recv.round
- agreement.restart(nIDs, nextPos)
+ agreement.restart(nIDs, nextPos, crs)
default:
}
if agreement.pullVotes() {
@@ -809,14 +804,15 @@ func (con *Consensus) ProcessAgreementResult(
con.logger.Debug("Calling Network.PullBlocks for syncing BA",
"hash", rand.BlockHash)
con.network.PullBlocks(common.Hashes{rand.BlockHash})
+ con.logger.Debug("Calling Governance.CRS", "round", rand.Position.Round)
+ crs := con.gov.CRS(rand.Position.Round)
nIDs := nodes.GetSubSet(
int(con.gov.Configuration(rand.Position.Round).NotarySetSize),
- types.NewNotarySetTarget(
- con.gov.CRS(rand.Position.Round), rand.Position.ChainID))
+ types.NewNotarySetTarget(crs, rand.Position.ChainID))
for _, vote := range rand.Votes {
agreement.processVote(&vote)
}
- agreement.restart(nIDs, rand.Position)
+ agreement.restart(nIDs, rand.Position, crs)
}
// Calculating randomness.
if rand.Position.Round == 0 {
diff --git a/core/consensus_test.go b/core/consensus_test.go
index 209a91a..f5ab69f 100644
--- a/core/consensus_test.go
+++ b/core/consensus_test.go
@@ -200,7 +200,7 @@ func (s *ConsensusTestSuite) prepareConsensus(
Round: round,
ChainID: chainID,
Height: uint64(0),
- })
+ }, gov.CRS(round))
}
return app, con
}
diff --git a/core/leader-selector.go b/core/leader-selector.go
index 2be596a..08006db 100644
--- a/core/leader-selector.go
+++ b/core/leader-selector.go
@@ -59,13 +59,8 @@ type leaderSelector struct {
lock sync.Mutex
}
-func newLeaderSelector(
- crs common.Hash, validLeader validLeaderFn) *leaderSelector {
- numCRS := big.NewInt(0)
- numCRS.SetBytes(crs[:])
+func newLeaderSelector(validLeader validLeaderFn) *leaderSelector {
return &leaderSelector{
- numCRS: numCRS,
- hashCRS: crs,
minCRSBlock: maxHash,
validLeader: validLeader,
}
@@ -86,9 +81,13 @@ func (l *leaderSelector) probability(sig crypto.Signature) float64 {
return p
}
-func (l *leaderSelector) restart() {
+func (l *leaderSelector) restart(crs common.Hash) {
+ numCRS := big.NewInt(0)
+ numCRS.SetBytes(crs[:])
l.lock.Lock()
defer l.lock.Unlock()
+ l.numCRS = numCRS
+ l.hashCRS = crs
l.minCRSBlock = maxHash
l.minBlockHash = common.Hash{}
l.pendingBlocks = []*types.Block{}
diff --git a/core/leader-selector_test.go b/core/leader-selector_test.go
index 07e561a..975fd13 100644
--- a/core/leader-selector_test.go
+++ b/core/leader-selector_test.go
@@ -46,7 +46,9 @@ func (s *LeaderSelectorTestSuite) SetupTest() {
}
func (s *LeaderSelectorTestSuite) newLeader() *leaderSelector {
- return newLeaderSelector(common.NewRandomHash(), s.mockValidLeader)
+ l := newLeaderSelector(s.mockValidLeader)
+ l.restart(common.NewRandomHash())
+ return l
}
func (s *LeaderSelectorTestSuite) TestDistance() {
@@ -138,7 +140,7 @@ func (s *LeaderSelectorTestSuite) TestValidLeaderFn() {
blockHash := leader.leaderBlockHash()
s.mockValidLeaderDB[blockHash] = false
- leader.restart()
+ leader.restart(leader.hashCRS)
for _, b := range blocks {
s.Require().NoError(leader.processBlock(b))
}