Skip to content

Commit 437f972

Browse files
committed
Wrap validated Peras certificates with arrival time
Conflicts: ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/MiniProtocol/ObjectDiffusion/ObjectPool/PerasCert.hs ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/ChainDB/API.hs ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/PerasCertDB/API.hs ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/PerasCertDB/Model.hs
1 parent 50bcede commit 437f972

File tree

17 files changed

+164
-59
lines changed

17 files changed

+164
-59
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: see https://github.com/tweag/cardano-peras/issues/97
329330
, 10 -- TODO: see 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
@@ -580,6 +580,7 @@ runWith RunNodeArgs{..} encAddrNtN decAddrNtN LowLevelRunNodeArgs{..} =
580580
llrnFeatureFlags
581581
rnTraceConsensus
582582
btime
583+
systemTime
583584
(InFutureCheck.realHeaderInFutureCheck llrnMaxClockSkew systemTime)
584585
historicityCheck
585586
chainDB
@@ -858,6 +859,7 @@ mkNodeKernelArgs ::
858859
Set CardanoFeatureFlag ->
859860
Tracers m (ConnectionId addrNTN) (ConnectionId addrNTC) blk ->
860861
BlockchainTime m ->
862+
SystemTime m ->
861863
InFutureCheck.SomeHeaderInFutureCheck m blk ->
862864
(m GSM.GsmState -> HistoricityCheck m blk) ->
863865
ChainDB m blk ->
@@ -878,6 +880,7 @@ mkNodeKernelArgs
878880
featureFlags
879881
tracers
880882
btime
883+
systemTime
881884
chainSyncFutureCheck
882885
chainSyncHistoricityCheck
883886
chainDB
@@ -897,6 +900,7 @@ mkNodeKernelArgs
897900
, cfg
898901
, featureFlags
899902
, btime
903+
, systemTime
900904
, chainDB
901905
, initChainDB = nodeInitChainDB
902906
, 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,9 +11,12 @@ module Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.ObjectPool.PerasCert
1111
, makePerasCertPoolWriterFromChainDB
1212
) where
1313

14+
import Control.Monad ((>=>))
1415
import qualified Data.Map as Map
1516
import GHC.Exception (throw)
1617
import Ouroboros.Consensus.Block
18+
import Ouroboros.Consensus.BlockchainTime (WithArrivalTime)
19+
import Ouroboros.Consensus.BlockchainTime.WallClock.Types (SystemTime (..), addArrivalTime)
1720
import Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.ObjectPool.API
1821
import Ouroboros.Consensus.Storage.ChainDB.API (ChainDB)
1922
import qualified Ouroboros.Consensus.Storage.ChainDB.API as ChainDB
@@ -52,13 +55,13 @@ makePerasCertPoolReaderFromCertDB perasCertDB =
5255

5356
makePerasCertPoolWriterFromCertDB ::
5457
(StandardHash blk, IOLike m) =>
55-
PerasCertDB m blk -> ObjectPoolWriter PerasRoundNo (PerasCert blk) m
56-
makePerasCertPoolWriterFromCertDB perasCertDB =
58+
SystemTime m ->
59+
PerasCertDB m blk ->
60+
ObjectPoolWriter PerasRoundNo (PerasCert blk) m
61+
makePerasCertPoolWriterFromCertDB systemTime perasCertDB =
5762
ObjectPoolWriter
5863
{ opwObjectId = getPerasCertRound
59-
, opwAddObjects = \certs -> do
60-
validatePerasCerts certs
61-
>>= mapM_ (PerasCertDB.addCert perasCertDB)
64+
, opwAddObjects = addPerasCerts systemTime (PerasCertDB.addCert perasCertDB)
6265
, opwHasObject = do
6366
certSnapshot <- PerasCertDB.getCertSnapshot perasCertDB
6467
pure $ PerasCertDB.containsCert certSnapshot
@@ -72,13 +75,13 @@ makePerasCertPoolReaderFromChainDB chainDB =
7275

7376
makePerasCertPoolWriterFromChainDB ::
7477
(StandardHash blk, IOLike m) =>
75-
ChainDB m blk -> ObjectPoolWriter PerasRoundNo (PerasCert blk) m
76-
makePerasCertPoolWriterFromChainDB chainDB =
78+
SystemTime m ->
79+
ChainDB m blk ->
80+
ObjectPoolWriter PerasRoundNo (PerasCert blk) m
81+
makePerasCertPoolWriterFromChainDB systemTime chainDB =
7782
ObjectPoolWriter
7883
{ opwObjectId = getPerasCertRound
79-
, opwAddObjects = \certs -> do
80-
validatePerasCerts certs
81-
>>= mapM_ (ChainDB.addPerasCertAsync chainDB)
84+
, opwAddObjects = addPerasCerts systemTime (ChainDB.addPerasCertAsync chainDB)
8285
, opwHasObject = do
8386
certSnapshot <- ChainDB.getPerasCertSnapshot chainDB
8487
pure $ PerasCertDB.containsCert certSnapshot
@@ -106,3 +109,23 @@ validatePerasCerts certs = do
106109
case traverse (validatePerasCert perasCfg) certs of
107110
Left validationErr -> throw (PerasCertValidationError validationErr)
108111
Right validatedCerts -> return validatedCerts
112+
113+
-- | Add a list of 'PerasCert's into an object pool.
114+
--
115+
-- NOTE: we first validate the certificates, throwing an exception if any of
116+
-- them are invalid. We then wrap them with their arrival time, and finally add
117+
-- them to the pool using the provided adder function.
118+
--
119+
-- The order of the first two operations (i.e., validation and timestamping) are
120+
-- rather arbitrary, and the abstract Peras protocol just assumes it can happen
121+
-- "within" a slot.
122+
addPerasCerts ::
123+
(StandardHash blk, MonadThrow m) =>
124+
SystemTime m ->
125+
(WithArrivalTime (ValidatedPerasCert blk) -> m a) ->
126+
[PerasCert blk] ->
127+
m ()
128+
addPerasCerts systemTime adder = do
129+
validatePerasCerts
130+
>=> mapM (addArrivalTime systemTime)
131+
>=> 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
)
@@ -396,7 +397,7 @@ data ChainDB m blk = ChainDB
396397
, getStatistics :: m (Maybe Statistics)
397398
-- ^ Get statistics from the LedgerDB, in particular the number of entries
398399
-- in the tables.
399-
, addPerasCertAsync :: ValidatedPerasCert blk -> m (AddPerasCertPromise m)
400+
, addPerasCertAsync :: WithArrivalTime (ValidatedPerasCert blk) -> m (AddPerasCertPromise m)
400401
-- ^ Asynchronously insert a certificate to the DB. If this leads to a fork to
401402
-- be weightier than our current selection, this will trigger a fork switch.
402403
, getPerasWeightSnapshot :: STM m (WithFingerprint (PerasWeightSnapshot blk))
@@ -537,7 +538,7 @@ newtype AddPerasCertPromise m = AddPerasCertPromise
537538
-- impossible).
538539
}
539540

