node icon indicating copy to clipboard operation
node copied to clipboard

fix(market): bound bids per order with a monotonic bids-ever-created counter

Open renezander030 opened this issue 5 months ago • 3 comments

Follow-up to #2068, implementing the design discussed in https://github.com/akash-network/node/pull/2068#issuecomment-4666732089 (ack: https://github.com/akash-network/node/pull/2068#issuecomment-4676418653).

Note: stacked on #2068 — the first commit here is that PR's commit; the diff collapses to the second commit once #2068 merges.

Problem

CreateBid enforced OrderMaxBids by scanning the order's open, active and closed bids on every call:

  • Counting closed bids was the only backpressure on an order, so the count doubled as a lifetime cap. Excluding them (as proposed in the #2068 review thread) would have opened a churn loop: a provider cycles CreateBid > CloseBid forever, never hits the cap, and every cycle leaves a BidClosed record behind.
  • The scan missed BidLost entirely, so lost bids escaped the cap.
  • It cost an iteration over three state indexes per CreateBid.

Change

  • New per-order monotonic counter (bid_counts, prefix 0x12 0x06): bumped on CreateBid, never decremented when a bid closes or loses. The cap check reads it in O(1), and total bid records per order stay bounded by OrderMaxBids regardless of churn — closed and lost bids included.
  • Lazy seeding for orders that predate the counter: when no counter record exists, the count is derived from a one-time scan of all stored bids (all four states). The cap therefore carries over an upgrade or genesis import unchanged, and no store migration is needed. The counter is read (and seeded) before the new bid is stored so the seed never double-counts the bid being created.
  • BidCountForOrder now returns only live (open or active) bids, for consumers that care about current competition on an order rather than the lifetime cap.

Tests

  • TestCreateBidCapSurvivesBidChurn — closing a bid drops the live count to zero but does not free cap capacity; a further bid is rejected.
  • TestBidsEverCreatedLazySeedsFromStore — bids seeded directly in the store (genesis-style, no counter record) still count toward the cap, including closed and lost ones.
  • TestCreateBidExceedsOrderMaxBids from #2068 passes unchanged, now exercising the counter path.

go test ./x/market/... -count=1 passes; full repo builds.

🤖 Generated with Claude Code

renezander030 avatar Jun 11 '26 08:06 renezander030

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4a1964a3-3bef-4e46-9827-768bbba068c6

📥 Commits

Reviewing files that changed from the base of the PR and between 748681ecccea63e4842910008d5e4dd9ea48930d and 8c5280f1e8964ac30e2c2cfdbc708a5d9b5a352e.

📒 Files selected for processing (1)
  • x/market/keeper/keeper.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • x/market/keeper/keeper.go

Walkthrough

This PR switches bid-cap enforcement from live bid count to a monotonic “bids ever created” counter. The keeper adds persistent counter storage and lazy seeding, the handler validates against the new cap rule, and tests cover cap enforcement and counter initialization behavior.

Changes

Bid Capacity Counter Implementation

Layer / File(s) Summary
Storage Schema Setup
x/market/keeper/keys/key.go, x/market/keeper/keeper.go
BidCountPrefix is added; Keeper gains bidCounts; schema registration and keeper wiring are updated.
Keeper API Contract & Imports
x/market/keeper/keeper.go
IKeeper now exposes BidsEverCreatedForOrder, and errors is imported for missing-counter handling.
Keeper Counting Implementation
x/market/keeper/keeper.go
BidCountForOrder now counts only live bids; BidsEverCreatedForOrder lazily seeds from stored bids and CreateBid persists the incremented monotonic count.
Handler Capacity Validation
x/market/handler/server.go
CreateBid checks the ever-created count with >= OrderMaxBids before allowing new bids.
Regression Tests
x/market/handler/handler_test.go
Three tests cover pre-seeded cap rejection, cap survival after bid closure, and lazy seeding from existing stored bids.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Poem

🐰 I counted bids both old and new,
And kept the cap from slipping through.
Close a bid? The count stays tall,
So state won’t grow without a wall.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% 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 describes the main change: enforcing per-order bid caps with a monotonic bids-ever-created counter.
Description check ✅ Passed The description directly explains the cap bug, the monotonic counter fix, lazy seeding, and the added tests.
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.
✨ 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 11 '26 08:06 coderabbitai[bot]

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]

@chalabi2 friendly ping — this is the monotonic-counter follow-up you green-lit on #2068. Ready for review whenever you have a moment, thanks!

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]

This was auto-closed by the stale bot, not on review grounds. @chalabi2 you green-lit this direction back on #2068 ("go for it"), and it's clean — CodeRabbit found nothing actionable and the commits are signed. The off-by-one it addresses is still live in master (x/market/handler/server.go still uses > against OrderMaxBids).

I can't reopen from my side (the repo blocks reopening bot-closed PRs), but the branch and commits are all intact. Could you reopen it from your end, or would you rather I open a fresh PR from the same branch? Happy to rebase if main has drifted. Would like to get this landed rather than let it lapse a second time.

renezander030 avatar Jul 10 '26 05:07 renezander030