Skip to main content

soil_network/transactions/
config.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
6
7//! Configuration of the transaction protocol
8
9use futures::prelude::*;
10use soil_network::common::ExHashT;
11use soil_network::MAX_RESPONSE_SIZE;
12use std::{collections::HashMap, future::Future, pin::Pin, sync::Arc, time};
13use subsoil::runtime::traits::Block as BlockT;
14
15/// Interval at which we propagate transactions;
16pub(crate) const PROPAGATE_TIMEOUT: time::Duration = time::Duration::from_millis(2900);
17
18/// Maximum number of known transaction hashes to keep for a peer.
19///
20/// This should be approx. 2 blocks full of transactions for the network to function properly.
21pub(crate) const MAX_KNOWN_TRANSACTIONS: usize = 10240; // ~300kb per peer + overhead.
22
23/// Maximum allowed size for a transactions notification.
24pub(crate) const MAX_TRANSACTIONS_SIZE: u64 = MAX_RESPONSE_SIZE;
25
26/// Maximum number of transaction validation request we keep at any moment.
27pub(crate) const MAX_PENDING_TRANSACTIONS: usize = 8192;
28
29/// Result of the transaction import.
30#[derive(Clone, Copy, Debug)]
31pub enum TransactionImport {
32	/// Transaction is good but already known by the transaction pool.
33	KnownGood,
34	/// Transaction is good and not yet known.
35	NewGood,
36	/// Transaction is invalid.
37	Bad,
38	/// Transaction import was not performed.
39	None,
40}
41
42/// Future resolving to transaction import result.
43pub type TransactionImportFuture = Pin<Box<dyn Future<Output = TransactionImport> + Send>>;
44
45/// Transaction pool interface
46pub trait TransactionPool<H: ExHashT, B: BlockT>: Send + Sync {
47	/// Get transactions from the pool that are ready to be propagated.
48	fn transactions(&self) -> Vec<(H, Arc<B::Extrinsic>)>;
49	/// Get hash of transaction.
50	fn hash_of(&self, transaction: &B::Extrinsic) -> H;
51	/// Import a transaction into the pool.
52	///
53	/// This will return future.
54	fn import(&self, transaction: B::Extrinsic) -> TransactionImportFuture;
55	/// Notify the pool about transactions broadcast.
56	fn on_broadcasted(&self, propagations: HashMap<H, Vec<String>>);
57	/// Get transaction by hash.
58	fn transaction(&self, hash: &H) -> Option<Arc<B::Extrinsic>>;
59}
60
61/// Dummy implementation of the [`TransactionPool`] trait for a transaction pool that is always
62/// empty and discards all incoming transactions.
63///
64/// Requires the "hash" type to implement the `Default` trait.
65///
66/// Useful for testing purposes.
67pub struct EmptyTransactionPool;
68
69impl<H: ExHashT + Default, B: BlockT> TransactionPool<H, B> for EmptyTransactionPool {
70	fn transactions(&self) -> Vec<(H, Arc<B::Extrinsic>)> {
71		Vec::new()
72	}
73
74	fn hash_of(&self, _transaction: &B::Extrinsic) -> H {
75		Default::default()
76	}
77
78	fn import(&self, _transaction: B::Extrinsic) -> TransactionImportFuture {
79		Box::pin(future::ready(TransactionImport::KnownGood))
80	}
81
82	fn on_broadcasted(&self, _: HashMap<H, Vec<String>>) {}
83
84	fn transaction(&self, _h: &H) -> Option<Arc<B::Extrinsic>> {
85		None
86	}
87}