Skip to content

Commit a909419

Browse files
committed
Wrap validated Peras certificates with arrival time
1 parent e0288d4 commit a909419

File tree

17 files changed

+162
-57
lines changed

17 files changed

+162
-57
lines changed

ouroboros-consensus-diffusion/src/ouroboros-consensus-diffusion/Ouroboros/Consensus/Network/NodeToNode.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ mkHandlers
269269
, keepAliveRng
270270
, miniProtocolParameters
271271
, getDiffusionPipeliningSupport
272+
, systemTime
272273
}
273274
NodeKernel
274275
{ getChainDB
@@ -328,7 +329,7 @@ mkHandlers
328329
, 10 -- TODO https://github.com/tweag/cardano-peras/issues/97
329330
, 10 -- TODO https://github.com/tweag/cardano-peras/issues/97
330331
)
331-
(makePerasCertPoolWriterFromChainDB $ getChainDB)
332+
(makePerasCertPoolWriterFromChainDB systemTime getChainDB)
332333
version
333334
controlMessageSTM
334335
state

ouroboros-consensus-diffusion/src/ouroboros-consensus-diffusion/Ouroboros/Consensus/Node.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ runWith RunNodeArgs{..} encAddrNtN decAddrNtN LowLevelRunNodeArgs{..} =
579579
llrnFeatureFlags
580580
rnTraceConsensus
581581
btime
582+
systemTime
582583
(InFutureCheck.realHeaderInFutureCheck llrnMaxClockSkew systemTime)
583584
historicityCheck
584585
chainDB
@@ -857,6 +858,7 @@ mkNodeKernelArgs ::
857858
Set CardanoFeatureFlag ->
858859
Tracers m (ConnectionId addrNTN) (ConnectionId addrNTC) blk ->
859860
BlockchainTime m ->
861+
SystemTime m ->
860862
InFutureCheck.SomeHeaderInFutureCheck m blk ->
861863
(m GSM.GsmState -> HistoricityCheck m blk) ->
862864
ChainDB m blk ->
@@ -877,6 +879,7 @@ mkNodeKernelArgs
877879
featureFlags
878880
tracers
879881
btime
882+
systemTime
880883
chainSyncFutureCheck
881884
chainSyncHistoricityCheck
882885
chainDB
@@ -896,6 +899,7 @@ mkNodeKernelArgs
896899
, cfg
897900
, featureFlags
898901
, btime
902+
, systemTime
899903
, chainDB
900904
, initChainDB = nodeInitChainDB
901905
, chainSyncFutureCheck

ouroboros-consensus-diffusion/src/ouroboros-consensus-diffusion/Ouroboros/Consensus/NodeKernel.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ data NodeKernelArgs m addrNTN addrNTC blk = NodeKernelArgs
210210
, cfg :: TopLevelConfig blk
211211
, featureFlags :: Set CardanoFeatureFlag
212212
, btime :: BlockchainTime m
213+
, systemTime :: SystemTime m
213214
, chainDB :: ChainDB m blk
214215
, initChainDB :: StorageConfig blk -> InitChainDB m blk -> m ()
215216
, chainSyncFutureCheck :: SomeHeaderInFutureCheck m blk

ouroboros-consensus-diffusion/src/unstable-diffusion-testlib/Test/ThreadNet/Network.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,7 @@ runThreadNetwork
10471047
, cfg = pInfoConfig
10481048
, featureFlags = mempty
10491049
, btime
1050+
, systemTime
10501051
, chainDB
10511052
, initChainDB = nodeInitChainDB
10521053
, chainSyncFutureCheck =

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

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ module Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.ObjectPool.PerasCert
1111
, makePerasCertPoolWriterFromChainDB
1212
) where
1313

