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 5172ce9
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 54 deletions.
44 changes: 29 additions & 15 deletions host/config/chain_spec_list_default.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
"rpc": "https://rpc.ankr.com/eth",
"beacon_rpc": "https://ethereum-beacon-api.publicnode.com",
"verifier_address": {
"SGX":"0x532efbf6d62720d0b2a2bb9d11066e8588cae6d9",
"SP1":null,
"RISC0":"0x0000000000000000000000000000000000000000"
"FRONTIER": {
"SGX": "0x532efbf6d62720d0b2a2bb9d11066e8588cae6d9",
"SP1": null,
"RISC0": "0x0000000000000000000000000000000000000000"
}
},
"genesis_time": 1606824023,
"seconds_per_slot": 12,
Expand Down Expand Up @@ -62,9 +64,11 @@
"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"
"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": 99999
},
"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": {
"HEKLA": {
"SGX": "0x532efbf6d62720d0b2a2bb9d11066e8588cae6d9",
"SP1": null,
"RISC0": "0x0000000000000000000000000000000000000000"
},
"ONTAKE": {
"SGX": "0x532efbf6d62720d0b2a2bb9d11066e8588cae6d9",
"SP1": null,
"RISC0": "0x0000000000000000000000000000000000000000"
}
},
"genesis_time": 0,
"seconds_per_slot": 1,
Expand All @@ -120,9 +134,9 @@
"rpc": "https://rpc.mainnet.taiko.xyz",
"beacon_rpc": null,
"verifier_address": {
"SGX":"0xb0f3186FC1963f774f52ff455DC86aEdD0b31F81",
"SP1":null,
"RISC0":"0x0000000000000000000000000000000000000000"
"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
50 changes: 42 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 @@ -265,6 +285,17 @@ mod tests {
);
}

#[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::default());
}

#[ignore]
#[test]
fn serde_chain_spec() {
Expand All @@ -288,11 +319,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
54 changes: 25 additions & 29 deletions lib/src/protocol_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,27 @@ impl BlockMetaDataFork {
fn from(input: &GuestInput, header: &Header, tx_list_hash: B256) -> Self {
match &input.taiko.block_proposed {
BlockProposedFork::Nothing => unimplemented!("no block proposed"),
BlockProposedFork::Hekla(block_proposed) => {
BlockMetaDataFork::Hekla(BlockMetadata {
l1Hash: input.taiko.l1_header.hash_slow(),
difficulty: block_proposed.meta.difficulty,
blobHash: tx_list_hash,
extraData: bytes_to_bytes32(&header.extra_data).into(),
depositsHash: keccak(Vec::<EthDeposit>::new().abi_encode()).into(),
coinbase: header.beneficiary,
id: header.number,
gasLimit: (header.gas_limit
- if input.chain_spec.is_taiko() {
ANCHOR_GAS_LIMIT
} else {
0
}) as u32,
timestamp: header.timestamp,
l1Height: input.taiko.l1_header.number,
minTier: block_proposed.meta.minTier,
blobUsed: block_proposed.meta.blobUsed,
parentMetaHash: block_proposed.meta.parentMetaHash,
sender: block_proposed.meta.sender,
})
}
BlockProposedFork::Hekla(block_proposed) => BlockMetaDataFork::Hekla(BlockMetadata {
l1Hash: input.taiko.l1_header.hash_slow(),
difficulty: block_proposed.meta.difficulty,
blobHash: tx_list_hash,
extraData: bytes_to_bytes32(&header.extra_data).into(),
depositsHash: keccak(Vec::<EthDeposit>::new().abi_encode()).into(),
coinbase: header.beneficiary,
id: header.number,
gasLimit: (header.gas_limit
- if input.chain_spec.is_taiko() {
ANCHOR_GAS_LIMIT
} else {
0
}) as u32,
timestamp: header.timestamp,
l1Height: input.taiko.l1_header.number,
minTier: block_proposed.meta.minTier,
blobUsed: block_proposed.meta.blobUsed,
parentMetaHash: block_proposed.meta.parentMetaHash,
sender: block_proposed.meta.sender,
}),
BlockProposedFork::Ontake(block_proposed_v2) => {
BlockMetaDataFork::Ontake(BlockMetadataV2 {
anchorBlockHash: input.taiko.l1_header.hash_slow(),
Expand Down Expand Up @@ -177,12 +175,9 @@ impl ProtocolInstance {
);
}

let verifier_address = (*input
let verifier_address = input
.chain_spec
.verifier_address
.get(&proof_type)
.unwrap_or(&None))
.unwrap_or_default();
.get_fork_verifier_address(input.taiko.block_proposed.block_number(), proof_type)?;

let pi = ProtocolInstance {
transition: Transition {
Expand All @@ -202,7 +197,8 @@ impl ProtocolInstance {
// Sanity check
if input.chain_spec.is_taiko() {
ensure!(
pi.block_metadata.match_block_proposal(&input.taiko.block_proposed),
pi.block_metadata
.match_block_proposal(&input.taiko.block_proposed),
format!(
"block hash mismatch, expected: {:?}, got: {:?}",
input.taiko.block_proposed, pi.block_metadata
Expand Down

0 comments on commit 5172ce9

Please sign in to comment.