-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
134 lines (97 loc) · 3.02 KB
/
event.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
package dag
import (
"fmt"
"github.com/corex-mn/corex-base/hash"
"github.com/corex-mn/corex-base/inter/idx"
)
type Event interface {
Epoch() idx.Epoch
Seq() idx.Event
Frame() idx.Frame
Creator() idx.ValidatorID
Lamport() idx.Lamport
Parents() hash.Events
SelfParent() *hash.Event
IsSelfParent(hash hash.Event) bool
ID() hash.Event
String() string
Size() int
}
type MutableEvent interface {
Event
SetEpoch(idx.Epoch)
SetSeq(idx.Event)
SetFrame(idx.Frame)
SetCreator(idx.ValidatorID)
SetLamport(idx.Lamport)
SetParents(hash.Events)
SetID(id [24]byte)
}
// BaseEvent is the consensus message in the Lachesis consensus algorithm
// The structure isn't supposed to be used as-is:
// Doesn't contain payload, it should be extended by an app
// Doesn't contain event signature, it should be extended by an app
type BaseEvent struct {
epoch idx.Epoch
seq idx.Event
frame idx.Frame
creator idx.ValidatorID
parents hash.Events
lamport idx.Lamport
id hash.Event
}
type MutableBaseEvent struct {
BaseEvent
}
// Build build immutable event
func (me *MutableBaseEvent) Build(rID [24]byte) *BaseEvent {
e := me.BaseEvent
copy(e.id[0:4], e.epoch.Bytes())
copy(e.id[4:8], e.lamport.Bytes())
copy(e.id[8:], rID[:])
return &e
}
// fmtFrame returns frame string representation.
func fmtFrame(frame idx.Frame, isRoot bool) string {
if isRoot {
return fmt.Sprintf("%d:y", frame)
}
return fmt.Sprintf("%d:n", frame)
}
// String returns string representation.
func (e *BaseEvent) String() string {
return fmt.Sprintf("{id=%s, p=%s, by=%d, frame=%d}", e.id.ShortID(3), e.parents.String(), e.creator, e.frame)
}
// SelfParent returns event's self-parent, if any
func (e *BaseEvent) SelfParent() *hash.Event {
if e.seq <= 1 || len(e.parents) == 0 {
return nil
}
return &e.parents[0]
}
// IsSelfParent is true if specified ID is event's self-parent
func (e *BaseEvent) IsSelfParent(hash hash.Event) bool {
if e.SelfParent() == nil {
return false
}
return *e.SelfParent() == hash
}
func (e *BaseEvent) Epoch() idx.Epoch { return e.epoch }
func (e *BaseEvent) Seq() idx.Event { return e.seq }
func (e *BaseEvent) Frame() idx.Frame { return e.frame }
func (e *BaseEvent) Creator() idx.ValidatorID { return e.creator }
func (e *BaseEvent) Parents() hash.Events { return e.parents }
func (e *BaseEvent) Lamport() idx.Lamport { return e.lamport }
func (e *BaseEvent) ID() hash.Event { return e.id }
func (e *BaseEvent) Size() int { return 4 + 4 + 4 + 4 + len(e.parents)*32 + 4 + 32 }
func (e *MutableBaseEvent) SetEpoch(v idx.Epoch) { e.epoch = v }
func (e *MutableBaseEvent) SetSeq(v idx.Event) { e.seq = v }
func (e *MutableBaseEvent) SetFrame(v idx.Frame) { e.frame = v }
func (e *MutableBaseEvent) SetCreator(v idx.ValidatorID) { e.creator = v }
func (e *MutableBaseEvent) SetParents(v hash.Events) { e.parents = v }
func (e *MutableBaseEvent) SetLamport(v idx.Lamport) { e.lamport = v }
func (e *MutableBaseEvent) SetID(rID [24]byte) {
copy(e.id[0:4], e.epoch.Bytes())
copy(e.id[4:8], e.lamport.Bytes())
copy(e.id[8:], rID[:])
}