14+
import Control.Monad ((>=>))
1415
import GHC.Exception (throw)
1516
import Ouroboros.Consensus.Block
17+
import Ouroboros.Consensus.BlockchainTime (WithArrivalTime)
18+
import Ouroboros.Consensus.BlockchainTime.WallClock.Types (SystemTime (..), addArrivalTime)
1619
import Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.ObjectPool.API
1720
import Ouroboros.Consensus.Storage.ChainDB.API (ChainDB)
1821
import qualified Ouroboros.Consensus.Storage.ChainDB.API as ChainDB
@@ -49,13 +52,13 @@ makePerasCertPoolReaderFromCertDB perasCertDB =
4952

5053
makePerasCertPoolWriterFromCertDB ::
5154
(StandardHash blk, IOLike m) =>
52-
PerasCertDB m blk -> ObjectPoolWriter PerasRoundNo (PerasCert blk) m
53-
makePerasCertPoolWriterFromCertDB perasCertDB =
55+
SystemTime m ->
56+
PerasCertDB m blk ->
57+
ObjectPoolWriter PerasRoundNo (PerasCert blk) m
58+
makePerasCertPoolWriterFromCertDB systemTime perasCertDB =
5459
ObjectPoolWriter
5560
{ opwObjectId = getPerasCertRound
56-
, opwAddObjects = \certs -> do
57-
validatePerasCerts certs
58-
>>= mapM_ (PerasCertDB.addCert perasCertDB)
61+
, opwAddObjects = addPerasCerts systemTime (PerasCertDB.addCert perasCertDB)
5962
, opwHasObject = do
6063
certSnapshot <- PerasCertDB.getCertSnapshot perasCertDB
6164
pure $ PerasCertDB.containsCert certSnapshot
@@ -69,13 +72,13 @@ makePerasCertPoolReaderFromChainDB chainDB =
6972

7073
makePerasCertPoolWriterFromChainDB ::
7174
(StandardHash blk, IOLike m) =>
72-
ChainDB m blk -> ObjectPoolWriter PerasRoundNo (PerasCert blk) m
73-
makePerasCertPoolWriterFromChainDB chainDB =
75+
SystemTime m ->
76+
ChainDB m blk ->
77+
ObjectPoolWriter PerasRoundNo (PerasCert blk) m
78+
makePerasCertPoolWriterFromChainDB systemTime chainDB =
7479
ObjectPoolWriter
7580
{ opwObjectId = getPerasCertRound
76-
, opwAddObjects = \certs -> do
77-
validatePerasCerts certs
78-
>>= mapM_ (ChainDB.addPerasCertAsync chainDB)
81+
, opwAddObjects = addPerasCerts systemTime (ChainDB.addPerasCertAsync chainDB)
7982
, opwHasObject = do
8083
certSnapshot <- ChainDB.getPerasCertSnapshot chainDB
8184
pure $ PerasCertDB.containsCert certSnapshot
@@ -101,3 +104,23 @@ validatePerasCerts certs = do
101104
case traverse (validatePerasCert perasCfg) certs of
102105
Left validationErr -> throw (PerasCertValidationError validationErr)
103106
Right validatedCerts -> return validatedCerts
107+
108+
-- | Add a list of 'PerasCert's into an object pool.
109+
--
110+
-- NOTE: we first validate the certificates, throwing an exception if any of
111+
-- them are invalid. We then wrap them with their arrival time, and finally add
112+
-- them to the pool using the provided adder function.
113+
--
114+
-- The order of the first two operations (i.e., validation and timestamping) are
115+
-- rather arbitrary, and the abstract Peras protocol just assumes it can happen
116+
-- "within" a slot.
117+
addPerasCerts ::
118+
(StandardHash blk, MonadThrow m) =>
119+
SystemTime m ->
120+
(WithArrivalTime (ValidatedPerasCert blk) -> m a) ->
121+
[PerasCert blk] ->
122+
m ()
123+
addPerasCerts systemTime adder = do
124+
validatePerasCerts
125+
>=> mapM (addArrivalTime systemTime)
126+
>=> mapM_ adder

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import Control.ResourceRegistry
8181
import Data.Typeable (Typeable)
8282
import GHC.Generics (Generic)
8383
import Ouroboros.Consensus.Block
84+
import Ouroboros.Consensus.BlockchainTime.WallClock.Types (WithArrivalTime)
8485
import Ouroboros.Consensus.HeaderStateHistory
8586
( HeaderStateHistory (..)
8687
)
@@ -392,7 +393,7 @@ data ChainDB m blk = ChainDB
392393
, getStatistics :: m (Maybe Statistics)
393394
-- ^ Get statistics from the LedgerDB, in particular the number of entries
394395
-- in the tables.
395-
, addPerasCertAsync :: ValidatedPerasCert blk -> m (AddPerasCertPromise m)
396+
, addPerasCertAsync :: WithArrivalTime (ValidatedPerasCert blk) -> m (AddPerasCertPromise m)
396397
-- ^ TODO docs
397398
, getPerasWeightSnapshot :: STM m (WithFingerprint (PerasWeightSnapshot blk))
398399
-- ^ TODO
@@ -530,7 +531,7 @@ newtype AddPerasCertPromise m = AddPerasCertPromise
530531
-- impossible).
531532
}
532533

