Skip to content

Commit d07a2ac

Browse files
committed
Refactor
1 parent d696dd0 commit d07a2ac

File tree

13 files changed

+31
-159
lines changed

13 files changed

+31
-159
lines changed

cabal.project

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ packages:
3535
-- Needed when cross compiling
3636
extra-packages: alex
3737

38-
-- program-options
39-
-- ghc-options: -Werror
38+
program-options
39+
ghc-options: -Werror
4040

4141
test-show-details: direct
4242

cardano-testnet/cardano-testnet.cabal

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ library
100100
, time
101101
, transformers
102102
, transformers-except
103-
, unliftio-core
104103
, vector
105104
, yaml
106105

@@ -271,7 +270,6 @@ test-suite cardano-testnet-test
271270
, mtl
272271
, process
273272
, regex-compat
274-
, resourcet
275273
, rio
276274
, tasty ^>= 1.5
277275
, text

cardano-testnet/src/Parsers/Run.hs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ module Parsers.Run
1111

1212
import Cardano.CLI.Environment
1313
import Control.Monad
14-
import Control.Monad.Trans.Class (lift)
15-
import Control.Monad.Trans.Resource (getInternalState)
1614

1715
import Data.Default.Class (def)
1816
import Data.Foldable
@@ -65,8 +63,7 @@ createEnvOptions CardanoTestnetCreateEnvOptions
6563
, createEnvCreateEnvOptions=ceOptions
6664
} =
6765
testnetRoutine (UserProvidedEnv outputDir) $ \conf -> do
68-
r <- lift $ lift getInternalState
69-
liftToIntegration r $
66+
liftToIntegration $
7067
createTestnetEnv
7168
testnetOptions genesisOptions ceOptions
7269
-- Do not add hashes to the main config file, so that genesis files

cardano-testnet/src/Testnet/Runtime.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ startNode tp node ipv4 port _testnetMagic nodeCmd = GHC.withFrozenCallStack $ do
147147
, "--port", show port
148148
, "--host-addr", showIpv4Address ipv4
149149
]
150-
-- error "startNode 1" -- works
151150
nodeProcess <- newExceptT . fmap (first ExecutableRelatedFailure) . try $ runRIO () $ procNode completeNodeCmd
152151

153152
-- The port number if it is obtained using 'H.randomPort', it is firstly bound to and then closed. The closing
@@ -176,15 +175,15 @@ startNode tp node ipv4 port _testnetMagic nodeCmd = GHC.withFrozenCallStack $ do
176175

177176
-- We then log the pid in the temp dir structure.
178177
liftIO $ IO.writeFile nodePidFile $ show pid
179-
-- error "pre eSprocketError " -- THIS FAILS DIAGNOSE THIS
178+
180179
-- Wait for socket to be created
181180
eSprocketError <-
182181
liftIO $
183182
Ping.waitForSprocket
184183
60 -- timeout
185184
0.2 -- check interval
186185
sprocket
187-
-- error $ "startNode 2:" <> show eSprocketError -- Works! after replacing bracket
186+
188187
-- If we do have anything on stderr, fail.
189188
stdErrContents <- liftIO $ IO.readFile nodeStderrFile
190189
unless (null stdErrContents) $

cardano-testnet/src/Testnet/Start/Cardano.hs

Lines changed: 9 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ module Testnet.Start.Cardano
2525
, retryOnAddressInUseError
2626
-- Move to my own module
2727
, liftToIntegration
28-
, reconcileMaps
2928
) where
3029

3130

@@ -44,17 +43,13 @@ import Prelude hiding (lines)
4443
import Control.Concurrent (threadDelay)
4544
import Control.Exception (Exception (..))
4645
import Control.Monad
47-
import Control.Monad.Trans.Resource.Internal (ReleaseMap(..))
4846
import Data.Aeson
4947
import qualified Data.Aeson.Encode.Pretty as A
5048
import qualified Data.Aeson.KeyMap as A
51-
import Data.Acquire.Internal (ReleaseType)
5249
import qualified Data.ByteString.Lazy as LBS
5350
import Data.Default.Class (def)
5451
import Data.Either
5552
import Data.Functor
56-
import Data.IntMap (IntMap)
57-
import qualified Data.IntMap as IntMap
5853
import Data.MonoTraversable (Element, MonoFunctor, omap)
5954
import qualified Data.Text as Text
6055
import Data.Time (diffUTCTime)
@@ -78,11 +73,10 @@ import qualified Hedgehog.Extras as H
7873
import qualified Hedgehog.Extras.Stock.IO.Network.Port as H
7974

