mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 10:31:04 +00:00
Run cargo fmt on the whole code base (#9394)
* Run cargo fmt on the whole code base * Second run * Add CI check * Fix compilation * More unnecessary braces * Handle weights * Use --all * Use correct attributes... * Fix UI tests * AHHHHHHHHH * 🤦 * Docs * Fix compilation * 🤷 * Please stop * 🤦 x 2 * More * make rustfmt.toml consistent with polkadot Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
@@ -44,15 +44,17 @@ mod pruning;
|
||||
#[cfg(test)]
|
||||
mod test;
|
||||
|
||||
use std::fmt;
|
||||
use parking_lot::RwLock;
|
||||
use codec::Codec;
|
||||
use std::collections::{HashMap, hash_map::Entry};
|
||||
use noncanonical::NonCanonicalOverlay;
|
||||
use pruning::RefWindow;
|
||||
use log::trace;
|
||||
use parity_util_mem::{MallocSizeOf, malloc_size};
|
||||
use sc_client_api::{StateDbMemoryInfo, MemorySize};
|
||||
use noncanonical::NonCanonicalOverlay;
|
||||
use parity_util_mem::{malloc_size, MallocSizeOf};
|
||||
use parking_lot::RwLock;
|
||||
use pruning::RefWindow;
|
||||
use sc_client_api::{MemorySize, StateDbMemoryInfo};
|
||||
use std::{
|
||||
collections::{hash_map::Entry, HashMap},
|
||||
fmt,
|
||||
};
|
||||
|
||||
const PRUNING_MODE: &[u8] = b"mode";
|
||||
const PRUNING_MODE_ARCHIVE: &[u8] = b"archive";
|
||||
@@ -63,8 +65,35 @@ const PRUNING_MODE_CONSTRAINED: &[u8] = b"constrained";
|
||||
pub type DBValue = Vec<u8>;
|
||||
|
||||
/// Basic set of requirements for the Block hash and node key types.
|
||||
pub trait Hash: Send + Sync + Sized + Eq + PartialEq + Clone + Default + fmt::Debug + Codec + std::hash::Hash + 'static {}
|
||||
impl<T: Send + Sync + Sized + Eq + PartialEq + Clone + Default + fmt::Debug + Codec + std::hash::Hash + 'static> Hash for T {}
|
||||
pub trait Hash:
|
||||
Send
|
||||
+ Sync
|
||||
+ Sized
|
||||
+ Eq
|
||||
+ PartialEq
|
||||
+ Clone
|
||||
+ Default
|
||||
+ fmt::Debug
|
||||
+ Codec
|
||||
+ std::hash::Hash
|
||||
+ 'static
|
||||
{
|
||||
}
|
||||
impl<
|
||||
T: Send
|
||||
+ Sync
|
||||
+ Sized
|
||||
+ Eq
|
||||
+ PartialEq
|
||||
+ Clone
|
||||
+ Default
|
||||
+ fmt::Debug
|
||||
+ Codec
|
||||
+ std::hash::Hash
|
||||
+ 'static,
|
||||
> Hash for T
|
||||
{
|
||||
}
|
||||
|
||||
/// Backend database trait. Read-only.
|
||||
pub trait MetaDb {
|
||||
@@ -168,17 +197,14 @@ pub enum PruningMode {
|
||||
impl PruningMode {
|
||||
/// Create a mode that keeps given number of blocks.
|
||||
pub fn keep_blocks(n: u32) -> PruningMode {
|
||||
PruningMode::Constrained(Constraints {
|
||||
max_blocks: Some(n),
|
||||
max_mem: None,
|
||||
})
|
||||
PruningMode::Constrained(Constraints { max_blocks: Some(n), max_mem: None })
|
||||
}
|
||||
|
||||
/// Is this an archive (either ArchiveAll or ArchiveCanonical) pruning mode?
|
||||
pub fn is_archive(&self) -> bool {
|
||||
match *self {
|
||||
PruningMode::ArchiveAll | PruningMode::ArchiveCanonical => true,
|
||||
PruningMode::Constrained(_) => false
|
||||
PruningMode::Constrained(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,20 +250,12 @@ impl<BlockHash: Hash + MallocSizeOf, Key: Hash + MallocSizeOf> StateDbSync<Block
|
||||
|
||||
let non_canonical: NonCanonicalOverlay<BlockHash, Key> = NonCanonicalOverlay::new(db)?;
|
||||
let pruning: Option<RefWindow<BlockHash, Key>> = match mode {
|
||||
PruningMode::Constrained(Constraints {
|
||||
max_mem: Some(_),
|
||||
..
|
||||
}) => unimplemented!(),
|
||||
PruningMode::Constrained(Constraints { max_mem: Some(_), .. }) => unimplemented!(),
|
||||
PruningMode::Constrained(_) => Some(RefWindow::new(db, ref_counting)?),
|
||||
PruningMode::ArchiveAll | PruningMode::ArchiveCanonical => None,
|
||||
};
|
||||
|
||||
Ok(StateDbSync {
|
||||
mode,
|
||||
non_canonical,
|
||||
pruning,
|
||||
pinned: Default::default(),
|
||||
})
|
||||
Ok(StateDbSync { mode, non_canonical, pruning, pinned: Default::default() })
|
||||
}
|
||||
|
||||
fn check_meta<D: MetaDb>(mode: &PruningMode, db: &D) -> Result<(), Error<D::Error>> {
|
||||
@@ -270,10 +288,7 @@ impl<BlockHash: Hash + MallocSizeOf, Key: Hash + MallocSizeOf> StateDbSync<Block
|
||||
PruningMode::ArchiveAll => {
|
||||
changeset.deleted.clear();
|
||||
// write changes immediately
|
||||
Ok(CommitSet {
|
||||
data: changeset,
|
||||
meta,
|
||||
})
|
||||
Ok(CommitSet { data: changeset, meta })
|
||||
},
|
||||
PruningMode::Constrained(_) | PruningMode::ArchiveCanonical => {
|
||||
let commit = self.non_canonical.insert(hash, number, parent_hash, changeset);
|
||||
@@ -281,7 +296,7 @@ impl<BlockHash: Hash + MallocSizeOf, Key: Hash + MallocSizeOf> StateDbSync<Block
|
||||
c.meta.inserted.extend(meta.inserted);
|
||||
c
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -294,11 +309,10 @@ impl<BlockHash: Hash + MallocSizeOf, Key: Hash + MallocSizeOf> StateDbSync<Block
|
||||
return Ok(commit)
|
||||
}
|
||||
match self.non_canonical.canonicalize(&hash, &mut commit) {
|
||||
Ok(()) => {
|
||||
Ok(()) =>
|
||||
if self.mode == PruningMode::ArchiveCanonical {
|
||||
commit.data.deleted.clear();
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
if let Some(ref mut pruning) = self.pruning {
|
||||
@@ -319,31 +333,30 @@ impl<BlockHash: Hash + MallocSizeOf, Key: Hash + MallocSizeOf> StateDbSync<Block
|
||||
if self.best_canonical().map(|c| number > c).unwrap_or(true) {
|
||||
!self.non_canonical.have_block(hash)
|
||||
} else {
|
||||
self.pruning
|
||||
.as_ref()
|
||||
.map_or(
|
||||
false,
|
||||
|pruning| number < pruning.pending() || !pruning.have_block(hash),
|
||||
)
|
||||
self.pruning.as_ref().map_or(false, |pruning| {
|
||||
number < pruning.pending() || !pruning.have_block(hash)
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn prune(&mut self, commit: &mut CommitSet<Key>) {
|
||||
if let (&mut Some(ref mut pruning), &PruningMode::Constrained(ref constraints)) = (&mut self.pruning, &self.mode) {
|
||||
if let (&mut Some(ref mut pruning), &PruningMode::Constrained(ref constraints)) =
|
||||
(&mut self.pruning, &self.mode)
|
||||
{
|
||||
loop {
|
||||
if pruning.window_size() <= constraints.max_blocks.unwrap_or(0) as u64 {
|
||||
break;
|
||||
break
|
||||
}
|
||||
|
||||
if constraints.max_mem.map_or(false, |m| pruning.mem_used() > m) {
|
||||
break;
|
||||
break
|
||||
}
|
||||
|
||||
let pinned = &self.pinned;
|
||||
if pruning.next_hash().map_or(false, |h| pinned.contains_key(&h)) {
|
||||
break;
|
||||
break
|
||||
}
|
||||
pruning.prune_one(commit);
|
||||
}
|
||||
@@ -355,23 +368,17 @@ impl<BlockHash: Hash + MallocSizeOf, Key: Hash + MallocSizeOf> StateDbSync<Block
|
||||
/// For archive an empty commit set is returned.
|
||||
fn revert_one(&mut self) -> Option<CommitSet<Key>> {
|
||||
match self.mode {
|
||||
PruningMode::ArchiveAll => {
|
||||
Some(CommitSet::default())
|
||||
},
|
||||
PruningMode::ArchiveCanonical | PruningMode::Constrained(_) => {
|
||||
self.non_canonical.revert_one()
|
||||
},
|
||||
PruningMode::ArchiveAll => Some(CommitSet::default()),
|
||||
PruningMode::ArchiveCanonical | PruningMode::Constrained(_) =>
|
||||
self.non_canonical.revert_one(),
|
||||
}
|
||||
}
|
||||
|
||||
fn remove(&mut self, hash: &BlockHash) -> Option<CommitSet<Key>> {
|
||||
match self.mode {
|
||||
PruningMode::ArchiveAll => {
|
||||
Some(CommitSet::default())
|
||||
},
|
||||
PruningMode::ArchiveCanonical | PruningMode::Constrained(_) => {
|
||||
self.non_canonical.remove(hash)
|
||||
},
|
||||
PruningMode::ArchiveAll => Some(CommitSet::default()),
|
||||
PruningMode::ArchiveCanonical | PruningMode::Constrained(_) =>
|
||||
self.non_canonical.remove(hash),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -392,7 +399,7 @@ impl<BlockHash: Hash + MallocSizeOf, Key: Hash + MallocSizeOf> StateDbSync<Block
|
||||
} else {
|
||||
Err(PinError::InvalidBlock)
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -412,14 +419,18 @@ impl<BlockHash: Hash + MallocSizeOf, Key: Hash + MallocSizeOf> StateDbSync<Block
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get<D: NodeDb, Q: ?Sized>(&self, key: &Q, db: &D) -> Result<Option<DBValue>, Error<D::Error>>
|
||||
pub fn get<D: NodeDb, Q: ?Sized>(
|
||||
&self,
|
||||
key: &Q,
|
||||
db: &D,
|
||||
) -> Result<Option<DBValue>, Error<D::Error>>
|
||||
where
|
||||
Q: AsRef<D::Key>,
|
||||
Key: std::borrow::Borrow<Q>,
|
||||
Q: std::hash::Hash + Eq,
|
||||
{
|
||||
if let Some(value) = self.non_canonical.get(key) {
|
||||
return Ok(Some(value));
|
||||
return Ok(Some(value))
|
||||
}
|
||||
db.get(key.as_ref()).map_err(|e| Error::Db(e))
|
||||
}
|
||||
@@ -469,9 +480,7 @@ impl<BlockHash: Hash + MallocSizeOf, Key: Hash + MallocSizeOf> StateDb<BlockHash
|
||||
ref_counting: bool,
|
||||
db: &D,
|
||||
) -> Result<StateDb<BlockHash, Key>, Error<D::Error>> {
|
||||
Ok(StateDb {
|
||||
db: RwLock::new(StateDbSync::new(mode, ref_counting, db)?)
|
||||
})
|
||||
Ok(StateDb { db: RwLock::new(StateDbSync::new(mode, ref_counting, db)?) })
|
||||
}
|
||||
|
||||
/// Add a new non-canonical block.
|
||||
@@ -504,11 +513,15 @@ impl<BlockHash: Hash + MallocSizeOf, Key: Hash + MallocSizeOf> StateDb<BlockHash
|
||||
}
|
||||
|
||||
/// Get a value from non-canonical/pruning overlay or the backing DB.
|
||||
pub fn get<D: NodeDb, Q: ?Sized>(&self, key: &Q, db: &D) -> Result<Option<DBValue>, Error<D::Error>>
|
||||
where
|
||||
Q: AsRef<D::Key>,
|
||||
Key: std::borrow::Borrow<Q>,
|
||||
Q: std::hash::Hash + Eq,
|
||||
pub fn get<D: NodeDb, Q: ?Sized>(
|
||||
&self,
|
||||
key: &Q,
|
||||
db: &D,
|
||||
) -> Result<Option<DBValue>, Error<D::Error>>
|
||||
where
|
||||
Q: AsRef<D::Key>,
|
||||
Key: std::borrow::Borrow<Q>,
|
||||
Q: std::hash::Hash + Eq,
|
||||
{
|
||||
self.db.read().get(key, db)
|
||||
}
|
||||
@@ -554,10 +567,12 @@ impl<BlockHash: Hash + MallocSizeOf, Key: Hash + MallocSizeOf> StateDb<BlockHash
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::io;
|
||||
use crate::{
|
||||
test::{make_changeset, make_db, TestDb},
|
||||
Constraints, PruningMode, StateDb,
|
||||
};
|
||||
use sp_core::H256;
|
||||
use crate::{StateDb, PruningMode, Constraints};
|
||||
use crate::test::{make_db, make_changeset, TestDb};
|
||||
use std::io;
|
||||
|
||||
fn make_test_db(settings: PruningMode) -> (TestDb, StateDb<H256, H256>) {
|
||||
let mut db = make_db(&[91, 921, 922, 93, 94]);
|
||||
@@ -679,13 +694,13 @@ mod tests {
|
||||
let state_db = StateDb::new(PruningMode::ArchiveAll, false, &db).unwrap();
|
||||
db.commit(
|
||||
&state_db
|
||||
.insert_block::<io::Error>(
|
||||
&H256::from_low_u64_be(0),
|
||||
0,
|
||||
&H256::from_low_u64_be(0),
|
||||
make_changeset(&[], &[]),
|
||||
)
|
||||
.unwrap(),
|
||||
.insert_block::<io::Error>(
|
||||
&H256::from_low_u64_be(0),
|
||||
0,
|
||||
&H256::from_low_u64_be(0),
|
||||
make_changeset(&[], &[]),
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
let new_mode = PruningMode::Constrained(Constraints { max_blocks: Some(2), max_mem: None });
|
||||
let state_db: Result<StateDb<H256, H256>, _> = StateDb::new(new_mode, false, &db);
|
||||
|
||||
Reference in New Issue
Block a user