-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
STR-949 chainstate manager v2 and minor chainstate restructures #633
Open
delbonis
wants to merge
11
commits into
main
Choose a base branch
from
STR-949-chainstate-mgr-v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5e07076
primitives: added block/epoch commitment types
delbonis b0979a4
primitives: fixed typo
delbonis 40c2143
state: reworked chainstate `WriteBatch` and `StateCache` types to dir…
delbonis 417e501
storage: added `ChainstateOps` and other adjustments
delbonis 21263ab
storage: added most of chainstate manager
delbonis 9661f16
primitives: removed sanity concept from `EpochCommitment`
delbonis f386384
primitives: added epoch and l2 modules to prelude exports
delbonis 6e4877d
misc: refactoring to make use of new chainstate interface in more places
delbonis 57cf761
consensus-logic: fixed CSM tests
delbonis 22acb0c
consensus-logic: fix unused imports
delbonis 164930d
misc: updated `Chainstate` to start to use new block commitment types
delbonis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
//! Types relating to epoch bookkeeping. | ||
//! | ||
//! An epoch of a range of sequential blocks defined by the terminal block of | ||
//! the epoch going back to (but not including) the terminal block of a previous | ||
//! epoch. This uniquely identifies the epoch's final state indirectly, | ||
//! although it's possible for conflicting epochs with different terminal blocks | ||
//! to exist in theory, depending on the consensus algorithm. | ||
//! | ||
//! Epochs are *usually* always the same number of slots, but we're not | ||
//! guaranteeing this yet, so we always include both the epoch number and slot | ||
//! number of the terminal block. | ||
//! | ||
//! We also have a sentinel "null" epoch used to refer to the "finalized epoch" | ||
//! as of the genesis block. | ||
|
||
use arbitrary::Arbitrary; | ||
use borsh::{BorshDeserialize, BorshSerialize}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::{ | ||
buf::Buf32, | ||
l2::{L2BlockCommitment, L2BlockId}, | ||
}; | ||
|
||
/// Commits to a particular epoch by the last block and slot. | ||
#[derive( | ||
Copy, | ||
Clone, | ||
Debug, | ||
Eq, | ||
PartialEq, | ||
Ord, | ||
PartialOrd, | ||
Hash, | ||
Arbitrary, | ||
BorshDeserialize, | ||
BorshSerialize, | ||
Deserialize, | ||
Serialize, | ||
)] | ||
pub struct EpochCommitment { | ||
epoch: u64, | ||
last_slot: u64, | ||
last_blkid: L2BlockId, | ||
} | ||
|
||
impl EpochCommitment { | ||
pub fn new(epoch: u64, last_slot: u64, last_blkid: L2BlockId) -> Self { | ||
Self { | ||
epoch, | ||
last_slot, | ||
last_blkid, | ||
} | ||
} | ||
|
||
/// Creates a "null" epoch with | ||
pub fn null() -> Self { | ||
Self::new(0, 0, L2BlockId::from(Buf32::zero())) | ||
} | ||
|
||
pub fn epoch(&self) -> u64 { | ||
self.epoch | ||
} | ||
|
||
pub fn last_slot(&self) -> u64 { | ||
self.last_slot | ||
} | ||
|
||
pub fn last_blkid(&self) -> &L2BlockId { | ||
&self.last_blkid | ||
} | ||
|
||
/// Returns a [`L2BlockCommitment`] for the final block of the epoch. | ||
pub fn to_block_commitment(&self) -> L2BlockCommitment { | ||
L2BlockCommitment::new(self.last_slot, self.last_blkid) | ||
} | ||
|
||
/// Returns if the terminal blkid is zero. | ||
pub fn is_null(&self) -> bool { | ||
Buf32::from(self.last_blkid).is_zero() | ||
} | ||
|
||
/// Checks if the epoch is sane. | ||
/// | ||
/// Ie. if the terminal blkid is zero then the last slot and epoch number | ||
/// are also zero. | ||
fn sanity_check(&self) -> bool { | ||
if self.is_null() { | ||
self.last_slot == 0 && self.epoch == 0 | ||
} else { | ||
self.last_slot != 0 | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::EpochCommitment; | ||
|
||
#[test] | ||
fn test_epoch_sanity() { | ||
// TODO write test | ||
} | ||
|
||
#[test] | ||
fn test_epoch_insanity() { | ||
// TODO write test | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not include this check in the constructor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I should just get rid of the sanity check altogether. This special casing isn't well-defined at the moment since it's not actually used in this context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it work to avoid the sentinel value and use an
Option
instead? Or does that screw up the commitment call...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be nice to always have a well-defined finalized block so that we don't have to do special casing just for the genesis. Maybe we can rework the interfaces/definitions around this.