Skip to content

Commit 8103ed1

Browse files
committed
feat: draft implementation
1 parent 9c4add7 commit 8103ed1

10 files changed

Lines changed: 643 additions & 27 deletions

File tree

openapi/Swarm.yaml

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
openapi: 3.0.3
22

33
info:
4-
version: 8.1.1
4+
version: 8.2.0
55
title: Bee API
66
description: "API endpoints for interacting with the Swarm network, supporting file operations, messaging, and node management"
77

@@ -2426,6 +2426,37 @@ paths:
24262426
default:
24272427
description: Default response
24282428

2429+
"/redistribution":
2430+
put:
2431+
summary: Enable or disable participation in new redistribution rounds
2432+
description: >
2433+
Controls whether the node will commit in a new redistribution round.
2434+
Disabling does not abort an in-flight commit and does not skip reveal or claim
2435+
for a round that already committed. Re-enabling during a commit phase that was
2436+
already skipped does not retry that round; participation resumes on the next
2437+
sample/commit cycle. After a node restart participation is enabled again.
2438+
tags:
2439+
- RedistributionState
2440+
requestBody:
2441+
required: true
2442+
content:
2443+
application/json:
2444+
schema:
2445+
$ref: "SwarmCommon.yaml#/components/schemas/RedistributionEnableRequest"
2446+
responses:
2447+
"200":
2448+
description: Participation flag updated
2449+
content:
2450+
application/json:
2451+
schema:
2452+
$ref: "SwarmCommon.yaml#/components/schemas/RedistributionEnableResponse"
2453+
"400":
2454+
$ref: "SwarmCommon.yaml#/components/responses/400"
2455+
"500":
2456+
$ref: "SwarmCommon.yaml#/components/responses/500"
2457+
default:
2458+
description: Default response
2459+
24292460
"/redistributionstate":
24302461
get:
24312462
summary: Get the node's redistribution game status

openapi/SwarmCommon.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,9 @@ components:
825825
type: boolean
826826
isHealthy:
827827
type: boolean
828+
enabled:
829+
type: boolean
830+
description: Whether the node will commit in new redistribution rounds. A disabled node still finishes a round that already has an on-chain commit. Re-enabling during a commit phase that was already skipped does not retry that round; participation resumes on the next sample/commit cycle.
828831
phase:
829832
type: string
830833
round:
@@ -846,6 +849,21 @@ components:
846849
fees:
847850
$ref: "#/components/schemas/BigInt"
848851

852+
RedistributionEnableRequest:
853+
type: object
854+
required:
855+
- enabled
856+
properties:
857+
enabled:
858+
type: boolean
859+
description: Whether the node should enter new redistribution rounds.
860+
861+
RedistributionEnableResponse:
862+
type: object
863+
properties:
864+
enabled:
865+
type: boolean
866+
849867
PendingTransactionsResponse:
850868
type: object
851869
properties:

