summaryrefslogtreecommitdiff
path: root/data/account/participationRegistry_test.go
blob: 8cc5a4f1112d30edb9ed1201a3f709f7dd706274 (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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
// 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 account

import (
	"bytes"
	"context"
	"database/sql"
	"encoding/binary"
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"sync"
	"sync/atomic"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/crypto"
	"github.com/algorand/go-algorand/crypto/merklesignature"
	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/protocol"
	"github.com/algorand/go-algorand/test/partitiontest"
	"github.com/algorand/go-algorand/util/db"
)

var stateProofIntervalForTests = config.Consensus[protocol.ConsensusCurrentVersion].StateProofInterval

func getRegistry(t testing.TB) (registry *participationDB, dbfile string) {
	return getRegistryImpl(t, true, false)
}

func getRegistryImpl(t testing.TB, inMem bool, erasable bool) (registry *participationDB, dbName string) {
	var rootDB db.Pair
	var err error
	dbName = strings.Replace(t.Name(), "/", "_", -1)
	if erasable {
		require.False(t, inMem, "erasable registry can't be in-memory")
		rootDB, err = db.OpenErasablePair(dbName)
	} else {
		rootDB, err = db.OpenPair(dbName, inMem)
	}
	require.NoError(t, err)

	registry, err = makeParticipationRegistry(rootDB, logging.TestingLog(t))
	require.NoError(t, err)
	require.NotNil(t, registry)

	if inMem { // no files to clean up
		dbName = ""
	}
	return registry, dbName
}

func assertParticipation(t testing.TB, p Participation, pr ParticipationRecord) {
	require.Equal(t, p.FirstValid, pr.FirstValid)
	require.Equal(t, p.LastValid, pr.LastValid)
	require.Equal(t, p.KeyDilution, pr.KeyDilution)
	require.Equal(t, p.Parent, pr.Account)
	if p.StateProofSecrets != nil {
		require.Equal(t, p.StateProofSecrets.GetVerifier().Commitment[:], pr.StateProof.Commitment[:])
		require.Equal(t, p.StateProofSecrets.GetVerifier().KeyLifetime, pr.StateProof.KeyLifetime)
	}

}

func makeTestParticipation(a *require.Assertions, addrID int, first, last basics.Round, dilution uint64) Participation {
	return makeTestParticipationWithLifetime(a, addrID, first, last, dilution, uint64((last+1)/2))
}

func makeTestParticipationWithLifetime(a *require.Assertions, addrID int, first, last basics.Round, dilution uint64, keyLifetime uint64) Participation {
	a.True(first < last)

	// Generate sample of stateproof keys. because it might take time we will reduce the number always to get 2 keys
	stateProofSecrets, err := merklesignature.New(uint64(first), uint64(last), keyLifetime)
	a.NoError(err)

	// Generate part keys like in partGenerateCmd and FillDBWithParticipationKeys
	if dilution == 0 {
		dilution = DefaultKeyDilution(first, last)
	}

	// Compute how many distinct participation keys we should generate
	firstID := basics.OneTimeIDForRound(first, dilution)
	lastID := basics.OneTimeIDForRound(last, dilution)
	numBatches := lastID.Batch - firstID.Batch + 1

	// Generate them
	votingSecrets := crypto.GenerateOneTimeSignatureSecrets(firstID.Batch, numBatches)

	// Generate a new VRF key, which lives in the participation keys db
	vrf := crypto.GenerateVRFSecrets()

	p := Participation{
		FirstValid:        first,
		LastValid:         last,
		KeyDilution:       dilution,
		Voting:            votingSecrets,
		VRF:               vrf,
		StateProofSecrets: stateProofSecrets,
	}

	binary.LittleEndian.PutUint32(p.Parent[:], uint32(addrID))
	return p
}

func registryCloseTest(t testing.TB, registry *participationDB, dbfilePrefix string) {
	start := time.Now()
	registry.Close()
	duration := time.Since(start)
	assert.Less(t, uint64(duration), uint64(defaultTimeout))
	// clean up DB files
	if dbfilePrefix != "" {
		dbfiles, err := filepath.Glob(dbfilePrefix + "*")
		require.NoError(t, err)
		for _, f := range dbfiles {
			t.Log("removing", f)
			require.NoError(t, os.Remove(f))
		}
	}
}

// Insert participation records and make sure they can be fetched.
func TestParticipation_InsertGet(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	p := makeTestParticipation(a, 1, 1, 2, 3)
	p2 := makeTestParticipation(a, 2, 4, 5, 6)

	insertAndVerify := func(part Participation) {
		id, err := registry.Insert(part)
		a.NoError(err)
		a.Equal(part.ID(), id)

		record := registry.Get(part.ID())
		a.False(record.IsZero())
		assertParticipation(t, part, record)
	}

	// Verify inserting some records.
	insertAndVerify(p)
	insertAndVerify(p2)

	// Data should be available immediately
	results := registry.GetAll()
	a.Len(results, 2)
	for _, record := range results {
		if record.Account == p.Parent {
			assertParticipation(t, p, record)
		} else if record.Account == p2.Parent {
			assertParticipation(t, p2, record)
		} else {
			a.Fail("unexpected account")
		}
	}

	// Check that Flush works, re-initialize cache and verify GetAll.
	a.NoError(registry.Flush(defaultTimeout))
	a.NoError(registry.initializeCache())
	results = registry.GetAll()
	a.Len(results, 2)
	for _, record := range results {
		if record.Account == p.Parent {
			assertParticipation(t, p, record)
		} else if record.Account == p2.Parent {
			assertParticipation(t, p2, record)
		} else {
			a.Fail("unexpected account")
		}
	}
}

// Insert participation records and make sure they can be fetched.
func TestParticipation_InsertGetWithoutEmptyStateproof(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	p := Participation{
		FirstValid:  1,
		LastValid:   3,
		KeyDilution: 1,
		Voting:      &crypto.OneTimeSignatureSecrets{},
		VRF:         &crypto.VRFSecrets{},
	}

	binary.LittleEndian.PutUint32(p.Parent[:], uint32(1))

	insertAndVerify := func(part Participation) {
		id, err := registry.Insert(part)
		a.NoError(err)
		a.Equal(part.ID(), id)

		record := registry.Get(part.ID())
		a.False(record.IsZero())
		assertParticipation(t, part, record)
	}

	// Verify inserting some records.
	insertAndVerify(p)

	// Data should be available immediately
	results := registry.GetAll()
	if results[0].Account == p.Parent {
		assertParticipation(t, p, results[0])
		a.Nil(results[0].StateProof)
	} else {
		a.Fail("unexpected account")
	}
}

// Make sure a record can be deleted by id.
func TestParticipation_Delete(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)
	registry, dbfile := getRegistryImpl(t, false, true) // inMem=false, erasable=true
	defer registryCloseTest(t, registry, dbfile)

	p := makeTestParticipation(a, 1, 1, 2, 3)
	p2 := makeTestParticipation(a, 2, 4, 5, 6)

	id, err := registry.Insert(p)
	a.NoError(err)
	a.Equal(p.ID(), id)

	id, err = registry.Insert(p2)
	a.NoError(err)
	a.Equal(p2.ID(), id)

	err = registry.Delete(p.ID())
	a.NoError(err)

	results := registry.GetAll()
	a.Len(results, 1)
	assertParticipation(t, p2, results[0])

	// Check that result was persisted.
	a.NoError(registry.Flush(defaultTimeout))
	a.NoError(registry.initializeCache())
	results = registry.GetAll()
	a.Len(results, 1)
	assertParticipation(t, p2, results[0])
}

