Skip to content

Commit 28fd5db

Browse files
authored
PKI PR Feedback. (#3191)
1 parent 0ca20bd commit 28fd5db

File tree

13 files changed

+232
-163
lines changed

13 files changed

+232
-163
lines changed

agreement/abstractions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ type KeyManager interface {
226226
// keysRound.
227227
VotingKeys(votingRound, keysRound basics.Round) []account.Participation
228228

229-
// RecordAsync indicates that the given participation action has been taken.
229+
// Record indicates that the given participation action has been taken.
230230
// The operation needs to be asynchronous to avoid impacting agreement.
231-
RecordAsync(account basics.Address, round basics.Round, participationType account.ParticipationAction)
231+
Record(account basics.Address, round basics.Round, participationType account.ParticipationAction)
232232
}
233233

234234
// MessageHandle is an ID referring to a specific message.

agreement/agreementtest/keyManager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ func (m SimpleKeyManager) VotingKeys(votingRound, _ basics.Round) []account.Part
3939
func (m SimpleKeyManager) DeleteOldKeys(r basics.Round) {
4040
}
4141

42-
// RecordAsync implements KeyManager.RecordAsync.
43-
func (m SimpleKeyManager) RecordAsync(account basics.Address, round basics.Round, action account.ParticipationAction) {
42+
// Record implements KeyManager.Record.
43+
func (m SimpleKeyManager) Record(account basics.Address, round basics.Round, action account.ParticipationAction) {
4444
}

agreement/keyManager_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
package agreement
1818

1919
import (
20+
"testing"
21+
2022
"github.com/algorand/go-deadlock"
23+
"github.com/stretchr/testify/require"
2124

2225
"github.com/algorand/go-algorand/data/account"
2326
"github.com/algorand/go-algorand/data/basics"
@@ -53,11 +56,19 @@ func (m *recordingKeyManager) DeleteOldKeys(r basics.Round) {
5356
}
5457

5558
// Record implements KeyManager.Record.
56-
func (m *recordingKeyManager) RecordAsync(acct basics.Address, round basics.Round, action account.ParticipationAction) {
59+
func (m *recordingKeyManager) Record(acct basics.Address, round basics.Round, action account.ParticipationAction) {
5760
m.mutex.Lock()
5861
defer m.mutex.Unlock()
5962
if _, ok := m.recording[acct]; !ok {
6063
m.recording[acct] = make(map[account.ParticipationAction]basics.Round)
6164
}
6265
m.recording[acct][action] = round
6366
}
67+
68+
// ValidateVoteRound requires that the given address voted on a particular round.
69+
func (m *recordingKeyManager) ValidateVoteRound(t *testing.T, address basics.Address, round basics.Round) {
70+
m.mutex.Lock()
71+
require.Equal(t, round, m.recording[address][account.Vote])
72+
require.Equal(t, round, m.recording[address][account.BlockProposal])
73+
m.mutex.Unlock()
74+
}

agreement/pseudonode.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ func (t pseudonodeVotesTask) execute(verifier *AsyncVoteVerifier, quit chan stru
441441
for _, r := range verifiedResults {
442442
select {
443443
case t.out <- messageEvent{T: voteVerified, Input: r.message, Err: makeSerErr(r.err)}:
444-
t.node.keys.RecordAsync(r.v.R.Sender, r.v.R.Round, account.Vote)
444+
t.node.keys.Record(r.v.R.Sender, r.v.R.Round, account.Vote)
445445
case <-quit:
446446
return
447447
case <-t.context.Done():
@@ -529,7 +529,7 @@ func (t pseudonodeProposalsTask) execute(verifier *AsyncVoteVerifier, quit chan
529529
for _, r := range verifiedVotes {
530530
select {
531531
case t.out <- messageEvent{T: voteVerified, Input: r.message, Err: makeSerErr(r.err)}:
532-
t.node.keys.RecordAsync(r.v.R.Sender, r.v.R.Round, account.BlockProposal)
532+
t.node.keys.Record(r.v.R.Sender, r.v.R.Round, account.BlockProposal)
533533
case <-quit:
534534
return
535535
case <-t.context.Done():

agreement/pseudonode_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,8 @@ func TestPseudonode(t *testing.T) {
223223
messageEvent, typeOk := ev.(messageEvent)
224224
assert.True(t, true, typeOk)
225225
// Verify votes are recorded - everyone is voting and proposing blocks.
226-
keyManager.mutex.Lock()
227-
assert.Equal(t, startRound, keyManager.recording[messageEvent.Input.Vote.R.Sender][account.Vote])
228-
assert.Equal(t, startRound, keyManager.recording[messageEvent.Input.Vote.R.Sender][account.BlockProposal])
226+
keyManager.ValidateVoteRound(t, messageEvent.Input.Vote.R.Sender, startRound)
229227
events[messageEvent.t()] = append(events[messageEvent.t()], messageEvent)
230-
keyManager.mutex.Unlock()
231228
}
232229
assert.Subset(t, []int{5, 6, 7, 8, 9, 10}, []int{len(events[voteVerified])})
233230
assert.Equal(t, 0, len(events[payloadVerified]))
@@ -395,7 +392,7 @@ func (k *KeyManagerProxy) VotingKeys(votingRound, balanceRound basics.Round) []a
395392
return k.target(votingRound, balanceRound)
396393
}
397394

398-
func (k *KeyManagerProxy) RecordAsync(account basics.Address, round basics.Round, action account.ParticipationAction) {
395+
func (k *KeyManagerProxy) Record(account basics.Address, round basics.Round, action account.ParticipationAction) {
399396
}
400397

401398
func TestPseudonodeLoadingOfParticipationKeys(t *testing.T) {

daemon/algod/api/server/v2/handlers.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ type NodeInterface interface {
6868
StartCatchup(catchpoint string) error
6969
AbortCatchup(catchpoint string) error
7070
Config() config.Local
71-
InstallParticipationKey(partKeyBinary *[]byte) (account.ParticipationID, error)
71+
InstallParticipationKey(partKeyBinary []byte) (account.ParticipationID, error)
7272
ListParticipationKeys() ([]account.ParticipationRecord, error)
7373
GetParticipationKey(account.ParticipationID) (account.ParticipationRecord, error)
7474
RemoveParticipationKey(account.ParticipationID) error
@@ -102,19 +102,19 @@ func convertParticipationRecord(record account.ParticipationRecord) generated.Pa
102102
}
103103

104104
// Optional fields.
105-
participationKey.EffectiveFirstValid = roundToPtrOrNil(record.EffectiveFirst)
105+
if record.EffectiveLast != 0 && record.EffectiveFirst == 0 {
106+
// Special case for first valid on round 0
107+
zero := uint64(0)
108+
participationKey.EffectiveFirstValid = &zero
109+
} else {
110+
participationKey.EffectiveFirstValid = roundToPtrOrNil(record.EffectiveFirst)
111+
}
106112
participationKey.EffectiveLastValid = roundToPtrOrNil(record.EffectiveLast)
107113
participationKey.LastVote = roundToPtrOrNil(record.LastVote)
108114
participationKey.LastBlockProposal = roundToPtrOrNil(record.LastBlockProposal)
109115
participationKey.LastVote = roundToPtrOrNil(record.LastVote)
110116
participationKey.LastStateProof = roundToPtrOrNil(record.LastStateProof)
111117

112-
// Special case for first valid on round 0
113-
if record.EffectiveLast != 0 && record.EffectiveFirst == 0 {
114-
zero := uint64(0)
115-
participationKey.EffectiveFirstValid = &zero
116-
}
117-
118118
return participationKey
119119
}
120120

@@ -152,7 +152,7 @@ func (v2 *Handlers) AddParticipationKey(ctx echo.Context) error {
152152
return badRequest(ctx, err, err.Error(), v2.Log)
153153
}
154154

155-
partID, err := v2.Node.InstallParticipationKey(&partKeyBinary)
155+
partID, err := v2.Node.InstallParticipationKey(partKeyBinary)
156156

157157
if err != nil {
158158
return badRequest(ctx, err, err.Error(), v2.Log)
@@ -167,7 +167,7 @@ func (v2 *Handlers) AddParticipationKey(ctx echo.Context) error {
167167
// (DELETE /v2/participation/{participation-id})
168168
func (v2 *Handlers) DeleteParticipationKeyByID(ctx echo.Context, participationID string) error {
169169

170-
decodedParticipationID, err := account.ParticipationIDFromString(participationID)
170+
decodedParticipationID, err := account.ParseParticipationID(participationID)
171171

172172
if err != nil {
173173
return badRequest(ctx, err, err.Error(), v2.Log)
@@ -176,12 +176,11 @@ func (v2 *Handlers) DeleteParticipationKeyByID(ctx echo.Context, participationID
176176
err = v2.Node.RemoveParticipationKey(decodedParticipationID)
177177

178178
if err != nil {
179-
180179
if errors.Is(err, account.ErrParticipationIDNotFound) {
181-
return ctx.JSON(http.StatusOK, generated.ErrorResponse{Message: "participation id not found"})
180+
return notFound(ctx, account.ErrParticipationIDNotFound, "participation id not found", v2.Log)
182181
}
183182

184-
return badRequest(ctx, err, err.Error(), v2.Log)
183+
return internalError(ctx, err, err.Error(), v2.Log)
185184
}
186185

187186
return ctx.NoContent(http.StatusOK)
@@ -191,7 +190,7 @@ func (v2 *Handlers) DeleteParticipationKeyByID(ctx echo.Context, participationID
191190
// (GET /v2/participation/{participation-id})
192191
func (v2 *Handlers) GetParticipationKeyByID(ctx echo.Context, participationID string) error {
193192

194-
decodedParticipationID, err := account.ParticipationIDFromString(participationID)
193+
decodedParticipationID, err := account.ParseParticipationID(participationID)
195194

196195
if err != nil {
197196
return badRequest(ctx, err, err.Error(), v2.Log)
@@ -200,7 +199,11 @@ func (v2 *Handlers) GetParticipationKeyByID(ctx echo.Context, participationID st
200199
participationRecord, err := v2.Node.GetParticipationKey(decodedParticipationID)
201200

202201
if err != nil {
203-
return badRequest(ctx, err, err.Error(), v2.Log)
202+
return internalError(ctx, err, err.Error(), v2.Log)
203+
}
204+
205+
if participationRecord.IsZero() {
206+
return notFound(ctx, account.ErrParticipationIDNotFound, account.ErrParticipationIDNotFound.Error(), v2.Log)
204207
}
205208

206209
response := convertParticipationRecord(participationRecord)

daemon/algod/api/server/v2/test/helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ type mockNode struct {
8686
err error
8787
}
8888

89-
func (m mockNode) InstallParticipationKey(partKeyBinary *[]byte) (account.ParticipationID, error) {
89+
func (m mockNode) InstallParticipationKey(partKeyBinary []byte) (account.ParticipationID, error) {
9090
panic("implement me")
9191
}
9292

0 commit comments

Comments
 (0)