Skip to content

Commit

Permalink
L2ExecutePayloadProof Hint Type (#832)
Browse files Browse the repository at this point in the history
* l2 execute payload proof hint type

* lint

* correct types

* tested & linted

* rm engine featue import

* change naming to align with spec
  • Loading branch information
zobront authored Nov 22, 2024
1 parent cddeaf4 commit 657d313
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ alloy-provider = { version = "0.6.4", default-features = false }
alloy-primitives = { version = "0.8.12", default-features = false }
alloy-consensus = { version = "0.6.4", default-features = false }
alloy-transport = { version = "0.6.4", default-features = false }
alloy-rpc-types = { version = "0.6.4", default-features = false }
alloy-rpc-types = { version = "0.6.4", default-features = false, features = ["debug"] }
alloy-rpc-client = { version = "0.6.4", default-features = false }
alloy-node-bindings = { version = "0.6.4", default-features = false }
alloy-transport-http = { version = "0.6.4", default-features = false }
Expand All @@ -93,7 +93,7 @@ op-alloy-genesis = { version = "0.6.8", default-features = false }
op-alloy-registry = { version = "0.6.8", default-features = false }
op-alloy-protocol = { version = "0.6.8", default-features = false }
op-alloy-consensus = { version = "0.6.8", default-features = false }
op-alloy-rpc-types-engine = { version = "0.6.8", default-features = false }
op-alloy-rpc-types-engine = { version = "0.6.8", default-features = false, features = ["serde"] }

# General
lru = "0.12.4"
Expand Down
1 change: 1 addition & 0 deletions bin/host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ alloy-rpc-types-beacon.workspace = true
# Op Alloy
op-alloy-genesis = { workspace = true, features = ["std", "serde"] }
op-alloy-protocol = { workspace = true, features = ["std", "serde"] }
op-alloy-rpc-types-engine.workspace = true

# Revm
revm = { workspace = true, features = ["std", "c-kzg", "secp256k1", "portable", "blst"] }
Expand Down
38 changes: 36 additions & 2 deletions bin/host/src/fetcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ use crate::{
};
use alloy_consensus::{Header, TxEnvelope, EMPTY_ROOT_HASH};
use alloy_eips::{eip2718::Encodable2718, eip4844::FIELD_ELEMENTS_PER_BLOB, BlockId};
use alloy_primitives::{address, keccak256, Address, Bytes, B256};
use alloy_primitives::{address, keccak256, map::HashMap, Address, Bytes, B256};
use alloy_provider::{Provider, ReqwestProvider};
use alloy_rlp::{Decodable, EMPTY_STRING_CODE};
use alloy_rpc_types::{
Block, BlockNumberOrTag, BlockTransactions, BlockTransactionsKind, Transaction,
debug::ExecutionWitness, Block, BlockNumberOrTag, BlockTransactions, BlockTransactionsKind,
Transaction,
};
use anyhow::{anyhow, Result};
use kona_derive::sources::IndexedBlobHash;
use kona_preimage::{PreimageKey, PreimageKeyType};
use kona_proof::HintType;
use op_alloy_protocol::BlockInfo;
use op_alloy_rpc_types_engine::OpPayloadAttributes;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{error, trace, warn};
Expand Down Expand Up @@ -507,6 +509,38 @@ where
Ok::<(), anyhow::Error>(())
})?;
}
HintType::L2PayloadWitness => {
if hint_data.len() < 32 {
anyhow::bail!("Invalid hint data length: {}", hint_data.len());
}
let parent_block_hash = B256::from_slice(&hint_data.as_ref()[..32]);
let payload_attributes: OpPayloadAttributes =
serde_json::from_slice(&hint_data[32..])?;

let execute_payload_response: ExecutionWitness = self
.l2_provider
.client()
.request::<(B256, OpPayloadAttributes), ExecutionWitness>(
"debug_executePayload",
(parent_block_hash, payload_attributes),
)
.await
.map_err(|e| anyhow!("Failed to fetch preimage: {e}"))?;

let mut merged = HashMap::<B256, Bytes>::new();
merged.extend(execute_payload_response.state);
merged.extend(execute_payload_response.codes);
merged.extend(execute_payload_response.keys);

let mut kv_write_lock = self.kv_store.write().await;
for (hash, preimage) in merged.into_iter() {
let computed_hash = keccak256(preimage.as_ref());
assert_eq!(computed_hash, hash, "Preimage hash does not match expected hash");

let key = PreimageKey::new(*hash, PreimageKeyType::Keccak256);
kv_write_lock.set(key.into(), preimage.into())?;
}
}
}

Ok(())
Expand Down
5 changes: 5 additions & 0 deletions crates/proof-sdk/proof/src/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pub enum HintType {
/// A hint that specifies the proof on the path to a storage slot in an account within in the
/// L2 state trie.
L2AccountStorageProof,
/// A hint that specifies bulk storage of all the code, state and keys generated by an
/// execution witness.
L2PayloadWitness,
}

impl HintType {
Expand Down Expand Up @@ -63,6 +66,7 @@ impl TryFrom<&str> for HintType {
"l2-state-node" => Ok(Self::L2StateNode),
"l2-account-proof" => Ok(Self::L2AccountProof),
"l2-account-storage-proof" => Ok(Self::L2AccountStorageProof),
"l2-payload-witness" => Ok(Self::L2PayloadWitness),
_ => Err(HintParsingError(value.to_string())),
}
}
Expand All @@ -83,6 +87,7 @@ impl From<HintType> for &str {
HintType::L2StateNode => "l2-state-node",
HintType::L2AccountProof => "l2-account-proof",
HintType::L2AccountStorageProof => "l2-account-storage-proof",
HintType::L2PayloadWitness => "l2-payload-witness",
}
}
}
Expand Down

0 comments on commit 657d313

Please sign in to comment.