Skip to content

Commit

Permalink
Merge pull request #545 from input-output-hk/fix/txSubmitProvider-not…
Browse files Browse the repository at this point in the history
…-initialized-on-submitTx-in-srv-mode

fix(cardano-services): `OgmiosTxSubmitProvider` is not initialized error
  • Loading branch information
rhyslbw committed Nov 30, 2022
2 parents 322ad27 + 44f2455 commit 8f0bfb8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
5 changes: 3 additions & 2 deletions flake.nix
Expand Up @@ -80,14 +80,13 @@
mkdir -p $out/libexec/$sourceRoot $out/bin
test -f ${production-deps}/libexec/$sourceRoot/packages/cardano-services/node_modules && cp -r ${production-deps}/libexec/$sourceRoot/node_modules $out/libexec/$sourceRoot/node_modules
cp -r ${production-deps}/libexec/$sourceRoot/node_modules $out/libexec/$sourceRoot/node_modules
cp -r scripts $out/libexec/$sourceRoot/scripts
for p in cardano-services core ogmios util; do
mkdir -p $out/libexec/$sourceRoot/packages/$p
cp -r packages/$p/dist $out/libexec/$sourceRoot/packages/$p/dist
cp -r packages/$p/package.json $out/libexec/$sourceRoot/packages/$p/package.json
done
test -f ${production-deps}/libexec/$sourceRoot/packages/cardano-services/node_modules && cp -r ${production-deps}/libexec/$sourceRoot/packages/cardano-services/node_modules $out/libexec/$sourceRoot/packages/cardano-services/node_modules
cp -r ${production-deps}/libexec/$sourceRoot/packages/cardano-services/config $out/libexec/$sourceRoot/packages/cardano-services/config
cd "$out/libexec/$sourceRoot"
Expand All @@ -103,6 +102,8 @@
chmod a+x $out/bin/cli
'';
meta.mainProgram = "cli";
passthru.nodejs = pkgs.nodejs;
passthru.production-deps = production-deps;
});

