Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 81f8f8e

Browse files
committed
start implementing startNode and stopNode
1 parent 00b85b4 commit 81f8f8e

File tree

6 files changed

+92
-57
lines changed

6 files changed

+92
-57
lines changed

pkgs/default.nix

+4
Original file line numberDiff line numberDiff line change
@@ -16242,6 +16242,7 @@ license = stdenv.lib.licenses.mit;
1624216242
, lifted-async
1624316243
, mtl
1624416244
, optparse-applicative
16245+
, process
1624516246
, reflection
1624616247
, resourcet
1624716248
, serokell-util
@@ -16250,6 +16251,7 @@ license = stdenv.lib.licenses.mit;
1625016251
, text
1625116252
, transformers
1625216253
, universum
16254+
, unix
1625316255
, unordered-containers
1625416256
, vty
1625516257
}:
@@ -16286,13 +16288,15 @@ lens
1628616288
lifted-async
1628716289
mtl
1628816290
optparse-applicative
16291+
process
1628916292
reflection
1629016293
resourcet
1629116294
serokell-util
1629216295
stm
1629316296
text
1629416297
transformers
1629516298
universum
16299+
unix
1629616300
unordered-containers
1629716301
vty
1629816302
];

script-runner/AutomatedTestRunner.hs

+39-23
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{-# LANGUAGE RankNTypes #-}
99
{-# LANGUAGE TypeApplications #-}
1010

11-
module AutomatedTestRunner (Example, getGenesisConfig, loadNKeys, doUpdate, onStartup, on, getScript, runScript, NodeType(..), startNode) where
11+
module AutomatedTestRunner (Example, getGenesisConfig, loadNKeys, doUpdate, onStartup, on, getScript, runScript, NodeType(..), startNode, stopNode, NodeHandle) where
1212

1313
import Brick hiding (on)
1414
import Brick.BChan
@@ -35,7 +35,7 @@ import Options.Applicative (Parser, execParser, footerDoc, fullDesc,
3535
header, help, helper, info, infoOption, long, progDesc)
3636
import Paths_cardano_sl (version)
3737
import PocMode (AuxxContext (AuxxContext, acRealModeContext),
38-
AuxxMode, realModeToAuxx)
38+
PocMode, realModeToAuxx)
3939
import Pos.Chain.Block (LastKnownHeaderTag)
4040
import Pos.Chain.Genesis as Genesis
4141
(Config (configGeneratedSecrets, configProtocolMagic),
@@ -69,11 +69,13 @@ import Pos.Util (lensOf, logException)
6969
import Pos.Util.CompileInfo (CompileTimeInfo (ctiGitRevision),
7070
HasCompileInfo, compileInfo, withCompileInfo)
7171
import Pos.Util.UserSecret (readUserSecret, usKeys, usPrimKey, usVss)
72-
import Pos.Util.Wlog (LoggerName)
72+
import Pos.Util.Wlog (LoggerName, logWarning)
7373
import Pos.WorkMode (EmptyMempoolExt, RealMode)
7474
import Prelude (show)
7575
import Text.PrettyPrint.ANSI.Leijen (Doc)
7676
import Universum hiding (on, show, state, when)
77+
import System.Process
78+
import System.Posix.Signals
7779

7880
class TestScript a where
7981
getScript :: a -> Script
@@ -97,7 +99,7 @@ data Script = Script
9799
, startupActions :: [ SlotTrigger ]
98100
} deriving (Show, Generic)
99101

100-
data SlotTrigger = SlotTrigger (Dict HasConfigurations -> Diffusion AuxxMode -> AuxxMode ())
102+
data SlotTrigger = SlotTrigger (Dict HasConfigurations -> Diffusion PocMode -> PocMode ())
101103

102104
instance Show SlotTrigger where
103105
show _ = "IO ()"
@@ -115,8 +117,6 @@ instance HasEpochSlots => TestScript (Example a) where
115117
instance TestScript Script where
116118
getScript = identity
117119

118-
data NodeHandle = NodeHandle (Async ())
119-
120120
data EpochSlots = EpochSlots { epochSlots :: SlotCount, config :: Config }
121121
type HasEpochSlots = Given EpochSlots
122122

@@ -196,34 +196,34 @@ runWithConfig ScriptRunnerOptions{srCommonNodeArgs,srPeers} inputParams genesisC
196196
thing3 :: (TestScript a, HasCompileInfo, HasConfigurations) => Config -> TxpConfiguration -> InputParams2 a -> NodeResources () -> IO ()
197197
thing3 genesisConfig txpConfig inputParams nr = do
198198
let
199-
toRealMode :: AuxxMode a -> RealMode EmptyMempoolExt a
199+
toRealMode :: PocMode a -> RealMode EmptyMempoolExt a
200200
toRealMode auxxAction = do
201201
realModeContext <- ask
202202
lift $ runReaderT auxxAction $ AuxxContext { acRealModeContext = realModeContext }
203203
thing2 :: Diffusion (RealMode ()) -> RealMode EmptyMempoolExt ()
204204
thing2 diffusion = toRealMode (thing5 (hoistDiffusion realModeToAuxx toRealMode diffusion))
205-
thing5 :: Diffusion AuxxMode -> AuxxMode ()
205+
thing5 :: Diffusion PocMode -> PocMode ()
206206
thing5 = runNode genesisConfig txpConfig nr thing4
207-
thing4 :: [ (Text, Diffusion AuxxMode -> AuxxMode ()) ]
207+
thing4 :: [ (Text, Diffusion PocMode -> PocMode ()) ]
208208
thing4 = workers genesisConfig inputParams
209209
runRealMode updateConfiguration genesisConfig txpConfig nr thing2
210210

211-
workers :: (HasConfigurations, TestScript a) => Genesis.Config -> InputParams2 a -> [ (Text, Diffusion AuxxMode -> AuxxMode ()) ]
211+
workers :: (HasConfigurations, TestScript a) => Genesis.Config -> InputParams2 a -> [ (Text, Diffusion PocMode -> PocMode ()) ]
212212
workers genesisConfig InputParams2{ip2EventChan,ip2Script,ip2ReplyChan} =
213213
[ ( T.pack "worker1", worker1 genesisConfig ip2Script ip2EventChan)
214214
, ( "worker2", worker2 ip2EventChan)
215215
, ( "brick reply worker", brickReplyWorker ip2ReplyChan)
216216
]
217217

218-
brickReplyWorker :: HasConfigurations => BChan Reply -> Diffusion AuxxMode -> AuxxMode ()
218+
brickReplyWorker :: HasConfigurations => BChan Reply -> Diffusion PocMode -> PocMode ()
219219
brickReplyWorker replyChan diffusion = do
220220
reply <- liftIO $ readBChan replyChan
221221
case reply of
222222
TriggerShutdown -> do
223223
triggerShutdown
224224
brickReplyWorker replyChan diffusion
225225

226-
worker2 :: HasConfigurations => BChan CustomEvent -> Diffusion AuxxMode -> AuxxMode ()
226+
worker2 :: HasConfigurations => BChan CustomEvent -> Diffusion PocMode -> PocMode ()
227227
worker2 eventChan diffusion = do
228228
localTip <- getTipHeader
229229
headerRef <- view (lensOf @LastKnownHeaderTag)
@@ -238,20 +238,20 @@ worker2 eventChan diffusion = do
238238
threadDelay 100000
239239
worker2 eventChan diffusion
240240

241-
worker1 :: (HasConfigurations, TestScript a) => Genesis.Config -> a -> BChan CustomEvent -> Diffusion (AuxxMode) -> AuxxMode ()
241+
worker1 :: (HasConfigurations, TestScript a) => Genesis.Config -> a -> BChan CustomEvent -> Diffusion (PocMode) -> PocMode ()
242242
worker1 genesisConfig script eventChan diffusion = do
243243
let
244-
handler :: SlotId -> AuxxMode ()
244+
handler :: SlotId -> PocMode ()
245245
handler slotid = do
246246
liftIO $ writeBChan eventChan $ CESlotStart $ SlotStart (getEpochIndex $ siEpoch slotid) (getSlotIndex $ siSlot slotid)
247247
case Map.lookup slotid (slotTriggers realScript) of
248248
Just (SlotTrigger act) -> runAction act
249249
Nothing -> pure ()
250250
pure ()
251251
realScript = getScript script
252-
errhandler :: Show e => e -> AuxxMode ()
252+
errhandler :: Show e => e -> PocMode ()
253253
errhandler e = print e
254-
runAction :: (Dict HasConfigurations -> Diffusion AuxxMode -> AuxxMode ()) -> AuxxMode ()
254+
runAction :: (Dict HasConfigurations -> Diffusion PocMode -> PocMode ()) -> PocMode ()
255255
runAction act = do
256256
act Dict diffusion `catch` errhandler @SomeException
257257
realWorker = do
@@ -314,7 +314,7 @@ getGenesisConfig = sbGenesisConfig <$> get
314314
data SlotCreationFailure = SlotCreationFailure { msg :: Text, slotsInEpoch :: SlotCount } deriving Show
315315
instance Exception SlotCreationFailure where
316316

317-
onStartup :: (Dict HasConfigurations -> Diffusion AuxxMode -> AuxxMode ()) -> Example ()
317+
onStartup :: (Dict HasConfigurations -> Diffusion PocMode -> PocMode ()) -> Example ()
318318
onStartup action = do
319319
oldsb <- get
320320
let
@@ -328,7 +328,7 @@ onStartup action = do
328328
put newsb
329329
pure ()
330330

331-
on :: (Word64, Word16) -> (Dict HasConfigurations -> Diffusion AuxxMode -> AuxxMode ()) -> Example ()
331+
on :: (Word64, Word16) -> (Dict HasConfigurations -> Diffusion PocMode -> PocMode ()) -> Example ()
332332
on (epoch, slot) action = do
333333
oldsb <- get
334334
let
@@ -349,7 +349,7 @@ on (epoch, slot) action = do
349349
}
350350
put newsb
351351

