aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorJimmy Hu <jimmy.hu@dexon.org>2018-10-26 16:45:58 +0800
committerGitHub <noreply@github.com>2018-10-26 16:45:58 +0800
commit8aec64250e6d9dbdc263f11716a7575c3f3af789 (patch)
tree4a479acd308603eda645460f9fb6ddb8fabac6d1 /core
parent12f7924d4e2496bb336131c4917d45c5bd6b414b (diff)
downloaddexon-consensus-8aec64250e6d9dbdc263f11716a7575c3f3af789.tar
dexon-consensus-8aec64250e6d9dbdc263f11716a7575c3f3af789.tar.gz
dexon-consensus-8aec64250e6d9dbdc263f11716a7575c3f3af789.tar.bz2
dexon-consensus-8aec64250e6d9dbdc263f11716a7575c3f3af789.tar.lz
dexon-consensus-8aec64250e6d9dbdc263f11716a7575c3f3af789.tar.xz
dexon-consensus-8aec64250e6d9dbdc263f11716a7575c3f3af789.tar.zst
dexon-consensus-8aec64250e6d9dbdc263f11716a7575c3f3af789.zip
core: Unit test for BA sync (#265)
Diffstat (limited to 'core')
-rw-r--r--core/consensus.go13
-rw-r--r--core/consensus_test.go73
2 files changed, 86 insertions, 0 deletions
diff --git a/core/consensus.go b/core/consensus.go
index 23b33ed..5ad37a9 100644
--- a/core/consensus.go
+++ b/core/consensus.go
@@ -49,6 +49,10 @@ var (
"not enought votes")
ErrIncorrectVoteBlockHash = fmt.Errorf(
"incorrect vote block hash")
+ ErrIncorrectVoteType = fmt.Errorf(
+ "incorrect vote type")
+ ErrIncorrectVotePosition = fmt.Errorf(
+ "incorrect vote position")
ErrIncorrectVoteProposer = fmt.Errorf(
"incorrect vote proposer")
ErrIncorrectBlockRandomnessResult = fmt.Errorf(
@@ -754,10 +758,19 @@ func (con *Consensus) ProcessAgreementResult(
if len(rand.Votes) < len(notarySet)/3*2+1 {
return ErrNotEnoughVotes
}
+ if len(rand.Votes) > len(notarySet) {
+ return ErrIncorrectVoteProposer
+ }
for _, vote := range rand.Votes {
if vote.BlockHash != rand.BlockHash {
return ErrIncorrectVoteBlockHash
}
+ if vote.Type != types.VoteCom {
+ return ErrIncorrectVoteType
+ }
+ if vote.Position != rand.Position {
+ return ErrIncorrectVotePosition
+ }
if _, exist := notarySet[vote.ProposerID]; !exist {
return ErrIncorrectVoteProposer
}
diff --git a/core/consensus_test.go b/core/consensus_test.go
index 7aa2493..e79028f 100644
--- a/core/consensus_test.go
+++ b/core/consensus_test.go
@@ -541,6 +541,79 @@ func (s *ConsensusTestSuite) TestDKGCRS() {
}
s.NotNil(gov.CRS(1))
}
+
+func (s *ConsensusTestSuite) TestSyncBA() {
+ conn := s.newNetworkConnection()
+ prvKeys, pubKeys, err := test.NewKeys(4)
+ s.Require().NoError(err)
+ gov, err := test.NewGovernance(pubKeys, time.Second)
+ s.Require().NoError(err)
+ prvKey := prvKeys[0]
+ _, con := s.prepareConsensus(time.Now().UTC(), gov, prvKey, conn)
+ hash := common.NewRandomHash()
+ auths := make([]*Authenticator, 0, len(prvKeys))
+ for _, prvKey := range prvKeys {
+ auths = append(auths, NewAuthenticator(prvKey))
+ }
+ pos := types.Position{
+ Round: 0,
+ ChainID: 0,
+ Height: 20,
+ }
+ baResult := &types.AgreementResult{
+ BlockHash: hash,
+ Position: pos,
+ }
+ for _, auth := range auths {
+ vote := &types.Vote{
+ Type: types.VoteCom,
+ BlockHash: hash,
+ Position: pos,
+ }
+ s.Require().NoError(auth.SignVote(vote))
+ baResult.Votes = append(baResult.Votes, *vote)
+ }
+ s.Require().NoError(con.ProcessAgreementResult(baResult))
+ aID := con.baModules[0].agreementID()
+ s.Equal(pos, aID)
+
+ // Test negative case.
+ baResult.BlockHash = common.NewRandomHash()
+ s.Equal(ErrIncorrectVoteBlockHash, con.ProcessAgreementResult(baResult))
+ baResult.BlockHash = hash
+
+ baResult.Position.Height++
+ s.Equal(ErrIncorrectVotePosition, con.ProcessAgreementResult(baResult))
+ baResult.Position = pos
+
+ baResult.Votes[0].Type = types.VotePreCom
+ s.Equal(ErrIncorrectVoteType, con.ProcessAgreementResult(baResult))
+ baResult.Votes[0].Type = types.VoteCom
+
+ baResult.Votes[0].ProposerID = types.NodeID{Hash: common.NewRandomHash()}
+ s.Equal(ErrIncorrectVoteProposer, con.ProcessAgreementResult(baResult))
+ baResult.Votes[0].ProposerID = types.NewNodeID(pubKeys[0])
+
+ baResult.Votes[0].Signature, err = prvKeys[0].Sign(common.NewRandomHash())
+ s.Require().NoError(err)
+ s.Equal(ErrIncorrectVoteSignature, con.ProcessAgreementResult(baResult))
+ s.Require().NoError(auths[0].SignVote(&baResult.Votes[0]))
+
+ for _, auth := range auths {
+ vote := &types.Vote{
+ Type: types.VoteCom,
+ BlockHash: hash,
+ Position: pos,
+ }
+ s.Require().NoError(auth.SignVote(vote))
+ baResult.Votes = append(baResult.Votes, *vote)
+ }
+ s.Equal(ErrIncorrectVoteProposer, con.ProcessAgreementResult(baResult))
+
+ baResult.Votes = baResult.Votes[:1]
+ s.Equal(ErrNotEnoughVotes, con.ProcessAgreementResult(baResult))
+}
+
func TestConsensus(t *testing.T) {
suite.Run(t, new(ConsensusTestSuite))
}