Skip to content

Commit

Permalink
[AUDIT FIX] decrement total delegations in adjust delegated balance
Browse files Browse the repository at this point in the history
  • Loading branch information
sampocs committed Jul 9, 2024
1 parent 25ed61b commit 66041f3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
12 changes: 12 additions & 0 deletions x/staketia/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ func (k msgServer) AdjustDelegatedBalance(goCtx context.Context, msg *types.MsgA
}
k.SetHostZone(ctx, hostZone)

// Repeat the same thing on the stakeibc host zone
stakeibcHostZone, found := k.stakeibcKeeper.GetHostZone(ctx, types.CelestiaChainId)
if !found {
return nil, errors.New("celestia host zone not found in stakeibc")
}
stakeibcHostZone.TotalDelegations = stakeibcHostZone.TotalDelegations.Add(msg.DelegationOffset)

if stakeibcHostZone.TotalDelegations.IsNegative() {
return nil, types.ErrNegativeNotAllowed.Wrapf("offset would cause the delegated balance to be negative")
}
k.stakeibcKeeper.SetHostZone(ctx, stakeibcHostZone)

// create a corresponding slash record
latestSlashRecordId := k.IncrementSlashRecordId(ctx)
slashRecord := types.SlashRecord{
Expand Down
13 changes: 11 additions & 2 deletions x/staketia/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"

stakeibctypes "github.com/Stride-Labs/stride/v22/x/stakeibc/types"
"github.com/Stride-Labs/stride/v22/x/staketia/types"
)

Expand Down Expand Up @@ -156,11 +157,15 @@ func (s *KeeperTestSuite) TestAdjustDelegatedBalance() {

safeAddress := "safe"

// Create the host zone
// Create the host zones
s.App.StaketiaKeeper.SetHostZone(s.Ctx, types.HostZone{
SafeAddressOnStride: safeAddress,
RemainingDelegatedBalance: sdk.NewInt(0),
})
s.App.StakeibcKeeper.SetHostZone(s.Ctx, stakeibctypes.HostZone{
ChainId: types.CelestiaChainId,
TotalDelegations: sdk.NewInt(0),
})

// we're halting the zone to test that the tx works even when the host zone is halted
s.App.StaketiaKeeper.HaltZone(s.Ctx)
Expand All @@ -187,7 +192,11 @@ func (s *KeeperTestSuite) TestAdjustDelegatedBalance() {
s.Require().NoError(err, "no error expected when adjusting delegated bal properly for %s", tc.address)

hostZone := s.MustGetHostZone()
s.Require().Equal(tc.endDelegation, hostZone.RemainingDelegatedBalance, "delegation after change for %s", tc.address)
s.Require().Equal(tc.endDelegation, hostZone.RemainingDelegatedBalance, "remaining delegation after change for %s", tc.address)

stakeibcHostZone, found := s.App.StakeibcKeeper.GetHostZone(s.Ctx, types.CelestiaChainId)
s.Require().True(found)
s.Require().Equal(tc.endDelegation, stakeibcHostZone.TotalDelegations, "total delegation after change for %s", tc.address)
}

// Attempt to call it with an amount that would make it negative, it should fail
Expand Down

0 comments on commit 66041f3

Please sign in to comment.