/
index.go
106 lines (83 loc) · 2.26 KB
/
index.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
package idx
import (
"github.com/TechPay-io/sirius-base/common/bigendian"
)
type (
// Epoch numeration.
Epoch uint32
// Event numeration.
Event uint32
// Block numeration.
Block uint64
// Lamport numeration.
Lamport uint32
// Frame numeration.
Frame uint32
// Pack numeration.
Pack uint32
// ValidatorID numeration.
ValidatorID uint32
)
// Bytes gets the byte representation of the index.
func (e Epoch) Bytes() []byte {
return bigendian.Uint32ToBytes(uint32(e))
}
// Bytes gets the byte representation of the index.
func (e Event) Bytes() []byte {
return bigendian.Uint32ToBytes(uint32(e))
}
// Bytes gets the byte representation of the index.
func (b Block) Bytes() []byte {
return bigendian.Uint64ToBytes(uint64(b))
}
// Bytes gets the byte representation of the index.
func (l Lamport) Bytes() []byte {
return bigendian.Uint32ToBytes(uint32(l))
}
// Bytes gets the byte representation of the index.
func (p Pack) Bytes() []byte {
return bigendian.Uint32ToBytes(uint32(p))
}
// Bytes gets the byte representation of the index.
func (s ValidatorID) Bytes() []byte {
return bigendian.Uint32ToBytes(uint32(s))
}
// Bytes gets the byte representation of the index.
func (f Frame) Bytes() []byte {
return bigendian.Uint32ToBytes(uint32(f))
}
// BytesToEpoch converts bytes to epoch index.
func BytesToEpoch(b []byte) Epoch {
return Epoch(bigendian.BytesToUint32(b))
}
// BytesToEvent converts bytes to event index.
func BytesToEvent(b []byte) Event {
return Event(bigendian.BytesToUint32(b))
}
// BytesToBlock converts bytes to block index.
func BytesToBlock(b []byte) Block {
return Block(bigendian.BytesToUint64(b))
}
// BytesToLamport converts bytes to block index.
func BytesToLamport(b []byte) Lamport {
return Lamport(bigendian.BytesToUint32(b))
}
// BytesToFrame converts bytes to block index.
func BytesToFrame(b []byte) Frame {
return Frame(bigendian.BytesToUint32(b))
}
// BytesToPack converts bytes to block index.
func BytesToPack(b []byte) Pack {
return Pack(bigendian.BytesToUint32(b))
}
// BytesToValidatorID converts bytes to validator index.
func BytesToValidatorID(b []byte) ValidatorID {
return ValidatorID(bigendian.BytesToUint32(b))
}
// MaxLamport return max value
func MaxLamport(x, y Lamport) Lamport {
if x > y {
return x
}
return y
}