|
| 1 | +// Copyright 2018-2019 Parity Technologies (UK) Ltd. |
| 2 | +// This file is part of Substrate. |
| 3 | + |
| 4 | +// Substrate is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// Substrate is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Substrate. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +use criterion::{criterion_group, criterion_main, Criterion}; |
| 18 | + |
| 19 | +use futures::executor::block_on; |
| 20 | +use substrate_transaction_graph::*; |
| 21 | +use sr_primitives::transaction_validity::{ValidTransaction, InvalidTransaction}; |
| 22 | +use codec::Encode; |
| 23 | +use test_runtime::{Block, Extrinsic, Transfer, H256, AccountId}; |
| 24 | +use sr_primitives::{ |
| 25 | + generic::BlockId, |
| 26 | + transaction_validity::{TransactionValidity, TransactionTag as Tag}, |
| 27 | +}; |
| 28 | +use primitives::blake2_256; |
| 29 | + |
| 30 | +#[derive(Clone, Debug, Default)] |
| 31 | +struct TestApi { |
| 32 | + nonce_dependant: bool, |
| 33 | +} |
| 34 | + |
| 35 | +impl TestApi { |
| 36 | + fn new_dependant() -> Self { |
| 37 | + TestApi { nonce_dependant: true } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +fn to_tag(nonce: u64, from: AccountId) -> Tag { |
| 42 | + let mut data = [0u8; 40]; |
| 43 | + data[..8].copy_from_slice(&nonce.to_le_bytes()[..]); |
| 44 | + data[8..].copy_from_slice(&from.0[..]); |
| 45 | + data.to_vec() |
| 46 | +} |
| 47 | + |
| 48 | +impl ChainApi for TestApi { |
| 49 | + type Block = Block; |
| 50 | + type Hash = H256; |
| 51 | + type Error = error::Error; |
| 52 | + type ValidationFuture = futures::future::Ready<error::Result<TransactionValidity>>; |
| 53 | + |
| 54 | + fn validate_transaction( |
| 55 | + &self, |
| 56 | + at: &BlockId<Self::Block>, |
| 57 | + uxt: ExtrinsicFor<Self>, |
| 58 | + ) -> Self::ValidationFuture { |
| 59 | + let nonce = uxt.transfer().nonce; |
| 60 | + let from = uxt.transfer().from.clone(); |
| 61 | + |
| 62 | + match self.block_id_to_number(at) { |
| 63 | + Ok(Some(num)) if num > 5 => { |
| 64 | + return futures::future::ready( |
| 65 | + Ok(Err(InvalidTransaction::Stale.into())) |
| 66 | + ) |
| 67 | + }, |
| 68 | + _ => {}, |
| 69 | + } |
| 70 | + |
| 71 | + futures::future::ready( |
| 72 | + Ok(Ok(ValidTransaction { |
| 73 | + priority: 4, |
| 74 | + requires: if nonce > 1 && self.nonce_dependant { |
| 75 | + vec![to_tag(nonce-1, from.clone())] |
| 76 | + } else { vec![] }, |
| 77 | + provides: vec![to_tag(nonce, from)], |
| 78 | + longevity: 10, |
| 79 | + propagate: true, |
| 80 | + })) |
| 81 | + ) |
| 82 | + } |
| 83 | + |
| 84 | + fn block_id_to_number( |
| 85 | + &self, |
| 86 | + at: &BlockId<Self::Block>, |
| 87 | + ) -> Result<Option<NumberFor<Self>>, Self::Error> { |
| 88 | + Ok(match at { |
| 89 | + BlockId::Number(num) => Some(*num), |
| 90 | + BlockId::Hash(_) => None, |
| 91 | + }) |
| 92 | + } |
| 93 | + |
| 94 | + fn block_id_to_hash( |
| 95 | + &self, |
| 96 | + at: &BlockId<Self::Block>, |
| 97 | + ) -> Result<Option<BlockHash<Self>>, Self::Error> { |
| 98 | + Ok(match at { |
| 99 | + BlockId::Number(num) => Some(H256::from_low_u64_be(*num)).into(), |
| 100 | + BlockId::Hash(_) => None, |
| 101 | + }) |
| 102 | + } |
| 103 | + |
| 104 | + fn hash_and_length(&self, uxt: &ExtrinsicFor<Self>) -> (Self::Hash, usize) { |
| 105 | + let encoded = uxt.encode(); |
| 106 | + (blake2_256(&encoded).into(), encoded.len()) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +fn uxt(transfer: Transfer) -> Extrinsic { |
| 111 | + Extrinsic::Transfer(transfer, Default::default()) |
| 112 | +} |
| 113 | + |
| 114 | +fn bench_configured(pool: Pool<TestApi>, number: u64) { |
| 115 | + let mut futures = Vec::new(); |
| 116 | + let mut tags = Vec::new(); |
| 117 | + |
| 118 | + for nonce in 1..=number { |
| 119 | + let xt = uxt(Transfer { |
| 120 | + from: AccountId::from_h256(H256::from_low_u64_be(1)), |
| 121 | + to: AccountId::from_h256(H256::from_low_u64_be(2)), |
| 122 | + amount: 5, |
| 123 | + nonce, |
| 124 | + }); |
| 125 | + |
| 126 | + tags.push(to_tag(nonce, AccountId::from_h256(H256::from_low_u64_be(1)))); |
| 127 | + futures.push(pool.submit_one(&BlockId::Number(1), xt)); |
| 128 | + } |
| 129 | + |
| 130 | + let res = block_on(futures::future::join_all(futures.into_iter())); |
| 131 | + assert!(res.iter().all(Result::is_ok)); |
| 132 | + |
| 133 | + assert_eq!(pool.status().future, 0); |
| 134 | + assert_eq!(pool.status().ready, number as usize); |
| 135 | + |
| 136 | + // Prune all transactions. |
| 137 | + let block_num = 6; |
| 138 | + block_on(pool.prune_tags( |
| 139 | + &BlockId::Number(block_num), |
| 140 | + tags, |
| 141 | + vec![], |
| 142 | + )).expect("Prune failed"); |
| 143 | + |
| 144 | + // pool is empty |
| 145 | + assert_eq!(pool.status().ready, 0); |
| 146 | + assert_eq!(pool.status().future, 0); |
| 147 | +} |
| 148 | + |
| 149 | +fn benchmark_main(c: &mut Criterion) { |
| 150 | + |
| 151 | + c.bench_function("sequential 50 tx", |b| { |
| 152 | + b.iter(|| { |
| 153 | + bench_configured(Pool::new(Default::default(), TestApi::new_dependant()), 50); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + c.bench_function("random 100 tx", |b| { |
| 158 | + b.iter(|| { |
| 159 | + bench_configured(Pool::new(Default::default(), TestApi::default()), 100); |
| 160 | + }); |
| 161 | + }); |
| 162 | +} |
| 163 | + |
| 164 | +criterion_group!(benches, benchmark_main); |
| 165 | +criterion_main!(benches); |
0 commit comments