From eaf4d1b4b29f888bff68db736ded5f1488fd13e1 Mon Sep 17 00:00:00 2001 From: rustaceanrob Date: Thu, 13 Nov 2025 14:07:37 +0000 Subject: [PATCH] hintfile: Add height to file metadata Depending on the client the height may also be useful along with the hash. --- hintfile/src/bin/construct.rs | 8 ++++++-- hintfile/src/lib.rs | 14 +++++++++++++- node/src/bin/ibd.rs | 1 + 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/hintfile/src/bin/construct.rs b/hintfile/src/bin/construct.rs index 74f21ba..cdfbea6 100644 --- a/hintfile/src/bin/construct.rs +++ b/hintfile/src/bin/construct.rs @@ -34,8 +34,12 @@ fn main() { let chainman = ChainstateManager::new(options).unwrap(); println!("Chain state initialized"); // Writing the chain tip allows the client to know where to stop - let tip = chainman.block_index_tip().block_hash().hash; - file.write_all(&tip).expect("file cannot be written to"); + let tip = chainman.block_index_tip(); + let stop_height = (tip.height() as u32).to_le_bytes(); + file.write_all(&stop_height) + .expect("file cannot be written to"); + file.write_all(&tip.block_hash().hash) + .expect("file cannot be written to"); let genesis = chainman.block_index_genesis(); let mut current = chainman.next_block_index(genesis).unwrap(); diff --git a/hintfile/src/lib.rs b/hintfile/src/lib.rs index 0923672..daa425c 100644 --- a/hintfile/src/lib.rs +++ b/hintfile/src/lib.rs @@ -47,6 +47,7 @@ pub fn read_compact_size(reader: &mut R) -> Result { pub struct Hints { map: BTreeMap>, assume_valid: BlockHash, + stop_height: BlockHeight, } impl Hints { @@ -56,6 +57,8 @@ impl Hints { pub fn from_file(reader: &mut R) -> Self { let mut map = BTreeMap::new(); let mut height = 1; + let mut stop_height = [0; 4]; + reader.read_exact(&mut stop_height).expect("empty file"); let mut assume_valid = [0; 32]; reader.read_exact(&mut assume_valid).expect("empty file"); while let Ok(count) = read_compact_size(reader) { @@ -67,7 +70,11 @@ impl Hints { map.insert(height, offsets); height += 1; } - Self { map, assume_valid } + Self { + map, + assume_valid, + stop_height: BlockHeight::from_le_bytes(stop_height), + } } /// Get the last hash encoded in the hintfile. @@ -75,6 +82,11 @@ impl Hints { self.assume_valid } + /// Get the stop height of the hint file. + pub fn stop_height(&self) -> BlockHeight { + self.stop_height + } + /// # Panics /// /// If there are no offset present at that height, aka an overflow, or the entry has already diff --git a/node/src/bin/ibd.rs b/node/src/bin/ibd.rs index 252e06d..7dfc5ba 100644 --- a/node/src/bin/ibd.rs +++ b/node/src/bin/ibd.rs @@ -44,6 +44,7 @@ fn main() { let mut hintfile = File::open(hint_path).expect("invalid hintfile path"); let hints = Arc::new(Hints::from_file(&mut hintfile)); elapsed_time(hintfile_start_time); + tracing::info!("Syncing to height {}", hints.stop_height()); let block_file_path = blocks_dir.map(PathBuf::from); if let Some(block_file_path) = block_file_path.as_ref() { std::fs::create_dir(block_file_path).expect("could not create block file directory");