533-
addPerasCertSync :: IOLike m => ChainDB m blk -> ValidatedPerasCert blk -> m ()
534+
addPerasCertSync :: IOLike m => ChainDB m blk -> WithArrivalTime (ValidatedPerasCert blk) -> m ()
534535
addPerasCertSync chainDB cert =
535536
waitPerasCertProcessed =<< addPerasCertAsync chainDB cert
536537

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import qualified Data.Set as Set
4545
import Data.Traversable (for)
4646
import GHC.Stack (HasCallStack)
4747
import Ouroboros.Consensus.Block
48+
import Ouroboros.Consensus.BlockchainTime.WallClock.Types (WithArrivalTime)
4849
import Ouroboros.Consensus.Config
4950
import Ouroboros.Consensus.Fragment.Diff (ChainDiff (..))
5051
import qualified Ouroboros.Consensus.Fragment.Diff as Diff
@@ -328,7 +329,7 @@ addPerasCertAsync ::
328329
forall m blk.
329330
(IOLike m, HasHeader blk) =>
330331
ChainDbEnv m blk ->
331-
ValidatedPerasCert blk ->
332+
WithArrivalTime (ValidatedPerasCert blk) ->
332333
m (AddPerasCertPromise m)
333334
addPerasCertAsync CDB{cdbTracer, cdbChainSelQueue} =
334335
addPerasCertToQueue (TraceAddPerasCertEvent >$< cdbTracer) cdbChainSelQueue

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ import Data.Word (Word64)
9494
import GHC.Generics (Generic)
9595
import NoThunks.Class (OnlyCheckWhnfNamed (..))
9696
import Ouroboros.Consensus.Block
97+
import Ouroboros.Consensus.BlockchainTime.WallClock.Types (WithArrivalTime)
9798
import Ouroboros.Consensus.Config
9899
import Ouroboros.Consensus.Fragment.Diff (ChainDiff)
99100
import Ouroboros.Consensus.HeaderValidation (HeaderWithTime (..))
@@ -553,7 +554,7 @@ data ChainSelMessage m blk
553554
ChainSelAddBlock !(BlockToAdd m blk)
554555
| -- | Add a Peras certificate
555556
ChainSelAddPerasCert
556-
!(ValidatedPerasCert blk)
557+
!(WithArrivalTime (ValidatedPerasCert blk))
557558
-- | Used for 'AddPerasCertPromise'.
558559
!(StrictTMVar m ())
559560
| -- | Reprocess blocks that have been postponed by the LoE.
@@ -609,7 +610,7 @@ addPerasCertToQueue ::
609610
(IOLike m, StandardHash blk) =>
610611
Tracer m (TraceAddPerasCertEvent blk) ->
611612
ChainSelQueue m blk ->
612-
ValidatedPerasCert blk ->
613+
WithArrivalTime (ValidatedPerasCert blk) ->
613614
m (AddPerasCertPromise m)
614615
addPerasCertToQueue tracer ChainSelQueue{varChainSelQueue} cert = do
615616
varProcessed <- newEmptyTMVarIO

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ module Ouroboros.Consensus.Storage.PerasCertDB.API
1616
import Data.Word (Word64)
1717
import NoThunks.Class
1818
import Ouroboros.Consensus.Block
19+
import Ouroboros.Consensus.BlockchainTime.WallClock.Types (WithArrivalTime)
1920
import Ouroboros.Consensus.Peras.Weight
2021
import Ouroboros.Consensus.Util.IOLike
2122
import Ouroboros.Consensus.Util.STM (WithFingerprint (..))
2223

