depolymerization

wire gateway for Bitcoin/Ethereum
Log | Files | Refs | Submodules | README | LICENSE

commit b898b96922ac80325d6371c0aafa3ee55c2c21b2
parent b64461c9e5133f63b9edbfb9bb2fc07017a2b735
Author: Antoine A <>
Date:   Wed,  4 Oct 2023 23:21:40 +0200

Improve test runner and improve initdb commands

Diffstat:
MCargo.lock | 166+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------
Mbtc-wire/src/main.rs | 8+++++---
Meth-wire/src/main.rs | 8+++++---
Minstrumentation/Cargo.toml | 2++
Minstrumentation/src/main.rs | 133++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
Minstrumentation/src/utils.rs | 46+++++++++++++++-------------------------------
6 files changed, 271 insertions(+), 92 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -82,7 +82,7 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" dependencies = [ - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -92,7 +92,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628" dependencies = [ "anstyle", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -264,7 +264,7 @@ dependencies = [ "iana-time-zone", "num-traits", "serde", - "windows-targets", + "windows-targets 0.48.5", ] [[package]] @@ -380,6 +380,19 @@ dependencies = [ ] [[package]] +name = "console" +version = "0.15.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c926e00cc70edefdc64d3a5ff31cc65bb97a3460097762bd23afb4d8145fccf8" +dependencies = [ + "encode_unicode", + "lazy_static", + "libc", + "unicode-width", + "windows-sys 0.45.0", +] + +[[package]] name = "const-hex" version = "1.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -653,6 +666,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" [[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + +[[package]] name = "env_logger" version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -676,7 +695,7 @@ checksum = "add4f07d43996f76ef320709726a556a9d4f965d9410d8d0271132d2f8293480" dependencies = [ "errno-dragonfly", "libc", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -1081,6 +1100,28 @@ dependencies = [ ] [[package]] +name = "indicatif" +version = "0.17.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb28741c9db9a713d93deb3bb9515c20788cef5815265bee4980e87bde7e0f25" +dependencies = [ + "console", + "instant", + "number_prefix", + "portable-atomic", + "unicode-width", +] + +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", +] + +[[package]] name = "instrumentation" version = "0.1.0" dependencies = [ @@ -1095,6 +1136,7 @@ dependencies = [ "ethereum-types", "fastrand", "hex", + "indicatif", "libdeflater", "owo-colors", "rust-ini", @@ -1112,7 +1154,7 @@ checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" dependencies = [ "hermit-abi", "rustix", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -1244,7 +1286,7 @@ checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2" dependencies = [ "libc", "wasi", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -1267,6 +1309,12 @@ dependencies = [ ] [[package]] +name = "number_prefix" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" + +[[package]] name = "object" version = "0.32.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1323,7 +1371,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-targets", + "windows-targets 0.48.5", ] [[package]] @@ -1411,6 +1459,12 @@ dependencies = [ ] [[package]] +name = "portable-atomic" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31114a898e107c51bb1609ffaf55a0e011cf6a4d7f1170d0015a165082c0338b" + +[[package]] name = "postgres" version = "0.19.7" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1657,7 +1711,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -1871,7 +1925,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4031e820eb552adee9295814c0ced9e5cf38ddf1e8b7d566d6de8e2538ea989e" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -1941,7 +1995,7 @@ dependencies = [ "fastrand", "redox_syscall", "rustix", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -2055,7 +2109,7 @@ dependencies = [ "pin-project-lite", "socket2 0.5.4", "tokio-macros", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -2193,6 +2247,12 @@ dependencies = [ ] [[package]] +name = "unicode-width" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" + +[[package]] name = "untrusted" version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2402,7 +2462,16 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" dependencies = [ - "windows-targets", + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets 0.42.2", ] [[package]] @@ -2411,7 +2480,22 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ - "windows-targets", + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" +dependencies = [ + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", ] [[package]] @@ -2420,53 +2504,95 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] [[package]] name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + +[[package]] +name = "windows_aarch64_gnullvm" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + +[[package]] +name = "windows_aarch64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + +[[package]] +name = "windows_i686_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] name = "windows_i686_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + +[[package]] +name = "windows_i686_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_x86_64_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + +[[package]] +name = "windows_x86_64_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + +[[package]] +name = "windows_x86_64_gnullvm" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" + +[[package]] +name = "windows_x86_64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" diff --git a/btc-wire/src/main.rs b/btc-wire/src/main.rs @@ -78,21 +78,23 @@ fn init(config: Option<PathBuf>, init: Init) -> LoopResult<()> { let mut rpc = Rpc::common(&btc_conf).or_fail(|e| format!("rpc connect: {}", e)); match init { Init::Initdb => { + let mut tx = db.transaction()?; // Load schema - db.batch_execute(include_str!("../../db/btc.sql"))?; + tx.batch_execute(include_str!("../../db/btc.sql"))?; // Init status to true - db + tx .execute( "INSERT INTO state (name, value) VALUES ('status', $1) ON CONFLICT (name) DO NOTHING", &[&[1u8].as_slice()], )?; // Init last_hash if not already set let genesis_hash = rpc.get_genesis()?; - db + tx .execute( "INSERT INTO state (name, value) VALUES ('last_hash', $1) ON CONFLICT (name) DO NOTHING", &[&genesis_hash.as_byte_array().as_slice()], )?; + tx.commit()?; println!("Database initialised"); } Init::Initwallet => { diff --git a/eth-wire/src/main.rs b/eth-wire/src/main.rs @@ -73,10 +73,11 @@ fn init(config: Option<PathBuf>, init: Init) -> LoopResult<()> { match init { Init::Initdb => { + let mut tx = db.transaction()?; // Load schema - db.batch_execute(include_str!("../../db/eth.sql"))?; + tx.batch_execute(include_str!("../../db/eth.sql"))?; // Init status to true - db + tx .execute( "INSERT INTO state (name, value) VALUES ('status', $1) ON CONFLICT (name) DO NOTHING", &[&[1u8].as_ref()], @@ -88,10 +89,11 @@ fn init(config: Option<PathBuf>, init: Init) -> LoopResult<()> { tip_height: block.number.unwrap(), conf_height: block.number.unwrap(), }; - db.execute( + tx.execute( "INSERT INTO state (name, value) VALUES ('sync', $1) ON CONFLICT (name) DO NOTHING", &[&state.to_bytes().as_ref()], )?; + tx.commit()?; println!("Database initialised"); } Init::Initwallet => { diff --git a/instrumentation/Cargo.toml b/instrumentation/Cargo.toml @@ -33,6 +33,8 @@ color-backtrace = "0.6.0" signal-child = "1.0.5" # Edit toml files rust-ini = "0.19.0" +# Progress reporting +indicatif = "0.17.7" thread-local-panic-hook = "0.1.0" diff --git a/instrumentation/src/main.rs b/instrumentation/src/main.rs @@ -14,15 +14,20 @@ TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -use std::{panic::catch_unwind, path::PathBuf, time::Instant}; +use std::{ + panic::catch_unwind, + path::PathBuf, + time::{Duration, Instant}, +}; use clap::Parser; use color_backtrace::termcolor::NoColor; use common::{config::TalerConfig, currency::Currency}; +use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use owo_colors::OwoColorize; use thread_local_panic_hook::set_hook; -use crate::utils::TestCtx; +use crate::utils::{try_cmd_redirect, TestCtx}; mod btc; mod eth; @@ -70,42 +75,81 @@ pub fn main() { } Cmd::Offline { filters } => { std::fs::remove_dir_all("log").ok(); - std::fs::create_dir_all("log").unwrap(); + std::fs::create_dir_all("log/bin").unwrap(); + + // Build binaries + let p = ProgressBar::new_spinner(); + p.set_style(ProgressStyle::with_template("building {msg} {elapsed:.dim}").unwrap()); + p.enable_steady_tick(Duration::from_millis(1000)); + build_bin(&p, "wire-gateway", Some("test"), "wire-gateway"); + for name in ["btc-wire", "eth-wire"] { + build_bin(&p, name, None, name); + build_bin(&p, name, Some("fail"), &format!("{name}-fail")); + } + p.finish_and_clear(); + + // Run tests + let m = MultiProgress::new(); + let start_style = + &ProgressStyle::with_template("{prefix:.magenta} {elapsed:.dim}").unwrap(); + let ok_style = + &ProgressStyle::with_template("{prefix:.magenta} {msg:.green} {elapsed:.dim}") + .unwrap(); + let err_style = + &ProgressStyle::with_template("{prefix:.magenta} {msg:.red} {elapsed:.dim}") + .unwrap(); let start = Instant::now(); - let tests: Vec<_> = TESTS - .iter() - .filter(|(_, name)| { - filters.is_empty() || filters.iter().any(|f| name.starts_with(f)) - }) - .map(|(action, name)| { - let n = name.to_string(); - let join = std::thread::spawn(move || { - let start = Instant::now(); - let ctx = TestCtx::new(&n); - let tmp = ctx.clone(); - set_hook(Box::new(move |info| { - let mut buf = Vec::new(); - color_backtrace::BacktracePrinter::new() - .print_panic_info(info, &mut NoColor::new(&mut buf)) - .ok(); - let str = String::from_utf8(buf).unwrap_or_default(); - tmp.out.lock().unwrap().push_str(&str); - })); - let tmp = ctx.clone(); - let result = catch_unwind(|| { - action(tmp); + let results: Vec<_> = std::thread::scope(|s| { + let tests: Vec<_> = TESTS + .iter() + .filter(|(_, name)| { + filters.is_empty() || filters.iter().any(|f| name.starts_with(f)) + }) + .map(|(action, name)| { + let pb = m.add(ProgressBar::new_spinner()); + pb.set_style(start_style.clone()); + pb.set_prefix(*name); + pb.enable_steady_tick(Duration::from_millis(1000)); + let join = s.spawn(move || { + let start = Instant::now(); + let ctx = TestCtx::new(name); + let tmp = ctx.clone(); + set_hook(Box::new(move |info| { + let mut buf = Vec::new(); + color_backtrace::BacktracePrinter::new() + .print_panic_info(info, &mut NoColor::new(&mut buf)) + .ok(); + let str = String::from_utf8(buf).unwrap_or_default(); + tmp.out.lock().unwrap().push_str(&str); + })); + let tmp = ctx.clone(); + let result = catch_unwind(|| { + action(tmp); + }); + let lock = ctx.out.lock().unwrap(); + if result.is_ok() { + pb.set_style(ok_style.clone()); + pb.finish_with_message("OK"); + } else { + pb.set_style(err_style.clone()); + pb.finish_with_message("ERR"); + } + + (result, start.elapsed(), lock.clone()) }); - let lock = ctx.out.lock().unwrap(); - (result, start.elapsed(), lock.clone()) - }); - (join, name) - }) - .collect(); - - let len = tests.len(); - for (handle, name) in tests { - let (result, time, out) = handle.join().unwrap(); + (join, name) + }) + .collect(); + tests + .into_iter() + .map(|(j, n)| (j.join().unwrap(), n)) + .collect() + }); + + let len = results.len(); + m.clear().unwrap(); + for ((result, time, out), name) in results { match result { Ok(_) => { println!( @@ -131,6 +175,25 @@ pub fn main() { } } +pub fn build_bin(p: &ProgressBar, name: &str, features: Option<&str>, bin_name: &str) { + p.set_message(bin_name.to_string()); + let mut args = vec!["build", "--bin", name, "--release"]; + if let Some(features) = features { + args.extend_from_slice(&["--features", features]); + } + let result = try_cmd_redirect("cargo", &args, "log/bin/build") + .unwrap() + .0 + .wait() + .unwrap(); + assert!(result.success()); + std::fs::rename( + format!("target/release/{name}"), + format!("log/bin/{bin_name}"), + ) + .unwrap(); +} + pub const TESTS: &[(fn(TestCtx), &str)] = &[ (gateway::api, "gateway_api"), (gateway::auth, "gateway_auth"), diff --git a/instrumentation/src/utils.rs b/instrumentation/src/utils.rs @@ -238,7 +238,7 @@ pub struct TalerCtx { pub taler_conf: TalerConfig, ctx: TestCtx, db: ChildGuard, - wire_name: String, + wire_bin_path: String, stressed: bool, gateway: Option<ChildGuard>, pub gateway_url: String, @@ -345,7 +345,7 @@ impl TalerCtx { db }; - + let wire_name = wire_name.into(); Self { ctx: ctx.clone(), gateway_url: format!("http://localhost:{}/", taler_conf.port()), @@ -356,7 +356,11 @@ impl TalerCtx { conf, taler_conf, db, - wire_name: wire_name.into(), + wire_bin_path: if stressed { + format!("log/bin/{wire_name}-fail") + } else { + format!("log/bin/{wire_name}") + }, stressed, gateway: None, wire: None, @@ -365,22 +369,12 @@ impl TalerCtx { } } - fn wire_args<'a>(&'a self, args: &[&'a str]) -> Vec<&'a str> { - let mut tmp = vec!["run", "--bin", &self.wire_name, "--release"]; - if self.stressed { - tmp.extend_from_slice(&["--features", "fail"]); - } - tmp.push("--"); - tmp.extend_from_slice(args); - return tmp; - } - pub fn init_db(&self) { // Init db retry(|| { cmd_redirect( - "cargo", - &self.wire_args(&["-c", self.conf.to_string_lossy().as_ref(), "initdb"]), + &self.wire_bin_path, + &["-c", self.conf.to_string_lossy().as_ref(), "initdb"], &self.log("cmd"), ) .0 @@ -393,32 +387,22 @@ impl TalerCtx { pub fn run(&mut self) { // Start wires self.wire = Some(cmd_redirect( - "cargo", - &self.wire_args(&["-c", self.conf.to_string_lossy().as_ref()]), + &self.wire_bin_path, + &["-c", self.conf.to_string_lossy().as_ref()], &self.log("wire"), )); self.wire2 = self.stressed.then(|| { cmd_redirect( - "cargo", - &self.wire_args(&["-c", self.conf.to_string_lossy().as_ref()]), + &self.wire_bin_path, + &["-c", self.conf.to_string_lossy().as_ref()], &self.log("wire1"), ) }); // Run gateway self.gateway = Some(cmd_redirect( - "cargo", - &[ - "run", - "--bin", - "wire-gateway", - "--release", - "--features", - "test", - "--", - "-c", - self.conf.to_string_lossy().as_ref(), - ], + "log/bin/wire-gateway", + &["-c", self.conf.to_string_lossy().as_ref()], &self.log("gateway"), )); retry(|| {