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(market): added config options for setting the ProposalFilter #946

Merged
merged 3 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions examples/advanced/proposal-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { MarketOrderSpec, GolemNetwork, ProposalFilter } from "@golem-sdk/golem-js";
import { pinoPrettyLogger } from "@golem-sdk/pino-logger";

/**
* Example demonstrating how to write a custom proposal filter.
* In this case the proposal must include VPN access and must not be from "bad-provider"
*/
const myFilter: ProposalFilter = (proposal) => {
return (
proposal.provider.name !== "bad-provider" && proposal.properties["golem.runtime.capabilities"]?.includes("vpn")
);
};

const order: MarketOrderSpec = {
demand: {
workload: { imageTag: "golem/alpine:latest" },
},
market: {
maxAgreements: 1,
rentHours: 0.5,
pricing: {
model: "linear",
maxStartPrice: 0.5,
maxCpuPerHourPrice: 1.0,
maxEnvPerHourPrice: 0.5,
},
proposalFilter: myFilter,
},
};

(async () => {
const glm = new GolemNetwork({
logger: pinoPrettyLogger({
level: "info",
}),
});

try {
await glm.connect();
const lease = await glm.oneOf(order);
await lease
.getExeUnit()
.then((exe) => exe.run(`echo [provider:${exe.provider.name}] Hello, Golem! 👋`))
.then((res) => console.log(res.stdout));
await lease.finalize();
} catch (err) {
console.error("Failed to run the example", err);
} finally {
await glm.disconnect();
}
})().catch(console.error);
48 changes: 48 additions & 0 deletions examples/advanced/proposal-predefined-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { MarketOrderSpec, GolemNetwork, ProposalFilterFactory } from "@golem-sdk/golem-js";
import { pinoPrettyLogger } from "@golem-sdk/pino-logger";

/**
* Example showing how to use a proposal filter using the predefined filter `disallowProvidersByName`,
* which blocks any proposal from a provider whose name is in the array of
*/

const blackListProvidersNames = ["provider-1", "bad-provider", "slow-provider"];

const order: MarketOrderSpec = {
demand: {
workload: { imageTag: "golem/alpine:latest" },
},
market: {
maxAgreements: 1,
rentHours: 0.5,
pricing: {
model: "linear",
maxStartPrice: 0.5,
maxCpuPerHourPrice: 1.0,
maxEnvPerHourPrice: 0.5,
},
proposalFilter: ProposalFilterFactory.disallowProvidersByName(blackListProvidersNames),
},
};

