helios/src/execution/execution.rs

54 lines
1.3 KiB
Rust
Raw Normal View History

2022-08-21 13:13:56 +00:00
use ethers::prelude::{Address, U256, H256};
2022-08-20 17:18:40 +00:00
use ethers::utils::keccak256;
use eyre::Result;
2022-08-21 15:21:50 +00:00
use crate::consensus::types::ExecutionPayload;
use super::proof::{encode_account, verify_proof};
2022-08-21 16:27:19 +00:00
use super::rpc::Rpc;
2022-08-20 17:18:40 +00:00
pub struct ExecutionClient {
2022-08-21 16:27:19 +00:00
rpc: Rpc,
2022-08-20 17:18:40 +00:00
}
impl ExecutionClient {
pub fn new(rpc: &str) -> Self {
2022-08-21 16:27:19 +00:00
let rpc = Rpc::new(rpc);
ExecutionClient { rpc }
2022-08-20 17:18:40 +00:00
}
2022-08-21 13:13:56 +00:00
pub async fn get_account(&self, address: &Address, payload: &ExecutionPayload) -> Result<Account> {
2022-08-20 20:33:32 +00:00
let proof = self
2022-08-21 16:27:19 +00:00
.rpc
2022-08-21 13:13:56 +00:00
.get_proof(&address, payload.block_number)
2022-08-20 20:33:32 +00:00
.await?;
2022-08-20 17:18:40 +00:00
2022-08-21 13:13:56 +00:00
let account_path = keccak256(address.as_bytes()).to_vec();
2022-08-20 17:18:40 +00:00
let account_encoded = encode_account(&proof);
2022-08-20 20:33:32 +00:00
let is_valid = verify_proof(
&proof.account_proof,
&payload.state_root,
&account_path,
&account_encoded,
);
2022-08-20 17:18:40 +00:00
if !is_valid {
eyre::bail!("Invalid Proof");
}
2022-08-21 13:13:56 +00:00
Ok(Account {
balance: proof.balance,
nonce: proof.nonce,
code_hash: proof.code_hash,
storage_hash: proof.storage_hash,
})
2022-08-20 17:18:40 +00:00
}
}
2022-08-21 13:13:56 +00:00
pub struct Account {
pub balance: U256,
pub nonce: U256,
pub code_hash: H256,
pub storage_hash: H256,
}