Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit 0b7a895

Browse files
committed
chore: do some stuff :)
1 parent 86d0039 commit 0b7a895

File tree

9 files changed

+694
-447
lines changed

9 files changed

+694
-447
lines changed

Cargo.lock

Lines changed: 556 additions & 282 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
# trevm
8-
trevm = { version = "0.14", features = ["full_env_cfg"] }
8+
trevm = { version = "0.15", features = ["full_env_cfg"] }
99

1010
# Alloy base crates
1111
alloy-primitives = "0.8"

src/db_connect.rs

Lines changed: 0 additions & 157 deletions
This file was deleted.
File renamed without changes.

src/extractor/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mod example;
2+
3+
mod r#trait;
4+
pub use r#trait::BlockExtractor;
File renamed without changes.

src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
#![allow(async_fn_in_trait)]
22

3-
mod db_connect;
4-
pub use db_connect::{DbConnect, EvmFactory, EvmParts};
5-
63
mod extractor;
74
pub use extractor::BlockExtractor;
85

9-
mod example;
10-
116
mod pool;
127
pub use pool::{Best, EvmPool};
138

149
mod shared;
1510
pub use shared::{Child, ConcurrentCache, Root};
11+
12+
mod new;

src/new.rs

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
use alloy_primitives::U256;
2+
use std::{
3+
future::Future,
4+
sync::{Arc, Weak},
5+
};
6+
use tokio::{sync::Semaphore, task::JoinSet};
7+
use trevm::{revm::primitives::ResultAndState, Block, Cfg, EvmFactory, Tx};
8+
9+
/// A trait for extracting transactions from a block.
10+
#[derive(Debug, Clone)]
11+
pub struct EvmCtxInner<Ef, C, B> {
12+
evm_factory: Ef,
13+
cfg: C,
14+
block: B,
15+
}
16+
17+
#[derive(Debug, Clone)]
18+
pub struct EvmCtx<Ef, C, B>(Arc<EvmCtxInner<Ef, C, B>>);
19+
20+
pub struct EvmPool<Ef, C, B> {
21+
evm: EvmCtx<Ef, C, B>,
22+
}
23+
24+
impl<Ef, C, B> EvmPool<Ef, C, B>
25+
where
26+
Ef: for<'a> EvmFactory<'a> + Send + 'static,
27+
C: Cfg + 'static,
28+
B: Block + 'static,
29+
{
30+
fn weak_evm(&self) -> Weak<EvmCtxInner<Ef, C, B>> {
31+
Arc::downgrade(&self.evm.0)
32+
}
33+
34+
fn spawn_eval<T, F>(
35+
&self,
36+
tx: Weak<T>,
37+
evaluator: F,
38+
) -> tokio::task::JoinHandle<Option<Best<T>>>
39+
where
40+
T: Tx + 'static,
41+
F: Fn(&ResultAndState) -> U256 + Send + Sync + 'static,
42+
{
43+
let evm = self.weak_evm();
44+
tokio::task::spawn_blocking(|| eval_fn(evm, tx, evaluator))
45+
}
46+
}
47+
48+
fn eval_fn<Ef, C, B, T, F>(
49+
evm: Weak<EvmCtxInner<Ef, C, B>>,
50+
tx: Weak<T>,
51+
evaluator: F,
52+
) -> Option<Best<T>>
53+
where
54+
Ef: for<'a> EvmFactory<'a> + Send + 'static,
55+
C: Cfg + 'static,
56+
B: Block + 'static,
57+
T: Tx + 'static,
58+
F: Fn(&ResultAndState) -> U256 + Send + Sync + 'static,
59+
{
60+
// If none, then simulation is over.
61+
let evm = evm.upgrade()?;
62+
// If none, tx can be skipped
63+
let tx = tx.upgrade()?;
64+
65+
// If none, then tx errored, and can be skipped.
66+
let result = evm
67+
.evm_factory
68+
.run(&evm.cfg, &evm.block, tx.as_ref())
69+
.ok()?;
70+
71+
let score = evaluator(&result);
72+
Some(Best { tx, result, score })
73+
}
74+
75+
pub struct Best<T, Score: PartialOrd + Ord = U256> {
76+
pub tx: Arc<T>,
77+
pub result: ResultAndState,
78+
pub score: Score,
79+
}
80+
81+
impl<Ef, C, B> EvmPool<Ef, C, B>
82+
where
83+
Ef: for<'a> EvmFactory<'a> + Send + 'static,
84+
C: Cfg + 'static,
85+
B: Block + 'static,
86+
{
87+
pub fn spawn<T, F>(
88+
self,
89+
mut rx: tokio::sync::mpsc::Receiver<Arc<T>>,
90+
evaluator: F,
91+
deadline: tokio::time::Instant,
92+
) -> tokio::task::JoinHandle<Option<Best<T>>>
93+
where
94+
T: Tx + 'static,
95+
F: Fn(&ResultAndState) -> U256 + Send + Sync + 'static + Clone,
96+
{
97+
tokio::spawn(async move {
98+
let mut futs = JoinSet::new();
99+
let sleep = tokio::time::sleep_until(deadline);
100+
tokio::pin!(sleep);
101+
102+
let mut best: Option<Best<T>> = None;
103+
104+
loop {
105+
tokio::select! {
106+
biased;
107+
_ = &mut sleep => break,
108+
tx = rx.recv() => {
109+
let tx = match tx {
110+
Some(tx) => tx,
111+
None => break,
112+
};
113+
114+
let weak_tx = Arc::downgrade(&tx);
115+
let evm = self.weak_evm();
116+
let eval = evaluator.clone();
117+
futs.spawn_blocking(|| eval_fn(evm, weak_tx, eval));
118+
}
119+
Some(Ok(Some(candidate))) = futs.join_next() => {
120+
if candidate.score > best.as_ref().map(|b| b.score).unwrap_or_default() {
121+
best = Some(candidate);
122+
}
123+
}
124+
}
125+
}
126+
best
127+
})
128+
}
129+
}

src/pool.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::db_connect::EvmFactory;
21
use alloy_primitives::U256;
32
use rayon::{prelude::*, ThreadPool};
3+
use trevm::EvmFactory;
44
use trevm::{revm::primitives::ResultAndState, Block, Cfg, Tx};
55

66
pub struct EvmPool<EF, C, B> {
@@ -18,7 +18,7 @@ pub struct Best<'a, T, Score: PartialOrd + Ord = U256> {
1818

1919
impl<EF, C, B> EvmPool<EF, C, B>
2020
where
21-
EF: EvmFactory,
21+
EF: for<'a> EvmFactory<'a>,
2222
C: Cfg,
2323
B: Block,
2424
{

0 commit comments

Comments
 (0)