-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(host): Add L1 txs, L1 receipts, & L1 precompile hint routes
- Loading branch information
Showing
9 changed files
with
446 additions
and
218 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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,42 @@ | ||
//! Accelerated precompile runner for the host program. | ||
use alloy_primitives::{Address, Bytes}; | ||
use anyhow::{anyhow, Result}; | ||
use revm::{ | ||
precompile::{self, PrecompileWithAddress}, | ||
primitives::{Env, Precompile}, | ||
}; | ||
|
||
/// List of precompiles that are accelerated by the host program. | ||
pub(crate) const ACCELERATED_PRECOMPILES: &[PrecompileWithAddress] = &[ | ||
precompile::secp256k1::ECRECOVER, // ecRecover | ||
precompile::bn128::pair::ISTANBUL, // ecPairing | ||
precompile::kzg_point_evaluation::POINT_EVALUATION, // KZG point evaluation | ||
]; | ||
|
||
/// Executes an accelerated precompile on [revm]. | ||
pub(crate) fn execute<T: Into<Bytes>>(address: Address, input: T) -> Result<Vec<u8>> { | ||
if let Some(precompile) = | ||
ACCELERATED_PRECOMPILES.iter().find(|precompile| precompile.0 == address) | ||
{ | ||
match precompile.1 { | ||
Precompile::Standard(std_precompile) => { | ||
// Standard precompile execution - no access to environment required. | ||
let (_, result) = std_precompile(&input.into(), u64::MAX) | ||
.map_err(|e| anyhow!("Failed precompile execution: {e}"))?; | ||
|
||
Ok(result.to_vec()) | ||
} | ||
Precompile::Env(env_precompile) => { | ||
// Use default environment for KZG point evaluation. | ||
let (_, result) = env_precompile(&input.into(), u64::MAX, &Env::default()) | ||
.map_err(|e| anyhow!("Failed precompile execution: {e}"))?; | ||
|
||
Ok(result.to_vec()) | ||
} | ||
_ => anyhow::bail!("Precompile not accelerated"), | ||
} | ||
} else { | ||
anyhow::bail!("Precompile not accelerated"); | ||
} | ||
} |
This file contains 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 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 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 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 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,51 @@ | ||
//! Utilities for `kona-mpt` | ||
use alloc::vec::Vec; | ||
use alloy_rlp::{BufMut, Encodable}; | ||
use alloy_trie::{HashBuilder, Nibbles}; | ||
|
||
/// Compute a trie root of the collection of items with a custom encoder. | ||
pub fn ordered_trie_with_encoder<T, F>(items: &[T], mut encode: F) -> HashBuilder | ||
where | ||
F: FnMut(&T, &mut dyn BufMut), | ||
{ | ||
let mut index_buffer = Vec::new(); | ||
let mut value_buffer = Vec::new(); | ||
let items_len = items.len(); | ||
|
||
// Store preimages for all intermediates | ||
let path_nibbles = (0..items_len) | ||
.map(|i| { | ||
let i = adjust_index_for_rlp(i, items_len); | ||
index_buffer.clear(); | ||
i.encode(&mut index_buffer); | ||
Nibbles::unpack(&index_buffer) | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
let mut hb = HashBuilder::default().with_proof_retainer(path_nibbles); | ||
for i in 0..items_len { | ||
let index = adjust_index_for_rlp(i, items_len); | ||
|
||
index_buffer.clear(); | ||
index.encode(&mut index_buffer); | ||
|
||
value_buffer.clear(); | ||
encode(&items[index], &mut value_buffer); | ||
|
||
hb.add_leaf(Nibbles::unpack(&index_buffer), &value_buffer); | ||
} | ||
|
||
hb | ||
} | ||
|
||
/// Adjust the index of an item for rlp encoding. | ||
pub(crate) const fn adjust_index_for_rlp(i: usize, len: usize) -> usize { | ||
if i > 0x7f { | ||
i | ||
} else if i == 0x7f || i + 1 == len { | ||
0 | ||
} else { | ||
i + 1 | ||
} | ||
} |