type testMessage string

func (m testMessage) ToBeHashed() (protocol.HashID, []byte) {
	return protocol.Message, []byte(m)
}

func TestParticipation_DeleteExpired(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)
	registry, dbfile := getRegistryImpl(t, false, true) // inMem=false, erasable=true
	defer registryCloseTest(t, registry, dbfile)

	keyDilution := 1
	for i := 10; i < 20; i++ {
		p := makeTestParticipation(a, i, 1, basics.Round(i), uint64(keyDilution))
		id, err := registry.Insert(p)
		a.NoError(err)
		a.Equal(p.ID(), id)
	}

	latestRound := basics.Round(15)
	err := registry.DeleteExpired(latestRound, config.Consensus[protocol.ConsensusCurrentVersion])
	a.NoError(err)

	checkExpired := func(getAll []ParticipationRecord) {
		a.Len(getAll, 5, "The first 5 should be deleted.")

		proto := config.Consensus[protocol.ConsensusCurrentVersion]
		for _, p := range getAll {
			// like in loadRoundParticipationKeys
			prfr := ParticipationRecordForRound{p}
			voting := prfr.VotingSigner()

			// count remaining batches (with keyDilution = 1)
			keysLeft := len(p.Voting.Offsets) + len(p.Voting.Batches)*keyDilution
			a.Equal(int(p.LastValid-latestRound), keysLeft)

			// attempt to sign old rounds (will log warning)
			ephID := basics.OneTimeIDForRound(basics.Round(latestRound), voting.KeyDilution(proto.DefaultKeyDilution))
			sig := voting.Sign(ephID, testMessage("hello"))
			a.Empty(sig)
		}
	}
	checkExpired(registry.GetAll())

	// Check persisting. Verify by re-initializing the cache.
	a.NoError(registry.Flush(defaultTimeout))
	a.NoError(registry.initializeCache())
	checkExpired(registry.GetAll())
}

func TestParticipation_CleanupTablesAfterDeleteExpired(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)
	registry, dbfile := getRegistryImpl(t, false, true) // inMem=false, erasable=true
	defer registryCloseTest(t, registry, dbfile)

	keyDilution := 1
	for i := 10; i < 20; i++ {
		p := makeTestParticipation(a, i, 1, basics.Round(i), uint64(keyDilution))
		id, err := registry.Insert(p)
		a.NoError(err)
		a.Equal(p.ID(), id)

		err = registry.AppendKeys(id, p.StateProofSecrets.GetAllKeys())
		a.NoError(err)
	}

	a.NoError(registry.Flush(defaultTimeout))

	latestRound := basics.Round(50)
	err := registry.DeleteExpired(latestRound, config.Consensus[protocol.ConsensusCurrentVersion])
	a.NoError(err)

	a.NoError(registry.Flush(defaultTimeout))
	var numOfRecords int
	// make sure tables are clean
	err = registry.store.Wdb.Atomic(func(ctx context.Context, tx *sql.Tx) error {
		row := tx.QueryRow(`select count(*) from Keysets`)
		err = row.Scan(&numOfRecords)
		if err != nil {
			return fmt.Errorf("unable to scan pk: %w", err)
		}
		return nil
	})

	a.NoError(err)
	a.Equal(0, numOfRecords)

	err = registry.store.Wdb.Atomic(func(ctx context.Context, tx *sql.Tx) error {
		row := tx.QueryRow(`select count(*) from Rolling`)
		err = row.Scan(&numOfRecords)
		if err != nil {
			return fmt.Errorf("unable to scan pk: %w", err)
		}
		return nil
	})
	a.NoError(err)
	a.Equal(0, numOfRecords)

	err = registry.store.Wdb.Atomic(func(ctx context.Context, tx *sql.Tx) error {
		row := tx.QueryRow(`select count(*) from stateproofkeys`)
		err = row.Scan(&numOfRecords)
		if err != nil {
			return fmt.Errorf("unable to scan pk: %w", err)
		}
		return nil
	})
	a.NoError(err)
	a.Equal(0, numOfRecords)
}

