Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Storage chains sync (#9171)
Browse files Browse the repository at this point in the history
* Sync storage chains

* Test

* Apply suggestions from code review

Co-authored-by: cheme <emericchevalier.pro@gmail.com>

* Separate block body and indexed body

* Update client/db/src/lib.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

Co-authored-by: cheme <emericchevalier.pro@gmail.com>
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
  • Loading branch information
4 people authored Jul 17, 2021
1 parent b47244c commit 02fe835
Show file tree
Hide file tree
Showing 27 changed files with 221 additions and 59 deletions.
1 change: 1 addition & 0 deletions client/api/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ pub trait BlockImportOperation<Block: BlockT> {
&mut self,
header: Block::Header,
body: Option<Vec<Block::Extrinsic>>,
indexed_body: Option<Vec<Vec<u8>>>,
justifications: Option<Justifications>,
state: NewBlockState,
) -> sp_blockchain::Result<()>;
Expand Down
1 change: 1 addition & 0 deletions client/api/src/in_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ impl<Block: BlockT> backend::BlockImportOperation<Block> for BlockImportOperatio
&mut self,
header: <Block as BlockT>::Header,
body: Option<Vec<<Block as BlockT>::Extrinsic>>,
_indexed_body: Option<Vec<Vec<u8>>>,
justifications: Option<Justifications>,
state: NewBlockState,
) -> sp_blockchain::Result<()> {
Expand Down
10 changes: 8 additions & 2 deletions client/cli/src/arg_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,14 @@ impl Into<sc_network::config::SyncMode> for SyncMode {
fn into(self) -> sc_network::config::SyncMode {
match self {
SyncMode::Full => sc_network::config::SyncMode::Full,
SyncMode::Fast => sc_network::config::SyncMode::Fast { skip_proofs: false },
SyncMode::FastUnsafe => sc_network::config::SyncMode::Fast { skip_proofs: true },
SyncMode::Fast => sc_network::config::SyncMode::Fast {
skip_proofs: false,
storage_chain_mode: false,
},
SyncMode::FastUnsafe => sc_network::config::SyncMode::Fast {
skip_proofs: true,
storage_chain_mode: false,
},
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions client/db/src/changes_tries_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ mod tests {
};
let mut op = backend.begin_operation().unwrap();
backend.begin_state_operation(&mut op, block_id).unwrap();
op.set_block_data(header, None, None, NewBlockState::Best).unwrap();
op.set_block_data(header, None, None, None, NewBlockState::Best).unwrap();
op.update_changes_trie((changes_trie_update, ChangesTrieCacheAction::Clear)).unwrap();
backend.commit_operation(op).unwrap();

Expand Down Expand Up @@ -916,7 +916,7 @@ mod tests {
backend.begin_state_operation(&mut op, BlockId::Hash(block2)).unwrap();
op.mark_finalized(BlockId::Hash(block1), None).unwrap();
op.mark_finalized(BlockId::Hash(block2), None).unwrap();
op.set_block_data(header3, None, None, NewBlockState::Final).unwrap();
op.set_block_data(header3, None, None, None, NewBlockState::Final).unwrap();
backend.commit_operation(op).unwrap();

// insert more unfinalized headers
Expand All @@ -941,7 +941,7 @@ mod tests {
op.mark_finalized(BlockId::Hash(block4), None).unwrap();
op.mark_finalized(BlockId::Hash(block5), None).unwrap();
op.mark_finalized(BlockId::Hash(block6), None).unwrap();
op.set_block_data(header7, None, None, NewBlockState::Final).unwrap();
op.set_block_data(header7, None, None, None, NewBlockState::Final).unwrap();
backend.commit_operation(op).unwrap();
}

Expand Down
43 changes: 40 additions & 3 deletions client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ use sp_arithmetic::traits::Saturating;
use sp_runtime::{generic::{DigestItem, BlockId}, Justification, Justifications, Storage};
use sp_runtime::traits::{
Block as BlockT, Header as HeaderT, NumberFor, Zero, One, SaturatedConversion, HashFor,
Hash,
};
use sp_state_machine::{
DBValue, ChangesTrieTransaction, ChangesTrieCacheAction, UsageInfo as StateUsageInfo,
Expand Down Expand Up @@ -384,6 +385,7 @@ struct PendingBlock<Block: BlockT> {
header: Block::Header,
justifications: Option<Justifications>,
body: Option<Vec<Block::Extrinsic>>,
indexed_body: Option<Vec<Vec<u8>>>,
leaf_state: NewBlockState,
}

Expand Down Expand Up @@ -824,6 +826,7 @@ impl<Block: BlockT> sc_client_api::backend::BlockImportOperation<Block> for Bloc
&mut self,
header: Block::Header,
body: Option<Vec<Block::Extrinsic>>,
indexed_body: Option<Vec<Vec<u8>>>,
justifications: Option<Justifications>,
leaf_state: NewBlockState,
) -> ClientResult<()> {
Expand All @@ -834,6 +837,7 @@ impl<Block: BlockT> sc_client_api::backend::BlockImportOperation<Block> for Bloc
self.pending_block = Some(PendingBlock {
header,
body,
indexed_body,
justifications,
leaf_state,
});
Expand Down Expand Up @@ -1068,7 +1072,7 @@ impl<Block: BlockT> Backend<Block> {

/// Create new memory-backed client backend for tests.
#[cfg(any(test, feature = "test-helpers"))]
fn new_test_with_tx_storage(
pub fn new_test_with_tx_storage(
keep_blocks: u32,
canonicalization_delay: u64,
transaction_storage: TransactionStorageMode,
Expand Down Expand Up @@ -1393,6 +1397,16 @@ impl<Block: BlockT> Backend<Block> {
},
}
}
if let Some(body) = pending_block.indexed_body {
match self.transaction_storage {
TransactionStorageMode::BlockBody => {
debug!(target: "db", "Commit: ignored indexed block body");
},
TransactionStorageMode::StorageChain => {
apply_indexed_body::<Block>(&mut transaction, body);
},
}
}
if let Some(justifications) = pending_block.justifications {
transaction.set_from_vec(columns::JUSTIFICATIONS, &lookup_key, justifications.encode());
}
Expand Down Expand Up @@ -1881,6 +1895,20 @@ fn apply_index_ops<Block: BlockT>(
extrinsic_headers.encode()
}

fn apply_indexed_body<Block: BlockT>(
transaction: &mut Transaction<DbHash>,
body: Vec<Vec<u8>>,
) {
for extrinsic in body {
let hash = sp_runtime::traits::BlakeTwo256::hash(&extrinsic);
transaction.store(
columns::TRANSACTION,
DbHash::from_slice(hash.as_ref()),
extrinsic,
);
}
}

impl<Block> sc_client_api::backend::AuxStore for Backend<Block> where Block: BlockT {
fn insert_aux<
'a,
Expand Down Expand Up @@ -2439,7 +2467,7 @@ pub(crate) mod tests {
};
let mut op = backend.begin_operation().unwrap();
backend.begin_state_operation(&mut op, block_id).unwrap();
op.set_block_data(header, Some(body), None, NewBlockState::Best).unwrap();
op.set_block_data(header, Some(body), None, None, NewBlockState::Best).unwrap();
if let Some(index) = transaction_index {
op.update_transaction_index(index).unwrap();
}
Expand Down Expand Up @@ -2481,6 +2509,7 @@ pub(crate) mod tests {
header,
Some(vec![]),
None,
None,
NewBlockState::Best,
).unwrap();
db.commit_operation(op).unwrap();
Expand Down Expand Up @@ -2537,6 +2566,7 @@ pub(crate) mod tests {
header.clone(),
Some(vec![]),
None,
None,
NewBlockState::Best,
).unwrap();

Expand Down Expand Up @@ -2579,6 +2609,7 @@ pub(crate) mod tests {
header,
Some(vec![]),
None,
None,
NewBlockState::Best,
).unwrap();

Expand Down Expand Up @@ -2622,6 +2653,7 @@ pub(crate) mod tests {
header,
Some(vec![]),
None,
None,
NewBlockState::Best,
).unwrap();

Expand Down Expand Up @@ -2659,6 +2691,7 @@ pub(crate) mod tests {
header,
Some(vec![]),
None,
None,
NewBlockState::Best,
).unwrap();

Expand Down Expand Up @@ -2695,6 +2728,7 @@ pub(crate) mod tests {
header,
Some(vec![]),
None,
None,
NewBlockState::Best,
).unwrap();

Expand Down Expand Up @@ -2730,6 +2764,7 @@ pub(crate) mod tests {
header,
Some(vec![]),
None,
None,
NewBlockState::Best,
).unwrap();

Expand Down Expand Up @@ -3067,6 +3102,7 @@ pub(crate) mod tests {
header.clone(),
Some(vec![]),
None,
None,
NewBlockState::Best,
).unwrap();

Expand Down Expand Up @@ -3106,6 +3142,7 @@ pub(crate) mod tests {
header,
Some(vec![]),
None,
None,
NewBlockState::Normal,
).unwrap();

Expand All @@ -3118,7 +3155,7 @@ pub(crate) mod tests {
let header = backend.blockchain().header(BlockId::Hash(hash1)).unwrap().unwrap();
let mut op = backend.begin_operation().unwrap();
backend.begin_state_operation(&mut op, BlockId::Hash(hash0)).unwrap();
op.set_block_data(header, None, None, NewBlockState::Best).unwrap();
op.set_block_data(header, None, None, None, NewBlockState::Best).unwrap();
backend.commit_operation(op).unwrap();
}

Expand Down
1 change: 1 addition & 0 deletions client/light/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ impl<S, Block> BlockImportOperation<Block> for ImportOperation<Block, S>
&mut self,
header: Block::Header,
_body: Option<Vec<Block::Extrinsic>>,
_indexed_body: Option<Vec<Vec<u8>>>,
_justifications: Option<Justifications>,
state: NewBlockState,
) -> ClientResult<()> {
Expand Down
14 changes: 14 additions & 0 deletions client/network/src/block_request_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ impl<B: BlockT> BlockRequestHandler<B> {
) -> Result<BlockResponse, HandleRequestError> {
let get_header = attributes.contains(BlockAttributes::HEADER);
let get_body = attributes.contains(BlockAttributes::BODY);
let get_indexed_body = attributes.contains(BlockAttributes::INDEXED_BODY);
let get_justification = attributes.contains(BlockAttributes::JUSTIFICATION);

let mut blocks = Vec::new();
Expand Down Expand Up @@ -321,6 +322,18 @@ impl<B: BlockT> BlockRequestHandler<B> {
Vec::new()
};

let indexed_body = if get_indexed_body {
match self.client.block_indexed_body(&BlockId::Hash(hash))? {
Some(transactions) => transactions,
None => {
log::trace!(target: LOG_TARGET, "Missing indexed block data for block request.");
break;
}
}
} else {
Vec::new()
};

let block_data = crate::schema::v1::BlockData {
hash: hash.encode(),
header: if get_header {
Expand All @@ -334,6 +347,7 @@ impl<B: BlockT> BlockRequestHandler<B> {
justification,
is_empty_justification,
justifications,
indexed_body,
};

total_size += block_data.body.len();
Expand Down
4 changes: 3 additions & 1 deletion client/network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,9 @@ pub enum SyncMode {
/// Download blocks and the latest state.
Fast {
/// Skip state proof download and verification.
skip_proofs: bool
skip_proofs: bool,
/// Download indexed transactions for recent blocks.
storage_chain_mode: bool,
},
}

Expand Down
1 change: 0 additions & 1 deletion client/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ pub mod light_client_requests;
pub mod state_request_handler;
pub mod config;
pub mod error;
pub mod gossip;
pub mod network_state;
pub mod transactions;

Expand Down
14 changes: 13 additions & 1 deletion client/network/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,13 @@ impl ProtocolConfig {
} else {
match self.sync_mode {
config::SyncMode::Full => sync::SyncMode::Full,
config::SyncMode::Fast { skip_proofs } => sync::SyncMode::LightState { skip_proofs },
config::SyncMode::Fast {
skip_proofs,
storage_chain_mode,
} => sync::SyncMode::LightState {
skip_proofs,
storage_chain_mode
},
}
}
}
Expand Down Expand Up @@ -597,6 +603,11 @@ impl<B: BlockT> Protocol<B> {
} else {
None
},
indexed_body: if request.fields.contains(message::BlockAttributes::INDEXED_BODY) {
Some(block_data.indexed_body)
} else {
None
},
receipt: if !block_data.message_queue.is_empty() {
Some(block_data.receipt)
} else {
Expand Down Expand Up @@ -965,6 +976,7 @@ impl<B: BlockT> Protocol<B> {
hash: header.hash(),
header: Some(header),
body: None,
indexed_body: None,
receipt: None,
message_queue: None,
justification: None,
Expand Down
4 changes: 4 additions & 0 deletions client/network/src/protocol/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ bitflags! {
const MESSAGE_QUEUE = 0b00001000;
/// Include a justification for the block.
const JUSTIFICATION = 0b00010000;
/// Include indexed transactions for a block.
const INDEXED_BODY = 0b00100000;
}
}

Expand Down Expand Up @@ -248,6 +250,8 @@ pub mod generic {
pub header: Option<Header>,
/// Block body if requested.
pub body: Option<Vec<Extrinsic>>,
/// Block body indexed transactions if requested.
pub indexed_body: Option<Vec<Vec<u8>>>,
/// Block receipt if requested.
pub receipt: Option<Vec<u8>>,
/// Block message queue if requested.
Expand Down
Loading

0 comments on commit 02fe835

Please sign in to comment.