Skip to content

Conversation

@vertex451
Copy link
Contributor

@vertex451 vertex451 commented Jan 28, 2026

Description

Closes: akash-network/support#403

  1. Made sure the reason is stored onchain.
  2. Fixed bug with hardcoded LeaseCloseReason.
  3. Tests, of course.

Manual testing results

Insufficient funds scenario:

akash q market lease get --dseq $DSEQ --owner akash1fjdy5x8v936l34pxgdn3e0cg30k22n7je6hph9 --provider akash1q8t0g9yalktyla6xrmjz4d6j0pyz4etnk2z9rz
escrow_payment:
  id:
    aid:
      scope: deployment
      xid: akash1fjdy5x8v936l34pxgdn3e0cg30k22n7je6hph9/12
    xid: 1/1/akash1q8t0g9yalktyla6xrmjz4d6j0pyz4etnk2z9rz
  state:
    balance:
      amount: "0.000000000000000000"
      denom: uakt
    owner: akash1q8t0g9yalktyla6xrmjz4d6j0pyz4etnk2z9rz
    rate:
      amount: "1031634.370000000000000000"
      denom: uakt
    state: overdrawn
    unsettled:
      amount: "124327758.000000000000000000"
      denom: uakt
    withdrawn:
      amount: "500000"
      denom: uakt
lease:
  closed_on: "7065"
  created_at: "6944"
  id:
    bseq: 0
    dseq: "12"
    gseq: 1
    oseq: 1
    owner: akash1fjdy5x8v936l34pxgdn3e0cg30k22n7je6hph9
    provider: akash1q8t0g9yalktyla6xrmjz4d6j0pyz4etnk2z9rz
  price:
    amount: "1031634.370000000000000000"
    denom: uakt
  reason: lease_closed_reason_insufficient_funds
  state: insufficient_funds

Unstable deployment scenario:

akash q market lease get --dseq $DSEQ --owner akash1fjdy5x8v936l34pxgdn3e0cg30k22n7je6hph9 --provider akash1q8t0g9yalktyla6xrmjz4d6j0pyz4etnk2z9rz
escrow_payment:
  id:
    aid:
      scope: deployment
      xid: akash1fjdy5x8v936l34pxgdn3e0cg30k22n7je6hph9/13
    xid: 1/1/akash1q8t0g9yalktyla6xrmjz4d6j0pyz4etnk2z9rz
  state:
    balance:
      amount: "0.000000000000000000"
      denom: uakt
    owner: akash1q8t0g9yalktyla6xrmjz4d6j0pyz4etnk2z9rz
    rate:
      amount: "13.679111000000000000"
      denom: uakt
    state: closed
    unsettled:
      amount: "0.000000000000000000"
      denom: uakt
    withdrawn:
      amount: "1272"
      denom: uakt
lease:
  closed_on: "7985"
  created_at: "7892"
  id:
    bseq: 0
    dseq: "13"
    gseq: 1
    oseq: 1
    owner: akash1fjdy5x8v936l34pxgdn3e0cg30k22n7je6hph9
    provider: akash1q8t0g9yalktyla6xrmjz4d6j0pyz4etnk2z9rz
  price:
    amount: "13.679111000000000000"
    denom: uakt
  reason: lease_closed_reason_unstable
  state: closed

Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow-up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

@coderabbitai
Copy link

coderabbitai bot commented Jan 28, 2026

Walkthrough

The MarketKeeper.OnGroupClosed API gained a Group_State parameter. Call sites and implementations were updated to pass and act on that state; keeper logic maps group state to lease state/reason, closes related leases and bids, and attempts to close associated escrow payments.

Changes

