1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
// Copyright 2018 The dexon-consensus-core Authors
// This file is part of the dexon-consensus-core library.
//
// The dexon-consensus-core library is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// The dexon-consensus-core library is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the dexon-consensus-core library. If not, see
// <http://www.gnu.org/licenses/>.
package core
import (
"testing"
"github.com/dexon-foundation/dexon-consensus-core/common"
"github.com/dexon-foundation/dexon-consensus-core/core/crypto/ecdsa"
"github.com/dexon-foundation/dexon-consensus-core/core/types"
"github.com/stretchr/testify/suite"
)
// agreementTestReceiver implements core.agreementReceiveer
type agreementTestReceiver struct {
s *AgreementTestSuite
agreementIndex int
}
func (r *agreementTestReceiver) ProposeVote(vote *types.Vote) {
r.s.voteChan <- vote
}
func (r *agreementTestReceiver) ProposeBlock() {
block := r.s.proposeBlock(r.agreementIndex)
r.s.blockChan <- block.Hash
}
func (r *agreementTestReceiver) ConfirmBlock(block common.Hash,
_ map[types.NodeID]*types.Vote) {
r.s.confirmChan <- block
}
func (s *AgreementTestSuite) proposeBlock(
agreementIdx int) *types.Block {
block := &types.Block{
ProposerID: s.ID,
Hash: common.NewRandomHash(),
}
s.block[block.Hash] = block
s.Require().NoError(s.auths[s.ID].SignCRS(
block, s.agreement[agreementIdx].data.leader.hashCRS))
return block
}
type AgreementTestSuite struct {
suite.Suite
ID types.NodeID
auths map[types.NodeID]*Authenticator
voteChan chan *types.Vote
blockChan chan common.Hash
confirmChan chan common.Hash
block map[common.Hash]*types.Block
agreement []*agreement
}
func (s *AgreementTestSuite) SetupTest() {
prvKey, err := ecdsa.NewPrivateKey()
s.Require().Nil(err)
s.ID = types.NewNodeID(prvKey.PublicKey())
s.auths = map[types.NodeID]*Authenticator{
s.ID: NewAuthenticator(prvKey),
}
s.voteChan = make(chan *types.Vote, 100)
s.blockChan = make(chan common.Hash, 100)
s.confirmChan = make(chan common.Hash, 100)
s.block = make(map[common.Hash]*types.Block)
}
func (s *AgreementTestSuite) newAgreement(numNotarySet int) *agreement {
leader := newLeaderSelector(common.NewRandomHash())
agreementIdx := len(s.agreement)
notarySet := make(map[types.NodeID]struct{})
for i := 0; i < numNotarySet-1; i++ {
prvKey, err := ecdsa.NewPrivateKey()
s.Require().Nil(err)
nID := types.NewNodeID(prvKey.PublicKey())
notarySet[nID] = struct{}{}
s.auths[nID] = NewAuthenticator(prvKey)
}
notarySet[s.ID] = struct{}{}
agreement := newAgreement(
s.ID,
&agreementTestReceiver{
s: s,
agreementIndex: agreementIdx,
},
notarySet,
leader,
s.auths[s.ID],
)
s.agreement = append(s.agreement, agreement)
return agreement
}
func (s *AgreementTestSuite) copyVote(
vote *types.Vote, proposer types.NodeID) *types.Vote {
v := vote.Clone()
s.auths[proposer].SignVote(v)
return v
}
func (s *AgreementTestSuite) TestSimpleConfirm() {
a := s.newAgreement(4)
// PrepareState
a.nextState()
// AckState
s.Require().Len(s.blockChan, 1)
blockHash := <-s.blockChan
block, exist := s.block[blockHash]
s.Require().True(exist)
s.Require().NoError(a.processBlock(block))
a.nextState()
// ConfirmState
s.Require().Len(s.voteChan, 1)
vote := <-s.voteChan
s.Equal(types.VoteAck, vote.Type)
for nID := range s.auths {
v := s.copyVote(vote, nID)
s.Require().NoError(a.processVote(v))
}
a.nextState()
// Pass1State
s.Require().Len(s.voteChan, 1)
vote = <-s.voteChan
s.Equal(types.VoteConfirm, vote.Type)
for nID := range s.auths {
v := s.copyVote(vote, nID)
s.Require().NoError(a.processVote(v))
}
// We have enough of Confirm-Votes.
s.Require().Len(s.confirmChan, 1)
confirmBlock := <-s.confirmChan
s.Equal(blockHash, confirmBlock)
}
func TestAgreement(t *testing.T) {
suite.Run(t, new(AgreementTestSuite))
}
|