Skip to content

Commit 4e005ab

Browse files
committed
impl create_access_list in Evm
1 parent 10090e7 commit 4e005ab

File tree

15 files changed

+238
-283
lines changed

15 files changed

+238
-283
lines changed

common/src/types.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
1-
use std::collections::HashMap;
21
use std::fmt::Display;
32

43
use alloy::{
4+
consensus::Account as TrieAccount,
55
eips::{BlockId, BlockNumberOrTag},
66
primitives::{Bytes, B256, U256},
7+
rpc::types::EIP1186StorageProof,
78
};
89
use eyre::{eyre, Report, Result};
910
use serde::{de::Error, Deserialize, Serialize};
1011

1112
#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
13+
#[serde(rename_all = "camelCase")]
1214
pub struct Account {
13-
pub balance: U256,
14-
pub nonce: u64,
15-
pub code_hash: B256,
15+
pub account: TrieAccount,
16+
#[serde(skip_serializing_if = "Option::is_none")]
1617
pub code: Option<Bytes>,
17-
pub storage_hash: B256,
18-
pub slots: HashMap<B256, U256>,
18+
pub account_proof: Vec<Bytes>,
19+
pub storage_proof: Vec<EIP1186StorageProof>,
20+
}
21+
22+
impl Account {
23+
/// Retrieve the value at the given storage slot.
24+
pub fn get_storage_value(&self, slot: B256) -> Option<U256> {
25+
self.storage_proof
26+
.iter()
27+
.find_map(|EIP1186StorageProof { key, value, .. }| {
28+
if key.as_b256() == slot {
29+
Some(*value)
30+
} else {
31+
None
32+
}
33+
})
34+
}
1935
}
2036

2137
#[derive(Debug, Clone, Copy)]

core/src/client/node.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<N: NetworkSpec, C: Consensus<N::BlockResponse>> Node<N, C> {
9595
.execution
9696
.get_account(address, None, tag, false)
9797
.await?;
98-
Ok(account.balance)
98+
Ok(account.account.balance)
9999
}
100100

