Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove CHTs from snapshots
  • Loading branch information
expenses committed Aug 27, 2020
commit 2b7fd858a34f760f258a2b55ddb0c5f81b8f3ab3
4 changes: 2 additions & 2 deletions bin/node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ pub fn run() -> Result<()> {
runner.async_run(|config| {
let chain_spec = config.chain_spec.cloned_box();
let network_config = config.network.clone();
let NewFullBase { task_manager, client, backend, network_status_sinks, .. }
let NewFullBase { task_manager, client, network_status_sinks, .. }
= new_full_base(config, |_, _| ())?;

Ok((cmd.run(chain_spec, network_config, client, backend, network_status_sinks), task_manager))
Ok((cmd.run(chain_spec, network_config, client, network_status_sinks), task_manager))
})
},
Some(Subcommand::CheckBlock(cmd)) => {
Expand Down
3 changes: 1 addition & 2 deletions bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ pub struct NewFullBase {
pub task_manager: TaskManager,
pub inherent_data_providers: InherentDataProviders,
pub client: Arc<FullClient>,
pub backend: Arc<FullBackend>,
pub network: Arc<NetworkService<Block, <Block as BlockT>::Hash>>,
pub network_status_sinks: sc_service::NetworkStatusSinks<Block>,
pub transaction_pool: Arc<sc_transaction_pool::FullPool<Block, FullClient>>,
Expand Down Expand Up @@ -337,7 +336,7 @@ pub fn new_full_base(

network_starter.start_network();
Ok(NewFullBase {
task_manager, inherent_data_providers, client, backend, network, network_status_sinks,
task_manager, inherent_data_providers, client, network, network_status_sinks,
transaction_pool,
})
}
Expand Down
18 changes: 0 additions & 18 deletions client/api/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,21 +536,3 @@ pub fn changes_tries_state_at_block<'a, Block: BlockT>(
None => Ok(None),
}
}

/// Provide CHT roots. These are stored on a light client and generated dynamically on a full
/// client.
pub trait ProvideChtRoots<Block: BlockT> {
/// Get headers CHT root for given block. Returns None if the block is not a part of any CHT.
fn header_cht_root(
&self,
cht_size: NumberFor<Block>,
block: NumberFor<Block>,
) -> sp_blockchain::Result<Option<Block::Hash>>;

/// Get changes trie CHT root for given block. Returns None if the block is not a part of any CHT.
fn changes_trie_cht_root(
&self,
cht_size: NumberFor<Block>,
block: NumberFor<Block>,
) -> sp_blockchain::Result<Option<Block::Hash>>;
}
20 changes: 9 additions & 11 deletions client/api/src/in_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use sp_state_machine::{
use sp_blockchain::{CachedHeaderMetadata, HeaderMetadata};

use crate::{
backend::{self, NewBlockState, ProvideChtRoots},
backend::{self, NewBlockState},
blockchain::{
self, BlockStatus, HeaderBackend, well_known_cache_keys::Id as CacheKeyId
},
Expand Down Expand Up @@ -447,16 +447,6 @@ impl<Block: BlockT> light::Storage<Block> for Blockchain<Block>
Blockchain::finalize_header(self, id, None)
}

fn cache(&self) -> Option<Arc<dyn blockchain::Cache<Block>>> {
None
}

fn usage_info(&self) -> Option<UsageInfo> {
None
}
}

