Skip to content

Commit

Permalink
use forked verifier set together with taiko hard forks
Browse files Browse the repository at this point in the history
Signed-off-by: smtmfft <[email protected]>
  • Loading branch information
smtmfft committed Jul 26, 2024
1 parent 049f115 commit 2b3b82a
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 71 deletions.
16 changes: 10 additions & 6 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use raiko_lib::{
input::{GuestInput, GuestOutput, TaikoProverData},
prover::{IdStore, IdWrite},
};
use reth_primitives::revm_primitives::SpecId;
use reth_primitives::Header;
use serde_json::Value;
use std::{collections::HashMap, hint::black_box};
Expand Down Expand Up @@ -52,8 +53,11 @@ impl Raiko {
provider: BDP,
) -> RaikoResult<GuestInput> {
//TODO: read fork from config
if self.request.block_number <= 999999999 {
preflight(
match self
.taiko_chain_spec
.active_fork(self.request.block_number, 0)?
{
SpecId::HEKLA => preflight(
provider,
self.request.block_number,
self.l1_chain_spec.to_owned(),
Expand All @@ -65,9 +69,8 @@ impl Raiko {
self.request.blob_proof_type.clone(),
)
.await
.map_err(Into::<RaikoError>::into)
} else {
crate::preflight::ontake::preflight(
.map_err(Into::<RaikoError>::into),
SpecId::ONTAKE => crate::preflight::ontake::preflight(
provider,
self.request.block_number,
self.request.block_number,
Expand All @@ -80,7 +83,8 @@ impl Raiko {
self.request.blob_proof_type.clone(),
)
.await
.map_err(Into::<RaikoError>::into)
.map_err(Into::<RaikoError>::into),
_ => Err(RaikoError::Preflight("Unsupported fork".to_owned())),
}
}

Expand Down
1 change: 0 additions & 1 deletion core/src/preflight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ pub mod ontake;

pub use hekla::*;


// block_time_to_block_slot returns the slots of the given timestamp.
fn block_time_to_block_slot(
block_time: u64,
Expand Down
59 changes: 39 additions & 20 deletions host/config/chain_spec_list_default.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
"l2_contract": null,
"rpc": "https://rpc.ankr.com/eth",
"beacon_rpc": "https://ethereum-beacon-api.publicnode.com",
"verifier_address": {
"SGX":"0x532efbf6d62720d0b2a2bb9d11066e8588cae6d9",
"SP1":null,
"RISC0":"0x0000000000000000000000000000000000000000"
"verifier_address_forks": {
"FRONTIER": {
"SGX": "0x532efbf6d62720d0b2a2bb9d11066e8588cae6d9",
"SP1": null,
"RISC0": "0x0000000000000000000000000000000000000000"
}
},
"genesis_time": 1606824023,
"seconds_per_slot": 12,
Expand Down Expand Up @@ -61,10 +63,12 @@
"l2_contract": null,
"rpc": "https://ethereum-holesky-rpc.publicnode.com",
"beacon_rpc": "https://fabled-weathered-cherry.ethereum-holesky.quiknode.pro/8f1c66935fa5f9afbda0db43318fe3c9e7b061e1/",
"verifier_address": {
"SGX":"0x532efbf6d62720d0b2a2bb9d11066e8588cae6d9",
"SP1":null,
"RISC0":"0x0000000000000000000000000000000000000000"
"verifier_address_forks": {
"FRONTIER": {
"SGX": "0x532efbf6d62720d0b2a2bb9d11066e8588cae6d9",
"SP1": null,
"RISC0": "0x0000000000000000000000000000000000000000"
}
},
"genesis_time": 1695902400,
"seconds_per_slot": 12,
Expand All @@ -73,11 +77,14 @@
{
"name": "taiko_a7",
"chain_id": 167009,
"max_spec_id": "SHANGHAI",
"max_spec_id": "ONTAKE",
"hard_forks": {
"SHANGHAI": {
"HEKLA": {
"Block": 0
},
"ONTAKE": {
"Block": 999999
},
"CANCUN": "TBD"
},
"eip_1559_constants": {
Expand All @@ -90,10 +97,17 @@
"l2_contract": "0x1670090000000000000000000000000000010001",
"rpc": "https://rpc.hekla.taiko.xyz",
"beacon_rpc": null,
"verifier_address":{
"SGX":"0x532efbf6d62720d0b2a2bb9d11066e8588cae6d9",
"SP1":null,
"RISC0":"0x0000000000000000000000000000000000000000"
"verifier_address_forks": {
"HEKLA": {
"SGX": "0x532efbf6d62720d0b2a2bb9d11066e8588cae6d9",
"SP1": null,
"RISC0": "0x0000000000000000000000000000000000000000"
},
"ONTAKE": {
"SGX": "0xf6d620d0b2a2bb9d11066e8532efb72588cae6d9",
"SP1": null,
"RISC0": "0x0000000000000000000000000000000000000000"
}
},
"genesis_time": 0,
"seconds_per_slot": 1,
Expand All @@ -102,11 +116,14 @@
{
"name": "taiko_mainnet",
"chain_id": 167000,
"max_spec_id": "SHANGHAI",
"max_spec_id": "ONTAKE",
"hard_forks": {
"SHANGHAI": {
"HEKLA": {
"Block": 0
},
"ONTAKE": {
"Block": 999999
},
"CANCUN": "TBD"
},
"eip_1559_constants": {
Expand All @@ -119,10 +136,12 @@
"l2_contract": "0x1670000000000000000000000000000000010001",
"rpc": "https://rpc.mainnet.taiko.xyz",
"beacon_rpc": null,
"verifier_address": {
"SGX":"0xb0f3186FC1963f774f52ff455DC86aEdD0b31F81",
"SP1":null,
"RISC0":"0x0000000000000000000000000000000000000000"
"verifier_address_forks": {
"HEKLA": {
"SGX": "0xb0f3186FC1963f774f52ff455DC86aEdD0b31F81",
"SP1": null,
"RISC0": "0x0000000000000000000000000000000000000000"
}
},
"genesis_time": 0,
"seconds_per_slot": 1,
Expand Down
35 changes: 33 additions & 2 deletions lib/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ use crate::{
CycleTracker,
};
use anyhow::{bail, ensure, Result};
use reth_chainspec::{ChainSpecBuilder, HOLESKY, MAINNET, TAIKO_A7, TAIKO_DEV, TAIKO_MAINNET};
use reth_chainspec::{
ChainSpecBuilder, Hardfork, HOLESKY, MAINNET, TAIKO_A7, TAIKO_DEV, TAIKO_MAINNET,
};
use reth_evm::execute::{BlockExecutionOutput, BlockValidationError, Executor, ProviderError};
use reth_evm_ethereum::execute::{
validate_block_post_execution, Consensus, EthBeaconConsensus, EthExecutorProvider,
};
use reth_evm_ethereum::taiko::TaikoData;
use reth_primitives::revm_primitives::db::{Database, DatabaseCommit};
use reth_primitives::revm_primitives::{
Account, AccountInfo, AccountStatus, Bytecode, Bytes, HashMap,
Account, AccountInfo, AccountStatus, Bytecode, Bytes, HashMap, SpecId,
};
use reth_primitives::{Address, BlockWithSenders, Header, B256, KECCAK_EMPTY, U256};
use tracing::debug;
Expand Down Expand Up @@ -96,6 +98,35 @@ impl<DB: Database<Error = ProviderError> + DatabaseCommit + OptimisticDatabase>
_ => unimplemented!(),
};

if reth_chain_spec.is_taiko() {
let block_num = self.input.taiko.block_proposed.block_number();
let block_timestamp = 0u64; // self.input.taiko.block_proposed.block_timestamp();
let taiko_fork = self
.input
.chain_spec
.spec_id(block_num, block_timestamp)
.unwrap();
match taiko_fork {
SpecId::HEKLA => {
assert!(
reth_chain_spec
.fork(Hardfork::Ontake)
.active_at_block(block_num),
"evm fork is not active, please update the chain spec"
);
}
SpecId::ONTAKE => {
assert!(
reth_chain_spec
.fork(Hardfork::Ontake)
.active_at_block(block_num),
"evm fork is not active, please update the chain spec"
);
}
_ => unimplemented!(),
}
}

// Generate the transactions from the tx list
let mut block = self.input.block.clone();
block.body = generate_transactions(
Expand Down
112 changes: 104 additions & 8 deletions lib/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub struct ChainSpec {
pub l2_contract: Option<Address>,
pub rpc: String,
pub beacon_rpc: Option<String>,
pub verifier_address: BTreeMap<VerifierType, Option<Address>>,
pub verifier_address_forks: BTreeMap<SpecId, BTreeMap<VerifierType, Option<Address>>>,
pub genesis_time: u64,
pub seconds_per_slot: u64,
pub is_taiko: bool,
Expand All @@ -173,7 +173,7 @@ impl ChainSpec {
l2_contract: None,
rpc: "".to_string(),
beacon_rpc: None,
verifier_address: BTreeMap::new(),
verifier_address_forks: BTreeMap::new(),
genesis_time: 0u64,
seconds_per_slot: 1u64,
is_taiko,
Expand Down Expand Up @@ -204,7 +204,7 @@ impl ChainSpec {
&self.eip_1559_constants
}

fn spec_id(&self, block_no: BlockNumber, timestamp: u64) -> Option<SpecId> {
pub fn spec_id(&self, block_no: BlockNumber, timestamp: u64) -> Option<SpecId> {
for (spec_id, fork) in self.hard_forks.iter().rev() {
if fork.active(block_no, timestamp) {
return Some(*spec_id);
Expand All @@ -213,6 +213,26 @@ impl ChainSpec {
None
}

pub fn get_fork_verifier_address(
&self,
block_num: u64,
verifier_type: VerifierType,
) -> Result<Address> {
// fall down to the first fork that is active as default
for (spec_id, fork) in self.hard_forks.iter().rev() {
if fork.active(block_num, 0u64) {
if let Some(fork_verifier) = self.verifier_address_forks.get(&spec_id) {
return fork_verifier
.get(&verifier_type)
.unwrap()
.ok_or_else(|| anyhow!("Verifier address not found"));
}
}
}

Err(anyhow!("fork verifier is not active"))
}

pub fn is_taiko(&self) -> bool {
self.is_taiko
}
Expand Down Expand Up @@ -249,6 +269,8 @@ impl std::fmt::Display for Network {

#[cfg(test)]
mod tests {
use reth_primitives::address;

use super::*;

#[test]
Expand All @@ -265,6 +287,77 @@ mod tests {
);
}

#[test]
fn raiko_active_fork() {
let eth_mainnet_spec = SupportedChainSpecs::default()
.get_chain_spec(&Network::Ethereum.to_string())
.unwrap();
assert_eq!(
eth_mainnet_spec.active_fork(0, 0).unwrap(),
SpecId::FRONTIER
);
assert_eq!(
eth_mainnet_spec.active_fork(15_537_394, 0).unwrap(),
SpecId::MERGE
);
assert_eq!(
eth_mainnet_spec.active_fork(17_034_869, 0).unwrap(),
SpecId::MERGE
);
assert_eq!(
eth_mainnet_spec.active_fork(17_034_870, 0).unwrap(),
SpecId::SHANGHAI
);

let taiko_mainnet_spec = SupportedChainSpecs::default()
.get_chain_spec(&Network::TaikoMainnet.to_string())
.unwrap();
assert_eq!(
taiko_mainnet_spec.active_fork(0, 0).unwrap(),
SpecId::HEKLA
);
assert_eq!(
taiko_mainnet_spec.active_fork(999998, 0).unwrap(),
SpecId::HEKLA
);
assert_eq!(
taiko_mainnet_spec.active_fork(999999, 0).unwrap(),
SpecId::ONTAKE
);
}

#[test]
fn forked_verifier_address() {
let eth_mainnet_spec = SupportedChainSpecs::default()
.get_chain_spec(&Network::Ethereum.to_string())
.unwrap();
let verifier_address = eth_mainnet_spec
.get_fork_verifier_address(15_537_394, VerifierType::SGX)
.unwrap();
assert_eq!(
verifier_address,
address!("532efbf6d62720d0b2a2bb9d11066e8588cae6d9")
);

let hekla_mainnet_spec = SupportedChainSpecs::default()
.get_chain_spec(&Network::TaikoA7.to_string())
.unwrap();
let verifier_address = hekla_mainnet_spec
.get_fork_verifier_address(12345, VerifierType::SGX)
.unwrap();
assert_eq!(
verifier_address,
address!("532efbf6d62720d0b2a2bb9d11066e8588cae6d9")
);
let verifier_address = hekla_mainnet_spec
.get_fork_verifier_address(15_537_394, VerifierType::SGX)
.unwrap();
assert_eq!(
verifier_address,
address!("f6d620d0b2a2bb9d11066e8532efb72588cae6d9")
);
}

#[ignore]
#[test]
fn serde_chain_spec() {
Expand All @@ -288,11 +381,14 @@ mod tests {
l2_contract: None,
rpc: "".to_string(),
beacon_rpc: None,
verifier_address: BTreeMap::from([
(VerifierType::SGX, Some(Address::default())),
(VerifierType::SP1, None),
(VerifierType::RISC0, Some(Address::default())),
]),
verifier_address_forks: BTreeMap::from([(
SpecId::FRONTIER,
BTreeMap::from([
(VerifierType::SGX, Some(Address::default())),
(VerifierType::SP1, None),
(VerifierType::RISC0, Some(Address::default())),
]),
)]),
genesis_time: 0u64,
seconds_per_slot: 1u64,
is_taiko: false,
Expand Down
16 changes: 16 additions & 0 deletions lib/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ impl BlockProposedFork {
_ => false,
}
}

pub fn block_number(&self) -> u64 {
match self {
BlockProposedFork::Hekla(block) => block.meta.id,
BlockProposedFork::Ontake(block) => block.meta.id,
_ => 0,
}
}

pub fn block_timestamp(&self) -> u64 {
match self {
BlockProposedFork::Hekla(block) => block.meta.timestamp,
BlockProposedFork::Ontake(block) => block.meta.timestamp,
_ => 0,
}
}
}

#[serde_as]
Expand Down
Loading

0 comments on commit 2b3b82a

Please sign in to comment.