Skip to content

Commit 025125d

Browse files
authored
Merge pull request #54 from rustaceanrob/11-13-hints
hintfile: Add height to file metadata
2 parents a64d07a + eaf4d1b commit 025125d

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

hintfile/src/bin/construct.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ fn main() {
3434
let chainman = ChainstateManager::new(options).unwrap();
3535
println!("Chain state initialized");
3636
// Writing the chain tip allows the client to know where to stop
37-
let tip = chainman.block_index_tip().block_hash().hash;
38-
file.write_all(&tip).expect("file cannot be written to");
37+
let tip = chainman.block_index_tip();
38+
let stop_height = (tip.height() as u32).to_le_bytes();
39+
file.write_all(&stop_height)
40+
.expect("file cannot be written to");
41+
file.write_all(&tip.block_hash().hash)
42+
.expect("file cannot be written to");
3943

4044
let genesis = chainman.block_index_genesis();
4145
let mut current = chainman.next_block_index(genesis).unwrap();

hintfile/src/lib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub fn read_compact_size<R: Read>(reader: &mut R) -> Result<u64, io::Error> {
4747
pub struct Hints {
4848
map: BTreeMap<BlockHeight, Vec<u64>>,
4949
assume_valid: BlockHash,
50+
stop_height: BlockHeight,
5051
}
5152

5253
impl Hints {
@@ -56,6 +57,8 @@ impl Hints {
5657
pub fn from_file<R: Read>(reader: &mut R) -> Self {
5758
let mut map = BTreeMap::new();
5859
let mut height = 1;
60+
let mut stop_height = [0; 4];
61+
reader.read_exact(&mut stop_height).expect("empty file");
5962
let mut assume_valid = [0; 32];
6063
reader.read_exact(&mut assume_valid).expect("empty file");
6164
while let Ok(count) = read_compact_size(reader) {
@@ -67,14 +70,23 @@ impl Hints {
6770
map.insert(height, offsets);
6871
height += 1;
6972
}
70-
Self { map, assume_valid }
73+
Self {
74+
map,
75+
assume_valid,
76+
stop_height: BlockHeight::from_le_bytes(stop_height),
77+
}
7178
}
7279

7380
/// Get the last hash encoded in the hintfile.
7481
pub fn stop_hash(&self) -> BlockHash {
7582
self.assume_valid
7683
}
7784

85+
/// Get the stop height of the hint file.
86+
pub fn stop_height(&self) -> BlockHeight {
87+
self.stop_height
88+
}
89+
7890
/// # Panics
7991
///
8092
/// If there are no offset present at that height, aka an overflow, or the entry has already

node/src/bin/ibd.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ fn main() {
4444
let mut hintfile = File::open(hint_path).expect("invalid hintfile path");
4545
let hints = Arc::new(Hints::from_file(&mut hintfile));
4646
elapsed_time(hintfile_start_time);
47+
tracing::info!("Syncing to height {}", hints.stop_height());
4748
let block_file_path = blocks_dir.map(PathBuf::from);
4849
if let Some(block_file_path) = block_file_path.as_ref() {
4950
std::fs::create_dir(block_file_path).expect("could not create block file directory");

0 commit comments

Comments
 (0)