Skip to content

Commit a5cc01a

Browse files
committed
PerasCertDB.getWeightSnapshot: add Fingerprint
1 parent dd9770d commit a5cc01a

File tree

5 files changed

+45
-23
lines changed

5 files changed

+45
-23
lines changed

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/ChainDB/API.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ data ChainDB m blk = ChainDB
389389
-- in the tables.
390390
, addPerasCert :: PerasCert blk -> m ()
391391
-- ^ TODO
392-
, getPerasWeightSnapshot :: STM m (PerasWeightSnapshot blk)
392+
, getPerasWeightSnapshot :: STM m (WithFingerprint (PerasWeightSnapshot blk))
393393
-- ^ TODO
394394
, closeDB :: m ()
395395
-- ^ Close the ChainDB

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/ChainDB/Impl/Query.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ getReadOnlyForkerAtPoint CDB{..} = LedgerDB.getReadOnlyForker cdbLedgerDB
265265
getStatistics :: IOLike m => ChainDbEnv m blk -> m (Maybe LedgerDB.Statistics)
266266
getStatistics CDB{..} = LedgerDB.getTipStatistics cdbLedgerDB
267267

268-
getPerasWeightSnapshot :: ChainDbEnv m blk -> STM m (PerasWeightSnapshot blk)
268+
getPerasWeightSnapshot ::
269+
ChainDbEnv m blk -> STM m (WithFingerprint (PerasWeightSnapshot blk))
269270
getPerasWeightSnapshot CDB{..} = PerasCertDB.getWeightSnapshot cdbPerasCertDB
270271