// Make sure the register function properly sets effective first/last for all effected records.
func TestParticipation_Register(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	// Overlapping keys.
	p := makeTestParticipation(a, 1, 250000, 3000000, 0)
	p2 := makeTestParticipation(a, 1, 200000, 4000000, 0)

	id, err := registry.Insert(p)
	a.NoError(err)
	a.Equal(p.ID(), id)

	id, err = registry.Insert(p2)
	a.NoError(err)
	a.Equal(p2.ID(), id)

	verifyEffectiveRound := func(id ParticipationID, first, last int) {
		record := registry.Get(id)
		a.False(record.IsZero())
		require.Equal(t, first, int(record.EffectiveFirst))
		require.Equal(t, last, int(record.EffectiveLast))
	}

	// Register the first key.
	err = registry.Register(p.ID(), 500000)
	a.NoError(err)
	verifyEffectiveRound(p.ID(), 500000, int(p.LastValid))

	// Register second key.
	err = registry.Register(p2.ID(), 2500000)
	a.NoError(err)
	verifyEffectiveRound(p.ID(), 500000, 2499999)
	verifyEffectiveRound(p2.ID(), 2500000, int(p2.LastValid))
}

// Test error when registering a non-existing participation ID.
func TestParticipation_RegisterInvalidID(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	p := makeTestParticipation(a, 0, 250000, 3000000, 0)

	err := registry.Register(p.ID(), 10000000)
	a.EqualError(err, ErrParticipationIDNotFound.Error())
}

// Test error attempting to register a key with an invalid range.
func TestParticipation_RegisterInvalidRange(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	p := makeTestParticipation(a, 0, 250000, 3000000, 0)

	id, err := registry.Insert(p)
	a.NoError(err)
	a.Equal(p.ID(), id)

	// Register the first key.
	err = registry.Register(p.ID(), 1000000000)
	a.EqualError(err, ErrInvalidRegisterRange.Error())
}

// Test the recording function.
func TestParticipation_Record(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	// Setup p
	p := makeTestParticipation(a, 1, 0, 3000000, 0)
	// Setup some other keys to make sure they are not updated.
	p2 := makeTestParticipation(a, 2, 0, 3000000, 0)
	p3 := makeTestParticipation(a, 3, 0, 3000000, 0)

	// Install and register all of the keys
	for _, part := range []Participation{p, p2, p3} {
		id, err := registry.Insert(part)
		a.NoError(err)
		a.Equal(part.ID(), id)
		err = registry.Register(part.ID(), 0)
		a.NoError(err)
	}

	a.NotNil(registry.GetAll())

	a.NoError(registry.Record(p.Parent, 1000, Vote))
	a.NoError(registry.Record(p.Parent, 2000, BlockProposal))
	a.NoError(registry.Record(p.Parent, 3000, StateProof))

	// Verify that one and only one key was updated.
	test := func(registry ParticipationRegistry) {
		records := registry.GetAll()
		a.Len(records, 3)
		for _, record := range records {
			if record.ParticipationID == p.ID() {
				require.Equal(t, 1000, int(record.LastVote))
				require.Equal(t, 2000, int(record.LastBlockProposal))
				require.Equal(t, 3000, int(record.LastStateProof))
			} else {
				require.Equal(t, 0, int(record.LastVote))
				require.Equal(t, 0, int(record.LastBlockProposal))
				require.Equal(t, 0, int(record.LastStateProof))
			}
		}
	}

	test(registry)
	a.NoError(registry.Flush(defaultTimeout))
	a.Len(registry.dirty, 0)

	// Re-initialize
	a.NoError(registry.initializeCache())
	test(registry)
}

// Test that attempting to record an invalid action generates an error.
func TestParticipation_RecordInvalidActionAndOutOfRange(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	p := makeTestParticipation(a, 1, 0, 3000000, 0)
	id, err := registry.Insert(p)
	a.NoError(err)
	err = registry.Register(id, 0)
	a.NoError(err)

	err = registry.Record(p.Parent, 0, ParticipationAction(9000))
	a.EqualError(err, ErrUnknownParticipationAction.Error())

	err = registry.Record(p.Parent, 3000000, ParticipationAction(9000))
	a.EqualError(err, ErrUnknownParticipationAction.Error())

	err = registry.Record(p.Parent, 3000001, ParticipationAction(9000))
	a.EqualError(err, ErrActiveKeyNotFound.Error())
}

func TestParticipation_RecordNoKey(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	err := registry.Record(basics.Address{}, 0, Vote)
	a.EqualError(err, ErrActiveKeyNotFound.Error())
}

