summaryrefslogtreecommitdiff
path: root/agreement/common_test.go
blob: 9ec85618e8ebf0910e385eccc7b001c892837a62 (plain)
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
// Copyright (C) 2019-2024 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 agreement

import (
	"context"
	"fmt"
	"math/rand"
	"testing"

	"github.com/algorand/go-deadlock"
	"github.com/stretchr/testify/require"
	"golang.org/x/exp/maps"

	"github.com/algorand/go-algorand/config"
	"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/logging"
	"github.com/algorand/go-algorand/protocol"
)

const randseed = 0
const keyBatchesForward = 10
const minMoneyAtStart = 10000
const maxMoneyAtStart = 100000

var readOnlyGenesis10 map[basics.Address]basics.AccountData
var readOnlyAddrs10 []basics.Address
var readOnlyVRF10 []*crypto.VRFSecrets
var readOnlyOT10 []crypto.OneTimeSigner

var readOnlyGenesis100 map[basics.Address]basics.AccountData
var readOnlyAddrs100 []basics.Address
var readOnlyVRF100 []*crypto.VRFSecrets
var readOnlyOT100 []crypto.OneTimeSigner

var readOnlyGenesis7000 map[basics.Address]basics.AccountData
var readOnlyAddrs7000 []basics.Address
var readOnlyVRF7000 []*crypto.VRFSecrets
var readOnlyOT7000 []crypto.OneTimeSigner

var routerFixture rootRouter

func init() {
	rand.Seed(randseed)

	readOnlyGenesis10, readOnlyAddrs10, readOnlyVRF10, readOnlyOT10 = generateEnvironment(10)
	readOnlyGenesis100, readOnlyAddrs100, readOnlyVRF100, readOnlyOT100 = generateEnvironment(100)
	readOnlyGenesis7000, readOnlyAddrs7000, readOnlyVRF7000, readOnlyOT7000 = generateEnvironment(7000)
}

func readOnlyFixture10() (Ledger, []basics.Address, []*crypto.VRFSecrets, []crypto.OneTimeSigner) {
	// generate accounts
	ledger := makeTestLedger(readOnlyGenesis10)
	return ledger, readOnlyAddrs10, readOnlyVRF10, readOnlyOT10
}

func readOnlyFixture100() (Ledger, []basics.Address, []*crypto.VRFSecrets, []crypto.OneTimeSigner) {
	// generate accounts
	ledger := makeTestLedger(readOnlyGenesis100)
	return ledger, readOnlyAddrs100, readOnlyVRF100, readOnlyOT100
}

func readOnlyFixture7000() (Ledger, []basics.Address, []*crypto.VRFSecrets, []crypto.OneTimeSigner) {
	// generate accounts
	ledger := makeTestLedger(readOnlyGenesis7000)
	return ledger, readOnlyAddrs7000, readOnlyVRF7000, readOnlyOT7000
}

func generateKeys(latest basics.Round, keyBatchesForward uint) (basics.Address, *crypto.VRFSecrets, *crypto.OneTimeSignatureSecrets) {
	var seed crypto.Seed
	crypto.RandBytes(seed[:])
	s := crypto.GenerateSignatureSecrets(seed)
	v := crypto.GenerateVRFSecrets()
	o := crypto.GenerateOneTimeSignatureSecrets(basics.OneTimeIDForRound(latest, config.Consensus[protocol.ConsensusCurrentVersion].DefaultKeyDilution).Batch, uint64(keyBatchesForward))
	addr := basics.Address(s.SignatureVerifier)
	return addr, v, o
}

