Skip to content

Commit

Permalink
feat: removeCUFromCC only for units that are not already exited (#1056)
Browse files Browse the repository at this point in the history
Co-authored-by: folex <[email protected]>
  • Loading branch information
shamsartem and folex authored Sep 23, 2024
1 parent c71d0ba commit 4981e80
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions cli/src/lib/chain/commitment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,15 +480,23 @@ export async function collateralWithdraw(

for (const commitment of commitments) {
const { commitmentId } = commitment;
const commitmentInfo = await capacity.getCommitment(commitmentId);
const unitIds = await market.getComputeUnitIds(commitmentInfo.peerId);

const [unitIds, isExitedStatuses] =
await capacity.getUnitExitStatuses(commitmentId);

const units = await batchRead(
unitIds.map((unitId) => {
unitIds.map((unitId, i) => {
return async () => {
return {
unitId,
unitInfo: await market.getComputeUnit(unitId),
isExited:
isExitedStatuses[i] ??
(() => {
throw new Error(
`Unreachable. No exit status returned from getUnitExitStatuses for unit ${unitId}`,
);
})(),
};
};
}),
Expand Down Expand Up @@ -564,9 +572,13 @@ export async function collateralWithdraw(
await signBatch(
`Remove compute units from capacity commitments and finish commitment ${commitmentId}`,
[
...unitIds.map((unitId) => {
return populateTx(capacity.removeCUFromCC, commitmentId, [unitId]);
}),
...units
.filter(({ isExited }) => {
return !isExited;
})
.map(({ unitId }) => {
return populateTx(capacity.removeCUFromCC, commitmentId, [unitId]);
}),
populateTx(capacity.finishCommitment, commitmentId),
],
);
Expand Down

0 comments on commit 4981e80

Please sign in to comment.