soil-txpool 0.2.0

Soil transaction pool implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
// This file is part of Soil.

// Copyright (C) Soil contributors.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

//! Logging helper. Sliding window statistics with retention-based pruning.
//!
//! `SlidingStats<T>` tracks timestamped values and computes statistical summaries
//! (min, max, average, percentiles, count) over a rolling time window.
//!
//! Old entries are automatically pruned based on a configurable retention `Duration`.
//! Values can be logged periodically using `insert_with_log` or the `insert_and_log_throttled!`
//! macro.

use std::{
	collections::{BTreeSet, HashMap, VecDeque},
	fmt::Display,
	sync::Arc,
	time::{Duration, Instant},
};
use tokio::sync::RwLock;

mod sealed {
	pub trait HasDefaultStatFormatter {}
}

impl sealed::HasDefaultStatFormatter for u32 {}
impl sealed::HasDefaultStatFormatter for i64 {}

pub trait StatFormatter {
	fn format_stat(value: f64) -> String;
}

impl<T> StatFormatter for T
where
	T: Display + sealed::HasDefaultStatFormatter,
{
	fn format_stat(value: f64) -> String {
		format!("{value:.2}")
	}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct StatDuration(pub std::time::Duration);

impl Into<f64> for StatDuration {
	fn into(self) -> f64 {
		self.0.as_secs_f64()
	}
}

impl Into<StatDuration> for Duration {
	fn into(self) -> StatDuration {
		StatDuration(self)
	}
}

impl std::fmt::Display for StatDuration {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(f, "{:?}", self.0)
	}
}

impl StatFormatter for StatDuration {
	fn format_stat(value: f64) -> String {
		format!("{:?}", Duration::from_secs_f64(value))
	}
}

/// Sliding window statistics collector.
///
/// `SlidingStats<T>` maintains a rolling buffer of values with timestamps,
/// automatically pruning values older than the configured `retention` period.
/// It provides percentile queries (e.g., p50, p95), min/max, average, and count.
pub struct SlidingStats<T> {
	inner: Arc<RwLock<Inner<T>>>,
}

/// Sync version of `SlidingStats`
pub struct SyncSlidingStats<T> {
	inner: Arc<parking_lot::RwLock<Inner<T>>>,
}

/// A type alias for `SlidingStats` specialized for durations with human-readable formatting.
///
/// Wraps `std::time::Duration` values using `StatDuration`, allowing for statistical summaries
/// (e.g. p50, p95, average) to be displayed in units like nanoseconds, milliseconds, or seconds.
pub type DurationSlidingStats = SlidingStats<StatDuration>;

/// Sync version of `DurationSlidingStats`
pub type SyncDurationSlidingStats = SyncSlidingStats<StatDuration>;

/// Internal state of the statistics buffer.
pub struct Inner<T> {
	/// How long to retain items after insertion.
	retention: Duration,

	/// Counter to assign unique ids to each entry.
	next_id: usize,

	/// Maps id to actual value + timestamp.
	entries: HashMap<usize, Entry<T>>,

	/// Queue of IDs in insertion order for expiration.
	by_time: VecDeque<usize>,

	/// Set of values with ids, ordered by value.
	by_value: BTreeSet<(T, usize)>,

	/// The time stamp of most recent insertion with log.
	///
	/// Used to throttle debug messages.
	last_log: Option<Instant>,
}

impl<T> Default for Inner<T> {
	fn default() -> Self {
		Self {
			retention: Default::default(),
			next_id: Default::default(),
			entries: Default::default(),
			by_time: Default::default(),
			by_value: Default::default(),
			last_log: None,
		}
	}
}

impl<T> Display for Inner<T>
where
	T: Ord + Copy + Into<f64> + std::fmt::Display + StatFormatter,
{
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		let mut parts = Vec::new();

		parts.push(format!("count={}", self.count()));
		if let Some(min) = self.min() {
			parts.push(format!("min={}", min));
		}
		if let Some(max) = self.max() {
			parts.push(format!("max={}", max));
		}
		if let Some(avg) = self.avg() {
			parts.push(format!("avg={}", <T as StatFormatter>::format_stat(avg)));
		}

		for p in [50, 90, 95, 99] {
			let val = self.percentile(p);
			if val.is_finite() {
				parts.push(format!("p{}={}", p, <T as StatFormatter>::format_stat(val)));
			}
		}
		parts.push(format!("span={:?}", self.retention));
		write!(f, "{}", parts.join(", "))
	}
}

/// A value inserted into the buffer, along with its insertion time.
#[derive(Clone, Copy)]
struct Entry<T> {
	timestamp: Instant,
	value: T,
}