func generateEnvironment(numAccounts int) (map[basics.Address]basics.AccountData, []basics.Address, []*crypto.VRFSecrets, []crypto.OneTimeSigner) {
	genesis := make(map[basics.Address]basics.AccountData)
	gen := rand.New(rand.NewSource(2))
	addrs := make([]basics.Address, numAccounts)
	vrfSecrets := make([]*crypto.VRFSecrets, numAccounts)
	otSecrets := make([]crypto.OneTimeSigner, numAccounts)
	var total basics.MicroAlgos
	for i := 0; i < numAccounts; i++ {
		addr, vrfSec, otSec := generateKeys(0, keyBatchesForward)
		addrs[i] = addr
		vrfSecrets[i] = vrfSec
		otSecrets[i].OneTimeSignatureSecrets = otSec

		startamt := uint64(minMoneyAtStart + (gen.Int() % (maxMoneyAtStart - minMoneyAtStart)))
		genesis[addr] = basics.AccountData{
			Status:      basics.Online,
			MicroAlgos:  basics.MicroAlgos{Raw: startamt},
			SelectionID: vrfSec.PK,
			VoteID:      otSec.OneTimeSignatureVerifier,
		}
		total.Raw += startamt
	}

	return genesis, addrs, vrfSecrets, otSecrets
}

func randomBlockHash() (h crypto.Digest) {
	rand.Read(h[:])
	return
}

func randomVRFProof() (v crypto.VRFProof) {
	rand.Read(v[:])
	return
}

type signal struct {
	ch    chan struct{}
	fired bool
}

func makeSignal() signal {
	var s signal
	s.ch = make(chan struct{})
	return s
}

func (s signal) wait() {
	<-s.ch
}

func (s signal) fire() signal {
	if !s.fired {
		close(s.ch)
	}
	return signal{s.ch, true}
}

type nullWriter struct{}

func (w nullWriter) Write(data []byte) (n int, err error) {
	return len(data), nil
}

type testValidatedBlock struct {
	Inside bookkeeping.Block
}

func (b testValidatedBlock) Block() bookkeeping.Block {
	return b.Inside
}

func (b testValidatedBlock) WithSeed(s committee.Seed) ValidatedBlock {
	b.Inside.BlockHeader.Seed = s
	return b
}

type testBlockValidator struct{}

func (v testBlockValidator) Validate(ctx context.Context, e bookkeeping.Block) (ValidatedBlock, error) {
	return testValidatedBlock{Inside: e}, nil
}

type testBlockFactory struct {
	Owner int
}

func (f testBlockFactory) AssembleBlock(r basics.Round) (ValidatedBlock, error) {
	return testValidatedBlock{Inside: bookkeeping.Block{BlockHeader: bookkeeping.BlockHeader{Round: r}}}, nil
}

// If we try to read from high rounds, we panic and do not emit an error to find bugs during testing.
type testLedger struct {
	mu deadlock.Mutex

	entries   map[basics.Round]bookkeeping.Block
	certs     map[basics.Round]Certificate
	nextRound basics.Round

	maxNumBlocks uint64

	// constant
	state map[basics.Address]basics.AccountData

	notifications map[basics.Round]signal

	consensusVersion func(basics.Round) (protocol.ConsensusVersion, error)
}

func makeTestLedger(state map[basics.Address]basics.AccountData) Ledger {
	l := new(testLedger)
	l.entries = make(map[basics.Round]bookkeeping.Block)
	l.certs = make(map[basics.Round]Certificate)
	l.nextRound = 1

	l.state = maps.Clone(state)

	l.notifications = make(map[basics.Round]signal)

	l.consensusVersion = func(r basics.Round) (protocol.ConsensusVersion, error) {
		return protocol.ConsensusCurrentVersion, nil
	}
	return l
}

func makeTestLedgerWithConsensusVersion(state map[basics.Address]basics.AccountData, consensusVersion func(basics.Round) (protocol.ConsensusVersion, error)) Ledger {
	l := new(testLedger)
	l.entries = make(map[basics.Round]bookkeeping.Block)
	l.certs = make(map[basics.Round]Certificate)
	l.nextRound = 1

	l.state = maps.Clone(state)

	l.notifications = make(map[basics.Round]signal)

	l.consensusVersion = consensusVersion
	return l
}

func makeTestLedgerMaxBlocks(state map[basics.Address]basics.AccountData, maxNumBlocks uint64) Ledger {
	l := new(testLedger)
	l.entries = make(map[basics.Round]bookkeeping.Block)
	l.certs = make(map[basics.Round]Certificate)
	l.nextRound = 1

	l.maxNumBlocks = maxNumBlocks

	l.state = maps.Clone(state)

	l.notifications = make(map[basics.Round]signal)

	l.consensusVersion = func(r basics.Round) (protocol.ConsensusVersion, error) {
		return protocol.ConsensusCurrentVersion, nil
	}
	return l
}

