-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnodeid.go
51 lines (41 loc) · 1.03 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
package ghord
import (
"bytes"
"encoding/hex"
"math/big"
)
// Represents a Hash algorithm
type Hasher interface {
Hash(data []byte) []byte
Size() int
}
// Represents a NodeID in the form of a hash
// Unless you know what you are doing, do not create a
// NodeID yourself, always use NodeIDFromBytes().
type NodeID []byte
// Create a hashed NodeID from a given byte array
func (c *Cluster) NodeIDFromBytes(id []byte) NodeID {
return NodeID(c.hasher.Hash(id))
}
func (n NodeID) String() string {
return hex.EncodeToString(n)
}
// Add a integer to the NodeID.
func (n NodeID) Add(i *big.Int) NodeID {
newVal := big.NewInt(0)
y := big.NewInt(0)
y.SetBytes(n)
return NodeID(newVal.Add(y, i).Bytes())
}
// Returns true iff NodeID n < id
func (n NodeID) Less(id NodeID) bool {
return bytes.Compare(n, id) == -1
}
// Returns true iff NodeID n > id
func (n NodeID) Greater(id NodeID) bool {
return bytes.Compare(n, id) == 1
}
// Returns true iff NodeID n == id
func (n NodeID) Equal(id NodeID) bool {
return bytes.Compare(n, id) == 0
}