Skip to content

Commit

Permalink
add 2x when creating offer, check enough ram per core
Browse files Browse the repository at this point in the history
  • Loading branch information
shamsartem committed Jan 3, 2025
1 parent 9e5327d commit 0267e74
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 107 deletions.
41 changes: 40 additions & 1 deletion packages/cli/package/src/lib/chain/conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { bufferToBase64 } from "../helpers/typesafeStringify.js";
import type { ResourceType } from "../configs/project/provider/provider4.js";
import { stringifyUnknown } from "../helpers/stringifyUnknown.js";
import { bufferToBase64, numToStr } from "../helpers/typesafeStringify.js";

const PREFIX = new Uint8Array([0, 36, 8, 1, 18, 32]);
const BASE_58_PREFIX = "z";
Expand Down Expand Up @@ -77,3 +79,40 @@ export function hexStringToUTF8ToBase64String(hexString: string): string {

return bufferToBase64(Buffer.from(cleanHexString, "utf-8"));
}

export async function resourceSupply(
resourceType: ResourceType,
supply: number,
) {
const xbytes = (await import("xbytes")).default;

switch (resourceType) {
case "cpu":
return { supply, supplyString: numToStr(supply) };
case "ram":
return {
supply: Math.round(supply / (await getRamToBytesFromChain())),
supplyString: xbytes(supply),
};
case "storage":
return {
supply: Math.round(supply / STORAGE_TO_BYTES),
supplyString: xbytes(supply),
};
case "bandwidth":
return { supply, supplyString: numToStr(supply) };
case "ip":
return { supply, supplyString: numToStr(supply) };
default:
const unknownResourceType: never = resourceType;
throw new Error(
`Unknown resource type ${stringifyUnknown(unknownResourceType)}`,
);
}
}

const STORAGE_TO_BYTES = 1_000_000_000;

