Skip to content

Commit 99399a3

Browse files
committed
feat(connector-xdai): add ledger connector plugin for xdai #852
A 1 to 1 clone of the besu connector for now but I thought it best to keep it separate regardless because a lot of Besu specific changes are about to drop for the Besu connector as we add the differentiating features of it to the connector as well and once that happens the codebases will probably look quite different at that point. Fixes #852 Signed-off-by: Peter Somogyvari <[email protected]>
1 parent 9ca1f68 commit 99399a3

32 files changed

+12335
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# `@hyperledger/cactus-plugin-ledger-connector-xdai`
2+
3+
This plugin provides `Cactus` a way to interact with Xdai networks. Using this we can perform:
4+
* Deploy Smart-contracts through bytecode.
5+
* Build and sign transactions using different keystores.
6+
* Invoke smart-contract functions that we have deployed on the network.
7+
## Summary
8+
9+
- [Getting Started](#getting-started)
10+
- [Architecture](#architecture)
11+
- [Usage](#usage)
12+
- [Prometheus Exporter](#prometheus-exporter)
13+
- [Runing the tests](#running-the-tests)
14+
- [Built With](#built-with)
15+
- [Contributing](#contributing)
16+
- [License](#license)
17+
- [Acknowledgments](#acknowledgments)
18+
19+
## Getting Started
20+
21+
Clone the git repository on your local machine. Follow these instructions that will get you a copy of the project up and running on
22+
your local machine for development and testing purposes.
23+
24+
### Prerequisites
25+
26+
In the root of the project to install the dependencies execute the command:
27+
```sh
28+
npm run configure
29+
```
30+
31+
### Compiling
32+
33+
In the project root folder, run this command to compile the plugin and create the dist directory:
34+
```sh
35+
npm run tsc
36+
```
37+
38+
### Architecture
39+
The sequence diagrams for various endpoints are mentioned below
40+
41+
#### run-transaction-endpoint
42+
![run-transaction-endpoint sequence diagram](docs/architecture/images/run-transaction-endpoint.png)
43+
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.
44+
![run-transaction-endpoint transact() method](docs/architecture/images/run-transaction-endpoint-transact.png)
45+
The above diagram shows the sequence diagraom of transact() method of the PluginLedgerConnectorXdai class. The caller to this function, which in reference to the above sequence diagram is API server, sends RunTransactionV1Request object as an argument to the transact() method. Based on the type of Web3SigningCredentialType, corresponsing responses are sent back to the caller.
46+
![run-transaction-endpoint transactCactusKeychainRef() method](docs/architecture/images/run-transaction-endpoint-transact-cactuskeychainref.png)
47+
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 transactPrivateKey() which calls the signTransaction() method of web3 library.
48+
![runtransaction-endpoint transactPrivateKey() method](docs/architecture/images/run-transaction-endpoint-transact-privatekey.png)
49+
The above diagram shows transactPrivateKey() method being called by the transact() method of the PluginLedgerConnector class when the Web3SigningCredentialType is PRIVATEKEYHEX. This method then calls the signTransaction() method of the web3 library.
50+
![run-transaction-endpoint transactSigned() method](docs/architecture/images/run-transaction-endpoint-transact-signed.png)
51+
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 sendSignedTransaction() of the web3 library and then calls pollForTxReceipt() method.
52+
![run-transaction-endpoint pollForTxReceipt() method](docs/architecture/images/run-transaction-endpoint-transact-pollfortxreceipt.png)
53+
The above diagram shows pollForTxReceipt() method which is called by the transactSigned() method as described in the previous sequence diagram. This method waits for the block confirmation in a loop and then sends the corresponding response back to the caller.
54+
55+
### Usage
56+
57+
To use this import public-api and create new **PluginFactoryLedgerConnector**. Then use it to create a connector.
58+
```typescript
59+
const factory = new PluginFactoryLedgerConnector({
60+
pluginImportType: PluginImportType.LOCAL,
61+
});
62+
const connector: PluginLedgerConnectorXdai = await factory.create({
63+
rpcApiHttpHost,
64+
instanceId: uuidv4(),
65+
pluginRegistry: new PluginRegistry(),
66+
});
67+
```
68+
You can make calls through the connector to the plugin API:
69+
70+
```typescript
71+
async invokeContract(req: InvokeContractV1Request):Promise<InvokeContractV1Response>;
72+
async transactSigned(rawTransaction: string): Promise<RunTransactionV1Response>;
73+
async transactPrivateKey(req: RunTransactionV1Request): Promise<RunTransactionV1Response>;
74+
async transactCactusKeychainRef(req: RunTransactionV1Request):Promise<RunTransactionV1Response>;
75+
async deployContract(req: DeployContractV1Request):Promise<RunTransactionV1Response>;
76+
async signTransaction(req: SignTransactionRequest):Promise<Optional<SignTransactionResponse>>;
77+
```
78+
79+
Call example to deploy a contract:
80+
```typescript
81+
const deployOut = await connector.deployContract({
82+
web3SigningCredential: {
83+
ethAccount: firstHighNetWorthAccount,
84+
secret: xdaiKeyPair.privateKey,
85+
type: Web3SigningCredentialType.PRIVATEKEYHEX,
86+
},
87+
bytecode: SmartContractJson.bytecode,
88+
gas: 1000000,
89+
});
90+
```
91+
The field "type" can have the following values:
92+
```typescript
93+
enum Web3SigningCredentialType {
94+
CACTUSKEYCHAINREF = 'CACTUS_KEYCHAIN_REF',
95+
GETHKEYCHAINPASSWORD = 'GETH_KEYCHAIN_PASSWORD',
96+
PRIVATEKEYHEX = 'PRIVATE_KEY_HEX',
97+
NONE = 'NONE'
98+
}
99+
```
100+
> Extensive documentation and examples in the [readthedocs](https://readthedocs.org/projects/hyperledger-cactus/) (WIP)
101+
102+
## Prometheus Exporter
103+
104+
This class creates a prometheus exporter, which scrapes the transactions (total transaction count) for the use cases incorporating the use of Xdai connector plugin.
105+
106+
### Prometheus Exporter Usage
107+
The prometheus exporter object is initialized in the `PluginLedgerConnectorXdai` class constructor itself, so instantiating the object of the `PluginLedgerConnectorXdai` class, gives access to the exporter object.
108+
You can also initialize the prometheus exporter object seperately and then pass it to the `IPluginLedgerConnectorXdaiOptions` interface for `PluginLedgerConnectoXdai` constructor.
109+
110+
`getPrometheusExporterMetricsV1` function returns the prometheus exporter metrics, currently displaying the total transaction count, which currently increments everytime the `transact()` method of the `PluginLedgerConnectorXdai` class is called.
111+
112+
### Prometheus Integration
113+
To use Prometheus with this exporter make sure to install [Prometheus main component](https://prometheus.io/download/).
114+
Once Prometheus is setup, the corresponding scrape_config needs to be added to the prometheus.yml
115+
116+
```(yaml)
117+
- job_name: 'xdai_ledger_connector_exporter'
118+
metrics_path: api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-xdai/get-prometheus-exporter-metrics
119+
scrape_interval: 5s
120+
static_configs:
121+
- targets: ['{host}:{port}']
122+
```
123+
124+
Here the `host:port` is where the prometheus exporter metrics are exposed. The test cases (For example, packages/cactus-plugin-ledger-connector-xdai/src/test/typescript/integration/plugin-ledger-connector-xdai/deploy-contract/deploy-contract-from-json.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)
125+
`Metrics URL: http://0.0.0.0:42379/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-xdai/get-prometheus-exporter-metrics`
126+
127+
Once edited, you can start the prometheus service by referencing the above edited prometheus.yml file.
128+
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_xdai_total_tx_count** and click **execute**
129+
130+
### Helper code
131+
132+
###### response.type.ts
133+
This file contains the various responses of the metrics.
134+
135+
###### data-fetcher.ts
136+
This file contains functions encasing the logic to process the data points
137+
138+
###### metrics.ts
139+
This file lists all the prometheus metrics and what they are used for.
140+
141+
## Running the tests
142+
143+
To check that all has been installed correctly and that the pugin has no errors run the tests:
144+
145+
* Run this command at the project's root:
146+
```sh
147+
npm run test:plugin-ledger-connector-xdai
148+
```
149+
150+
## Contributing
151+
152+
We welcome contributions to Hyperledger Cactus in many forms, and there’s always plenty to do!
153+
154+
Please review [CONTIRBUTING.md](../../CONTRIBUTING.md) to get started.
155+
156+
## License
157+
158+
This distribution is published under the Apache License Version 2.0 found in the [LICENSE](../../LICENSE) file.
159+
160+
## Acknowledgments

0 commit comments

Comments
 (0)