// Test that an error is generated if the record function updates multiple records.
// This would only happen if the DB was in an inconsistent state.
func TestParticipation_RecordMultipleUpdates(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	// We'll test that recording at this round fails because both keys are active
	testRound := basics.Round(5000)

	p := makeTestParticipation(a, 1, 0, 3000000, 0)
	p2 := makeTestParticipation(a, 1, 1, 3000000, 0)

	_, err := registry.Insert(p)
	a.NoError(err)
	_, err = registry.Insert(p2)
	a.NoError(err)
	err = registry.Register(p.ID(), p.FirstValid)
	a.NoError(err)

	// Force the DB to have 2 active keys for one account by tampering with the private cache variable
	recordCopy := registry.cache[p2.ID()]
	recordCopy.EffectiveFirst = p2.FirstValid
	recordCopy.EffectiveLast = p2.LastValid
	registry.cache[p2.ID()] = recordCopy
	registry.dirty[p2.ID()] = struct{}{}
	a.NoError(registry.Flush(defaultTimeout))
	a.Len(registry.dirty, 0)
	a.NoError(registry.initializeCache())

	// Verify bad state - both records are valid until round 3 million
	a.NotEqual(p.ID(), p2.ID())
	recordTest := make([]ParticipationRecord, 0)

	recordP := registry.Get(p.ID())
	a.False(recordP.IsZero())
	recordTest = append(recordTest, recordP)

	recordP2 := registry.Get(p2.ID())
	a.False(recordP2.IsZero())
	recordTest = append(recordTest, recordP2)

	// Make sure both accounts are active for the test round
	for _, record := range recordTest {
		a.True(recordActive(record, testRound), "both records should be active")
	}

	err = registry.Record(p.Parent, testRound, Vote)
	a.EqualError(err, ErrMultipleValidKeys.Error())
}

func TestParticipation_MultipleInsertError(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	p := makeTestParticipation(a, 1, 1, 2, 3)

	_, err := registry.Insert(p)
	a.NoError(err)
	_, err = registry.Insert(p)
	a.Error(err, ErrAlreadyInserted.Error())
}

// This is a contrived test on every level. To workaround errors we setup the
// DB and cache in ways that are impossible with public methods.
//
// Basically multiple records with the same ParticipationID are a big no-no and
// it should be detected as quickly as possible.
func TestParticipation_RecordMultipleUpdates_DB(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)
	registry, _ := getRegistry(t)

	p := makeTestParticipation(a, 1, 1, 2000000, 0)
	id := p.ID()

	// Insert the same record twice
	// Pretty much copied from the Insert function without error checking.
	err := registry.store.Wdb.Atomic(func(ctx context.Context, tx *sql.Tx) error {
		for i := 0; i < 2; i++ {
			record := p
			_, err := tx.Exec(
				insertKeysetQuery,
				id[:],
				record.Parent[:],
				record.FirstValid,
				record.LastValid,
				record.KeyDilution,
				nil,
				nil)
			if err != nil {
				return fmt.Errorf("unable to insert keyset: %w", err)
			}

			// Fetch primary key
			var pk int
			row := tx.QueryRow(selectLastPK, id[:])
			err = row.Scan(&pk)
			if err != nil {
				return fmt.Errorf("unable to scan pk: %w", err)
			}

			// Create Rolling entry
			_, err = tx.Exec(`INSERT INTO Rolling (pk, effectiveFirstRound, effectiveLastRound) VALUES (?, ?, ?)`, pk, 1, 200000)
			if err != nil {
				return fmt.Errorf("unable insert rolling: %w", err)
			}

			var num int
			row = tx.QueryRow(`SELECT COUNT(*) FROM Keysets WHERE participationID=?`, id[:])
			err = row.Scan(&num)
			if err != nil {
				return fmt.Errorf("unable to scan pk: %w", err)
			}
		}

		return nil
	})

	a.NoError(err)

	// Now that the DB has multiple records for one participation ID, check that all the methods notice.

	// Initializing the cache
	err = registry.initializeCache()
	a.EqualError(err, ErrMultipleKeysForID.Error())

	// Registering the ID - No error because it is already registered so we don't try to re-register.
	registry.cache[id] = ParticipationRecord{
		ParticipationID: id,
		Account:         p.Parent,
		FirstValid:      p.FirstValid,
		LastValid:       p.LastValid,
		KeyDilution:     p.KeyDilution,
		EffectiveFirst:  p.FirstValid,
		EffectiveLast:   p.LastValid,
	}
	err = registry.Register(id, 1)
	a.NoError(err)

	// Clear the first/last so that the no-op registration can't be detected
	record := registry.cache[id]
	record.EffectiveFirst = 0
	record.EffectiveLast = 0
	registry.cache[id] = record

	err = registry.Register(id, 1)
	a.NoError(err)
	err = registry.Flush(defaultTimeout)
	a.Error(err)
	a.Contains(err.Error(), "unable to disable old key")
	a.EqualError(errors.Unwrap(err), ErrMultipleKeysForID.Error())

	// Flushing changes detects that multiple records are updated
	registry.dirty[id] = struct{}{}
	err = registry.Flush(defaultTimeout)
	a.EqualError(err, ErrMultipleKeysForID.Error())
	a.Len(registry.dirty, 1)

	err = registry.Flush(defaultTimeout)
	a.EqualError(err, ErrMultipleKeysForID.Error())

	// Make sure the error message is logged when closing the registry.
	var logOutput strings.Builder
	registry.log.SetOutput(&logOutput)
	registry.Close()
	a.Contains(logOutput.String(), "participationDB unhandled error during Close/Flush")
	a.Contains(logOutput.String(), ErrMultipleKeysForID.Error())
}

