Skip to content

Conversation

@tbagrel1
Copy link
Contributor

@tbagrel1 tbagrel1 commented Sep 11, 2025

This pull request introduces some base datatypes and functionality required for Peras, along with supporting tests, benchmarks, and documentation.

Specifically, it adds:

  • PerasWeight and PerasCert, which associate a weight with a block.
  • PerasWeightSnapshot, a type for capturing the known weight state of blocks
  • Functions for computing weights of chains and fragments.
  • Tests, microbenchmarks, documentation, and runnable examples for weight-related utilities.

Peras types and weight computation

  • Added module Ouroboros.Consensus.Block.SupportsPeras, defining types and logic for Peras weights and certificates.
    • Key elements are re-exported via Ouroboros.Consensus.Block.
  • Added module Ouroboros.Consensus.Peras.Weight, introducing PerasWeightSnapshot and weight computation functions for chains and fragments.

Documentation

  • Added glossary entries for Peras, weight boost, and chain fragment weight in glossary.md.
  • Created a new benchmarks page explaining how to run Peras-related microbenchmarks and their purpose.

Tests and benchmarks

  • Introduced benchmark suite PerasCertDB-bench, including a weight computation microbenchmark (bench/PerasCertDB-bench/Main.hs).
  • Added module Test.Consensus.Peras.WeightSnapshot with property-based tests for weight computation on chains and fragments.

Regression

The new code added with this PR is not actually used, so it shouldn't introduce regressions.

