Skip to content

Commit 5da78b6

Browse files
committed
Simplify HasPerasCertX field accessors
Conflicts: ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/MiniProtocol/ObjectDiffusion/ObjectPool/PerasCert.hs ouroboros-consensus/test/consensus-test/Test/Consensus/MiniProtocol/ObjectDiffusion/PerasCert/Smoke.hs
1 parent b51669d commit 5da78b6

File tree

7 files changed

+51
-30
lines changed

7 files changed

+51
-30
lines changed

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

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ module Ouroboros.Consensus.Block.SupportsPeras
1919
, PerasCfg (..)
2020
, ValidatedPerasCert (..)
2121
, makePerasCfg
22-
, HasPerasCert (..)
23-
, getPerasCertRound
24-
, getPerasCertBoostedBlock
25-
, getPerasCertBoost
22+
, HasPerasCertRound (..)
23+
, HasPerasCertBoostedBlock (..)
24+
, HasPerasCertBoost (..)
2625

2726
-- * Ouroboros Peras round length
2827
, PerasRoundLength (..)
@@ -167,29 +166,47 @@ makePerasCfg _ =
167166
{ perasCfgWeightBoost = boostPerCert
168167
}
169168

170-
class StandardHash blk => HasPerasCert cert blk | cert -> blk where
171-
getPerasCert :: cert -> PerasCert blk
169+
-- | Extract the certificate round from a Peras certificate container
170+
class HasPerasCertRound cert where
171+
getPerasCertRound :: cert -> PerasRoundNo
172172

173-
getPerasCertRound :: HasPerasCert cert blk => cert -> PerasRoundNo
174-
getPerasCertRound = pcCertRound . getPerasCert
173+
instance HasPerasCertRound (PerasCert blk) where
174+
getPerasCertRound = pcCertRound
175175

176-
getPerasCertBoostedBlock :: HasPerasCert cert blk => cert -> Point blk
177-
getPerasCertBoostedBlock = pcCertBoostedBlock . getPerasCert
176+
instance HasPerasCertRound (ValidatedPerasCert blk) where
177+
getPerasCertRound = getPerasCertRound . vpcCert
178178

179-
instance StandardHash blk => HasPerasCert (PerasCert blk) blk where
180-
getPerasCert = id
179+
instance
180+
HasPerasCertRound cert =>
181+
HasPerasCertRound (WithArrivalTime cert)
182+
where
183+
getPerasCertRound = getPerasCertRound . forgetArrivalTime
184+
185+
-- | Extract the boosted block point from a Peras certificate container
186+
class HasPerasCertBoostedBlock cert blk | cert -> blk where
187+
getPerasCertBoostedBlock :: cert -> Point blk
181188

182-
instance StandardHash blk => HasPerasCert (ValidatedPerasCert blk) blk where
183-
getPerasCert = vpcCert
189+
instance HasPerasCertBoostedBlock (PerasCert blk) blk where
190+
getPerasCertBoostedBlock = pcCertBoostedBlock
184191

185-
instance HasPerasCert cert blk => HasPerasCert (WithArrivalTime cert) blk where
186-
getPerasCert = getPerasCert . forgetArrivalTime
192+
instance HasPerasCertBoostedBlock (ValidatedPerasCert blk) blk where
193+
getPerasCertBoostedBlock = getPerasCertBoostedBlock . vpcCert
187194

188-
class HasPerasCertBoost cert blk | cert -> blk where
195+
instance
196+
HasPerasCertBoostedBlock cert blk =>
197+
HasPerasCertBoostedBlock (WithArrivalTime cert) blk
198+
where
199+
getPerasCertBoostedBlock = getPerasCertBoostedBlock . forgetArrivalTime
200+
201+
-- | Extract the certificate boost from a Peras certificate container
202+
class HasPerasCertBoost cert where
189203
getPerasCertBoost :: cert -> PerasWeight
190204

191-
instance HasPerasCertBoost (ValidatedPerasCert blk) blk where
205+
instance HasPerasCertBoost (ValidatedPerasCert blk) where
192206
getPerasCertBoost = vpcCertBoost
193207

194-
instance HasPerasCertBoost cert blk => HasPerasCertBoost (WithArrivalTime cert) blk where
208+
instance
209+
HasPerasCertBoost cert =>
210+
HasPerasCertBoost (WithArrivalTime cert)
211+
where
195212
getPerasCertBoost = getPerasCertBoost . forgetArrivalTime

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/MiniProtocol/ObjectDiffusion/ObjectPool/PerasCert.hs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import qualified Data.Map as Map
1616
import GHC.Exception (throw)
1717
import Ouroboros.Consensus.Block
1818
import Ouroboros.Consensus.BlockchainTime (WithArrivalTime)
19-
import Ouroboros.Consensus.BlockchainTime.WallClock.Types (SystemTime (..), addArrivalTime)
19+
import Ouroboros.Consensus.BlockchainTime.WallClock.Types
20+
( SystemTime (..)
21+
, WithArrivalTime (..)
22+
, addArrivalTime
23+
)
2024
import Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.ObjectPool.API
2125
import Ouroboros.Consensus.Storage.ChainDB.API (ChainDB)
2226
import qualified Ouroboros.Consensus.Storage.ChainDB.API as ChainDB
@@ -29,7 +33,7 @@ import qualified Ouroboros.Consensus.Storage.PerasCertDB.API as PerasCertDB
2933
import Ouroboros.Consensus.Util.IOLike
3034

