Skip to content

Commit

Permalink
save checkpoint when syncing, correct state root in genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Nov 8, 2024
1 parent b1dd6a7 commit fa1abf0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
6 changes: 6 additions & 0 deletions dan_layer/consensus/src/hotstuff/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ pub enum ProposalValidationError {
},
#[error("Node proposed by {proposed_by} with hash {hash} is the genesis block")]
ProposingGenesisBlock { proposed_by: String, hash: BlockId },
#[error("Parent {parent_id} not found in block {block_id} proposed by {proposed_by}")]
ParentNotFound {
proposed_by: String,
parent_id: BlockId,
block_id: BlockId,
},
#[error("Justified block {justify_block} for proposed block {block_description} by {proposed_by} not found")]
JustifyBlockNotFound {
proposed_by: String,
Expand Down
9 changes: 9 additions & 0 deletions dan_layer/consensus/src/hotstuff/on_receive_local_proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,15 @@ impl<TConsensusSpec: ConsensusSpec> OnReceiveLocalProposalHandler<TConsensusSpec
.into());
};

if candidate_block.justifies_parent() && !candidate_block.parent_exists(tx)? {
return Err(ProposalValidationError::ParentNotFound {
proposed_by: candidate_block.proposed_by().to_string(),
parent_id: *candidate_block.parent(),
block_id: *candidate_block.id(),
}
.into());
}

if justify_block.height() != candidate_block.justify().block_height() {
return Err(ProposalValidationError::JustifyBlockInvalid {
proposed_by: candidate_block.proposed_by().to_string(),
Expand Down
10 changes: 3 additions & 7 deletions dan_layer/consensus/src/hotstuff/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,8 @@ impl<TConsensusSpec: ConsensusSpec> HotstuffWorker<TConsensusSpec> {

fn create_genesis_block_if_required(&self, epoch: Epoch, shard_group: ShardGroup) -> Result<(), HotStuffError> {
self.state_store.with_write_tx(|tx| {
let checkpoint = EpochCheckpoint::get(&**tx, epoch).optional()?;
let previous_epoch = epoch.saturating_sub(Epoch(1));
let checkpoint = EpochCheckpoint::get(&**tx, previous_epoch).optional()?;
let state_merkle_root = checkpoint
.map(|cp| cp.compute_state_merkle_root())
.transpose()?
Expand All @@ -889,11 +890,6 @@ impl<TConsensusSpec: ConsensusSpec> HotstuffWorker<TConsensusSpec> {
zero_block.justify().insert(tx)?;
zero_block.insert(tx)?;
zero_block.set_as_justified(tx)?;
zero_block.as_locked_block().set(tx)?;
// zero_block.as_leaf_block().set(tx)?;
zero_block.as_last_executed().set(tx)?;
zero_block.as_last_voted().set(tx)?;
zero_block.justify().as_high_qc().set(tx)?;
zero_block.commit_diff(tx, BlockDiff::empty(*zero_block.id()))?;
}

Expand All @@ -906,7 +902,7 @@ impl<TConsensusSpec: ConsensusSpec> HotstuffWorker<TConsensusSpec> {
);
if !genesis.exists(&**tx)? {
info!(target: LOG_TARGET, "✨Creating genesis block {genesis}");
genesis.justify().insert(tx)?;
genesis.justify().save(tx)?;
genesis.insert(tx)?;
genesis.set_as_justified(tx)?;
genesis.as_locked_block().set(tx)?;
Expand Down
1 change: 1 addition & 0 deletions dan_layer/rpc_state_sync/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ where TConsensusSpec: ConsensusSpec<Addr = PeerAddress> + Send + Sync + 'static
info!(target: LOG_TARGET, "🛜 Checkpoint: {checkpoint}");

self.validate_checkpoint(&checkpoint)?;
self.state_store.with_write_tx(|tx| checkpoint.save(tx))?;

match self.start_state_sync(&mut client, shard, &checkpoint).await {
Ok(current_version) => {
Expand Down
11 changes: 5 additions & 6 deletions integration_tests/tests/features/state_sync.feature
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ Feature: State Sync
When I wait for validator VN has leaf block height of at least 1 at epoch 3
When I wait for validator VN2 has leaf block height of at least 1 at epoch 3

# FIXME: These steps fail (because VN2 does not participate timeously in consensus)
# When I create an account UNUSED4 via the wallet daemon WALLET_D
# When I create an account UNUSED5 via the wallet daemon WALLET_D
#
# When I wait for validator VN has leaf block height of at least 5 at epoch 3
# When I wait for validator VN2 has leaf block height of at least 5 at epoch 3
When I create an account UNUSED4 via the wallet daemon WALLET_D
When I create an account UNUSED5 via the wallet daemon WALLET_D

When I wait for validator VN has leaf block height of at least 5 at epoch 3
When I wait for validator VN2 has leaf block height of at least 5 at epoch 3

0 comments on commit fa1abf0

Please sign in to comment.