func (l *testLedger) NextRound() basics.Round {
	l.mu.Lock()
	defer l.mu.Unlock()

	return l.nextRound
}

func (l *testLedger) Wait(r basics.Round) chan struct{} {
	l.mu.Lock()
	defer l.mu.Unlock()

	if _, ok := l.notifications[r]; !ok {
		l.notifications[r] = makeSignal()
	}

	if l.nextRound > r {
		l.notify(r)
	}

	return l.notifications[r].ch
}

// note: this must be called when any new block is written
// this should be called while the lock l.mu is held
func (l *testLedger) notify(r basics.Round) {
	if _, ok := l.notifications[r]; !ok {
		l.notifications[r] = makeSignal()
	}

	l.notifications[r] = l.notifications[r].fire()
}

func (l *testLedger) Seed(r basics.Round) (committee.Seed, error) {
	l.mu.Lock()
	defer l.mu.Unlock()

	if r >= l.nextRound {
		err := fmt.Errorf("Seed called on future round: %v >= %v! (this is probably a bug)", r, l.nextRound)
		panic(err)
	}

	b := l.entries[r]
	return b.Seed(), nil
}

func (l *testLedger) LookupDigest(r basics.Round) (crypto.Digest, error) {
	l.mu.Lock()
	defer l.mu.Unlock()

	if r >= l.nextRound {
		err := fmt.Errorf("LookupDigest called on future round: %v >= %v! (this is probably a bug)", r, l.nextRound)
		panic(err)
	}

	if l.maxNumBlocks != 0 && r+round(l.maxNumBlocks) < l.nextRound {
		return crypto.Digest{}, &LedgerDroppedRoundError{}
	}

	return l.entries[r].Digest(), nil
}

func (l *testLedger) LookupAgreement(r basics.Round, a basics.Address) (basics.OnlineAccountData, error) {
	l.mu.Lock()
	defer l.mu.Unlock()

	if r >= l.nextRound {
		err := fmt.Errorf("Lookup called on future round: %v >= %v! (this is probably a bug)", r, l.nextRound)
		panic(err)
	}

	if l.maxNumBlocks != 0 && r+round(l.maxNumBlocks) < l.nextRound {
		return basics.OnlineAccountData{}, &LedgerDroppedRoundError{}
	}

	return l.state[a].OnlineAccountData(), nil
}

func (l *testLedger) Circulation(r basics.Round, voteRnd basics.Round) (basics.MicroAlgos, error) {
	l.mu.Lock()
	defer l.mu.Unlock()

	if r >= l.nextRound {
		err := fmt.Errorf("Circulation called on future round: %v >= %v! (this is probably a bug)", r, l.nextRound)
		panic(err)
	}

	var sum basics.MicroAlgos
	var overflowed bool
	for _, rec := range l.state {
		sum, overflowed = basics.OAddA(sum, rec.OnlineAccountData().VotingStake())
		if overflowed {
			panic("circulation computation overflowed")
		}
	}
	return sum, nil
}

func (l *testLedger) EnsureValidatedBlock(e ValidatedBlock, c Certificate) {
	l.EnsureBlock(e.Block(), c)
}

func (l *testLedger) EnsureBlock(e bookkeeping.Block, c Certificate) {
	l.mu.Lock()
	defer l.mu.Unlock()

	if _, ok := l.entries[e.Round()]; ok {
		if l.entries[e.Round()].Digest() != e.Digest() {
			err := fmt.Errorf("testLedger.EnsureBlock: called with conflicting entries in round %d", e.Round())
			panic(err)
		}
	}

	l.entries[e.Round()] = e
	l.certs[e.Round()] = c

	if l.nextRound == e.Round() {
		l.nextRound = e.Round() + 1
	} else if l.nextRound < e.Round() {
		err := fmt.Errorf("testLedger.EnsureBlock: attempted to write block in future round: %d < %d", l.nextRound, e.Round())
		panic(err)
	}

	l.notify(e.Round())
}

