Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cc-tx-visualization): capture transactional data with RxJS #3100 #3208

Open
wants to merge 2 commits into
base: satp-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"cccs",
"ccep",
"ccid",
"cctx",
"celo",
"cids",
"clsx",
Expand Down Expand Up @@ -81,6 +82,7 @@
"hada",
"hashicorp",
"Healthcheck",
"hephaestus",
"htlc",
"Htlc",
"HTLC",
Expand Down
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
* @petermetz @takeutak @izuru0 @jagpreetsinghsasan @vramakrishna @sandeepnRES @outSH

packages/cactus-plugin-ccmodel-hephaestus @RafaelAPB
packages/cactus-plugin-satp-hermes @RafaelAPB
packages/cactus-plugin-bungee-hermes @RafaelAPB
examples/cactus-example-cbdc-bridging @RafaelAPB
Expand Down
Empty file.
1 change: 1 addition & 0 deletions packages/cactus-core-api/src/main/json/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
"BESU_2X",
"BURROW_0X",
"CORDA_4X",
"ETHEREUM",
"FABRIC_14X",
"FABRIC_2",
"QUORUM_2X",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import com.squareup.moshi.JsonClass
/**
* Enumerates the different ledger vendors and their major versions encoded within the name of the LedgerType. For example \"BESU_1X\" involves all of the [1.0.0;2.0.0) where 1.0.0 is included and anything up until, but not 2.0.0. See: https://stackoverflow.com/a/4396303/698470 for further explanation.
*
* Values: bESU1X,bESU2X,bURROW0X,cORDA4X,fABRIC14X,fABRIC2,qUORUM2X,sAWTOOTH1X
* Values: bESU1X,bESU2X,bURROW0X,cORDA4X,eTHEREUM,fABRIC14X,fABRIC2,qUORUM2X,sAWTOOTH1X
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to change this file?

*/

