Skip to content

Commit 982d7f4

Browse files
agustinmistaamesgengeo2atbagrel1nbacquey
committed
Define WithArrivalTime combinator
This commit defines a generic WithArrivalTime combinator to wrap a value with its arrival time (as a Relative time). This is needed by Peras in several places, e.g., to evaluate the voting rules. Notably, we store a raw Relative time instead of a (arguably more apt) SlotNo or PerasRoundNo to defer as much as possible having to deal with the case where making this translation (timestamp -> slot/round) is not possible due to the HFC time translation horizon. Instead, the client will need to perform this translation in a context where such a failure cannot occur or can be more easily dealt with. Co-authored-by: Agustin Mista <agustin.mista@moduscreate.com> Co-authored-by: Alexander Esgen <alexander.esgen@iohk.io> Co-authored-by: Georgy Lukyanov <georgy.lukyanov@iohk.io> Co-authored-by: Thomas BAGREL <thomas.bagrel@tweag.io> Co-authored-by: Nicolas BACQUEY <nicolas.bacquey@tweag.io>
1 parent b4f596a commit 982d7f4

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import Data.Word (Word64)
3939
import GHC.Generics (Generic)
4040
import NoThunks.Class
4141
import Ouroboros.Consensus.Block.Abstract
42+
import Ouroboros.Consensus.BlockchainTime.WallClock.Types (WithArrivalTime (..))
4243
import Ouroboros.Consensus.Util
4344
import Ouroboros.Consensus.Util.Condense
4445
import Quiet (Quiet (..))
@@ -183,6 +184,12 @@ instance HasPerasCertRound (PerasCert blk) where
183184
instance HasPerasCertRound (ValidatedPerasCert blk) where
184185
getPerasCertRound = getPerasCertRound . vpcCert
185186

187+
instance
188+
HasPerasCertRound cert =>
189+
HasPerasCertRound (WithArrivalTime cert)
190+
where
191+
getPerasCertRound = getPerasCertRound . forgetArrivalTime
192+
186193
-- | Extract the boosted block point from a Peras certificate container
187194
class HasPerasCertBoostedBlock cert blk | cert -> blk where
188195
getPerasCertBoostedBlock :: cert -> Point blk
@@ -193,9 +200,21 @@ instance HasPerasCertBoostedBlock (PerasCert blk) blk where
193200
instance HasPerasCertBoostedBlock (ValidatedPerasCert blk) blk where
194201
getPerasCertBoostedBlock = getPerasCertBoostedBlock . vpcCert
195202

203+
instance
204+
HasPerasCertBoostedBlock cert blk =>
205+
HasPerasCertBoostedBlock (WithArrivalTime cert) blk
206+
where
207+
getPerasCertBoostedBlock = getPerasCertBoostedBlock . forgetArrivalTime
208+
196209
-- | Extract the certificate boost from a Peras certificate container
197210
class HasPerasCertBoost cert where
198211
getPerasCertBoost :: cert -> PerasWeight
199212

200213
instance HasPerasCertBoost (ValidatedPerasCert blk) where
201214
getPerasCertBoost = vpcCertBoost
215+
216+
instance
217+
HasPerasCertBoost cert =>
218+
HasPerasCertBoost (WithArrivalTime cert)
219+
where
220+
getPerasCertBoost = getPerasCertBoost . forgetArrivalTime

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/BlockchainTime/WallClock/Types.hs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE DeriveAnyClass #-}
3+
{-# LANGUAGE DeriveGeneric #-}
24
{-# LANGUAGE DerivingVia #-}
35

46
module Ouroboros.Consensus.BlockchainTime.WallClock.Types
@@ -15,6 +17,10 @@ module Ouroboros.Consensus.BlockchainTime.WallClock.Types
1517
-- * Get current time (as 'RelativeTime')
1618
, SystemTime (..)
1719

20+
-- * Attach an arrival time (as 'RelativeTime') to an object
21+
, WithArrivalTime (..)
22+
, addArrivalTime
23+
1824
-- * Slot length
1925
, getSlotLength
2026
, mkSlotLength
@@ -31,6 +37,7 @@ module Ouroboros.Consensus.BlockchainTime.WallClock.Types
3137

3238
import Cardano.Slotting.Time
3339
import Data.Time.Clock (NominalDiffTime)
40+
import GHC.Generics (Generic)
3441
import NoThunks.Class (NoThunks, OnlyCheckWhnfNamed (..))
3542

3643
addRelTime :: NominalDiffTime -> RelativeTime -> RelativeTime
@@ -60,3 +67,22 @@ data SystemTime m = SystemTime
6067
-- to reach 'SystemStart'. In tests this does nothing.
6168
}
6269
deriving NoThunks via OnlyCheckWhnfNamed "SystemTime" (SystemTime m)
70+
71+
{-------------------------------------------------------------------------------
72+
Attach an arrival time (as RelativeTime) to an object
73+
-------------------------------------------------------------------------------}
74+
75+
-- | WithArrivalTime
76+
data WithArrivalTime a = WithArrivalTime
77+
{ getArrivalTime :: !RelativeTime
78+
-- ^ The time at which the object arrived
79+
, forgetArrivalTime :: !a
80+
-- ^ The object without its arrival time
81+
}
82+
deriving (Show, Eq, Ord, Generic, NoThunks)
83+
84+
-- | Add an arrival time to an object
85+
addArrivalTime :: Monad m => SystemTime m -> a -> m (WithArrivalTime a)
86+
addArrivalTime systemTime a = do
87+
t <- systemTimeCurrent systemTime
88+
return (WithArrivalTime t a)

0 commit comments

Comments
 (0)