Skip to content

Commit

Permalink
refactor: replace cardano-cli per ogmios
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Cruz committed May 30, 2022
1 parent b5c89d4 commit 477e82b
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 224 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "cardano-rosetta-server/ogmios"]
path = cardano-rosetta-server/ogmios
url = https://github.com/CardanoSolutions/ogmios
7 changes: 6 additions & 1 deletion cardano-rosetta-server/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ services:
max-size: "400k"
max-file: "20"
cardano-node-ogmios:
image: cardanosolutions/cardano-node-ogmios:${CARDANO_NODE_OGMIOS_VERSION:-v4.1.0}-${NETWORK:-mainnet}
build:
args:
- NETWORK=${NETWORK:-mainnet}
cache_from: [ cardanosolutions/cardano-node-ogmios:latest ]
context: ./ogmios
image: cardanosolutions/cardano-node-ogmios:${CARDANO_NODE_OGMIOS_VERSION:-v5.4.0}-${NETWORK:-mainnet}
logging:
driver: "json-file"
options:
Expand Down
1 change: 1 addition & 0 deletions cardano-rosetta-server/ogmios
Submodule ogmios added at 78bcc0
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { AddressType } from '../utils/constants';
import { isAddressTypeValid, isKeyValid } from '../utils/validations';
import { BlockService } from '../services/block-service';
import ApiError from '../api-error';
import { OgmiosClient } from '../utils/cardano/cli/cardanonode-client';
import { OgmiosClient } from '../utils/cardano/node/ogmios-client';