352-
doUpdate :: HasConfigurations => Diffusion AuxxMode -> Config -> Int -> BlockVersion -> SoftwareVersion -> BlockVersionModifier -> AuxxMode ()
352+
doUpdate :: HasConfigurations => Diffusion PocMode -> Config -> Int -> BlockVersion -> SoftwareVersion -> BlockVersionModifier -> PocMode ()
353353
doUpdate diffusion genesisConfig keyIndex blockVersion softwareVersion blockVersionModifier = do
354354
let
355355
--tag = SystemTag "win64"
@@ -379,12 +379,12 @@ doUpdate diffusion genesisConfig keyIndex blockVersion softwareVersion blockVers
379379
putText (sformat ("Update proposal submitted along with votes, upId: "%hashHexF%"\n") upid)
380380
print updateProposal
381381

382-
loadNKeys :: Integer -> AuxxMode ()
382+
loadNKeys :: Integer -> PocMode ()
383383
loadNKeys n = do
384384
let
385385
fmt :: Format r (Integer -> r)
386386
fmt = "../state-demo/generated-keys/rich/" % int % ".key"
387-
loadKey :: Integer -> AuxxMode ()
387+
loadKey :: Integer -> PocMode ()
388388
loadKey x = do
389389
secret <- readUserSecret (T.unpack $ sformat fmt x)
390390
let
@@ -394,14 +394,30 @@ loadNKeys n = do
394394
addSecretKey $ noPassEncrypt primSk
395395
mapM_ loadKey (range (0,n - 1))
396396

