Skip to content

Commit d4d5ca0

Browse files
tbagrel1agustinmistaamesgengeo2anbacquey
committed
Finish plugging PerasCertDB and PerasWeightSnapshot with CertDB
Co-authored-by: Agustin Mista <[email protected]> Co-authored-by: Alexander Esgen <[email protected]> Co-authored-by: Georgy Lukyanov <[email protected]> Co-authored-by: Thomas BAGREL <[email protected]> Co-authored-by: Nicolas BACQUEY <[email protected]> Co-authored-by: Nicolas "Niols" Jeannerod <[email protected]>
1 parent 15669b7 commit d4d5ca0

File tree

9 files changed

+73
-2
lines changed

9 files changed

+73
-2
lines changed

ouroboros-consensus/ouroboros-consensus.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ library
198198
Ouroboros.Consensus.Node.Run
199199
Ouroboros.Consensus.Node.Serialisation
200200
Ouroboros.Consensus.NodeId
201+
Ouroboros.Consensus.Peras.SelectView
201202
Ouroboros.Consensus.Peras.Weight
202203
Ouroboros.Consensus.Protocol.Abstract
203204
Ouroboros.Consensus.Protocol.BFT

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ import Ouroboros.Consensus.HeaderStateHistory
8787
import Ouroboros.Consensus.HeaderValidation (HeaderWithTime (..))
8888
import Ouroboros.Consensus.Ledger.Abstract
8989
import Ouroboros.Consensus.Ledger.Extended
90+
import Ouroboros.Consensus.Peras.Weight (PerasWeightSnapshot)
9091
import Ouroboros.Consensus.Storage.ChainDB.API.Types.InvalidBlockPunishment
9192
import Ouroboros.Consensus.Storage.Common
9293
import Ouroboros.Consensus.Storage.LedgerDB

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ import qualified Ouroboros.Consensus.Storage.ImmutableDB as ImmutableDB
8080
import qualified Ouroboros.Consensus.Storage.ImmutableDB.Stream as ImmutableDB
8181
import Ouroboros.Consensus.Storage.LedgerDB (LedgerSupportsLedgerDB)
8282
import qualified Ouroboros.Consensus.Storage.LedgerDB as LedgerDB
83+
import qualified Ouroboros.Consensus.Storage.PerasCertDB as PerasCertDB
8384
import qualified Ouroboros.Consensus.Storage.VolatileDB as VolatileDB
8485
import Ouroboros.Consensus.Util (newFuse, whenJust, withFuse)
8586
import Ouroboros.Consensus.Util.Args
@@ -174,6 +175,8 @@ openDBInternal args launchBgTasks = runWithTempRegistry $ do
174175
ledgerDbGetVolatileSuffix
175176
traceWith tracer $ TraceOpenEvent OpenedLgrDB
176177

178+
perasCertDB <- PerasCertDB.openDB argsPerasCertDB
179+
177180
varInvalid <- newTVarIO (WithFingerprint Map.empty (Fingerprint 0))
178181

179182
let initChainSelTracer = TraceInitChainSelEvent >$< tracer
@@ -253,6 +256,7 @@ openDBInternal args launchBgTasks = runWithTempRegistry $ do
253256
, cdbChainSelQueue = chainSelQueue
254257
, cdbLoE = Args.cdbsLoE cdbSpecificArgs
255258
, cdbChainSelStarvation = varChainSelStarvation
259+
, cdbPerasCertDB = perasCertDB
256260
}
257261

258262
setGetCurrentChainForLedgerDB $ Query.getCurrentChain env
@@ -316,7 +320,12 @@ openDBInternal args launchBgTasks = runWithTempRegistry $ do
316320
return ((chainDB, testing), env)
317321
where
318322
tracer = Args.cdbsTracer cdbSpecificArgs
319-
Args.ChainDbArgs argsImmutableDb argsVolatileDb argsLgrDb cdbSpecificArgs = args
323+
Args.ChainDbArgs
324+
argsImmutableDb
325+
argsVolatileDb
326+
argsLgrDb
327+
argsPerasCertDB
328+
cdbSpecificArgs = args
320329