2324
data PerasCertDB m blk = PerasCertDB
24-
{ addCert :: ValidatedPerasCert blk -> m AddPerasCertResult
25+
{ addCert :: WithArrivalTime (ValidatedPerasCert blk) -> m AddPerasCertResult
2526
-- ^ TODO docs
2627
, getWeightSnapshot :: STM m (WithFingerprint (PerasWeightSnapshot blk))
2728
-- ^ Return the Peras weights in order compare the current selection against
@@ -45,7 +46,9 @@ data AddPerasCertResult = AddedPerasCertToDB | PerasCertAlreadyInDB
4546
data PerasCertSnapshot blk = PerasCertSnapshot
4647
{ containsCert :: PerasRoundNo -> Bool
4748
-- ^ Do we have the certificate for this round?
48-
, getCertsAfter :: PerasCertTicketNo -> [(ValidatedPerasCert blk, PerasCertTicketNo)]
49+
, getCertsAfter ::
50+
PerasCertTicketNo ->
51+
[(WithArrivalTime (ValidatedPerasCert blk), PerasCertTicketNo)]
4952
}
5053

5154
-- TODO: Once we store historical certificates on disk, this should (also) track

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import qualified Data.Map.Strict as Map
2828
import GHC.Generics (Generic)
2929
import NoThunks.Class
3030
import Ouroboros.Consensus.Block
31+
import Ouroboros.Consensus.BlockchainTime.WallClock.Types (WithArrivalTime)
3132
import Ouroboros.Consensus.Peras.Weight
3233
import Ouroboros.Consensus.Storage.PerasCertDB.API
3334
import Ouroboros.Consensus.Util.Args
@@ -144,7 +145,7 @@ implAddCert ::
144145
, StandardHash blk
145146
) =>
146147
PerasCertDbEnv m blk ->
147-
ValidatedPerasCert blk ->
148+
WithArrivalTime (ValidatedPerasCert blk) ->
148149
m AddPerasCertResult
149150
implAddCert env cert = do
150151
traceWith pcdbTracer $ AddingPerasCert roundNo boostedPt
@@ -246,13 +247,13 @@ implGarbageCollect PerasCertDbEnv{pcdbVolatileState} slot =
246247
-- | Volatile Peras certificate state, i.e. certificates that could influence
247248
-- chain selection by boosting a volatile block.
248249
data PerasVolatileCertState blk = PerasVolatileCertState
249-
{ pvcsCerts :: !(Map PerasRoundNo (ValidatedPerasCert blk))
250+
{ pvcsCerts :: !(Map PerasRoundNo (WithArrivalTime (ValidatedPerasCert blk)))
250251
-- ^ The boosted blocks by 'RoundNo' of all certificates currently in the db.
251252
, pvcsWeightByPoint :: !(PerasWeightSnapshot blk)
252253
-- ^ The weight of boosted blocks w.r.t. the certificates currently in the db.
253254
--
254255
-- INVARIANT: In sync with 'pvcsCerts'.
255-
, pvcsCertsByTicket :: !(Map PerasCertTicketNo (ValidatedPerasCert blk))
256+
, pvcsCertsByTicket :: !(Map PerasCertTicketNo (WithArrivalTime (ValidatedPerasCert blk)))
256257
-- ^ The certificates by 'PerasCertTicketNo'.
257258
--
258259
-- INVARIANT: In sync with 'pvcsCerts'.

0 commit comments

Comments
 (0)