397+
data NodeHandle = NodeHandle (Async ()) ProcessHandle
398+
397399
startNode :: NodeType -> IO NodeHandle
398400
startNode (Core idx) = do
401+
stdout <- openFile ("poc-state/core-stdout-" <> show idx) WriteMode
399402
let
400-
_params = [ "--configuration-file", "../lib/configuration.yaml"
403+
params = [ "--configuration-file", "../lib/configuration.yaml"
401404
, "--system-start", "1543100429"
402405
, "--db-path", "poc-state/core" <> (show idx) <> "-db"
403406
, "--keyfile", "poc-state/secret" <> (show idx) <> ".key"
404407
]
408+
pc :: CreateProcess
409+
pc = (proc "cardano-node-simple" params) { std_out = UseHandle stdout }
410+
(stdin, stdout, stderr, ph) <- createProcess pc
405411
later <- async $ do
412+
waitForProcess ph
406413
pure ()
407-
pure $ NodeHandle later
414+
pure $ NodeHandle later ph
415+
416+
stopNode :: NodeHandle -> IO ()
417+
stopNode (NodeHandle async ph) = do
418+
maybePid <- getPid ph
419+
case maybePid of
420+
Just pid -> do
421+
signalProcess sigINT pid
422+
Nothing -> do
423+
logWarning "node already stopped when trying to stop it"

script-runner/BrickUI.hs

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ handleEvent state (VtyEvent (EvKey key [])) = do
4646
case key of
4747
KChar 'q' -> halt state
4848
_ -> continue $ state { asLastMsg = show key }
49+
4950
handleEvent state (AppEvent ae) = do
5051
case ae of
5152
CENodeInfo (NodeInfo{niLocalHeight,niGlobalHeight,niLocalEpochOrSlot}) -> do
@@ -56,5 +57,6 @@ handleEvent state (AppEvent ae) = do
5657
}
5758
QuitEvent -> halt state
5859
CESlotStart (SlotStart e s) -> continue $ state { asLastMsg = (show e) <> " " <> (show s) }
60+
5961
handleEvent state evt = do
6062
continue $ state { asLastMsg = show evt }