321330
-- The LedgerDB requires a criterion ('LedgerDB.GetVolatileSuffix')
322331
-- determining which of its states are volatile/immutable. Once we have

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import qualified Ouroboros.Consensus.Storage.ImmutableDB as ImmutableDB
4141
import Ouroboros.Consensus.Storage.LedgerDB (LedgerDbFlavorArgs)
4242
import qualified Ouroboros.Consensus.Storage.LedgerDB as LedgerDB
4343
import Ouroboros.Consensus.Storage.LedgerDB.Snapshots
44+
import qualified Ouroboros.Consensus.Storage.PerasCertDB as PerasCertDB
4445
import qualified Ouroboros.Consensus.Storage.VolatileDB as VolatileDB
4546
import Ouroboros.Consensus.Util.Args
4647
import Ouroboros.Consensus.Util.IOLike
@@ -54,6 +55,7 @@ data ChainDbArgs f m blk = ChainDbArgs
5455
{ cdbImmDbArgs :: ImmutableDB.ImmutableDbArgs f m blk
5556
, cdbVolDbArgs :: VolatileDB.VolatileDbArgs f m blk
5657
, cdbLgrDbArgs :: LedgerDB.LedgerDbArgs f m blk
58+
, cdbPerasCertDbArgs :: PerasCertDB.PerasCertDbArgs f m blk
5759
, cdbsArgs :: ChainDbSpecificArgs f m blk
5860
}
5961

@@ -138,6 +140,7 @@ defaultArgs =
138140
ImmutableDB.defaultArgs
139141
VolatileDB.defaultArgs
140142
LedgerDB.defaultArgs
143+
PerasCertDB.defaultArgs
141144
defaultSpecificArgs
142145

143146
ensureValidateAll ::
@@ -209,6 +212,10 @@ completeChainDbArgs
209212
, LedgerDB.lgrFlavorArgs = flavorArgs
210213
, LedgerDB.lgrRegistry = registry
211214
}
215+
, cdbPerasCertDbArgs =
216+
PerasCertDB.PerasCertDbArgs
217+
{ PerasCertDB.pcdbaTracer = PerasCertDB.pcdbaTracer (cdbPerasCertDbArgs defArgs)
218+
}
212219
, cdbsArgs =
213220
(cdbsArgs defArgs)
214221
{ cdbsRegistry = registry
@@ -226,6 +233,8 @@ updateTracer trcr args =
226233
{ cdbImmDbArgs = (cdbImmDbArgs args){ImmutableDB.immTracer = TraceImmutableDBEvent >$< trcr}
227234
, cdbVolDbArgs = (cdbVolDbArgs args){VolatileDB.volTracer = TraceVolatileDBEvent >$< trcr}
228235
, cdbLgrDbArgs = (cdbLgrDbArgs args){LedgerDB.lgrTracer = TraceLedgerDBEvent >$< trcr}
236+
, cdbPerasCertDbArgs =
237+
(cdbPerasCertDbArgs args){PerasCertDB.pcdbaTracer = TracePerasCertDbEvent >$< trcr}
229238
, cdbsArgs = (cdbsArgs args){cdbsTracer = trcr}
230239
}
231240

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
@@ -285,7 +285,8 @@ getReadOnlyForkerAtPoint CDB{..} = LedgerDB.getReadOnlyForker cdbLedgerDB
285285
getStatistics :: IOLike m => ChainDbEnv m blk -> m (Maybe LedgerDB.Statistics)
286286
getStatistics CDB{..} = LedgerDB.getTipStatistics cdbLedgerDB
287287

288-
getPerasWeightSnapshot :: ChainDbEnv m blk -> STM m (PerasWeightSnapshot blk)
288+
getPerasWeightSnapshot ::
289+
ChainDbEnv m blk -> STM m (WithFingerprint (PerasWeightSnapshot blk))
289290
getPerasWeightSnapshot CDB{..} = PerasCertDB.getWeightSnapshot cdbPerasCertDB
290291

291292
getPerasCertSnapshot ::

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ import Ouroboros.Consensus.Storage.LedgerDB
127127
, LedgerDbSerialiseConstraints
128128
)
129129
import qualified Ouroboros.Consensus.Storage.LedgerDB as LedgerDB
130+
import Ouroboros.Consensus.Storage.PerasCertDB (PerasCertDB)
131+
import qualified Ouroboros.Consensus.Storage.PerasCertDB as PerasCertDB
130132
import Ouroboros.Consensus.Storage.Serialisation
131133
import Ouroboros.Consensus.Storage.VolatileDB
132134
( VolatileDB
@@ -352,6 +354,7 @@ data ChainDbEnv m blk = CDB
352354
, cdbChainSelStarvation :: !(StrictTVar m ChainSelStarvation)
353355
-- ^ Information on the last starvation of ChainSel, whether ongoing or
354356
-- ended recently.
357+
, cdbPerasCertDB :: !(PerasCertDB m blk)
355358
}
356359
deriving Generic
357360

