From c75fa3fe6ea3885f76bfd61e358fb69089cf643a Mon Sep 17 00:00:00 2001 From: istae <14264581+istae@users.noreply.github.com> Date: Tue, 22 Oct 2024 18:23:59 +0300 Subject: [PATCH] fix: update height if staked when height decreases (#4873) --- pkg/node/node.go | 34 ++++----- pkg/storageincentives/staking/contract.go | 4 + .../staking/contract_test.go | 73 +++++++++++++++++-- 3 files changed, 87 insertions(+), 24 deletions(-) diff --git a/pkg/node/node.go b/pkg/node/node.go index 1fad6195e26..37500cb2a37 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -1006,12 +1006,14 @@ func NewBee( if chainEnabled { - if changedOverlay { - stake, err := stakingContract.GetPotentialStake(ctx) - if err != nil { - return nil, err - } - if stake.Cmp(big.NewInt(0)) > 0 { + stake, err := stakingContract.GetPotentialStake(ctx) + if err != nil { + return nil, err + } + + if stake.Cmp(big.NewInt(0)) > 0 { + + if changedOverlay { logger.Debug("changing overlay address in staking contract") tx, err := stakingContract.ChangeStakeOverlay(ctx, common.BytesToHash(nonce)) if err != nil { @@ -1019,23 +1021,17 @@ 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, err - } - if updated { - logger.Info("updated new reserve capacity doubling height in the staking contract", "transaction", tx, "new_height", o.ReserveCapacityDoubling) - } - if o.ReserveCapacityDoubling > 0 { - stake, err := stakingContract.GetPotentialStake(ctx) + // make sure that the staking contract has the up to date height + tx, updated, err := stakingContract.UpdateHeight(ctx) if err != nil { return nil, err } - if stake.Cmp(big.NewInt(0)) > 0 { + if updated { + logger.Info("updated new reserve capacity doubling height in the staking contract", "transaction", tx, "new_height", o.ReserveCapacityDoubling) + } + + if o.ReserveCapacityDoubling > 0 { // Check if the staked amount is sufficient to cover the additional neighborhoods. // The staked amount must be at least 2^h * MinimumStake. if stake.Cmp(big.NewInt(0).Mul(big.NewInt(1<