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

feature(cardano-services): support Blockfrost NetworkInfoProvider in PgBossWorker #1581

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
METADATA_FETCH_MODE = values.pg-boss-worker.metadata-fetch-mode;

STAKE_POOL_PROVIDER_URL = "http://${config.name}-backend.${config.namespace}.svc.cluster.local";
NETWORK_INFO_PROVIDER_URL = "http://${config.name}-backend.${config.namespace}.svc.cluster.local";
NETWORK_INFO_PROVIDER = "blockfrost";

POSTGRES_POOL_MAX_STAKE_POOL = "5";
POSTGRES_HOST_STAKE_POOL = values.postgresName;
Expand Down
16 changes: 12 additions & 4 deletions packages/cardano-services/src/PgBoss/stakePoolRewardsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import { ServiceNames } from '../Program/programs/types';
import { accountActiveStake, poolDelegators, poolRewards } from './stakePoolRewardsQueries';
import { computeROS } from '../StakePool/TypeormStakePoolProvider/util';
import { missingProviderUrlOption } from '../Program/options/common';
import { networkInfoHttpProvider } from '@cardano-sdk/cardano-services-client';
import { BlockfrostNetworkInfoProvider, networkInfoHttpProvider } from '@cardano-sdk/cardano-services-client';
import { getBlockfrostClient } from '../util';
import { ProviderImplementation } from '../Program';

/** The version of the algorithm to compute rewards. */
export const REWARDS_COMPUTE_VERSION = 1;
Expand Down Expand Up @@ -211,16 +213,22 @@ const getLastSlot = async (provider: NetworkInfoProvider, epochNo: Cardano.Epoch

/** Creates a `stakePoolRewardsHandler`. */
export const stakePoolRewardsHandlerFactory: WorkerHandlerFactory = (options) => {
const { dataSource, db, lastRosEpochs, logger, networkInfoProviderUrl } = options;
const { dataSource, db, lastRosEpochs, logger, networkInfoProvider, networkInfoProviderUrl } = options;

// Introduced following code repetition as the correct form is source of a circular-deps:check failure.
// Solving it would require an invasive refactoring action, probably better to defer it.
// if (!lastRosEpochs) throw new MissingProgramOption(STAKE_POOL_REWARDS, Descriptions.LastRosEpochs);
if (!lastRosEpochs)
throw new MissingProgramOption(STAKE_POOL_REWARDS, 'Number of epochs over which lastRos is computed');
if (!networkInfoProviderUrl) throw missingProviderUrlOption(STAKE_POOL_REWARDS, ServiceNames.NetworkInfo);

const provider = networkInfoHttpProvider({ baseUrl: networkInfoProviderUrl, logger });
let provider: NetworkInfoProvider;
if (networkInfoProvider === ProviderImplementation.BLOCKFROST) {
provider = new BlockfrostNetworkInfoProvider(getBlockfrostClient(), logger);
} else if (networkInfoProviderUrl) {
provider = networkInfoHttpProvider({ baseUrl: networkInfoProviderUrl, logger });
} else {
throw missingProviderUrlOption(STAKE_POOL_REWARDS, ServiceNames.NetworkInfo);
}

return async (data: StakePoolRewardsJob) => {
const { epochNo } = data;
Expand Down
2 changes: 1 addition & 1 deletion packages/cardano-services/src/Program/options/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type ProviderImplementations = {
stakePoolProvider?: ProviderImplementation;
};
export const ProviderImplementationDescription = 'Select one of the available provider implementations';
const argParser = (impl: string) => ProviderImplementation[impl.toUpperCase() as keyof typeof ProviderImplementation];
export const argParser = (impl: string) => ProviderImplementation[impl.toUpperCase() as keyof typeof ProviderImplementation];
export const providerSelectionOptions = [
newOption(
'--asset-provider <assetProvider>',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const PARALLEL_JOBS_DEFAULT = 10;
export const PG_BOSS_WORKER_API_URL_DEFAULT = new URL('http://localhost:3003');

export enum PgBossWorkerOptionDescriptions {
NetworkInfoProvider = 'Select one of the available provider implementations',
ParallelJobs = 'Parallel jobs to run',
Queues = 'Comma separated queue names',
Schedules = 'File path for schedules configurations'
Expand All @@ -21,6 +22,7 @@ export enum PgBossWorkerOptionDescriptions {
export interface LoadPgBossWorkerDependencies {
dnsResolver?: (serviceName: string) => Promise<SrvRecord>;
logger?: Logger;
networkInfoProvider?: string;
}

const pgBossWorker = 'pg-boss-worker';
Expand Down
3 changes: 2 additions & 1 deletion packages/cardano-services/src/Program/services/pgboss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
createPgBoss,
isRecoverableTypeormError
} from '@cardano-sdk/projection-typeorm';
import { CommonProgramOptions, PosgresProgramOptions } from '../options';
import { CommonProgramOptions, PosgresProgramOptions, ProviderImplementation } from '../options';
import { DataSource } from 'typeorm';
import { HealthCheckResponse } from '@cardano-sdk/core';
import { HttpService } from '../../Http/HttpService';
Expand Down Expand Up @@ -88,6 +88,7 @@ export type PgBossWorkerArgs = CommonProgramOptions &
StakePoolMetadataProgramOptions &
PosgresProgramOptions<'DbSync'> &
PosgresProgramOptions<'StakePool'> & {
networkInfoProvider?: ProviderImplementation;
parallelJobs: number;
queues: PgBossQueue[];
schedules: Array<ScheduleConfig>;
Expand Down
14 changes: 12 additions & 2 deletions packages/cardano-services/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ import {
withHandlePolicyIdsOptions,
withOgmiosOptions,
withPostgresOptions,
withStakePoolMetadataOptions
withStakePoolMetadataOptions,
ProviderImplementationDescription,
ProviderImplementation,
argParser as providerImplArgParser,
} from './Program';
import { Command } from 'commander';
import { DB_CACHE_TTL_DEFAULT } from './InMemoryCache';
Expand Down Expand Up @@ -491,7 +494,14 @@ addOptions(
return readScheduleConfig(schedules);
},
[]
)
),
newOption(
'--network-info-provider <networkInfoProvider>',
ProviderImplementationDescription,
'NETWORK_INFO_PROVIDER',
providerImplArgParser,
ProviderImplementation.DBSYNC
).choices([ProviderImplementation.BLOCKFROST, ProviderImplementation.DBSYNC]),
]
).action(async (args: PgBossWorkerArgs) =>
runServer('pg-boss worker', { args }, () =>
Expand Down
Loading