script-runner/Poc.hs

+20-9
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ import PocMode
1515
import Pos.Chain.Update (ApplicationName (ApplicationName),
1616
BlockVersion (BlockVersion),
1717
BlockVersionData (bvdMaxBlockSize, bvdMaxTxSize),
18-
BlockVersionModifier (bvmMaxTxSize),
18+
BlockVersionModifier,
1919
SoftwareVersion (SoftwareVersion))
2020
import Pos.DB.Class (gsAdoptedBVData)
2121
import Pos.Infra.Diffusion.Types (Diffusion)
2222
import Pos.Launcher (HasConfigurations)
2323
import Serokell.Data.Memory.Units (Byte)
2424
import Universum hiding (on)
25+
import Pos.Util.Wlog (logInfo)
2526

26-
printbvd :: Dict HasConfigurations -> Diffusion AuxxMode -> AuxxMode ()
27+
printbvd :: Dict HasConfigurations -> Diffusion PocMode -> PocMode ()
2728
printbvd Dict _diffusion = do
2829
let
2930
bvdfmt :: Format r (Byte -> Byte -> r)
@@ -39,23 +40,33 @@ test4 = do
3940
--on (82,19180) $ print "it is now epoch 0 slot 10"
4041
--on (82,19200) $ print "it is now epoch 0 slot 20"
4142
let
42-
proposal1 :: Dict HasConfigurations -> Diffusion AuxxMode -> AuxxMode ()
43+
proposal1 :: Dict HasConfigurations -> Diffusion PocMode -> PocMode ()
4344
proposal1 Dict diffusion = do
4445
let
4546
keyIndex :: Int
4647
keyIndex = 0
4748
blockVersion = BlockVersion 0 0 0
4849
softwareVersion = SoftwareVersion (ApplicationName "cardano-sl") 1
49-
blockVersionModifier = def { bvmMaxTxSize = Just 131072 }
50+
blockVersionModifier :: BlockVersionModifier
51+
blockVersionModifier = def -- { bvmMaxTxSize = Just 131072 }
5052
doUpdate diffusion genesisConfig keyIndex blockVersion softwareVersion blockVersionModifier
5153
print ("done?"::String)
5254
onStartup $ \Dict _diffusion -> loadNKeys 4
53-
on (1,0) printbvd
5455
on (1,10) proposal1
55-
on (2,0) printbvd
56-
on (3,0) printbvd
56+
forM_ (range (0,20)) $ \epoch -> on(epoch, 0) printbvd
5757

5858
main :: IO ()
5959
main = do
60-
_corenodes <- forM (range (0,3)) $ \node -> startNode (Core node)
61-
runScript $ return $ getScript test4
60+
let
61+
createNodes :: IO [NodeHandle]
62+
createNodes = do
63+
corenodes <- forM (range (0,3)) $ \node -> startNode (Core node)
64+
pure corenodes
65+
cleanupNodes :: [NodeHandle] -> IO ()
66+
cleanupNodes corenodes = do
67+
logInfo "stopping all nodes"
68+
mapM_ stopNode corenodes
69+
runScript' :: [NodeHandle] -> IO ()
70+
runScript' corenodes = do
71+
runScript $ return $ getScript test4
72+
bracket createNodes cleanupNodes runScript'

0 commit comments

Comments
 (0)