func TestParticipation_NoKeyToUpdate(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := assert.New(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	registry.store.Wdb.Atomic(func(ctx context.Context, tx *sql.Tx) error {
		record := ParticipationRecord{
			ParticipationID: ParticipationID{},
			Account:         basics.Address{},
			FirstValid:      1,
			LastValid:       2,
			KeyDilution:     3,
			EffectiveFirst:  4,
			EffectiveLast:   5,
		}
		err := updateRollingFields(ctx, tx, record)
		a.EqualError(err, ErrNoKeyForID.Error())
		return nil
	})
}

// TestParticipion_Blobs adds some secrets to the registry and makes sure the same ones are returned.
func TestParticipion_Blobs(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	access, err := db.MakeAccessor(t.Name()+"_writetest_root", false, true)
	if err != nil {
		panic(err)
	}
	root, err := GenerateRoot(access)
	access.Close()
	a.NoError(err)

	access, err = db.MakeAccessor(t.Name()+"_writetest", false, true)
	if err != nil {
		panic(err)
	}
	part, err := FillDBWithParticipationKeys(access, root.Address(), 0, 101, config.Consensus[protocol.ConsensusCurrentVersion].DefaultKeyDilution)
	access.Close()
	a.NoError(err)

	check := func(id ParticipationID) {
		record := registry.Get(id)
		a.NotEqual(ParticipationRecord{}, record)
		a.Equal(id, record.ParticipationID)
		a.Equal(part.VRF, record.VRF)
		a.Equal(part.Voting.Snapshot(), record.Voting.Snapshot())
	}

	id, err := registry.Insert(part.Participation)
	a.NoError(err)
	a.NoError(registry.Flush(defaultTimeout))
	a.Equal(id, part.ID())
	// check the initial caching
	check(id)

	// check the re-initialized object
	a.NoError(registry.initializeCache())
	check(id)
}

// TestParticipion_EmptyBlobs makes sure empty blobs are set to nil
func TestParticipion_EmptyBlobs(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := assert.New(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	access, err := db.MakeAccessor(t.Name()+"_writetest_root", false, true)
	if err != nil {
		panic(err)
	}
	root, err := GenerateRoot(access)
	access.Close()
	a.NoError(err)

	access, err = db.MakeAccessor(t.Name()+"_writetest", false, true)
	if err != nil {
		panic(err)
	}
	part, err := FillDBWithParticipationKeys(access, root.Address(), 0, 101, config.Consensus[protocol.ConsensusCurrentVersion].DefaultKeyDilution)
	access.Close()
	a.NoError(err)
	part.VRF = nil
	part.Voting = nil

	check := func(id ParticipationID) {
		record := registry.Get(id)
		a.NotEqual(ParticipationRecord{}, record)
		a.Equal(id, record.ParticipationID)
		a.True(record.VRF.MsgIsZero())
		a.True(record.Voting.MsgIsZero())
	}

	id, err := registry.Insert(part.Participation)
	a.NoError(err)
	a.NoError(registry.Flush(defaultTimeout))
	a.Equal(id, part.ID())
	// check the initial caching
	check(id)

	// check the re-initialized object
	a.NoError(registry.initializeCache())
	check(id)
}

func TestRegisterUpdatedEvent(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	p := makeTestParticipation(a, 1, 1, 2, 3)
	p2 := makeTestParticipation(a, 2, 4, 5, 6)

	id1, err := registry.Insert(p)
	a.NoError(err)
	a.Equal(p.ID(), id1)

	id2, err := registry.Insert(p2)
	a.NoError(err)
	a.Equal(p2.ID(), id2)

	record1 := registry.Get(id1)
	a.False(record1.IsZero())
	record2 := registry.Get(id2)
	a.False(record2.IsZero())

	// Delete the second one to make sure it can't be updated.
	a.NoError(registry.Delete(id2))
	a.NoError(registry.Flush(defaultTimeout))

	// Ignore optional error
	updates := make(map[ParticipationID]updatingParticipationRecord)
	updates[id1] = updatingParticipationRecord{
		ParticipationRecord: record1,
		required:            true,
	}
	updates[id2] = updatingParticipationRecord{
		ParticipationRecord: record2,
		required:            false,
	}

	registry.writeQueue <- makeOpRequest(&registerOp{updates})

	a.NoError(registry.Flush(defaultTimeout))

	// This time, make it required and we should have an error
	updates[id2] = updatingParticipationRecord{
		ParticipationRecord: record2,
		required:            true,
	}

	registry.writeQueue <- makeOpRequest(&registerOp{updates})

	err = registry.Flush(defaultTimeout)
	a.Contains(err.Error(), "unable to disable old key when registering")
	a.Contains(err.Error(), ErrNoKeyForID.Error())
}

// TestFlushDeadlock reproduced a deadlock when calling Flush repeatedly. This test reproduced the deadlock and
// verifies the fix.
func TestFlushDeadlock(t *testing.T) {
	var wg sync.WaitGroup

	partitiontest.PartitionTest(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	spam := func() {
		defer wg.Done()
		timeout := time.After(time.Second)
		for {
			select {
			case <-timeout:
				return
			default:
				// If there is a deadlock, this timeout will trigger.
				assert.NoError(t, registry.Flush(2*time.Second))
			}
		}
	}

	// Start spammers.
	for i := 0; i < 100; i++ {
		wg.Add(1)
		go spam()
	}

	wg.Wait()
}

func TestAddStateProofKeys(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	// Install a key to add StateProof keys.
	max := uint64(20)
	p := makeTestParticipationWithLifetime(a, 1, 0, basics.Round(max), 3, 3)
	id, err := registry.Insert(p)
	a.NoError(err)
	a.Equal(p.ID(), id)

	// Wait for async DB operations to finish.
	err = registry.Flush(10 * time.Second)
	a.NoError(err)

	signer, err := merklesignature.New(1, max, 3)
	a.NoError(err)
	// Initialize keys array.
	keys := signer.GetAllKeys()

	err = registry.AppendKeys(id, keys)
	a.NoError(err)

	// Wait for async DB operations to finish.
	err = registry.Flush(10 * time.Second)
	a.NoError(err)

	_, err = registry.GetStateProofSecretsForRound(id, basics.Round(1))
	a.Error(err)
	_, err = registry.GetStateProofSecretsForRound(id, basics.Round(2))
	a.Error(err)

	// Make sure we're able to fetch the same data that was put in.
	for i := uint64(3); i < max; i++ {
		r, err := registry.GetStateProofSecretsForRound(id, basics.Round(i))
		a.NoError(err)

		if r.StateProofSecrets != nil {
			j := i/3 - 1 // idx in keys array

			a.Equal(*keys[j].Key, *r.StateProofSecrets.SigningKey)

			keyFirstValidRound, err := r.StateProofSecrets.FirstRoundInKeyLifetime()
			a.NoError(err)

			a.Equal(keys[j].Round, keyFirstValidRound)
		}
	}
}

func TestGetRoundSecretsWithNilStateProofVerifier(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := assert.New(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	access, err := db.MakeAccessor(t.Name()+"_stateprooftest", false, true)
	if err != nil {
		panic(err)
	}
	root, err := GenerateRoot(access)
	p, err := FillDBWithParticipationKeys(access, root.Address(), 0, basics.Round(stateProofIntervalForTests*2), 3)
	access.Close()
	a.NoError(err)

	// Install a key for testing
	id, err := registry.Insert(p.Participation)
	a.NoError(err)

	// ensuring that GetStateProof will receive from cache a participationRecord without StateProof field.
	prt := registry.cache[id]
	prt.StateProof = nil
	registry.cache[id] = prt

	a.NoError(registry.Flush(defaultTimeout))

	_, err = registry.GetStateProofSecretsForRound(id, basics.Round(stateProofIntervalForTests)-1)
	a.ErrorIs(err, ErrStateProofVerifierNotFound)
}

func TestSecretNotFound(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	// Install a key for testing
	p := makeTestParticipation(a, 1, 0, 2, 3)
	id, err := registry.Insert(p)
	a.NoError(err)
	a.Equal(p.ID(), id)

	_, err = registry.GetForRound(id, basics.Round(2))
	a.NoError(err)

	_, err = registry.GetForRound(id, basics.Round(100))
	a.ErrorIs(err, ErrRequestedRoundOutOfRange)
}

func TestAddingSecretTwice(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := assert.New(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	access, err := db.MakeAccessor(t.Name()+"_stateprooftest", false, true)
	if err != nil {
		panic(err)
	}
	root, err := GenerateRoot(access)
	p, err := FillDBWithParticipationKeys(access, root.Address(), 0, basics.Round(stateProofIntervalForTests*2), 3)
	access.Close()
	a.NoError(err)

	// Install a key for testing
	id, err := registry.Insert(p.Participation)
	a.NoError(err)
	a.Equal(p.ID(), id)

	// Append key
	var keys StateProofKeys

	keysRound := merklesignature.KeyRoundPair{Round: stateProofIntervalForTests, Key: p.StateProofSecrets.GetKey(stateProofIntervalForTests)}
	keys = append(keys, keysRound)

	err = registry.AppendKeys(id, keys)
	a.NoError(err)

	// The error doesn't happen until the data persists.
	err = registry.AppendKeys(id, keys)
	a.NoError(err)

	err = registry.Flush(10 * time.Second)
	a.Error(err)
	a.EqualError(err, "unable to execute append keys: UNIQUE constraint failed: StateProofKeys.pk, StateProofKeys.round")
}

func TestGetRoundSecretsWithoutStateProof(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := assert.New(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	access, err := db.MakeAccessor(t.Name()+"_stateprooftest", false, true)
	if err != nil {
		panic(err)
	}
	root, err := GenerateRoot(access)
	p, err := FillDBWithParticipationKeys(access, root.Address(), 0, basics.Round(stateProofIntervalForTests*2), 3)
	access.Close()
	a.NoError(err)

	// Install a key for testing
	id, err := registry.Insert(p.Participation)
	a.NoError(err)

	a.NoError(registry.Flush(defaultTimeout))

	partPerRound, err := registry.GetStateProofSecretsForRound(id, 1)
	a.Error(err)

	partPerRound, err = registry.GetStateProofSecretsForRound(id, basics.Round(stateProofIntervalForTests))
	a.Error(err)

	// Append key
	keys := make(StateProofKeys, 1)
	keys[0] = merklesignature.KeyRoundPair{Round: stateProofIntervalForTests, Key: p.StateProofSecrets.GetKey(stateProofIntervalForTests)}

	err = registry.AppendKeys(id, keys)
	a.NoError(err)

	a.NoError(registry.Flush(defaultTimeout))

	partPerRound, err = registry.GetStateProofSecretsForRound(id, basics.Round(stateProofIntervalForTests)-1)
	a.Error(err)

	partPerRound, err = registry.GetStateProofSecretsForRound(id, basics.Round(stateProofIntervalForTests))
	a.NoError(err)
	a.NotNil(partPerRound.StateProofSecrets)

	a.Equal(*partPerRound.StateProofSecrets.SigningKey, *keys[0].Key)
	a.Equal(stateProofIntervalForTests, keys[0].Round)
}

type keypairs []merklesignature.KeyRoundPair

func (k keypairs) findPairForSpecificRound(round uint64) merklesignature.KeyRoundPair {
	for _, pair := range k {
		if pair.Round == round {
			return pair
		}
	}
	return merklesignature.KeyRoundPair{}
}

func TestDeleteStateProofKeys(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	// Install a key to add StateProof keys.
	maxRound := uint64(20)
	p := makeTestParticipationWithLifetime(a, 1, 0, basics.Round(maxRound), 3, 4)
	id, err := registry.Insert(p)
	a.NoError(err)
	a.Equal(p.ID(), id)

	// Wait for async DB operations to finish.
	a.NoError(registry.Flush(10 * time.Second))

	keys := keypairs(p.StateProofSecrets.GetAllKeys())

	a.NoError(registry.AppendKeys(id, StateProofKeys(keys)))

	// Wait for async DB operations to finish.
	a.NoError(registry.Flush(10 * time.Second))

	// Make sure we're able to fetch the same data that was put in.
	for i := uint64(4); i < maxRound; i += 4 {
		r, err := registry.GetStateProofSecretsForRound(id, basics.Round(i))
		a.NoError(err)

		a.Equal(keys.findPairForSpecificRound(i).Key, r.StateProofSecrets.SigningKey)
	}

	removeKeysRound := basics.Round(maxRound / 2)
	a.NoError(registry.DeleteStateProofKeys(id, removeKeysRound))

	a.NoError(registry.Flush(10 * time.Second))

	// verify that the db does not contain any state proof key with round less than 10

	registry.store.Rdb.Atomic(func(ctx context.Context, tx *sql.Tx) error {
		var pk int
		a.NoError(tx.QueryRow(selectPK, id[:]).Scan(&pk))

		// make certain keys below the cutting round do not exist in the db.
		var num int
		a.NoError(
			tx.QueryRow(
				"SELECT COUNT(*) FROM StateProofKeys where pk=? AND round <=?",
				pk,
				removeKeysRound,
			).Scan(&num),
		)
		a.Zero(num)

		// make certain keys above the cutting round exist in the db.
		a.NoError(
			tx.QueryRow(
				"SELECT COUNT(*) FROM StateProofKeys where pk=? AND round >?",
				pk,
				removeKeysRound,
			).Scan(&num),
		)

		// includes removeKeysRound
		a.Equal(int(maxRound)/4-int(removeKeysRound)/4, num) // 1 DELETED 1 NOT
		return nil
	})
}

// test that sets up an error that should come up while flushing, and ensures that flush resets the last error
func TestFlushResetsLastError(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := assert.New(t)
	registry, dbfile := getRegistry(t)
	defer registryCloseTest(t, registry, dbfile)

	access, err := db.MakeAccessor(t.Name()+"_stateprooftest", false, true)
	a.NoError(err)

	root, err := GenerateRoot(access)
	p, err := FillDBWithParticipationKeys(access, root.Address(), 0, basics.Round(stateProofIntervalForTests*2), 3)
	access.Close()
	a.NoError(err)

	// Install a key for testing
	id, err := registry.Insert(p.Participation)
	a.NoError(err)
	a.Equal(p.ID(), id)

	// Append key
	var keys StateProofKeys

	keysRound := merklesignature.KeyRoundPair{Round: stateProofIntervalForTests, Key: p.StateProofSecrets.GetKey(stateProofIntervalForTests)}
	keys = append(keys, keysRound)

	err = registry.AppendKeys(id, keys)
	a.NoError(err)

	// The error doesn't happen until the data persists.
	err = registry.AppendKeys(id, keys)
	a.NoError(err)

	a.Error(registry.Flush(10 * time.Second))
	a.NoError(registry.Flush(10 * time.Second))
}

// TestParticipationDB_Locking tries fetching StateProof keys from the DB while the Rolling table is being updated.
// Makes sure the table is not locked for reading while a different one is locked for writing.
func TestParticipationDB_Locking(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)

	dbName := strings.Replace(t.Name(), "/", "_", -1)

	dbpair, err := db.OpenErasablePair(dbName + ".sqlite3")
	a.NoError(err)

	var bufNewLogger bytes.Buffer
	log := logging.NewLogger()
	log.SetLevel(logging.Warn)
	log.SetOutput(&bufNewLogger)
	dbpair.Rdb.SetLogger(log)

	registry, err := makeParticipationRegistry(dbpair, logging.TestingLog(t))
	require.NoError(t, err)
	require.NotNil(t, registry)

	defer registryCloseTest(t, registry, dbName)

	var id2 ParticipationID
	for i := 0; i < 3; i++ {
		part := makeTestParticipation(a, 1, 0, 511, config.Consensus[protocol.ConsensusCurrentVersion].DefaultKeyDilution)
		id, err := registry.Insert(part)
		if i == 0 {
			id2 = id
		}
		a.NoError(err)
		a.NoError(registry.AppendKeys(id, part.StateProofSecrets.GetAllKeys()))
		a.NoError(registry.Flush(defaultTimeout))
		a.Equal(id, part.ID())
	}

	var wg sync.WaitGroup
	wg.Add(1)

	var flushCount int32
	const targetFlushes = 5
	go func() {
		for i := 0; i < 25; i++ {
			registry.DeleteExpired(basics.Round(i), config.Consensus[protocol.ConsensusCurrentVersion])
			registry.Flush(defaultTimeout)
			if atomic.LoadInt32(&flushCount) < targetFlushes {
				atomic.AddInt32(&flushCount, 1)
			}
		}
		wg.Done()
	}()

	for i := 0; i < 25; i++ {
	repeat:
		// to not start lookup until deleted some keys
		if atomic.LoadInt32(&flushCount) < targetFlushes {
			time.Sleep(time.Second)
			goto repeat
		}
		_, err = registry.GetStateProofSecretsForRound(id2, basics.Round(256))
		// The error we're trying to avoid is "database is locked", since we're reading from StateProofKeys table,
		// while the main thread is updating the Rolling table.
		a.NoError(err)
		time.Sleep(100 * time.Millisecond)
	}

	warnings := bufNewLogger.String()
	deadlineCount := strings.Count(warnings, "tx surpassed expected deadline")
	a.Empty(deadlineCount, fmt.Sprintf("found %d messages 'tx surpassed expected deadline' but expected 0", deadlineCount))
	wg.Wait()
}

func TestParticipationDBInstallWhileReading(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)

	if testing.Short() {
		t.Skip()
	}

	dbName := strings.Replace(t.Name(), "/", "_", -1)

	dbpair, err := db.OpenErasablePair(dbName + ".sqlite3")
	a.NoError(err)

	registry, err := makeParticipationRegistry(dbpair, logging.TestingLog(t))
	require.NoError(t, err)
	require.NotNil(t, registry)
	defer registryCloseTest(t, registry, dbName)

	var sampledPartID ParticipationID
	for i := 0; i < 3; i++ {
		part := makeTestParticipation(a, 1, 0, 511, config.Consensus[protocol.ConsensusCurrentVersion].DefaultKeyDilution)
		id, err := registry.Insert(part)
		if i == 0 {
			sampledPartID = id
		}
		a.NoError(err)
		a.NoError(registry.AppendKeys(id, part.StateProofSecrets.GetAllKeys()))
		a.NoError(registry.Flush(defaultTimeout))
		a.Equal(id, part.ID())
	}

	appendedKeys := make(chan struct{})
	newPart := makeTestParticipationWithLifetime(a, 1, 0, 3000000, config.Consensus[protocol.ConsensusCurrentVersion].DefaultKeyDilution, merklesignature.KeyLifetimeDefault)
	go func() {
		id, err := registry.Insert(newPart)
		a.NoError(err)
		a.NoError(registry.AppendKeys(id, newPart.StateProofSecrets.GetAllKeys()))
		appendedKeys <- struct{}{}
		a.NoError(registry.Flush(defaultTimeout))
		a.Equal(id, newPart.ID())
	}()

	<-appendedKeys // Makes sure we start fetching keys after the append keys operation has already started
	for i := 0; i < 50; i++ {
		_, err = registry.GetStateProofSecretsForRound(sampledPartID, basics.Round(256))
		// The error we're trying to avoid is "database is locked", since we're reading from StateProofKeys table,
		// while a different go routine is installing new keys.
		a.NoError(err)
	}
}

// based on BenchmarkOldKeysDeletion
func BenchmarkDeleteExpired(b *testing.B) {
	for _, erasable := range []bool{true, false} {
		b.Run(fmt.Sprintf("erasable=%v", erasable), func(b *testing.B) {
			a := require.New(b)

			registry, dbfile := getRegistryImpl(b, false, erasable) // set inMem=false
			defer func() {
				registryCloseTest(b, registry, dbfile)
			}()

			// make participation key
			lastValid := 3000000
			keyDilution := 10000
			if kd, err := strconv.Atoi(os.Getenv("DILUTION")); err == nil { // allow setting key dilution via env var
				keyDilution = kd
			}
			if lv, err := strconv.Atoi(os.Getenv("LASTVALID")); err == nil { // allow setting last valid via env var
				lastValid = lv
			}
			var part Participation

			numKeys := 1
			if nk, err := strconv.Atoi(os.Getenv("NUMKEYS")); err == nil { // allow setting numKeys via env var
				numKeys = nk
			}
			for i := 0; i < numKeys; i++ {
				if os.Getenv("SLOWKEYS") == "" {
					// makeTestParticipation makes small state proof secrets to save time
					b.Log("making fast part key", i, "for firstValid 0 lastValid", lastValid, "dilution", keyDilution)
					part = makeTestParticipation(a, i+1, 0, basics.Round(lastValid), uint64(keyDilution))
					a.NotNil(part)
				} else {
					// generate key the same way as BenchmarkOldKeysDeletion
					var rootAddr basics.Address
					crypto.RandBytes(rootAddr[:])

					ppartDB, err := db.MakeErasableAccessor("bench_part")
					a.NoError(err)
					a.NotNil(ppartDB)
					defer func() {
						os.Remove("bench_part")
					}()

					b.Log("making part key", i, "for firstValid 0 lastValid", lastValid, "dilution", keyDilution)
					ppart, err := FillDBWithParticipationKeys(ppartDB, rootAddr, 0, basics.Round(lastValid), uint64(keyDilution))
					ppartDB.Close()
					a.NoError(err)
					part = ppart.Participation
				}

				// insertAndVerify new registry key
				id, err := registry.Insert(part)
				a.NoError(err)
				a.Equal(part.ID(), id)
				record := registry.Get(part.ID())
				a.False(record.IsZero())
				assertParticipation(b, part, record)
			}

			results := registry.GetAll()
			a.Len(results, numKeys, "registry.GetAll() should return %d keys, but instead returned %v", numKeys, len(results))

			// run N rounds of DeleteExpired + Flush
			var err error
			proto := config.Consensus[protocol.ConsensusCurrentVersion]
			b.Log("starting DeleteExpired benchmark up to round", b.N)
			b.ResetTimer()
			for i := 0; i < b.N; i++ {
				err = registry.DeleteExpired(basics.Round(i), proto)
				if err != nil {
					break
				}
				err = registry.Flush(defaultTimeout)
				if err != nil {
					break
				}
			}
			b.StopTimer()
			a.NoError(err)
		})
	}
}