Skip to content

Commit

Permalink
feat: add well-known 0.13.2-style block hashes for Sepolia testnet
Browse files Browse the repository at this point in the history
We will use these 0.13.2 block hashes for all pre-0.13.2 blocks
to verify data we receive from peers via P2P.
  • Loading branch information
kkovaacs committed Oct 4, 2024
1 parent fb2b90b commit 51a6db2
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 8 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = [
"crates/block-hashes",
"crates/common",
"crates/compiler",
"crates/crypto",
Expand Down
11 changes: 11 additions & 0 deletions crates/block-hashes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "pathfinder-block-hashes"
version = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
rust-version = { workspace = true }

[dependencies]
pathfinder-common = { path = "../common" }
pathfinder-crypto = { path = "../crypto" }
Binary file not shown.
21 changes: 21 additions & 0 deletions crates/block-hashes/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use pathfinder_common::{BlockHash, BlockNumber, Chain};

mod sepolia;

#[derive(Clone)]
pub struct BlockHashDb {
chain: Chain,
}

impl BlockHashDb {
pub fn new(chain: Chain) -> Self {
Self { chain }
}

pub fn block_hash(&self, block_number: BlockNumber) -> Option<BlockHash> {
match self.chain {
Chain::SepoliaTestnet => sepolia::block_hash(block_number),
_ => None,
}
}
}
16 changes: 16 additions & 0 deletions crates/block-hashes/src/sepolia.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use pathfinder_common::{BlockHash, BlockNumber};
use pathfinder_crypto::Felt;

const FIRST_0_13_2_BLOCK: usize = 86311;
static BLOCK_HASHES: &[u8; 32 * FIRST_0_13_2_BLOCK] =
include_bytes!("../fixtures/sepolia_block_hashes.bin");

pub(super) fn block_hash(block_number: BlockNumber) -> Option<BlockHash> {
if block_number.get() >= FIRST_0_13_2_BLOCK as u64 {
None
} else {
let offset = (block_number.get() as usize) * 32;
let felt = Felt::from_be_slice(&BLOCK_HASHES[offset..offset + 32]).unwrap();
Some(BlockHash(felt))
}
}
24 changes: 16 additions & 8 deletions crates/pathfinder/examples/compute_pre0132_hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use pathfinder_lib::state::block_hash::{
BlockHeaderData,
};

const VERSION_CUTOFF: StarknetVersion = StarknetVersion::new(0, 13, 2, 0);
const VERSION_CUTOFF: StarknetVersion = StarknetVersion::V_0_13_2;

/// Computes block hashes for all blocks under the 0.13.2 cutoff in 0.13.2 style
/// and stores them in a CSV file "block_hashes.csv" with the format:
Expand Down Expand Up @@ -48,7 +48,8 @@ fn main() -> anyhow::Result<()> {
};

// Open a file where we'll save the computed hashes
let mut file = std::fs::File::create("block_hashes.csv")?;
let mut csv_file = std::fs::File::create("block_hashes.csv")?;
let mut binary_file = std::fs::File::create("block_hashes.bin")?;

// Iterate through all pre-0.13.2 blocks
for block_number in 0..latest_block_number.get() {
Expand All @@ -64,6 +65,12 @@ fn main() -> anyhow::Result<()> {
.context("Fetching block header")?
.context("Block header missing")?;

// As soon as we reach blocks in 0.13.2 we're done
if header.starknet_version == VERSION_CUTOFF {
println!("\rBlock {}. Done!", block_number);
break;
}

// Load block tx's (to compute receipt commitment)
let txn_data_for_block = tx
.transaction_data_for_block(block_id)?
Expand Down Expand Up @@ -122,15 +129,16 @@ fn main() -> anyhow::Result<()> {
let new_block_hash = compute_final_hash(&header_data).context("Computing block hash")?;

// Write to the CSV file
writeln!(file, "{},{}", block_number, new_block_hash)?;
writeln!(csv_file, "{},{}", block_number, new_block_hash)?;

// As soon as we reach blocks in 0.13.2 we're done
if header.starknet_version == VERSION_CUTOFF {
println!("\rBlock {}. Done!", block_number);
break;
}
// Write to the binary file
binary_file
.write_all(new_block_hash.0.as_be_bytes())
.context("Writing block hash to binary file")?;
}

println!("\nResults are in `block_hashes.csv` and `block_hashes.bin`");

Ok(())
}

Expand Down

0 comments on commit 51a6db2

Please sign in to comment.