Skip to content

Commit

Permalink
pull gas limit computation to common crate
Browse files Browse the repository at this point in the history
  • Loading branch information
ralexstokes committed Oct 27, 2023
1 parent 834940b commit 1c5ce06
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 53 deletions.
57 changes: 4 additions & 53 deletions mev-build-rs/src/reth_builder/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ use ethereum_consensus::{
state_transition::Context,
};
use ethers::signers::{LocalWallet, Signer};
use mev_rs::{blinded_block_relayer::BlindedBlockRelayer, types::ProposerSchedule, Relay};
use mev_rs::{
blinded_block_relayer::BlindedBlockRelayer, compute_preferred_gas_limit,
types::ProposerSchedule, Relay,
};
use reth_payload_builder::PayloadBuilderAttributes;
use reth_primitives::{Address, BlockNumberOrTag, Bytes, ChainSpec, B256, U256};
use reth_provider::{BlockReaderIdExt, BlockSource, StateProviderFactory};
use reth_transaction_pool::TransactionPool;
use std::{
cmp::Ordering,
collections::{BTreeMap, HashMap},
ops::Deref,
sync::{Arc, Mutex},
Expand All @@ -31,57 +33,6 @@ const BUILD_DEADLINE_INTO_SLOT: Duration = Duration::from_millis(500);
// better payload in the context of one job.
const BUILD_PROGRESSION_INTERVAL: Duration = Duration::from_millis(500);

const GAS_BOUND_DIVISOR: u64 = 1024;

fn compute_preferred_gas_limit(preferred_gas_limit: u64, parent_gas_limit: u64) -> u64 {
match preferred_gas_limit.cmp(&parent_gas_limit) {
Ordering::Equal => preferred_gas_limit,
Ordering::Greater => {
let bound = parent_gas_limit + parent_gas_limit / GAS_BOUND_DIVISOR;
preferred_gas_limit.min(bound - 1)
}
Ordering::Less => {
let bound = parent_gas_limit - parent_gas_limit / GAS_BOUND_DIVISOR;
preferred_gas_limit.max(bound + 1)
}
}
}

#[cfg(test)]
mod tests {
use super::{compute_preferred_gas_limit, GAS_BOUND_DIVISOR};
use std::cmp::Ordering;

fn verify_limits(gas_limit: u64, parent_gas_limit: u64) -> bool {
match gas_limit.cmp(&parent_gas_limit) {
Ordering::Equal => true,
Ordering::Greater => {
let bound = parent_gas_limit + parent_gas_limit / GAS_BOUND_DIVISOR;
gas_limit < bound
}
Ordering::Less => {
let bound = parent_gas_limit - parent_gas_limit / GAS_BOUND_DIVISOR;
gas_limit > bound
}
}
}

#[test]
fn test_compute_preferred_gas_limit() {
for t in &[
// preferred, parent, computed
(30_000_000, 30_000_000, 30_000_000),
(30_029_000, 30_000_000, 30_029_000),
(30_029_300, 30_000_000, 30_029_295),
(29_970_710, 30_000_000, 29_970_710),
(29_970_700, 30_000_000, 29_970_705),
] {
assert_eq!(compute_preferred_gas_limit(t.0, t.1), t.2);
assert!(verify_limits(t.2, t.1))
}
}
}

/// `Builder` builds blocks for proposers registered to connected relays.
#[derive(Clone)]
pub struct Builder<Pool, Client>(Arc<Inner<Pool, Client>>);
Expand Down
51 changes: 51 additions & 0 deletions mev-rs/src/block_validation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use std::cmp::Ordering;

pub const GAS_BOUND_DIVISOR: u64 = 1024;

pub fn compute_preferred_gas_limit(preferred_gas_limit: u64, parent_gas_limit: u64) -> u64 {
match preferred_gas_limit.cmp(&parent_gas_limit) {
Ordering::Equal => preferred_gas_limit,
Ordering::Greater => {
let bound = parent_gas_limit + parent_gas_limit / GAS_BOUND_DIVISOR;
preferred_gas_limit.min(bound - 1)
}
Ordering::Less => {
let bound = parent_gas_limit - parent_gas_limit / GAS_BOUND_DIVISOR;
preferred_gas_limit.max(bound + 1)
}
}
}

#[cfg(test)]
mod tests {
use super::*;

fn verify_limits(gas_limit: u64, parent_gas_limit: u64) -> bool {
match gas_limit.cmp(&parent_gas_limit) {
Ordering::Equal => true,
Ordering::Greater => {
let bound = parent_gas_limit + parent_gas_limit / GAS_BOUND_DIVISOR;
gas_limit < bound
}
Ordering::Less => {
let bound = parent_gas_limit - parent_gas_limit / GAS_BOUND_DIVISOR;
gas_limit > bound
}
}
}

#[test]
fn test_compute_preferred_gas_limit() {
for t in &[
// preferred, parent, computed
(30_000_000, 30_000_000, 30_000_000),
(30_029_000, 30_000_000, 30_029_000),
(30_029_300, 30_000_000, 30_029_295),
(29_970_710, 30_000_000, 29_970_710),
(29_970_700, 30_000_000, 29_970_705),
] {
assert_eq!(compute_preferred_gas_limit(t.0, t.1), t.2);
assert!(verify_limits(t.2, t.1))
}
}
}
2 changes: 2 additions & 0 deletions mev-rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod blinded_block_provider;
pub mod blinded_block_relayer;
pub mod block_validation;
mod error;
mod proposer_scheduler;
pub mod relay;
Expand All @@ -12,6 +13,7 @@ mod validator_registry;
pub use blinded_block_provider::BlindedBlockProvider;
pub use blinded_block_relayer::BlindedBlockRelayer;

pub use block_validation::*;
pub use error::Error;
pub use proposer_scheduler::ProposerScheduler;
pub use relay::{Relay, RelayEndpoint};
Expand Down

0 comments on commit 1c5ce06

Please sign in to comment.