Skip to content

Commit 7741bcd

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 e12f5ae commit 7741bcd

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
@@ -34,6 +34,7 @@ import Data.Word (Word64)
3434
import GHC.Generics (Generic)
3535
import NoThunks.Class
3636
import Ouroboros.Consensus.Block.Abstract
37+
import Ouroboros.Consensus.BlockchainTime.WallClock.Types (WithArrivalTime (..))
3738
import Ouroboros.Consensus.Peras.Params
3839
import Ouroboros.Consensus.Util
3940
import Ouroboros.Consensus.Util.Condense
@@ -143,6 +144,12 @@ instance HasPerasCertRound (PerasCert blk) where
143144
instance HasPerasCertRound (ValidatedPerasCert blk) where
144145
getPerasCertRound = getPerasCertRound . vpcCert
145146

147+
instance
148+
HasPerasCertRound cert =>
149+
HasPerasCertRound (WithArrivalTime cert)
150+
where
151+
getPerasCertRound = getPerasCertRound . forgetArrivalTime
152+
146153
-- | Extract the boosted block point from a Peras certificate container
147154
class HasPerasCertBoostedBlock cert blk | cert -> blk where
148155
getPerasCertBoostedBlock :: cert -> Point blk
@@ -153,9 +160,21 @@ instance HasPerasCertBoostedBlock (PerasCert blk) blk where
153160
instance HasPerasCertBoostedBlock (ValidatedPerasCert blk) blk where
154161
getPerasCertBoostedBlock = getPerasCertBoostedBlock . vpcCert
155162

163+
instance
164+
HasPerasCertBoostedBlock cert blk =>
165+
HasPerasCertBoostedBlock (WithArrivalTime cert) blk
166+
where
167+
getPerasCertBoostedBlock = getPerasCertBoostedBlock . forgetArrivalTime
168+
156169
-- | Extract the certificate boost from a Peras certificate container
157170
class HasPerasCertBoost cert where
158171
getPerasCertBoost :: cert -> PerasWeight
159172

160173
instance HasPerasCertBoost (ValidatedPerasCert blk) where
161174
getPerasCertBoost = vpcCertBoost
175+
176+
instance
177+
HasPerasCertBoost cert =>
178+
HasPerasCertBoost (WithArrivalTime cert)
179+
where
180+
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)