impl<T> SlidingStats<T>
where
	T: Ord + Copy,
{
	/// Creates a new `SlidingStats` with the given retention duration.
	pub fn new(retention: Duration) -> Self {
		Self { inner: Arc::new(RwLock::new(Inner { retention, ..Default::default() })) }
	}

	/// Inserts a value into the buffer, timestamped with `Instant::now()`.
	///
	/// May trigger pruning of old items.
	#[cfg(test)]
	pub async fn insert(&self, value: T) {
		self.inner.write().await.insert(value)
	}

	/// Inserts a value into the buffer with provided timestamp.
	///
	/// May trigger pruning of old items.
	#[cfg(test)]
	pub async fn insert_using_timestamp(&self, value: T, now: Instant) {
		self.inner.write().await.insert_using_timestamp(value, now)
	}

	#[cfg(test)]
	pub async fn len(&self) -> usize {
		self.inner.read().await.len()
	}

	/// Grants temporary read-only access to the locked inner structure,
	/// passing it into the provided closure.
	///
	/// Intended to dump stats and prune inner based on current timestamp.
	#[cfg(test)]
	pub async fn with_inner<R>(&self, f: impl FnOnce(&mut Inner<T>) -> R) -> R {
		let mut guard = self.inner.write().await;
		f(&mut *guard)
	}
}

impl<T> SyncSlidingStats<T>
where
	T: Ord + Copy,
{
	/// Creates a new `SlidingStats` with the given retention duration.
	pub fn new(retention: Duration) -> Self {
		Self {
			inner: Arc::new(parking_lot::RwLock::new(Inner { retention, ..Default::default() })),
		}
	}
}

impl<T> SlidingStats<T>
where
	T: Ord + Copy + Into<f64> + std::fmt::Display + StatFormatter,
{
	/// Inserts a value and optionally returns a formatted log string of the current stats.
	///
	/// If enough time has passed since the last log (determined by `log_interval` or retention),
	/// this method returns `Some(log_string)`, otherwise it returns `None`.
	///
	/// This method performs:
	/// - Automatic pruning of expired entries
	/// - Throttling via `last_log` timestamp
	///
	///  Note: The newly inserted value may not  be included in the returned summary.
	pub async fn insert_with_log(
		&self,
		value: T,
		log_interval: Option<Duration>,
		now: Instant,
	) -> Option<String> {
		let mut inner = self.inner.write().await;
		inner.insert_with_log(value, log_interval, now)
	}
}

impl<T> SyncSlidingStats<T>
where
	T: Ord + Copy + Into<f64> + std::fmt::Display + StatFormatter,
{
	pub fn insert_with_log(
		&self,
		value: T,
		log_interval: Option<Duration>,
		now: Instant,
	) -> Option<String> {
		let mut inner = self.inner.write();
		inner.insert_with_log(value, log_interval, now)
	}
}

impl<T> Inner<T>
where
	T: Ord + Copy,
{
	#[cfg(test)]
	fn insert(&mut self, value: T) {
		self.insert_using_timestamp(value, Instant::now())
	}

	/// Refer to [`SlidingStats::insert_using_timestamp`]
	fn insert_using_timestamp(&mut self, value: T, now: Instant) {
		let id = self.next_id;
		self.next_id += 1;

		let entry = Entry { timestamp: now, value };

		self.entries.insert(id, entry);
		self.by_time.push_back(id);
		self.by_value.insert((value, id));

		self.prune(now);
	}

	/// Returns the minimum value in the current window.
	pub fn min(&self) -> Option<T> {
		self.by_value.first().map(|(v, _)| *v)
	}

	/// Returns the maximum value in the current window.
	pub fn max(&self) -> Option<T> {
		self.by_value.last().map(|(v, _)| *v)
	}

	/// Returns the number of items currently retained.
	pub fn count(&self) -> usize {
		self.len()
	}

	/// Explicitly prunes expired items from the buffer.
	///
	/// This is also called automatically during insertions.
	pub fn prune(&mut self, now: Instant) {
		let cutoff = now - self.retention;

		while let Some(&oldest_id) = self.by_time.front() {
			let expired = match self.entries.get(&oldest_id) {
				Some(entry) => entry.timestamp < cutoff,
				None => {
					debug_assert!(false);
					true
				},
			};

			if !expired {
				break;
			}

			if let Some(entry) = self.entries.remove(&oldest_id) {
				self.by_value.remove(&(entry.value, oldest_id));
			} else {
				debug_assert!(false);
			}
			self.by_time.pop_front();
		}
	}

	pub fn len(&self) -> usize {
		debug_assert_eq!(self.entries.len(), self.by_time.len());
		debug_assert_eq!(self.entries.len(), self.by_value.len());
		self.entries.len()
	}
}