271272
{-------------------------------------------------------------------------------

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,18 @@ import NoThunks.Class
1010
import Ouroboros.Consensus.Block
1111
import Ouroboros.Consensus.Peras.Weight
1212
import Ouroboros.Consensus.Util.IOLike
13+
import Ouroboros.Consensus.Util.STM (WithFingerprint (..))
1314

1415
data PerasCertDB m blk = PerasCertDB
1516
{ addCert :: PerasCert blk -> m ()
16-
, getWeightSnapshot :: STM m (PerasWeightSnapshot blk)
17+
, getWeightSnapshot :: STM m (WithFingerprint (PerasWeightSnapshot blk))
18+
-- ^ Return the Peras weights in order compare the current selection against
19+
-- potential candidate chains, namely the weights for blocks not older than
20+
-- the current immutable tip. It might contain weights for even older blocks
21+
-- if they have not yet been garbage-collected.
22+
--
23+
-- The 'Fingerprint' is updated every time a new certificate is added, but it
24+
-- stays the same when certificates are garbage-collected.
1725
, garbageCollect :: SlotNo -> m ()
1826
-- ^ Garbage-collect state older than the given slot number.
1927
, closeDB :: m ()

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

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import Ouroboros.Consensus.Storage.PerasCertDB.API
3434
import Ouroboros.Consensus.Util.Args
3535
import Ouroboros.Consensus.Util.CallStack
3636
import Ouroboros.Consensus.Util.IOLike
37+
import Ouroboros.Consensus.Util.STM
3738

3839
{------------------------------------------------------------------------------
3940
Opening the database
@@ -92,7 +93,7 @@ data PerasCertDbState m blk
9293

9394
data PerasCertDbEnv m blk = PerasCertDbEnv
9495
{ pcdbTracer :: !(Tracer m (TraceEvent blk))
95-
, pcdbVolatileState :: !(StrictTVar m (PerasVolatileCertState blk))
96+
, pcdbVolatileState :: !(StrictTVar m (WithFingerprint (PerasVolatileCertState blk)))
9697
-- ^ The 'RoundNo's of all certificates currently in the db.
9798
}
9899
deriving NoThunks via OnlyCheckWhnfNamed "PerasCertDbEnv" (PerasCertDbEnv m blk)
@@ -148,20 +149,27 @@ implAddCert ::
148149
implAddCert env cert = do
149150
traceWith pcdbTracer $ AddingPerasCert roundNo boostedPt
150151
join $ atomically $ do
151-
PerasVolatileCertState{pvcsCerts, pvcsWeightByPoint} <- readTVar pcdbVolatileState
152+
WithFingerprint
153+
PerasVolatileCertState
154+
{ pvcsCerts
155+
, pvcsWeightByPoint
156+
}
157+
fp <-
158+
readTVar pcdbVolatileState
152159
if Map.member roundNo pvcsCerts
153160
then do
154161
pure $ traceWith pcdbTracer $ IgnoredCertAlreadyInDB roundNo boostedPt
155162
else do
156-
writeTVar
157-
pcdbVolatileState
158-
PerasVolatileCertState
159-
{ pvcsCerts =
160-
Map.insert roundNo cert pvcsCerts
161-
, -- Note that the same block might be boosted by multiple points.
162-
pvcsWeightByPoint =
163-
Map.insertWith (<>) boostedPt boostPerCert pvcsWeightByPoint
164-
}
163+
writeTVar pcdbVolatileState $
164+
WithFingerprint
165+
PerasVolatileCertState
166+
{ pvcsCerts =
167+
Map.insert roundNo cert pvcsCerts
168+
, -- Note that the same block might be boosted by multiple points.
169+
pvcsWeightByPoint =
170+
Map.insertWith (<>) boostedPt boostPerCert pvcsWeightByPoint
171+
}
172+
(succ fp)
165173
pure $ traceWith pcdbTracer $ AddedPerasCert roundNo boostedPt
166174
where
167175
PerasCertDbEnv
@@ -174,16 +182,18 @@ implAddCert env cert = do
174182

175183
implGetWeightSnapshot ::
176184
IOLike m =>
177-
PerasCertDbEnv m blk -> STM m (PerasWeightSnapshot blk)
185+
PerasCertDbEnv m blk -> STM m (WithFingerprint (PerasWeightSnapshot blk))
178186
implGetWeightSnapshot PerasCertDbEnv{pcdbVolatileState} =
179-
PerasWeightSnapshot . pvcsWeightByPoint <$> readTVar pcdbVolatileState
187+
fmap (PerasWeightSnapshot . pvcsWeightByPoint) <$> readTVar pcdbVolatileState
180188

181189
implGarbageCollect ::
182190
forall m blk.
183191
(IOLike m, StandardHash blk) =>
184192
PerasCertDbEnv m blk -> SlotNo -> m ()
185193
implGarbageCollect PerasCertDbEnv{pcdbVolatileState} slot =
186-
atomically $ modifyTVar pcdbVolatileState gc
194+
-- No need to update the 'Fingerprint' as we only remove certificates that do
195+
-- not matter for comparing interesting chains.
196+
atomically $ modifyTVar pcdbVolatileState (fmap gc)
187197
where
188198
gc :: PerasVolatileCertState blk -> PerasVolatileCertState blk
189199
gc PerasVolatileCertState{pvcsCerts, pvcsWeightByPoint} =
@@ -235,12 +245,14 @@ data PerasVolatileCertState blk = PerasVolatileCertState
235245
deriving stock (Show, Generic)
236246
deriving anyclass NoThunks
237247

238-
initialPerasVolatileCertState :: PerasVolatileCertState blk
248+
initialPerasVolatileCertState :: WithFingerprint (PerasVolatileCertState blk)
239249
initialPerasVolatileCertState =
240-
PerasVolatileCertState
241-
{ pvcsCerts = Map.empty
242-
, pvcsWeightByPoint = Map.empty
243-
}
250+
WithFingerprint
251+
PerasVolatileCertState
252+
{ pvcsCerts = Map.empty
253+
, pvcsWeightByPoint = Map.empty
254+
}
255+
(Fingerprint 0)
244256

245257
{-------------------------------------------------------------------------------
246258
Trace types

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Ouroboros.Consensus.Peras.Weight (PerasWeightSnapshot)
2222
import qualified Ouroboros.Consensus.Storage.PerasCertDB as PerasCertDB
2323
import Ouroboros.Consensus.Storage.PerasCertDB.API (PerasCertDB)
2424
import Ouroboros.Consensus.Util.IOLike
25+
import Ouroboros.Consensus.Util.STM
2526
import qualified Test.Ouroboros.Storage.PerasCertDB.Model as Model
2627
import Test.QuickCheck
2728
import qualified Test.QuickCheck.Monadic as QC
@@ -118,7 +119,7 @@ instance RunModel Model (StateT (PerasCertDB IO TestBlock) IO) where
118119
lift $ PerasCertDB.addCert perasCertDB cert
119120
GetWeightSnapshot -> do
120121
perasCertDB <- get
121-
lift $ atomically $ PerasCertDB.getWeightSnapshot perasCertDB
122+
lift $ atomically $ forgetFingerprint <$> PerasCertDB.getWeightSnapshot perasCertDB
122123
GarbageCollect slot -> do
123124
perasCertDB <- get
124125
lift $ PerasCertDB.garbageCollect perasCertDB slot

0 commit comments

Comments
 (0)