// This file is part of Substrate.
// Copyright (C) 2018-2021 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 .
use criterion::{criterion_group, criterion_main, Criterion};
use futures::{future::{ready, Ready}, executor::block_on};
use sc_transaction_graph::*;
use codec::Encode;
use substrate_test_runtime::{Block, Extrinsic, Transfer, H256, AccountId};
use sp_runtime::{
generic::BlockId,
transaction_validity::{
ValidTransaction, InvalidTransaction, TransactionValidity, TransactionTag as Tag,
TransactionSource,
},
};
use sp_core::blake2_256;
#[derive(Clone, Debug, Default)]
struct TestApi {
nonce_dependant: bool,
}
impl TestApi {
fn new_dependant() -> Self {
TestApi { nonce_dependant: true }
}
}
fn to_tag(nonce: u64, from: AccountId) -> Tag {
let mut data = [0u8; 40];
data[..8].copy_from_slice(&nonce.to_le_bytes()[..]);
data[8..].copy_from_slice(&from.0[..]);
data.to_vec()
}
impl ChainApi for TestApi {
type Block = Block;
type Error = sp_transaction_pool::error::Error;
type ValidationFuture = Ready>;
type BodyFuture = Ready>>>;
fn validate_transaction(
&self,
at: &BlockId,
_source: TransactionSource,
uxt: ExtrinsicFor,
) -> Self::ValidationFuture {
let nonce = uxt.transfer().nonce;
let from = uxt.transfer().from.clone();
match self.block_id_to_number(at) {
Ok(Some(num)) if num > 5 => {
return ready(
Ok(Err(InvalidTransaction::Stale.into()))
)
},
_ => {},
}
ready(
Ok(Ok(ValidTransaction {
priority: 4,
requires: if nonce > 1 && self.nonce_dependant {
vec![to_tag(nonce-1, from.clone())]
} else { vec![] },
provides: vec![to_tag(nonce, from)],
longevity: 10,
propagate: true,
}))
)
}
fn block_id_to_number(
&self,
at: &BlockId,
) -> Result