Skip to content

Commit 7180932

Browse files
committed
address review comments
1 parent 099f993 commit 7180932

File tree

16 files changed

+192
-118
lines changed

16 files changed

+192
-118
lines changed

common/src/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ pub enum BlockTag {
4141
Number(u64),
4242
}
4343

44+
impl From<u64> for BlockTag {
45+
fn from(num: u64) -> Self {
46+
BlockTag::Number(num)
47+
}
48+
}
49+
4450
impl Display for BlockTag {
4551
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4652
let formatted = match self {

core/src/client/node.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ impl<N: NetworkSpec, C: Consensus<N::BlockResponse>> Node<N, C> {
4848
)
4949
.map_err(ClientError::InternalError)?;
5050
let execution = Arc::new(
51-
ExecutionClient::new(client_inner.clone(), state.clone(), fork_schedule)
51+
ExecutionClient::new(client_inner, state.clone(), fork_schedule)
5252
.map_err(ClientError::InternalError)?,
5353
);
5454

55-
start_state_updater(state, client_inner, block_recv, finalized_block_recv);
55+
start_state_updater(state, execution.clone(), block_recv, finalized_block_recv);
5656

5757
Ok(Node {
5858
consensus,
@@ -153,7 +153,9 @@ impl<N: NetworkSpec, C: Consensus<N::BlockResponse>> Node<N, C> {
153153
self.check_blocktag_age(&tag).await?;
154154

155155
let account = self.execution.get_account(address, None, tag, true).await?;
156-
Ok(account.code.unwrap())
156+
account
157+
.code
158+
.ok_or(eyre!("Failed to fetch code for address"))
157159
}
158160

159161
pub async fn get_storage_at(

core/src/execution/client/rpc.rs

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use crate::execution::constants::{
2121
};
2222
use crate::execution::errors::ExecutionError;
2323
use crate::execution::proof::{
24-
ordered_trie_root_noop_encoder, verify_account_proof, verify_code_hash_proof,
25-
verify_storage_proof,
24+
ordered_trie_root_noop_encoder, verify_account_proof, verify_block_receipts,
25+
verify_code_hash_proof, verify_storage_proof,
2626
};
2727
use crate::execution::rpc::ExecutionRpc;
2828
use crate::execution::state::State;
@@ -66,8 +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)
70-
.await
69+
self.verify_account_proof(proof, &block, include_code).await
7170
}
7271

7372
async fn get_transaction_receipt(&self, tx_hash: B256) -> Result<Option<N::ReceiptResponse>> {
@@ -88,7 +87,7 @@ impl<N: NetworkSpec, R: ExecutionRpc<N>> ExecutionSpec<N> for ExecutionInnerRpcC
8887
.rpc
8988
.get_block_receipts(block_id)
9089
.await?
91-
.ok_or(eyre::eyre!(ExecutionError::NoReceiptsForBlock(tag)))?;
90+
.ok_or(ExecutionError::NoReceiptsForBlock(tag))?;
9291

9392
let receipts_encoded = receipts.iter().map(N::encode_receipt).collect::<Vec<_>>();
9493
let expected_receipt_root = ordered_trie_root_noop_encoder(&receipts_encoded);
@@ -136,7 +135,7 @@ impl<N: NetworkSpec, R: ExecutionRpc<N>> ExecutionSpec<N> for ExecutionInnerRpcC
136135
Ok(logs)
137136
}
138137

139-
async fn create_access_list(
138+
async fn create_extended_access_list(
140139
&self,
141140
tx: &N::TransactionRequest,
142141
block_id: Option<BlockId>,
@@ -208,6 +207,14 @@ impl<N: NetworkSpec, R: ExecutionRpc<N>> ExecutionSpec<N> for ExecutionInnerRpcC
208207
&self,
209208
block_id: BlockId,
210209
full_tx: bool,
210+
) -> Result<Option<N::BlockResponse>> {
211+
self.state.get_block_by_id(block_id, full_tx).await
212+
}
213+
214+
async fn get_untrusted_block(
215+
&self,
216+
block_id: BlockId,
217+
full_tx: bool,
211218
) -> Result<Option<N::BlockResponse>> {
212219
self.rpc.get_block(block_id, full_tx.into()).await
213220
}
@@ -216,7 +223,21 @@ impl<N: NetworkSpec, R: ExecutionRpc<N>> ExecutionSpec<N> for ExecutionInnerRpcC
216223
&self,
217224
block_id: BlockId,
218225
) -> Result<Option<Vec<N::ReceiptResponse>>> {
219-
self.rpc.get_block_receipts(block_id).await
226+
let block = self.state.get_block_by_id(block_id, false).await?;
227+
let Some(block) = block else {
228+
return Ok(None);
229+
};
230+
let block_num = block.header().number();
231+
232+
let receipts = self
233+
.rpc
234+
.get_block_receipts(block_num.into())
235+
.await?
236+
.ok_or(ExecutionError::NoReceiptsForBlock(block_num.into()))?;
237+
238+
verify_block_receipts::<N>(&receipts, &block)?;
239+
240+
Ok(Some(receipts))
220241
}
221242

222243
async fn send_raw_transaction(&self, bytes: &[u8]) -> Result<B256> {
@@ -241,7 +262,7 @@ impl<N: NetworkSpec, R: ExecutionRpc<N>> ExecutionSpec<N> for ExecutionInnerRpcC
241262
}
242263

243264
impl<N: NetworkSpec, R: ExecutionRpc<N>> ExecutionInnerRpcClient<N, R> {
244-
async fn verify_proof_to_account(
265+
async fn verify_account_proof(
245266
&self,
246267
proof: EIP1186AccountProofResponse,
247268
block: &N::BlockResponse,
@@ -302,10 +323,9 @@ impl<N: NetworkSpec, R: ExecutionRpc<N>> ExecutionInnerRpcClient<N, R> {
302323

303324
// Collect all (proven) tx receipts for all block numbers
304325
let blocks_receipts_fut = block_nums.into_iter().map(|block_num| async move {
305-
let tag = BlockTag::Number(block_num);
306-
// ToDo(@eshaan7): use verified version of `get_block_receipts`
307-
let receipts = self.rpc.get_block_receipts(block_num.into()).await;
308-
receipts?.ok_or_else(|| eyre::eyre!(ExecutionError::NoReceiptsForBlock(tag)))
326+
let receipts = self.get_block_receipts(block_num.into()).await;
327+
receipts?
328+
.ok_or_else(|| eyre::eyre!(ExecutionError::NoReceiptsForBlock(block_num.into())))
309329
});
310330
let blocks_receipts = try_join_all(blocks_receipts_fut).await?;
311331
let receipts = blocks_receipts.into_iter().flatten().collect::<Vec<_>>();
@@ -504,7 +524,7 @@ mod tests {
504524
}
505525

506526
#[tokio::test]
507-
async fn test_create_access_list() {
527+
async fn test_create_extended_access_list() {
508528
let client = get_client().await;
509529
let address = rpc_proof().address;
510530
let block_beneficiary = rpc_block().header.beneficiary;
@@ -514,7 +534,7 @@ mod tests {
514534
.value(U256::ZERO);
515535

516536
let response = client
517-
.create_access_list(&tx, BlockId::latest().into())
537+
.create_extended_access_list(&tx, BlockId::latest().into())
518538
.await
519539
.unwrap();
520540

@@ -548,6 +568,19 @@ mod tests {
548568
assert_eq!(response, rpc_block());
549569
}
550570

571+
#[tokio::test]
572+
async fn test_get_untrusted_block() {
573+
let client = get_client().await;
574+
575+
let response = client
576+
.get_untrusted_block(BlockId::latest(), false)
577+
.await
578+
.unwrap()
579+
.unwrap();
580+
581+
assert_eq!(response, rpc_block());
582+
}
583+
551584
#[tokio::test]
552585
async fn test_get_block_receipts() {
553586
let client = get_client().await;

core/src/execution/client/verifiable_api.rs

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use helios_verifiable_api_client::{types::*, VerifiableApi};
1717

1818
use crate::execution::errors::ExecutionError;
1919
use crate::execution::proof::{
20-
verify_account_proof, verify_code_hash_proof, verify_receipt_proof, verify_storage_proof,
20+
verify_account_proof, verify_block_receipts, verify_code_hash_proof, verify_receipt_proof,
21+
verify_storage_proof,
2122
};
2223
use crate::execution::state::State;
2324

@@ -121,7 +122,7 @@ impl<N: NetworkSpec, A: VerifiableApi<N>> ExecutionSpec<N>
121122
Ok(logs)
122123
}
123124

124-
async fn create_access_list(
125+
async fn create_extended_access_list(
125126
&self,
126127
tx: &N::TransactionRequest,
127128
block_id: Option<BlockId>,
@@ -135,9 +136,9 @@ impl<N: NetworkSpec, A: VerifiableApi<N>> ExecutionSpec<N>
135136
.ok_or(ExecutionError::BlockNotFound(tag))?;
136137
let block_id = BlockId::Number(block.header().number().into());
137138

138-
let AccessListResponse { accounts } = self
139+
let ExtendedAccessListResponse { accounts } = self
139140
.api
140-
.create_access_list(tx.clone(), Some(block_id))
141+
.create_extended_access_list(tx.clone(), Some(block_id))
141142
.await?;
142143

143144
for (address, account) in &accounts {
@@ -156,6 +157,14 @@ impl<N: NetworkSpec, A: VerifiableApi<N>> ExecutionSpec<N>
156157
&self,
157158
block_id: BlockId,
158159
full_tx: bool,
160+
) -> Result<Option<N::BlockResponse>> {
161+
self.state.get_block_by_id(block_id, full_tx).await
162+
}
163+
164+
async fn get_untrusted_block(
165+
&self,
166+
block_id: BlockId,
167+
full_tx: bool,
159168
) -> Result<Option<N::BlockResponse>> {
160169
self.api.get_block(block_id, full_tx).await
161170
}
@@ -164,7 +173,21 @@ impl<N: NetworkSpec, A: VerifiableApi<N>> ExecutionSpec<N>
164173
&self,
165174
block_id: BlockId,
166175
) -> Result<Option<Vec<N::ReceiptResponse>>> {
167-
self.api.get_block_receipts(block_id).await
176+
let block = self.state.get_block_by_id(block_id, false).await?;
177+
let Some(block) = block else {
178+
return Ok(None);
179+
};
180+
let block_num = block.header().number();
181+
182+
let receipts = self
183+
.api
184+
.get_block_receipts(block_num.into())
185+
.await?
186+
.ok_or(ExecutionError::NoReceiptsForBlock(block_num.into()))?;
187+
188+
verify_block_receipts::<N>(&receipts, &block)?;
189+
190+
Ok(Some(receipts))
168191
}
169192

170193
async fn send_raw_transaction(&self, bytes: &[u8]) -> Result<B256> {
@@ -444,13 +467,13 @@ mod tests {
444467
}
445468

446469
#[tokio::test]
447-
async fn test_create_access_list() {
470+
async fn test_create_extended_access_list() {
448471
let client = get_client().await;
449472
let address = rpc_proof().address;
450473
let tx = TransactionRequest::default().from(address).to(address);
451474

452475
let response = client
453-
.create_access_list(&tx, BlockId::latest().into())
476+
.create_extended_access_list(&tx, BlockId::latest().into())
454477
.await
455478
.unwrap();
456479

@@ -480,6 +503,19 @@ mod tests {
480503
assert_eq!(response, rpc_block());
481504
}
482505

506+
#[tokio::test]
507+
async fn test_get_untrusted_block() {
508+
let client = get_client().await;
509+
510+
let response = client
511+
.get_untrusted_block(BlockId::latest(), false)
512+
.await
513+
.unwrap()
514+
.unwrap();
515+
516+
assert_eq!(response, rpc_block());
517+
}
518+
483519
#[tokio::test]
484520
async fn test_get_block_receipts() {
485521
let client = get_client().await;

core/src/execution/evm.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl<N: NetworkSpec> Evm<N> {
4848
}
4949

5050
pub async fn call(&mut self, tx: &N::TransactionRequest) -> Result<Bytes, EvmError> {
51-
let (result, ..) = self.call_inner(tx).await?;
51+
let (result, ..) = self.call_inner(tx, false).await?;
5252

5353
match result {
5454
ExecutionResult::Success { output, .. } => Ok(output.into_data()),
@@ -60,7 +60,7 @@ impl<N: NetworkSpec> Evm<N> {
6060
}
6161

6262
pub async fn estimate_gas(&mut self, tx: &N::TransactionRequest) -> Result<u64, EvmError> {
63-
let (result, ..) = self.call_inner(tx).await?;
63+
let (result, ..) = self.call_inner(tx, false).await?;
6464

6565
match result {
6666
ExecutionResult::Success { gas_used, .. } => Ok(gas_used),
@@ -73,7 +73,7 @@ impl<N: NetworkSpec> Evm<N> {
7373
&mut self,
7474
tx: &N::TransactionRequest,
7575
) -> Result<AccessListResultWithAccounts, EvmError> {
76-
let (result, accounts) = self.call_inner(tx).await?;
76+
let (result, accounts) = self.call_inner(tx, false).await?;
7777

7878
match result {
7979
ExecutionResult::Success { .. } => Ok(AccessListResultWithAccounts {
@@ -108,11 +108,12 @@ impl<N: NetworkSpec> Evm<N> {
108108
async fn call_inner(
109109
&mut self,
110110
tx: &N::TransactionRequest,
111+
simulate_call: bool,
111112
) -> Result<(ExecutionResult, HashMap<Address, Account>), EvmError> {
112113
let mut db = ProofDB::new(self.tag, self.execution.clone());
113114
_ = db.state.prefetch_state(tx).await;
114115

115-
let env = Box::new(self.get_env(tx, self.tag).await);
116+
let env = Box::new(self.get_env(tx, self.tag, simulate_call).await);
116117
let evm = Revm::builder().with_db(db).with_env(env).build();
117118
let mut ctx = evm.into_context_with_handler_cfg();
118119

@@ -130,14 +131,14 @@ impl<N: NetworkSpec> Evm<N> {
130131
let needs_update = db.state.needs_update();
131132

132133
if res.is_ok() || !needs_update {
133-
break res.map(|res| (res.result, db.state.accounts.to_owned()));
134+
break res.map(|res| (res.result, std::mem::take(&mut db.state.accounts)));
134135
}
135136
};
136137

137138
tx_res.map_err(|_| EvmError::Generic("evm error".to_string()))
138139
}
139140

140-
async fn get_env(&self, tx: &N::TransactionRequest, tag: BlockTag) -> Env {
141+
async fn get_env(&self, tx: &N::TransactionRequest, tag: BlockTag, simulate_call: bool) -> Env {
141142
let block = self
142143
.execution
143144
.get_block(tag.into(), false)
@@ -148,9 +149,9 @@ impl<N: NetworkSpec> Evm<N> {
148149

149150
let mut cfg = CfgEnv::default();
150151
cfg.chain_id = self.chain_id;
151-
cfg.disable_block_gas_limit = true;
152-
cfg.disable_eip3607 = true;
153-
cfg.disable_base_fee = true;
152+
cfg.disable_block_gas_limit = simulate_call;
153+
cfg.disable_eip3607 = simulate_call;
154+
cfg.disable_base_fee = simulate_call;
154155

155156
Env {
156157
tx: N::tx_env(tx),
@@ -273,7 +274,7 @@ impl<N: NetworkSpec> EvmState<N> {
273274
let block_id = Some(self.block.into());
274275
let account_map = self
275276
.execution
276-
.create_access_list(tx, block_id)
277+
.create_extended_access_list(tx, block_id)
277278
.await
278279
.map_err(EvmError::RpcError)?;
279280

0 commit comments

Comments
 (0)