@@ -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)
1919import qualified Data.Foldable1 as F1
2020import Data.Function (on )
2121import qualified Data.List.NonEmpty as NE
22- import Data.Word (Word64 )
2322import GHC.Stack
2423import Ouroboros.Consensus.Block
2524import Ouroboros.Consensus.Peras.SelectView
@@ -55,20 +54,32 @@ compareHeadBlockNo ::
5554 Ordering
5655compareHeadBlockNo = 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--
0 commit comments