Skip to content

Commit

Permalink
feat(polkadot): update new changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AnmolBansalDEV committed Jan 13, 2024
1 parent 87ec994 commit 6410b7e
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 48 deletions.
7 changes: 5 additions & 2 deletions packages/cactus-plugin-ledger-connector-polkadot/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
FROM ghcr.io/hyperledger/cactus-cmd-api-server:2024-01-02-1fb2551
RUN npm install -g [email protected]

RUN npm install -g yarn \
&& yarn set version 3.6.3 \
&& yarn config set nodeLinker node-modules

ENV NODE_ENV=production
ARG NPM_PKG_VERSION=latest

RUN yarn add @hyperledger/cactus-plugin-ledger-connector-polkadot@${NPM_PKG_VERSION} --production --ignore-engines
RUN yarn add @hyperledger/cactus-plugin-ledger-connector-polkadot@${NPM_PKG_VERSION}
35 changes: 32 additions & 3 deletions packages/cactus-plugin-ledger-connector-polkadot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,48 @@ DOCKER_BUILDKIT=1 docker build -f ./packages/cactus-plugin-ledger-connector-polk

Build with a specific version of the npm package:
```sh
DOCKER_BUILDKIT=1 docker build --build-arg NPM_PKG_VERSION=0.4.1 -f ./packages/cactus-plugin-ledger-connector-polkadot/Dockerfile . -t cplcb
DOCKER_BUILDKIT=1 docker build --build-arg NPM_PKG_VERSION=2.0.0-lfx-connector.163 -f ./packages/cactus-plugin-ledger-connector-polkadot/Dockerfile . -t cplcb
```

### 3.2. Running the container

Launch container:
Launch container with plugin configuration as an **environment variable**:

```sh
docker run \
--rm \
--publish 9944:9944 \
--publish 3000:3000 \
--publish 4000:4000 \
--env PLUGINS='[{"packageName": "@hyperledger/cactus-plugin-ledger-connector-polkadot", "type": "org.hyperledger.cactus.plugin_import_type.LOCAL", "action": "org.hyperledger.cactus.plugin_import_action.INSTALL", "options": {"wsProviderUrl":"ws://127.0.0.1:9944", "instanceId": "some-unique-polkadot-connector-instance-id"}}]' \
cplcb
```

Launch container with plugin configuration as a **CLI argument**:
```sh
docker run \
--rm \
--publish 3000:3000 \
--publish 4000:4000 \
cplcb \
./node_modules/.bin/cactusapi \
--plugins='[{"packageName": "@hyperledger/cactus-plugin-ledger-connector-polkadot", "type": "org.hyperledger.cactus.plugin_import_type.LOCAL", "action": "org.hyperledger.cactus.plugin_import_action.INSTALL", "options": {"wsProviderUrl":"ws://127.0.0.1:9944", "instanceId": "some-unique-polkadot-connector-instance-id"}}]'
```

Launch container with **configuration file** mounted from host machine:
```sh

echo '[{"packageName": "@hyperledger/cactus-plugin-ledger-connector-polkadot", "type": "org.hyperledger.cactus.plugin_import_type.LOCAL", "action": "org.hyperledger.cactus.plugin_import_action.INSTALL", "options": {"wsProviderUrl":"ws://127.0.0.1:9944", "instanceId": "some-unique-polkadot-connector-instance-id"}}]' > cactus.json

docker run \
--rm \
--publish 3000:3000 \
--publish 4000:4000 \
--mount type=bind,source="$(pwd)"/cactus.json,target=/cactus.json \
cplcb \
./node_modules/.bin/cactusapi \
--config-file=/cactus.json
```

## 4. Prometheus Exporter

This class creates a Prometheus exporter, which scraps the transactions (total transaction count) for the use cases incorporating the use of Fabric connector plugin.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
PluginLedgerConnectorPolkadot,
} from "./plugin-ledger-connector-polkadot";

export class PluginFactoryLedgerConnectorPolkadot extends PluginFactory<
export class PluginFactoryLedgerConnector extends PluginFactory<
PluginLedgerConnectorPolkadot,
IPluginLedgerConnectorPolkadotOptions,
IPluginFactoryOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,6 @@ export class PluginLedgerConnectorPolkadot
}
}

public async createAPI(): Promise<void> {
try {
this.api = await ApiPromise.create({ provider: this.wsProvider });
} catch (err) {
const errorMessage = `Could not create API. InnerException: + ${err}`;
throw createHttpError[500](errorMessage);
}
}

