Skip to content

Commit

Permalink
refactor(builder): add builder crate
Browse files Browse the repository at this point in the history
  • Loading branch information
dancoombs committed Sep 18, 2023
1 parent 8be3699 commit 78cf1af
Show file tree
Hide file tree
Showing 39 changed files with 396 additions and 216 deletions.
60 changes: 48 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ members = [
"bin/rundler",
"bin/tools",
"crates/rundler",
"crates/builder",
"crates/pool",
"crates/provider",
"crates/sim",
Expand Down
2 changes: 1 addition & 1 deletion buf.work.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: v1
directories:
- crates/rundler/proto
- crates/builder/proto
- crates/pool/proto
53 changes: 53 additions & 0 deletions crates/builder/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
[package]
name = "rundler-builder"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
license.workspace = true
repository.workspace = true

[dependencies]
rundler-pool = { path = "../pool" }
rundler-provider = { path = "../provider" }
rundler-sim = { path = "../sim" }
rundler-task = { path = "../task" }
rundler-types = { path = "../types" }
rundler-utils = { path = "../utils" }

anyhow.workspace = true
async-trait.workspace = true
enum_dispatch = "0.3.11"
ethers.workspace = true
ethers-signers = {version = "2.0.8", features = ["aws"] }
futures.workspace = true
futures-timer = "3.0.2"
futures-util.workspace = true
linked-hash-map = "0.5.6"
metrics.workspace = true
pin-project.workspace = true
prost.workspace = true
parse-display.workspace = true
reqwest.workspace = true
rslock = "0.2.2"
rusoto_core = { version = "0.48.0", default-features = false, features = ["rustls"] }
rusoto_kms = { version = "0.48.0", default-features = false, features = ["rustls"] }
thiserror.workspace = true
tokio.workspace = true
tokio-util.workspace = true
tonic.workspace = true
tonic-health.workspace = true
tonic-reflection.workspace = true
tracing.workspace = true
serde.workspace = true
serde_json.workspace = true

mockall = {workspace = true, optional = true }

[dev-dependencies]
mockall.workspace = true

[build-dependencies]
tonic-build.workspace = true

[features]
test-utils = [ "mockall" ]
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# TODO

# Protocol Buffer Definitions

This directory stores the schema definitions used by Rundler and the supporting gRPC services of it's internal components.
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
};

