This repository was archived by the owner on Sep 20, 2022. It is now read-only.
forked from dedis/onet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.go
913 lines (823 loc) · 25.1 KB
/
tree.go
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
package onet
import (
"bytes"
cryptorand "crypto/rand"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"math/rand"
"sort"
"go.dedis.ch/onet/v3/ciphersuite"
"go.dedis.ch/onet/v3/log"
"go.dedis.ch/onet/v3/network"
"golang.org/x/xerrors"
uuid "gopkg.in/satori/go.uuid.v1"
)
// In this file we define the main structures used for a running protocol
// instance. First there is the ServerIdentity struct: it represents the ServerIdentity of
// someone, a server over the internet, mainly tied by its public key.
// The tree contains the peerId which is the ID given to a an ServerIdentity / server
// during one protocol instance. A server can have many peerId in one tree.
//
// ProtocolInstance needs to know:
// - which Roster we are using ( a selection of proper servers )
// - which Tree we are using
// - The overlay network: a mapping from PeerId
//
// It contains the PeerId of the parent and the sub tree of the children.
func init() {
network.RegisterMessage(Tree{})
network.RegisterMessage(tbmStruct{})
}
// Tree is a topology to be used by any network layer/host layer.
// It contains the peer list we use, and the tree we use
type Tree struct {
ID TreeID
Roster *Roster
Root *TreeNode
}
// TreeID uniquely identifies a Tree struct in the onet framework.
type TreeID uuid.UUID
// Equal returns true if and only if tID2 equals this TreeID.
func (tId TreeID) Equal(tID2 TreeID) bool {
return uuid.Equal(uuid.UUID(tId), uuid.UUID(tID2))
}
// Equals will be removed!
func (tId TreeID) Equals(tID2 TreeID) bool {
log.Warn("Deprecated: TreeID.Equals will be removed in onet.v2")
return tId.Equal(tID2)
}
// String returns a canonical representation of the TreeID.
func (tId TreeID) String() string {
return uuid.UUID(tId).String()
}
// IsNil returns true iff the TreeID is Nil
func (tId TreeID) IsNil() bool {
return tId.Equal(TreeID(uuid.Nil))
}
// NewTree creates a new tree using the given roster and root. It
// also generates the id.
func NewTree(roster *Roster, root *TreeNode) *Tree {
// walk the tree with DFS to build a unique hash
h := sha256.New()
root.Visit(0, func(d int, tn *TreeNode) {
_, err := tn.ServerIdentity.PublicKey.WriteTo(h)
if err != nil {
log.Error(err)
}
if tn.IsLeaf() {
// to prevent generating the same hash for tree with
// the same nodes but with a different structure
_, err = h.Write([]byte{1})
if err != nil {
log.Error(err)
}
}
})
url := network.NamespaceURL + "tree/" + roster.GetID().String() + hex.EncodeToString(h.Sum(nil))
return &Tree{
Roster: roster,
Root: root,
ID: TreeID(uuid.NewV5(uuid.NamespaceURL, url)),
}
}
// NewTreeFromMarshal takes a slice of bytes and an Roster to re-create
// the original tree
func NewTreeFromMarshal(buf []byte, el *Roster) (*Tree, error) {
tp, pm, err := network.Unmarshal(buf)
if err != nil {
return nil, err
}
if !tp.Equal(TreeMarshalTypeID) {
return nil, xerrors.New("Didn't receive TreeMarshal-struct")
}
t, err := pm.(*TreeMarshal).MakeTree(el)
if err != nil {
return nil, xerrors.Errorf("making tree: %v", err)
}
return t, nil
}
// MakeTreeMarshal creates a replacement-tree that is safe to send: no
// parent (creates loops), only sends ids (not send the roster again)
func (t *Tree) MakeTreeMarshal() *TreeMarshal {
if t.Roster == nil {
return &TreeMarshal{}
}
treeM := &TreeMarshal{
TreeID: t.ID,
RosterID: t.Roster.GetID(),
}
treeM.Children = append(treeM.Children, TreeMarshalCopyTree(t.Root))
return treeM
}
// Marshal creates a simple binary-representation of the tree containing only
// the ids of the elements. Use NewTreeFromMarshal to get back the original
// tree
func (t *Tree) Marshal() ([]byte, error) {
buf, err := network.Marshal(t.MakeTreeMarshal())
if err != nil {
return nil, xerrors.Errorf("making tree marshal: %v", err)
}
return buf, nil
}
type tbmStruct struct {
T []byte
Ro *Roster
}
// BinaryMarshaler does the same as Marshal
func (t *Tree) BinaryMarshaler() ([]byte, error) {
bt, err := t.Marshal()
if err != nil {
return nil, xerrors.Errorf("marshaling: %v", err)
}
tbm := &tbmStruct{
T: bt,
Ro: t.Roster,
}
b, err := network.Marshal(tbm)
if err != nil {
return nil, xerrors.Errorf("marshaling: %v", err)
}
return b, nil
}
// BinaryUnmarshaler takes a TreeMarshal and stores it in the tree
func (t *Tree) BinaryUnmarshaler(b []byte) error {
_, m, err := network.Unmarshal(b)
tbm, ok := m.(*tbmStruct)
if !ok {
return xerrors.New("Didn't find TBMstruct")
}
tree, err := NewTreeFromMarshal(tbm.T, tbm.Ro)
if err != nil {
return xerrors.Errorf("making tree marshal: %v", err)
}
t.Roster = tbm.Ro
t.ID = tree.ID
t.Root = tree.Root
return nil
}
// Equal verifies if the given tree is equal
func (t *Tree) Equal(t2 *Tree) bool {
if !t.ID.Equal(t2.ID) || !t.Roster.Equal(t2.Roster) {
log.Lvl4("Ids of trees don't match")
return false
}
return t.Root.Equal(t2.Root)
}
// String writes the definition of the tree
func (t *Tree) String() string {
return fmt.Sprintf("TreeId:%s - RosterId:%s - RootId:%s",
t.ID, t.Roster.GetID(), t.Root.ID)
}
// Dump returns string about the tree
func (t *Tree) Dump() string {
ret := "Tree " + t.ID.String() + " is:"
t.Root.Visit(0, func(d int, tn *TreeNode) {
if tn.Parent != nil {
ret += fmt.Sprintf("\n%d - %s/%s has parent %s/%s", d,
tn.ServerIdentity.PublicKey, tn.ServerIdentity.Address,
tn.Parent.ServerIdentity.PublicKey, tn.Parent.ServerIdentity.Address)
} else {
ret += fmt.Sprintf("\n%s/%s is root", tn.ServerIdentity.PublicKey, tn.ServerIdentity.Address)
}
})
return ret
}
// Search searches the Tree for the given TreeNodeID and returns the corresponding TreeNode
func (t *Tree) Search(tn TreeNodeID) (ret *TreeNode) {
found := func(d int, tns *TreeNode) {
if tns.ID.Equal(tn) {
ret = tns
}
}
t.Root.Visit(0, found)
return ret
}
// List returns a list of TreeNodes generated by DFS-iterating the Tree
func (t *Tree) List() (ret []*TreeNode) {
ret = make([]*TreeNode, 0)
add := func(d int, tns *TreeNode) {
ret = append(ret, tns)
}
t.Root.Visit(0, add)
return ret
}
// IsBinary returns true if every node has two or no children
func (t *Tree) IsBinary(root *TreeNode) bool {
return t.IsNary(root, 2)
}
// IsNary returns true if every node has two or no children
func (t *Tree) IsNary(root *TreeNode, N int) bool {
nChild := len(root.Children)
if nChild != N && nChild != 0 {
log.Lvl3("Only", nChild, "children for", root.ID)
return false
}
for _, c := range root.Children {
if !t.IsNary(c, N) {
return false
}
}
return true
}
// Size returns the number of all TreeNodes
func (t *Tree) Size() int {
size := 0
t.Root.Visit(0, func(d int, tn *TreeNode) {
size++
})
return size
}
// UsesList returns true if all ServerIdentities of the list are used at least once
// in the tree
func (t *Tree) UsesList() bool {
nodes := t.List()
for _, p := range t.Roster.List {
found := false
for _, n := range nodes {
if n.ServerIdentity.ID.Equal(p.ID) {
found = true
break
}
}
if !found {
return false
}
}
return true
}
// TreeMarshal is used to send and receive a tree-structure without having
// to copy the whole nodelist
type TreeMarshal struct {
// This is the UUID of the corresponding TreeNode
TreeNodeID TreeNodeID
// TreeId identifies the Tree for the top-node
TreeID TreeID
// This is the UUID of the ServerIdentity, except
ServerIdentityID network.ServerIdentityID
// for the top-node this contains the Roster's ID
RosterID RosterID
// All children from this tree. The top-node only has one child, which is
// the root
Children []*TreeMarshal
}
func (tm *TreeMarshal) String() string {
s := fmt.Sprintf("%v", tm.ServerIdentityID)
s += "\n"
for i := range tm.Children {
s += tm.Children[i].String()
}
return s
}
// TreeMarshalTypeID of TreeMarshal message as registered in network
var TreeMarshalTypeID = network.RegisterMessage(TreeMarshal{})
// TreeMarshalCopyTree takes a TreeNode and returns a corresponding
// TreeMarshal
func TreeMarshalCopyTree(tr *TreeNode) *TreeMarshal {
tm := &TreeMarshal{
TreeNodeID: tr.ID,
ServerIdentityID: tr.ServerIdentity.ID,
}
for i := range tr.Children {
tm.Children = append(tm.Children,
TreeMarshalCopyTree(tr.Children[i]))
}
return tm
}
// MakeTree creates a tree given an Roster
func (tm TreeMarshal) MakeTree(ro *Roster) (*Tree, error) {
if !ro.GetID().Equal(tm.RosterID) {
return nil, xerrors.New("Not correct Roster-Id")
}
tree := &Tree{
ID: tm.TreeID,
Roster: ro,
}
var err error
tree.Root, err = tm.Children[0].MakeTreeFromList(nil, ro)
if err != nil {
return nil, xerrors.Errorf("making tree: %v", err)
}
return tree, nil
}
// MakeTreeFromList creates a sub-tree given an Roster
func (tm *TreeMarshal) MakeTreeFromList(parent *TreeNode, ro *Roster) (*TreeNode, error) {
idx, ent := ro.Search(tm.ServerIdentityID)
if idx < 0 {
return nil, xerrors.New("didn't find node in roster")
}
tn := &TreeNode{
Parent: parent,
ID: tm.TreeNodeID,
ServerIdentity: ent,
RosterIndex: idx,
}
for _, c := range tm.Children {
ntn, err := c.MakeTreeFromList(tn, ro)
if err != nil {
return nil, xerrors.Errorf("making tree: %v", err)
}
tn.Children = append(tn.Children, ntn)
}
return tn, nil
}
// A Roster is a list of ServerIdentity we choose to run some tree on it ( and
// therefor some protocols). Access is not safe from multiple goroutines.
type Roster struct {
// List is the list of actual entities.
List []*network.ServerIdentity
}
// RosterID uniquely identifies an Roster
type RosterID uuid.UUID
// String returns the default representation of the ID (wrapper around
// uuid.UUID.String()
func (roID RosterID) String() string {
return uuid.UUID(roID).String()
}
// Equal returns true if and only if roID2 equals this RosterID.
func (roID RosterID) Equal(roID2 RosterID) bool {
return uuid.Equal(uuid.UUID(roID), uuid.UUID(roID2))
}
// IsNil returns true iff the RosterID is Nil
func (roID RosterID) IsNil() bool {
return roID.Equal(RosterID(uuid.Nil))
}
// WriteTo takes a writer and writes the bytes of the roster ID.
func (roID RosterID) WriteTo(w io.Writer) (int64, error) {
n, err := w.Write(uuid.UUID(roID).Bytes())
return int64(n), err
}
// RosterTypeID of Roster message as registered in network
var RosterTypeID = network.RegisterMessage(Roster{})
// NewRoster creates a new roster from a list of entities. It also
// adds a UUID which is randomly chosen.
func NewRoster(ids []*network.ServerIdentity) *Roster {
// Don't allow a crash if things are not as expected.
if len(ids) < 1 || ids[0].PublicKey == nil {
return nil
}
// Take a copy of ids, in case the caller tries to change it later.
list := make([]*network.ServerIdentity, len(ids))
copy(list, ids)
return &Roster{List: list}
}
// Len returns the length of the roster.
func (ro *Roster) Len() int {
return len(ro.List)
}
// GetID generates the ID for the list of server identities of the roster
func (ro *Roster) GetID() RosterID {
h := sha256.New()
for _, id := range ro.List {
_, err := id.PublicKey.WriteTo(h)
if err != nil {
// An error at that point would mean something worst is
// happening, so we keep the function simple and panic.
panic(xerrors.Errorf("hashing: %v", err))
}
// Order is important for the hash.
sort.Sort(network.ServiceIdentities(id.ServiceIdentities))
for _, srvid := range id.ServiceIdentities {
_, err = srvid.PublicKey.WriteTo(h)
if err != nil {
// Same as the previous panic.
panic(xerrors.Errorf("hashing: %v", err))
}
}
}
return RosterID(uuid.NewV5(uuid.NamespaceURL, hex.EncodeToString(h.Sum(nil))))
}
// Search searches the Roster for the given ServerIdentityID and returns the
// corresponding ServerIdentity.
func (ro *Roster) Search(eID network.ServerIdentityID) (int, *network.ServerIdentity) {
for i, e := range ro.List {
if e.ID.Equal(eID) {
return i, e
}
}
return -1, nil
}
// Get simply returns the entity that is stored at that index in the entitylist
// returns nil if index error
func (ro *Roster) Get(idx int) *network.ServerIdentity {
if idx < 0 || idx > len(ro.List) {
return nil
}
return ro.List[idx]
}
// PublicKeys uses the given mapper to convert the server identities into public
// keys. If any error happens, it will interrupt and return it.
// Use NewRegistryMapper to get the list of public keys of a service using a
// cipher suite registry.
// Use NewCipherSuiteMapper to get the list of public keys of a service using
// a single cipher suite.
func (ro *Roster) PublicKeys(name string) []ciphersuite.PublicKey {
res := make([]ciphersuite.PublicKey, len(ro.List))
for i, si := range ro.List {
res[i] = si.ServicePublic(name)
}
return res
}
// GenerateBigNaryTree creates a tree where each node has N children.
// It will make a tree with exactly 'nodes' elements, regardless of the
// size of the Roster. If 'nodes' is bigger than the number of elements
// in the Roster, it will add some or all elements in the Roster
// more than once.
// If the length of the Roster is equal to 'nodes', it is guaranteed that
// all ServerIdentities from the Roster will be used in the tree.
// However, for some configurations it is impossible to use all ServerIdentities from
// the Roster and still avoid having a parent and a child from the same
// host. In this case use-all has preference over not-the-same-host.
func (ro *Roster) GenerateBigNaryTree(N, nodes int) *Tree {
if len(ro.List) == 0 {
panic("empty roster")
}
// list of which hosts are already used
used := make([]bool, len(ro.List))
ilLen := len(ro.List)
// only use all ServerIdentities if we have the same number of nodes and hosts
useAll := ilLen == nodes
root := NewTreeNode(0, ro.List[0])
used[0] = true
levelNodes := []*TreeNode{root}
totalNodes := 1
roIndex := 1 % ilLen
for totalNodes < nodes {
newLevelNodes := make([]*TreeNode, len(levelNodes)*N)
newLevelNodesCounter := 0
for i, parent := range levelNodes {
children := (nodes - totalNodes) * (i + 1) / len(levelNodes)
if children > N {
children = N
}
parent.Children = make([]*TreeNode, children)
parentHost := parent.ServerIdentity.Address.Host()
for n := 0; n < children; n++ {
// Check on host-address, so that no child is
// on the same host as the parent.
childHost := ro.List[roIndex].Address.Host()
roIndexFirst := roIndex
notSameHost := true
for (notSameHost && childHost == parentHost && ilLen > 1) ||
(useAll && used[roIndex]) {
roIndex = (roIndex + 1) % ilLen
if useAll && used[roIndex] {
// In case we searched all ServerIdentities,
// give up on finding another host, but
// keep using all ServerIdentities
if roIndex == roIndexFirst {
notSameHost = false
}
continue
}
// If we tried all hosts, it means we're using
// just one hostname, as we didn't find any
// other name
if roIndex == roIndexFirst {
break
}
childHost = ro.List[roIndex].Address.Host()
}
child := NewTreeNode(roIndex, ro.List[roIndex])
used[roIndex] = true
roIndex = (roIndex + 1) % ilLen
totalNodes++
parent.Children[n] = child
child.Parent = parent
newLevelNodes[newLevelNodesCounter] = child
newLevelNodesCounter++
}
}
levelNodes = newLevelNodes[:newLevelNodesCounter]
}
return NewTree(ro, root)
}
// NewRosterWithRoot returns a copy of the roster but with the given ServerIdentity
// at the first entry in the roster.
func (ro *Roster) NewRosterWithRoot(root *network.ServerIdentity) *Roster {
list := make([]*network.ServerIdentity, len(ro.List))
copy(list, ro.List)
rootIndex, _ := ro.Search(root.ID)
if rootIndex < 0 {
return nil
}
list[0], list[rootIndex] = list[rootIndex], list[0]
return NewRoster(list)
}
// GenerateNaryTreeWithRoot creates a tree where each node has N children.
// The root is given as an ServerIdentity. If root doesn't exist in the
// roster, `nil` will be returned.
// The generation of the tree is done in a simple for-loop, so that the
// original roster can be used for tree creation, even if the root is not
// at position 0.
// If root == nil, the first element of the roster will be taken as root.
//
// If you need the root node to be at the first position of the roster, then
// you need to create a new roster using roster.NewRosterWithRoot. Else this method
// does not change the underlying roster or create a new one.
func (ro *Roster) GenerateNaryTreeWithRoot(N int, root *network.ServerIdentity) *Tree {
// Fetch the root node, set to the first element of the roster if
// root == nil.
rootIndex := 0
if root != nil {
rootIndex, _ = ro.Search(root.ID)
if rootIndex < 0 {
log.Lvl2("Asked for non-existing root:", root, ro.List)
return nil
}
} else {
root = ro.List[0]
}
rootNode := NewTreeNode(rootIndex, root)
parents := []*TreeNode{rootNode}
children := []*TreeNode{}
for i := 1; i < len(ro.List); i++ {
index := (i + rootIndex) % len(ro.List)
// If a parent is full, remove it from the list.
if parents[0].SubtreeCount() == N {
parents = parents[1:]
}
// If there are no parents, pass all children to the parents, and
// continue
if len(parents) == 0 {
parents = children
children = []*TreeNode{}
}
// Create the new child and add it to the parent node.
newChild := NewTreeNode(index, ro.List[index])
children = append(children, newChild)
parents[0].AddChild(newChild)
}
return NewTree(ro, rootNode)
}
// GenerateNaryTree creates a tree where each node has N children.
// The first element of the Roster will be the root element.
func (ro *Roster) GenerateNaryTree(N int) *Tree {
return ro.GenerateNaryTreeWithRoot(N, nil)
}
// GenerateBinaryTree creates a binary tree out of the Roster
// out of it. The first element of the Roster will be the root element.
func (ro *Roster) GenerateBinaryTree() *Tree {
return ro.GenerateNaryTree(2)
}
// GenerateStar creates a star topology with the first element
// of Roster as root, and all other elements as children of the root.
func (ro *Roster) GenerateStar() *Tree {
return ro.GenerateNaryTree(len(ro.List) - 1)
}
// RandomServerIdentity returns a random element of the Roster.
func (ro *Roster) RandomServerIdentity() *network.ServerIdentity {
if ro.List == nil || len(ro.List) == 0 {
return nil
}
return ro.List[rand.Int()%len(ro.List)]
}
// RandomSubset returns a new Roster which starts with root and is
// followed by a random subset of n elements of ro, not including root.
func (ro *Roster) RandomSubset(root *network.ServerIdentity, n int) *Roster {
if n > len(ro.List) {
n = len(ro.List)
}
out := make([]*network.ServerIdentity, 1, n+1)
out[0] = root
perm := securePermute(len(ro.List))
for _, p := range perm {
if !ro.List[p].ID.Equal(root.ID) {
out = append(out, ro.List[p])
if len(out) == n+1 {
break
}
}
}
return NewRoster(out)
}
// securePermute is like rand.Perm, but seeded via cryptographically
// secure random data.
func securePermute(n int) []int {
var buf [8]byte
nb, err := cryptorand.Read(buf[:])
if nb != 8 {
panic("securePermute cannot get random")
}
if err != nil {
panic("securePermute cannot get random: " + err.Error())
}
buf2 := bytes.NewReader(buf[:])
var seed int64
err = binary.Read(buf2, binary.LittleEndian, &seed)
if err != nil {
panic("securePermute failed to seed: " + err.Error())
}
src := rand.NewSource(seed)
r := rand.New(src)
return r.Perm(n)
}
// IsRotation returns true if the target is a rotated (the same roster but with
// shifted server identities) version of the receiver.
func (ro Roster) IsRotation(target *Roster) bool {
if target == nil {
return false
}
n := len(ro.List)
if n < 2 {
return false
}
if n != len(target.List) {
return false
}
// find the first element of ro in target
var offset int
for _, sid := range target.List {
if sid.Equal(ro.List[0]) {
break
}
offset++
}
if offset == 0 || offset >= n {
return false
}
// check that the identities are the same, starting at the offset
for i, sid := range ro.List {
if !sid.Equal(target.List[(i+offset)%n]) {
return false
}
}
return true
}
// Equal checks if two roster are the same by checking the generated ID
func (ro *Roster) Equal(other *Roster) bool {
return ro.GetID().Equal(other.GetID())
}
// Concat makes a new roster using an existing one and a list
// of server identities while preserving the order of the
// roster by appending at the end
func (ro *Roster) Concat(sis ...*network.ServerIdentity) *Roster {
tmpRoster := NewRoster(ro.List)
for _, si := range sis {
if i, _ := tmpRoster.Search(si.ID); i < 0 {
tmpRoster.List = append(tmpRoster.List, si)
}
}
return NewRoster(tmpRoster.List)
}
// addNary is a recursive function to create the binary tree.
func (ro *Roster) addNary(parent *TreeNode, N, start, end int) *TreeNode {
if !(start <= end && end < len(ro.List)) {
return nil
}
node := NewTreeNode(start, ro.List[start])
if parent != nil {
node.Parent = parent
parent.Children = append(parent.Children, node)
}
diff := end - start
for n := 0; n < N; n++ {
s := diff * n / N
e := diff * (n + 1) / N
ro.addNary(node, N, start+s+1, start+e)
}
return node
}
// TreeNode is one node in the tree
type TreeNode struct {
// The Id represents that node of the tree
ID TreeNodeID
// The ServerIdentity points to the corresponding host. One given host
// can be used more than once in a tree.
ServerIdentity *network.ServerIdentity
// RosterIndex is the index in the Roster where the `ServerIdentity` is located
RosterIndex int
// Parent link
Parent *TreeNode
// Children links
Children []*TreeNode
}
// TreeNodeID identifies a given TreeNode struct in the onet framework.
type TreeNodeID uuid.UUID
// String returns a canonical representation of the TreeNodeID.
func (tId TreeNodeID) String() string {
return uuid.UUID(tId).String()
}
// Equal returns true if and only if the given TreeNodeID equals tId.
func (tId TreeNodeID) Equal(tID2 TreeNodeID) bool {
return uuid.Equal(uuid.UUID(tId), uuid.UUID(tID2))
}
// IsNil returns true iff the TreeNodID is Nil
func (tId TreeNodeID) IsNil() bool {
return tId.Equal(TreeNodeID(uuid.Nil))
}
// Name returns a human readable representation of the TreeNode (IP address).
func (t *TreeNode) Name() string {
return t.ServerIdentity.Address.String()
}
var _ = network.RegisterMessage(TreeNode{})
// NewTreeNode creates a new TreeNode with the proper Id
func NewTreeNode(entityIdx int, ni *network.ServerIdentity) *TreeNode {
tn := &TreeNode{
ServerIdentity: ni,
RosterIndex: entityIdx,
Parent: nil,
Children: make([]*TreeNode, 0),
ID: TreeNodeID(uuid.NewV5(uuid.NamespaceURL, ni.PublicKey.String())),
}
return tn
}
// IsConnectedTo checks if the TreeNode can communicate with its parent or
// children.
func (t *TreeNode) IsConnectedTo(si *network.ServerIdentity) bool {
if t.Parent != nil && t.Parent.ServerIdentity.Equal(si) {
return true
}
for i := range t.Children {
if t.Children[i].ServerIdentity.Equal(si) {
return true
}
}
return false
}
// IsLeaf returns true for a node without children
func (t *TreeNode) IsLeaf() bool {
return len(t.Children) == 0
}
// IsRoot returns true for a node without a parent
func (t *TreeNode) IsRoot() bool {
return t.Parent == nil
}
// IsInTree - verifies if the TreeNode is in the given Tree
func (t *TreeNode) IsInTree(tree *Tree) bool {
root := *t
for root.Parent != nil {
root = *root.Parent
}
return tree.Root.ID.Equal(root.ID)
}
// AddChild adds a child to this tree-node.
func (t *TreeNode) AddChild(c *TreeNode) {
t.Children = append(t.Children, c)
c.Parent = t
}
// Equal tests if that node is equal to the given node
func (t *TreeNode) Equal(t2 *TreeNode) bool {
if !t.ID.Equal(t2.ID) || !t.ServerIdentity.ID.Equal(t2.ServerIdentity.ID) {
log.Lvl4("TreeNode: ids are not equal")
return false
}
if len(t.Children) != len(t2.Children) {
log.Lvl4("TreeNode: number of children are not equal")
return false
}
for i, c := range t.Children {
if !c.Equal(t2.Children[i]) {
log.Lvl4("TreeNode: children are not equal")
return false
}
}
return true
}
// String returns the current treenode's Id as a string.
func (t *TreeNode) String() string {
return string(t.ID.String())
}
// Visit is a recursive function that allows for depth-first calling on all
// nodes
func (t *TreeNode) Visit(firstDepth int, fn func(depth int, n *TreeNode)) {
fn(firstDepth, t)
for _, c := range t.Children {
c.Visit(firstDepth+1, fn)
}
}
// SubtreeCount returns how many children are attached to that
// TreeNode.
func (t *TreeNode) SubtreeCount() int {
ret := -1
t.Visit(0, func(int, *TreeNode) { ret++ })
return ret
}
// RosterToml is the struct can can embedded ServerIdentityToml to be written in a
// toml file
type RosterToml struct {
ID RosterID
List []*network.ServerIdentityToml
}
// Toml returns the toml-writable version of this roster.
func (ro *Roster) Toml() *RosterToml {
ids := make([]*network.ServerIdentityToml, len(ro.List))
for i := range ro.List {
ids[i] = ro.List[i].Toml()
}
return &RosterToml{
List: ids,
}
}
// Roster returns the Id list from this toml read struct
func (rot *RosterToml) Roster() *Roster {
ids := make([]*network.ServerIdentity, len(rot.List))
for i := range rot.List {
ids[i] = rot.List[i].ServerIdentity()
}
return &Roster{
List: ids,
}
}