#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::testnet::TestNetworkConfig;
use super::verify_storage_gate::find_key_for_target;
use super::TestHarness;
use ant_node::client::XorName;
use ant_node::replication::config::storage_admission_width;
use ant_node::replication::{
verification_requests_for_key_from_for_test, watch_verification_requests_for_test,
};
use ant_node::{ReplicationConfig, ReplicationEngine};
use saorsa_core::identity::PeerId;
use serial_test::serial;
use std::collections::HashMap;
use std::time::Duration;
const NODE_COUNT: usize = 12;
const CLOSE_GROUP_SIZE: usize = 7;
const TARGET_INDEX: usize = 5;
const HOLDER_INDEX: usize = 6;
const IMPOSSIBLE_DISK_RESERVE: u64 = u64::MAX / 2;
const OBSERVATION_WINDOW: Duration = Duration::from_secs(15);
const OBSERVATION_POLL: Duration = Duration::from_millis(200);
const CONTROL_INDEX: usize = 7;
const CAPACITY_BACKOFF_FLOOR: Duration = Duration::from_secs(60);
async fn ensure_pending_verify(engine: &ReplicationEngine, key: XorName, hinter: PeerId) {
if engine.enqueue_pending_verify_for_test(key, hinter).await {
return;
}
assert!(
engine.pending_verify_delay_for_test(&key).await.is_some(),
"key {} was refused admission to pending verification and is not already \
there, so the phase that follows has nothing to observe",
hex::encode(key)
);
}
#[tokio::test]
#[serial]
async fn write_blocked_node_neither_probes_nor_dials() {
let mut storage_disk_reserve_overrides = HashMap::new();
storage_disk_reserve_overrides.insert(TARGET_INDEX, IMPOSSIBLE_DISK_RESERVE);
let config = TestNetworkConfig {
node_count: NODE_COUNT,
storage_disk_reserve_overrides,
replication_config: Some(ReplicationConfig {
close_group_size: CLOSE_GROUP_SIZE,
..ReplicationConfig::default()
}),
..TestNetworkConfig::default()
};
let harness = TestHarness::setup_with_config(config)
.await
.expect("setup write-blocked network");
harness.warmup_dht().await.expect("warmup");
let width = storage_admission_width(CLOSE_GROUP_SIZE);
let (content, key) = find_key_for_target(&harness, TARGET_INDEX, width, true, "blocked").await;
let target = harness.test_node(TARGET_INDEX).expect("target");
let target_storage = target
.ant_protocol
.as_ref()
.expect("target protocol")
.storage();
let target_engine = target.replication_engine.as_ref().expect("target engine");
let target_peer = *target.p2p_node.as_ref().expect("target p2p").peer_id();
let holder = harness.test_node(HOLDER_INDEX).expect("holder");
let holder_peer = *holder.p2p_node.as_ref().expect("holder p2p").peer_id();
let holder_storage = holder
.ant_protocol
.as_ref()
.expect("holder protocol")
.storage();
holder_storage
.put(&key, &content)
.await
.expect("host the chunk");
let probe = b"local-write-guard-precondition-probe".to_vec();
let probe_address = ant_node::client::compute_address(&probe);
assert!(
target_storage.put(&probe_address, &probe).await.is_err(),
"the target's storage must be refusing writes, or this test proves nothing"
);
let served_before = holder_storage.stats().chunks_retrieved;
assert!(
target_engine
.enqueue_fetch_for_test(key, vec![holder_peer])
.await,
"candidate must enqueue"
);
let deadline = tokio::time::Instant::now() + OBSERVATION_WINDOW;
while target_engine.fetch_pipeline_contains_for_test(&key).await {
assert!(
tokio::time::Instant::now() < deadline,
"the candidate never resolved; a key stuck in the fetch pipeline \
would stall bootstrap drain (key {})",
hex::encode(key)
);
tokio::time::sleep(OBSERVATION_POLL).await;
}
tokio::time::sleep(Duration::from_secs(1)).await;
let served = holder_storage.stats().chunks_retrieved - served_before;
println!("LOCAL-WRITE-GUARD-RESULT served_by_holder={served}");
assert_eq!(
served, 0,
"a node whose storage cannot accept a write pulled {served} chunk(s) \
from the holder. The bytes could never have been stored, and the old \
classification treated the failure as the source's for retry-selection \
purposes, so the worker walked to every remaining holder and each \
re-sent the same chunk into the same full store."
);
assert!(
!target_storage.exists(&key).unwrap_or(true),
"the target cannot have stored a chunk its storage refuses to write"
);
let control = harness.test_node(CONTROL_INDEX).expect("control");
let control_storage = control
.ant_protocol
.as_ref()
.expect("control protocol")
.storage();
let control_engine = control.replication_engine.as_ref().expect("control engine");
let control_peer = *control.p2p_node.as_ref().expect("control p2p").peer_id();
let (blocked_content, blocked_key) =
find_key_for_target(&harness, TARGET_INDEX, width, true, "capblocked").await;
let (control_content, control_key) =
find_key_for_target(&harness, CONTROL_INDEX, width, true, "capcontrol").await;
holder_storage
.put(&blocked_key, &blocked_content)
.await
.expect("host the blocked key");
holder_storage
.put(&control_key, &control_content)
.await
.expect("host the control key");
watch_verification_requests_for_test(&[blocked_key, control_key]);
target_engine
.paid_list()
.insert(&blocked_key)
.await
.expect("seed target paid list");
control_engine
.paid_list()
.insert(&control_key)
.await
.expect("seed control paid list");
ensure_pending_verify(target_engine, blocked_key, holder_peer).await;
ensure_pending_verify(control_engine, control_key, holder_peer).await;
let deadline = tokio::time::Instant::now() + OBSERVATION_WINDOW;
let mut blocked_delay = None;
let mut control_stored = false;
while tokio::time::Instant::now() < deadline {
let delay = target_engine
.pending_verify_delay_for_test(&blocked_key)
.await;
if delay.is_some_and(|d| d >= CAPACITY_BACKOFF_FLOOR) {
blocked_delay = delay;
}
control_stored = control_storage.exists(&control_key).unwrap_or(false);
if blocked_delay.is_some() && control_stored {
break;
}
tokio::time::sleep(OBSERVATION_POLL).await;
}
let blocked_probes = verification_requests_for_key_from_for_test(&target_peer, &blocked_key);
let control_probes = verification_requests_for_key_from_for_test(&control_peer, &control_key);
println!(
"CAPACITY-GATE-RESULT blocked_probes={blocked_probes} control_probes={control_probes} \
blocked_delay={blocked_delay:?} control_stored={control_stored}"
);
assert_eq!(
blocked_probes, 0,
"the write-blocked node sent {blocked_probes} verification request(s) for a key \
its own capacity check will refuse. That probe is what scales with the owed \
set, and it is paid whether or not the dial behind it is stopped"
);
assert!(
control_probes > 0,
"the control asked nobody either, so the zero above is not evidence of \
anything — the cycle never reached the probe on either node"
);
assert!(
control_stored,
"the control never acquired its key, so the run says nothing about the gate"
);
assert!(
blocked_delay.is_some_and(|d| d >= CAPACITY_BACKOFF_FLOOR),
"the blocked key must be held on the capacity backoff, not merely skipped \
for one cycle: a skip without a deferral is re-selected on the next poll"
);
assert!(
!target_storage.exists(&blocked_key).unwrap_or(true),
"the target cannot have stored a chunk its storage refuses to write"
);
let (quorum_content, quorum_key) =
find_key_for_target(&harness, TARGET_INDEX, width, true, "capquorum").await;
for idx in 0..harness.node_count() {
if idx == TARGET_INDEX {
continue;
}
if let Some(node) = harness.test_node(idx) {
if let Some(protocol) = node.ant_protocol.as_ref() {
let _ = protocol.storage().put(&quorum_key, &quorum_content).await;
}
}
}
ensure_pending_verify(target_engine, quorum_key, holder_peer).await;
let deadline = tokio::time::Instant::now() + OBSERVATION_WINDOW;
let mut quorum_delay = None;
while tokio::time::Instant::now() < deadline {
let delay = target_engine
.pending_verify_delay_for_test(&quorum_key)
.await;
if delay.is_some_and(|d| d >= CAPACITY_BACKOFF_FLOOR) {
quorum_delay = delay;
break;
}
tokio::time::sleep(OBSERVATION_POLL).await;
}
let quorum_authorized = target_engine
.paid_list()
.contains(&quorum_key)
.unwrap_or(false);
println!(
"CAPACITY-GATE-PROMOTION-RESULT quorum_delay={quorum_delay:?} \
quorum_authorized={quorum_authorized}"
);
assert!(
quorum_authorized,
"the key was held without being authorized first: the quorum round did not \
reach the target's paid list, so the gate is in front of authorization \
rather than behind it (key {})",
hex::encode(quorum_key)
);
assert!(
quorum_delay.is_some(),
"the network-verified key was not held at the promotion gate. Either it was \
promoted and refused at the dial — which requeues on the 15 s schedule, not \
the capacity backoff — or the round never resolved (key {})",
hex::encode(quorum_key)
);
assert!(
!target_storage.exists(&quorum_key).unwrap_or(true),
"the target cannot have stored a chunk its storage refuses to write"
);
harness.teardown().await.expect("teardown");
}
#[tokio::test]
#[serial]
async fn already_held_key_is_not_fetched_again() {
let config = TestNetworkConfig {
node_count: NODE_COUNT,
replication_config: Some(ReplicationConfig {
close_group_size: CLOSE_GROUP_SIZE,
..ReplicationConfig::default()
}),
..TestNetworkConfig::default()
};
let harness = TestHarness::setup_with_config(config)
.await
.expect("setup possession-recheck network");
harness.warmup_dht().await.expect("warmup");
let width = storage_admission_width(CLOSE_GROUP_SIZE);
let (held_content, held_key) =
find_key_for_target(&harness, TARGET_INDEX, width, true, "held").await;
let (missing_content, missing_key) =
find_key_for_target(&harness, TARGET_INDEX, width, true, "missing").await;
let target = harness.test_node(TARGET_INDEX).expect("target");
let target_storage = target
.ant_protocol
.as_ref()
.expect("target protocol")
.storage();
let target_engine = target.replication_engine.as_ref().expect("target engine");
let holder = harness.test_node(HOLDER_INDEX).expect("holder");
let holder_peer = *holder.p2p_node.as_ref().expect("holder p2p").peer_id();
let holder_storage = holder
.ant_protocol
.as_ref()
.expect("holder protocol")
.storage();
holder_storage
.put(&held_key, &held_content)
.await
.expect("host held key");
holder_storage
.put(&missing_key, &missing_content)
.await
.expect("host control key");
target_storage
.put(&held_key, &held_content)
.await
.expect("target already holds the key");
let served_before = holder_storage.stats().chunks_retrieved;
assert!(
target_engine
.enqueue_fetch_for_test(held_key, vec![holder_peer])
.await,
"held-key candidate must enqueue"
);
let deadline = tokio::time::Instant::now() + OBSERVATION_WINDOW;
while target_engine
.fetch_pipeline_contains_for_test(&held_key)
.await
{
assert!(
tokio::time::Instant::now() < deadline,
"a candidate for an already-held key must leave the fetch pipeline"
);
tokio::time::sleep(OBSERVATION_POLL).await;
}
tokio::time::sleep(Duration::from_secs(1)).await;
let held_serves = holder_storage.stats().chunks_retrieved - served_before;
let served_before = holder_storage.stats().chunks_retrieved;
assert!(
target_engine
.enqueue_fetch_for_test(missing_key, vec![holder_peer])
.await,
"control candidate must enqueue"
);
let deadline = tokio::time::Instant::now() + OBSERVATION_WINDOW;
while !target_storage.exists(&missing_key).unwrap_or(false) {
assert!(
tokio::time::Instant::now() < deadline,
"REGRESSION: a key the target does not hold was not fetched — the \
possession recheck must not decline genuine work"
);
tokio::time::sleep(OBSERVATION_POLL).await;
}
let control_serves = holder_storage.stats().chunks_retrieved - served_before;
println!("POSSESSION-RESULT held_serves={held_serves} control_serves={control_serves}");
assert_eq!(
held_serves, 0,
"the target pulled {held_serves} chunk(s) for a key it already had"
);
assert!(
control_serves >= 1,
"the holder served nothing for a key the target lacks, so the \
zero-serve assertion above proves nothing"
);
harness.teardown().await.expect("teardown");
}