impl<T> Inner<T>
where
	T: Ord + Copy + Into<f64>,
{
	/// Returns the average (mean) of values in the current window.
	pub fn avg(&self) -> Option<f64> {
		let len = self.len();
		if len == 0 {
			None
		} else {
			Some(self.entries.values().map(|e| e.value.into()).sum::<f64>() / len as f64)
		}
	}

	/// Returns the value at the given percentile (e.g., 0.5 for p50).
	///
	/// Returns `None` if the buffer is empty.
	// note: copied from: https://docs.rs/statrs/0.18.0/src/statrs/statistics/slice_statistics.rs.html#164-182
	pub fn percentile(&self, percentile: usize) -> f64 {
		if self.len() == 0 || percentile > 100 {
			return f64::NAN;
		}

		let tau = percentile as f64 / 100.0;
		let len = self.len();

		let h = (len as f64 + 1.0 / 3.0) * tau + 1.0 / 3.0;
		let hf = h as i64;

		if hf <= 0 || percentile == 0 {
			return self.min().map(|v| v.into()).unwrap_or(f64::NAN);
		}

		if hf >= len as i64 || percentile == 100 {
			return self.max().map(|v| v.into()).unwrap_or(f64::NAN);
		}

		let mut iter = self.by_value.iter().map(|(v, _)| (*v).into());

		let a = iter.nth((hf as usize).saturating_sub(1)).unwrap_or(f64::NAN);
		let b = iter.next().unwrap_or(f64::NAN);

		a + (h - hf as f64) * (b - a)
	}
}

impl<T> Inner<T>
where
	T: Ord + Copy + Into<f64> + std::fmt::Display + StatFormatter,
{
	/// Refer to [`SlidingStats::insert_with_log`]
	pub fn insert_with_log(
		&mut self,
		value: T,
		log_interval: Option<Duration>,
		now: Instant,
	) -> Option<String> {
		let Some(last_log) = self.last_log else {
			self.last_log = Some(now);
			self.insert_using_timestamp(value, now);
			return None;
		};

		let log_interval = log_interval.unwrap_or(self.retention);
		let should_log = now.duration_since(last_log) >= log_interval;
		let result = should_log.then(|| {
			self.last_log = Some(now);
			format!("{self}")
		});
		self.insert_using_timestamp(value, now);
		result
	}
}

impl<T> Clone for SlidingStats<T> {
	fn clone(&self) -> Self {
		Self { inner: Arc::clone(&self.inner) }
	}
}

impl<T> Clone for SyncSlidingStats<T> {
	fn clone(&self) -> Self {
		Self { inner: Arc::clone(&self.inner) }
	}
}

/// Inserts a value into a `SlidingStats` and conditionally logs the current stats using `tracing`.
///
/// This macro inserts the given `$value` into the `$stats` collector only if tracing is enabled
/// for the given `$target` and `$level`. The log will be emiited only if enough time has passed
/// since the last logged output (as tracked by the internal last_log timestamp).
///
/// The macro respects throttling: stats will not be logged more frequently than either the
/// explicitly provided `log_interval` or the stats' retention period (if no interval is given).
///
/// Note that:
/// - Logging is skipped unless `tracing::enabled!` returns true for the target and level.
/// - All entries older than the retention period will be logged and pruned,
/// - The newly inserted value may not be included in the logged statistics output (it is inserted
///   *after* the log decision).
#[macro_export]
macro_rules! insert_and_log_throttled {
    (
        $level:expr,
        target: $target:expr,
        log_interval: $log_interval:expr,
        prefix: $prefix:expr,
        $stats:expr,
        $value:expr
    ) => {{
        if tracing::enabled!(target: $target, $level) {
            let now = Instant::now();
            if let Some(msg) = $stats.insert_with_log($value, Some($log_interval), now).await {
                tracing::event!(target: $target, $level, "{}: {}", $prefix, msg);
            }
        }
    }};

    (
        $level:expr,
        target: $target:expr,
        prefix: $prefix:expr,
        $stats:expr,
        $value:expr
    ) => {{
        if tracing::enabled!(target: $target, $level) {
            let now = std::time::Instant::now();
            if let Some(msg) = $stats.insert_with_log($value, None, now).await {
                tracing::event!(target: $target, $level, "{}: {}", $prefix, msg);
            }
        }
    }};
}

/// Sync version of `insert_and_log_throttled`
#[macro_export]
macro_rules! insert_and_log_throttled_sync {
    (
        $level:expr,
        target: $target:literal,
        prefix: $prefix:expr,
        $stats:expr,
        $value:expr
    ) => {{
        if tracing::enabled!(target: $target, $level) {
            let now = std::time::Instant::now();
            if let Some(msg) = $stats.insert_with_log($value, None, now){
                tracing::event!(target: $target, $level, "{}: {}", $prefix, msg);
            }
        }
    }};
}

