sui_core/authority/
submitted_transaction_cache.rs

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
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use lru::LruCache;
use parking_lot::RwLock;
use prometheus::{
    register_histogram_with_registry, register_int_counter_with_registry,
    register_int_gauge_with_registry, Histogram, IntCounter, IntGauge, Registry,
};
use std::collections::BTreeSet;
use std::net::IpAddr;
use std::num::NonZeroUsize;
use std::sync::Arc;
use sui_types::digests::TransactionDigest;
use sui_types::traffic_control::Weight;
use tracing::debug;

pub(crate) const DEFAULT_CACHE_CAPACITY: usize = 100_000;

pub struct SubmittedTransactionCacheMetrics {
    pub transactions_tracked: IntGauge,
    pub spam_detected: IntCounter,
    pub submission_count_exceeded: Histogram,
    pub amplification_factor_distribution: Histogram,
}

impl SubmittedTransactionCacheMetrics {
    pub fn new(registry: &Registry) -> Self {
        Self {
            transactions_tracked: register_int_gauge_with_registry!(
                "submitted_transaction_cache_transactions_tracked",
                "Number of transactions currently tracked in the submission cache",
                registry,
            )
            .unwrap(),
            spam_detected: register_int_counter_with_registry!(
                "submitted_transaction_cache_spam_detected",
                "Number of transactions that exceeded submission limits",
                registry,
            )
            .unwrap(),
            submission_count_exceeded: register_histogram_with_registry!(
                "submitted_transaction_cache_submission_count_exceeded",
                "Distribution of submission counts when spam is detected",
                vec![
                    1.0, 2.0, 5.0, 10.0, 20.0, 50.0, 100.0, 200.0, 500.0, 1000.0, 2000.0, 5000.0,
                    10000.0,
                ],
                registry,
            )
            .unwrap(),
            amplification_factor_distribution: register_histogram_with_registry!(
                "submitted_transaction_cache_amplification_factor_distribution",
                "Distribution of amplification factors used for transaction submissions",
                vec![
                    1.0, 2.0, 5.0, 10.0, 20.0, 50.0, 100.0, 200.0, 500.0, 1000.0, 2000.0, 5000.0,
                    10000.0,
                ],
                registry,
            )
            .unwrap(),
        }
    }

    #[cfg(test)]
    pub(crate) fn new_test() -> Self {
        Self::new(&Registry::new())
    }
}

/// Cache for tracking submitted transactions to prevent DoS through excessive resubmissions.
/// Uses LRU eviction to automatically remove least recently used entries when at capacity.
/// Tracks submission counts and enforces gas-price-based amplification limits.
pub(crate) struct SubmittedTransactionCache {
    inner: RwLock<Inner>,
    metrics: Arc<SubmittedTransactionCacheMetrics>,
}

struct Inner {
    transactions: LruCache<TransactionDigest, SubmissionMetadata>,
}

#[derive(Debug, Clone)]
struct SubmissionMetadata {
    /// Number of times this transaction has been submitted
    submission_count: u32,
    /// Maximum allowed submissions based on gas price amplification
    max_allowed_submissions: u32,
    /// Set of client IP addresses that have submitted this transaction
    submitter_client_addrs: BTreeSet<IpAddr>,
}

impl SubmittedTransactionCache {
    pub(crate) fn new(
        cache_capacity: Option<usize>,
        metrics: Arc<SubmittedTransactionCacheMetrics>,
    ) -> Self {
        let capacity = cache_capacity
            .and_then(NonZeroUsize::new)
            .unwrap_or_else(|| NonZeroUsize::new(DEFAULT_CACHE_CAPACITY).unwrap());

        Self {
            inner: RwLock::new(Inner {
                transactions: LruCache::new(capacity),
            }),
            metrics,
        }
    }

    pub(crate) fn metrics(&self) -> Arc<SubmittedTransactionCacheMetrics> {
        self.metrics.clone()
    }

