@@ -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)
4443import Control.Concurrent (threadDelay )
4544import Control.Exception (Exception (.. ))
4645import Control.Monad
47- import Control.Monad.Trans.Resource.Internal (ReleaseMap (.. ))
4846import Data.Aeson
4947import qualified Data.Aeson.Encode.Pretty as A
5048import qualified Data.Aeson.KeyMap as A
51- import Data.Acquire.Internal (ReleaseType )
5249import qualified Data.ByteString.Lazy as LBS
5350import Data.Default.Class (def )
5451import Data.Either
5552import Data.Functor
56- import Data.IntMap (IntMap )
57- import qualified Data.IntMap as IntMap
5853import Data.MonoTraversable (Element , MonoFunctor , omap )
5954import qualified Data.Text as Text
6055import Data.Time (diffUTCTime )
@@ -78,11 +73,10 @@ import qualified Hedgehog.Extras as H
7873import 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 )
8378import Testnet.Orphans ()
84- import RIO.Orphans (HasResourceMap , withResourceMap )
85- import Control.Monad.IO.Unlift
79+ import RIO.Orphans (ResourceMap )
8680
8781
8882newtype 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
199103createTestnetEnv :: ()
200104 => HasCallStack
@@ -321,10 +225,9 @@ createTestnetEnv
321225-- > ├── current-stake-pools.json
322226-- > └── module
323227cardanoTestnet :: 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
328231cardanoTestnet
329232 testnetOptions
330233 Conf
@@ -547,8 +450,7 @@ createAndRunTestnet :: ()
547450 -> Conf -- ^ Path to the test sandbox
548451 -> H. Integration TestnetRuntime
549452createAndRunTestnet 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
0 commit comments