Skip to content

Commit

Permalink
dev: return err instead of panic, require no error in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hard-nett committed Mar 11, 2025
1 parent dc38640 commit 64d02b0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 41 deletions.
46 changes: 21 additions & 25 deletions app/upgrades/v022/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func CreateV022UpgradeHandler(mm *module.Manager, configurator module.Configurat
}

// apply logic patch
CustomV022PatchLogic(sdkCtx, k, false)
err = CustomV022PatchLogic(sdkCtx, k, false)

logger.Info(fmt.Sprintf("post migrate version map: %v", versionMap))
return versionMap, err
Expand All @@ -43,7 +43,7 @@ func CustomV022PatchLogic(ctx sdk.Context, k *keepers.AppKeepers, simulated bool

err := CustomValPatch(ctx, k, simulated)
if err != nil {
panic(err)
return err
}

return nil
Expand All @@ -61,18 +61,18 @@ func CustomValPatch(sdkCtx sdk.Context, k *keepers.AppKeepers, simulated bool) e

allVals, err := k.StakingKeeper.GetAllValidators(sdkCtx)
if err != nil {
panic(err)
return err
}

for _, val := range allVals {
valAddr, err := k.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
if err != nil {
panic(err)
return err
}

dels, err := k.StakingKeeper.GetValidatorDelegations(sdkCtx, sdk.ValAddress(valAddr))
if err != nil {
panic(err)
return err
}
/* patch all delegators rewards */
for _, del := range dels {
Expand All @@ -85,27 +85,27 @@ func CustomValPatch(sdkCtx sdk.Context, k *keepers.AppKeepers, simulated bool) e
DelegatorAddress: del.DelegatorAddress,
})
if err != nil {
panic(err)
return err
}
continue
}
val, err := sdk.ValAddressFromBech32(del.ValidatorAddress)
if err != nil {
panic(err)
return err
}
delAddr, err := k.AccountKeeper.AddressCodec().StringToBytes(del.GetDelegatorAddr())
if err != nil {
panic(err)
return err
}
validator, err := k.StakingKeeper.Validator(sdkCtx, val)
if err != nil {
panic(err)
return err
}

// end current period and calculate rewards
endingPeriod, err := k.DistrKeeper.IncrementValidatorPeriod(sdkCtx, validator)
if err != nil {
panic(err)
return err
}
delegationRewards, patched := CustomCalculateDelegationRewards(sdkCtx, k, validator, del, endingPeriod)
if patched {
Expand All @@ -124,38 +124,36 @@ func CustomValPatch(sdkCtx sdk.Context, k *keepers.AppKeepers, simulated bool) e
_, err := CustomWithdrawDelegationRewards(sdkCtx, k, validator, del, endingPeriod)

if err != nil {
panic(err)
return err

}

// reinitialize the delegation
err = customInitializeDelegation(sdkCtx, *k, val, delAddr)
if err != nil {
panic(err)
return err
}
}

}

if val.OperatorAddress == PatchVal1 ||
val.OperatorAddress == PatchVal2 {
if val.OperatorAddress == PatchVal1 || val.OperatorAddress == PatchVal2 {
rewards, err := k.DistrKeeper.GetValidatorCurrentRewards(sdkCtx, valAddr)
if err != nil {
panic(err)
return err
}
// set ghost slash event
err = k.DistrKeeper.SetValidatorSlashEvent(sdkCtx, valAddr, 1, rewards.Period, distrtypes.NewValidatorSlashEvent(rewards.Period, math.LegacySmallestDec()))
if err != nil {
panic(err)
return err
}
}
}

k.DistrKeeper.IterateValidatorHistoricalRewards(sdkCtx,
func(val sdk.ValAddress, period uint64, rewards distrtypes.ValidatorHistoricalRewards) (stop bool) {
if val.String() == PatchVal1 ||
val.String() == PatchVal2 {
// print to logs and update validator reward reference to 1
if val.String() == PatchVal1 || val.String() == PatchVal2 {
// print to logs and update validator reward reference to 1
condJSON.PatchedHistRewards = append(condJSON.PatchedHistRewards, distrtypes.ValidatorHistoricalRewardsRecord{
ValidatorAddress: val.String(),
Period: period,
Expand All @@ -172,23 +170,21 @@ func CustomValPatch(sdkCtx sdk.Context, k *keepers.AppKeepers, simulated bool) e
},
)

err = PrintConditionalJsonLogs(condJSON, "conditionalsUpgradeHandler.json")
if err != nil {
panic(err)
}
PrintConditionalJsonLogs(condJSON, "conditionalsUpgradeHandler.json")

return nil
}

func PrintConditionalJsonLogs(condJSON ConditionalJSON, fileName string) error {
// Marshal the ConditionalJSON object to JSON
jsonBytes, err := json.MarshalIndent(condJSON, "", " ")
if err != nil {
panic(err)
fmt.Printf("Failed to marshal and indent conditional logs, continuing with upgrade... %s\n", fileName)
}
// Write the JSON to a file
err = os.WriteFile(fileName, jsonBytes, 0644)
if err != nil {
panic(err)
fmt.Printf("Failed to write debugging log, continuing with upgrade... %s\n", fileName)
}
fmt.Printf("Wrote conditionals to %s\n", fileName)
return nil
Expand Down
21 changes: 5 additions & 16 deletions app/upgrades/v022/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ func (s *UpgradeTestSuite) TestUpgrade() {
)

err := s.App.AppKeepers.StakingKeeper.SetValidator(s.Ctx, val)
if err != nil {
panic(err)
}
s.Require().NoError(err)
// store 0 share delegation to fist validator if test requires
if i == 0 && zeroDel {
s.App.AppKeepers.StakingKeeper.SetDelegation(s.Ctx, stakingtypes.NewDelegation(s.TestAccs[0].String(), val.OperatorAddress, math.LegacyZeroDec()))
Expand All @@ -72,26 +70,17 @@ func (s *UpgradeTestSuite) TestUpgrade() {
// }
// withdraw all delegator rewards
dels, err := s.App.AppKeepers.StakingKeeper.GetAllDelegations(s.Ctx)
if err != nil {
panic(err)
}
s.Require().NoError(err)
for _, delegation := range dels {
valAddr, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress)
if err != nil {
panic(err)
}

s.Require().NoError(err)
delAddr, err := sdk.AccAddressFromBech32(delegation.DelegatorAddress)
if err != nil {
panic(err)
}
s.Require().NoError(err)
fmt.Println("~~~~~~~~~ POST UPGRADE DEBUG ~~~~~~~~~~~~")
fmt.Printf("delAddr: %v\n", delAddr)
fmt.Printf("valAddr: %v\n", valAddr)
_, err = s.App.AppKeepers.DistrKeeper.WithdrawDelegationRewards(s.Ctx, delAddr, valAddr)
if err != nil {
panic(err)
}
s.Require().NoError(err)
}
}

Expand Down

0 comments on commit 64d02b0

Please sign in to comment.