Skip to content

Commit

Permalink
feat!: support the complete set of protocol parameters
Browse files Browse the repository at this point in the history
- rename method `currentWalletProtocolParameters` to `protocolParameters`
- update `/current-wallet-protocol-parameters` -> `/protocol-parameters`
- remove `ProtocolParametersRequiredByWallet` entirely
- support all protocol params by NetworkInfo and Blockfrost providers
- align code base with the introduced changes
- insert cost models fixture db data for tests
  • Loading branch information
Ivaylo Andonov committed Oct 31, 2022
1 parent b4dda8f commit 46d7aa9
Show file tree
Hide file tree
Showing 30 changed files with 194 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { NetworkInfoProvider } from '@cardano-sdk/core';
* The NetworkInfoProvider endpoint paths.
*/
const paths: HttpProviderConfigPaths<NetworkInfoProvider> = {
currentWalletProtocolParameters: '/current-wallet-protocol-parameters',
eraSummaries: '/era-summaries',
genesisParameters: '/genesis-parameters',
healthCheck: '/health',
ledgerTip: '/ledger-tip',
lovelaceSupply: '/lovelace-supply',
protocolParameters: '/protocol-parameters',
stake: '/stake'
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ describe('networkInfoHttpProvider', () => {
await expect(provider.genesisParameters()).resolves.toEqual({});
});

test('currentWalletProtocolParameters does not throw', async () => {
test('protocolParameters does not throw', async () => {
axiosMock.onPost().replyOnce(200, {});
const provider = networkInfoHttpProvider(config);
await expect(provider.currentWalletProtocolParameters()).resolves.toEqual({});
await expect(provider.protocolParameters()).resolves.toEqual({});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
CardanoNodeUtil,
EraSummary,
NetworkInfoProvider,
ProtocolParametersRequiredByWallet,
StakeSummary,
SupplySummary
} from '@cardano-sdk/core';
Expand All @@ -17,7 +16,7 @@ import { NetworkInfoBuilder } from './NetworkInfoBuilder';
import { NetworkInfoCacheKey } from '.';
import { Pool } from 'pg';
import { RunnableModule } from '@cardano-sdk/util';
import { loadGenesisData, toGenesisParams, toLedgerTip, toSupply, toWalletProtocolParams } from './mappers';
import { loadGenesisData, toGenesisParams, toLedgerTip, toProtocolParams, toSupply } from './mappers';

export interface NetworkInfoProviderProps {
cardanoNodeConfigPath: string;
Expand Down Expand Up @@ -54,9 +53,9 @@ export class DbSyncNetworkInfoProvider extends DbSyncProvider(RunnableModule) im
return toLedgerTip(tip);
}

public async currentWalletProtocolParameters(): Promise<ProtocolParametersRequiredByWallet> {
const currentProtocolParams = await this.#builder.queryCurrentWalletProtocolParams();
return toWalletProtocolParams(currentProtocolParams);
public async protocolParameters(): Promise<Cardano.ProtocolParameters> {
const currentProtocolParams = await this.#builder.queryProtocolParams();
return toProtocolParams(currentProtocolParams);
}

public async genesisParameters(): Promise<Cardano.CompactGenesis> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
CirculatingSupplyModel,
EpochModel,
LedgerTipModel,
TotalSupplyModel,
WalletProtocolParamsModel
ProtocolParamsModel,
TotalSupplyModel
} from './types';
import { Cardano } from '@cardano-sdk/core';
import { Logger } from 'ts-log';
Expand Down Expand Up @@ -49,11 +49,9 @@ export class NetworkInfoBuilder {
return result.rows[0];
}

public async queryCurrentWalletProtocolParams() {
this.#logger.debug('About to query current wallet protocol params');
const result: QueryResult<WalletProtocolParamsModel> = await this.#db.query(
Queries.findCurrentWalletProtocolParams
);
public async queryProtocolParams() {
this.#logger.debug('About to query protocol params');
const result: QueryResult<ProtocolParamsModel> = await this.#db.query(Queries.findProtocolParams);
return result.rows[0];
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import {
Cardano,
ProtocolParametersRequiredByWallet,
ProviderError,
ProviderFailure,
SupplySummary
} from '@cardano-sdk/core';
import { GenesisData, LedgerTipModel, WalletProtocolParamsModel } from './types';
import { Cardano, ProviderError, ProviderFailure, SupplySummary } from '@cardano-sdk/core';
import { CostModelsParamModel, GenesisData, LedgerTipModel, ProtocolParamsModel } from './types';
import JSONbig from 'json-bigint';
import fs from 'fs';
import path from 'path';
Expand All @@ -31,7 +25,14 @@ export const toLedgerTip = ({ block_no, slot_no, hash }: LedgerTipModel): Cardan
slot: Number(slot_no)
});

export const toWalletProtocolParams = ({
export const mapCostModels = (costs: CostModelsParamModel | null) => {
const models: Cardano.CostModels = [];
if (costs?.PlutusV1) models[Cardano.PlutusLanguageVersion.V1] = costs.PlutusV1;
if (costs?.PlutusV2) models[Cardano.PlutusLanguageVersion.V2] = costs.PlutusV2;
return models;
};

export const toProtocolParams = ({
coins_per_utxo_size,
max_tx_size,
max_val_size,
Expand All @@ -42,21 +43,59 @@ export const toWalletProtocolParams = ({
protocol_major,
protocol_minor,
min_fee_a,
min_fee_b
}: WalletProtocolParamsModel): ProtocolParametersRequiredByWallet => ({
min_fee_b,
max_block_size,
max_bh_size,
optimal_pool_count,
influence,
monetary_expand_rate,
treasury_growth_rate,
decentralisation,
collateral_percent,
price_mem,
price_step,
max_tx_ex_mem,
max_tx_ex_steps,
max_block_ex_mem,
max_block_ex_steps,
max_epoch,
costs
}: ProtocolParamsModel): Cardano.ProtocolParameters => ({
coinsPerUtxoByte: Number(coins_per_utxo_size),
collateralPercentage: collateral_percent,
costModels: mapCostModels(costs),
decentralizationParameter: String(decentralisation),
desiredNumberOfPools: optimal_pool_count,
maxBlockBodySize: max_block_size,
maxBlockHeaderSize: max_bh_size,
maxCollateralInputs: max_collateral_inputs,
maxExecutionUnitsPerBlock: {
memory: Number(max_block_ex_mem),
steps: Number(max_block_ex_steps)
},
maxExecutionUnitsPerTransaction: {
memory: Number(max_tx_ex_mem),
steps: Number(max_tx_ex_steps)
},
maxTxSize: max_tx_size,
maxValueSize: Number(max_val_size),
minFeeCoefficient: min_fee_a,
minFeeConstant: min_fee_b,
minPoolCost: Number(min_pool_cost),
monetaryExpansion: String(monetary_expand_rate),
poolDeposit: Number(pool_deposit),
poolInfluence: String(influence),
poolRetirementEpochBound: max_epoch,
prices: {
memory: price_mem,
steps: price_step
},
protocolVersion: {
major: protocol_major,
minor: protocol_minor
},
stakeKeyDeposit: Number(key_deposit)
stakeKeyDeposit: Number(key_deposit),
treasuryExpansion: String(treasury_growth_rate)
});

export const toGenesisParams = (genesis: GenesisData): Cardano.CompactGenesis => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const findLedgerTip = `
LIMIT 1;
`;

export const findCurrentWalletProtocolParams = `
export const findProtocolParams = `
SELECT
min_fee_a,
min_fee_b,
Expand All @@ -80,18 +80,21 @@ export const findCurrentWalletProtocolParams = `
max_tx_ex_steps,
max_block_ex_mem,
max_block_ex_steps,
max_epoch
FROM public.epoch_param
max_epoch,
cost_model.costs
FROM epoch_param
LEFT JOIN cost_model
ON cost_model.id = epoch_param.cost_model_id
ORDER BY epoch_no DESC NULLS LAST
LIMIT 1;
`;

const Queries = {
findActiveStake,
findCirculatingSupply,
findCurrentWalletProtocolParams,
findLatestCompleteEpoch,
findLedgerTip,
findProtocolParams,
findTotalSupply
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ export interface LedgerTipModel {
hash: Buffer;
}

export interface WalletProtocolParamsModel {
export interface CostModelsParamModel {
PlutusV1?: Cardano.CostModel;
PlutusV2?: Cardano.CostModel;
}

export interface ProtocolParamsModel {
coins_per_utxo_size: string;
max_tx_size: number;
max_val_size: string;
Expand All @@ -53,6 +58,7 @@ export interface WalletProtocolParamsModel {
max_block_ex_mem: string;
max_block_ex_steps: string;
max_epoch: number;
costs: CostModelsParamModel | null;
}

export interface GenesisData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,16 @@ export class NetworkInfoHttpService extends HttpService {
'/ledger-tip',
providerHandler(networkInfoProvider.ledgerTip.bind(networkInfoProvider))(HttpService.routeHandler(logger), logger)
);
router.post(
'/protocol-parameters',
providerHandler(networkInfoProvider.protocolParameters.bind(networkInfoProvider))(
HttpService.routeHandler(logger),
logger
)
);
router.post(
'/current-wallet-protocol-parameters',
providerHandler(networkInfoProvider.currentWalletProtocolParameters.bind(networkInfoProvider))(
providerHandler(networkInfoProvider.protocolParameters.bind(networkInfoProvider))(
HttpService.routeHandler(logger),
logger
)
Expand Down
31 changes: 29 additions & 2 deletions packages/cardano-services/src/NetworkInfo/openApi.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,33 @@
}
}
},
"/network-info/protocol-parameters": {
"post": {
"summary": "fetch protocol params",
"description": "Fetch Protocol Params",
"operationId": "protocolParams",
"requestBody": {
"content": {
"application/json": {}
}
},
"responses": {
"200": {
"description": "protocol params fetched",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProtocolParametersResponse"
}
}
}
},
"400": {
"description": "invalid request"
}
}
}
},
"/network-info/current-wallet-protocol-parameters": {
"post": {
"summary": "fetch current wallet protocol params",
Expand All @@ -136,7 +163,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CurrentWalletProtocolParametersResponse"
"$ref": "#/components/schemas/ProtocolParametersResponse"
}
}
}
Expand Down Expand Up @@ -308,7 +335,7 @@
}
}
},
"CurrentWalletProtocolParametersResponse": {
"ProtocolParametersResponse": {
"type": "object",
"required": [
"minFeeCoefficient",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ describe('NetworkInfoBuilder', () => {
});
});

describe('queryCurrentWalletProtocolParams', () => {
describe('queryProtocolParams', () => {
test('query wallet protocol params from current epoch', async () => {
const result = await builder.queryCurrentWalletProtocolParams();
const result = await builder.queryProtocolParams();
expect(result).toMatchSnapshot();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,27 @@ exports[`NetworkInfoBuilder queryActiveStake query active stake 1`] = `"15039823

exports[`NetworkInfoBuilder queryCirculatingSupply query circulating supply 1`] = `"42022749185221758"`;

exports[`NetworkInfoBuilder queryCurrentWalletProtocolParams query wallet protocol params from current epoch 1`] = `
exports[`NetworkInfoBuilder queryLatestEpoch query latest epoch 1`] = `205`;

exports[`NetworkInfoBuilder queryLedgerTip query ledger tip 1`] = `
Object {
"block_no": 3556390,
"hash": "332340bfcf47951b4bc8eca51c4e9190d29e2fd6dae30be231ebcdadb2d8c399",
"slot_no": "58336266",
}
`;

exports[`NetworkInfoBuilder queryProtocolParams query wallet protocol params from current epoch 1`] = `
Object {
"coins_per_utxo_size": "34482",
"collateral_percent": 150,
"costs": Object {
"PlutusV1": Object {
"bData-cpu-arguments": 150000,
"equalsString-memory-arguments": 1,
"iData-cpu-arguments": 150000,
},
},
"decentralisation": 0,
"influence": 0.3,
"key_deposit": "2000000",
Expand Down Expand Up @@ -35,14 +52,4 @@ Object {
}
`;

exports[`NetworkInfoBuilder queryLatestEpoch query latest epoch 1`] = `205`;

exports[`NetworkInfoBuilder queryLedgerTip query ledger tip 1`] = `
Object {
"block_no": 3556390,
"hash": "332340bfcf47951b4bc8eca51c4e9190d29e2fd6dae30be231ebcdadb2d8c399",
"slot_no": "58336266",
}
`;

exports[`NetworkInfoBuilder queryTotalSupply query total supply 1`] = `"40408479600434778"`;
Loading

0 comments on commit 46d7aa9

Please sign in to comment.