2022-08-20 20:33:32 +00:00
|
|
|
use ethers::prelude::{Address, U256};
|
2022-08-20 17:18:40 +00:00
|
|
|
use ethers::utils::keccak256;
|
|
|
|
use eyre::Result;
|
|
|
|
|
|
|
|
use crate::consensus_rpc::ExecutionPayload;
|
|
|
|
use crate::execution_rpc::ExecutionRpc;
|
2022-08-20 20:33:32 +00:00
|
|
|
use crate::proof::{encode_account, verify_proof};
|
2022-08-20 17:18:40 +00:00
|
|
|
|
|
|
|
pub struct ExecutionClient {
|
2022-08-20 20:33:32 +00:00
|
|
|
execution_rpc: ExecutionRpc,
|
2022-08-20 17:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ExecutionClient {
|
|
|
|
pub fn new(rpc: &str) -> Self {
|
|
|
|
let execution_rpc = ExecutionRpc::new(rpc);
|
|
|
|
ExecutionClient { execution_rpc }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_balance(&self, account: &Address, payload: &ExecutionPayload) -> Result<U256> {
|
2022-08-20 20:33:32 +00:00
|
|
|
let proof = self
|
|
|
|
.execution_rpc
|
|
|
|
.get_proof(&account, payload.block_number)
|
|
|
|
.await?;
|
2022-08-20 17:18:40 +00:00
|
|
|
|
|
|
|
let account_path = keccak256(account.as_bytes()).to_vec();
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(proof.balance)
|
|
|
|
}
|
|
|
|
}
|