-
Notifications
You must be signed in to change notification settings - Fork 210
/
malfeasance.go
238 lines (207 loc) · 5.38 KB
/
malfeasance.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
package types
import (
"errors"
"github.com/spacemeshos/go-scale"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/log"
)
//go:generate scalegen -types MalfeasanceProof,MalfeasanceGossip,AtxProof,BallotProof,HareProof,AtxProofMsg,BallotProofMsg,HareProofMsg,HareMetadata
const (
MultipleATXs byte = iota + 1
MultipleBallots
HareEquivocation
)
type MalfeasanceProof struct {
// for network upgrade
Layer LayerID
Proof Proof
}
func (mp *MalfeasanceProof) MarshalLogObject(encoder log.ObjectEncoder) error {
encoder.AddUint32("generated_layer", mp.Layer.Uint32())
switch mp.Proof.Type {
case MultipleATXs:
encoder.AddString("type", "multiple atxs")
p, ok := mp.Proof.Data.(*AtxProof)
if !ok {
encoder.AddString("msgs", "n/a")
} else {
encoder.AddObject("msgs", p)
}
case MultipleBallots:
encoder.AddString("type", "multiple ballots")
p, ok := mp.Proof.Data.(*BallotProof)
if !ok {
encoder.AddString("msgs", "n/a")
} else {
encoder.AddObject("msgs", p)
}
case HareEquivocation:
encoder.AddString("type", "hare equivocation")
p, ok := mp.Proof.Data.(*HareProof)
if !ok {
encoder.AddString("msgs", "n/a")
} else {
encoder.AddObject("msgs", p)
}
default:
encoder.AddString("type", "unknown")
}
return nil
}
type Proof struct {
// MultipleATXs | MultipleBallots | HareEquivocation
Type uint8
// AtxProof | BallotProof | HareProof
Data scale.Type
}
func (e *Proof) EncodeScale(enc *scale.Encoder) (int, error) {
var total int
{
// not compact, as scale spec uses "full" uint8 for enums
n, err := scale.EncodeByte(enc, e.Type)
if err != nil {
return total, err
}
total += n
}
{
n, err := e.Data.EncodeScale(enc)
if err != nil {
return total, err
}
total += n
}
return total, nil
}
func (e *Proof) DecodeScale(dec *scale.Decoder) (int, error) {
var total int
{
typ, n, err := scale.DecodeByte(dec)
if err != nil {
return total, err
}
e.Type = typ
total += n
}
switch e.Type {
case MultipleATXs:
var proof AtxProof
n, err := proof.DecodeScale(dec)
if err != nil {
return total, err
}
e.Data = &proof
total += n
case MultipleBallots:
var proof BallotProof
n, err := proof.DecodeScale(dec)
if err != nil {
return total, err
}
e.Data = &proof
total += n
case HareEquivocation:
var proof HareProof
n, err := proof.DecodeScale(dec)
if err != nil {
return total, err
}
e.Data = &proof
total += n
default:
return total, errors.New("unknown malfeasance type")
}
return total, nil
}
type MalfeasanceGossip struct {
MalfeasanceProof
Eligibility *HareEligibilityGossip // optional, only useful in live hare rounds
}
func (mg *MalfeasanceGossip) MarshalLogObject(encoder log.ObjectEncoder) error {
encoder.AddObject("proof", &mg.MalfeasanceProof)
if mg.Eligibility != nil {
encoder.AddObject("hare eligibility", mg.Eligibility)
}
return nil
}
type AtxProof struct {
Messages [2]AtxProofMsg
}
func (ap *AtxProof) MarshalLogObject(encoder log.ObjectEncoder) error {
encoder.AddObject("first", &ap.Messages[0].InnerMsg)
encoder.AddObject("second", &ap.Messages[1].InnerMsg)
return nil
}
type BallotProof struct {
Messages [2]BallotProofMsg
}
func (bp *BallotProof) MarshalLogObject(encoder log.ObjectEncoder) error {
encoder.AddObject("first", &bp.Messages[0].InnerMsg)
encoder.AddObject("second", &bp.Messages[1].InnerMsg)
return nil
}
type HareProof struct {
Messages [2]HareProofMsg
}
func (hp *HareProof) MarshalLogObject(encoder log.ObjectEncoder) error {
encoder.AddObject("first", &hp.Messages[0].InnerMsg)
encoder.AddObject("second", &hp.Messages[1].InnerMsg)
return nil
}
type AtxProofMsg struct {
InnerMsg ATXMetadata
SmesherID NodeID
Signature EdSignature
}
// SignedBytes returns the actual data being signed in a AtxProofMsg.
func (m *AtxProofMsg) SignedBytes() []byte {
data, err := codec.Encode(&m.InnerMsg)
if err != nil {
log.With().Fatal("failed to serialize AtxProofMsg", log.Err(err))
}
return data
}
type BallotProofMsg struct {
InnerMsg BallotMetadata
SmesherID NodeID
Signature EdSignature
}
// SignedBytes returns the actual data being signed in a BallotProofMsg.
func (m *BallotProofMsg) SignedBytes() []byte {
data, err := codec.Encode(&m.InnerMsg)
if err != nil {
log.With().Fatal("failed to serialize MultiBlockProposalsMsg", log.Err(err))
}
return data
}
type HareMetadata struct {
Layer LayerID
// the round counter (K)
Round uint32
// hash of hare.Message.InnerMessage
MsgHash Hash32
}
func (hm *HareMetadata) MarshalLogObject(encoder log.ObjectEncoder) error {
encoder.AddUint32("layer", hm.Layer.Uint32())
encoder.AddUint32("round", hm.Round)
encoder.AddString("msgHash", hm.MsgHash.String())
return nil
}
// Equivocation detects if two messages form an equivocation, based on their HareMetadata.
// It returns true if the two messages are from the same layer and round, but have different hashes.
func (hm *HareMetadata) Equivocation(other *HareMetadata) bool {
return hm.Layer == other.Layer && hm.Round == other.Round && hm.MsgHash != other.MsgHash
}
type HareProofMsg struct {
InnerMsg HareMetadata
SmesherID NodeID
Signature EdSignature
}
// SignedBytes returns the actual data being signed in a HareProofMsg.
func (m *HareProofMsg) SignedBytes() []byte {
data, err := codec.Encode(&m.InnerMsg)
if err != nil {
log.With().Fatal("failed to serialize MultiBlockProposalsMsg", log.Err(err))
}
return data
}