Skip to content

Commit

Permalink
code refine: re-org the genesis into the right chain spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
smtmfft committed May 17, 2024
1 parent 9d3e0a6 commit 07d3d82
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 57 deletions.
12 changes: 6 additions & 6 deletions host/config/chain_spec_list_default.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"rpc": "https://rpc.ankr.com/eth",
"beacon_rpc": "https://ethereum-beacon-api.publicnode.com",
"sgx_verifier_address": null,
"genesis_time": 0,
"seconds_per_slot": 1,
"genesis_time": 1606824023,
"seconds_per_slot": 12,
"is_taiko": false
},
{
Expand Down Expand Up @@ -58,8 +58,8 @@
"rpc": "https://ethereum-holesky-rpc.publicnode.com",
"beacon_rpc": " https://api.holesky.blobscan.com",
"sgx_verifier_address": null,
"genesis_time": 0,
"seconds_per_slot": 1,
"genesis_time": 1695902400,
"seconds_per_slot": 12,
"is_taiko": false
},
{
Expand All @@ -83,8 +83,8 @@
"rpc": "https://rpc.hekla.taiko.xyz",
"beacon_rpc": null,
"sgx_verifier_address": "0x532efbf6d62720d0b2a2bb9d11066e8588cae6d9",
"genesis_time": 1695902400,
"seconds_per_slot": 12,
"genesis_time": 0,
"seconds_per_slot": 1,
"is_taiko": true
}
]
42 changes: 20 additions & 22 deletions host/src/preflight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ use crate::{
pub async fn preflight<BDP: BlockDataProvider>(
provider: BDP,
block_number: u64,
chain_spec: ChainSpec,
l1_chain_spec: ChainSpec,
taiko_chain_spec: ChainSpec,
prover_data: TaikoProverData,
l1_rpc_url: Option<String>,
beacon_rpc_url: Option<String>,
) -> HostResult<GuestInput> {
let measurement = Measurement::start("Fetching block data...", false);

Expand All @@ -67,13 +66,12 @@ pub async fn preflight<BDP: BlockDataProvider>(
info!("block gas used: {:?}", block.header.gas_used);
info!("block transactions: {:?}", block.transactions.len());

let taiko_guest_input = if chain_spec.is_taiko() {
let taiko_guest_input = if taiko_chain_spec.is_taiko() {
prepare_taiko_chain_input(
&chain_spec,
&l1_chain_spec,
&taiko_chain_spec,
block_number,
block,
l1_rpc_url,
beacon_rpc_url,
prover_data,
)
.await?
Expand All @@ -89,7 +87,7 @@ pub async fn preflight<BDP: BlockDataProvider>(

let input =
GuestInput {
chain_spec: chain_spec.clone(),
chain_spec: taiko_chain_spec.clone(),
block_number,
gas_used: block.header.gas_used.try_into().map_err(|_| {
HostError::Conversion("Failed converting gas used to u64".to_string())
Expand Down Expand Up @@ -152,7 +150,7 @@ pub async fn preflight<BDP: BlockDataProvider>(
// Create the block builder, run the transactions and extract the DB
let provider_db = ProviderDb::new(
provider,
chain_spec,
taiko_chain_spec,
if let Some(parent_block_number) = parent_block.header.number {
parent_block_number
} else {
Expand Down Expand Up @@ -227,17 +225,13 @@ pub async fn preflight<BDP: BlockDataProvider>(

/// Prepare the input for a Taiko chain
async fn prepare_taiko_chain_input(
chain_spec: &ChainSpec,
l1_chain_spec: &ChainSpec,
taiko_chain_spec: &ChainSpec,
block_number: u64,
block: &Block,
l1_rpc_url: Option<String>,
beacon_rpc_url: Option<String>,
prover_data: TaikoProverData,
) -> HostResult<TaikoGuestInput> {
let l1_rpc_url = l1_rpc_url.ok_or_else(|| {
HostError::Preflight("L1 RPC URL is required for Taiko chains".to_owned())
})?;
let provider_l1 = RpcBlockDataProvider::new(&l1_rpc_url, block_number)?;
let provider_l1 = RpcBlockDataProvider::new(&l1_chain_spec.rpc, block_number)?;

// Decode the anchor tx to find out which L1 blocks we need to fetch
let anchor_tx = match &block.transactions {
Expand Down Expand Up @@ -275,7 +269,7 @@ async fn prepare_taiko_chain_input(
// Get the block proposal data
let (proposal_tx, proposal_event) = get_block_proposed_event(
provider_l1.provider(),
chain_spec.clone(),
taiko_chain_spec.clone(),
l1_inclusion_block_hash,
block_number,
)
Expand All @@ -292,10 +286,10 @@ async fn prepare_taiko_chain_input(
// Get the blob data for this block
let slot_id = block_time_to_block_slot(
l1_inclusion_block.header.timestamp,
chain_spec.genesis_time,
chain_spec.seconds_per_slot,
l1_chain_spec.genesis_time,
l1_chain_spec.seconds_per_slot,
)?;
let beacon_rpc_url = beacon_rpc_url.ok_or_else(|| {
let beacon_rpc_url: String = l1_chain_spec.beacon_rpc.clone().ok_or_else(|| {
HostError::Preflight("Beacon RPC URL is required for Taiko chains".to_owned())
})?;
let blob = get_blob_data(&beacon_rpc_url, slot_id, blob_hash).await?;
Expand All @@ -309,7 +303,7 @@ async fn prepare_taiko_chain_input(

// Create the transactions from the proposed tx list
let transactions = generate_transactions(
&chain_spec,
&taiko_chain_spec,
proposal_event.meta.blobUsed,
&tx_data,
Some(anchor_tx.clone()),
Expand Down Expand Up @@ -338,7 +332,11 @@ fn block_time_to_block_slot(
genesis_time: u64,
block_per_slot: u64,
) -> HostResult<u64> {
if block_time < genesis_time {
if genesis_time == 0u64 {
Err(HostError::Anyhow(anyhow!(
"genesis time is 0, please check chain spec"
)))
} else if block_time < genesis_time {
Err(HostError::Anyhow(anyhow!(
"provided block_time precedes genesis time",
)))
Expand Down
30 changes: 12 additions & 18 deletions host/src/raiko.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ pub trait BlockDataProvider {

pub struct Raiko {
l1_chain_spec: ChainSpec,
raiko_chain_spec: ChainSpec,
taiko_chain_spec: ChainSpec,
request: ProofRequest,
}

impl Raiko {
pub fn new(
l1_chain_spec: ChainSpec,
raiko_chain_spec: ChainSpec,
taiko_chain_spec: ChainSpec,
request: ProofRequest,
) -> Self {
Self {
l1_chain_spec,
raiko_chain_spec,
taiko_chain_spec,
request,
}
}
Expand All @@ -59,18 +59,12 @@ impl Raiko {
preflight(
provider,
self.request.block_number,
self.raiko_chain_spec.clone(),
self.l1_chain_spec.clone(),
self.taiko_chain_spec.clone(),
TaikoProverData {
graffiti: self.request.graffiti,
prover: self.request.prover,
},
Some(self.l1_chain_spec.rpc.clone()),
Some(
self.l1_chain_spec
.beacon_rpc
.clone()
.ok_or_else(|| error::HostError::Preflight("Missing beacon rpc".to_owned()))?,
),
)
.await
.map_err(Into::<error::HostError>::into)
Expand Down Expand Up @@ -259,13 +253,13 @@ mod tests {

async fn prove_block(
l1_chain_spec: ChainSpec,
raiko_chain_spec: ChainSpec,
taiko_chain_spec: ChainSpec,
proof_request: ProofRequest,
) {
let provider =
RpcBlockDataProvider::new(&raiko_chain_spec.rpc, proof_request.block_number - 1)
RpcBlockDataProvider::new(&taiko_chain_spec.rpc, proof_request.block_number - 1)
.expect("Could not create RpcBlockDataProvider");
let raiko = Raiko::new(l1_chain_spec, raiko_chain_spec, proof_request.clone());
let raiko = Raiko::new(l1_chain_spec, taiko_chain_spec, proof_request.clone());
let mut input = raiko
.generate_input(provider)
.await
Expand All @@ -288,7 +282,7 @@ mod tests {
// Give the CI an simpler block to test because it doesn't have enough memory.
// Unfortunately that also means that kzg is not getting fully verified by CI.
let block_number = if is_ci() { 105987 } else { 101368 };
let raiko_chain_spec = SupportedChainSpecs::default()
let taiko_chain_spec = SupportedChainSpecs::default()
.get_chain_spec(&network)
.unwrap();
let l1_chain_spec = SupportedChainSpecs::default()
Expand All @@ -304,7 +298,7 @@ mod tests {
proof_type,
prover_args: test_proof_params(),
};
prove_block(l1_chain_spec, raiko_chain_spec, proof_request).await;
prove_block(l1_chain_spec, taiko_chain_spec, proof_request).await;
}

#[tokio::test(flavor = "multi_thread")]
Expand All @@ -315,7 +309,7 @@ mod tests {
let network = Network::Ethereum.to_string();
let l1_network = Network::Ethereum.to_string();
let block_number = 19707175;
let raiko_chain_spec = SupportedChainSpecs::default()
let taiko_chain_spec = SupportedChainSpecs::default()
.get_chain_spec(&network)
.unwrap();
let l1_chain_spec = SupportedChainSpecs::default()
Expand All @@ -330,7 +324,7 @@ mod tests {
proof_type,
prover_args: test_proof_params(),
};
prove_block(l1_chain_spec, raiko_chain_spec, proof_request).await;
prove_block(l1_chain_spec, taiko_chain_spec, proof_request).await;
}
}
}
1 change: 1 addition & 0 deletions lib/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ pub struct ChainSpec {
pub l2_contract: Option<Address>,
pub rpc: String,
pub beacon_rpc: Option<String>,
// TRICKY: the sgx_verifier_addr is in l1, not in itself
pub sgx_verifier_address: Option<Address>,
pub genesis_time: u64,
pub seconds_per_slot: u64,
Expand Down
16 changes: 5 additions & 11 deletions script/prove-block.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,14 @@ rangeEnd="$4"

# Check the chain name and set the corresponding RPC values
if [ "$chain" == "ethereum" ]; then
rpc="https://rpc.ankr.com/eth"
l1_network="ethereum"
elif [ "$chain" == "holesky" ]; then
rpc="https://ethereum-holesky-rpc.publicnode.com"
l1_network="holesky"
elif [ "$chain" == "taiko_a7" ]; then
rpc="https://rpc.hekla.taiko.xyz"
l1Rpc="https://ethereum-holesky-rpc.publicnode.com"
beaconRpc="https://eth-holesky-beacon.public.blastapi.io"
l1_network="holesky"
else
echo "Using customized chain name $1. Please double check the RPCs."
rpc="https://rpc.hekla.taiko.xyz"
l1Rpc="https://ethereum-holesky-rpc.publicnode.com"
beaconRpc="https://eth-holesky-beacon.public.blastapi.io"
l1_network="holesky"
fi

if [ "$proof" == "native" ]; then
Expand Down Expand Up @@ -120,9 +116,7 @@ for block in $(eval echo {$rangeStart..$rangeEnd}); do
--header 'Content-Type: application/json' \
--data-raw "{
\"network\": \"$chain\",
\"rpc\": \"$rpc\",
\"l1_rpc\": \"$l1Rpc\",
\"beacon_rpc\": \"$beaconRpc\",
\"l1_network\": \"$l1_network\",
\"block_number\": $block,
\"prover\": \"$prover\",
\"graffiti\": \"$graffiti\",
Expand Down

0 comments on commit 07d3d82

Please sign in to comment.