use anyhow::Context;
use async_trait::async_trait;
use ethers::types::{Address, BlockId, Bytes, H256, U256};
use futures::future;
use linked_hash_map::LinkedHashMap;
Expand All @@ -20,10 +21,9 @@ use rundler_sim::{
use rundler_types::{Entity, EntityType, GasFees, Timestamp, UserOperation, UserOpsPerAggregator};
use rundler_utils::{emit::WithEntryPoint, math};
use tokio::{sync::broadcast, try_join};
use tonic::async_trait;
use tracing::{error, info};

use crate::builder::emit::{BuilderEvent, OpRejectionReason, SkipReason};
use crate::emit::{BuilderEvent, OpRejectionReason, SkipReason};

/// A user op must be valid for at least this long into the future to be included.
const TIME_RANGE_BUFFER: Duration = Duration::from_secs(60);
Expand All @@ -33,40 +33,40 @@ const BUNDLE_TRANSACTION_GAS_OVERHEAD_BUFFER: u64 = 5000;
const BUNDLE_TRANSACTION_GAS_OVERHEAD_PERCENT: u64 = 5;

#[derive(Debug, Default)]
pub struct Bundle {
pub ops_per_aggregator: Vec<UserOpsPerAggregator>,
pub gas_estimate: U256,
pub gas_fees: GasFees,
pub expected_storage: ExpectedStorage,
pub rejected_ops: Vec<UserOperation>,
pub rejected_entities: Vec<Entity>,
pub(crate) struct Bundle {
pub(crate) ops_per_aggregator: Vec<UserOpsPerAggregator>,
pub(crate) gas_estimate: U256,
pub(crate) gas_fees: GasFees,
pub(crate) expected_storage: ExpectedStorage,
pub(crate) rejected_ops: Vec<UserOperation>,
pub(crate) rejected_entities: Vec<Entity>,
}

impl Bundle {
pub fn len(&self) -> usize {
pub(crate) fn len(&self) -> usize {
self.ops_per_aggregator
.iter()
.map(|ops| ops.user_ops.len())
.sum()
}

pub fn is_empty(&self) -> bool {
pub(crate) fn is_empty(&self) -> bool {
self.ops_per_aggregator.is_empty()
}

pub fn iter_ops(&self) -> impl Iterator<Item = &UserOperation> + '_ {
pub(crate) fn iter_ops(&self) -> impl Iterator<Item = &UserOperation> + '_ {
self.ops_per_aggregator.iter().flat_map(|ops| &ops.user_ops)
}
}

#[cfg_attr(test, automock)]
#[async_trait]
pub trait BundleProposer: Send + Sync + 'static {
pub(crate) trait BundleProposer: Send + Sync + 'static {
async fn make_bundle(&self, required_fees: Option<GasFees>) -> anyhow::Result<Bundle>;
}

#[derive(Debug)]
pub struct BundleProposerImpl<S, E, P, C>
pub(crate) struct BundleProposerImpl<S, E, P, C>
where
S: Simulator,
E: EntryPoint,
Expand All @@ -83,14 +83,14 @@ where
}

#[derive(Debug)]
pub struct Settings {
pub chain_id: u64,
pub max_bundle_size: u64,
pub max_bundle_gas: u64,
pub beneficiary: Address,
pub use_bundle_priority_fee: Option<bool>,
pub bundle_priority_fee_overhead_percent: u64,
pub priority_fee_mode: PriorityFeeMode,
pub(crate) struct Settings {
pub(crate) chain_id: u64,
pub(crate) max_bundle_size: u64,
pub(crate) max_bundle_gas: u64,
pub(crate) beneficiary: Address,
pub(crate) use_bundle_priority_fee: Option<bool>,
pub(crate) bundle_priority_fee_overhead_percent: u64,
pub(crate) priority_fee_mode: PriorityFeeMode,
}

#[async_trait]
Expand Down Expand Up @@ -191,7 +191,7 @@ where
P: Provider,
C: PoolServer,
{
pub fn new(
pub(crate) fn new(
pool: C,
simulator: S,
entry_point: E,
Expand Down Expand Up @@ -295,7 +295,7 @@ where
};
let max_cost = gas::user_operation_max_gas_cost(&op, self.settings.chain_id);
if *balance < max_cost {
info!("Rejected paymaster {paymaster:?} becauase its balance {balance:?} was too low.");
info!("Rejected paymaster {paymaster:?} because its balance {balance:?} was too low.");
paymasters_to_reject.push(paymaster);
continue;
} else {
Expand Down Expand Up @@ -413,10 +413,10 @@ where

async fn get_balances_by_paymaster(
&self,
addreses: impl Iterator<Item = Address>,
addresses: impl Iterator<Item = Address>,
block_hash: H256,
) -> anyhow::Result<HashMap<Address, U256>> {
let futures = addreses.map(|address| async move {
let futures = addresses.map(|address| async move {
let deposit = self
.entry_point
.balance_of(address, Some(BlockId::Hash(block_hash)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
};

use anyhow::{bail, Context};
use async_trait::async_trait;
use ethers::types::{transaction::eip2718::TypedTransaction, Address, H256, U256};
use futures_util::StreamExt;
use rundler_pool::PoolServer;
Expand All @@ -19,10 +20,9 @@ use tokio::{
sync::{broadcast, mpsc, oneshot},
time,
};
use tonic::async_trait;
use tracing::{error, info, trace, warn};

use crate::builder::{
use crate::{
bundle_proposer::BundleProposer,
emit::{BuilderEvent, BundleTxDetails},
transaction_tracker::{SendResult, TrackerUpdate, TransactionTracker},
Expand All @@ -32,18 +32,18 @@ use crate::builder::{
const GAS_ESTIMATE_OVERHEAD_PERCENT: u64 = 10;

#[async_trait]
pub trait BundleSender: Send + Sync + 'static {
pub(crate) trait BundleSender: Send + Sync + 'static {
async fn send_bundles_in_loop(&mut self);
}

#[derive(Debug)]
pub struct Settings {
pub replacement_fee_percent_increase: u64,
pub max_fee_increases: u64,
pub(crate) struct Settings {
pub(crate) replacement_fee_percent_increase: u64,
pub(crate) max_fee_increases: u64,
}

#[derive(Debug)]
pub struct BundleSenderImpl<P, E, T, C>
pub(crate) struct BundleSenderImpl<P, E, T, C>
where
P: BundleProposer,
E: EntryPoint,
Expand Down Expand Up @@ -210,7 +210,7 @@ where
C: PoolServer,
{
#[allow(clippy::too_many_arguments)]
pub fn new(
pub(crate) fn new(
manual_bundling_mode: Arc<AtomicBool>,
send_bundle_receiver: mpsc::Receiver<SendBundleRequest>,
chain_id: u64,
Expand Down Expand Up @@ -362,7 +362,6 @@ where
TrackerUpdate::Mined {
tx_hash,
nonce,
gas_fees: _,
block_number,
attempt_number,
} => {
Expand Down
Loading

0 comments on commit 78cf1af

Please sign in to comment.