540-
addPerasCertSync :: IOLike m => ChainDB m blk -> ValidatedPerasCert blk -> m ()
541+
addPerasCertSync :: IOLike m => ChainDB m blk -> WithArrivalTime (ValidatedPerasCert blk) -> m ()
541542
addPerasCertSync chainDB cert =
542543
waitPerasCertProcessed =<< addPerasCertAsync chainDB cert
543544

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
@@ -17,12 +17,13 @@ import Data.Map (Map)
1717
import Data.Word (Word64)
1818
import NoThunks.Class
1919
import Ouroboros.Consensus.Block
20+
import Ouroboros.Consensus.BlockchainTime.WallClock.Types (WithArrivalTime)
2021
import Ouroboros.Consensus.Peras.Weight
2122
import Ouroboros.Consensus.Util.IOLike
2223
import Ouroboros.Consensus.Util.STM (WithFingerprint (..))
2324

2425
data PerasCertDB m blk = PerasCertDB
25-
{ addCert :: ValidatedPerasCert blk -> m AddPerasCertResult
26+
{ addCert :: WithArrivalTime (ValidatedPerasCert blk) -> m AddPerasCertResult
2627
-- ^ Add a Peras certificate to the database. The result indicates whether
2728
-- the certificate was actually added, or if it was already present.
2829
, getWeightSnapshot :: STM m (WithFingerprint (PerasWeightSnapshot blk))
@@ -46,7 +47,9 @@ data AddPerasCertResult = AddedPerasCertToDB | PerasCertAlreadyInDB
4647
data PerasCertSnapshot blk = PerasCertSnapshot
4748
{ containsCert :: PerasRoundNo -> Bool
4849
-- ^ Do we have the certificate for this round?
49-
, getCertsAfter :: PerasCertTicketNo -> Map PerasCertTicketNo (ValidatedPerasCert blk)
50+
, getCertsAfter ::
51+
PerasCertTicketNo ->
52+
Map PerasCertTicketNo (WithArrivalTime (ValidatedPerasCert blk))
5053
-- ^ Get certificates after the given ticket number (excluded).
5154
-- The result is a map of ticket numbers to validated certificates.
5255
}

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
@@ -32,6 +32,7 @@ import qualified Data.Set as Set
3232
import GHC.Generics (Generic)
3333
import NoThunks.Class
3434
import Ouroboros.Consensus.Block
35+
import Ouroboros.Consensus.BlockchainTime.WallClock.Types (WithArrivalTime)
3536
import Ouroboros.Consensus.Peras.Weight
3637
import Ouroboros.Consensus.Storage.PerasCertDB.API
3738
import Ouroboros.Consensus.Util.Args
@@ -152,7 +153,7 @@ implAddCert ::
152153
, StandardHash blk
153154
) =>
154155
PerasCertDbEnv m blk ->
155-
ValidatedPerasCert blk ->
156+
WithArrivalTime (ValidatedPerasCert blk) ->
156157
m AddPerasCertResult
157158
implAddCert env cert = do
158159
traceWith pcdbTracer $ AddingPerasCert roundNo boostedPt
@@ -255,13 +256,13 @@ implGarbageCollect PerasCertDbEnv{pcdbVolatileState} slot =
255256
--
256257
-- INVARIANT: See 'invariantForPerasVolatileCertState'.
257258
data PerasVolatileCertState blk = PerasVolatileCertState
258-
{ pvcsCerts :: !(Map PerasRoundNo (ValidatedPerasCert blk))
259+
{ pvcsCerts :: !(Map PerasRoundNo (WithArrivalTime (ValidatedPerasCert blk)))
259260
-- ^ The boosted blocks by 'RoundNo' of all certificates currently in the db.
260261
, pvcsWeightByPoint :: !(PerasWeightSnapshot blk)
261262
-- ^ The weight of boosted blocks w.r.t. the certificates currently in the db.
262263
--
263264
-- INVARIANT: In sync with 'pvcsCerts'.
264-
, pvcsCertsByTicket :: !(Map PerasCertTicketNo (ValidatedPerasCert blk))
265+
, pvcsCertsByTicket :: !(Map PerasCertTicketNo (WithArrivalTime (ValidatedPerasCert blk)))
265266
-- ^ The certificates by 'PerasCertTicketNo'.
266267
--
267268
-- INVARIANT: In sync with 'pvcsCerts'.

0 commit comments

Comments
 (0)