aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJimmy Hu <jimmy.hu@dexon.org>2019-05-03 17:59:47 +0800
committerGitHub <noreply@github.com>2019-05-03 17:59:47 +0800
commit889c61c4ea9a38f627a93f7cea617d5073a26f5f (patch)
tree4a93a5c442262c9bbe0eae9cc3067202e852c6d4
parentbbd5a1546ae3ee97034a7df820779d866e932346 (diff)
downloaddexon-consensus-889c61c4ea9a38f627a93f7cea617d5073a26f5f.tar
dexon-consensus-889c61c4ea9a38f627a93f7cea617d5073a26f5f.tar.gz
dexon-consensus-889c61c4ea9a38f627a93f7cea617d5073a26f5f.tar.bz2
dexon-consensus-889c61c4ea9a38f627a93f7cea617d5073a26f5f.tar.lz
dexon-consensus-889c61c4ea9a38f627a93f7cea617d5073a26f5f.tar.xz
dexon-consensus-889c61c4ea9a38f627a93f7cea617d5073a26f5f.tar.zst
dexon-consensus-889c61c4ea9a38f627a93f7cea617d5073a26f5f.zip
core: optimize syncer handling agreement result (#581)
-rw-r--r--core/consensus.go6
-rw-r--r--core/syncer/agreement.go14
-rw-r--r--core/utils.go6
-rw-r--r--core/utils_test.go23
4 files changed, 18 insertions, 31 deletions
diff --git a/core/consensus.go b/core/consensus.go
index a1def58..fd84564 100644
--- a/core/consensus.go
+++ b/core/consensus.go
@@ -1380,11 +1380,7 @@ func (con *Consensus) ProcessAgreementResult(
return nil
}
// Sanity Check.
- notarySet, err := con.nodeSetCache.GetNotarySet(rand.Position.Round)
- if err != nil {
- return err
- }
- if err := VerifyAgreementResult(rand, notarySet); err != nil {
+ if err := VerifyAgreementResult(rand, con.nodeSetCache); err != nil {
con.baMgr.untouchAgreementResult(rand)
return err
}
diff --git a/core/syncer/agreement.go b/core/syncer/agreement.go
index b414e11..d39c246 100644
--- a/core/syncer/agreement.go
+++ b/core/syncer/agreement.go
@@ -176,12 +176,7 @@ func (a *agreement) processAgreementResult(r *types.AgreementResult) {
a.logger.Trace("Agreement result cached", "result", r)
return
}
- notarySet, err := a.cache.GetNotarySet(r.Position.Round)
- if err != nil {
- a.logger.Error("unable to get notary set", "result", r, "error", err)
- return
- }
- if err := core.VerifyAgreementResult(r, notarySet); err != nil {
+ if err := core.VerifyAgreementResult(r, a.cache); err != nil {
a.logger.Error("Agreement result verification failed",
"result", r,
"error", err)
@@ -257,18 +252,13 @@ func (a *agreement) processNewCRS(round uint64) {
a.latestCRSRound = round
// Verify all pending results.
for r := prevRound; r <= a.latestCRSRound; r++ {
- notarySet, err := a.cache.GetNotarySet(r)
- if err != nil {
- a.logger.Error("Unable to get notary set", "round", r, "error", err)
- continue
- }
pendingsForRound := a.pendingAgrs[r]
if pendingsForRound == nil {
continue
}
delete(a.pendingAgrs, r)
for _, res := range pendingsForRound {
- if err := core.VerifyAgreementResult(res, notarySet); err != nil {
+ if err := core.VerifyAgreementResult(res, a.cache); err != nil {
a.logger.Error("Invalid agreement result",
"result", res,
"error", err)
diff --git a/core/utils.go b/core/utils.go
index c9d5f84..c4d7b0f 100644
--- a/core/utils.go
+++ b/core/utils.go
@@ -158,13 +158,17 @@ func HashConfigurationBlock(
// VerifyAgreementResult perform sanity check against a types.AgreementResult
// instance.
func VerifyAgreementResult(
- res *types.AgreementResult, notarySet map[types.NodeID]struct{}) error {
+ res *types.AgreementResult, cache *NodeSetCache) error {
if res.Position.Round >= DKGDelayRound {
if len(res.Randomness) == 0 {
return ErrMissingRandomness
}
return nil
}
+ notarySet, err := cache.GetNotarySet(res.Position.Round)
+ if err != nil {
+ return err
+ }
if len(res.Votes) < len(notarySet)*2/3+1 {
return ErrNotEnoughVotes
}
diff --git a/core/utils_test.go b/core/utils_test.go
index e1bbb1e..560e923 100644
--- a/core/utils_test.go
+++ b/core/utils_test.go
@@ -71,55 +71,52 @@ func (s *UtilsTestSuite) TestVerifyAgreementResult() {
s.Require().NoError(signer.SignVote(vote))
baResult.Votes = append(baResult.Votes, *vote)
}
- nSet, err := cache.GetNotarySet(pos.Round)
- s.Require().NoError(err)
- s.Require().NotEmpty(nSet)
- s.Require().NoError(VerifyAgreementResult(baResult, nSet))
+ s.Require().NoError(VerifyAgreementResult(baResult, cache))
// Test negative case.
// All period should be the same.
baResult.Votes[1].Period++
- s.Equal(ErrIncorrectVotePeriod, VerifyAgreementResult(baResult, nSet))
+ s.Equal(ErrIncorrectVotePeriod, VerifyAgreementResult(baResult, cache))
baResult.Votes[1].Period--
// Blockhash should match the one in votes.
baResult.BlockHash = common.NewRandomHash()
- s.Equal(ErrIncorrectVoteBlockHash, VerifyAgreementResult(baResult, nSet))
+ s.Equal(ErrIncorrectVoteBlockHash, VerifyAgreementResult(baResult, cache))
baResult.BlockHash = hash
// Position should match.
baResult.Position.Height++
- s.Equal(ErrIncorrectVotePosition, VerifyAgreementResult(baResult, nSet))
+ s.Equal(ErrIncorrectVotePosition, VerifyAgreementResult(baResult, cache))
baResult.Position = pos
// types.VotePreCom is not accepted in agreement result.
baResult.Votes[0].Type = types.VotePreCom
- s.Equal(ErrIncorrectVoteType, VerifyAgreementResult(baResult, nSet))
+ s.Equal(ErrIncorrectVoteType, VerifyAgreementResult(baResult, cache))
baResult.Votes[0].Type = types.VoteCom
// Vote type should be the same.
baResult.Votes[1].Type = types.VoteFastCom
- s.Equal(ErrIncorrectVoteType, VerifyAgreementResult(baResult, nSet))
+ s.Equal(ErrIncorrectVoteType, VerifyAgreementResult(baResult, cache))
baResult.Votes[1].Type = types.VoteCom
// Only vote proposed by notarySet is valid.
baResult.Votes[0].ProposerID = types.NodeID{Hash: common.NewRandomHash()}
- s.Equal(ErrIncorrectVoteProposer, VerifyAgreementResult(baResult, nSet))
+ s.Equal(ErrIncorrectVoteProposer, VerifyAgreementResult(baResult, cache))
baResult.Votes[0].ProposerID = types.NewNodeID(pubKeys[0])
// Vote shuold have valid signature.
baResult.Votes[0].Signature, err = prvKeys[0].Sign(common.NewRandomHash())
s.Require().NoError(err)
- s.Equal(ErrIncorrectVoteSignature, VerifyAgreementResult(baResult, nSet))
+ s.Equal(ErrIncorrectVoteSignature, VerifyAgreementResult(baResult, cache))
s.Require().NoError(signers[0].SignVote(&baResult.Votes[0]))
// Unique votes shuold be more than threshold.
baResult.Votes = baResult.Votes[:1]
- s.Equal(ErrNotEnoughVotes, VerifyAgreementResult(baResult, nSet))
+ s.Equal(ErrNotEnoughVotes, VerifyAgreementResult(baResult, cache))
for range signers {
baResult.Votes = append(baResult.Votes, baResult.Votes[0])
}
- s.Equal(ErrNotEnoughVotes, VerifyAgreementResult(baResult, nSet))
+ s.Equal(ErrNotEnoughVotes, VerifyAgreementResult(baResult, cache))
}
func TestUtils(t *testing.T) {