diff --git a/bin/client/src/interop/util.rs b/bin/client/src/interop/util.rs index b42764350..03f05c9db 100644 --- a/bin/client/src/interop/util.rs +++ b/bin/client/src/interop/util.rs @@ -30,13 +30,13 @@ where } }; - caching_oracle - .write(&HintType::L2OutputRoot.encode_with(&[ + HintType::L2OutputRoot + .with_data(&[ rich_output.output_root.as_slice(), rich_output.chain_id.to_be_bytes().as_slice(), - ])) - .await - .map_err(OracleProviderError::Preimage)?; + ]) + .send(caching_oracle) + .await?; let output_preimage = caching_oracle .get(PreimageKey::new(*rich_output.output_root, PreimageKeyType::Keccak256)) .await diff --git a/bin/client/src/precompiles/bls12.rs b/bin/client/src/precompiles/bls12.rs index 60ef6d6d8..22aaf12cb 100644 --- a/bin/client/src/precompiles/bls12.rs +++ b/bin/client/src/precompiles/bls12.rs @@ -10,8 +10,7 @@ use crate::{HINT_WRITER, ORACLE_READER}; use alloc::{string::ToString, vec::Vec}; use alloy_primitives::{address, keccak256, Address, Bytes}; use kona_preimage::{ - errors::PreimageOracleError, HintWriterClient, PreimageKey, PreimageKeyType, - PreimageOracleClient, + errors::PreimageOracleError, PreimageKey, PreimageKeyType, PreimageOracleClient, }; use kona_proof::{errors::OracleProviderError, HintType}; use revm::{ @@ -57,10 +56,7 @@ fn fpvm_bls12_pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult { let result_data = kona_proof::block_on(async move { // Write the hint for the ecrecover precompile run. let hint_data = &[BLS12_PAIRING_CHECK.as_ref(), input.as_ref()]; - HINT_WRITER - .write(&HintType::L1Precompile.encode_with(hint_data)) - .await - .map_err(OracleProviderError::Preimage)?; + HintType::L1Precompile.with_data(hint_data).send(&HINT_WRITER).await?; // Construct the key hash for the ecrecover precompile run. let raw_key_data = hint_data.iter().copied().flatten().copied().collect::>(); diff --git a/bin/client/src/precompiles/bn128_pair.rs b/bin/client/src/precompiles/bn128_pair.rs index a62eff284..5501d0c88 100644 --- a/bin/client/src/precompiles/bn128_pair.rs +++ b/bin/client/src/precompiles/bn128_pair.rs @@ -4,8 +4,7 @@ use crate::{HINT_WRITER, ORACLE_READER}; use alloc::{string::ToString, vec::Vec}; use alloy_primitives::{keccak256, Address, Bytes}; use kona_preimage::{ - errors::PreimageOracleError, HintWriterClient, PreimageKey, PreimageKeyType, - PreimageOracleClient, + errors::PreimageOracleError, PreimageKey, PreimageKeyType, PreimageOracleClient, }; use kona_proof::{errors::OracleProviderError, HintType}; use revm::{ @@ -41,10 +40,7 @@ fn fpvm_ecpairing(input: &Bytes, gas_limit: u64) -> PrecompileResult { let result_data = kona_proof::block_on(async move { // Write the hint for the ecrecover precompile run. let hint_data = &[ECPAIRING_ADDRESS.as_ref(), input.as_ref()]; - HINT_WRITER - .write(&HintType::L1Precompile.encode_with(hint_data)) - .await - .map_err(OracleProviderError::Preimage)?; + HintType::L1Precompile.with_data(hint_data).send(&HINT_WRITER).await?; // Construct the key hash for the ecrecover precompile run. let raw_key_data = hint_data.iter().copied().flatten().copied().collect::>(); diff --git a/bin/client/src/precompiles/ecrecover.rs b/bin/client/src/precompiles/ecrecover.rs index cf0a78976..3c09df274 100644 --- a/bin/client/src/precompiles/ecrecover.rs +++ b/bin/client/src/precompiles/ecrecover.rs @@ -4,8 +4,7 @@ use crate::{HINT_WRITER, ORACLE_READER}; use alloc::{string::ToString, vec::Vec}; use alloy_primitives::{keccak256, Address, Bytes}; use kona_preimage::{ - errors::PreimageOracleError, HintWriterClient, PreimageKey, PreimageKeyType, - PreimageOracleClient, + errors::PreimageOracleError, PreimageKey, PreimageKeyType, PreimageOracleClient, }; use kona_proof::{errors::OracleProviderError, HintType}; use revm::{ @@ -29,10 +28,7 @@ fn fpvm_ecrecover(input: &Bytes, gas_limit: u64) -> PrecompileResult { let result_data = kona_proof::block_on(async move { // Write the hint for the ecrecover precompile run. let hint_data = &[ECRECOVER_ADDRESS.as_ref(), input.as_ref()]; - HINT_WRITER - .write(&HintType::L1Precompile.encode_with(hint_data)) - .await - .map_err(OracleProviderError::Preimage)?; + HintType::L1Precompile.with_data(hint_data).send(&HINT_WRITER).await?; // Construct the key hash for the ecrecover precompile run. let raw_key_data = hint_data.iter().copied().flatten().copied().collect::>(); diff --git a/bin/client/src/precompiles/kzg_point_eval.rs b/bin/client/src/precompiles/kzg_point_eval.rs index 84b04d61b..986db290b 100644 --- a/bin/client/src/precompiles/kzg_point_eval.rs +++ b/bin/client/src/precompiles/kzg_point_eval.rs @@ -4,8 +4,7 @@ use crate::{HINT_WRITER, ORACLE_READER}; use alloc::{string::ToString, vec::Vec}; use alloy_primitives::{keccak256, Address, Bytes}; use kona_preimage::{ - errors::PreimageOracleError, HintWriterClient, PreimageKey, PreimageKeyType, - PreimageOracleClient, + errors::PreimageOracleError, PreimageKey, PreimageKeyType, PreimageOracleClient, }; use kona_proof::{errors::OracleProviderError, HintType}; use revm::{ @@ -33,10 +32,7 @@ fn fpvm_kzg_point_eval(input: &Bytes, gas_limit: u64) -> PrecompileResult { let result_data = kona_proof::block_on(async move { // Write the hint for the ecrecover precompile run. let hint_data = &[POINT_EVAL_ADDRESS.as_ref(), input.as_ref()]; - HINT_WRITER - .write(&HintType::L1Precompile.encode_with(hint_data)) - .await - .map_err(OracleProviderError::Preimage)?; + HintType::L1Precompile.with_data(hint_data).send(&HINT_WRITER).await?; // Construct the key hash for the ecrecover precompile run. let raw_key_data = hint_data.iter().copied().flatten().copied().collect::>(); diff --git a/bin/client/src/single.rs b/bin/client/src/single.rs index 3a00a6fc5..bdf25da3d 100644 --- a/bin/client/src/single.rs +++ b/bin/client/src/single.rs @@ -6,7 +6,7 @@ use alloy_primitives::B256; use core::fmt::Debug; use kona_driver::{Driver, DriverError}; use kona_executor::{ExecutorError, KonaHandleRegister, TrieDBProvider}; -use kona_preimage::{CommsClient, HintWriterClient, PreimageKeyType, PreimageOracleClient}; +use kona_preimage::{CommsClient, HintWriterClient, PreimageKey, PreimageOracleClient}; use kona_proof::{ errors::OracleProviderError, executor::KonaExecutor, @@ -162,12 +162,11 @@ where { let mut output_preimage = [0u8; 128]; HintType::StartingL2Output - .get_exact_preimage( - caching_oracle, - agreed_l2_output_root, - PreimageKeyType::Keccak256, - &mut output_preimage, - ) + .with_data(&[agreed_l2_output_root.as_ref()]) + .send(caching_oracle) + .await?; + caching_oracle + .get_exact(PreimageKey::new_keccak256(*agreed_l2_output_root), output_preimage.as_mut()) .await?; output_preimage[96..128].try_into().map_err(OracleProviderError::SliceConversion) diff --git a/bin/host/src/interop/handler.rs b/bin/host/src/interop/handler.rs index aecc0375b..ff54c5e46 100644 --- a/bin/host/src/interop/handler.rs +++ b/bin/host/src/interop/handler.rs @@ -34,11 +34,11 @@ impl HintHandler for InteropHintHandler { providers: &::Providers, kv: SharedKeyValueStore, ) -> Result<()> { - match hint.hint_type { + match hint.ty { HintType::L1BlockHeader => { - ensure!(hint.hint_data.len() == 32, "Invalid hint data length"); + ensure!(hint.data.len() == 32, "Invalid hint data length"); - let hash: B256 = hint.hint_data.as_ref().try_into()?; + let hash: B256 = hint.data.as_ref().try_into()?; let raw_header: Bytes = providers.l1.client().request("debug_getRawHeader", [hash]).await?; @@ -46,9 +46,9 @@ impl HintHandler for InteropHintHandler { kv_lock.set(PreimageKey::new_keccak256(*hash).into(), raw_header.into())?; } HintType::L1Transactions => { - ensure!(hint.hint_data.len() == 32, "Invalid hint data length"); + ensure!(hint.data.len() == 32, "Invalid hint data length"); - let hash: B256 = hint.hint_data.as_ref().try_into()?; + let hash: B256 = hint.data.as_ref().try_into()?; let Block { transactions, .. } = providers .l1 .get_block_by_hash(hash, BlockTransactionsKind::Full) @@ -62,20 +62,20 @@ impl HintHandler for InteropHintHandler { store_ordered_trie(kv.as_ref(), encoded_transactions.as_slice()).await?; } HintType::L1Receipts => { - ensure!(hint.hint_data.len() == 32, "Invalid hint data length"); + ensure!(hint.data.len() == 32, "Invalid hint data length"); - let hash: B256 = hint.hint_data.as_ref().try_into()?; + let hash: B256 = hint.data.as_ref().try_into()?; let raw_receipts: Vec = providers.l1.client().request("debug_getRawReceipts", [hash]).await?; store_ordered_trie(kv.as_ref(), raw_receipts.as_slice()).await?; } HintType::L1Blob => { - ensure!(hint.hint_data.len() == 48, "Invalid hint data length"); + ensure!(hint.data.len() == 48, "Invalid hint data length"); - let hash_data_bytes: [u8; 32] = hint.hint_data[0..32].try_into()?; - let index_data_bytes: [u8; 8] = hint.hint_data[32..40].try_into()?; - let timestamp_data_bytes: [u8; 8] = hint.hint_data[40..48].try_into()?; + let hash_data_bytes: [u8; 32] = hint.data[0..32].try_into()?; + let index_data_bytes: [u8; 8] = hint.data[32..40].try_into()?; + let timestamp_data_bytes: [u8; 8] = hint.data[40..48].try_into()?; let hash: B256 = hash_data_bytes.into(); let index = u64::from_be_bytes(index_data_bytes); @@ -132,11 +132,11 @@ impl HintHandler for InteropHintHandler { )?; } HintType::L1Precompile => { - ensure!(hint.hint_data.len() >= 20, "Invalid hint data length"); + ensure!(hint.data.len() >= 20, "Invalid hint data length"); - let address = Address::from_slice(&hint.hint_data.as_ref()[..20]); - let input = hint.hint_data[20..].to_vec(); - let input_hash = keccak256(hint.hint_data.as_ref()); + let address = Address::from_slice(&hint.data.as_ref()[..20]); + let input = hint.data[20..].to_vec(); + let input_hash = keccak256(hint.data.as_ref()); let result = crate::eth::execute(address, input).map_or_else( |_| vec![0u8; 1], @@ -149,17 +149,16 @@ impl HintHandler for InteropHintHandler { ); let mut kv_lock = kv.write().await; - kv_lock - .set(PreimageKey::new_keccak256(*input_hash).into(), hint.hint_data.into())?; + kv_lock.set(PreimageKey::new_keccak256(*input_hash).into(), hint.data.into())?; kv_lock.set( PreimageKey::new(*input_hash, PreimageKeyType::Precompile).into(), result, )?; } HintType::AgreedPreState => { - ensure!(hint.hint_data.len() == 32, "Invalid hint data length"); + ensure!(hint.data.len() == 32, "Invalid hint data length"); - let hash: B256 = hint.hint_data.as_ref().try_into()?; + let hash: B256 = hint.data.as_ref().try_into()?; if hash != keccak256(cfg.agreed_l2_pre_state.as_ref()) { anyhow::bail!("Agreed pre-state hash does not match."); @@ -176,13 +175,10 @@ impl HintHandler for InteropHintHandler { const L2_TO_L1_MESSAGE_PASSER_ADDRESS: Address = address!("4200000000000000000000000000000000000016"); - ensure!( - hint.hint_data.len() >= 32 && hint.hint_data.len() <= 40, - "Invalid hint data length" - ); + ensure!(hint.data.len() >= 32 && hint.data.len() <= 40, "Invalid hint data length"); - let hash = B256::from_slice(&hint.hint_data.as_ref()[0..32]); - let chain_id = u64::from_be_bytes(hint.hint_data.as_ref()[32..40].try_into()?); + let hash = B256::from_slice(&hint.data.as_ref()[0..32]); + let chain_id = u64::from_be_bytes(hint.data.as_ref()[32..40].try_into()?); let l2_provider = providers.l2(&chain_id)?; // Decode the pre-state to determine the timestamp of the block. @@ -237,14 +233,11 @@ impl HintHandler for InteropHintHandler { kv_lock.set(PreimageKey::new_keccak256(*output_root).into(), raw_output.into())?; } HintType::L2BlockHeader => { - ensure!( - hint.hint_data.len() >= 32 && hint.hint_data.len() <= 40, - "Invalid hint data length" - ); + ensure!(hint.data.len() >= 32 && hint.data.len() <= 40, "Invalid hint data length"); - let hash: B256 = hint.hint_data.as_ref()[..32].try_into()?; - let chain_id = if hint.hint_data.len() == 40 { - u64::from_be_bytes(hint.hint_data[32..40].try_into()?) + let hash: B256 = hint.data.as_ref()[..32].try_into()?; + let chain_id = if hint.data.len() == 40 { + u64::from_be_bytes(hint.data[32..40].try_into()?) } else { cfg.active_l2_chain_id()? }; @@ -256,14 +249,11 @@ impl HintHandler for InteropHintHandler { kv_lock.set(PreimageKey::new_keccak256(*hash).into(), raw_header.into())?; } HintType::L2Transactions => { - ensure!( - hint.hint_data.len() >= 32 && hint.hint_data.len() <= 40, - "Invalid hint data length" - ); + ensure!(hint.data.len() >= 32 && hint.data.len() <= 40, "Invalid hint data length"); - let hash: B256 = hint.hint_data.as_ref()[..32].try_into()?; - let chain_id = if hint.hint_data.len() == 40 { - u64::from_be_bytes(hint.hint_data[32..40].try_into()?) + let hash: B256 = hint.data.as_ref()[..32].try_into()?; + let chain_id = if hint.data.len() == 40 { + u64::from_be_bytes(hint.data[32..40].try_into()?) } else { cfg.active_l2_chain_id()? }; @@ -281,14 +271,11 @@ impl HintHandler for InteropHintHandler { store_ordered_trie(kv.as_ref(), encoded_transactions.as_slice()).await?; } HintType::L2Receipts => { - ensure!( - hint.hint_data.len() >= 32 && hint.hint_data.len() <= 40, - "Invalid hint data length" - ); + ensure!(hint.data.len() >= 32 && hint.data.len() <= 40, "Invalid hint data length"); - let hash: B256 = hint.hint_data.as_ref()[..32].try_into()?; - let chain_id = if hint.hint_data.len() == 40 { - u64::from_be_bytes(hint.hint_data[32..40].try_into()?) + let hash: B256 = hint.data.as_ref()[..32].try_into()?; + let chain_id = if hint.data.len() == 40 { + u64::from_be_bytes(hint.data[32..40].try_into()?) } else { cfg.active_l2_chain_id()? }; @@ -305,14 +292,11 @@ impl HintHandler for InteropHintHandler { // geth hashdb scheme code hash key prefix const CODE_PREFIX: u8 = b'c'; - ensure!( - hint.hint_data.len() >= 32 && hint.hint_data.len() <= 40, - "Invalid hint data length" - ); + ensure!(hint.data.len() >= 32 && hint.data.len() <= 40, "Invalid hint data length"); - let hash: B256 = hint.hint_data[..32].as_ref().try_into()?; - let chain_id = if hint.hint_data.len() == 40 { - u64::from_be_bytes(hint.hint_data[32..40].try_into()?) + let hash: B256 = hint.data[..32].as_ref().try_into()?; + let chain_id = if hint.data.len() == 40 { + u64::from_be_bytes(hint.data[32..40].try_into()?) } else { cfg.active_l2_chain_id()? }; @@ -340,14 +324,11 @@ impl HintHandler for InteropHintHandler { kv_lock.set(PreimageKey::new_keccak256(*hash).into(), code.into())?; } HintType::L2StateNode => { - ensure!( - hint.hint_data.len() >= 32 && hint.hint_data.len() <= 40, - "Invalid hint data length" - ); + ensure!(hint.data.len() >= 32 && hint.data.len() <= 40, "Invalid hint data length"); - let hash: B256 = hint.hint_data.as_ref().try_into()?; - let chain_id = if hint.hint_data.len() == 40 { - u64::from_be_bytes(hint.hint_data[32..40].try_into()?) + let hash: B256 = hint.data.as_ref().try_into()?; + let chain_id = if hint.data.len() == 40 { + u64::from_be_bytes(hint.data[32..40].try_into()?) } else { cfg.active_l2_chain_id()? }; @@ -361,14 +342,14 @@ impl HintHandler for InteropHintHandler { } HintType::L2AccountProof => { ensure!( - hint.hint_data.len() >= 8 + 20 && hint.hint_data.len() <= 8 + 20 + 8, + hint.data.len() >= 8 + 20 && hint.data.len() <= 8 + 20 + 8, "Invalid hint data length" ); - let block_number = u64::from_be_bytes(hint.hint_data.as_ref()[..8].try_into()?); - let address = Address::from_slice(&hint.hint_data.as_ref()[8..28]); - let chain_id = if hint.hint_data.len() == 8 + 20 + 8 { - u64::from_be_bytes(hint.hint_data[28..].try_into()?) + let block_number = u64::from_be_bytes(hint.data.as_ref()[..8].try_into()?); + let address = Address::from_slice(&hint.data.as_ref()[8..28]); + let chain_id = if hint.data.len() == 8 + 20 + 8 { + u64::from_be_bytes(hint.data[28..].try_into()?) } else { cfg.active_l2_chain_id()? }; @@ -390,15 +371,15 @@ impl HintHandler for InteropHintHandler { } HintType::L2AccountStorageProof => { ensure!( - hint.hint_data.len() >= 8 + 20 + 32 && hint.hint_data.len() <= 8 + 20 + 32 + 8, + hint.data.len() >= 8 + 20 + 32 && hint.data.len() <= 8 + 20 + 32 + 8, "Invalid hint data length" ); - let block_number = u64::from_be_bytes(hint.hint_data.as_ref()[..8].try_into()?); - let address = Address::from_slice(&hint.hint_data.as_ref()[8..28]); - let slot = B256::from_slice(&hint.hint_data.as_ref()[28..]); - let chain_id = if hint.hint_data.len() == 8 + 20 + 32 + 8 { - u64::from_be_bytes(hint.hint_data[60..].try_into()?) + let block_number = u64::from_be_bytes(hint.data.as_ref()[..8].try_into()?); + let address = Address::from_slice(&hint.data.as_ref()[8..28]); + let slot = B256::from_slice(&hint.data.as_ref()[28..]); + let chain_id = if hint.data.len() == 8 + 20 + 32 + 8 { + u64::from_be_bytes(hint.data[60..].try_into()?) } else { cfg.active_l2_chain_id()? }; diff --git a/bin/host/src/single/handler.rs b/bin/host/src/single/handler.rs index 0b722caf4..86a283db9 100644 --- a/bin/host/src/single/handler.rs +++ b/bin/host/src/single/handler.rs @@ -35,11 +35,11 @@ impl HintHandler for SingleChainHintHandler { providers: &::Providers, kv: SharedKeyValueStore, ) -> Result<()> { - match hint.hint_type { + match hint.ty { HintType::L1BlockHeader => { - ensure!(hint.hint_data.len() == 32, "Invalid hint data length"); + ensure!(hint.data.len() == 32, "Invalid hint data length"); - let hash: B256 = hint.hint_data.as_ref().try_into()?; + let hash: B256 = hint.data.as_ref().try_into()?; let raw_header: Bytes = providers.l1.client().request("debug_getRawHeader", [hash]).await?; @@ -47,9 +47,9 @@ impl HintHandler for SingleChainHintHandler { kv_lock.set(PreimageKey::new_keccak256(*hash).into(), raw_header.into())?; } HintType::L1Transactions => { - ensure!(hint.hint_data.len() == 32, "Invalid hint data length"); + ensure!(hint.data.len() == 32, "Invalid hint data length"); - let hash: B256 = hint.hint_data.as_ref().try_into()?; + let hash: B256 = hint.data.as_ref().try_into()?; let Block { transactions, .. } = providers .l1 .get_block_by_hash(hash, BlockTransactionsKind::Full) @@ -63,20 +63,20 @@ impl HintHandler for SingleChainHintHandler { store_ordered_trie(kv.as_ref(), encoded_transactions.as_slice()).await?; } HintType::L1Receipts => { - ensure!(hint.hint_data.len() == 32, "Invalid hint data length"); + ensure!(hint.data.len() == 32, "Invalid hint data length"); - let hash: B256 = hint.hint_data.as_ref().try_into()?; + let hash: B256 = hint.data.as_ref().try_into()?; let raw_receipts: Vec = providers.l1.client().request("debug_getRawReceipts", [hash]).await?; store_ordered_trie(kv.as_ref(), raw_receipts.as_slice()).await?; } HintType::L1Blob => { - ensure!(hint.hint_data.len() == 48, "Invalid hint data length"); + ensure!(hint.data.len() == 48, "Invalid hint data length"); - let hash_data_bytes: [u8; 32] = hint.hint_data[0..32].try_into()?; - let index_data_bytes: [u8; 8] = hint.hint_data[32..40].try_into()?; - let timestamp_data_bytes: [u8; 8] = hint.hint_data[40..48].try_into()?; + let hash_data_bytes: [u8; 32] = hint.data[0..32].try_into()?; + let index_data_bytes: [u8; 8] = hint.data[32..40].try_into()?; + let timestamp_data_bytes: [u8; 8] = hint.data[40..48].try_into()?; let hash: B256 = hash_data_bytes.into(); let index = u64::from_be_bytes(index_data_bytes); @@ -133,11 +133,11 @@ impl HintHandler for SingleChainHintHandler { )?; } HintType::L1Precompile => { - ensure!(hint.hint_data.len() >= 20, "Invalid hint data length"); + ensure!(hint.data.len() >= 20, "Invalid hint data length"); - let address = Address::from_slice(&hint.hint_data.as_ref()[..20]); - let input = hint.hint_data[20..].to_vec(); - let input_hash = keccak256(hint.hint_data.as_ref()); + let address = Address::from_slice(&hint.data.as_ref()[..20]); + let input = hint.data[20..].to_vec(); + let input_hash = keccak256(hint.data.as_ref()); let result = crate::eth::execute(address, input).map_or_else( |_| vec![0u8; 1], @@ -150,18 +150,17 @@ impl HintHandler for SingleChainHintHandler { ); let mut kv_lock = kv.write().await; - kv_lock - .set(PreimageKey::new_keccak256(*input_hash).into(), hint.hint_data.into())?; + kv_lock.set(PreimageKey::new_keccak256(*input_hash).into(), hint.data.into())?; kv_lock.set( PreimageKey::new(*input_hash, PreimageKeyType::Precompile).into(), result, )?; } HintType::L2BlockHeader => { - ensure!(hint.hint_data.len() == 32, "Invalid hint data length"); + ensure!(hint.data.len() == 32, "Invalid hint data length"); // Fetch the raw header from the L2 chain provider. - let hash: B256 = hint.hint_data.as_ref().try_into()?; + let hash: B256 = hint.data.as_ref().try_into()?; let raw_header: Bytes = providers.l2.client().request("debug_getRawHeader", [hash]).await?; @@ -170,9 +169,9 @@ impl HintHandler for SingleChainHintHandler { kv_lock.set(PreimageKey::new_keccak256(*hash).into(), raw_header.into())?; } HintType::L2Transactions => { - ensure!(hint.hint_data.len() == 32, "Invalid hint data length"); + ensure!(hint.data.len() == 32, "Invalid hint data length"); - let hash: B256 = hint.hint_data.as_ref().try_into()?; + let hash: B256 = hint.data.as_ref().try_into()?; let Block { transactions, .. } = providers .l2 .get_block_by_hash(hash, BlockTransactionsKind::Full) @@ -190,7 +189,7 @@ impl HintHandler for SingleChainHintHandler { const L2_TO_L1_MESSAGE_PASSER_ADDRESS: Address = address!("4200000000000000000000000000000000000016"); - ensure!(hint.hint_data.len() == 32, "Invalid hint data length"); + ensure!(hint.data.len() == 32, "Invalid hint data length"); // Fetch the header for the L2 head block. let raw_header: Bytes = providers @@ -227,9 +226,9 @@ impl HintHandler for SingleChainHintHandler { // geth hashdb scheme code hash key prefix const CODE_PREFIX: u8 = b'c'; - ensure!(hint.hint_data.len() == 32, "Invalid hint data length"); + ensure!(hint.data.len() == 32, "Invalid hint data length"); - let hash: B256 = hint.hint_data.as_ref().try_into()?; + let hash: B256 = hint.data.as_ref().try_into()?; // Attempt to fetch the code from the L2 chain provider. let code_key = [&[CODE_PREFIX], hash.as_slice()].concat(); @@ -255,9 +254,9 @@ impl HintHandler for SingleChainHintHandler { kv_lock.set(PreimageKey::new_keccak256(*hash).into(), code.into())?; } HintType::L2StateNode => { - ensure!(hint.hint_data.len() == 32, "Invalid hint data length"); + ensure!(hint.data.len() == 32, "Invalid hint data length"); - let hash: B256 = hint.hint_data.as_ref().try_into()?; + let hash: B256 = hint.data.as_ref().try_into()?; // Fetch the preimage from the L2 chain provider. let preimage: Bytes = providers.l2.client().request("debug_dbGet", &[hash]).await?; @@ -266,10 +265,10 @@ impl HintHandler for SingleChainHintHandler { kv_write_lock.set(PreimageKey::new_keccak256(*hash).into(), preimage.into())?; } HintType::L2AccountProof => { - ensure!(hint.hint_data.len() == 8 + 20, "Invalid hint data length"); + ensure!(hint.data.len() == 8 + 20, "Invalid hint data length"); - let block_number = u64::from_be_bytes(hint.hint_data.as_ref()[..8].try_into()?); - let address = Address::from_slice(&hint.hint_data.as_ref()[8..28]); + let block_number = u64::from_be_bytes(hint.data.as_ref()[..8].try_into()?); + let address = Address::from_slice(&hint.data.as_ref()[8..28]); let proof_response = providers .l2 @@ -287,11 +286,11 @@ impl HintHandler for SingleChainHintHandler { })?; } HintType::L2AccountStorageProof => { - ensure!(hint.hint_data.len() == 8 + 20 + 32, "Invalid hint data length"); + ensure!(hint.data.len() == 8 + 20 + 32, "Invalid hint data length"); - let block_number = u64::from_be_bytes(hint.hint_data.as_ref()[..8].try_into()?); - let address = Address::from_slice(&hint.hint_data.as_ref()[8..28]); - let slot = B256::from_slice(&hint.hint_data.as_ref()[28..]); + let block_number = u64::from_be_bytes(hint.data.as_ref()[..8].try_into()?); + let address = Address::from_slice(&hint.data.as_ref()[8..28]); + let slot = B256::from_slice(&hint.data.as_ref()[28..]); let mut proof_response = providers .l2 @@ -319,11 +318,11 @@ impl HintHandler for SingleChainHintHandler { })?; } HintType::L2PayloadWitness => { - ensure!(hint.hint_data.len() >= 32, "Invalid hint data length"); + ensure!(hint.data.len() >= 32, "Invalid hint data length"); - let parent_block_hash = B256::from_slice(&hint.hint_data.as_ref()[..32]); + let parent_block_hash = B256::from_slice(&hint.data.as_ref()[..32]); let payload_attributes: OpPayloadAttributes = - serde_json::from_slice(&hint.hint_data[32..])?; + serde_json::from_slice(&hint.data[32..])?; let execute_payload_response: ExecutionWitness = providers .l2 diff --git a/crates/proof-sdk/proof-interop/src/boot.rs b/crates/proof-sdk/proof-interop/src/boot.rs index 984989166..4e66ffff6 100644 --- a/crates/proof-sdk/proof-interop/src/boot.rs +++ b/crates/proof-sdk/proof-interop/src/boot.rs @@ -143,10 +143,10 @@ pub(crate) async fn read_raw_pre_state( where O: CommsClient, { - caching_oracle - .write(&HintType::AgreedPreState.encode_with(&[agreed_pre_state_commitment.as_ref()])) - .await - .map_err(OracleProviderError::Preimage)?; + HintType::AgreedPreState + .with_data(&[agreed_pre_state_commitment.as_ref()]) + .send(caching_oracle) + .await?; let pre = caching_oracle .get(PreimageKey::new(*agreed_pre_state_commitment, PreimageKeyType::Keccak256)) .await diff --git a/crates/proof-sdk/proof-interop/src/hint.rs b/crates/proof-sdk/proof-interop/src/hint.rs index 4ad99c023..0f3b9b09e 100644 --- a/crates/proof-sdk/proof-interop/src/hint.rs +++ b/crates/proof-sdk/proof-interop/src/hint.rs @@ -1,12 +1,8 @@ //! This module contains the [HintType] enum. -use alloc::{ - string::{String, ToString}, - vec::Vec, -}; -use alloy_primitives::hex; +use alloc::{string::ToString, vec::Vec}; use core::{fmt::Display, str::FromStr}; -use kona_proof::errors::HintParsingError; +use kona_proof::{errors::HintParsingError, Hint}; /// The [HintType] enum is used to specify the type of hint that was received. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -46,10 +42,15 @@ pub enum HintType { } impl HintType { - /// Encodes the hint type as a string. - pub fn encode_with(&self, data: &[&[u8]]) -> String { - let concatenated = hex::encode(data.iter().copied().flatten().copied().collect::>()); - alloc::format!("{} {}", self, concatenated) + /// Creates a new [Hint] from `self` and the specified data. The data passed will be + /// concatenated into a single byte array before being stored in the resulting [Hint]. + pub fn with_data(self, data: &[&[u8]]) -> Hint { + let total_len = data.iter().map(|d| d.len()).sum(); + let hint_data = data.iter().fold(Vec::with_capacity(total_len), |mut acc, d| { + acc.extend_from_slice(d); + acc + }); + Hint::new(self, hint_data) } } diff --git a/crates/proof-sdk/proof-interop/src/provider.rs b/crates/proof-sdk/proof-interop/src/provider.rs index 46b4e77b1..57cdd2766 100644 --- a/crates/proof-sdk/proof-interop/src/provider.rs +++ b/crates/proof-sdk/proof-interop/src/provider.rs @@ -41,13 +41,10 @@ where chain_id: u64, block_hash: B256, ) -> Result::Error> { - self.oracle - .write( - &HintType::L2BlockHeader - .encode_with(&[block_hash.as_slice(), chain_id.to_be_bytes().as_ref()]), - ) - .await - .map_err(OracleProviderError::Preimage)?; + HintType::L2BlockHeader + .with_data(&[block_hash.as_slice(), chain_id.to_be_bytes().as_ref()]) + .send(self.oracle.as_ref()) + .await?; let header_rlp = self .oracle @@ -67,13 +64,10 @@ where ) -> Result, ::Error> { // Send a hint for the block's receipts, and walk through the receipts trie in the header to // verify them. - self.oracle - .write( - &HintType::L2Receipts - .encode_with(&[block_hash.as_ref(), chain_id.to_be_bytes().as_slice()]), - ) - .await - .map_err(OracleProviderError::Preimage)?; + HintType::L2Receipts + .with_data(&[block_hash.as_ref(), chain_id.to_be_bytes().as_slice()]) + .send(self.oracle.as_ref()) + .await?; let trie_walker = OrderedListWalker::try_new_hydrated(header.receipts_root, self) .map_err(OracleProviderError::TrieWalker)?; @@ -115,13 +109,13 @@ where .iter() .find(|o| o.chain_id == chain_id) .ok_or(OracleProviderError::UnknownChainId(chain_id))?; - self.oracle - .write(&HintType::L2OutputRoot.encode_with(&[ + HintType::L2OutputRoot + .with_data(&[ output.output_root.as_slice(), output.chain_id.to_be_bytes().as_slice(), - ])) - .await - .map_err(OracleProviderError::Preimage)?; + ]) + .send(self.oracle.as_ref()) + .await?; let output_preimage = self .oracle .get(PreimageKey::new(*output.output_root, PreimageKeyType::Keccak256)) diff --git a/crates/proof-sdk/proof/src/hint.rs b/crates/proof-sdk/proof/src/hint.rs index aa8a9f6e9..0f369c664 100644 --- a/crates/proof-sdk/proof/src/hint.rs +++ b/crates/proof-sdk/proof/src/hint.rs @@ -5,9 +5,9 @@ use alloc::{ string::{String, ToString}, vec::Vec, }; -use alloy_primitives::{hex, Bytes, B256}; +use alloy_primitives::{hex, Bytes}; use core::{fmt::Display, str::FromStr}; -use kona_preimage::{CommsClient, PreimageKey, PreimageKeyType}; +use kona_preimage::HintWriterClient; /// A [Hint] is parsed in the format ` `, where `` is a string that /// represents the type of hint, and `` is the data associated with the hint (bytes @@ -15,15 +15,33 @@ use kona_preimage::{CommsClient, PreimageKey, PreimageKeyType}; #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Hint { /// The type of hint. - pub hint_type: HT, + pub ty: HT, /// The data associated with the hint. - pub hint_data: Bytes, + pub data: Bytes, } -impl Hint { +impl Hint +where + HT: Display, +{ + /// Creates a new [Hint] with the specified type and data. + pub fn new>(ty: HT, data: T) -> Self { + Self { ty, data: data.into() } + } + /// Splits the [Hint] into its components. pub fn split(self) -> (HT, Bytes) { - (self.hint_type, self.hint_data) + (self.ty, self.data) + } + + /// Sends the hint to the passed [HintWriterClient]. + pub async fn send(&self, comms: &T) -> Result<(), OracleProviderError> { + comms.write(&self.encode()).await.map_err(OracleProviderError::Preimage) + } + + /// Encodes the hint as a string. + pub fn encode(&self) -> String { + alloc::format!("{} {}", self.ty, self.data) } } @@ -44,7 +62,7 @@ where let hint_data = hex::decode(parts.remove(0)).map_err(|e| HintParsingError(e.to_string()))?.into(); - Ok(Self { hint_type, hint_data }) + Ok(Self { ty: hint_type, data: hint_data }) } } @@ -82,45 +100,15 @@ pub enum HintType { } impl HintType { - /// Encodes the hint type as a string. - pub fn encode_with(&self, data: &[&[u8]]) -> String { - let concatenated = hex::encode(data.iter().copied().flatten().copied().collect::>()); - alloc::format!("{} {}", self, concatenated) - } - - /// Retrieves a preimage through an oracle - pub async fn get_preimage( - &self, - oracle: &T, - image: B256, - preimage_key_type: PreimageKeyType, - ) -> Result, OracleProviderError> { - oracle - .write(&self.encode_with(&[image.as_ref()])) - .await - .map_err(OracleProviderError::Preimage)?; - oracle - .get(PreimageKey::new(*image, preimage_key_type)) - .await - .map_err(OracleProviderError::Preimage) - } - - /// Retrieves a preimage through an oracle - pub async fn get_exact_preimage( - &self, - oracle: &T, - image: B256, - preimage_key_type: PreimageKeyType, - buf: &mut [u8], - ) -> Result<(), OracleProviderError> { - oracle - .write(&self.encode_with(&[image.as_ref()])) - .await - .map_err(OracleProviderError::Preimage)?; - oracle - .get_exact(PreimageKey::new(*image, preimage_key_type), buf) - .await - .map_err(OracleProviderError::Preimage) + /// Creates a new [Hint] from `self` and the specified data. The data passed will be + /// concatenated into a single byte array before being stored in the resulting [Hint]. + pub fn with_data(self, data: &[&[u8]]) -> Hint { + let total_len = data.iter().map(|d| d.len()).sum(); + let hint_data = data.iter().fold(Vec::with_capacity(total_len), |mut acc, d| { + acc.extend_from_slice(d); + acc + }); + Hint::new(self, hint_data) } } diff --git a/crates/proof-sdk/proof/src/l1/blob_provider.rs b/crates/proof-sdk/proof/src/l1/blob_provider.rs index 44a813427..d42e41df3 100644 --- a/crates/proof-sdk/proof/src/l1/blob_provider.rs +++ b/crates/proof-sdk/proof/src/l1/blob_provider.rs @@ -42,10 +42,7 @@ impl OracleBlobProvider { blob_req_meta[40..48].copy_from_slice(block_ref.timestamp.to_be_bytes().as_ref()); // Send a hint for the blob commitment and field elements. - self.oracle - .write(&HintType::L1Blob.encode_with(&[blob_req_meta.as_ref()])) - .await - .map_err(OracleProviderError::Preimage)?; + HintType::L1Blob.with_data(&[blob_req_meta.as_ref()]).send(self.oracle.as_ref()).await?; // Fetch the blob commitment. let mut commitment = [0u8; 48]; diff --git a/crates/proof-sdk/proof/src/l1/chain_provider.rs b/crates/proof-sdk/proof/src/l1/chain_provider.rs index f9166982b..6e63e526e 100644 --- a/crates/proof-sdk/proof/src/l1/chain_provider.rs +++ b/crates/proof-sdk/proof/src/l1/chain_provider.rs @@ -34,9 +34,8 @@ impl ChainProvider for OracleL1ChainProvider { async fn header_by_hash(&mut self, hash: B256) -> Result { // Fetch the header RLP from the oracle. - let header_rlp = HintType::L1BlockHeader - .get_preimage(self.oracle.as_ref(), hash, PreimageKeyType::Keccak256) - .await?; + HintType::L1BlockHeader.with_data(&[hash.as_ref()]).send(self.oracle.as_ref()).await?; + let header_rlp = self.oracle.get(PreimageKey::new_keccak256(*hash)).await?; // Decode the header RLP into a Header. Header::decode(&mut header_rlp.as_slice()).map_err(OracleProviderError::Rlp) @@ -70,10 +69,7 @@ impl ChainProvider for OracleL1ChainProvider { // Send a hint for the block's receipts, and walk through the receipts trie in the header to // verify them. - self.oracle - .write(&HintType::L1Receipts.encode_with(&[hash.as_ref()])) - .await - .map_err(OracleProviderError::Preimage)?; + HintType::L1Receipts.with_data(&[hash.as_ref()]).send(self.oracle.as_ref()).await?; let trie_walker = OrderedListWalker::try_new_hydrated(header.receipts_root, self) .map_err(OracleProviderError::TrieWalker)?; @@ -105,10 +101,7 @@ impl ChainProvider for OracleL1ChainProvider { // Send a hint for the block's transactions, and walk through the transactions trie in the // header to verify them. - self.oracle - .write(&HintType::L1Transactions.encode_with(&[hash.as_ref()])) - .await - .map_err(OracleProviderError::Preimage)?; + HintType::L1Transactions.with_data(&[hash.as_ref()]).send(self.oracle.as_ref()).await?; let trie_walker = OrderedListWalker::try_new_hydrated(header.transactions_root, self) .map_err(OracleProviderError::TrieWalker)?; diff --git a/crates/proof-sdk/proof/src/l2/chain_provider.rs b/crates/proof-sdk/proof/src/l2/chain_provider.rs index 2185abbc6..e42d4b889 100644 --- a/crates/proof-sdk/proof/src/l2/chain_provider.rs +++ b/crates/proof-sdk/proof/src/l2/chain_provider.rs @@ -91,10 +91,10 @@ impl BatchValidationProvider for OracleL2ChainProv let header_hash = header.hash_slow(); // Fetch the transactions in the block. - self.oracle - .write(&HintType::L2Transactions.encode_with(&[header_hash.as_ref()])) - .await - .map_err(OracleProviderError::Preimage)?; + HintType::L2Transactions + .with_data(&[header_hash.as_ref()]) + .send(self.oracle.as_ref()) + .await?; let trie_walker = OrderedListWalker::try_new_hydrated(transactions_root, self) .map_err(OracleProviderError::TrieWalker)?; @@ -165,19 +165,23 @@ impl TrieDBProvider for OracleL2ChainProvider { fn bytecode_by_hash(&self, hash: B256) -> Result { // Fetch the bytecode preimage from the caching oracle. crate::block_on(async move { - HintType::L2Code - .get_preimage(self.oracle.as_ref(), hash, PreimageKeyType::Keccak256) + HintType::L2Code.with_data(&[hash.as_slice()]).send(self.oracle.as_ref()).await?; + self.oracle + .get(PreimageKey::new_keccak256(*hash)) .await .map(Into::into) + .map_err(OracleProviderError::Preimage) }) } fn header_by_hash(&self, hash: B256) -> Result { // Fetch the header from the caching oracle. crate::block_on(async move { - let header_bytes = HintType::L2BlockHeader - .get_preimage(self.oracle.as_ref(), hash, PreimageKeyType::Keccak256) + HintType::L2BlockHeader + .with_data(&[hash.as_slice()]) + .send(self.oracle.as_ref()) .await?; + let header_bytes = self.oracle.get(PreimageKey::new_keccak256(*hash)).await?; Header::decode(&mut header_bytes.as_slice()).map_err(OracleProviderError::Rlp) }) @@ -189,22 +193,16 @@ impl TrieHinter for OracleL2ChainProvider { fn hint_trie_node(&self, hash: B256) -> Result<(), Self::Error> { crate::block_on(async move { - self.oracle - .write(&HintType::L2StateNode.encode_with(&[hash.as_slice()])) - .await - .map_err(OracleProviderError::Preimage) + HintType::L2StateNode.with_data(&[hash.as_slice()]).send(self.oracle.as_ref()).await }) } fn hint_account_proof(&self, address: Address, block_number: u64) -> Result<(), Self::Error> { crate::block_on(async move { - self.oracle - .write( - &HintType::L2AccountProof - .encode_with(&[block_number.to_be_bytes().as_ref(), address.as_slice()]), - ) + HintType::L2AccountProof + .with_data(&[block_number.to_be_bytes().as_ref(), address.as_slice()]) + .send(self.oracle.as_ref()) .await - .map_err(OracleProviderError::Preimage) }) } @@ -215,14 +213,14 @@ impl TrieHinter for OracleL2ChainProvider { block_number: u64, ) -> Result<(), Self::Error> { crate::block_on(async move { - self.oracle - .write(&HintType::L2AccountStorageProof.encode_with(&[ + HintType::L2AccountStorageProof + .with_data(&[ block_number.to_be_bytes().as_ref(), address.as_slice(), slot.to_be_bytes::<32>().as_ref(), - ])) + ]) + .send(self.oracle.as_ref()) .await - .map_err(OracleProviderError::Preimage) }) } }