Skip to content

Commit

Permalink
feat: add checksum to pending block hash
Browse files Browse the repository at this point in the history
Previously, the pseudo pending block hash format always ends with zeros,
which does not work with the block hash truncation in `bstream`. This
commit fixes it but clusters need to be re-synced.
  • Loading branch information
xJonathanLEI committed Apr 25, 2024
1 parent eec4495 commit 479deda
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
12 changes: 12 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
Expand Up @@ -15,6 +15,7 @@ reqwest = { version = "0.11.18", default-features = false, features = ["rustls-t
serde = "1.0.180"
serde_json = "1.0.104"
serde_with = "2.3.2"
sha1 = "0.10.6"
starknet = { git = "https://github.com/xJonathanLEI/starknet-rs", rev = "6cadb198644cf5e942187896c2e6bb3ecc9edc6e" }
tokio = { version = "1.29.1", features = ["full"] }
url = "2.4.0"
31 changes: 18 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use clap::Parser;
use log::{debug, error, info};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use sha1::Digest;
use starknet::{
core::{
serde::unsigned_field_element::UfeHex,
Expand Down Expand Up @@ -96,19 +97,19 @@ impl MaybePendingBlock {
pseudo_height,
block,
} => {
// +-------+-----------+--------------+---------------------+------------------+
// | Fixed | "PENDING" | Block Number | Transaction Count | Reserved |
// | 1 byte| 7 bytes | 8 bytes | 4 bytes | 12 bytes |
// +-------+-----------+--------------+---------------------+------------------+
// | 00 | 50 | NN | NN | 00 |
// | | 45 | NN | NN | 00 |
// | | 4E | NN | NN | 00 |
// | | 44 | NN | NN | 00 |
// | | 49 | NN | | 00 |
// | | 4E | NN | | 00 |
// | | 47 | NN | | 00 |
// | | | NN | | 00 |
// +-------+-----------+--------------+---------------------+------------------+
// +--------+-----------+--------------+-------------------+----------+----------+
// | Fixed | "PENDING" | Block Number | Transaction Count | Reserved | Checksum |
// | 1 byte | 7 bytes | 8 bytes | 4 bytes | 8 bytes | 4 bytes |
// +--------+-----------+--------------+-------------------+----------+----------+
// | 00 | 50 | NN | NN | 00 | NN |
// | | 45 | NN | NN | 00 | NN |
// | | 4E | NN | NN | 00 | NN |
// | | 44 | NN | NN | 00 | NN |
// | | 49 | NN | | 00 | |
// | | 4E | NN | | 00 | |
// | | 47 | NN | | 00 | |
// | | | NN | | 00 | |
// +--------+-----------+--------------+-------------------+----------+----------+

let mut buffer = [0u8; 32];
buffer[1] = b'P';
Expand All @@ -123,6 +124,10 @@ impl MaybePendingBlock {
buffer[(8 + 8)..(8 + 8 + 4)]
.copy_from_slice(&u32::to_be_bytes(block.transactions.len() as u32));

let mut hasher = sha1::Sha1::new();
hasher.update(&buffer[..(8 + 8 + 4 + 8)]);
buffer[(8 + 8 + 4 + 8)..].copy_from_slice(&hasher.finalize()[0..4]);

// This cannot fail as the buffer is always in range
FieldElement::from_bytes_be(&buffer).unwrap()
}
Expand Down

0 comments on commit 479deda

Please sign in to comment.