summaryrefslogtreecommitdiff
path: root/agreement/fuzzer/ledger_test.go
blob: a62caee4d918ecf39ee0cfa3e57b4be5fc909dbd (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
// 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 (
	"context"
	"fmt"
	"math/rand"

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

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

var readOnlyParticipationVotes = []*crypto.OneTimeSignatureSecrets{}

func init() {
	rand.Seed(randseed)
	for i := 0; i < 64; i++ {
		prngSeed := []byte(fmt.Sprintf("Fuzzer-OTSS-PRNG-%d", i))
		rng := crypto.MakePRNG(prngSeed)
		readOnlyParticipationVotes = append(readOnlyParticipationVotes, crypto.GenerateOneTimeSignatureSecretsRNG(0, 1000, rng))
	}
}

func generatePseudoRandomVRF(keynum int) *crypto.VRFSecrets {
	seed := [32]byte{}
	seed[0] = byte(keynum % 255)
	seed[1] = byte(keynum / 255)
	pk, sk := crypto.VrfKeygenFromSeed(seed)
	return &crypto.VRFSecrets{
		PK: pk,
		SK: sk,
	}
}

func randomBlockHash() (h crypto.Digest) {
	rand.Read(h[:])
	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 testValidatedBlock struct {
	Inside bookkeeping.Block
}

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

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

type testBlockValidator struct{}

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

type testBlockFactory struct {
	Owner int
}

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

type testLedgerSyncFunc func(l *testLedger, r basics.Round, c agreement.Certificate) bool

// 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]agreement.Certificate
	nextRound basics.Round

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

	notifications map[basics.Round]signal

	Sync                  testLedgerSyncFunc
	EnsuringDigestStartCh chan struct{}
	EnsuringDigestDoneCh  chan struct{}
	ensuringDigestMu      deadlock.Mutex
	ensuringDigest        bool
	ensuringDigestTry     chan struct{}
	catchingUp            bool
}

func makeTestLedger(state map[basics.Address]basics.AccountData, sync testLedgerSyncFunc) *testLedger {
	l := new(testLedger)
	l.Sync = sync
	l.entries = make(map[basics.Round]bookkeeping.Block)
	l.certs = make(map[basics.Round]agreement.Certificate)
	l.nextRound = 1

	// deep copy of state
	l.state = make(map[basics.Address]basics.AccountData)
	for k, v := range state {
		l.state[k] = v
	}

	l.notifications = make(map[basics.Round]signal)
	l.EnsuringDigestStartCh = make(chan struct{})
	l.EnsuringDigestDoneCh = make(chan struct{})
	close(l.EnsuringDigestDoneCh)
	l.ensuringDigestTry = make(chan struct{}, 1)

	return l
}

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

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
}

func (l *testLedger) ClearNotifications() {
	l.mu.Lock()
	defer l.mu.Unlock()
	l.notifications = make(map[basics.Round]signal)
}

// note: this must be called when any new entry 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 for round %d doesn't exists in ledger. Current ledger round is %d", r, l.nextRound-1)
		return committee.Seed{}, 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: %d >= %d! (this is probably a bug)", r, l.nextRound)
		panic(err)
	}

	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: %d >= %d! (this is probably a bug)", r, l.nextRound)
		panic(err)
	}
	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: %d >= %d! (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) EnsureBlock(e bookkeeping.Block, c agreement.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())
	l.catchingUp = false
}

func (l *testLedger) EnsureDigest(c agreement.Certificate, verifier *agreement.AsyncVoteVerifier) {
	r := c.Round
	consistencyCheck := func() bool {
		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)
			}
			return true
		}
		return false
	}

	if consistencyCheck() {
		return
	}
	// try without any locks.
	if l.Sync(l, r, c) {
		return
	}

	l.ensuringDigestMu.Lock()
	l.ensuringDigest = true
	l.EnsuringDigestDoneCh = make(chan struct{})
	close(l.EnsuringDigestStartCh)
	select {
	case <-l.ensuringDigestTry:
	default:
	}
	l.ensuringDigestMu.Unlock()

	for exitSync := false; exitSync == false; {
		if l.Sync(l, r, c) {
			exitSync = true
			continue
		}
		<-l.ensuringDigestTry
	}

	l.ensuringDigestMu.Lock()
	select {
	case <-l.ensuringDigestTry:
	default:
	}
	l.ensuringDigest = false
	close(l.EnsuringDigestDoneCh)
	l.EnsuringDigestStartCh = make(chan struct{})
	l.ensuringDigestMu.Unlock()
}

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

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

func (l *testLedger) TryEnsuringDigest() bool {
	l.ensuringDigestMu.Lock()
	defer l.ensuringDigestMu.Unlock()
	select {
	case l.ensuringDigestTry <- struct{}{}:
		return true
	default:
		return false
	}
}

func (l *testLedger) GetEnsuringDigestCh(start bool) chan struct{} {
	l.ensuringDigestMu.Lock()
	defer l.ensuringDigestMu.Unlock()
	if start {
		return l.EnsuringDigestStartCh
	}
	return l.EnsuringDigestDoneCh
}

func (l *testLedger) IsEnsuringDigest() bool {
	l.ensuringDigestMu.Lock()
	defer l.ensuringDigestMu.Unlock()
	return l.ensuringDigest
}

func (l *testLedger) Catchup(o *testLedger, targetNextRound basics.Round) {
	l.mu.Lock()
	o.mu.Lock()

	startRound := l.nextRound
	for l.nextRound < targetNextRound {
		l.entries[l.nextRound] = o.entries[l.nextRound]
		l.certs[l.nextRound] = o.certs[l.nextRound]
		l.nextRound++
	}

	o.mu.Unlock()

	for r := startRound; r < l.nextRound; r++ {
		l.notify(r)
	}

	l.mu.Unlock()
}