Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ncitron committed Sep 23, 2024
1 parent 6819f75 commit dd40137
Show file tree
Hide file tree
Showing 18 changed files with 198 additions and 246 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ typenum = "1.17.0"
# Top Level Dependencies
######################################

[dependencies]
helios-core = { path = "./core" }
helios-ethereum = { path = "./ethereum" }

[dev-dependencies]
tokio = { version = "1", features = ["full"] }
dotenv = "0.15.0"
Expand Down
8 changes: 2 additions & 6 deletions core/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ impl<N: NetworkSpec, C: Consensus<N::TransactionResponse>> Client<N, C> {
pub fn new(
execution_rpc: &str,
consensus: C,
#[cfg(not(target_arch = "wasm32"))]
rpc_address: Option<SocketAddr>
#[cfg(not(target_arch = "wasm32"))] rpc_address: Option<SocketAddr>,
) -> Result<Self> {
let node = Node::new(execution_rpc, consensus)?;
let node = Arc::new(node);
Expand All @@ -39,10 +38,7 @@ impl<N: NetworkSpec, C: Consensus<N::TransactionResponse>> Client<N, C> {

#[cfg(not(target_arch = "wasm32"))]
if let Some(rpc_address) = rpc_address {
rpc = Some(Rpc::new(
node.clone(),
rpc_address,
));
rpc = Some(Rpc::new(node.clone(), rpc_address));
}

Ok(Client {
Expand Down
81 changes: 40 additions & 41 deletions core/src/execution/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,44 +333,43 @@ fn is_precompile(address: &Address) -> bool {
address.le(&address!("0000000000000000000000000000000000000009")) && address.gt(&Address::ZERO)
}

#[cfg(test)]
mod tests {
use alloy::network::Ethereum;
use revm::primitives::KECCAK_EMPTY;
use tokio::sync::{mpsc::channel, watch};

use crate::{rpc::mock_rpc::MockRpc, state::State};

use super::*;

fn get_client() -> ExecutionClient<Ethereum, MockRpc> {
let (_, block_recv) = channel(256);
let (_, finalized_recv) = watch::channel(None);
let state = State::new(block_recv, finalized_recv, 64);
ExecutionClient::new("testdata/", state).unwrap()
}

#[tokio::test]
async fn test_proof_db() {
// Construct proofdb params
let execution = get_client();
let tag = BlockTag::Latest;

// Construct the proof database with the given client
let mut proof_db = ProofDB::new(tag, Arc::new(execution));

let address = address!("388C818CA8B9251b393131C08a736A67ccB19297");
let info = AccountInfo::new(
U256::from(500),
10,
KECCAK_EMPTY,
Bytecode::new_raw(revm::primitives::Bytes::default()),
);
proof_db.state.basic.insert(address, info.clone());

// Get the account from the proof database
let account = proof_db.basic(address).unwrap().unwrap();

assert_eq!(account, info);
}
}
// #[cfg(test)]
// mod tests {
// use revm::primitives::KECCAK_EMPTY;
// use tokio::sync::{mpsc::channel, watch};
//
// use crate::execution::{rpc::mock_rpc::MockRpc, state::State};
//
// use super::*;
//
// fn get_client() -> ExecutionClient<Ethereum, MockRpc> {
// let (_, block_recv) = channel(256);
// let (_, finalized_recv) = watch::channel(None);
// let state = State::new(block_recv, finalized_recv, 64);
// ExecutionClient::new("testdata/", state).unwrap()
// }
//
// #[tokio::test]
// async fn test_proof_db() {
// // Construct proofdb params
// let execution = get_client();
// let tag = BlockTag::Latest;
//
// // Construct the proof database with the given client
// let mut proof_db = ProofDB::new(tag, Arc::new(execution));
//
// let address = address!("388C818CA8B9251b393131C08a736A67ccB19297");
// let info = AccountInfo::new(
// U256::from(500),
// 10,
// KECCAK_EMPTY,
// Bytecode::new_raw(revm::primitives::Bytes::default()),
// );
// proof_db.state.basic.insert(address, info.clone());
//
// // Get the account from the proof database
// let account = proof_db.basic(address).unwrap().unwrap();
//
// assert_eq!(account, info);
// }
// }
2 changes: 1 addition & 1 deletion core/src/execution/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub fn encode_account(proof: &EIP1186AccountProofResponse) -> Vec<u8> {

#[cfg(test)]
mod tests {
use crate::proof::shared_prefix_length;
use crate::execution::proof::shared_prefix_length;

#[tokio::test]
async fn test_shared_prefix_length() {
Expand Down
196 changes: 98 additions & 98 deletions core/tests/execution.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
use alloy::primitives::{address, b256, U256, U64};
use common::types::{Block, BlockTag};

use execution::rpc::mock_rpc::MockRpc;
use execution::state::State;
use execution::ExecutionClient;
use tokio::sync::mpsc::channel;
use tokio::sync::watch;

fn create_state() -> State {
let (_, block_recv) = channel(256);
let (_, finalized_recv) = watch::channel(None);
State::new(block_recv, finalized_recv, 64)
}

fn create_client(state: State) -> ExecutionClient<MockRpc> {
ExecutionClient::new("testdata/", state).unwrap()
}

#[tokio::test]
async fn test_get_account() {
let state = create_state();

let address = address!("14f9D4aF749609c1438528C0Cce1cC3f6D411c47");
let block = Block {
state_root: b256!("aa02f5db2ee75e3da400d10f3c30e894b6016ce8a2501680380a907b6674ce0d"),
..Default::default()
};

state.push_block(block).await;
let execution = create_client(state);

let account = execution
.get_account(address, None, BlockTag::Latest)
.await
.unwrap();

assert_eq!(
account.balance,
U256::from_str_radix("48c27395000", 16).unwrap()
);
}

#[tokio::test]
async fn test_get_account_bad_proof() {
let state = create_state();

let address = address!("14f9D4aF749609c1438528C0Cce1cC3f6D411c47");
let block = Block::default();
state.push_block(block).await;

let execution = create_client(state);
let account_res = execution.get_account(address, None, BlockTag::Latest).await;

assert!(account_res.is_err());
}

// use alloy::primitives::{address, b256, U256, U64};
// use common::types::{Block, BlockTag};
//
// use execution::rpc::mock_rpc::MockRpc;
// use execution::state::State;
// use execution::ExecutionClient;
// use tokio::sync::mpsc::channel;
// use tokio::sync::watch;
//
// fn create_state() -> State {
// let (_, block_recv) = channel(256);
// let (_, finalized_recv) = watch::channel(None);
// State::new(block_recv, finalized_recv, 64)
// }
//
// fn create_client(state: State) -> ExecutionClient<MockRpc> {
// ExecutionClient::new("testdata/", state).unwrap()
// }
//
// #[tokio::test]
// async fn test_get_account() {
// let state = create_state();
//
// let address = address!("14f9D4aF749609c1438528C0Cce1cC3f6D411c47");
// let block = Block {
// state_root: b256!("aa02f5db2ee75e3da400d10f3c30e894b6016ce8a2501680380a907b6674ce0d"),
// ..Default::default()
// };
//
// state.push_block(block).await;
// let execution = create_client(state);
//
// let account = execution
// .get_account(address, None, BlockTag::Latest)
// .await
// .unwrap();
//
// assert_eq!(
// account.balance,
// U256::from_str_radix("48c27395000", 16).unwrap()
// );
// }
//
// #[tokio::test]
// async fn test_get_account_bad_proof() {
// let state = create_state();
//
// let address = address!("14f9D4aF749609c1438528C0Cce1cC3f6D411c47");
// let block = Block::default();
// state.push_block(block).await;
//
// let execution = create_client(state);
// let account_res = execution.get_account(address, None, BlockTag::Latest).await;
//
// assert!(account_res.is_err());
// }
//
// #[tokio::test]
// async fn test_get_tx() {
// let state = create_state();
Expand All @@ -73,21 +73,21 @@ async fn test_get_account_bad_proof() {
//
// assert_eq!(tx.hash(), tx_hash);
// }

#[tokio::test]
async fn test_get_tx_not_included() {
let state = create_state();
let tx_hash = b256!("2dac1b27ab58b493f902dda8b63979a112398d747f1761c0891777c0983e591f");

let block = Block::default();
state.push_block(block).await;

let execution = create_client(state);
let tx_res = execution.get_transaction(tx_hash).await;

assert!(tx_res.is_none());
}

//
// #[tokio::test]
// async fn test_get_tx_not_included() {
// let state = create_state();
// let tx_hash = b256!("2dac1b27ab58b493f902dda8b63979a112398d747f1761c0891777c0983e591f");
//
// let block = Block::default();
// state.push_block(block).await;
//
// let execution = create_client(state);
// let tx_res = execution.get_transaction(tx_hash).await;
//
// assert!(tx_res.is_none());
// }
//
// #[tokio::test]
// async fn test_get_logs() {
// let tx = Transaction::decode(&Rlp::new(&hex::decode("02f8b20583623355849502f900849502f91082ea6094326c977e6efc84e512bb9c30f76e30c160ed06fb80b844a9059cbb0000000000000000000000007daccf9b3c1ae2fa5c55f1c978aeef700bc83be0000000000000000000000000000000000000000000000001158e460913d00000c080a0e1445466b058b6f883c0222f1b1f3e2ad9bee7b5f688813d86e3fa8f93aa868ca0786d6e7f3aefa8fe73857c65c32e4884d8ba38d0ecfb947fbffb82e8ee80c167").unwrap())).unwrap();
Expand Down Expand Up @@ -169,32 +169,32 @@ async fn test_get_tx_not_included() {
// assert!(receipt_res.is_err());
// }

#[tokio::test]
async fn test_get_receipt_not_included() {
let state = create_state();
let execution = create_client(state);
let tx_hash = b256!("2dac1b27ab58b493f902dda8b63979a112398d747f1761c0891777c0983e591f");
let receipt_opt = execution.get_transaction_receipt(tx_hash).await.unwrap();

assert!(receipt_opt.is_none());
}

#[tokio::test]
async fn test_get_block() {
let block = Block {
number: U64::from(12345),
..Default::default()
};

let state = create_state();
state.push_block(block).await;
let execution = create_client(state);

let block = execution.get_block(BlockTag::Latest, false).await.unwrap();

assert_eq!(block.number.to::<u64>(), 12345);
}

// #[tokio::test]
// async fn test_get_receipt_not_included() {
// let state = create_state();
// let execution = create_client(state);
// let tx_hash = b256!("2dac1b27ab58b493f902dda8b63979a112398d747f1761c0891777c0983e591f");
// let receipt_opt = execution.get_transaction_receipt(tx_hash).await.unwrap();
//
// assert!(receipt_opt.is_none());
// }
//
// #[tokio::test]
// async fn test_get_block() {
// let block = Block {
// number: U64::from(12345),
// ..Default::default()
// };
//
// let state = create_state();
// state.push_block(block).await;
// let execution = create_client(state);
//
// let block = execution.get_block(BlockTag::Latest, false).await.unwrap();
//
// assert_eq!(block.number.to::<u64>(), 12345);
// }
//
// #[tokio::test]
// async fn test_get_tx_by_block_hash_and_index() {
// let tx = Transaction::decode(&Rlp::new(&hex::decode("02f8b20583623355849502f900849502f91082ea6094326c977e6efc84e512bb9c30f76e30c160ed06fb80b844a9059cbb0000000000000000000000007daccf9b3c1ae2fa5c55f1c978aeef700bc83be0000000000000000000000000000000000000000000000001158e460913d00000c080a0e1445466b058b6f883c0222f1b1f3e2ad9bee7b5f688813d86e3fa8f93aa868ca0786d6e7f3aefa8fe73857c65c32e4884d8ba38d0ecfb947fbffb82e8ee80c167").unwrap())).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion ethereum/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl EthereumClientBuilder {
} else if let Some(config) = &self.config {
config.rpc_bind_ip
} else {
None
Some(base_config.rpc_bind_ip)
};

#[cfg(not(target_arch = "wasm32"))]
Expand Down
13 changes: 10 additions & 3 deletions ethereum/src/config/checkpoints.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::collections::HashMap;
use std::{collections::HashMap, time::Duration};

use alloy::primitives::B256;
use eyre::Result;
use reqwest::ClientBuilder;
use retri::{retry, BackoffSettings};
use serde::{
de::{self, Error},
Expand Down Expand Up @@ -81,7 +82,13 @@ pub struct CheckpointFallback {

async fn get(req: &str) -> Result<reqwest::Response> {
retry(
|| async { Ok::<_, eyre::Report>(reqwest::get(req).await?) },
|| async {
let client = ClientBuilder::new()
.timeout(Duration::from_secs(1))
.build()
.unwrap();
Ok::<_, eyre::Report>(client.get(req).send().await?)
},
BackoffSettings::default(),
)
.await
Expand Down Expand Up @@ -229,7 +236,7 @@ impl CheckpointFallback {
/// This is an associated function and can be used like so:
///
/// ```rust
/// use config::CheckpointFallback;
/// use helios_ethereum::config::checkpoints::CheckpointFallback;
///
/// let url = CheckpointFallback::construct_url("https://sync-mainnet.beaconcha.in");
/// assert_eq!("https://sync-mainnet.beaconcha.in/checkpointz/v1/beacon/slots", url);
Expand Down
Loading

0 comments on commit dd40137

Please sign in to comment.