Skip to content

Commit

Permalink
constants for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas089 committed Dec 9, 2024
1 parent f05fbbb commit 7f33231
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 75 deletions.
16 changes: 6 additions & 10 deletions prover/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,30 @@ fn main() {
mod tests {
use crate::MERKLE_ELF;
use sp1_sdk::{ProverClient, SP1Stdin};
use trie_utils::get_ethereum_transaction_proof_inputs;
use trie_utils::{constants::DEFAULT_BLOCK_HASH, get_ethereum_transaction_proof_inputs};
#[tokio::test]
async fn test_generate_transaction_zk_proof() {
sp1_sdk::utils::setup_logger();
let client = ProverClient::new();
let mut stdin = SP1Stdin::new();
let proof_input = serde_json::to_vec(
&get_ethereum_transaction_proof_inputs(
0u32,
"0x8230bd00f36e52e68dd4a46bfcddeceacbb689d808327f4c76dbdf8d33d58ca8",
)
.await,
&get_ethereum_transaction_proof_inputs(0u32, DEFAULT_BLOCK_HASH).await,
)
.unwrap();
stdin.write(&proof_input);
let (pk, vk) = client.setup(MERKLE_ELF);
let proof = client
.prove(&pk, stdin)
.run()
.expect("failed to generate proof");
.expect("Failed to generate proof!");
let transaction_hash = proof.public_values.to_vec();
println!(
"Successfully generated proof for Transaction: {:?}",
"[Success] Generated proof for Transaction: {:?}.",
transaction_hash
);
client.verify(&proof, &vk).expect("failed to verify proof");
client.verify(&proof, &vk).expect("Failed to verify proof!");
println!(
"Successfully verified proof for Transaction: {:?}",
"[Success] Verified proof for Transaction: {:?}.",
transaction_hash
);
}
Expand Down
9 changes: 9 additions & 0 deletions trie-utils/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub const NODE_RPC_URL: &'static str = "https://mainnet.infura.io/v3/";
// used for testing
pub const DEFAULT_BLOCK_HASH: &'static str =
"0x8230bd00f36e52e68dd4a46bfcddeceacbb689d808327f4c76dbdf8d33d58ca8";
// used for testing
pub const USDT_CONTRACT_ADDRESS: &'static str = "0xdAC17F958D2ee523a2206206994597C13D831ec7";
// used for testing
pub const DEFAULT_STORAGE_KEY: &'static str =
"0000000000000000000000000000000000000000000000000000000000000000";
1 change: 1 addition & 0 deletions trie-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ pub use proofs::{
account::get_ethereum_account_proof_inputs, receipt::get_ethereum_receipt_proof_inputs,
storage::get_ethereum_storage_proof_inputs, transaction::get_ethereum_transaction_proof_inputs,
};
pub mod constants;
6 changes: 3 additions & 3 deletions trie-utils/src/proofs/account.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::load_infura_key_from_env;
use crate::{constants::NODE_RPC_URL, load_infura_key_from_env};
use alloy::{
primitives::Address,
providers::{Provider, ProviderBuilder},
Expand All @@ -9,7 +9,7 @@ use url::Url;

pub async fn get_ethereum_account_proof_inputs(address: Address) -> MerkleProofInput {
let key = load_infura_key_from_env();
let rpc_url = "https://mainnet.infura.io/v3/".to_string() + &key;
let rpc_url = NODE_RPC_URL.to_string() + &key;
let provider = ProviderBuilder::new().on_http(Url::from_str(&rpc_url).unwrap());
let block = provider
.get_block(
Expand All @@ -22,7 +22,7 @@ pub async fn get_ethereum_account_proof_inputs(address: Address) -> MerkleProofI
let proof = provider
.get_proof(address, vec![])
.await
.expect("Failed to get proof");
.expect("Failed to get proof!");

MerkleProofInput {
proof: proof
Expand Down
7 changes: 3 additions & 4 deletions trie-utils/src/proofs/receipt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{load_infura_key_from_env, receipt::insert_receipt};
use crate::{constants::NODE_RPC_URL, load_infura_key_from_env, receipt::insert_receipt};
use alloy::{
consensus::ReceiptEnvelope,
primitives::B256,
Expand All @@ -16,8 +16,7 @@ pub async fn get_ethereum_receipt_proof_inputs(
block_hash: &str,
) -> MerkleProofInput {
let key = load_infura_key_from_env();
println!("Key: {}", key);
let rpc_url = "https://mainnet.infura.io/v3/".to_string() + &key;
let rpc_url = NODE_RPC_URL.to_string() + &key;
let provider = ProviderBuilder::new().on_http(Url::from_str(&rpc_url).unwrap());
let block_hash_b256 = B256::from_str(block_hash).unwrap();
let block = provider
Expand Down Expand Up @@ -63,7 +62,7 @@ pub async fn get_ethereum_receipt_proof_inputs(
insert_receipt(r, &mut trie, index_encoded, None);
}
_ => {
eprintln!("Critical: Unknown Receipt Type")
eprintln!("Unknown Receipt Type!")
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions trie-utils/src/proofs/storage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::load_infura_key_from_env;
use crate::{constants::NODE_RPC_URL, load_infura_key_from_env};
use alloy::{
primitives::{Address, FixedBytes},
providers::{Provider, ProviderBuilder},
Expand All @@ -12,7 +12,7 @@ pub async fn get_ethereum_storage_proof_inputs(
keys: Vec<FixedBytes<32>>,
) -> MerkleProofListInput {
let key = load_infura_key_from_env();
let rpc_url = "https://mainnet.infura.io/v3/".to_string() + &key;
let rpc_url = NODE_RPC_URL.to_string() + &key;
let provider = ProviderBuilder::new().on_http(Url::from_str(&rpc_url).unwrap());
let block = provider
.get_block(
Expand All @@ -25,7 +25,7 @@ pub async fn get_ethereum_storage_proof_inputs(
let proof = provider
.get_proof(address, keys)
.await
.expect("Failed to get proof");
.expect("Failed to get proof!");

MerkleProofListInput {
account_proof: proof
Expand Down
9 changes: 4 additions & 5 deletions trie-utils/src/proofs/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::load_infura_key_from_env;
use crate::{constants::NODE_RPC_URL, load_infura_key_from_env};
use alloy::{
consensus::TxEnvelope,
primitives::B256,
Expand All @@ -14,8 +14,7 @@ pub async fn get_ethereum_transaction_proof_inputs(
block_hash: &str,
) -> MerkleProofInput {
let key = load_infura_key_from_env();
println!("Key: {}", key);
let rpc_url = "https://mainnet.infura.io/v3/".to_string() + &key;
let rpc_url = NODE_RPC_URL.to_string() + &key;
let provider = ProviderBuilder::new().on_http(Url::from_str(&rpc_url).unwrap());
let block = provider
.get_block_by_hash(
Expand Down Expand Up @@ -45,9 +44,9 @@ pub async fn get_ethereum_transaction_proof_inputs(
TxEnvelope::Eip7702(tx) => {
tx.eip2718_encode(&mut encoded_tx);
}
_ => panic!("Unsupported transaction type"),
_ => panic!("Unsupported transaction type!"),
}
trie.insert(&path, &encoded_tx).expect("Failed to insert");
trie.insert(&path, &encoded_tx).expect("Failed to insert!");
}

trie.root_hash().unwrap();
Expand Down
3 changes: 1 addition & 2 deletions trie-utils/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::encode;
use alloy::primitives::Address;
use alloy_rlp::{Encodable, RlpEncodableWrapper};

use crate::encode;

pub struct Log {
pub address: Address,
pub topics: Vec<H256>,
Expand Down
29 changes: 12 additions & 17 deletions trie-utils/tests/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ mod test {
providers::{Provider, ProviderBuilder},
};
use crypto_ops::{keccak::digest_keccak, types::MerkleProofInput, verify_merkle_proof};
use trie_utils::{get_ethereum_account_proof_inputs, load_infura_key_from_env};
use trie_utils::{
constants::{DEFAULT_STORAGE_KEY, NODE_RPC_URL, USDT_CONTRACT_ADDRESS},
get_ethereum_account_proof_inputs, load_infura_key_from_env,
};
use url::Url;

/// This test could fail in case a new block is produced during execution.
Expand All @@ -19,7 +22,7 @@ mod test {
#[tokio::test]
async fn test_verify_account_and_storage_proof() {
let key = load_infura_key_from_env();
let rpc_url = "https://mainnet.infura.io/v3/".to_string() + &key;
let rpc_url = NODE_RPC_URL.to_string() + &key;
let provider = ProviderBuilder::new().on_http(Url::from_str(&rpc_url).unwrap());
let block = provider
.get_block(
Expand All @@ -31,24 +34,19 @@ mod test {
.unwrap();
let proof = provider
.get_proof(
Address::from_hex("0xdAC17F958D2ee523a2206206994597C13D831ec7").unwrap(),
vec![FixedBytes::from_hex(
"0000000000000000000000000000000000000000000000000000000000000000",
)
.unwrap()],
Address::from_hex(USDT_CONTRACT_ADDRESS).unwrap(),
vec![FixedBytes::from_hex(DEFAULT_STORAGE_KEY).unwrap()],
)
.await
.expect("Failed to get proof");

let inputs: MerkleProofInput = get_ethereum_account_proof_inputs(
Address::from_hex("0xdAC17F958D2ee523a2206206994597C13D831ec7").unwrap(),
)
.await;
println!("Proof: {:?}", &proof);
let inputs: MerkleProofInput =
get_ethereum_account_proof_inputs(Address::from_hex(USDT_CONTRACT_ADDRESS).unwrap())
.await;
let account_proof: Vec<u8> = verify_merkle_proof(
block.header.state_root,
inputs.proof,
&digest_keccak(&hex::decode("0xdAC17F958D2ee523a2206206994597C13D831ec7").unwrap()),
&digest_keccak(&hex::decode(USDT_CONTRACT_ADDRESS).unwrap()),
);
let decoded_account: Account = alloy_rlp::decode_exact(&account_proof).unwrap();
assert_eq!(
Expand All @@ -66,10 +64,7 @@ mod test {
.into_iter()
.map(|b| b.to_vec())
.collect(),
&digest_keccak(
&hex::decode("0000000000000000000000000000000000000000000000000000000000000000")
.unwrap(),
),
&digest_keccak(&hex::decode(DEFAULT_STORAGE_KEY).unwrap()),
);
println!("[Success] Verified Account Proof against Block Root")
}
Expand Down
19 changes: 9 additions & 10 deletions trie-utils/tests/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,36 @@ mod tests {
};
use crypto_ops::verify_merkle_proof;
use std::str::FromStr;
use trie_utils::{get_ethereum_receipt_proof_inputs, load_infura_key_from_env};
use trie_utils::{
constants::{DEFAULT_BLOCK_HASH, NODE_RPC_URL},
get_ethereum_receipt_proof_inputs, load_infura_key_from_env,
};
use url::Url;

#[tokio::test]
async fn test_get_and_verify_ethereum_receipt_merkle_proof() {
let key = load_infura_key_from_env();
let rpc_url: String = "https://mainnet.infura.io/v3/".to_string() + &key;
let rpc_url: String = NODE_RPC_URL.to_string() + &key;
let provider = ProviderBuilder::new().on_http(Url::from_str(&rpc_url).unwrap());
let block_hash = "0x8230bd00f36e52e68dd4a46bfcddeceacbb689d808327f4c76dbdf8d33d58ca8";
let target_index: u32 = 0u32;
let inputs: crypto_ops::types::MerkleProofInput =
get_ethereum_receipt_proof_inputs(target_index, block_hash).await;
get_ethereum_receipt_proof_inputs(target_index, DEFAULT_BLOCK_HASH).await;

let block = provider
.get_block_by_hash(
B256::from_str(block_hash).unwrap(),
B256::from_str(DEFAULT_BLOCK_HASH).unwrap(),
alloy::rpc::types::BlockTransactionsKind::Full,
)
.await
.expect("Failed to get Block!")
.expect("Block not found!");

let transaction = verify_merkle_proof(
let _ = verify_merkle_proof(
block.header.receipts_root,
inputs.proof,
&alloy_rlp::encode(target_index),
);

println!(
"[Success] Verified {:?} against {:?}.",
&transaction, &block.header.receipts_root
);
println!("[Success] Verified Transaction against Receipts Root.",);
}
}
23 changes: 10 additions & 13 deletions trie-utils/tests/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ mod tests {
};
use crypto_ops::{keccak::digest_keccak, verify_merkle_proof};
use std::str::FromStr;
use trie_utils::{get_ethereum_storage_proof_inputs, load_infura_key_from_env};
use trie_utils::{
constants::{DEFAULT_STORAGE_KEY, NODE_RPC_URL, USDT_CONTRACT_ADDRESS},
get_ethereum_storage_proof_inputs, load_infura_key_from_env,
};
use url::Url;

/// This test could fail in case a new block is produced during execution.
Expand All @@ -19,7 +22,7 @@ mod tests {
#[tokio::test]
async fn test_verify_account_and_storage_proof() {
let key = load_infura_key_from_env();
let rpc_url = "https://mainnet.infura.io/v3/".to_string() + &key;
let rpc_url = NODE_RPC_URL.to_string() + &key;
let provider = ProviderBuilder::new().on_http(Url::from_str(&rpc_url).unwrap());
let block = provider
.get_block(
Expand All @@ -31,11 +34,8 @@ mod tests {
.unwrap();
let proof = provider
.get_proof(
Address::from_hex("0xdAC17F958D2ee523a2206206994597C13D831ec7").unwrap(),
vec![FixedBytes::from_hex(
"0000000000000000000000000000000000000000000000000000000000000000",
)
.unwrap()],
Address::from_hex(USDT_CONTRACT_ADDRESS).unwrap(),
vec![FixedBytes::from_hex(DEFAULT_STORAGE_KEY).unwrap()],
)
.await
.expect("Failed to get proof");
Expand All @@ -47,19 +47,16 @@ mod tests {
.into_iter()
.map(|b| b.to_vec())
.collect(),
&digest_keccak(&hex::decode("0xdAC17F958D2ee523a2206206994597C13D831ec7").unwrap()),
&digest_keccak(&hex::decode(USDT_CONTRACT_ADDRESS).unwrap()),
);
let decoded_account: Account = alloy_rlp::decode_exact(&account_proof).unwrap();
assert_eq!(
decoded_account.storage_root.encode_hex::<String>(),
hex::encode(&proof.storage_hash)
);
let storage_proof = get_ethereum_storage_proof_inputs(
Address::from_hex("0xdAC17F958D2ee523a2206206994597C13D831ec7").unwrap(),
vec![FixedBytes::from_hex(
"0000000000000000000000000000000000000000000000000000000000000000",
)
.unwrap()],
Address::from_hex(USDT_CONTRACT_ADDRESS).unwrap(),
vec![FixedBytes::from_hex(DEFAULT_STORAGE_KEY).unwrap()],
)
.await;
let _ = verify_merkle_proof(
Expand Down
16 changes: 8 additions & 8 deletions trie-utils/tests/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ mod tests {
};
use crypto_ops::verify_merkle_proof;
use std::str::FromStr;
use trie_utils::{get_ethereum_transaction_proof_inputs, load_infura_key_from_env};
use trie_utils::{
constants::{DEFAULT_BLOCK_HASH, NODE_RPC_URL},
get_ethereum_transaction_proof_inputs, load_infura_key_from_env,
};
use url::Url;

#[tokio::test]
async fn test_get_and_verify_ethereum_transaction_merkle_proof() {
let key = load_infura_key_from_env();
let rpc_url: String = "https://mainnet.infura.io/v3/".to_string() + &key;
let rpc_url: String = NODE_RPC_URL.to_string() + &key;
let provider = ProviderBuilder::new().on_http(Url::from_str(&rpc_url).unwrap());
let block_hash = "0x8230bd00f36e52e68dd4a46bfcddeceacbb689d808327f4c76dbdf8d33d58ca8";
let block_hash = DEFAULT_BLOCK_HASH;
let target_index: u32 = 15u32;
let inputs: crypto_ops::types::MerkleProofInput =
get_ethereum_transaction_proof_inputs(target_index, block_hash).await;
Expand All @@ -26,14 +29,11 @@ mod tests {
.await
.expect("Failed to get Block!")
.expect("Block not found!");
let transaction = verify_merkle_proof(
let _ = verify_merkle_proof(
block.header.transactions_root,
inputs.proof,
&alloy_rlp::encode(target_index),
);
println!(
"[Success] Verified {:?} against {:?}.",
&transaction, &block.header.transactions_root
);
println!("[Success] Verified Transaction against Transactions Root.",);
}
}

0 comments on commit 7f33231

Please sign in to comment.