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
use self::core::core::hash::{Hash, Hashed};
use self::core::core::id::ShortId;
use self::core::core::verifier_cache::VerifierCache;
use self::core::core::{transaction, Block, BlockHeader, Transaction, Weighting};
use self::util::RwLock;
use crate::pool::Pool;
use crate::types::{BlockChain, PoolAdapter, PoolConfig, PoolEntry, PoolError, TxSource};
use chrono::prelude::*;
use grin_core as core;
use grin_util as util;
use std::collections::VecDeque;
use std::sync::Arc;
pub struct TransactionPool {
pub config: PoolConfig,
pub txpool: Pool,
pub stempool: Pool,
pub reorg_cache: Arc<RwLock<VecDeque<PoolEntry>>>,
pub blockchain: Arc<dyn BlockChain>,
pub verifier_cache: Arc<RwLock<dyn VerifierCache>>,
pub adapter: Arc<dyn PoolAdapter>,
}
impl TransactionPool {
pub fn new(
config: PoolConfig,
chain: Arc<dyn BlockChain>,
verifier_cache: Arc<RwLock<dyn VerifierCache>>,
adapter: Arc<dyn PoolAdapter>,
) -> TransactionPool {
TransactionPool {
config,
txpool: Pool::new(chain.clone(), verifier_cache.clone(), "txpool".to_string()),
stempool: Pool::new(
chain.clone(),
verifier_cache.clone(),
"stempool".to_string(),
),
reorg_cache: Arc::new(RwLock::new(VecDeque::new())),
blockchain: chain,
verifier_cache,
adapter,
}
}
pub fn chain_head(&self) -> Result<BlockHeader, PoolError> {
self.blockchain.chain_head()
}
fn add_to_stempool(&mut self, entry: PoolEntry, header: &BlockHeader) -> Result<(), PoolError> {
self.stempool
.add_to_pool(entry, self.txpool.all_transactions(), header)?;
Ok(())
}
fn add_to_reorg_cache(&mut self, entry: PoolEntry) {
let mut cache = self.reorg_cache.write();
cache.push_back(entry);
if cache.len() > self.config.max_pool_size {
let _ = cache.pop_front();
}
debug!("added tx to reorg_cache: size now {}", cache.len());
}
fn add_to_txpool(
&mut self,
mut entry: PoolEntry,
header: &BlockHeader,
) -> Result<(), PoolError> {
if entry.tx.kernels().len() > 1 {
let txs = self.txpool.find_matching_transactions(entry.tx.kernels());
if !txs.is_empty() {
let tx = transaction::deaggregate(entry.tx, txs)?;
tx.validate(Weighting::AsTransaction, self.verifier_cache.clone())?;
entry.tx = tx;
entry.src = TxSource::Deaggregate;
}
}
self.txpool.add_to_pool(entry.clone(), vec![], header)?;
{
let txpool_tx = self.txpool.all_transactions_aggregate()?;
self.stempool.reconcile(txpool_tx, header)?;
}
Ok(())
}
pub fn add_to_pool(
&mut self,
src: TxSource,
tx: Transaction,
stem: bool,
header: &BlockHeader,
) -> Result<(), PoolError> {
if !stem && self.txpool.contains_tx(tx.hash()) {
return Err(PoolError::DuplicateTx);
}
let acceptability = self.is_acceptable(&tx, stem);
let mut evict = false;
if !stem && acceptability.as_ref().err() == Some(&PoolError::OverCapacity) {
evict = true;
} else if acceptability.is_err() {
return acceptability;
}
tx.validate(Weighting::AsTransaction, self.verifier_cache.clone())
.map_err(PoolError::InvalidTx)?;
self.blockchain.verify_tx_lock_height(&tx)?;
self.blockchain.verify_coinbase_maturity(&tx)?;
let entry = PoolEntry {
src,
tx_at: Utc::now(),
tx,
};
if !stem
|| self
.add_to_stempool(entry.clone(), header)
.and_then(|_| self.adapter.stem_tx_accepted(&entry))
.is_err()
{
self.add_to_txpool(entry.clone(), header)?;
self.add_to_reorg_cache(entry.clone());
self.adapter.tx_accepted(&entry);
}
if evict {
self.evict_from_txpool();
}
Ok(())
}
pub fn evict_from_txpool(&mut self) {
let bucket_transactions = self.txpool.bucket_transactions(Weighting::NoLimit);
if let Some(evictable_transaction) = bucket_transactions.last() {
self.txpool
.entries
.retain(|x| x.tx != *evictable_transaction);
};
}
pub fn truncate_reorg_cache(&mut self, cutoff: DateTime<Utc>) {
let mut cache = self.reorg_cache.write();
while cache.front().map(|x| x.tx_at < cutoff).unwrap_or(false) {
let _ = cache.pop_front();
}
debug!("truncate_reorg_cache: size: {}", cache.len());
}
pub fn reconcile_reorg_cache(&mut self, header: &BlockHeader) -> Result<(), PoolError> {
let entries = self.reorg_cache.read().iter().cloned().collect::<Vec<_>>();
debug!(
"reconcile_reorg_cache: size: {}, block: {:?} ...",
entries.len(),
header.hash(),
);
for entry in entries {
let _ = &self.add_to_txpool(entry.clone(), header);
}
debug!(
"reconcile_reorg_cache: block: {:?} ... done.",
header.hash()
);
Ok(())
}
pub fn reconcile_block(&mut self, block: &Block) -> Result<(), PoolError> {
self.txpool.reconcile_block(block);
self.txpool.reconcile(None, &block.header)?;
self.stempool.reconcile_block(block);
{
let txpool_tx = self.txpool.all_transactions_aggregate()?;
self.stempool.reconcile(txpool_tx, &block.header)?;
}
Ok(())
}
pub fn retrieve_tx_by_kernel_hash(&self, hash: Hash) -> Option<Transaction> {
self.txpool.retrieve_tx_by_kernel_hash(hash)
}
pub fn retrieve_transactions(
&self,
hash: Hash,
nonce: u64,
kern_ids: &[ShortId],
) -> (Vec<Transaction>, Vec<ShortId>) {
self.txpool.retrieve_transactions(hash, nonce, kern_ids)
}
fn is_acceptable(&self, tx: &Transaction, stem: bool) -> Result<(), PoolError> {
if self.total_size() > self.config.max_pool_size {
return Err(PoolError::OverCapacity);
}
if stem && self.stempool.size() > self.config.max_stempool_size
|| self.total_size() > self.config.max_pool_size
{
return Err(PoolError::OverCapacity);
}
if self.config.accept_fee_base > 0 {
let threshold = (tx.tx_weight() as u64) * self.config.accept_fee_base;
if tx.fee() < threshold {
return Err(PoolError::LowFeeTransaction(threshold));
}
}
Ok(())
}
pub fn total_size(&self) -> usize {
self.txpool.size()
}
pub fn prepare_mineable_transactions(&self) -> Result<Vec<Transaction>, PoolError> {
self.txpool
.prepare_mineable_transactions(self.config.mineable_max_weight)
}
}