2021-09-22 00:37:28 +00:00
|
|
|
//! Implementation of historic state reconstruction (given complete block history).
|
|
|
|
use crate::hot_cold_store::{HotColdDB, HotColdDBError};
|
2023-04-12 01:48:22 +00:00
|
|
|
use crate::{Error, ItemStore};
|
2021-09-22 00:37:28 +00:00
|
|
|
use itertools::{process_results, Itertools};
|
|
|
|
use slog::info;
|
2021-12-21 06:30:52 +00:00
|
|
|
use state_processing::{
|
2022-10-15 22:25:54 +00:00
|
|
|
per_block_processing, per_slot_processing, BlockSignatureStrategy, ConsensusContext,
|
2023-05-09 10:48:15 +00:00
|
|
|
StateProcessingStrategy, VerifyBlockRoot,
|
2021-12-21 06:30:52 +00:00
|
|
|
};
|
2021-09-22 00:37:28 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use types::{EthSpec, Hash256};
|
|
|
|
|
|
|
|
impl<E, Hot, Cold> HotColdDB<E, Hot, Cold>
|
|
|
|
where
|
|
|
|
E: EthSpec,
|
2023-04-12 01:48:22 +00:00
|
|
|
Hot: ItemStore<E>,
|
|
|
|
Cold: ItemStore<E>,
|
2021-09-22 00:37:28 +00:00
|
|
|
{
|
|
|
|
pub fn reconstruct_historic_states(self: &Arc<Self>) -> Result<(), Error> {
|
2023-10-31 10:31:02 +00:00
|
|
|
let Some(mut anchor) = self.get_anchor_info() else {
|
2021-09-22 00:37:28 +00:00
|
|
|
// Nothing to do, history is complete.
|
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check that all historic blocks are known.
|
|
|
|
if anchor.oldest_block_slot != 0 {
|
|
|
|
return Err(Error::MissingHistoricBlocks {
|
|
|
|
oldest_block_slot: anchor.oldest_block_slot,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
info!(
|
|
|
|
self.log,
|
|
|
|
"Beginning historic state reconstruction";
|
|
|
|
"start_slot" => anchor.state_lower_limit,
|
|
|
|
);
|
|
|
|
|
|
|
|
let slots_per_restore_point = self.config.slots_per_restore_point;
|
|
|
|
|
|
|
|
// Iterate blocks from the state lower limit to the upper limit.
|
|
|
|
let lower_limit_slot = anchor.state_lower_limit;
|
|
|
|
let split = self.get_split_info();
|
|
|
|
let upper_limit_state = self.get_restore_point(
|
|
|
|
anchor.state_upper_limit.as_u64() / slots_per_restore_point,
|
|
|
|
&split,
|
|
|
|
)?;
|
|
|
|
let upper_limit_slot = upper_limit_state.slot();
|
|
|
|
|
|
|
|
// Use a dummy root, as we never read the block for the upper limit state.
|
|
|
|
let upper_limit_block_root = Hash256::repeat_byte(0xff);
|
|
|
|
|
2021-12-21 06:30:52 +00:00
|
|
|
let block_root_iter = self.forwards_block_roots_iterator(
|
2021-09-22 00:37:28 +00:00
|
|
|
lower_limit_slot,
|
|
|
|
upper_limit_state,
|
|
|
|
upper_limit_block_root,
|
|
|
|
&self.spec,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// The state to be advanced.
|
|
|
|
let mut state = self
|
|
|
|
.load_cold_state_by_slot(lower_limit_slot)?
|
|
|
|
.ok_or(HotColdDBError::MissingLowerLimitState(lower_limit_slot))?;
|
|
|
|
|
2023-06-30 01:13:06 +00:00
|
|
|
state.build_caches(&self.spec)?;
|
2021-09-22 00:37:28 +00:00
|
|
|
|
|
|
|
process_results(block_root_iter, |iter| -> Result<(), Error> {
|
|
|
|
let mut io_batch = vec![];
|
|
|
|
|
|
|
|
let mut prev_state_root = None;
|
|
|
|
|
|
|
|
for ((prev_block_root, _), (block_root, slot)) in iter.tuple_windows() {
|
|
|
|
let is_skipped_slot = prev_block_root == block_root;
|
|
|
|
|
|
|
|
let block = if is_skipped_slot {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(
|
Separate execution payloads in the DB (#3157)
## Proposed Changes
Reduce post-merge disk usage by not storing finalized execution payloads in Lighthouse's database.
:warning: **This is achieved in a backwards-incompatible way for networks that have already merged** :warning:. Kiln users and shadow fork enjoyers will be unable to downgrade after running the code from this PR. The upgrade migration may take several minutes to run, and can't be aborted after it begins.
The main changes are:
- New column in the database called `ExecPayload`, keyed by beacon block root.
- The `BeaconBlock` column now stores blinded blocks only.
- Lots of places that previously used full blocks now use blinded blocks, e.g. analytics APIs, block replay in the DB, etc.
- On finalization:
- `prune_abanonded_forks` deletes non-canonical payloads whilst deleting non-canonical blocks.
- `migrate_db` deletes finalized canonical payloads whilst deleting finalized states.
- Conversions between blinded and full blocks are implemented in a compositional way, duplicating some work from Sean's PR #3134.
- The execution layer has a new `get_payload_by_block_hash` method that reconstructs a payload using the EE's `eth_getBlockByHash` call.
- I've tested manually that it works on Kiln, using Geth and Nethermind.
- This isn't necessarily the most efficient method, and new engine APIs are being discussed to improve this: https://github.com/ethereum/execution-apis/pull/146.
- We're depending on the `ethers` master branch, due to lots of recent changes. We're also using a workaround for https://github.com/gakonst/ethers-rs/issues/1134.
- Payload reconstruction is used in the HTTP API via `BeaconChain::get_block`, which is now `async`. Due to the `async` fn, the `blocking_json` wrapper has been removed.
- Payload reconstruction is used in network RPC to serve blocks-by-{root,range} responses. Here the `async` adjustment is messier, although I think I've managed to come up with a reasonable compromise: the handlers take the `SendOnDrop` by value so that they can drop it on _task completion_ (after the `fn` returns). Still, this is introducing disk reads onto core executor threads, which may have a negative performance impact (thoughts appreciated).
## Additional Info
- [x] For performance it would be great to remove the cloning of full blocks when converting them to blinded blocks to write to disk. I'm going to experiment with a `put_block` API that takes the block by value, breaks it into a blinded block and a payload, stores the blinded block, and then re-assembles the full block for the caller.
- [x] We should measure the latency of blocks-by-root and blocks-by-range responses.
- [x] We should add integration tests that stress the payload reconstruction (basic tests done, issue for more extensive tests: https://github.com/sigp/lighthouse/issues/3159)
- [x] We should (manually) test the schema v9 migration from several prior versions, particularly as blocks have changed on disk and some migrations rely on being able to load blocks.
Co-authored-by: Paul Hauner <paul@paulhauner.com>
2022-05-12 00:42:17 +00:00
|
|
|
self.get_blinded_block(&block_root)?
|
2021-09-22 00:37:28 +00:00
|
|
|
.ok_or(Error::BlockNotFound(block_root))?,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Advance state to slot.
|
|
|
|
per_slot_processing(&mut state, prev_state_root.take(), &self.spec)
|
|
|
|
.map_err(HotColdDBError::BlockReplaySlotError)?;
|
|
|
|
|
|
|
|
// Apply block.
|
|
|
|
if let Some(block) = block {
|
2022-10-15 22:25:54 +00:00
|
|
|
let mut ctxt = ConsensusContext::new(block.slot())
|
|
|
|
.set_current_block_root(block_root)
|
|
|
|
.set_proposer_index(block.message().proposer_index());
|
|
|
|
|
2021-09-22 00:37:28 +00:00
|
|
|
per_block_processing(
|
|
|
|
&mut state,
|
|
|
|
&block,
|
|
|
|
BlockSignatureStrategy::NoVerification,
|
2023-05-09 10:48:15 +00:00
|
|
|
StateProcessingStrategy::Accurate,
|
2021-12-21 06:30:52 +00:00
|
|
|
VerifyBlockRoot::True,
|
2022-10-15 22:25:54 +00:00
|
|
|
&mut ctxt,
|
2021-09-22 00:37:28 +00:00
|
|
|
&self.spec,
|
|
|
|
)
|
|
|
|
.map_err(HotColdDBError::BlockReplayBlockError)?;
|
|
|
|
|
|
|
|
prev_state_root = Some(block.state_root());
|
|
|
|
}
|
|
|
|
|
|
|
|
let state_root = prev_state_root
|
|
|
|
.ok_or(())
|
|
|
|
.or_else(|_| state.update_tree_hash_cache())?;
|
|
|
|
|
|
|
|
// Stage state for storage in freezer DB.
|
|
|
|
self.store_cold_state(&state_root, &state, &mut io_batch)?;
|
|
|
|
|
|
|
|
// If the slot lies on an epoch boundary, commit the batch and update the anchor.
|
|
|
|
if slot % slots_per_restore_point == 0 || slot + 1 == upper_limit_slot {
|
|
|
|
info!(
|
|
|
|
self.log,
|
|
|
|
"State reconstruction in progress";
|
|
|
|
"slot" => slot,
|
|
|
|
"remaining" => upper_limit_slot - 1 - slot
|
|
|
|
);
|
|
|
|
|
|
|
|
self.cold_db.do_atomically(std::mem::take(&mut io_batch))?;
|
|
|
|
|
|
|
|
// Update anchor.
|
|
|
|
let old_anchor = Some(anchor.clone());
|
|
|
|
|
|
|
|
if slot + 1 == upper_limit_slot {
|
|
|
|
// The two limits have met in the middle! We're done!
|
|
|
|
// Perform one last integrity check on the state reached.
|
|
|
|
let computed_state_root = state.update_tree_hash_cache()?;
|
|
|
|
if computed_state_root != state_root {
|
|
|
|
return Err(Error::StateReconstructionRootMismatch {
|
|
|
|
slot,
|
|
|
|
expected: state_root,
|
|
|
|
computed: computed_state_root,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-05 03:53:17 +00:00
|
|
|
self.compare_and_set_anchor_info_with_write(old_anchor, None)?;
|
2021-09-22 00:37:28 +00:00
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
} else {
|
|
|
|
// The lower limit has been raised, store it.
|
|
|
|
anchor.state_lower_limit = slot;
|
|
|
|
|
2021-10-05 03:53:17 +00:00
|
|
|
self.compare_and_set_anchor_info_with_write(
|
|
|
|
old_anchor,
|
|
|
|
Some(anchor.clone()),
|
|
|
|
)?;
|
2021-09-22 00:37:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should always reach the `upper_limit_slot` and return early above.
|
|
|
|
Err(Error::StateReconstructionDidNotComplete)
|
|
|
|
})??;
|
|
|
|
|
|
|
|
// Check that the split point wasn't mutated during the state reconstruction process.
|
|
|
|
// It shouldn't have been, due to the serialization of requests through the store migrator,
|
|
|
|
// so this is just a paranoid check.
|
|
|
|
let latest_split = self.get_split_info();
|
|
|
|
if split != latest_split {
|
|
|
|
return Err(Error::SplitPointModified(latest_split.slot, split.slot));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|