Skip to content

Commit b5a63de

Browse files
committed
Use NominalDiffTime in RunningNode blockTime
This is generally a more desirable type to describe a given duration of time, e.g. how long to wait. Updating our wait.. functions to NominalDiffTime also removed some conversions in test code (realToFrac).
1 parent baaca52 commit b5a63de

File tree

11 files changed

+45
-46
lines changed

11 files changed

+45
-46
lines changed

hydra-cluster/bench/Bench/EndToEnd.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ data Event = Event
7676
deriving stock (Generic, Eq, Show)
7777
deriving anyclass (ToJSON)
7878

79-
bench :: Int -> DiffTime -> FilePath -> Dataset -> IO Summary
79+
bench :: Int -> NominalDiffTime -> FilePath -> Dataset -> IO Summary
8080
bench startingNodeId timeoutSeconds workDir dataset@Dataset{clientDatasets, title, description} = do
8181
putStrLn $ "Test logs available in: " <> (workDir </> "test.log")
8282
withFile (workDir </> "test.log") ReadWriteMode $ \hdl ->
@@ -122,7 +122,7 @@ bench startingNodeId timeoutSeconds workDir dataset@Dataset{clientDatasets, titl
122122
v ^? key "contestationDeadline" . _JSON
123123

124124
-- Expect to see ReadyToFanout within 3 seconds after deadline
125-
remainingTime <- realToFrac . diffUTCTime deadline <$> getCurrentTime
125+
remainingTime <- diffUTCTime deadline <$> getCurrentTime
126126
waitFor hydraTracer (remainingTime + 3) [leader] $
127127
output "ReadyToFanout" ["headId" .= headId]
128128

hydra-cluster/bench/Bench/Options.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ data Options
3131
{ workDirectory :: Maybe FilePath
3232
, outputDirectory :: Maybe FilePath
3333
, scalingFactor :: Int
34-
, timeoutSeconds :: DiffTime
34+
, timeoutSeconds :: NominalDiffTime
3535
, clusterSize :: Word64
3636
, startingNodeId :: Int
3737
}
3838
| DatasetOptions
3939
{ datasetFiles :: [FilePath]
4040
, outputDirectory :: Maybe FilePath
41-
, timeoutSeconds :: DiffTime
41+
, timeoutSeconds :: NominalDiffTime
4242
, startingNodeId :: Int
4343
}
4444

@@ -119,7 +119,7 @@ scalingFactorParser =
119119
<> help "The scaling factor to apply to transactions generator (default: 100)"
120120
)
121121

122-
timeoutParser :: Parser DiffTime
122+
timeoutParser :: Parser NominalDiffTime
123123
timeoutParser =
124124
option
125125
auto

hydra-cluster/src/CardanoClient.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,6 @@ mkGenesisTx networkId pparams signingKey initialAmount recipients =
164164
data RunningNode = RunningNode
165165
{ nodeSocket :: SocketPath
166166
, networkId :: NetworkId
167-
, blockTime :: DiffTime
167+
, blockTime :: NominalDiffTime
168168
-- ^ Expected time between blocks (varies a lot on testnets)
169169
}

