subcoin_mempool/
options.rs1use std::time::Duration;
2
3const DEFAULT_MAX_MEMPOOL_SIZE_MB: usize = 300;
5
6const DEFAULT_MEMPOOL_EXPIRY_HOURS: u32 = 336;
8
9#[allow(dead_code)]
11const DEFAULT_ACCEPT_NON_STD_TXN: bool = false;
12
13pub const MAX_OP_RETURN_RELAY: usize = 83;
16
17#[derive(Clone, Debug)]
19pub struct MemPoolLimits {
20 pub max_ancestors: usize,
22
23 pub max_ancestor_size_vbytes: usize,
25
26 pub max_descendants: usize,
28
29 pub max_descendant_size_vbytes: usize,
31}
32
33impl Default for MemPoolLimits {
34 fn default() -> Self {
35 Self {
36 max_ancestors: 25,
37 max_ancestor_size_vbytes: 101_000,
38 max_descendants: 25,
39 max_descendant_size_vbytes: 101_000,
40 }
41 }
42}
43
44#[derive(Clone, Debug)]
46pub struct MemPoolOptions {
47 pub max_size_bytes: usize,
49
50 pub expiry_seconds: u32,
52
53 pub min_relay_feerate: u64,
55
56 pub dust_relay_feerate: u64,
58
59 pub max_datacarrier_bytes: usize,
65
66 pub permit_bare_multisig: bool,
68
69 pub require_standard: bool,
71
72 pub max_script_cache_size: usize,
74
75 pub limits: MemPoolLimits,
76
77 pub enable_rbf: bool,
79
80 pub max_replacement_txs: usize,
82
83 pub enable_package_relay: bool,
85
86 pub max_package_count: usize,
88
89 pub max_package_vsize: u64,
91}
92
93impl Default for MemPoolOptions {
94 fn default() -> Self {
95 Self {
96 max_size_bytes: DEFAULT_MAX_MEMPOOL_SIZE_MB * 1_000_000,
97 expiry_seconds: DEFAULT_MEMPOOL_EXPIRY_HOURS * 60 * 60, min_relay_feerate: 1000, dust_relay_feerate: 1000, max_datacarrier_bytes: MAX_OP_RETURN_RELAY,
101 permit_bare_multisig: true,
102 require_standard: true,
103 max_script_cache_size: 32 << 20, limits: MemPoolLimits::default(),
105 enable_rbf: true,
106 max_replacement_txs: 100,
107 enable_package_relay: true,
108 max_package_count: 25,
109 max_package_vsize: 101_000,
110 }
111 }
112}
113
114impl MemPoolOptions {
115 pub fn new() -> Self {
117 Self::default()
118 }
119
120 pub fn builder() -> MemPoolOptionsBuilder {
122 MemPoolOptionsBuilder::default()
123 }
124
125 pub fn max_size_bytes(&self) -> usize {
127 self.max_size_bytes * 1_000_000
128 }
129
130 pub fn expiry_duration(&self) -> Duration {
132 Duration::from_secs(self.expiry_seconds as u64 * 3600)
133 }
134
135 pub fn max_ancestors(&self) -> usize {
137 self.limits.max_ancestors
138 }
139
140 pub fn max_ancestor_size(&self) -> i64 {
142 self.limits.max_ancestor_size_vbytes as i64
143 }
144
145 pub fn max_descendants(&self) -> u64 {
147 self.limits.max_descendants as u64
148 }
149
150 pub fn max_descendant_size(&self) -> i64 {
152 self.limits.max_descendant_size_vbytes as i64
153 }
154
155 pub fn min_relay_fee_rate(&self) -> crate::types::FeeRate {
157 crate::types::FeeRate::from_sat_per_kvb(self.min_relay_feerate)
158 }
159}
160
161#[derive(Default)]
163pub struct MemPoolOptionsBuilder {
164 options: MemPoolOptions,
165}
166
167impl MemPoolOptionsBuilder {
168 pub fn max_size_bytes(mut self, size: usize) -> Self {
170 self.options.max_size_bytes = size;
171 self
172 }
173
174 pub fn min_relay_feerate(mut self, rate: u64) -> Self {
176 self.options.min_relay_feerate = rate;
177 self
178 }
179
180 pub fn expiry_seconds(mut self, hours: u32) -> Self {
182 self.options.expiry_seconds = hours;
183 self
184 }
185
186 pub fn max_ancestors(mut self, count: usize) -> Self {
188 self.options.limits.max_ancestors = count;
189 self
190 }
191
192 pub fn max_ancestor_size_vbytes(mut self, size: usize) -> Self {
194 self.options.limits.max_ancestor_size_vbytes = size;
195 self
196 }
197
198 pub fn max_descendants(mut self, count: usize) -> Self {
200 self.options.limits.max_descendants = count;
201 self
202 }
203
204 pub fn max_descendant_size_vbytes(mut self, size: usize) -> Self {
206 self.options.limits.max_descendant_size_vbytes = size;
207 self
208 }
209
210 pub fn require_standard(mut self, require: bool) -> Self {
212 self.options.require_standard = require;
213 self
214 }
215
216 pub fn build(self) -> MemPoolOptions {
218 self.options
219 }
220}
221
222#[cfg(test)]
223mod tests {
224 use super::*;
225
226 #[test]
227 fn test_mempool_options_builder() {
228 let options = MemPoolOptions::builder()
229 .max_size_bytes(500)
230 .min_relay_feerate(2000)
231 .expiry_seconds(168) .max_ancestors(50)
233 .build();
234
235 assert_eq!(options.max_size_bytes, 500);
236 assert_eq!(options.min_relay_feerate, 2000);
237 assert_eq!(options.expiry_seconds, 168);
238 assert_eq!(options.limits.max_ancestors, 50);
239 }
240
241 #[test]
242 fn test_mempool_options_defaults() {
243 let options = MemPoolOptions::default();
244
245 assert_eq!(options.max_size_bytes, 300_000_000);
246 assert_eq!(options.min_relay_feerate, 1000);
247 assert_eq!(options.expiry_seconds, 336 * 60 * 60);
248 assert_eq!(options.limits.max_ancestors, 25);
249 }
250}