ruzell-cactus-plugin-ledger-connector-besu
v1.0.13
Published
Allows Cactus nodes to connect to a Besu ledger.
Downloads
20
Maintainers
Readme
@hyperledger/cactus-plugin-ledger-connector-besu
This plugin provides Cactus
a way to interact with Besu networks. Using this we can perform:
- Deploy Smart-contracts through bytecode.
- Build and sign transactions using different keystores.
- Invoke smart-contract functions that we have deployed on the network.
Summary
- Getting Started
- Architecture
- Usage
- Prometheus Exporter
- Runing the tests
- Built With
- Contributing
- License
- Acknowledgments
Getting Started
Clone the git repository on your local machine. Follow these instructions that will get you a copy of the project up and running on your local machine for development and testing purposes.
Prerequisites
In the root of the project to install the dependencies execute the command:
npm run configure
Compiling
In the project root folder, run this command to compile the plugin and create the dist directory:
npm run tsc
Architecture
The sequence diagrams for various endpoints are mentioned below
run-transaction-endpoint
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.
The above diagram shows the sequence diagram of transact() method of the PluginLedgerConnectorBesu 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, corresponsing responses are sent back to the caller.
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.
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.
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.
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.
Usage
To use this import public-api and create new PluginFactoryLedgerConnector. Then use it to create a connector.
const factory = new PluginFactoryLedgerConnector({
pluginImportType: PluginImportType.LOCAL,
});
const connector: PluginLedgerConnectorBesu = await factory.create({
rpcApiHttpHost,
instanceId: uuidv4(),
pluginRegistry: new PluginRegistry(),
});
You can make calls through the connector to the plugin API:
async invokeContract(req: InvokeContractV1Request):Promise<InvokeContractV1Response>;
async transactSigned(rawTransaction: string): Promise<RunTransactionResponse>;
async transactPrivateKey(req: RunTransactionRequest): Promise<RunTransactionResponse>;
async transactCactusKeychainRef(req: RunTransactionRequest):Promise<RunTransactionResponse>;
async deployContract(req: DeployContractSolidityBytecodeV1Request):Promise<RunTransactionResponse>;
async signTransaction(req: SignTransactionRequest):Promise<Optional<SignTransactionResponse>>;
Call example to deploy a contract:
const deployOut = await connector.deployContract({
web3SigningCredential: {
ethAccount: firstHighNetWorthAccount,
secret: besuKeyPair.privateKey,
type: Web3SigningCredentialType.PrivateKeyHex,
},
bytecode: SmartContractJson.bytecode,
gas: 1000000,
});
The field "type" can have the following values:
enum Web3SigningCredentialType {
CACTUSKEYCHAINREF = 'CACTUS_KEYCHAIN_REF',
GETHKEYCHAINPASSWORD = 'GETH_KEYCHAIN_PASSWORD',
PRIVATEKEYHEX = 'PRIVATE_KEY_HEX',
NONE = 'NONE'
}
Transaction Privacy Feature
Private transactions using Besu are currently enabled.
The privateFor and privateFrom fields must be populated, more information about Besu Private Transactions here.
Call example to deploy a private contract:
const deployOut = await connector1.deployContract({
bytecode: SmartContract.bytecode,
contractAbi: SmartContract.abi,
contractName: SmartContract.contractName,
constructorArgs: [],
privateTransactionConfig: {
privateFrom: SendingTesseraPublicKey,
privateFor: [
Member1TesseraPrivateKey,
Member2TesseraPrivateKey,
],
},
web3SigningCredential: {
secret:SendingBesuPrivateKey,
type: Web3SigningCredentialType.PrivateKeyHex,
},
gas: 3000000
});
Extensive documentation and examples in the readthedocs (WIP)
Running the container
Launch container with plugin configuration as an environment variable:
docker run \
--rm \
--publish 3000:3000 \
--publish 4000:4000 \
--env AUTHORIZATION_PROTOCOL='NONE' \
--env AUTHORIZATION_CONFIG_JSON='{}' \
--env GRPC_TLS_ENABLED=false \
--env API_TLS_CERT_PEM=- \
--env API_TLS_CLIENT_CA_PEM=- \
--env API_TLS_KEY_PEM=- \
--env API_TLS_ENABLED=false \
--env API_MTLS_ENABLED=false \
--env API_HOST=0.0.0.0 \
--env LOG_LEVEL=INFO \
ghcr.io/hyperledger/cactus-cmd-api-server:2024-07-03t18-38-45-dev-65adc3255 \
node index.js \
--plugins='[{"packageName": "@hyperledger/cactus-plugin-ledger-connector-besu", "type": "org.hyperledger.cactus.plugin_import_type.LOCAL", "action": "org.hyperledger.cactus.plugin_import_action.INSTALL", "options": {"rpcApiHttpHost": "http://localhost:8545", "rpcApiWsHost":"ws://localhost:8546", "instanceId": "some-unique-besu-connector-instance-id"}}]'
Launch container with plugin configuration as a CLI argument:
docker run \
--rm \
--publish 3000:3000 \
--publish 4000:4000 \
--env AUTHORIZATION_PROTOCOL='NONE' \
--env AUTHORIZATION_CONFIG_JSON='{}' \
--env GRPC_TLS_ENABLED=false \
--env API_TLS_CERT_PEM=- \
--env API_TLS_CLIENT_CA_PEM=- \
--env API_TLS_KEY_PEM=- \
--env API_TLS_ENABLED=false \
--env API_MTLS_ENABLED=false \
--env API_HOST=0.0.0.0 \
--env LOG_LEVEL=INFO \
ghcr.io/hyperledger/cactus-cmd-api-server:2024-07-03t18-38-45-dev-65adc3255 \
node index.js \
--plugins='[{"packageName": "@hyperledger/cactus-plugin-ledger-connector-besu", "type": "org.hyperledger.cactus.plugin_import_type.LOCAL", "action": "org.hyperledger.cactus.plugin_import_action.INSTALL", "options": {"rpcApiHttpHost": "http://localhost:8545", "rpcApiWsHost":"ws://localhost:8546", "instanceId": "some-unique-besu-connector-instance-id"}}]'
Launch container with configuration file mounted from host machine:
echo '{"plugins": [{"packageName": "@hyperledger/cactus-plugin-ledger-connector-besu", "type": "org.hyperledger.cactus.plugin_import_type.LOCAL", "action": "org.hyperledger.cactus.plugin_import_action.INSTALL", "options": {"rpcApiHttpHost": "http://localhost:8545", "rpcApiWsHost":"ws://localhost:8546", "instanceId": "some-unique-besu-connector-instance-id"}}]}' > .cacti-config.json
docker run \
--rm \
--publish 3000:3000 \
--publish 4000:4000 \
--env AUTHORIZATION_PROTOCOL='NONE' \
--env AUTHORIZATION_CONFIG_JSON='{}' \
--env GRPC_TLS_ENABLED=false \
--env API_TLS_CERT_PEM=- \
--env API_TLS_CLIENT_CA_PEM=- \
--env API_TLS_KEY_PEM=- \
--env API_TLS_ENABLED=false \
--env API_MTLS_ENABLED=false \
--env API_HOST=0.0.0.0 \
--env LOG_LEVEL=INFO \
--mount type=bind,source="$(pwd)"/.cacti-config.json,target=/.cacti-config.json \
ghcr.io/hyperledger/cactus-cmd-api-server:2024-07-03t18-38-45-dev-65adc3255 \
node index.js \
--config-file=/.cacti-config.json
Testing API calls with the container
Don't have a Besu network on hand to test with? Test or develop against our Besu All-In-One container!
Terminal Window 1 (Ledger)
docker run \
--publish 0.0.0.0:8545:8545/tcp \
--publish 0.0.0.0:8546:8546/tcp \
--publish 0.0.0.0:8888:8888/tcp \
--publish 0.0.0.0:9001:9001/tcp \
--publish 0.0.0.0:9545:9545/tcp \
ghcr.io/hyperledger/cactus-besu-all-in-one:2024-07-04-8c030ae
Terminal Window 2 (Cacti API Server)
docker run \
--network=host \
--rm \
--publish 3000:3000 \
--publish 4000:4000 \
--env AUTHORIZATION_PROTOCOL='NONE' \
--env AUTHORIZATION_CONFIG_JSON='{}' \
--env GRPC_TLS_ENABLED=false \
--env API_TLS_CERT_PEM=- \
--env API_TLS_CLIENT_CA_PEM=- \
--env API_TLS_KEY_PEM=- \
--env API_TLS_ENABLED=false \
--env API_MTLS_ENABLED=false \
--env API_HOST=0.0.0.0 \
--env LOG_LEVEL=INFO \
ghcr.io/hyperledger/cactus-cmd-api-server:2024-07-03t18-38-45-dev-65adc3255 \
node index.js \
--plugins='[{"packageName": "@hyperledger/cactus-plugin-ledger-connector-besu", "type": "org.hyperledger.cactus.plugin_import_type.LOCAL", "action": "org.hyperledger.cactus.plugin_import_action.INSTALL", "options": {"rpcApiHttpHost": "http://127.0.0.1:8545", "rpcApiWsHost":"ws://127.0.0.1:8546", "instanceId": "some-unique-besu-connector-instance-id"}}]'
Terminal Window 3 (curl - replace eth accounts as needed)
curl --location --request POST 'http://127.0.0.1:4000/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/run-transaction' \
--header 'Content-Type: application/json' \
--data-raw '{
"web3SigningCredential": {
"ethAccount": "627306090abaB3A6e1400e9345bC60c78a8BEf57",
"secret": "c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3",
"type": "PRIVATE_KEY_HEX"
},
"consistencyStrategy": {
"blockConfirmations": 0,
"receiptType": "NODE_TX_POOL_ACK"
},
"transactionConfig": {
"from": "627306090abaB3A6e1400e9345bC60c78a8BEf57",
"to": "f17f52151EbEF6C7334FAD080c5704D77216b732",
"value": 1,
"gas": 10000000
}
}'
The above should produce a response that looks similar to this:
{
"success": true,
"data": {
"transactionReceipt": {
"blockHash": "0x7c97c038a5d3bd84613fe23ed442695276d5d2df97f4e7c4f10ca06765033ffd",
"blockNumber": 1218,
"contractAddress": null,
"cumulativeGasUsed": 21000,
"from": "0x627306090abab3a6e1400e9345bc60c78a8bef57",
"gasUsed": 21000,
"logs": [],
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"status": true,
"to": "0xf17f52151ebef6c7334fad080c5704d77216b732",
"transactionHash": "0xc7fcb46c735bdc696d500bfc70c72595a2b8c31813929e5c61d9a5aec3376d6f",
"transactionIndex": 0
}
}
}
Prometheus Exporter
This class creates a prometheus exporter, which scrapes the transactions (total transaction count) for the use cases incorporating the use of Besu connector plugin.
Prometheus Exporter Usage
The prometheus exporter object is initialized in the PluginLedgerConnectorBesu
class constructor itself, so instantiating the object of the PluginLedgerConnectorBesu
class, gives access to the exporter object.
You can also initialize the prometheus exporter object seperately and then pass it to the IPluginLedgerConnectorBesuOptions
interface for PluginLedgerConnectoBesu
constructor.
getPrometheusMetricsV1
function returns the prometheus exporter metrics, currently displaying the total transaction count, which currently increments everytime the transact()
method of the PluginLedgerConnectorBesu
class is called.
Prometheus Integration
To use Prometheus with this exporter make sure to install Prometheus main component. Once Prometheus is setup, the corresponding scrape_config needs to be added to the prometheus.yml
- job_name: 'besu_ledger_connector_exporter'
metrics_path: api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/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-besu/src/test/typescript/integration/plugin-ledger-connector-besu/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)
Metrics URL: http://0.0.0.0:42379/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/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_besu_total_tx_count and click execute
Helper code
response.type.ts
This file contains the various responses of the metrics.
data-fetcher.ts
This file contains functions encasing the logic to process the data points
metrics.ts
This file lists all the prometheus metrics and what they are used for.
Running the tests
To check that all has been installed correctly and that the pugin has no errors run the tests:
- Run this command at the project's root:
npm run test:plugin-ledger-connector-besu
Contributing
We welcome contributions to Hyperledger Cactus in many forms, and there’s always plenty to do!
Please review CONTRIBUTING.md to get started.
License
This distribution is published under the Apache License Version 2.0 found in the LICENSE file.