Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed zero error case in slash query #1025

Merged
merged 32 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
cc5a1c2
undel host: rm protos, proto-gen
riley-stride Oct 11, 2023
7cbafdd
undel host: revert all logic
riley-stride Oct 11, 2023
c0b6b99
undel host: revert all logic, add back undelcb proto + proto-gen
riley-stride Oct 11, 2023
2742044
Merge branch 'cleanup_v15' of ssh://github.com/Stride-Labs/stride int…
riley-stride Oct 11, 2023
4c42dfe
undel host: re-add tx update delegation
riley-stride Oct 11, 2023
e8bad99
calibration: revert proto + protogen; re-add delegation protos from u…
riley-stride Oct 11, 2023
876b1e0
calibration: logic cleanup
riley-stride Oct 11, 2023
00f5f27
calibration: fix typo
riley-stride Oct 11, 2023
2541d18
Revert "calibration: fix typo"
riley-stride Oct 11, 2023
7f8e57e
Revert "calibration: logic cleanup"
riley-stride Oct 11, 2023
d897ace
Revert "calibration: revert proto + protogen; re-add delegation proto…
riley-stride Oct 11, 2023
753bdce
undel host: add back undelegatecallback + proto-gen
riley-stride Oct 11, 2023
57d61c6
calibrate: rm admin gate
riley-stride Oct 11, 2023
9463f14
calibration: do not block ICAs
riley-stride Oct 11, 2023
9e2c398
calibration: limit calibration adjustment to record keeping to <1000m…
riley-stride Oct 11, 2023
dea43a7
calibration: limit calibration adjustment to record keeping to <1000m…
riley-stride Oct 11, 2023
1b1095b
Merge branch 'cleanup_v15' of ssh://github.com/Stride-Labs/stride int…
riley-stride Oct 11, 2023
856bf8c
add calibrate delegation test
riley-stride Oct 11, 2023
6ca4fef
raise calibration threshold
riley-stride Oct 31, 2023
834898c
small fixes per sam's pr comments
riley-stride Nov 9, 2023
86cbe4a
Set CalibrationThreshold to 3000
riley-stride Nov 30, 2023
e4b05ad
add test for slash beyond threshold
riley-stride Dec 4, 2023
ed6fce9
Merge branch 'main' of ssh://github.com/Stride-Labs/stride into clean…
riley-stride Dec 4, 2023
bf47e1b
fix errors.go
riley-stride Dec 4, 2023
2996313
remove callback data for calibrationquery (sam's pr comment)
riley-stride Dec 4, 2023
b953092
fix typo
riley-stride Dec 4, 2023
6542233
Unit tests for calibration ICQ callback (#1013)
sampocs Dec 5, 2023
06144f3
added back GetValidatorFromAddress check
riley-stride Dec 6, 2023
5ba3c7a
Merge branch 'main' into cleanup_v15
riley-stride Dec 6, 2023
e981184
Merge branch 'main' into cleanup_v15
riley-stride Dec 7, 2023
c33a78c
removed zero error case in slash query
sampocs Dec 12, 2023
0b1871f
Merge branch 'main' into sam/slash-query-zero-case
sampocs Dec 14, 2023
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
5 changes: 0 additions & 5 deletions x/stakeibc/keeper/icqcallbacks_delegator_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ func DelegatorSharesCallback(k Keeper, ctx sdk.Context, args []byte, query icqty
return errorsmod.Wrapf(types.ErrValidatorNotFound, "no registered validator for address (%s)", queriedDelegation.ValidatorAddress)
}

// Check if delegation is zero since this will affect measuring the slash amount
if validator.Delegation.IsZero() {
return errorsmod.Wrapf(types.ErrNoValidatorAmts, "Current delegation to validator is zero, unable to check slash magnitude %+v", validator)
}

// Check if the ICQ overlapped a delegation, undelegation, or detokenization ICA
// that would have modfied the number of delegated tokens
prevInternalDelegation := callbackData.InitialValidatorDelegation
Expand Down
82 changes: 69 additions & 13 deletions x/stakeibc/keeper/icqcallbacks_delegator_shares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ func (s *KeeperTestSuite) TestDelegatorSharesCallback_Retry_DelegationChange() {
// Confirm the validator's delegation was not modified
hostZone, found := s.App.StakeibcKeeper.GetHostZone(s.Ctx, tc.hostZone.ChainId)
s.Require().True(found, "host zone found")
s.Require().Equal(initialDelegation.Int64(), hostZone.Validators[tc.valIndexQueried].Delegation.Int64(), "validator delegation")

validator := hostZone.Validators[tc.valIndexQueried]
s.Require().Equal(initialDelegation.Int64(), validator.Delegation.Int64(), "validator delegation")
s.Require().True(validator.SlashQueryInProgress, "slash query should still be in progress")

// Confirm the query was resubmitted
// The new delegation amount should be stored in the callback data
Expand Down Expand Up @@ -235,7 +238,9 @@ func (s *KeeperTestSuite) TestDelegatorSharesCallback_Retry_DelegationICAInProgr
s.Require().True(found, "host zone found")

initialDelegation := initialHostZone.Validators[tc.valIndexQueried].Delegation
s.Require().Equal(initialDelegation.Int64(), actualHostZone.Validators[tc.valIndexQueried].Delegation.Int64(), "validator delegation")
actualValidator := actualHostZone.Validators[tc.valIndexQueried]
s.Require().Equal(initialDelegation.Int64(), actualValidator.Delegation.Int64(), "validator delegation")
s.Require().True(actualValidator.SlashQueryInProgress, "slash query should still be in progress")

// Confirm the query was resubmitted
s.CheckQueryWasResubmitted(tc, actualHostZone)
Expand Down Expand Up @@ -269,6 +274,7 @@ func (s *KeeperTestSuite) checkStateIfValidatorNotSlashed(tc DelegatorSharesICQC
finalValidator := hostZone.Validators[tc.valIndexQueried]
s.Require().Equal(initialValidator.Weight, finalValidator.Weight, "validator weight should not have updated")
s.Require().Equal(initialValidator.Delegation, finalValidator.Delegation, "validator delegation amount should not have updated")
s.Require().False(finalValidator.SlashQueryInProgress, "slash query in progress flag should be reset to false")
}

func (s *KeeperTestSuite) TestDelegatorSharesCallback_HostZoneNotFound() {
Expand Down Expand Up @@ -373,26 +379,76 @@ func (s *KeeperTestSuite) TestDelegatorSharesCallback_PrecisionError() {

validator := hostZone.Validators[tc.valIndexQueried]
expectedValDelegation := tc.hostZone.Validators[tc.valIndexQueried].Delegation.Add(precisionErrorTokens)
s.Require().False(validator.SlashQueryInProgress, "slash query in progress should have been reset")
s.Require().Equal(expectedValDelegation.Int64(), validator.Delegation.Int64(), "validator delegation amount")
}

func (s *KeeperTestSuite) TestCalibrateDelegationCallback_Successful() {
func (s *KeeperTestSuite) TestDelegatorSharesCallback_ZeroInternalDelegation() {
tc := s.SetupDelegatorSharesICQCallback()
initialValidator := tc.hostZone.Validators[tc.valIndexQueried]

// Callback
err := keeper.CalibrateDelegationCallback(s.App.StakeibcKeeper, s.Ctx, tc.validArgs.callbackArgs, tc.validArgs.query)
s.Require().NoError(err, "calibrate delegation callback error")
// Update the validator so that it currently has 0 assumed tokens
initialValidator.Delegation = sdkmath.ZeroInt()
s.App.StakeibcKeeper.SetHostZone(s.Ctx, tc.hostZone)

// Confirm the staked balance was decreased on the host
tc.validArgs.query.CallbackData, _ = proto.Marshal(&types.DelegatorSharesQueryCallback{
InitialValidatorDelegation: sdk.ZeroInt(),
})

// Update the delegator shares query response so that it shows that there are 5 more tokens delegated
queryTokens := sdk.NewInt(5)
queryShares := sdk.NewDecFromInt(queryTokens).Quo(tc.sharesToTokensRate)

callbackArgs := s.CreateDelegatorSharesQueryResponse(initialValidator.Address, queryShares)
err := keeper.DelegatorSharesCallback(s.App.StakeibcKeeper, s.Ctx, callbackArgs, tc.validArgs.query)
s.Require().NoError(err)

// Confirm host zone and validator were updated
hostZone, found := s.App.StakeibcKeeper.GetHostZone(s.Ctx, HostChainId)
s.Require().True(found, "host zone found")
s.Require().Equal(tc.expectedSlashAmount.Int64(), tc.hostZone.TotalDelegations.Sub(hostZone.TotalDelegations).Int64(), "staked bal slash")

// Confirm the validator's weight and delegation amount were not reduced
expectedTotalDelegation := tc.hostZone.TotalDelegations.Add(queryTokens)
s.Require().Equal(expectedTotalDelegation.Int64(), hostZone.TotalDelegations.Int64(), "host zone staked balance")

validator := hostZone.Validators[tc.valIndexQueried]
s.Require().NotEqual(tc.expectedWeight, validator.Weight, "validator weight")
s.Require().Equal(tc.expectedDelegationAmount.Int64(), validator.Delegation.Int64(), "validator delegation amount")
s.Require().False(validator.SlashQueryInProgress, "slash query in progress should have been reset")
s.Require().Equal(queryTokens.Int64(), validator.Delegation.Int64(), "validator delegation amount")
}

func (s *KeeperTestSuite) TestDelegatorSharesCallback_ZeroExternalDelegation() {
tc := s.SetupDelegatorSharesICQCallback()
initialValidator := tc.hostZone.Validators[tc.valIndexQueried]

// Update the delegator shares query response so that it shows that there are 0 delegated tokens
queryShares := sdk.NewDec(0)

callbackArgs := s.CreateDelegatorSharesQueryResponse(initialValidator.Address, queryShares)
err := keeper.DelegatorSharesCallback(s.App.StakeibcKeeper, s.Ctx, callbackArgs, tc.validArgs.query)
s.Require().NoError(err)

// Confirm host zone and validator were updated
hostZone, found := s.App.StakeibcKeeper.GetHostZone(s.Ctx, HostChainId)
s.Require().True(found, "host zone found")

expectedTotalDelegation := tc.hostZone.TotalDelegations.Sub(initialValidator.Delegation)
s.Require().Equal(expectedTotalDelegation.Int64(), hostZone.TotalDelegations.Int64(), "host zone staked balance")

validator := hostZone.Validators[tc.valIndexQueried]
s.Require().False(validator.SlashQueryInProgress, "slash query in progress should have been reset")
s.Require().Zero(validator.Delegation.Int64(), "validator delegation amount")

// Run the callback again, and confirm there's no failure (testing the case of 0 internal and external)
callbackArgs = s.CreateDelegatorSharesQueryResponse(initialValidator.Address, sdk.ZeroDec())
err = keeper.DelegatorSharesCallback(s.App.StakeibcKeeper, s.Ctx, callbackArgs, tc.validArgs.query)
s.Require().NoError(err)

hostZone, found = s.App.StakeibcKeeper.GetHostZone(s.Ctx, HostChainId)
s.Require().True(found, "host zone found")

expectedTotalDelegation = tc.hostZone.TotalDelegations.Sub(initialValidator.Delegation)
s.Require().Equal(expectedTotalDelegation.Int64(), hostZone.TotalDelegations.Int64(), "host zone staked balance")

// Confirm the validator query is still in progress (calibration callback does not set it false)
s.Require().True(validator.SlashQueryInProgress, "slash query in progress")
validator = hostZone.Validators[tc.valIndexQueried]
s.Require().False(validator.SlashQueryInProgress, "slash query in progress should have been reset")
s.Require().Zero(validator.Delegation.Int64(), "validator delegation amount")
}
Loading