mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-30 02:37:27 +00:00
ead6815ae4
* Make WASM browser thing compile * Fix * updated exit-future (github repo) * Switch to broadcast crate * Migrate client/cli * Switch exit-future to modernize branch * Small changes * Switch to cargo version and fix fg tests * fix basic-authorship * Fix crash on grafana macro * Fix grafana macro * Switch node python version * Disable record_metrics_slice in grafana macro on wasm * Update client/grafana-data-source/src/lib.rs * Revert "Update client/grafana-data-source/src/lib.rs" This reverts commit 888009a8e0b7051bd4bfbbfdb0448bcf2e2aae93. * Add wasm support for state machine * Switch to my own libp2p version * Revert "Switch to my own libp2p version" This reverts commit ce613871b59264b3165b45c37943e6560240daa7. * Revert "Add wasm support for state machine" This reverts commit de7eaa0694d9534fc3b164621737968e9a6a7c5f. * Add sc-browser * Squash * remove sc-browser * Fix keystore on wasm * stubs for removed functions to make env compatible with old runtimes * Add test (that doesn't work) * Fix build scripts * Revert basic-authorship due to no panics * Revert cli/informant * Revert consensus * revert offchain * Update utils/browser/Cargo.toml Co-Authored-By: Benjamin Kampmann <ben@gnunicorn.org> * export console functions * Add new chainspec * Fix ws in chain spec * revert chainspec * Fix chainspec * Use an Option<PathBuf> in keystore instead of cfg flags * Remove crud * Only use wasm-timer for instant and systemtime * Remove telemetry changes * Assuming this is ok * Add a KeystoreConfig * Add stubs back in * Update libp2p * Revert "Add stubs back in" This reverts commit 4690cf1882aa0f99f7f00a58c4080c8aa9b77c36. * Remove commented js again * Bump kvdb-web version * Fix cli * Switch branch on futures-timer * Fix tests * Remove sc-client test build in check-web-wasm because there isn't a good way to build futures-timer with wasm-bindgen support in the build * Remove more things ^^ * Switch branch on futures-timer back * Put DB io stats behind a cfg flag * Fix things * Don't timeout transports on wasm * Update branch of futures-timer and fix bad merge * Spawn informant * Fix network test * Fix delay resets * Changes * Fix tests * use wasm_timer for transaction pool * Fixes * Switch futures-timer to crates * Only diagnose futures on native * Fix sc-network-test tests * Select log level in js * Fix syncing ;^) * Allow disabling colours in the informant * Use OutputFormat enum for informant * MallocSizeOf impl on transaction pool broke stuff because wasm_timer::Instant doesnt impl it so just revert the transaction pool to master * Update futures-diagnose * Revert "MallocSizeOf impl on transaction pool broke stuff because wasm_timer::Instant doesnt impl it so just revert the transaction pool to master" This reverts commit baa4ffc94fd968b6660a2c17ba8113e06af15548. * Pass whole chain spec in start_client * Get Instant::now to work in transaction pool again * Informant dep reordering Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com> Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> Co-authored-by: Svyatoslav Nikolsky <svyatonik@gmail.com> Co-authored-by: Benjamin Kampmann <ben.kampmann@googlemail.com> Co-authored-by: Demi Obenour <48690212+DemiMarie-parity@users.noreply.github.com>
213 lines
4.9 KiB
Rust
213 lines
4.9 KiB
Rust
// Copyright 2018-2020 Parity Technologies (UK) Ltd.
|
|
// This file is part of Substrate.
|
|
|
|
// Substrate 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.
|
|
|
|
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//! Rotate extrinsic inside the pool.
|
|
//!
|
|
//! Keeps only recent extrinsic and discard the ones kept for a significant amount of time.
|
|
//! Discarded extrinsics are banned so that they don't get re-imported again.
|
|
|
|
use std::{
|
|
collections::HashMap,
|
|
hash,
|
|
iter,
|
|
time::Duration,
|
|
};
|
|
use parking_lot::RwLock;
|
|
use wasm_timer::Instant;
|
|
|
|
use crate::base_pool::Transaction;
|
|
|
|
/// Expected size of the banned extrinsics cache.
|
|
const EXPECTED_SIZE: usize = 2048;
|
|
|
|
/// Pool rotator is responsible to only keep fresh extrinsics in the pool.
|
|
///
|
|
/// Extrinsics that occupy the pool for too long are culled and temporarily banned from entering
|
|
/// the pool again.
|
|
pub struct PoolRotator<Hash> {
|
|
/// How long the extrinsic is banned for.
|
|
ban_time: Duration,
|
|
/// Currently banned extrinsics.
|
|
banned_until: RwLock<HashMap<Hash, Instant>>,
|
|
}
|
|
|
|
impl<Hash: hash::Hash + Eq> Default for PoolRotator<Hash> {
|
|
fn default() -> Self {
|
|
PoolRotator {
|
|
ban_time: Duration::from_secs(60 * 30),
|
|
banned_until: Default::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<Hash: hash::Hash + Eq + Clone> PoolRotator<Hash> {
|
|
/// Returns `true` if extrinsic hash is currently banned.
|
|
pub fn is_banned(&self, hash: &Hash) -> bool {
|
|
self.banned_until.read().contains_key(hash)
|
|
}
|
|
|
|
/// Bans given set of hashes.
|
|
pub fn ban(&self, now: &Instant, hashes: impl IntoIterator<Item=Hash>) {
|
|
let mut banned = self.banned_until.write();
|
|
|
|
for hash in hashes {
|
|
banned.insert(hash, *now + self.ban_time);
|
|
}
|
|
|
|
if banned.len() > 2 * EXPECTED_SIZE {
|
|
while banned.len() > EXPECTED_SIZE {
|
|
if let Some(key) = banned.keys().next().cloned() {
|
|
banned.remove(&key);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/// Bans extrinsic if it's stale.
|
|
///
|
|
/// Returns `true` if extrinsic is stale and got banned.
|
|
pub fn ban_if_stale<Ex>(&self, now: &Instant, current_block: u64, xt: &Transaction<Hash, Ex>) -> bool {
|
|
if xt.valid_till > current_block {
|
|
return false;
|
|
}
|
|
|
|
self.ban(now, iter::once(xt.hash.clone()));
|
|
true
|
|
}
|
|
|
|
/// Removes timed bans.
|
|
pub fn clear_timeouts(&self, now: &Instant) {
|
|
let mut banned = self.banned_until.write();
|
|
|
|
banned.retain(|_, &mut v| v >= *now);
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
type Hash = u64;
|
|
type Ex = ();
|
|
|
|
fn rotator() -> PoolRotator<Hash> {
|
|
PoolRotator {
|
|
ban_time: Duration::from_millis(10),
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
fn tx() -> (Hash, Transaction<Hash, Ex>) {
|
|
let hash = 5u64;
|
|
let tx = Transaction {
|
|
data: (),
|
|
bytes: 1,
|
|
hash: hash.clone(),
|
|
priority: 5,
|
|
valid_till: 1,
|
|
requires: vec![],
|
|
provides: vec![],
|
|
propagate: true,
|
|
};
|
|
|
|
(hash, tx)
|
|
}
|
|
|
|
#[test]
|
|
fn should_not_ban_if_not_stale() {
|
|
// given
|
|
let (hash, tx) = tx();
|
|
let rotator = rotator();
|
|
assert!(!rotator.is_banned(&hash));
|
|
let now = Instant::now();
|
|
let past_block = 0;
|
|
|
|
// when
|
|
assert!(!rotator.ban_if_stale(&now, past_block, &tx));
|
|
|
|
// then
|
|
assert!(!rotator.is_banned(&hash));
|
|
}
|
|
|
|
#[test]
|
|
fn should_ban_stale_extrinsic() {
|
|
// given
|
|
let (hash, tx) = tx();
|
|
let rotator = rotator();
|
|
assert!(!rotator.is_banned(&hash));
|
|
|
|
// when
|
|
assert!(rotator.ban_if_stale(&Instant::now(), 1, &tx));
|
|
|
|
// then
|
|
assert!(rotator.is_banned(&hash));
|
|
}
|
|
|
|
|
|
#[test]
|
|
fn should_clear_banned() {
|
|
// given
|
|
let (hash, tx) = tx();
|
|
let rotator = rotator();
|
|
assert!(rotator.ban_if_stale(&Instant::now(), 1, &tx));
|
|
assert!(rotator.is_banned(&hash));
|
|
|
|
// when
|
|
let future = Instant::now() + rotator.ban_time + rotator.ban_time;
|
|
rotator.clear_timeouts(&future);
|
|
|
|
// then
|
|
assert!(!rotator.is_banned(&hash));
|
|
}
|
|
|
|
#[test]
|
|
fn should_garbage_collect() {
|
|
// given
|
|
fn tx_with(i: u64, valid_till: u64) -> Transaction<Hash, Ex> {
|
|
let hash = i;
|
|
Transaction {
|
|
data: (),
|
|
bytes: 2,
|
|
hash,
|
|
priority: 5,
|
|
valid_till,
|
|
requires: vec![],
|
|
provides: vec![],
|
|
propagate: true,
|
|
}
|
|
}
|
|
|
|
let rotator = rotator();
|
|
|
|
let now = Instant::now();
|
|
let past_block = 0;
|
|
|
|
// when
|
|
for i in 0..2*EXPECTED_SIZE {
|
|
let tx = tx_with(i as u64, past_block);
|
|
assert!(rotator.ban_if_stale(&now, past_block, &tx));
|
|
}
|
|
assert_eq!(rotator.banned_until.read().len(), 2*EXPECTED_SIZE);
|
|
|
|
// then
|
|
let tx = tx_with(2*EXPECTED_SIZE as u64, past_block);
|
|
// trigger a garbage collection
|
|
assert!(rotator.ban_if_stale(&now, past_block, &tx));
|
|
assert_eq!(rotator.banned_until.read().len(), EXPECTED_SIZE);
|
|
}
|
|
}
|