Skip to content

Commit

Permalink
guestchain: clear candidates changed flag only if new epoch to start (#…
Browse files Browse the repository at this point in the history
…386)

The Candidates struct has a changed flag indicating whether the set of
candidates has changed (in such a way that it would affect the
validators set). This is used to determine whether a new epoch should be
started or not. Once next epoch is scheduled in the block, the flag
should be cleared.

Alas, the code clears the flag each time block is generated even if a
new epoch isn’t about to start. As a consequence, if set of candidates
changes in the middle of an epoch, the change will not be reflected once
the new epoch is ready to start.

Fix this by clearing the flag only if next_epoch is populated in
produced block.
  • Loading branch information
mina86 authored Sep 2, 2024
1 parent 20932c1 commit 9048b16
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions common/guestchain/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,13 @@ impl<PK: crate::PubKey> ChainManager<PK> {
host_height: crate::HostHeight,
host_timestamp: NonZeroU64,
state_root: CryptoHash,
) -> Result<bool, GenerateError> {
) -> Result<(), GenerateError> {
let next_epoch = self.validate_generate_next(
host_height,
host_timestamp,
&state_root,
)?;
let epoch_ends = self.header.next_epoch_commitment.is_some();
let has_next_epoch = next_epoch.is_some();
let next_block = self.header.generate_next(
host_height,
host_timestamp,
Expand All @@ -227,11 +227,21 @@ impl<PK: crate::PubKey> ChainManager<PK> {
signers: Set::new(),
signing_stake: 0,
});
self.candidates.clear_changed_flag();

Ok(epoch_ends)
if has_next_epoch {
self.candidates.clear_changed_flag();
}

Ok(())
}

/// Verifies whether new block can be generated.
///
/// Like [`generate_next`] returns an error if the new block cannot be
/// generated. If it can, returns an `Ok` value.
///
/// If the new block should contain a next epoch commitment, returns `Some`
/// new epoch. Otherwise returns `None`.
pub fn validate_generate_next(
&self,
host_height: crate::HostHeight,
Expand Down

0 comments on commit 9048b16

Please sign in to comment.