Skip to content

Commit

Permalink
move interop hints over
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Feb 4, 2025
1 parent 7b3369a commit 00dd261
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 40 deletions.
10 changes: 5 additions & 5 deletions bin/client/src/interop/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions crates/proof-sdk/proof-interop/src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ pub(crate) async fn read_raw_pre_state<O>(
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
Expand Down
21 changes: 11 additions & 10 deletions crates/proof-sdk/proof-interop/src/hint.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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::<Vec<_>>());
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<Self> {
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)
}
}

Expand Down
32 changes: 13 additions & 19 deletions crates/proof-sdk/proof-interop/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,10 @@ where
chain_id: u64,
block_hash: B256,
) -> Result<Header, <Self as InteropProvider>::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
Expand All @@ -67,13 +64,10 @@ where
) -> Result<Vec<OpReceiptEnvelope>, <Self as InteropProvider>::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)?;

Expand Down Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions crates/proof-sdk/proof/src/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ where
(self.ty, self.data)
}

/// Sends the hint to the passed [CommsClient].
/// Sends the hint to the passed [HintWriterClient].
pub async fn send<T: HintWriterClient>(&self, comms: &T) -> Result<(), OracleProviderError> {
comms.write(&self.encode()).await.map_err(OracleProviderError::Preimage)
}
Expand Down Expand Up @@ -100,7 +100,7 @@ pub enum HintType {
}

impl HintType {
/// Creates a new [Hint] from [self] and the specified data. The data passed will be
/// 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<Self> {
let total_len = data.iter().map(|d| d.len()).sum();
Expand Down

0 comments on commit 00dd261

Please sign in to comment.