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
|
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package fuzzer
import (
"bytes"
"encoding/json"
"github.com/algorand/go-deadlock"
//"github.com/algorand/go-algorand/agreement"
"github.com/algorand/go-algorand/crypto"
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/data/bookkeeping"
"github.com/algorand/go-algorand/data/committee"
"github.com/algorand/go-algorand/protocol"
)
type MessageDecoderStore struct {
deadlock.Mutex
votes map[string]unauthenticatedVote
proposals map[string]compoundMessage
bundles map[string]unauthenticatedBundle
}
type (
// round denotes a single round of the agreement protocol
round = basics.Round
// step is a sequence number denoting distinct stages in Algorand
step uint64
// period is used to track progress with a given round in the protocol
period uint64
// roundPeriod represents a specific period in a specific Round of the protocol
roundPeriod struct {
basics.Round
Period period
}
// rawVote is the inner struct which is authenticated with keys
rawVote struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
Sender basics.Address `codec:"snd"`
Round basics.Round `codec:"rnd"`
Period period `codec:"per"`
Step step `codec:"step"`
Proposal proposalValue `codec:"prop"`
}
// unauthenticatedVote is a vote which has not been verified
unauthenticatedVote struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
R rawVote `codec:"r"`
Cred committee.UnauthenticatedCredential `codec:"cred"`
Sig crypto.OneTimeSignature `codec:"sig,omitempty,omitemptycheckstruct"`
}
proposalValue struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
OriginalPeriod period `codec:"oper"`
OriginalProposer basics.Address `codec:"oprop"`
EntryDigest crypto.Digest `codec:"dig"`
EncodingDigest crypto.Digest `coded:"encdig"`
}
// unauthenticatedBundle is a bundle which has not yet been verified.
unauthenticatedBundle struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
Round basics.Round `codec:"rnd"`
Period period `codec:"per"`
Step step `codec:"step"`
Proposal proposalValue `codec:"prop"`
Votes []voteAuthenticator `codec:"vote"`
EquivocationVotes []equivocationVoteAuthenticator `codec:"eqv"`
}
// bundle is a set of votes, all from the same round, period, and step, and from distinct senders, that reaches quorum.
//
// It also include equivocation pairs -- pairs of votes where someone maliciously voted for two different values -- as these count as votes for *any* value.
bundle struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
U unauthenticatedBundle `codec:"u"`
// Round basics.Round `codec:"rnd"`
// Period period `codec:"per"`
// Step step `codec:"step"`
// Proposal proposal `codec:"prop"`
Votes []vote `codec:"vote"`
EquivocationVotes []equivocationVote `codec:"eqv"`
}
// voteAuthenticators omit the Round, Period, Step, and Proposal for compression
// and to simplify checking logic.
voteAuthenticator struct {
Sender basics.Address `codec:"snd"`
Cred committee.UnauthenticatedCredential `codec:"cred"`
Sig crypto.OneTimeSignature `codec:"sig,omitempty,omitemptycheckstruct"`
}
equivocationVoteAuthenticator struct {
Sender basics.Address `codec:"snd"`
Cred committee.UnauthenticatedCredential `codec:"cred"`
Sigs [2]crypto.OneTimeSignature `codec:"sig,omitempty,omitemptycheckstruct"`
Proposals [2]proposalValue `codec:"props"`
}
// A vote is an endorsement of a particular proposal in Algorand
vote struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
R rawVote `codec:"r"`
Cred committee.Credential `codec:"cred"`
Sig crypto.OneTimeSignature `codec:"sig,omitempty,omitemptycheckstruct"`
}
// unauthenticatedEquivocationVote is a pair of votes which has not
// been verified to be equivocating.
unauthenticatedEquivocationVote struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
Sender basics.Address `codec:"snd"`
Round basics.Round `codec:"rnd"`
Period period `codec:"per"`
Step step `codec:"step"`
Cred committee.UnauthenticatedCredential `codec:"cred"`
Proposals [2]proposalValue `codec:"props"`
Sigs [2]crypto.OneTimeSignature `codec:"sigs"`
}
// An equivocationVote is a pair of votes from the same sender that
// votes for two different hashes.
//
// These pairs are necessarily generated by a faulty node. However, if
// we ever receive such a pair, we must count this as a single
// "wildcard" vote to avoid violating vote propagation assumptions and
// causing a fork.
equivocationVote struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
Sender basics.Address `codec:"snd"`
Round basics.Round `codec:"rnd"`
Period period `codec:"per"`
Step step `codec:"step"`
Cred committee.Credential `codec:"cred"`
Proposals [2]proposalValue `codec:"props"`
Sigs [2]crypto.OneTimeSignature `codec:"sigs"`
}
// An unauthenticatedProposal is an Entry which has not been validated yet.
unauthenticatedProposal struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
bookkeeping.Block
SeedProof crypto.VrfProof `codec:"sdpf"`
}
compoundMessage struct {
Vote unauthenticatedVote
Proposal unauthenticatedProposal
}
// A transmittedPayload is the representation of a proposal payload on the wire.
transmittedPayload struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
unauthenticatedProposal
PriorVote unauthenticatedVote `codec:"pv"`
}
)
// Algorand 2.0 steps
const (
propose step = iota
soft
cert
next
)
type MessageDecoderFilter struct {
NetworkFilter
upstream UpstreamFilter
downstream DownstreamFilter
fuzzer *Fuzzer
NetworkFilterFactory
msgStore *MessageDecoderStore
}
func (n *MessageDecoderFilter) SendMessage(sourceNode, targetNode int, tag protocol.Tag, data []byte) {
switch tag {
case protocol.AgreementVoteTag:
n.decodeVote(data)
case protocol.ProposalPayloadTag:
n.decodeProposal(data)
case protocol.VoteBundleTag:
n.decodeBundle(data)
}
n.downstream.SendMessage(sourceNode, targetNode, tag, data)
}
func (n *MessageDecoderFilter) GetDownstreamFilter() DownstreamFilter {
return n.downstream
}
func (n *MessageDecoderFilter) ReceiveMessage(sourceNode int, tag protocol.Tag, data []byte) {
n.upstream.ReceiveMessage(sourceNode, tag, data)
}
func (n *MessageDecoderFilter) SetDownstreamFilter(f DownstreamFilter) {
n.downstream = f
}
func (n *MessageDecoderFilter) SetUpstreamFilter(f UpstreamFilter) {
n.upstream = f
}
func (n *MessageDecoderFilter) CreateFilter(nodeID int, fuzzer *Fuzzer) NetworkFilter {
if n.msgStore == nil {
n.msgStore = &MessageDecoderStore{
votes: make(map[string]unauthenticatedVote),
proposals: make(map[string]compoundMessage),
bundles: make(map[string]unauthenticatedBundle),
}
}
return &MessageDecoderFilter{
fuzzer: fuzzer,
msgStore: n.msgStore,
}
}
func (n *MessageDecoderFilter) Tick(newClockTime int) bool {
return n.upstream.Tick(newClockTime)
}
func (n *MessageDecoderFilter) decodeVote(data []byte) {
var uv unauthenticatedVote
err := protocol.DecodeStream(bytes.NewBuffer(data), &uv)
if err != nil {
return
}
key := string(data)
n.msgStore.Lock()
defer n.msgStore.Unlock()
n.msgStore.votes[key] = uv
}
func (n *MessageDecoderFilter) decodeProposal(data []byte) {
var p transmittedPayload
err := protocol.DecodeStream(bytes.NewBuffer(data), &p)
if err != nil {
return
}
c := compoundMessage{
Vote: p.PriorVote,
Proposal: p.unauthenticatedProposal,
}
key := string(data)
n.msgStore.Lock()
defer n.msgStore.Unlock()
n.msgStore.proposals[key] = c
}
func (n *MessageDecoderFilter) decodeBundle(data []byte) {
var ub unauthenticatedBundle
err := protocol.DecodeStream(bytes.NewBuffer(data), &ub)
if err != nil {
return
}
key := string(data)
n.msgStore.Lock()
defer n.msgStore.Unlock()
n.msgStore.bundles[key] = ub
}
func (n *MessageDecoderFilter) getDecodedMessage(tag protocol.Tag, data []byte) (*unauthenticatedVote, *compoundMessage, *unauthenticatedBundle) {
key := string(data)
n.msgStore.Lock()
defer n.msgStore.Unlock()
switch tag {
case protocol.AgreementVoteTag:
if uv, has := n.msgStore.votes[key]; has {
return &uv, nil, nil
}
case protocol.ProposalPayloadTag:
if up, has := n.msgStore.proposals[key]; has {
return nil, &up, nil
}
case protocol.VoteBundleTag:
if ub, has := n.msgStore.bundles[key]; has {
return nil, nil, &ub
}
}
return nil, nil, nil
}
func (n *MessageDecoderFilter) getDecodedMessageCounts(tag protocol.Tag) int {
n.msgStore.Lock()
defer n.msgStore.Unlock()
switch tag {
case protocol.AgreementVoteTag:
return len(n.msgStore.votes)
case protocol.ProposalPayloadTag:
return len(n.msgStore.proposals)
case protocol.VoteBundleTag:
return len(n.msgStore.bundles)
}
return -1
}
// Unmarshall MessageDecoderFilter
func (n *MessageDecoderFilter) Unmarshal(b []byte) NetworkFilterFactory {
type messageDecoderFilterJSON struct {
Name string
}
var jsonConfig messageDecoderFilterJSON
if err := json.Unmarshal(b, &jsonConfig); err != nil {
return nil
}
if jsonConfig.Name != "MessageDecoderFilter" {
return nil
}
return &MessageDecoderFilter{}
}
// register MessageDecoderFilter
func init() {
registeredFilterFactories = append(registeredFilterFactories, &MessageDecoderFilter{})
}
|