Skip to content

Commit

Permalink
refactor: participation id hash
Browse files Browse the repository at this point in the history
returns participation id on generate
  • Loading branch information
PhearZero committed Nov 14, 2024
1 parent 6e5d773 commit 2b4ea0c
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 99 deletions.
4 changes: 2 additions & 2 deletions daemon/algod/api/algod.oas2.json
Original file line number Diff line number Diff line change
Expand Up @@ -1167,9 +1167,9 @@
],
"responses": {
"200": {
"description": "An empty JSON object is returned if the generation process was started. Currently no status is available.",
"description": "The participation ID. Currently no status is available.",
"schema": {
"$ref": "#/definitions/ParticipationKey"
"type": "string"
}
},
"400": {
Expand Down
2 changes: 1 addition & 1 deletion daemon/algod/api/algod.oas3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5833,7 +5833,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ParticipationKey"
"type": "string"
}
}
},
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 27 additions & 21 deletions daemon/algod/api/server/v2/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (v2 *Handlers) GetParticipationKeys(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, response)
}

func (v2 *Handlers) generateKeyHandler(address string, params model.GenerateParticipationKeysParams) (model.ParticipationKey, error) {
func (v2 *Handlers) generateKeyHandler(address string, params model.GenerateParticipationKeysParams) error {
installFunc := func(path string) error {
bytes, err := os.ReadFile(path)
if err != nil {
Expand All @@ -280,38 +280,44 @@ func (v2 *Handlers) generateKeyHandler(address string, params model.GeneratePart
v2.Log.Infof("Installed participation key %s", partID)
return err
}
partKeys, _, err := participation.GenParticipationKeysTo(address, params.First, params.Last, nilToZero(params.Dilution), "", installFunc)
if err != nil {
return model.ParticipationKey{}, err
}
nodePartKey, err := v2.Node.GetParticipationKey(partKeys.ID())
if err != nil {
return model.ParticipationKey{}, err
}

return convertParticipationRecord(nodePartKey), nil
_, _, err := participation.GenParticipationKeysTo(address, params.First, params.Last, nilToZero(params.Dilution), "", installFunc)
return err
}

// GenerateParticipationKeys generates and installs participation keys to the node.
// (POST /v2/participation/generate/{address})
func (v2 *Handlers) GenerateParticipationKeys(ctx echo.Context, address string, params model.GenerateParticipationKeysParams) error {
addr, err := basics.UnmarshalChecksumAddress(address)
if err != nil {
return badRequest(ctx, err, err.Error(), v2.Log)
}
if !v2.KeygenLimiter.TryAcquire(1) {
err := fmt.Errorf("participation key generation already in progress")
return badRequest(ctx, err, err.Error(), v2.Log)
}
// Generate the keys on the main thread to block response
// KeysBuilder already handles coroutines, this response feedback
// is necessary for end users
part, err := v2.generateKeyHandler(address, params)

defer v2.KeygenLimiter.Release(1)
// Semaphore was acquired, generate the key.
go func() {
defer v2.KeygenLimiter.Release(1)
err := v2.generateKeyHandler(address, params)
if err != nil {
v2.Log.Warnf("Error generating participation keys: %v", err)
}
}()

if err != nil {
return badRequest(ctx, err, err.Error(), v2.Log)
firstRound := basics.Round(params.First)
lastRound := basics.Round(params.Last)
keyDilution := nilToZero(params.Dilution)
if keyDilution == 0 {
keyDilution = account.DefaultKeyDilution(firstRound, lastRound)
}

// ParticipationKey. Returns the stored participation key.
return ctx.JSON(http.StatusOK, part)
identity := account.ParticipationKeyIdentity{
Parent: addr,
FirstValid: firstRound,
LastValid: lastRound,
KeyDilution: keyDilution,
}
return ctx.JSON(http.StatusOK, identity.ID().String())
}

// AddParticipationKey Add a participation key to the node
Expand Down
57 changes: 5 additions & 52 deletions data/account/msgp_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions data/account/participation.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,10 @@ type Participation struct {
type ParticipationKeyIdentity struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`

Parent basics.Address `codec:"addr"`
VRFSK crypto.VrfPrivkey `codec:"vrfsk"`
VoteID crypto.OneTimeSignatureVerifier `codec:"vote-id"`
FirstValid basics.Round `codec:"fv"`
LastValid basics.Round `codec:"lv"`
KeyDilution uint64 `codec:"kd"`
Parent basics.Address `codec:"addr"`
FirstValid basics.Round `codec:"fv"`
LastValid basics.Round `codec:"lv"`
KeyDilution uint64 `codec:"kd"`
}

// ToBeHashed implements the Hashable interface.
Expand All @@ -92,12 +90,6 @@ func (part Participation) ID() ParticipationID {
LastValid: part.LastValid,
KeyDilution: part.KeyDilution,
}
if part.VRF != nil {
copy(idData.VRFSK[:], part.VRF.SK[:])
}
if part.Voting != nil {
copy(idData.VoteID[:], part.Voting.OneTimeSignatureVerifier[:])
}

return idData.ID()
}
Expand Down
2 changes: 0 additions & 2 deletions data/accountManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ func (manager *AccountManager) AddParticipation(participation account.PersistedP
Parent: address,
FirstValid: first,
LastValid: last,
VRFSK: participation.VRF.SK,
VoteID: participation.Voting.OneTimeSignatureVerifier,
KeyDilution: participation.KeyDilution,
}

Expand Down

0 comments on commit 2b4ea0c

Please sign in to comment.