-
Notifications
You must be signed in to change notification settings - Fork 767
/
Copy pathpool.rs
1205 lines (1062 loc) · 35.5 KB
/
pool.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::{common::log_xt::log_xt_trace, LOG_TARGET};
use futures::{channel::mpsc::Receiver, Future};
use indexmap::IndexMap;
use sc_transaction_pool_api::error;
use sp_blockchain::{HashAndNumber, TreeRoute};
use sp_runtime::{
generic::BlockId,
traits::{self, Block as BlockT, SaturatedConversion},
transaction_validity::{
TransactionSource, TransactionTag as Tag, TransactionValidity, TransactionValidityError,
},
};
use std::{
collections::HashMap,
sync::Arc,
time::{Duration, Instant},
};
use super::{
base_pool as base,
validated_pool::{IsValidator, ValidatedPool, ValidatedTransaction},
watcher::Watcher,
};
/// Modification notification event stream type;
pub type EventStream<H> = Receiver<H>;
/// Block hash type for a pool.
pub type BlockHash<A> = <<A as ChainApi>::Block as traits::Block>::Hash;
/// Extrinsic hash type for a pool.
pub type ExtrinsicHash<A> = <<A as ChainApi>::Block as traits::Block>::Hash;
/// Extrinsic type for a pool (reference counted).
pub type ExtrinsicFor<A> = Arc<<<A as ChainApi>::Block as traits::Block>::Extrinsic>;
/// Extrinsic type for a pool (raw data).
pub type RawExtrinsicFor<A> = <<A as ChainApi>::Block as traits::Block>::Extrinsic;
/// Block number type for the ChainApi
pub type NumberFor<A> = traits::NumberFor<<A as ChainApi>::Block>;
/// A type of transaction stored in the pool
pub type TransactionFor<A> = Arc<base::Transaction<ExtrinsicHash<A>, ExtrinsicFor<A>>>;
/// A type of validated transaction stored in the pool.
pub type ValidatedTransactionFor<A> =
ValidatedTransaction<ExtrinsicHash<A>, ExtrinsicFor<A>, <A as ChainApi>::Error>;
/// Concrete extrinsic validation and query logic.
pub trait ChainApi: Send + Sync {
/// Block type.
type Block: BlockT;
/// Error type.
type Error: From<error::Error> + error::IntoPoolError;
/// Validate transaction future.
type ValidationFuture: Future<Output = Result<TransactionValidity, Self::Error>> + Send + Unpin;
/// Body future (since block body might be remote)
type BodyFuture: Future<Output = Result<Option<Vec<<Self::Block as traits::Block>::Extrinsic>>, Self::Error>>
+ Unpin
+ Send
+ 'static;
/// Asynchronously verify extrinsic at given block.
fn validate_transaction(
&self,
at: <Self::Block as BlockT>::Hash,
source: TransactionSource,
uxt: ExtrinsicFor<Self>,
) -> Self::ValidationFuture;
/// Synchronously verify given extrinsic at given block.
///
/// Validates a transaction by calling into the runtime. Same as `validate_transaction` but
/// blocks the current thread when performing validation.
fn validate_transaction_blocking(
&self,
at: <Self::Block as BlockT>::Hash,
source: TransactionSource,
uxt: ExtrinsicFor<Self>,
) -> Result<TransactionValidity, Self::Error>;
/// Returns a block number given the block id.
fn block_id_to_number(
&self,
at: &BlockId<Self::Block>,
) -> Result<Option<NumberFor<Self>>, Self::Error>;
/// Returns a block hash given the block id.
fn block_id_to_hash(
&self,
at: &BlockId<Self::Block>,
) -> Result<Option<<Self::Block as BlockT>::Hash>, Self::Error>;
/// Returns hash and encoding length of the extrinsic.
fn hash_and_length(&self, uxt: &RawExtrinsicFor<Self>) -> (ExtrinsicHash<Self>, usize);
/// Returns a block body given the block.
fn block_body(&self, at: <Self::Block as BlockT>::Hash) -> Self::BodyFuture;
/// Returns a block header given the block id.
fn block_header(
&self,
at: <Self::Block as BlockT>::Hash,
) -> Result<Option<<Self::Block as BlockT>::Header>, Self::Error>;
/// Compute a tree-route between two blocks. See [`TreeRoute`] for more details.
fn tree_route(
&self,
from: <Self::Block as BlockT>::Hash,
to: <Self::Block as BlockT>::Hash,
) -> Result<TreeRoute<Self::Block>, Self::Error>;
/// Resolves block number by id.
fn resolve_block_number(
&self,
at: <Self::Block as BlockT>::Hash,
) -> Result<NumberFor<Self>, Self::Error> {
self.block_id_to_number(&BlockId::Hash(at)).and_then(|number| {
number.ok_or_else(|| error::Error::InvalidBlockId(format!("{:?}", at)).into())
})
}
}
/// Pool configuration options.
#[derive(Debug, Clone)]
pub struct Options {
/// Ready queue limits.
pub ready: base::Limit,
/// Future queue limits.
pub future: base::Limit,
/// Reject future transactions.
pub reject_future_transactions: bool,
/// How long the extrinsic is banned for.
pub ban_time: Duration,
}
impl Default for Options {
fn default() -> Self {
Self {
ready: base::Limit { count: 8192, total_bytes: 20 * 1024 * 1024 },
future: base::Limit { count: 512, total_bytes: 1 * 1024 * 1024 },
reject_future_transactions: false,
ban_time: Duration::from_secs(60 * 30),
}
}
}
/// Should we check that the transaction is banned
/// in the pool, before we verify it?
#[derive(Copy, Clone)]
enum CheckBannedBeforeVerify {
Yes,
No,
}
/// Extrinsics pool that performs validation.
pub struct Pool<B: ChainApi> {
validated_pool: Arc<ValidatedPool<B>>,
}
impl<B: ChainApi> Pool<B> {
/// Create a new transaction pool.
pub fn new(options: Options, is_validator: IsValidator, api: Arc<B>) -> Self {
Self { validated_pool: Arc::new(ValidatedPool::new(options, is_validator, api)) }
}
/// Imports a bunch of unverified extrinsics to the pool
pub async fn submit_at(
&self,
at: &HashAndNumber<B::Block>,
xts: impl IntoIterator<Item = (base::TimedTransactionSource, ExtrinsicFor<B>)>,
) -> Vec<Result<ExtrinsicHash<B>, B::Error>> {
let validated_transactions = self.verify(at, xts, CheckBannedBeforeVerify::Yes).await;
self.validated_pool.submit(validated_transactions.into_values())
}
/// Resubmit the given extrinsics to the pool.
///
/// This does not check if a transaction is banned, before we verify it again.
pub async fn resubmit_at(
&self,
at: &HashAndNumber<B::Block>,
xts: impl IntoIterator<Item = (base::TimedTransactionSource, ExtrinsicFor<B>)>,
) -> Vec<Result<ExtrinsicHash<B>, B::Error>> {
let validated_transactions = self.verify(at, xts, CheckBannedBeforeVerify::No).await;
self.validated_pool.submit(validated_transactions.into_values())
}
/// Imports one unverified extrinsic to the pool
pub async fn submit_one(
&self,
at: &HashAndNumber<B::Block>,
source: base::TimedTransactionSource,
xt: ExtrinsicFor<B>,
) -> Result<ExtrinsicHash<B>, B::Error> {
let res = self.submit_at(at, std::iter::once((source, xt))).await.pop();
res.expect("One extrinsic passed; one result returned; qed")
}
/// Import a single extrinsic and starts to watch its progress in the pool.
pub async fn submit_and_watch(
&self,
at: &HashAndNumber<B::Block>,
source: base::TimedTransactionSource,
xt: ExtrinsicFor<B>,
) -> Result<Watcher<ExtrinsicHash<B>, ExtrinsicHash<B>>, B::Error> {
let (_, tx) = self
.verify_one(at.hash, at.number, source, xt, CheckBannedBeforeVerify::Yes)
.await;
self.validated_pool.submit_and_watch(tx)
}
/// Resubmit some transaction that were validated elsewhere.
pub fn resubmit(
&self,
revalidated_transactions: HashMap<ExtrinsicHash<B>, ValidatedTransactionFor<B>>,
) {
let now = Instant::now();
self.validated_pool.resubmit(revalidated_transactions);
log::trace!(
target: LOG_TARGET,
"Resubmitted. Took {} ms. Status: {:?}",
now.elapsed().as_millis(),
self.validated_pool.status()
);
}
/// Prunes known ready transactions.
///
/// Used to clear the pool from transactions that were part of recently imported block.
/// The main difference from the `prune` is that we do not revalidate any transactions
/// and ignore unknown passed hashes.
pub fn prune_known(&self, at: &HashAndNumber<B::Block>, hashes: &[ExtrinsicHash<B>]) {
// Get details of all extrinsics that are already in the pool
let in_pool_tags =
self.validated_pool.extrinsics_tags(hashes).into_iter().flatten().flatten();
// Prune all transactions that provide given tags
let prune_status = self.validated_pool.prune_tags(in_pool_tags);
let pruned_transactions =
hashes.iter().cloned().chain(prune_status.pruned.iter().map(|tx| tx.hash));
self.validated_pool.fire_pruned(at, pruned_transactions);
}
/// Prunes ready transactions.
///
/// Used to clear the pool from transactions that were part of recently imported block.
/// To perform pruning we need the tags that each extrinsic provides and to avoid calling
/// into runtime too often we first look up all extrinsics that are in the pool and get
/// their provided tags from there. Otherwise we query the runtime at the `parent` block.
pub async fn prune(
&self,
at: &HashAndNumber<B::Block>,
parent: <B::Block as BlockT>::Hash,
extrinsics: &[RawExtrinsicFor<B>],
) {
log::debug!(
target: LOG_TARGET,
"Starting pruning of block {:?} (extrinsics: {})",
at,
extrinsics.len()
);
// Get details of all extrinsics that are already in the pool
let in_pool_hashes =
extrinsics.iter().map(|extrinsic| self.hash_of(extrinsic)).collect::<Vec<_>>();
let in_pool_tags = self.validated_pool.extrinsics_tags(&in_pool_hashes);
// Zip the ones from the pool with the full list (we get pairs `(Extrinsic,
// Option<Vec<Tag>>)`)
let all = extrinsics.iter().zip(in_pool_tags.into_iter());
let mut validated_counter: usize = 0;
let mut future_tags = Vec::new();
for (extrinsic, in_pool_tags) in all {
match in_pool_tags {
// reuse the tags for extrinsics that were found in the pool
Some(tags) => future_tags.extend(tags),
// if it's not found in the pool query the runtime at parent block
// to get validity info and tags that the extrinsic provides.
None => {
// Avoid validating block txs if the pool is empty
if !self.validated_pool.status().is_empty() {
validated_counter = validated_counter + 1;
let validity = self
.validated_pool
.api()
.validate_transaction(
parent,
TransactionSource::InBlock,
Arc::from(extrinsic.clone()),
)
.await;
log::trace!(target: LOG_TARGET,"[{:?}] prune::revalidated {:?}", self.validated_pool.api().hash_and_length(&extrinsic.clone()).0, validity);
if let Ok(Ok(validity)) = validity {
future_tags.extend(validity.provides);
}
} else {
log::trace!(
target: LOG_TARGET,
"txpool is empty, skipping validation for block {at:?}",
);
}
},
}
}
log::trace!(target: LOG_TARGET,"prune: validated_counter:{validated_counter}");
self.prune_tags(at, future_tags, in_pool_hashes).await
}
/// Prunes ready transactions that provide given list of tags.
///
/// Given tags are assumed to be always provided now, so all transactions
/// in the Future Queue that require that particular tag (and have other
/// requirements satisfied) are promoted to Ready Queue.
///
/// Moreover for each provided tag we remove transactions in the pool that:
/// 1. Provide that tag directly
/// 2. Are a dependency of pruned transaction.
///
/// Returns transactions that have been removed from the pool and must be reverified
/// before reinserting to the pool.
///
/// By removing predecessor transactions as well we might actually end up
/// pruning too much, so all removed transactions are reverified against
/// the runtime (`validate_transaction`) to make sure they are invalid.
///
/// However we avoid revalidating transactions that are contained within
/// the second parameter of `known_imported_hashes`. These transactions
/// (if pruned) are not revalidated and become temporarily banned to
/// prevent importing them in the (near) future.
pub async fn prune_tags(
&self,
at: &HashAndNumber<B::Block>,
tags: impl IntoIterator<Item = Tag>,
known_imported_hashes: impl IntoIterator<Item = ExtrinsicHash<B>> + Clone,
) {
log::trace!(target: LOG_TARGET, "Pruning at {:?}", at);
// Prune all transactions that provide given tags
let prune_status = self.validated_pool.prune_tags(tags);
// Make sure that we don't revalidate extrinsics that were part of the recently
// imported block. This is especially important for UTXO-like chains cause the
// inputs are pruned so such transaction would go to future again.
self.validated_pool
.ban(&Instant::now(), known_imported_hashes.clone().into_iter());
// Try to re-validate pruned transactions since some of them might be still valid.
// note that `known_imported_hashes` will be rejected here due to temporary ban.
let pruned_transactions =
prune_status.pruned.into_iter().map(|tx| (tx.source.clone(), tx.data.clone()));
let reverified_transactions =
self.verify(at, pruned_transactions, CheckBannedBeforeVerify::Yes).await;
let pruned_hashes = reverified_transactions.keys().map(Clone::clone).collect();
log::trace!(target: LOG_TARGET, "Pruning at {:?}. Resubmitting transactions: {}", &at, reverified_transactions.len());
log_xt_trace!(data: tuple, target: LOG_TARGET, &reverified_transactions, "[{:?}] Resubmitting transaction: {:?}");
// And finally - submit reverified transactions back to the pool
self.validated_pool.resubmit_pruned(
&at,
known_imported_hashes,
pruned_hashes,
reverified_transactions.into_values().collect(),
)
}
/// Returns transaction hash
pub fn hash_of(&self, xt: &RawExtrinsicFor<B>) -> ExtrinsicHash<B> {
self.validated_pool.api().hash_and_length(xt).0
}
/// Returns future that validates a bunch of transactions at given block.
async fn verify(
&self,
at: &HashAndNumber<B::Block>,
xts: impl IntoIterator<Item = (base::TimedTransactionSource, ExtrinsicFor<B>)>,
check: CheckBannedBeforeVerify,
) -> IndexMap<ExtrinsicHash<B>, ValidatedTransactionFor<B>> {
let HashAndNumber { number, hash } = *at;
let res = futures::future::join_all(
xts.into_iter()
.map(|(source, xt)| self.verify_one(hash, number, source, xt, check)),
)
.await
.into_iter()
.collect::<IndexMap<_, _>>();
res
}
/// Returns future that validates single transaction at given block.
async fn verify_one(
&self,
block_hash: <B::Block as BlockT>::Hash,
block_number: NumberFor<B>,
source: base::TimedTransactionSource,
xt: ExtrinsicFor<B>,
check: CheckBannedBeforeVerify,
) -> (ExtrinsicHash<B>, ValidatedTransactionFor<B>) {
let (hash, bytes) = self.validated_pool.api().hash_and_length(&xt);
let ignore_banned = matches!(check, CheckBannedBeforeVerify::No);
if let Err(err) = self.validated_pool.check_is_known(&hash, ignore_banned) {
return (hash, ValidatedTransaction::Invalid(hash, err))
}
let validation_result = self
.validated_pool
.api()
.validate_transaction(block_hash, source.clone().into(), xt.clone())
.await;
let status = match validation_result {
Ok(status) => status,
Err(e) => return (hash, ValidatedTransaction::Invalid(hash, e)),
};
let validity = match status {
Ok(validity) =>
if validity.provides.is_empty() {
ValidatedTransaction::Invalid(hash, error::Error::NoTagsProvided.into())
} else {
ValidatedTransaction::valid_at(
block_number.saturated_into::<u64>(),
hash,
source,
xt,
bytes,
validity,
)
},
Err(TransactionValidityError::Invalid(e)) =>
ValidatedTransaction::Invalid(hash, error::Error::InvalidTransaction(e).into()),
Err(TransactionValidityError::Unknown(e)) =>
ValidatedTransaction::Unknown(hash, error::Error::UnknownTransaction(e).into()),
};
(hash, validity)
}
/// Get a reference to the underlying validated pool.
pub fn validated_pool(&self) -> &ValidatedPool<B> {
&self.validated_pool
}
/// Clears the recently pruned transactions in validated pool.
pub fn clear_recently_pruned(&mut self) {
self.validated_pool.pool.write().clear_recently_pruned();
}
}
impl<B: ChainApi> Pool<B> {
/// Deep clones the pool.
///
/// Must be called on purpose: it duplicates all the internal structures.
pub fn deep_clone(&self) -> Self {
let other: ValidatedPool<B> = (*self.validated_pool).clone();
Self { validated_pool: Arc::from(other) }
}
}
#[cfg(test)]
mod tests {
use super::{super::base_pool::Limit, *};
use crate::common::tests::{pool, uxt, TestApi, INVALID_NONCE};
use assert_matches::assert_matches;
use base::TimedTransactionSource;
use codec::Encode;
use futures::executor::block_on;
use parking_lot::Mutex;
use sc_transaction_pool_api::TransactionStatus;
use sp_runtime::transaction_validity::TransactionSource;
use std::{collections::HashMap, time::Instant};
use substrate_test_runtime::{AccountId, ExtrinsicBuilder, Transfer, H256};
use substrate_test_runtime_client::Sr25519Keyring::{Alice, Bob};
const SOURCE: TimedTransactionSource =
TimedTransactionSource { source: TransactionSource::External, timestamp: None };
#[test]
fn should_validate_and_import_transaction() {
// given
let (pool, api) = pool();
// when
let hash = block_on(
pool.submit_one(
&api.expect_hash_and_number(0),
SOURCE,
uxt(Transfer {
from: Alice.into(),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
})
.into(),
),
)
.unwrap();
// then
assert_eq!(pool.validated_pool().ready().map(|v| v.hash).collect::<Vec<_>>(), vec![hash]);
}
#[test]
fn submit_at_preserves_order() {
sp_tracing::try_init_simple();
// given
let (pool, api) = pool();
let txs = (0..10)
.map(|i| {
uxt(Transfer {
from: Alice.into(),
to: AccountId::from_h256(H256::from_low_u64_be(i)),
amount: 5,
nonce: i,
})
.into()
})
.collect::<Vec<_>>();
let initial_hashes = txs.iter().map(|t| api.hash_and_length(t).0).collect::<Vec<_>>();
// when
let txs = txs.into_iter().map(|x| (SOURCE, Arc::from(x))).collect::<Vec<_>>();
let hashes = block_on(pool.submit_at(&api.expect_hash_and_number(0), txs));
log::debug!("--> {hashes:#?}");
// then
hashes.into_iter().zip(initial_hashes.into_iter()).for_each(
|(result_hash, initial_hash)| {
assert_eq!(result_hash.unwrap(), initial_hash);
},
);
}
#[test]
fn should_reject_if_temporarily_banned() {
// given
let (pool, api) = pool();
let uxt = uxt(Transfer {
from: Alice.into(),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
});
// when
pool.validated_pool.ban(&Instant::now(), vec![pool.hash_of(&uxt)]);
let res = block_on(pool.submit_one(&api.expect_hash_and_number(0), SOURCE, uxt.into()));
assert_eq!(pool.validated_pool().status().ready, 0);
assert_eq!(pool.validated_pool().status().future, 0);
// then
assert_matches!(res.unwrap_err(), error::Error::TemporarilyBanned);
}
#[test]
fn should_reject_unactionable_transactions() {
// given
let api = Arc::new(TestApi::default());
let pool = Pool::new(
Default::default(),
// the node does not author blocks
false.into(),
api.clone(),
);
// after validation `IncludeData` will be set to non-propagable (validate_transaction mock)
let uxt = ExtrinsicBuilder::new_include_data(vec![42]).build();
// when
let res = block_on(pool.submit_one(&api.expect_hash_and_number(0), SOURCE, uxt.into()));
// then
assert_matches!(res.unwrap_err(), error::Error::Unactionable);
}
#[test]
fn should_notify_about_pool_events() {
let (stream, hash0, hash1) = {
// given
let (pool, api) = pool();
let han_of_block0 = api.expect_hash_and_number(0);
let stream = pool.validated_pool().import_notification_stream();
// when
let hash0 = block_on(
pool.submit_one(
&han_of_block0,
SOURCE,
uxt(Transfer {
from: Alice.into(),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
})
.into(),
),
)
.unwrap();
let hash1 = block_on(
pool.submit_one(
&han_of_block0,
SOURCE,
uxt(Transfer {
from: Alice.into(),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 1,
})
.into(),
),
)
.unwrap();
// future doesn't count
let _hash = block_on(
pool.submit_one(
&han_of_block0,
SOURCE,
uxt(Transfer {
from: Alice.into(),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 3,
})
.into(),
),
)
.unwrap();
assert_eq!(pool.validated_pool().status().ready, 2);
assert_eq!(pool.validated_pool().status().future, 1);
(stream, hash0, hash1)
};
// then
let mut it = futures::executor::block_on_stream(stream);
assert_eq!(it.next(), Some(hash0));
assert_eq!(it.next(), Some(hash1));
assert_eq!(it.next(), None);
}
#[test]
fn should_clear_stale_transactions() {
// given
let (pool, api) = pool();
let han_of_block0 = api.expect_hash_and_number(0);
let hash1 = block_on(
pool.submit_one(
&han_of_block0,
SOURCE,
uxt(Transfer {
from: Alice.into(),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
})
.into(),
),
)
.unwrap();
let hash2 = block_on(
pool.submit_one(
&han_of_block0,
SOURCE,
uxt(Transfer {
from: Alice.into(),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 1,
})
.into(),
),
)
.unwrap();
let hash3 = block_on(
pool.submit_one(
&han_of_block0,
SOURCE,
uxt(Transfer {
from: Alice.into(),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 3,
})
.into(),
),
)
.unwrap();
// when
pool.validated_pool.clear_stale(&api.expect_hash_and_number(5));
// then
assert_eq!(pool.validated_pool().ready().count(), 0);
assert_eq!(pool.validated_pool().status().future, 0);
assert_eq!(pool.validated_pool().status().ready, 0);
// make sure they are temporarily banned as well
assert!(pool.validated_pool.is_banned(&hash1));
assert!(pool.validated_pool.is_banned(&hash2));
assert!(pool.validated_pool.is_banned(&hash3));
}
#[test]
fn should_ban_mined_transactions() {
// given
let (pool, api) = pool();
let hash1 = block_on(
pool.submit_one(
&api.expect_hash_and_number(0),
SOURCE,
uxt(Transfer {
from: Alice.into(),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
})
.into(),
),
)
.unwrap();
// when
block_on(pool.prune_tags(&api.expect_hash_and_number(1), vec![vec![0]], vec![hash1]));
// then
assert!(pool.validated_pool.is_banned(&hash1));
}
#[test]
fn should_limit_futures() {
sp_tracing::try_init_simple();
let xt = uxt(Transfer {
from: Alice.into(),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 1,
});
// given
let limit = Limit { count: 100, total_bytes: xt.encoded_size() };
let options = Options { ready: limit.clone(), future: limit.clone(), ..Default::default() };
let api = Arc::new(TestApi::default());
let pool = Pool::new(options, true.into(), api.clone());
let hash1 =
block_on(pool.submit_one(&api.expect_hash_and_number(0), SOURCE, xt.into())).unwrap();
assert_eq!(pool.validated_pool().status().future, 1);
// when
let hash2 = block_on(
pool.submit_one(
&api.expect_hash_and_number(0),
SOURCE,
uxt(Transfer {
from: Bob.into(),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 10,
})
.into(),
),
)
.unwrap();
// then
assert_eq!(pool.validated_pool().status().future, 1);
assert!(pool.validated_pool.is_banned(&hash1));
assert!(!pool.validated_pool.is_banned(&hash2));
}
#[test]
fn should_error_if_reject_immediately() {
// given
let limit = Limit { count: 100, total_bytes: 10 };
let options = Options { ready: limit.clone(), future: limit.clone(), ..Default::default() };
let api = Arc::new(TestApi::default());
let pool = Pool::new(options, true.into(), api.clone());
// when
block_on(
pool.submit_one(
&api.expect_hash_and_number(0),
SOURCE,
uxt(Transfer {
from: Alice.into(),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 1,
})
.into(),
),
)
.unwrap_err();
// then
assert_eq!(pool.validated_pool().status().ready, 0);
assert_eq!(pool.validated_pool().status().future, 0);
}
#[test]
fn should_reject_transactions_with_no_provides() {
// given
let (pool, api) = pool();
// when
let err = block_on(
pool.submit_one(
&api.expect_hash_and_number(0),
SOURCE,
uxt(Transfer {
from: Alice.into(),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: INVALID_NONCE,
})
.into(),
),
)
.unwrap_err();
// then
assert_eq!(pool.validated_pool().status().ready, 0);
assert_eq!(pool.validated_pool().status().future, 0);
assert_matches!(err, error::Error::NoTagsProvided);
}
mod listener {
use super::*;
#[test]
fn should_trigger_ready_and_finalized() {
// given
let (pool, api) = pool();
let watcher = block_on(
pool.submit_and_watch(
&api.expect_hash_and_number(0),
SOURCE,
uxt(Transfer {
from: Alice.into(),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
})
.into(),
),
)
.unwrap();
assert_eq!(pool.validated_pool().status().ready, 1);
assert_eq!(pool.validated_pool().status().future, 0);
let han_of_block2 = api.expect_hash_and_number(2);
// when
block_on(pool.prune_tags(&han_of_block2, vec![vec![0u8]], vec![]));
assert_eq!(pool.validated_pool().status().ready, 0);
assert_eq!(pool.validated_pool().status().future, 0);
// then
let mut stream = futures::executor::block_on_stream(watcher.into_stream());
assert_eq!(stream.next(), Some(TransactionStatus::Ready));
assert_eq!(
stream.next(),
Some(TransactionStatus::InBlock((han_of_block2.hash.into(), 0))),
);
}
#[test]
fn should_trigger_ready_and_finalized_when_pruning_via_hash() {
// given
let (pool, api) = pool();
let watcher = block_on(
pool.submit_and_watch(
&api.expect_hash_and_number(0),
SOURCE,
uxt(Transfer {
from: Alice.into(),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
})
.into(),
),
)
.unwrap();
assert_eq!(pool.validated_pool().status().ready, 1);
assert_eq!(pool.validated_pool().status().future, 0);
let han_of_block2 = api.expect_hash_and_number(2);
// when
block_on(pool.prune_tags(&han_of_block2, vec![vec![0u8]], vec![*watcher.hash()]));
assert_eq!(pool.validated_pool().status().ready, 0);
assert_eq!(pool.validated_pool().status().future, 0);
// then
let mut stream = futures::executor::block_on_stream(watcher.into_stream());
assert_eq!(stream.next(), Some(TransactionStatus::Ready));
assert_eq!(
stream.next(),
Some(TransactionStatus::InBlock((han_of_block2.hash.into(), 0))),
);
}
#[test]
fn should_trigger_future_and_ready_after_promoted() {
// given
let (pool, api) = pool();
let han_of_block0 = api.expect_hash_and_number(0);
let watcher = block_on(
pool.submit_and_watch(
&han_of_block0,
SOURCE,
uxt(Transfer {
from: Alice.into(),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 1,
})
.into(),
),
)
.unwrap();
assert_eq!(pool.validated_pool().status().ready, 0);
assert_eq!(pool.validated_pool().status().future, 1);
// when
block_on(
pool.submit_one(
&han_of_block0,
SOURCE,
uxt(Transfer {
from: Alice.into(),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
})
.into(),
),
)
.unwrap();
assert_eq!(pool.validated_pool().status().ready, 2);
// then
let mut stream = futures::executor::block_on_stream(watcher.into_stream());
assert_eq!(stream.next(), Some(TransactionStatus::Future));
assert_eq!(stream.next(), Some(TransactionStatus::Ready));
}
#[test]
fn should_trigger_invalid_and_ban() {
// given
let (pool, api) = pool();
let uxt = uxt(Transfer {
from: Alice.into(),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
});
let watcher =
block_on(pool.submit_and_watch(&api.expect_hash_and_number(0), SOURCE, uxt.into()))
.unwrap();
assert_eq!(pool.validated_pool().status().ready, 1);
// when
pool.validated_pool.remove_invalid(&[*watcher.hash()]);
// then
let mut stream = futures::executor::block_on_stream(watcher.into_stream());
assert_eq!(stream.next(), Some(TransactionStatus::Ready));
assert_eq!(stream.next(), Some(TransactionStatus::Invalid));