Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(prover): track cycles of sp1 guest & patch Secp256k1 #288

Merged
merged 23 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 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
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 = []
35 changes: 35 additions & 0 deletions lib/src/builder/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl TxExecStrategy for TkoTxExecStrategy {
D: Database + DatabaseCommit + OptimisticDatabase,
<D as Database>::Error: Debug,
{
println!("block no {}", block_builder.input.block_number);
CeciliaZ030 marked this conversation as resolved.
Show resolved Hide resolved
let mut tx_transact_duration = Duration::default();
let mut tx_misc_duration = Duration::default();

Expand Down Expand Up @@ -174,9 +175,16 @@ impl TxExecStrategy for TkoTxExecStrategy {
// track the actual tx number to use in the tx/receipt trees as the key
let mut actual_tx_no = 0usize;
let num_transactions = transactions.len();
println!("total Txs: {}", num_transactions);
CeciliaZ030 marked this conversation as resolved.
Show resolved Hide resolved
for (tx_no, tx) in take(&mut transactions).into_iter().enumerate() {
if !is_optimistic {
#[cfg(not(feature = "sp1-cycle-tracker"))]
Brechtpd marked this conversation as resolved.
Show resolved Hide resolved
inplace_print(&format!("\rprocessing tx {tx_no}/{num_transactions}..."));
#[cfg(feature = "sp1-cycle-tracker")]
println!(
"{:?}",
&format!("\rprocessing tx {tx_no}/{num_transactions}...")
);
} else {
trace!("\rprocessing tx {tx_no}/{num_transactions}...");
}
Expand Down Expand Up @@ -255,6 +263,8 @@ impl TxExecStrategy for TkoTxExecStrategy {

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

Expand Down Expand Up @@ -340,6 +354,10 @@ impl TxExecStrategy for TkoTxExecStrategy {
print_duration("Tx transact time: ", tx_transact_duration);
print_duration("Tx misc time: ", tx_misc_duration);
}
clear_line();
print_duration("Tx transact time: ", tx_transact_duration);
print_duration("Tx misc time: ", tx_misc_duration);
println!("actual Tx: {}", actual_tx_no);
CeciliaZ030 marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down Expand Up @@ -387,7 +405,12 @@ pub fn fill_eth_tx_env(tx_env: &mut TxEnv, tx: &TxEnvelope) -> Result<(), Error>
// TODO(Brecht): use optimized recover
match tx {
TxEnvelope::Legacy(tx) => {
#[cfg(feature = "sp1-cycle-tracker")]
println!("cycle-tracker-start: Legacy");
tx_env.caller = tx.recover_signer().unwrap_or_default();
#[cfg(feature = "sp1-cycle-tracker")]
println!("cycle-tracker-end: Legacy");
CeciliaZ030 marked this conversation as resolved.
Show resolved Hide resolved
//tx_env.caller = *GOLDEN_TOUCH_ACCOUNT;
CeciliaZ030 marked this conversation as resolved.
Show resolved Hide resolved
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 +427,11 @@ pub fn fill_eth_tx_env(tx_env: &mut TxEnv, tx: &TxEnvelope) -> Result<(), Error>
tx_env.access_list.clear();
}
TxEnvelope::Eip2930(tx) => {
#[cfg(feature = "sp1-cycle-tracker")]
println!("cycle-tracker-start: Eip2930");
tx_env.caller = tx.recover_signer().unwrap_or_default();
#[cfg(feature = "sp1-cycle-tracker")]
println!("cycle-tracker-end: Eip2930");
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 +448,11 @@ 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) => {
#[cfg(feature = "sp1-cycle-tracker")]
println!("cycle-tracker-start: Eip1559");
tx_env.caller = tx.recover_signer().unwrap_or_default();
#[cfg(feature = "sp1-cycle-tracker")]
println!("cycle-tracker-end: Eip1559");
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 +469,11 @@ 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) => {
#[cfg(feature = "sp1-cycle-tracker")]
println!("cycle-tracker-start: Eip1559");
tx_env.caller = tx.recover_signer().unwrap_or_default();
#[cfg(feature = "sp1-cycle-tracker")]
println!("cycle-tracker-end: Eip4844");
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
11 changes: 11 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,12 @@ impl DbInitStrategy<MemDb> for MemDbInitStrategy {
}
guest_mem_forget(contracts);

#[cfg(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
35 changes: 31 additions & 4 deletions lib/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,49 @@ where

/// Initializes the database from the input.
pub fn initialize_database<T: DbInitStrategy<D>>(self) -> Result<Self> {
T::initialize_database(self)
// Already have accounts u need from Host DB
// Form sparse MPT from DB -> bunch of *keccaks* 🌟
CeciliaZ030 marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "sp1-cycle-tracker")]
println!("cycle-tracker-start: initialize_database");
let res = T::initialize_database(self);
#[cfg(feature = "sp1-cycle-tracker")]
println!("cycle-tracker-end: initialize_database");
res
}

/// Initializes the header. This must be called before executing transactions.
pub fn prepare_header<T: HeaderPrepStrategy>(self) -> Result<Self> {
T::prepare_header(self)
// Form the Block header to be prover
// 1 *Keccak* of parent haader
#[cfg(feature = "sp1-cycle-tracker")]
println!("cycle-tracker-start: prepare_header");
let res = T::prepare_header(self);
#[cfg(feature = "sp1-cycle-tracker")]
println!("cycle-tracker-end: prepare_header");
res
}

/// Executes all input transactions.
pub fn execute_transactions<T: TxExecStrategy>(self) -> Result<Self> {
T::execute_transactions(self)
// Tx *kaccak* hash, *ECDSA* recover
// Full evm (*precompiles*)
#[cfg(feature = "sp1-cycle-tracker")]
println!("cycle-tracker-start: execute_transactions");
let res = T::execute_transactions(self);
#[cfg(feature = "sp1-cycle-tracker")]
println!("cycle-tracker-end: execute_transactions");
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)
// MPT: many *keccaks*
#[cfg(feature = "sp1-cycle-tracker")]
println!("cycle-tracker-start: finalize");
let res = T::finalize(self);
#[cfg(feature = "sp1-cycle-tracker")]
println!("cycle-tracker-end: finalize");
res
}

/// Returns a reference to the database.
Expand Down
Loading
Loading