-
Notifications
You must be signed in to change notification settings - Fork 122
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
[TRA-483] Implement functionality to acquire next perpetual id #1872
Conversation
Signed-off-by: Shrenuj Bansal <shrenuj@dydx.exchange>
WalkthroughThe changes introduce functionality to manage perpetual IDs within the Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Keeper
participant ModuleStore
User->>Keeper: Call AcquireNextPerpetualID()
Keeper->>ModuleStore: Get current NextPerpetualID
ModuleStore-->>Keeper: Return current NextPerpetualID
Keeper->>Keeper: Increment NextPerpetualID
Keeper->>ModuleStore: Set new NextPerpetualID
Keeper-->>User: Return new PerpetualID
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
Outside diff range and nitpick comments (1)
protocol/x/perpetuals/keeper/perpetual_test.go (1)
3124-3154
: Add more assertions to ensure robustness.The test function covers the intended functionality well. However, consider adding assertions to check for edge cases, such as when no perpetuals exist initially or when IDs reach their maximum value.
// Ensure no perpetuals exist initially pc = keepertest.PerpetualsKeepers(t) require.Equal(t, uint32(0), pc.PerpetualsKeeper.GetNextPerpetualID(pc.Ctx)) // Test acquiring the next perpetual ID when no perpetuals exist nextPerpetualId = pc.PerpetualsKeeper.AcquireNextPerpetualID(pc.Ctx) require.Equal(t, uint32(1), nextPerpetualId) // Ensure the next perpetual ID is stored correctly nextPerpetualIdFromStore = pc.PerpetualsKeeper.GetNextPerpetualID(pc.Ctx) require.Equal(t, nextPerpetualId+1, nextPerpetualIdFromStore) // Test edge case for maximum value pc.PerpetualsKeeper.SetNextPerpetualID(pc.Ctx, math.MaxUint32) require.Panics(t, func() { pc.PerpetualsKeeper.AcquireNextPerpetualID(pc.Ctx) })
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (3)
- protocol/x/perpetuals/keeper/perpetual.go (2 hunks)
- protocol/x/perpetuals/keeper/perpetual_test.go (1 hunks)
- protocol/x/perpetuals/types/keys.go (1 hunks)
Files skipped from review due to trivial changes (1)
- protocol/x/perpetuals/types/keys.go
Additional comments not posted (1)
protocol/x/perpetuals/keeper/perpetual.go (1)
1529-1534
: LGTM!The function correctly sets the next perpetual ID in the store.
// AcquireNextPerpetualID returns the next perpetual id to be used and increments the next perpetual id | ||
func (k Keeper) AcquireNextPerpetualID(ctx sdk.Context) uint32 { | ||
nextID := k.GetNextPerpetualID(ctx) | ||
// if perpetual id already exists, increment until we find one that doesn't | ||
for { | ||
_, err := k.GetPerpetual(ctx, nextID) | ||
if err != nil && errorsmod.IsOf(err, types.ErrPerpetualDoesNotExist) { | ||
break | ||
} | ||
nextID++ | ||
} | ||
|
||
k.SetNextPerpetualID(ctx, nextID+1) | ||
return nextID | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure the loop does not run indefinitely.
Consider adding a maximum limit to the loop to avoid potential infinite loops in case of an unexpected error.
func (k Keeper) AcquireNextPerpetualID(ctx sdk.Context) uint32 {
nextID := k.GetNextPerpetualID(ctx)
// if perpetual id already exists, increment until we find one that doesn't
+ maxAttempts := 1000 // or some reasonable limit
+ attempts := 0
for {
_, err := k.GetPerpetual(ctx, nextID)
if err != nil && errorsmod.IsOf(err, types.ErrPerpetualDoesNotExist) {
break
}
nextID++
+ attempts++
+ if attempts >= maxAttempts {
+ panic("Exceeded maximum attempts to acquire a unique perpetual ID")
+ }
}
k.SetNextPerpetualID(ctx, nextID+1)
return nextID
}
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
// AcquireNextPerpetualID returns the next perpetual id to be used and increments the next perpetual id | |
func (k Keeper) AcquireNextPerpetualID(ctx sdk.Context) uint32 { | |
nextID := k.GetNextPerpetualID(ctx) | |
// if perpetual id already exists, increment until we find one that doesn't | |
for { | |
_, err := k.GetPerpetual(ctx, nextID) | |
if err != nil && errorsmod.IsOf(err, types.ErrPerpetualDoesNotExist) { | |
break | |
} | |
nextID++ | |
} | |
k.SetNextPerpetualID(ctx, nextID+1) | |
return nextID | |
} | |
func (k Keeper) AcquireNextPerpetualID(ctx sdk.Context) uint32 { | |
nextID := k.GetNextPerpetualID(ctx) | |
// if perpetual id already exists, increment until we find one that doesn't | |
maxAttempts := 1000 // or some reasonable limit | |
attempts := 0 | |
for { | |
_, err := k.GetPerpetual(ctx, nextID) | |
if err != nil && errorsmod.IsOf(err, types.ErrPerpetualDoesNotExist) { | |
break | |
} | |
nextID++ | |
attempts++ | |
if attempts >= maxAttempts { | |
panic("Exceeded maximum attempts to acquire a unique perpetual ID") | |
} | |
} | |
k.SetNextPerpetualID(ctx, nextID+1) | |
return nextID | |
} |
Changelist
This change adds a new store key to store the next perpetual id to be used and getter/setter functions
Also provides a function to acquire a new perpetual id
Test Plan
Added tests
Author/Reviewer Checklist
state-breaking
label.indexer-postgres-breaking
label.PrepareProposal
orProcessProposal
, manually add the labelproposal-breaking
.feature:[feature-name]
.backport/[branch-name]
.refactor
,chore
,bug
.Summary by CodeRabbit
New Features
Tests