Skip to content

Commit ef9c511

Browse files
authored
refactor(l1): pass context to all RpcHandlers (#1039)
Closes #1038
1 parent 236c1a1 commit ef9c511

File tree

14 files changed

+304
-247
lines changed

14 files changed

+304
-247
lines changed

crates/networking/rpc/authentication.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub enum AuthenticationError {
1616
}
1717

1818
pub fn authenticate(
19-
secret: Bytes,
19+
secret: &Bytes,
2020
auth_header: Option<TypedHeader<Authorization<Bearer>>>,
2121
) -> Result<(), RpcErr> {
2222
match auth_header {
@@ -39,8 +39,8 @@ struct Claims {
3939
}
4040

4141
/// Authenticates bearer jwt to check that authrpc calls are sent by the consensus layer
42-
pub fn validate_jwt_authentication(token: &str, secret: Bytes) -> Result<(), AuthenticationError> {
43-
let decoding_key = DecodingKey::from_secret(&secret);
42+
pub fn validate_jwt_authentication(token: &str, secret: &Bytes) -> Result<(), AuthenticationError> {
43+
let decoding_key = DecodingKey::from_secret(secret);
4444
let mut validation = Validation::new(Algorithm::HS256);
4545
validation.validate_exp = false;
4646
validation.set_required_spec_claims(&["iat"]);

crates/networking/rpc/engine/exchange_transition_config.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use ethereum_rust_core::{serde_utils, H256};
2-
use ethereum_rust_storage::Store;
32
use serde::{Deserialize, Serialize};
43
use serde_json::Value;
54
use tracing::{info, warn};
65

7-
use crate::{utils::RpcErr, RpcHandler};
6+
use crate::{utils::RpcErr, RpcApiContext, RpcHandler};
87

98
#[derive(Debug, Serialize, Deserialize)]
109
#[serde(rename_all = "camelCase")]
@@ -45,11 +44,11 @@ impl RpcHandler for ExchangeTransitionConfigV1Req {
4544
Ok(ExchangeTransitionConfigV1Req { payload })
4645
}
4746

48-
fn handle(&self, storage: Store) -> Result<Value, RpcErr> {
47+
fn handle(&self, context: RpcApiContext) -> Result<Value, RpcErr> {
4948
info!("Received new engine request: {self}");
5049
let payload = &self.payload;
5150

52-
let chain_config = storage.get_chain_config()?;
51+
let chain_config = context.storage.get_chain_config()?;
5352
let terminal_total_difficulty = chain_config.terminal_total_difficulty;
5453

5554
if terminal_total_difficulty.unwrap_or_default() != payload.terminal_total_difficulty {
@@ -59,7 +58,9 @@ impl RpcHandler for ExchangeTransitionConfigV1Req {
5958
);
6059
};
6160

62-
let block = storage.get_block_header(payload.terminal_block_number)?;
61+
let block = context
62+
.storage
63+
.get_block_header(payload.terminal_block_number)?;
6364
let terminal_block_hash = block.map_or(H256::zero(), |block| block.compute_block_hash());
6465

6566
serde_json::to_value(ExchangeTransitionConfigPayload {

crates/networking/rpc/engine/fork_choice.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use ethereum_rust_blockchain::{
44
latest_canonical_block_hash,
55
payload::{create_payload, BuildPayloadArgs},
66
};
7-
use ethereum_rust_storage::Store;
87
use serde_json::Value;
98
use tracing::{info, warn};
109

@@ -14,7 +13,7 @@ use crate::{
1413
payload::PayloadStatus,
1514
},
1615
utils::RpcRequest,
17-
RpcErr, RpcHandler,
16+
RpcApiContext, RpcErr, RpcHandler,
1817
};
1918

2019
#[derive(Debug)]
@@ -58,7 +57,8 @@ impl RpcHandler for ForkChoiceUpdatedV3 {
5857
})
5958
}
6059

61-
fn handle(&self, storage: Store) -> Result<Value, RpcErr> {
60+
fn handle(&self, context: RpcApiContext) -> Result<Value, RpcErr> {
61+
let storage = &context.storage;
6262
info!(
6363
"New fork choice request with head: {}, safe: {}, finalized: {}.",
6464
self.fork_choice_state.head_block_hash,
@@ -68,7 +68,7 @@ impl RpcHandler for ForkChoiceUpdatedV3 {
6868
let fork_choice_error_to_response = |error| {
6969
let response = match error {
7070
InvalidForkChoice::NewHeadAlreadyCanonical => ForkChoiceResponse::from(
71-
PayloadStatus::valid_with_hash(latest_canonical_block_hash(&storage).unwrap()),
71+
PayloadStatus::valid_with_hash(latest_canonical_block_hash(storage).unwrap()),
7272
),
7373
InvalidForkChoice::Syncing => ForkChoiceResponse::from(PayloadStatus::syncing()),
7474
reason => {
@@ -83,7 +83,7 @@ impl RpcHandler for ForkChoiceUpdatedV3 {
8383
};
8484

8585
let head_block = match apply_fork_choice(
86-
&storage,
86+
storage,
8787
self.fork_choice_state.head_block_hash,
8888
self.fork_choice_state.safe_block_hash,
8989
self.fork_choice_state.finalized_block_hash,
@@ -125,7 +125,7 @@ impl RpcHandler for ForkChoiceUpdatedV3 {
125125
};
126126
let payload_id = args.id();
127127
response.set_id(payload_id);
128-
let payload = match create_payload(&args, &storage) {
128+
let payload = match create_payload(&args, storage) {
129129
Ok(payload) => payload,
130130
Err(ChainError::EvmError(error)) => return Err(error.into()),
131131
// Parent block is guaranteed to be present at this point,

crates/networking/rpc/engine/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pub mod exchange_transition_config;
22
pub mod fork_choice;
33
pub mod payload;
44

5-
use crate::{utils::RpcRequest, RpcErr, RpcHandler, Store};
5+
use crate::{utils::RpcRequest, RpcApiContext, RpcErr, RpcHandler};
66
use serde_json::{json, Value};
77

88
pub type ExchangeCapabilitiesRequest = Vec<String>;
@@ -30,7 +30,7 @@ impl RpcHandler for ExchangeCapabilitiesRequest {
3030
})
3131
}
3232

33-
fn handle(&self, _storage: Store) -> Result<Value, RpcErr> {
33+
fn handle(&self, _context: RpcApiContext) -> Result<Value, RpcErr> {
3434
Ok(json!(*self))
3535
}
3636
}

crates/networking/rpc/engine/payload.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use ethereum_rust_blockchain::error::ChainError;
33
use ethereum_rust_blockchain::payload::build_payload;
44
use ethereum_rust_core::types::Fork;
55
use ethereum_rust_core::{H256, U256};
6-
use ethereum_rust_storage::Store;
76
use serde_json::Value;
87
use tracing::{error, info, warn};
98

109
use crate::types::payload::ExecutionPayloadResponse;
1110
use crate::utils::RpcRequest;
11+
use crate::RpcApiContext;
1212
use crate::{
1313
types::payload::{ExecutionPayloadV3, PayloadStatus},
1414
RpcErr, RpcHandler,
@@ -56,7 +56,8 @@ impl RpcHandler for NewPayloadV3Request {
5656
})
5757
}
5858

59-
fn handle(&self, storage: Store) -> Result<Value, RpcErr> {
59+
fn handle(&self, context: RpcApiContext) -> Result<Value, RpcErr> {
60+
let storage = &context.storage;
6061
let block_hash = self.payload.block_hash;
6162
info!("Received new payload with block hash: {block_hash:#x}");
6263

@@ -114,7 +115,7 @@ impl RpcHandler for NewPayloadV3Request {
114115

115116
// Execute and store the block
116117
info!("Executing payload with block hash: {block_hash:#x}");
117-
let payload_status = match add_block(&block, &storage) {
118+
let payload_status = match add_block(&block, storage) {
118119
Err(ChainError::ParentNotFound) => Ok(PayloadStatus::syncing()),
119120
// Under the current implementation this is not possible: we always calculate the state
120121
// transition of any new payload as long as the parent is present. If we received the
@@ -185,15 +186,15 @@ impl RpcHandler for GetPayloadV3Request {
185186
Ok(GetPayloadV3Request { payload_id })
186187
}
187188

188-
fn handle(&self, storage: Store) -> Result<Value, RpcErr> {
189+
fn handle(&self, context: RpcApiContext) -> Result<Value, RpcErr> {
189190
info!("Requested payload with id: {:#018x}", self.payload_id);
190-
let Some(mut payload) = storage.get_payload(self.payload_id)? else {
191+
let Some(mut payload) = context.storage.get_payload(self.payload_id)? else {
191192
return Err(RpcErr::UnknownPayload(format!(
192193
"Payload with id {:#018x} not found",
193194
self.payload_id
194195
)));
195196
};
196-
let (blobs_bundle, block_value) = build_payload(&mut payload, &storage)
197+
let (blobs_bundle, block_value) = build_payload(&mut payload, &context.storage)
197198
.map_err(|err| RpcErr::Internal(err.to_string()))?;
198199
serde_json::to_value(ExecutionPayloadResponse {
199200
execution_payload: ExecutionPayloadV3::from_block(payload),

crates/networking/rpc/eth/account.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use ethereum_rust_storage::Store;
21
use serde_json::Value;
32
use tracing::info;
43

54
use crate::types::account_proof::{AccountProof, StorageProof};
65
use crate::types::block_identifier::BlockIdentifierOrHash;
6+
use crate::RpcApiContext;
77
use crate::{utils::RpcErr, RpcHandler};
88
use ethereum_rust_core::{Address, BigEndianHash, H256, U256};
99

@@ -47,19 +47,21 @@ impl RpcHandler for GetBalanceRequest {
4747
block: BlockIdentifierOrHash::parse(params[1].clone(), 1)?,
4848
})
4949
}
50-
fn handle(&self, storage: Store) -> Result<Value, RpcErr> {
50+
fn handle(&self, context: RpcApiContext) -> Result<Value, RpcErr> {
5151
info!(
5252
"Requested balance of account {} at block {}",
5353
self.address, self.block
5454
);
5555

56-
let Some(block_number) = self.block.resolve_block_number(&storage)? else {
56+
let Some(block_number) = self.block.resolve_block_number(&context.storage)? else {
5757
return Err(RpcErr::Internal(
5858
"Could not resolve block number".to_owned(),
5959
)); // Should we return Null here?
6060
};
6161

62-
let account = storage.get_account_info(block_number, self.address)?;
62+
let account = context
63+
.storage
64+
.get_account_info(block_number, self.address)?;
6365
let balance = account.map(|acc| acc.balance).unwrap_or_default();
6466

6567
serde_json::to_value(format!("{:#x}", balance))
@@ -80,19 +82,20 @@ impl RpcHandler for GetCodeRequest {
8082
block: BlockIdentifierOrHash::parse(params[1].clone(), 1)?,
8183
})
8284
}
83-
fn handle(&self, storage: Store) -> Result<Value, RpcErr> {
85+
fn handle(&self, context: RpcApiContext) -> Result<Value, RpcErr> {
8486
info!(
8587
"Requested code of account {} at block {}",
8688
self.address, self.block
8789
);
8890

89-
let Some(block_number) = self.block.resolve_block_number(&storage)? else {
91+
let Some(block_number) = self.block.resolve_block_number(&context.storage)? else {
9092
return Err(RpcErr::Internal(
9193
"Could not resolve block number".to_owned(),
9294
)); // Should we return Null here?
9395
};
9496

95-
let code = storage
97+
let code = context
98+
.storage
9699
.get_code_by_account_address(block_number, self.address)?
97100
.unwrap_or_default();
98101

@@ -115,19 +118,20 @@ impl RpcHandler for GetStorageAtRequest {
115118
block: BlockIdentifierOrHash::parse(params[2].clone(), 2)?,
116119
})
117120
}
118-
fn handle(&self, storage: Store) -> Result<Value, RpcErr> {
121+
fn handle(&self, context: RpcApiContext) -> Result<Value, RpcErr> {
119122
info!(
120123
"Requested storage sot {} of account {} at block {}",
121124
self.storage_slot, self.address, self.block
122125
);
123126

124-
let Some(block_number) = self.block.resolve_block_number(&storage)? else {
127+
let Some(block_number) = self.block.resolve_block_number(&context.storage)? else {
125128
return Err(RpcErr::Internal(
126129
"Could not resolve block number".to_owned(),
127130
)); // Should we return Null here?
128131
};
129132

130-
let storage_value = storage
133+
let storage_value = context
134+
.storage
131135
.get_storage_at(block_number, self.address, self.storage_slot)?
132136
.unwrap_or_default();
133137
let storage_value = H256::from_uint(&storage_value);
@@ -149,18 +153,19 @@ impl RpcHandler for GetTransactionCountRequest {
149153
block: BlockIdentifierOrHash::parse(params[1].clone(), 1)?,
150154
})
151155
}
152-
fn handle(&self, storage: Store) -> Result<Value, RpcErr> {
156+
fn handle(&self, context: RpcApiContext) -> Result<Value, RpcErr> {
153157
info!(
154158
"Requested nonce of account {} at block {}",
155159
self.address, self.block
156160
);
157161

158-
let Some(block_number) = self.block.resolve_block_number(&storage)? else {
162+
let Some(block_number) = self.block.resolve_block_number(&context.storage)? else {
159163
return serde_json::to_value("0x0")
160164
.map_err(|error| RpcErr::Internal(error.to_string()));
161165
};
162166

163-
let nonce = storage
167+
let nonce = context
168+
.storage
164169
.get_nonce_by_account_address(block_number, self.address)?
165170
.unwrap_or_default();
166171

@@ -186,12 +191,13 @@ impl RpcHandler for GetProofRequest {
186191
})
187192
}
188193

189-
fn handle(&self, storage: Store) -> Result<Value, RpcErr> {
194+
fn handle(&self, context: RpcApiContext) -> Result<Value, RpcErr> {
195+
let storage = &context.storage;
190196
info!(
191197
"Requested proof for account {} at block {} with storage keys: {:?}",
192198
self.address, self.block, self.storage_keys
193199
);
194-
let Some(block_number) = self.block.resolve_block_number(&storage)? else {
200+
let Some(block_number) = self.block.resolve_block_number(storage)? else {
195201
return Ok(Value::Null);
196202
};
197203
// Create account proof

0 commit comments

Comments
 (0)