Skip to content

Commit

Permalink
refactor!: standardize provider dependencies
Browse files Browse the repository at this point in the history
- standardize DbSyncProvider deps as DbSynProviderDependencies
- add debug log on DbSyncProvider health check
- align all provider dependencies structure
- fix dependency warning in core.package.json

BREAKING CHANGE:

- all provider constructors are updated to use standardized form of deps
  • Loading branch information
Ivaylo Andonov committed Jan 9, 2023
1 parent ee9868e commit 05b37e6
Show file tree
Hide file tree
Showing 18 changed files with 165 additions and 134 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,12 @@
import {
Asset,
AssetProvider,
Cardano,
CardanoNode,
GetAssetArgs,
ProviderError,
ProviderFailure
} from '@cardano-sdk/core';
import { Asset, AssetProvider, Cardano, GetAssetArgs, ProviderError, ProviderFailure } from '@cardano-sdk/core';
import { AssetBuilder } from './AssetBuilder';
import { Logger } from 'ts-log';
import { Pool } from 'pg';
import { DbSyncProvider } from '../../util/DbSyncProvider';
import { DbSyncProvider, DbSyncProviderDependencies } from '../../util/DbSyncProvider';
import { NftMetadataService, TokenMetadataService } from '../types';

/**
* Dependencies that are need to create DbSyncAssetProvider
*/
export type DbSyncAssetProviderDependencies = {
/**
* The db-sync database PgPool
*/
db: Pool;

/**
* The Ogmios Cardano node
*/
cardanoNode: CardanoNode;

/**
*
* The logger object
*/
logger: Logger;
export interface DbSyncAssetProviderDependencies extends DbSyncProviderDependencies {
/**
* The NftMetadataService to retrieve Asset.NftMetadata.
*/
Expand All @@ -40,7 +15,7 @@ export type DbSyncAssetProviderDependencies = {
* The TokenMetadataService to retrieve Asset.TokenMetadata.
*/
tokenMetadataService: TokenMetadataService;
};
}

/**
* AssetProvider implementation using NftMetadataService, TokenMetadataService
Expand All @@ -52,8 +27,7 @@ export class DbSyncAssetProvider extends DbSyncProvider() implements AssetProvid

constructor(dependencies: DbSyncAssetProviderDependencies) {
const { db, cardanoNode, logger } = dependencies;

super(db, cardanoNode);
super({ cardanoNode, db, logger });

this.#builder = new AssetBuilder(db, logger);
this.#dependencies = dependencies;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { BlockModel, BlockOutputModel, TipModel, TxInputModel, TxModel, TxOutput
import {
BlocksByIdsArgs,
Cardano,
CardanoNode,
ChainHistoryProvider,
Paginated,
ProviderError,
Expand All @@ -14,36 +13,45 @@ import {
} from '@cardano-sdk/core';
import { ChainHistoryBuilder } from './ChainHistoryBuilder';
import { DB_MAX_SAFE_INTEGER } from './queries';
import { DbSyncProvider } from '../../util/DbSyncProvider';
import { Logger } from 'ts-log';
import { Pool, QueryResult } from 'pg';
import { DbSyncProvider, DbSyncProviderDependencies } from '../../util/DbSyncProvider';
import { QueryResult } from 'pg';
import { TxMetadataService } from '../../Metadata';
import { applyPagination } from './util';
import { hexStringToBuffer } from '@cardano-sdk/util';
import { mapBlock, mapTxAlonzo, mapTxIn, mapTxInModel, mapTxOut, mapTxOutModel } from './mappers';
import orderBy from 'lodash/orderBy';
import uniq from 'lodash/uniq';

/**
* Properties that are need to create DbSyncChainHistoryProvider
*/
export interface ChainHistoryProviderProps {
/**
* Pagination page size limit used for provider methods constraint.
*/
paginationPageSizeLimit: number;
}
export interface ChainHistoryProviderDependencies {
db: Pool;
cardanoNode: CardanoNode;

/**
* Dependencies that are need to create DbSyncChainHistoryProvider
*/
export interface ChainHistoryProviderDependencies extends DbSyncProviderDependencies {
/**
* The TxMetadataService to retrieve transactions metadata by hashes.
*/
metadataService: TxMetadataService;
logger: Logger;
}

export class DbSyncChainHistoryProvider extends DbSyncProvider() implements ChainHistoryProvider {
#paginationPageSizeLimit: number;
#builder: ChainHistoryBuilder;
#metadataService: TxMetadataService;
#logger: Logger;

constructor(
{ paginationPageSizeLimit }: ChainHistoryProviderProps,
{ db, cardanoNode, metadataService, logger }: ChainHistoryProviderDependencies
) {
super(db, cardanoNode);
this.#logger = logger;
super({ cardanoNode, db, logger });
this.#builder = new ChainHistoryBuilder(db, logger);
this.#metadataService = metadataService;
this.#paginationPageSizeLimit = paginationPageSizeLimit;
Expand Down Expand Up @@ -73,7 +81,7 @@ export class DbSyncChainHistoryProvider extends DbSyncProvider() implements Chai
const lowerBound = blockRange?.lowerBound ?? 0;
const upperBound = blockRange?.upperBound ?? DB_MAX_SAFE_INTEGER;

this.#logger.debug(
this.logger.debug(
`About to find transactions of addresses ${addresses} ${
blockRange?.lowerBound ? `since block ${lowerBound}` : ''
} ${blockRange?.upperBound ? `and before ${upperBound}` : ''}`
Expand Down Expand Up @@ -114,7 +122,7 @@ export class DbSyncChainHistoryProvider extends DbSyncProvider() implements Chai
}

const byteIds = ids.map((id) => hexStringToBuffer(id.toString()));
this.#logger.debug('About to find transactions with hashes:', byteIds);
this.logger.debug('About to find transactions with hashes:', byteIds);
const txResults: QueryResult<TxModel> = await this.db.query(Queries.findTransactionsByHashes, [byteIds]);
if (txResults.rows.length === 0) return [];

Expand Down Expand Up @@ -157,17 +165,17 @@ export class DbSyncChainHistoryProvider extends DbSyncProvider() implements Chai
);
}

this.#logger.debug('About to find network tip');
this.logger.debug('About to find network tip');
const tipResult: QueryResult<TipModel> = await this.db.query(Queries.findTip);
const tip: TipModel = tipResult.rows[0];
if (!tip) return [];

const byteIds = ids.map((id) => hexStringToBuffer(id.toString()));
this.#logger.debug('About to find blocks with hashes:', byteIds);
this.logger.debug('About to find blocks with hashes:', byteIds);
const blocksResult: QueryResult<BlockModel> = await this.db.query(Queries.findBlocksByHashes, [byteIds]);
if (blocksResult.rows.length === 0) return [];

this.#logger.debug('About to find blocks outputs and fees for blocks:', byteIds);
this.logger.debug('About to find blocks outputs and fees for blocks:', byteIds);
const outputResult: QueryResult<BlockOutputModel> = await this.db.query(Queries.findBlocksOutputByHashes, [
byteIds
]);
Expand Down
10 changes: 7 additions & 3 deletions packages/cardano-services/src/Metadata/DbSyncMetadataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@ import { TxMetadataModel, TxMetadataService } from './types';
import { hexStringToBuffer } from '@cardano-sdk/util';
import { mapTxMetadata } from './mappers';

export type TxMetadataByHashes = Map<Cardano.TransactionId, Cardano.TxMetadata>;

export const createDbSyncMetadataService = (db: Pool, logger: Logger): TxMetadataService => ({
async queryTxMetadataByHashes(
hashes: Cardano.TransactionId[]
): Promise<Map<Cardano.TransactionId, Cardano.TxMetadata>> {
async queryTxMetadataByHashes(hashes: Cardano.TransactionId[]): Promise<TxMetadataByHashes> {
const byteHashes = hashes.map((hash) => hexStringToBuffer(hash.toString()));
logger.debug('About to find metadata for txs:', hashes);

const result: QueryResult<TxMetadataModel> = await db.query(Queries.findTxMetadata, [byteHashes]);

if (result.rows.length === 0) return new Map();
const metadataMap: Map<Cardano.TransactionId, TxMetadataModel[]> = new Map();

for (const metadata of result.rows) {
const txId = Cardano.TransactionId(metadata.tx_id.toString('hex'));
const currentMetadata: TxMetadataModel[] = metadataMap.get(txId) ?? [];
metadataMap.set(txId, [...currentMetadata, metadata]);
}

return new Map([...metadataMap].map(([id, metadata]) => [id, mapTxMetadata(metadata)]));
}
});
Original file line number Diff line number Diff line change
@@ -1,34 +1,46 @@
import * as NetworkInfoCacheKey from './keys';
import {
Cardano,
CardanoNode,
CardanoNodeUtil,
EraSummary,
NetworkInfoProvider,
StakeSummary,
SupplySummary,
createSlotEpochCalc
} from '@cardano-sdk/core';
import { DbSyncProvider } from '../../util/DbSyncProvider';
import { DbSyncProvider, DbSyncProviderDependencies } from '../../util/DbSyncProvider';
import { Disposer, EpochMonitor } from '../../util/polling/types';
import { GenesisData } from './types';
import { InMemoryCache, UNLIMITED_CACHE_TTL } from '../../InMemoryCache';
import { Logger } from 'ts-log';
import { NetworkInfoBuilder } from './NetworkInfoBuilder';
import { Pool } from 'pg';
import { RunnableModule } from '@cardano-sdk/util';
import { loadGenesisData, toGenesisParams, toLedgerTip, toProtocolParams, toSupply } from './mappers';

/**
* Properties that are need to create DbSyncNetworkInfoProvider
*/
export interface NetworkInfoProviderProps {
/**
* Cardano node config path string
*/
cardanoNodeConfigPath: string;
}
export interface NetworkInfoProviderDependencies {
db: Pool;

/**
* Dependencies that are need to create DbSyncNetworkInfoProvider
*/
export interface NetworkInfoProviderDependencies extends DbSyncProviderDependencies {
/**
* The in memory cache engine.
*/
cache: InMemoryCache;
logger: Logger;
cardanoNode: CardanoNode;
/**
* Monitor the epoch rollover through db polling.
*/
epochMonitor: EpochMonitor;
}

export class DbSyncNetworkInfoProvider extends DbSyncProvider(RunnableModule) implements NetworkInfoProvider {
#logger: Logger;
#cache: InMemoryCache;
Expand All @@ -43,7 +55,8 @@ export class DbSyncNetworkInfoProvider extends DbSyncProvider(RunnableModule) im
{ cardanoNodeConfigPath }: NetworkInfoProviderProps,
{ db, cache, logger, cardanoNode, epochMonitor }: NetworkInfoProviderDependencies
) {
super(db, cardanoNode, 'DbSyncNetworkInfoProvider', logger);
super({ cardanoNode, db, logger }, 'DbSyncNetworkInfoProvider', logger);

this.#logger = logger;
this.#cache = cache;
this.#currentEpoch = Cardano.EpochNo(0);
Expand Down
6 changes: 3 additions & 3 deletions packages/cardano-services/src/Program/services/ogmios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const recreateOgmiosTxSubmitProvider = async (
await ogmiosTxSubmitProvider
.shutdown()
.catch((error_) => logger.warn(`OgmiosTxSubmitProvider failed to shutdown after connection error: ${error_}`));
return new OgmiosTxSubmitProvider({ host: record.name, port: record.port }, logger);
return new OgmiosTxSubmitProvider({ host: record.name, port: record.port }, { logger });
};
/**
* Creates an extended TxSubmitProvider instance :
Expand All @@ -56,7 +56,7 @@ export const ogmiosTxSubmitProviderWithDiscovery = async (
serviceName: string
): Promise<OgmiosTxSubmitProvider> => {
const { name, port } = await dnsResolver(serviceName!);
let ogmiosProvider = new OgmiosTxSubmitProvider({ host: name, port }, logger);
let ogmiosProvider = new OgmiosTxSubmitProvider({ host: name, port }, { logger });

const txSubmitProviderProxy = new Proxy<OgmiosTxSubmitProvider>({} as OgmiosTxSubmitProvider, {
get(_, prop) {
Expand Down Expand Up @@ -103,7 +103,7 @@ export const getOgmiosTxSubmitProvider = async (
): Promise<OgmiosTxSubmitProvider> => {
if (options?.ogmiosSrvServiceName)
return ogmiosTxSubmitProviderWithDiscovery(dnsResolver, logger, options.ogmiosSrvServiceName);
if (options?.ogmiosUrl) return new OgmiosTxSubmitProvider(urlToConnectionConfig(options?.ogmiosUrl), logger);
if (options?.ogmiosUrl) return new OgmiosTxSubmitProvider(urlToConnectionConfig(options?.ogmiosUrl), { logger });
throw new MissingCardanoNodeOption([
CommonOptionDescriptions.OgmiosUrl,
CommonOptionDescriptions.OgmiosSrvServiceName
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
import { DbSyncProvider, DbSyncProviderDependencies } from '../../util/DbSyncProvider';
import {
CardanoNode,
ProviderError,
ProviderFailure,
RewardAccountBalanceArgs,
RewardsHistoryArgs,
RewardsProvider
} from '@cardano-sdk/core';
import { DbSyncProvider } from '../../util/DbSyncProvider';
import { Logger } from 'ts-log';
import { Pool } from 'pg';
import { RewardsBuilder } from './RewardsBuilder';
import { rewardsToCore } from './mappers';

/**
* Properties that are need to create DbSyncRewardsProvider
*/
export interface RewardsProviderProps {
/**
* Pagination page size limit used for provider methods constraint.
*/
paginationPageSizeLimit: number;
}
export interface RewardsProviderDependencies {
db: Pool;
cardanoNode: CardanoNode;
logger: Logger;
}

export class DbSyncRewardsProvider extends DbSyncProvider() implements RewardsProvider {
#logger: Logger;
#builder: RewardsBuilder;
#paginationPageSizeLimit: number;

constructor(
{ paginationPageSizeLimit }: RewardsProviderProps,
{ db, cardanoNode, logger }: RewardsProviderDependencies
{ db, cardanoNode, logger }: DbSyncProviderDependencies
) {
super(db, cardanoNode);
this.#logger = logger;
super({ cardanoNode, db, logger });
this.#builder = new RewardsBuilder(db, logger);
this.#paginationPageSizeLimit = paginationPageSizeLimit;
}
Expand All @@ -48,7 +44,7 @@ export class DbSyncRewardsProvider extends DbSyncProvider() implements RewardsPr
return rewardsToCore(rewards);
}
public async rewardAccountBalance({ rewardAccount }: RewardAccountBalanceArgs) {
this.#logger.debug(`About to get balance of reward account ${rewardAccount.toString()}`);
this.logger.debug(`About to get balance of reward account ${rewardAccount.toString()}`);
const balance = await this.#builder.getAccountBalance(rewardAccount);
return BigInt(balance?.balance || '0');
}
Expand Down
Loading

0 comments on commit 05b37e6

Please sign in to comment.