propagation: add a configurable stamp throttle and drop ignored sources

This commit is contained in:
DeFiDude committed 2026-11-10 19:19:20 -07:00
1 parent ed3ec7070c
commit ff3bf1031c
7 files changed
+153 -15

No files matched your search

+1 -1
View File
@@ -203,7 +203,7 @@ pub const PROPAGATION_COST_FLEX: f64 = 3.0;
pub const PEERING_COST: f64 = 18.0;
pub const MAX_PEERING_COST: f64 = 26.0;
pub const MAX_PEERS: usize = 20;
pub const PN_STAMP_THROTTLE: u64 = 180;
pub const PN_STAMP_THROTTLE: u64 = 300;
pub const AUTOPEER: bool = true;
pub const AUTOPEER_MAXDEPTH: usize = 4;
+26 -6
View File
@@ -46,6 +46,8 @@ pub struct PnInboundAdmissionConfig {
pub max_inbound_syncs: usize,
/// Accept propagation offers only from configured static peers.
pub from_static_only: bool,
/// Throttle installed after a known peer submits invalid stamps.
pub invalid_stamp_throttle: Duration,
}
impl Default for PnInboundAdmissionConfig {
@@ -55,6 +57,7 @@ impl Default for PnInboundAdmissionConfig {
static_sequential: false,
max_inbound_syncs: PN_DEFAULT_MAX_INBOUND_SYNCS,
from_static_only: false,
invalid_stamp_throttle: PN_INVALID_STAMP_THROTTLE,
}
}
}
@@ -535,7 +538,7 @@ impl PnInboundAdmission {
return Err(PnThrottleInstallError::UnknownPeerDestination);
}
let requested_deadline = now.saturating_add(PN_INVALID_STAMP_THROTTLE);
let requested_deadline = now.saturating_add(self.config.invalid_stamp_throttle);
let deadline = self
.throttle_deadlines
.entry(peer_destination_hash)
@@ -684,9 +687,9 @@ mod tests {
static_sequential: false,
max_inbound_syncs: 3,
from_static_only: false,
invalid_stamp_throttle: Duration::from_secs(300),
}
);
assert_eq!(PN_INVALID_STAMP_THROTTLE, Duration::from_secs(180));
let floored = PnInboundAdmission::new(PnInboundAdmissionConfig {
max_inbound_syncs: 0,
@@ -1469,15 +1472,15 @@ mod tests {
);
assert_eq!(
admission.throttle_deadline(&bad_destination, Duration::from_secs(100)),
Some(Duration::from_secs(280))
Some(Duration::from_secs(400))
);
assert!(matches!(
admission.preflight_offer(link(4), Some(bad_identity), Duration::from_millis(279_999),),
admission.preflight_offer(link(4), Some(bad_identity), Duration::from_millis(399_999),),
Err(PnOfferRejection::InvalidStampThrottle)
));
// No global or zero-key throttle is created for unrelated peers.
let unrelated_now = Duration::from_millis(279_999);
let unrelated_now = Duration::from_millis(399_999);
let unrelated = admission
.preflight_offer(link(5), Some(identity(5)), unrelated_now)
.unwrap();
@@ -1487,7 +1490,7 @@ mod tests {
);
assert!(!admission.is_peer_throttled(&[0; 16], unrelated_now));
let deadline = Duration::from_secs(280);
let deadline = Duration::from_secs(400);
let expired = admission
.preflight_offer(link(6), Some(bad_identity), deadline)
.unwrap();
@@ -1498,6 +1501,23 @@ mod tests {
assert_eq!(admission.throttle_count(), 0);
}
#[test]
fn invalid_stamp_throttle_lasts_the_configured_time() {
let throttle = Duration::from_secs(600);
let mut admission = PnInboundAdmission::new(PnInboundAdmissionConfig {
invalid_stamp_throttle: throttle,
..PnInboundAdmissionConfig::default()
});
let bad_identity = identity(0xA3);
let bad_destination = PnInboundAdmission::peer_destination_hash(&bad_identity);
let deadline = admission
.install_invalid_stamp_throttle(bad_destination, TEST_NOW)
.unwrap();
assert_eq!(deadline, TEST_NOW + throttle);
assert!(admission.is_peer_throttled(&bad_destination, deadline - Duration::from_secs(1)));
assert!(!admission.is_peer_throttled(&bad_destination, deadline));
}
#[test]
fn unsolicited_invalid_stamp_throttle_is_expiring_and_peer_scoped() {
let mut admission = PnInboundAdmission::default();
+30 -2
View File
@@ -55,6 +55,8 @@ pub enum SyncTaskState {
Complete,
/// No path to the peer arrived within the path request grace.
NoPath,
/// The peer answered the offer with `ERROR_THROTTLED`.
Throttled,
Failed,
}
@@ -63,6 +65,8 @@ pub enum PeerSyncTerminalState {
Complete,
/// The path request was not answered; no link was attempted.
NoPath,
/// The peer answered the offer with `ERROR_THROTTLED`.
Throttled,
Failed,
}
@@ -1106,6 +1110,7 @@ impl PropagationSyncTask {
self.record_handled_updates(&already_handled);
self.prepare_transfer_for_ids(&wanted_ids);
}
OfferResponse::ErrorThrottled => self.state = SyncTaskState::Throttled,
_ => {
self.state = SyncTaskState::Failed;
}
@@ -1189,7 +1194,10 @@ impl PropagationSyncTask {
if !self.pending_transport.is_empty()
&& !matches!(
self.state,
SyncTaskState::Complete | SyncTaskState::NoPath | SyncTaskState::Failed
SyncTaskState::Complete
| SyncTaskState::NoPath
| SyncTaskState::Throttled
| SyncTaskState::Failed
)
{
return;
@@ -1201,6 +1209,7 @@ impl PropagationSyncTask {
| SyncTaskState::PathRequested
| SyncTaskState::Complete
| SyncTaskState::NoPath
| SyncTaskState::Throttled
| SyncTaskState::Failed
) {
self.drive_link_watchdog();
@@ -1228,7 +1237,10 @@ impl PropagationSyncTask {
SyncTaskState::Transferring => {
self.drive_transfers();
}
SyncTaskState::Complete | SyncTaskState::NoPath | SyncTaskState::Failed => {
SyncTaskState::Complete
| SyncTaskState::NoPath
| SyncTaskState::Throttled
| SyncTaskState::Failed => {
if self.terminal_result.is_none()
&& let Some(peer_hash) = self.node_dest_hash
{
@@ -1238,6 +1250,7 @@ impl PropagationSyncTask {
state: match self.state {
SyncTaskState::Complete => PeerSyncTerminalState::Complete,
SyncTaskState::NoPath => PeerSyncTerminalState::NoPath,
SyncTaskState::Throttled => PeerSyncTerminalState::Throttled,
_ => PeerSyncTerminalState::Failed,
},
offer_generation: complete
@@ -3287,6 +3300,21 @@ mod tests {
assert_eq!(task.state, SyncTaskState::Failed);
}
#[test]
fn throttled_offer_response_ends_sync_as_throttled() {
let (tx, _rx) = mpsc::channel(16);
let mut task = test_task(tx, [0xAA; 16]);
task.set_node([0xBB; 16]);
task.state = SyncTaskState::AwaitingResponse;
task.handle_offer_response(OfferResponse::ErrorThrottled);
task.tick();
assert_eq!(
task.take_terminal_peer_result().unwrap().state,
PeerSyncTerminalState::Throttled
);
}
#[test]
fn test_handle_offer_response_want_all_no_storage() {
let (tx, _rx) = mpsc::channel(16);
+72 -4
View File
@@ -108,6 +108,8 @@ pub struct RouterConfigExt {
/// Name advertised in propagation announce metadata.
pub name: Option<String>,
pub from_static_only: bool,
/// Seconds a peer stays throttled after sending invalid propagation stamps.
pub pn_stamp_throttle: f64,
}
impl Default for RouterConfigExt {
@@ -125,6 +127,7 @@ impl Default for RouterConfigExt {
message_storage_limit: None,
name: None,
from_static_only: false,
pn_stamp_throttle: PN_STAMP_THROTTLE as f64,
}
}
}
@@ -1298,8 +1301,10 @@ impl LxmRouter {
/// receipt. Records both message and transient IDs; learns a ticket only from
/// a matching verified message with a committed/duplicate disposition.
/// Returns whether the new-commit delivery path ran; `false` also covers
/// duplicates, discards and mismatched reservations. The caller establishes
/// identity/node scope and durable effects before invoking this method.
/// duplicates, discards, ignored sources and mismatched reservations. An
/// ignored source records only the transient ID, not the message ID. The
/// caller establishes identity/node scope and durable effects before
/// invoking this method.
pub fn commit_inbound(
&mut self,
reservation: InboundReservation,
@@ -1325,6 +1330,10 @@ impl LxmRouter {
}
self.claim_locally_delivered(receipt.record().scope.transient_id);
self.claim_locally_processed(receipt.record().scope.transient_id);
// Python lxmf_delivery drops ignored sources before claiming the message id.
if self.is_ignored(&message.source_hash()) {
return false;
}
let new_message = !self
.propagation_store
.is_locally_delivered(&message.message_id());
@@ -1902,10 +1911,10 @@ impl LxmRouter {
}
}
/// Throttle a peer for [`PN_STAMP_THROTTLE`] seconds.
/// Throttle a peer for the configured `pn_stamp_throttle` seconds.
pub fn throttle_peer(&mut self, peer_hash: [u8; 16]) {
self.throttled_peers
.insert(peer_hash, now_f64() + PN_STAMP_THROTTLE as f64);
.insert(peer_hash, now_f64() + self.config.ext.pn_stamp_throttle);
}
/// Drop idle, non-static peers with the lowest acceptance rates.
@@ -3515,6 +3524,65 @@ mod tests {
assert!(!router.deliver_inbound(&direct, false));
}
#[test]
fn propagated_message_from_ignored_source_records_only_its_transient_id() {
use crate::admission::{
AdmissionRecord, AdmissionScope, DurableAdmissionReceipt, DurableDisposition,
};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
let mut router = LxmRouter::new(RouterConfig::default());
let deliveries = Arc::new(AtomicUsize::new(0));
let observed = Arc::clone(&deliveries);
router.register_delivery_callback(move |_| {
observed.fetch_add(1, Ordering::Relaxed);
});
let message = crate::message::test_message(
[0xAA; 16],
[0xBB; 16],
"ignored",
"message",
DeliveryMethod::Propagated,
);
router.ignore_destination([0xBB; 16]);
let temp = tempfile::tempdir().unwrap();
let store = crate::admission_store::FileAdmissionStore::open(
temp.path(),
&temp.path().join("messages"),
)
.unwrap();
let tid = [0x45; 32];
let record = AdmissionRecord {
scope: AdmissionScope {
identity: [1; 16],
node: [2; 16],
transient_id: tid,
},
message_id: message.message_id(),
received_wire_hash: rns_crypto::sha::full_hash(message.wire_bytes()),
stored_message_wire_hash: None,
disposition: DurableDisposition::Committed,
committed_at: 100,
};
let reservation = router.reserve_inbound(&message).unwrap();
let receipt =
DurableAdmissionReceipt::commit(store.prepare(record, message.wire_owned(), false))
.unwrap();
assert!(!router.commit_inbound(reservation, &message, &receipt, None));
assert_eq!(deliveries.load(Ordering::Relaxed), 0);
assert!(router.has_message(&tid));
assert!(router.propagation_store.is_locally_processed(&tid));
assert!(!router.has_message(&message.message_id()));
router.unignore_destination(&[0xBB; 16]);
let mut direct = message.clone();
direct.method = DeliveryMethod::Direct;
assert!(router.deliver_inbound(&direct, false));
assert_eq!(deliveries.load(Ordering::Relaxed), 1);
}
#[test]
fn test_ingest_lxm_uri() {
use rns_crypto::ed25519::Ed25519PrivateKey;
+6
View File
@@ -3854,6 +3854,12 @@ impl LxmdRunner {
peer.state = lxmf_core::constants::PeerState::Idle;
peer.alive = false;
}
lxmf_core::propagation_sync::PeerSyncTerminalState::Throttled => {
// LXMPeer.offer_response postpones by the class constant, not the configured throttle.
peer.state = lxmf_core::constants::PeerState::Idle;
peer.currently_transferring_messages = None;
peer.next_sync_attempt = now + lxmf_core::constants::PN_STAMP_THROTTLE as f64;
}
lxmf_core::propagation_sync::PeerSyncTerminalState::Failed => {
peer.sync_failed();
}
@@ -1187,6 +1187,7 @@ mod tests {
static_sequential: false,
max_inbound_syncs: 1,
from_static_only: true,
..PnInboundAdmissionConfig::default()
},
[peer_destination],
100,
@@ -1200,6 +1201,7 @@ mod tests {
static_sequential: true,
max_inbound_syncs: 1,
from_static_only: true,
..PnInboundAdmissionConfig::default()
},
[peer_destination],
100,
+16 -2
View File
@@ -38,6 +38,8 @@ pub struct PythonLxmdConfig {
pub propagation_stamp_cost_flexibility: f64,
pub peering_cost: f64,
pub remote_peering_cost_max: f64,
/// Seconds, floored at `PN_STAMP_THROTTLE` as Python does.
pub propagation_stamp_throttle: f64,
pub prioritised_lxmf_destinations: Vec<String>,
pub control_allowed_identities: Vec<String>,
pub static_peers: Vec<String>,
@@ -196,8 +198,11 @@ impl PythonLxmdConfig {
MAX_PEERING_COST,
)?;
// Parsed so a malformed value aborts as in Python; the throttle time itself is fixed.
get_cost(propagation, "propagation", "propagation_stamp_throttle")?;
let propagation_stamp_throttle =
get_cost(propagation, "propagation", "propagation_stamp_throttle")?
.map_or(PN_STAMP_THROTTLE as f64, |minutes| {
(minutes * 60.0).max(PN_STAMP_THROTTLE as f64)
});
let static_peers = get_list(propagation, "static_peers");
for peer in &static_peers {
parse_static_peer(peer)?;
@@ -275,6 +280,7 @@ impl PythonLxmdConfig {
propagation_stamp_cost_flexibility,
peering_cost,
remote_peering_cost_max,
propagation_stamp_throttle,
prioritised_lxmf_destinations: get_list(propagation, "prioritise_destinations"),
control_allowed_identities: get_list(propagation, "control_allowed"),
static_peers,
@@ -427,6 +433,8 @@ pub struct DaemonConfig {
pub enforce_stamps: bool,
pub message_storage_limit: Option<usize>,
pub from_static_only: bool,
/// Invalid-stamp peer throttle in seconds (`propagation_stamp_throttle`).
pub pn_stamp_throttle: f64,
/// Max accepted inbound delivery transfer size in decimal KB. Python reference:
/// `delivery_transfer_max_accepted_size` in `lxmd.py`.
pub delivery_transfer_max_accepted_size: f64,
@@ -464,6 +472,7 @@ impl Default for DaemonConfig {
enforce_stamps: false,
message_storage_limit: Some(500_000_000),
from_static_only: false,
pn_stamp_throttle: PN_STAMP_THROTTLE as f64,
// Current Python LXMF 1.1.0's standalone lxmd defaults
// direct-delivery Resource admission to 1 decimal KB when the
// setting is omitted. LXMRouter's library default is separate.
@@ -492,6 +501,7 @@ impl DaemonConfig {
message_storage_limit: self.message_storage_limit,
name: self.node_name.clone(),
from_static_only: self.from_static_only,
pn_stamp_throttle: self.pn_stamp_throttle,
..Default::default()
},
}
@@ -509,6 +519,8 @@ impl DaemonConfig {
static_sequential: !self.static_peers_bypass_sequential,
max_inbound_syncs: self.max_inbound_syncs,
from_static_only: self.from_static_only,
invalid_stamp_throttle: std::time::Duration::try_from_secs_f64(self.pn_stamp_throttle)
.unwrap_or(std::time::Duration::MAX),
}
}
@@ -550,6 +562,7 @@ impl DaemonConfig {
prioritise_destinations: py.prioritised_lxmf_destinations,
message_storage_limit: megabytes_to_bytes(py.message_storage_limit),
from_static_only: py.from_static_only,
pn_stamp_throttle: py.propagation_stamp_throttle,
delivery_transfer_max_accepted_size: py.delivery_transfer_max_accepted_size,
..DaemonConfig::default()
};
@@ -1081,6 +1094,7 @@ from_static_only = yes
static_sequential: true,
max_inbound_syncs: 5,
from_static_only: true,
invalid_stamp_throttle: std::time::Duration::from_secs(300),
}
);
assert_eq!(dc.static_peers, ["00112233445566778899aabbccddeeff"]);