Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sidecar): add max_committed_gas_per_slot to sidecar Limits #150

Merged
merged 12 commits into from
Jul 19, 2024
7 changes: 1 addition & 6 deletions bolt-sidecar/bin/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
namn-grg marked this conversation as resolved.
Show resolved Hide resolved

let mevboost_client = MevBoostClient::new(config.mevboost_url.clone());
let beacon_client = BeaconClient::new(config.beacon_api_url.clone());
Expand Down
16 changes: 8 additions & 8 deletions bolt-sidecar/src/builder/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
77 changes: 45 additions & 32 deletions bolt-sidecar/src/state/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down Expand Up @@ -147,11 +148,7 @@ impl Default for ValidationParams {
impl<C: StateFetcher> ExecutionState<C> {
/// 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<usize>,
max_committed_gas_per_slot: NonZero<u64>,
) -> Result<Self, TransportError> {
pub async fn new(client: C, limits: Limits) -> Result<Self, TransportError> {
let (basefee, blob_basefee, block_number, chain_id) = tokio::try_join!(
client.get_basefee(None),
client.get_blob_basefee(None),
Expand All @@ -164,8 +161,8 @@ impl<C: StateFetcher> ExecutionState<C> {
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(),
Expand Down Expand Up @@ -220,7 +217,7 @@ impl<C: StateFetcher> ExecutionState<C> {
}

// Check if the committed gas exceeds the maximum
if template.committed_gas().to::<u64>() >= 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,
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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?;
namn-grg marked this conversation as resolved.
Show resolved Hide resolved

let sender = anvil.addresses().first().unwrap();
let sender_pk = anvil.keys().first().unwrap();
Expand Down
Loading