@@ -746,6 +749,7 @@ data TraceEvent blk
746749
| TraceLedgerDBEvent (LedgerDB.TraceEvent blk)
747750
| TraceImmutableDBEvent (ImmutableDB.TraceEvent blk)
748751
| TraceVolatileDBEvent (VolatileDB.TraceEvent blk)
752+
| TracePerasCertDbEvent (PerasCertDB.TraceEvent blk)
749753
| TraceLastShutdownUnclean
750754
| TraceChainSelStarvationEvent (TraceChainSelStarvationEvent blk)
751755
| TraceAddPerasCertEvent (TraceAddPerasCertEvent blk)

ouroboros-consensus/src/unstable-consensus-testlib/Test/Util/ChainDB.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import qualified Ouroboros.Consensus.Storage.ImmutableDB as ImmutableDB
3232
import Ouroboros.Consensus.Storage.LedgerDB
3333
import qualified Ouroboros.Consensus.Storage.LedgerDB.Snapshots as LedgerDB
3434
import Ouroboros.Consensus.Storage.LedgerDB.V2.Args
35+
import Ouroboros.Consensus.Storage.PerasCertDB (PerasCertDbArgs (..))
3536
import Ouroboros.Consensus.Storage.VolatileDB
3637
import qualified Ouroboros.Consensus.Storage.VolatileDB as VolatileDB
3738
import Ouroboros.Consensus.Util.Args
@@ -135,6 +136,10 @@ fromMinimalChainDbArgs MinimalChainDbArgs{..} =
135136
, lgrQueryBatchSize = DefaultQueryBatchSize
136137
, lgrStartSnapshot = Nothing
137138
}
139+
, cdbPerasCertDbArgs =
140+
PerasCertDbArgs
141+
{ pcdbaTracer = nullTracer
142+
}
138143
, cdbsArgs =
139144
ChainDbSpecificArgs
140145
{ cdbsBlocksToAddSize = 1

ouroboros-consensus/test/consensus-test/Test/Consensus/Peras/WeightSnapshot.hs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
-- and fragments.
1515
module Test.Consensus.Peras.WeightSnapshot (tests) where
1616

17+
import Cardano.Ledger.BaseTypes (unNonZero)
1718
import Data.Containers.ListUtils (nubOrd)
1819
import Data.Map.Strict (Map)
1920
import qualified Data.Map.Strict as Map
2021
import Data.Maybe (catMaybes)
2122
import Data.Traversable (for)
2223
import Ouroboros.Consensus.Block
24+
import Ouroboros.Consensus.Config.SecurityParam
2325
import Ouroboros.Consensus.Peras.Weight
2426
import Ouroboros.Consensus.Util.Condense
2527
import Ouroboros.Network.AnchoredFragment (AnchoredFragment)
@@ -54,12 +56,26 @@ prop_perasWeightSnapshot testSetup =
5456
weightBoostOfFragmentReference frag =:= weightBoostOfFragment snap frag
5557
| frag <- tsFragments
5658
]
59+
, conjoin
60+
[ conjoin
61+
[ counterexample ("Incorrect volatile suffix for " <> condense frag) $
62+
takeVolatileSuffixReference frag =:= volSuffix
63+
, counterexample ("Volatile suffix must be a suffix of" <> condense frag) $
64+
AF.headPoint frag =:= AF.headPoint volSuffix
65+
.&&. AF.withinFragmentBounds (AF.anchorPoint volSuffix) frag
66+
, counterexample ("Volatile suffix of " <> condense frag <> " must contain at most k blocks") $
67+
AF.length volSuffix `le` fromIntegral (unNonZero (maxRollbacks tsSecParam))
68+
]
69+
| frag <- tsFragments
70+
, let volSuffix = takeVolatileSuffix snap tsSecParam frag
71+
]
5772
]
5873
where
5974
TestSetup
6075
{ tsWeights
6176
, tsPoints
6277
, tsFragments
78+
, tsSecParam
6379
} = testSetup
6480

6581
snap = mkPerasWeightSnapshot $ Map.toList tsWeights
@@ -73,12 +89,27 @@ prop_perasWeightSnapshot testSetup =
7389
(weightBoostOfPointReference . blockPoint)
7490
(AF.toOldestFirst frag)
7591

