Skip to content

Commit 504cbc8

Browse files
authored
Add ParticipationRegistry Insert/Delete usage in node. (#2810)
1 parent e9fc89c commit 504cbc8

File tree

8 files changed

+203
-117
lines changed

8 files changed

+203
-117
lines changed

data/account/msgp_gen.go

Lines changed: 44 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

data/account/msgp_gen_test.go

Lines changed: 10 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

data/account/participation.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,31 @@ type Participation struct {
5757
KeyDilution uint64
5858
}
5959

60-
// participationIDData is for msgpack encoding the participation data.
61-
type participationIDData struct {
60+
// ParticipationKeyIdentity is for msgpack encoding the participation data.
61+
type ParticipationKeyIdentity struct {
6262
_struct struct{} `codec:",omitempty,omitemptyarray"`
6363

64-
Parent basics.Address `codec:"addr"`
65-
VRFSK crypto.VrfPrivkey `codec:"vrfsk"`
66-
FirstValid basics.Round `codec:"fv"`
67-
LastValid basics.Round `codec:"lv"`
68-
KeyDilution uint64 `codec:"kd"`
64+
Parent basics.Address `codec:"addr"`
65+
VRFSK crypto.VrfPrivkey `codec:"vrfsk"`
66+
VoteID crypto.OneTimeSignatureVerifier `codec:"vote-id"`
67+
FirstValid basics.Round `codec:"fv"`
68+
LastValid basics.Round `codec:"lv"`
69+
KeyDilution uint64 `codec:"kd"`
6970
}
7071

7172
// ToBeHashed implements the Hashable interface.
72-
func (id *participationIDData) ToBeHashed() (protocol.HashID, []byte) {
73+
func (id *ParticipationKeyIdentity) ToBeHashed() (protocol.HashID, []byte) {
7374
return protocol.ParticipationKeys, protocol.Encode(id)
7475
}
7576

76-
// ParticipationID computes a ParticipationID.
77-
func (part Participation) ParticipationID() ParticipationID {
78-
idData := participationIDData{
77+
// ID creates a ParticipationID hash from the identity file.
78+
func (id ParticipationKeyIdentity) ID() ParticipationID {
79+
return ParticipationID(crypto.HashObj(&id))
80+
}
81+
82+
// ID computes a ParticipationID.
83+
func (part Participation) ID() ParticipationID {
84+
idData := ParticipationKeyIdentity{
7985
Parent: part.Parent,
8086
FirstValid: part.FirstValid,
8187
LastValid: part.LastValid,
@@ -84,6 +90,9 @@ func (part Participation) ParticipationID() ParticipationID {
8490
if part.VRF != nil {
8591
copy(idData.VRFSK[:], part.VRF.SK[:])
8692
}
93+
if part.Voting != nil {
94+
copy(idData.VoteID[:], part.Voting.OneTimeSignatureVerifier[:])
95+
}
8796

8897
return ParticipationID(crypto.HashObj(&idData))
8998
}

data/account/participationRegistry.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ type ParticipationRegistry interface {
126126
// Delete removes a record from storage.
127127
Delete(id ParticipationID) error
128128

129+
// DeleteExpired removes all records from storage which are expired on the given round.
130+
DeleteExpired(round basics.Round) error
131+
129132
// Get a participation record.
130133
Get(id ParticipationID) ParticipationRecord
131134

@@ -283,9 +286,9 @@ func (db *participationDB) Insert(record Participation) (id ParticipationID, err
283286
db.mutex.Lock()
284287
defer db.mutex.Unlock()
285288

286-
id = record.ParticipationID()
289+
id = record.ID()
287290
if _, ok := db.cache[id]; ok {
288-
return ParticipationID{}, ErrAlreadyInserted
291+
return id, ErrAlreadyInserted
289292
}
290293

291294
err = db.store.Wdb.Atomic(func(ctx context.Context, tx *sql.Tx) error {
@@ -387,6 +390,19 @@ func (db *participationDB) Delete(id ParticipationID) error {
387390
return err
388391
}
389392

393+
func (db *participationDB) DeleteExpired(round basics.Round) error {
394+
// This could be optimized to delete everything with one query.
395+
for _, v := range db.GetAll() {
396+
if v.LastValid < round {
397+
err := db.Delete(v.ParticipationID)
398+
if err != nil {
399+
return err
400+
}
401+
}
402+
}
403+
return nil
404+
}
405+
390406
// scanRecords is a helper to manage scanning participation records.
391407
func scanRecords(rows *sql.Rows) ([]ParticipationRecord, error) {
392408
results := make([]ParticipationRecord, 0)

0 commit comments

Comments
 (0)