feat: evm efficiency improvements (#53)

* feat: evm efficiency improvements

* remove get_code tests
This commit is contained in:
Noah Citron 2022-09-23 19:40:01 -04:00 committed by GitHub
parent e2eb7a731a
commit 4a347e3b2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 54 deletions

View File

@ -113,7 +113,8 @@ impl Node {
pub async fn get_code(&self, address: &Address, block: &BlockTag) -> Result<Vec<u8>> { pub async fn get_code(&self, address: &Address, block: &BlockTag) -> Result<Vec<u8>> {
let payload = self.get_payload(block)?; let payload = self.get_payload(block)?;
self.execution.get_code(&address, payload).await let account = self.execution.get_account(&address, None, payload).await?;
Ok(account.code)
} }
pub async fn get_storage_at(&self, address: &Address, slot: H256) -> Result<U256> { pub async fn get_storage_at(&self, address: &Address, slot: H256) -> Result<U256> {

View File

@ -4,6 +4,7 @@ use bytes::Bytes;
use ethers::{ use ethers::{
abi::ethereum_types::BigEndianHash, abi::ethereum_types::BigEndianHash,
prelude::{Address, H160, H256, U256}, prelude::{Address, H160, H256, U256},
types::transaction::eip2930::AccessListItem,
}; };
use eyre::Result; use eyre::Result;
use futures::future::join_all; use futures::future::join_all;
@ -87,9 +88,28 @@ impl<R: Rpc> Evm<R> {
let handle = thread::spawn(move || { let handle = thread::spawn(move || {
let list_fut = rpc.create_access_list(&opts_moved, block_moved); let list_fut = rpc.create_access_list(&opts_moved, block_moved);
let runtime = Runtime::new()?; let runtime = Runtime::new()?;
let list = runtime.block_on(list_fut)?; let mut list = runtime.block_on(list_fut)?.0;
let account_futs = list.0.iter().map(|account| { let from_access_entry = AccessListItem {
address: opts_moved.from.unwrap_or_default(),
storage_keys: Vec::default(),
};
let to_access_entry = AccessListItem {
address: opts_moved.to,
storage_keys: Vec::default(),
};
let producer_account = AccessListItem {
address: Address::from_slice(&payload.fee_recipient),
storage_keys: Vec::default(),
};
list.push(from_access_entry);
list.push(to_access_entry);
list.push(producer_account);
let account_futs = list.iter().map(|account| {
let addr_fut = futures::future::ready(account.address); let addr_fut = futures::future::ready(account.address);
let account_fut = execution.get_account( let account_fut = execution.get_account(
&account.address, &account.address,

View File

@ -10,6 +10,7 @@ use eyre::Result;
use common::utils::hex_str_to_bytes; use common::utils::hex_str_to_bytes;
use consensus::types::ExecutionPayload; use consensus::types::ExecutionPayload;
use revm::KECCAK_EMPTY;
use triehash_ethereum::ordered_trie_root; use triehash_ethereum::ordered_trie_root;
use super::proof::{encode_account, verify_proof}; use super::proof::{encode_account, verify_proof};
@ -76,6 +77,9 @@ impl<R: Rpc> ExecutionClient<R> {
slot_map.insert(storage_proof.key, storage_proof.value); slot_map.insert(storage_proof.key, storage_proof.value);
} }
let code = if proof.code_hash == KECCAK_EMPTY {
Vec::new()
} else {
let code = self.rpc.get_code(address, payload.block_number).await?; let code = self.rpc.get_code(address, payload.block_number).await?;
let code_hash = keccak256(&code).into(); let code_hash = keccak256(&code).into();
@ -83,6 +87,9 @@ impl<R: Rpc> ExecutionClient<R> {
eyre::bail!("Invalid Proof"); eyre::bail!("Invalid Proof");
} }
code
};
Ok(Account { Ok(Account {
balance: proof.balance, balance: proof.balance,
nonce: proof.nonce.as_u64(), nonce: proof.nonce.as_u64(),
@ -93,19 +100,6 @@ impl<R: Rpc> ExecutionClient<R> {
}) })
} }
pub async fn get_code(&self, address: &Address, payload: &ExecutionPayload) -> Result<Vec<u8>> {
let account = self.get_account(address, None, payload).await?;
let code = self.rpc.get_code(address, payload.block_number).await?;
let code_hash = keccak256(&code).into();
if account.code_hash != code_hash {
eyre::bail!("Invalid Proof");
}
Ok(code)
}
pub async fn send_raw_transaction(&self, bytes: &Vec<u8>) -> Result<H256> { pub async fn send_raw_transaction(&self, bytes: &Vec<u8>) -> Result<H256> {
self.rpc.send_raw_transaction(bytes).await self.rpc.send_raw_transaction(bytes).await
} }

View File

@ -47,39 +47,6 @@ async fn test_get_account_bad_proof() {
assert!(account_res.is_err()); assert!(account_res.is_err());
} }
#[tokio::test]
async fn test_get_code() {
let execution = get_client();
let address = Address::from_str("14f9D4aF749609c1438528C0Cce1cC3f6D411c47").unwrap();
let mut payload = ExecutionPayload::default();
payload.state_root = Vector::from_iter(
hex_str_to_bytes("0xaa02f5db2ee75e3da400d10f3c30e894b6016ce8a2501680380a907b6674ce0d")
.unwrap(),
);
let code = execution.get_code(&address, &payload).await.unwrap();
let code_hash = keccak256(code);
assert_eq!(
code_hash.as_slice(),
&hex_str_to_bytes("0xc6ca0679d7242fa080596f2fe2e6b172d9b927a6b52278343826e33745854327")
.unwrap()
);
}
#[tokio::test]
async fn test_get_code_bad_proof() {
let execution = get_client();
let address = Address::from_str("14f9D4aF749609c1438528C0Cce1cC3f6D411c47").unwrap();
let payload = ExecutionPayload::default();
let code_res = execution.get_code(&address, &payload).await;
assert!(code_res.is_err());
}
#[tokio::test] #[tokio::test]
async fn test_get_tx() { async fn test_get_tx() {
let execution = get_client(); let execution = get_client();