-
Notifications
You must be signed in to change notification settings - Fork 107
/
node.go
78 lines (67 loc) 路 2.01 KB
/
node.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
package key
import (
"encoding/binary"
"github.com/drand/drand/v2/crypto"
proto "github.com/drand/drand/v2/protobuf/drand"
"github.com/drand/kyber/share/dkg"
)
// Index is the index of the node
type Index = dkg.Index
// Node is a wrapper around identity that additionally includes the index that
// the node has within this group. The index is computed initially when the
// group is first created. The index is useful only for drand nodes, and
// shouldn't be taken into account from an external point of view.
// The index is useful to be able to reshare correctly, and gives the ability to
// have a better logging: packets sent during DKG only contain an index, from
// which we can derive the actual address from the index.
type Node struct {
*Identity
Index Index
}
// Hash is a compact representation of the node
func (n *Node) Hash() []byte {
h := hashFunc()
_ = binary.Write(h, binary.LittleEndian, n.Index)
_, _ = n.Identity.Key.MarshalTo(h)
return h.Sum(nil)
}
// Equal indicates if two nodes are equal
func (n *Node) Equal(n2 *Node) bool {
return n.Index == n2.Index && n.Identity.Equal(n2.Identity)
}
// TOML is a toml representation of the node
func (n *Node) TOML() interface{} {
return &NodeTOML{
PublicTOML: n.Identity.TOML().(*PublicTOML),
Index: n.Index,
}
}
// FromTOML unmarshals a node from TOML representation
func (n *Node) FromTOML(t interface{}) error {
ntoml := t.(*NodeTOML)
n.Index = ntoml.Index
if n.Identity == nil {
n.Identity = new(Identity)
}
return n.Identity.FromTOML(ntoml.PublicTOML)
}
// TOMLValue is used in marshaling
func (n *Node) TOMLValue() interface{} {
return new(NodeTOML)
}
// NodeTOML is the node's toml representation
type NodeTOML struct {
*PublicTOML
Index Index
}
// NodeFromProto creates a node from its wire representation
func NodeFromProto(n *proto.Node, targetScheme *crypto.Scheme) (*Node, error) {
id, err := IdentityFromProto(n.Public, targetScheme)
if err != nil {
return nil, err
}
return &Node{
Index: n.Index,
Identity: id,
}, nil
}