From c9868d472c3c97357a47272c1d2fe8cb56f1babc Mon Sep 17 00:00:00 2001 From: Matthew Little Date: Thu, 7 Jan 2021 14:36:10 +0100 Subject: [PATCH] feat: add test data mode for name_zonefiles.txt --- stx-genesis/build.rs | 54 +++++++++++++++++----- stx-genesis/name_zonefiles-test.txt | 24 ++++++++++ stx-genesis/name_zonefiles-test.txt.sha256 | 1 + stx-genesis/src/lib.rs | 28 ++++++----- 4 files changed, 85 insertions(+), 22 deletions(-) create mode 100644 stx-genesis/name_zonefiles-test.txt create mode 100644 stx-genesis/name_zonefiles-test.txt.sha256 diff --git a/stx-genesis/build.rs b/stx-genesis/build.rs index 2b10b736a..bc8083856 100644 --- a/stx-genesis/build.rs +++ b/stx-genesis/build.rs @@ -19,15 +19,22 @@ pub static CHAINSTATE_TEST_SHA256_FILE: &str = "chainstate-test.txt.sha256"; pub static NAME_ZONEFILES_FILE: &str = "name_zonefiles.txt"; pub static NAME_ZONEFILES_SHA256_FILE: &str = "name_zonefiles.txt.sha256"; +pub static NAME_ZONEFILES_TEST_FILE: &str = "name_zonefiles-test.txt"; +pub static NAME_ZONEFILES_TEST_SHA256_FILE: &str = "name_zonefiles-test.txt.sha256"; + fn main() { - verify_name_zonefiles_integrity().expect("failed to verify and output name_zonefiles.txt.sha256 hash"); + verify_name_zonefiles_integrity(true) + .expect("failed to verify and output name_zonefiles.txt.sha256 hash"); + verify_name_zonefiles_integrity(false) + .expect("failed to verify and output name_zonefiles-test.txt.sha256 hash"); verify_genesis_integrity(true) .expect("failed to verify and output chainstate-test.txt.sha256 hash"); verify_genesis_integrity(false) .expect("failed to verify and output chainstate.txt.sha256 hash"); write_chainstate_archives(true).expect("failed to write chainstate test data archives"); write_chainstate_archives(false).expect("failed to write chainstate prod data archives"); - write_name_zonefiles_archive().expect("failed to write name zonefiles data archive"); + write_name_zonefiles_archive(true).expect("failed to write name zonefiles test data archive"); + write_name_zonefiles_archive(false).expect("failed to write name zonefiles prod data archive"); println!("cargo:rerun-if-changed=build.rs"); println!("cargo:rerun-if-changed=chainstate-test.txt.sha256"); println!("cargo:rerun-if-changed=chainstate-test.txt"); @@ -90,12 +97,21 @@ fn write_chainstate_archive( Ok(()) } -fn write_name_zonefiles_archive() -> std::io::Result<()> { +fn write_name_zonefiles_archive(test_data: bool) -> std::io::Result<()> { let out_dir = env::var_os("OUT_DIR").unwrap(); - let zonefiles_file = File::open(NAME_ZONEFILES_FILE).unwrap(); + let zonefiles_file = File::open(if test_data { + NAME_ZONEFILES_TEST_FILE + } else { + NAME_ZONEFILES_FILE + }) + .unwrap(); let mut reader = BufReader::new(zonefiles_file); - let out_file_name = "name_zonefiles".to_owned(); - let out_file_path = Path::new(&out_dir).join(out_file_name + ".gz"); + let out_file_name = if test_data { + "name_zonefiles-test" + } else { + "name_zonefiles" + }; + let out_file_path = Path::new(&out_dir).join(out_file_name.to_owned() + ".gz"); let out_file = File::create(out_file_path)?; let mut encoder = deflate::Encoder::new(out_file); std::io::copy(&mut reader, &mut encoder).unwrap(); @@ -152,9 +168,21 @@ fn verify_genesis_integrity(test_data: bool) -> std::io::Result<()> { Ok(()) } -fn verify_name_zonefiles_integrity() -> std::io::Result<()> { - let zonefiles_data_sha = sha256_digest(File::open(NAME_ZONEFILES_FILE).unwrap()); - let expected_zonefiles_sha = fs::read_to_string(NAME_ZONEFILES_SHA256_FILE).unwrap(); +fn verify_name_zonefiles_integrity(test_data: bool) -> std::io::Result<()> { + let zonefiles_data_sha = sha256_digest( + File::open(if test_data { + NAME_ZONEFILES_TEST_FILE + } else { + NAME_ZONEFILES_FILE + }) + .unwrap(), + ); + let expected_zonefiles_sha = fs::read_to_string(if test_data { + NAME_ZONEFILES_TEST_SHA256_FILE + } else { + NAME_ZONEFILES_SHA256_FILE + }) + .unwrap(); if !zonefiles_data_sha.eq_ignore_ascii_case(&expected_zonefiles_sha) { panic!( "FATAL ERROR: name_zonefiles.txt hash mismatch, expected {}, got {}", @@ -162,9 +190,13 @@ fn verify_name_zonefiles_integrity() -> std::io::Result<()> { ); } let out_dir = env::var_os("OUT_DIR").unwrap(); - let zonefile_hash_file_path = Path::new(&out_dir).join(NAME_ZONEFILES_SHA256_FILE); + let zonefile_hash_file_path = Path::new(&out_dir).join(if test_data { + NAME_ZONEFILES_TEST_SHA256_FILE + } else { + NAME_ZONEFILES_SHA256_FILE + }); let mut zonefile_hash_file = File::create(zonefile_hash_file_path)?; zonefile_hash_file.write_all(zonefiles_data_sha.as_bytes())?; zonefile_hash_file.flush()?; Ok(()) -} \ No newline at end of file +} diff --git a/stx-genesis/name_zonefiles-test.txt b/stx-genesis/name_zonefiles-test.txt new file mode 100644 index 000000000..36409e3c8 --- /dev/null +++ b/stx-genesis/name_zonefiles-test.txt @@ -0,0 +1,24 @@ +c5f5b4197c94d4561f6332160f624d728b0da8bf +$ORIGIN 0.id\n$TTL 3600\n_http._tcp IN URI 10 1 "https://gaia.blockstack.org/hub/1A4aeTdM35rYtUoiCiV81jKwKyi1Egzb32/profile.json"\n\n +8961c32ad98df87af12b186c3d0be6ca22105936 +$ORIGIN 1.id\n$TTL 3600\n_http._tcp IN URI 10 1 "https://gaia.blockstack.org/hub/19i6q73cKHLFfZ12JmHqB6HaXZNWos2MeE/profile.json"\n\n +8ae7173b9a2f96521ba077bbcbe9b56b1cf2449f +$ORIGIN 10.id\n$TTL 3600\n_http._tcp IN URI 10 1 "https://gaia.blockstack.org/hub/17MJkt1kfmV5SjWDeNmzjdJpawwCXn9kyY/profile.json"\n\n +3d0576cd134fa1ff8d54d8e2ef5da97f5cae36f2 +$ORIGIN 10x.id\n$TTL 3600\n_http._tcp IN URI 10 1 "https://gaia.blockstack.org/hub/1NND3tMZuSfq7TJ6cHuV2Z72ij2JnyaRoX/profile.json"\n\n +01a8c60c852b75f9c6417491ac89275fb9de774c +$ORIGIN 111111111.id\n$TTL 3600\n_http._tcp IN URI 10 1 "https://gaia.blockstack.org/hub/17uym3RccQG97RQAETTHe32YPGAhq6A1qq/profile.json"\n\n +ae34427289a8865028eb2d475dafb085060d5625 +$ORIGIN 123.id\n$TTL 3600\n_http._tcp IN URI 10 1 "https://gaia.blockstack.org/hub/1GmQ9hgopAeSinysvJzpSggSknEjJJtDkg/profile.json"\n\n +c7b0bbeb92c2575a3e4a82632d8ec4b7fc86cc19 +$ORIGIN zinai.id\n$TTL 3600\n_http._tcp IN URI 10 1 "https://gaia.blockstack.org/hub/18cR3haFbmuYQVNcrCM9N1GvfPzDraQGN9/profile.json"\n\n +da6a954fdf90c890966cde5ae808ca6c96082428 +$ORIGIN zlh.id\n$TTL 3600\n_http._tcp IN URI 10 1 "https://gaia.blockstack.org/hub/19QTJKJDiUuGcQaDqF3cixdEcvRffpggp4/profile.json"\n\n +578ed433dabeb24952c892d5f3fc0cd7b9844bb8 +$ORIGIN zone117x.id\n$TTL 3600\n_http._tcp IN URI 10 1 "https://gaia.blockstack.org/hub/1Nw25PemCRv24UQAcZdaj4uD11nkTCWRTE/profile.json"\n\n +fa602059cb01dbe8d1885d8fc2a7c6b265c57e64 +$ORIGIN zumminer_crux.id\n$TTL 3600\n_http._tcp IN URI 10 1 "https://hub.cruxpay.com/1LyHVnkR3Dn227ywxEiYRRhL4AmiJF4sqJ/profile.json"\n\n +f9569aa25b6325b0f2e3a5f9edb552cd98260f9b +$ORIGIN zumminer_dev_crux.id\n$TTL 3600\n_http._tcp IN URI 10 1 "https://hub.cruxpay.com/15hvnWj74LiamdFnQW9PMvqnLAb6PQQnKt/profile.json"\n\n +853cd126478237bc7392e65091f7ffa5a1556a33 +$ORIGIN zumrai.id\n$TTL 3600\n_http._tcp IN URI 10 1 "https://gaia.blockstack.org/hub/1EPno1VcdGx89ukN2we4iVpnFtkHzw8i5d/profile.json"\n\n \ No newline at end of file diff --git a/stx-genesis/name_zonefiles-test.txt.sha256 b/stx-genesis/name_zonefiles-test.txt.sha256 new file mode 100644 index 000000000..c5c922cac --- /dev/null +++ b/stx-genesis/name_zonefiles-test.txt.sha256 @@ -0,0 +1 @@ +a7082d1ad4cfbc28820120531e02fa8b318110dae82ea98d8775ffaf9df945e6 \ No newline at end of file diff --git a/stx-genesis/src/lib.rs b/stx-genesis/src/lib.rs index c050282a9..d509728e7 100644 --- a/stx-genesis/src/lib.rs +++ b/stx-genesis/src/lib.rs @@ -1,5 +1,5 @@ -use std::io::{Cursor, Lines, prelude::*}; use std::io::{self, BufReader}; +use std::io::{prelude::*, Cursor, Lines}; use libflate::deflate::{self, Decoder}; @@ -87,13 +87,16 @@ impl GenesisData { }) } pub fn read_name_zonefiles(&self) -> Box> { - let zonefiles_bytes: &'static [u8] = include_bytes!(concat!(env!("OUT_DIR"), "/name_zonefiles.gz")); - read_deflated_zonefiles(zonefiles_bytes) + read_deflated_zonefiles(if self.use_test_chainstate_data { + include_bytes!(concat!(env!("OUT_DIR"), "/name_zonefiles-test.gz")) + } else { + include_bytes!(concat!(env!("OUT_DIR"), "/name_zonefiles.gz")) + }) } } struct LinePairReader { - val: Lines>>> + val: Lines>>>, } impl Iterator for LinePairReader { @@ -107,18 +110,18 @@ impl Iterator for LinePairReader { } } -fn read_deflated_zonefiles(deflate_bytes: &'static [u8]) -> Box> { +fn read_deflated_zonefiles( + deflate_bytes: &'static [u8], +) -> Box> { let cursor = io::Cursor::new(deflate_bytes); let deflate_decoder = deflate::Decoder::new(cursor); let buff_reader = BufReader::new(deflate_decoder); let pairs = LinePairReader { - val: buff_reader.lines() + val: buff_reader.lines(), }; - let pair_iter = pairs.into_iter().map(|pair| { - GenesisZonefile { - zonefile_hash: pair[0].to_owned(), - zonefile_content: pair[1].replace("\\n", "\n"), - } + let pair_iter = pairs.into_iter().map(|pair| GenesisZonefile { + zonefile_hash: pair[0].to_owned(), + zonefile_content: pair[1].replace("\\n", "\n"), }); return Box::new(pair_iter); } @@ -229,5 +232,8 @@ mod tests { for zonefile in GenesisData::new(false).read_name_zonefiles() { assert_eq!(zonefile.zonefile_hash.len(), 40); } + for zonefile in GenesisData::new(true).read_name_zonefiles() { + assert_eq!(zonefile.zonefile_hash.len(), 40); + } } }