Skip to content

Commit 32efa83

Browse files
authored
Update key registry schema for state proof support (#3156)
1 parent 25c8f26 commit 32efa83

File tree

2 files changed

+75
-50
lines changed

2 files changed

+75
-50
lines changed

data/account/participationRegistry.go

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -230,36 +230,35 @@ var (
230230
createKeysets = `CREATE TABLE Keysets (
231231
pk INTEGER PRIMARY KEY NOT NULL,
232232
233-
participationID BLOB,
234-
account BLOB,
233+
participationID BLOB NOT NULL,
234+
account BLOB NOT NULL,
235235
236-
firstValidRound INTEGER NOT NULL DEFAULT 0,
237-
lastValidRound INTEGER NOT NULL DEFAULT 0,
238-
keyDilution INTEGER NOT NULL DEFAULT 0,
236+
firstValidRound INTEGER NOT NULL,
237+
lastValidRound INTEGER NOT NULL,
238+
keyDilution INTEGER NOT NULL,
239239
240-
vrf BLOB --* msgpack encoding of ParticipationAccount.vrf
240+
vrf BLOB, --* msgpack encoding of ParticipationAccount.vrf
241+
stateProof BLOB --* msgpack encoding of ParticipationAccount.BlockProof
241242
)`
243+
242244
createRolling = `CREATE TABLE Rolling (
243245
pk INTEGER PRIMARY KEY NOT NULL,
244246
245-
lastVoteRound INTEGER NOT NULL DEFAULT 0,
246-
lastBlockProposalRound INTEGER NOT NULL DEFAULT 0,
247-
lastCompactCertificateRound INTEGER NOT NULL DEFAULT 0,
248-
effectiveFirstRound INTEGER NOT NULL DEFAULT 0,
249-
effectiveLastRound INTEGER NOT NULL DEFAULT 0,
247+
lastVoteRound INTEGER,
248+
lastBlockProposalRound INTEGER,
249+
lastCompactCertificateRound INTEGER,
250+
effectiveFirstRound INTEGER,
251+
effectiveLastRound INTEGER,
250252
251253
voting BLOB --* msgpack encoding of ParticipationAccount.voting
252-
253-
-- blockProof BLOB --* msgpack encoding of ParticipationAccount.BlockProof
254254
)`
255255

256-
/*
257-
createBlockProof = `CREATE TABLE BlockProofKeys (
258-
id INTEGER PRIMARY KEY,
259-
round INTEGER, --* committed round for this key
260-
key BLOB --* msgpack encoding of ParticipationAccount.BlockProof.SignatureAlgorithm
261-
)`
262-
*/
256+
createStateProof = `CREATE TABLE StateProofKeys (
257+
pk INTEGER NOT NULL, --* join with keyset to find key for a particular participation id
258+
round INTEGER NOT NULL, --* committed round for this key
259+
key BLOB NOT NULL, --* msgpack encoding of ParticipationAccount.BlockProof.SignatureAlgorithm
260+
PRIMARY KEY (pk, round)
261+
)`
263262
insertKeysetQuery = `INSERT INTO Keysets (participationID, account, firstValidRound, lastValidRound, keyDilution, vrf) VALUES (?, ?, ?, ?, ?, ?)`
264263
insertRollingQuery = `INSERT INTO Rolling (pk, voting) VALUES (?, ?)`
265264

@@ -299,6 +298,12 @@ func dbSchemaUpgrade0(ctx context.Context, tx *sql.Tx, newDatabase bool) error {
299298
return err
300299
}
301300

301+
// For performance reasons, state proofs are in a separate table.
302+
_, err = tx.Exec(createStateProof)
303+
if err != nil {
304+
return err
305+
}
306+
302307
return nil
303308
}
304309

@@ -663,18 +668,25 @@ func scanRecords(rows *sql.Rows) ([]ParticipationRecord, error) {
663668
var rawAccount []byte
664669
var rawVRF []byte
665670
var rawVoting []byte
671+
672+
var lastVote sql.NullInt64
673+
var lastBlockProposal sql.NullInt64
674+
var lastCompactCertificate sql.NullInt64
675+
var effectiveFirst sql.NullInt64
676+
var effectiveLast sql.NullInt64
677+
666678
err := rows.Scan(
667679
&rawParticipation,
668680
&rawAccount,
669681
&record.FirstValid,
670682
&record.LastValid,
671683
&record.KeyDilution,
672684
&rawVRF,
673-
&record.LastVote,
674-
&record.LastBlockProposal,
675-
&record.LastCompactCertificate,
676-
&record.EffectiveFirst,
677-
&record.EffectiveLast,
685+
&lastVote,
686+
&lastBlockProposal,
687+
&lastCompactCertificate,
688+
&effectiveFirst,
689+
&effectiveLast,
678690
&rawVoting,
679691
)
680692
if err != nil {
@@ -700,6 +712,27 @@ func scanRecords(rows *sql.Rows) ([]ParticipationRecord, error) {
700712
}
701713
}
702714

715+
// Check optional values.
716+
if lastVote.Valid {
717+
record.LastVote = basics.Round(lastVote.Int64)
718+
}
719+
720+
if lastBlockProposal.Valid {
721+
record.LastBlockProposal = basics.Round(lastBlockProposal.Int64)
722+
}
723+
724+
if lastCompactCertificate.Valid {
725+
record.LastCompactCertificate = basics.Round(lastCompactCertificate.Int64)
726+
}
727+
728+
if effectiveFirst.Valid {
729+
record.EffectiveFirst = basics.Round(effectiveFirst.Int64)
730+
}
731+
732+
if effectiveLast.Valid {
733+
record.EffectiveLast = basics.Round(effectiveLast.Int64)
734+
}
735+
703736
results = append(results, record)
704737
}
705738

data/account/participationRegistry_test.go

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,8 @@ func TestParticipation_InsertGet(t *testing.T) {
105105
}
106106

107107
// Check that Flush works, re-initialize cache and verify GetAll.
108-
err := registry.Flush()
109-
a.NoError(err)
110-
registry.initializeCache()
108+
a.NoError(registry.Flush())
109+
a.NoError(registry.initializeCache())
111110
results = registry.GetAll()
112111
a.Len(results, 2)
113112
for _, record := range results {
@@ -147,9 +146,8 @@ func TestParticipation_Delete(t *testing.T) {
147146
assertParticipation(t, p2, results[0])
148147

149148
// Check that result was persisted.
150-
err = registry.Flush()
151-
a.NoError(err)
152-
registry.initializeCache()
149+
a.NoError(registry.Flush())
150+
a.NoError(registry.initializeCache())
153151
results = registry.GetAll()
154152
a.Len(results, 1)
155153
assertParticipation(t, p2, results[0])
@@ -174,10 +172,8 @@ func TestParticipation_DeleteExpired(t *testing.T) {
174172
a.Len(registry.GetAll(), 5, "The first 5 should be deleted.")
175173

176174
// Check persisting. Verify by re-initializing the cache.
177-
err = registry.Flush()
178-
a.NoError(err)
179-
err = registry.initializeCache()
180-
a.NoError(err)
175+
a.NoError(registry.Flush())
176+
a.NoError(registry.initializeCache())
181177
a.Len(registry.GetAll(), 5, "The first 5 should be deleted.")
182178
}
183179

@@ -272,15 +268,11 @@ func TestParticipation_Record(t *testing.T) {
272268
a.NoError(err)
273269
}
274270

275-
all := registry.GetAll()
276-
a.NotNil(all)
271+
a.NotNil(registry.GetAll())
277272

278-
err := registry.Record(p.Parent, 1000, Vote)
279-
a.NoError(err)
280-
err = registry.Record(p.Parent, 2000, BlockProposal)
281-
a.NoError(err)
282-
err = registry.Record(p.Parent, 3000, CompactCertificate)
283-
a.NoError(err)
273+
a.NoError(registry.Record(p.Parent, 1000, Vote))
274+
a.NoError(registry.Record(p.Parent, 2000, BlockProposal))
275+
a.NoError(registry.Record(p.Parent, 3000, CompactCertificate))
284276

285277
// Verify that one and only one key was updated.
286278
test := func(registry ParticipationRegistry) {
@@ -300,11 +292,11 @@ func TestParticipation_Record(t *testing.T) {
300292
}
301293

302294
test(registry)
303-
registry.Flush()
295+
a.NoError(registry.Flush())
304296
a.Len(registry.dirty, 0)
305297

306298
// Re-initialize
307-
registry.initializeCache()
299+
a.NoError(registry.initializeCache())
308300
test(registry)
309301
}
310302

@@ -368,9 +360,9 @@ func TestParticipation_RecordMultipleUpdates(t *testing.T) {
368360
recordCopy.EffectiveLast = p2.LastValid
369361
registry.cache[p2.ID()] = recordCopy
370362
registry.dirty[p2.ID()] = struct{}{}
371-
registry.Flush()
363+
a.NoError(registry.Flush())
372364
a.Len(registry.dirty, 0)
373-
registry.initializeCache()
365+
a.NoError(registry.initializeCache())
374366

375367
// Verify bad state - both records are valid until round 3 million
376368
a.NotEqual(p.ID(), p2.ID())
@@ -538,7 +530,7 @@ func TestParticipation_NoKeyToUpdate(t *testing.T) {
538530
// TestParticipion_Blobs adds some secrets to the registry and makes sure the same ones are returned.
539531
func TestParticipion_Blobs(t *testing.T) {
540532
partitiontest.PartitionTest(t)
541-
a := assert.New(t)
533+
a := require.New(t)
542534
registry := getRegistry(t)
543535
defer registry.Close()
544536

@@ -574,7 +566,7 @@ func TestParticipion_Blobs(t *testing.T) {
574566
check(id)
575567

576568
// check the re-initialized object
577-
registry.initializeCache()
569+
a.NoError(registry.initializeCache())
578570
check(id)
579571
}
580572

@@ -619,7 +611,7 @@ func TestParticipion_EmptyBlobs(t *testing.T) {
619611
check(id)
620612

621613
// check the re-initialized object
622-
registry.initializeCache()
614+
a.NoError(registry.initializeCache())
623615
check(id)
624616
}
625617

0 commit comments

Comments
 (0)