@@ -10,7 +10,7 @@ module Ouroboros.Consensus.Util.AnchoredFragment
10
10
( compareAnchoredFragments
11
11
, compareHeadBlockNo
12
12
, cross
13
- , forksAtMostKBlocks
13
+ , forksAtMostKWeight
14
14
, preferAnchoredCandidate
15
15
, stripCommonPrefix
16
16
) where
@@ -19,7 +19,6 @@ import Data.Foldable (toList)
19
19
import qualified Data.Foldable1 as F1
20
20
import Data.Function (on )
21
21
import qualified Data.List.NonEmpty as NE
22
- import Data.Word (Word64 )
23
22
import GHC.Stack
24
23
import Ouroboros.Consensus.Block
25
24
import Ouroboros.Consensus.Peras.SelectView
@@ -55,20 +54,32 @@ compareHeadBlockNo ::
55
54
Ordering
56
55
compareHeadBlockNo = compare `on` AF. headBlockNo
57
56
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@.
63
72
AnchoredFragment b ->
64
- -- | Their chain
73
+ -- | Their chain @theirs@.
65
74
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 .
68
77
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
72
83
73
84
-- | Compare two (potentially empty!) 'AnchoredFragment's.
74
85
--
0 commit comments