Skip to content

Commit 1390449

Browse files
committed
Make PerasWeightSnapshot opaque
1 parent 340f938 commit 1390449

File tree

5 files changed

+69
-42
lines changed

5 files changed

+69
-42
lines changed

ouroboros-consensus/bench/PerasCertDB-bench/Main.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
module Main (main) where
1313

1414
import Data.List (iterate')
15-
import Data.Map.Strict qualified as Map
1615
import Numeric.Natural (Natural)
1716
import Ouroboros.Consensus.Block (PerasWeight (PerasWeight), SlotNo (..))
1817
import Ouroboros.Consensus.Peras.Weight
19-
( PerasWeightSnapshot (..)
18+
( PerasWeightSnapshot
2019
, boostedWeightForFragment
20+
, mkPerasWeightSnapshot
2121
)
2222
import Ouroboros.Network.AnchoredFragment qualified as AF
2323
import Test.Ouroboros.Storage.TestBlock (TestBlock (..), TestBody (..), TestHeader (..))
@@ -96,7 +96,7 @@ uniformWeightSnapshot fragment =
9696
. AF.toOldestFirst
9797
$ fragment
9898
weights = repeat (boostWeight benchmarkParams)
99-
in PerasWeightSnapshot{getPerasWeightSnapshot = Map.fromList $ zip pointsToBoost weights}
99+
in mkPerasWeightSnapshot $ pointsToBoost `zip` weights
100100

101101
getEveryN :: Natural -> [(Natural, a)] -> [(Natural, a)]
102102
getEveryN n = filter (\(i, _) -> (i `mod` n) == 0)

ouroboros-consensus/ouroboros-consensus.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,6 @@ benchmark PerasCertDB-bench
846846
other-modules:
847847
build-depends:
848848
base,
849-
containers,
850849
ouroboros-consensus,
851850
ouroboros-network-api,
852851
tasty-bench,

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Peras/Weight.hs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@
66
{-# LANGUAGE TypeOperators #-}
77

88
module Ouroboros.Consensus.Peras.Weight
9-
( PerasWeightSnapshot (..)
9+
( -- * 'PerasWeightSnapshot'
10+
PerasWeightSnapshot
11+
, emptyPerasWeightSnapshot
12+
, mkPerasWeightSnapshot
13+
, perasWeightSnapshotToList
14+
, addToPerasWeightSnapshot
15+
, removeFromPerasWeightSnapshot
1016
, boostedWeightForPoint
1117
, boostedWeightForFragment
1218
) where
1319

20+
import Data.Foldable as Foldable (foldl')
1421
import Data.Map.Strict (Map)
1522
import qualified Data.Map.Strict as Map
1623
import GHC.Generics (Generic)
@@ -26,6 +33,44 @@ newtype PerasWeightSnapshot blk = PerasWeightSnapshot
2633
deriving Generic
2734
deriving newtype NoThunks
2835

36+
emptyPerasWeightSnapshot :: PerasWeightSnapshot blk
37+
emptyPerasWeightSnapshot = PerasWeightSnapshot Map.empty
38+
39+
mkPerasWeightSnapshot ::
40+
StandardHash blk =>
41+
[(Point blk, PerasWeight)] ->
42+
PerasWeightSnapshot blk
43+
mkPerasWeightSnapshot =
44+
Foldable.foldl'
45+
(\s (pt, weight) -> addToPerasWeightSnapshot pt weight s)
46+
emptyPerasWeightSnapshot
47+
48+
perasWeightSnapshotToList :: PerasWeightSnapshot blk -> [(Point blk, PerasWeight)]
49+
perasWeightSnapshotToList = Map.toList . getPerasWeightSnapshot
50+
51+
addToPerasWeightSnapshot ::
52+
StandardHash blk =>
53+
Point blk ->
54+
PerasWeight ->
55+
PerasWeightSnapshot blk ->
56+
PerasWeightSnapshot blk
57+
addToPerasWeightSnapshot pt weight =
58+
PerasWeightSnapshot . Map.insertWith (<>) pt weight . getPerasWeightSnapshot
59+
60+
removeFromPerasWeightSnapshot ::
61+
StandardHash blk =>
62+
Point blk ->
63+
PerasWeight ->
64+
PerasWeightSnapshot blk ->
65+
PerasWeightSnapshot blk
66+
removeFromPerasWeightSnapshot pt (PerasWeight weight) =
67+
PerasWeightSnapshot . Map.update subtractWeight pt . getPerasWeightSnapshot
68+
where
69+
subtractWeight :: PerasWeight -> Maybe PerasWeight
70+
subtractWeight (PerasWeight w)
71+
| w > weight = Just $ PerasWeight (w - weight)
72+
| otherwise = Nothing
73+
2974
boostedWeightForPoint ::
3075
forall blk.
3176
StandardHash blk =>

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/PerasCertDB/Impl.hs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ module Ouroboros.Consensus.Storage.PerasCertDB.Impl
2121
) where
2222

2323
import Control.Tracer (Tracer, nullTracer, traceWith)
24+
import Data.Foldable as Foldable (foldl')
2425
import Data.Kind (Type)
25-
import qualified Data.Map.Merge.Strict as Map
2626
import Data.Map.Strict (Map)
2727
import qualified Data.Map.Strict as Map
2828
import GHC.Generics (Generic)
@@ -165,7 +165,7 @@ implAddCert env cert = do
165165
Map.insert roundNo cert pvcsCerts
166166
, -- Note that the same block might be boosted by multiple points.
167167
pvcsWeightByPoint =
168-
Map.insertWith (<>) boostedPt boostPerCert pvcsWeightByPoint
168+
addToPerasWeightSnapshot boostedPt boostPerCert pvcsWeightByPoint
169169
}
170170
(succ fp)
171171
pure AddedPerasCertToDB
@@ -186,7 +186,7 @@ implGetWeightSnapshot ::
186186
IOLike m =>
187187
PerasCertDbEnv m blk -> STM m (WithFingerprint (PerasWeightSnapshot blk))
188188
implGetWeightSnapshot PerasCertDbEnv{pcdbVolatileState} =
189-
fmap (PerasWeightSnapshot . pvcsWeightByPoint) <$> readTVar pcdbVolatileState
189+
fmap pvcsWeightByPoint <$> readTVar pcdbVolatileState
190190

191191
implGarbageCollect ::
192192
forall m blk.
@@ -202,33 +202,21 @@ implGarbageCollect PerasCertDbEnv{pcdbVolatileState} slot =
202202
PerasVolatileCertState
203203
{ pvcsCerts = certsToKeep
204204
, pvcsWeightByPoint =
205-
Map.merge
206-
-- Do not touch weight of boosted blocks that we do not subtract any
207-
-- weight from.
208-
Map.preserveMissing
209-
-- Irrelevant, the key set of @weightToRemove@ is a subset of the
210-
-- key set of @pvcsWeightByPoint@.
211-
Map.dropMissing
212-
(Map.zipWithMaybeMatched $ \_pt -> subtractWeight)
205+
Foldable.foldl'
206+
( \s cert ->
207+
removeFromPerasWeightSnapshot
208+
(perasCertBoostedBlock cert)
209+
boostPerCert
210+
s
211+
)
213212
pvcsWeightByPoint
214-
weightToRemove
213+
certsToRemove
215214
}
216215
where
217216
(certsToRemove, certsToKeep) =
218217
Map.partition isTooOld pvcsCerts
219218
isTooOld cert =
220219
pointSlot (perasCertBoostedBlock cert) < NotOrigin slot
221-
weightToRemove =
222-
Map.fromListWith
223-
(<>)
224-
[ (perasCertBoostedBlock cert, boostPerCert)
225-
| cert <- Map.elems certsToRemove
226-
]
227-
228-
subtractWeight :: PerasWeight -> PerasWeight -> Maybe PerasWeight
229-
subtractWeight (PerasWeight w1) (PerasWeight w2)
230-
| w1 > w2 = Just $ PerasWeight (w1 - w2)
231-
| otherwise = Nothing
232220

233221
{-------------------------------------------------------------------------------
234222
Implementation-internal types
@@ -239,7 +227,7 @@ implGarbageCollect PerasCertDbEnv{pcdbVolatileState} slot =
239227
data PerasVolatileCertState blk = PerasVolatileCertState
240228
{ pvcsCerts :: !(Map PerasRoundNo (PerasCert blk))
241229
-- ^ The boosted blocks by 'RoundNo' of all certificates currently in the db.
242-
, pvcsWeightByPoint :: !(Map (Point blk) PerasWeight)
230+
, pvcsWeightByPoint :: !(PerasWeightSnapshot blk)
243231
-- ^ The weight of boosted blocks w.r.t. the certificates currently in the db.
244232
--
245233
-- INVARIANT: In sync with 'pvcsCerts'.
@@ -252,7 +240,7 @@ initialPerasVolatileCertState =
252240
WithFingerprint
253241
PerasVolatileCertState
254242
{ pvcsCerts = Map.empty
255-
, pvcsWeightByPoint = Map.empty
243+
, pvcsWeightByPoint = emptyPerasWeightSnapshot
256244
}
257245
(Fingerprint 0)
258246

ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/PerasCertDB/Model.hs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ module Test.Ouroboros.Storage.PerasCertDB.Model
1414
, garbageCollect
1515
) where
1616

17-
import qualified Data.Map as Map
1817
import Data.Set (Set)
1918
import qualified Data.Set as Set
2019
import GHC.Generics (Generic)
2120
import Ouroboros.Consensus.Block
22-
import Ouroboros.Consensus.Peras.Weight (PerasWeightSnapshot (..))
21+
import Ouroboros.Consensus.Peras.Weight
22+
( PerasWeightSnapshot
23+
, mkPerasWeightSnapshot
24+
)
2325

2426
data Model blk = Model
2527
{ certs :: Set (PerasCert blk)
@@ -47,16 +49,9 @@ addCert model@Model{certs} cert =
4749
getWeightSnapshot ::
4850
StandardHash blk =>
4951
Model blk -> PerasWeightSnapshot blk
50-
getWeightSnapshot Model{certs} = snap
51-
where
52-
snap =
53-
PerasWeightSnapshot
54-
{ getPerasWeightSnapshot =
55-
Set.fold
56-
(\cert acc -> Map.insertWith (<>) (perasCertBoostedBlock cert) boostPerCert acc)
57-
Map.empty
58-
certs
59-
}
52+
getWeightSnapshot Model{certs} =
53+
mkPerasWeightSnapshot
54+
[(perasCertBoostedBlock cert, boostPerCert) | cert <- Set.toList certs]
6055

6156
garbageCollect :: StandardHash blk => SlotNo -> Model blk -> Model blk
6257
garbageCollect slot model@Model{certs} =

0 commit comments

Comments
 (0)