apps = flake-utils.lib.eachDefaultSystemMap (system: let
Expand Down
10 changes: 7 additions & 3 deletions packages/cardano-services/src/Program/services/ogmios.ts
Expand Up @@ -6,8 +6,8 @@ import { DnsResolver } from '../utils';
import { Logger } from 'ts-log';
import { MissingCardanoNodeOption } from '../errors';
import { OgmiosCardanoNode, OgmiosTxSubmitProvider, urlToConnectionConfig } from '@cardano-sdk/ogmios';
import { RunnableModule, isConnectionError } from '@cardano-sdk/util';
import { SubmitTxArgs } from '@cardano-sdk/core';
import { isConnectionError } from '@cardano-sdk/util';

const isCardanoNodeOperation = (prop: string | symbol): prop is 'eraSummaries' | 'systemStart' | 'stakeDistribution' =>
['eraSummaries', 'systemStart', 'stakeDistribution'].includes(prop as string);
Expand Down Expand Up @@ -58,7 +58,7 @@ export const ogmiosTxSubmitProviderWithDiscovery = async (
const { name, port } = await dnsResolver(serviceName!);
let ogmiosProvider = new OgmiosTxSubmitProvider({ host: name, port }, logger);

return new Proxy<OgmiosTxSubmitProvider>({} as OgmiosTxSubmitProvider, {
const txSubmitProviderProxy = new Proxy<OgmiosTxSubmitProvider>({} as OgmiosTxSubmitProvider, {
get(_, prop) {
if (prop === 'then') return;
if (prop === 'initialize') {
Expand Down Expand Up @@ -92,6 +92,8 @@ export const ogmiosTxSubmitProviderWithDiscovery = async (
return ogmiosProvider[prop as keyof OgmiosTxSubmitProvider];
}
});

return Object.setPrototypeOf(txSubmitProviderProxy, RunnableModule.prototype);
};

export const getOgmiosTxSubmitProvider = async (
Expand Down Expand Up @@ -127,7 +129,7 @@ export const ogmiosCardanoNodeWithDiscovery = async (
const { name, port } = await dnsResolver(serviceName!);
let ogmiosCardanoNode = new OgmiosCardanoNode({ host: name, port }, logger);

return new Proxy<OgmiosCardanoNode>({} as OgmiosCardanoNode, {
const cardanoNodeProxy = new Proxy<OgmiosCardanoNode>({} as OgmiosCardanoNode, {
get(_, prop) {
if (prop === 'then') return;
if (prop === 'initialize') {
Expand Down Expand Up @@ -161,6 +163,8 @@ export const ogmiosCardanoNodeWithDiscovery = async (
return ogmiosCardanoNode[prop as keyof OgmiosCardanoNode];
}
});

return Object.setPrototypeOf(cardanoNodeProxy, RunnableModule.prototype);
};

export const getOgmiosCardanoNode = async (
Expand Down
28 changes: 26 additions & 2 deletions packages/cardano-services/test/Program/services/ogmios.test.ts
@@ -1,7 +1,7 @@
/* eslint-disable sonarjs/no-identical-functions */
/* eslint-disable sonarjs/no-duplicate-string */
/* eslint-disable max-len */
import { CardanoNodeErrors, TxSubmitProvider } from '@cardano-sdk/core';
import { CardanoNodeErrors } from '@cardano-sdk/core';
import { Connection } from '@cardano-ogmios/client';
import { DbSyncEpochPollService, listenPromise, serverClosePromise } from '../../../src/util';
import { DbSyncNetworkInfoProvider, NetworkInfoHttpService } from '../../../src/NetworkInfo';
Expand Down Expand Up @@ -52,7 +52,7 @@ describe('Service dependency abstractions', () => {
let apiUrlBase: string;
let ogmiosServer: http.Server;
let ogmiosConnection: Connection;
let txSubmitProvider: TxSubmitProvider;
let txSubmitProvider: OgmiosTxSubmitProvider;
let ogmiosCardanoNode: OgmiosCardanoNode;
let httpServer: HttpServer;
let port: number;
Expand Down Expand Up @@ -92,6 +92,10 @@ describe('Service dependency abstractions', () => {
await httpServer.shutdown();
});

it('txSubmitProvider state should be running when http server has started', () => {
expect(txSubmitProvider.state).toEqual('running');
});

it('txSubmitProvider should be instance of a Proxy ', () => {
expect(types.isProxy(txSubmitProvider)).toEqual(true);
});
Expand All @@ -103,6 +107,15 @@ describe('Service dependency abstractions', () => {
expect(res.status).toBe(200);
expect(res.data).toEqual(healthCheckResponseMock());
});

it('TxSubmitHttpService replies with status 200 OK when /submit endpoint is reached', async () => {
const res = await axios.post(
`${apiUrlBase}/submit`,
{ signedTransaction: bufferToHexString(Buffer.from(new Uint8Array())) },
{ headers: { 'Content-Type': APPLICATION_JSON } }
);
expect(res.status).toBe(200);
});
});

describe('NetworkInfoHttpService', () => {
Expand Down Expand Up @@ -142,6 +155,10 @@ describe('Service dependency abstractions', () => {
await httpServer.shutdown();
});

it('ogmiosCardanoNode state should be running when http server has started', () => {
expect(ogmiosCardanoNode.state).toEqual('running');
});

it('ogmiosCardanoNode should be instance of a Proxy ', () => {
expect(types.isProxy(ogmiosCardanoNode)).toEqual(true);
});
Expand All @@ -153,6 +170,13 @@ describe('Service dependency abstractions', () => {
expect(res.status).toBe(200);
expect(res.data).toEqual(healthCheckResponseMock());
});

it('NetworkInfoHttpService replies with status 200 OK when /stake endpoint is reached', async () => {
const res = await axios.post(`${apiUrlBase}/stake`, undefined, {
headers: { 'Content-Type': APPLICATION_JSON }
});
expect(res.status).toBe(200);
});
});
});
});
Expand Down
6 changes: 5 additions & 1 deletion packages/cardano-services/test/util.ts
Expand Up @@ -21,7 +21,11 @@ export const ogmiosServerReady = (connection: Ogmios.Connection): Promise<void>
export const createHealthyMockOgmiosServer = (submitTxHook?: () => void) =>
createMockOgmiosServer({
healthCheck: { response: { networkSynchronization: 0.999, success: true } },
stateQuery: { eraSummaries: { response: { success: true } }, systemStart: { response: { success: true } } },
stateQuery: {
eraSummaries: { response: { success: true } },
stakeDistribution: { response: { success: true } },
systemStart: { response: { success: true } }
},
submitTx: { response: { success: true } },
submitTxHook
});
Expand Down

0 comments on commit 8f0bfb8

Please sign in to comment.