92+
takeVolatileSuffixReference ::
93+
AnchoredFragment TestBlock -> AnchoredFragment TestBlock
94+
takeVolatileSuffixReference frag =
95+
head
96+
[ suffix
97+
| len <- reverse [0 .. AF.length frag]
98+
, -- Consider suffixes of @frag@, longest first
99+
let suffix = AF.anchorNewest (fromIntegral len) frag
100+
weightBoost = weightBoostOfFragmentReference suffix
101+
lengthWeight = PerasWeight (fromIntegral (AF.length suffix))
102+
totalWeight = lengthWeight <> weightBoost
103+
, totalWeight <= maxRollbackWeight tsSecParam
104+
]
105+
76106
data TestSetup = TestSetup
77107
{ tsWeights :: Map (Point TestBlock) PerasWeight
78108
, tsPoints :: [Point TestBlock]
79109
-- ^ Check the weight of these points.
80110
, tsFragments :: [AnchoredFragment TestBlock]
81111
-- ^ Check the weight of these fragments.
112+
, tsSecParam :: SecurityParam
82113
}
83114
deriving stock Show
84115

@@ -106,11 +137,13 @@ instance Arbitrary TestSetup where
106137
pure $
107138
AF.dropNewest nDropNewest $
108139
AF.anchorNewest (fromIntegral nTakeNewest) fullFrag
140+
tsSecParam <- arbitrary
109141
pure
110142
TestSetup
111143
{ tsWeights
112144
, tsPoints
113145
, tsFragments
146+
, tsSecParam
114147
}
115148

116149
shrink ts =
@@ -128,6 +161,9 @@ instance Arbitrary TestSetup where
128161
, [ ts{tsFragments = tsFragments'}
129162
| tsFragments' <- shrinkList (\_frag -> []) tsFragments
130163
]
164+
, [ ts{tsSecParam = tsSecParam'}
165+
| tsSecParam' <- shrink tsSecParam
166+
]
131167
]
132168
where
133169
w1 = PerasWeight 1
@@ -136,4 +172,5 @@ instance Arbitrary TestSetup where
136172
{ tsWeights
137173
, tsPoints
138174
, tsFragments
175+
, tsSecParam
139176
} = ts

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ import Ouroboros.Consensus.Storage.ImmutableDB.Chunks.Internal
127127
import Ouroboros.Consensus.Storage.LedgerDB (LedgerSupportsLedgerDB)
128128
import qualified Ouroboros.Consensus.Storage.LedgerDB.TraceEvent as LedgerDB
129129
import qualified Ouroboros.Consensus.Storage.LedgerDB.V1.DbChangelog as DbChangelog
130+
import qualified Ouroboros.Consensus.Storage.PerasCertDB as PerasCertDB
130131
import qualified Ouroboros.Consensus.Storage.VolatileDB as VolatileDB
131132
import Ouroboros.Consensus.Util (split)
132133
import Ouroboros.Consensus.Util.CallStack
@@ -1371,6 +1372,8 @@ deriving instance SOP.Generic (ImmutableDB.TraceEvent blk)
13711372
deriving instance SOP.HasDatatypeInfo (ImmutableDB.TraceEvent blk)
13721373
deriving instance SOP.Generic (VolatileDB.TraceEvent blk)
13731374
deriving instance SOP.HasDatatypeInfo (VolatileDB.TraceEvent blk)
1375+
deriving instance SOP.Generic (PerasCertDB.TraceEvent blk)
1376+
deriving instance SOP.HasDatatypeInfo (PerasCertDB.TraceEvent blk)
13741377
deriving anyclass instance SOP.Generic (TraceChainSelStarvationEvent blk)
13751378
deriving anyclass instance SOP.HasDatatypeInfo (TraceChainSelStarvationEvent blk)
13761379
deriving anyclass instance SOP.Generic (TraceAddPerasCertEvent blk)
@@ -1816,6 +1819,7 @@ traceEventName = \case
18161819
TraceLedgerDBEvent ev -> "Ledger." <> constrName ev
18171820
TraceImmutableDBEvent ev -> "ImmutableDB." <> constrName ev
18181821
TraceVolatileDBEvent ev -> "VolatileDB." <> constrName ev
1822+
TracePerasCertDbEvent ev -> "PerasCertDB." <> constrName ev
18191823
TraceLastShutdownUnclean -> "LastShutdownUnclean"
18201824
TraceChainSelStarvationEvent ev -> "ChainSelStarvation." <> constrName ev
18211825
TraceAddPerasCertEvent ev -> "AddPerasCert." <> constrName ev

0 commit comments

Comments
 (0)