Skip to content

Commit

Permalink
feat!: group cc infos by status, store offer id right after it's crea…
Browse files Browse the repository at this point in the history
…ted [JS-814, JS-815] (#1059)

* feat: group cc infos by status, store offer id right after it's created, decrease deciderPeriodSec to 15 on local network [JS-814, JS-815, JS-816]

* chore(ci,tests): print load average

* set workerPeriodSec

* fix

* Apply automatic changes

* fix(ci): run uptime always

* remove additional noxes

* fix

* fix

* Apply automatic changes

* fix

* run provider tests first

* fix

* fix

* improve

* restore deciderPeriodSec

* improve

* fix

* fix

---------

Co-authored-by: folex <[email protected]>
Co-authored-by: shamsartem <[email protected]>
  • Loading branch information
3 people authored Sep 30, 2024
1 parent 76b3020 commit e90c087
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 181 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ jobs:
run: yarn local-up
working-directory: cli

- name: Run provider tests
run: yarn vitest-provider
working-directory: cli

- name: Run deal deploy tests
run: yarn vitest-deal-deploy
working-directory: cli
Expand All @@ -296,9 +300,9 @@ jobs:
run: yarn vitest-js-to-aqua
working-directory: cli

- name: Run provider tests
run: yarn vitest-provider
working-directory: cli
- name: Show Runner Load Average
run: uptime
if: always()

- name: Dump container logs
if: always()
Expand Down
12 changes: 6 additions & 6 deletions cli/docs/commands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
* [`fluence provider cc-rewards-withdraw`](#fluence-provider-cc-rewards-withdraw)
* [`fluence provider deal-exit`](#fluence-provider-deal-exit)
* [`fluence provider deal-list`](#fluence-provider-deal-list)
* [`fluence provider deal-rewards-info [DEAL-ADDRESS] [UNIT-ID]`](#fluence-provider-deal-rewards-info-deal-address-unit-id)
* [`fluence provider deal-rewards-info [DEAL-ADDRESS] [ON-CHAIN-WORKER-ID]`](#fluence-provider-deal-rewards-info-deal-address-on-chain-worker-id)
* [`fluence provider deal-rewards-withdraw`](#fluence-provider-deal-rewards-withdraw)
* [`fluence provider gen`](#fluence-provider-gen)
* [`fluence provider info`](#fluence-provider-info)
Expand Down Expand Up @@ -1466,18 +1466,18 @@ ALIASES

_See code: [src/commands/provider/deal-list.ts](https://github.com/fluencelabs/cli/blob/fluence-cli-v0.21.0/src/commands/provider/deal-list.ts)_

## `fluence provider deal-rewards-info [DEAL-ADDRESS] [UNIT-ID]`
## `fluence provider deal-rewards-info [DEAL-ADDRESS] [ON-CHAIN-WORKER-ID]`

Deal rewards info

```
USAGE
$ fluence provider deal-rewards-info [DEAL-ADDRESS] [UNIT-ID] [--no-input] [--env <testnet | mainnet | stage | local |
custom>] [--priv-key <private-key>]
$ fluence provider deal-rewards-info [DEAL-ADDRESS] [ON-CHAIN-WORKER-ID] [--no-input] [--env <testnet | mainnet | stage |
local | custom>] [--priv-key <private-key>]
ARGUMENTS
DEAL-ADDRESS Deal address
UNIT-ID Compute unit ID
DEAL-ADDRESS Deal address
ON-CHAIN-WORKER-ID On-chain worker id
FLAGS
--env=<testnet | mainnet | stage | local | custom> Fluence Environment to use when running the command
Expand Down
2 changes: 2 additions & 0 deletions cli/docs/configs/provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ Decider service configuration
| `startBlock` | string | No | Start block (deprecated) |
| `walletKey` | string | No | Wallet key (deprecated) |
| `workerIpfsMultiaddr` | string | No | Multiaddress of worker IPFS node |
| `workerPeriodSec` | integer | No | Worker period in seconds |

##### vm

Expand Down Expand Up @@ -517,6 +518,7 @@ Decider service configuration
| `startBlock` | string | No | Start block (deprecated) |
| `walletKey` | string | No | Wallet key (deprecated) |
| `workerIpfsMultiaddr` | string | No | Multiaddress of worker IPFS node |
| `workerPeriodSec` | integer | No | Worker period in seconds |

### vm

Expand Down
2 changes: 1 addition & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"vitest-deal-update": "vitest run dealUpdate.test.ts",
"vitest-smoke": "vitest run smoke.test.ts",
"vitest-js-to-aqua": "vitest run jsToAqua.test.ts",
"vitest": "yarn vitest-deal-deploy && yarn vitest-deal-update && yarn vitest-smoke && yarn vitest-js-to-aqua && yarn vitest-provider",
"vitest": "yarn vitest-provider && yarn vitest-deal-deploy && yarn vitest-deal-update && yarn vitest-smoke && yarn vitest-js-to-aqua",
"pack-linux-x64": "oclif pack tarballs -t \"linux-x64\" --no-xz",
"pack-darwin-x64": "oclif pack tarballs -t \"darwin-x64\" --no-xz",
"pack-darwin-arm64": "oclif pack tarballs -t \"darwin-arm64\" --no-xz",
Expand Down
11 changes: 6 additions & 5 deletions cli/src/commands/provider/deal-rewards-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export default class DealRewardsInfo extends BaseCommand<
"DEAL-ADDRESS": Args.string({
description: "Deal address",
}),
"UNIT-ID": Args.string({
description: "Compute unit ID",
"ON-CHAIN-WORKER-ID": Args.string({
description: "On-chain worker id",
}),
};

Expand All @@ -51,12 +51,13 @@ export default class DealRewardsInfo extends BaseCommand<
const dealAddress =
args["DEAL-ADDRESS"] ?? (await input({ message: "Enter deal address" }));

const unitId =
args["UNIT-ID"] ?? (await input({ message: "Enter unit id" }));
const onChainWorkerId =
args["ON-CHAIN-WORKER-ID"] ??
(await input({ message: "Enter on-chain worker id" }));

const { readonlyDealClient } = await getReadonlyDealClient();
const deal = readonlyDealClient.getDeal(dealAddress);
const rewardAmount = await deal.getRewardAmount(unitId);
const rewardAmount = await deal.getRewardAmount(onChainWorkerId);

commandObj.log(
`Provider reward: ${color.yellow(
Expand Down
16 changes: 15 additions & 1 deletion cli/src/commands/provider/deal-rewards-withdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import { BaseCommand, baseFlags } from "../../baseCommand.js";
import { peerIdHexStringToBase58String } from "../../lib/chain/conversions.js";
import { ptFormatWithSymbol } from "../../lib/chain/currencies.js";
import { commandObj } from "../../lib/commandObj.js";
import {
Expand Down Expand Up @@ -66,7 +67,20 @@ export default class DealRewardsWithdraw extends BaseCommand<
let providerRewards = 0n;
let stakerRewards = 0n;

for (const { onchainId } of workers) {
for (const { onchainId, peerId } of workers) {
const rewardAmount = await deal.getRewardAmount(onchainId);

if (
rewardAmount.providerReward === 0n &&
rewardAmount.stakerReward === 0n
) {
commandObj.logToStderr(
`No rewards to withdraw for worker ${onchainId} (${await peerIdHexStringToBase58String(peerId)})`,
);

continue;
}

const txReceipt = await sign({
title: `Withdraw rewards for worker ${onchainId}`,
method: deal.withdrawRewards,
Expand Down
96 changes: 72 additions & 24 deletions cli/src/lib/chain/commitment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { type ICapacity } from "@fluencelabs/deal-ts-clients";
import { CommitmentStatus, type ICapacity } from "@fluencelabs/deal-ts-clients";
import { color } from "@oclif/color";
import isUndefined from "lodash-es/isUndefined.js";
import omitBy from "lodash-es/omitBy.js";
Expand Down Expand Up @@ -478,8 +478,10 @@ export async function collateralWithdraw(
const capacity = dealClient.getCapacity();
const market = dealClient.getMarket();

for (const commitment of commitments) {
const { commitmentId } = commitment;
for (const commitment of commitments.flatMap(({ ccInfos }) => {
return ccInfos;
})) {
const { commitmentId, noxName } = commitment;

const [unitIds, isExitedStatuses] =
await capacity.getUnitExitStatuses(commitmentId);
Expand Down Expand Up @@ -570,7 +572,7 @@ export async function collateralWithdraw(
}

await signBatch(
`Remove compute units from capacity commitments and finish commitment ${commitmentId}`,
`Remove compute units from capacity commitments and finish commitment ${noxName === undefined ? commitmentId : `for ${noxName} (${commitmentId})`} ${commitmentId}`,
[
...units
.filter(({ isExited }) => {
Expand Down Expand Up @@ -606,7 +608,7 @@ export async function collateralRewardWithdraw(flags: CCFlags) {
export function stringifyBasicCommitmentInfo(
commitment:
| Awaited<ReturnType<typeof getCommitments>>[number]
| Awaited<ReturnType<typeof getCommitmentsInfo>>[number],
| Awaited<ReturnType<typeof getCommitmentsInfo>>[number]["ccInfos"][number],
) {
if ("providerConfigComputePeer" in commitment) {
return `${color.yellow(
Expand Down Expand Up @@ -637,6 +639,7 @@ export function stringifyBasicCommitmentInfo(

export async function getCommitmentsInfo(flags: CCFlags) {
const { readonlyDealClient } = await getReadonlyDealClient();
const { CommitmentStatus } = await import("@fluencelabs/deal-ts-clients");
const capacity = readonlyDealClient.getCapacity();
const core = readonlyDealClient.getCore();

Expand All @@ -656,7 +659,7 @@ export async function getCommitmentsInfo(flags: CCFlags) {

const dealExplorerClient = await getDealExplorerClient();

return Promise.all(
const commitmentsInfo = await Promise.all(
commitments.map(async (c) => {
let commitment: Partial<ICapacity.CommitmentViewStructOutput> =
"commitmentCreatedEvent" in c
Expand Down Expand Up @@ -713,6 +716,8 @@ export async function getCommitmentsInfo(flags: CCFlags) {
initTimestamp + commitment.endEpoch * epochDuration,
);

const status = Number(commitment.status);

return {
...("providerConfigComputePeer" in c
? {
Expand All @@ -722,10 +727,7 @@ export async function getCommitmentsInfo(flags: CCFlags) {
: {}),
ccFromExplorer,
commitmentId: c.commitmentId,
status:
commitment.status === undefined
? undefined
: Number(commitment.status),
status: status in CommitmentStatus ? status : undefined,
currentEpoch: bigintToStr(currentEpoch),
startEpoch: optBigIntToStr(commitment.startEpoch),
startDate: ccStartDate,
Expand All @@ -744,6 +746,29 @@ export async function getCommitmentsInfo(flags: CCFlags) {
};
}),
);

// group commitments by status
return Array.from(
commitmentsInfo
.reduce<
Map<CommitmentStatus | undefined, (typeof commitmentsInfo)[number][]>
>((acc, v) => {
const infos = acc.get(v.status) ?? [];
infos.push(v);

acc.set(
v.status !== undefined && v.status in CommitmentStatus
? v.status
: undefined,
infos,
);

return acc;
}, new Map())
.entries(),
).map(([status, ccInfos]) => {
return { status, ccInfos };
});
}

function optBigIntToStr(value: bigint | undefined) {
Expand All @@ -769,25 +794,46 @@ export async function printCommitmentsInfo(flags: CCFlags) {
commandObj.logToStderr(
(
await Promise.all(
ccInfos.map(async (ccInfo) => {
const noxName =
ccInfo.noxName === undefined
? ""
: color.yellow(`Nox: ${ccInfo.noxName}\n`);

return `${noxName}${await getCommitmentInfoString(ccInfo)}`;
ccInfos.map(async ({ status, ccInfos }) => {
return `${await getStatusHeading(status, ccInfos)}${(
await Promise.all(
ccInfos.map(async (ccInfo) => {
const noxName =
ccInfo.noxName === undefined
? ""
: color.yellow(`Nox: ${ccInfo.noxName}\n`);
return `${noxName}${await getCommitmentInfoString(ccInfo)}`;
}),
)
).join("\n\n")}`;
}),
)
).join("\n\n"),
);
}

async function getStatusHeading(
status: CommitmentStatus | undefined,
ccInfos: { noxName?: undefined | string; commitmentId: string }[],
) {
return color.yellow(
`Status: ${await ccStatusToString(status)} (${ccInfos
.map(({ commitmentId, noxName }) => {
return noxName ?? commitmentId;
})
.join(",")})\n\n`,
);
}

export async function printCommitmentsInfoJSON(flags: CCFlags) {
commandObj.log(jsonStringify(await getCommitmentsInfo(flags)));
}

export async function getCommitmentInfoString(
ccInfo: Awaited<ReturnType<typeof getCommitmentsInfo>>[number],
async function getCommitmentInfoString(
ccInfo: Awaited<
ReturnType<typeof getCommitmentsInfo>
>[number]["ccInfos"][number],
) {
const { ethers } = await import("ethers");

Expand Down Expand Up @@ -877,18 +923,20 @@ async function ccStatusToString(status: number | undefined) {
return `Unknown (${numToStr(status)})`;
}

return statusStr;
return statusStr === "Inactive" ? "Completed" : statusStr;
}

export async function basicCCInfoAndStatusToString(
ccInfos: Awaited<ReturnType<typeof getCommitmentsInfo>>,
) {
return (
await Promise.all(
ccInfos.map(async (ccInfo) => {
return `${stringifyBasicCommitmentInfo(
ccInfo,
)} Status: ${await ccStatusToString(ccInfo.status)}`;
ccInfos.map(async ({ status, ccInfos }) => {
return `${await getStatusHeading(status, ccInfos)}${ccInfos
.map((ccInfo) => {
return stringifyBasicCommitmentInfo(ccInfo);
})
.join("\n\n")} `;
}),
)
).join("\n\n");
Expand Down
12 changes: 7 additions & 5 deletions cli/src/lib/chain/depositCollateral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@ import { fltFormatWithSymbol } from "./currencies.js";
export async function depositCollateral(flags: CCFlags) {
const { CommitmentStatus } = await import("@fluencelabs/deal-ts-clients");

const [commitmentsWithInvalidStatus, commitments] = splitErrorsAndResults(
await getCommitmentsInfo(flags),
(c) => {
const [commitmentsWithInvalidStatus, commitmentsWithWaitDelegation] =
splitErrorsAndResults(await getCommitmentsInfo(flags), (c) => {
if (c.status === CommitmentStatus.WaitDelegation) {
return { result: c };
}

return { error: c };
},
);
});

if (commitmentsWithInvalidStatus.length > 0) {
commandObj.warn(
Expand All @@ -51,6 +49,10 @@ export async function depositCollateral(flags: CCFlags) {
);
}

const commitments = commitmentsWithWaitDelegation.flatMap(({ ccInfos }) => {
return ccInfos;
});

if (commitments.length === 0) {
return commandObj.error(
"No capacity commitments in the 'WaitDelegation' status found",
Expand Down
Loading

0 comments on commit e90c087

Please sign in to comment.