Skip to content

Commit

Permalink
Only use database if it's more recent than checkpoint (#2401)
Browse files Browse the repository at this point in the history
* Rename variables to make it clearer

* Only use database if it's more recent than checkpoint

* CHANGELOG
  • Loading branch information
tomaka authored Jun 19, 2022
1 parent a28314d commit aa7a378
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
45 changes: 34 additions & 11 deletions bin/light-base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,35 @@ impl<TChain, TPlat: Platform> Client<TChain, TPlat> {
}),
finalized_serialize::decode_chain(config.database_content),
) {
(Ok(Ok(genesis_ci)), _, Ok((ci, _))) => {
// Use the database if it contains a more recent block than the chain spec checkpoint.
(Ok(Ok(genesis_ci)), checkpoint, Ok((database, _)))
if checkpoint
.as_ref()
.map(|r| r.as_ref().ok())
.flatten()
.map_or(true, |cp| {
cp.as_ref().finalized_block_header.number
< database.as_ref().finalized_block_header.number
}) =>
{
let genesis_header = genesis_ci.as_ref().finalized_block_header.clone();
(ci, genesis_header.into())
(database, genesis_header.into())
}

(Err(chain_spec::FromGenesisStorageError::UnknownStorageItems), _, Ok((ci, _))) => {
// Use the database if it contains a more recent block than the chain spec checkpoint.
(
Err(chain_spec::FromGenesisStorageError::UnknownStorageItems),
checkpoint,
Ok((database, _)),
) if checkpoint
.as_ref()
.map(|r| r.as_ref().ok())
.flatten()
.map_or(true, |cp| {
cp.as_ref().finalized_block_header.number
< database.as_ref().finalized_block_header.number
}) =>
{
let genesis_header = header::Header {
parent_hash: [0; 32],
number: 0,
Expand All @@ -423,7 +446,7 @@ impl<TChain, TPlat: Platform> Client<TChain, TPlat> {
digest: header::DigestRef::empty().into(),
};

(ci, genesis_header)
(database, genesis_header)
}

(Err(chain_spec::FromGenesisStorageError::UnknownStorageItems), None, _) => {
Expand All @@ -438,7 +461,7 @@ impl<TChain, TPlat: Platform> Client<TChain, TPlat> {

(
Err(chain_spec::FromGenesisStorageError::UnknownStorageItems),
Some(Ok(ci)),
Some(Ok(checkpoint)),
_,
) => {
let genesis_header = header::Header {
Expand All @@ -449,7 +472,7 @@ impl<TChain, TPlat: Platform> Client<TChain, TPlat> {
digest: header::DigestRef::empty().into(),
};

(ci, genesis_header)
(checkpoint, genesis_header)
}

(Err(err), _, _) => {
Expand All @@ -473,15 +496,15 @@ impl<TChain, TPlat: Platform> Client<TChain, TPlat> {
}));
}

(Ok(Ok(genesis_ci)), Some(Ok(ci)), _) => {
(Ok(Ok(genesis_ci)), Some(Ok(checkpoint)), _) => {
let genesis_header = genesis_ci.as_ref().finalized_block_header.clone();
(ci, genesis_header.into())
(checkpoint, genesis_header.into())
}

(Ok(Ok(ci)), None, _) => {
(Ok(Ok(genesis_ci)), None, _) => {
let genesis_header =
header::Header::from(ci.as_ref().finalized_block_header.clone());
(ci, genesis_header)
header::Header::from(genesis_ci.as_ref().finalized_block_header.clone());
(genesis_ci, genesis_header)
}
}
};
Expand Down
4 changes: 4 additions & 0 deletions bin/wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Changed

- When a database and a chain specification checkpoint are both provided to `addChain`, the block in the database is used only if it has a higher block number than the block in the chain specification checkpoint. This makes it possible to bypass issues where smoldot is incapable of syncing over a certain block by updating the chain specification, without having to manually clear existing databases. ([#2401](https://github.com/paritytech/smoldot/pull/2401))

## 0.6.19 - 2022-06-14

### Fixed
Expand Down

0 comments on commit aa7a378

Please sign in to comment.