/
event_hash.go
331 lines (273 loc) · 6.55 KB
/
event_hash.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
package hash
import (
"bytes"
"crypto/sha256"
"fmt"
"math/big"
"math/rand"
"sort"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/Ecosystem-Knowledge/lachesis-base/common/bigendian"
"github.com/Ecosystem-Knowledge/lachesis-base/inter/idx"
)
type (
// Event is a unique identifier of event.
// It is a hash of Event.
Event Hash
// OrderedEvents is a sortable slice of event hashes.
OrderedEvents []Event
// Events is a slice of event hashes.
Events []Event
EventsStack []Event
// EventsSet provides additional methods of event hash index.
EventsSet map[Event]struct{}
)
var (
// ZeroEvent is a hash of virtual initial event.
ZeroEvent = Event{}
)
/*
* Event methods:
*/
// Bytes returns value as byte slice.
func (h Event) Bytes() []byte {
return (Hash)(h).Bytes()
}
// Big converts a hash to a big integer.
func (h *Event) Big() *big.Int {
return (*Hash)(h).Big()
}
// setBytes converts bytes to event hash.
// If b is larger than len(h), b will be cropped from the left.
func (h *Event) SetBytes(raw []byte) {
(*Hash)(h).SetBytes(raw)
}
// BytesToEvent converts bytes to event hash.
// If b is larger than len(h), b will be cropped from the left.
func BytesToEvent(b []byte) Event {
return Event(FromBytes(b))
}
// FromBytes converts bytes to hash.
// If b is larger than len(h), b will be cropped from the left.
func FromBytes(b []byte) Hash {
var h Hash
h.SetBytes(b)
return h
}
// HexToEventHash sets byte representation of s to hash.
// If b is larger than len(h), b will be cropped from the left.
func HexToEventHash(s string) Event {
return Event(HexToHash(s))
}
// Hex converts an event hash to a hex string.
func (h Event) Hex() string {
return Hash(h).Hex()
}
// Lamport returns [4:8] bytes, which store event's Lamport.
func (h Event) Lamport() idx.Lamport {
return idx.BytesToLamport(h[4:8])
}
// Epoch returns [0:4] bytes, which store event's Epoch.
func (h Event) Epoch() idx.Epoch {
return idx.BytesToEpoch(h[0:4])
}
// String returns human readable string representation.
func (h Event) String() string {
return h.ShortID(3)
}
// FullID returns human readable string representation with no information loss.
func (h Event) FullID() string {
return h.ShortID(32 - 4 - 4)
}
// ShortID returns human readable ID representation, suitable for API calls.
func (h Event) ShortID(precision int) string {
if name := GetEventName(h); len(name) > 0 {
return name
}
// last bytes, because first are occupied by epoch and lamport
return fmt.Sprintf("%d:%d:%s", h.Epoch(), h.Lamport(), common.Bytes2Hex(h[8:8+precision]))
}
// IsZero returns true if hash is empty.
func (h *Event) IsZero() bool {
return *h == Event{}
}
/*
* EventsSet methods:
*/
// NewEventsSet makes event hash index.
func NewEventsSet(h ...Event) EventsSet {
hh := EventsSet{}
hh.Add(h...)
return hh
}
// Copy copies events to a new structure.
func (hh EventsSet) Copy() EventsSet {
ee := make(EventsSet, len(hh))
for k, v := range hh {
ee[k] = v
}
return ee
}
// String returns human readable string representation.
func (hh EventsSet) String() string {
ss := make([]string, 0, len(hh))
for h := range hh {
ss = append(ss, h.String())
}
return "[" + strings.Join(ss, ", ") + "]"
}
// Slice returns whole index as slice.
func (hh EventsSet) Slice() Events {
arr := make(Events, len(hh))
i := 0
for h := range hh {
arr[i] = h
i++
}
return arr
}
// Add appends hash to the index.
func (hh EventsSet) Add(hash ...Event) {
for _, h := range hash {
hh[h] = struct{}{}
}
return
}
// Erase erase hash from the index.
func (hh EventsSet) Erase(hash ...Event) {
for _, h := range hash {
delete(hh, h)
}
return
}
// Contains returns true if hash is in.
func (hh EventsSet) Contains(hash Event) bool {
_, ok := hh[hash]
return ok
}
/*
* Events methods:
*/
// NewEvents makes event hash slice.
func NewEvents(h ...Event) Events {
hh := Events{}
hh.Add(h...)
return hh
}
// Copy copies events to a new structure.
func (hh Events) Copy() Events {
ee := make(Events, len(hh))
for k, v := range hh {
ee[k] = v
}
return ee
}
// String returns human readable string representation.
func (hh Events) String() string {
ss := make([]string, 0, len(hh))
for _, h := range hh {
ss = append(ss, h.String())
}
return "[" + strings.Join(ss, ", ") + "]"
}
// Set returns whole index as a EventsSet.
func (hh Events) Set() EventsSet {
set := make(EventsSet, len(hh))
for _, h := range hh {
set[h] = struct{}{}
}
return set
}
// Add appends hash to the slice.
func (hh *Events) Add(hash ...Event) {
*hh = append(*hh, hash...)
}
/*
* EventsStack methods:
*/
// Push event ID on top
func (s *EventsStack) Push(v Event) {
*s = append(*s, v)
}
// PushAll event IDs on top
func (s *EventsStack) PushAll(vv Events) {
*s = append(*s, vv...)
}
// Pop event ID from top. Erases element.
func (s *EventsStack) Pop() *Event {
l := len(*s)
if l == 0 {
return nil
}
res := &(*s)[l-1]
*s = (*s)[:l-1]
return res
}
/*
* OrderedEvents methods:
*/
// String returns string representation.
func (hh OrderedEvents) String() string {
buf := &strings.Builder{}
out := func(s string) {
if _, err := buf.WriteString(s); err != nil {
panic(err)
}
}
out("[")
for _, h := range hh {
out(h.String() + ", ")
}
out("]")
return buf.String()
}
func (hh OrderedEvents) Len() int { return len(hh) }
func (hh OrderedEvents) Swap(i, j int) { hh[i], hh[j] = hh[j], hh[i] }
func (hh OrderedEvents) Less(i, j int) bool {
return bytes.Compare(hh[i].Bytes(), hh[j].Bytes()) < 0
}
// ByEpochAndLamport sorts events by epoch first, by lamport second, by ID third
func (hh OrderedEvents) ByEpochAndLamport() {
sort.Sort(hh)
}
// Of returns hash of data
func Of(data ...[]byte) (hash Hash) {
d := sha256.New()
for _, b := range data {
_, err := d.Write(b)
if err != nil {
panic(err)
}
}
d.Sum(hash[:0])
return hash
}
/*
* Utils:
*/
// FakePeer generates random fake peer id for testing purpose.
func FakePeer(seed ...int64) idx.ValidatorID {
return idx.BytesToValidatorID(FakeHash(seed...).Bytes()[:4])
}
// FakeEpoch gives fixed value of fake epoch for testing purpose.
func FakeEpoch() idx.Epoch {
return 123456
}
// FakeEvent generates random fake event hash with the same epoch for testing purpose.
func FakeEvent() (h Event) {
_, err := rand.Read(h[:])
if err != nil {
panic(err)
}
copy(h[0:4], bigendian.Uint32ToBytes(uint32(FakeEpoch())))
return
}
// FakeEvents generates random hashes of fake event with the same epoch for testing purpose.
func FakeEvents(n int) Events {
res := Events{}
for i := 0; i < n; i++ {
res.Add(FakeEvent())
}
return res
}