Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: minor style improvements in keygen code. #5786

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions daemon/algod/api/server/v2/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@

// Handlers is an implementation to the V2 route handler interface defined by the generated code.
type Handlers struct {
Node NodeInterface
Log logging.Logger
Shutdown <-chan struct{}
Node NodeInterface
Log logging.Logger
Shutdown <-chan struct{}

// KeygenLimiter is used to limit the number of concurrent key generation requests.
KeygenLimiter *semaphore.Weighted
}

Expand Down Expand Up @@ -267,18 +269,21 @@
// 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 {
if v2.KeygenLimiter != nil && v2.KeygenLimiter.TryAcquire(1) {
go func() {
defer v2.KeygenLimiter.Release(1)
err := v2.generateKeyHandler(address, params)
if err != nil {
v2.Log.Warnf("Error generating participation keys: %v", err)
}
}()
} else {
if !v2.KeygenLimiter.TryAcquire(1) {

Check warning on line 272 in daemon/algod/api/server/v2/handlers.go

View check run for this annotation

Codecov / codecov/patch

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

Added line #L272 was not covered by tests
err := fmt.Errorf("participation key generation already in progress")
return badRequest(ctx, err, err.Error(), v2.Log)
}

// 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)

Check warning on line 282 in daemon/algod/api/server/v2/handlers.go

View check run for this annotation

Codecov / codecov/patch

daemon/algod/api/server/v2/handlers.go#L278-L282

Added lines #L278 - L282 were not covered by tests
}
}()

// Empty object. In the future we may want to add a field for the participation ID.
return ctx.String(http.StatusOK, "{}")
}

Expand Down
Loading