Cohort / File(s) Summary
Interface Signature Updates
x/deployment/handler/keepers.go, x/market/hooks/external.go, x/market/keeper/keeper.go
MarketKeeper.OnGroupClosed signature changed to include a state (Group_State/dtypes.Group_State); interfaces and implementations updated.
Call Site Updates
x/deployment/handler/server.go, x/market/hooks/hooks.go
Calls to OnGroupClosed now pass the computed group state (e.g., GroupClosed, GroupPaused, GroupInsufficientFunds).
Keeper Logic
x/market/keeper/keeper.go
OnGroupClosed maps group state → lease state and reason (e.g., LeaseInsufficientFunds/LeaseClosedReasonInsufficientFunds), sets lease.Reason, calls OnLeaseClosed per lease, and attempts PaymentClose for escrow accounts (errors logged, not returned).
Tests and Helpers
x/market/handler/handler_test.go, x/market/keeper/keeper_test.go
Tests updated for new lease state/reason expectations; helper removal/refactor; keeper tests expanded to table-driven scenarios and idempotency checks; type/import adjustments to use market aliases.
Changelog
CHANGELOG.md
Added bugfix note about lease close reason.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant MarketKeeper
    participant LeaseModule as Lease
    participant PaymentModule as Payment

    Caller->>MarketKeeper: OnGroupClosed(ctx, groupID, state)
    activate MarketKeeper

    alt state == GroupInsufficientFunds
        MarketKeeper->>Lease: OnLeaseClosed(ctx, leaseID, LeaseInsufficientFunds, LeaseClosedReasonInsufficientFunds)
    else state == GroupClosed
        MarketKeeper->>Lease: OnLeaseClosed(ctx, leaseID, LeaseClosed, LeaseClosedReasonOwner)
    else
        MarketKeeper->>Lease: OnLeaseClosed(ctx, leaseID, <mapped state/reason>)
    end
    activate Lease
    Lease->>Lease: update state and reason
    deactivate Lease

    MarketKeeper->>Payment: PaymentClose(ctx, paymentID)
    activate Payment
    Payment->>Payment: close escrow (log errors, don't fail)
    deactivate Payment

    deactivate MarketKeeper
Loading

Estimated Code Review Effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰 I hopped through fields of code and state,
Groups now tell why leases meet their fate.
I nudged each payment, gave reasons true,
Closed what must close, then danced anew.
🥕 Hop, patch, and test — a tidy view.

🚥 Pre-merge checks | ✅ 3 | ❌ 2
❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Out of Scope Changes check ❓ Inconclusive Changes include updates to the OnGroupClosed method signature to accept an additional Group_State parameter, which extends beyond the linked issue scope and may require clarification. Clarify whether the OnGroupClosed signature change is necessary for the lease close reason fix or if it represents scope creep that should be in a separate PR.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The PR title 'fix: lease close reason' clearly and concisely summarizes the main change: fixing a bug where lease close reasons were not being correctly stored on-chain.
Linked Issues check ✅ Passed The PR successfully addresses issue #403 by fixing the lease close reason to properly record LeaseClosedReasonInsufficientFunds instead of LeaseClosedReasonInvalid in cases where escrow is overdrawn, as demonstrated in the provided examples.
Description check ✅ Passed The PR description is directly related to the changeset, clearly describing the bug fix for lease close reason handling and documenting manual testing results.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch artem/fix-lease-close-reason

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 and usage tips.

@vertex451 vertex451 self-assigned this Jan 28, 2026
@vertex451 vertex451 force-pushed the artem/fix-lease-close-reason branch from 2b0a8b6 to 6ddfa56 Compare January 28, 2026 19:00
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
x/deployment/handler/server.go (1)

156-162: Don’t ignore OnGroupClosed errors.

If OnGroupClosed fails, the deployment group is closed but market state can remain partially open. Propagating the error keeps state transitions atomic.

🔧 Proposed fix
-	_ = ms.market.OnGroupClosed(ctx, group.ID, types.GroupClosed)
+	if err := ms.market.OnGroupClosed(ctx, group.ID, types.GroupClosed); err != nil {
+		return nil, err
+	}
-	_ = ms.market.OnGroupClosed(ctx, group.ID, types.GroupPaused)
+	if err := ms.market.OnGroupClosed(ctx, group.ID, types.GroupPaused); err != nil {
+		return nil, err
+	}

Also applies to: 180-186

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

lease close reason is LeaseClosedReasonInvalid when it should be LeaseClosedReasonInsufficientFunds

2 participants