Skip to content

Commit

Permalink
feat(polkadot): creation of readme and architecture reference diagrams
Browse files Browse the repository at this point in the history
Primary Changes
---------------
1. Added README.md for the connector
2. Added Architecture diagrams for the plugin

Secondary Changes
-----------------
1. Added the docker image for the polkadot connector

Signed-off-by: Anmol Bansal <[email protected]>
  • Loading branch information
AnmolBansalDEV committed Feb 3, 2024
1 parent e3ada7e commit 17fb399
Show file tree
Hide file tree
Showing 12 changed files with 389 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/cactus-plugin-ledger-connector-polkadot/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM ghcr.io/hyperledger/cactus-cmd-api-server:2024-01-02-1fb2551

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}
240 changes: 240 additions & 0 deletions packages/cactus-plugin-ledger-connector-polkadot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
# `@hyperledger/cactus-plugin-ledger-connector-polkadot` <!-- omit in toc -->

## Table of Contents <!-- omit in toc -->

- [1. Usage](#1-usage)
- [1.1. Installation](#11-installation)
- [1.2. Using as a Library](#12-using-as-a-library)
- [1.3. Using Via The API Client](#13-using-via-the-api-client)
- [2. Architecture](#2-architecture)
- [2.1. run-transaction-endpoint](#21-run-transaction-endpoint)
- [3. Containerization](#3-containerization)
- [3.1. Building/running the container image locally](#31-buildingrunning-the-container-image-locally)
- [3.2. Running the container](#32-running-the-container)
- [4. Prometheus Exporter](#4-prometheus-exporter)
- [4.1. Usage Prometheus](#41-usage-prometheus)
- [4.2. Prometheus Integration](#42-prometheus-integration)
- [4.3. Helper code](#43-helper-code)
- [4.3.1. response.type.ts](#431-responsetypets)
- [4.3.2. data-fetcher.ts](#432-data-fetcherts)
- [4.3.3. metrics.ts](#433-metricsts)
- [5. Contributing](#5-contributing)
- [6. License](#6-license)
- [7. Acknowledgments](#7-acknowledgments)


## 1. Usage

This plugin provides a way to interact with Substrate networks.
Using this one can perform:
* Deploy smart contracts (ink! contract).
* Execute transactions on the ledger.
* Invoke ink! contract functions.

The above functionality can either be accessed by importing the plugin directly as a library (embedding) or by hosting it as a REST API through the [Cactus API server](https://www.npmjs.com/package/@hyperledger/cactus-cmd-api-server)

We also publish the [Cactus API server as a container image](https://github.com/hyperledger/cactus/pkgs/container/cactus-cmd-api-server) to the GitHub Container Registry that you can run easily with a one liner.
The API server is also embeddable in your own NodeJS project if you choose to do so.

### 1.1. Installation

**npm**

```sh
npm install @hyperledger/cactus-plugin-ledger-connector-polkadot
```

**yarn**

```sh
yarn add @hyperledger/cactus-plugin-ledger-connector-polkadot
```

### 1.2. Using as a Library

```typescript
import {
PluginLedgerConnectorPolkadot,
} from "@hyperledger/cactus-plugin-ledger-connector-polkadot";

const plugin = new PluginLedgerConnectorPolkadot({
// See test cases for exact details on what parameters are needed
});

const req: RunTransactionRequest = {
// See tests for specific examples on request properties
};

try {
const res = await plugin.transact(req);
} catch (ex: Error) {
// Make sure to handle errors gracefully (which is dependent on your use-case)
console.error(ex);
throw ex;
}
```

### 1.3. Using Via The API Client

**Prerequisites**
- A running Substrate ledger (network)
- You have a running Cactus API server on `$HOST:$PORT` with the Polkadot connector plugin installed on it (and the latter configured to have access to the Substrate ledger from point 1)

```typescript
import {
PluginLedgerConnectorPolkadot,
DefaultApi as PolkadotApi,
} from "@hyperledger/cactus-plugin-ledger-connector-polkadot";

// Step zero is to deploy your Substrate ledger and the Cactus API server

const apiHost = `http://${address}:${port}`;

const apiConfig = new Configuration({ basePath: apiHost });

const apiClient = new PolkadotApi(apiConfig);

const req: RunTransactionRequest = {
// See tests for specific examples on request properties
};

try {
const res = await apiClient.runTransaction(req);
} catch (ex: Error) {
// Make sure to handle errors gracefully (which is dependent on your use-case)
console.error(ex);
throw ex;
}
```
## 2. Architecture
The sequence diagrams for various endpoints are mentioned below

### 2.1. run-transaction-endpoint

![run-transaction-endpoint sequence diagram](docs/architecture/images/run-transaction-endpoint.png)
The above diagram shows the sequence diagram of run-transaction-endpoint. User A (One of the many Users) interacts with the API Client which in turn, calls the API server. API server then executes transact() method which is explained in detailed in the subsequent diagrams.
![run-transaction-endpoint transact() method](docs/architecture/images/run-transaction-endpoint-transact.png)
The above diagram shows the sequence diagram of transact() method of the PluginLedgerConnectorPolkadot class. The caller to this function, which in reference to the above sequence diagram is API server, sends RunTransactionRequest object as an argument to the transact() method. Based on the type of Web3SigningCredentialType, corresponding responses are sent back to the caller.
![run-transaction-endpoint transactCactusKeychainRef() method](docs/architecture/images/run-transaction-endpoint-transact-cactuskeychainref.png)
The above diagram shows transactCactusKeychainReference() method being called by the transact() method of the PluginLedgerConnector class when the Web3SigningCredentialType is CACTUSKEYCHAINREF. This method inturn calls transactMnemonicString() which calls the signAndSend() method of the Polkadot library.
![runtransaction-endpoint transactMnemonicString() method](docs/architecture/images/run-transaction-endpoint-transact-mnemonicstring.png)
The above diagram shows transactMnemonicString() method being called by the transact() method of the PluginLedgerConnector class when the Web3SigningCredentialType is MNEMONICSTRING. This method then calls the signAndSend() method of the Polkadot library.
![run-transaction-endpoint transactSigned() method](docs/architecture/images/run-transaction-endpoint-transact-signed.png)
The above diagram shows transactSigned() method being called by the transact() method of the PluginLedgerConnector class when the Web3SigningCredentialType is NONE. This method calls the api.rpc.author.submitAndWatchExtrinsic() of the Polkadot library.


## 3. Containerization
### 3.1. Building/running the container image locally

In the Cactus project root say:

```sh
DOCKER_BUILDKIT=1 docker build -f ./packages/cactus-plugin-ledger-connector-polkadot/Dockerfile . -t cplcb
```

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

### 3.2. Running the container

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

```sh
docker run \
--rm \
--publish 3000:3000 \
--publish 4000:4000 \
--env AUTHORIZATION_PROTOCOL='NONE' \
--env AUTHORIZATION_CONFIG_JSON='{}' \
--env GRPC_TLS_ENABLED=false \
cplcb \
node_modules/@hyperledger/cactus-cmd-api-server/dist/lib/main/typescript/cmd/cactus-api.js \
--env PLUGINS='[{"packageName": "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 plugin configuration as a **CLI argument**:
```sh
docker run \
--rm \
--publish 3000:3000 \
--publish 4000:4000 \
--publish 5000:5000 \
--env AUTHORIZATION_PROTOCOL='NONE' \
--env AUTHORIZATION_CONFIG_JSON='{}' \
--env GRPC_TLS_ENABLED=false \
cplcb \
node_modules/@hyperledger/cactus-cmd-api-server/dist/lib/main/typescript/cmd/cactus-api.js \
--plugins='[{"packageName": "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": "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 \
--env AUTHORIZATION_PROTOCOL='NONE' \
--env AUTHORIZATION_CONFIG_JSON='{}' \
--env GRPC_TLS_ENABLED=false \
--mount type=bind,source="$(pwd)"/cactus.json,target=/cactus.json \
cplcb \
node_modules/@hyperledger/cactus-cmd-api-server/dist/lib/main/typescript/cmd/cactus-api.js \
--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.


### 4.1. Usage Prometheus
The Prometheus exporter object is initialized in the `PluginLedgerConnectorPolkadot` class constructor itself, so instantiating the object of the `PluginLedgerConnectorPolkadot` class, gives access to the exporter object.
You can also initialize the Prometheus exporter object separately and then pass it to the `IPluginLedgerConnectorPolkadotOptions` interface for `PluginLedgerConnectorPolkadot` constructor.

`getPrometheusExporterMetricsEndpoint` function returns the Prometheus exporter metrics, currently displaying the total transaction count, which currently increments every time the `transact()` method of the `PluginLedgerConnectoPolkadot` class is called.

### 4.2. Prometheus Integration
To use Prometheus with this exporter make sure to install [Prometheus main component](https://prometheus.io/download/).
Once Prometheus is setup, the corresponding scrape_config needs to be added to the prometheus.yml

```(yaml)
- job_name: 'polkadot_ledger_connector_exporter'
metrics_path: api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-polkadot/get-prometheus-exporter-metrics
scrape_interval: 5s
static_configs:
- targets: ['{host}:{port}']
```

Here the `host:port` is where the Prometheus exporter metrics are exposed. The test cases (For example, packages/cactus-plugin-ledger-connector-polkadot/src/test/typescript/integration/run-transaction.test.ts) exposes it over `0.0.0.0` and a random port(). The random port can be found in the running logs of the test case and looks like (42379 in the below mentioned URL)
`Metrics URL: http://0.0.0.0:42379/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-polkadot/get-prometheus-exporter-metrics`

Once edited, you can start the Prometheus service by referencing the above edited prometheus.yml file.
On the Prometheus graphical interface (defaulted to http://localhost:9090), choose **Graph** from the menu bar, then select the **Console** tab. From the **Insert metric at cursor** drop down, select **cactus_Polkadot_total_tx_count** and click **execute**

### 4.3. Helper code

#### 4.3.1. response.type.ts
This file contains the various responses of the metrics.

#### 4.3.2. data-fetcher.ts
This file contains functions encasing the logic to process the data points

#### 4.3.3. metrics.ts
This file lists all the Prometheus metrics and what they are used for.

## 5. Contributing

We welcome contributions to Hyperledger Cactus in many forms, and there’s always plenty to do!

Please review [CONTIRBUTING.md](../../CONTRIBUTING.md) to get started.

## 6. License

This distribution is published under the Apache License Version 2.0 found in the [LICENSE](../../LICENSE) file.

## 7. Acknowledgments
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@startuml
title Hyperledger Cactus\nSequence Diagram\nRun Transaction Endpoint\ntransactCactusKeychainRef() method

skinparam sequenceArrowThickness 2
skinparam roundcorner 20
skinparam maxmessagesize 120
skinparam sequenceParticipant underline

actor "Caller" as caller
participant "transactCactusKeychainRef()" as tckr
participant "transactMnemonicString()" as tms
participant ".signAndSend()" as sas

caller-> tckr: req
activate caller
group #LightBlue if web3SigningCredential == CACTUSKEYCHAINREF
activate tckr
tckr -> tms: [transactionConfig, web3SigningCredential]
activate tms
tms -> sas: [transactionConfig, mnemonicString]
activate sas
sas --> tms: return [success, blockhash, transactionHash]
deactivate sas
tms --> tckr: return [success, blockhash, transactionHash]
tckr --> caller : return {success, blockhash, transactionHash} as resBody
deactivate tckr
deactivate tms
end
@enduml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@startuml
title Hyperledger Cactus\nSequence Diagram\nRun Transaction Endpoint\ntransactMnemonicString() method

skinparam sequenceArrowThickness 2
skinparam roundcorner 20
skinparam maxmessagesize 120
skinparam sequenceParticipant underline

actor "Caller" as caller
participant "transactMnemonicString()" as tms
participant ".signAndSend()" as sas

caller -> tms: req
activate caller
alt #LightGreen web3SigningCredential == MNEMONICSTRING
activate tms
tms -> sas: [transactionConfig, mnemonicString]
activate sas
sas --> tms: return [success, txHash, blockHash]
deactivate sas
tms --> caller: return [success, txHash, blockHash] as resBody
end
@enduml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@startuml
title Hyperledger Cactus\nSequence Diagram\nRun Transaction Endpoint\ntransactSigned() method

skinparam sequenceArrowThickness 2
skinparam roundcorner 20
skinparam maxmessagesize 120
skinparam sequenceParticipant underline

actor "Caller" as caller
participant "transactSigned()" as ts
participant "api.rpc.author.submitAndWatchExtrinsic()" as aras

caller -> ts: req
activate caller
group #e6e632 if web3SigningCredential == NONE
activate ts
ts -> aras: deserializedTransaction
activate aras
aras --> ts: success, txHash, blockHash
deactivate aras
ts --> caller: returns [success, txHash, blockHash] as resBody
end
deactivate caller
@enduml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@startuml
title Hyperledger Cactus\nSequence Diagram\nRun Transaction Endpoint\ntransact() method

skinparam sequenceArrowThickness 2
skinparam roundcorner 20
skinparam maxmessagesize 120
skinparam sequenceParticipant underline

actor "Caller" as caller
participant "PluginLedgerConnectorPolkadot" as t << (C,#ADD1B2) class >>

autoactivate on

activate caller
caller -> t: transact(RunTransactionRequest)

alt #LightBlue web3SigningCredential == CACTUSKEYCHAINREF
t -> t: transactCactusKeychainRef(RunTransactionRequest)
return RunTransactionResponse
t --> caller: return RunTransactionResponse
else #LightGreen web3SigningCredential == MNEMONICSTRING
t -> t: transactMnemonicString(RunTransactionRequest)
return RunTransactionResponse
t --> caller: return RunTransactionResponse
else #e6e632 web3SigningCredential == NONE
group #LightGray if defined: req.transactionConfig.transferSubmittable
t -> t: transactSigned(RunTransactionRequest)
return RunTransactionResponse
t --> caller: return RunTransactionResponse
else #LightCoral
t --> caller: throw Error: Expected pre-signed raw transaction
end
else #LightCoral default
t --> caller: throw Error: Unrecognized Web3SigningCredentialType
end
@enduml
Loading

0 comments on commit 17fb399

Please sign in to comment.