pkg/api/api_test.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import (
2222
"time"
2323

2424
"github.com/ethereum/go-ethereum/common"
25+
"github.com/gorilla/websocket"
26+
"resenje.org/web"
27+
2528
"github.com/ethersphere/bee/v2/pkg/accesscontrol"
2629
mockac "github.com/ethersphere/bee/v2/pkg/accesscontrol/mock"
2730
accountingmock "github.com/ethersphere/bee/v2/pkg/accounting/mock"
@@ -70,8 +73,6 @@ import (
7073
"github.com/ethersphere/bee/v2/pkg/transaction/backendmock"
7174
transactionmock "github.com/ethersphere/bee/v2/pkg/transaction/mock"
7275
"github.com/ethersphere/bee/v2/pkg/util/testutil"
73-
"github.com/gorilla/websocket"
74-
"resenje.org/web"
7576
)
7677

7778
var (
@@ -126,17 +127,18 @@ type testServerOptions struct {
126127
BatchStore postage.Storer
127128
SyncStatus func() (bool, error)
128129

129-
BackendOpts []backendmock.Option
130-
Erc20Opts []erc20mock.Option
131-
BeeMode api.BeeNodeMode
132-
RedistributionAgent *storageincentives.Agent
133-
NodeStatus *status.Service
134-
PinIntegrity api.PinIntegrity
135-
WhitelistedAddr string
136-
FullAPIDisabled bool
137-
ChequebookDisabled bool
138-
SwapDisabled bool
139-
Erc20ServiceNil bool
130+
BackendOpts []backendmock.Option
131+
Erc20Opts []erc20mock.Option
132+
BeeMode api.BeeNodeMode
133+
RedistributionAgent *storageincentives.Agent
134+
RedistributionAgentDisabled bool
135+
NodeStatus *status.Service
136+
PinIntegrity api.PinIntegrity
137+
WhitelistedAddr string
138+
FullAPIDisabled bool
139+
ChequebookDisabled bool
140+
SwapDisabled bool
141+
Erc20ServiceNil bool
140142
}
141143

142144
func newTestServer(t *testing.T, o testServerOptions) (*http.Client, *websocket.Conn, string, *chanStorer) {
@@ -223,11 +225,13 @@ func newTestServer(t *testing.T, o testServerOptions) (*http.Client, *websocket.
223225

224226
s.SetP2P(o.P2P)
225227

226-
if o.RedistributionAgent == nil {
227-
o.RedistributionAgent, _ = createRedistributionAgentService(t, o.Overlay, o.StateStorer, erc20, transaction, backend, o.BatchStore)
228-
s.SetRedistributionAgent(o.RedistributionAgent)
228+
if !o.RedistributionAgentDisabled {
229+
if o.RedistributionAgent == nil {
230+
o.RedistributionAgent, _ = createRedistributionAgentService(t, o.Overlay, o.StateStorer, erc20, transaction, backend, o.BatchStore)
231+
s.SetRedistributionAgent(o.RedistributionAgent)
232+
}
233+
testutil.CleanupCloser(t, o.RedistributionAgent)
229234
}
230-
testutil.CleanupCloser(t, o.RedistributionAgent)
231235

232236
s.SetSwarmAddress(&o.Overlay)
233237
s.SetProbe(o.Probe)

pkg/api/export_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ type (
9898
StakeTransactionReponse = stakeTransactionReponse
9999
StatusSnapshotResponse = statusSnapshotResponse
100100
StatusResponse = statusResponse
101+
RedistributionStatusResponse = redistributionStatusResponse
102+
RedistributionToggleResponse = redistributionToggleResponse
101103
)
102104

103105
var (

pkg/api/redistribution.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package api
66

77
import (
8+
"encoding/json"
89
"net/http"
910

1011
"github.com/ethersphere/bee/v2/pkg/bigint"
@@ -28,6 +29,15 @@ type redistributionStatusResponse struct {
2829
Reward *bigint.BigInt `json:"reward"`
2930
Fees *bigint.BigInt `json:"fees"`
3031
IsHealthy bool `json:"isHealthy"`
32+
Enabled bool `json:"enabled"`
33+
}
34+
35+
type redistributionToggleRequest struct {
36+
Enabled *bool `json:"enabled"`
37+
}
38+
39+
type redistributionToggleResponse struct {
40+
Enabled bool `json:"enabled"`
3141
}
3242

3343
func (s *Service) redistributionStatusHandler(w http.ResponseWriter, r *http.Request) {
@@ -70,5 +80,30 @@ func (s *Service) redistributionStatusHandler(w http.ResponseWriter, r *http.Req
7080
Reward: bigint.Wrap(status.Reward),
7181
Fees: bigint.Wrap(status.Fees),
7282
IsHealthy: status.IsHealthy,
83+
Enabled: s.redistributionAgent.IsEnabled(),
7384
})
7485
}
86+
87+
func (s *Service) redistributionToggleHandler(w http.ResponseWriter, r *http.Request) {
88+
logger := tracing.NewLoggerWithTraceID(r.Context(), s.logger.WithName("put_redistribution").Build())
89+
90+
if s.beeMode != FullMode {
91+
jsonhttp.BadRequest(w, errOperationSupportedOnlyInFullMode)
92+
return
93+
}
94+
95+
var body redistributionToggleRequest
96+
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
97+
logger.Debug("decode body failed", "error", err)
98+
logger.Error(nil, "decode body failed")
99+
jsonhttp.BadRequest(w, "invalid request body")
100+
return
101+
}
102+
if body.Enabled == nil {
103+
jsonhttp.BadRequest(w, "enabled is required")
104+
return
105+
}
106+
107+
s.redistributionAgent.SetEnabled(*body.Enabled)
108+
jsonhttp.OK(w, redistributionToggleResponse{Enabled: *body.Enabled})
109+
}

0 commit comments

Comments
 (0)