#[cfg(test)]
mod test {
	use super::*;
	use std::time::{Duration, Instant};

	#[tokio::test]
	async fn retention_prunes_old_items() {
		let stats = SlidingStats::<u64>::new(Duration::from_secs(10));

		let base = Instant::now();
		for i in 0..5 {
			stats.insert_using_timestamp(i * 10, base + Duration::from_secs(i * 5)).await;
		}
		assert_eq!(stats.len().await, 3);

		stats.insert_using_timestamp(999, base + Duration::from_secs(26)).await;

		assert_eq!(stats.len().await, 2);
	}

	#[tokio::test]
	async fn retention_prunes_old_items2() {
		let stats = SlidingStats::<u64>::new(Duration::from_secs(10));

		let base = Instant::now();
		for i in 0..100 {
			stats.insert_using_timestamp(i * 10, base + Duration::from_secs(5)).await;
		}
		assert_eq!(stats.len().await, 100);

		stats.insert_using_timestamp(999, base + Duration::from_secs(16)).await;

		let len = stats.len().await;
		assert_eq!(len, 1);
	}

	#[tokio::test]
	async fn insert_with_log_message_contains_all_old_items() {
		let stats = SlidingStats::<u32>::new(Duration::from_secs(100));

		let base = Instant::now();
		for _ in 0..10 {
			stats.insert_with_log(1, None, base + Duration::from_secs(5)).await;
		}
		assert_eq!(stats.len().await, 10);

		let output = stats.insert_with_log(1, None, base + Duration::from_secs(200)).await.unwrap();
		assert!(output.contains("count=10"));

		let len = stats.len().await;
		assert_eq!(len, 1);
	}

	#[tokio::test]
	async fn insert_with_log_message_prunes_all_old_items() {
		let stats = SlidingStats::<u32>::new(Duration::from_secs(25));

		let base = Instant::now();
		for i in 0..10 {
			stats.insert_with_log(1, None, base + Duration::from_secs(i * 5)).await;
		}
		assert_eq!(stats.len().await, 6);

		let output = stats.insert_with_log(1, None, base + Duration::from_secs(200)).await.unwrap();
		assert!(output.contains("count=6"));

		let len = stats.len().await;
		assert_eq!(len, 1);
	}

	#[tokio::test]
	async fn test_avg_min_max() {
		let stats = SlidingStats::<u32>::new(Duration::from_secs(100));
		let base = Instant::now();

		stats.insert_using_timestamp(10, base).await;
		stats.insert_using_timestamp(20, base + Duration::from_secs(1)).await;
		stats.insert_using_timestamp(30, base + Duration::from_secs(2)).await;

		stats
			.with_inner(|inner| {
				assert_eq!(inner.count(), 3);
				assert_eq!(inner.avg(), Some(20.0));
				assert_eq!(inner.min(), Some(10));
				assert_eq!(inner.max(), Some(30));
			})
			.await;
	}

	#[tokio::test]
	async fn duration_format() {
		let stats = SlidingStats::<StatDuration>::new(Duration::from_secs(100));
		stats.insert(Duration::from_nanos(100).into()).await;
		let output = stats.with_inner(|i| format!("{i}")).await;
		assert!(output.contains("max=100ns"));

		let stats = SlidingStats::<StatDuration>::new(Duration::from_secs(100));
		stats.insert(Duration::from_micros(100).into()).await;
		let output = stats.with_inner(|i| format!("{i}")).await;
		assert!(output.contains("max=100µs"));

		let stats = SlidingStats::<StatDuration>::new(Duration::from_secs(100));
		stats.insert(Duration::from_millis(100).into()).await;
		let output = stats.with_inner(|i| format!("{i}")).await;
		assert!(output.contains("max=100ms"));

		let stats = SlidingStats::<StatDuration>::new(Duration::from_secs(100));
		stats.insert(Duration::from_secs(100).into()).await;
		let output = stats.with_inner(|i| format!("{i}")).await;
		assert!(output.contains("max=100s"));

		let stats = SlidingStats::<StatDuration>::new(Duration::from_secs(100));
		stats.insert(Duration::from_nanos(100).into()).await;
		stats.insert(Duration::from_micros(100).into()).await;
		stats.insert(Duration::from_millis(100).into()).await;
		stats.insert(Duration::from_secs(100).into()).await;
		let output = stats.with_inner(|i| format!("{i}")).await;
		println!("{output}");
		assert_eq!(output, "count=4, min=100ns, max=100s, avg=25.025025025s, p50=50.05ms, p90=100s, p95=100s, p99=100s, span=100s");
	}
}