Skip to content

Commit f994dc8

Browse files
committed
ChainDB q-s-m: test weighted chain selection
1 parent a01908c commit f994dc8

File tree

5 files changed

+156
-93
lines changed

5 files changed

+156
-93
lines changed

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Util/AnchoredFragment.hs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Ouroboros.Consensus.Util.AnchoredFragment
1010
( compareAnchoredFragments
1111
, compareHeadBlockNo
1212
, cross
13-
, forksAtMostKBlocks
13+
, forksAtMostKWeight
1414
, preferAnchoredCandidate
1515
, stripCommonPrefix
1616
) where
@@ -19,7 +19,6 @@ import Data.Foldable (toList)
1919
import qualified Data.Foldable1 as F1
2020
import Data.Function (on)
2121
import qualified Data.List.NonEmpty as NE
22-
import Data.Word (Word64)
2322
import GHC.Stack
2423
import Ouroboros.Consensus.Block
2524
import Ouroboros.Consensus.Peras.SelectView
@@ -55,20 +54,32 @@ compareHeadBlockNo ::
5554
Ordering
5655
compareHeadBlockNo = compare `on` AF.headBlockNo
5756

58-
forksAtMostKBlocks ::
59-
HasHeader b =>
60-
-- | How many blocks can it fork?
61-
Word64 ->
62-
-- | Our chain.
57+
-- | Check that we can switch from @ours@ to @theirs@ by rolling back our chain
58+
-- by at most @k@ weight.
59+
--
60+
-- If @ours@ and @cand@ do not intersect, this returns 'False'. If they do
61+
-- intersect, then we check that the suffix of @ours@ after the intersection has
62+
-- total weight at most @k@.
63+
forksAtMostKWeight ::
64+
( StandardHash blk
65+
, HasHeader b
66+
, HeaderHash blk ~ HeaderHash b
67+
) =>
68+
PerasWeightSnapshot blk ->
69+
-- | By how much weight can we roll back our chain at most?
70+
PerasWeight ->
71+
-- | Our chain @ours@.
6372
AnchoredFragment b ->
64-
-- | Their chain
73+
-- | Their chain @theirs@.
6574
AnchoredFragment b ->
66-
-- | Indicates whether their chain forks at most the
67-
-- specified number of blocks.
75+
-- | Indicates whether their chain forks at most the given the amount of
76+
-- weight. Returns 'False' if the two fragments do not intersect.
6877
Bool
69-
forksAtMostKBlocks k ours theirs = case ours `AF.intersect` theirs of
70-
Nothing -> False
71-
Just (_, _, ourSuffix, _) -> fromIntegral (AF.length ourSuffix) <= k
78+
forksAtMostKWeight weights maxWeight ours theirs =
79+
case ours `AF.intersect` theirs of
80+
Nothing -> False
81+
Just (_, _, ourSuffix, _) ->
82+
totalWeightOfFragment weights ourSuffix <= maxWeight
7283

7384
-- | Compare two (potentially empty!) 'AnchoredFragment's.
7485
--

ouroboros-consensus/src/unstable-consensus-testlib/Test/Util/Orphans/ToExpr.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ instance ToExpr FsError where
119119

120120
deriving instance ToExpr a => ToExpr (LoE a)
121121

122+
deriving anyclass instance ToExpr PerasRoundNo
123+
124+
deriving anyclass instance ToExpr PerasWeight
125+
126+
deriving anyclass instance ToExpr (HeaderHash blk) => ToExpr (PerasCert blk)
127+
122128
{-------------------------------------------------------------------------------
123129
si-timers
124130
--------------------------------------------------------------------------------}

0 commit comments

Comments
 (0)