    pub(crate) fn record_submitted_tx(
        &self,
        digest: &TransactionDigest,
        amplification_factor: u32,
        submitter_client_addr: Option<IpAddr>,
    ) {
        let mut inner = self.inner.write();

        let max_allowed_submissions = amplification_factor;

        if let Some(metadata) = inner.transactions.get_mut(digest) {
            // Track additional client addresses for resubmissions
            if let Some(addr) = submitter_client_addr {
                if metadata.submitter_client_addrs.insert(addr) {
                    debug!("Added new client address {addr} for transaction {digest}");
                }
            }
            debug!("Transaction {digest} already tracked in submission cache");
        } else {
            // First time we're submitting this transaction, however we will wait till
            // we see the transaction in consensus output to increment the submission count.
            let submitter_client_addrs = submitter_client_addr.into_iter().collect();
            let metadata = SubmissionMetadata {
                submission_count: 0,
                max_allowed_submissions,
                submitter_client_addrs,
            };

            inner.transactions.put(*digest, metadata);

            self.metrics
                .transactions_tracked
                .set(inner.transactions.len() as i64);
            self.metrics
                .amplification_factor_distribution
                .observe(amplification_factor as f64);

            debug!(
                "First submission of transaction {digest} (max_allowed: {max_allowed_submissions})",
            );
        }
    }

    /// Increments the submission count when we see a transaction in consensus output.
    /// This tracks how many times the transaction has appeared in consensus (from any validator).
    /// Returns the spam weight and set of submitter client addresses if the transaction exceeds allowed submissions.
    pub(crate) fn increment_submission_count(
        &self,
        digest: &TransactionDigest,
    ) -> Option<(Weight, BTreeSet<IpAddr>)> {
        let mut inner = self.inner.write();

        if let Some(metadata) = inner.transactions.get_mut(digest) {
            metadata.submission_count += 1;

            if metadata.submission_count > metadata.max_allowed_submissions {
                let spam_weight = Weight::one();
                self.metrics.spam_detected.inc();
                self.metrics
                    .submission_count_exceeded
                    .observe(metadata.submission_count as f64);

                debug!(
                    "Transaction {} seen in consensus {} times, exceeds limit {} (spam_weight: {:?})",
                    digest,
                    metadata.submission_count,
                    metadata.max_allowed_submissions,
                    spam_weight
                );

                return Some((spam_weight, metadata.submitter_client_addrs.clone()));
            }
        }
        // If we don't know about this transaction, it was submitted by another validator
        // We don't track spam weight for transactions we didn't submit
        None
    }

    #[cfg(test)]
    pub(crate) fn contains(&self, digest: &TransactionDigest) -> bool {
        self.inner.read().transactions.contains(digest)
    }

