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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
// Copyright 2018 The dexon-consensus Authors
// This file is part of the dexon-consensus library.
//
// The dexon-consensus 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 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 library. If not, see
// <http://www.gnu.org/licenses/>.
package test
import (
"sort"
"testing"
"time"
"github.com/dexon-foundation/dexon-consensus/common"
"github.com/dexon-foundation/dexon-consensus/core/crypto"
"github.com/dexon-foundation/dexon-consensus/core/crypto/dkg"
"github.com/dexon-foundation/dexon-consensus/core/crypto/ecdsa"
"github.com/dexon-foundation/dexon-consensus/core/types"
typesDKG "github.com/dexon-foundation/dexon-consensus/core/types/dkg"
"github.com/stretchr/testify/suite"
)
type StateTestSuite struct {
suite.Suite
}
func (s *StateTestSuite) newDKGMasterPublicKey(
round uint64) *typesDKG.MasterPublicKey {
prvKey, err := ecdsa.NewPrivateKey()
s.Require().NoError(err)
pubKey := prvKey.PublicKey()
nodeID := types.NewNodeID(pubKey)
_, pubShare := dkg.NewPrivateKeyShares(3)
dID, err := dkg.BytesID(nodeID.Hash[:])
s.Require().NoError(err)
return &typesDKG.MasterPublicKey{
ProposerID: nodeID,
Round: round,
DKGID: dID,
PublicKeyShares: *pubShare,
}
}
func (s *StateTestSuite) newDKGComplaint(round uint64) *typesDKG.Complaint {
prvKey, err := ecdsa.NewPrivateKey()
s.Require().NoError(err)
pubKey := prvKey.PublicKey()
nodeID := types.NewNodeID(pubKey)
// TODO(mission): sign it, and it doesn't make sense to complaint self.
return &typesDKG.Complaint{
ProposerID: nodeID,
Round: round,
PrivateShare: typesDKG.PrivateShare{
ProposerID: nodeID,
ReceiverID: nodeID,
Round: round,
PrivateShare: *dkg.NewPrivateKey(),
},
}
}
func (s *StateTestSuite) newDKGFinal(round uint64) *typesDKG.Finalize {
prvKey, err := ecdsa.NewPrivateKey()
s.Require().NoError(err)
pubKey := prvKey.PublicKey()
nodeID := types.NewNodeID(pubKey)
// TODO(mission): sign it.
return &typesDKG.Finalize{
ProposerID: nodeID,
Round: round,
}
}
func (s *StateTestSuite) compareNodes(node1, node2 []crypto.PublicKey) bool {
id1 := common.Hashes{}
for _, n := range node1 {
id1 = append(id1, types.NewNodeID(n).Hash)
}
sort.Sort(id1)
id2 := common.Hashes{}
for _, n := range node2 {
id2 = append(id2, types.NewNodeID(n).Hash)
}
sort.Sort(id2)
if len(id1) != len(id2) {
return false
}
for idx, id := range id1 {
if id != id2[idx] {
return false
}
}
return true
}
func (s *StateTestSuite) findNode(
nodes []crypto.PublicKey, node crypto.PublicKey) bool {
nodeID := types.NewNodeID(node)
for _, n := range nodes {
nID := types.NewNodeID(n)
if nID == nodeID {
return true
}
}
return false
}
func (s *StateTestSuite) makeDKGChanges(
st *State,
masterPubKey *typesDKG.MasterPublicKey,
complaint *typesDKG.Complaint,
final *typesDKG.Finalize) {
st.RequestChange(StateAddDKGMasterPublicKey, masterPubKey)
st.RequestChange(StateAddDKGComplaint, complaint)
st.RequestChange(StateAddDKGFinal, final)
}
func (s *StateTestSuite) makeConfigChanges(st *State) {
st.RequestChange(StateChangeNumChains, uint32(7))
st.RequestChange(StateChangeLambdaBA, time.Nanosecond)
st.RequestChange(StateChangeLambdaDKG, time.Millisecond)
st.RequestChange(StateChangeRoundInterval, time.Hour)
st.RequestChange(StateChangeMinBlockInterval, time.Second)
st.RequestChange(StateChangeMaxBlockInterval, time.Minute)
st.RequestChange(StateChangeK, 1)
st.RequestChange(StateChangePhiRatio, float32(0.5))
st.RequestChange(StateChangeNotarySetSize, uint32(5))
st.RequestChange(StateChangeDKGSetSize, uint32(6))
}
func (s *StateTestSuite) checkConfigChanges(config *types.Config) {
req := s.Require()
req.Equal(config.NumChains, uint32(7))
req.Equal(config.LambdaBA, time.Nanosecond)
req.Equal(config.LambdaDKG, time.Millisecond)
req.Equal(config.RoundInterval, time.Hour)
req.Equal(config.MinBlockInterval, time.Second)
req.Equal(config.K, 1)
req.Equal(config.PhiRatio, float32(0.5))
req.Equal(config.NotarySetSize, uint32(5))
req.Equal(config.DKGSetSize, uint32(6))
}
func (s *StateTestSuite) TestEqual() {
var (
req = s.Require()
lambda = 250 * time.Millisecond
)
_, genesisNodes, err := NewKeys(20)
req.NoError(err)
st := NewState(genesisNodes, lambda, true)
req.NoError(st.Equal(st))
// One node is missing.
st1 := NewState(genesisNodes, lambda, true)
for nID := range st1.nodes {
delete(st1.nodes, nID)
break
}
req.Equal(st.Equal(st1), ErrStateNodeSetNotEqual)
// Make some changes.
st2 := st.Clone()
req.NoError(st.Equal(st2))
s.makeConfigChanges(st)
req.Equal(st.Equal(st2), ErrStateConfigNotEqual)
crs := common.NewRandomHash()
req.NoError(st.ProposeCRS(1, crs))
masterPubKey := s.newDKGMasterPublicKey(2)
comp := s.newDKGComplaint(2)
final := s.newDKGFinal(2)
s.makeDKGChanges(st, masterPubKey, comp, final)
// Remove dkg complaints from cloned one to check if equal.
st3 := st.Clone()
req.NoError(st.Equal(st3))
delete(st3.dkgComplaints, uint64(2))
req.Equal(st.Equal(st3), ErrStateDKGComplaintsNotEqual)
// Remove dkg master public key from cloned one to check if equal.
st4 := st.Clone()
req.NoError(st.Equal(st4))
delete(st4.dkgMasterPublicKeys, uint64(2))
req.Equal(st.Equal(st4), ErrStateDKGMasterPublicKeysNotEqual)
// Remove dkg finalize from cloned one to check if equal.
st5 := st.Clone()
req.NoError(st.Equal(st5))
delete(st5.dkgFinals, uint64(2))
req.Equal(st.Equal(st5), ErrStateDKGFinalsNotEqual)
}
func (s *StateTestSuite) TestPendingChangesEqual() {
var (
req = s.Require()
lambda = 250 * time.Millisecond
)
// Setup a non-local mode State instance.
_, genesisNodes, err := NewKeys(20)
req.NoError(err)
st := NewState(genesisNodes, lambda, false)
req.NoError(st.Equal(st))
// Apply some changes.
s.makeConfigChanges(st)
crs := common.NewRandomHash()
req.NoError(st.ProposeCRS(1, crs))
masterPubKey := s.newDKGMasterPublicKey(2)
comp := s.newDKGComplaint(2)
final := s.newDKGFinal(2)
s.makeDKGChanges(st, masterPubKey, comp, final)
// Remove pending config changes.
st1 := st.Clone()
req.NoError(st.Equal(st1))
st1.pendingChangedConfigs = make(map[StateChangeType]interface{})
req.Equal(st.Equal(st1), ErrStatePendingChangesNotEqual)
// Remove pending crs changes.
st2 := st.Clone()
req.NoError(st.Equal(st2))
st2.pendingCRS = []*crsAdditionRequest{}
req.Equal(st.Equal(st2), ErrStatePendingChangesNotEqual)
// Remove pending dkg complaints changes.
st3 := st.Clone()
req.NoError(st.Equal(st3))
st3.pendingDKGComplaints = []*typesDKG.Complaint{}
req.Equal(st.Equal(st3), ErrStatePendingChangesNotEqual)
// Remove pending dkg master public key changes.
st4 := st.Clone()
req.NoError(st.Equal(st4))
st4.pendingDKGMasterPublicKeys = []*typesDKG.MasterPublicKey{}
req.Equal(st.Equal(st4), ErrStatePendingChangesNotEqual)
// Remove pending dkg finalize changes.
st5 := st.Clone()
req.NoError(st.Equal(st5))
st5.pendingDKGFinals = []*typesDKG.Finalize{}
req.Equal(st.Equal(st5), ErrStatePendingChangesNotEqual)
}
func (s *StateTestSuite) TestLocalMode() {
// Test State with local mode.
var (
req = s.Require()
lambda = 250 * time.Millisecond
)
_, genesisNodes, err := NewKeys(20)
req.NoError(err)
st := NewState(genesisNodes, lambda, true)
config1, nodes1 := st.Snapshot()
req.True(s.compareNodes(genesisNodes, nodes1))
// Check settings of config1 affected by genesisNodes and lambda.
req.Equal(config1.NumChains, uint32(len(genesisNodes)))
req.Equal(config1.LambdaBA, lambda)
req.Equal(config1.LambdaDKG, lambda*10)
req.Equal(config1.RoundInterval, lambda*10000)
req.Equal(config1.NotarySetSize, uint32(len(genesisNodes)))
req.Equal(config1.DKGSetSize, uint32(len(genesisNodes)))
req.Equal(config1.K, 0)
req.Equal(config1.PhiRatio, float32(0.667))
// Request some changes, every fields for config should be affected.
s.makeConfigChanges(st)
// Add new node.
prvKey, err := ecdsa.NewPrivateKey()
req.NoError(err)
pubKey := prvKey.PublicKey()
st.RequestChange(StateAddNode, pubKey)
config2, newNodes := st.Snapshot()
// Check if config changes are applied.
s.checkConfigChanges(config2)
// Check if new node is added.
req.True(s.findNode(newNodes, pubKey))
// Test adding CRS.
crs := common.NewRandomHash()
req.NoError(st.ProposeCRS(1, crs))
req.Equal(st.CRS(1), crs)
// Test adding node set, DKG complaints, final, master public key.
// Make sure everything is empty before changed.
req.Empty(st.DKGMasterPublicKeys(2))
req.Empty(st.DKGComplaints(2))
req.False(st.IsDKGFinal(2, 0))
// Add DKG stuffs.
masterPubKey := s.newDKGMasterPublicKey(2)
comp := s.newDKGComplaint(2)
final := s.newDKGFinal(2)
s.makeDKGChanges(st, masterPubKey, comp, final)
// Check DKGMasterPublicKeys.
masterKeyForRound := st.DKGMasterPublicKeys(2)
req.Len(masterKeyForRound, 1)
req.True(masterKeyForRound[0].Equal(masterPubKey))
// Check DKGComplaints.
compForRound := st.DKGComplaints(2)
req.Len(compForRound, 1)
req.True(compForRound[0].Equal(comp))
// Check IsDKGFinal.
req.True(st.IsDKGFinal(2, 0))
}
func (s *StateTestSuite) TestPacking() {
// Make sure everything works when requests are packing
// and unpacked to apply.
var (
req = s.Require()
lambda = 250 * time.Millisecond
)
// Make config changes.
_, genesisNodes, err := NewKeys(20)
req.NoError(err)
st := NewState(genesisNodes, lambda, false)
s.makeConfigChanges(st)
// Add new CRS.
crs := common.NewRandomHash()
req.NoError(st.ProposeCRS(1, crs))
// Add new node.
prvKey, err := ecdsa.NewPrivateKey()
req.NoError(err)
pubKey := prvKey.PublicKey()
st.RequestChange(StateAddNode, pubKey)
// Add DKG stuffs.
masterPubKey := s.newDKGMasterPublicKey(2)
comp := s.newDKGComplaint(2)
final := s.newDKGFinal(2)
s.makeDKGChanges(st, masterPubKey, comp, final)
// Make sure everything is empty before changed.
req.Empty(st.DKGMasterPublicKeys(2))
req.Empty(st.DKGComplaints(2))
req.False(st.IsDKGFinal(2, 0))
// Pack changes into bytes.
b, err := st.PackRequests()
req.NoError(err)
req.NotEmpty(b)
// Apply those bytes back.
req.NoError(st.Apply(b))
// Check if configs are changed.
config, nodes := st.Snapshot()
s.checkConfigChanges(config)
// Check if CRS is added.
req.Equal(st.CRS(1), crs)
// Check if new node is added.
req.True(s.findNode(nodes, pubKey))
// Check DKGMasterPublicKeys.
masterKeyForRound := st.DKGMasterPublicKeys(2)
req.Len(masterKeyForRound, 1)
req.True(masterKeyForRound[0].Equal(masterPubKey))
// Check DKGComplaints.
compForRound := st.DKGComplaints(2)
req.Len(compForRound, 1)
req.True(compForRound[0].Equal(comp))
// Check IsDKGFinal.
req.True(st.IsDKGFinal(2, 0))
}
func TestState(t *testing.T) {
suite.Run(t, new(StateTestSuite))
}
|