-
Notifications
You must be signed in to change notification settings - Fork 190
9.5 upgrade & add underflow protection for referred volume #3261
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7cbeb01
9.5 upgrade & add underflow protection
jusbar23 4983747
Remove sort
jusbar23 6324145
Change Version*
jusbar23 1b5f555
Clamp underflow
jusbar23 bb030f6
Lint
jusbar23 f91a7e7
Rename upgrade test
jusbar23 83ab1e1
Upgrade container test
jusbar23 a87ac34
Update pregenesis
jusbar23 dce8636
Add upgrade test
jusbar23 bc85147
Fix test
jusbar23 19286e4
Up
jusbar23 cb3861e
Update upgrade to v9.5
jusbar23 f5e243b
Fix sort
jusbar23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package v_9_5 | ||
|
|
||
| import ( | ||
| store "cosmossdk.io/store/types" | ||
| "github.com/dydxprotocol/v4-chain/protocol/app/upgrades" | ||
| ) | ||
|
|
||
| const ( | ||
| UpgradeName = "v9.5" | ||
| ) | ||
|
|
||
| var Upgrade = upgrades.Upgrade{ | ||
| UpgradeName: UpgradeName, | ||
| StoreUpgrades: store.StoreUpgrades{}, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| package v_9_5 | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "sort" | ||
|
|
||
| upgradetypes "cosmossdk.io/x/upgrade/types" | ||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
| "github.com/cosmos/cosmos-sdk/types/module" | ||
|
|
||
| "github.com/dydxprotocol/v4-chain/protocol/lib" | ||
| epochskeeper "github.com/dydxprotocol/v4-chain/protocol/x/epochs/keeper" | ||
| statskeeper "github.com/dydxprotocol/v4-chain/protocol/x/stats/keeper" | ||
| statstypes "github.com/dydxprotocol/v4-chain/protocol/x/stats/types" | ||
| ) | ||
|
|
||
| // Migrate30dReferredVolumeToEpochStats migrates all users' 30d referred volume | ||
| // to epoch stats in the current epoch. | ||
| func Migrate30dReferredVolumeToEpochStats( | ||
| ctx sdk.Context, | ||
| statsKeeper statskeeper.Keeper, | ||
| epochsKeeper epochskeeper.Keeper, | ||
| ) { | ||
| // Get the current stats epoch | ||
| statsEpochInfo := epochsKeeper.MustGetStatsEpochInfo(ctx) | ||
| currentEpoch := statsEpochInfo.CurrentEpoch | ||
|
|
||
| ctx.Logger().Info(fmt.Sprintf( | ||
| "Migrating 30d referred volume to epoch stats for epoch %d", | ||
| currentEpoch, | ||
| )) | ||
|
|
||
| // Get or create epoch stats for current epoch | ||
| epochStats := statsKeeper.GetEpochStatsOrNil(ctx, currentEpoch) | ||
| if epochStats == nil { | ||
| epochStats = &statstypes.EpochStats{ | ||
| Stats: []*statstypes.EpochStats_UserWithStats{}, | ||
| } | ||
| } | ||
|
|
||
| // Create a map for existing epoch stats for quick lookup | ||
| userStatsMap := make(map[string]*statstypes.EpochStats_UserWithStats) | ||
| for _, userWithStats := range epochStats.Stats { | ||
| userStatsMap[userWithStats.User] = userWithStats | ||
| } | ||
|
|
||
| // Get all addresses with referred volume from the global UserStats | ||
| allAddressesWithReferredVolume := statsKeeper.GetAllAddressesWithReferredVolume(ctx) | ||
|
|
||
| migratedCount := 0 | ||
|
|
||
| for _, address := range allAddressesWithReferredVolume { | ||
| // Get the global user stats which contains the 30d referred volume | ||
| globalUserStats := statsKeeper.GetUserStats(ctx, address) | ||
| if globalUserStats == nil { | ||
| continue | ||
| } | ||
|
|
||
| referredVolume := globalUserStats.Affiliate_30DReferredVolumeQuoteQuantums | ||
|
|
||
| // Get or create user stats for this epoch | ||
| epochUserStats, exists := userStatsMap[address] | ||
| if !exists { | ||
| // User not in epoch stats yet, create new entry | ||
| epochUserStats = &statstypes.EpochStats_UserWithStats{ | ||
| User: address, | ||
| Stats: &statstypes.UserStats{ | ||
| Affiliate_30DReferredVolumeQuoteQuantums: referredVolume, | ||
| }, | ||
| } | ||
| userStatsMap[address] = epochUserStats | ||
| } else { | ||
| // User already in epoch stats, add the referred volume | ||
| epochUserStats.Stats.Affiliate_30DReferredVolumeQuoteQuantums += referredVolume | ||
| } | ||
|
|
||
| migratedCount++ | ||
|
|
||
| ctx.Logger().Info(fmt.Sprintf( | ||
| "Migrated referred volume for address %s (%d of %d): Affiliate_30DReferredVolumeQuoteQuantums=%d", | ||
| address, | ||
| migratedCount, | ||
| len(allAddressesWithReferredVolume), | ||
| referredVolume, | ||
jusbar23 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| )) | ||
| } | ||
|
|
||
| // Convert map back to slice - must be deterministic to avoid state hash mismatch | ||
| keys := make([]string, 0, len(userStatsMap)) | ||
| for k := range userStatsMap { | ||
| keys = append(keys, k) | ||
| } | ||
| sort.Strings(keys) | ||
| epochStats.Stats = make([]*statstypes.EpochStats_UserWithStats, 0, len(userStatsMap)) | ||
| for _, k := range keys { | ||
| epochStats.Stats = append(epochStats.Stats, userStatsMap[k]) | ||
| } | ||
|
|
||
| // Save the updated epoch stats | ||
| statsKeeper.SetEpochStats(ctx, currentEpoch, epochStats) | ||
|
|
||
| ctx.Logger().Info(fmt.Sprintf( | ||
| "Successfully migrated 30d referred volume for %d addresses to epoch %d", | ||
| migratedCount, | ||
| currentEpoch, | ||
| )) | ||
| } | ||
|
|
||
| func CreateUpgradeHandler( | ||
| mm *module.Manager, | ||
| configurator module.Configurator, | ||
| statsKeeper statskeeper.Keeper, | ||
| epochsKeeper epochskeeper.Keeper, | ||
| ) upgradetypes.UpgradeHandler { | ||
| return func(ctx context.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { | ||
| sdkCtx := lib.UnwrapSDKContext(ctx, "app/upgrades") | ||
| sdkCtx.Logger().Info(fmt.Sprintf("Running %s Upgrade...", UpgradeName)) | ||
|
|
||
| // Migrate 30d referred volume to epoch stats | ||
| Migrate30dReferredVolumeToEpochStats(sdkCtx, statsKeeper, epochsKeeper) | ||
|
|
||
| sdkCtx.Logger().Info(fmt.Sprintf("Successfully completed %s Upgrade", UpgradeName)) | ||
|
|
||
| return mm.RunMigrations(ctx, configurator, vm) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.