From 479dedaa8254705a3013c077c5619d202b3fa3d6 Mon Sep 17 00:00:00 2001 From: Jonathan LEI Date: Fri, 26 Apr 2024 03:39:48 +0800 Subject: [PATCH] feat: add checksum to pending block hash 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. --- Cargo.lock | 12 ++++++++++++ Cargo.toml | 1 + src/main.rs | 31 ++++++++++++++++++------------- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 609047b..4766e14 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1071,6 +1071,7 @@ dependencies = [ "serde", "serde_json", "serde_with", + "sha1", "starknet", "tokio", "url", @@ -1697,6 +1698,17 @@ dependencies = [ "syn 2.0.28", ] +[[package]] +name = "sha1" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "sha2" version = "0.10.7" diff --git a/Cargo.toml b/Cargo.toml index 812ae43..15c8574 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/main.rs b/src/main.rs index b6d8736..80a0c71 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, @@ -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'; @@ -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() }