8075

81-
import RIO (RIO(..),runRIO, IORef, readIORef, modifyIORef', throwM)
82-
import Control.Monad.Trans.Resource (ResourceT, getInternalState)
76+
import RIO (RIO(..),runRIO, throwM)
77+
import Control.Monad.Trans.Resource (getInternalState)
8378
import Testnet.Orphans ()
84-
import RIO.Orphans (HasResourceMap, withResourceMap)
85-
import Control.Monad.IO.Unlift
79+
import RIO.Orphans (ResourceMap)
8680

8781

8882
newtype MinimumConfigRequirementsError
@@ -101,100 +95,10 @@ testMinimumConfigurationRequirements options = withFrozenCallStack $ do
10195
when (cardanoNumPools options < 1) $ do
10296
throwM $ MinimumConfigRequirementsError "Need at least one SPO node to produce blocks, but got none."
10397

104-
liftToIntegration :: IORef ReleaseMap -> RIO (IORef ReleaseMap) a -> H.Integration a
105-
liftToIntegration m r = lift . lift $ updateRIOState m r
106-
107-
108-
-- Why do we need to do this? What happens if an IO action fails
109-
-- when running the RIO monad? We need to clean up everthing else.
110-
-- TODO: Accomodate for the IntegrationState as well!
111-
updateRIOState
112-
:: MonadUnliftIO m
113-
=> IORef ReleaseMap -- ^ External resource map
114-
-> RIO (IORef ReleaseMap) a
115-
-> ResourceT m a
116-
updateRIOState externalResourceMap rioAction =
117-
withResourceMap
118-
(\rioActionResoureMap -> do
119-
liftIO $ appendResourceMap externalResourceMap rioActionResoureMap
120-
updatedState <- getInternalState
121-
runRIO updatedState rioAction
122-
)
123-
where
124-
-- TODO: Figure out how to do this!
125-
appendResourceMap :: IORef ReleaseMap -> IORef ReleaseMap -> IO ()
126-
appendResourceMap externalMap internalMap = do
127-
extMap <- readIORef externalMap
128-
intMap <- readIORef internalMap
129-
modifyIORef' internalMap $ const $ appendResourceMapPure extMap intMap
130-
131-
132-
appendResourceMapPure :: ReleaseMap -> ReleaseMap -> ReleaseMap
133-
appendResourceMapPure (ReleaseMap _ rf1 m1) (ReleaseMap _ rf2 m2) =
134-
let finalMap = reconcileMaps m1 m2
135-
in ReleaseMap (getNextKey finalMap) (rf1 + rf2) finalMap
136-
appendResourceMapPure _ _ = error "failed"
137-
138-
{-
139-
createInternalState :: MonadIO m => m InternalState
140-
createInternalState = liftIO
141-
$ I.newIORef
142-
$ ReleaseMap maxBound (minBound + 1) IntMap.empty
143-
register' :: I.IORef ReleaseMap
144-
-> IO ()
145-
-> IO ReleaseKey
146-
register' istate rel = I.atomicModifyIORef istate $ \rm ->
147-
case rm of
148-
ReleaseMap key rf m ->
149-
( ReleaseMap (key - 1) rf (IntMap.insert key (const rel) m)
150-
, ReleaseKey istate key
151-
)
152-
ReleaseMapClosed -> throw $ InvalidAccess "register'"
153-
154-
-}
155-
156-
getNextKey :: IntMap (ReleaseType -> IO ()) -> Int
157-
getNextKey m = case IntMap.lookupMin m of
158-
Just (mini,_) -> mini - 1
159-
Nothing -> maxBound
160-
161-
reconcileMaps :: IntMap a-> IntMap a -> IntMap a
162-
reconcileMaps externalMap internalMap =
163-
let externalValues = IntMap.elems externalMap
164-
internalValues = IntMap.elems internalMap
165-
finalValues = externalValues ++ internalValues
166-
in IntMap.fromList $ zip (dec maxBound) finalValues
167-
where
168-
dec 0 = []
169-
dec start =
170-
start : dec (start - 1)
171-
172-
-- TODO: Left off here
173-
-- You are already decrementing the key. Therefore get the mod of each key
174-
-- add them and then add the negative
175-
-- The reference account appears to stay the same when registering actions
176-
-- You need to generate the next key which is the negative of the sum of both keys plus 1
177-
-- You need to merge the maps. However you need to reassign the keys in the RIO internal map.
178-
-- You need to start from the most negative key in the external map and decrement it for each key in the internal
179-
-- RIO map.
180-
181-
182-
{-
183-
register' :: I.IORef ReleaseMap
184-
-> IO ()
185-
-> IO ReleaseKey
186-
register' istate rel = I.atomicModifyIORef istate $ \rm ->
187-
case rm of
188-
ReleaseMap key rf m ->
189-
-- First value in tuple is important
190-
191-
( ReleaseMap (key - 1) rf (IntMap.insert key (const rel) m)
192-
, ReleaseKey istate key
193-
)
194-
ReleaseMapClosed -> throw $ InvalidAccess "register'"
195-
196-
-}
197-
98+
liftToIntegration :: RIO ResourceMap a -> H.Integration a
99+
liftToIntegration r = do
100+
rMap <- lift $ lift getInternalState
101+
liftIO $ runRIO rMap r
198102

199103
createTestnetEnv :: ()
200104
=> HasCallStack
@@ -321,10 +225,9 @@ createTestnetEnv
321225
-- > ├── current-stake-pools.json
322226
-- > └── module
323227
cardanoTestnet :: HasCallStack
324-
=> HasResourceMap env
325228
=> CardanoTestnetOptions -- ^ The options to use
326229
-> Conf -- ^ Path to the test sandbox
327-
-> RIO env TestnetRuntime
230+
-> RIO ResourceMap TestnetRuntime
328231
cardanoTestnet
329232
testnetOptions
330233
Conf
@@ -547,8 +450,7 @@ createAndRunTestnet :: ()
547450
-> Conf -- ^ Path to the test sandbox
548451
-> H.Integration TestnetRuntime
549452
createAndRunTestnet testnetOptions genesisOptions conf = do
550-
r <- lift $ lift getInternalState
551-
liftToIntegration r $ do
453+
liftToIntegration $ do
552454
-- works error "here"
553455
createTestnetEnv
554456
testnetOptions genesisOptions def

cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/Cli/KesPeriodInfo.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Prelude
2222

2323
import Control.Monad
2424
import qualified Data.Aeson as Aeson
25+
import qualified Data.Aeson.Encode.Pretty as Aeson
2526
import qualified Data.Aeson as J
2627
import Data.Default.Class
2728
import Data.Function
@@ -49,6 +50,7 @@ import qualified Hedgehog.Extras.Stock.IO.Network.Sprocket as IO
4950
import qualified Hedgehog.Extras.Test.Base as H
5051
import qualified Hedgehog.Extras.Test.File as H
5152
import qualified Hedgehog.Extras.Test.TestWatchdog as H
53+
import Testnet.Start.Cardano (liftToIntegration)
5254

5355

5456
-- | Execute me with:
@@ -254,7 +256,7 @@ hprop_kes_period_info = integrationRetryWorkspace 2 "kes-period-info" $ \tempAbs
254256
, "--out-file", testSpoOperationalCertFp
255257
]
256258

