Skip to content
Draft
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
58 changes: 47 additions & 11 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -1116,19 +1116,55 @@ func NewBee(
logger.Info("overlay address changed in staking contract", "transaction", tx)
}

// make sure that the staking contract has the up to date height
tx, updated, err := stakingContract.UpdateHeight(ctx)
if err != nil {
return nil, fmt.Errorf("update height in staking contract: %w", err)
}
if updated {
logger.Info("updated new reserve capacity doubling height in the staking contract", "transaction", tx, "new_height", o.ReserveCapacityDoubling)
}

// Check if the staked amount is sufficient to cover the additional neighborhoods.
// The staked amount must be at least 2^h * MinimumStake.
if o.ReserveCapacityDoubling > 0 && stake.Cmp(big.NewInt(0).Mul(big.NewInt(1<<o.ReserveCapacityDoubling), staking.MinimumStakeAmount)) < 0 {
logger.Warning("staked amount does not sufficiently cover the additional reserve capacity. Stake should be at least 2^h * 10 BZZ, where h is the number extra doublings.")
minStake := big.NewInt(0).Mul(big.NewInt(1<<o.ReserveCapacityDoubling), staking.MinimumStakeAmount)
if o.ReserveCapacityDoubling > 0 && stake.Cmp(minStake) < 0 {
logger.Warning("staked amount does not sufficiently cover the additional reserve capacity. On-chain height update will be skipped. Node will start, but storage incentives may not function for this capacity.", "missing_stake", new(big.Int).Sub(minStake, stake))

// Start a background worker to monitor the stake and update the height when the stake is sufficient.
go func() {
// Check every 10 minutes
ticker := time.NewTicker(10 * time.Minute)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return
case <-ticker.C:
currentStake, err := stakingContract.GetPotentialStake(ctx)
if err != nil {
logger.Error(err, "runtime staking monitor: get potential stake")
continue
}

if currentStake.Cmp(minStake) >= 0 {
tx, updated, err := stakingContract.UpdateHeight(ctx)
if err != nil {
logger.Error(err, "runtime staking monitor: update height")
continue
}
if updated {
logger.Warning("Runtime stake top-up detected. Reserve capacity doubling updated. Node will be FROZEN for ~2 rounds.", "transaction", tx)
} else {
logger.Info("Runtime stake top-up detected. Reserve capacity doubling already updated.")
}
return
}
}
}
}()

} else {
// make sure that the staking contract has the up to date height
tx, updated, err := stakingContract.UpdateHeight(ctx)
if err != nil {
return nil, fmt.Errorf("update height in staking contract: %w", err)
}
if updated {
logger.Info("updated new reserve capacity doubling height in the staking contract", "transaction", tx, "new_height", o.ReserveCapacityDoubling)
}
}
}
}
Expand Down
Loading