add /tests

This commit is contained in:
hinto.janai 2024-12-12 11:47:44 -05:00
parent 7b8756fa80
commit bd2624572a
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
5 changed files with 506 additions and 0 deletions

227
Cargo.lock generated
View file

@ -1363,6 +1363,15 @@ version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
[[package]]
name = "encoding_rs"
version = "0.8.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3"
dependencies = [
"cfg-if",
]
[[package]]
name = "equivalent"
version = "1.0.1"
@ -1431,6 +1440,21 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "foreign-types"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1"
dependencies = [
"foreign-types-shared",
]
[[package]]
name = "foreign-types-shared"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
[[package]]
name = "form_urlencoded"
version = "1.2.1"
@ -1779,6 +1803,22 @@ dependencies = [
"tower-service 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "hyper-tls"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0"
dependencies = [
"bytes",
"http-body-util",
"hyper",
"hyper-util",
"native-tls",
"tokio",
"tokio-native-tls",
"tower-service 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "hyper-util"
version = "0.1.10"
@ -1841,6 +1881,12 @@ dependencies = [
"hashbrown 0.15.0",
]
[[package]]
name = "ipnet"
version = "2.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708"
[[package]]
name = "is-terminal"
version = "0.4.12"
@ -2186,6 +2232,23 @@ dependencies = [
"tokio",
]
[[package]]
name = "native-tls"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8614eb2c83d59d1c8cc974dd3f920198647674a0a035e1af1fa58707e317466"
dependencies = [
"libc",
"log",
"openssl",
"openssl-probe",
"openssl-sys",
"schannel",
"security-framework",
"security-framework-sys",
"tempfile",
]
[[package]]
name = "nu-ansi-term"
version = "0.46.0"
@ -2227,12 +2290,50 @@ version = "11.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575"
[[package]]
name = "openssl"
version = "0.10.68"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6174bc48f102d208783c2c84bf931bb75927a617866870de8a4ea85597f871f5"
dependencies = [
"bitflags 2.6.0",
"cfg-if",
"foreign-types",
"libc",
"once_cell",
"openssl-macros",
"openssl-sys",
]
[[package]]
name = "openssl-macros"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "openssl-probe"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
[[package]]
name = "openssl-sys"
version = "0.9.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45abf306cbf99debc8195b66b7346498d7b10c210de50418b5ccd7ceba08c741"
dependencies = [
"cc",
"libc",
"pkg-config",
"vcpkg",
]
[[package]]
name = "option-ext"
version = "0.2.0"
@ -2364,6 +2465,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkg-config"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2"
[[package]]
name = "plotters"
version = "0.3.6"
@ -2676,6 +2783,49 @@ version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]]
name = "reqwest"
version = "0.12.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a77c62af46e79de0a562e1a9849205ffcb7fc1238876e9bd743357570e04046f"
dependencies = [
"base64",
"bytes",
"encoding_rs",
"futures-core",
"futures-util",
"h2",
"http",
"http-body",
"http-body-util",
"hyper",
"hyper-rustls",
"hyper-tls",
"hyper-util",
"ipnet",
"js-sys",
"log",
"mime",
"native-tls",
"once_cell",
"percent-encoding",
"pin-project-lite",
"rustls-pemfile",
"serde",
"serde_json",
"serde_urlencoded",
"sync_wrapper 1.0.1",
"system-configuration",
"tokio",
"tokio-native-tls",
"tower-service 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"url",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"windows-registry",
]
[[package]]
name = "ring"
version = "0.17.8"
@ -3104,6 +3254,9 @@ name = "sync_wrapper"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394"
dependencies = [
"futures-core",
]
[[package]]
name = "synchronoise"
@ -3114,6 +3267,27 @@ dependencies = [
"crossbeam-queue",
]
[[package]]
name = "system-configuration"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c879d448e9d986b661742763247d3693ed13609438cf3d006f51f5368a5ba6b"
dependencies = [
"bitflags 2.6.0",
"core-foundation",
"system-configuration-sys",
]
[[package]]
name = "system-configuration-sys"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e1d1b10ced5ca923a1fcb8d03e96b8d3268065d724548c0211415ff6ac6bac4"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "tap"
version = "1.0.1"
@ -3143,6 +3317,20 @@ dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "tests-monero-serai"
version = "0.1.0"
dependencies = [
"futures",
"hex",
"monero-serai",
"rayon",
"reqwest",
"serde",
"serde_json",
"tokio",
]
[[package]]
name = "thiserror"
version = "1.0.66"
@ -3233,6 +3421,16 @@ dependencies = [
"syn",
]
[[package]]
name = "tokio-native-tls"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2"
dependencies = [
"native-tls",
"tokio",
]
[[package]]
name = "tokio-rustls"
version = "0.26.0"
@ -3520,6 +3718,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "version_check"
version = "0.9.5"
@ -3586,6 +3790,18 @@ dependencies = [
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc7ec4f8827a71586374db3e87abdb5a2bb3a15afed140221307c3ec06b1f63b"
dependencies = [
"cfg-if",
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.95"
@ -3719,6 +3935,17 @@ dependencies = [
"syn",
]
[[package]]
name = "windows-registry"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e400001bb720a623c1c69032f8e3e4cf09984deec740f007dd2b03ec864804b0"
dependencies = [
"windows-result",
"windows-strings",
"windows-targets 0.52.6",
]
[[package]]
name = "windows-result"
version = "0.2.0"

View file

@ -52,6 +52,9 @@ members = [
"pruning",
"test-utils",
"types",
# Tests
"tests/monero-serai",
]
[profile.release]

View file

@ -0,0 +1,17 @@
[package]
name = "tests-monero-serai"
version = "0.1.0"
edition = "2021"
[dependencies]
monero-serai = { workspace = true }
hex = { workspace = true, features = ["serde", "std"] }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true, features = ["std"] }
tokio = { workspace = true, features = ["full"] }
reqwest = { version = "0.12", features = ["json"] }
rayon = { workspace = true }
futures = { workspace = true, features = ["std"] }
[lints]
workspace = true

View file

@ -0,0 +1,72 @@
use std::{
sync::atomic::{AtomicUsize, Ordering},
time::{Duration, Instant},
};
mod rpc;
pub static TESTED_BLOCK_COUNT: AtomicUsize = AtomicUsize::new(0);
#[tokio::main]
async fn main() {
let now = Instant::now();
let rpc_node_url = if let Ok(url) = std::env::var("RPC_NODE_URL") {
url
} else {
"http://127.0.0.1:18081/json_rpc".to_string()
};
println!("rpc_node_url: {rpc_node_url}");
let top_height = rpc::RpcClient::top_height(rpc_node_url.clone()).await;
println!("top_height: {top_height}");
assert!(top_height > 3301441, "node is behind");
let ranges = (0..top_height)
.collect::<Vec<usize>>()
.chunks(100_000)
.map(<[usize]>::to_vec)
.collect::<Vec<Vec<usize>>>();
println!("ranges: ");
for range in &ranges {
println!("[{}..{}]", range.first().unwrap(), range.last().unwrap());
}
let rpc_client = rpc::RpcClient::new(rpc_node_url);
let iter = ranges.into_iter().map(move |range| {
let c = rpc_client.clone();
async move {
tokio::task::spawn_blocking(move || async move {
c.get_block_test_batch(range.into_iter().collect()).await;
})
.await
.unwrap()
.await;
}
});
std::thread::spawn(move || {
let mut count = 0;
#[expect(clippy::cast_precision_loss)]
while count != top_height {
let c = TESTED_BLOCK_COUNT.load(Ordering::Acquire);
count = c;
println!(
"blocks processed ... {c} ({:.2}%)",
(c as f64 / top_height as f64) * 100.0
);
std::thread::sleep(Duration::from_millis(250));
}
println!("finished all blocks, took: {}s", now.elapsed().as_secs());
std::process::exit(0);
});
futures::future::join_all(iter).await;
std::thread::park();
}

View file

@ -0,0 +1,187 @@
use std::{collections::BTreeSet, sync::atomic::Ordering};
use hex::serde::deserialize;
use monero_serai::block::Block;
use reqwest::{
header::{HeaderMap, HeaderValue},
Client, ClientBuilder,
};
use serde::Deserialize;
use serde_json::json;
use crate::TESTED_BLOCK_COUNT;
#[derive(Debug, Clone, Deserialize)]
pub(crate) struct BlockHeader {
#[serde(deserialize_with = "deserialize")]
pub hash: Vec<u8>,
#[serde(deserialize_with = "deserialize")]
pub miner_tx_hash: Vec<u8>,
#[serde(deserialize_with = "deserialize")]
pub prev_hash: Vec<u8>,
pub block_weight: usize,
pub height: usize,
pub major_version: u8,
pub minor_version: u8,
pub nonce: u32,
pub num_txes: usize,
pub reward: u64,
pub timestamp: u64,
}
#[derive(Debug, Clone)]
pub(crate) struct RpcClient {
client: Client,
rpc_node_url: String,
}
impl RpcClient {
pub(crate) fn new(rpc_node_url: String) -> Self {
let headers = {
let mut h = HeaderMap::new();
h.insert("Content-Type", HeaderValue::from_static("application/json"));
h
};
let client = ClientBuilder::new()
.default_headers(headers)
.build()
.unwrap();
Self {
client,
rpc_node_url,
}
}
pub(crate) async fn top_height(rpc_node_url: String) -> usize {
#[derive(Debug, Clone, Deserialize)]
struct JsonRpcResponse {
result: GetLastBlockHeaderResponse,
}
#[derive(Debug, Clone, Deserialize)]
pub(crate) struct GetLastBlockHeaderResponse {
pub block_header: BlockHeader,
}
let this = Self::new(rpc_node_url);
let request = json!({
"jsonrpc": "2.0",
"id": 0,
"method": "get_last_block_header",
"params": {}
});
this.client
.get(this.rpc_node_url)
.json(&request)
.send()
.await
.unwrap()
.json::<JsonRpcResponse>()
.await
.unwrap()
.result
.block_header
.height
}
pub(crate) async fn get_block_test_batch(&self, heights: BTreeSet<usize>) {
#[derive(Debug, Clone, Deserialize)]
struct JsonRpcResponse {
result: GetBlockResponse,
}
#[derive(Debug, Clone, Deserialize)]
pub(crate) struct GetBlockResponse {
#[serde(deserialize_with = "deserialize")]
pub blob: Vec<u8>,
pub block_header: BlockHeader,
}
let tasks = heights.into_iter().map(|height| {
let request = json!({
"jsonrpc": "2.0",
"id": 0,
"method": "get_block",
"params": {"height": height}
});
let task =
tokio::task::spawn(self.client.get(&self.rpc_node_url).json(&request).send());
(height, task)
});
for (height, task) in tasks {
let resp = task
.await
.unwrap()
.unwrap()
.json::<JsonRpcResponse>()
.await
.unwrap()
.result;
rayon::spawn(move || {
let info = format!("\nheight: {height}\nresponse: {resp:#?}");
// Test block deserialization.
let block = match Block::read(&mut resp.blob.as_slice()) {
Ok(b) => b,
Err(e) => panic!("{e:?}\n{info}"),
};
// Test block properties.
assert_eq!(resp.blob, block.serialize(), "{info}");
assert!(
!block.miner_transaction.prefix().outputs.is_empty(),
"miner_tx has no outputs\n{info}"
);
let block_reward = block
.miner_transaction
.prefix()
.outputs
.iter()
.map(|o| o.amount.unwrap())
.sum::<u64>();
assert_ne!(block_reward, 0, "block reward is 0\n{info}");
// Test fields are correct.
let BlockHeader {
block_weight,
hash,
height,
major_version,
minor_version,
miner_tx_hash,
nonce,
num_txes,
prev_hash,
reward,
timestamp,
} = resp.block_header;
assert_ne!(block_weight, 0, "{info}"); // TODO: test this
assert_ne!(block.miner_transaction.weight(), 0, "{info}"); // TODO: test this
assert_eq!(hash, block.hash(), "{info}");
assert_eq!(height, block.number().unwrap(), "{info}");
assert_eq!(major_version, block.header.hardfork_version, "{info}");
assert_eq!(minor_version, block.header.hardfork_signal, "{info}");
assert_eq!(miner_tx_hash, block.miner_transaction.hash(), "{info}");
assert_eq!(nonce, block.header.nonce, "{info}");
assert_eq!(num_txes, block.transactions.len(), "{info}");
assert_eq!(prev_hash, block.header.previous, "{info}");
assert_eq!(reward, block_reward, "{info}");
assert_eq!(timestamp, block.header.timestamp, "{info}");
TESTED_BLOCK_COUNT.fetch_add(1, Ordering::Release);
});
}
}
}