    #[cfg(test)]
    pub(crate) fn get_submission_count(&self, digest: &TransactionDigest) -> Option<u32> {
        self.inner
            .read()
            .transactions
            .peek(digest)
            .map(|m| m.submission_count)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::net::{IpAddr, Ipv4Addr};

    fn create_test_digest(val: u8) -> TransactionDigest {
        let mut bytes = [0u8; 32];
        bytes[0] = val;
        TransactionDigest::new(bytes)
    }

    #[test]
    fn test_first_submission_allowed() {
        let cache = SubmittedTransactionCache::new(
            None,
            Arc::new(SubmittedTransactionCacheMetrics::new_test()),
        );
        let digest = create_test_digest(1);

        cache.record_submitted_tx(&digest, 1, None);
        assert!(cache.contains(&digest));
        assert_eq!(cache.get_submission_count(&digest), Some(0));

        let spam_weight = cache.increment_submission_count(&digest);
        assert_eq!(spam_weight, None);
        assert_eq!(cache.get_submission_count(&digest), Some(1));
    }

    #[test]
    fn test_amplification_factor() {
        let cache = SubmittedTransactionCache::new(
            None,
            Arc::new(SubmittedTransactionCacheMetrics::new_test()),
        );
        let digest = create_test_digest(1);

        // Record with amplification_factor=5, should allow 5 submissions
        cache.record_submitted_tx(&digest, 5, None);

        // Should allow 5 submissions
        for i in 0..5 {
            let spam_weight = cache.increment_submission_count(&digest);
            assert_eq!(spam_weight, None, "Submission {} should be allowed", i + 1);
        }

        // 6th submission should trigger spam weight
        let spam_weight = cache.increment_submission_count(&digest);
        assert_eq!(spam_weight.map(|(w, _)| w), Some(Weight::one()));

        // Additional submissions should also trigger spam weight
        for i in 6..10 {
            let spam_weight = cache.increment_submission_count(&digest);
            assert_eq!(
                spam_weight.map(|(w, _)| w),
                Some(Weight::one()),
                "Submission {} should trigger spam weight",
                i + 1
            );
        }
    }

    #[test]
    fn test_lru_eviction() {
        // Create a cache with capacity for only 3 transactions
        let cache = SubmittedTransactionCache::new(
            Some(3),
            Arc::new(SubmittedTransactionCacheMetrics::new_test()),
        );

        // Add 3 transactions
        for i in 1..=3 {
            let digest = create_test_digest(i);
            cache.record_submitted_tx(&digest, 1, None);
        }

        // Verify all 3 transactions are in cache
        for i in 1..=3 {
            let digest = create_test_digest(i);
            assert!(cache.contains(&digest));
        }

        // Add a 4th transaction, which should evict the least recently used (digest 1)
        let digest4 = create_test_digest(4);
        cache.record_submitted_tx(&digest4, 1, None);

        // Transaction 1 should be evicted (least recently used)
        assert!(!cache.contains(&create_test_digest(1)));
        // Transactions 2, 3, and 4 should still be in cache
        assert!(cache.contains(&create_test_digest(2)));
        assert!(cache.contains(&create_test_digest(3)));
        assert!(cache.contains(&digest4));
    }

    #[test]
    fn test_lru_access_updates_position() {
        // Create a cache with capacity for only 3 transactions
        let cache = SubmittedTransactionCache::new(
            Some(3),
            Arc::new(SubmittedTransactionCacheMetrics::new_test()),
        );

        // Add 3 transactions
        for i in 1..=3 {
            let digest = create_test_digest(i);
            cache.record_submitted_tx(&digest, 1, None);
        }

        // Access transaction 1 (moves it to front of LRU)
        let digest1 = create_test_digest(1);
        cache.increment_submission_count(&digest1);

        // Add a 4th transaction, which should now evict transaction 2 (now least recently used)
        let digest4 = create_test_digest(4);
        cache.record_submitted_tx(&digest4, 1, None);

        // Transaction 2 should be evicted
        assert!(!cache.contains(&create_test_digest(2)));
        // Transactions 1, 3, and 4 should still be in cache
        assert!(cache.contains(&digest1));
        assert!(cache.contains(&create_test_digest(3)));
        assert!(cache.contains(&digest4));
    }

    #[test]
    fn test_multiple_client_addresses() {
        let cache = SubmittedTransactionCache::new(
            None,
            Arc::new(SubmittedTransactionCacheMetrics::new_test()),
        );
        let digest = create_test_digest(1);
        let addr1 = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
        let addr2 = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 2));
        let addr3 = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 3));

        // First submission with addr1
        cache.record_submitted_tx(&digest, 2, Some(addr1));

        // Resubmission with addr2 - should track both addresses
        cache.record_submitted_tx(&digest, 2, Some(addr2));

        // Resubmission with addr1 again - should not duplicate
        cache.record_submitted_tx(&digest, 2, Some(addr1));

        // Resubmission with addr3 - should track all three
        cache.record_submitted_tx(&digest, 2, Some(addr3));

        // Increment submission count twice to exceed limit
        cache.increment_submission_count(&digest);
        cache.increment_submission_count(&digest);

        // Third submission should trigger spam weight for all addresses
        let result = cache.increment_submission_count(&digest);
        assert!(result.is_some());

        let (spam_weight, addrs) = result.unwrap();
        assert_eq!(spam_weight, Weight::one());
        assert_eq!(addrs.len(), 3);
        assert!(addrs.contains(&addr1));
        assert!(addrs.contains(&addr2));
        assert!(addrs.contains(&addr3));
    }

    #[test]
    fn test_retry_tracking() {
        // Create a cache with capacity for only 3 transactions
        let cache = SubmittedTransactionCache::new(
            Some(3),
            Arc::new(SubmittedTransactionCacheMetrics::new_test()),
        );
        let digest1 = create_test_digest(1);
        let digest2 = create_test_digest(2);
        let digest3 = create_test_digest(3);
        let digest4 = create_test_digest(4);

        // Add 3 transactions
        cache.record_submitted_tx(&digest1, 1, None);
        cache.record_submitted_tx(&digest2, 1, None);
        cache.record_submitted_tx(&digest3, 1, None);

        // Verify all 3 transactions are in cache
        assert!(cache.contains(&digest1));
        assert!(cache.contains(&digest2));
        assert!(cache.contains(&digest3));

        // Retry digest1 - this should move it to the front of LRU
        cache.record_submitted_tx(&digest1, 1, None);

        // Add a 4th transaction, which should evict the least recently used (digest2)
        cache.record_submitted_tx(&digest4, 1, None);

        // digest1 should still be in cache (moved to front by retry)
        assert!(cache.contains(&digest1));
        // digest2 should be evicted (was least recently used)
        assert!(!cache.contains(&digest2));
        // digest3 and digest4 should still be in cache
        assert!(cache.contains(&digest3));
        assert!(cache.contains(&digest4));
    }
}