101101
pub async fn get_nonce(&self, address: Address, tag: BlockTag) -> Result<u64> {
@@ -105,7 +105,7 @@ impl<N: NetworkSpec, C: Consensus<N::BlockResponse>> Node<N, C> {
105105
.execution
106106
.get_account(address, None, tag, false)
107107
.await?;
108-
Ok(account.nonce)
108+
Ok(account.account.nonce)
109109
}
110110

111111
pub async fn get_block_transaction_count_by_hash(&self, hash: B256) -> Result<Option<u64>> {

core/src/execution/client/rpc.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::{HashMap, HashSet};
22

3-
use alloy::consensus::BlockHeader;
3+
use alloy::consensus::{Account as TrieAccount, BlockHeader};
44
use alloy::eips::BlockId;
55
use alloy::network::{BlockResponse, ReceiptResponse, TransactionBuilder};
66
use alloy::primitives::{Address, B256, U256};
@@ -66,7 +66,7 @@ impl<N: NetworkSpec, R: ExecutionRpc<N>> ExecutionSpec<N> for ExecutionInnerRpcC
6666
.get_proof(address, slots, block.header().number().into())
6767
.await?;
6868

69-
self.verify_proof_to_account(&proof, &block, include_code)
69+
self.verify_proof_to_account(proof, &block, include_code)
7070
.await
7171
}
7272

@@ -243,34 +243,37 @@ impl<N: NetworkSpec, R: ExecutionRpc<N>> ExecutionSpec<N> for ExecutionInnerRpcC
243243
impl<N: NetworkSpec, R: ExecutionRpc<N>> ExecutionInnerRpcClient<N, R> {
244244
async fn verify_proof_to_account(
245245
&self,
246-
proof: &EIP1186AccountProofResponse,
246+
proof: EIP1186AccountProofResponse,
247247
block: &N::BlockResponse,
248248
verify_code: bool,
249249
) -> Result<Account> {
250250
// Verify the account proof
251-
verify_account_proof(proof, block.header().state_root())?;
252-
// Verify the storage proofs, collecting the slot values
253-
let slot_map = verify_storage_proof(proof)?;
251+
verify_account_proof(&proof, block.header().state_root())?;
252+
// Verify the storage proofs
253+
verify_storage_proof(&proof)?;
254254
// Verify the code hash
255255
let code = if verify_code {
256256
let code = self
257257
.rpc
258258
.get_code(proof.address, block.header().number().into())
259259
.await?
260260
.into();
261-
verify_code_hash_proof(proof, &code)?;
261+
verify_code_hash_proof(&proof, &code)?;
262262
Some(code)
263263
} else {
264264
None
265265
};
266266

267267
Ok(Account {
268-
balance: proof.balance,
269-
nonce: proof.nonce,
270-
code_hash: proof.code_hash,
268+
account: TrieAccount {
269+
nonce: proof.nonce,
270+
balance: proof.balance,
271+
storage_root: proof.storage_hash,
272+
code_hash: proof.code_hash,
273+
},
271274
code,
272-
storage_hash: proof.storage_hash,
273-
slots: slot_map,
275+
account_proof: proof.account_proof,
276+
storage_proof: proof.storage_proof,
274277
})
275278
}
276279

core/src/execution/client/verifiable_api.rs

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ impl<N: NetworkSpec, A: VerifiableApi<N>> ExecutionSpec<N>
6464
.map(|s| (*s).into())
6565
.collect::<Vec<_>>();
6666

67-
let account_response = self
67+
let account = self
6868
.api
6969
.get_account(address, &slots, Some(block_id), include_code)
7070
.await?;
7171

72-
self.verify_account_response(address, account_response, &block)
72+
self.verify_account(address, &account, &block)?;
73+
74+
Ok(account)
7375
}
7476

7577
async fn get_transaction_receipt(&self, tx_hash: B256) -> Result<Option<N::ReceiptResponse>> {
@@ -138,15 +140,11 @@ impl<N: NetworkSpec, A: VerifiableApi<N>> ExecutionSpec<N>
138140
.create_access_list(tx.clone(), Some(block_id))
139141
.await?;
140142

141-
let account_map = accounts
142-
.into_iter()
143-
.map(|(address, account_response)| {
144-
self.verify_account_response(address, account_response, &block)
145-
.map(|account| (address, account))
146-
})
147-
.collect::<Result<HashMap<_, _>>>()?;
143+
for (address, account) in &accounts {
144+
self.verify_account(*address, account, &block)?;
145+
}
148146

149-
Ok(account_map)
147+
Ok(accounts)
150148
}
151149

152150
async fn chain_id(&self) -> Result<u64> {
@@ -196,42 +194,31 @@ impl<N: NetworkSpec, A: VerifiableApi<N>> ExecutionSpec<N>
196194
}
197195

198196
impl<N: NetworkSpec, A: VerifiableApi<N>> ExecutionInnerVerifiableApiClient<N, A> {
199-
fn verify_account_response(
197+
fn verify_account(
200198
&self,
201199
address: Address,
202-
account: AccountResponse,
200+
account: &AccountResponse,
203201
block: &N::BlockResponse,
204-
) -> Result<Account> {
202+
) -> Result<()> {
205203
let proof = EIP1186AccountProofResponse {
206204
address,
207205
balance: account.account.balance,
208206
code_hash: account.account.code_hash,
209207
nonce: account.account.nonce,
210208
storage_hash: account.account.storage_root,
211-
account_proof: account.account_proof,
212-
storage_proof: account.storage_proof,
209+
account_proof: account.account_proof.clone(),
210+
storage_proof: account.storage_proof.clone(),
213211
};
214212
// Verify the account proof
215213
verify_account_proof(&proof, block.header().state_root())?;
216-
// Verify the storage proofs, collecting the slot values
217-
let slot_map = verify_storage_proof(&proof)?;
214+
// Verify the storage proofs
215+
verify_storage_proof(&proof)?;
218216
// Verify the code hash (if code is included in the response)
219-
let code = match account.code {
220-
Some(code) => {
221-
verify_code_hash_proof(&proof, &code)?;
222-
Some(code)
223-
}
224-
None => None,
225-
};
217+
if let Some(code) = &account.code {
218+
verify_code_hash_proof(&proof, code)?;
219+
}
226220

227-
Ok(Account {
228-
balance: proof.balance,
229-
nonce: proof.nonce,
230-
code_hash: proof.code_hash,
231-
code,
232-
storage_hash: proof.storage_hash,
233-
slots: slot_map,
234-
})
221+
Ok(())
235222
}
236223

237224
async fn verify_logs_and_receipts(

0 commit comments

Comments
 (0)