@JsonClass(generateAdapter = false)
Expand All @@ -40,6 +40,9 @@ enum class LedgerType(val value: kotlin.String) {
@Json(name = "CORDA_4X")
cORDA4X("CORDA_4X"),

@Json(name = "ETHEREUM")
eTHEREUM("ETHEREUM"),

@Json(name = "FABRIC_14X")
fABRIC14X("FABRIC_14X"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ export const LedgerType = {
Besu2X: 'BESU_2X',
Burrow0X: 'BURROW_0X',
Corda4X: 'CORDA_4X',
Ethereum: 'ETHEREUM',
Fabric14X: 'FABRIC_14X',
Fabric2: 'FABRIC_2',
Quorum2X: 'QUORUM_2X',
Expand Down
4 changes: 4 additions & 0 deletions packages/cactus-plugin-ccmodel-hephaestus/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cactus-openapi-spec-plugin-consortium-manual.json
src/main/typescript/generated/openapi/typescript-axios/.npmignore
src/test/csv
src/test/json
140 changes: 140 additions & 0 deletions packages/cactus-plugin-ccmodel-hephaestus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# `@hyperledger/cactus-plugin-ccmodel-hephaestus`

The package provides `Hyperledger Cacti` a way to generate process models from arbitrary cross-chain use cases. The implementation follows the paper [Hephaestus](https://www.techrxiv.org/doi/full/10.36227/techrxiv.20718058.v3).

With this plugin it will be possible to generate cross-chain models from local transactions in different ledgers (currently supports Besu, Ethereum, and Fabric), realizing arbitrary cross-chain use cases and allowing operators to monitor their applications.
Through monitoring, errors like outliers and malicious behavior can be identified, which can enable programmatically stopping attacks (circuit breaker), including bridge hacks.

## Summary

- [`@hyperledger/cactus-plugin-ccmodel-hephaestus`](#hyperledgercactus-plugin-ccmodel-hephaestus)
- [Summary](#summary)
- [Getting Started](#getting-started)
- [Prerequisites](#prerequisites)
- [Architecture](#architecture)
- [RxJS Transaction Monitoring](#rxjs-transaction-monitoring)
- [Cross-Chain Model Pipeline](#cross-chain-model-pipeline)
- [PM4Py Overview](#pm4py-overview)
- [Running the tests](#running-the-tests)
- [Usage](#usage)
- [Contributing](#contributing)
- [License](#license)


## 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:
```sh
npm run configure
```

Know how to use the following plugins of the project:

- [cactus-plugin-ledger-connector-besu](https://github.com/hyperledger/cactus/tree/main/packages/cactus-plugin-ledger-connector-besu)
- [cactus-plugin-ledger-connector-ethereum](https://github.com/hyperledger/cactus/tree/main/packages/cactus-plugin-ledger-connector-ethereum)
- [cactus-plugin-ledger-connector-fabric](https://github.com/hyperledger/cactus/tree/main/packages/cactus-plugin-ledger-connector-fabric)


## Architecture

### RxJS Transaction Monitoring

This plugin utilizes RxJS (Reactive Extensions for JavaScript) to monitor transactions issued in Hyperledger Besu, Hyperledger Ethereum, and Hyperledger Fabric connectors.

RxJS provides a powerful framework for asynchronous or callback-based code, each connector maintains an RxJS `ReplaySubject`, named `txSubject`, which acts as a message bus for emitting transaction data.

- When a transaction is issued in a connector, the `ReplaySubject` stores the value it observes in an internal buffer. This observation is achieved by passing the value to its `next` method.
- When a new subscriber subscribes to the `txSubject`, it synchronously emits all values in its buffer in a First-In-First-Out (FIFO) manner. This ensures that subscribers receive the most recent transactional data, regardless of when they subscribe.
- The transactional data emitted includes essential information such as the transaction ID, timestamp, and other parameters, which are necessary for creating transaction receipts within the plugin.

### Cross-Chain Model Pipeline

The plugin employs a structured pipeline to create a cross-chain model from monitored connector transactions:

1. **Transaction Emission**: Connectors issue local transactions against their respective target blockchains. Each transaction's data is emitted by the `txSubject` to the subscribers within our plugin.

2. **Receipt Polling**: Upon receiving of transactional data, the plugin processes it into transaction receipts. This step involves precessing transactional information received such as transaction IDs, timestamps, and other parameters.

3. **Cross-Chain Event Logging**: Processed receipts information can then be used to create cross-chain events, forming a cross-chain event log.

4. **Cross-Chain Model**: The plugin uses the cross chain event log to create the cross-chain model with the information received from the connectors, using the Process Mining for Python (PM4PY) library. This model can then be used to verify new and unmodeled transactional information received from the connectors.

### PM4Py Overview

[PM4Py](https://pm4py.fit.fraunhofer.de/) is an open-source library designed for process mining, that provides a suite of tools to analyze and visualize business processes based on event logs.
With it we can generate process models from event logs and compare event logs with process models to identify deviations.

## Running the tests
- **basic-mock-test.test.ts**: Conducts the simulation of a transaction without instantiating the connectors, and tests that the plugin monitors, captures, and processes the transactional data and creates a cross-chain event.
- **monitor-4-besu-events.test.ts**: Tests the plugin's ability to monitor, capture, and process the transactional data from besu connectors, and exports transactional data, in both CSV and JSON formats, as cross-chain event logs.
- **monitor-4-ethereum-events.test.ts**: Tests the plugin's ability to monitor, capture, and process the transactional data from ethereum connectors, and exports transactional data, in both CSV and JSON formats, as cross-chain event logs.
- **monitor-4-fabric-events.test.ts**: Tests the plugin's ability to monitor, capture, and process the transactional data from fabric connectors, and exports transactional data, in both CSV and JSON formats, as cross-chain event logs.
- **monitor-besu-ethereum-fabric-events.test.ts**: Tests the plugin's ability to monitor transactional data from besu, ethereum, and fabric connectors at the same time.
- **cross-chain-model-serialization.test.ts**: Tests the plugin's ability to create the cross-chain model using the PM4PY (Process Mining for Python) library and save it serialized.
- **cross-chain-model-conformance-checking.test.ts**: Tests the plugin's ability to check the conformity of unmodeled transactions against the cross-chain model using the PM4PY (Process Mining for Python) library.
- **cross-chain-model-periodic-update.test.ts**: Tests the plugin's ability to periodically update the cross-chain model with received transactional data.

## Usage
Let us consider three conectors: one connected to Hyperledger Besu and, another connected to Hyperledger Ethereum, and a third connected to Hyperledger Fabric. To monitor cross-chain transactions and create a cross-chain model we should follow the next steps.

After instantiating the connectors, we instantiate the plugin as follows:
```typescript
let hephaestusOptions: IPluginCcModelHephaestusOptions;

hephaestusOptions = {
instanceId: randomUUID(),
logLevel: logLevel,
besuTxObservable: besuConnector.getTxSubjectObservable(),
ethereumTxObservable: ethereumConnector.getTxSubjectObservable(),
fabricTxObservable: fabricConnector.getTxSubjectObservable(),
};
hephaestus = new CcModelHephaestus(hephaestusOptions);
```

We set the desired caseID and start monitoring and processing the transactions into transaction receipts:

```typescript
hephaestus.setCaseId("Desired_CaseID");
hephaestus.monitorTransactions();
```

We can create cross-chain events from processed transaction receipts and add them to the cross-chain event log:

```typescript
hephaestus.txReceiptToCrossChainEventLogEntry();
```

We can export the transactional data captured to CSV and JSON files:

```typescript
hephaestus.persistCrossChainLogCsv("output-file-CSV");
hephaestus.persistCrossChainLogJson("output-file-JSON");
```

We can create the cross-chain model with events created from the transactional data captured:
```typescript
await hephaestus.createModel();
```

We can periodically update the cross-chain model with events created from the transactional data captured:
```typescript
const timeInterval = 10000;
const fileName = "file-with-modeled-logs"
await hephaestus.periodicCCModelUpdate(
fileName,
timeInterval,
);
await hephaestus.stopPeriodicCCModelUpdate(fileName);
```

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

Please review [CONTIRBUTING.md](https://github.com/hyperledger/cactus/blob/main/CONTRIBUTING.md "CONTIRBUTING.md") to get started.

## License
This distribution is published under the Apache License Version 2.0 found in the [LICENSE ](https://github.com/hyperledger/cactus/blob/main/LICENSE "LICENSE ")file.
97 changes: 97 additions & 0 deletions packages/cactus-plugin-ccmodel-hephaestus/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"name": "@hyperledger/cactus-plugin-ccmodel-hephaestus",
"version": "1.0.0",
"description": "A web service plugin that provides management capabilities on cross-chain transactions visualization.",
"keywords": [
"Hyperledger",
"Cactus",
"Integration",
"Blockchain",
"Distributed Ledger Technology"
],
"homepage": "https://github.com/hyperledger/cactus#readme",
"bugs": {
"url": "https://github.com/hyperledger/cactus/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/hyperledger/cactus.git"
},
"license": "Apache-2.0",
"author": {
"name": "Hyperledger Cactus Contributors",
"email": "cactus@lists.hyperledger.org",
"url": "https://www.hyperledger.org/use/cactus"
},
"contributors": [
{
"name": "Please add yourself to the list of contributors",
"email": "your.name@example.com",
"url": "https://example.com"
},
{
"name": "Iulia Mihaiu"
},
{
"name": "Sabrina Scuri"
},
{
"name": "Rafael Belchior"
},
{
"name": "Bruno Mateus"
}
],
"main": "dist/lib/main/typescript/index.js",
"module": "dist/lib/main/typescript/index.js",
"browser": "dist/cactus-plugin-ccmodel-hephaestus.web.umd.js",
"types": "dist/types/main/typescript/index.d.ts",
"files": [
"dist/*"
],
"scripts": {
"build": "npm run tsc",
"tsc": "tsc --project ./tsconfig.json",
"watch": "npm-watch",
"webpack": "npm-run-all webpack:dev webpack:prod",
"webpack:dev": "npm-run-all webpack:dev:node webpack:dev:web",
"webpack:dev:web": "webpack --env=dev --target=web --config ../../webpack.config.js",
"webpack:dev:node": "webpack --env=dev --target=node --config ../../webpack.config.js",
"webpack:prod": "npm-run-all webpack:prod:node webpack:prod:web",
"webpack:prod:web": "webpack --env=prod --target=web --config ../../webpack.config.js",
"webpack:prod:node": "webpack --env=prod --target=node --config ../../webpack.config.js"
},
"dependencies": {
"@hyperledger/cactus-common": "2.0.0-alpha.2",
"@hyperledger/cactus-core": "2.0.0-alpha.2",
"@hyperledger/cactus-core-api": "2.0.0-alpha.2",
"@hyperledger/cactus-plugin-ledger-connector-besu": "2.0.0-alpha.2",
"@hyperledger/cactus-plugin-ledger-connector-ethereum": "2.0.0-alpha.2",
"@hyperledger/cactus-plugin-ledger-connector-fabric": "2.0.0-alpha.2",
"run-time-error-cjs": "1.4.0",
"typescript-optional": "2.0.1"
},
"devDependencies": {
"@hyperledger/cactus-plugin-keychain-memory": "2.0.0-alpha.2",
"@hyperledger/cactus-test-geth-ledger": "2.0.0-alpha.2",
"@hyperledger/cactus-test-tooling": "2.0.0-alpha.2",
"@types/express": "4.17.8",
"@types/uuid": "8.3.0",
"body-parser": "1.19.0",
"express": "4.17.1",
"fabric-network": "2.2.20",
"socket.io": "4.5.4",
"uuid": "8.3.2",
"web3": "1.6.1",
"web3-core": "1.6.1"
},
"engines": {
"node": ">=10",
"npm": ">=6"
},
"publishConfig": {
"access": "public"
},
"browserMinified": "dist/cactus-plugin-ccmodel-hephaestus.web.umd.min.js",
"mainMinified": "dist/cactus-plugin-ccmodel-hephaestus.node.umd.min.js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./public-api";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { LedgerType } from "@hyperledger/cactus-core-api";

export function calculateGasPriceEth(
gasUsed: number,
effectiveGasPrice: string | undefined,
): number {
if (!gasUsed || !effectiveGasPrice) {
return 0;
}
const gasPrice = parseFloat(effectiveGasPrice);
return gasUsed * gasPrice;
}

export function calculateCarbonFootPrintFabric(
peers: string[] | undefined,
): number {
if (!peers) {
return 0;
}
return peers.length * CarbonFootPrintConstants(LedgerType.Fabric2);
}
export function calculateCarbonFootPrintBesu(): number {
return CarbonFootPrintConstants(LedgerType.Besu2X);
}

export const CarbonFootPrintConstants = (ledger: LedgerType): number => {
switch (ledger) {
case LedgerType.Besu2X:
return 0.00018;

case LedgerType.Besu1X:
return 0.00018;

case LedgerType.Fabric2:
return 0.00018;

case LedgerType.Fabric14X:
return 0.00018;
default:
return 0;
}
};