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

chore(raiko-lib): auto logging #219

Merged
merged 19 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
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
1 change: 1 addition & 0 deletions Cargo.lock

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

8 changes: 3 additions & 5 deletions host/src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![allow(incomplete_features)]
use std::path::PathBuf;

use raiko_host::{interfaces::error::HostResult, server::serve, ProverState};
use std::path::PathBuf;
use tracing::debug;
use tracing_appender::{
non_blocking::WorkerGuard,
Expand All @@ -13,14 +12,13 @@ use tracing_subscriber::FmtSubscriber;
async fn main() -> HostResult<()> {
env_logger::init();
let state = ProverState::init()?;
debug!("Start config:\n{:#?}", state.opts.proof_request_opt);
debug!("Args:\n{:#?}", state.opts);

let _guard = subscribe_log(
&state.opts.log_path,
&state.opts.log_level,
state.opts.max_log,
);
debug!("Start config:\n{:#?}", state.opts.proof_request_opt);
debug!("Args:\n{:#?}", state.opts);

serve(state).await?;
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions host/src/interfaces/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,23 @@ impl ProofType {
return sp1_driver::Sp1Prover::run(input, output, config)
.await
.map_err(|e| e.into());

#[cfg(not(feature = "sp1"))]
Err(HostError::FeatureNotSupportedError(self.clone()))
}
ProofType::Risc0 => {
#[cfg(feature = "risc0")]
return risc0_driver::Risc0Prover::run(input, output, config)
.await
.map_err(|e| e.into());

#[cfg(not(feature = "risc0"))]
Err(HostError::FeatureNotSupportedError(self.clone()))
}
ProofType::Sgx => {
#[cfg(feature = "sgx")]
return sgx_prover::SgxProver::run(input, output, config)
.await
.map_err(|e| e.into());

#[cfg(not(feature = "sgx"))]
Err(HostError::FeatureNotSupportedError(self.clone()))
}
}
Expand Down
4 changes: 2 additions & 2 deletions host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl ProverState {
}

mod memory {
use tracing::info;
use tracing::debug;

use crate::ALLOCATOR;

Expand All @@ -184,7 +184,7 @@ mod memory {

pub(crate) fn print_stats(title: &str) {
let max_memory = get_max_allocated();
info!(
debug!(
"{title}{}.{:06} MB",
max_memory / 1_000_000,
max_memory % 1_000_000
Expand Down
39 changes: 24 additions & 15 deletions host/src/preflight.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use crate::{
interfaces::error::{HostError, HostResult},
provider::{db::ProviderDb, rpc::RpcBlockDataProvider, BlockDataProvider},
};
use alloy_consensus::{
SignableTransaction, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEnvelope, TxLegacy,
};
Expand All @@ -11,7 +15,9 @@ use raiko_lib::{
builder::{
prepare::TaikoHeaderPrepStrategy, BlockBuilder, OptimisticDatabase, TkoTxExecStrategy,
},
clear_line,
consts::ChainSpec,
inplace_print,
input::{
decode_anchor, proposeBlockCall, BlockProposed, GuestInput, TaikoGuestInput,
TaikoProverData,
Expand All @@ -25,12 +31,7 @@ use raiko_primitives::{
};
use serde::{Deserialize, Serialize};
use std::{collections::HashSet, sync::Arc};
use tracing::{info, warn};

use crate::{
interfaces::error::{HostError, HostResult},
provider::{db::ProviderDb, rpc::RpcBlockDataProvider, BlockDataProvider},
};
use tracing::{debug, info, warn};

pub async fn preflight<BDP: BlockDataProvider>(
provider: BDP,
Expand Down Expand Up @@ -59,10 +60,13 @@ pub async fn preflight<BDP: BlockDataProvider>(
.hash
.ok_or_else(|| HostError::Preflight("No block hash for the requested block".to_string()))?;

info!("\nblock.hash: {hash:?}");
info!("block.parent_hash: {:?}", block.header.parent_hash);
info!("block gas used: {:?}", block.header.gas_used);
info!("block transactions: {:?}", block.transactions.len());
info!(
"Processing block {:?} with block.hash: {:?}",
block.header.number, block.header.hash
);
debug!("block.parent_hash: {:?}", block.header.parent_hash);
debug!("block gas used: {:?}", block.header.gas_used);
debug!("block transactions: {:?}", block.transactions.len());

let taiko_guest_input = if taiko_chain_spec.is_taiko() {
prepare_taiko_chain_input(
Expand Down Expand Up @@ -165,14 +169,17 @@ pub async fn preflight<BDP: BlockDataProvider>(
let mut done = false;
let mut num_iterations = 0;
while !done {
info!("Execution iteration {num_iterations}...");
inplace_print(&format!("Execution iteration {num_iterations}..."));
builder.mut_db().unwrap().optimistic = num_iterations + 1 < max_iterations;
builder = builder.execute_transactions::<TkoTxExecStrategy>()?;
if builder.mut_db().unwrap().fetch_data().await {
done = true;
}
num_iterations += 1;
}
clear_line();
println!("State data fetched in {num_iterations} iterations");

let provider_db = builder.mut_db().unwrap();

// Gather inclusion proofs for the initial and final state
Expand Down Expand Up @@ -236,8 +243,10 @@ async fn prepare_taiko_chain_input(
let l1_state_block_number = anchor_call.l1BlockId;
let l1_inclusion_block_number = l1_state_block_number + 1;

info!("anchor L1 block id: {:?}", anchor_call.l1BlockId);
info!("anchor L1 state root: {:?}", anchor_call.l1StateRoot);
debug!(
"anchor L1 block id: {:?}\nanchor L1 state root: {:?}",
anchor_call.l1BlockId, anchor_call.l1StateRoot
);

// Get the L1 block in which the L2 block was included so we can fetch the DA data.
// Also get the L1 state block header so that we can prove the L1 state root.
Expand All @@ -253,7 +262,7 @@ async fn prepare_taiko_chain_input(
HostError::Preflight("No L1 state block hash for the requested block".to_owned())
})?;

info!("l1_state_root_block hash: {l1_state_block_hash:?}");
debug!("l1_state_root_block hash: {l1_state_block_hash:?}");

let l1_inclusion_block_hash = l1_inclusion_block.header.hash.ok_or_else(|| {
HostError::Preflight("No L1 inclusion block hash for the requested block".to_owned())
Expand All @@ -270,7 +279,7 @@ async fn prepare_taiko_chain_input(

// Fetch the tx data from either calldata or blobdata
let (tx_data, tx_blob_hash) = if proposal_event.meta.blobUsed {
info!("blob active");
debug!("blob active");
// Get the blob hashes attached to the propose tx
let blob_hashes = proposal_tx.blob_versioned_hashes.unwrap_or_default();
assert!(!blob_hashes.is_empty());
Expand Down
13 changes: 9 additions & 4 deletions host/src/provider/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use alloy_provider::{ProviderBuilder, ReqwestProvider, RootProvider};
use alloy_rpc_client::{ClientBuilder, RpcClient};
use alloy_rpc_types::{Block, BlockId, BlockNumberOrTag, EIP1186AccountProofResponse};
use alloy_transport_http::Http;
use raiko_lib::{clear_line, inplace_print};
use raiko_lib::clear_line;
use reqwest_alloy::Client;
use revm::primitives::{AccountInfo, Bytecode};
use std::collections::HashMap;
use tracing::trace;

use crate::{
interfaces::error::{HostError, HostResult},
Expand Down Expand Up @@ -227,9 +228,13 @@ impl BlockDataProvider for RpcBlockDataProvider {

let batch_limit = 1000;
while !accounts.is_empty() {
inplace_print(&format!(
"fetching storage proof {idx}/{num_storage_proofs}..."
));
if cfg!(debug_assertions) {
raiko_lib::inplace_print(&format!(
"fetching storage proof {idx}/{num_storage_proofs}..."
));
} else {
trace!("Fetching storage proof {idx}/{num_storage_proofs}...");
}

// Create a batch for all storage proofs
let mut batch = self.client.new_batch();
Expand Down
4 changes: 2 additions & 2 deletions host/src/raiko.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use raiko_lib::protocol_instance::{assemble_protocol_instance, ProtocolInstance}
use raiko_lib::prover::{to_proof, Proof, Prover, ProverError, ProverResult};
use raiko_lib::utils::HeaderHasher;
use serde::{Deserialize, Serialize};
use tracing::{error, info, trace, warn};
use tracing::{debug, error, info, trace, warn};

use crate::preflight::preflight;
use crate::{
Expand Down Expand Up @@ -59,7 +59,7 @@ impl Raiko {
Ok((header, _mpt_node)) => {
info!("Verifying final state using provider data ...");
info!("Final block hash derived successfully. {}", header.hash());
info!("Final block header derived successfully. {header:?}");
debug!("Final block header derived successfully. {header:?}");
let pi = self
.request
.proof_type
Expand Down
10 changes: 4 additions & 6 deletions host/src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::{net::SocketAddr, str::FromStr};

use crate::{interfaces::error::HostError, server::api::create_router, ProverState};
use anyhow::Context;
use std::{net::SocketAddr, str::FromStr};
use tokio::net::TcpListener;
use tracing::debug;

use crate::{interfaces::error::HostError, server::api::create_router, ProverState};
use tracing::info;

pub mod api;

Expand All @@ -14,7 +12,7 @@ pub async fn serve(state: ProverState) -> anyhow::Result<()> {
.map_err(|_| HostError::InvalidAddress(state.opts.address.clone()))?;
let listener = TcpListener::bind(addr).await?;

debug!("Listening on: {}", listener.local_addr()?);
info!("Listening on: {}", listener.local_addr()?);

let router = create_router(state.opts.concurrency_limit).with_state(state);
axum::serve(listener, router)
Expand Down
1 change: 1 addition & 0 deletions lib/Cargo.toml
petarvujovic98 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ hex = { workspace = true }
c-kzg = { workspace = true }
sha2 = { workspace = true }
cfg-if = { workspace = true }
tracing = { workspace = true }

# [target.'cfg(feature = "std")'.dependencies]
thiserror = { workspace = true, optional = true }
Expand Down
29 changes: 15 additions & 14 deletions lib/src/builder/execute.rs
petarvujovic98 marked this conversation as resolved.
Show resolved Hide resolved
petarvujovic98 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use core::{fmt::Debug, mem::take, str::from_utf8};
use std::collections::HashSet;

use alloy_consensus::{constants::BEACON_ROOTS_ADDRESS, TxEnvelope};
use alloy_primitives::{TxKind, U256};
use anyhow::{anyhow, bail, ensure, Context, Error, Result};
use core::{fmt::Debug, mem::take, str::from_utf8};
#[cfg(feature = "std")]
use log::debug;
use raiko_primitives::{
Expand All @@ -32,6 +30,8 @@ use revm::{
},
taiko, Database, DatabaseCommit, Evm, JournaledState,
};
use std::collections::HashSet;
use tracing::trace;
cfg_if::cfg_if! {
if #[cfg(feature = "tracer")] {
use std::{fs::{OpenOptions, File}, io::{BufWriter, Write}, sync::{Arc, Mutex}};
Expand All @@ -47,7 +47,6 @@ use crate::{
guest_mem_forget, inplace_print, print_duration,
time::{AddAssign, Duration, Instant},
utils::{check_anchor_tx, generate_transactions},
Measurement,
};

/// Minimum supported protocol version: SHANGHAI
Expand Down Expand Up @@ -82,7 +81,7 @@ impl TxExecStrategy for TkoTxExecStrategy {
let chain_spec = &block_builder.input.chain_spec;
let chain_id = chain_spec.chain_id();
let is_taiko = chain_spec.is_taiko();
println!("spec_id: {spec_id:?}");
trace!("spec_id: {spec_id:?}");

// generate the transactions from the tx list
// For taiko blocks, insert the anchor tx as the first transaction
Expand Down Expand Up @@ -175,7 +174,11 @@ impl TxExecStrategy for TkoTxExecStrategy {
let mut actual_tx_no = 0usize;
let num_transactions = transactions.len();
for (tx_no, tx) in take(&mut transactions).into_iter().enumerate() {
inplace_print(&format!("\rprocessing tx {tx_no}/{num_transactions}..."));
if !is_optimistic {
inplace_print(&format!("\rprocessing tx {tx_no}/{num_transactions}..."));
} else {
trace!("\rprocessing tx {tx_no}/{num_transactions}...");
}

#[cfg(feature = "tracer")]
let trace = set_trace_writer(
Expand Down Expand Up @@ -283,7 +286,7 @@ impl TxExecStrategy for TkoTxExecStrategy {
}
};
#[cfg(feature = "std")]
debug!(" Ok: {result:?}");
trace!(" Ok: {result:?}");

#[cfg(feature = "tracer")]
// Flush the trace writer
Expand Down Expand Up @@ -331,14 +334,15 @@ impl TxExecStrategy for TkoTxExecStrategy {

tx_misc_duration.add_assign(start.elapsed());
}
clear_line();
print_duration("Tx transact time: ", tx_transact_duration);
print_duration("Tx misc time: ", tx_misc_duration);
if !is_optimistic {
clear_line();
print_duration("Tx transact time: ", tx_transact_duration);
print_duration("Tx misc time: ", tx_misc_duration);
}

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

// process withdrawals unconditionally after any transactions
let measurement = Measurement::start("Processing withdrawals...", true);
let mut withdrawals_trie = MptNode::default();
for (i, withdrawal) in block_builder.input.withdrawals.iter().enumerate() {
// the withdrawal amount is given in Gwei
Expand All @@ -353,10 +357,8 @@ impl TxExecStrategy for TkoTxExecStrategy {
.insert_rlp(&i.to_rlp(), withdrawal)
.with_context(|| "failed to insert withdrawal")?;
}
measurement.stop();

// Update result header with computed values
let measurement = Measurement::start("Generating block header...", true);
header.transactions_root = tx_trie.hash();
header.receipts_root = receipt_trie.hash();
header.logs_bloom = logs_bloom;
Expand All @@ -367,7 +369,6 @@ impl TxExecStrategy for TkoTxExecStrategy {
if spec_id >= SpecId::CANCUN {
header.blob_gas_used = Some(blob_gas_used.into());
}
measurement.stop();

// Leak memory, save cycles
guest_mem_forget([tx_trie, receipt_trie, withdrawals_trie]);
Expand Down
10 changes: 6 additions & 4 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ mod no_std {
};
}

use tracing::debug;

pub mod builder;
pub mod consts;
pub mod input;
Expand Down Expand Up @@ -91,11 +93,11 @@ pub struct Measurement {
impl Measurement {
pub fn start(title: &str, inplace: bool) -> Measurement {
if inplace {
print!("{title}");
debug!("{title}");
#[cfg(feature = "std")]
io::stdout().flush().unwrap();
} else if !title.is_empty() {
println!("{title}");
debug!("{title}");
}

Self {
Expand Down Expand Up @@ -124,7 +126,7 @@ impl Measurement {
}

pub fn print_duration(title: &str, duration: time::Duration) {
println!(
debug!(
"{title}{}.{:03} seconds",
duration.as_secs(),
duration.subsec_millis()
Expand All @@ -133,7 +135,7 @@ pub fn print_duration(title: &str, duration: time::Duration) {

pub fn inplace_print(title: &str) {
print!("\r{title}");
#[cfg(feature = "std")]
#[cfg(all(feature = "std", debug_assertions))]
io::stdout().flush().unwrap();
}

Expand Down
Loading
Loading