public async getOrCreateWebServices(): Promise<IWebServiceEndpoint[]> {
if (Array.isArray(this.endpoints)) {
return this.endpoints;
Expand Down Expand Up @@ -282,8 +273,13 @@ export class PluginLedgerConnectorPolkadot
return Optional.empty();
}

public async onPluginInit(): Promise<unknown> {
return;
public async onPluginInit(): Promise<void> {
try {
this.api = await ApiPromise.create({ provider: this.wsProvider });
} catch (err) {
const errorMessage = `Could not create API. InnerException: + ${err}`;
throw createHttpError[500](errorMessage);
}
}

public async getConsensusAlgorithmFamily(): Promise<ConsensusAlgorithmFamily> {
Expand Down Expand Up @@ -326,9 +322,9 @@ export class PluginLedgerConnectorPolkadot
};
return response;
} catch (err) {
const errorMessage =
const errorMessage =
`${fnTag} Obtaining raw transaction has failed. ` +
`InnerException: ${err}`;
`InnerException: ${err}`;
throw createHttpError[500](errorMessage);
}
}
Expand Down Expand Up @@ -358,9 +354,9 @@ export class PluginLedgerConnectorPolkadot
};
return response;
} catch (err) {
const errorMessage =
const errorMessage =
`${fnTag} signing raw transaction has failed. ` +
`InnerException: ${err}`;
`InnerException: ${err}`;
throw createHttpError[500](errorMessage);
}
}
Expand All @@ -383,17 +379,17 @@ export class PluginLedgerConnectorPolkadot
} else {
const errorMessage =
`${fnTag} Expected pre-signed raw transaction ` +
` since signing credential is specified as` +
`Web3SigningCredentialType.NONE`;
` since signing credential is specified as` +
`Web3SigningCredentialType.NONE`;
throw createHttpError[400](errorMessage);
}
}
default: {
const errorMessage =
`${fnTag} Unrecognized Web3SigningCredentialType: ` +
`Supported ones are: ` +
`${Object.values(Web3SigningCredentialType).join(";")}`;
throw createHttpError[400](errorMessage);
`${fnTag} Unrecognized Web3SigningCredentialType: ` +
`Supported ones are: ` +
`${Object.values(Web3SigningCredentialType).join(";")}`;
throw createHttpError[400](errorMessage);
}
}
}
Expand Down Expand Up @@ -435,7 +431,7 @@ export class PluginLedgerConnectorPolkadot
const { transactionConfig, web3SigningCredential } = req;
const { mnemonic } =
web3SigningCredential as Web3SigningCredentialMnemonicString;
if(!mnemonic){
if (!mnemonic) {
throw createHttpError[400](
`cannot perform transaction without mnemonic string`,
);
Expand Down Expand Up @@ -520,9 +516,7 @@ export class PluginLedgerConnectorPolkadot
const deserializedTransaction = this.api.tx(signedTx);
const signature = deserializedTransaction.signature.toHex();
if (!signature) {
throw createHttpError[400](
`${fnTag} Transaction is not signed.`,
);
throw createHttpError[400](`${fnTag} Transaction is not signed.`);
}

if (!isHex(signature)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ export {
PluginLedgerConnectorPolkadot,
IPluginLedgerConnectorPolkadotOptions,
} from "./plugin-ledger-connector-polkadot";
export { PluginFactoryLedgerConnectorPolkadot } from "./plugin-factory-ledger-connector-polkadot";
export { PluginFactoryLedgerConnector } from "./plugin-factory-ledger-connector-polkadot";

import { IPluginFactoryOptions } from "@hyperledger/cactus-core-api";
import { PluginFactoryLedgerConnectorPolkadot } from "./plugin-factory-ledger-connector-polkadot";
import { PluginFactoryLedgerConnector } from "./plugin-factory-ledger-connector-polkadot";

export async function createPluginFactory(
pluginFactoryOptions: IPluginFactoryOptions,
): Promise<PluginFactoryLedgerConnectorPolkadot> {
return new PluginFactoryLedgerConnectorPolkadot(pluginFactoryOptions);
): Promise<PluginFactoryLedgerConnector> {
return new PluginFactoryLedgerConnector(pluginFactoryOptions);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import {
IPluginLedgerConnectorPolkadotOptions,
DefaultApi as PolkadotApi,
Web3SigningCredentialType,
PluginFactoryLedgerConnector,
} from "../../../main/typescript/public-api";
import { PluginKeychainMemory } from "@hyperledger/cactus-plugin-keychain-memory";
import { PluginRegistry } from "@hyperledger/cactus-core";
import { AddressInfo } from "net";
import { Configuration } from "@hyperledger/cactus-core-api";
import { Configuration, PluginImportType } from "@hyperledger/cactus-core-api";
import "jest-extended";

const testCase = "deploy contract through all available methods";
Expand Down Expand Up @@ -83,8 +84,12 @@ describe(testCase, () => {
wsProviderUrl: DEFAULT_WSPROVIDER,
instanceId: instanceId,
};
plugin = new PluginLedgerConnectorPolkadot(connectorOptions);
await plugin.createAPI();
const factory = new PluginFactoryLedgerConnector({
pluginImportType: PluginImportType.Local,
});

plugin = await factory.create(connectorOptions);
await plugin.onPluginInit();

const listenOptions: IListenOptions = {
hostname: "127.0.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import {
DefaultApi as PolkadotApi,
Web3SigningCredentialType,
PolkadotContractInvocationType,
PluginFactoryLedgerConnector,
} from "../../../main/typescript/public-api";
import { PluginKeychainMemory } from "@hyperledger/cactus-plugin-keychain-memory";
import { PluginRegistry } from "@hyperledger/cactus-core";
import { AddressInfo } from "net";
import { Configuration } from "@hyperledger/cactus-core-api";
import { Configuration, PluginImportType } from "@hyperledger/cactus-core-api";
import "jest-extended";

const testCase = "invoke contract with all invocation types";
Expand Down Expand Up @@ -90,8 +91,13 @@ describe(testCase, () => {
wsProviderUrl: DEFAULT_WSPROVIDER,
instanceId: instanceId,
};
plugin = new PluginLedgerConnectorPolkadot(connectorOptions);
await plugin.createAPI();

const factory = new PluginFactoryLedgerConnector({
pluginImportType: PluginImportType.Local,
});

plugin = await factory.create(connectorOptions);
await plugin.onPluginInit();

const listenOptions: IListenOptions = {
hostname: "127.0.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import {
IPluginLedgerConnectorPolkadotOptions,
DefaultApi as PolkadotApi,
Web3SigningCredentialType,
PluginFactoryLedgerConnector,
} from "../../../main/typescript/public-api";
import { PluginKeychainMemory } from "@hyperledger/cactus-plugin-keychain-memory";
import { PluginRegistry } from "@hyperledger/cactus-core";
import { AddressInfo } from "net";
import { Configuration } from "@hyperledger/cactus-core-api";
import { Configuration, PluginImportType } from "@hyperledger/cactus-core-api";
import { Keyring } from "@polkadot/api";
import { K_CACTUS_POLKADOT_TOTAL_TX_COUNT } from "../../../main/typescript/prometheus-exporter/metrics";
import "jest-extended";
Expand Down Expand Up @@ -82,8 +83,13 @@ describe(testCase, () => {
wsProviderUrl: DEFAULT_WSPROVIDER,
instanceId: instanceId,
};
plugin = new PluginLedgerConnectorPolkadot(connectorOptions);
await plugin.createAPI();

const factory = new PluginFactoryLedgerConnector({
pluginImportType: PluginImportType.Local,
});

plugin = await factory.create(connectorOptions);
await plugin.onPluginInit();

const listenOptions: IListenOptions = {
hostname: "127.0.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { pruneDockerAllIfGithubAction } from "@hyperledger/cactus-test-tooling";
import {
PluginLedgerConnectorPolkadot,
IPluginLedgerConnectorPolkadotOptions,
PluginFactoryLedgerConnector,
} from "../../../main/typescript";
import { PluginRegistry } from "@hyperledger/cactus-core";
import "jest-extended";
import { PluginImportType } from "@hyperledger/cactus-core-api";

const testCase = "Instantiate plugin";
const logLevel: LogLevelDesc = "TRACE";
Expand All @@ -18,6 +20,7 @@ const prometheus: PrometheusExporter = new PrometheusExporter({
});

describe(testCase, () => {
let plugin: PluginLedgerConnectorPolkadot;
const connectorOptions: IPluginLedgerConnectorPolkadotOptions = {
logLevel: logLevel,
prometheusExporter: prometheus,
Expand All @@ -31,7 +34,10 @@ describe(testCase, () => {
emitContainerLogs: true,
};
const ledger = new SubstrateTestLedger(ledgerOptions);
const plugin = new PluginLedgerConnectorPolkadot(connectorOptions);
const factory = new PluginFactoryLedgerConnector({
pluginImportType: PluginImportType.Local,
});

beforeAll(async () => {
const pruning = pruneDockerAllIfGithubAction({ logLevel });
await expect(pruning).toResolve();
Expand All @@ -44,7 +50,8 @@ describe(testCase, () => {
test(testCase, async () => {
await ledger.start();
expect(ledger).toBeTruthy();
await plugin.createAPI();
plugin = await factory.create(connectorOptions);
await plugin.onPluginInit();
await plugin.getOrCreateWebServices();
})
})
});
});

0 comments on commit 6410b7e

Please sign in to comment.