Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add datacenter and resource tests, print offer-info to stdout #1096

Merged
merged 13 commits into from
Jan 21, 2025
2 changes: 1 addition & 1 deletion packages/cli/package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"whatwg-url": "^14.0.0"
},
"dependencies": {
"@fluencelabs/deal-ts-clients": "0.23.2-feat-marketplace-v2-resources-eb89b78-7511-1.0",
"@fluencelabs/deal-ts-clients": "0.23.2-feat-marketplace-v2-resources-a3ae33d-7546-1.0",
"@kubernetes/client-node": "github:fluencelabs/kubernetes-client-javascript#e72ee00a52fec4eb4a8327632895d888ee504f4d",
"@libp2p/crypto": "4.0.1",
"@libp2p/peer-id-factory": "4.0.5",
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/package/src/commands/provider/deal-exit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ export default class DealExit extends BaseCommand<typeof DealExit> {
continue;
}

await signBatch(
`Remove the following workers from deal ${dealId}:\n\n${workers
await signBatch({
title: `Remove the following workers from deal ${dealId}:\n\n${workers
.map(({ worker: { onchainId } }) => {
return onchainId;
})
.join("\n")}`,
[
populatedTxs: [
populateTx(
firstWorker.deal.removeWorker,
firstWorker.worker.onchainId,
Expand All @@ -136,7 +136,7 @@ export default class DealExit extends BaseCommand<typeof DealExit> {
return populateTx(deal.removeWorker, onchainId);
}),
],
);
});
}
}
}
2 changes: 1 addition & 1 deletion packages/cli/package/src/commands/provider/offer-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ export default class OfferInfo extends BaseCommand<typeof OfferInfo> {
const { flags } = await initCli(this, await this.parse(OfferInfo));
const offers = await resolveCreatedOffers(flags);
const offerInfoResult = await getOffersInfo(offers);
commandObj.logToStderr(await offersInfoToString(offerInfoResult));
commandObj.log(await offersInfoToString(offerInfoResult));
}
}
68 changes: 36 additions & 32 deletions packages/cli/package/src/lib/chain/commitment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,14 @@ export async function createCommitments(flags: PeerAndOfferNameFlags) {
let createCommitmentsTxReceipts;

try {
createCommitmentsTxReceipts = await signBatch(
`Create commitments for the following peers:\n\n${computePeers
createCommitmentsTxReceipts = await signBatch({
title: `Create commitments for the following peers:\n\n${computePeers
.map(({ name, peerId }) => {
return `Peer: ${name}\nPeerId: ${peerId}`;
})
.join("\n\n")}`,
[firstCommitmentTx, ...restCommitmentTxs],
);
populatedTxs: [firstCommitmentTx, ...restCommitmentTxs],
});
} catch (e) {
const errorString = stringifyUnknown(e);

Expand Down Expand Up @@ -440,9 +440,9 @@ export async function removeCommitments(flags: CCFlags) {
},
).join("\n\n");

await signBatch(
`Remove the following commitments:\n\n${CCsWithWaitDelegationStatusString}`,
[
await signBatch({
title: `Remove the following commitments:\n\n${CCsWithWaitDelegationStatusString}`,
populatedTxs: [
populateTx(
contracts.diamond.removeCommitment,
firstCCWithWaitDelegationStatus.ccId,
Expand All @@ -451,7 +451,7 @@ export async function removeCommitments(flags: CCFlags) {
return populateTx(contracts.diamond.removeCommitment, ccId);
}),
],
);
});

commandObj.logToStderr(
`Removed commitments:\n\n${CCsWithWaitDelegationStatusString}`,
Expand Down Expand Up @@ -573,10 +573,13 @@ export async function collateralWithdraw(

if (firstMoveResourcesFromDealTx !== undefined) {
try {
await signBatch(
`Moving resources from the following deals:\n${dealsString}`,
[firstMoveResourcesFromDealTx, ...restMoveResourcesFromDealTxs],
);
await signBatch({
title: `Moving resources from the following deals:\n${dealsString}`,
populatedTxs: [
firstMoveResourcesFromDealTx,
...restMoveResourcesFromDealTxs,
],
});
} catch (e) {
commandObj.warn(
`Wasn't able to move resources from deals for ${stringifyBasicCommitmentInfo(commitment)}. Most likely the reason is you must wait until the provider exits from all the following deals:\n${dealsString}`,
Expand Down Expand Up @@ -609,22 +612,23 @@ export async function collateralWithdraw(
},
);

await signBatch(
`${firstNotExitedUnit === undefined ? "Finish" : "Remove compute units from capacity commitments and finish"} commitment ${peerName === undefined ? ccId : `for ${peerName} (${ccId})`} ${ccId}`,
firstNotExitedUnit === undefined
? [populateTx(contracts.diamond.finishCommitment, ccId)]
: [
populateTx(contracts.diamond.removeCUFromCC, ccId, [
firstNotExitedUnit.unitId,
]),
...restNotExitedUnits.map(({ unitId }) => {
return populateTx(contracts.diamond.removeCUFromCC, ccId, [
unitId,
]);
}),
populateTx(contracts.diamond.finishCommitment, ccId),
],
);
await signBatch({
title: `${firstNotExitedUnit === undefined ? "Finish" : "Remove compute units from capacity commitments and finish"} commitment ${peerName === undefined ? ccId : `for ${peerName} (${ccId})`} ${ccId}`,
populatedTxs:
firstNotExitedUnit === undefined
? [populateTx(contracts.diamond.finishCommitment, ccId)]
: [
populateTx(contracts.diamond.removeCUFromCC, ccId, [
firstNotExitedUnit.unitId,
]),
...restNotExitedUnits.map(({ unitId }) => {
return populateTx(contracts.diamond.removeCUFromCC, ccId, [
unitId,
]);
}),
populateTx(contracts.diamond.finishCommitment, ccId),
],
});
}
}

Expand All @@ -633,19 +637,19 @@ export async function collateralRewardWithdraw(flags: CCFlags) {
const [firstCommitment, ...restCommitments] = commitments;
const { contracts } = await getContracts();

await signBatch(
`Withdraw rewards for commitments:\n\n${commitments
await signBatch({
title: `Withdraw rewards for commitments:\n\n${commitments
.map(({ ccId }) => {
return ccId;
})
.join("\n")}`,
[
populatedTxs: [
populateTx(contracts.diamond.withdrawReward, firstCommitment.ccId),
...restCommitments.map(({ ccId }) => {
return populateTx(contracts.diamond.withdrawReward, ccId);
}),
],
);
});
}

export function stringifyBasicCommitmentInfo({
Expand Down
62 changes: 31 additions & 31 deletions packages/cli/package/src/lib/chain/offer/offer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,16 +637,15 @@ async function formatOfferInfo(
"Created At": offerIndexerInfo.createdAt,
"Last Updated At": offerIndexerInfo.updatedAt,
"Resource Prices": await Promise.all(
(offerIndexerInfo.resources ?? []).map(
(offerIndexerInfo.offerResources ?? []).map(
async ({
resourceId,
resourcePrice,
resource: { type, metadata },
resourceDescription: { type, metadata, id },
}) => {
const resourceType = onChainResourceTypeToResourceType(type);
return {
"Resource Type": resourceType,
"Resource ID": resourceId,
"Resource ID": id,
Metadata: metadata,
Price: await ptFormatWithSymbol(
resourceType === "cpu"
Expand Down Expand Up @@ -789,16 +788,16 @@ export async function resolveOffersFromProviderConfig(
const offersNotDefinedLocally = await Promise.all(
offerInfos.map(async ({ offerId, offerIndexerInfo }) => {
const resourcePricesWithIds = (
offerIndexerInfo.resources ?? []
offerIndexerInfo.offerResources ?? []
).reduce<ResourcePricesWithIds>(
(acc, { resourceId, resourcePrice, resource: { type } }) => {
(acc, { resourcePrice, resourceDescription: { type, id } }) => {
const resourceType = onChainResourceTypeToResourceType(type);

acc[resourceType].push({
ty: type,
resourceType,
resourceId,
resourceName: resourceId,
resourceId: id,
resourceName: id,
price: resourcePrice,
});

Expand Down Expand Up @@ -1325,18 +1324,24 @@ type OfferIndexerInfo = Awaited<ReturnType<typeof getOffers>>["offers"][number];
function indexerResourcesToOnchainResources(
resourceType: ResourceType,
resources: NonNullable<
NonNullable<OfferIndexerInfo["peers"]>[number]["resources"]
NonNullable<OfferIndexerInfo["peers"]>[number]["peerResources"]
>,
offerId: string,
): [OnChainResource, ...OnChainResource[]] {
const [firstResource, ...restResources] = resources
.filter(({ resource: { type } }) => {
.filter(({ resourceDescription: { type } }) => {
return onChainResourceTypeToResourceType(type) === resourceType;
})
.map(({ details, resource: { id: resourceId, metadata }, maxSupply }) => {
assertIsHex(resourceId, `Invalid Resource ID for offer ${offerId}`);
return { resourceId, supply: Number(maxSupply), details, metadata };
});
.map(
({
details,
resourceDescription: { id: resourceId, metadata },
maxSupply,
}) => {
assertIsHex(resourceId, `Invalid Resource ID for offer ${offerId}`);
return { resourceId, supply: Number(maxSupply), details, metadata };
},
);

if (firstResource === undefined) {
throw new Error(
Expand All @@ -1349,8 +1354,8 @@ function indexerResourcesToOnchainResources(

function serializeOfferInfo(offerIndexerInfo: OfferIndexerInfo) {
const serializedPeers =
offerIndexerInfo.peers?.map(({ id, computeUnits, resources }) => {
if (resources === null || resources === undefined) {
offerIndexerInfo.peers?.map(({ id, computeUnits, peerResources }) => {
if (peerResources === null || peerResources === undefined) {
throw new Error(
`Resources for peer ${id} are not found in indexer for offer ${offerIndexerInfo.id}`,
);
Expand All @@ -1359,27 +1364,27 @@ function serializeOfferInfo(offerIndexerInfo: OfferIndexerInfo) {
const resourcesByType = {
cpu: indexerResourcesToOnchainResources(
"cpu",
resources,
peerResources,
offerIndexerInfo.id,
)[0],
ram: indexerResourcesToOnchainResources(
"ram",
resources,
peerResources,
offerIndexerInfo.id,
)[0],
storage: indexerResourcesToOnchainResources(
"storage",
resources,
peerResources,
offerIndexerInfo.id,
),
ip: indexerResourcesToOnchainResources(
"ip",
resources,
peerResources,
offerIndexerInfo.id,
)[0],
bandwidth: indexerResourcesToOnchainResources(
"bandwidth",
resources,
peerResources,
offerIndexerInfo.id,
)[0],
} as const satisfies Record<
Expand Down Expand Up @@ -1416,15 +1421,10 @@ function serializeOfferInfo(offerIndexerInfo: OfferIndexerInfo) {
freeComputeUnits: offerIndexerInfo.computeUnitsAvailable,
providerId: offerIndexerInfo.provider.id,
peers: serializedPeers,
resources: offerIndexerInfo.resources?.map(({ price, resource }) => {
const resourceId = resource.id;

assertIsHex(
resourceId,
`Invalid Resource ID for offer ${offerIndexerInfo.id}`,
);

return { resourceId, resourcePrice: BigInt(price), resource };
}),
offerResources: offerIndexerInfo.offerResources?.map(
({ price, resourceDescription }) => {
return { resourcePrice: BigInt(price), resourceDescription };
},
),
};
}
32 changes: 16 additions & 16 deletions packages/cli/package/src/lib/chain/offer/updateOffers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ export async function updateOffers(flags: OffersArgs) {
return;
}

await signBatch(
`Updating offers:\n\n${populatedTxs
await signBatch({
title: `Updating offers:\n\n${populatedTxs
.map(({ offerName, offerId }) => {
return `${offerName} (${offerId})`;
})
.join("\n")}`,
[firstUpdateOffersTx, ...restUpdateOffersTxs],
assertProviderIsRegistered,
);
populatedTxs: [firstUpdateOffersTx, ...restUpdateOffersTxs],
validateAddress: assertProviderIsRegistered,
});

const peersToDeploy = populatedTxs.flatMap(({ txs }) => {
return txs.flatMap(({ peersToDeploy }) => {
Expand Down Expand Up @@ -190,15 +190,15 @@ export async function removeOffers(flags: OffersArgs) {
return;
}

await signBatch(
`Removing offers:\n\n${populatedTxs
await signBatch({
title: `Removing offers:\n\n${populatedTxs
.map(({ offerName, offerId }) => {
return `${offerName} (${offerId})`;
})
.join("\n")}`,
[firstRemoveOffersTx, ...restRemoveOffersTxs],
assertProviderIsRegistered,
);
populatedTxs: [firstRemoveOffersTx, ...restRemoveOffersTxs],
validateAddress: assertProviderIsRegistered,
});

const providerArtifactsConfig = await initNewProviderArtifactsConfig();

Expand Down Expand Up @@ -494,9 +494,9 @@ async function populateChangeResourcePriceTx({
);

const prevResourcePriceChanges = await Promise.all(
(offerIndexerInfo.resources ?? []).map(
async ({ resourceId, resourcePrice }) => {
const newResource = allResourcePrices[resourceId];
(offerIndexerInfo.offerResources ?? []).map(
async ({ resourceDescription: { id }, resourcePrice }) => {
const newResource = allResourcePrices[id];

if (newResource === undefined) {
return null;
Expand All @@ -516,7 +516,7 @@ async function populateChangeResourcePriceTx({
tx: populateTx(
contracts.diamond.changeResourcePriceV2,
offerId,
resourceId,
id,
newPrice,
),
};
Expand All @@ -527,8 +527,8 @@ async function populateChangeResourcePriceTx({
const newResources = await Promise.all(
resourcePricesWithIdsArr.map(
async ({ price, resourceId, resourceName, resourceType }) => {
const onChainResource = offerIndexerInfo.resources?.find((r) => {
return r.resourceId === resourceId;
const onChainResource = offerIndexerInfo.offerResources?.find((r) => {
return r.resourceDescription.id === resourceId;
});

if (onChainResource !== undefined) {
Expand Down
Loading
Loading