257-
jsonBS <- undefined -- Aeson.encodePretty . Aeson.Object <$> createConfigJson tempAbsPath sbe
259+
jsonBS <- liftToIntegration $ Aeson.encodePretty . Aeson.Object <$> createConfigJson tempAbsPath sbe
258260
H.lbsWriteFile (unFile configurationFile) jsonBS
259261
newNodePortNumber <- H.randomPort testnetDefaultIpv4Address
260262
eRuntime <- runExceptT . retryOnAddressInUseError $

cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/Cli/LeadershipSchedule.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import Cardano.Testnet
2222
import Prelude
2323

2424
import Control.Monad (void)
25-
import Control.Monad.Trans.Resource (getInternalState)
2625
import qualified Data.Aeson as Aeson
2726
import qualified Data.Aeson as J
2827
import qualified Data.Aeson.Encode.Pretty as Aeson
@@ -258,8 +257,7 @@ hprop_leadershipSchedule = integrationRetryWorkspace 2 "leadership-schedule" $ \
258257
, "--operational-certificate-issue-counter-file", testSpoOperationalCertFp
259258
, "--out-file", testSpoOperationalCertFp
260259
]
261-
r1 <- lift $ lift getInternalState
262-
jsonBS <- liftToIntegration r1 $
260+
jsonBS <- liftToIntegration $
263261
Aeson.encodePretty . Aeson.Object <$> createConfigJson tempAbsPath sbe
264262
H.lbsWriteFile (unFile configurationFile) jsonBS
265263
newNodePort <- H.randomPort testnetDefaultIpv4Address

cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/DumpConfig.hs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import Cardano.Testnet.Test.Utils (nodesProduceBlocks)
1717

