You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This repository is dedicated to experimenting with blockchain technologies.
2
+
This repository is dedicated to experimenting with blockchain technologies using a hybrid architecture that involves both Go and Rust. It focuses on cross-language communication via FFI (Foreign Function Interface) to fetch blockchain data, analyze and simulate transactions.
3
3
4
4
## Project Structure
5
-
```
6
-
./
7
-
├── README.md*
8
-
├── goCode/
9
-
│ ├── go.mod*
10
-
│ ├── go.sum*
11
-
│ ├── goApp/
12
-
│ │ ├── librpc.a*
13
-
│ │ ├── main.go*
14
-
│ │ └── rpc.dll*
15
-
│ └── internal/
16
-
│ ├── config/
17
-
│ │ └── config.go*
18
-
│ ├── rpc/
19
-
│ │ ├── rpc.go*
20
-
│ │ └── types.go*
21
-
│ ├── transaction/
22
-
│ │ ├── balance.go*
23
-
│ │ └── transaction.go*
24
-
│ └── utilis/
25
-
│ └── utilis.go*
26
-
└── rustCode/
27
-
├── Cargo.lock*
28
-
├── Cargo.toml*
29
-
└── src/
30
-
├── config/
31
-
│ └── mod.rs*
32
-
├── lib.rs*
33
-
├── main.rs*
34
-
├── models/
35
-
│ └── mod.rs*
36
-
└── rpc/
37
-
├── fetch.rs*
38
-
└── mod.rs*
5
+
```bash
6
+
.
7
+
├── .github
8
+
│ └── workflows
9
+
│ └── check-env.yml
10
+
├── .gitignore
11
+
├── goCode
12
+
│ ├── .env # .env file
13
+
│ ├── goApp
14
+
│ │ └── main.go
15
+
│ ├── go.mod
16
+
│ ├── go.sum
17
+
│ ├── internal
18
+
│ │ ├── config
19
+
│ │ │ └── config.go
20
+
│ │ ├── rpc
21
+
│ │ │ ├── rpc.go
22
+
│ │ │ └── types.go
23
+
│ │ ├── transaction
24
+
│ │ │ ├── balance.go
25
+
│ │ │ └── transaction.go
26
+
│ │ └── utilis
27
+
│ │ └── utilis.go
28
+
│ └── libs
29
+
│ └── (librpc.so) or (rpc.dll & librpc.a) # Rust library used in Go via FFI
30
+
├── Makefile
31
+
├── README.md
32
+
└── rustCode
33
+
├── Cargo.lock
34
+
├── Cargo.toml
35
+
└── src
36
+
├── config
37
+
│ └── mod.rs
38
+
├── lib.rs
39
+
├── main.rs
40
+
├── models
41
+
│ └── mod.rs
42
+
├── rpc
43
+
│ ├── fetch.rs
44
+
│ └── mod.rs
45
+
└── utils
46
+
├── functions.rs
47
+
├── mod.rs
48
+
└── prints.rs
39
49
```
40
50
## Setup Instructions
41
51
@@ -47,71 +57,71 @@ Before you can run the program, ensure you have the following installed:
47
57
48
58
2.**Rust**: Ensure you have the Rust toolchain installed, which includes `cargo` and `rustc`. You can install it from the official [Rust website](https://www.rust-lang.org/learn/get-started).
49
59
50
-
3.**Geth**: The **Go Ethereum** client is required for the Go program to interact with the Ethereum blockchain. You can download it from the official [Geth website](https://geth.ethereum.org/downloads/).
51
-
52
-
4.**.env file**: Ensure the `.env` file is properly configured in the `goCode` folder with the following variables:
60
+
3.**.env file**: Ensure the `.env` file is properly configured in the `goCode` folder with the following variables:
5.**Tenderly Virtual Network** (Optional but Recommended)
61
-
62
-
- The project runs on the **Tenderly Virtual Network**, which simulates Ethereum blockchain interactions for testing and safe experimentation. Tenderly provides a controlled environment where you can simulate and test Ethereum transactions, making it ideal for development and experimentation without risking real assets.
63
-
64
-
- You can connect to the Tenderly Virtual Network by configuring the appropriate RPC URL in the `.env` file.
65
-
66
-
67
-
68
-
### Building and Running the Program on Windows
69
-
**Rust Part (FFI Library)**
70
-
- Navigate to the `rustCode` folder.
71
-
- Run the following command to build the Rust FFI library:
72
-
```
73
-
cargo build --release
74
-
```
75
-
- for **Ubuntu/Linux** -> This will generate librpc.so in the rustCode/target/release folder.
76
-
- Then copy them to `goCode/goApp` folder as
77
-
```
78
-
cp target/release/librpc.so ../goCode/goApp/
79
-
```
80
-
81
-
- for **Windows** -> This will generate **rpc.dll** and **rpc.dll.lib** in `rustCode/target/release` folder
82
-
- Then copy them to `goCode/goApp` folder as
83
-
- rpc.dll -> **rpc.dll**
84
-
- rpc.dll.lib -> **librpc.a** *(renamed)*
85
-
86
-
**Go Part**
87
-
- Navigate to the `goCode/goApp` folder.
88
-
- Then run
89
-
```go run main.go```
90
-
91
-
You can allso run just rust part of rust code trough command `cargo run` in `rustCode` folder
92
-
93
-
68
+
-**[Tenderly](https://tenderly.co/about-us)** is used for testnet interactions, providing a simulated blockchain environment ideal for testing and experimentation.
69
+
-**[Infura](https://www.infura.io/)** is used for mainnet data, enabling access to real-world blockchain data via a robust API service.
70
+
-**[Geth](https://geth.ethereum.org/downloads/)**. (if using locally): If running a local Geth node, set the RPC URL to your configured port. Ensure Geth is synced and running with RPC enabled.
71
+
72
+
## Building and Running the Program
73
+
74
+
You can build and run the program for both Linux and Windows using the Makefile. This automates the process of building the Rust FFI library, copying the appropriate library files, and running the Go application.
75
+
76
+
1. Navigate to the root of the project.
77
+
2. Run the following command to build and run the program:
78
+
-**Build** and **run** for **Linux**
79
+
```
80
+
make allLinux
81
+
```
82
+
> This will:
83
+
> - Build the Rust library (librpc.so)
84
+
> - Copy the librpc.so file to the goCode/libs folder
85
+
> - Run the Go application
86
+
87
+
- **Build** and **run** for **Windows**
88
+
```
89
+
make allWindows
90
+
```
91
+
>This will:
92
+
> - Build the Rust library (rpc.dll and rpc.dll.lib)
93
+
> - Copy the rpc.dll to goCode/libs/rpc.dll and rpc.dll.lib to goCode/libs/librpc.a
94
+
> - Run the Go application
95
+
96
+
## Makefile Commands
97
+
- `make allLinux`: Builds the Rust library for Linux, copies the library to the Go app's libs folder, and runs the Go application.
98
+
- `make allWindows`: Builds the Rust library for Windows, copies the library to the Go app's libs folder, and runs the Go application.
99
+
- `make clean`: Cleans up the build artifacts, including the `target` directory for Rust and the `librpc.so/rpc.dll` in the Go `libs` folder.
100
+
- `make run_rust`: Runs only the Rust code (`cargo run`).
101
+
- `make run_go`: Runs only the Go application (`go run main.go`).
94
102
95
103
## Functionalities
96
104
97
105
1. **Go Program**:
98
106
- **Fetches the latest block number** from the blockchain using the provided RPC URL via a standard RPC connection. It sends an RPC request to the Ethereum node and retrieves the latest block's details, including transactions.
99
-
- **Sets a fake balance** for a recipient address using the **Geth client** in the Go program, by calling the `tenderly_setBalance` method to simulate a blockchain environment and test transaction functionalities.
100
-
- **Sends a transaction** to the recipient address with a specified amount of Ether, utilizing the **Geth client** to interact directly with the Ethereum blockchain. This includes creating and signing the transaction using a private key and then broadcasting it to the network.
101
-
- **Uses the FFI mechanism** to call the Rust program and fetch transaction data from the latest block, enabling cross-language communication for blockchain data fetching and analysis.
102
-
107
+
- **Sets a fake balance** for a recipient address using the Tenderly-specific `tenderly_setBalance` method via RPC to simulate a blockchain environment and test transaction functionalities.
108
+
- **Sends a transaction** to the recipient address with a specified amount of Ether, utilizing the go-ethereum library (`ethclient`) to connect to an RPC endpoint, create, sign, and broadcast the transaction. This interacts with the blockchain via RPC.
109
+
- **Uses the FFI mechanism** to call the Rust program and fetch transaction data from the latest block, enabling **cross-language communication** for blockchain data fetching and analysis.
110
+
- **Fetches maximum gas transactions** from the last 5 blocks by calling Rust via **FFI**, which retrieves and analyzes transaction receipts to identify the **highest gas-used** transactions per block, including their **percentage** of the block's total gas.
111
+
103
112
2. **Rust Program**:
104
-
- The Rust Program can also **fetch the latest block number** directly as Go Program
105
-
- The Rust library (`rpc.dll`) exposes the function `fetch_transactions`, which retrieves **transaction data** from the latest block on the blockchain. This function is called from the Go program through FFI.
113
+
- The Rust program can independently **fetch the latest block number** and details directly via RPC, similar to the Go program.
114
+
- The Rust library (`rpc.dll` or `librpc.so`) exposes functions like `fetch_transactions`, which retrieves transaction data from the latest block on the blockchain. This function is called from the Go program through FFI.
115
+
- Additionally, it exposes `fetch_max_tx_per_last_5_blocks`, which fetches the last 5 blocks, retrieves transaction receipts, and identifies the transaction with the maximum gas usage in each block, calculating its percentage of the block's gas limit. This data is returned as JSON for processing in Go.
116
+
- The Rust program can run standalone to fetch and analyze blocks from both testnet and mainnet, printing block info, transaction details, and summaries of max gas transactions.
106
117
107
118
In summary:
108
-
- The **Go program** uses **Geth client** for sending transactions and setting fake balances, directly interacting with the Ethereum blockchain.
109
-
- The **Rust library** is used for fetching blockchain data via RPC calls for getting transaction details from the latest block.
110
-
119
+
- The **Go program** handles blockchain interactions like setting fake balances (via Tenderly RPC), sending transactions (via go-ethereum RPC client), and fetching block data, all through external RPC endpoints without requiring a local Geth client.
120
+
- The **Rust library** is used for fetching and analyzing blockchain data via RPC calls, including transaction details and gas usage summaries from multiple blocks. It communicates with Go through FFI for integrated functionality.
111
121
112
122
113
123
### FFI Usage:
114
-
The project uses **FFI (Foreign Function Interface)** to enable communication between Go and Rust. Go interacts with the Ethereum blockchain using the **Geth client** for transaction creation and fake balance setting. Rust, on the other hand, fetches transaction data from the latest block through the `rpc.dll` library. Go calls Rust functions via FFI to retrieve blockchain data.
124
+
The project uses **FFI (Foreign Function Interface)** to enable communication between Go and Rust. Go interacts with the Ethereum blockchain using RPC endpoints (via the go-ethereum library) for transaction creation and fake balance setting. Rust, on the other hand, fetches transaction data from the latest block through the rpc.dll library. Go calls Rust functions via FFI to retrieve blockchain data.
115
125
116
126
---
117
127
@@ -122,8 +132,9 @@ The project uses **FFI (Foreign Function Interface)** to enable communication be
122
132
* **Tenderly Virtual Network**: The project runs on the **Virtual TestNets**, simulating Ethereum blockchain interactions for testing and safe experimentation.
123
133
124
134
* **Communication**:
125
-
- **Go** handles blockchain interaction (sending transactions, setting balances) using the **Geth client**.
126
-
- **Rust** fetches transaction details from the latest block via **RPC** and communicates with Go through **FFI**.
135
+
- **Go** handles blockchain interaction (sending transactions, setting balances) using RPC endpoints via the go-ethereum library.
136
+
- **Rust** fetches transaction details from the latest blocks via **RPC** and communicates with Go through **FFI**.
127
137
128
138
* **Simplify**: In this project, for the sake of simplicity and testing, the same address `RECIPIENT_ADDRESS` was used for both the sender and the recipient. This is done to simulate the transaction process without involving multiple real addresses. In a real-world scenario, the sender and recipient would have distinct addresses.
0 commit comments