Skip to content

Commit

Permalink
refactor(market): remove old Demand class and leave the new implement…
Browse files Browse the repository at this point in the history
…ation

* refactor(market): remove old Demand class and leave the new implementation
* refactor: applied self review remarks
* chore: naming fixes and separation of DemandRequestBody and DemandBodyPrototype

BREAKING CHANGE: This refactor removes several deprecated types and
modules from the market module in an effort to enhance code readability
and maintainability. This includes the deletion of classes such as
Package, DemandConfig, and various DemandDirectorConfig classes.
  • Loading branch information
grisha87 authored May 9, 2024
1 parent fcbf93f commit e276ea2
Show file tree
Hide file tree
Showing 57 changed files with 1,014 additions and 1,400 deletions.
5 changes: 1 addition & 4 deletions examples/basic/hello-world-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ import { pinoPrettyLogger } from "@golem-sdk/pino-logger";

const lease = await glm.oneOf({
demand: {
imageTag: "golem/alpine:latest",
minCpuCores: 4,
minMemGib: 8,
minStorageGib: 16,
activity: { imageTag: "golem/alpine:latest" },
},
market: {
rentHours: 12,
Expand Down
9 changes: 3 additions & 6 deletions examples/basic/hello-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ import { pinoPrettyLogger } from "@golem-sdk/pino-logger";

const demand = {
demand: {
imageTag: "golem/alpine:latest",
resources: {
minCpu: 4,
minMemGib: 8,
minStorageGib: 16,
activity: {
imageTag: "golem/alpine:latest",
},
},
market: {
Expand All @@ -38,7 +35,7 @@ import { pinoPrettyLogger } from "@golem-sdk/pino-logger";
});

const payerDetails = await glm.payment.getPayerDetails();
const demandSpecification = await glm.market.buildDemand(demand.demand, payerDetails);
const demandSpecification = await glm.market.buildDemandDetails(demand.demand, payerDetails);
const proposal$ = glm.market.startCollectingProposals({
demandSpecification,
bufferSize: 15,
Expand Down
20 changes: 9 additions & 11 deletions examples/deployment/new-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@ async function main() {
})
.createActivityPool("app", {
demand: {
imageTag: "golem/node:latest",
// image: "golem/node:20",
// image: "http://golem.io/node:20",
// imageHash: "0x30984084039480493840",
minCpuCores: 1,
minMemGib: 2,
minStorageGib: 10,
activity: {
imageTag: "golem/node:latest",
},
},
market: {
rentHours: 12,
Expand All @@ -48,10 +44,12 @@ async function main() {
})
.createActivityPool("db", {
demand: {
imageTag: "golem/alpine:latest",
minCpuCores: 1,
minMemGib: 2,
minStorageGib: 4,
activity: {
imageTag: "golem/alpine:latest",
minCpuCores: 1,
minMemGib: 2,
minStorageGib: 4,
},
},
market: {
rentHours: 12 /* REQUIRED */,
Expand Down
4 changes: 3 additions & 1 deletion examples/experimental/express/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ app.post("/tts", async (req, res) => {
}
const job = golemClient.createJob({
demand: {
imageTag: "severyn/espeak:latest",
activity: {
imageTag: "severyn/espeak:latest",
},
},
market: {}, // TODO: This should be optional
payment: {
Expand Down
2 changes: 1 addition & 1 deletion examples/experimental/job/cancel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async function main() {

const job = golem.createJob<string>({
demand: {
imageTag: "severyn/espeak:latest",
activity: { imageTag: "severyn/espeak:latest" },
},
market: {}, // TODO: This should be optional
payment: {
Expand Down
2 changes: 1 addition & 1 deletion examples/experimental/job/getJobById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const golem = new JobManager({
function startJob() {
const job = golem.createJob<string>({
demand: {
imageTag: "severyn/espeak:latest",
activity: { imageTag: "severyn/espeak:latest" },
},
market: {}, // TODO: This should be optional
payment: {
Expand Down
4 changes: 3 additions & 1 deletion examples/experimental/job/waitForResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ async function main() {

const job = golem.createJob<string>({
demand: {
imageTag: "severyn/espeak:latest",
activity: {
imageTag: "severyn/espeak:latest",
},
},
market: {}, // TODO: This should be optional
payment: {
Expand Down
13 changes: 5 additions & 8 deletions examples/local-image/serveLocalGvmi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ const getImagePath = (path: string) => fileURLToPath(new URL(path, import.meta.u

const demand = {
demand: {
// Here you supply the path to the GVMI file that you want to deploy and use
// using the file:// protocol will make the SDK switch to "GVMI" serving mode
imageUrl: `file://${getImagePath("./alpine.gvmi")}`,
resources: {
minCpu: 4,
minMemGib: 8,
minStorageGib: 16,
activity: {
// Here you supply the path to the GVMI file that you want to deploy and use
// using the file:// protocol will make the SDK switch to "GVMI" serving mode
imageUrl: `file://${getImagePath("./alpine.gvmi")}`,
},
},
market: {
Expand All @@ -44,7 +41,7 @@ const getImagePath = (path: string) => fileURLToPath(new URL(path, import.meta.u
});

const payerDetails = await glm.payment.getPayerDetails();
const demandSpecification = await glm.market.buildDemand(demand.demand, payerDetails);
const demandSpecification = await glm.market.buildDemandDetails(demand.demand, payerDetails);
const proposal$ = glm.market.startCollectingProposals({
demandSpecification,
bufferSize: 15,
Expand Down
4 changes: 2 additions & 2 deletions examples/market/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import { GolemNetwork, ProposalNew } from "@golem-sdk/golem-js";
await glm.connect();

const payerDetails = await glm.payment.getPayerDetails();
const demandSpecification = await glm.market.buildDemand(
const demandSpecification = await glm.market.buildDemandDetails(
{
imageTag: "golem/alpine:latest",
activity: { imageTag: "golem/alpine:latest" },
},
payerDetails,
);
Expand Down
12 changes: 7 additions & 5 deletions examples/pool/hello-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import { pinoPrettyLogger } from "@golem-sdk/pino-logger";

const demandOptions = {
demand: {
imageTag: "golem/alpine:latest",
minCpuCores: 1,
minMemGib: 1,
minStorageGib: 2,
activity: {
imageTag: "golem/alpine:latest",
minCpuCores: 1,
minMemGib: 1,
minStorageGib: 2,
},
},
market: {
rentHours: 12,
Expand All @@ -42,7 +44,7 @@ import { pinoPrettyLogger } from "@golem-sdk/pino-logger";

const proposalPool = new DraftOfferProposalPool({ minCount: 1 });
const payerDetails = await glm.payment.getPayerDetails();
const demandSpecification = await glm.market.buildDemand(demandOptions.demand, payerDetails);
const demandSpecification = await glm.market.buildDemandDetails(demandOptions.demand, payerDetails);

const proposals$ = glm.market.startCollectingProposals({
demandSpecification,
Expand Down
1 change: 1 addition & 0 deletions src/activity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { Result } from "./results";
export { ExecutionConfig } from "./config";
export { ActivityPool, ActivityPoolOptions, ActivityPoolEvents } from "./activity-pool";
export * from "./activity.module";
export * from "./work";
6 changes: 3 additions & 3 deletions src/experimental/job/job.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Agreement, AgreementPoolService, IActivityApi } from "../../agreement";
import { WorkContext } from "../../activity/work";
import { NetworkNode, NetworkService } from "../../network";
import { Activity } from "../../activity";
import { Package } from "../../market/package";
import { anything, imock, instance, mock, verify, when } from "@johanblumenberg/ts-mockito";
import { Logger } from "../../shared/utils";
import { GolemNetwork } from "../../golem-network";
Expand All @@ -24,7 +23,6 @@ describe.skip("Job", () => {
it("stops the activity and releases the agreement when canceled", async () => {
jest.spyOn(AgreementPoolService.prototype, "run").mockResolvedValue();
jest.spyOn(NetworkService.prototype, "run").mockResolvedValue();
jest.spyOn(Package, "create").mockReturnValue({} as unknown as Package);
jest.spyOn(WorkContext.prototype, "before").mockResolvedValue();
jest.spyOn(AgreementPoolService.prototype, "releaseAgreement").mockResolvedValue();
jest.spyOn(NetworkService.prototype, "addNode").mockResolvedValue({} as unknown as NetworkNode);
Expand All @@ -46,7 +44,9 @@ describe.skip("Job", () => {
instance(mock(GolemNetwork)),
{
demand: {
imageTag: "test_image",
activity: {
imageTag: "test_image",
},
},
market: {},
payment: {
Expand Down
6 changes: 3 additions & 3 deletions src/experimental/job/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { LegacyAgreementServiceOptions } from "../../agreement";
import { DemandSpec } from "../../market";
import { NetworkOptions } from "../../network";
import { PaymentModuleOptions } from "../../payment";
import { PackageOptions } from "../../market/package";
import { EventEmitter } from "eventemitter3";
import { GolemAbortError, GolemUserError } from "../../shared/error/golem-error";
import { GolemNetwork } from "../../golem-network";
import { Logger } from "../../shared/utils";
import { ActivityDemandDirectorConfigOptions } from "../../market/demand/options";

export enum JobState {
New = "new",
Expand All @@ -22,7 +22,7 @@ export type RunJobOptions = {
payment?: PaymentModuleOptions;
agreement?: LegacyAgreementServiceOptions;
network?: NetworkOptions;
package?: PackageOptions;
activity?: ActivityDemandDirectorConfigOptions;
work?: WorkOptions;
};

Expand Down Expand Up @@ -72,6 +72,7 @@ export class Job<Output = unknown> {
* @param id
* @param glm
* @param demandSpec
* @param logger
*/
constructor(
public readonly id: string,
Expand All @@ -94,7 +95,6 @@ export class Job<Output = unknown> {
* If you want to run multiple jobs in parallel, you can use {@link GolemNetwork.createJob} to create multiple jobs and run them in parallel.
*
* @param workOnGolem - Your worker function that will be run on the Golem Network.
* @param demandSpec - Specify the image and resource for the demand that will be used to find resources on Golem Network.
*/
startWork(workOnGolem: Worker<Output>) {
this.logger.debug("Staring work in a Job");
Expand Down
25 changes: 13 additions & 12 deletions src/experimental/new-api/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@ describe("Deployment builder", () => {
builder
.createActivityPool("my-pool", {
demand: {
imageTag: "image",
minCpuCores: 1,
minMemGib: 1,
minStorageGib: 1,
activity: {
imageTag: "image",
minCpuCores: 1,
minMemGib: 1,
minStorageGib: 1,
},
},
market: {},
})
.createActivityPool("my-pool", {
demand: {
imageTag: "image",
minCpuCores: 1,
minMemGib: 1,
minStorageGib: 1,
activity: {
imageTag: "image",
minCpuCores: 1,
minMemGib: 1,
minStorageGib: 1,
},
},
market: {},
});
Expand All @@ -51,10 +55,7 @@ describe("Deployment builder", () => {
})
.createActivityPool("my-pool", {
demand: {
imageTag: "image",
minCpuCores: 1,
minMemGib: 1,
minStorageGib: 1,
activity: { imageTag: "image", minCpuCores: 1, minMemGib: 1, minStorageGib: 1 },
},
market: {},
deployment: {
Expand Down
4 changes: 2 additions & 2 deletions src/experimental/new-api/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { GolemNetwork } from "../../golem-network";
import { validateDeployment } from "./validate-deployment";
import { MarketOptions } from "../../market";
import { PaymentModuleOptions } from "../../payment";
import { DemandOptionsNew } from "../../market/demand";
import { BuildDemandOptions } from "../../market/demand";

interface DeploymentOptions {
replicas?: number | { min: number; max: number };
network?: string;
}

export interface CreateActivityPoolOptions {
demand: DemandOptionsNew;
demand: BuildDemandOptions;
market: MarketOptions;
deployment?: DeploymentOptions;
payment?: PaymentModuleOptions;
Expand Down
2 changes: 1 addition & 1 deletion src/experimental/new-api/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export class Deployment {
for (const pool of this.components.activityPools) {
const { demandBuildOptions, agreementPoolOptions, activityPoolOptions } = this.prepareParams(pool.options);

const demandSpecification = await this.modules.market.buildDemand(demandBuildOptions.demand, payerDetails);
const demandSpecification = await this.modules.market.buildDemandDetails(demandBuildOptions.demand, payerDetails);
const proposalPool = new DraftOfferProposalPool();

const proposalSubscription = this.modules.market
Expand Down
6 changes: 3 additions & 3 deletions src/golem-network.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DataTransferProtocol, DeploymentOptions, GolemDeploymentBuilder, MarketOptions } from "./experimental";
import { defaultLogger, Logger, YagnaApi } from "./shared/utils";
import {
DemandNew,
Demand,
DemandSpec,
DraftOfferProposalPool,
MarketApi,
Expand Down Expand Up @@ -125,7 +125,7 @@ export class GolemNetwork {

this.storageProvider = this.createStorageProvider();

const demandCache = new CacheService<DemandNew>();
const demandCache = new CacheService<Demand>();
const proposalCache = new CacheService<ProposalNew>();

const demandRepository = new DemandRepository(this.yagna.market, demandCache);
Expand Down Expand Up @@ -237,7 +237,7 @@ export class GolemNetwork {
logger: this.logger,
});
const payerDetails = await this.payment.getPayerDetails();
const demandSpecification = await this.market.buildDemand(demand.demand, payerDetails);
const demandSpecification = await this.market.buildDemandDetails(demand.demand, payerDetails);

const proposalSubscription = this.market
.startCollectingProposals({
Expand Down
21 changes: 13 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
export * from "./shared/storage";
export * from "./activity";
export * from "./agreement";
// High-level entry points
export * from "./golem-network";

// Low level entry points for advanced users
export * from "./market";
export * from "./market/package";
export * from "./payment";
export * from "./network";
export * from "./shared/utils";
export * from "./shared/yagna";
export * from "./activity/work";
export * from "./activity";
export * from "./agreement";

// Necessary domain entities for users to consume
export * from "./shared/error/golem-error";
export * from "./network/tcpProxy";
export * from "./golem-network";

// Internals
export * from "./shared/utils";
export * from "./shared/yagna";
export * from "./shared/storage";
8 changes: 4 additions & 4 deletions src/market/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Observable } from "rxjs";
import { DemandNew, DemandSpecification } from "./demand";
import { Demand, DemandSpecification } from "./demand";
import YaTsClient from "ya-ts-client";
import { ProposalNew } from "./proposal";

Expand All @@ -15,17 +15,17 @@ export interface MarketApi {
* refreshed periodically (see `refreshDemand` method).
* Use `unpublishDemand` to remove the demand from the market.
*/
publishDemandSpecification(specification: DemandSpecification): Promise<DemandNew>;
publishDemandSpecification(specification: DemandSpecification): Promise<Demand>;

/**
* Remove the given demand from the market.
*/
unpublishDemand(demand: DemandNew): Promise<void>;
unpublishDemand(demand: Demand): Promise<void>;

/**
* Creates a new observable that emits proposal events related to the given demand.
*/
observeProposalEvents(demand: DemandNew): Observable<ProposalEvent>;
observeProposalEvents(demand: Demand): Observable<ProposalEvent>;

/**
* Sends a counter-proposal to the given proposal. Returns the newly created counter-proposal.
Expand Down
Loading

0 comments on commit e276ea2

Please sign in to comment.