forked from Liqwid-Labs/plutip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
280 lines (249 loc) · 7.88 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NumericUnderscores #-}
module Main (main) where
import Cardano.Launcher.Node (CardanoNodeConn, nodeSocketFile)
import Cardano.Ledger.Slot (EpochSize (EpochSize))
import Control.Applicative (optional, (<**>), (<|>))
import Control.Monad (forM_, replicateM, void)
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Reader (MonadReader (ask), ReaderT (ReaderT), lift)
import Data.Aeson (FromJSON, ToJSON, encodeFile)
import Data.Default (def)
import Data.Time (NominalDiffTime)
import GHC.Conc (TVar, threadDelay)
import GHC.Generics (Generic)
import GHC.Natural (Natural)
import GHC.Word (Word64)
import Numeric.Positive (Positive)
import Options.Applicative (Parser, helper, info)
import Options.Applicative qualified as Options
import System.Posix (Handler (CatchOnce), installHandler, sigINT)
import Test.Plutip.Config (
ChainIndexMode (CustomPort, DefaultPort, NotNeeded),
PlutipConfig (chainIndexMode, clusterWorkingDir, extraConfig),
WorkingDirectory (Fixed, Temporary),
)
import Test.Plutip.Internal.BotPlutusInterface.Wallet (
BpiWallet,
addSomeWalletDir,
cardanoMainnetAddress,
mkMainnetAddress,
walletPkh,
)
import Test.Plutip.Internal.Cluster.Extra.Types (
ExtraConfig (ExtraConfig),
)
import Test.Plutip.Internal.LocalCluster (
ClusterStatus,
startCluster,
stopCluster,
)
import Test.Plutip.Internal.Types (nodeSocket)
import Test.Plutip.Tools.CardanoApi (awaitAddressFunded)
main :: IO ()
main = do
config <- Options.execParser (info (pClusterConfig <**> helper) mempty)
case totalAmount config of
Left e -> error e
Right amt -> do
let ClusterConfig {numWallets, dirWallets, numUtxos, workDir, slotLength, epochSize, cIndexMode} = config
workingDir = maybe Temporary (`Fixed` False) workDir
extraConf = ExtraConfig slotLength epochSize
plutipConfig = def {clusterWorkingDir = workingDir, extraConfig = extraConf, chainIndexMode = cIndexMode}
putStrLn "Starting cluster..."
(st, _) <- startCluster plutipConfig $ do
ws <- initWallets numWallets numUtxos amt dirWallets
liftIO $ putStrLn "Waiting for wallets to be funded..."
awaitFunds ws slotLength
separate
liftIO $ forM_ (zip ws [(1 :: Int) ..]) printWallet
printNodeRelatedInfo
separate
forM_ (dumpInfo config) $ \dInfo -> do
cEnv <- ask
lift $
dumpClusterInfo
dInfo
(nodeSocket cEnv)
ws
void $ installHandler sigINT (termHandler st) Nothing
putStrLn "Cluster is running. Ctrl-C to stop."
loopThreadDelay
where
loopThreadDelay = threadDelay 100000000 >> loopThreadDelay
printNodeRelatedInfo = ReaderT $ \cEnv -> do
putStrLn $ "Node socket: " <> show (nodeSocket cEnv)
separate = liftIO $ putStrLn "\n------------\n"
totalAmount :: ClusterConfig -> Either String Positive
totalAmount cwc =
case toAda (adaAmount cwc) + lvlAmount cwc of
0 -> Left "One of --ada or --lovelace arguments should not be 0"
amt -> Right $ fromInteger . toInteger $ amt
initWallets numWallets numUtxos amt dirWallets = do
let collateralAmount = 10_000_000
replicateM (max 0 numWallets) $
addSomeWalletDir
(collateralAmount : replicate numUtxos amt)
dirWallets
dumpClusterInfo :: FilePath -> CardanoNodeConn -> [BpiWallet] -> IO ()
dumpClusterInfo fp nodeConn ws = do
encodeFile
fp
( ClusterInfo
{ ciWallets = [(show . walletPkh $ w, show . mkMainnetAddress $ w) | w <- ws]
, ciNodeSocket = nodeSocketFile nodeConn
}
)
printWallet (w, n) = do
putStrLn $ "Wallet " ++ show n ++ " PKH: " ++ show (walletPkh w)
putStrLn $ "Wallet " ++ show n ++ " mainnet address: " ++ show (mkMainnetAddress w)
toAda = (* 1_000_000)
-- waits for the last wallet to be funded
awaitFunds ws delay = do
let lastWallet = last ws
liftIO $ putStrLn "Waiting till all wallets will be funded..."
awaitAddressFunded (cardanoMainnetAddress lastWallet) delay
termHandler :: TVar (ClusterStatus ()) -> System.Posix.Handler
termHandler st = CatchOnce $ do
putStrLn "Caught SIGTERM, stopping cluster"
stopCluster st
data ClusterInfo = ClusterInfo
{ ciWallets :: [(String, String)]
, ciNodeSocket :: String
}
deriving (Show, Generic, ToJSON, FromJSON)
pnumWallets :: Parser Int
pnumWallets =
Options.option
Options.auto
( Options.long "num-wallets"
<> Options.long "wallets"
<> Options.short 'n'
<> Options.metavar "NUM_WALLETS"
<> Options.value 1
)
pdirWallets :: Parser (Maybe FilePath)
pdirWallets =
optional $
Options.strOption
( Options.long "wallets-dir"
<> Options.long "wallet-dir"
<> Options.short 'd'
<> Options.metavar "FILEPATH"
)
padaAmount :: Parser Natural
padaAmount =
Options.option
Options.auto
( Options.long "ada"
<> Options.short 'a'
<> Options.metavar "ADA"
<> Options.value 10_000
)
plvlAmount :: Parser Natural
plvlAmount =
Options.option
Options.auto
( Options.long "lovelace"
<> Options.short 'l'
<> Options.metavar "Lovelace"
<> Options.value 0
)
pnumUtxos :: Parser Int
pnumUtxos =
Options.option
Options.auto
( Options.long "utxos"
<> Options.short 'u'
<> Options.metavar "NUM_UTXOS"
<> Options.value 1
)
pWorkDir :: Parser (Maybe FilePath)
pWorkDir =
optional $
Options.strOption
( Options.long "working-dir"
<> Options.short 'w'
<> Options.metavar "FILEPATH"
)
pSlotLen :: Parser NominalDiffTime
pSlotLen =
Options.option
Options.auto
( Options.long "slot-len"
<> Options.short 's'
<> Options.metavar "SLOT_LEN"
<> Options.value 0.2
)
pEpochSize :: Parser EpochSize
pEpochSize =
EpochSize <$> wordParser
where
wordParser :: Parser Word64
wordParser =
Options.option
Options.auto
( Options.long "epoch-size"
<> Options.short 'e'
<> Options.metavar "EPOCH_SIZE"
<> Options.value 160
)
pChainIndexMode :: Parser ChainIndexMode
pChainIndexMode =
noIndex <|> withIndexPort <|> pure DefaultPort
where
noIndex =
Options.flag'
NotNeeded
( Options.long "no-index"
<> Options.help "Start cluster with chain-index on default port"
)
withIndexPort = CustomPort <$> portParser
portParser =
Options.option
Options.auto
( Options.long "chain-index-port"
<> Options.metavar "PORT"
<> Options.help "Start cluster with chain-index on custom port"
)
pInfoJson :: Parser (Maybe FilePath)
pInfoJson =
optional $
Options.strOption
( Options.long "dump-info-json"
<> Options.metavar "FILEPATH"
<> Options.help "After starting the cluster, add some useful runtime information to a JSON file (wallets, node socket path etc)"
<> Options.value "local-cluster-info.json"
)
pClusterConfig :: Parser ClusterConfig
pClusterConfig =
ClusterConfig
<$> pnumWallets
<*> pdirWallets
<*> padaAmount
<*> plvlAmount
<*> pnumUtxos
<*> pWorkDir
<*> pSlotLen
<*> pEpochSize
<*> pChainIndexMode
<*> pInfoJson
-- | Basic info about the cluster, to
-- be used by the command-line
data ClusterConfig = ClusterConfig
{ numWallets :: Int
, dirWallets :: Maybe FilePath
, adaAmount :: Natural
, lvlAmount :: Natural
, numUtxos :: Int
, workDir :: Maybe FilePath
, slotLength :: NominalDiffTime
, epochSize :: EpochSize
, cIndexMode :: ChainIndexMode
, dumpInfo :: Maybe FilePath
}
deriving stock (Show, Eq)