Skip to content

Commit 4c69d1c

Browse files
committed
PerasCertDB.addCert: return whether we added the cert
1 parent a5cc01a commit 4c69d1c

File tree

4 files changed

+23
-10
lines changed
  • ouroboros-consensus

4 files changed

+23
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ openDBInternal args launchBgTasks = runWithTempRegistry $ do
277277
, getReadOnlyForkerAtPoint = getEnv2 h Query.getReadOnlyForkerAtPoint
278278
, getStatistics = getEnv h Query.getStatistics
279279
, addPerasCert = getEnv1 h $ \cdb@CDB{..} cert -> do
280-
PerasCertDB.addCert cdbPerasCertDB cert
280+
_ <- PerasCertDB.addCert cdbPerasCertDB cert
281281
-- TODO trigger chain selection in a more efficient way
282282
waitChainSelectionPromise =<< ChainSel.triggerChainSelectionAsync cdb
283283
, getPerasWeightSnapshot = getEnvSTM h Query.getPerasWeightSnapshot

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
module Ouroboros.Consensus.Storage.PerasCertDB.API
66
( PerasCertDB (..)
7+
, AddPerasCertResult (..)
78
) where
89

910
import NoThunks.Class
@@ -13,7 +14,7 @@ import Ouroboros.Consensus.Util.IOLike
1314
import Ouroboros.Consensus.Util.STM (WithFingerprint (..))
1415

1516
data PerasCertDB m blk = PerasCertDB
16-
{ addCert :: PerasCert blk -> m ()
17+
{ addCert :: PerasCert blk -> m AddPerasCertResult
1718
, getWeightSnapshot :: STM m (WithFingerprint (PerasWeightSnapshot blk))
1819
-- ^ Return the Peras weights in order compare the current selection against
1920
-- potential candidate chains, namely the weights for blocks not older than
@@ -27,3 +28,6 @@ data PerasCertDB m blk = PerasCertDB
2728
, closeDB :: m ()
2829
}
2930
deriving NoThunks via OnlyCheckWhnfNamed "PerasCertDB" (PerasCertDB m blk)
31+
32+
data AddPerasCertResult = AddedPerasCertToDB | PerasCertAlreadyInDB
33+
deriving stock (Show, Eq)

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ module Ouroboros.Consensus.Storage.PerasCertDB.Impl
2020
, PerasCertDbError (..)
2121
) where
2222

23-
import Control.Monad (join)
2423
import Control.Tracer (Tracer, nullTracer, traceWith)
2524
import Data.Kind (Type)
2625
import qualified Data.Map.Merge.Strict as Map
@@ -145,10 +144,10 @@ implAddCert ::
145144
) =>
146145
PerasCertDbEnv m blk ->
147146
PerasCert blk ->
148-
m ()
147+
m AddPerasCertResult
149148
implAddCert env cert = do
150149
traceWith pcdbTracer $ AddingPerasCert roundNo boostedPt
151-
join $ atomically $ do
150+
res <- atomically $ do
152151
WithFingerprint
153152
PerasVolatileCertState
154153
{ pvcsCerts
@@ -157,8 +156,7 @@ implAddCert env cert = do
157156
fp <-
158157
readTVar pcdbVolatileState
159158
if Map.member roundNo pvcsCerts
160-
then do
161-
pure $ traceWith pcdbTracer $ IgnoredCertAlreadyInDB roundNo boostedPt
159+
then pure PerasCertAlreadyInDB
162160
else do
163161
writeTVar pcdbVolatileState $
164162
WithFingerprint
@@ -170,7 +168,11 @@ implAddCert env cert = do
170168
Map.insertWith (<>) boostedPt boostPerCert pvcsWeightByPoint
171169
}
172170
(succ fp)
173-
pure $ traceWith pcdbTracer $ AddedPerasCert roundNo boostedPt
171+
pure AddedPerasCertToDB
172+
traceWith pcdbTracer $ case res of
173+
AddedPerasCertToDB -> AddedPerasCert roundNo boostedPt
174+
PerasCertAlreadyInDB -> IgnoredCertAlreadyInDB roundNo boostedPt
175+
pure res
174176
where
175177
PerasCertDbEnv
176178
{ pcdbTracer

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ module Test.Ouroboros.Storage.PerasCertDB.StateMachine (tests) where
1717
import Control.Monad.State
1818
import Control.Tracer (nullTracer)
1919
import qualified Data.List.NonEmpty as NE
20+
import qualified Data.Set as Set
2021
import Ouroboros.Consensus.Block
2122
import Ouroboros.Consensus.Peras.Weight (PerasWeightSnapshot)
2223
import qualified Ouroboros.Consensus.Storage.PerasCertDB as PerasCertDB
23-
import Ouroboros.Consensus.Storage.PerasCertDB.API (PerasCertDB)
24+
import Ouroboros.Consensus.Storage.PerasCertDB.API (AddPerasCertResult (..), PerasCertDB)
2425
import Ouroboros.Consensus.Util.IOLike
2526
import Ouroboros.Consensus.Util.STM
2627
import qualified Test.Ouroboros.Storage.PerasCertDB.Model as Model
@@ -51,7 +52,7 @@ instance StateModel Model where
5152
data Action Model a where
5253
OpenDB :: Action Model ()
5354
CloseDB :: Action Model ()
54-
AddCert :: PerasCert TestBlock -> Action Model ()
55+
AddCert :: PerasCert TestBlock -> Action Model AddPerasCertResult
5556
GetWeightSnapshot :: Action Model (PerasWeightSnapshot TestBlock)
5657
GarbageCollect :: SlotNo -> Action Model ()
5758

@@ -124,6 +125,12 @@ instance RunModel Model (StateT (PerasCertDB IO TestBlock) IO) where
124125
perasCertDB <- get
125126
lift $ PerasCertDB.garbageCollect perasCertDB slot
126127

128+
postcondition (Model model, _) (AddCert cert) _ actual = do
129+
let expected
130+
| cert `Set.member` model.certs = PerasCertAlreadyInDB
131+
| otherwise = AddedPerasCertToDB
132+
counterexamplePost $ show expected <> " /= " <> show actual
133+
pure $ expected == actual
127134
postcondition (Model model, _) GetWeightSnapshot _ actual = do
128135
let expected = Model.getWeightSnapshot model
129136
counterexamplePost $ "Model: " <> show expected

0 commit comments

Comments
 (0)