/
types.go
410 lines (327 loc) · 11.1 KB
/
types.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package epoch
import (
"context"
"fmt"
"github.com/cockroachdb/errors"
"github.com/iotaledger/hive.go/core/generics/set"
"github.com/iotaledger/hive.go/core/generics/shrinkingmap"
"github.com/iotaledger/hive.go/core/identity"
"strings"
"time"
"github.com/iotaledger/hive.go/core/byteutils"
"github.com/iotaledger/hive.go/core/generics/model"
"github.com/iotaledger/hive.go/core/serix"
"github.com/mr-tron/base58"
"golang.org/x/crypto/blake2b"
)
var (
// GenesisTime is the time (Unix in seconds) of the genesis.
GenesisTime int64 = 1662385954
// Duration is the default epoch duration in seconds.
Duration int64 = 10
)
func init() {
err := serix.DefaultAPI.RegisterTypeSettings(nodesActivitySerializableMap{}, serix.TypeSettings{}.WithLengthPrefixType(serix.LengthPrefixTypeAsUint32))
if err != nil {
panic(fmt.Errorf("error registering NodesActivityLog type settings: %w", err))
}
}
// Index is the ID of an epoch.
type Index int64
func IndexFromBytes(bytes []byte) (ei Index, consumedBytes int, err error) {
consumedBytes, err = serix.DefaultAPI.Decode(context.Background(), bytes, &ei)
if err != nil {
panic(err)
}
return
}
// IndexFromTime calculates the EI for the given time.
func IndexFromTime(t time.Time) Index {
elapsedSeconds := t.Unix() - GenesisTime
if elapsedSeconds < 0 {
return 0
}
return Index(elapsedSeconds/Duration + 1)
}
func (i Index) Bytes() []byte {
bytes, err := serix.DefaultAPI.Encode(context.Background(), i, serix.WithValidation())
if err != nil {
panic(err)
}
return bytes
}
func (i Index) String() string {
return fmt.Sprintf("EI(%d)", i)
}
// StartTime calculates the start time of the given epoch.
func (i Index) StartTime() time.Time {
startUnix := GenesisTime + int64(i-1)*Duration
return time.Unix(startUnix, 0)
}
// EndTime calculates the end time of the given epoch.
func (i Index) EndTime() time.Time {
endUnix := GenesisTime + int64(i-1)*Duration + Duration - 1
return time.Unix(endUnix, 0)
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
type MerkleRoot [blake2b.Size256]byte
type (
ECR = MerkleRoot
EC = MerkleRoot
)
func NewMerkleRoot(bytes []byte) MerkleRoot {
b := [blake2b.Size256]byte{}
copy(b[:], bytes[:])
return b
}
func (m MerkleRoot) Base58() string {
return base58.Encode(m[:])
}
func (m MerkleRoot) Bytes() []byte {
return m[:]
}
// CommitmentRoots contains roots of trees of an epoch.
type CommitmentRoots struct {
TangleRoot MerkleRoot `serix:"0"`
StateMutationRoot MerkleRoot `serix:"1"`
StateRoot MerkleRoot `serix:"2"`
ManaRoot MerkleRoot `serix:"3"`
}
// ECRecord is a storable object represents the ecRecord of an epoch.
type ECRecord struct {
model.Storable[Index, ECRecord, *ECRecord, ecRecord] `serix:"0"`
}
type ecRecord struct {
EI Index `serix:"0"`
ECR ECR `serix:"1"`
PrevEC EC `serix:"2"`
Roots *CommitmentRoots `serix:"3"`
}
// NewECRecord creates and returns a ECRecord of the given EI.
func NewECRecord(ei Index) (new *ECRecord) {
new = model.NewStorable[Index, ECRecord](&ecRecord{
EI: ei,
ECR: MerkleRoot{},
PrevEC: MerkleRoot{},
Roots: &CommitmentRoots{},
})
new.SetID(ei)
return
}
func (e *ECRecord) EI() Index {
e.RLock()
defer e.RUnlock()
return e.M.EI
}
func (e *ECRecord) SetEI(ei Index) {
e.Lock()
defer e.Unlock()
e.M.EI = ei
e.SetID(ei)
e.SetModified()
}
// ECR returns the ECR of an ECRecord.
func (e *ECRecord) ECR() ECR {
e.RLock()
defer e.RUnlock()
return e.M.ECR
}
// SetECR sets the ECR of an ECRecord.
func (e *ECRecord) SetECR(ecr ECR) {
e.Lock()
defer e.Unlock()
e.M.ECR = NewMerkleRoot(ecr[:])
e.SetModified()
}
// PrevEC returns the EC of an ECRecord.
func (e *ECRecord) PrevEC() EC {
e.RLock()
defer e.RUnlock()
return e.M.PrevEC
}
// SetPrevEC sets the PrevEC of an ECRecord.
func (e *ECRecord) SetPrevEC(prevEC EC) {
e.Lock()
defer e.Unlock()
e.M.PrevEC = NewMerkleRoot(prevEC[:])
e.SetModified()
}
func (e *ECRecord) Bytes() (bytes []byte, err error) {
bytes, err = e.Storable.Bytes()
return
}
func (e *ECRecord) FromBytes(bytes []byte) (err error) {
err = e.Storable.FromBytes(bytes)
e.SetID(e.EI())
return
}
// Roots returns the CommitmentRoots of an ECRecord.
func (e *ECRecord) Roots() *CommitmentRoots {
e.RLock()
defer e.RUnlock()
return e.M.Roots
}
// SetRoots sets the CommitmentRoots of an ECRecord.
func (e *ECRecord) SetRoots(roots *CommitmentRoots) {
e.Lock()
defer e.Unlock()
e.M.Roots = roots
e.SetModified()
}
// ComputeEC calculates the epoch commitment hash from the given ECRecord.
func (e *ECRecord) ComputeEC() (ec EC) {
ecHash := blake2b.Sum256(byteutils.ConcatBytes(e.EI().Bytes(), e.ECR().Bytes(), e.PrevEC().Bytes()))
return NewMerkleRoot(ecHash[:])
}
// region hashing functions ////////////////////////////////////////////////////////////////////////////////////////////
// ComputeECR calculates an ECR from the tree roots.
func ComputeECR(tangleRoot, stateMutationRoot, stateRoot, manaRoot MerkleRoot) ECR {
branch1Hashed := blake2b.Sum256(byteutils.ConcatBytes(tangleRoot.Bytes(), stateMutationRoot.Bytes()))
branch2Hashed := blake2b.Sum256(byteutils.ConcatBytes(stateRoot.Bytes(), manaRoot.Bytes()))
rootHashed := blake2b.Sum256(byteutils.ConcatBytes(branch1Hashed[:], branch2Hashed[:]))
return NewMerkleRoot(rootHashed[:])
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// region NodesActivityLog //////////////////////////////////////////////////////////////////////////////////////////////////
type nodesActivitySerializableMap map[Index]*ActivityLog
func (al *nodesActivitySerializableMap) FromBytes(data []byte) (err error) {
_, err = serix.DefaultAPI.Decode(context.Background(), data, al, serix.WithValidation())
if err != nil {
err = errors.Errorf("failed to parse activeNodes: %w", err)
return
}
return
}
func (al *nodesActivitySerializableMap) Bytes() []byte {
objBytes, err := serix.DefaultAPI.Encode(context.Background(), *al, serix.WithValidation())
if err != nil {
panic(err)
}
return objBytes
}
func (al *nodesActivitySerializableMap) nodesActivityLog() *NodesActivityLog {
activity := NewNodesActivityLog()
for ei, a := range *al {
activity.Set(ei, a)
}
return activity
}
type NodesActivityLog struct {
shrinkingmap.ShrinkingMap[Index, *ActivityLog] `serix:"0,lengthPrefixType=uint32"`
}
func (al *NodesActivityLog) FromBytes(data []byte) (err error) {
m := make(nodesActivitySerializableMap)
err = m.FromBytes(data)
if err != nil {
return err
}
al.loadActivityLogsMap(m)
return
}
func (al *NodesActivityLog) Bytes() []byte {
m := al.activityLogsMap()
return m.Bytes()
}
func NewNodesActivityLog() *NodesActivityLog {
return &NodesActivityLog{*shrinkingmap.New[Index, *ActivityLog]()}
}
func (al *NodesActivityLog) activityLogsMap() *nodesActivitySerializableMap {
activityMap := make(nodesActivitySerializableMap)
al.ForEach(func(ei Index, activity *ActivityLog) bool {
activityMap[ei] = activity
return true
})
return &activityMap
}
func (al *NodesActivityLog) loadActivityLogsMap(m nodesActivitySerializableMap) {
for ei, a := range m {
al.Set(ei, a)
}
return
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// region ActivityLog //////////////////////////////////////////////////////////////////////////////////////////////////
// ActivityLog is a time-based log of node activity. It stores information when a node is active and provides
// functionality to query for certain timeframes.
type ActivityLog struct {
model.Mutable[ActivityLog, *ActivityLog, activityLogModel] `serix:"0"`
}
// nodeActivityModel stores node identities and corresponding accepted block counters indicating how many blocks node issued in a given epoch.
type activityLogModel struct {
ActivityLog *set.AdvancedSet[identity.ID] `serix:"0,lengthPrefixType=uint32"`
}
// NewActivityLog is the constructor for ActivityLog.
func NewActivityLog() *ActivityLog {
return model.NewMutable[ActivityLog](&activityLogModel{ActivityLog: set.NewAdvancedSet[identity.ID]()})
}
// Add adds a node to the activity log.
func (a *ActivityLog) Add(nodeID identity.ID) (added bool) {
return a.InnerModel().ActivityLog.Add(nodeID)
}
// Remove removes a node from the activity log.
func (a *ActivityLog) Remove(nodeID identity.ID) (removed bool) {
return a.InnerModel().ActivityLog.Delete(nodeID)
}
// Active returns true if the provided node was active.
func (a *ActivityLog) Active(nodeID identity.ID) (active bool) {
if a.InnerModel().ActivityLog.Has(nodeID) {
return true
}
return
}
// String returns a human-readable version of ActivityLog.
func (a *ActivityLog) String() string {
var builder strings.Builder
builder.WriteString(fmt.Sprintf("ActivityLog(len=%d, elements=", a.Size()))
a.InnerModel().ActivityLog.ForEach(func(nodeID identity.ID) (err error) {
builder.WriteString(fmt.Sprintf("%s, ", nodeID.String()))
return
})
builder.WriteString(")")
return builder.String()
}
// Clone clones the ActivityLog.
func (a *ActivityLog) Clone() *ActivityLog {
clone := NewActivityLog()
clone.InnerModel().ActivityLog = a.InnerModel().ActivityLog.Clone()
return clone
}
// ForEach iterates through the activity set and calls the callback for every element.
func (a *ActivityLog) ForEach(callback func(nodeID identity.ID) (err error)) (err error) {
return a.InnerModel().ActivityLog.ForEach(callback)
}
// Size returns the size of the activity log.
func (a *ActivityLog) Size() int {
return a.InnerModel().ActivityLog.Size()
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// SnapshotEpochActivity is the data structure to store node activity for the snapshot.
type SnapshotEpochActivity map[Index]*SnapshotNodeActivity
// NewSnapshotEpochActivity creates a new SnapshotEpochActivity instance.
func NewSnapshotEpochActivity() SnapshotEpochActivity {
return make(SnapshotEpochActivity)
}
// SnapshotNodeActivity is structure to store nodes activity for an epoch.
type SnapshotNodeActivity struct {
model.Mutable[SnapshotNodeActivity, *SnapshotNodeActivity, nodeActivityModel] `serix:"0"`
}
// NewSnapshotNodeActivity creates a new SnapshotNodeActivity instance.
func NewSnapshotNodeActivity() *SnapshotNodeActivity {
return model.NewMutable[SnapshotNodeActivity](&nodeActivityModel{NodesLog: make(map[identity.ID]uint64)})
}
// nodeActivityModel stores node identities and corresponding accepted block counters indicating how many blocks node issued in a given epoch.
type nodeActivityModel struct {
NodesLog map[identity.ID]uint64 `serix:"0,lengthPrefixType=uint32"`
}
// NodesLog returns its activity map of nodes.
func (s *SnapshotNodeActivity) NodesLog() map[identity.ID]uint64 {
return s.M.NodesLog
}
// NodeActivity returns activity counter for a given node.
func (s *SnapshotNodeActivity) NodeActivity(nodeID identity.ID) uint64 {
return s.M.NodesLog[nodeID]
}
// SetNodeActivity adds a node activity record to the activity log.
func (s *SnapshotNodeActivity) SetNodeActivity(nodeID identity.ID, activity uint64) {
s.M.NodesLog[nodeID] = activity
}