Skip to content

Commit

Permalink
remove global txs list
Browse files Browse the repository at this point in the history
  • Loading branch information
F3kilo committed Nov 29, 2024
1 parent 4502c54 commit d26ccc7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 24 deletions.
74 changes: 54 additions & 20 deletions bin/reth/tests/commands/bitfinity_node_it.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use reth_node_ethereum::EthereumNode;
use reth_primitives::{Transaction, TransactionSigned};
use reth_tasks::TaskManager;
use reth_transaction_pool::test_utils::MockTransaction;
use revm_primitives::{Address, U256};
use revm_primitives::{Address, B256, U256};
use std::time::Duration;
use std::{net::SocketAddr, str::FromStr, sync::Arc};
use tokio::sync::Mutex;
Expand All @@ -45,7 +45,7 @@ async fn bitfinity_test_node_forward_ic_or_eth_get_last_certified_block() {
// Arrange
let _log = init_logs();

let eth_server = EthImpl::new().await;
let eth_server = EthImpl::new();
let (_server, eth_server_address) =
mock_eth_server_start(EthServer::into_rpc(eth_server)).await;
let (reth_client, _reth_node) =
Expand Down Expand Up @@ -75,7 +75,7 @@ async fn bitfinity_test_node_forward_get_gas_price_requests() {
// Arrange
let _log = init_logs();

let eth_server = EthImpl::new().await;
let eth_server = EthImpl::new();
let gas_price = eth_server.gas_price;
let (_server, eth_server_address) =
mock_eth_server_start(EthServer::into_rpc(eth_server)).await;
Expand All @@ -94,7 +94,7 @@ async fn bitfinity_test_node_forward_max_priority_fee_per_gas_requests() {
// Arrange
let _log = init_logs();

let eth_server = EthImpl::new().await;
let eth_server = EthImpl::new();
let max_priority_fee_per_gas = eth_server.max_priority_fee_per_gas;
let (_server, eth_server_address) =
mock_eth_server_start(EthServer::into_rpc(eth_server)).await;
Expand All @@ -113,7 +113,7 @@ async fn bitfinity_test_node_forward_eth_get_genesis_balances() {
// Arrange
let _log = init_logs();

let eth_server = EthImpl::new().await;
let eth_server = EthImpl::new();
let (_server, eth_server_address) =
mock_eth_server_start(EthServer::into_rpc(eth_server)).await;
let (reth_client, _reth_node) =
Expand Down Expand Up @@ -147,7 +147,7 @@ async fn bitfinity_test_node_forward_ic_get_genesis_balances() {
// Arrange
let _log = init_logs();

let eth_server = EthImpl::new().await;
let eth_server = EthImpl::new();
let (_server, eth_server_address) =
mock_eth_server_start(EthServer::into_rpc(eth_server)).await;
let (reth_client, _reth_node) =
Expand All @@ -174,7 +174,9 @@ async fn bitfinity_test_node_forward_send_raw_transaction_requests() {
// Arrange
let _log = init_logs();

let eth_server = EthImpl::new().await;
let eth_server = EthImpl::new();
let received_txs = eth_server.txs.clone();

let (_server, eth_server_address) =
mock_eth_server_start(EthServer::into_rpc(eth_server)).await;
let (reth_client, _reth_node) =
Expand All @@ -191,25 +193,29 @@ async fn bitfinity_test_node_forward_send_raw_transaction_requests() {
// Assert
assert_eq!(result.to_fixed_bytes(), expected_tx_hash.0.to_fixed_bytes());

tokio::time::sleep(Duration::from_secs(1)).await;
assert!(check_transactions_received(&received_txs, 1).await);

assert_eq!(eth_server::TXS_ORDER.lock().await[0].0, expected_tx_hash.0.to_fixed_bytes());
assert_eq!(received_txs.lock().await[0].0, expected_tx_hash.0.to_fixed_bytes());
}

#[tokio::test]
async fn bitfinity_test_node_send_raw_transaction_in_gas_price_order() {
// Arrange
let _log = init_logs();

let eth_server = EthImpl::new().await;
let eth_server = EthImpl::new();
let received_txs = eth_server.txs.clone();

let (_server, eth_server_address) =
mock_eth_server_start(EthServer::into_rpc(eth_server)).await;
let (reth_client, _reth_node) =
start_reth_node(Some(format!("http://{}", eth_server_address)), None).await;

const TXS_NUMBER: usize = 10;

// Create a random transactions
let transactions = (1..=10)
.map(|i| alloy_rlp::encode(transaction_with_gas_price(100 * i)))
let transactions = (1..=TXS_NUMBER)
.map(|i| alloy_rlp::encode(transaction_with_gas_price(100 * i as u128)))
.collect::<Vec<_>>();

let expected_hashes = transactions.iter().map(|tx| keccak::keccak_hash(tx)).collect::<Vec<_>>();
Expand All @@ -220,13 +226,31 @@ async fn bitfinity_test_node_send_raw_transaction_in_gas_price_order() {
assert_eq!(hash.to_fixed_bytes(), expected_hash.0.to_fixed_bytes());
}

tokio::time::sleep(Duration::from_secs(1)).await;
assert!(check_transactions_received(&received_txs, TXS_NUMBER).await);

for (idx, expected_hash) in expected_hashes.iter().rev().enumerate() {
assert_eq!(eth_server::TXS_ORDER.lock().await[idx].0, expected_hash.0.to_fixed_bytes());
assert_eq!(received_txs.lock().await[idx].0, expected_hash.0.to_fixed_bytes());
}
}

/// Waits until `n` transactions appear in `received_txs` with one second timeout.
/// Returns true if `received_txs` contains at least `n` transactions.
async fn check_transactions_received(received_txs: &Mutex<Vec<B256>>, n: usize) -> bool {
let wait_future = async {
loop {
let txs_number = received_txs.lock().await.len();
if txs_number >= n {
break;
}

tokio::time::sleep(Duration::from_millis(50)).await;
}
};

let wait_result = tokio::time::timeout(Duration::from_secs(3), wait_future).await;
wait_result.is_ok()
}

fn transaction_with_gas_price(gas_price: u128) -> TransactionSigned {
let mock = MockTransaction::legacy().with_gas_price(gas_price);
let transaction: Transaction = mock.into();
Expand Down Expand Up @@ -354,6 +378,8 @@ async fn mock_eth_server_start(methods: impl Into<Methods>) -> (ServerHandle, So
}

pub mod eth_server {
use std::sync::Arc;

use alloy_rlp::{Bytes, Decodable};
use ethereum_json_rpc_client::{Block, CertifiedResult, H256};
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
Expand All @@ -379,18 +405,26 @@ pub mod eth_server {
async fn get_last_certified_block(&self) -> RpcResult<CertifiedResult<Block<H256>>>;
}

pub static TXS_ORDER: Mutex<Vec<B256>> = Mutex::const_new(Vec::new());

#[derive(Debug)]
pub struct EthImpl {
pub gas_price: u128,
pub max_priority_fee_per_gas: u128,
pub txs: Arc<Mutex<Vec<B256>>>,
}

impl EthImpl {
pub async fn new() -> Self {
TXS_ORDER.lock().await.clear();
Self { gas_price: rand::random(), max_priority_fee_per_gas: rand::random() }
pub fn new() -> Self {
Self {
gas_price: rand::random(),
max_priority_fee_per_gas: rand::random(),
txs: Arc::default(),
}
}
}

impl Default for EthImpl {
fn default() -> Self {
Self::new()
}
}

Expand All @@ -408,7 +442,7 @@ pub mod eth_server {
let decoded = hex::decode(&tx).unwrap();
let tx = TransactionSigned::decode(&mut decoded.as_ref()).unwrap();
let hash = tx.hash();
TXS_ORDER.lock().await.push(hash);
self.txs.lock().await.push(hash);
Ok(hash)
}

Expand Down
3 changes: 2 additions & 1 deletion bin/reth/tests/commands/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ use tracing::{debug, info};

pub const LOCAL_EVM_CANISTER_ID: &str = "bkyz2-fmaaa-aaaaa-qaaaq-cai";
/// EVM block extractor for devnet running on Digital Ocean.
pub const DEFAULT_EVM_DATASOURCE_URL: &str = "https://orca-app-5yyst.ondigitalocean.app";
pub const DEFAULT_EVM_DATASOURCE_URL: &str =
"https://block-extractor-testnet-1052151659755.europe-west9.run.app";

pub fn init_logs() -> eyre::Result<Option<FileWorkerGuard>> {
let mut tracer = RethTracer::new();
Expand Down
4 changes: 2 additions & 2 deletions bitfinity.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ reth node -vvv --http --http.port 8080 --http.addr 0.0.0.0 --http.api "debug,eth
With cargo:

```sh
cargo run -p reth -- node -vvv --http --http.port 8080 --http.addr 0.0.0.0 --http.api "debug,eth,net,trace,txpool,web3" --disable-discovery --ipcdisable --no-persist-peers -r https://orca-app-5yyst.ondigitalocean.app -i 30 -b 100 --max-fetch-blocks 5000 --log.file.directory ./target/logs --datadir ./target/reth
cargo run -p reth -- node -vvv --http --http.port 8080 --http.addr 0.0.0.0 --http.api "debug,eth,net,trace,txpool,web3" --disable-discovery --ipcdisable --no-persist-peers -r https://block-extractor-testnet-1052151659755.europe-west9.run.app -i 30 -b 100 --max-fetch-blocks 5000 --log.file.directory ./target/logs --datadir ./target/reth
```

You can query the node using the JSON-RPC API. For example, to get the block number, you can use the following command:
Expand Down Expand Up @@ -83,7 +83,7 @@ dfx canister --network=ic call EVM_CANISTER_ID admin_disable_evm '(true)'

3. Run the EVM reset command. For example:
```sh
cargo run -p reth -- bitfinity-reset-evm-state -vvv --datadir ./target/reth --ic-identity-file-path PATH_TO_IDENTITY/identity.pem --evm-network https://ic0.app --evmc-principal EVM_CANISTER_ID --evm-datasource-url https://orca-app-5yyst.ondigitalocean.app
cargo run -p reth -- bitfinity-reset-evm-state -vvv --datadir ./target/reth --ic-identity-file-path PATH_TO_IDENTITY/identity.pem --evm-network https://ic0.app --evmc-principal EVM_CANISTER_ID --evm-datasource-url https://block-extractor-testnet-1052151659755.europe-west9.run.app
```

4. Enable the EVM canister:
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ services:
dockerfile: ./Dockerfile
ports:
- '8080:8080'
command: node -vvv --http --http.port 8080 --http.addr 0.0.0.0 --http.api "debug,eth,net,trace,txpool,web3" --disable-discovery --ipcdisable --no-persist-peers -r https://orca-app-5yyst.ondigitalocean.app -i 10 -b 100 --datadir /reth/data
# command: node -vvv --http --http.port 8080 --http.addr 0.0.0.0 --http.api "debug,eth,net,trace,txpool,web3" --disable-discovery --ipcdisable --no-persist-peers -r https://block-extractor-testnet-1052151659755.europe-west9.run.app -i 10 -b 100 --datadir /reth/data
volumes:
- ./target/reth:/reth

0 comments on commit d26ccc7

Please sign in to comment.