referrerpolicy=no-referrer-when-downgrade

sc_network_transactions/
config.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Configuration of the transaction protocol
20
21use futures::prelude::*;
22use sc_network::MAX_RESPONSE_SIZE;
23use sc_network_common::ExHashT;
24use sp_runtime::traits::Block as BlockT;
25use std::{collections::HashMap, future::Future, pin::Pin, sync::Arc, time};
26
27/// Interval at which we propagate transactions;
28pub(crate) const PROPAGATE_TIMEOUT: time::Duration = time::Duration::from_millis(2900);
29
30/// Maximum number of known transaction hashes to keep for a peer.
31///
32/// This should be approx. 2 blocks full of transactions for the network to function properly.
33pub(crate) const MAX_KNOWN_TRANSACTIONS: usize = 10240; // ~300kb per peer + overhead.
34
35/// Maximum allowed size for a transactions notification.
36pub(crate) const MAX_TRANSACTIONS_SIZE: u64 = MAX_RESPONSE_SIZE;
37
38/// Maximum number of transaction validation request we keep at any moment.
39pub(crate) const MAX_PENDING_TRANSACTIONS: usize = 8192;
40
41/// Result of the transaction import.
42#[derive(Clone, Copy, Debug)]
43pub enum TransactionImport {
44	/// Transaction is good but already known by the transaction pool.
45	KnownGood,
46	/// Transaction is good and not yet known.
47	NewGood,
48	/// Transaction is invalid.
49	Bad,
50	/// Transaction import was not performed.
51	None,
52}
53
54/// Future resolving to transaction import result.
55pub type TransactionImportFuture = Pin<Box<dyn Future<Output = TransactionImport> + Send>>;
56
57/// Transaction pool interface
58pub trait TransactionPool<H: ExHashT, B: BlockT>: Send + Sync {
59	/// Get transactions from the pool that are ready to be propagated.
60	fn transactions(&self) -> Vec<(H, Arc<B::Extrinsic>)>;
61	/// Get hash of transaction.
62	fn hash_of(&self, transaction: &B::Extrinsic) -> H;
63	/// Import a transaction into the pool.
64	///
65	/// This will return future.
66	fn import(&self, transaction: B::Extrinsic) -> TransactionImportFuture;
67	/// Notify the pool about transactions broadcast.
68	fn on_broadcasted(&self, propagations: HashMap<H, Vec<String>>);
69	/// Get transaction by hash.
70	fn transaction(&self, hash: &H) -> Option<Arc<B::Extrinsic>>;
71}
72
73/// Dummy implementation of the [`TransactionPool`] trait for a transaction pool that is always
74/// empty and discards all incoming transactions.
75///
76/// Requires the "hash" type to implement the `Default` trait.
77///
78/// Useful for testing purposes.
79pub struct EmptyTransactionPool;
80
81impl<H: ExHashT + Default, B: BlockT> TransactionPool<H, B> for EmptyTransactionPool {
82	fn transactions(&self) -> Vec<(H, Arc<B::Extrinsic>)> {
83		Vec::new()
84	}
85
86	fn hash_of(&self, _transaction: &B::Extrinsic) -> H {
87		Default::default()
88	}
89
90	fn import(&self, _transaction: B::Extrinsic) -> TransactionImportFuture {
91		Box::pin(future::ready(TransactionImport::KnownGood))
92	}
93
94	fn on_broadcasted(&self, _: HashMap<H, Vec<String>>) {}
95
96	fn transaction(&self, _h: &H) -> Option<Arc<B::Extrinsic>> {
97		None
98	}
99}