func (l *testLedger) EnsureDigest(c Certificate, verifier *AsyncVoteVerifier) {
	r := c.Round
	l.mu.Lock()
	defer l.mu.Unlock()

	if r < l.nextRound {
		if l.entries[r].Digest() != c.Proposal.BlockDigest {
			err := fmt.Errorf("testLedger.EnsureDigest called with conflicting entries in round %d", r)
			panic(err)
		}
	}
	// the mock ledger does not actually need to wait for the block.
	// Agreement should function properly even if it never happens.
	// No test right now expects the ledger to eventually ensure digest (we can add one if need be)
	return
}

func (l *testLedger) ConsensusParams(r basics.Round) (config.ConsensusParams, error) {
	version, err := l.ConsensusVersion(r)
	if err != nil {
		return config.ConsensusParams{}, err
	}
	return config.Consensus[version], nil
}

func (l *testLedger) ConsensusVersion(r basics.Round) (protocol.ConsensusVersion, error) {
	return l.consensusVersion(r)
}

// simulation helpers

type testAccountData struct {
	addresses []basics.Address
	vrfs      []*crypto.VRFSecrets
	ots       []crypto.OneTimeSigner
}

func makeProposalsTesting(accs testAccountData, round basics.Round, period period, factory BlockFactory, ledger Ledger) (ps []proposal, vs []vote) {
	ve, err := factory.AssembleBlock(round)
	if err != nil {
		logging.Base().Errorf("Could not generate a proposal for round %d: %v", round, err)
		return nil, nil
	}

	// TODO this common code should be refactored out
	var votes []vote
	proposals := make([]proposal, 0)
	for i := range accs.addresses {
		payload, proposal, err := proposalForBlock(accs.addresses[i], accs.vrfs[i], ve, period, ledger)
		if err != nil {
			logging.Base().Errorf("proposalForBlock could not create proposal under address %v (corrupt VRF key?): %v", accs.addresses[i], err)
			return
		}

		// attempt to make the vote
		rv := rawVote{Sender: accs.addresses[i], Round: round, Period: period, Step: propose, Proposal: proposal}
		uv, err := makeVote(rv, accs.ots[i], accs.vrfs[i], ledger)
		if err != nil {
			logging.Base().Errorf("AccountManager.makeVotes: Could not create vote: %v", err)
			return
		}
		vote, err := uv.verify(ledger)
		if err != nil {
			continue
		}

		// create the block proposal
		proposals = append(proposals, payload)
		votes = append(votes, vote)
	}
	return proposals, votes
}

// makeVotes creates a slice of votes for a given proposal value in a given
// round, period, and step.
func makeVotesTesting(accs testAccountData, round basics.Round, period period, step step, proposal proposalValue, ledger Ledger) (vs []vote) {
	// TODO this common code should be refactored out
	votes := make([]vote, 0)
	for i := range accs.addresses {
		rv := rawVote{Sender: accs.addresses[i], Round: round, Period: period, Step: step, Proposal: proposal}
		uv, err := makeVote(rv, accs.ots[i], accs.vrfs[i], ledger)
		if err != nil {
			logging.Base().Errorf("AccountManager.makeVotes: Could not create vote: %v", err)
			return
		}

		vote, err := uv.verify(ledger)
		if err == nil {
			votes = append(votes, vote)
		}
	}

	return votes
}

func makeRandomBlock(rnd round) bookkeeping.Block {
	return bookkeeping.Block{BlockHeader: bookkeeping.BlockHeader{Round: rnd, Branch: bookkeeping.BlockHash(randomBlockHash())}}
}

// equals returns true if and only if two votes are from the same sender and voted for the same thing.
func (v vote) equals(other vote) bool {
	if v.R.Sender != other.R.Sender {
		return false
	}

	if v.R.Round != other.R.Round {
		return false
	}

	if v.R.Period != other.R.Period {
		return false
	}

	if v.R.Step != other.R.Step {
		return false
	}

	if !v.Cred.Equals(other.Cred) {
		return false
	}

	if v.R.Proposal.BlockDigest != other.R.Proposal.BlockDigest {
		return false
	}

	return true
}

/* Fine-grained unit test helpers */

type voteMakerHelper struct {
	proposal  *proposalValue
	addresses map[int]basics.Address
}

