Skip to content

Commit

Permalink
Merge pull request risc0#78 from smtmfft/ts-to-slot
Browse files Browse the repository at this point in the history
use timestamp based slot num to query blob
  • Loading branch information
Brechtpd committed Apr 2, 2024
2 parents 9ebd97c + 9865174 commit 211093d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
47 changes: 45 additions & 2 deletions host/src/host/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,15 @@ pub fn preflight(
assert!(!blob_hashs.is_empty());
// Currently the protocol enforces the first blob hash to be used
let blob_hash = blob_hashs[0];

let l2_chain_spec = get_network_spec(network);
let slot_id = block_time_to_block_slot(
l1_inclusion_block.header.timestamp.as_limbs()[0],
l2_chain_spec.genesis_time,
l2_chain_spec.seconds_per_slot,
)?;
// Get the blob data for this block
let blobs = get_blob_data(&beacon_rpc_url.clone().unwrap(), l1_inclusion_block_no)?;
let blobs = get_blob_data(&beacon_rpc_url.clone().unwrap(), slot_id)?;
assert!(!blobs.data.is_empty(), "blob data not available anymore");
// Get the blob data for the blob storing the tx list
let tx_blobs: Vec<GetBlobData> = blobs
Expand Down Expand Up @@ -203,6 +210,21 @@ pub fn preflight(
})
}

// block_time_to_block_slot returns the slots of the given timestamp.
fn block_time_to_block_slot(
block_time: u64,
genesis_time: u64,
block_per_slot: u64,
) -> Result<u64> {
if block_time < genesis_time {
Err(anyhow::Error::msg(
"provided block_time precedes genesis time",
))
} else {
Ok((block_time - genesis_time) / block_per_slot)
}
}

fn blob_to_bytes(blob_str: &str) -> Vec<u8> {
match hex::decode(blob_str.to_lowercase().trim_start_matches("0x")) {
Ok(b) => b,
Expand All @@ -225,7 +247,8 @@ fn get_blob_data(beacon_rpc_url: &str, block_id: u64) -> Result<GetBlobsResponse
tokio_handle.block_on(async {
let url = format!(
"{}/eth/v1/beacon/blob_sidecars/{}",
beacon_rpc_url, block_id
beacon_rpc_url.trim_end_matches('/'),
block_id
);
let response = reqwest::get(url.clone()).await?;
if response.status().is_success() {
Expand Down Expand Up @@ -588,6 +611,26 @@ mod test {
// .unwrap();
// }

#[test]
fn test_slot_block_num_mapping() {
let chain_spec = get_taiko_chain_spec("testnet");
let expected_slot = 1000u64;
let second_per_slot = 12u64;
let block_time = chain_spec.genesis_time + expected_slot * second_per_slot;
let block_num =
block_time_to_block_slot(block_time, chain_spec.genesis_time, second_per_slot)
.expect("block time to slot failed");
assert_eq!(block_num, expected_slot);

assert!(block_time_to_block_slot(
chain_spec.genesis_time - 10,
chain_spec.genesis_time,
second_per_slot
)
.is_err());
}

#[ignore]
#[test]
fn json_to_ethers_blob_tx() {
let response = "{
Expand Down
18 changes: 14 additions & 4 deletions lib/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ pub const ETH_MAINNET_CHAIN_SPEC: Lazy<ChainSpec> = Lazy::new(|| {
l1_contract: None,
l2_contract: None,
sgx_verifier_address: None,
genesis_time: 0u64,
seconds_per_slot: 1u64,
}
});

Expand All @@ -81,11 +83,13 @@ pub const TAIKO_A6_CHAIN_SPEC: Lazy<ChainSpec> = Lazy::new(|| ChainSpec {
sgx_verifier_address: Some(
Address::from_str("0x558E38a3286916934Cb63ced04558A52F7Ce67a9").unwrap(),
),
genesis_time: 0u64,
seconds_per_slot: 1u64,
});

/// The Taiko A7 specification.
pub const TAIKO_A7_CHAIN_SPEC: Lazy<ChainSpec> = Lazy::new(|| ChainSpec {
chain_id: 167001,
chain_id: 167009,
hard_forks: BTreeMap::from([
(SpecId::SHANGHAI, ForkCondition::Block(0)),
(SpecId::CANCUN, ForkCondition::TBD),
Expand All @@ -96,11 +100,13 @@ pub const TAIKO_A7_CHAIN_SPEC: Lazy<ChainSpec> = Lazy::new(|| ChainSpec {
base_fee_max_decrease_denominator: uint!(8_U256),
elasticity_multiplier: uint!(2_U256),
},
l1_contract: Some(Address::from_str("0xC069c3d2a9f2479F559AD34485698ad5199C555f").unwrap()),
l2_contract: Some(Address::from_str("0x1670010000000000000000000000000000010001").unwrap()),
l1_contract: Some(Address::from_str("0x79C9109b764609df928d16fC4a91e9081F7e87DB").unwrap()),
l2_contract: Some(Address::from_str("0x1670090000000000000000000000000000010001").unwrap()),
sgx_verifier_address: Some(
Address::from_str("0x558E38a3286916934Cb63ced04558A52F7Ce67a9").unwrap(),
Address::from_str("0x532EFBf6D62720D0B2a2Bb9d11066E8588cAE6D9").unwrap(),
),
genesis_time: 1695902400u64,
seconds_per_slot: 12u64,
});

pub fn get_network_spec(network: Network) -> ChainSpec {
Expand Down Expand Up @@ -160,6 +166,8 @@ pub struct ChainSpec {
pub l1_contract: Option<Address>,
pub l2_contract: Option<Address>,
pub sgx_verifier_address: Option<Address>,
pub genesis_time: u64,
pub seconds_per_slot: u64,
}

impl ChainSpec {
Expand All @@ -176,6 +184,8 @@ impl ChainSpec {
l1_contract: None,
l2_contract: None,
sgx_verifier_address: None,
genesis_time: 0u64,
seconds_per_slot: 1u64,
}
}
/// Returns the network chain ID.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/protocol_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub fn assemble_protocol_instance(
transition: Transition {
parentHash: header.parent_hash,
blockHash: header.hash(),
stateRoot: input.taiko.l1_header.state_root,
stateRoot: header.state_root,
graffiti: input.taiko.prover_data.graffiti,
},
block_metadata: BlockMetadata {
Expand Down
7 changes: 4 additions & 3 deletions prove_block.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ rangeEnd="$4"
if [ "$chain" == "taiko_a6" ]; then
l2Rpc="https://rpc.katla.taiko.xyz"
l1Rpc="https://l1rpc.katla.taiko.xyz"
beaconRpc="https://l1beacon.hekla.taiko.xyz"
elif [ "$chain" == "taiko_a7" ]; then
l2Rpc="https://rpc.internal.taiko.xyz"
l1Rpc="https://l1rpc.internal.taiko.xyz"
l2Rpc="https://rpc.hekla.taiko.xyz/"
l1Rpc="https://l1rpc.hekla.taiko.xyz/"
beaconRpc="https://l1beacon.hekla.taiko.xyz"
else
echo "Invalid chain name. Please use 'taiko_a6' or 'taiko_a7'."
exit 1
Expand Down Expand Up @@ -59,7 +61,6 @@ if [ "$rangeEnd" == "" ]; then
rangeEnd=$rangeStart
fi

beaconRpc="https://l1beacon.internal.taiko.xyz"
prover="0x70997970C51812dc3A010C7d01b50e0d17dc79C8"
graffiti="8008500000000000000000000000000000000000000000000000000000000000"

Expand Down

0 comments on commit 211093d

Please sign in to comment.