hydra-cluster/src/Hydra/Cluster/Scenarios.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import Hydra.Cluster.Faucet (FaucetLog, createOutputAtAddress, seedFromFaucet, s
6060
import Hydra.Cluster.Faucet qualified as Faucet
6161
import Hydra.Cluster.Fixture (Actor (..), actorName, alice, aliceSk, aliceVk, bob, bobSk, bobVk, carol, carolSk)
6262
import Hydra.Cluster.Util (chainConfigFor, keysFor, modifyConfig, setNetworkId)
63-
import Hydra.ContestationPeriod (ContestationPeriod (UnsafeContestationPeriod), fromDiffTime)
63+
import Hydra.ContestationPeriod (ContestationPeriod (UnsafeContestationPeriod), fromNominalDiffTime)
6464
import Hydra.HeadId (HeadId)
6565
import Hydra.Ledger (IsTx (balance))
6666
import Hydra.Ledger.Cardano (genKeyPair)
@@ -232,7 +232,7 @@ singlePartyHeadFullLifeCycle tracer workDir node hydraScriptsTxId =
232232
refuelIfNeeded tracer node Alice 25_000_000
233233
-- Start hydra-node on chain tip
234234
tip <- queryTip networkId nodeSocket
235-
contestationPeriod <- fromDiffTime $ 10 * blockTime
235+
contestationPeriod <- fromNominalDiffTime $ 10 * blockTime
236236
aliceChainConfig <-
237237
chainConfigFor Alice workDir nodeSocket hydraScriptsTxId [] contestationPeriod
238238
<&> modifyConfig (\config -> config{networkId, startChainFrom = Just tip})
@@ -250,7 +250,7 @@ singlePartyHeadFullLifeCycle tracer workDir node hydraScriptsTxId =
250250
guard $ v ^? key "tag" == Just "HeadIsClosed"
251251
guard $ v ^? key "headId" == Just (toJSON headId)
252252
v ^? key "contestationDeadline" . _JSON
253-
remainingTime <- realToFrac . diffUTCTime deadline <$> getCurrentTime
253+
remainingTime <- diffUTCTime deadline <$> getCurrentTime
254254
waitFor hydraTracer (remainingTime + 3 * blockTime) [n1] $
255255
output "ReadyToFanout" ["headId" .= headId]
256256
send n1 $ input "Fanout" []

hydra-cluster/src/HydraNode.hs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ output tag pairs = object $ ("tag" .= tag) : pairs
7474
-- | Wait some time for a single API server output from each of given nodes.
7575
-- This function waits for @delay@ seconds for message @expected@ to be seen by all
7676
-- given @nodes@.
77-
waitFor :: HasCallStack => Tracer IO HydraNodeLog -> DiffTime -> [HydraClient] -> Aeson.Value -> IO ()
77+
waitFor :: HasCallStack => Tracer IO HydraNodeLog -> NominalDiffTime -> [HydraClient] -> Aeson.Value -> IO ()
7878
waitFor tracer delay nodes v = waitForAll tracer delay nodes [v]
7979

8080
-- | Wait up to some time for an API server output to match the given predicate.
81-
waitMatch :: HasCallStack => DiffTime -> HydraClient -> (Aeson.Value -> Maybe a) -> IO a
81+
waitMatch :: HasCallStack => NominalDiffTime -> HydraClient -> (Aeson.Value -> Maybe a) -> IO a
8282
waitMatch delay client@HydraClient{tracer, hydraNodeId} match = do
8383
seenMsgs <- newTVarIO []
84-
timeout delay (go seenMsgs) >>= \case
84+
timeout (realToFrac delay) (go seenMsgs) >>= \case
8585
Just x -> pure x
8686
Nothing -> do
8787
msgs <- readTVarIO seenMsgs
@@ -106,7 +106,7 @@ waitMatch delay client@HydraClient{tracer, hydraNodeId} match = do
106106
-- | Wait up to some `delay` for some JSON `Value` to match given function.
107107
--
108108
-- This is a generalisation of `waitMatch` to multiple nodes.
109-
waitForAllMatch :: (Eq a, Show a, HasCallStack) => DiffTime -> [HydraClient] -> (Aeson.Value -> Maybe a) -> IO a
109+
waitForAllMatch :: (Eq a, Show a, HasCallStack) => NominalDiffTime -> [HydraClient] -> (Aeson.Value -> Maybe a) -> IO a
110110
waitForAllMatch delay nodes match = do
111111
when (null nodes) $
112112
failure "no clients to wait for"
@@ -122,13 +122,12 @@ waitForAllMatch delay nodes match = do
122122
-- | Wait some time for a list of outputs from each of given nodes.
123123
-- This function is the generalised version of 'waitFor', allowing several messages
124124
-- to be waited for and received in /any order/.
125-
waitForAll :: HasCallStack => Tracer IO HydraNodeLog -> DiffTime -> [HydraClient] -> [Aeson.Value] -> IO ()
125+
waitForAll :: HasCallStack => Tracer IO HydraNodeLog -> NominalDiffTime -> [HydraClient] -> [Aeson.Value] -> IO ()
126126
waitForAll tracer delay nodes expected = do
127127
traceWith tracer (StartWaiting (map hydraNodeId nodes) expected)
128128
forConcurrently_ nodes $ \client@HydraClient{hydraNodeId} -> do
129129
msgs <- newIORef []
130-
-- The chain is slow...
131-
result <- timeout delay $ tryNext client msgs expected
130+
result <- timeout (realToFrac delay) $ tryNext client msgs expected
132131
case result of
133132
Just x -> pure x
134133
Nothing -> do
@@ -393,13 +392,13 @@ withConnectionToNode tracer hydraNodeId action = do
393392
hydraNodeProcess :: RunOptions -> CreateProcess
394393
hydraNodeProcess = proc "hydra-node" . toArgs
395394

396-
waitForNodesConnected :: HasCallStack => Tracer IO HydraNodeLog -> DiffTime -> [HydraClient] -> IO ()
397-
waitForNodesConnected tracer timeOut clients =
395+
waitForNodesConnected :: HasCallStack => Tracer IO HydraNodeLog -> NominalDiffTime -> [HydraClient] -> IO ()
396+
waitForNodesConnected tracer delay clients =
398397
mapM_ waitForNodeConnected clients
399398
where
400399
allNodeIds = hydraNodeId <$> clients
401400
waitForNodeConnected n@HydraClient{hydraNodeId} =
402-
waitForAll tracer timeOut [n] $
401+
waitForAll tracer delay [n] $
403402
fmap
404403
( \nodeId ->
405404
object

hydra-cluster/test/Test/EndToEndSpec.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ spec = around (showLogsOnFailure "EndToEndSpec") $ do
269269
v ^? key "contestationDeadline" . _JSON
270270

271271
-- Expect to see ReadyToFanout within 3 seconds after deadline
272-
remainingTime <- realToFrac . diffUTCTime deadline <$> getCurrentTime
272+
remainingTime <- diffUTCTime deadline <$> getCurrentTime
273273
waitFor hydraTracer (remainingTime + 3) [n1] $
274274
output "ReadyToFanout" ["headId" .= headId]
275275

@@ -831,7 +831,7 @@ initAndClose tmpDir tracer clusterIx hydraScriptsTxId node@RunningNode{nodeSocke
831831
v ^? key "contestationDeadline" . _JSON
832832

833833
-- Expect to see ReadyToFanout within 3 seconds after deadline
834-
remainingTime <- realToFrac . diffUTCTime deadline <$> getCurrentTime
834+
remainingTime <- diffUTCTime deadline <$> getCurrentTime
835835
waitFor hydraTracer (remainingTime + 3) [n1] $
836836
output "ReadyToFanout" ["headId" .= headId]
837837

hydra-node/src/Hydra/ContestationPeriod.hs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,20 @@ instance Arbitrary ContestationPeriod where
3737
oneMonth = oneDay * 30
3838
oneYear = oneDay * 365
3939

40-
-- | Create a 'ContestationPeriod' from a 'DiffTime'. This will fail if a
41-
-- negative DiffTime is provided and truncates to 1s if values < 1s are given.
42-
fromDiffTime :: MonadFail m => DiffTime -> m ContestationPeriod
43-
fromDiffTime dt =
40+
-- | Create a 'ContestationPeriod' from a 'NominalDiffTime'. This will fail if a
41+
-- negative NominalDiffTime is provided and truncates to 1s if values < 1s are given.
42+
fromNominalDiffTime :: MonadFail m => NominalDiffTime -> m ContestationPeriod
43+
fromNominalDiffTime dt =
4444
if seconds > 0
4545
then pure . UnsafeContestationPeriod $ ceiling seconds
46-
else fail $ "fromDiffTime: contestation period <= 0: " <> show dt
46+
else fail $ "fromNominalDiffTime: contestation period <= 0: " <> show dt
4747
where
4848
seconds :: Pico = realToFrac dt
4949

50+
toNominalDiffTime :: ContestationPeriod -> NominalDiffTime
51+
toNominalDiffTime (UnsafeContestationPeriod s) =
52+
secondsToNominalDiffTime $ fromIntegral s
53+
5054
-- | Convert an off-chain contestation period to its on-chain representation.
5155
toChain :: ContestationPeriod -> OnChain.ContestationPeriod
5256
toChain (UnsafeContestationPeriod s) =
@@ -61,7 +65,3 @@ fromChain cp =
6165
UnsafeContestationPeriod
6266
. truncate
6367
$ toInteger (OnChain.milliseconds cp) % 1000
64-
65-
toNominalDiffTime :: ContestationPeriod -> NominalDiffTime
66-
toNominalDiffTime (UnsafeContestationPeriod s) =
67-
secondsToNominalDiffTime $ fromIntegral s

hydra-node/src/Hydra/Options.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import Hydra.Cardano.Api (
3434
serialiseToRawBytesHexText,
3535
)
3636
import Hydra.Chain (maximumNumberOfParties)
37-
import Hydra.ContestationPeriod (ContestationPeriod (UnsafeContestationPeriod), fromDiffTime)
37+
import Hydra.ContestationPeriod (ContestationPeriod (UnsafeContestationPeriod), fromNominalDiffTime)
3838
import Hydra.Contract qualified as Contract
3939
import Hydra.Ledger.Cardano ()
4040
import Hydra.Logging (Verbosity (..))
@@ -745,7 +745,7 @@ contestationPeriodParser =
745745
where
746746
parseNatural = UnsafeContestationPeriod <$> auto
747747

748-
parseViaDiffTime = auto >>= fromDiffTime
748+
parseViaDiffTime = auto >>= fromNominalDiffTime
749749

750750
data InvalidOptions
751751
= MaximumNumberOfPartiesExceeded

hydra-node/test/Hydra/BehaviorSpec.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,10 +721,10 @@ testContestationPeriod = UnsafeContestationPeriod 3600
721721
nothingHappensFor ::
722722
(MonadTimer m, MonadThrow m, IsChainState tx) =>
723723
TestHydraClient tx m ->
724-
DiffTime ->
724+
NominalDiffTime ->
725725
m ()
726726
nothingHappensFor node secs =
727-
timeout secs (waitForNext node) >>= (`shouldBe` Nothing)
727+
timeout (realToFrac secs) (waitForNext node) >>= (`shouldBe` Nothing)
728728

729729
withHydraNode ::
730730
forall s a.
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
module Hydra.ContestationPeriodSpec where
22

3-
import Hydra.Prelude
3+
import Hydra.Prelude hiding (label)
44

5-
import Data.Time (picosecondsToDiffTime)
6-
import Hydra.ContestationPeriod (ContestationPeriod, fromDiffTime)
5+
import Data.Time (secondsToNominalDiffTime)
6+
import Hydra.ContestationPeriod (ContestationPeriod, fromNominalDiffTime)
77
import Test.Hspec (Spec, describe)
88
import Test.Hspec.QuickCheck (prop)
99
import Test.QuickCheck (getNonPositive, getPositive, (===))
1010
import Test.QuickCheck.Instances.Time ()
1111

1212
spec :: Spec
1313
spec = do
14-
describe "fromDiffTime" $ do
14+
describe "fromNominalDiffTime" $ do
1515
prop "works for diff times > 0" $
16-
isJust . fromDiffTime . getPositive
16+
isJust . fromNominalDiffTime . getPositive
1717

1818
prop "fails for diff times <= 0" $
19-
isNothing . fromDiffTime . getNonPositive
19+
isNothing . fromNominalDiffTime . getNonPositive
2020

2121
prop "rounds to 1 second" $ \n ->
22-
let subSecondPicos = getPositive n `mod` 1_000_000_000_000
23-
in fromDiffTime (picosecondsToDiffTime subSecondPicos)
24-
=== (fromDiffTime 1 :: Maybe ContestationPeriod)
22+
let subSecond = getPositive n / 100 -- Definitely < 1 second
23+
in fromNominalDiffTime (secondsToNominalDiffTime subSecond)
24+
=== (fromNominalDiffTime 1 :: Maybe ContestationPeriod)

0 commit comments

Comments
 (0)