Skip to content

Commit 035ee36

Browse files
fix: resolve panic in test
1 parent 1b99a12 commit 035ee36

File tree

4 files changed

+25
-43
lines changed

4 files changed

+25
-43
lines changed

pkg/node/bootstrap.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func bootstrapNode(
7373
networkID uint64,
7474
logger log.Logger,
7575
libp2pPrivateKey *ecdsa.PrivateKey,
76+
detector *stabilization.Detector,
7677
o *Options,
7778
) (snapshot *postage.ChainSnapshot, retErr error) {
7879
tracer, tracerCloser, err := tracing.NewTracer(&tracing.Options{
@@ -116,15 +117,6 @@ func bootstrapNode(
116117
}
117118
b.hiveCloser = hive
118119

119-
detector, err := stabilization.NewDetector(stabilization.Config{
120-
RelativeSlowdownFactor: 10,
121-
MinSlowSamples: 10,
122-
WarmupTime: 0,
123-
})
124-
if err != nil {
125-
return nil, fmt.Errorf("rate stabilizer: %w", err)
126-
}
127-
128120
kad, err := kademlia.New(swarmAddress, addressbook, hive, p2ps, detector, logger,
129121
kademlia.Options{Bootnodes: bootnodes, BootnodeMode: o.BootnodeMode, StaticNodes: o.StaticNodes, DataDir: o.DataDir})
130122
if err != nil {

pkg/node/node.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,27 @@ func NewBee(
578578
return nil, fmt.Errorf("invalid payment early: %d", o.PaymentEarly)
579579
}
580580

581+
detector, err := stabilization.NewDetector(stabilization.Config{
582+
RelativeSlowdownFactor: 100,
583+
MinSlowSamples: 20,
584+
WarmupTime: warmupTime,
585+
})
586+
if err != nil {
587+
return nil, fmt.Errorf("rate stabilizer: %w", err)
588+
}
589+
590+
detector.OnPeakStart = func(t time.Time) {
591+
logger.Info("rateStabilizer: START", "timestamp", t)
592+
}
593+
594+
detector.OnStabilized = func(t time.Time, counter int) {
595+
logger.Info("rateStabilizer: STABLE", "timestamp", t, "counter", counter)
596+
}
597+
598+
detector.OnRateIncrease = func(t time.Time, minDuration time.Duration) {
599+
logger.Info("rateStabilizer: RATE", "timestamp", t, "minDuration", minDuration)
600+
}
601+
581602
var initBatchState *postage.ChainSnapshot
582603
// Bootstrap node with postage snapshot only if it is running on mainnet, is a fresh
583604
// install or explicitly asked by user to resync
@@ -597,6 +618,7 @@ func NewBee(
597618
networkID,
598619
log.Noop,
599620
libp2pPrivateKey,
621+
detector,
600622
o,
601623
)
602624
logger.Info("bootstrapper created", "elapsed", time.Since(start))
@@ -701,27 +723,6 @@ func NewBee(
701723

702724
var swapService *swap.Service
703725

704-
detector, err := stabilization.NewDetector(stabilization.Config{
705-
RelativeSlowdownFactor: 100,
706-
MinSlowSamples: 20,
707-
WarmupTime: warmupTime,
708-
})
709-
if err != nil {
710-
return nil, fmt.Errorf("rate stabilizer: %w", err)
711-
}
712-
713-
detector.OnPeakStart = func(t time.Time) {
714-
logger.Info("rateStabilizer: START", "timestamp", t)
715-
}
716-
717-
detector.OnStabilized = func(t time.Time, counter int) {
718-
logger.Info("rateStabilizer: STABLE", "timestamp", t, "counter", counter)
719-
}
720-
721-
detector.OnRateIncrease = func(t time.Time, minDuration time.Duration) {
722-
logger.Info("rateStabilizer: RATE", "timestamp", t, "minDuration", minDuration)
723-
}
724-
725726
kad, err := kademlia.New(swarmAddress, addressbook, hive, p2ps, detector, logger,
726727
kademlia.Options{Bootnodes: bootnodes, BootnodeMode: o.BootnodeMode, StaticNodes: o.StaticNodes, DataDir: o.DataDir})
727728
if err != nil {

pkg/stabilization/stabilization.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,6 @@ func NewDetector(cfg Config) (*Detector, error) {
134134
// Returns the timestamp of the recorded event.
135135
// If the state is already Stabilized, this function does nothing.
136136
func (d *Detector) Record() time.Time {
137-
if d == nil {
138-
return time.Time{}
139-
}
140-
141137
d.mutex.Lock()
142138
defer d.mutex.Unlock()
143139

@@ -225,10 +221,6 @@ func (d *Detector) recordAt(t time.Time) {
225221

226222
// State returns the current detected rate state.
227223
func (d *Detector) State() RateState {
228-
if d == nil {
229-
return StateIdle
230-
}
231-
232224
d.mutex.Lock()
233225
defer d.mutex.Unlock()
234226
return d.currentState
@@ -263,10 +255,6 @@ func (d *Detector) Subscribe() (c <-chan struct{}) {
263255
}
264256

265257
func (d *Detector) IsStabilized() bool {
266-
if d == nil {
267-
return true
268-
}
269-
270258
d.mutex.Lock()
271259
defer d.mutex.Unlock()
272260

pkg/storer/storer_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/ethersphere/bee/v2/pkg/log"
1515
"github.com/ethersphere/bee/v2/pkg/postage"
1616
batchstore "github.com/ethersphere/bee/v2/pkg/postage/batchstore/mock"
17+
ssmock "github.com/ethersphere/bee/v2/pkg/stabilization/mock"
1718
"github.com/ethersphere/bee/v2/pkg/storage"
1819
"github.com/ethersphere/bee/v2/pkg/storage/migration"
1920
"github.com/ethersphere/bee/v2/pkg/storer"
@@ -178,7 +179,6 @@ func TestNew(t *testing.T) {
178179
}
179180

180181
func dbTestOps(baseAddr swarm.Address, reserveCapacity int, bs postage.Storer, radiusSetter topology.SetStorageRadiuser, reserveWakeUpTime time.Duration) *storer.Options {
181-
182182
opts := storer.DefaultOptions()
183183

184184
if radiusSetter == nil {
@@ -194,6 +194,7 @@ func dbTestOps(baseAddr swarm.Address, reserveCapacity int, bs postage.Storer, r
194194
opts.ReserveCapacity = reserveCapacity
195195
opts.Batchstore = bs
196196
opts.ReserveWakeUpDuration = reserveWakeUpTime
197+
opts.StabilizationSubscriber = ssmock.NewSubscriber(true)
197198

198199
return opts
199200
}

0 commit comments

Comments
 (0)