Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions pkg/api/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package api

import (
"context"
"errors"
"math/big"
"net/http"
Expand Down Expand Up @@ -77,6 +78,21 @@ func (s *Service) stakingDepositHandler(w http.ResponseWriter, r *http.Request)
jsonhttp.InternalServerError(w, "cannot stake")
return
}

// if the deposit is successful, we should update the height of the node in the staking contract
// this is done to make sure that the node is participating in the redistribution game with the correct height
// if the node has started with insufficient stake
go func() {
tx, updated, err := s.stakingContract.UpdateHeight(context.Background())
if err != nil {
logger.Error(err, "update height failed")
return
}
if updated {
logger.Warning("reserve capacity doubling updated after stake deposit. Node will be FROZEN for ~2 rounds.", "transaction", tx)
}
}()

jsonhttp.OK(w, stakeTransactionReponse{
TxHash: txHash.String(),
})
Expand Down
24 changes: 13 additions & 11 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -1116,19 +1116,21 @@ 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))

} 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