From 43ddb152768946563a50ec96bf1768c4c354bc24 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Wed, 11 Nov 2026 16:01:12 +0000 Subject: [PATCH 1/2] test: mock JAM RPC node; check onchain endpoint values Feed onchain tests through a mock JAM node and the new one-shot ingest_until_closed hook, then assert only on API responses. --- src/onchain_stats.rs | 12 + tests/common/mock_jam_rpc.rs | 101 ++++++ tests/common/mod.rs | 2 + tests/onchain_tests.rs | 684 ++++++++++++++++++++++++++++++----- 4 files changed, 704 insertions(+), 95 deletions(-) create mode 100644 tests/common/mock_jam_rpc.rs diff --git a/src/onchain_stats.rs b/src/onchain_stats.rs index 942e9ca..81953e4 100644 --- a/src/onchain_stats.rs +++ b/src/onchain_stats.rs @@ -96,6 +96,18 @@ pub fn spawn_onchain_ingestion( .collect() } +/// One-shot ingestion for tests: runs a single connection to `url` without +/// reconnecting, returning once the node has closed it and everything it sent +/// has been processed. +pub async fn ingest_until_closed( + url: &str, + pool: &PgPool, + slot_period_secs: u16, +) -> anyhow::Result<()> { + let counters = RwLock::new(IngestCounters::new()); + run_connection(url, pool, slot_period_secs, &counters, 0).await +} + /// Run a single RPC connection: subscribe to stats + finalization, process both. async fn run_connection( url: &str, diff --git a/tests/common/mock_jam_rpc.rs b/tests/common/mock_jam_rpc.rs new file mode 100644 index 0000000..10188bd --- /dev/null +++ b/tests/common/mock_jam_rpc.rs @@ -0,0 +1,101 @@ +#![allow(dead_code)] +//! Minimal mock of the JAM node JSON-RPC WebSocket API, implementing just +//! enough of the subscription handshake for jsonrpsee's ws-client to receive +//! statistics updates. + +use futures_util::{SinkExt, StreamExt}; +use jam_codec::Encode; +use jam_std_common::{ChainSubUpdate, Statistics}; +use jam_types::{AnyBytes, HeaderHash, Slot}; +use serde_json::{json, Value}; +use std::sync::Arc; +use tokio::net::{TcpListener, TcpStream}; +use tokio::task::JoinHandle; +use tokio_tungstenite::tungstenite::Message; + +const STATISTICS_SUBSCRIPTION: u64 = 1; +const FINALIZED_SUBSCRIPTION: u64 = 2; + +pub struct Block { + pub header_hash: [u8; 32], + pub slot: Slot, + pub statistics: Statistics, +} + +pub struct MockJamRpc { + url: String, + accept_loop: JoinHandle<()>, +} + +impl MockJamRpc { + /// Every client that connects receives all `blocks` as statistics updates, + /// in order, and then has its connection closed. + pub async fn start(blocks: Vec) -> Self { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("ws://{}", listener.local_addr().unwrap()); + let blocks = Arc::new(blocks); + let accept_loop = tokio::spawn(async move { + loop { + let (stream, _) = listener.accept().await.unwrap(); + tokio::spawn(serve_connection(stream, Arc::clone(&blocks))); + } + }); + Self { url, accept_loop } + } + + pub fn url(&self) -> &str { + &self.url + } +} + +impl Drop for MockJamRpc { + fn drop(&mut self) { + self.accept_loop.abort(); + } +} + +async fn serve_connection(stream: TcpStream, blocks: Arc>) { + let mut ws = tokio_tungstenite::accept_async(stream).await.unwrap(); + + // Closing before both subscriptions are acknowledged fails the client's subscribe call. + let mut pending = vec!["subscribeStatistics", "subscribeFinalizedBlock"]; + while !pending.is_empty() { + let Some(message) = ws.next().await else { + return; + }; + let Message::Text(text) = message.unwrap() else { + continue; + }; + let request: Value = serde_json::from_str(&text).unwrap(); + let method = request["method"].as_str().unwrap(); + let subscription = match method { + "subscribeStatistics" => STATISTICS_SUBSCRIPTION, + "subscribeFinalizedBlock" => FINALIZED_SUBSCRIPTION, + other => panic!("mock JAM RPC got unexpected method {other}"), + }; + pending.retain(|pending_method| *pending_method != method); + let ack = json!({ "jsonrpc": "2.0", "result": subscription, "id": request["id"] }); + ws.send(Message::Text(ack.to_string())).await.unwrap(); + } + + for block in blocks.iter() { + let update = ChainSubUpdate { + header_hash: HeaderHash(block.header_hash), + slot: block.slot, + value: AnyBytes(block.statistics.encode().into()), + }; + let notification = json!({ + "jsonrpc": "2.0", + "method": "subscribeStatistics", + "params": { "subscription": STATISTICS_SUBSCRIPTION, "result": update }, + }); + ws.send(Message::Text(notification.to_string())) + .await + .unwrap(); + } + + ws.close(None).await.unwrap(); + // Reading until the client hangs up keeps its unsubscribe requests from + // turning our close into a TCP reset that could drop unread notifications. + while let Some(Ok(_)) = ws.next().await {} +} diff --git a/tests/common/mod.rs b/tests/common/mod.rs index fcdcbb0..5b4f2a1 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -9,6 +9,8 @@ use tokio::io::AsyncWriteExt; use tokio::net::TcpStream; use tokio::time::sleep; +pub mod mock_jam_rpc; + /// Core count used in test_protocol_params(). Status events must have /// num_guarantees with exactly this many elements to match the decoder. #[allow(dead_code)] diff --git a/tests/onchain_tests.rs b/tests/onchain_tests.rs index fc49c96..718daa5 100644 --- a/tests/onchain_tests.rs +++ b/tests/onchain_tests.rs @@ -2,10 +2,14 @@ mod common; use axum::http::StatusCode; use axum_test::TestServer; -use chrono::DurationRound; +use chrono::{DateTime, Duration, DurationRound, Utc}; +use common::mock_jam_rpc::{Block, MockJamRpc}; +use jam_std_common::{CoreActivityRecord, ServiceActivityRecord, Statistics, ValActivityRecord}; +use jam_types::{FixedVec, ServiceId, Slot, JAM_COMMON_ERA}; use serde_json::Value; use std::sync::Arc; use tart_backend::api::{create_api_router, ApiState}; +use tart_backend::onchain_stats::{ingest_until_closed, slot_to_timestamp}; use tart_backend::EventStore; async fn setup_test_api() -> (TestServer, Arc) { @@ -65,62 +69,119 @@ fn time_range_params() -> String { ) } -/// Two timestamps inside the previous complete 30m bucket, so `interval=30m` -/// groups them together no matter when the test runs. -fn timestamps_in_one_30m_bucket() -> (String, String) { - let bucket = chrono::Duration::minutes(30); - let bucket_start = chrono::Utc::now().duration_trunc(bucket).unwrap() - bucket; - let format = |ts: chrono::DateTime| ts.format("%Y-%m-%dT%H:%M:%SZ").to_string(); - ( - format(bucket_start + chrono::Duration::minutes(10)), - format(bucket_start + chrono::Duration::minutes(20)), - ) +const SLOT_PERIOD_SECS: u16 = 6; +const ORPHAN_HASH: u8 = 0xEE; + +fn core_count() -> i64 { + jam_types::core_count() as i64 } -/// Insert test rows into onchain_core_stats. -async fn insert_core_stats( - pool: &sqlx::PgPool, - ts: &str, - slot: i32, - core: i16, - gas_used: i64, - da_load: i32, -) { - sqlx::query( - "INSERT INTO onchain_core_stats (timestamp, slot, header_hash, core, gas_used, da_load, popularity, imports, extrinsic_count, extrinsic_size, exports, bundle_size, on_best_chain) \ - VALUES ($1::timestamptz, $2, '\\x0000000000000000000000000000000000000000000000000000000000000000', $3, $4, $5, 100, 2, 1, 256, 3, 512, true)", - ) - .bind(ts) - .bind(slot) - .bind(core) - .bind(gas_used) - .bind(da_load) - .execute(pool) - .await - .expect("Failed to insert core stats"); +fn slot_at(timestamp: DateTime) -> Slot { + let seconds_since_era = timestamp.timestamp() as u64 - JAM_COMMON_ERA; + let slot = (seconds_since_era / SLOT_PERIOD_SECS as u64) as Slot; + assert_eq!( + slot_to_timestamp(slot, SLOT_PERIOD_SECS), + timestamp, + "timestamp is not on a slot boundary" + ); + slot } -/// Insert test rows into onchain_validator_stats. -async fn insert_validator_stats( - pool: &sqlx::PgPool, - ts: &str, - slot: i32, - validator_index: i16, - blocks_produced: i32, - guarantees: i32, -) { - sqlx::query( - "INSERT INTO onchain_validator_stats (timestamp, slot, header_hash, validator_index, blocks_produced, tickets, preimages, preimages_size, guarantees, assurances, on_best_chain) \ - VALUES ($1::timestamptz, $2, '\\x0000000000000000000000000000000000000000000000000000000000000000', $3, $4, 0, 0, 0, $5, 0, true)", - ) - .bind(ts) - .bind(slot) - .bind(validator_index) - .bind(blocks_produced) - .bind(guarantees) - .execute(pool) - .await - .expect("Failed to insert validator stats"); +/// Two consecutive slots inside the previous complete 30m bucket, so +/// `interval=30m` groups them together no matter when the test runs. +fn slots_in_one_30m_bucket() -> (Slot, Slot) { + let bucket = Duration::minutes(30); + let bucket_start = Utc::now().duration_trunc(bucket).unwrap() - bucket; + let slot = slot_at(bucket_start + Duration::minutes(10)); + (slot, slot + 1) +} + +fn slot_minutes_ago(minutes: i64) -> Slot { + let this_minute = Utc::now().duration_trunc(Duration::minutes(1)).unwrap(); + slot_at(this_minute - Duration::minutes(minutes)) +} + +/// Cores past the end of `cores` report zero activity. +fn block( + header_hash: u8, + slot: Slot, + cores: Vec, + validators: Vec, + services: Vec<(ServiceId, ServiceActivityRecord)>, +) -> Block { + Block { + header_hash: [header_hash; 32], + slot, + statistics: Statistics { + vals_curr: validators.try_into().unwrap(), + vals_last: Default::default(), + cores: FixedVec::padded(&cores), + services: services.into_iter().collect(), + }, + } +} + +fn cores_block(header_hash: u8, slot: Slot, cores: Vec) -> Block { + block(header_hash, slot, cores, vec![], vec![]) +} + +/// Validator tallies are running totals for the epoch. A block raises only its +/// author's `blocks`, tickets and preimages, while guarantees and assurances +/// can rise for many validators at once. +fn validators_block(header_hash: u8, slot: Slot, validators: Vec) -> Block { + block(header_hash, slot, vec![], validators, vec![]) +} + +fn services_block( + header_hash: u8, + slot: Slot, + services: Vec<(ServiceId, ServiceActivityRecord)>, +) -> Block { + block(header_hash, slot, vec![], vec![], services) +} + +fn core(gas_used: u64, da_load: u32) -> CoreActivityRecord { + CoreActivityRecord { + gas_used, + da_load, + popularity: 100, + imports: 2, + extrinsic_count: 1, + extrinsic_size: 256, + exports: 3, + bundle_size: 512, + } +} + +fn validator(blocks: u32, guarantees: u32) -> ValActivityRecord { + ValActivityRecord { + blocks, + guarantees, + ..Default::default() + } +} + +fn service( + provided_count: u16, + refinement_count: u32, + refinement_gas_used: u64, + accumulate_gas_used: u64, +) -> ServiceActivityRecord { + ServiceActivityRecord { + provided_count, + refinement_count, + refinement_gas_used, + accumulate_gas_used, + ..Default::default() + } +} + +/// Feeds `blocks` through a mock JAM node and returns once all are stored. +async fn ingest(store: &EventStore, blocks: Vec) { + let node = MockJamRpc::start(blocks).await; + ingest_until_closed(node.url(), store.pool(), SLOT_PERIOD_SECS) + .await + .expect("ingestion failed"); } // ───────────────────────────────────────────────────────────────────────────── @@ -184,18 +245,24 @@ async fn test_onchain_validators_timeseries_empty_200() { #[tokio::test] async fn test_onchain_cores_timeseries_aggregate() { let (server, store) = setup_test_api().await; - let pool = store.pool(); - - let (t1, t2) = timestamps_in_one_30m_bucket(); - // Core 0: gas=1000, da=100 at t1 and t2 - insert_core_stats(pool, &t1, 100, 0, 1000, 100).await; - insert_core_stats(pool, &t2, 101, 0, 2000, 200).await; - // Core 1: gas=3000, da=300 at t1 and t2 - insert_core_stats(pool, &t1, 100, 1, 3000, 300).await; - insert_core_stats(pool, &t2, 101, 1, 4000, 400).await; - // Core 2: gas=5000, da=500 at t1 only - insert_core_stats(pool, &t1, 100, 2, 5000, 500).await; + let (slot1, slot2) = slots_in_one_30m_bucket(); + + ingest( + &store, + vec![ + // Core 0: gas=1000, da=100 then gas=2000, da=200 + // Core 1: gas=3000, da=300 then gas=4000, da=400 + // Core 2: gas=5000, da=500 in the first block only + cores_block( + 1, + slot1, + vec![core(1000, 100), core(3000, 300), core(5000, 500)], + ), + cores_block(2, slot2, vec![core(2000, 200), core(4000, 400)]), + ], + ) + .await; // Aggregate — should SUM across all cores per bucket, no `core` field let path = format!( @@ -230,16 +297,17 @@ async fn test_onchain_cores_timeseries_aggregate() { #[tokio::test] async fn test_onchain_cores_timeseries_filtered() { let (server, store) = setup_test_api().await; - let pool = store.pool(); - let (t1, t2) = timestamps_in_one_30m_bucket(); + let (slot1, slot2) = slots_in_one_30m_bucket(); - // Core 0 - insert_core_stats(pool, &t1, 100, 0, 1000, 100).await; - insert_core_stats(pool, &t2, 101, 0, 2000, 200).await; - // Core 1 - insert_core_stats(pool, &t1, 100, 1, 3000, 300).await; - insert_core_stats(pool, &t2, 101, 1, 4000, 400).await; + ingest( + &store, + vec![ + cores_block(1, slot1, vec![core(1000, 100), core(3000, 300)]), + cores_block(2, slot2, vec![core(2000, 200), core(4000, 400)]), + ], + ) + .await; // Filtered to core 1, 30m bucket let path = format!( @@ -268,18 +336,27 @@ async fn test_onchain_cores_timeseries_filtered() { #[tokio::test] async fn test_onchain_validators_timeseries_aggregate() { let (server, store) = setup_test_api().await; - let pool = store.pool(); - - let (t1, t2) = timestamps_in_one_30m_bucket(); - // Validator 0: blocks_produced=1, guarantees=5 at t1; blocks=2, guarantees=8 at t2 - insert_validator_stats(pool, &t1, 100, 0, 1, 5).await; - insert_validator_stats(pool, &t2, 101, 0, 2, 8).await; - // Validator 1: blocks_produced=0, guarantees=3 at t1; blocks=1, guarantees=6 at t2 - insert_validator_stats(pool, &t1, 100, 1, 0, 3).await; - insert_validator_stats(pool, &t2, 101, 1, 1, 6).await; - // Validator 2: blocks_produced=3, guarantees=10 at t1 only - insert_validator_stats(pool, &t1, 100, 2, 3, 10).await; + let (slot1, slot2) = slots_in_one_30m_bucket(); + + ingest( + &store, + vec![ + // Authored by validator 2. + validators_block( + 1, + slot1, + vec![validator(1, 5), validator(0, 3), validator(3, 10)], + ), + // Authored by validator 1; the network has produced 5 blocks this epoch. + validators_block( + 2, + slot2, + vec![validator(1, 7), validator(1, 6), validator(3, 10)], + ), + ], + ) + .await; // Aggregate — SUM across all validators per bucket, no `validator_index` field let path = format!( @@ -299,25 +376,28 @@ async fn test_onchain_validators_timeseries_aggregate() { "Aggregate should not have `validator_index` field" ); - // blocks_produced = 1 + 2 + 0 + 1 + 3 = 7 - assert_eq!(row["blocks_produced"].as_i64().unwrap(), 7); - // guarantees = 5 + 8 + 3 + 6 + 10 = 32 - assert_eq!(row["guarantees"].as_i64().unwrap(), 32); + // blocks_produced = (1 + 0 + 3) + (1 + 1 + 3) = 9 + assert_eq!(row["blocks_produced"].as_i64().unwrap(), 9); + // guarantees = (5 + 3 + 10) + (7 + 6 + 10) = 41 + assert_eq!(row["guarantees"].as_i64().unwrap(), 41); } #[tokio::test] async fn test_onchain_validators_timeseries_filtered() { let (server, store) = setup_test_api().await; - let pool = store.pool(); - let (t1, t2) = timestamps_in_one_30m_bucket(); + let (slot1, slot2) = slots_in_one_30m_bucket(); - // Validator 0 - insert_validator_stats(pool, &t1, 100, 0, 1, 5).await; - insert_validator_stats(pool, &t2, 101, 0, 2, 8).await; - // Validator 1 - insert_validator_stats(pool, &t1, 100, 1, 0, 3).await; - insert_validator_stats(pool, &t2, 101, 1, 1, 6).await; + ingest( + &store, + vec![ + // Authored by validator 1. + validators_block(1, slot1, vec![validator(1, 5), validator(1, 3)]), + // Authored by validator 0. + validators_block(2, slot2, vec![validator(2, 8), validator(1, 6)]), + ], + ) + .await; // Filtered to validator 0, 30m bucket let path = format!( @@ -338,3 +418,417 @@ async fn test_onchain_validators_timeseries_filtered() { // MAX aggregation: guarantees = max(5, 8) = 8 assert_eq!(row["guarantees"].as_i64().unwrap(), 8); } + +// ───────────────────────────────────────────────────────────────────────────── +// Core timeseries: extra columns +// ───────────────────────────────────────────────────────────────────────────── + +#[tokio::test] +async fn test_onchain_cores_timeseries_aggregate_extra_columns() { + let (server, store) = setup_test_api().await; + + let (slot1, slot2) = slots_in_one_30m_bucket(); + + // Five active core rows with the `core()` defaults: popularity=100, + // imports=2, extrinsic_count=1, extrinsic_size=256, exports=3, + // bundle_size=512; core 0 of the first block has popularity=964. + ingest( + &store, + vec![ + cores_block( + 1, + slot1, + vec![ + CoreActivityRecord { + popularity: 964, + ..core(1000, 100) + }, + core(3000, 300), + core(5000, 500), + ], + ), + cores_block(2, slot2, vec![core(2000, 200), core(4000, 400)]), + ], + ) + .await; + + let path = format!( + "/api/grafana/onchain/cores/timeseries?{}&interval=30m", + time_range_params() + ); + let response = server.get(&path).await; + assert_eq!(response.status_code(), StatusCode::OK); + let body: Vec = response.json(); + assert_eq!(body.len(), 1); + let row = &body[0]; + + // AVG over every core of both blocks: (964 + 4 x 100) / (2 x 341) = 2 + assert_eq!(row["popularity"].as_i64().unwrap(), 2); + // imports=2 x 5 = 10 + assert_eq!(row["imports"].as_i64().unwrap(), 10); + // extrinsic_count=1 x 5 = 5 + assert_eq!(row["extrinsic_count"].as_i64().unwrap(), 5); + // extrinsic_size=256 x 5 = 1280 + assert_eq!(row["extrinsic_size"].as_i64().unwrap(), 1280); + // exports=3 x 5 = 15 + assert_eq!(row["exports"].as_i64().unwrap(), 15); + // bundle_size=512 x 5 = 2560 + assert_eq!(row["bundle_size"].as_i64().unwrap(), 2560); +} + +#[tokio::test] +async fn test_onchain_cores_timeseries_filtered_extra_columns() { + let (server, store) = setup_test_api().await; + + let (slot1, slot2) = slots_in_one_30m_bucket(); + + ingest( + &store, + vec![ + cores_block(1, slot1, vec![core(1000, 100), core(3000, 300)]), + cores_block(2, slot2, vec![core(2000, 200), core(4000, 400)]), + ], + ) + .await; + + let path = format!( + "/api/grafana/onchain/cores/timeseries?{}&interval=30m&core=1", + time_range_params() + ); + let response = server.get(&path).await; + assert_eq!(response.status_code(), StatusCode::OK); + let body: Vec = response.json(); + assert_eq!(body.len(), 1); + let row = &body[0]; + + assert_eq!(row["core"].as_i64().unwrap(), 1); + // core 1: 2 rows -> imports=4, extrinsic_count=2, extrinsic_size=512, + // exports=6, bundle_size=1024. + assert_eq!(row["imports"].as_i64().unwrap(), 4); + assert_eq!(row["extrinsic_count"].as_i64().unwrap(), 2); + assert_eq!(row["extrinsic_size"].as_i64().unwrap(), 512); + assert_eq!(row["exports"].as_i64().unwrap(), 6); + assert_eq!(row["bundle_size"].as_i64().unwrap(), 1024); +} + +// ───────────────────────────────────────────────────────────────────────────── +// Summary and detail endpoints +// ───────────────────────────────────────────────────────────────────────────── + +#[tokio::test] +async fn test_onchain_cores_summary() { + let (server, store) = setup_test_api().await; + + let (slot1, slot2) = (slot_minutes_ago(10), slot_minutes_ago(5)); + + ingest( + &store, + vec![ + cores_block( + ORPHAN_HASH, + slot1, + vec![CoreActivityRecord { + popularity: 400, + ..core(9000, 900) + }], + ), + cores_block(1, slot1, vec![core(1000, 100), core(3000, 300)]), + cores_block(2, slot2, vec![core(2000, 200), core(4000, 400)]), + ], + ) + .await; + + let path = format!("/api/grafana/onchain/cores?{}", time_range_params()); + let response = server.get(&path).await; + assert_eq!(response.status_code(), StatusCode::OK); + let body: Vec = response.json(); + + // Every block reports every core; one row per core, ascending by core index. + let cores: Vec = body + .iter() + .map(|row| row["core"].as_i64().unwrap()) + .collect(); + assert_eq!(cores, (0..core_count()).collect::>()); + // Orphaned block excluded. Core 0: gas = 1000 + 2000 = 3000, da = 300. + assert_eq!(body[0]["gas_used"].as_i64().unwrap(), 3000); + assert_eq!(body[0]["da_load"].as_i64().unwrap(), 300); + assert_eq!(body[0]["popularity_avg"].as_i64().unwrap(), 100); + // Core 1: gas = 3000 + 4000 = 7000, da = 700. + assert_eq!(body[1]["gas_used"].as_i64().unwrap(), 7000); + assert_eq!(body[1]["da_load"].as_i64().unwrap(), 700); + assert_eq!(body[2]["gas_used"].as_i64().unwrap(), 0); +} + +#[tokio::test] +async fn test_onchain_core_detail() { + let (server, store) = setup_test_api().await; + + let (slot1, slot2, slot3) = ( + slot_minutes_ago(10), + slot_minutes_ago(5), + slot_minutes_ago(0), + ); + + // Only the three best-chain core-0 rows should come back. + ingest( + &store, + vec![ + cores_block(ORPHAN_HASH, slot1, vec![core(9000, 900)]), + cores_block(1, slot1, vec![core(1000, 100), core(5000, 500)]), + cores_block(2, slot2, vec![core(2000, 200)]), + cores_block(3, slot3, vec![core(3000, 300)]), + ], + ) + .await; + + let path = format!("/api/grafana/onchain/cores/0?{}", time_range_params()); + let response = server.get(&path).await; + assert_eq!(response.status_code(), StatusCode::OK); + let body: Vec = response.json(); + + assert_eq!(body.len(), 3, "expected 3 rows, got {}", body.len()); + // Newest first. + assert_eq!(body[0]["slot"].as_i64().unwrap(), slot3 as i64); + assert_eq!(body[1]["slot"].as_i64().unwrap(), slot2 as i64); + assert_eq!(body[2]["slot"].as_i64().unwrap(), slot1 as i64); + assert_eq!(body[0]["gas_used"].as_i64().unwrap(), 3000); + assert_eq!(body[2]["gas_used"].as_i64().unwrap(), 1000); + assert_eq!(body[0]["core"].as_i64().unwrap(), 0); +} + +#[tokio::test] +async fn test_onchain_services_summary() { + let (server, store) = setup_test_api().await; + + let (slot1, slot2) = (slot_minutes_ago(10), slot_minutes_ago(5)); + + ingest( + &store, + vec![ + services_block(ORPHAN_HASH, slot1, vec![(10, service(9, 9, 900, 9))]), + services_block( + 1, + slot1, + vec![(10, service(1, 2, 50, 70)), (20, service(1, 1, 10, 9))], + ), + services_block(2, slot2, vec![(10, service(1, 2, 50, 70))]), + ], + ) + .await; + + let path = format!("/api/grafana/onchain/services?{}", time_range_params()); + let response = server.get(&path).await; + assert_eq!(response.status_code(), StatusCode::OK); + let body: Vec = response.json(); + + assert_eq!(body.len(), 2, "expected 2 services, got {}", body.len()); + // Ascending by service_id. + assert_eq!(body[0]["service_id"].as_str().unwrap(), "0x0000000a"); + assert_eq!(body[1]["service_id"].as_str().unwrap(), "0x00000014"); + // Service 10: provided_count=2, refinement_count=4, refinement_gas=100. + assert_eq!(body[0]["provided_count"].as_i64().unwrap(), 2); + assert_eq!(body[0]["refinement_count"].as_i64().unwrap(), 4); + assert_eq!(body[0]["refinement_gas"].as_i64().unwrap(), 100); + assert_eq!(body[0]["accumulate_gas"].as_i64().unwrap(), 140); +} + +#[tokio::test] +async fn test_onchain_services_timeseries() { + let (server, store) = setup_test_api().await; + + let (slot1, slot2) = slots_in_one_30m_bucket(); + + ingest( + &store, + vec![ + services_block( + 1, + slot1, + vec![(10, service(0, 0, 50, 70)), (20, service(0, 0, 10, 9))], + ), + services_block(2, slot2, vec![(10, service(0, 0, 50, 70))]), + ], + ) + .await; + + let path = format!( + "/api/grafana/onchain/services/timeseries?{}&interval=30m", + time_range_params() + ); + let response = server.get(&path).await; + assert_eq!(response.status_code(), StatusCode::OK); + let body: Vec = response.json(); + + // Grouped by ts + service_id -> 2 rows in one bucket. + assert_eq!(body.len(), 2, "expected 2 rows, got {}", body.len()); + assert_eq!(body[0]["service_id"].as_str().unwrap(), "0x0000000a"); + assert_eq!(body[1]["service_id"].as_str().unwrap(), "0x00000014"); + assert_eq!(body[0]["refinement_gas"].as_i64().unwrap(), 100); + assert_eq!(body[0]["accumulate_gas"].as_i64().unwrap(), 140); +} + +#[tokio::test] +async fn test_onchain_service_detail() { + let (server, store) = setup_test_api().await; + + let (slot1, slot2, slot3) = ( + slot_minutes_ago(10), + slot_minutes_ago(5), + slot_minutes_ago(0), + ); + + // Only the three best-chain service-10 rows should come back. + ingest( + &store, + vec![ + services_block(ORPHAN_HASH, slot1, vec![(10, service(0, 0, 900, 0))]), + services_block( + 1, + slot1, + vec![(10, service(0, 0, 50, 0)), (20, service(0, 0, 10, 0))], + ), + services_block(2, slot2, vec![(10, service(0, 0, 60, 0))]), + services_block(3, slot3, vec![(10, service(0, 0, 70, 0))]), + ], + ) + .await; + + let path = format!( + "/api/grafana/onchain/services/0x0000000a?{}", + time_range_params() + ); + let response = server.get(&path).await; + assert_eq!(response.status_code(), StatusCode::OK); + let body: Vec = response.json(); + + assert_eq!(body.len(), 3, "expected 3 rows, got {}", body.len()); + // Newest first. + assert_eq!(body[0]["slot"].as_i64().unwrap(), slot3 as i64); + assert_eq!(body[1]["slot"].as_i64().unwrap(), slot2 as i64); + assert_eq!(body[2]["slot"].as_i64().unwrap(), slot1 as i64); + assert_eq!(body[0]["refinement_gas"].as_i64().unwrap(), 70); + assert_eq!(body[2]["refinement_gas"].as_i64().unwrap(), 50); + assert_eq!(body[0]["service_id"].as_str().unwrap(), "0x0000000a"); + + // Decimal form must resolve to the same service. + let path = format!("/api/grafana/onchain/services/10?{}", time_range_params()); + let response = server.get(&path).await; + assert_eq!(response.status_code(), StatusCode::OK); + let body2: Vec = response.json(); + assert_eq!(body2.len(), 3); + + // Invalid service id -> BAD_REQUEST. + let response = server + .get(&format!( + "/api/grafana/onchain/services/zzz?{}", + time_range_params() + )) + .await; + assert_eq!(response.status_code(), StatusCode::BAD_REQUEST); +} + +#[tokio::test] +async fn test_onchain_validators_summary() { + let (server, store) = setup_test_api().await; + + let slot1 = slot_minutes_ago(10); + let slot2 = slot1 + 1; + + let tallies = + |blocks, tickets, preimages, preimages_size, guarantees, assurances| ValActivityRecord { + blocks, + tickets, + preimages, + preimages_size, + guarantees, + assurances, + }; + ingest( + &store, + vec![ + // Fork authored by validator 1. + validators_block( + ORPHAN_HASH, + slot1, + vec![tallies(0, 0, 0, 0, 4, 6), tallies(3, 5, 6, 60, 5, 3)], + ), + // Authored by validator 0, on the same parent as the fork. + validators_block( + 1, + slot1, + vec![tallies(1, 2, 3, 30, 5, 6), tallies(2, 1, 1, 10, 3, 2)], + ), + // Authored by validator 0. + validators_block( + 2, + slot2, + vec![tallies(2, 5, 6, 60, 8, 9), tallies(2, 1, 1, 10, 3, 2)], + ), + ], + ) + .await; + + let path = format!("/api/grafana/onchain/validators?{}", time_range_params()); + let response = server.get(&path).await; + assert_eq!(response.status_code(), StatusCode::OK); + let body: Vec = response.json(); + + assert_eq!(body.len(), 2, "expected 2 validators, got {}", body.len()); + assert_eq!(body[0]["validator_index"].as_i64().unwrap(), 0); + assert_eq!(body[1]["validator_index"].as_i64().unwrap(), 1); + // MAX semantics: validator 0 blocks=max(1,2)=2, tickets=max(2,5)=5, + // preimages=max(3,6)=6, preimages_size=max(30,60)=60, guarantees=max(5,8)=8, + // assurances=max(6,9)=9. + assert_eq!(body[0]["blocks_produced"].as_i64().unwrap(), 2); + assert_eq!(body[0]["tickets"].as_i64().unwrap(), 5); + assert_eq!(body[0]["preimages"].as_i64().unwrap(), 6); + assert_eq!(body[0]["preimages_size"].as_i64().unwrap(), 60); + assert_eq!(body[0]["guarantees"].as_i64().unwrap(), 8); + assert_eq!(body[0]["assurances"].as_i64().unwrap(), 9); + // The fork's tallies for validator 1 (3 blocks, 5 tickets, 5 guarantees) are excluded. + assert_eq!(body[1]["blocks_produced"].as_i64().unwrap(), 2); + assert_eq!(body[1]["tickets"].as_i64().unwrap(), 1); + assert_eq!(body[1]["guarantees"].as_i64().unwrap(), 3); +} + +#[tokio::test] +async fn test_onchain_validator_detail() { + let (server, store) = setup_test_api().await; + + let slot1 = slot_minutes_ago(10); + let (slot2, slot3) = (slot1 + 1, slot1 + 2); + + let tallies = |blocks, tickets, assurances| ValActivityRecord { + blocks, + tickets, + assurances, + ..Default::default() + }; + // Only the three best-chain validator-0 rows should come back. + ingest( + &store, + vec![ + // Authors: fork validator 1, then validators 0, 1, 0. + validators_block(ORPHAN_HASH, slot1, vec![tallies(0, 0, 6), tallies(3, 3, 3)]), + validators_block(1, slot1, vec![tallies(1, 2, 6), tallies(2, 1, 3)]), + validators_block(2, slot2, vec![tallies(1, 2, 7), tallies(3, 3, 4)]), + validators_block(3, slot3, vec![tallies(2, 5, 8), tallies(3, 3, 5)]), + ], + ) + .await; + + let path = format!("/api/grafana/onchain/validators/0?{}", time_range_params()); + let response = server.get(&path).await; + assert_eq!(response.status_code(), StatusCode::OK); + let body: Vec = response.json(); + + assert_eq!(body.len(), 3, "expected 3 rows, got {}", body.len()); + // Newest first. + assert_eq!(body[0]["slot"].as_i64().unwrap(), slot3 as i64); + assert_eq!(body[1]["slot"].as_i64().unwrap(), slot2 as i64); + assert_eq!(body[2]["slot"].as_i64().unwrap(), slot1 as i64); + assert_eq!(body[0]["blocks_produced"].as_i64().unwrap(), 2); + assert_eq!(body[0]["tickets"].as_i64().unwrap(), 5); + assert_eq!(body[0]["assurances"].as_i64().unwrap(), 8); + assert_eq!(body[2]["blocks_produced"].as_i64().unwrap(), 1); +} From f6f8adc0fef9d4acd8cb131ab72fed34ccf2087f Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Wed, 11 Nov 2026 16:07:03 +0000 Subject: [PATCH 2/2] test: onchain bug tests Ignored until fixed: #14 (redundant RPC URLs insert a block twice), P2-4 (u16 stats wrap in SMALLINT) and the validators timeseries aggregate summing running totals instead of per-validator MAX. --- tests/README.md | 10 ++++++ tests/onchain_tests.rs | 81 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 86 insertions(+), 5 deletions(-) diff --git a/tests/README.md b/tests/README.md index f7f7d38..5e7eeab 100644 --- a/tests/README.md +++ b/tests/README.md @@ -45,6 +45,16 @@ cargo sqlx migrate run cargo test --test api_tests --test integration_tests --test optimized_server_tests -- --test-threads=1 ``` +## Ignored Tests (Known Bugs) + +Tests marked `#[ignore = "bug: ..."]` assert correct behavior that `src/` does not +deliver yet; the reason names the bug. Keep them, and remove the marker together +with the fix. Run them with: + +```bash +cargo test -- --ignored --test-threads=1 +``` + ## Why Tests Must Run Serially (`--test-threads=1`) **Problem**: Integration tests share the same PostgreSQL database `tart_test`. diff --git a/tests/onchain_tests.rs b/tests/onchain_tests.rs index 718daa5..b24cac3 100644 --- a/tests/onchain_tests.rs +++ b/tests/onchain_tests.rs @@ -334,6 +334,7 @@ async fn test_onchain_cores_timeseries_filtered() { // ───────────────────────────────────────────────────────────────────────────── #[tokio::test] +#[ignore = "bug: validators timeseries aggregate sums running totals instead of per-validator MAX"] async fn test_onchain_validators_timeseries_aggregate() { let (server, store) = setup_test_api().await; @@ -358,7 +359,7 @@ async fn test_onchain_validators_timeseries_aggregate() { ) .await; - // Aggregate — SUM across all validators per bucket, no `validator_index` field + // Aggregate — SUM of each validator's MAX per bucket, no `validator_index` field let path = format!( "/api/grafana/onchain/validators/timeseries?{}&interval=30m", time_range_params() @@ -376,10 +377,10 @@ async fn test_onchain_validators_timeseries_aggregate() { "Aggregate should not have `validator_index` field" ); - // blocks_produced = (1 + 0 + 3) + (1 + 1 + 3) = 9 - assert_eq!(row["blocks_produced"].as_i64().unwrap(), 9); - // guarantees = (5 + 3 + 10) + (7 + 6 + 10) = 41 - assert_eq!(row["guarantees"].as_i64().unwrap(), 41); + // blocks_produced = max(1, 1) + max(0, 1) + max(3, 3) = 5 + assert_eq!(row["blocks_produced"].as_i64().unwrap(), 5); + // guarantees = max(5, 7) + max(3, 6) + max(10, 10) = 23 + assert_eq!(row["guarantees"].as_i64().unwrap(), 23); } #[tokio::test] @@ -832,3 +833,73 @@ async fn test_onchain_validator_detail() { assert_eq!(body[0]["assurances"].as_i64().unwrap(), 8); assert_eq!(body[2]["blocks_produced"].as_i64().unwrap(), 1); } + +// ───────────────────────────────────────────────────────────────────────────── +// Ingestion bugs +// ───────────────────────────────────────────────────────────────────────────── + +/// Two redundant JAM_RPC_URLs pointing at the same node double-insert: each +/// connection carries its own `seen` set and the insert has no existence guard. +#[tokio::test] +#[ignore = "bug: #14 redundant JAM_RPC_URLs insert the same block twice"] +async fn test_onchain_multi_url_duplicate_inserts() { + let (server, store) = setup_test_api().await; + + let node = MockJamRpc::start(vec![cores_block( + 1, + slot_minutes_ago(5), + vec![core(1000, 100)], + )]) + .await; + let (first, second) = tokio::join!( + ingest_until_closed(node.url(), store.pool(), SLOT_PERIOD_SECS), + ingest_until_closed(node.url(), store.pool(), SLOT_PERIOD_SECS), + ); + first.expect("first ingestion failed"); + second.expect("second ingestion failed"); + + let path = format!("/api/grafana/onchain/cores/0?{}", time_range_params()); + let response = server.get(&path).await; + assert_eq!(response.status_code(), StatusCode::OK); + let body: Vec = response.json(); + + assert_eq!( + body.len(), + 1, + "one block must be stored once despite two redundant connections, got {} rows", + body.len() + ); +} + +/// A core `popularity` above 32767 must survive ingestion un-wrapped: the source +/// is a u16, and the insert must not narrow it to i16. +#[tokio::test] +#[ignore = "bug: onchain P2-4 u16 stats narrowed to i16 SMALLINT on insert"] +async fn test_onchain_ingestion_u16_narrowing() { + let (server, store) = setup_test_api().await; + + ingest( + &store, + vec![cores_block( + 1, + slot_minutes_ago(5), + vec![CoreActivityRecord { + popularity: 40_000, + ..Default::default() + }], + )], + ) + .await; + + let path = format!("/api/grafana/onchain/cores/0?{}", time_range_params()); + let response = server.get(&path).await; + assert_eq!(response.status_code(), StatusCode::OK); + let body: Vec = response.json(); + + assert_eq!(body.len(), 1); + let popularity = body[0]["popularity"].as_i64().unwrap(); + assert_eq!( + popularity, 40_000, + "popularity must not wrap when stored, got {popularity}" + ); +}