Skip to content

Commit

Permalink
combines tests into compat
Browse files Browse the repository at this point in the history
  • Loading branch information
hinto-janai committed Dec 16, 2024
1 parent a6e506a commit a973671
Show file tree
Hide file tree
Showing 16 changed files with 558 additions and 675 deletions.
87 changes: 72 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ members = [
"types",

# Tests
"tests/pow",
"tests/monero-serai",
"tests/compat",
]

[profile.release]
panic = "abort"
panic = "abort"
lto = true # Build with LTO
strip = "none" # Keep panic stack traces
codegen-units = 1 # Optimize for binary speed over compile times
Expand Down
23 changes: 19 additions & 4 deletions tests/pow/Cargo.toml → tests/compat/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
[package]
name = "tests-pow"
version = "0.0.0"
edition = "2021"
name = "tests-compat"
version = "0.0.0"
edition = "2021"
description = "Compatability tests between `cuprated` and `monerod`"
license = "MIT"
authors = ["hinto-janai"]
repository = "https://github.com/Cuprate/cuprate/tree/main/tests/compat"
keywords = ["cuprate", "tests", "compat"]


[dependencies]
cuprate-consensus-rules = { workspace = true }
cuprate-cryptonight = { workspace = true }
cuprate-cryptonight = { workspace = true }

clap = { workspace = true, features = ["cargo", "derive", "default"] }
crossbeam = { workspace = true, features = ["std"] }
futures = { workspace = true, features = ["std"] }
monero-serai = { workspace = true }
rayon = { workspace = true }
hex = { workspace = true, features = ["serde", "std"] }
hex-literal = { workspace = true }
serde = { workspace = true, features = ["derive"] }
Expand All @@ -20,3 +28,10 @@ randomx-rs = { workspace = true }

[lints]
workspace = true

[profile.release]
panic = "unwind"
lto = true
strip = "none"
codegen-units = 1
opt-level = 3
30 changes: 30 additions & 0 deletions tests/compat/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::num::{NonZeroU64, NonZeroUsize};

use clap::Parser;

/// `cuprate` <-> `monerod` compatability tester.

Check warning on line 5 in tests/compat/src/cli.rs

View workflow job for this annotation

GitHub Actions / typo

"compatability" should be "compatibility".
#[derive(Parser, Debug)]
#[command(about, long_about = None)]
pub struct Args {
/// Name of the person to greet
#[arg(short, long, default_value_t = String::from("http://127.0.0.1:18081"))]
pub rpc_url: String,

/// Amount of verifying threads to spawn.
#[arg(short, long, default_value_t = std::thread::available_parallelism().unwrap())]
pub threads: NonZeroUsize,

/// Print an update every `update` amount of blocks.
#[arg(short, long, default_value_t = NonZeroU64::new(500).unwrap())]
pub update: NonZeroU64,
}

impl Args {
pub fn get() -> Self {
let this = Self::parse();

println!("{this:#?}");

this
}
}
10 changes: 10 additions & 0 deletions tests/compat/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::sync::atomic::{AtomicU64, AtomicUsize};

/// Height at which RandomX activated.
pub const RANDOMX_START_HEIGHT: u64 = 1978433;

/// Total amount of blocks tested, used as a global counter.
pub static TESTED_BLOCK_COUNT: AtomicU64 = AtomicU64::new(0);

/// Total amount of transactions tested, used as a global counter.
pub static TESTED_TX_COUNT: AtomicUsize = AtomicUsize::new(0);
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::Display;
use hex_literal::hex;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum CryptoNightHash {
pub enum CryptoNightHash {
V0,
V1,
V2,
Expand All @@ -12,7 +12,7 @@ pub(crate) enum CryptoNightHash {

impl CryptoNightHash {
/// The last height this hash function is used for proof-of-work.
pub(crate) const fn from_height(height: u64) -> Self {
pub const fn from_height(height: u64) -> Self {
if height < 1546000 {
Self::V0
} else if height < 1685555 {
Expand All @@ -26,7 +26,7 @@ impl CryptoNightHash {
}
}

pub(crate) fn hash(data: &[u8], height: u64) -> (&'static str, [u8; 32]) {
pub fn hash(data: &[u8], height: u64) -> (&'static str, [u8; 32]) {
let this = Self::from_height(height);

let hash = match Self::from_height(height) {
Expand All @@ -45,7 +45,7 @@ impl CryptoNightHash {
(this.as_str(), hash)
}

pub(crate) const fn as_str(self) -> &'static str {
pub const fn as_str(self) -> &'static str {
match self {
Self::V0 => "cryptonight_v0",
Self::V1 => "cryptonight_v1",
Expand Down
53 changes: 53 additions & 0 deletions tests/compat/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#![allow(
clippy::doc_markdown,
reason = "TODO: add exception to doc clippy for `RandomX`"
)]
#![allow(unreachable_pub, reason = "This is a binary, everything `pub` is ok")]

mod cli;
mod constants;
mod cryptonight;
mod randomx;
mod rpc;
mod types;
mod verify;

use std::{
sync::atomic::Ordering,
time::{Duration, Instant},
};

#[tokio::main]
async fn main() {
let now = Instant::now();

// Parse CLI args.
let cli::Args {
rpc_url,
update,
threads,
} = cli::Args::get();

// Set-up RPC client.
let client = rpc::RpcClient::new(rpc_url).await;
let top_height = client.top_height;
println!("top_height: {top_height}");
println!();

// Test.
let (tx, rx) = crossbeam::channel::unbounded();
verify::spawn_verify_pool(threads, update, top_height, rx);
client.test(top_height, tx).await;

// Wait for other threads to finish.
loop {
let count = constants::TESTED_BLOCK_COUNT.load(Ordering::Acquire);

if top_height == count {
println!("finished, took {}s", now.elapsed().as_secs());
std::process::exit(0);
}

std::thread::sleep(Duration::from_secs(1));
}
}
4 changes: 2 additions & 2 deletions tests/pow/src/randomx.rs → tests/compat/src/randomx.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use randomx_rs::{RandomXCache, RandomXDataset, RandomXFlag, RandomXVM};

/// Returns a [`RandomXVM`] with no optimization flags (default, light-verification).
pub(crate) fn randomx_vm_default(seed_hash: &[u8; 32]) -> RandomXVM {
pub fn randomx_vm_default(seed_hash: &[u8; 32]) -> RandomXVM {
const FLAG: RandomXFlag = RandomXFlag::FLAG_DEFAULT;

let cache = RandomXCache::new(FLAG, seed_hash).unwrap();
RandomXVM::new(FLAG, Some(cache), None).unwrap()
}

/// Returns a [`RandomXVM`] with most optimization flags.
pub(crate) fn randomx_vm_optimized(seed_hash: &[u8; 32]) -> RandomXVM {
pub fn randomx_vm_optimized(seed_hash: &[u8; 32]) -> RandomXVM {
// TODO: conditional FLAG_LARGE_PAGES, FLAG_JIT

let mut vm_flag = RandomXFlag::FLAG_FULL_MEM;
Expand Down
Loading

0 comments on commit a973671

Please sign in to comment.