-
Notifications
You must be signed in to change notification settings - Fork 269
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
brunoffmateus
wants to merge
2
commits into
hyperledger:satp-dev
Choose a base branch
from
brunoffmateus:satp-3100-cctx
base: satp-dev
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file modified
0
examples/cactus-example-carbon-accounting-backend/src/main/typescript/index.web.ts
100755 → 100644
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -186,6 +186,7 @@ | |
"BESU_2X", | ||
"BURROW_0X", | ||
"CORDA_4X", | ||
"ETHEREUM", | ||
"FABRIC_14X", | ||
"FABRIC_2", | ||
"QUORUM_2X", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
1 change: 1 addition & 0 deletions
1
packages/cactus-plugin-ccmodel-hephaestus/src/main/typescript/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./public-api"; |
1 change: 1 addition & 0 deletions
1
packages/cactus-plugin-ccmodel-hephaestus/src/main/typescript/index.web.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {}; |
42 changes: 42 additions & 0 deletions
42
packages/cactus-plugin-ccmodel-hephaestus/src/main/typescript/models/carbon-footprint.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?