3135
makePerasCertPoolReaderFromSnapshot ::
32-
(IOLike m, StandardHash blk) =>
36+
IOLike m =>
3337
STM m (PerasCertSnapshot blk) ->
3438
ObjectPoolReader PerasRoundNo (PerasCert blk) PerasCertTicketNo m
3539
makePerasCertPoolReaderFromSnapshot getCertSnapshot =
@@ -40,15 +44,15 @@ makePerasCertPoolReaderFromSnapshot getCertSnapshot =
4044
certSnapshot <- getCertSnapshot
4145
pure $
4246
take (fromIntegral limit) $
43-
[ (ticketNo, getPerasCertRound cert, pure (getPerasCert cert))
47+
[ (ticketNo, getPerasCertRound cert, pure (vpcCert (forgetArrivalTime cert)))
4448
| (ticketNo, cert) <-
4549
Map.toAscList $
4650
PerasCertDB.getCertsAfter certSnapshot lastKnown
4751
]
4852
}
4953

5054
makePerasCertPoolReaderFromCertDB ::
51-
(IOLike m, StandardHash blk) =>
55+
IOLike m =>
5256
PerasCertDB m blk -> ObjectPoolReader PerasRoundNo (PerasCert blk) PerasCertTicketNo m
5357
makePerasCertPoolReaderFromCertDB perasCertDB =
5458
makePerasCertPoolReaderFromSnapshot (PerasCertDB.getCertSnapshot perasCertDB)
@@ -68,7 +72,7 @@ makePerasCertPoolWriterFromCertDB systemTime perasCertDB =
6872
}
6973

7074
makePerasCertPoolReaderFromChainDB ::
71-
(IOLike m, StandardHash blk) =>
75+
IOLike m =>
7276
ChainDB m blk -> ObjectPoolReader PerasRoundNo (PerasCert blk) PerasCertTicketNo m
7377
makePerasCertPoolReaderFromChainDB chainDB =
7478
makePerasCertPoolReaderFromSnapshot (ChainDB.getPerasCertSnapshot chainDB)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ addBlockAsync CDB{cdbTracer, cdbChainSelQueue} =
327327

328328
addPerasCertAsync ::
329329
forall m blk.
330-
(IOLike m, HasHeader blk) =>
330+
IOLike m =>
331331
ChainDbEnv m blk ->
332332
WithArrivalTime (ValidatedPerasCert blk) ->
333333
m (AddPerasCertPromise m)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ addBlockToAdd tracer (ChainSelQueue{varChainSelQueue, varChainSelPoints}) punish
607607

608608
-- | Add a Peras certificate to the background queue.
609609
addPerasCertToQueue ::
610-
(IOLike m, StandardHash blk) =>
610+
IOLike m =>
611611
Tracer m (TraceAddPerasCertEvent blk) ->
612612
ChainSelQueue m blk ->
613613
WithArrivalTime (ValidatedPerasCert blk) ->

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ implGetCertSnapshot PerasCertDbEnv{pcdbVolatileState} =
222222

223223
implGarbageCollect ::
224224
forall m blk.
225-
(IOLike m, StandardHash blk) =>
225+
IOLike m =>
226226
PerasCertDbEnv m blk -> SlotNo -> m ()
227227
implGarbageCollect PerasCertDbEnv{pcdbVolatileState} slot =
228228
-- No need to update the 'Fingerprint' as we only remove certificates that do

ouroboros-consensus/test/consensus-test/Test/Consensus/MiniProtocol/ObjectDiffusion/PerasCert/Smoke.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Ouroboros.Consensus.Block.SupportsPeras
1818
import Ouroboros.Consensus.BlockchainTime.WallClock.Types
1919
( RelativeTime (..)
2020
, SystemTime (..)
21+
, WithArrivalTime (..)
2122
, addArrivalTime
2223
, systemTimeCurrent
2324
)
@@ -155,6 +156,6 @@ prop_smoke protocolConstants (ListWithUniqueIds certs) =
155156
let rawContent =
156157
Map.toAscList $
157158
PerasCertDB.getCertsAfter snap (PerasCertDB.zeroPerasCertTicketNo)
158-
pure $ getPerasCert . snd <$> rawContent
159+
pure $ vpcCert . forgetArrivalTime . snd <$> rawContent
159160

160161
return (outboundPoolReader, inboundPoolWriter, getAllInboundPoolContent)

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ addCert model@Model{certs} cert
5050
| otherwise = model{certs = Set.insert cert certs}
5151

5252
hasRoundNo ::
53-
StandardHash blk =>
5453
Set (WithArrivalTime (ValidatedPerasCert blk)) ->
5554
WithArrivalTime (ValidatedPerasCert blk) ->
5655
Bool
@@ -66,7 +65,7 @@ getWeightSnapshot Model{certs} =
6665
| cert <- Set.toList certs
6766
]
6867

69-
garbageCollect :: StandardHash blk => SlotNo -> Model blk -> Model blk
68+
garbageCollect :: SlotNo -> Model blk -> Model blk
7069
garbageCollect slot model@Model{certs} =
7170
model{certs = Set.filter keepCert certs}
7271
where

0 commit comments

Comments
 (0)