-
Notifications
You must be signed in to change notification settings - Fork 33
[Peras 2] Introduce PerasCertDB and related tests
#1674
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
24 changes: 24 additions & 0 deletions
24
ouroboros-consensus/changelog.d/20250917_101832_thomas.bagrel_peras_cert_db.md
This file contains hidden or 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,24 @@ | ||
| <!-- | ||
| A new scriv changelog fragment. | ||
|
|
||
| Uncomment the section that is right (remove the HTML comment wrapper). | ||
| For top level release notes, leave all the headers commented out. | ||
| --> | ||
|
|
||
| <!-- | ||
| ### Patch | ||
|
|
||
| - A bullet item for the Patch category. | ||
|
|
||
| --> | ||
| <!-- | ||
| ### Non-Breaking | ||
|
|
||
| - A bullet item for the Non-Breaking category. | ||
|
|
||
| --> | ||
|
|
||
| ### Breaking | ||
|
|
||
| - Add modules `Ouroboros.Consensus.Storage.PerasCertDB{,.API,.Impl}`, notably defining the types`PerasCertDB`, `PerasCertSnapshot` (read-only snapshot of certs contained in the DB), and `AddPerasCertResult`; alongside their respective methods | ||
| - Add modules `Test.Ouroboros.Storage.PerasCertDB{,.StateMachine,.Model}` for q-s-m testing of the `PerasCertDB` datatype. The corresponding tests are included in the test suite defined by `Test.Ouroboros.Storage` |
This file contains hidden or 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
4 changes: 4 additions & 0 deletions
4
ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/PerasCertDB.hs
This file contains hidden or 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,4 @@ | ||
| module Ouroboros.Consensus.Storage.PerasCertDB (module X) where | ||
|
|
||
| import Ouroboros.Consensus.Storage.PerasCertDB.API as X | ||
| import Ouroboros.Consensus.Storage.PerasCertDB.Impl as X |
67 changes: 67 additions & 0 deletions
67
ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/PerasCertDB/API.hs
This file contains hidden or 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,67 @@ | ||
| {-# LANGUAGE DataKinds #-} | ||
| {-# LANGUAGE DerivingVia #-} | ||
| {-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
| {-# LANGUAGE ScopedTypeVariables #-} | ||
|
|
||
| module Ouroboros.Consensus.Storage.PerasCertDB.API | ||
| ( PerasCertDB (..) | ||
| , AddPerasCertResult (..) | ||
|
|
||
| -- * 'PerasCertSnapshot' | ||
| , PerasCertSnapshot (..) | ||
| , PerasCertTicketNo | ||
| , zeroPerasCertTicketNo | ||
| ) where | ||
|
|
||
| import Data.Map (Map) | ||
| import Data.Word (Word64) | ||
| import NoThunks.Class | ||
| import Ouroboros.Consensus.Block | ||
| import Ouroboros.Consensus.Peras.Weight | ||
| import Ouroboros.Consensus.Util.IOLike | ||
| import Ouroboros.Consensus.Util.STM (WithFingerprint (..)) | ||
|
|
||
| data PerasCertDB m blk = PerasCertDB | ||
| { addCert :: ValidatedPerasCert blk -> m AddPerasCertResult | ||
| -- ^ Add a Peras certificate to the database. The result indicates whether | ||
| -- the certificate was actually added, or if it was already present. | ||
| , getWeightSnapshot :: STM m (WithFingerprint (PerasWeightSnapshot blk)) | ||
| -- ^ Return the Peras weights in order compare the current selection against | ||
| -- potential candidate chains, namely the weights for blocks not older than | ||
| -- the current immutable tip. It might contain weights for even older blocks | ||
| -- if they have not yet been garbage-collected. | ||
| -- | ||
| -- The 'Fingerprint' is updated every time a new certificate is added, but it | ||
| -- stays the same when certificates are garbage-collected. | ||
| , getCertSnapshot :: STM m (PerasCertSnapshot blk) | ||
| , garbageCollect :: SlotNo -> m () | ||
| -- ^ Garbage-collect state older than the given slot number. | ||
| , closeDB :: m () | ||
| } | ||
| deriving NoThunks via OnlyCheckWhnfNamed "PerasCertDB" (PerasCertDB m blk) | ||
|
|
||
| data AddPerasCertResult = AddedPerasCertToDB | PerasCertAlreadyInDB | ||
| deriving stock (Show, Eq) | ||
|
|
||
| data PerasCertSnapshot blk = PerasCertSnapshot | ||
bladyjoker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { containsCert :: PerasRoundNo -> Bool | ||
| -- ^ Do we have the certificate for this round? | ||
| , getCertsAfter :: PerasCertTicketNo -> Map PerasCertTicketNo (ValidatedPerasCert blk) | ||
| -- ^ Get certificates after the given ticket number (excluded). | ||
| -- The result is a map of ticket numbers to validated certificates. | ||
| } | ||
|
|
||
| -- | A sequence number, incremented every time we receive a new certificate. | ||
| -- | ||
| -- Note that we will /usually/ receive certificates monotonically by round | ||
| -- number, so round numbers could /almost/ fulfill the role of ticket numbers. | ||
| -- However, in certain edge cases (while catching up, or during cooldowns), this | ||
| -- might not be true, such as during syncing or during cooldown periods. | ||
| -- Therefore, for robustness, we choose to maintain dedicated ticket numbers | ||
| -- separately. | ||
| newtype PerasCertTicketNo = PerasCertTicketNo Word64 | ||
| deriving stock Show | ||
| deriving newtype (Eq, Ord, Enum, NoThunks) | ||
|
|
||
| zeroPerasCertTicketNo :: PerasCertTicketNo | ||
| zeroPerasCertTicketNo = PerasCertTicketNo 0 | ||
Oops, something went wrong.
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 do we need ticket numbers for Peras certificates?
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.
We allocate
PerasCertTicketNos in monotonically increasing fashion as new certificates arrive, just as we do for transactions in the mempool (we should add this in a Haddock comment). This way, anyone can subscribe to certificates as they arrive, by asking for more certificates after the last ticket number they know of, usinggetCertsAfter.One thing to note here is that Peras round numbers almost could play the role of ticket numbers: Apart from edge cases, we will never receive a Peras certificate for round
rafter we received one for roundsifr < s. However, because of these edge cases and a general concern for robustness, we are not using round numbers as "almost ticket numbers".Lastly, we could also hide the ticket numbers by exposing a more monadic API (essentially a dequeue interface), where the
PerasCertNois maintained "internally". (This would be possible both here and in the Mempool API)Status quo (simplified) in this PR:
Here, the caller is responsible for maintaining the
PerasCertTicketNoacross calls togetCertsAfter/getCertSnapshot.More monadic option without ticket numbers:
Here,
getNextPerasCertwould internally maintain thePerasCertTicketNoin a TVar (or we could change the implementation to be based on aTChan🤔 ).Let us know if you find this appealing, we can explore that.
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.
That makes sense. Should we document this properly (if it's not documented already). Thanks!
I can't say I have a preference, but the current design seems simpler.
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.
I enriched the Haddocks of
PerasCertTicketNoappropriately 👍