Skip to content

Commit

Permalink
build on derive
Browse files Browse the repository at this point in the history
  • Loading branch information
hashcashier committed Jan 24, 2024
1 parent e96c68f commit f3cb39e
Show file tree
Hide file tree
Showing 23 changed files with 383 additions and 226 deletions.
2 changes: 1 addition & 1 deletion guests/eth-block/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn main() {
let mut output = EthereumStrategy::build_from(&ETH_MAINNET_CHAIN_SPEC, input)
.expect("Failed to build the resulting block");
// Abridge successful construction results
if let BlockBuildOutput::SUCCESS { new_block_hash, new_block_head, new_block_state } = &mut output {
if let BlockBuildOutput::SUCCESS { new_block_head, new_block_state, .. } = &mut output {
let trie_root = core::mem::replace(new_block_state, new_block_head.state_root.into());
// Leak memory, save cycles
core::mem::forget(trie_root);
Expand Down
4 changes: 2 additions & 2 deletions guests/op-block/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ pub fn main() {
// Read the input previous block and transaction data
let input = env::read();
// Build the resulting block
let output = OptimismStrategy::build_from(&OP_MAINNET_CHAIN_SPEC, input)
let mut output = OptimismStrategy::build_from(&OP_MAINNET_CHAIN_SPEC, input)
.expect("Failed to build the resulting block");
// Abridge successful construction results
if let BlockBuildOutput::SUCCESS { new_block_hash, new_block_head, new_block_state } = &mut output {
if let BlockBuildOutput::SUCCESS { new_block_head, new_block_state, .. } = &mut output {
let trie_root = core::mem::replace(new_block_state, new_block_head.state_root.into());
// Leak memory, save cycles
core::mem::forget(trie_root);
Expand Down
13 changes: 4 additions & 9 deletions guests/op-compose/Cargo.lock

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

3 changes: 1 addition & 2 deletions guests/op-compose/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ zeth-lib = { path = "../../lib", default-features = false }
zeth-primitives = { path = "../../primitives", default-features = false }

[patch.crates-io]
radium = { git = 'https://github.com/ferrilab/radium.git', rev = "723bed5" }
# use optimized risc0 circuit
crypto-bigint = { git = "https://github.com/risc0/RustCrypto-crypto-bigint", tag = "v0.5.2-risc0" }
k256 = { git = "https://github.com/risc0/RustCrypto-elliptic-curves", tag = "k256/v0.13.1-risc0" }
k256 = { git = "https://github.com/risc0/RustCrypto-elliptic-curves", tag = "k256/v0.13.3-risczero.0" }
sha2 = { git = "https://github.com/risc0/RustCrypto-hashes", tag = "sha2/v0.10.6-risc0" }

[profile.release]
Expand Down
2 changes: 1 addition & 1 deletion guests/op-derive/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ risc0_zkvm::guest::entry!(main);

pub fn main() {
let derive_input: DeriveInput<MemDb> = env::read();
let mut derive_machine = DeriveMachine::new(&OPTIMISM_CHAIN_SPEC, derive_input)
let mut derive_machine = DeriveMachine::new(&OPTIMISM_CHAIN_SPEC, derive_input, None)
.expect("Could not create derive machine");
let output = derive_machine
.derive()
Expand Down
12 changes: 1 addition & 11 deletions host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{
fs,
path::{Path, PathBuf},
};
use std::fs;

use risc0_zkvm::Receipt;

pub mod cli;
pub mod operations;

pub fn cache_file_path(cache_path: &Path, network: &str, block_no: u64, ext: &str) -> PathBuf {
cache_path
.join(network)
.join(block_no.to_string())
.with_extension(ext)
}

pub fn save_receipt(file_reference: &String, receipt: &Receipt, index: Option<&mut usize>) {
let receipt_serialized = bincode::serialize(receipt).expect("Failed to serialize receipt!");
let path = if let Some(number) = index {
Expand Down
3 changes: 1 addition & 2 deletions host/src/operations/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ use serde::{Deserialize, Serialize};
use zeth_lib::{
builder::BlockBuilderStrategy,
consts::ChainSpec,
host::{preflight::Preflight, verify::Verifier},
host::{cache_file_path, preflight::Preflight, verify::Verifier},
input::BlockBuildInput,
output::BlockBuildOutput,
};

use crate::{
cache_file_path,
cli::Cli,
operations::{execute, maybe_prove},
};
Expand Down
7 changes: 5 additions & 2 deletions host/src/operations/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ use alloy_sol_types::SolInterface;
use log::warn;
use zeth_lib::{
consts::Network,
host::provider::{new_provider, BlockQuery},
host::{
cache_file_path,
provider::{new_provider, BlockQuery},
},
optimism::OpSystemInfo,
};

use crate::{cache_file_path, cli::Cli};
use crate::cli::Cli;

pub async fn op_info(cli: Cli) -> anyhow::Result<()> {
let core_args = cli.core_args().clone();
Expand Down
145 changes: 79 additions & 66 deletions host/src/operations/rollups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ use anyhow::Context;
use log::info;
use zeth_guests::*;
use zeth_lib::{
builder::OptimismStrategy,
builder::{BlockBuilderStrategy, OptimismStrategy},
consts::{Network, OP_MAINNET_CHAIN_SPEC},
host::{preflight::Preflight, rpc_db::RpcDb},
input::BlockBuildInput,
host::{rpc_db::RpcDb, ProviderFactory},
optimism::{
batcher_db::BatcherDb,
composition::{ComposeInput, ComposeInputOperation, ComposeOutputOperation},
Expand All @@ -31,61 +30,22 @@ use zeth_lib::{
};
use zeth_primitives::{
block::Header,
transactions::optimism::OptimismTxEssence,
tree::{MerkleMountainRange, MerkleProof},
};

use crate::{
cache_file_path,
cli::{Cli, CoreArgs},
cli::Cli,
operations::{execute, maybe_prove},
};

async fn fetch_op_blocks(
core_args: &CoreArgs,
block_number: u64,
block_count: u64,
) -> anyhow::Result<Vec<BlockBuildInput<OptimismTxEssence>>> {
let mut op_blocks = vec![];
for i in 0..block_count {
let block_number = block_number + i;
let rpc_cache = core_args.cache.as_ref().map(|dir| {
cache_file_path(dir, &Network::Optimism.to_string(), block_number, "json.gz")
});
let rpc_url = core_args.op_rpc_url.clone();
// Collect block building data
let preflight_result = tokio::task::spawn_blocking(move || {
OptimismStrategy::run_preflight(
OP_MAINNET_CHAIN_SPEC.clone(),
rpc_cache,
rpc_url,
block_number,
)
})
.await?
.context("preflight failed")?;

// Create the guest input from [Init]
let input = preflight_result
.clone()
.try_into()
.context("invalid preflight data")?;

op_blocks.push(input);
}

Ok(op_blocks)
}

pub async fn derive_rollup_blocks(cli: Cli, file_reference: &String) -> anyhow::Result<()> {
info!("Fetching data ...");
let core_args = cli.core_args().clone();
let op_blocks = fetch_op_blocks(
&core_args,
core_args.block_number + 1,
core_args.block_count,
)
.await?;
let op_builder_provider_factory = ProviderFactory::new(
core_args.cache.clone(),
Network::Optimism,
core_args.op_rpc_url.clone(),
);

let (derive_input, output) = tokio::task::spawn_blocking(move || {
let derive_input = DeriveInput {
Expand All @@ -96,16 +56,31 @@ pub async fn derive_rollup_blocks(cli: Cli, file_reference: &String) -> anyhow::
),
op_head_block_no: core_args.block_number,
op_derive_block_count: core_args.block_count,
op_blocks: op_blocks.clone(),
op_block_outputs: vec![],
builder_image_id: OP_BLOCK_ID,
};
let mut derive_machine = DeriveMachine::new(&OPTIMISM_CHAIN_SPEC, derive_input)
.context("Could not create derive machine")?;
let mut derive_machine = DeriveMachine::new(
&OPTIMISM_CHAIN_SPEC,
derive_input,
Some(op_builder_provider_factory),
)
.context("Could not create derive machine")?;
let derive_output = derive_machine.derive().context("could not derive")?;
let op_block_outputs = derive_output
.op_block_inputs
.clone()
.into_iter()
.map(|input| {
OptimismStrategy::build_from(&OP_MAINNET_CHAIN_SPEC, input)
.expect("Failed to build op block")
})
.collect();
let derive_input_mem = DeriveInput {
db: derive_machine.derive_input.db.get_mem_db(),
op_head_block_no: core_args.block_number,
op_derive_block_count: core_args.block_count,
op_blocks,
op_block_outputs,
builder_image_id: OP_BLOCK_ID,
};
let out: anyhow::Result<_> = Ok((derive_input_mem, derive_output));
out
Expand All @@ -115,10 +90,21 @@ pub async fn derive_rollup_blocks(cli: Cli, file_reference: &String) -> anyhow::

info!("Running from memory ...");
{
let output_mem = DeriveMachine::new(&OPTIMISM_CHAIN_SPEC, derive_input.clone())
.context("Could not create derive machine")?
.derive()
.unwrap();
// todo: run without factory (using outputs)
let core_args = cli.core_args().clone();
let op_builder_provider_factory = ProviderFactory::new(
core_args.cache.clone(),
Network::Optimism,
core_args.op_rpc_url.clone(),
);
let output_mem = DeriveMachine::new(
&OPTIMISM_CHAIN_SPEC,
derive_input.clone(),
Some(op_builder_provider_factory),
)
.context("Could not create derive machine")?
.derive()
.unwrap();
assert_eq!(output, output_mem);
}

Expand Down Expand Up @@ -280,18 +266,26 @@ pub async fn compose_derived_rollup_blocks(
core_args.op_rpc_url.clone(),
core_args.cache.clone(),
);
let op_head_block_no = core_args.block_number + op_block_index;
let op_blocks = fetch_op_blocks(&core_args, op_head_block_no + 1, composition_size).await?;
let op_builder_provider_factory = ProviderFactory::new(
core_args.cache.clone(),
Network::Optimism,
core_args.op_rpc_url.clone(),
);

let (input, output, chain) = tokio::task::spawn_blocking(move || {
let derive_input = DeriveInput {
db,
op_head_block_no: core_args.block_number + op_block_index,
op_derive_block_count: composition_size,
op_blocks: op_blocks.clone(),
op_block_outputs: vec![],
builder_image_id: OP_BLOCK_ID,
};
let mut derive_machine = DeriveMachine::new(&OPTIMISM_CHAIN_SPEC, derive_input)
.expect("Could not create derive machine");
let mut derive_machine = DeriveMachine::new(
&OPTIMISM_CHAIN_SPEC,
derive_input,
Some(op_builder_provider_factory),
)
.expect("Could not create derive machine");
let eth_head_no = derive_machine.op_batcher.state.epoch.number;
let eth_head = derive_machine
.derive_input
Expand Down Expand Up @@ -322,11 +316,21 @@ pub async fn compose_derived_rollup_blocks(
}
eth_chain.push(eth_tail);

let op_block_outputs = derive_output
.op_block_inputs
.clone()
.into_iter()
.map(|input| {
OptimismStrategy::build_from(&OP_MAINNET_CHAIN_SPEC, input)
.expect("Failed to build op block")
})
.collect();
let derive_input_mem = DeriveInput {
db: derive_machine.derive_input.db.get_mem_db(),
op_head_block_no: core_args.block_number + op_block_index,
op_derive_block_count: composition_size,
op_blocks,
op_block_outputs,
builder_image_id: OP_BLOCK_ID,
};
let out: anyhow::Result<_> = Ok((derive_input_mem, derive_output, eth_chain));
out
Expand All @@ -335,10 +339,19 @@ pub async fn compose_derived_rollup_blocks(

info!("Deriving ...");
{
let output_mem = DeriveMachine::new(&OPTIMISM_CHAIN_SPEC, input.clone())
.expect("Could not create derive machine")
.derive()
.context("could not derive")?;
let op_builder_provider_factory = ProviderFactory::new(
core_args.cache.clone(),
Network::Optimism,
core_args.op_rpc_url.clone(),
);
let output_mem = DeriveMachine::new(
&OPTIMISM_CHAIN_SPEC,
input.clone(),
Some(op_builder_provider_factory),
)
.expect("Could not create derive machine")
.derive()
.context("could not derive")?;
assert_eq!(output, output_mem);
}

Expand Down
11 changes: 5 additions & 6 deletions lib/src/builder/execute/optimism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,19 @@ impl TxExecStrategy<OptimismTxEssence> for OpTxExecStrategy {
.into_iter()
.enumerate()
{
// verify the transaction signature
let tx_from = tx
.recover_from()
.with_context(|| format!("Error recovering address for transaction {}", tx_no))?;

#[cfg(not(target_os = "zkvm"))]
{
let tx_hash = tx.hash();
debug!("Tx no. {} (hash: {})", tx_no, tx_hash);
debug!(" Type: {}", tx.essence.tx_type());
debug!(" Fr: {:?}", tx_from);
debug!(" To: {:?}", tx.essence.to().unwrap_or_default());
}

// verify the transaction signature
let tx_from = tx
.recover_from()
.with_context(|| format!("Error recovering address for transaction {}", tx_no))?;

// verify transaction gas
let block_available_gas = block_builder.input.gas_limit - cumulative_gas_used;
if block_available_gas < tx.essence.gas_limit() {
Expand Down
Loading

0 comments on commit f3cb39e

Please sign in to comment.