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

[mod] dex: make json export compatible for duniter sync

parent 88ade4c0
No related branches found
No related tags found
No related merge requests found
...@@ -65,9 +65,25 @@ pub(crate) fn export_bc<B: Backend>( ...@@ -65,9 +65,25 @@ pub(crate) fn export_bc<B: Backend>(
let jsonifier_handle = std::thread::spawn(move || { let jsonifier_handle = std::thread::spawn(move || {
r.iter().try_for_each(|block_res| { r.iter().try_for_each(|block_res| {
let json_block_res = match block_res { let json_block_res = match block_res {
Ok(block) => { Ok(block) => match serde_json::to_value(&block) {
serde_json::to_value(&block).map_err(|e| KvError::DeserError(e.into())) Ok(serde_json::Value::Object(mut block_json)) => {
block_json.remove("UDTime");
block_json.remove("fork");
// Remove field `transactions.hash`
if let Some(serde_json::Value::Array(txs_json)) =
block_json.get_mut("transactions")
{
for tx_json in txs_json {
if let serde_json::Value::Object(tx_json) = tx_json {
tx_json.remove("hash");
}
}
}
Ok(serde_json::Value::Object(block_json))
} }
Err(e) => Err(KvError::DeserError(e.into())),
Ok(_) => unreachable!(),
},
Err(e) => Err(e), Err(e) => Err(e),
}; };
s2.send(json_block_res).map_err(|_| anyhow!("fail to send")) s2.send(json_block_res).map_err(|_| anyhow!("fail to send"))
...@@ -87,9 +103,8 @@ pub(crate) fn export_bc<B: Backend>( ...@@ -87,9 +103,8 @@ pub(crate) fn export_bc<B: Backend>(
let chunk = std::mem::take(&mut json_blocks); let chunk = std::mem::take(&mut json_blocks);
json_blocks.reserve_exact(chunk_size); json_blocks.reserve_exact(chunk_size);
// Write chunk "asynchronously" // Write chunk "asynchronously"
writers_handle writers_handle.push(threadpool.launch(move |_| {
.push(threadpool.launch(move |_| { write_chunk(chunk, chunk_index, chunk_size, output_dir, pretty)
write_chunk(chunk_index, chunk, output_dir, pretty)
})?); })?);
chunk_index += 1; chunk_index += 1;
if chunk_index % 8 == 0 { if chunk_index % 8 == 0 {
...@@ -100,7 +115,7 @@ pub(crate) fn export_bc<B: Backend>( ...@@ -100,7 +115,7 @@ pub(crate) fn export_bc<B: Backend>(
})?; })?;
// Write last chunk // Write last chunk
if !json_blocks.is_empty() { if !json_blocks.is_empty() {
write_chunk(chunk_index, json_blocks, output_dir, pretty)?; write_chunk(json_blocks, chunk_index, chunk_size, output_dir, pretty)?;
} }
progress_bar.set_progress(1.0); progress_bar.set_progress(1.0);
...@@ -129,19 +144,23 @@ pub(crate) fn export_bc<B: Backend>( ...@@ -129,19 +144,23 @@ pub(crate) fn export_bc<B: Backend>(
} }
fn write_chunk( fn write_chunk(
chunk_index: usize,
chunk: Vec<serde_json::Value>, chunk: Vec<serde_json::Value>,
chunk_index: usize,
chunk_size: usize,
output_dir: &'static Path, output_dir: &'static Path,
pretty: bool, pretty: bool,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let file = let mut object_json = serde_json::Map::new();
File::create(output_dir.join(format!("chunk_{}-{}.json", chunk_index, chunk.len())))?; object_json.insert("blocks".to_owned(), serde_json::Value::Array(chunk));
let chunk_json = serde_json::Value::Object(object_json);
let file = File::create(output_dir.join(format!("chunk_{}-{}.json", chunk_index, chunk_size)))?;
let mut buffer = BufWriter::new(file); let mut buffer = BufWriter::new(file);
if pretty { if pretty {
serde_json::to_writer_pretty(&mut buffer, &serde_json::Value::Array(chunk))?; serde_json::to_writer_pretty(&mut buffer, &chunk_json)?;
} else { } else {
serde_json::to_writer(&mut buffer, &serde_json::Value::Array(chunk))?; serde_json::to_writer(&mut buffer, &chunk_json)?;
} }
buffer.flush()?; buffer.flush()?;
......
...@@ -153,6 +153,8 @@ impl DuniterServer { ...@@ -153,6 +153,8 @@ impl DuniterServer {
}); });
}); });
log::info!("Duniter sever started.");
Ok(DuniterServer { Ok(DuniterServer {
bc_db, bc_db,
conf, conf,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment