cuprate_fast_sync_create_hashes/
create.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#![expect(
    unused_crate_dependencies,
    reason = "binary shares same Cargo.toml as library"
)]

use std::{fmt::Write, fs::write};

use clap::Parser;
use tower::{Service, ServiceExt};

use cuprate_blockchain::{
    config::ConfigBuilder, cuprate_database::RuntimeError, service::BlockchainReadHandle,
};
use cuprate_types::{
    blockchain::{BlockchainReadRequest, BlockchainResponse},
    Chain,
};

use cuprate_fast_sync::{hash_of_hashes, BlockId, HashOfHashes};

const BATCH_SIZE: usize = 512;

async fn read_batch(
    handle: &mut BlockchainReadHandle,
    height_from: usize,
) -> Result<Vec<BlockId>, RuntimeError> {
    let mut block_ids = Vec::<BlockId>::with_capacity(BATCH_SIZE);

    for height in height_from..(height_from + BATCH_SIZE) {
        let request = BlockchainReadRequest::BlockHash(height, Chain::Main);
        let response_channel = handle.ready().await?.call(request);
        let response = response_channel.await?;

        match response {
            BlockchainResponse::BlockHash(block_id) => block_ids.push(block_id),
            _ => unreachable!(),
        }
    }

    Ok(block_ids)
}

fn generate_hex(hashes: &[HashOfHashes]) -> String {
    let mut s = String::new();

    writeln!(&mut s, "[").unwrap();

    for hash in hashes {
        writeln!(&mut s, "\thex!(\"{}\"),", hex::encode(hash)).unwrap();
    }

    writeln!(&mut s, "]").unwrap();

    s
}

#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Args {
    #[arg(short, long)]
    height: usize,
}

#[tokio::main]
async fn main() {
    let args = Args::parse();
    let height_target = args.height;

    let config = ConfigBuilder::new().build();

    let (mut read_handle, _, _) = cuprate_blockchain::service::init(config).unwrap();

    let mut hashes_of_hashes = Vec::new();

    let mut height = 0_usize;

    while height < height_target {
        if let Ok(block_ids) = read_batch(&mut read_handle, height).await {
            let hash = hash_of_hashes(block_ids.as_slice());
            hashes_of_hashes.push(hash);
        } else {
            println!("Failed to read next batch from database");
            break;
        }
        height += BATCH_SIZE;
    }

    drop(read_handle);

    let generated = generate_hex(&hashes_of_hashes);
    write("src/data/hashes_of_hashes", generated).expect("Could not write file");

    println!("Generated hashes up to block height {height}");
}