@amesgen amesgen added the Peras label Sep 11, 2025
@tbagrel1 tbagrel1 force-pushed the main-pr/basic-peras-types branch from d3cd2fc to 8df7902 Compare September 12, 2025 07:59
@tbagrel1 tbagrel1 changed the title Introduce basic types to support Peras [Peras #1] Introduce basic types to support Peras Sep 12, 2025
@tbagrel1 tbagrel1 force-pushed the main-pr/basic-peras-types branch 4 times, most recently from 738daaf to f3fbd4f Compare September 16, 2025 11:36
@tbagrel1 tbagrel1 marked this pull request as ready for review September 16, 2025 12:59
@tbagrel1 tbagrel1 force-pushed the main-pr/basic-peras-types branch from f3fbd4f to 01e1c69 Compare September 16, 2025 13:50
@dnadales dnadales moved this to 👀 In review in Consensus Team Backlog Sep 19, 2025
tbagrel1 and others added 4 commits September 25, 2025 13:46
Co-authored-by: Agustin Mista <[email protected]>
Co-authored-by: Alexander Esgen <[email protected]>
Co-authored-by: Georgy Lukyanov <[email protected]>
Co-authored-by: Thomas BAGREL <[email protected]>
Co-authored-by: Nicolas BACQUEY <[email protected]>
Co-authored-by: Nicolas "Niols" Jeannerod <[email protected]>
Co-authored-by: Agustin Mista <[email protected]>
Co-authored-by: Alexander Esgen <[email protected]>
Co-authored-by: Georgy Lukyanov <[email protected]>
Co-authored-by: Thomas BAGREL <[email protected]>
Co-authored-by: Nicolas BACQUEY <[email protected]>
Co-authored-by: Nicolas "Niols" Jeannerod <[email protected]>
Co-authored-by: Agustin Mista <[email protected]>
Co-authored-by: Alexander Esgen <[email protected]>
Co-authored-by: Georgy Lukyanov <[email protected]>
Co-authored-by: Thomas BAGREL <[email protected]>
Co-authored-by: Nicolas BACQUEY <[email protected]>
Co-authored-by: Nicolas "Niols" Jeannerod <[email protected]>
Co-authored-by: Agustin Mista <[email protected]>
Co-authored-by: Alexander Esgen <[email protected]>
Co-authored-by: Georgy Lukyanov <[email protected]>
Co-authored-by: Thomas BAGREL <[email protected]>
Co-authored-by: Nicolas BACQUEY <[email protected]>
Co-authored-by: Nicolas "Niols" Jeannerod <[email protected]>
@amesgen amesgen force-pushed the main-pr/basic-peras-types branch from 2fdba58 to 3db7a62 Compare September 25, 2025 11:47
@amesgen amesgen enabled auto-merge September 25, 2025 11:47
@amesgen amesgen added this pull request to the merge queue Sep 25, 2025
Merged via the queue into main with commit a3245c5 Sep 25, 2025
16 of 18 checks passed
@amesgen amesgen deleted the main-pr/basic-peras-types branch September 25, 2025 13:59
@github-project-automation github-project-automation bot moved this from 👀 In review to ✅ Done in Consensus Team Backlog Sep 25, 2025
@tbagrel1 tbagrel1 changed the title [Peras #1] Introduce basic types to support Peras [Peras 1] Introduce basic types to support Peras Sep 25, 2025
github-merge-queue bot pushed a commit that referenced this pull request Oct 10, 2025
This pull request introduces the `PerasCertDB` datatype, used to hold
and access all `PerasCert`s known by a node that could affect chain
selection.

In the mid/near-term future, this will be enriched to persist
certificates across restarts, and probably also to store historical
certificates.

---

### **Peras types**

- Added 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 respectives methods

---

### **Tests**

- Added modules
`Test.Ouroboros.Storage.PerasCertDB{,.StateMachine,.Model}` for q-s-m
testing of the `PerasCertDB` datatype. The corresponding tests have been
included in the test suite defined by `Test.Ouroboros.Storage`

---

### **Regression**

As the [previous
PR](#1673), all
the components introduced by this PR are not used at the moment by the
rest of the code, so no regression is expected.
github-merge-queue bot pushed a commit that referenced this pull request Oct 21, 2025
…ed chain selection (#1678)

# Description

This PR introduces:

- **Weighted chain comparisons**: We compare chains based on their
*weight* instead of their length. Non-trivial weight is given by Peras
certificates as introduced in
#1673.
- **Weighted chain selection**: Select the *weightiest* chain instead of
the *longest* chain.
- **Weighted immutability criterion**: Define the immutable tip in terms
of *weight* instead of length.

### Commits

The commits are intended to be reviewed individually:

 - *ChainDB: expose PerasCertDB functionality*

The ChainDB maintains a PerasCertDB internally, and exposes most of its
functionality through its public API. This is analogous to how the
LedgerDB is managed by the ChainDB.

 - *`SecurityParam`: mention weighted nature*

Purely a documentation update to mention the richer dynamics under
Peras, plus a small helper function.

 - *O.C.Peras.Weight: add `takeVolatileSuffix`*

In Praos, the volatile suffix of a chain is defined to be the `k` most
recent blocks. Analogously, in Peras, the volatile suffix of a chain is
defined to be the longest suffix with weight at most `k`.

This commits adds an appropriate function (via a binary search), as well
as documentation and tests.

 - *ChainDB: define `getCurrentChain` in terms of weight*

This makes use of the previous commit in the ChainDB. Note that
#1619 guarantees
that the LedgerDB automatically uses the same notion of immutability.

Note that this means that the immutable tip will be less than `k` blocks
behind the tip when Peras is working well, ie every Peras round is
giving rise to a certificate. Concretely, for plausible parameters, ie a
round length of $U = 90$ slots and a Peras boost of $B = 15$, the length
of the volatile suffix decreases from $k=2160$ to

   $$\frac{k}{1+\frac{B}{U\cdot f}} \approx 499$$

   on average.

 - *ChainDB.StateMachine: check immutable tip monotonicity*

To make sure that the more refined immutability criterion doesn't
introduce any surprises, we add a postcondition to the ChainDB q-s-m
test that the immutable tip never recedes.

- *GSM: allow `candidateOverSelection` to be stateful* and *ChainSel:
make `rollbackExceedsSuffix` weight-aware*

Preparatory refactorings for when the chain comparison will become
weighted and hence additionally depend on the `PerasWeightSnapshot`.

 - *Peras.SelectView: initialize, expose `WeightedSelectView`*

This introduces the notion of a *weighted* `SelectView`, making use of
#1591

https://github.com/IntersectMBO/ouroboros-consensus/blob/90131b282b4e002e9ddc0d7196043c50b255a1d4/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Peras/SelectView.hs#L35-L48

This will used in place of the "normal" `SelectView`. Eventually, we
could also remove the normal `SelectView` and replace it with
`WeightedSelectView`. However, to keep this already big PR more focused,
we propose to do this in the future.

 - *Introduce weighted chain comparisons*

The core change in this commit is in the `O.C.Util.AnchoredFragment` to
the `preferAnchoredCandidate` and `compareAnchoredFragments` functions.
These now

- have a slightly strengthened precondition, namely that they intersect
(as we otherwise can't meaningfully compare their weight), that we
ensure is satisfied anywhere, and
    - take `PerasWeightSnapshot` as an additional argument.

We keep the old implementation in case the `PerasWeightSnapshot` is
empty; which is semantically unnecessary, but is a trivial way to make
sure that no performance regression is introduced.

The rest of the diff of this commit is simply due to adapting the
respective call sites in a rather straightforward fashion.

 - *Integrate weighted BlockFetch decision logic*

This commit plugs the weighted chain comparison logic into the
BlockFetch decision logic. This relies on a (merged, but not yet
released, hence the s-r-p) Network change, see
IntersectMBO/ouroboros-network#5161 for a
detailed description.

 - *ChainDB: implement chain selection for Peras certificates*

We allow new Peras certificates to be added to the ChainDB (and
therefore to the managed PerasCertDB). If the certificate is boosting a
block that is not on the current selection, we perform chain selection
for it, potentially switching to a fork containing it if it now is
weightier than our selection.

 - *MockChainSel: switch to weighted chain selection*

   This logic is for example used in tests.

 - *ChainDB q-s-m: test weighted chain selection*

We enrich the model ChainDB implementation with weighted chain
selection, and add a new command with a simple generator for adding
certificates. We also enrich labelling with the
`TagSwitchedToShorterChain` tag that shows that we sometimes *do* switch
to a shorter chain; which would be a bug without Peras.

A follow-up PR
(#1670) will
further improve the generators of this test.

---

### **Regression**

As of today, there is no way to generate certificates, so `PerasCertDB`
is always empty, and the change to the chain selection algorithm is
therefore invisible: in the absence of certificates, the Peras weight of
a chain is equal to its length. Therefore, this PR does not introduce
any semantic changes.

Furthermore, from a performance standpoint, care is taken to ensure that
if there are no certificates, we use the unweighted Praos logic, meaning
that there is no change in performance when Peras is disabled.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

4 participants