/
nodeID.go
144 lines (129 loc) · 2.75 KB
/
nodeID.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
package nodeID
import (
"bytes"
"encoding/hex"
"errors"
xr "github.com/jddixon/rnglib_go"
xu "github.com/jddixon/xlUtil_go"
)
// CONSTRUCTORS /////////////////////////////////////////////////////
type NodeID struct {
_nodeID []byte
}
var (
BadNodeIDLen = errors.New("bad length for nodeID")
NilNodeID = errors.New("nil byte array for nodeID")
)
func New(id []byte) (q *NodeID, err error) {
q = new(NodeID)
if id == nil {
id = make([]byte, xu.SHA1_BIN_LEN)
rng := xr.MakeSystemRNG()
rng.NextBytes(id)
q._nodeID = id
} else {
// deep copy the slice
size := len(id)
myID := make([]byte, size)
for i := 0; i < size; i++ {
myID[i] = id[i]
}
q._nodeID = myID
}
if !IsValidID(id) {
err = BadNodeIDLen
}
return
}
// XXX CONSIDER ME DEPRECATED
func NewNodeID(id []byte) (q *NodeID, err error) {
return New(id)
}
// func NewNodeIDFromString(id string) *NodeID {
// ...
// }
func (n *NodeID) Clone() (*NodeID, error) {
v := n.Value()
return NewNodeID(v)
}
// OTHER METHODS ////////////////////////////////////////////////////
func (n *NodeID) Compare(any interface{}) (int, error) {
result := 0
err := error(nil)
if any == nil {
err = errors.New("IllegalArgument: nil comparand")
} else if any == n {
return result, err // defaults to 0, nil
} else {
switch v := any.(type) {
case *NodeID:
_ = v
default:
err = errors.New("IllegalArgument: not pointer to NodeID")
}
}
if err != nil {
return result, err
}
other := any.(*NodeID)
if n.Length() != other.Length() {
return 0, errors.New("IllegalArgument: NodeIDs of different length")
}
return bytes.Compare(n.Value(), other.Value()), nil
}
func SameNodeID(a, b *NodeID) bool {
if a == nil || b == nil {
return false
}
aVal, bVal := a.Value(), b.Value()
return bytes.Equal(aVal, bVal)
}
func (n *NodeID) Equal(any interface{}) bool {
if any == n {
return true
}
if any == nil {
return false
}
switch v := any.(type) {
case *NodeID:
_ = v
default:
return false
}
other := any.(*NodeID) // type assertion
if n.Length() != other.Length() {
return false
}
//for i := 0; i < n.Length(); i++ {
// if (*n)._nodeID[i] != (*other)._nodeID[i] {
// return false
// }
//}
// return true
return SameNodeID(n, other)
}
func IsValidID(value []byte) bool {
if value == nil {
return false
}
// XXX check type?
x := len(value)
return x == 20 || x == 32 // SHA1 or SHA3
}
func (n *NodeID) Length() int {
return len(n._nodeID)
}
// Returns a deep copy of the slice.
func (n *NodeID) Value() []byte {
size := len(n._nodeID)
v := make([]byte, size)
for i := 0; i < size; i++ {
v[i] = n._nodeID[i]
}
return v
}
// SERIALIZATION ////////////////////////////////////////////////////
func (n *NodeID) String() string {
return hex.EncodeToString(n._nodeID)
}