Skip to content

Commit

Permalink
More fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
winder committed Oct 16, 2023
1 parent aa13fde commit 184a31f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
8 changes: 4 additions & 4 deletions daemon/algod/api/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ func NewRouter(logger logging.Logger, node APINodeInterface, shutdown <-chan str

// Registering v2 routes
v2Handler := v2.Handlers{
Node: node,
Log: logger,
Shutdown: shutdown,
Limiter: semaphore.NewWeighted(1),
Node: node,
Log: logger,
Shutdown: shutdown,
KeygenLimiter: semaphore.NewWeighted(1),
}
nppublic.RegisterHandlers(e, &v2Handler, publicMiddleware...)
npprivate.RegisterHandlers(e, &v2Handler, adminMiddleware...)
Expand Down
22 changes: 12 additions & 10 deletions daemon/algod/api/server/v2/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ var WaitForBlockTimeout = 1 * time.Minute

// 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{}
Limiter *semaphore.Weighted
Node NodeInterface
Log logging.Logger
Shutdown <-chan struct{}
KeygenLimiter *semaphore.Weighted
}

// LedgerForAPI describes the Ledger methods used by the v2 API.
Expand Down Expand Up @@ -267,12 +267,14 @@ func (v2 *Handlers) generateKeyHandler(address string, params model.GeneratePart
// 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.Limiter == nil || v2.Limiter.TryAcquire(1) {
defer v2.Limiter.Release(1)
err := v2.generateKeyHandler(address, params)
if err != nil {
v2.Log.Warnf("Error generating participation keys: %v", err)
}
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 {
err := fmt.Errorf("participation key generation already in progress")
return badRequest(ctx, err, err.Error(), v2.Log)
Expand Down
14 changes: 7 additions & 7 deletions daemon/algod/api/server/v2/test/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2392,10 +2392,10 @@ func TestGeneratePartkeys(t *testing.T) {
dummyShutdownChan := make(chan struct{})
mockNode := makeMockNode(mockLedger, t.Name(), nil, cannedStatusReportGolden, false)
handler := v2.Handlers{
Node: mockNode,
Log: logging.Base(),
Shutdown: dummyShutdownChan,
Limiter: semaphore.NewWeighted(1),
Node: mockNode,
Log: logging.Base(),
Shutdown: dummyShutdownChan,
KeygenLimiter: semaphore.NewWeighted(1),
}
e := echo.New()

Expand All @@ -2417,18 +2417,18 @@ func TestGeneratePartkeys(t *testing.T) {
assert.Equal(t, http.StatusOK, rec.Code)

// Wait for keygen to complete
err = handler.Limiter.Acquire(context.Background(), 1)
err = handler.KeygenLimiter.Acquire(context.Background(), 1)
require.NoError(t, err)
require.Greater(t, len(mockNode.PartKeyBinary), 0)
handler.Limiter.Release(1)
handler.KeygenLimiter.Release(1)
}

{
req := httptest.NewRequest(http.MethodPost, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
// Simulate a blocked keygen process (and block until the previous keygen is complete)
err := handler.Limiter.Acquire(context.Background(), 1)
err := handler.KeygenLimiter.Acquire(context.Background(), 1)
require.NoError(t, err)
err = handler.GenerateParticipationKeys(c, addr.String(), model.GenerateParticipationKeysParams{
First: 1000,
Expand Down

0 comments on commit 184a31f

Please sign in to comment.