mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-30 18:57:57 +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:
+294
-203
@@ -18,31 +18,31 @@
|
||||
|
||||
//! RocksDB-based light client blockchain storage.
|
||||
|
||||
use std::{sync::Arc, collections::HashMap};
|
||||
use std::convert::TryInto;
|
||||
use parking_lot::RwLock;
|
||||
use std::{collections::HashMap, convert::TryInto, sync::Arc};
|
||||
|
||||
use crate::{
|
||||
cache::{ComplexBlockId, DbCache, DbCacheSync, EntryType as CacheEntryType},
|
||||
utils::{self, block_id_to_lookup_key, meta_keys, read_db, read_meta, DatabaseType, Meta},
|
||||
DatabaseSettings, DbHash, FrozenForDuration,
|
||||
};
|
||||
use codec::{Decode, Encode};
|
||||
use log::{debug, trace, warn};
|
||||
use sc_client_api::{
|
||||
cht, backend::{AuxStore, NewBlockState, ProvideChtRoots}, UsageInfo,
|
||||
blockchain::{
|
||||
BlockStatus, Cache as BlockchainCache, Info as BlockchainInfo,
|
||||
},
|
||||
Storage,
|
||||
backend::{AuxStore, NewBlockState, ProvideChtRoots},
|
||||
blockchain::{BlockStatus, Cache as BlockchainCache, Info as BlockchainInfo},
|
||||
cht, Storage, UsageInfo,
|
||||
};
|
||||
use sp_blockchain::{
|
||||
CachedHeaderMetadata, HeaderMetadata, HeaderMetadataCache,
|
||||
Error as ClientError, Result as ClientResult,
|
||||
HeaderBackend as BlockchainHeaderBackend,
|
||||
well_known_cache_keys,
|
||||
well_known_cache_keys, CachedHeaderMetadata, Error as ClientError,
|
||||
HeaderBackend as BlockchainHeaderBackend, HeaderMetadata, HeaderMetadataCache,
|
||||
Result as ClientResult,
|
||||
};
|
||||
use sp_database::{Database, Transaction};
|
||||
use codec::{Decode, Encode};
|
||||
use sp_runtime::generic::{DigestItem, BlockId};
|
||||
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Zero, One, NumberFor, HashFor};
|
||||
use crate::cache::{DbCacheSync, DbCache, ComplexBlockId, EntryType as CacheEntryType};
|
||||
use crate::utils::{self, meta_keys, DatabaseType, Meta, read_db, block_id_to_lookup_key, read_meta};
|
||||
use crate::{DatabaseSettings, FrozenForDuration, DbHash};
|
||||
use log::{trace, warn, debug};
|
||||
use sp_runtime::{
|
||||
generic::{BlockId, DigestItem},
|
||||
traits::{Block as BlockT, HashFor, Header as HeaderT, NumberFor, One, Zero},
|
||||
};
|
||||
|
||||
pub(crate) mod columns {
|
||||
pub const META: u32 = crate::utils::COLUMN_META;
|
||||
@@ -139,8 +139,8 @@ impl<Block: BlockT> LightStorage<Block> {
|
||||
}
|
||||
|
||||
impl<Block> BlockchainHeaderBackend<Block> for LightStorage<Block>
|
||||
where
|
||||
Block: BlockT,
|
||||
where
|
||||
Block: BlockT,
|
||||
{
|
||||
fn header(&self, id: BlockId<Block>) -> ClientResult<Option<Block::Header>> {
|
||||
utils::read_header(&*self.db, columns::KEY_LOOKUP, columns::HEADER, id)
|
||||
@@ -165,12 +165,8 @@ impl<Block> BlockchainHeaderBackend<Block> for LightStorage<Block>
|
||||
|
||||
fn status(&self, id: BlockId<Block>) -> ClientResult<BlockStatus> {
|
||||
let exists = match id {
|
||||
BlockId::Hash(_) => read_db(
|
||||
&*self.db,
|
||||
columns::KEY_LOOKUP,
|
||||
columns::HEADER,
|
||||
id
|
||||
)?.is_some(),
|
||||
BlockId::Hash(_) =>
|
||||
read_db(&*self.db, columns::KEY_LOOKUP, columns::HEADER, id)?.is_some(),
|
||||
BlockId::Number(n) => n <= self.meta.read().best_number,
|
||||
};
|
||||
match exists {
|
||||
@@ -180,7 +176,9 @@ impl<Block> BlockchainHeaderBackend<Block> for LightStorage<Block>
|
||||
}
|
||||
|
||||
fn number(&self, hash: Block::Hash) -> ClientResult<Option<NumberFor<Block>>> {
|
||||
if let Some(lookup_key) = block_id_to_lookup_key::<Block>(&*self.db, columns::KEY_LOOKUP, BlockId::Hash(hash))? {
|
||||
if let Some(lookup_key) =
|
||||
block_id_to_lookup_key::<Block>(&*self.db, columns::KEY_LOOKUP, BlockId::Hash(hash))?
|
||||
{
|
||||
let number = utils::lookup_key_to_number(&lookup_key)?;
|
||||
Ok(Some(number))
|
||||
} else {
|
||||
@@ -196,17 +194,25 @@ impl<Block> BlockchainHeaderBackend<Block> for LightStorage<Block>
|
||||
impl<Block: BlockT> HeaderMetadata<Block> for LightStorage<Block> {
|
||||
type Error = ClientError;
|
||||
|
||||
fn header_metadata(&self, hash: Block::Hash) -> Result<CachedHeaderMetadata<Block>, Self::Error> {
|
||||
self.header_metadata_cache.header_metadata(hash).map_or_else(|| {
|
||||
self.header(BlockId::hash(hash))?.map(|header| {
|
||||
let header_metadata = CachedHeaderMetadata::from(&header);
|
||||
self.header_metadata_cache.insert_header_metadata(
|
||||
header_metadata.hash,
|
||||
header_metadata.clone(),
|
||||
);
|
||||
header_metadata
|
||||
}).ok_or_else(|| ClientError::UnknownBlock(format!("header not found in db: {}", hash)))
|
||||
}, Ok)
|
||||
fn header_metadata(
|
||||
&self,
|
||||
hash: Block::Hash,
|
||||
) -> Result<CachedHeaderMetadata<Block>, Self::Error> {
|
||||
self.header_metadata_cache.header_metadata(hash).map_or_else(
|
||||
|| {
|
||||
self.header(BlockId::hash(hash))?
|
||||
.map(|header| {
|
||||
let header_metadata = CachedHeaderMetadata::from(&header);
|
||||
self.header_metadata_cache
|
||||
.insert_header_metadata(header_metadata.hash, header_metadata.clone());
|
||||
header_metadata
|
||||
})
|
||||
.ok_or_else(|| {
|
||||
ClientError::UnknownBlock(format!("header not found in db: {}", hash))
|
||||
})
|
||||
},
|
||||
Ok,
|
||||
)
|
||||
}
|
||||
|
||||
fn insert_header_metadata(&self, hash: Block::Hash, metadata: CachedHeaderMetadata<Block>) {
|
||||
@@ -221,10 +227,9 @@ impl<Block: BlockT> HeaderMetadata<Block> for LightStorage<Block> {
|
||||
impl<Block: BlockT> LightStorage<Block> {
|
||||
// Get block changes trie root, if available.
|
||||
fn changes_trie_root(&self, block: BlockId<Block>) -> ClientResult<Option<Block::Hash>> {
|
||||
self.header(block)
|
||||
.map(|header| header.and_then(|header|
|
||||
header.digest().log(DigestItem::as_changes_trie_root)
|
||||
.cloned()))
|
||||
self.header(block).map(|header| {
|
||||
header.and_then(|header| header.digest().log(DigestItem::as_changes_trie_root).cloned())
|
||||
})
|
||||
}
|
||||
|
||||
/// Handle setting head within a transaction. `route_to` should be the last
|
||||
@@ -251,14 +256,16 @@ impl<Block: BlockT> LightStorage<Block> {
|
||||
for retracted in tree_route.retracted() {
|
||||
if retracted.hash == meta.finalized_hash {
|
||||
// TODO: can we recover here?
|
||||
warn!("Safety failure: reverting finalized block {:?}",
|
||||
(&retracted.number, &retracted.hash));
|
||||
warn!(
|
||||
"Safety failure: reverting finalized block {:?}",
|
||||
(&retracted.number, &retracted.hash)
|
||||
);
|
||||
}
|
||||
|
||||
utils::remove_number_to_key_mapping(
|
||||
transaction,
|
||||
columns::KEY_LOOKUP,
|
||||
retracted.number
|
||||
retracted.number,
|
||||
)?;
|
||||
}
|
||||
|
||||
@@ -267,7 +274,7 @@ impl<Block: BlockT> LightStorage<Block> {
|
||||
transaction,
|
||||
columns::KEY_LOOKUP,
|
||||
enacted.number,
|
||||
enacted.hash
|
||||
enacted.hash,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
@@ -292,10 +299,11 @@ impl<Block: BlockT> LightStorage<Block> {
|
||||
) -> ClientResult<()> {
|
||||
let meta = self.meta.read();
|
||||
if &meta.finalized_hash != header.parent_hash() {
|
||||
return Err(::sp_blockchain::Error::NonSequentialFinalization(
|
||||
format!("Last finalized {:?} not parent of {:?}",
|
||||
meta.finalized_hash, hash),
|
||||
).into())
|
||||
return Err(::sp_blockchain::Error::NonSequentialFinalization(format!(
|
||||
"Last finalized {:?} not parent of {:?}",
|
||||
meta.finalized_hash, hash
|
||||
))
|
||||
.into())
|
||||
}
|
||||
|
||||
let lookup_key = utils::number_and_hash_to_lookup_key(header.number().clone(), hash)?;
|
||||
@@ -313,12 +321,14 @@ impl<Block: BlockT> LightStorage<Block> {
|
||||
});
|
||||
|
||||
let new_header_cht_root = cht::compute_root::<Block::Header, HashFor<Block>, _>(
|
||||
cht::size(), new_cht_number, cht_range.map(|num| self.hash(num))
|
||||
cht::size(),
|
||||
new_cht_number,
|
||||
cht_range.map(|num| self.hash(num)),
|
||||
)?;
|
||||
transaction.set(
|
||||
columns::CHT,
|
||||
&cht_key(HEADER_CHT_PREFIX, new_cht_start)?,
|
||||
new_header_cht_root.as_ref()
|
||||
new_header_cht_root.as_ref(),
|
||||
);
|
||||
|
||||
// if the header includes changes trie root, let's build a changes tries roots CHT
|
||||
@@ -329,14 +339,16 @@ impl<Block: BlockT> LightStorage<Block> {
|
||||
current_num = current_num + One::one();
|
||||
Some(old_current_num)
|
||||
});
|
||||
let new_changes_trie_cht_root = cht::compute_root::<Block::Header, HashFor<Block>, _>(
|
||||
cht::size(), new_cht_number, cht_range
|
||||
.map(|num| self.changes_trie_root(BlockId::Number(num)))
|
||||
)?;
|
||||
let new_changes_trie_cht_root =
|
||||
cht::compute_root::<Block::Header, HashFor<Block>, _>(
|
||||
cht::size(),
|
||||
new_cht_number,
|
||||
cht_range.map(|num| self.changes_trie_root(BlockId::Number(num))),
|
||||
)?;
|
||||
transaction.set(
|
||||
columns::CHT,
|
||||
&cht_key(CHANGES_TRIE_CHT_PREFIX, new_cht_start)?,
|
||||
new_changes_trie_cht_root.as_ref()
|
||||
new_changes_trie_cht_root.as_ref(),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -354,7 +366,7 @@ impl<Block: BlockT> LightStorage<Block> {
|
||||
transaction,
|
||||
columns::KEY_LOOKUP,
|
||||
prune_block,
|
||||
hash
|
||||
hash,
|
||||
)?;
|
||||
transaction.remove(columns::HEADER, &lookup_key);
|
||||
}
|
||||
@@ -370,7 +382,7 @@ impl<Block: BlockT> LightStorage<Block> {
|
||||
&self,
|
||||
cht_type: u8,
|
||||
cht_size: NumberFor<Block>,
|
||||
block: NumberFor<Block>
|
||||
block: NumberFor<Block>,
|
||||
) -> ClientResult<Option<Block::Hash>> {
|
||||
let no_cht_for_block = || ClientError::Backend(format!("Missing CHT for block {}", block));
|
||||
|
||||
@@ -383,7 +395,8 @@ impl<Block: BlockT> LightStorage<Block> {
|
||||
}
|
||||
|
||||
let cht_start = cht::start_number(cht_size, cht_number);
|
||||
self.db.get(columns::CHT, &cht_key(cht_type, cht_start)?)
|
||||
self.db
|
||||
.get(columns::CHT, &cht_key(cht_type, cht_start)?)
|
||||
.ok_or_else(no_cht_for_block)
|
||||
.and_then(|hash| Block::Hash::decode(&mut &*hash).map_err(|_| no_cht_for_block()))
|
||||
.map(Some)
|
||||
@@ -391,15 +404,20 @@ impl<Block: BlockT> LightStorage<Block> {
|
||||
}
|
||||
|
||||
impl<Block> AuxStore for LightStorage<Block>
|
||||
where Block: BlockT,
|
||||
where
|
||||
Block: BlockT,
|
||||
{
|
||||
fn insert_aux<
|
||||
'a,
|
||||
'b: 'a,
|
||||
'c: 'a,
|
||||
I: IntoIterator<Item=&'a(&'c [u8], &'c [u8])>,
|
||||
D: IntoIterator<Item=&'a &'b [u8]>,
|
||||
>(&self, insert: I, delete: D) -> ClientResult<()> {
|
||||
I: IntoIterator<Item = &'a (&'c [u8], &'c [u8])>,
|
||||
D: IntoIterator<Item = &'a &'b [u8]>,
|
||||
>(
|
||||
&self,
|
||||
insert: I,
|
||||
delete: D,
|
||||
) -> ClientResult<()> {
|
||||
let mut transaction = Transaction::new();
|
||||
for (k, v) in insert {
|
||||
transaction.set(columns::AUX, k, v);
|
||||
@@ -418,7 +436,8 @@ impl<Block> AuxStore for LightStorage<Block>
|
||||
}
|
||||
|
||||
impl<Block> Storage<Block> for LightStorage<Block>
|
||||
where Block: BlockT,
|
||||
where
|
||||
Block: BlockT,
|
||||
{
|
||||
fn import_header(
|
||||
&self,
|
||||
@@ -447,19 +466,12 @@ impl<Block> Storage<Block> for LightStorage<Block>
|
||||
self.set_head_with_transaction(&mut transaction, parent_hash, (number, hash))?;
|
||||
}
|
||||
|
||||
utils::insert_hash_to_key_mapping(
|
||||
&mut transaction,
|
||||
columns::KEY_LOOKUP,
|
||||
number,
|
||||
hash,
|
||||
)?;
|
||||
utils::insert_hash_to_key_mapping(&mut transaction, columns::KEY_LOOKUP, number, hash)?;
|
||||
transaction.set_from_vec(columns::HEADER, &lookup_key, header.encode());
|
||||
|
||||
let header_metadata = CachedHeaderMetadata::from(&header);
|
||||
self.header_metadata_cache.insert_header_metadata(
|
||||
header.hash().clone(),
|
||||
header_metadata,
|
||||
);
|
||||
self.header_metadata_cache
|
||||
.insert_header_metadata(header.hash().clone(), header_metadata);
|
||||
|
||||
let is_genesis = number.is_zero();
|
||||
if is_genesis {
|
||||
@@ -474,25 +486,28 @@ impl<Block> Storage<Block> for LightStorage<Block>
|
||||
};
|
||||
|
||||
if finalized {
|
||||
self.note_finalized(
|
||||
&mut transaction,
|
||||
&header,
|
||||
hash,
|
||||
)?;
|
||||
self.note_finalized(&mut transaction, &header, hash)?;
|
||||
}
|
||||
|
||||
// update changes trie configuration cache
|
||||
if !cache_at.contains_key(&well_known_cache_keys::CHANGES_TRIE_CONFIG) {
|
||||
if let Some(new_configuration) = crate::changes_tries_storage::extract_new_configuration(&header) {
|
||||
cache_at.insert(well_known_cache_keys::CHANGES_TRIE_CONFIG, new_configuration.encode());
|
||||
if let Some(new_configuration) =
|
||||
crate::changes_tries_storage::extract_new_configuration(&header)
|
||||
{
|
||||
cache_at
|
||||
.insert(well_known_cache_keys::CHANGES_TRIE_CONFIG, new_configuration.encode());
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
let mut cache = self.cache.0.write();
|
||||
let cache_ops = cache.transaction(&mut transaction)
|
||||
let cache_ops = cache
|
||||
.transaction(&mut transaction)
|
||||
.on_block_insert(
|
||||
ComplexBlockId::new(*header.parent_hash(), if number.is_zero() { Zero::zero() } else { number - One::one() }),
|
||||
ComplexBlockId::new(
|
||||
*header.parent_hash(),
|
||||
if number.is_zero() { Zero::zero() } else { number - One::one() },
|
||||
),
|
||||
ComplexBlockId::new(hash, number),
|
||||
cache_at,
|
||||
if finalized { CacheEntryType::Final } else { CacheEntryType::NonFinal },
|
||||
@@ -502,9 +517,10 @@ impl<Block> Storage<Block> for LightStorage<Block>
|
||||
debug!("Light DB Commit {:?} ({})", hash, number);
|
||||
|
||||
self.db.commit(transaction)?;
|
||||
cache.commit(cache_ops)
|
||||
.expect("only fails if cache with given name isn't loaded yet;\
|
||||
cache is already loaded because there are cache_ops; qed");
|
||||
cache.commit(cache_ops).expect(
|
||||
"only fails if cache with given name isn't loaded yet;\
|
||||
cache is already loaded because there are cache_ops; qed",
|
||||
);
|
||||
}
|
||||
|
||||
self.update_meta(hash, number, leaf_state.is_best(), finalized);
|
||||
@@ -518,7 +534,11 @@ impl<Block> Storage<Block> for LightStorage<Block>
|
||||
let number = header.number();
|
||||
|
||||
let mut transaction = Transaction::new();
|
||||
self.set_head_with_transaction(&mut transaction, hash.clone(), (number.clone(), hash.clone()))?;
|
||||
self.set_head_with_transaction(
|
||||
&mut transaction,
|
||||
hash.clone(),
|
||||
(number.clone(), hash.clone()),
|
||||
)?;
|
||||
self.db.commit(transaction)?;
|
||||
self.update_meta(hash, header.number().clone(), true, false);
|
||||
|
||||
@@ -536,17 +556,22 @@ impl<Block> Storage<Block> for LightStorage<Block>
|
||||
self.note_finalized(&mut transaction, &header, hash.clone())?;
|
||||
{
|
||||
let mut cache = self.cache.0.write();
|
||||
let cache_ops = cache.transaction(&mut transaction)
|
||||
let cache_ops = cache
|
||||
.transaction(&mut transaction)
|
||||
.on_block_finalize(
|
||||
ComplexBlockId::new(*header.parent_hash(), if number.is_zero() { Zero::zero() } else { number - One::one() }),
|
||||
ComplexBlockId::new(hash, number)
|
||||
ComplexBlockId::new(
|
||||
*header.parent_hash(),
|
||||
if number.is_zero() { Zero::zero() } else { number - One::one() },
|
||||
),
|
||||
ComplexBlockId::new(hash, number),
|
||||
)?
|
||||
.into_ops();
|
||||
|
||||
self.db.commit(transaction)?;
|
||||
cache.commit(cache_ops)
|
||||
.expect("only fails if cache with given name isn't loaded yet;\
|
||||
cache is already loaded because there are cache_ops; qed");
|
||||
cache.commit(cache_ops).expect(
|
||||
"only fails if cache with given name isn't loaded yet;\
|
||||
cache is already loaded because there are cache_ops; qed",
|
||||
);
|
||||
}
|
||||
self.update_meta(hash, header.number().clone(), false, true);
|
||||
|
||||
@@ -566,7 +591,7 @@ impl<Block> Storage<Block> for LightStorage<Block>
|
||||
|
||||
#[cfg(not(target_os = "unknown"))]
|
||||
fn usage_info(&self) -> Option<UsageInfo> {
|
||||
use sc_client_api::{MemoryInfo, IoInfo, MemorySize};
|
||||
use sc_client_api::{IoInfo, MemoryInfo, MemorySize};
|
||||
|
||||
// TODO: reimplement IO stats
|
||||
let database_cache = MemorySize::from_bytes(0);
|
||||
@@ -591,7 +616,7 @@ impl<Block> Storage<Block> for LightStorage<Block>
|
||||
state_reads_cache: 0,
|
||||
state_writes_cache: 0,
|
||||
state_writes_nodes: 0,
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -602,7 +627,8 @@ impl<Block> Storage<Block> for LightStorage<Block>
|
||||
}
|
||||
|
||||
impl<Block> ProvideChtRoots<Block> for LightStorage<Block>
|
||||
where Block: BlockT,
|
||||
where
|
||||
Block: BlockT,
|
||||
{
|
||||
fn header_cht_root(
|
||||
&self,
|
||||
@@ -630,12 +656,14 @@ fn cht_key<N: TryInto<u32>>(cht_type: u8, block: N) -> ClientResult<[u8; 5]> {
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
use sc_client_api::cht;
|
||||
use sp_core::ChangesTrieConfiguration;
|
||||
use sp_runtime::generic::{DigestItem, ChangesTrieSignal};
|
||||
use sp_runtime::testing::{H256 as Hash, Header, Block as RawBlock, ExtrinsicWrapper};
|
||||
use sp_blockchain::{lowest_common_ancestor, tree_route};
|
||||
use super::*;
|
||||
use sc_client_api::cht;
|
||||
use sp_blockchain::{lowest_common_ancestor, tree_route};
|
||||
use sp_core::ChangesTrieConfiguration;
|
||||
use sp_runtime::{
|
||||
generic::{ChangesTrieSignal, DigestItem},
|
||||
testing::{Block as RawBlock, ExtrinsicWrapper, Header, H256 as Hash},
|
||||
};
|
||||
|
||||
type Block = RawBlock<ExtrinsicWrapper<u32>>;
|
||||
type AuthorityId = sp_core::ed25519::Public;
|
||||
@@ -652,7 +680,10 @@ pub(crate) mod tests {
|
||||
|
||||
fn header_with_changes_trie(parent: &Hash, number: u64) -> Header {
|
||||
let mut header = default_header(parent, number);
|
||||
header.digest.logs.push(DigestItem::ChangesTrieRoot([(number % 256) as u8; 32].into()));
|
||||
header
|
||||
.digest
|
||||
.logs
|
||||
.push(DigestItem::ChangesTrieRoot([(number % 256) as u8; 32].into()));
|
||||
header
|
||||
}
|
||||
|
||||
@@ -698,7 +729,8 @@ pub(crate) mod tests {
|
||||
#[test]
|
||||
fn returns_known_header() {
|
||||
let db = LightStorage::new_test();
|
||||
let known_hash = insert_block(&db, HashMap::new(), || default_header(&Default::default(), 0));
|
||||
let known_hash =
|
||||
insert_block(&db, HashMap::new(), || default_header(&Default::default(), 0));
|
||||
let header_by_hash = db.header(BlockId::Hash(known_hash)).unwrap().unwrap();
|
||||
let header_by_number = db.header(BlockId::Number(0)).unwrap().unwrap();
|
||||
assert_eq!(header_by_hash, header_by_number);
|
||||
@@ -714,7 +746,8 @@ pub(crate) mod tests {
|
||||
#[test]
|
||||
fn returns_info() {
|
||||
let db = LightStorage::new_test();
|
||||
let genesis_hash = insert_block(&db, HashMap::new(), || default_header(&Default::default(), 0));
|
||||
let genesis_hash =
|
||||
insert_block(&db, HashMap::new(), || default_header(&Default::default(), 0));
|
||||
let info = db.info();
|
||||
assert_eq!(info.best_hash, genesis_hash);
|
||||
assert_eq!(info.best_number, 0);
|
||||
@@ -729,17 +762,22 @@ pub(crate) mod tests {
|
||||
#[test]
|
||||
fn returns_block_status() {
|
||||
let db = LightStorage::new_test();
|
||||
let genesis_hash = insert_block(&db, HashMap::new(), || default_header(&Default::default(), 0));
|
||||
let genesis_hash =
|
||||
insert_block(&db, HashMap::new(), || default_header(&Default::default(), 0));
|
||||
assert_eq!(db.status(BlockId::Hash(genesis_hash)).unwrap(), BlockStatus::InChain);
|
||||
assert_eq!(db.status(BlockId::Number(0)).unwrap(), BlockStatus::InChain);
|
||||
assert_eq!(db.status(BlockId::Hash(Hash::from_low_u64_be(1))).unwrap(), BlockStatus::Unknown);
|
||||
assert_eq!(
|
||||
db.status(BlockId::Hash(Hash::from_low_u64_be(1))).unwrap(),
|
||||
BlockStatus::Unknown
|
||||
);
|
||||
assert_eq!(db.status(BlockId::Number(1)).unwrap(), BlockStatus::Unknown);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn returns_block_hash() {
|
||||
let db = LightStorage::new_test();
|
||||
let genesis_hash = insert_block(&db, HashMap::new(), || default_header(&Default::default(), 0));
|
||||
let genesis_hash =
|
||||
insert_block(&db, HashMap::new(), || default_header(&Default::default(), 0));
|
||||
assert_eq!(db.hash(0).unwrap(), Some(genesis_hash));
|
||||
assert_eq!(db.hash(1).unwrap(), None);
|
||||
}
|
||||
@@ -749,7 +787,8 @@ pub(crate) mod tests {
|
||||
let raw_db = Arc::new(sp_database::MemDb::default());
|
||||
let db = LightStorage::from_kvdb(raw_db.clone()).unwrap();
|
||||
|
||||
let genesis_hash = insert_block(&db, HashMap::new(), || default_header(&Default::default(), 0));
|
||||
let genesis_hash =
|
||||
insert_block(&db, HashMap::new(), || default_header(&Default::default(), 0));
|
||||
assert_eq!(raw_db.count(columns::HEADER), 1);
|
||||
assert_eq!(raw_db.count(columns::KEY_LOOKUP), 2);
|
||||
|
||||
@@ -760,43 +799,41 @@ pub(crate) mod tests {
|
||||
|
||||
#[test]
|
||||
fn finalized_ancient_headers_are_replaced_with_cht() {
|
||||
fn insert_headers<F: Fn(&Hash, u64) -> Header>(header_producer: F) ->
|
||||
(Arc<sp_database::MemDb>, LightStorage<Block>)
|
||||
{
|
||||
fn insert_headers<F: Fn(&Hash, u64) -> Header>(
|
||||
header_producer: F,
|
||||
) -> (Arc<sp_database::MemDb>, LightStorage<Block>) {
|
||||
let raw_db = Arc::new(sp_database::MemDb::default());
|
||||
let db = LightStorage::from_kvdb(raw_db.clone()).unwrap();
|
||||
let cht_size: u64 = cht::size();
|
||||
let ucht_size: usize = cht_size as _;
|
||||
|
||||
// insert genesis block header (never pruned)
|
||||
let mut prev_hash = insert_final_block(&db, HashMap::new(), || header_producer(&Default::default(), 0));
|
||||
let mut prev_hash =
|
||||
insert_final_block(&db, HashMap::new(), || header_producer(&Default::default(), 0));
|
||||
|
||||
// insert SIZE blocks && ensure that nothing is pruned
|
||||
|
||||
for number in 0..cht::size() {
|
||||
prev_hash = insert_block(&db, HashMap::new(), || header_producer(&prev_hash, 1 + number));
|
||||
prev_hash =
|
||||
insert_block(&db, HashMap::new(), || header_producer(&prev_hash, 1 + number));
|
||||
}
|
||||
assert_eq!(raw_db.count(columns::HEADER), 1 + ucht_size);
|
||||
assert_eq!(raw_db.count(columns::CHT), 0);
|
||||
|
||||
// insert next SIZE blocks && ensure that nothing is pruned
|
||||
for number in 0..(cht_size as _) {
|
||||
prev_hash = insert_block(
|
||||
&db,
|
||||
HashMap::new(),
|
||||
|| header_producer(&prev_hash, 1 + cht_size + number),
|
||||
);
|
||||
prev_hash = insert_block(&db, HashMap::new(), || {
|
||||
header_producer(&prev_hash, 1 + cht_size + number)
|
||||
});
|
||||
}
|
||||
assert_eq!(raw_db.count(columns::HEADER), 1 + ucht_size + ucht_size);
|
||||
assert_eq!(raw_db.count(columns::CHT), 0);
|
||||
|
||||
// insert block #{2 * cht::size() + 1} && check that new CHT is created + headers of this CHT are pruned
|
||||
// nothing is yet finalized, so nothing is pruned.
|
||||
prev_hash = insert_block(
|
||||
&db,
|
||||
HashMap::new(),
|
||||
|| header_producer(&prev_hash, 1 + cht_size + cht_size),
|
||||
);
|
||||
prev_hash = insert_block(&db, HashMap::new(), || {
|
||||
header_producer(&prev_hash, 1 + cht_size + cht_size)
|
||||
});
|
||||
assert_eq!(raw_db.count(columns::HEADER), 2 + ucht_size + ucht_size);
|
||||
assert_eq!(raw_db.count(columns::CHT), 0);
|
||||
|
||||
@@ -839,7 +876,10 @@ pub(crate) mod tests {
|
||||
#[test]
|
||||
fn get_cht_fails_for_non_existent_cht() {
|
||||
let cht_size: u64 = cht::size();
|
||||
assert!(LightStorage::<Block>::new_test().header_cht_root(cht_size, cht_size / 2).unwrap().is_none());
|
||||
assert!(LightStorage::<Block>::new_test()
|
||||
.header_cht_root(cht_size, cht_size / 2)
|
||||
.unwrap()
|
||||
.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -847,26 +887,41 @@ pub(crate) mod tests {
|
||||
let db = LightStorage::new_test();
|
||||
|
||||
// insert 1 + SIZE + SIZE + 1 blocks so that CHT#0 is created
|
||||
let mut prev_hash = insert_final_block(&db, HashMap::new(), || header_with_changes_trie(&Default::default(), 0));
|
||||
let mut prev_hash = insert_final_block(&db, HashMap::new(), || {
|
||||
header_with_changes_trie(&Default::default(), 0)
|
||||
});
|
||||
let cht_size: u64 = cht::size();
|
||||
let ucht_size: usize = cht_size as _;
|
||||
for i in 1..1 + ucht_size + ucht_size + 1 {
|
||||
prev_hash = insert_block(&db, HashMap::new(), || header_with_changes_trie(&prev_hash, i as u64));
|
||||
prev_hash = insert_block(&db, HashMap::new(), || {
|
||||
header_with_changes_trie(&prev_hash, i as u64)
|
||||
});
|
||||
db.finalize_header(BlockId::Hash(prev_hash)).unwrap();
|
||||
}
|
||||
|
||||
let cht_root_1 = db.header_cht_root(cht_size, cht::start_number(cht_size, 0)).unwrap().unwrap();
|
||||
let cht_root_2 = db.header_cht_root(cht_size, cht::start_number(cht_size, 0) + cht_size / 2).unwrap().unwrap();
|
||||
let cht_root_3 = db.header_cht_root(cht_size, cht::end_number(cht_size, 0)).unwrap().unwrap();
|
||||
let cht_root_1 =
|
||||
db.header_cht_root(cht_size, cht::start_number(cht_size, 0)).unwrap().unwrap();
|
||||
let cht_root_2 = db
|
||||
.header_cht_root(cht_size, cht::start_number(cht_size, 0) + cht_size / 2)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let cht_root_3 =
|
||||
db.header_cht_root(cht_size, cht::end_number(cht_size, 0)).unwrap().unwrap();
|
||||
assert_eq!(cht_root_1, cht_root_2);
|
||||
assert_eq!(cht_root_2, cht_root_3);
|
||||
|
||||
let cht_root_1 = db.changes_trie_cht_root(cht_size, cht::start_number(cht_size, 0)).unwrap().unwrap();
|
||||
let cht_root_2 = db.changes_trie_cht_root(
|
||||
cht_size,
|
||||
cht::start_number(cht_size, 0) + cht_size / 2,
|
||||
).unwrap().unwrap();
|
||||
let cht_root_3 = db.changes_trie_cht_root(cht_size, cht::end_number(cht_size, 0)).unwrap().unwrap();
|
||||
let cht_root_1 = db
|
||||
.changes_trie_cht_root(cht_size, cht::start_number(cht_size, 0))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let cht_root_2 = db
|
||||
.changes_trie_cht_root(cht_size, cht::start_number(cht_size, 0) + cht_size / 2)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let cht_root_3 = db
|
||||
.changes_trie_cht_root(cht_size, cht::end_number(cht_size, 0))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(cht_root_1, cht_root_2);
|
||||
assert_eq!(cht_root_2, cht_root_3);
|
||||
}
|
||||
@@ -882,15 +937,23 @@ pub(crate) mod tests {
|
||||
let a3 = insert_block(&db, HashMap::new(), || default_header(&a2, 3));
|
||||
|
||||
// fork from genesis: 2 prong.
|
||||
let b1 = insert_block(&db, HashMap::new(), || header_with_extrinsics_root(&block0, 1, Hash::from([1; 32])));
|
||||
let b1 = insert_block(&db, HashMap::new(), || {
|
||||
header_with_extrinsics_root(&block0, 1, Hash::from([1; 32]))
|
||||
});
|
||||
let b2 = insert_block(&db, HashMap::new(), || default_header(&b1, 2));
|
||||
|
||||
{
|
||||
let tree_route = tree_route(&db, a3, b2).unwrap();
|
||||
|
||||
assert_eq!(tree_route.common_block().hash, block0);
|
||||
assert_eq!(tree_route.retracted().iter().map(|r| r.hash).collect::<Vec<_>>(), vec![a3, a2, a1]);
|
||||
assert_eq!(tree_route.enacted().iter().map(|r| r.hash).collect::<Vec<_>>(), vec![b1, b2]);
|
||||
assert_eq!(
|
||||
tree_route.retracted().iter().map(|r| r.hash).collect::<Vec<_>>(),
|
||||
vec![a3, a2, a1]
|
||||
);
|
||||
assert_eq!(
|
||||
tree_route.enacted().iter().map(|r| r.hash).collect::<Vec<_>>(),
|
||||
vec![b1, b2]
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -898,14 +961,20 @@ pub(crate) mod tests {
|
||||
|
||||
assert_eq!(tree_route.common_block().hash, a1);
|
||||
assert!(tree_route.retracted().is_empty());
|
||||
assert_eq!(tree_route.enacted().iter().map(|r| r.hash).collect::<Vec<_>>(), vec![a2, a3]);
|
||||
assert_eq!(
|
||||
tree_route.enacted().iter().map(|r| r.hash).collect::<Vec<_>>(),
|
||||
vec![a2, a3]
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
let tree_route = tree_route(&db, a3, a1).unwrap();
|
||||
|
||||
assert_eq!(tree_route.common_block().hash, a1);
|
||||
assert_eq!(tree_route.retracted().iter().map(|r| r.hash).collect::<Vec<_>>(), vec![a3, a2]);
|
||||
assert_eq!(
|
||||
tree_route.retracted().iter().map(|r| r.hash).collect::<Vec<_>>(),
|
||||
vec![a3, a2]
|
||||
);
|
||||
assert!(tree_route.enacted().is_empty());
|
||||
}
|
||||
|
||||
@@ -929,7 +998,9 @@ pub(crate) mod tests {
|
||||
let a3 = insert_block(&db, HashMap::new(), || default_header(&a2, 3));
|
||||
|
||||
// fork from genesis: 2 prong.
|
||||
let b1 = insert_block(&db, HashMap::new(), || header_with_extrinsics_root(&block0, 1, Hash::from([1; 32])));
|
||||
let b1 = insert_block(&db, HashMap::new(), || {
|
||||
header_with_extrinsics_root(&block0, 1, Hash::from([1; 32]))
|
||||
});
|
||||
let b2 = insert_block(&db, HashMap::new(), || default_header(&b1, 2));
|
||||
|
||||
{
|
||||
@@ -979,7 +1050,11 @@ pub(crate) mod tests {
|
||||
fn authorities_are_cached() {
|
||||
let db = LightStorage::new_test();
|
||||
|
||||
fn run_checks(db: &LightStorage<Block>, max: u64, checks: &[(u64, Option<Vec<AuthorityId>>)]) {
|
||||
fn run_checks(
|
||||
db: &LightStorage<Block>,
|
||||
max: u64,
|
||||
checks: &[(u64, Option<Vec<AuthorityId>>)],
|
||||
) {
|
||||
for (at, expected) in checks.iter().take_while(|(at, _)| *at <= max) {
|
||||
let actual = authorities(db.cache(), BlockId::Number(*at));
|
||||
assert_eq!(*expected, actual);
|
||||
@@ -990,14 +1065,21 @@ pub(crate) mod tests {
|
||||
HashMap::new()
|
||||
}
|
||||
|
||||
fn make_authorities(authorities: Vec<AuthorityId>) -> HashMap<well_known_cache_keys::Id, Vec<u8>> {
|
||||
fn make_authorities(
|
||||
authorities: Vec<AuthorityId>,
|
||||
) -> HashMap<well_known_cache_keys::Id, Vec<u8>> {
|
||||
let mut map = HashMap::new();
|
||||
map.insert(well_known_cache_keys::AUTHORITIES, authorities.encode());
|
||||
map
|
||||
}
|
||||
|
||||
fn authorities(cache: &dyn BlockchainCache<Block>, at: BlockId<Block>) -> Option<Vec<AuthorityId>> {
|
||||
cache.get_at(&well_known_cache_keys::AUTHORITIES, &at).unwrap_or(None)
|
||||
fn authorities(
|
||||
cache: &dyn BlockchainCache<Block>,
|
||||
at: BlockId<Block>,
|
||||
) -> Option<Vec<AuthorityId>> {
|
||||
cache
|
||||
.get_at(&well_known_cache_keys::AUTHORITIES, &at)
|
||||
.unwrap_or(None)
|
||||
.and_then(|(_, _, val)| Decode::decode(&mut &val[..]).ok())
|
||||
}
|
||||
|
||||
@@ -1021,17 +1103,27 @@ pub(crate) mod tests {
|
||||
(6, Some(vec![auth1(), auth2()])),
|
||||
];
|
||||
|
||||
let hash0 = insert_final_block(&db, same_authorities(), || default_header(&Default::default(), 0));
|
||||
let hash0 = insert_final_block(&db, same_authorities(), || {
|
||||
default_header(&Default::default(), 0)
|
||||
});
|
||||
run_checks(&db, 0, &checks);
|
||||
let hash1 = insert_final_block(&db, same_authorities(), || default_header(&hash0, 1));
|
||||
run_checks(&db, 1, &checks);
|
||||
let hash2 = insert_final_block(&db, make_authorities(vec![auth1()]), || default_header(&hash1, 2));
|
||||
let hash2 = insert_final_block(&db, make_authorities(vec![auth1()]), || {
|
||||
default_header(&hash1, 2)
|
||||
});
|
||||
run_checks(&db, 2, &checks);
|
||||
let hash3 = insert_final_block(&db, make_authorities(vec![auth1()]), || default_header(&hash2, 3));
|
||||
let hash3 = insert_final_block(&db, make_authorities(vec![auth1()]), || {
|
||||
default_header(&hash2, 3)
|
||||
});
|
||||
run_checks(&db, 3, &checks);
|
||||
let hash4 = insert_final_block(&db, make_authorities(vec![auth1(), auth2()]), || default_header(&hash3, 4));
|
||||
let hash4 = insert_final_block(&db, make_authorities(vec![auth1(), auth2()]), || {
|
||||
default_header(&hash3, 4)
|
||||
});
|
||||
run_checks(&db, 4, &checks);
|
||||
let hash5 = insert_final_block(&db, make_authorities(vec![auth1(), auth2()]), || default_header(&hash4, 5));
|
||||
let hash5 = insert_final_block(&db, make_authorities(vec![auth1(), auth2()]), || {
|
||||
default_header(&hash4, 5)
|
||||
});
|
||||
run_checks(&db, 5, &checks);
|
||||
let hash6 = insert_final_block(&db, same_authorities(), || default_header(&hash5, 6));
|
||||
run_checks(&db, 6, &checks);
|
||||
@@ -1043,9 +1135,14 @@ pub(crate) mod tests {
|
||||
// some older non-best blocks are inserted
|
||||
// ... -> B2(1) -> B2_1(1) -> B2_2(2)
|
||||
// => the cache ignores all writes before best finalized block
|
||||
let hash2_1 = insert_non_best_block(&db, make_authorities(vec![auth1()]), || default_header(&hash2, 3));
|
||||
let hash2_1 = insert_non_best_block(&db, make_authorities(vec![auth1()]), || {
|
||||
default_header(&hash2, 3)
|
||||
});
|
||||
assert_eq!(None, authorities(db.cache(), BlockId::Hash(hash2_1)));
|
||||
let hash2_2 = insert_non_best_block(&db, make_authorities(vec![auth1(), auth2()]), || default_header(&hash2_1, 4));
|
||||
let hash2_2 =
|
||||
insert_non_best_block(&db, make_authorities(vec![auth1(), auth2()]), || {
|
||||
default_header(&hash2_1, 4)
|
||||
});
|
||||
assert_eq!(None, authorities(db.cache(), BlockId::Hash(hash2_2)));
|
||||
}
|
||||
|
||||
@@ -1056,51 +1153,41 @@ pub(crate) mod tests {
|
||||
// \> B6_1_1(5)
|
||||
// \> B6_1_2(6) -> B6_1_3(7)
|
||||
|
||||
let hash7 = insert_block(&db, make_authorities(vec![auth3()]), || default_header(&hash6, 7));
|
||||
assert_eq!(
|
||||
authorities(db.cache(), BlockId::Hash(hash6)),
|
||||
Some(vec![auth1(), auth2()]),
|
||||
);
|
||||
let hash7 =
|
||||
insert_block(&db, make_authorities(vec![auth3()]), || default_header(&hash6, 7));
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash6)), Some(vec![auth1(), auth2()]),);
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash7)), Some(vec![auth3()]));
|
||||
let hash8 = insert_block(&db, make_authorities(vec![auth3()]), || default_header(&hash7, 8));
|
||||
assert_eq!(
|
||||
authorities(db.cache(), BlockId::Hash(hash6)),
|
||||
Some(vec![auth1(), auth2()]),
|
||||
);
|
||||
let hash8 =
|
||||
insert_block(&db, make_authorities(vec![auth3()]), || default_header(&hash7, 8));
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash6)), Some(vec![auth1(), auth2()]),);
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash7)), Some(vec![auth3()]));
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash8)), Some(vec![auth3()]));
|
||||
let hash6_1 = insert_block(&db, make_authorities(vec![auth4()]), || default_header(&hash6, 7));
|
||||
assert_eq!(
|
||||
authorities(db.cache(), BlockId::Hash(hash6)),
|
||||
Some(vec![auth1(), auth2()]),
|
||||
);
|
||||
let hash6_1 =
|
||||
insert_block(&db, make_authorities(vec![auth4()]), || default_header(&hash6, 7));
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash6)), Some(vec![auth1(), auth2()]),);
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash7)), Some(vec![auth3()]));
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash8)), Some(vec![auth3()]));
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash6_1)), Some(vec![auth4()]));
|
||||
let hash6_1_1 = insert_non_best_block(&db, make_authorities(vec![auth5()]), || default_header(&hash6_1, 8));
|
||||
assert_eq!(
|
||||
authorities(db.cache(), BlockId::Hash(hash6)),
|
||||
Some(vec![auth1(), auth2()]),
|
||||
);
|
||||
let hash6_1_1 = insert_non_best_block(&db, make_authorities(vec![auth5()]), || {
|
||||
default_header(&hash6_1, 8)
|
||||
});
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash6)), Some(vec![auth1(), auth2()]),);
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash7)), Some(vec![auth3()]));
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash8)), Some(vec![auth3()]));
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash6_1)), Some(vec![auth4()]));
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash6_1_1)), Some(vec![auth5()]));
|
||||
let hash6_1_2 = insert_non_best_block(&db, make_authorities(vec![auth6()]), || default_header(&hash6_1, 8));
|
||||
assert_eq!(
|
||||
authorities(db.cache(), BlockId::Hash(hash6)),
|
||||
Some(vec![auth1(), auth2()]),
|
||||
);
|
||||
let hash6_1_2 = insert_non_best_block(&db, make_authorities(vec![auth6()]), || {
|
||||
default_header(&hash6_1, 8)
|
||||
});
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash6)), Some(vec![auth1(), auth2()]),);
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash7)), Some(vec![auth3()]));
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash8)), Some(vec![auth3()]));
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash6_1)), Some(vec![auth4()]));
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash6_1_1)), Some(vec![auth5()]));
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash6_1_2)), Some(vec![auth6()]));
|
||||
let hash6_2 = insert_block(&db, make_authorities(vec![auth4()]), || default_header(&hash6_1, 8));
|
||||
assert_eq!(
|
||||
authorities(db.cache(), BlockId::Hash(hash6)),
|
||||
Some(vec![auth1(), auth2()]),
|
||||
);
|
||||
let hash6_2 =
|
||||
insert_block(&db, make_authorities(vec![auth4()]), || default_header(&hash6_1, 8));
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash6)), Some(vec![auth1(), auth2()]),);
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash7)), Some(vec![auth3()]));
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash8)), Some(vec![auth3()]));
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash6_1)), Some(vec![auth4()]));
|
||||
@@ -1114,10 +1201,7 @@ pub(crate) mod tests {
|
||||
{
|
||||
// finalize block hash6_1
|
||||
db.finalize_header(BlockId::Hash(hash6_1)).unwrap();
|
||||
assert_eq!(
|
||||
authorities(db.cache(), BlockId::Hash(hash6)),
|
||||
Some(vec![auth1(), auth2()]),
|
||||
);
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash6)), Some(vec![auth1(), auth2()]),);
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash7)), None);
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash8)), None);
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash6_1)), Some(vec![auth4()]));
|
||||
@@ -1126,10 +1210,7 @@ pub(crate) mod tests {
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash6_2)), Some(vec![auth4()]));
|
||||
// finalize block hash6_2
|
||||
db.finalize_header(BlockId::Hash(hash6_2)).unwrap();
|
||||
assert_eq!(
|
||||
authorities(db.cache(), BlockId::Hash(hash6)),
|
||||
Some(vec![auth1(), auth2()]),
|
||||
);
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash6)), Some(vec![auth1(), auth2()]),);
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash7)), None);
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash8)), None);
|
||||
assert_eq!(authorities(db.cache(), BlockId::Hash(hash6_1)), Some(vec![auth4()]));
|
||||
@@ -1142,7 +1223,8 @@ pub(crate) mod tests {
|
||||
#[test]
|
||||
fn database_is_reopened() {
|
||||
let db = LightStorage::new_test();
|
||||
let hash0 = insert_final_block(&db, HashMap::new(), || default_header(&Default::default(), 0));
|
||||
let hash0 =
|
||||
insert_final_block(&db, HashMap::new(), || default_header(&Default::default(), 0));
|
||||
assert_eq!(db.info().best_hash, hash0);
|
||||
assert_eq!(db.header(BlockId::Hash(hash0)).unwrap().unwrap().hash(), hash0);
|
||||
|
||||
@@ -1157,7 +1239,8 @@ pub(crate) mod tests {
|
||||
let db = LightStorage::<Block>::new_test();
|
||||
|
||||
// insert aux1 + aux2 using direct store access
|
||||
db.insert_aux(&[(&[1][..], &[101][..]), (&[2][..], &[102][..])], ::std::iter::empty()).unwrap();
|
||||
db.insert_aux(&[(&[1][..], &[101][..]), (&[2][..], &[102][..])], ::std::iter::empty())
|
||||
.unwrap();
|
||||
|
||||
// check aux values
|
||||
assert_eq!(db.get_aux(&[1]).unwrap(), Some(vec![101]));
|
||||
@@ -1165,10 +1248,13 @@ pub(crate) mod tests {
|
||||
assert_eq!(db.get_aux(&[3]).unwrap(), None);
|
||||
|
||||
// delete aux1 + insert aux3 using import operation
|
||||
db.import_header(default_header(&Default::default(), 0), HashMap::new(), NewBlockState::Best, vec![
|
||||
(vec![3], Some(vec![103])),
|
||||
(vec![1], None),
|
||||
]).unwrap();
|
||||
db.import_header(
|
||||
default_header(&Default::default(), 0),
|
||||
HashMap::new(),
|
||||
NewBlockState::Best,
|
||||
vec![(vec![3], Some(vec![103])), (vec![1], None)],
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// check aux values
|
||||
assert_eq!(db.get_aux(&[1]).unwrap(), None);
|
||||
@@ -1208,7 +1294,8 @@ pub(crate) mod tests {
|
||||
};
|
||||
|
||||
// restart && check that after restart value is read from the cache
|
||||
let db = LightStorage::<Block>::from_kvdb(storage as Arc<_>).expect("failed to create test-db");
|
||||
let db =
|
||||
LightStorage::<Block>::from_kvdb(storage as Arc<_>).expect("failed to create test-db");
|
||||
assert_eq!(
|
||||
db.cache().get_at(b"test", &BlockId::Number(0)).unwrap(),
|
||||
Some(((0, genesis_hash.unwrap()), None, vec![42])),
|
||||
@@ -1224,7 +1311,9 @@ pub(crate) mod tests {
|
||||
// insert block#0 && block#1 (no value for cache is provided)
|
||||
let hash0 = insert_block(&db, HashMap::new(), || default_header(&Default::default(), 0));
|
||||
assert_eq!(
|
||||
db.cache().get_at(&well_known_cache_keys::CHANGES_TRIE_CONFIG, &BlockId::Number(0)).unwrap()
|
||||
db.cache()
|
||||
.get_at(&well_known_cache_keys::CHANGES_TRIE_CONFIG, &BlockId::Number(0))
|
||||
.unwrap()
|
||||
.map(|(_, _, v)| ChangesTrieConfiguration::decode(&mut &v[..]).unwrap()),
|
||||
None,
|
||||
);
|
||||
@@ -1232,13 +1321,15 @@ pub(crate) mod tests {
|
||||
// insert configuration at block#1 (starts from block#2)
|
||||
insert_block(&db, HashMap::new(), || {
|
||||
let mut header = default_header(&hash0, 1);
|
||||
header.digest_mut().push(
|
||||
DigestItem::ChangesTrieSignal(ChangesTrieSignal::NewConfiguration(new_config.clone()))
|
||||
);
|
||||
header.digest_mut().push(DigestItem::ChangesTrieSignal(
|
||||
ChangesTrieSignal::NewConfiguration(new_config.clone()),
|
||||
));
|
||||
header
|
||||
});
|
||||
assert_eq!(
|
||||
db.cache().get_at(&well_known_cache_keys::CHANGES_TRIE_CONFIG, &BlockId::Number(1)).unwrap()
|
||||
db.cache()
|
||||
.get_at(&well_known_cache_keys::CHANGES_TRIE_CONFIG, &BlockId::Number(1))
|
||||
.unwrap()
|
||||
.map(|(_, _, v)| Option::<ChangesTrieConfiguration>::decode(&mut &v[..]).unwrap()),
|
||||
Some(new_config),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user