Skip to content
Snippets Groups Projects
Commit 2885c78c authored by Éloïs's avatar Éloïs
Browse files

[tests] blocks-tests-tools: provision of the first chunks of the g1-test

parent 5003021c
No related branches found
No related tags found
2 merge requests!232Elois/local validation,!221WIP: Resolve "Fail to revert block with transactions"
......@@ -20,5 +20,7 @@ dubp-user-docs = { path = "../../dubp/user-docs" }
dup-crypto = { path = "../../crypto" }
dup-crypto-tests-tools = { path = "../crypto-tests-tools" }
dubp-user-docs-tests-tools = { path = "../user-docs-tests-tools" }
json-pest-parser = { path = "../../tools/json-pest-parser" }
failure = "0.1.5"
[dev-dependencies]
Source diff could not be displayed: it is too large. Options to address this: view the blob.
Source diff could not be displayed: it is too large. Options to address this: view the blob.
Source diff could not be displayed: it is too large. Options to address this: view the blob.
// Copyright (C) 2019 Éloïs SANCHEZ
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! G1-test blocks
use dubp_block_doc::BlockDocument;
/// Give g1-test chunks (packages of 250 blocks)
pub fn get_gt_chunk(chunk_number: usize) -> Vec<BlockDocument> {
let mut gt_json_chunks_path = std::env::current_exe().expect("Fail to get current exe path.");
gt_json_chunks_path.pop();
gt_json_chunks_path.pop();
gt_json_chunks_path.pop();
gt_json_chunks_path.pop();
gt_json_chunks_path.push("lib");
gt_json_chunks_path.push("tests-tools");
gt_json_chunks_path.push("blocks-tests-tools");
gt_json_chunks_path.push("rsc");
crate::json_chunk_parser::open_and_parse_one_json_chunk(&gt_json_chunks_path, chunk_number).1
}
// Copyright (C) 2019 Éloïs SANCHEZ
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! JSON blocks chunk parser
use dubp_block_doc::parser::parse_json_block;
use dubp_block_doc::BlockDocument;
use failure::Error;
use std::io::Read;
use std::path::PathBuf;
/// Chunk file name begin
static CHUNK_FILE_NAME_BEGIN: &str = "chunk_";
/// Chunk file name end
static CHUNK_FILE_NAME_END: &str = "-250.json";
static CHUNK_SIZE: &usize = &250;
/// Open and parse one JSON Chunk
pub fn open_and_parse_one_json_chunk(
json_chunks_path: &PathBuf,
chunk_number: usize,
) -> (usize, Vec<BlockDocument>) {
// Open chunk file
let chunk_file_content_result = open_json_chunk_file(json_chunks_path, chunk_number);
println!("json_chunks_path={:?}", json_chunks_path);
if chunk_file_content_result.is_err() {
panic!("Fail to open chunk file n°{}", chunk_number);
}
// Parse chunk file content
let blocks_result = parse_json_chunk(&chunk_file_content_result.expect("safe unwrap"));
let blocks = match blocks_result {
Ok(blocks) => blocks,
Err(e) => {
panic!("Fail to parse chunk file n°{} : {}", chunk_number, e);
}
};
(chunk_number, blocks)
}
fn open_json_chunk_file(
json_chunks_path: &PathBuf,
chunk_number: usize,
) -> std::io::Result<(String)> {
let mut chunk_file_path = json_chunks_path.clone();
chunk_file_path.push(&format!(
"{}{}{}",
CHUNK_FILE_NAME_BEGIN, chunk_number, CHUNK_FILE_NAME_END
));
let file = std::fs::File::open(chunk_file_path)?;
let mut buf_reader = std::io::BufReader::new(file);
let mut contents = String::new();
buf_reader.read_to_string(&mut contents)?;
Ok(contents)
}
/// Parse json chunk into BlockDocument Vector
fn parse_json_chunk(json_chunk_content: &str) -> Result<Vec<BlockDocument>, Error> {
let mut block_doc_vec = Vec::with_capacity(*CHUNK_SIZE);
let json_value = json_pest_parser::parse_json_string(json_chunk_content)?;
if let Some(json_object) = json_value.to_object() {
if let Some(blocks) = json_object.get("blocks") {
if let Some(blocks_array) = blocks.to_array() {
for json_block in blocks_array {
block_doc_vec.push(parse_json_block(json_block)?);
}
} else {
panic!("Fail to parse json chunk : field \"blocks\" must be an array !");
}
} else {
panic!("Fail to parse json chunk : field \"blocks\" don't exist !");
}
} else {
panic!("Fail to parse json chunk : json root node must be an object !");
}
Ok(block_doc_vec)
}
......@@ -26,4 +26,6 @@
unused_import_braces
)]
pub mod gt;
pub mod json_chunk_parser;
pub mod mocks;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment