Skip to content

Commit e7bc895

Browse files
authored
fix(redistribution): reveal with correct depth, swip21 (#4865)
1 parent 74db3d4 commit e7bc895

File tree

10 files changed

+88
-71
lines changed

10 files changed

+88
-71
lines changed

pkg/api/api_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ func createRedistributionAgentService(
709709
tranService,
710710
&mockHealth{},
711711
log.Noop,
712+
0,
712713
)
713714
}
714715

pkg/node/node.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ func NewBee(
906906
return nil, fmt.Errorf("status service: %w", err)
907907
}
908908

909-
saludService := salud.New(nodeStatus, kad, localStore, logger, warmupTime, api.FullMode.String(), salud.DefaultMinPeersPerBin, salud.DefaultDurPercentile, salud.DefaultConnsPercentile)
909+
saludService := salud.New(nodeStatus, kad, localStore, logger, warmupTime, api.FullMode.String(), salud.DefaultMinPeersPerBin, salud.DefaultDurPercentile, salud.DefaultConnsPercentile, uint8(o.ReserveCapacityDoubling))
910910
b.saludCloser = saludService
911911

912912
rC, unsub := saludService.SubscribeNetworkStorageRadius()
@@ -1091,6 +1091,7 @@ func NewBee(
10911091
transactionService,
10921092
saludService,
10931093
logger,
1094+
uint8(o.ReserveCapacityDoubling),
10941095
)
10951096
if err != nil {
10961097
return nil, fmt.Errorf("storage incentives agent: %w", err)

pkg/salud/salud.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ type peerStatus interface {
4040
PeerSnapshot(ctx context.Context, peer swarm.Address) (*status.Snapshot, error)
4141
}
4242

43-
type reserve interface {
44-
storer.RadiusChecker
45-
ReserveCapacityDoubling() int
46-
}
47-
4843
type service struct {
4944
wg sync.WaitGroup
5045
quit chan struct{}
@@ -53,34 +48,38 @@ type service struct {
5348
status peerStatus
5449
metrics metrics
5550
isSelfHealthy *atomic.Bool
56-
reserve reserve
51+
reserve storer.RadiusChecker
5752

5853
radiusSubsMtx sync.Mutex
5954
radiusC []chan uint8
55+
56+
capacityDoubling uint8
6057
}
6158

6259
func New(
6360
status peerStatus,
6461
topology topologyDriver,
65-
reserve reserve,
62+
reserve storer.RadiusChecker,
6663
logger log.Logger,
6764
warmup time.Duration,
6865
mode string,
6966
minPeersPerbin int,
7067
durPercentile float64,
7168
connsPercentile float64,
69+
capacityDoubling uint8,
7270
) *service {
7371

7472
metrics := newMetrics()
7573

7674
s := &service{
77-
quit: make(chan struct{}),
78-
logger: logger.WithName(loggerName).Register(),
79-
status: status,
80-
topology: topology,
81-
metrics: metrics,
82-
isSelfHealthy: atomic.NewBool(true),
83-
reserve: reserve,
75+
quit: make(chan struct{}),
76+
logger: logger.WithName(loggerName).Register(),
77+
status: status,
78+
topology: topology,
79+
metrics: metrics,
80+
isSelfHealthy: atomic.NewBool(true),
81+
reserve: reserve,
82+
capacityDoubling: capacityDoubling,
8483
}
8584

8685
s.wg.Add(1)
@@ -221,7 +220,7 @@ func (s *service) salud(mode string, minPeersPerbin int, durPercentile float64,
221220
}
222221
}
223222

224-
networkRadiusEstimation := s.reserve.StorageRadius() + uint8(s.reserve.ReserveCapacityDoubling())
223+
networkRadiusEstimation := s.reserve.StorageRadius() + s.capacityDoubling
225224

226225
selfHealth := true
227226
if nHoodRadius == networkRadius && networkRadiusEstimation != networkRadius {

pkg/salud/salud_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func TestSalud(t *testing.T) {
7070
mockstorer.WithReserveSize(100),
7171
)
7272

73-
service := salud.New(statusM, topM, reserve, log.Noop, -1, "full", 0, 0.8, 0.8)
73+
service := salud.New(statusM, topM, reserve, log.Noop, -1, "full", 0, 0.8, 0.8, 0)
7474

7575
err := spinlock.Wait(time.Minute, func() bool {
7676
return len(topM.PeersHealth()) == len(peers)
@@ -116,7 +116,7 @@ func TestSelfUnhealthyRadius(t *testing.T) {
116116
mockstorer.WithReserveSize(100),
117117
)
118118

119-
service := salud.New(statusM, topM, reserve, log.Noop, -1, "full", 0, 0.8, 0.8)
119+
service := salud.New(statusM, topM, reserve, log.Noop, -1, "full", 0, 0.8, 0.8, 0)
120120
testutil.CleanupCloser(t, service)
121121

122122
err := spinlock.Wait(time.Minute, func() bool {
@@ -151,10 +151,9 @@ func TestSelfHealthyCapacityDoubling(t *testing.T) {
151151
reserve := mockstorer.NewReserve(
152152
mockstorer.WithRadius(6),
153153
mockstorer.WithReserveSize(100),
154-
mockstorer.WithCapacityDoubling(2),
155154
)
156155

157-
service := salud.New(statusM, topM, reserve, log.Noop, -1, "full", 0, 0.8, 0.8)
156+
service := salud.New(statusM, topM, reserve, log.Noop, -1, "full", 0, 0.8, 0.8, 2)
158157
testutil.CleanupCloser(t, service)
159158

160159
err := spinlock.Wait(time.Minute, func() bool {
@@ -184,7 +183,7 @@ func TestSubToRadius(t *testing.T) {
184183

185184
topM := topMock.NewTopologyDriver(topMock.WithPeers(addrs...))
186185

187-
service := salud.New(&statusMock{make(map[string]peer)}, topM, mockstorer.NewReserve(), log.Noop, -1, "full", 0, 0.8, 0.8)
186+
service := salud.New(&statusMock{make(map[string]peer)}, topM, mockstorer.NewReserve(), log.Noop, -1, "full", 0, 0.8, 0.8, 0)
188187

189188
c, unsub := service.SubscribeNetworkStorageRadius()
190189
t.Cleanup(unsub)
@@ -217,7 +216,7 @@ func TestUnsub(t *testing.T) {
217216

218217
topM := topMock.NewTopologyDriver(topMock.WithPeers(addrs...))
219218

220-
service := salud.New(&statusMock{make(map[string]peer)}, topM, mockstorer.NewReserve(), log.Noop, -1, "full", 0, 0.8, 0.8)
219+
service := salud.New(&statusMock{make(map[string]peer)}, topM, mockstorer.NewReserve(), log.Noop, -1, "full", 0, 0.8, 0.8, 0)
221220
testutil.CleanupCloser(t, service)
222221

223222
c, unsub := service.SubscribeNetworkStorageRadius()

pkg/storageincentives/agent.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type Agent struct {
7070
chainStateGetter postage.ChainStateGetter
7171
commitLock sync.Mutex
7272
health Health
73+
capacityDoubling uint8
7374
}
7475

7576
func New(overlay swarm.Address,
@@ -89,6 +90,7 @@ func New(overlay swarm.Address,
8990
tranService transaction.Service,
9091
health Health,
9192
logger log.Logger,
93+
capacityDoubling uint8,
9294
) (*Agent, error) {
9395
a := &Agent{
9496
overlay: overlay,
@@ -104,6 +106,7 @@ func New(overlay swarm.Address,
104106
redistributionStatuser: redistributionStatuser,
105107
health: health,
106108
chainStateGetter: chainStateGetter,
109+
capacityDoubling: capacityDoubling,
107110
}
108111

109112
state, err := NewRedistributionState(logger, ethAddress, stateStore, erc20Service, tranService)
@@ -389,14 +392,15 @@ func (a *Agent) handleClaim(ctx context.Context, round uint64) error {
389392
}
390393

391394
func (a *Agent) handleSample(ctx context.Context, round uint64) (bool, error) {
392-
storageRadius := a.store.StorageRadius()
395+
// minimum proximity between the achor and the stored chunks
396+
commitedDepth := a.store.StorageRadius() + a.capacityDoubling
393397

394398
if a.state.IsFrozen() {
395399
a.logger.Info("skipping round because node is frozen")
396400
return false, nil
397401
}
398402

399-
isPlaying, err := a.contract.IsPlaying(ctx, storageRadius)
403+
isPlaying, err := a.contract.IsPlaying(ctx, commitedDepth)
400404
if err != nil {
401405
a.metrics.ErrCheckIsPlaying.Inc()
402406
return false, err
@@ -429,21 +433,21 @@ func (a *Agent) handleSample(ctx context.Context, round uint64) (bool, error) {
429433
}
430434

431435
now := time.Now()
432-
sample, err := a.makeSample(ctx, storageRadius)
436+
sample, err := a.makeSample(ctx, commitedDepth)
433437
if err != nil {
434438
return false, err
435439
}
436440
dur := time.Since(now)
437441
a.metrics.SampleDuration.Set(dur.Seconds())
438442

439-
a.logger.Info("produced sample", "hash", sample.ReserveSampleHash, "radius", sample.StorageRadius, "round", round)
443+
a.logger.Info("produced sample", "hash", sample.ReserveSampleHash, "radius", commitedDepth, "round", round)
440444

441445
a.state.SetSampleData(round, sample, dur)
442446

443447
return true, nil
444448
}
445449

446-
func (a *Agent) makeSample(ctx context.Context, storageRadius uint8) (SampleData, error) {
450+
func (a *Agent) makeSample(ctx context.Context, commitedDepth uint8) (SampleData, error) {
447451
salt, err := a.contract.ReserveSalt(ctx)
448452
if err != nil {
449453
return SampleData{}, err
@@ -454,7 +458,7 @@ func (a *Agent) makeSample(ctx context.Context, storageRadius uint8) (SampleData
454458
return SampleData{}, err
455459
}
456460

457-
rSample, err := a.store.ReserveSample(ctx, salt, storageRadius, uint64(timeLimiter), a.minBatchBalance())
461+
rSample, err := a.store.ReserveSample(ctx, salt, commitedDepth, uint64(timeLimiter), a.minBatchBalance())
458462
if err != nil {
459463
return SampleData{}, err
460464
}
@@ -468,7 +472,7 @@ func (a *Agent) makeSample(ctx context.Context, storageRadius uint8) (SampleData
468472
Anchor1: salt,
469473
ReserveSampleItems: rSample.Items,
470474
ReserveSampleHash: sampleHash,
471-
StorageRadius: storageRadius,
475+
StorageRadius: commitedDepth,
472476
}
473477

474478
return sample, nil

pkg/storageincentives/agent_test.go

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func TestAgent(t *testing.T) {
4141
limit uint64
4242
expectedCalls bool
4343
balance *big.Int
44+
doubling uint8
4445
}{{
4546
name: "3 blocks per phase, same block number returns twice",
4647
blocksPerRound: 9,
@@ -49,6 +50,7 @@ func TestAgent(t *testing.T) {
4950
expectedCalls: true,
5051
limit: 108, // computed with blocksPerRound * (exptectedCalls + 2)
5152
balance: bigBalance,
53+
doubling: 1,
5254
}, {
5355
name: "3 blocks per phase, block number returns every block",
5456
blocksPerRound: 9,
@@ -57,6 +59,7 @@ func TestAgent(t *testing.T) {
5759
expectedCalls: true,
5860
limit: 108,
5961
balance: bigBalance,
62+
doubling: 0,
6063
}, {
6164
name: "no expected calls - block number returns late after each phase",
6265
blocksPerRound: 9,
@@ -65,6 +68,7 @@ func TestAgent(t *testing.T) {
6568
expectedCalls: false,
6669
limit: 108,
6770
balance: bigBalance,
71+
doubling: 0,
6872
}, {
6973
name: "4 blocks per phase, block number returns every other block",
7074
blocksPerRound: 12,
@@ -73,6 +77,7 @@ func TestAgent(t *testing.T) {
7377
expectedCalls: true,
7478
limit: 144,
7579
balance: bigBalance,
80+
doubling: 1,
7681
}, {
7782
// This test case is based on previous, but this time agent will not have enough
7883
// balance to participate in the game so no calls are going to be made.
@@ -83,6 +88,7 @@ func TestAgent(t *testing.T) {
8388
expectedCalls: false,
8489
limit: 144,
8590
balance: big.NewInt(0),
91+
doubling: 1,
8692
},
8793
}
8894

@@ -106,9 +112,12 @@ func TestAgent(t *testing.T) {
106112
block: tc.blocksPerRound,
107113
balance: tc.balance,
108114
}
109-
contract := &mockContract{}
110115

111-
service, _ := createService(t, addr, backend, contract, tc.blocksPerRound, tc.blocksPerPhase)
116+
var radius uint8 = 8
117+
118+
contract := &mockContract{t: t, expectedRadius: radius + tc.doubling}
119+
120+
service, _ := createService(t, addr, backend, contract, tc.blocksPerRound, tc.blocksPerPhase, radius, tc.doubling)
112121
testutil.CleanupCloser(t, service)
113122

114123
<-wait
@@ -156,7 +165,10 @@ func createService(
156165
backend storageincentives.ChainBackend,
157166
contract redistribution.Contract,
158167
blocksPerRound uint64,
159-
blocksPerPhase uint64) (*storageincentives.Agent, error) {
168+
blocksPerPhase uint64,
169+
radius uint8,
170+
doubling uint8,
171+
) (*storageincentives.Agent, error) {
160172
t.Helper()
161173

162174
postageContract := contractMock.New(contractMock.WithExpiresBatchesFunc(func(context.Context) error {
@@ -168,7 +180,7 @@ func createService(
168180
}))
169181

170182
reserve := resMock.NewReserve(
171-
resMock.WithRadius(0),
183+
resMock.WithRadius(radius),
172184
resMock.WithSample(storer.RandSample(t, nil)),
173185
)
174186

@@ -189,6 +201,7 @@ func createService(
189201
transactionmock.New(),
190202
&mockHealth{},
191203
log.Noop,
204+
doubling,
192205
)
193206
}
194207

@@ -257,15 +270,20 @@ const (
257270
)
258271

259272
type mockContract struct {
260-
callsList []contractCall
261-
mtx sync.Mutex
273+
callsList []contractCall
274+
mtx sync.Mutex
275+
expectedRadius uint8
276+
t *testing.T
262277
}
263278

264279
func (m *mockContract) ReserveSalt(context.Context) ([]byte, error) {
265280
return nil, nil
266281
}
267282

268-
func (m *mockContract) IsPlaying(context.Context, uint8) (bool, error) {
283+
func (m *mockContract) IsPlaying(_ context.Context, r uint8) (bool, error) {
284+
if r != m.expectedRadius {
285+
m.t.Fatalf("isPlaying: expected radius %d, got %d", m.expectedRadius, r)
286+
}
269287
return true, nil
270288
}
271289

@@ -290,9 +308,14 @@ func (m *mockContract) Commit(context.Context, []byte, uint64) (common.Hash, err
290308
return common.Hash{}, nil
291309
}
292310

293-
func (m *mockContract) Reveal(context.Context, uint8, []byte, []byte) (common.Hash, error) {
311+
func (m *mockContract) Reveal(_ context.Context, r uint8, _ []byte, _ []byte) (common.Hash, error) {
294312
m.mtx.Lock()
295313
defer m.mtx.Unlock()
314+
315+
if r != m.expectedRadius {
316+
m.t.Fatalf("reveal: expected radius %d, got %d", m.expectedRadius, r)
317+
}
318+
296319
m.callsList = append(m.callsList, revealCall)
297320
return common.Hash{}, nil
298321
}

pkg/storer/mock/mockreserve.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,6 @@ func (s *ReserveStore) ReserveSize() int {
178178
return s.reservesize
179179
}
180180

181-
func (s *ReserveStore) ReserveCapacityDoubling() int {
182-
return s.capacityDoubling
183-
}
184-
185181
func (s *ReserveStore) ReserveLastBinIDs() (curs []uint64, epoch uint64, err error) {
186182
return s.cursors, s.epoch, s.cursorsErr
187183
}

pkg/storer/reserve.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,10 +412,6 @@ func (db *DB) StorageRadius() uint8 {
412412
return db.reserve.Radius()
413413
}
414414

415-
func (db *DB) ReserveCapacityDoubling() int {
416-
return db.reserveOptions.capacityDoubling
417-
}
418-
419415
func (db *DB) ReserveSize() int {
420416
if db.reserve == nil {
421417
return 0

0 commit comments

Comments
 (0)