Skip to content

Commit ea2901f

Browse files
committed
Scaffolding for Peras certs and PerasCertDB
1 parent 40d77fd commit ea2901f

File tree

10 files changed

+347
-1
lines changed

10 files changed

+347
-1
lines changed

ouroboros-consensus/ouroboros-consensus.cabal

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ library
7575
Ouroboros.Consensus.Block.RealPoint
7676
Ouroboros.Consensus.Block.SupportsDiffusionPipelining
7777
Ouroboros.Consensus.Block.SupportsMetrics
78+
Ouroboros.Consensus.Block.SupportsPeras
7879
Ouroboros.Consensus.Block.SupportsProtocol
7980
Ouroboros.Consensus.Block.SupportsSanityCheck
8081
Ouroboros.Consensus.BlockchainTime
@@ -254,6 +255,9 @@ library
254255
Ouroboros.Consensus.Storage.LedgerDB.V2.Forker
255256
Ouroboros.Consensus.Storage.LedgerDB.V2.InMemory
256257
Ouroboros.Consensus.Storage.LedgerDB.V2.LedgerSeq
258+
Ouroboros.Consensus.Storage.PerasCertDB
259+
Ouroboros.Consensus.Storage.PerasCertDB.API
260+
Ouroboros.Consensus.Storage.PerasCertDB.Impl
257261
Ouroboros.Consensus.Storage.Serialisation
258262
Ouroboros.Consensus.Storage.VolatileDB
259263
Ouroboros.Consensus.Storage.VolatileDB.API

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Block.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ import Ouroboros.Consensus.Block.NestedContent as X
88
import Ouroboros.Consensus.Block.RealPoint as X
99
import Ouroboros.Consensus.Block.SupportsDiffusionPipelining as X
1010
import Ouroboros.Consensus.Block.SupportsMetrics as X
11+
import Ouroboros.Consensus.Block.SupportsPeras as X
1112
import Ouroboros.Consensus.Block.SupportsProtocol as X
1213
import Ouroboros.Consensus.Block.SupportsSanityCheck as X
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{-# LANGUAGE DeriveAnyClass #-}
2+
{-# LANGUAGE DeriveGeneric #-}
3+
{-# LANGUAGE DerivingVia #-}
4+
{-# LANGUAGE FlexibleContexts #-}
5+
{-# LANGUAGE FlexibleInstances #-}
6+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
7+
{-# LANGUAGE ScopedTypeVariables #-}
8+
{-# LANGUAGE TypeFamilies #-}
9+
{-# LANGUAGE UndecidableInstances #-}
10+
11+
module Ouroboros.Consensus.Block.SupportsPeras
12+
( PerasRoundNo (..)
13+
, PerasWeight (..)
14+
, boostPerCert
15+
, BlockSupportsPeras (..)
16+
) where
17+
18+
import Data.Monoid (Sum (..))
19+
import Data.Word (Word64)
20+
import GHC.Generics (Generic)
21+
import NoThunks.Class
22+
import Ouroboros.Consensus.Block.Abstract
23+
24+
newtype PerasRoundNo = PerasRoundNo {unPerasRoundNo :: Word64}
25+
deriving stock Show
26+
deriving newtype (Eq, Ord, NoThunks)
27+
28+
newtype PerasWeight = PerasWeight {unPerasWeight :: Word64}
29+
deriving stock Show
30+
deriving newtype (Eq, Ord, NoThunks)
31+
deriving (Semigroup, Monoid) via Sum Word64
32+
33+
-- | TODO this will become a Ledger protocol parameter
34+
boostPerCert :: PerasWeight
35+
boostPerCert = PerasWeight 15
36+
37+
class
38+
NoThunks (PerasCert blk) =>
39+
BlockSupportsPeras blk
40+
where
41+
data PerasCert blk
42+
43+
perasCertRound :: PerasCert blk -> PerasRoundNo
44+
45+
perasCertBoostedBlock :: PerasCert blk -> Point blk
46+
47+
-- TODO degenerate instance for all blks to get things to compile
48+
instance StandardHash blk => BlockSupportsPeras blk where
49+
data PerasCert blk = PerasCert
50+
{ pcCertRound :: PerasRoundNo
51+
, pcCertBoostedBlock :: Point blk
52+
}
53+
deriving stock Generic
54+
deriving anyclass NoThunks
55+
56+
perasCertRound = pcCertRound
57+
perasCertBoostedBlock = pcCertBoostedBlock

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
@@ -78,6 +78,7 @@ import qualified Ouroboros.Consensus.Storage.ImmutableDB as ImmutableDB
7878
import qualified Ouroboros.Consensus.Storage.ImmutableDB.Stream as ImmutableDB
7979
import Ouroboros.Consensus.Storage.LedgerDB (LedgerSupportsLedgerDB)
8080
import qualified Ouroboros.Consensus.Storage.LedgerDB as LedgerDB
81+
import qualified Ouroboros.Consensus.Storage.PerasCertDB as PerasCertDB
8182
import qualified Ouroboros.Consensus.Storage.VolatileDB as VolatileDB
8283
import Ouroboros.Consensus.Util (newFuse, whenJust, withFuse)
8384
import Ouroboros.Consensus.Util.Args
@@ -168,6 +169,8 @@ openDBInternal args launchBgTasks = runWithTempRegistry $ do
168169
(Query.getAnyKnownBlock immutableDB volatileDB)
169170
traceWith tracer $ TraceOpenEvent OpenedLgrDB
170171

172+
perasCertDB <- PerasCertDB.openDB argsPerasCertDB
173+
171174
varInvalid <- newTVarIO (WithFingerprint Map.empty (Fingerprint 0))
172175

173176
let initChainSelTracer = TraceInitChainSelEvent >$< tracer
@@ -245,6 +248,7 @@ openDBInternal args launchBgTasks = runWithTempRegistry $ do
245248
, cdbChainSelQueue = chainSelQueue
246249
, cdbLoE = Args.cdbsLoE cdbSpecificArgs
247250
, cdbChainSelStarvation = varChainSelStarvation
251+
, cdbPerasCertDB = perasCertDB
248252
}
249253
h <- fmap CDBHandle $ newTVarIO $ ChainDbOpen env
250254
let chainDB =
@@ -300,7 +304,12 @@ openDBInternal args launchBgTasks = runWithTempRegistry $ do
300304
return ((chainDB, testing), env)
301305
where
302306
tracer = Args.cdbsTracer cdbSpecificArgs
303-
Args.ChainDbArgs argsImmutableDb argsVolatileDb argsLgrDb cdbSpecificArgs = args
307+
Args.ChainDbArgs
308+
argsImmutableDb
309+
argsVolatileDb
310+
argsLgrDb
311+
argsPerasCertDB
312+
cdbSpecificArgs = args
304313

305314
-- | We use 'runInnerWithTempRegistry' for the component databases.
306315
innerOpenCont ::

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/Types.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ import Ouroboros.Consensus.Storage.LedgerDB
124124
, LedgerDbSerialiseConstraints
125125
)
126126
import qualified Ouroboros.Consensus.Storage.LedgerDB as LedgerDB
127+
import Ouroboros.Consensus.Storage.PerasCertDB (PerasCertDB)
128+
import qualified Ouroboros.Consensus.Storage.PerasCertDB as PerasCertDB
127129
import Ouroboros.Consensus.Storage.Serialisation
128130
import Ouroboros.Consensus.Storage.VolatileDB
129131
( VolatileDB
@@ -349,6 +351,7 @@ data ChainDbEnv m blk = CDB
349351
, cdbChainSelStarvation :: !(StrictTVar m ChainSelStarvation)
350352
-- ^ Information on the last starvation of ChainSel, whether ongoing or
351353
-- ended recently.
354+
, cdbPerasCertDB :: !(PerasCertDB m blk)
352355
}
353356
deriving Generic
354357

@@ -719,6 +722,7 @@ data TraceEvent blk
719722
| TraceLedgerDBEvent (LedgerDB.TraceEvent blk)
720723
| TraceImmutableDBEvent (ImmutableDB.TraceEvent blk)
721724
| TraceVolatileDBEvent (VolatileDB.TraceEvent blk)
725+
| TracePerasCertDBEvent (PerasCertDB.TraceEvent blk)
722726
| TraceLastShutdownUnclean
723727
| TraceChainSelStarvationEvent (TraceChainSelStarvationEvent blk)
724728
deriving Generic
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Ouroboros.Consensus.Storage.PerasCertDB (module X) where
2+
3+
import Ouroboros.Consensus.Storage.PerasCertDB.API as X
4+
import Ouroboros.Consensus.Storage.PerasCertDB.Impl as X
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE DerivingVia #-}
3+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4+
{-# LANGUAGE ScopedTypeVariables #-}
5+
6+
module Ouroboros.Consensus.Storage.PerasCertDB.API
7+
( PerasCertDB (..)
8+
, PerasWeightSnapshot (..)
9+
, boostedWeightForPoint
10+
, boostedWeightForFragment
11+
) where
12+
13+
import Data.Map.Strict (Map)
14+
import qualified Data.Map.Strict as Map
15+
import NoThunks.Class
16+
import Ouroboros.Consensus.Block
17+
import Ouroboros.Consensus.Util.IOLike
18+
import Ouroboros.Network.AnchoredFragment (AnchoredFragment)
19+
import qualified Ouroboros.Network.AnchoredFragment as AF
20+
21+
data PerasCertDB m blk = PerasCertDB
22+
{ addCert :: PerasCert blk -> m ()
23+
, getWeightSnapshot :: STM m (PerasWeightSnapshot blk)
24+
, closeDB :: m ()
25+
}
26+
deriving NoThunks via OnlyCheckWhnfNamed "PerasCertDB" (PerasCertDB m blk)
27+
28+
newtype PerasWeightSnapshot blk = PerasWeightSnapshot
29+
{ getPerasWeightSnapshot :: Map (Point blk) PerasWeight
30+
}
31+
deriving stock Show
32+
deriving newtype NoThunks
33+
34+
boostedWeightForPoint ::
35+
forall blk.
36+
StandardHash blk =>
37+
PerasWeightSnapshot blk -> Point blk -> PerasWeight
38+
boostedWeightForPoint (PerasWeightSnapshot weightByPoint) pt =
39+
Map.findWithDefault mempty pt weightByPoint
40+
41+
boostedWeightForFragment ::
42+
forall blk.
43+
HasHeader blk =>
44+
PerasWeightSnapshot blk ->
45+
AnchoredFragment blk ->
46+
PerasWeight
47+
boostedWeightForFragment weightSnap frag =
48+
-- TODO think about whether this could be done in sublinear complexity
49+
-- probably should write microbenchmarks at some point to see if this is a bottleneck
50+
foldMap
51+
(boostedWeightForPoint weightSnap)
52+
(blockPoint <$> AF.toOldestFirst frag)

0 commit comments

Comments
 (0)