Warp sync part II (#9284)

* Gap sync

* Gap epoch test

* Simplified network requests

* Update client/db/src/utils.rs

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

* Fixed v1 migration and added some comments

* Next epoch is always regular

* Removed fork tree change

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Added a comment and converted assert to error

Co-authored-by: cheme <emericchevalier.pro@gmail.com>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Arkadiy Paronyan
2021-10-07 11:31:39 +02:00
committed by GitHub
parent 9f1c3acb7d
commit e6ff531d0b
29 changed files with 800 additions and 169 deletions
+46 -3
View File
@@ -502,6 +502,11 @@ impl<Block: BlockT> BlockchainDb<Block> {
}
}
fn update_block_gap(&self, gap: Option<(NumberFor<Block>, NumberFor<Block>)>) {
let mut meta = self.meta.write();
meta.block_gap = gap;
}
// Get block changes trie root, if available.
fn changes_trie_root(&self, block: BlockId<Block>) -> ClientResult<Option<Block::Hash>> {
self.header(block).map(|header| {
@@ -538,6 +543,7 @@ impl<Block: BlockT> sc_client_api::blockchain::HeaderBackend<Block> for Blockcha
finalized_number: meta.finalized_number,
finalized_state: meta.finalized_state.clone(),
number_leaves: self.leaves.read().count(),
block_gap: meta.block_gap,
}
}
@@ -1388,9 +1394,10 @@ impl<Block: BlockT> Backend<Block> {
operation.apply_offchain(&mut transaction);
let mut meta_updates = Vec::with_capacity(operation.finalized_blocks.len());
let mut last_finalized_hash = self.blockchain.meta.read().finalized_hash;
let mut last_finalized_num = self.blockchain.meta.read().finalized_number;
let best_num = self.blockchain.meta.read().best_number;
let (best_num, mut last_finalized_hash, mut last_finalized_num, mut block_gap) = {
let meta = self.blockchain.meta.read();
(meta.best_number, meta.finalized_hash, meta.finalized_number, meta.block_gap.clone())
};
let mut changes_trie_cache_ops = None;
for (block, justification) in operation.finalized_blocks {
@@ -1639,6 +1646,41 @@ impl<Block: BlockT> Backend<Block> {
children,
);
}
if let Some((mut start, end)) = block_gap {
if number == start {
start += One::one();
utils::insert_number_to_key_mapping(
&mut transaction,
columns::KEY_LOOKUP,
number,
hash,
)?;
}
if start > end {
transaction.remove(columns::META, meta_keys::BLOCK_GAP);
block_gap = None;
debug!(target: "db", "Removed block gap.");
} else {
block_gap = Some((start, end));
debug!(target: "db", "Update block gap. {:?}", block_gap);
transaction.set(
columns::META,
meta_keys::BLOCK_GAP,
&(start, end).encode(),
);
}
} else if number > best_num + One::one() &&
number > One::one() && self
.blockchain
.header(BlockId::hash(parent_hash))?
.is_none()
{
let gap = (best_num + One::one(), number - One::one());
transaction.set(columns::META, meta_keys::BLOCK_GAP, &gap.encode());
block_gap = Some(gap);
debug!(target: "db", "Detected block gap {:?}", block_gap);
}
}
meta_updates.push(MetaUpdate {
@@ -1716,6 +1758,7 @@ impl<Block: BlockT> Backend<Block> {
for m in meta_updates {
self.blockchain.update_meta(m);
}
self.blockchain.update_block_gap(block_gap);
Ok(())
}
+1
View File
@@ -157,6 +157,7 @@ where
None
},
number_leaves: 1,
block_gap: None,
}
}
+11 -1
View File
@@ -54,6 +54,8 @@ pub mod meta_keys {
pub const FINALIZED_BLOCK: &[u8; 5] = b"final";
/// Last finalized state key.
pub const FINALIZED_STATE: &[u8; 6] = b"fstate";
/// Block gap.
pub const BLOCK_GAP: &[u8; 3] = b"gap";
/// Meta information prefix for list-based caches.
pub const CACHE_META_PREFIX: &[u8; 5] = b"cache";
/// Meta information for changes tries key.
@@ -81,6 +83,8 @@ pub struct Meta<N, H> {
pub genesis_hash: H,
/// Finalized state, if any
pub finalized_state: Option<(H, N)>,
/// Block gap, start and end inclusive, if any.
pub block_gap: Option<(N, N)>,
}
/// A block lookup key: used for canonical lookup from block number to hash
@@ -527,6 +531,7 @@ where
finalized_number: Zero::zero(),
genesis_hash: Default::default(),
finalized_state: None,
block_gap: None,
}),
};
@@ -541,7 +546,7 @@ where
"Opened blockchain db, fetched {} = {:?} ({})",
desc,
hash,
header.number()
header.number(),
);
Ok((hash, *header.number()))
} else {
@@ -558,6 +563,10 @@ where
} else {
None
};
let block_gap = db
.get(COLUMN_META, meta_keys::BLOCK_GAP)
.and_then(|d| Decode::decode(&mut d.as_slice()).ok());
debug!(target: "db", "block_gap={:?}", block_gap);
Ok(Meta {
best_hash,
@@ -566,6 +575,7 @@ where
finalized_number,
genesis_hash,
finalized_state,
block_gap,
})
}