-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Sim Crate #33
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
Merged
Merged
feat: Sim Crate #33
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ff0cf98
feat: sim env
prestwich 5e58d54
chore: remove inst
prestwich a34d84b
chore: gas_limit enforcement in simulation
prestwich 28de797
feat: new fn
prestwich 6e0e931
chore: move aliases up
prestwich 174f3c1
chore: remove unused deps
prestwich 6803d0d
lint: add all clippies to sim lib
prestwich 5b47a0e
fix: better instantiators, remove duplicated finish_by
prestwich f9b9b96
fix: add a test :)
prestwich 74f1596
fix: send fut build
prestwich 4d15d32
fix: break loop
prestwich a3ba3c8
chore: cleanup and clippy
prestwich a7e626e
fix: bad tx scoring
prestwich 726ea61
docs: more of em
prestwich badf52f
fix: some docs and built api
prestwich 4e11e22
fix: simplify changed
prestwich 80de142
lint: clippy
prestwich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[package] | ||
name = "signet-sim" | ||
version.workspace = true | ||
edition.workspace = true | ||
rust-version.workspace = true | ||
authors.workspace = true | ||
license.workspace = true | ||
homepage.workspace = true | ||
repository.workspace = true | ||
|
||
[dependencies] | ||
alloy.workspace = true | ||
signet-bundle.workspace = true | ||
signet-evm.workspace = true | ||
signet-types.workspace = true | ||
signet-zenith.workspace = true | ||
tokio.workspace = true | ||
tracing.workspace = true | ||
trevm.workspace = true | ||
|
||
[dev-dependencies] | ||
signet-types = { workspace = true, features = ["test-utils"] } | ||
signet-evm = { workspace = true, features = ["test-utils"] } | ||
tracing-subscriber.workspace = true | ||
|
||
[features] | ||
test-utils = ["signet-types/test-utils", "signet-evm/test-utils"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
use alloy::{ | ||
consensus::{SidecarBuilder, SidecarCoder, TxEnvelope}, | ||
eips::Decodable2718, | ||
primitives::{keccak256, Bytes, B256}, | ||
rlp::Buf, | ||
}; | ||
use core::fmt; | ||
use signet_bundle::SignetEthBundle; | ||
use signet_zenith::{encode_txns, Alloy2718Coder, SignedOrder}; | ||
use std::sync::OnceLock; | ||
use tracing::{error, trace}; | ||
|
||
use crate::{outcome::SimulatedItem, SimItem}; | ||
|
||
/// A block that has been built by the simulator. | ||
#[derive(Clone, Default)] | ||
pub struct BuiltBlock { | ||
/// The host fill actions. | ||
pub(crate) host_fills: Vec<SignedOrder>, | ||
/// Transactions in the block. | ||
pub(crate) transactions: Vec<TxEnvelope>, | ||
|
||
/// The amount of gas used by the block so far | ||
pub(crate) gas_used: u64, | ||
|
||
/// Memoized raw encoding of the block. | ||
pub(crate) raw_encoding: OnceLock<Bytes>, | ||
/// Memoized hash of the block. | ||
pub(crate) hash: OnceLock<B256>, | ||
} | ||
|
||
impl fmt::Debug for BuiltBlock { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("BuiltBlock") | ||
.field("host_fills", &self.host_fills.len()) | ||
.field("transactions", &self.transactions.len()) | ||
.field("gas_used", &self.gas_used) | ||
.finish() | ||
} | ||
} | ||
|
||
impl BuiltBlock { | ||
/// Create a new `BuiltBlock` | ||
pub const fn new() -> Self { | ||
Self { | ||
host_fills: Vec::new(), | ||
transactions: Vec::new(), | ||
gas_used: 0, | ||
raw_encoding: OnceLock::new(), | ||
hash: OnceLock::new(), | ||
} | ||
} | ||
|
||
/// Get the amount of gas used by the block. | ||
pub const fn gas_used(&self) -> u64 { | ||
self.gas_used | ||
} | ||
|
||
/// Get the number of transactions in the block. | ||
pub fn tx_count(&self) -> usize { | ||
self.transactions.len() | ||
} | ||
|
||
/// Check if the block is empty. | ||
pub fn is_empty(&self) -> bool { | ||
self.transactions.is_empty() | ||
} | ||
|
||
/// Get the current list of transactions included in this block. | ||
#[allow(clippy::missing_const_for_fn)] // false positive, const deref | ||
pub fn transactions(&self) -> &[TxEnvelope] { | ||
&self.transactions | ||
} | ||
|
||
/// Unseal the block | ||
pub(crate) fn unseal(&mut self) { | ||
self.raw_encoding.take(); | ||
self.hash.take(); | ||
} | ||
|
||
/// Seal the block by encoding the transactions and calculating the hash of | ||
/// the block contents. | ||
pub(crate) fn seal(&self) { | ||
self.raw_encoding.get_or_init(|| encode_txns::<Alloy2718Coder>(&self.transactions).into()); | ||
self.hash.get_or_init(|| keccak256(self.raw_encoding.get().unwrap().as_ref())); | ||
} | ||
|
||
/// Ingest a transaction into the in-progress block. | ||
pub fn ingest_tx(&mut self, tx: TxEnvelope) { | ||
trace!(hash = %tx.tx_hash(), "ingesting tx"); | ||
self.unseal(); | ||
self.transactions.push(tx); | ||
} | ||
|
||
/// Ingest a bundle into the in-progress block. | ||
/// Ignores Signed Orders for now. | ||
pub fn ingest_bundle(&mut self, bundle: SignetEthBundle) { | ||
trace!(replacement_uuid = bundle.replacement_uuid(), "adding bundle to block"); | ||
|
||
let txs = bundle | ||
.bundle | ||
.txs | ||
.into_iter() | ||
.map(|tx| TxEnvelope::decode_2718(&mut tx.chunk())) | ||
.collect::<Result<Vec<_>, _>>(); | ||
|
||
if let Ok(txs) = txs { | ||
self.unseal(); | ||
// extend the transactions with the decoded transactions. | ||
// As this builder does not provide bundles landing "top of block", its fine to just extend. | ||
self.transactions.extend(txs); | ||
|
||
if let Some(host_fills) = bundle.host_fills { | ||
self.host_fills.push(host_fills); | ||
} | ||
} else { | ||
error!("failed to decode bundle. dropping"); | ||
} | ||
} | ||
|
||
/// Ingest a simulated item, extending the block. | ||
pub fn ingest(&mut self, item: SimulatedItem) { | ||
prestwich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.gas_used += item.gas_used; | ||
|
||
match item.item { | ||
SimItem::Bundle(bundle) => self.ingest_bundle(bundle), | ||
SimItem::Tx(tx) => self.ingest_tx(tx), | ||
} | ||
} | ||
|
||
/// Encode the in-progress block. | ||
pub(crate) fn encode_raw(&self) -> &Bytes { | ||
self.seal(); | ||
self.raw_encoding.get().unwrap() | ||
} | ||
|
||
/// Calculate the hash of the in-progress block, finishing the block. | ||
pub fn contents_hash(&self) -> &B256 { | ||
self.seal(); | ||
self.hash.get().unwrap() | ||
} | ||
|
||
/// Convert the in-progress block to sign request contents. | ||
pub fn encode_calldata(&self) -> &Bytes { | ||
self.encode_raw() | ||
} | ||
|
||
/// Convert the in-progress block to a blob transaction sidecar. | ||
pub fn encode_blob<T: SidecarCoder + Default>(&self) -> SidecarBuilder<T> { | ||
let mut coder = SidecarBuilder::<T>::default(); | ||
coder.ingest(self.encode_raw()); | ||
coder | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.