Skip to content

Commit

Permalink
Mock execution API for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
pkakelas committed Dec 1, 2023
1 parent 2aeade1 commit 2b06219
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 8,203 deletions.
51 changes: 32 additions & 19 deletions feeder/src/eth/execution.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
use std::str::FromStr;

use async_trait::async_trait;
use ethers::prelude::Http;
use ethers::providers::{FilterKind, HttpRateLimitRetryPolicy, Middleware, Provider, RetryClient};
use ethers::types::{Block, Filter, Log, Transaction, TransactionReceipt, H256, U256, U64};
use eyre::Result;

use crate::error::RpcError;

#[async_trait]
pub trait ExecutionAPI {
async fn get_transaction_receipt(&self, tx_hash: &H256) -> Result<Option<TransactionReceipt>>;
async fn get_block_receipts(&self, block_number: u64) -> Result<Vec<TransactionReceipt>>;
async fn get_block(&self, block_number: u64) -> Result<Option<Block<H256>>>;
async fn get_block_with_txs(&self, block_number: u64) -> Result<Option<Block<Transaction>>>;
async fn get_blocks(&self, block_numbers: &[u64]) -> Result<Vec<Option<Block<H256>>>>;
async fn get_latest_block_number(&self) -> Result<U64>;
async fn get_transaction(&self, tx_hash: &H256) -> Result<Option<Transaction>>;
async fn get_logs(&self, filter: &Filter) -> Result<Vec<Log>>;
async fn get_filter_changes(&self, filter_id: &U256) -> Result<Vec<Log>>;
async fn uninstall_filter(&self, filter_id: &U256) -> Result<bool>;
async fn get_new_filter(&self, filter: &Filter) -> Result<U256>;
async fn chain_id(&self) -> Result<u64>;
}

pub struct ExecutionRPC {
pub provider: Provider<RetryClient<Http>>,
}