1818
import Prelude
1919

20-
import Control.Monad.Trans.Class (lift)
21-
import Control.Monad.Trans.Resource (getInternalState)
2220
import Data.Aeson.Encode.Pretty (encodePretty)
2321
import Data.Default.Class (def)
2422
import qualified Data.Time.Clock as Time
@@ -48,8 +46,7 @@ hprop_dump_config = integrationRetryWorkspace 2 "dump-config-files" $ \tmpDir ->
4846
conf <- mkConf tmpDir
4947
-- TODO: Make this a standalone function for testing only
5048
-- see createAndRunTestnet
51-
r1 <- lift $ lift getInternalState
52-
liftToIntegration r1 $ createTestnetEnv
49+
liftToIntegration $ createTestnetEnv
5350
testnetOptions genesisOptions def
5451
-- Do not add hashes to the main config file, so that genesis files
5552
-- can be modified without having to recompute hashes every time.
@@ -75,7 +72,6 @@ hprop_dump_config = integrationRetryWorkspace 2 "dump-config-files" $ \tmpDir ->
7572
H.lbsWriteFile shelleyGenesisFile $ encodePretty shelleyGenesis
7673

7774
-- Run testnet with generated config
78-
r2 <- lift $ lift getInternalState
79-
runtime <- liftToIntegration r2 $ cardanoTestnet testnetOptions conf
75+
runtime <- liftToIntegration $ cardanoTestnet testnetOptions conf
8076

8177
nodesProduceBlocks tmpDir runtime

cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/Gov/NoConfidence.hs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import Cardano.Testnet
2121
import Prelude
2222

2323
import Control.Monad
24-
import Control.Monad.Trans.Resource (getInternalState)
2524
import qualified Data.ByteString.Char8 as BSC
2625
import Data.Default.Class
2726
import qualified Data.Map.Strict as Map
@@ -105,19 +104,17 @@ hprop_gov_no_confidence = integrationWorkspace "no-confidence" $ \tempAbsBasePat
105104
committeeThreshold = unsafeBoundedRational 0.5
106105
committee = L.Committee (Map.fromList [(comKeyCred1, EpochNo 100)]) committeeThreshold
107106

108-
r1 <- lift $ lift getInternalState
109-
liftToIntegration r1 $ createTestnetEnv fastTestnetOptions genesisOptions def conf
107+
liftToIntegration $ createTestnetEnv fastTestnetOptions genesisOptions def conf
110108

111109
H.rewriteJsonFile (tempAbsBasePath' </> "conway-genesis.json") $
112110
\conwayGenesis -> conwayGenesis { L.cgCommittee = committee }
113111

114-
r2 <- lift $ lift getInternalState
115112
TestnetRuntime
116113
{ testnetMagic
117114
, testnetNodes
118115
, wallets=wallet0:_wallet1:_
119116
, configurationFile
120-
} <- liftToIntegration r2 $ cardanoTestnet fastTestnetOptions conf
117+
} <- liftToIntegration $ cardanoTestnet fastTestnetOptions conf
121118

122119
poolNode1 <- H.headM testnetNodes
123120
poolSprocket1 <- H.noteShow $ nodeSprocket poolNode1

cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/MainnetParams.hs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ module Cardano.Testnet.Test.MainnetParams
99

1010
import Cardano.Testnet
1111

12-
import Control.Monad.Trans.Class (lift)
13-
import Control.Monad.Trans.Resource (getInternalState)
1412
import qualified Data.Aeson as A
1513
import qualified Data.Aeson.Lens as A
1614
import qualified Data.ByteString.Lazy.Char8 as B
@@ -41,16 +39,14 @@ hprop_mainnet_params = integrationRetryWorkspace 2 "mainnet-params" $ \tmpDir ->
4139

4240
-- Generate the sandbox
4341
conf <- mkConf tmpDir
44-
r <- lift $ lift getInternalState
45-
liftToIntegration r $ createTestnetEnv
42+
liftToIntegration $ createTestnetEnv
4643
testnetOptions genesisOptions createEnvOptions conf
4744

4845
-- Run testnet with mainnet on-chain params
49-
r1 <- lift $ lift getInternalState
5046
TestnetRuntime
5147
{ testnetNodes
5248
, testnetMagic
53-
} <- liftToIntegration r1 $ cardanoTestnet testnetOptions conf
49+
} <- liftToIntegration $ cardanoTestnet testnetOptions conf
5450

5551
-- Get a running node
5652
TestnetNode{nodeSprocket} <- H.headM testnetNodes

0 commit comments

Comments
 (0)