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

Refactor/replace cardano cli per ogmios #471

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ jobs:
GENESIS_SHELLEY_PATH: test/e2e/utils/mainnet-genesis.json
PAGE_SIZE: 5
DEFAULT_RELATIVE_TTL: 1000
OGMIOS_HOST: localhost
OGMIOS_PORT: 1337
- name: Build Cardano Rosetta
uses: ./cardano-rosetta/.github/actions/build_cardano_rosetta
with:
Expand Down
24 changes: 15 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ RUN cabal install cardano-node \
--install-method=copy \
--installdir=/usr/local/bin \
-f -systemd
RUN cabal install cardano-cli \
--install-method=copy \
--installdir=/usr/local/bin \
-f -systemd
WORKDIR /app/src
ARG CARDANO_DB_SYNC_VERSION=13.0.2
RUN git clone https://github.com/input-output-hk/cardano-db-sync.git &&\
Expand All @@ -80,6 +76,16 @@ WORKDIR /app/src/cardano-db-sync
RUN cabal install cardano-db-sync \
--install-method=copy \
--installdir=/usr/local/bin
WORKDIR /app/src
ARG OGMIOS_VERSION=v5.5.3
RUN git clone https://github.com/CardanoSolutions/ogmios.git &&\
cd ogmios &&\
git fetch --all --tags &&\
git checkout ${OGMIOS_VERSION}
WORKDIR /app/src/ogmios/server
RUN cabal install exe:ogmios \
--install-method=copy \
--installdir=/usr/local/bin
# Cleanup for runtiume-base copy of /usr/local/lib
RUN rm -rf /usr/local/lib/ghc-${GHC_VERSION} /usr/local/lib/pkgconfig

Expand All @@ -106,9 +112,9 @@ RUN curl --proto '=https' --tlsv1.2 -sSf -L https://www.postgresql.org/media/key
npm install pm2 -g
COPY --from=haskell-builder /usr/local/lib /usr/local/lib
COPY --from=haskell-builder /usr/local/bin/cardano-node /usr/local/bin/
COPY --from=haskell-builder /usr/local/bin/cardano-cli /usr/local/bin/
COPY --from=haskell-builder /usr/local/bin/cardano-db-sync /usr/local/bin/
COPY --from=haskell-builder /app/src/cardano-db-sync/schema /cardano-db-sync/schema
COPY --from=haskell-builder /usr/local/bin/ogmios /usr/local/bin/
# easy step-down from root
# https://github.com/tianon/gosu/releases
ENV GOSU_VERSION 1.12
Expand Down Expand Up @@ -156,8 +162,7 @@ RUN yarn --offline --frozen-lockfile --non-interactive --production