#[allow(dead_code)]
impl ExecutionRPC {
pub fn new(rpc: &str) -> Self {
let http = Http::from_str(rpc).expect("Could not initialize HTTP provider");
Expand All @@ -22,11 +38,11 @@ impl ExecutionRPC {

ExecutionRPC { provider }
}
}

pub async fn get_transaction_receipt(
&self,
tx_hash: &H256,
) -> Result<Option<TransactionReceipt>> {
#[async_trait]
impl ExecutionAPI for ExecutionRPC {
async fn get_transaction_receipt(&self, tx_hash: &H256) -> Result<Option<TransactionReceipt>> {
let receipt = self
.provider
.get_transaction_receipt(*tx_hash)
Expand All @@ -36,7 +52,7 @@ impl ExecutionRPC {
Ok(receipt)
}

pub async fn get_block_receipts(&self, block_number: u64) -> Result<Vec<TransactionReceipt>> {
async fn get_block_receipts(&self, block_number: u64) -> Result<Vec<TransactionReceipt>> {
let block_receipts = self
.provider
.get_block_receipts(block_number)
Expand All @@ -46,7 +62,7 @@ impl ExecutionRPC {
Ok(block_receipts)
}

pub async fn get_block(&self, block_number: u64) -> Result<Option<Block<H256>>> {
async fn get_block(&self, block_number: u64) -> Result<Option<Block<H256>>> {
let block = self
.provider
.get_block(block_number)
Expand All @@ -56,10 +72,7 @@ impl ExecutionRPC {
Ok(block)
}

pub async fn get_block_with_txs(
&self,
block_number: u64,
) -> Result<Option<Block<Transaction>>> {
async fn get_block_with_txs(&self, block_number: u64) -> Result<Option<Block<Transaction>>> {
let block = self
.provider
.get_block_with_txs(block_number)
Expand All @@ -69,7 +82,7 @@ impl ExecutionRPC {
Ok(block)
}

pub async fn get_blocks(&self, block_numbers: &[u64]) -> Result<Vec<Option<Block<H256>>>> {
async fn get_blocks(&self, block_numbers: &[u64]) -> Result<Vec<Option<Block<H256>>>> {
let mut futures = vec![];
for &block_number in block_numbers {
futures.push(async move { self.get_block(block_number).await });
Expand All @@ -79,55 +92,55 @@ impl ExecutionRPC {
results
}

pub async fn get_latest_block_number(&self) -> Result<U64> {
async fn get_latest_block_number(&self) -> Result<U64> {
Ok(self
.provider
.get_block_number()
.await
.map_err(|e| RpcError::new("get_latest_block_number", e))?)
}

pub async fn get_transaction(&self, tx_hash: &H256) -> Result<Option<Transaction>> {
async fn get_transaction(&self, tx_hash: &H256) -> Result<Option<Transaction>> {
Ok(self
.provider
.get_transaction(*tx_hash)
.await
.map_err(|e| RpcError::new("get_transaction", e))?)
}

pub async fn get_logs(&self, filter: &Filter) -> Result<Vec<Log>> {
async fn get_logs(&self, filter: &Filter) -> Result<Vec<Log>> {
Ok(self
.provider
.get_logs(filter)
.await
.map_err(|e| RpcError::new("get_logs", e))?)
}

pub async fn get_filter_changes(&self, filter_id: &U256) -> Result<Vec<Log>> {
async fn get_filter_changes(&self, filter_id: &U256) -> Result<Vec<Log>> {
Ok(self
.provider
.get_filter_changes(filter_id)
.await
.map_err(|e| RpcError::new("get_filter_changes", e))?)
}

pub async fn uninstall_filter(&self, filter_id: &U256) -> Result<bool> {
async fn uninstall_filter(&self, filter_id: &U256) -> Result<bool> {
Ok(self
.provider
.uninstall_filter(filter_id)
.await
.map_err(|e| RpcError::new("uninstall_filter", e))?)
}

pub async fn get_new_filter(&self, filter: &Filter) -> Result<U256> {
async fn get_new_filter(&self, filter: &Filter) -> Result<U256> {
Ok(self
.provider
.new_filter(FilterKind::Logs(filter))
.await
.map_err(|e| RpcError::new("get_new_filter", e))?)
}

pub async fn chain_id(&self) -> Result<u64> {
async fn chain_id(&self) -> Result<u64> {
Ok(self
.provider
.get_chainid()
Expand Down
2 changes: 1 addition & 1 deletion feeder/src/eth/gateway.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::eth::constants::EXECUTION_RPC;
use crate::eth::execution::ExecutionRPC;
use crate::eth::execution::{ExecutionAPI, ExecutionRPC};
use crate::eth::utils::calc_slot_from_timestamp;
use crate::types::InternalMessage;
use consensus_types::lightclient::{CrossChainId, Message};
Expand Down
33 changes: 16 additions & 17 deletions feeder/src/prover/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,12 @@ mod tests {
use hasher::HasherKeccak;
use std::{fs::File, sync::Arc};
use sync_committee_rs::constants::Root;
use tokio::test as tokio_test;

use crate::prover::execution::generate_receipt_proof;

fn get_execution_block() -> Block<Transaction> {
let file = File::open("./src/prover/testdata/execution_block.json").unwrap();
serde_json::from_reader(file).unwrap()
}

fn get_receipts() -> Vec<TransactionReceipt> {
let file = File::open("./src/prover/testdata/receipts.json").unwrap();
serde_json::from_reader(file).unwrap()
}
use crate::{
eth::execution::ExecutionAPI,
prover::{execution::generate_receipt_proof, mocks::mock_execution_rpc::MockExecutionRPC},
};

fn verify_trie_proof(root: Root, key: u64, proof_bytes: Vec<Vec<u8>>) -> Option<Vec<u8>> {
let memdb = Arc::new(MemoryDB::new(true));
Expand All @@ -110,12 +104,17 @@ mod tests {
.unwrap()
}

#[test]
fn test_receipts_proof() {
let mut execution_block = get_execution_block();
let receipts = &get_receipts();

let proof = generate_receipt_proof(&mut execution_block, receipts, 1).unwrap();
#[tokio_test]
async fn test_receipts_proof() {
let execution_rpc = MockExecutionRPC::new();
let mut execution_block = execution_rpc
.get_block_with_txs(18615160)
.await
.unwrap()
.unwrap();
let receipts = execution_rpc.get_block_receipts(18615160).await.unwrap();

let proof = generate_receipt_proof(&mut execution_block, &receipts, 1).unwrap();
let bytes: Result<[u8; 32], _> = execution_block.receipts_root[0..32].try_into();
let root = Root::from_bytes(bytes.unwrap());

Expand Down
70 changes: 70 additions & 0 deletions feeder/src/prover/mocks/mock_execution_rpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use std::fs::File;

use crate::eth::execution::ExecutionAPI;
use async_trait::async_trait;
use ethers::types::{Block, Filter, Log, Transaction, TransactionReceipt, H256, U256, U64};
use eyre::Result;

pub struct MockExecutionRPC;

#[allow(dead_code)]
impl MockExecutionRPC {
pub fn new() -> Self {
MockExecutionRPC {}
}
}

#[async_trait]
impl ExecutionAPI for MockExecutionRPC {
async fn get_block_receipts(&self, block_number: u64) -> Result<Vec<TransactionReceipt>> {
let filename = format!(
"./src/prover/testdata/execution_blocks/receipts/{}.json",
block_number
);
let file = File::open(filename).unwrap();
let res: Vec<TransactionReceipt> = serde_json::from_reader(file).unwrap();
Ok(res)
}

async fn get_block_with_txs(&self, block_number: u64) -> Result<Option<Block<Transaction>>> {
let filename = format!(
"./src/prover/testdata/execution_blocks/{}.json",
block_number
);
println!("{}", filename);
let file = File::open(filename).unwrap();
let res: Option<Block<Transaction>> = Some(serde_json::from_reader(file).unwrap());
Ok(res)
}

async fn get_block(&self, block_number: u64) -> Result<Option<Block<H256>>> {
unimplemented!();
}
async fn get_transaction_receipt(&self, tx_hash: &H256) -> Result<Option<TransactionReceipt>> {
unimplemented!();
}
async fn get_blocks(&self, block_numbers: &[u64]) -> Result<Vec<Option<Block<H256>>>> {
unimplemented!();
}
async fn get_latest_block_number(&self) -> Result<U64> {
unimplemented!();
}
async fn get_transaction(&self, tx_hash: &H256) -> Result<Option<Transaction>> {
unimplemented!();
}
async fn get_logs(&self, filter: &Filter) -> Result<Vec<Log>> {
unimplemented!();
}
async fn get_filter_changes(&self, filter_id: &U256) -> Result<Vec<Log>> {
unimplemented!();
}
async fn uninstall_filter(&self, filter_id: &U256) -> Result<bool> {
unimplemented!();
}
async fn get_new_filter(&self, filter: &Filter) -> Result<U256> {
unimplemented!();
}
async fn chain_id(&self) -> Result<u64> {
unimplemented!();
}
}
1 change: 1 addition & 0 deletions feeder/src/prover/mocks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod mock_consensus_rpc;
pub mod mock_execution_rpc;
pub mod mock_state_prover;
4 changes: 2 additions & 2 deletions feeder/src/prover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ mod utils;
use crate::{
eth::{
consensus::{ConsensusRPC, EthBeaconAPI},
execution::ExecutionRPC,
state_prover::{StateProver},
execution::{ExecutionAPI, ExecutionRPC},
state_prover::StateProver,
utils::calc_slot_from_timestamp,
},
prover::{
Expand Down
File renamed without changes.
Loading

0 comments on commit 2b06219

Please sign in to comment.