node icon indicating copy to clipboard operation
node copied to clipboard

fix(market): enforce OrderMaxBids with >= instead of >

Open renezander030 opened this issue 5 months ago โ€ข 7 comments

Hi, and thanks for all the work on the node.

This fixes the OrderMaxBids off-by-one reported in akash-network/support#413.

CreateBid in x/market/handler/server.go compared the existing bid count against OrderMaxBids with >:

if ms.keepers.Market.BidCountForOrder(ctx, msg.ID.OrderID()) > params.OrderMaxBids {

BidCountForOrder counts Open, Active and Closed bids, so the count reaches OrderMaxBids exactly when the cap is hit. With >, the check only fired at OrderMaxBids + 1, so one extra bid was always allowed. This switches the comparison to >=.

Test

Added TestCreateBidExceedsOrderMaxBids: it sets the cap to 1, seeds one bid via the keeper, then asserts a second bid through the handler is rejected with "too many existing bids". It fails on the old > (the second bid falls through to an "unknown provider" error) and passes with >=.

go test ./x/market/handler/ and go vet ./x/market/handler/ pass, and gofmt is clean.

Note

This changes consensus behaviour (a bid that previously succeeded now fails), so it belongs in a coordinated upgrade. I saw the code freeze for the upcoming network upgrade, so feel free to queue it for whenever that lands. Happy to adjust if you would rather have BidCountForOrder exclude Closed bids, which the issue flags as a possible follow-up.

renezander030 avatar Jun 03 '26 06:06 renezander030

Review Change Stack

[!CAUTION]

Review failed

The head commit changed during the review from 245de0bde92ac496e0b0def5f80dc2a316f96411 to c5c1fc8b33071fe0e61f3993f9004787813e3707.

Walkthrough

CreateBid's bid-count check was changed from > to >= to reject bids when the maximum is reached. A new regression test seeds an order at the cap and verifies an additional bid is rejected with mv1.ErrInvalidBid and "too many existing bids".

Changes

Bid Maximum Boundary Condition Fix

Layer / File(s) Summary
Bid count boundary condition fix and regression test
x/market/handler/server.go, x/market/handler/handler_test.go
The CreateBid bid-count limit check changed from > params.OrderMaxBids to >= params.OrderMaxBids, rejecting bids when the count reaches the configured maximum. A new regression test TestCreateBidExceedsOrderMaxBids configures OrderMaxBids to 1, seeds one bid via the keeper, and confirms the handler rejects an extra MsgCreateBid with mv1.ErrInvalidBid and a "too many existing bids" message.

Estimated code review effort

๐ŸŽฏ 2 (Simple) | โฑ๏ธ ~8 minutes

Poem

I hopped through the code with bright-eyed cheer,
Found a cap reached and held it near,
I nudged the check to stop at the line,
Seeded a bid, then ran the sign,
Now extra bids fail โ€” the logs are clear! ๐Ÿ‡โœจ

๐Ÿšฅ Pre-merge checks | โœ… 4 | โŒ 1

โŒ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage โš ๏ธ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
โœ… Passed checks (4 passed)
Check name Status Explanation
Title check โœ… Passed The title accurately summarizes the main change: fixing OrderMaxBids enforcement by changing the comparison operator from > to >=.
Description check โœ… Passed The description clearly explains the off-by-one bug, the fix, the test added, and testing results, all directly related to the changeset.
Linked Issues check โœ… Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check โœ… Passed Check skipped because no linked issues were found for this pull request.

โœ๏ธ Tip: You can configure your own custom pre-merge checks in the settings.

โœจ Finishing Touches
๐Ÿงช Generate unit tests (beta)
  • [ ] Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

โค๏ธ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot] avatar Jun 03 '26 06:06 coderabbitai[bot]

Thanks! I left that out of scope here since this PR is just the off-by-one, but it's worth digging into.

Closed bids count today because BidCountForOrder is the only backpressure on an order, so that count doubles as a lifetime cap. If you drop BidClosed from it, the DOS and state inflation turn into the same bug: a provider loops CreateBid > CloseBid > CreateBid forever, never hits the cap, and every cycle leaves a BidClosed record until the order settles.

So closed bids can't just be excluded, you'd need to replace the cap that count was providing. One clean option is a monotonic per-order counter (bids-ever-created), bumped on CreateBid and never decremented on close. CreateBid caps on that, so total bids stay bounded regardless of churn, which kills both the DOS and the record buildup, while a separate live count can still exclude closed bids for anything that cares about current competition. It's also O(1) instead of the current three-state scan.

I can spin that into a follow-up PR if the direction sounds right.

renezander030 avatar Jun 10 '26 05:06 renezander030

I can spin that into a follow-up PR if the direction sounds right.

Yeah that would be great, your implementation idea sounds similar to what I had in mind so go for it.

chalabi2 avatar Jun 11 '26 01:06 chalabi2

@renezander030 can you sign your commits?

chalabi2 avatar Jun 11 '26 01:06 chalabi2

Commits are signed now. Follow-up PR with the monotonic counter is up: #2071.

renezander030 avatar Jun 11 '26 08:06 renezander030

Marked as stale; will be closed in five days. Cut bait or go fishing!

github-actions[bot] avatar Jun 22 '26 03:06 github-actions[bot]

Friendly ping โ€” this one's approved and still relevant. Anything blocking the merge, or is it waiting on a release window? Happy to help if needed.

renezander030 avatar Jun 22 '26 09:06 renezander030

Marked as stale; will be closed in five days. Cut bait or go fishing!

github-actions[bot] avatar Jul 04 '26 02:07 github-actions[bot]

Same note as on #2071 โ€” this was closed by the stale bot, not on review. @chalabi2 the off-by-one is still present in master (BidCountForOrder(...) > params.OrderMaxBids at x/market/handler/server.go:45), so this fix is still needed and isn't handled elsewhere. Commits are signed and CodeRabbit is clean.

I can't reopen it myself (repo blocks reopening bot-closed PRs); the branch is intact. Could you reopen from your side, or should I resubmit as a new PR? Happy to help however's least friction.

renezander030 avatar Jul 10 '26 05:07 renezander030