async function getRamToBytesFromChain() {
return Promise.resolve(1_000_000_000);
}
83 changes: 70 additions & 13 deletions packages/cli/package/src/lib/chain/offer/offer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export async function createOffers(flags: OffersArgs) {
return [
contracts.deployment.usdc,
resourcePricesArray,
computePeersToRegister,
setCPUSupplyForCP(computePeersToRegister),
];
},
getTitle() {
Expand Down Expand Up @@ -265,7 +265,7 @@ export async function createOffers(flags: OffersArgs) {
let offersInfo: GetOffersInfoReturnType[1] = [];

const getOffersInfoRes = await setTryTimeout(
"Getting offers info from indexer",
"get offers info from indexer",
async () => {
[offerInfoErrors, offersInfo] = await getOffersInfo(createdOffers);

Expand Down Expand Up @@ -299,6 +299,53 @@ ${await offersInfoToString([offerInfoErrors, offersInfo])}
`);
}

function setCPUSupplyForCP(
computePeersToRegister: {
peerId: Uint8Array;
unitIds: Uint8Array[];
resourcesByType: {
readonly cpu: {
readonly resourceId: `0x${string}`;
readonly details: string;
readonly name: string;
readonly supply: number;
};
};
owner: string;
}[],
) {
return computePeersToRegister.map(
({
peerId,
unitIds,
resourcesByType: { cpu, ...restResources },
owner,
}) => {
return {
peerId,
unitIds,
resources: [
setCPUSupply(cpu, unitIds),
...resourcesByTypeToArr(restResources),
],
owner,
};
},
);
}

function setCPUSupply(
cpu: {
readonly resourceId: `0x${string}`;
readonly details: string;
readonly name: string;
readonly supply: number;
},
unitIds: Uint8Array[],
): OnChainResource {
return { ...cpu, supply: unitIds.length * 2 };
}

async function confirmOffer(offer: EnsureOfferConfig) {
return confirm({
message: `The following offer will be created: ${color.yellow(offer.offerName)}\n${yamlDiffPatch(
Expand Down Expand Up @@ -361,7 +408,7 @@ function formatOfferResource({
details,
}: {
resourceId: `0x${string}`;
supply: number;
supply: number | string;
details: string;
}) {
return {
Expand Down Expand Up @@ -400,7 +447,7 @@ export async function addRemainingCPs({
sliceValuesToRegister: sliceCPsByNumberOfCUs(remainingCPs),
sliceIndex: CUsToRegisterCount,
getArgs(CPsToRegister) {
return [offerId, CPsToRegister];
return [offerId, setCPUSupplyForCP(CPsToRegister)];
},
getTitle({ valuesToRegister: CPsToRegister }) {
return `Add compute peers:\n${CPsToRegister.map(
Expand Down Expand Up @@ -462,7 +509,11 @@ async function addRemainingCUs({
},
sliceIndex: CUs.length,
getArgs(CUsToRegister) {
return [peerId, CUsToRegister, resourcesByType.cpu];
return [
peerId,
CUsToRegister,
setCPUSupply(resourcesByType.cpu, CUsToRegister),
];
},
getTitle({ sliceCount: numberOfCUsForAddCU }) {
return `Add ${numToStr(numberOfCUsForAddCU)} compute units\nto compute peer ${cpName} (${peerIdBase58})\nfor offer ${offerName} (${offerId})`;
Expand Down Expand Up @@ -723,11 +774,6 @@ export async function resolveOffersFromProviderConfig(
return new Uint8Array(Buffer.from(id.slice(2), "hex"));
}),
owner: offerIndexerInfo.providerId,
resources: Object.values(resourcesByTypeWithName).flatMap(
(resource) => {
return Array.isArray(resource) ? resource : [resource];
},
),
resourcesByType: resourcesByTypeWithName,
};
},
Expand Down Expand Up @@ -882,6 +928,8 @@ async function ensureOfferConfigs() {
"Unreachable. Config is validated so it must return result",
);

const xbytes = (await import("xbytes")).default;

const resources = {
cpu: {
...cpu,
Expand All @@ -890,13 +938,15 @@ async function ensureOfferConfigs() {
},
ram: {
...ram,
supply: xbytes.parseSize(ram.supply),
resourceId: `0x${ramId}`,
details: JSON.stringify(ram.details),
},
storage: resourcesWithIds.storage.map(
({ id: storageId, ...storage }) => {
return {
...storage,
supply: xbytes.parseSize(storage.supply),
resourceId: `0x${storageId}`,
details: JSON.stringify(storage.details),
};
Expand Down Expand Up @@ -925,9 +975,6 @@ async function ensureOfferConfigs() {
unitIds: times(resourcesWithIds.cpu.supply).map(() => {
return randomBytes(32);
}),
resources: Object.values(resources).flatMap((resource) => {
return Array.isArray(resource) ? resource : [resource];
}),
resourcesByType: resources,
owner: walletAddress,
};
Expand Down Expand Up @@ -964,6 +1011,16 @@ async function ensureOfferConfigs() {
);
}

function resourcesByTypeToArr(
resourcesByType: Partial<
Record<ResourceType, OnChainResource | OnChainResource[]>
>,
) {
return Object.values(resourcesByType).flatMap((resource) => {
return Array.isArray(resource) ? resource : [resource];
});
}

type CPFromProviderConfig = Awaited<
ReturnType<typeof ensureOfferConfigs>
>[number]["computePeersFromProviderConfig"][number];
Expand Down
42 changes: 28 additions & 14 deletions packages/cli/package/src/lib/chain/offer/updateOffers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { color } from "@oclif/color";
import omit from "lodash-es/omit.js";

import { commandObj } from "../../commandObj.js";
import type { ResourceType } from "../../configs/project/provider/provider4.js";
import { initNewProviderArtifactsConfig } from "../../configs/project/providerArtifacts/providerArtifacts.js";
import {
CLI_NAME,
Expand All @@ -29,7 +30,10 @@ import { numToStr } from "../../helpers/typesafeStringify.js";
import { splitErrorsAndResults } from "../../helpers/utils.js";
import { confirm } from "../../prompt.js";
import { ensureFluenceEnv } from "../../resolveFluenceEnv.js";
import { peerIdHexStringToBase58String } from "../conversions.js";
import {
peerIdHexStringToBase58String,
resourceSupply,
} from "../conversions.js";
import { ptFormat, ptFormatWithSymbol } from "../currencies.js";
import { assertProviderIsRegistered } from "../providerInfo.js";

Expand Down Expand Up @@ -449,30 +453,40 @@ type ResourceInfo = {
};

type ResourceSupplyUpdate = {
name: string;
resourceType: ResourceType;
onChainResource: ResourceInfo;
configuredResource: ResourceInfo;
};

async function createResourceSupplyUpdateTx(
peerId: string,
{ name, onChainResource, configuredResource }: ResourceSupplyUpdate,
{ resourceType, onChainResource, configuredResource }: ResourceSupplyUpdate,
) {
if (onChainResource.supply === configuredResource.supply) {
if (onChainResource.supply === configuredResource.supply * 2) {
return null;
}

const { contracts } = await getContracts();

const { supplyString: onChainSupplyString } = await resourceSupply(
resourceType,
configuredResource.supply,
);

const { supply, supplyString } = await resourceSupply(
resourceType,
configuredResource.supply,
);

return {
description: `\nChanging ${name} supply from ${numToStr(
onChainResource.supply,
)} to ${numToStr(configuredResource.supply)}`,
description: `\nChanging ${resourceType} supply from ${
onChainSupplyString
} to ${supplyString}`,
tx: populateTx(
contracts.diamond.changeResourceMaxSupplyV2,
peerId,
onChainResource.resourceId,
configuredResource.supply,
supply * 2,
),
};
}
Expand All @@ -496,12 +510,12 @@ async function populateChangeResourceSupplyTx({

const resourcePriceUpdates: ResourceSupplyUpdate[] = [
{
name: "CPU",
resourceType: "cpu",
onChainResource: peer.resourcesByType.cpu,
configuredResource: configuredPeer.resourcesByType.cpu,
},
{
name: "RAM",
resourceType: "ram",
onChainResource: peer.resourcesByType.ram,
configuredResource: configuredPeer.resourcesByType.ram,
},
Expand All @@ -516,19 +530,19 @@ async function populateChangeResourceSupplyTx({
}

return {
name: "storage",
resourceType: "storage",
onChainResource: onChainStorage,
configuredResource: configuredStorage,
};
} as const;
})
.filter(Boolean),
{
name: "IP",
resourceType: "ip",
onChainResource: peer.resourcesByType.ip,
configuredResource: configuredPeer.resourcesByType.ip,
},
{
name: "bandwidth",
resourceType: "bandwidth",
onChainResource: peer.resourcesByType.bandwidth,
configuredResource: configuredPeer.resourcesByType.bandwidth,
},
Expand Down
Loading

0 comments on commit 0267e74

Please sign in to comment.