Storage chains sync (#9171)

* 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>
This commit is contained in:
Arkadiy Paronyan
2021-07-17 10:58:37 +02:00
committed by GitHub
parent f07a41e87d
commit 5a65bf5515
27 changed files with 221 additions and 59 deletions
@@ -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();
@@ -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
@@ -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();
}
+40 -3
View File
@@ -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,
@@ -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,
}
@@ -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<()> {
@@ -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,
});
@@ -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,
@@ -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());
}
@@ -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,
@@ -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();
}
@@ -2481,6 +2509,7 @@ pub(crate) mod tests {
header,
Some(vec![]),
None,
None,
NewBlockState::Best,
).unwrap();
db.commit_operation(op).unwrap();
@@ -2537,6 +2566,7 @@ pub(crate) mod tests {
header.clone(),
Some(vec![]),
None,
None,
NewBlockState::Best,
).unwrap();
@@ -2579,6 +2609,7 @@ pub(crate) mod tests {
header,
Some(vec![]),
None,
None,
NewBlockState::Best,
).unwrap();
@@ -2622,6 +2653,7 @@ pub(crate) mod tests {
header,
Some(vec![]),
None,
None,
NewBlockState::Best,
).unwrap();
@@ -2659,6 +2691,7 @@ pub(crate) mod tests {
header,
Some(vec![]),
None,
None,
NewBlockState::Best,
).unwrap();
@@ -2695,6 +2728,7 @@ pub(crate) mod tests {
header,
Some(vec![]),
None,
None,
NewBlockState::Best,
).unwrap();
@@ -2730,6 +2764,7 @@ pub(crate) mod tests {
header,
Some(vec![]),
None,
None,
NewBlockState::Best,
).unwrap();
@@ -3067,6 +3102,7 @@ pub(crate) mod tests {
header.clone(),
Some(vec![]),
None,
None,
NewBlockState::Best,
).unwrap();
@@ -3106,6 +3142,7 @@ pub(crate) mod tests {
header,
Some(vec![]),
None,
None,
NewBlockState::Normal,
).unwrap();
@@ -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();
}