export interface ConstructionController {
constructionDerive(
Expand Down
4 changes: 2 additions & 2 deletions cardano-rosetta-server/src/server/controllers/controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import accountController, { AccountController } from './account-controller';
import networkController, { NetworkController } from './network-controller';
import constructionController, { ConstructionController } from './construction-controller';
import searchController, { SearchController } from './search-controller';
import { CardanoNode } from '../utils/cardano/cli/cardano-node';
import { OgmiosClient } from '../utils/cardano/cli/cardanonode-client';
import { CardanoNode } from '../utils/cardano/node/cardano-node';
import { OgmiosClient } from '../utils/cardano/node/ogmios-client';

export interface Controllers extends BlockController, AccountController, NetworkController, ConstructionController {
searchTransactions?(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FastifyRequest } from 'fastify';
import { NetworkService } from '../services/network-service';
import { CardanoNode } from '../utils/cardano/cli/cardano-node';
import { CardanoNode } from '../utils/cardano/node/cardano-node';
import {
INVALID_OPERATION_STATE,
MIDDLEWARE_VERSION,
Expand Down
11 changes: 5 additions & 6 deletions cardano-rosetta-server/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import createPool from './db/connection';
import * as Repostories from './db/repositories';
import buildServer from './server';
import * as Services from './services/services';
import * as CardanoNode from './utils/cardano/cli/cardano-node';
import * as CardanoNode from './utils/cardano/node/cardano-node';
import { Environment, parseEnvironment } from './utils/environment-parser';
import { DepositParameters } from './services/cardano-services';
import { OgmiosClient } from './utils/cardano/cli/cardanonode-client';
import * as OgmiosClient from './utils/cardano/node/ogmios-client';

// FIXME: validate the following paraemeters when implementing (2)
// https://github.com/input-output-hk/cardano-rosetta/issues/101
Expand All @@ -24,14 +24,13 @@ const depositParameters: DepositParameters = {

const start = async (databaseInstance: Pool) => {
let server;
let ogmiosClient: OgmiosClient | undefined;
let ogmiosClient: OgmiosClient.OgmiosClient | undefined;
try {
const environment: Environment = parseEnvironment();
const repository = Repostories.configure(databaseInstance);
// FIXME: validate the following paraemeters when implementing (2)
// https://github.com/input-output-hk/cardano-rosetta/issues/101
ogmiosClient = new OgmiosClient();
await ogmiosClient.initialize({ host: environment.OGMIOS_HOST, port: environment.OGMIOS_PORT });
ogmiosClient = await OgmiosClient.configure({ host: environment.OGMIOS_HOST, port: environment.OGMIOS_PORT });
const cardanoNode = CardanoNode.configure(environment.CARDANO_NODE_PATH);
const services = Services.configure(
repository,
Expand All @@ -49,7 +48,7 @@ const start = async (databaseInstance: Pool) => {

server.addHook('onClose', async (_, done) => {
await databaseInstance.end(done);
await OgmiosClient?.shutdown();
await ogmiosClient?.shutdown();
});
// eslint-disable-next-line no-magic-numbers
await server.listen(environment.PORT, environment.BIND_ADDRESS);
Expand Down
12 changes: 3 additions & 9 deletions cardano-rosetta-server/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import ApiError from './api-error';
import { Services } from './services/services';
import * as Controllers from './controllers/controllers';
import { IncomingMessage, Server, ServerResponse } from 'http';
import { CardanoNode } from './utils/cardano/cli/cardano-node';
import { CardanoNode } from './utils/cardano/node/cardano-node';
import { ErrorFactory } from './utils/errors';
import { OgmiosClient } from './utils/cardano/cli/cardanonode-client';
import { OgmiosClient } from './utils/cardano/node/ogmios-client';

interface ExtraParams {
networkId: string;
Expand Down Expand Up @@ -43,13 +43,7 @@ const buildServer = (
server.register(fastifyBlipp);
server.register(openapiGlue, {
specification: `${__dirname}/openApi.json`,
service: Controllers.configure(
services,
ogmiosClient,
cardanoNode,
pageSize,
disableSearchApi
),
service: Controllers.configure(services, ogmiosClient, cardanoNode, pageSize, disableSearchApi),
noAdditional: true
});

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* eslint-disable no-console */
/* eslint-disable no-magic-numbers */
/* eslint-disable no-useless-return */
import pRetry from 'p-retry';
import {
ConnectionConfig,
createConnectionObject,
createInteractionContext,
createStateQueryClient,
createTxSubmissionClient,
getServerHealth,
ServerHealth,
// Schema,
StateQuery,
TxSubmission
} from '@cardano-ogmios/client';
import { Logger } from 'fastify';

const MODULE_NAME = 'OgmiosClient';

export interface OgmiosClient {
submitTransaction(logger: Logger, transaction: string): Promise<void>;
shutdown(): Promise<void>;
}

export const configure = async (ogmiosConnectionConfig?: ConnectionConfig): Promise<OgmiosClient> => {
const serverHealthFetcher = await getServerHealth({ connection: createConnectionObject(ogmiosConnectionConfig) });
let txSubmissionClient: TxSubmission.TxSubmissionClient | undefined;
await pRetry(
async () => {
txSubmissionClient = await createTxSubmissionClient(
await createInteractionContext(
error => {
console.error({ module: MODULE_NAME, error: error.name }, error.message);
},
console.info,
{
connection: ogmiosConnectionConfig,
interactionType: 'LongRunning'
}
)
);
},
{
factor: 1.2,
retries: 100,
onFailedAttempt: () => console.log('Establishing connection to cardano-node')
}
);
return {
submitTransaction: async (logger: Logger, transaction: string) => {
if (serverHealthFetcher.networkSynchronization < 0.95) {
throw new Error('Operation requires synced node');
}
logger.info('[submitTransaction] About to submit transaction', transaction);
await txSubmissionClient?.submitTx(transaction);
logger.info('[submitTransaction] transaction successfully sent', transaction);
},
shutdown: async () => {
await txSubmissionClient?.shutdown();
}
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import StatusCodes from 'http-status-codes';
import { Pool } from 'pg';
import { FastifyInstance } from 'fastify';
import { cardanoCliMock, setupOfflineDatabase, setupServer } from '../utils/test-utils';
import { ogmiosClientMock, setupOfflineDatabase, setupServer } from '../utils/test-utils';
import { ErrorFactory, Errors } from '../../../src/server/utils/errors';
import {
CONSTRUCTION_SIGNED_TRANSACTION_WITH_EXTRA_DATA,
Expand Down Expand Up @@ -64,7 +64,7 @@ describe(CONSTRUCTION_SUBMIT_ENDPOINT, () => {
});

it('Should return the transaction identifier is request is valid', async () => {
const mock = cardanoCliMock.submitTransaction as jest.Mock;
const mock = ogmiosClientMock.submitTransaction as jest.Mock;
mock.mockClear();
const response = await server.inject({
method: 'post',
Expand All @@ -83,7 +83,7 @@ describe(CONSTRUCTION_SUBMIT_ENDPOINT, () => {
});

it('Should return an error if there is a problem when sending the transaction', async () => {
const mock = cardanoCliMock.submitTransaction as jest.Mock;
const mock = ogmiosClientMock.submitTransaction as jest.Mock;
mock.mockClear();
mock.mockImplementation(() => {
throw new Error(ERROR_WHEN_CALLING_CARDANO_CLI);
Expand All @@ -98,7 +98,7 @@ describe(CONSTRUCTION_SUBMIT_ENDPOINT, () => {
)
});
expect(response.statusCode).toEqual(StatusCodes.INTERNAL_SERVER_ERROR);
expect((cardanoCliMock.submitTransaction as jest.Mock).mock.calls.length).toBe(1);
expect((ogmiosClientMock.submitTransaction as jest.Mock).mock.calls.length).toBe(1);
expect(response.json()).toEqual({
code: 5006,
details: {
Expand All @@ -110,7 +110,7 @@ describe(CONSTRUCTION_SUBMIT_ENDPOINT, () => {
});

it('Should return an non retriable error if there is OutsideValidityIntervalUTxO error from the node', async () => {
const mock = cardanoCliMock.submitTransaction as jest.Mock;
const mock = ogmiosClientMock.submitTransaction as jest.Mock;
mock.mockClear();
mock.mockImplementation(() => {
throw ErrorFactory.sendOutsideValidityIntervalUtxoError(ERROR_OUTSIDE_VALIDITY_INTERVAL_UTXO);
Expand All @@ -125,7 +125,7 @@ describe(CONSTRUCTION_SUBMIT_ENDPOINT, () => {
)
});
expect(response.statusCode).toEqual(StatusCodes.INTERNAL_SERVER_ERROR);
expect((cardanoCliMock.submitTransaction as jest.Mock).mock.calls.length).toBe(1);
expect((ogmiosClientMock.submitTransaction as jest.Mock).mock.calls.length).toBe(1);
expect(response.json()).toEqual({
code: 4037,
message: ERROR_OUTSIDE_VALIDITY_INTERVAL_UTXO,
Expand Down
8 changes: 4 additions & 4 deletions cardano-rosetta-server/test/e2e/environment-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ describe('Environment parser test', () => {
process.env.PORT = previousPort;
});
test('Should throw an error if a field is expected to be a valid file path but file does not exists', () => {
const previousPath = process.env.CARDANO_CLI_PATH;
process.env.CARDANO_CLI_PATH = fakePath;
const previousPath = process.env.TOPOLOGY_FILE_PATH;
process.env.TOPOLOGY_FILE_PATH = fakePath;
const mockExit = jest.spyOn(process, 'exit').mockImplementation((code?: number | undefined): never => {
throw new Error(code?.toString());
});
expect(environmentParser).toThrowError();
expect(mockExit).toHaveBeenCalledWith(1);
process.env.CARDANO_CLI_PATH = previousPath;
process.env.TOPOLOGY_FILE_PATH = previousPath;
});
// eslint-disable-next-line max-len
test('Should throw an error if a field is expected to be a valid file path with a valid schema but file has not a valid schema', () => {
const previousPath = process.env.TOPOLOGY_FILE_PATH;
process.env.TOPOLOGY_FILE_PATH = process.env.CARDANO_CLI_PATH;
process.env.TOPOLOGY_FILE_PATH = process.env.CARDANO_NODE_PATH;
const mockExit = jest.spyOn(process, 'exit').mockImplementation((code?: number | undefined): never => {
throw new Error(code?.toString());
});
Expand Down
9 changes: 4 additions & 5 deletions cardano-rosetta-server/test/e2e/utils/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import * as Services from '../../../src/server/services/services';
import createPool from '../../../src/server/db/connection';
import buildServer from '../../../src/server/server';
import { Pool } from 'pg';
import { CardanoNode } from '../../../src/server/utils/cardano/cli/cardano-node';
import { CardanoNode } from '../../../src/server/utils/cardano/node/cardano-node';
import { OperationType } from '../../../src/server/utils/constants';
import { OgmiosClient } from '../../../src/server/utils/cardano/cli/cardanonode-client';
import { OgmiosClient } from '../../../src/server/utils/cardano/node/ogmios-client';

const DEFAULT_PAGE_SIZE = 5;

Expand All @@ -37,9 +37,8 @@ export const setupDatabase = (connectionString = process.env.DB_CONNECTION_STRIN
return createPool(`postgresql://${user}:${password}@${host}:${port}/${database}`);
};

export const cardanoCliMock: OgmiosClient = {
export const ogmiosClientMock: OgmiosClient = {
submitTransaction: jest.fn(),
initialize: jest.fn(),
shutdown: jest.fn()
};

Expand Down Expand Up @@ -67,7 +66,7 @@ export const setupServer = (database: Pool, disableSearchApi = false): FastifyIn
Number(process.env.DEFAULT_RELATIVE_TTL),
depositParameters
);
return buildServer(services, cardanoCliMock, cardanoNodeMock, process.env.LOGGER_LEVEL, {
return buildServer(services, ogmiosClientMock, cardanoNodeMock, process.env.LOGGER_LEVEL, {
networkId: NETWORK_ID,
pageSize: Number(process.env.PAGE_SIZE) || DEFAULT_PAGE_SIZE,
mock: true,
Expand Down
Loading

0 comments on commit 477e82b

Please sign in to comment.