Skip to content

Commit

Permalink
Merge pull request #92 from taikoxyz/holesky
Browse files Browse the repository at this point in the history
Add support for holesky + sync mode
  • Loading branch information
Brechtpd authored Apr 11, 2024
2 parents 78a6c56 + 956e8c4 commit 14ea068
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 69 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,17 @@ cargo run
Then in another terminal you can do requests like this:

```
./prove_block.sh testnet native 10
./prove_block.sh taiko_a7 native 10
```

Look into `prove_block.sh` for the available options or run the script without inputs and it will tell you.

You can also automatically sync with the tip of the chain and prove all new blocks:

```
./prove_block.sh taiko_a7 native sync
```

## Provers

Provers can be enabled using features. To compile with all of them (using standard options):
Expand All @@ -63,7 +69,7 @@ RISC0_DEV_MODE=1 cargo run --release --features risc0
# edit run_bonsai.sh and run
run_bonsai.sh
# then
prove_block.sh testnet risc0-bonsai 10
prove_block.sh taiko_a7 risc0-bonsai 10
```

#### CPU
Expand Down
4 changes: 4 additions & 0 deletions host/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub async fn execute<D: Prover>(
config: &serde_json::Value,
cached_input: Option<GuestInput>,
) -> Result<(GuestInput, Proof)> {
let total_proving_time = Measurement::start("", false);

// Generate the input
let input = if let Some(cached_input) = cached_input {
println!("Using cached input");
Expand Down Expand Up @@ -68,6 +70,8 @@ pub async fn execute<D: Prover>(
measurement.stop_with("=> Proof generated");
memory::print_stats("Prover peak memory used: ");

total_proving_time.stop_with("====> Complete proof generated");

res
}

Expand Down
2 changes: 1 addition & 1 deletion host/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ mod memory {
pub(crate) fn print_stats(title: &str) {
let max_memory = get_max_allocated();
println!(
"{}{}.{} MB",
"{}{}.{:06} MB",
title,
max_memory / 1000000,
max_memory % 1000000
Expand Down
39 changes: 24 additions & 15 deletions host/src/preflight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ pub fn preflight(

println!("\nblock.hash: {:?}", block.header.hash.unwrap());
println!("block.parent_hash: {:?}", block.header.parent_hash);
println!("block gas used: {:?}", block.header.gas_used.as_limbs()[0]);
println!("block transactions: {:?}", block.transactions.len());

let taiko_guest_input = if network != Network::Ethereum {
let taiko_guest_input = if network.is_taiko() {
let http_l1 = Http::new(Url::parse(&l1_rpc_url.clone().unwrap()).expect("invalid rpc url"));
let provider_l1 =
ProviderBuilder::new().provider(RootProvider::new(RpcClient::new(http_l1, true)));
Expand Down Expand Up @@ -157,6 +158,7 @@ pub fn preflight(
let input = GuestInput {
network,
block_number,
gas_used: block.header.gas_used.try_into().unwrap(),
block_hash: block.header.hash.unwrap().0.into(),
beneficiary: block.header.miner,
gas_limit: block.header.gas_limit.try_into().unwrap(),
Expand Down Expand Up @@ -187,6 +189,7 @@ pub fn preflight(
// Create the block builder, run the transactions and extract the DB
let provider_db = ProviderDb::new(
provider,
network,
parent_block.header.number.unwrap().try_into().unwrap(),
);
let mut builder = BlockBuilder::new(&input)
Expand All @@ -199,8 +202,12 @@ pub fn preflight(

// Gather inclusion proofs for the initial and final state
let measurement = Measurement::start("Fetching storage proofs...", true);
let (parent_proofs, proofs) = provider_db.get_proofs()?;
measurement.stop();
let (parent_proofs, proofs, num_storage_proofs) = provider_db.get_proofs()?;
measurement.stop_with_count(&format!(
"[{} Account/{} Storage]",
parent_proofs.len() + proofs.len(),
num_storage_proofs
));

// Construct the state trie and storage from the storage proofs.
let measurement = Measurement::start("Constructing MPT...", true);
Expand Down Expand Up @@ -416,18 +423,20 @@ fn get_block_proposed_event(

fn get_transactions_from_block(block: &AlloyBlock) -> Vec<TxEnvelope> {
let mut transactions: Vec<TxEnvelope> = Vec::new();
match &block.transactions {
BlockTransactions::Full(txs) => {
for tx in txs {
transactions.push(from_block_tx(tx));
}
},
_ => unreachable!("Block is too old, please connect to an archive node or use a block that is at most 128 blocks old."),
};
assert!(
transactions.len() == block.transactions.len(),
"unexpected number of transactions"
);
if !block.transactions.is_empty() {
match &block.transactions {
BlockTransactions::Full(txs) => {
for tx in txs {
transactions.push(from_block_tx(tx));
}
},
_ => unreachable!("Block is too old, please connect to an archive node or use a block that is at most 128 blocks old."),
};
assert!(
transactions.len() == block.transactions.len(),
"unexpected number of transactions"
);
}
transactions
}

Expand Down
78 changes: 47 additions & 31 deletions host/src/provider_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use std::{
use alloy_consensus::Header as AlloyConsensusHeader;
use alloy_provider::{Provider, ReqwestProvider};
use alloy_rpc_types::{BlockId, EIP1186AccountProofResponse};
use raiko_lib::{clear_line, inplace_print, mem_db::MemDb, taiko_utils::to_header};
use raiko_lib::{
clear_line, consts::Network, inplace_print, mem_db::MemDb, print_duration,
taiko_utils::to_header,
};
use raiko_primitives::{Address, B256, U256};
use revm::{
primitives::{Account, AccountInfo, Bytecode, HashMap},
Expand All @@ -31,16 +34,18 @@ use crate::preflight::{batch_get_history_headers, get_block};

pub struct ProviderDb {
pub provider: ReqwestProvider,
pub network: Network,
pub block_number: u64,
pub initial_db: MemDb,
pub current_db: MemDb,
async_executor: Handle,
}

impl ProviderDb {
pub fn new(provider: ReqwestProvider, block_number: u64) -> Self {
pub fn new(provider: ReqwestProvider, network: Network, block_number: u64) -> Self {
ProviderDb {
provider,
network,
block_number,
initial_db: MemDb::default(),
current_db: MemDb::default(),
Expand Down Expand Up @@ -90,6 +95,7 @@ impl ProviderDb {
(
HashMap<Address, EIP1186AccountProofResponse>,
HashMap<Address, EIP1186AccountProofResponse>,
usize,
),
anyhow::Error,
> {
Expand Down Expand Up @@ -128,7 +134,7 @@ impl ProviderDb {
num_storage_proofs,
)?;

Ok((initial_proofs, latest_proofs))
Ok((initial_proofs, latest_proofs, num_storage_proofs))
}

pub fn get_ancestor_headers(&mut self) -> Result<Vec<AlloyConsensusHeader>, anyhow::Error> {
Expand Down Expand Up @@ -224,16 +230,35 @@ impl Database for ProviderDb {
return Ok(block_hash);
}

// Get the 256 history block hashes from the provider at first time for anchor
// transaction.
let block_number = u64::try_from(number).unwrap();
for block in batch_get_history_headers(&self.provider, &self.async_executor, block_number)?
{
let block_number = block.header.number.unwrap().try_into().unwrap();
let block_hash = block.header.hash.unwrap();
if self.network.is_taiko() {
// Get the 256 history block hashes from the provider at first time for anchor
// transaction.
for block in
batch_get_history_headers(&self.provider, &self.async_executor, block_number)?
{
let block_number = block.header.number.unwrap().try_into().unwrap();
let block_hash = block.header.hash.unwrap();
self.initial_db.insert_block_hash(block_number, block_hash);
}
self.block_hash(number)
} else {
// Get the block hash from the provider.
let block_hash = self.async_executor.block_on(async {
self.provider
.get_block_by_number(block_number.into(), false)
.await
.unwrap()
.unwrap()
.header
.hash
.unwrap()
.0
.into()
});
self.initial_db.insert_block_hash(block_number, block_hash);
Ok(block_hash)
}
self.block_hash(number)
}

fn code_by_hash(&mut self, _code_hash: B256) -> Result<Bytecode, Self::Error> {
Expand Down Expand Up @@ -278,23 +303,17 @@ impl MeasuredProviderDb {

pub fn print_report(&self) {
println!("db accesses: ");
println!(
"- account: {}.{} seconds ({} ops)",
self.time_basic.as_secs(),
self.time_basic.subsec_millis(),
self.num_basic
print_duration(
&format!("- account [{} ops]: ", self.num_basic),
self.time_basic,
);
println!(
"- storage: {}.{} seconds ({} ops)",
self.time_storage.as_secs(),
self.time_storage.subsec_millis(),
self.num_storage
print_duration(
&format!("- storage [{} ops]: ", self.num_storage),
self.time_storage,
);
println!(
"- block_hash: {}.{} seconds ({} ops)",
self.time_block_hash.as_secs(),
self.time_block_hash.subsec_millis(),
self.num_block_hash
print_duration(
&format!("- block_hash [{} ops]: ", self.num_block_hash),
self.time_block_hash,
);
println!("- code_by_hash: {}", self.num_code_by_hash);
}
Expand All @@ -307,26 +326,23 @@ impl Database for MeasuredProviderDb {
self.num_basic += 1;
let start = Instant::now();
let res = self.provider.basic(address);
self.time_basic
.add_assign(Instant::now().duration_since(start));
self.time_basic.add_assign(start.elapsed());
res
}

fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error> {
self.num_storage += 1;
let start = Instant::now();
let res = self.provider.storage(address, index);
self.time_storage
.add_assign(Instant::now().duration_since(start));
self.time_storage.add_assign(start.elapsed());
res
}

fn block_hash(&mut self, number: U256) -> Result<B256, Self::Error> {
self.num_block_hash += 1;
let start = Instant::now();
let res = self.provider.block_hash(number);
self.time_block_hash
.add_assign(Instant::now().duration_since(start));
self.time_block_hash.add_assign(start.elapsed());
res
}

Expand Down
16 changes: 8 additions & 8 deletions lib/src/builder/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use super::TxExecStrategy;
use crate::{
builder::BlockBuilder,
clear_line,
consts::{get_network_spec, Network, GWEI_TO_WEI},
consts::{get_network_spec, GWEI_TO_WEI},
guest_mem_forget, inplace_print, print_duration,
taiko_utils::{check_anchor_tx, generate_transactions},
time::{AddAssign, Duration, Instant},
Expand Down Expand Up @@ -73,14 +73,14 @@ impl TxExecStrategy for TkoTxExecStrategy {
println!("spec_id: {:?}", spec_id);

let network = block_builder.input.network;
let is_taiko = network != Network::Ethereum;
let is_taiko = network.is_taiko();

// generate the transactions from the tx list
// For taiko blocks, insert the anchor tx as the first transaction
let anchor_tx = if block_builder.input.network == Network::Ethereum {
None
} else {
let anchor_tx = if block_builder.input.network.is_taiko() {
Some(serde_json::from_str(&block_builder.input.taiko.anchor_tx.clone()).unwrap())
} else {
None
};
let mut transactions = generate_transactions(
block_builder.input.taiko.block_proposed.meta.blobUsed,
Expand Down Expand Up @@ -170,7 +170,7 @@ impl TxExecStrategy for TkoTxExecStrategy {
let tx_env = &mut evm.env_mut().tx;
fill_eth_tx_env(tx_env, &tx)?;
// Set and check some taiko specific values
if network != Network::Ethereum {
if network.is_taiko() {
// set if the tx is the anchor tx
tx_env.taiko.is_anchor = is_anchor;
// set the treasury address
Expand Down Expand Up @@ -260,7 +260,7 @@ impl TxExecStrategy for TkoTxExecStrategy {
#[cfg(feature = "std")]
debug!(" Ok: {result:?}");

tx_transact_duration.add_assign(Instant::now().duration_since(start));
tx_transact_duration.add_assign(start.elapsed());

let start = Instant::now();

Expand Down Expand Up @@ -300,7 +300,7 @@ impl TxExecStrategy for TkoTxExecStrategy {
// If we got here it means the tx is not invalid
actual_tx_no += 1;

tx_misc_duration.add_assign(Instant::now().duration_since(start));
tx_misc_duration.add_assign(start.elapsed());
}
clear_line();
print_duration("Tx transact time: ", tx_transact_duration);
Expand Down
Loading

0 comments on commit 14ea068

Please sign in to comment.