func (v *voteMakerHelper) Setup() {
	if v.proposal == nil {
		v.proposal = v.MakeRandomProposalValue()
		v.addresses = make(map[int]basics.Address)
	}
}

func (v *voteMakerHelper) MakeRandomProposalValue() *proposalValue {
	return &proposalValue{
		BlockDigest: randomBlockHash(),
	}
}

func (v *voteMakerHelper) MakeRandomProposalPayload(t *testing.T, r round) (*proposal, *proposalValue) {
	f := testBlockFactory{Owner: 1}
	ve, err := f.AssembleBlock(r)
	require.NoError(t, err)

	var payload unauthenticatedProposal
	payload.Block = ve.Block()
	payload.SeedProof = randomVRFProof()

	propVal := proposalValue{
		BlockDigest:    payload.Digest(),
		EncodingDigest: crypto.HashObj(payload),
	}

	return &proposal{unauthenticatedProposal: payload, ve: ve}, &propVal
}

// make a vote for a fixed proposal value
func (v *voteMakerHelper) MakeValidVoteAccepted(t *testing.T, index int, step step) voteAcceptedEvent {
	return v.MakeValidVoteAcceptedVal(t, index, step, *v.proposal)
}

func (v *voteMakerHelper) MakeRawVote(t *testing.T, index int, r round, p period, s step, value proposalValue) rawVote {
	if _, ok := v.addresses[index]; !ok {
		v.addresses[index] = basics.Address(randomBlockHash())
	}
	return rawVote{Sender: v.addresses[index], Round: r, Period: p, Step: s, Proposal: value}
}

func (v *voteMakerHelper) MakeVerifiedVote(t *testing.T, index int, r round, p period, s step, value proposalValue) vote {
	return vote{
		R:    v.MakeRawVote(t, index, r, p, s, value),
		Cred: committee.Credential{Weight: 1},
	}
}
func (v *voteMakerHelper) MakeEquivocationVote(t *testing.T, index int, r round, p period, s step, weight uint64) equivocationVote {
	if _, ok := v.addresses[index]; !ok {
		v.addresses[index] = basics.Address(randomBlockHash())
	}
	pV1 := v.MakeRandomProposalValue()
	pV2 := v.MakeRandomProposalValue()
	return equivocationVote{
		Sender:    v.addresses[index],
		Round:     r,
		Period:    p,
		Step:      s,
		Cred:      committee.Credential{Weight: weight},
		Proposals: [2]proposalValue{*pV1, *pV2},
	}
}

// make a vote for specified proposal value
func (v *voteMakerHelper) MakeValidVoteAcceptedVal(t *testing.T, index int, step step, value proposalValue) voteAcceptedEvent {
	// these unit tests assume that the vote tracker doesn't validate the integrity of the vote event itself
	vt := v.MakeVerifiedVote(t, index, round(0), period(8), step, value)
	return voteAcceptedEvent{vt, protocol.ConsensusCurrentVersion}
}

func (v *voteMakerHelper) MakeUnauthenticatedVote(t *testing.T, index int, r round, p period, s step, value proposalValue) unauthenticatedVote {
	return unauthenticatedVote{
		R:    v.MakeRawVote(t, index, r, p, s, value),
		Cred: committee.UnauthenticatedCredential{},
	}
}

// MakeUnauthenticatedBundle creates a bundle where no participant has more than 1 weight
func (v *voteMakerHelper) MakeUnauthenticatedBundle(t *testing.T, r round, p period, s step, value proposalValue) unauthenticatedBundle {
	return v.MakeVerifiedBundle(t, r, p, s, value).U
}

func (v *voteMakerHelper) MakeVerifiedBundle(t *testing.T, r round, p period, s step, value proposalValue) bundle {
	votes := make([]vote, int(s.threshold(config.Consensus[protocol.ConsensusCurrentVersion])))
	for i := 0; i < int(s.threshold(config.Consensus[protocol.ConsensusCurrentVersion])); i++ {
		votes[i] = v.MakeVerifiedVote(t, i, r, p, s, value)
	}
	bun := unauthenticatedBundle{
		Round:    r,
		Period:   p,
		Step:     s,
		Proposal: value,
	}
	return bundle{
		U:     bun,
		Votes: votes,
	}
}