From 2c15ceccf2dc9859283481100c8693554d36653b Mon Sep 17 00:00:00 2001 From: Naman Garg <0708ng@gmail.com> Date: Fri, 19 Jul 2024 13:42:20 +0530 Subject: [PATCH] chore(sidecar): clean code and address review --- bolt-sidecar/bin/sidecar.rs | 7 +-- bolt-sidecar/src/builder/template.rs | 16 +++--- bolt-sidecar/src/state/execution.rs | 77 ++++++++++++++++------------ 3 files changed, 54 insertions(+), 46 deletions(-) diff --git a/bolt-sidecar/bin/sidecar.rs b/bolt-sidecar/bin/sidecar.rs index 47eb1a912..0fa50d7d3 100644 --- a/bolt-sidecar/bin/sidecar.rs +++ b/bolt-sidecar/bin/sidecar.rs @@ -28,12 +28,7 @@ async fn main() -> eyre::Result<()> { let signer = Signer::new(config.private_key.clone().unwrap()); let state_client = StateClient::new(config.execution_api_url.clone()); - let mut execution_state = ExecutionState::new( - state_client, - config.limits.max_commitments_per_slot, - config.limits.max_committed_gas_per_slot, - ) - .await?; + let mut execution_state = ExecutionState::new(state_client, config.limits.clone()).await?; let mevboost_client = MevBoostClient::new(config.mevboost_url.clone()); let beacon_client = BeaconClient::new(config.beacon_api_url.clone()); diff --git a/bolt-sidecar/src/builder/template.rs b/bolt-sidecar/src/builder/template.rs index 68f9112e9..fdd59d755 100644 --- a/bolt-sidecar/src/builder/template.rs +++ b/bolt-sidecar/src/builder/template.rs @@ -113,14 +113,14 @@ impl BlockTemplate { /// Returns the committed gas in the block template. #[inline] - pub fn committed_gas(&self) -> U256 { - self.signed_constraints_list - .iter() - .fold(U256::ZERO, |acc, sc| { - acc + sc.message.constraints.iter().fold(U256::ZERO, |acc, c| { - acc + max_transaction_cost(&c.transaction) - }) - }) + pub fn committed_gas(&self) -> u64 { + self.signed_constraints_list.iter().fold(0, |acc, sc| { + acc + sc + .message + .constraints + .iter() + .fold(0, |acc, c| acc + &c.transaction.gas_limit()) + }) } /// Returns the blob count of the block template. diff --git a/bolt-sidecar/src/state/execution.rs b/bolt-sidecar/src/state/execution.rs index 0be10e9f4..26efd123c 100644 --- a/bolt-sidecar/src/state/execution.rs +++ b/bolt-sidecar/src/state/execution.rs @@ -10,6 +10,7 @@ use thiserror::Error; use crate::{ builder::BlockTemplate, common::{calculate_max_basefee, validate_transaction}, + config::Limits, primitives::{AccountState, CommitmentRequest, SignedConstraints, Slot, TransactionExt}, }; @@ -147,11 +148,7 @@ impl Default for ValidationParams { impl ExecutionState { /// Creates a new state with the given client, initializing the /// basefee and head block number. - pub async fn new( - client: C, - max_commitments_per_slot: NonZero, - max_committed_gas_per_slot: NonZero, - ) -> Result { + pub async fn new(client: C, limits: Limits) -> Result { let (basefee, blob_basefee, block_number, chain_id) = tokio::try_join!( client.get_basefee(None), client.get_blob_basefee(None), @@ -164,8 +161,8 @@ impl ExecutionState { blob_basefee, block_number, chain_id, - max_commitments_per_slot, - max_committed_gas_per_slot, + max_commitments_per_slot: limits.max_commitments_per_slot, + max_committed_gas_per_slot: limits.max_committed_gas_per_slot, client, slot: 0, account_states: HashMap::new(), @@ -220,7 +217,7 @@ impl ExecutionState { } // Check if the committed gas exceeds the maximum - if template.committed_gas().to::() >= max_committed_gas_per_slot { + if template.committed_gas() + req.tx.gas_limit() >= max_committed_gas_per_slot { return Err(ValidationError::MaxCommittedGasReachedForSlot( self.slot, max_committed_gas_per_slot, @@ -478,9 +475,11 @@ mod tests { let anvil = launch_anvil(); let client = StateClient::new(anvil.endpoint_url()); - let max_comms = NonZero::new(10).unwrap(); - let max_gas = NonZero::new(10_000_000).unwrap(); - let mut state = ExecutionState::new(client.clone(), max_comms, max_gas).await?; + let limits: Limits = Limits { + max_commitments_per_slot: NonZero::new(10).unwrap(), + max_committed_gas_per_slot: NonZero::new(10_000_000).unwrap(), + }; + let mut state = ExecutionState::new(client.clone(), limits).await?; let sender = anvil.addresses().first().unwrap(); let sender_pk = anvil.keys().first().unwrap(); @@ -505,9 +504,11 @@ mod tests { let anvil = launch_anvil(); let client = StateClient::new(anvil.endpoint_url()); - let max_comms = NonZero::new(10).unwrap(); - let max_gas = NonZero::new(10_000_000).unwrap(); - let mut state = ExecutionState::new(client.clone(), max_comms, max_gas).await?; + let limits: Limits = Limits { + max_commitments_per_slot: NonZero::new(10).unwrap(), + max_committed_gas_per_slot: NonZero::new(10_000_000).unwrap(), + }; + let mut state = ExecutionState::new(client.clone(), limits).await?; let sender = anvil.addresses().first().unwrap(); let sender_pk = anvil.keys().first().unwrap(); @@ -547,9 +548,11 @@ mod tests { let anvil = launch_anvil(); let client = StateClient::new(anvil.endpoint_url()); - let max_comms = NonZero::new(10).unwrap(); - let max_gas = NonZero::new(10_000_000).unwrap(); - let mut state = ExecutionState::new(client.clone(), max_comms, max_gas).await?; + let limits: Limits = Limits { + max_commitments_per_slot: NonZero::new(10).unwrap(), + max_committed_gas_per_slot: NonZero::new(10_000_000).unwrap(), + }; + let mut state = ExecutionState::new(client.clone(), limits).await?; let sender = anvil.addresses().first().unwrap(); let sender_pk = anvil.keys().first().unwrap(); @@ -601,9 +604,11 @@ mod tests { let anvil = launch_anvil(); let client = StateClient::new(anvil.endpoint_url()); - let max_comms = NonZero::new(10).unwrap(); - let max_gas = NonZero::new(10_000_000).unwrap(); - let mut state = ExecutionState::new(client.clone(), max_comms, max_gas).await?; + let limits: Limits = Limits { + max_commitments_per_slot: NonZero::new(10).unwrap(), + max_committed_gas_per_slot: NonZero::new(10_000_000).unwrap(), + }; + let mut state = ExecutionState::new(client.clone(), limits).await?; let sender = anvil.addresses().first().unwrap(); let sender_pk = anvil.keys().first().unwrap(); @@ -633,9 +638,11 @@ mod tests { let anvil = launch_anvil(); let client = StateClient::new(anvil.endpoint_url()); - let max_comms = NonZero::new(10).unwrap(); - let max_gas = NonZero::new(10_000_000).unwrap(); - let mut state = ExecutionState::new(client.clone(), max_comms, max_gas).await?; + let limits: Limits = Limits { + max_commitments_per_slot: NonZero::new(10).unwrap(), + max_committed_gas_per_slot: NonZero::new(10_000_000).unwrap(), + }; + let mut state = ExecutionState::new(client.clone(), limits).await?; let sender = anvil.addresses().first().unwrap(); let sender_pk = anvil.keys().first().unwrap(); @@ -697,9 +704,11 @@ mod tests { let anvil = launch_anvil(); let client = StateClient::new(anvil.endpoint_url()); - let max_comms = NonZero::new(10).unwrap(); - let max_gas = NonZero::new(10_000_000).unwrap(); - let mut state = ExecutionState::new(client.clone(), max_comms, max_gas).await?; + let limits: Limits = Limits { + max_commitments_per_slot: NonZero::new(10).unwrap(), + max_committed_gas_per_slot: NonZero::new(10_000_000).unwrap(), + }; + let mut state = ExecutionState::new(client.clone(), limits).await?; let basefee = state.basefee(); @@ -733,9 +742,11 @@ mod tests { let client = StateClient::new(anvil.endpoint_url()); let provider = ProviderBuilder::new().on_http(anvil.endpoint_url()); - let max_comms = NonZero::new(10).unwrap(); - let max_gas = NonZero::new(10_000_000).unwrap(); - let mut state = ExecutionState::new(client.clone(), max_comms, max_gas).await?; + let limits: Limits = Limits { + max_commitments_per_slot: NonZero::new(10).unwrap(), + max_committed_gas_per_slot: NonZero::new(10_000_000).unwrap(), + }; + let mut state = ExecutionState::new(client.clone(), limits).await?; let sender = anvil.addresses().first().unwrap(); let sender_pk = anvil.keys().first().unwrap(); @@ -801,9 +812,11 @@ mod tests { let anvil = launch_anvil(); let client = StateClient::new(anvil.endpoint_url()); - let max_comms = NonZero::new(10).unwrap(); - let max_gas = NonZero::new(10_000_000).unwrap(); - let mut state = ExecutionState::new(client.clone(), max_comms, max_gas).await?; + let limits: Limits = Limits { + max_commitments_per_slot: NonZero::new(10).unwrap(), + max_committed_gas_per_slot: NonZero::new(10_000_000).unwrap(), + }; + let mut state = ExecutionState::new(client.clone(), limits).await?; let sender = anvil.addresses().first().unwrap(); let sender_pk = anvil.keys().first().unwrap();