Skip to content

Commit c75fa3f

Browse files
authored
fix: update height if staked when height decreases (#4873)
1 parent 253d382 commit c75fa3f

File tree

3 files changed

+87
-24
lines changed

3 files changed

+87
-24
lines changed

pkg/node/node.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,36 +1006,32 @@ func NewBee(
10061006

10071007
if chainEnabled {
10081008

1009-
if changedOverlay {
1010-
stake, err := stakingContract.GetPotentialStake(ctx)
1011-
if err != nil {
1012-
return nil, err
1013-
}
1014-
if stake.Cmp(big.NewInt(0)) > 0 {
1009+
stake, err := stakingContract.GetPotentialStake(ctx)
1010+
if err != nil {
1011+
return nil, err
1012+
}
1013+
1014+
if stake.Cmp(big.NewInt(0)) > 0 {
1015+
1016+
if changedOverlay {
10151017
logger.Debug("changing overlay address in staking contract")
10161018
tx, err := stakingContract.ChangeStakeOverlay(ctx, common.BytesToHash(nonce))
10171019
if err != nil {
10181020
return nil, fmt.Errorf("cannot change staking overlay address: %v", err.Error())
10191021
}
10201022
logger.Info("overlay address changed in staking contract", "transaction", tx)
10211023
}
1022-
}
1023-
1024-
// make sure that the staking contract has the up to date height
1025-
tx, updated, err := stakingContract.UpdateHeight(ctx)
1026-
if err != nil {
1027-
return nil, err
1028-
}
1029-
if updated {
1030-
logger.Info("updated new reserve capacity doubling height in the staking contract", "transaction", tx, "new_height", o.ReserveCapacityDoubling)
1031-
}
10321024

1033-
if o.ReserveCapacityDoubling > 0 {
1034-
stake, err := stakingContract.GetPotentialStake(ctx)
1025+
// make sure that the staking contract has the up to date height
1026+
tx, updated, err := stakingContract.UpdateHeight(ctx)
10351027
if err != nil {
10361028
return nil, err
10371029
}
1038-
if stake.Cmp(big.NewInt(0)) > 0 {
1030+
if updated {
1031+
logger.Info("updated new reserve capacity doubling height in the staking contract", "transaction", tx, "new_height", o.ReserveCapacityDoubling)
1032+
}
1033+
1034+
if o.ReserveCapacityDoubling > 0 {
10391035
// Check if the staked amount is sufficient to cover the additional neighborhoods.
10401036
// The staked amount must be at least 2^h * MinimumStake.
10411037
if stake.Cmp(big.NewInt(0).Mul(big.NewInt(1<<o.ReserveCapacityDoubling), staking.MinimumStakeAmount)) < 0 {

pkg/storageincentives/staking/contract.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ func (c *contract) DepositStake(ctx context.Context, stakedAmount *big.Int) (com
103103
}
104104
}
105105

106+
if big.NewInt(0).Add(prevStakedAmount, stakedAmount).Cmp(big.NewInt(0).Mul(big.NewInt(1<<c.height), MinimumStakeAmount)) < 0 {
107+
return common.Hash{}, fmt.Errorf("stake amount does not sufficiently cover the additional reserve capacity: %w", ErrInsufficientStakeAmount)
108+
}
109+
106110
balance, err := c.getBalance(ctx)
107111
if err != nil {
108112
return common.Hash{}, err

pkg/storageincentives/staking/contract_test.go

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,73 @@ func TestDepositStake(t *testing.T) {
290290
}
291291
})
292292

293-
t.Run("insufficient stake amount", func(t *testing.T) {
293+
t.Run("sufficient stake amount extra height", func(t *testing.T) {
294294
t.Parallel()
295295

296-
totalAmount := big.NewInt(0)
296+
balance := big.NewInt(0).Mul(staking.MinimumStakeAmount, big.NewInt(2))
297+
prevStake := staking.MinimumStakeAmount
298+
299+
expectedCallData, err := stakingContractABI.Pack("manageStake", nonce, staking.MinimumStakeAmount, stakingHeight)
300+
if err != nil {
301+
t.Fatal(err)
302+
}
303+
304+
contract := staking.New(
305+
owner,
306+
stakingContractAddress,
307+
stakingContractABI,
308+
bzzTokenAddress,
309+
transactionMock.New(
310+
transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) {
311+
if *request.To == bzzTokenAddress {
312+
return txHashApprove, nil
313+
}
314+
if *request.To == stakingContractAddress {
315+
if !bytes.Equal(expectedCallData[:80], request.Data[:80]) {
316+
return common.Hash{}, fmt.Errorf("got wrong call data. wanted %x, got %x", expectedCallData, request.Data)
317+
}
318+
return txHashDeposited, nil
319+
}
320+
return common.Hash{}, errors.New("sent to wrong contract")
321+
}),
322+
transactionMock.WithWaitForReceiptFunc(func(ctx context.Context, txHash common.Hash) (receipt *types.Receipt, err error) {
323+
if txHash == txHashDeposited {
324+
return &types.Receipt{
325+
Status: 1,
326+
}, nil
327+
}
328+
if txHash == txHashApprove {
329+
return &types.Receipt{
330+
Status: 1,
331+
}, nil
332+
}
333+
return nil, errors.New("unknown tx hash")
334+
}),
335+
transactionMock.WithCallFunc(func(ctx context.Context, request *transaction.TxRequest) (result []byte, err error) {
336+
if *request.To == bzzTokenAddress {
337+
return balance.FillBytes(make([]byte, 32)), nil
338+
}
339+
if *request.To == stakingContractAddress {
340+
return getPotentialStakeResponse(t, prevStake), nil
341+
}
342+
return nil, errors.New("unexpected call")
343+
}),
344+
),
345+
nonce,
346+
false,
347+
1,
348+
)
349+
350+
_, err = contract.DepositStake(ctx, stakedAmount)
351+
if err != nil {
352+
t.Fatal(err)
353+
}
354+
})
355+
356+
t.Run("insufficient stake amount extra height", func(t *testing.T) {
357+
t.Parallel()
358+
359+
totalAmount := big.NewInt(0).Mul(staking.MinimumStakeAmount, big.NewInt(10))
297360
prevStake := big.NewInt(0)
298361

299362
contract := staking.New(
@@ -314,11 +377,11 @@ func TestDepositStake(t *testing.T) {
314377
),
315378
nonce,
316379
false,
317-
stakingHeight,
380+
1,
318381
)
319382

320-
_, err := contract.DepositStake(ctx, big.NewInt(100000000000000000))
321-
if !errors.Is(err, staking.ErrInsufficientFunds) {
383+
_, err := contract.DepositStake(ctx, stakedAmount)
384+
if !errors.Is(err, staking.ErrInsufficientStakeAmount) {
322385
t.Fatal(fmt.Errorf("wanted %w, got %w", staking.ErrInsufficientStakeAmount, err))
323386
}
324387
})

0 commit comments

Comments
 (0)