@@ -34,6 +34,7 @@ import Ouroboros.Consensus.Storage.PerasCertDB.API
34
34
import Ouroboros.Consensus.Util.Args
35
35
import Ouroboros.Consensus.Util.CallStack
36
36
import Ouroboros.Consensus.Util.IOLike
37
+ import Ouroboros.Consensus.Util.STM
37
38
38
39
{- -----------------------------------------------------------------------------
39
40
Opening the database
@@ -92,7 +93,7 @@ data PerasCertDbState m blk
92
93
93
94
data PerasCertDbEnv m blk = PerasCertDbEnv
94
95
{ pcdbTracer :: ! (Tracer m (TraceEvent blk ))
95
- , pcdbVolatileState :: ! (StrictTVar m (PerasVolatileCertState blk ))
96
+ , pcdbVolatileState :: ! (StrictTVar m (WithFingerprint ( PerasVolatileCertState blk ) ))
96
97
-- ^ The 'RoundNo's of all certificates currently in the db.
97
98
}
98
99
deriving NoThunks via OnlyCheckWhnfNamed " PerasCertDbEnv" (PerasCertDbEnv m blk )
@@ -148,20 +149,27 @@ implAddCert ::
148
149
implAddCert env cert = do
149
150
traceWith pcdbTracer $ AddingPerasCert roundNo boostedPt
150
151
join $ atomically $ do
151
- PerasVolatileCertState {pvcsCerts, pvcsWeightByPoint} <- readTVar pcdbVolatileState
152
+ WithFingerprint
153
+ PerasVolatileCertState
154
+ { pvcsCerts
155
+ , pvcsWeightByPoint
156
+ }
157
+ fp <-
158
+ readTVar pcdbVolatileState
152
159
if Map. member roundNo pvcsCerts
153
160
then do
154
161
pure $ traceWith pcdbTracer $ IgnoredCertAlreadyInDB roundNo boostedPt
155
162
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)
165
173
pure $ traceWith pcdbTracer $ AddedPerasCert roundNo boostedPt
166
174
where
167
175
PerasCertDbEnv
@@ -174,16 +182,18 @@ implAddCert env cert = do
174
182
175
183
implGetWeightSnapshot ::
176
184
IOLike m =>
177
- PerasCertDbEnv m blk -> STM m (PerasWeightSnapshot blk )
185
+ PerasCertDbEnv m blk -> STM m (WithFingerprint ( PerasWeightSnapshot blk ) )
178
186
implGetWeightSnapshot PerasCertDbEnv {pcdbVolatileState} =
179
- PerasWeightSnapshot . pvcsWeightByPoint <$> readTVar pcdbVolatileState
187
+ fmap ( PerasWeightSnapshot . pvcsWeightByPoint) <$> readTVar pcdbVolatileState
180
188
181
189
implGarbageCollect ::
182
190
forall m blk .
183
191
(IOLike m , StandardHash blk ) =>
184
192
PerasCertDbEnv m blk -> SlotNo -> m ()
185
193
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)
187
197
where
188
198
gc :: PerasVolatileCertState blk -> PerasVolatileCertState blk
189
199
gc PerasVolatileCertState {pvcsCerts, pvcsWeightByPoint} =
@@ -235,12 +245,14 @@ data PerasVolatileCertState blk = PerasVolatileCertState
235
245
deriving stock (Show , Generic )
236
246
deriving anyclass NoThunks
237
247
238
- initialPerasVolatileCertState :: PerasVolatileCertState blk
248
+ initialPerasVolatileCertState :: WithFingerprint ( PerasVolatileCertState blk )
239
249
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 )
244
256
245
257
{- ------------------------------------------------------------------------------
246
258
Trace types
0 commit comments