(async () => {
const glm = new GolemNetwork({
logger: pinoPrettyLogger({
level: "info",
}),
});

try {
await glm.connect();
const lease = await glm.oneOf(order);
await lease
.getExeUnit()
.then((exe) => exe.run(`echo [provider:${exe.provider.name}] Hello, Golem! 👋`))
.then((res) => console.log(res.stdout));
await lease.finalize();
} catch (err) {
console.error("Failed to run the example", err);
} finally {
await glm.disconnect();
}
})().catch(console.error);
2 changes: 2 additions & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"advanced-hello-world": "tsx advanced/hello-world.ts",
"advanced-manual-pools": "tsx advanced/manual-pools.ts",
"advanced-payment-filters": "tsx advanced/payment-filters.ts",
"advanced-proposal-filters": "tsx advanced/proposal-filter.ts",
"advanced-proposal-predefined-filter": "tsx advanced/proposal-predefined-filter.ts",
"local-image": "tsx advanced/local-image/serveLocalGvmi.ts",
"deployment": "tsx experimental/deployment/new-api.ts",
"market-scan": "tsx market/scan.ts",
Expand Down
1 change: 1 addition & 0 deletions src/experimental/deployment/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export class Deployment {
const proposalSubscription = this.modules.market
.startCollectingProposals({
demandSpecification,
filter: pool.options.market.proposalFilter,
bufferSize: 10,
})
.subscribe({
Expand Down
4 changes: 2 additions & 2 deletions src/experimental/reputation/system.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProposalFilterNew, OfferProposal } from "../../market";
import { ProposalFilter, OfferProposal } from "../../market";
import { AgreementCandidate, AgreementSelector } from "../../market/agreement";
import { GolemReputationError } from "./error";
import {
Expand Down Expand Up @@ -332,7 +332,7 @@ export class ReputationSystem {
* Returns a proposal filter that can be used to filter out providers with low reputation scores.
* @param opts
*/
proposalFilter(opts?: ProposalFilterOptions): ProposalFilterNew {
proposalFilter(opts?: ProposalFilterOptions): ProposalFilter {
return (proposal: OfferProposal) => {
// Filter out rejected operators.
const operatorEntry = this.rejectedOperatorsMap.get(proposal.provider.walletAddress);
Expand Down
2 changes: 2 additions & 0 deletions src/golem-network/golem-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ export class GolemNetwork {

const proposal$ = this.market.startCollectingProposals({
demandSpecification,
filter: order.market.proposalFilter,
});
const proposalSubscription = proposalPool.readFrom(proposal$);

Expand Down Expand Up @@ -389,6 +390,7 @@ export class GolemNetwork {

const proposal$ = this.market.startCollectingProposals({
demandSpecification,
filter: order.market.proposalFilter,
});
const subscription = proposalPool.readFrom(proposal$);

Expand Down
2 changes: 1 addition & 1 deletion src/market/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { ProposalFilterNew } from "./offer-proposal";
export { ProposalFilter } from "./offer-proposal";
export { Demand, BasicDemandPropertyConfig, DemandSpecification } from "./demand";
export { OfferProposal, ProposalDTO } from "./offer-proposal";
export * as ProposalFilterFactory from "./strategy";
Expand Down
17 changes: 5 additions & 12 deletions src/market/market.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { defaultLogger, Logger, runOnNextEventLoopIteration, YagnaApi } from "../shared/utils";
import { Allocation, IPaymentApi } from "../payment";
import { bufferTime, catchError, filter, map, mergeMap, Observable, of, OperatorFunction, switchMap, tap } from "rxjs";
import { IProposalRepository, OfferProposal, ProposalFilterNew } from "./offer-proposal";
import { IProposalRepository, OfferProposal, ProposalFilter } from "./offer-proposal";
import { DemandBodyBuilder } from "./demand/demand-body-builder";
import { IAgreementApi } from "./agreement/agreement";
import { BuildDemandOptions, DemandSpecification, IDemandRepository } from "./demand";
Expand Down Expand Up @@ -54,15 +54,8 @@ export interface MarketOptions {
avgGlmPerHour: number;
};

/**
* List of provider Golem Node IDs that should be considered
*
* If not provided, the list will be pulled from: https://provider-health.golem.network/v1/provider-whitelist
*/
withProviders?: string[];
withoutProviders?: string[];
withOperators?: string[];
withoutOperators?: string[];
/** An optional filter that can filter proposals from the market before signing an agreement */
proposalFilter?: ProposalFilter;
}

export interface MarketModule {
Expand Down Expand Up @@ -149,7 +142,7 @@ export interface MarketModule {
*/
startCollectingProposals(options: {
demandSpecification: DemandSpecification;
filter?: ProposalFilterNew;
filter?: ProposalFilter;
bufferSize?: number;
}): Observable<OfferProposal[]>;

Expand Down Expand Up @@ -354,7 +347,7 @@ export class MarketModuleImpl implements MarketModule {

startCollectingProposals(options: {
demandSpecification: DemandSpecification;
filter?: ProposalFilterNew;
filter?: ProposalFilter;
bufferSize?: number;
bufferTimeout?: number;
minProposalsBatchSize?: number;
Expand Down
2 changes: 1 addition & 1 deletion src/market/offer-proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GolemMarketError, MarketErrorCode } from "./error";
import { ProviderInfo } from "./agreement";
import { Demand } from "./demand";

export type ProposalFilterNew = (proposal: OfferProposal) => boolean;
export type ProposalFilter = (proposal: OfferProposal) => boolean;

export type PricingInfo = {
cpuSec: number;
Expand Down
2 changes: 2 additions & 0 deletions tests/examples/examples.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
{ "cmd": "tsx", "path": "examples/advanced/hello-world.ts" },
{ "cmd": "tsx", "path": "examples/advanced/manual-pools.ts" },
{ "cmd": "tsx", "path": "examples/advanced/payment-filters.ts" },
{ "cmd": "tsx", "path": "examples/advanced/proposal-filter.ts" },
{ "cmd": "tsx", "path": "examples/advanced/proposal-predefined-filter.ts" },
{ "cmd": "tsx", "path": "examples/experimental/deployment/new-api.ts" },
{ "cmd": "tsx", "path": "examples/experimental/job/getJobById.ts" },
{ "cmd": "tsx", "path": "examples/experimental/job/waitForResults.ts" },
Expand Down
Loading