Skip to content

Commit

Permalink
feat(prover): track cycles of sp1 guest & patch Secp256k1 (#288)
Browse files Browse the repository at this point in the history
* add cycle-tracker

* git = "https://github.com/ceciliaz030/alloy", branch = "175_4e22b9e-cycle"

* revm v35_taiko_v2-cycls

* make harness part of root workspace

* update Cargo.lock

* [dev-dependencies]
substrate-bn = "0.6.0"

* use [email protected]

* cargo update

* clippy & fmt

* remove typo

* add [features] sp1-cycle-tracker in guest

* apply comment

* struct CycleTracker

* update

* fmt

* Update proof.rs

* Update proof.rs

* Update proof.rs
  • Loading branch information
CeciliaZ030 authored Jun 17, 2024
1 parent 125a349 commit 927e697
Show file tree
Hide file tree
Showing 25 changed files with 1,180 additions and 367 deletions.
424 changes: 328 additions & 96 deletions Cargo.lock

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ raiko-lib = { path = "./lib", features = ["std"] }
raiko-core = { path = "./core" }

# revm
revm-primitives = { git = "https://github.com/taikoxyz/revm.git", branch = "v35_taiko_v2", default-features = false }
revm = { git = "https://github.com/taikoxyz/revm.git", branch = "v35_taiko_v2", default-features = false, features = [
revm-primitives = { git = "https://github.com/taikoxyz/revm.git", branch = "v35-taiko", default-features = false }
revm = { git = "https://github.com/taikoxyz/revm.git", branch = "v35-taiko", default-features = false, features = [
"serde",
"std",
"c-kzg",
Expand All @@ -64,22 +64,22 @@ alloy-dyn-abi = { version = "0.7.1", default-features = false }
alloy-json-abi = { version = "0.7.1", default-features = false }
alloy-primitives = { version = "0.7.1", default-features = false }
alloy-sol-types = { version = "0.7.1", default-features = false }
alloy-rpc-types = { git = "https://github.com/brechtpd/alloy", branch = "175_4e22b9e" }
alloy-rpc-client = { git = "https://github.com/brechtpd/alloy", branch = "175_4e22b9e" }
alloy-consensus = { git = "https://github.com/brechtpd/alloy", branch = "175_4e22b9e", features = [
alloy-rpc-types = { git = "https://github.com/ceciliaz030/alloy", branch = "175_4e22b9e-cycle" }
alloy-rpc-client = { git = "https://github.com/ceciliaz030/alloy", branch = "175_4e22b9e-cycle" }
alloy-consensus = { git = "https://github.com/ceciliaz030/alloy", branch = "175_4e22b9e-cycle", features = [
"serde",
] }
alloy-network = { git = "https://github.com/brechtpd/alloy", branch = "175_4e22b9e", features = [
alloy-network = { git = "https://github.com/ceciliaz030/alloy", branch = "175_4e22b9e-cycle", features = [
"k256",
] }
alloy-contract = { git = "https://github.com/brechtpd/alloy", branch = "175_4e22b9e" }
alloy-eips = { git = "https://github.com/brechtpd/alloy", branch = "175_4e22b9e", features = [
alloy-contract = { git = "https://github.com/ceciliaz030/alloy", branch = "175_4e22b9e-cycle" }
alloy-eips = { git = "https://github.com/ceciliaz030/alloy", branch = "175_4e22b9e-cycle", features = [
"serde",
] }
alloy-provider = { git = "https://github.com/brechtpd/alloy", branch = "175_4e22b9e" }
alloy-transport-http = { git = "https://github.com/brechtpd/alloy", branch = "175_4e22b9e" }
alloy-signer = { git = "https://github.com/brechtpd/alloy", branch = "175_4e22b9e" }
alloy-signer-wallet = { git = "https://github.com/brechtpd/alloy", branch = "175_4e22b9e" }
alloy-provider = { git = "https://github.com/ceciliaz030/alloy", branch = "175_4e22b9e-cycle" }
alloy-transport-http = { git = "https://github.com/ceciliaz030/alloy", branch = "175_4e22b9e-cycle" }
alloy-signer = { git = "https://github.com/ceciliaz030/alloy", branch = "175_4e22b9e-cycle" }
alloy-signer-wallet = { git = "https://github.com/ceciliaz030/alloy", branch = "175_4e22b9e-cycle" }
reqwest_alloy = { package = "reqwest", version = "0.12.4", features = ["json"] }


Expand Down Expand Up @@ -139,7 +139,7 @@ c-kzg = { package = "c-kzg-taiko", git = "https://github.com/smtmfft/c-kzg-4844"
] }
sha3 = { version = "0.10", default-features = false }
sha2 = "0.10.8"
secp256k1 = { version = "0.27.0", features = [
secp256k1 = { git = "https://github.com/CeciliaZ030/rust-secp256k1", branch = "sp1-patch", features = [
'global-context',
"rand",
"recovery",
Expand Down
13 changes: 7 additions & 6 deletions core/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ impl Prover for NativeProver {
output: &GuestOutput,
config: &ProverConfig,
) -> ProverResult<Proof> {
let param = config
.get("native")
.map(|v| NativeParam::deserialize(v))
.ok_or(ProverError::Param(serde_json::Error::custom(
"native param not provided",
)))??;
let param =
config
.get("native")
.map(NativeParam::deserialize)
.ok_or(ProverError::Param(serde_json::Error::custom(
"native param not provided",
)))??;

if let Some(path) = param.write_guest_input_path {
let path = Path::new(&path);
Expand Down
2 changes: 1 addition & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,4 @@ services:
volumes:
- ${HOME}/.config/sgx-pccs/default.json:/opt/intel/pccs/config/default.json
- ${HOME}/.config/sgx-pccs/file.crt:/opt/intel/pccs/ssl_key/file.crt
- ${HOME}/.config/sgx-pccs/private.pem:/opt/intel/pccs/ssl_key/private.pem
- ${HOME}/.config/sgx-pccs/private.pem:/opt/intel/pccs/ssl_key/private.pem
6 changes: 3 additions & 3 deletions host/src/server/api/v1/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async fn validate_cache_input(

// double check if cache is valid
if cached_block_hash == real_block_hash {
return Ok(cache_input);
Ok(cache_input)
} else {
Err(HostError::InvalidRequestConfig(
"Cached input is not valid".to_owned(),
Expand Down Expand Up @@ -251,7 +251,7 @@ mod test {
block_number: u64,
) -> (GuestInput, RpcBlockDataProvider) {
let l1_chain_spec = SupportedChainSpecs::default()
.get_chain_spec(&l1_network)
.get_chain_spec(l1_network)
.unwrap();
let taiko_chain_spec = SupportedChainSpecs::default()
.get_chain_spec(network)
Expand Down Expand Up @@ -287,7 +287,7 @@ mod test {
async fn test_generate_input_from_cache() {
let l1 = &Network::Holesky.to_string();
let l2 = &Network::TaikoA7.to_string();
let block_number: u64 = 7;
let block_number: u64 = 69627;
let (input, provider) = create_cache_input(l1, l2, block_number).await;
let cache_path = Some("./".into());
assert!(set_cached_input(&cache_path, block_number, l2, &input).is_ok());
Expand Down
1 change: 1 addition & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,4 @@ sgx = []
sp1 = []
risc0 = []
c-kzg = ["revm-primitives/c-kzg", "dep:tempfile"]
sp1-cycle-tracker = []
25 changes: 24 additions & 1 deletion lib/src/builder/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use crate::{
print_duration,
time::{AddAssign, Duration, Instant},
utils::{check_anchor_tx, generate_transactions},
CycleTracker,
};

/// Minimum supported protocol version: SHANGHAI
Expand Down Expand Up @@ -176,7 +177,16 @@ impl TxExecStrategy for TkoTxExecStrategy {
let num_transactions = transactions.len();
for (tx_no, tx) in take(&mut transactions).into_iter().enumerate() {
if !is_optimistic {
inplace_print(&format!("\rprocessing tx {tx_no}/{num_transactions}..."));
cfg_if::cfg_if! {
if #[cfg(all(all(target_os = "zkvm", target_vendor = "succinct"), feature = "sp1-cycle-tracker"))]{
println!(
"{:?}",
&format!("\rprocessing tx {tx_no}/{num_transactions}...")
);
} else {
inplace_print(&format!("\rprocessing tx {tx_no}/{num_transactions}..."));
}
}
} else {
trace!("\rprocessing tx {tx_no}/{num_transactions}...");
}
Expand Down Expand Up @@ -255,6 +265,7 @@ impl TxExecStrategy for TkoTxExecStrategy {

// process the transaction
let start = Instant::now();
let cycle_tracker = CycleTracker::start("evm.transact()");
let ResultAndState { result, state } = match evm.transact() {
Ok(result) => result,
Err(err) => {
Expand All @@ -276,6 +287,7 @@ impl TxExecStrategy for TkoTxExecStrategy {
EVMError::Transaction(invalid_transaction) => {
#[cfg(feature = "std")]
debug!("Invalid tx at {tx_no}: {invalid_transaction:?}");
cycle_tracker.end();
// skip the tx
continue;
}
Expand All @@ -286,6 +298,7 @@ impl TxExecStrategy for TkoTxExecStrategy {
}
}
};
cycle_tracker.end();
#[cfg(feature = "std")]
trace!(" Ok: {result:?}");

Expand Down Expand Up @@ -340,6 +353,8 @@ impl TxExecStrategy for TkoTxExecStrategy {
print_duration("Tx transact time: ", tx_transact_duration);
print_duration("Tx misc time: ", tx_misc_duration);
}
clear_line();
println!("actual Tx: {}", actual_tx_no);

let mut db = &mut evm.context.evm.db;

Expand Down Expand Up @@ -387,7 +402,9 @@ pub fn fill_eth_tx_env(tx_env: &mut TxEnv, tx: &TxEnvelope) -> Result<(), Error>
// TODO(Brecht): use optimized recover
match tx {
TxEnvelope::Legacy(tx) => {
let cycle_tracker = CycleTracker::start("Legacy");
tx_env.caller = tx.recover_signer().unwrap_or_default();
cycle_tracker.end();
let tx = tx.tx();
tx_env.gas_limit = tx.gas_limit.try_into().unwrap();
tx_env.gas_price = tx.gas_price.try_into().unwrap();
Expand All @@ -404,7 +421,9 @@ pub fn fill_eth_tx_env(tx_env: &mut TxEnv, tx: &TxEnvelope) -> Result<(), Error>
tx_env.access_list.clear();
}
TxEnvelope::Eip2930(tx) => {
let cycle_tracker = CycleTracker::start("Eip2930");
tx_env.caller = tx.recover_signer().unwrap_or_default();
cycle_tracker.end();
let tx = tx.tx();
tx_env.gas_limit = tx.gas_limit.try_into().unwrap();
tx_env.gas_price = tx.gas_price.try_into().unwrap();
Expand All @@ -421,7 +440,9 @@ pub fn fill_eth_tx_env(tx_env: &mut TxEnv, tx: &TxEnvelope) -> Result<(), Error>
tx_env.access_list = tx.access_list.flattened();
}
TxEnvelope::Eip1559(tx) => {
let cycle_tracker = CycleTracker::start("Eip1559");
tx_env.caller = tx.recover_signer().unwrap_or_default();
cycle_tracker.end();
let tx = tx.tx();
tx_env.gas_limit = tx.gas_limit.try_into().unwrap();
tx_env.gas_price = tx.max_fee_per_gas.try_into().unwrap();
Expand All @@ -438,7 +459,9 @@ pub fn fill_eth_tx_env(tx_env: &mut TxEnv, tx: &TxEnvelope) -> Result<(), Error>
tx_env.access_list = tx.access_list.flattened();
}
TxEnvelope::Eip4844(tx) => {
let cycle_tracker = CycleTracker::start("Eip1559");
tx_env.caller = tx.recover_signer().unwrap_or_default();
cycle_tracker.end();
let tx = tx.tx().tx();
tx_env.gas_limit = tx.gas_limit.try_into().unwrap();
tx_env.gas_price = tx.max_fee_per_gas.try_into().unwrap();
Expand Down
13 changes: 13 additions & 0 deletions lib/src/builder/finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ impl BlockFinalizeStrategy<MemDb> for MemDbBlockFinalizeStrategy {
fn finalize(mut block_builder: BlockBuilder<MemDb>) -> Result<(AlloyConsensusHeader, MptNode)> {
let db: MemDb = block_builder.db.take().expect("DB not initialized");

let mut account_touched = 0;
let mut storage_touched = 0;

// apply state updates
let mut state_trie = mem::take(&mut block_builder.input.parent_state_trie);
for (address, account) in &db.accounts {
Expand All @@ -59,6 +62,8 @@ impl BlockFinalizeStrategy<MemDb> for MemDbBlockFinalizeStrategy {
continue;
}

account_touched += 1;

// otherwise, compute the updated storage root for that account
let state_storage = &account.storage;
let storage_root = {
Expand All @@ -82,6 +87,8 @@ impl BlockFinalizeStrategy<MemDb> for MemDbBlockFinalizeStrategy {
} else {
storage_trie.insert_rlp(&storage_trie_index, *value)?;
}

storage_touched += 1;
}

storage_trie.hash()
Expand All @@ -96,6 +103,12 @@ impl BlockFinalizeStrategy<MemDb> for MemDbBlockFinalizeStrategy {
state_trie.insert_rlp(&state_trie_index, state_account)?;
}

#[cfg(feature = "sp1-cycle-tracker")]
{
println!("finalize Account touched {:?}", account_touched);
println!("finalize Storage touched {:?}", storage_touched);
}

// update result header with the new state root
let mut header = block_builder.header.take().expect("Header not initialized");
header.state_root = state_trie.hash();
Expand Down
14 changes: 14 additions & 0 deletions lib/src/builder/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ impl DbInitStrategy<MemDb> for MemDbInitStrategy {
.map(|bytes| (keccak(&bytes).into(), bytes))
.collect();

let mut account_touched = 0;
let mut storage_touched = 0;

// Load account data into db
let mut accounts = HashMap::with_capacity(block_builder.input.parent_storage.len());
for (address, (storage_trie, slots)) in &mut block_builder.input.parent_storage {
Expand All @@ -82,6 +85,7 @@ impl DbInitStrategy<MemDb> for MemDbInitStrategy {
storage_trie.hash()
);
}
account_touched += 1;

// load the corresponding code
let code_hash = state_account.code_hash;
Expand All @@ -102,6 +106,7 @@ impl DbInitStrategy<MemDb> for MemDbInitStrategy {
.get_rlp(&keccak(slot.to_be_bytes::<32>()))?
.unwrap_or_default();
storage.insert(slot, value);
storage_touched += 1;
}

let mem_account = DbAccount {
Expand All @@ -119,6 +124,15 @@ impl DbInitStrategy<MemDb> for MemDbInitStrategy {
}
guest_mem_forget(contracts);

#[cfg(all(
all(target_os = "zkvm", target_vendor = "succinct"),
feature = "sp1-cycle-tracker"
))]
{
println!("initialize_db Account touch {:?}", account_touched);
println!("initialize_db Storage touch {:?}", storage_touched);
}

// prepare block hash history
let mut block_hashes =
HashMap::with_capacity(block_builder.input.ancestor_headers.len() + 1);
Expand Down
21 changes: 17 additions & 4 deletions lib/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::{
input::GuestInput,
mem_db::MemDb,
primitives::mpt::MptNode,
CycleTracker,
};

pub mod execute;
Expand Down Expand Up @@ -76,22 +77,34 @@ where

/// Initializes the database from the input.
pub fn initialize_database<T: DbInitStrategy<D>>(self) -> Result<Self> {
T::initialize_database(self)
let cycle_tracker = CycleTracker::start("initialize_database");
let res = T::initialize_database(self);
cycle_tracker.end();
res
}

/// Initializes the header. This must be called before executing transactions.
pub fn prepare_header<T: HeaderPrepStrategy>(self) -> Result<Self> {
T::prepare_header(self)
let cycle_tracker = CycleTracker::start("prepare_header");
let res = T::prepare_header(self);
cycle_tracker.end();
res
}

/// Executes all input transactions.
pub fn execute_transactions<T: TxExecStrategy>(self) -> Result<Self> {
T::execute_transactions(self)
let cycle_tracker = CycleTracker::start("execute_transactions");
let res = T::execute_transactions(self);
cycle_tracker.end();
res
}

/// Finalizes the block building and returns the header and the state trie.
pub fn finalize<T: BlockFinalizeStrategy<D>>(self) -> Result<(AlloyConsensusHeader, MptNode)> {
T::finalize(self)
let cycle_tracker = CycleTracker::start("finalize");
let res = T::finalize(self);
cycle_tracker.end();
res
}

/// Returns a reference to the database.
Expand Down
26 changes: 26 additions & 0 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,32 @@ mod time {
}
}

pub struct CycleTracker {
title: String,
}

impl CycleTracker {
pub fn start(title: &str) -> CycleTracker {
let ct = CycleTracker {
title: title.to_string(),
};
#[cfg(all(
all(target_os = "zkvm", target_vendor = "succinct"),
feature = "sp1-cycle-tracker"
))]
println!("cycle-tracker-start: {title}");
ct
}

pub fn end(&self) {
#[cfg(all(
all(target_os = "zkvm", target_vendor = "succinct"),
feature = "sp1-cycle-tracker"
))]
println!("cycle-tracker-end: {self.title}");
}
}

pub struct Measurement {
start: time::Instant,
title: String,
Expand Down
Loading

0 comments on commit 927e697

Please sign in to comment.