FROM ubuntu-nodejs as cardano-rosetta-server
ARG NETWORK=mainnet
COPY --from=haskell-builder /usr/local/bin/cardano-cli \
/usr/local/bin/cardano-node \
COPY --from=haskell-builder /usr/local/bin/cardano-node \
/usr/local/bin/
COPY --from=rosetta-server-builder /app/dist /cardano-rosetta-server/dist
COPY --from=rosetta-server-production-deps /app/node_modules /cardano-rosetta-server/node_modules
Expand All @@ -173,11 +178,12 @@ COPY --from=rosetta-server-builder /app/dist /cardano-rosetta-server/dist
COPY --from=rosetta-server-production-deps /app/node_modules /cardano-rosetta-server/node_modules
COPY ecosystem.config.js .
COPY postgresql.conf /etc/postgresql/12/main/postgresql.conf
COPY scripts/start_cardano-db-sync.sh scripts/maybe_download_cardano-db-sync_snapshot.sh /scripts/
COPY scripts/start_cardano-db-sync.sh scripts/maybe_download_cardano-db-sync_snapshot.sh scripts/modify_cardano-db-sync_prometheus_port.sh /scripts/
COPY config/network/${NETWORK} /config/
ENV PGPASSFILE=/config/cardano-db-sync/pgpass
RUN echo "/var/run/postgresql:5432:cexplorer:*:*" > $PGPASSFILE &&\
chmod 600 $PGPASSFILE && chown postgres:postgres $PGPASSFILE
RUN mkdir /snapshot &&\
./scripts/maybe_download_cardano-db-sync_snapshot.sh $SNAPSHOT_URL /snapshot
./scripts/maybe_download_cardano-db-sync_snapshot.sh $SNAPSHOT_URL /snapshot &&\
./scripts/modify_cardano-db-sync_prometheus_port.sh
COPY scripts/entrypoint.sh .
4 changes: 4 additions & 0 deletions cardano-rosetta-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ EXEMPTION_TYPES_PATH="/etc/node/exemptions.json"
BODY_LIMIT=1048576
# disables Search Api if its true
DISABLE_SEARCH_API=false
# Ogmios url
OGMIOS_HOST=localhost
# Ogmios port
OGMIOS_PORT=1337
```

### Install packages from offline cache
Expand Down
24 changes: 13 additions & 11 deletions cardano-rosetta-server/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ services:
driver: "json-file"
options:
max-size: "200k"
max-file: "10"
cardano-node:
image: inputoutput/cardano-node:${CARDANO_NODE_VERSION:-1.35.0}
environment:
- NETWORK=${NETWORK:-mainnet}
volumes:
- node-db:/data/db
- node-ipc:/ipc
max-file: "10"
cardano-node-ogmios:
image: cardanosolutions/cardano-node-ogmios:${CARDANO_NODE_OGMIOS_VERSION:-v5.5.3_1.35.2}-${NETWORK:-mainnet}
logging:
driver: "json-file"
options:
max-size: "400k"
max-file: "20"
ports:
- ${OGMIOS_PORT:-1337}:1337
restart: on-failure
volumes:
- node-db:/db
- node-ipc:/ipc
cardano-db-sync:
image: inputoutput/cardano-db-sync:${CARDANO_DB_SYNC_VERSION:-13.0.0}
command: [
Expand All @@ -45,15 +46,15 @@ services:
- RESTORE_SNAPSHOT=${RESTORE_SNAPSHOT:-}
- RESTORE_RECREATE_DB=N
depends_on:
- cardano-node
- cardano-node-ogmios
- postgres
secrets:
- postgres_password
- postgres_user
- postgres_db
volumes:
- ../config/network/${NETWORK:-mainnet}:/config
- db-sync-data:/data
- db-sync-data:/var/lib/cexplorer
- node-ipc:/node-ipc
restart: on-failure
logging:
Expand All @@ -69,7 +70,6 @@ services:
target: cardano-rosetta-server
environment:
- BIND_ADDRESS=0.0.0.0
- CARDANO_CLI_PATH=/usr/local/bin/cardano-cli
- CARDANO_NODE_PATH=/usr/local/bin/cardano-node
- CARDANO_NODE_SOCKET_PATH=/node-ipc/node.socket
- DB_CONNECTION_STRING=postgresql://postgres:notForProduction!@postgres:5432/cexplorer
Expand All @@ -79,6 +79,8 @@ services:
- PAGE_SIZE=30
- PORT=8080
- TOPOLOGY_FILE_PATH=/config/cardano-node/topology.json
- OGMIOS_HOST=cardano-node-ogmios
- OGMIOS_PORT=1337
expose:
- 8080
ports:
Expand Down
7 changes: 6 additions & 1 deletion cardano-rosetta-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@types/node": "14.0.14",
"@types/pg": "7.14.4",
"@types/pino": "6.3.0",
"@types/set-interval-async": "1.0.0",
"@typescript-eslint/eslint-plugin": "3.5.0",
"@typescript-eslint/parser": "3.5.0",
"dockerode": "3.2.0",
Expand All @@ -78,6 +79,7 @@
},
"dependencies": {
"@emurgo/cardano-serialization-lib-nodejs": "11.0.0",
"@cardano-ogmios/client": "5.5.3",
"cbor": "5.1.0",
"dotenv": "8.2.0",
"envalid": "6.0.2",
Expand All @@ -88,8 +90,11 @@
"fastify-openapi-glue": "1.6.0",
"http-status-codes": "1.4.0",
"moment": "2.29.4",
"p-retry": "4.6.2",
"pg": "8.3.0",
"temp-write": "4.0.0"
"set-interval-async": "2.0.3",
"temp-write": "4.0.0",
"ts-log": "2.2.3"
},
"resolutions": {
"typescript": "3.9.5"
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
8 changes: 7 additions & 1 deletion cardano-rosetta-server/src/server/api-error.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
interface Details {
message: string;
}
interface Reason {
name: string;
details: string;
}

/**
* Custom error class to implement Rosetta Error Schema
Expand All @@ -9,8 +13,9 @@ export default class ApiError extends Error implements Components.Schemas.Error
code: number;
retriable: boolean;
details?: Details;
reasons?: Reason[];

constructor(code: number, message: string, retriable: boolean, details?: string) {
constructor(code: number, message: string, retriable: boolean, details?: string, reasons?: Reason[]) {
super(message);

// Set the prototype explicitly.
Expand All @@ -22,5 +27,6 @@ export default class ApiError extends Error implements Components.Schemas.Error
if (details) {
this.details = { message: details };
}
if (reasons) this.reasons = reasons;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import {
mapAmount,
mapToConstructionHashResponse
} from '../utils/data-mapper';
import { ErrorFactory, ErrorUtils } from '../utils/errors';
import { ErrorFactory, Errors } from '../utils/errors';
import { withNetworkValidation } from './controllers-helper';
import { CardanoCli } from '../utils/cardano/cli/cardanonode-cli';
import { NetworkService } from '../services/network-service';
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/node/ogmios-client';
import { parseNodeErrorDetails } from '../utils/formatters';

export interface ConstructionController {
constructionDerive(
Expand Down Expand Up @@ -55,7 +56,7 @@ export interface ConstructionController {
const configure = (
constructionService: ConstructionService,
cardanoService: CardanoService,
cardanoCli: CardanoCli,
ogmiosClient: OgmiosClient,
networkService: NetworkService,
blockService: BlockService
): ConstructionController => ({
Expand Down Expand Up @@ -277,21 +278,29 @@ const configure = (
const logger = request.log;
const [signedTransaction] = await decodeExtraData(request.body.signed_transaction);
logger.info('[constructionHash] About to get hash of signed transaction');
const transactionHash = cardanoService.getHashOfSignedTransaction(logger, signedTransaction);
const hash = cardanoService.getHashOfSignedTransaction(logger, signedTransaction);
logger.info(`[constructionSubmit] About to submit ${signedTransaction}`);
await cardanoCli.submitTransaction(
logger,
signedTransaction,
request.body.network_identifier.network === 'mainnet'
);
// eslint-disable-next-line camelcase
return { transaction_identifier: { hash: transactionHash } };
await ogmiosClient.submitTransaction(signedTransaction);
logger.info('[constructionHash] About to get hash of signed transaction');
return { transaction_identifier: { hash } };
} catch (error) {
request.log.error(error);
if (error instanceof ApiError) throw error;
return ErrorUtils.resolveApiErrorFromNodeError(error.message)
.then((mappedError: ApiError) => mappedError)
.catch(() => ErrorFactory.sendTransactionError(error.message));
if (Array.isArray(error)) {
const { code, message } = Errors.SEND_TRANSACTION_ERROR;
throw new ApiError(
code,
message,
false,
undefined,
// eslint-disable-next-line unicorn/prevent-abbreviations
error.map(err => ({
name: err.name,
details: parseNodeErrorDetails(err.message)
}))
);
} else {
throw ErrorFactory.sendTransactionError(error.message);
}
}
},
request.log,
Expand Down
8 changes: 4 additions & 4 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 { CardanoCli } from '../utils/cardano/cli/cardanonode-cli';
import { CardanoNode } from '../utils/cardano/cli/cardano-node';
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 All @@ -21,7 +21,7 @@ export interface Controllers extends BlockController, AccountController, Network
*/
export const configure = (
services: Services,
cardanoCli: CardanoCli,
ogmiosClient: OgmiosClient,
cardanoNode: CardanoNode,
pageSize: number,
disableSearchApi = false
Expand All @@ -33,7 +33,7 @@ export const configure = (
...constructionController(
services.constructionService,
services.cardanoService,
cardanoCli,
ogmiosClient,
services.networkService,
services.blockService
)
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
16 changes: 11 additions & 5 deletions cardano-rosetta-server/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +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 CardanoCli from './utils/cardano/cli/cardanonode-cli';
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 * 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 @@ -18,32 +19,37 @@ const networkId = genesis.networkId.toLowerCase();

const start = async (databaseInstance: Pool) => {
let server;
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 = await OgmiosClient.configure({ host: environment.OGMIOS_HOST, port: environment.OGMIOS_PORT });
const cardanoNode = CardanoNode.configure(environment.CARDANO_NODE_PATH);
const cardanoCli = CardanoCli.configure(environment.CARDANO_CLI_PATH, networkMagic);
const services = Services.configure(
repository,
networkId,
networkMagic,
environment.TOPOLOGY_FILE,
environment.DEFAULT_RELATIVE_TTL
);
server = buildServer(services, cardanoCli, cardanoNode, environment.LOGGER_LEVEL, {
server = buildServer(services, ogmiosClient, cardanoNode, environment.LOGGER_LEVEL, {
networkId,
pageSize: environment.PAGE_SIZE,
disableSearchApi: environment.DISABLE_SEARCH_API
});

server.addHook('onClose', (_, done) => databaseInstance.end(done));
server.addHook('onClose', async (_, done) => {
await databaseInstance.end(done);
await ogmiosClient?.shutdown();
});
// eslint-disable-next-line no-magic-numbers
await server.listen(environment.PORT, environment.BIND_ADDRESS);
server.blipp();
} catch (error) {
server?.log.error(error);
await ogmiosClient?.shutdown();
await databaseInstance?.end();
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
Expand Down
13 changes: 13 additions & 0 deletions cardano-rosetta-server/src/server/openApi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2460,6 +2460,19 @@
"address": "0x1dcc4de8dec75d7aab85b567b6",
"error": "not base64"
}
},
"reasons": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"details": {
}
}
}
}
}
}
Expand Down
Loading