impl<Block: BlockT> ProvideChtRoots<Block> for Blockchain<Block> {
fn header_cht_root(
&self,
_cht_size: NumberFor<Block>,
Expand All @@ -476,6 +466,14 @@ impl<Block: BlockT> ProvideChtRoots<Block> for Blockchain<Block> {
.ok_or_else(|| sp_blockchain::Error::Backend(format!("Changes trie CHT for block {} not exists", block)))
.map(Some)
}

fn cache(&self) -> Option<Arc<dyn blockchain::Cache<Block>>> {
None
}

fn usage_info(&self) -> Option<UsageInfo> {
None
}
}

/// In-memory operation.
Expand Down
20 changes: 16 additions & 4 deletions client/api/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use sp_blockchain::{
HeaderMetadata, well_known_cache_keys, HeaderBackend, Cache as BlockchainCache,
Error as ClientError, Result as ClientResult,
};
use crate::{backend::{AuxStore, NewBlockState}, UsageInfo, ProvideChtRoots};
use crate::{backend::{AuxStore, NewBlockState}, UsageInfo};

/// Remote call request.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -232,9 +232,7 @@ pub trait FetchChecker<Block: BlockT>: Send + Sync {


/// Light client blockchain storage.
pub trait Storage<Block: BlockT>: AuxStore + HeaderBackend<Block>
+ HeaderMetadata<Block, Error=ClientError> + ProvideChtRoots<Block>
{
pub trait Storage<Block: BlockT>: AuxStore + HeaderBackend<Block> + HeaderMetadata<Block, Error=ClientError> {
/// Store new header. Should refuse to revert any finalized blocks.
///
/// Takes new authorities, the leaf state of the new block, and
Expand All @@ -256,6 +254,20 @@ pub trait Storage<Block: BlockT>: AuxStore + HeaderBackend<Block>
/// Get last finalized header.
fn last_finalized(&self) -> ClientResult<Block::Hash>;

/// Get headers CHT root for given block. Returns None if the block is not pruned (not a part of any CHT).
fn header_cht_root(
&self,
cht_size: NumberFor<Block>,
block: NumberFor<Block>,
) -> ClientResult<Option<Block::Hash>>;

/// Get changes trie CHT root for given block. Returns None if the block is not pruned (not a part of any CHT).
fn changes_trie_cht_root(
&self,
cht_size: NumberFor<Block>,
block: NumberFor<Block>,
) -> ClientResult<Option<Block::Hash>>;

/// Get storage cache.
fn cache(&self) -> Option<Arc<dyn BlockchainCache<Block>>>;

Expand Down
7 changes: 0 additions & 7 deletions client/chain-spec/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,6 @@ where
pub struct LightSyncState<Block: BlockT> {
/// The header of the best finalized block.
pub header: <Block as BlockT>::Header,
/// A list of all CHTs in the chain.
pub chts: Vec<<Block as BlockT>::Hash>,
}

impl<Block: BlockT> LightSyncState<Block> {
Expand All @@ -412,17 +410,13 @@ impl<Block: BlockT> LightSyncState<Block> {

SerializableLightSyncState {
header: StorageData(self.header.encode()),
chts: self.chts.iter().map(|hash| StorageData(hash.encode())).collect(),
}
}

/// Convert from a `SerializableLightSyncState`.
pub fn from_serializable(serialized: &SerializableLightSyncState) -> Result<Self, codec::Error> {
Ok(Self {
header: codec::Decode::decode(&mut &serialized.header.0[..])?,
chts: serialized.chts.iter()
.map(|cht| codec::Decode::decode(&mut &cht.0[..]))
.collect::<Result<_, _>>()?,
})
}
}
Expand All @@ -433,7 +427,6 @@ impl<Block: BlockT> LightSyncState<Block> {
#[serde(deny_unknown_fields)]
pub struct SerializableLightSyncState {
header: StorageData,
chts: Vec<StorageData>,
}

#[cfg(test)]
Expand Down
7 changes: 2 additions & 5 deletions client/cli/src/commands/build_sync_spec_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,24 @@ pub struct BuildSyncSpecCmd {

impl BuildSyncSpecCmd {
/// Run the build-sync-spec command
pub async fn run<B, CL, BA>(
pub async fn run<B, CL>(
&self,
mut spec: Box<dyn ChainSpec>,
network_config: NetworkConfiguration,
client: Arc<CL>,
backend: Arc<BA>,
network_status_sinks: NetworkStatusSinks<B>,
) -> error::Result<()>
where
B: BlockT,
CL: sp_blockchain::HeaderBackend<B>,
BA: sc_client_api::Backend<B>,
<BA as sc_client_api::Backend<B>>::Blockchain: sc_client_api::ProvideChtRoots<B>,
{
if self.sync_first {
network_status_sinks.network_status(std::time::Duration::from_secs(1)).filter(|(status, _)| {
ready(status.sync_state == sc_network::SyncState::Idle && status.num_sync_peers > 0)
}).into_future().map(drop).await;
}

let light_sync_state = build_light_sync_state(client, backend)?;
let light_sync_state = build_light_sync_state(client)?;
spec.set_light_sync_state(light_sync_state.to_serializable());

info!("Building chain spec");
Expand Down
66 changes: 3 additions & 63 deletions client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ use std::collections::{HashMap, HashSet};

use sc_client_api::{
UsageInfo, MemoryInfo, IoInfo, MemorySize,
backend::{NewBlockState, PrunableStateChangesTrieStorage, ProvideChtRoots},
leaves::{LeafSet, FinalizationDisplaced}, cht,
backend::{NewBlockState, PrunableStateChangesTrieStorage},
leaves::{LeafSet, FinalizationDisplaced},
};
use sp_blockchain::{
Result as ClientResult, Error as ClientError,
Expand All @@ -70,7 +70,7 @@ use sp_core::ChangesTrieConfiguration;
use sp_core::offchain::storage::{OffchainOverlayedChange, OffchainOverlayedChanges};
use sp_core::storage::{well_known_keys, ChildInfo};
use sp_arithmetic::traits::Saturating;
use sp_runtime::{generic::{DigestItem, BlockId}, Justification, Storage};
use sp_runtime::{generic::BlockId, Justification, Storage};
use sp_runtime::traits::{
Block as BlockT, Header as HeaderT, NumberFor, Zero, One, SaturatedConversion, HashFor,
};
Expand Down Expand Up @@ -405,14 +405,6 @@ impl<Block: BlockT> BlockchainDb<Block> {
meta.finalized_hash = hash;
}
}

// 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()))
}
}

impl<Block: BlockT> sc_client_api::blockchain::HeaderBackend<Block> for BlockchainDb<Block> {
Expand Down Expand Up @@ -533,58 +525,6 @@ impl<Block: BlockT> HeaderMetadata<Block> for BlockchainDb<Block> {
}
}

impl<Block: BlockT> ProvideChtRoots<Block> for BlockchainDb<Block> {
fn header_cht_root(
&self,
cht_size: NumberFor<Block>,
block: NumberFor<Block>,
) -> sp_blockchain::Result<Option<Block::Hash>> {
let cht_number = match cht::block_to_cht_number(cht_size, block) {
Some(number) => number,
None => return Ok(None),
};

let cht_start: NumberFor<Block> = cht::start_number(cht::size(), cht_number);

let mut current_num = cht_start;
let cht_range = ::std::iter::from_fn(|| {
let old_current_num = current_num;
current_num = current_num + One::one();
Some(old_current_num)
});

cht::compute_root::<Block::Header, HashFor<Block>, _>(
cht::size(), cht_number, cht_range.map(|num| self.hash(num))
).map(Some)
}

fn changes_trie_cht_root(
&self,
cht_size: NumberFor<Block>,
block: NumberFor<Block>,
) -> sp_blockchain::Result<Option<Block::Hash>> {
let cht_number = match cht::block_to_cht_number(cht_size, block) {
Some(number) => number,
None => return Ok(None),
};

let cht_start: NumberFor<Block> = cht::start_number(cht::size(), cht_number);

let mut current_num = cht_start;
let cht_range = ::std::iter::from_fn(|| {
let old_current_num = current_num;
current_num = current_num + One::one();
Some(old_current_num)
});

cht::compute_root::<Block::Header, HashFor<Block>, _>(
cht::size(),
cht_number,
cht_range.map(|num| self.changes_trie_root(BlockId::Number(num))),
).map(Some)
}
}

/// Database transaction
pub struct BlockImportOperation<Block: BlockT> {
old_state: SyncingCachingState<RefTrackingState<Block>, Block>,
Expand Down
38 changes: 17 additions & 21 deletions client/db/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::convert::TryInto;
use parking_lot::RwLock;

use sc_client_api::{
cht, backend::{AuxStore, NewBlockState, ProvideChtRoots}, UsageInfo,
cht, backend::{AuxStore, NewBlockState}, UsageInfo,
blockchain::{
BlockStatus, Cache as BlockchainCache, Info as BlockchainInfo,
},
Expand Down Expand Up @@ -523,6 +523,22 @@ impl<Block> Storage<Block> for LightStorage<Block>
}
}

fn header_cht_root(
&self,
cht_size: NumberFor<Block>,
block: NumberFor<Block>,
) -> ClientResult<Option<Block::Hash>> {
self.read_cht_root(HEADER_CHT_PREFIX, cht_size, block)
}

fn changes_trie_cht_root(
&self,
cht_size: NumberFor<Block>,
block: NumberFor<Block>,
) -> ClientResult<Option<Block::Hash>> {
self.read_cht_root(CHANGES_TRIE_CHT_PREFIX, cht_size, block)
}

fn finalize_header(&self, id: BlockId<Block>) -> ClientResult<()> {
if let Some(header) = self.header(id)? {
let mut transaction = Transaction::new();
Expand Down Expand Up @@ -596,26 +612,6 @@ impl<Block> Storage<Block> for LightStorage<Block>
}
}

impl<Block> ProvideChtRoots<Block> for LightStorage<Block>
where Block: BlockT,
{
fn header_cht_root(
&self,
cht_size: NumberFor<Block>,
block: NumberFor<Block>,
) -> ClientResult<Option<Block::Hash>> {
self.read_cht_root(HEADER_CHT_PREFIX, cht_size, block)
}

fn changes_trie_cht_root(
&self,
cht_size: NumberFor<Block>,
block: NumberFor<Block>,
) -> ClientResult<Option<Block::Hash>> {
self.read_cht_root(CHANGES_TRIE_CHT_PREFIX, cht_size, block)
}
}

/// Build the key for inserting header-CHT at given block.
fn cht_key<N: TryInto<u32>>(cht_type: u8, block: N) -> ClientResult<[u8; 5]> {
let mut key = [cht_type; 5];
Expand Down
20 changes: 1 addition & 19 deletions client/light/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use sp_blockchain::{
};
pub use sc_client_api::{
backend::{
AuxStore, NewBlockState, ProvideChtRoots,
AuxStore, NewBlockState,
},
blockchain::{
Backend as BlockchainBackend, BlockStatus, Cache as BlockchainCache,
Expand Down Expand Up @@ -173,21 +173,3 @@ impl<S, Block: BlockT> RemoteBlockchain<Block> for Blockchain<S>
}))
}
}

impl<S: Storage<Block>, Block: BlockT> ProvideChtRoots<Block> for Blockchain<S> {
fn header_cht_root(
&self,
cht_size: NumberFor<Block>,
block: NumberFor<Block>,
) -> sp_blockchain::Result<Option<Block::Hash>> {
self.storage().header_cht_root(cht_size, block)
}

fn changes_trie_cht_root(
&self,
cht_size: NumberFor<Block>,
block: NumberFor<Block>,
) -> sp_blockchain::Result<Option<Block::Hash>> {
self.storage().changes_trie_cht_root(cht_size, block)
}
}
Loading