-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathTestnet.hs
More file actions
506 lines (465 loc) · 17.4 KB
/
Testnet.hs
File metadata and controls
506 lines (465 loc) · 17.4 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE RecordWildCards #-}
{-# OPTIONS_GHC -Wno-incomplete-patterns #-}
{-# OPTIONS_GHC -Wno-incomplete-record-updates #-}
{-# OPTIONS_GHC -Wno-name-shadowing #-}
{-# OPTIONS_GHC -Wno-unused-do-bind #-}
{-# OPTIONS_GHC -Wno-unused-imports #-}
module Helpers.Testnet where
import Cardano.Api (ConwayEraOnwards)
import Cardano.Api qualified as C
import Cardano.Api.Ledger (
ConwayGenesis (cgUpgradePParams),
Credential (KeyHashObj),
EraCrypto,
KeyHash,
KeyRole (StakePool),
StandardCrypto,
UpgradeConwayPParams (ucppPlutusV3CostModel),
Voter (StakePoolVoter),
)
import Cardano.Api.Shelley qualified as C
import Cardano.Ledger.Alonzo.Genesis (AlonzoGenesis)
import Cardano.Ledger.Conway.Genesis (ConwayGenesis)
import Cardano.Ledger.Plutus (Language (PlutusV3), costModelFromMap, mkCostModel)
import Cardano.Testnet (Conf (tempAbsPath))
import Cardano.Testnet qualified as CTN
import Control.Lens ((&), (.~))
import Control.Monad (forM, forM_)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.Default.Class (def)
import Data.Maybe (fromJust)
import Data.Time.Clock.POSIX qualified as Time
import Hedgehog (MonadTest)
import Hedgehog qualified as H
import Hedgehog.Extras.Stock (waitSecondsForProcess)
import Hedgehog.Extras.Stock.IO.Network.Sprocket qualified as IO
import Hedgehog.Extras.Test qualified as H
import Hedgehog.Extras.Test qualified as HE
import Hedgehog.Extras.Test.Base qualified as H
import Helpers.Common (
makeAddress,
makeAddressWithStake,
toConwayEraOnwards,
toMaryEraOnwards,
toShelleyBasedEra,
)
import Helpers.Error (TimedOut (ProcessExitTimedOut))
import Helpers.Query qualified as Q
import Helpers.Utils (maybeReadAs)
import Helpers.Utils qualified as U
import Prettyprinter (Doc)
import System.Directory qualified as IO
import System.FilePath ((</>))
import System.Posix.Signals (sigKILL, signalProcess)
import System.Process (cleanupProcess)
import System.Process.Internals (
PHANDLE,
ProcessHandle__ (ClosedHandle, OpenExtHandle, OpenHandle),
withProcessHandle,
)
import Testnet.Defaults (defaultAlonzoGenesis)
import Testnet.Defaults qualified as CTN
import Testnet.Runtime qualified as CTN
import Testnet.Start.Types qualified as CTN
import Testnet.Types (
PoolNode (..),
TestnetRuntime,
poolNodes,
poolSprockets,
testnetMagic,
)
data TestEnvironmentOptions era
= TestnetOptions
{ testnetEra :: C.CardanoEra era
, testnetProtocolVersion :: Int
, testnetCardanoOptions :: CTN.CardanoTestnetOptions
, testnetShelleyOptions :: CTN.ShelleyTestnetOptions
}
| LocalNodeOptions
{ localNodeEra :: C.CardanoEra era
, localNodeProtocolVersion :: Int
, localNodeEnvDir :: FilePath
-- ^ path to directory containing 'utxo-keys' and 'ipc' directories
, localNodeTestnetMagic :: Int
}
deriving (Show)
defAlonzoTestnetOptions :: TestEnvironmentOptions C.AlonzoEra
defAlonzoTestnetOptions =
TestnetOptions
{ testnetEra = C.AlonzoEra
, testnetProtocolVersion = 6
, testnetShelleyOptions =
def
{ CTN.shelleyActiveSlotsCoeff = 0.1
, CTN.shelleySlotLength = 0.1
, CTN.shelleyEpochLength = 10_000
-- ↑ higher value so that txs can have higher
-- upper bound validity range
}
, testnetCardanoOptions =
def{CTN.cardanoNodeEra = C.AnyShelleyBasedEra C.ShelleyBasedEraAlonzo}
}
defBabbageTestnetOptions :: Int -> TestEnvironmentOptions C.BabbageEra
defBabbageTestnetOptions protocolVersion =
TestnetOptions
{ testnetEra = C.BabbageEra
, testnetProtocolVersion = protocolVersion
, testnetShelleyOptions =
def
{ CTN.shelleyActiveSlotsCoeff = 0.1
, CTN.shelleySlotLength = 0.1
, CTN.shelleyEpochLength = 10_000
-- ↑ higher value so that txs can have higher
-- upper bound validity range
}
, testnetCardanoOptions =
def{CTN.cardanoNodeEra = C.AnyShelleyBasedEra C.ShelleyBasedEraBabbage}
}
defConwayTestnetOptions :: Int -> TestEnvironmentOptions C.ConwayEra
defConwayTestnetOptions protocolVersion =
TestnetOptions
{ testnetEra = C.ConwayEra
, testnetProtocolVersion = protocolVersion
, testnetShelleyOptions =
def
{ CTN.shelleyActiveSlotsCoeff = 0.1
, CTN.shelleySlotLength = 0.1
, CTN.shelleyEpochLength = 10_000
-- ↑ higher value so that txs can have higher
-- upper bound validity range
}
, testnetCardanoOptions =
def{CTN.cardanoNodeEra = C.AnyShelleyBasedEra C.ShelleyBasedEraConway}
}
shortEpochConwayTestnetOptions :: Int -> TestEnvironmentOptions C.ConwayEra
shortEpochConwayTestnetOptions protocolVersion =
(defConwayTestnetOptions protocolVersion)
{ testnetCardanoOptions =
testnetCardanoOptions (defConwayTestnetOptions protocolVersion)
, testnetShelleyOptions =
def
{ CTN.shelleyActiveSlotsCoeff = 0.1
, -- ↑ adjusted from default due to short epoch length
-- 200 second epoch for testing outcome of governance actions
-- (shorter is unstable)
CTN.shelleyEpochLength = 2_000
, CTN.shelleySlotLength = 0.1
}
}
localNodeOptionsPreview :: TestEnvironmentOptions C.BabbageEra
localNodeOptionsPreview =
LocalNodeOptions
{ localNodeEra = C.BabbageEra
, localNodeProtocolVersion = 8
, localNodeEnvDir = "/tmp/preview"
, localNodeTestnetMagic = 2
}
testnetOptionsAlonzo6 :: TestEnvironmentOptions C.AlonzoEra
testnetOptionsAlonzo6 = defAlonzoTestnetOptions
testnetOptionsBabbage7 :: TestEnvironmentOptions C.BabbageEra
testnetOptionsBabbage7 = defBabbageTestnetOptions 7
testnetOptionsBabbage8 :: TestEnvironmentOptions C.BabbageEra
testnetOptionsBabbage8 = defBabbageTestnetOptions 8
testnetOptionsConway9 :: TestEnvironmentOptions C.ConwayEra
testnetOptionsConway9 = defConwayTestnetOptions 9
testnetOptionsConway10 :: TestEnvironmentOptions C.ConwayEra
testnetOptionsConway10 = defConwayTestnetOptions 10
testnetOptionsConway9Governance :: TestEnvironmentOptions C.ConwayEra
testnetOptionsConway9Governance = shortEpochConwayTestnetOptions 9
eraFromOptions :: TestEnvironmentOptions era -> C.CardanoEra era
eraFromOptions options = case options of
TestnetOptions{testnetEra} -> testnetEra
LocalNodeOptions{localNodeEra} -> localNodeEra
eraFromOptionsM
:: (MonadTest m)
=> TestEnvironmentOptions era
-> m (C.CardanoEra era)
eraFromOptionsM = pure . eraFromOptions
pvFromOptions :: TestEnvironmentOptions era -> Int
pvFromOptions TestnetOptions{testnetProtocolVersion} =
testnetProtocolVersion
pvFromOptions LocalNodeOptions{localNodeProtocolVersion} =
localNodeProtocolVersion
-- | Get path to where cardano-testnet files are
getProjectBase :: (MonadIO m, MonadTest m) => m String
getProjectBase = liftIO . IO.canonicalizePath =<< HE.getProjectBase
{- | Start a testnet with provided testnet options
(including era and protocol version)
-}
startTestnet
:: forall era
. TestEnvironmentOptions era
-> FilePath
-> H.Integration
( C.LocalNodeConnectInfo
, C.LedgerProtocolParameters era
, C.NetworkId
, Maybe [PoolNode]
)
startTestnet LocalNodeOptions{} _ = error "LocalNodeOptions not supported"
startTestnet TestnetOptions{..} tempAbsBasePath = do
conf :: CTN.Conf <- HE.noteShowM $ CTN.mkConf tempAbsBasePath
currentTime <- liftIO Time.getCurrentTime
anySbe@(C.AnyShelleyBasedEra sbe) <-
pure $ CTN.cardanoNodeEra testnetCardanoOptions
let
maxSupply = CTN.cardanoMaxSupply testnetCardanoOptions
genesisShelley =
CTN.defaultShelleyGenesis
anySbe
currentTime
maxSupply
testnetShelleyOptions
genesisAlonzo <- CTN.getDefaultAlonzoGenesis sbe
-- PlutusV3 cost model should have as many cost values as there are
-- different cost model parameters in the given Plutus version,
-- or else missing parameter values are set to `maxBound`.
-- Unfortunately at the moment Cardano Testnet uses `testingCostModelV3`
-- function from Ledger that generates a cost model with less values
-- than needed: 231 instead of 251.
costModelV3 <-
mkCostModel PlutusV3 (replicate 251 0)
& either (fail . show) pure
tn <-
CTN.cardanoTestnet
testnetCardanoOptions
conf
genesisShelley
genesisAlonzo
CTN.defaultConwayGenesis
{ cgUpgradePParams =
(cgUpgradePParams CTN.defaultConwayGenesis)
{ ucppPlutusV3CostModel = costModelV3
}
}
-- needed to avoid duplication of directory in filepath
let tmpAbsBasePath' = CTN.makeTmpBaseAbsPath $ CTN.tempAbsPath conf
-- Boilerplate codecs used for protocol serialisation. The number of
-- epochSlots is specific to each blockchain instance. This value is used by
-- cardano mainnet/testnet and only applies to the Byron era.
socketPathAbs <- getPoolSocketPathAbs tmpAbsBasePath' tn
let epochSlots = C.EpochSlots 21_600
localNodeConnectInfo =
C.LocalNodeConnectInfo
{ C.localConsensusModeParams = C.CardanoModeParams epochSlots
, C.localNodeNetworkId = getNetworkId tn
, C.localNodeSocketPath = socketPathAbs
}
networkId = getNetworkId tn
pparams <- Q.getProtocolParams testnetEra localNodeConnectInfo
pure (localNodeConnectInfo, pparams, networkId, Just $ poolNodes tn)
cleanupTestnet :: (MonadIO m) => Maybe [PoolNode] -> m [Either TimedOut ()]
cleanupTestnet mPoolNodes =
case mPoolNodes of
Just poolNodes -> do
forM_ poolNodes $ \(PoolNode poolRuntime _) -> do
-- graceful SIGTERM all nodes
liftIO $
cleanupProcess
( Just (CTN.nodeStdinHandle poolRuntime)
, Nothing
, Nothing
, CTN.nodeProcessHandle poolRuntime
)
forM poolNodes $ \node ->
-- kill signal for any node unix handles still open
killUnixHandle $ CTN.nodeProcessHandle $ poolRuntime node
_ ->
pure []
where
killUnixHandle ph = liftIO $ withProcessHandle ph $ \case
OpenHandle pid -> do
signalProcess sigKILL pid -- send kill signal if handle still open
eTimeOut <- waitSecondsForProcess 60 ph -- wait 60s for process to exit
case eTimeOut of
Left _ -> pure $ Left $ ProcessExitTimedOut 60 pid
Right _ -> pure $ Right ()
OpenExtHandle _ _ -> pure $ Right () -- do nothing on Windows
ClosedHandle _ -> pure $ Right () -- do nothing if already closed
connectToLocalNode
:: TestEnvironmentOptions era
-> FilePath
-> H.Integration
( C.LocalNodeConnectInfo
, C.LedgerProtocolParameters era
, C.NetworkId
, Maybe [PoolNode]
)
connectToLocalNode TestnetOptions{} _ = error "TestnetOptions not supported"
connectToLocalNode LocalNodeOptions{..} tempAbsPath = do
HE.createDirectoryIfMissing (tempAbsPath </> "utxo-keys")
HE.createDirectoryIfMissing (tempAbsPath </> "sockets")
HE.createFileLink
(localNodeEnvDir </> "test.skey")
(tempAbsPath </> "utxo-keys/utxo1.skey")
HE.createFileLink
(localNodeEnvDir </> "test.vkey")
(tempAbsPath </> "utxo-keys/utxo1.vkey")
HE.createFileLink
(localNodeEnvDir </> "ipc/node.socket")
(tempAbsPath </> "sockets/node.socket")
let socketPathAbs = C.File $ tempAbsPath </> "sockets/node.socket"
networkId =
C.Testnet . C.NetworkMagic $ fromIntegral localNodeTestnetMagic
-- Boilerplate codecs used for protocol serialisation. The number of
-- epochSlots is specific to each blockchain instance. This value is used by
-- cardano mainnet/testnet and only applies to the Byron era.
let epochSlots = C.EpochSlots 21_600
localNodeConnectInfo =
C.LocalNodeConnectInfo
{ C.localConsensusModeParams = C.CardanoModeParams epochSlots
, C.localNodeNetworkId = networkId
, C.localNodeSocketPath = socketPathAbs
}
pparams <- Q.getProtocolParams localNodeEra localNodeConnectInfo
pure (localNodeConnectInfo, pparams, networkId, Nothing)
{- | Start testnet with cardano-testnet or use local node that's already
connected to a public testnet
-}
setupTestEnvironment
:: TestEnvironmentOptions era
-> FilePath
-> H.Integration
( C.LocalNodeConnectInfo
, C.LedgerProtocolParameters era
, C.NetworkId
, Maybe [PoolNode]
)
setupTestEnvironment testnetOptions@TestnetOptions{..} tempAbsPath = do
liftIO $
putStrLn $
"\nStarting local testnet in "
++ show testnetEra
++ " PV"
++ show testnetProtocolVersion
++ "..."
startTestnet testnetOptions tempAbsPath
setupTestEnvironment localNodeOptions@LocalNodeOptions{} tempAbsPath = do
liftIO $ putStrLn "\nConnecting to local node..."
connectToLocalNode localNodeOptions tempAbsPath
-- | Network ID of the testnet
getNetworkId :: TestnetRuntime -> C.NetworkId
getNetworkId tn = C.Testnet $ C.NetworkMagic $ fromIntegral (testnetMagic tn)
-- | Path to a pool node's unix socket
getPoolSocketPathAbs
:: (MonadTest m, MonadIO m)
=> FilePath
-> TestnetRuntime
-> m C.SocketPath
getPoolSocketPathAbs tempAbsPath tn = do
socketPath <- IO.sprocketArgumentName <$> H.headM (poolSprockets tn)
fp <- liftIO $ IO.canonicalizePath $ tempAbsPath </> socketPath
H.annotate fp
pure $ C.File fp
{- | Signing key and address for wallet 1
Handles two key types: GenesisUTxOKey and PaymentKey
-}
w1All
:: (MonadIO m, MonadTest m)
=> FilePath
-> C.NetworkId
-> m
( C.SigningKey C.PaymentKey
, C.VerificationKey C.PaymentKey
, C.Address C.ShelleyAddr
)
w1All tempAbsPath networkId = do
let w1VKeyFile = C.File $ tempAbsPath </> "utxo-keys/utxo1/utxo.vkey"
w1SKeyFile = C.File $ tempAbsPath </> "utxo-keys/utxo1/utxo.skey"
-- GenesisUTxOKey comes from cardano-testnet
mGenesisVKey :: Maybe (C.VerificationKey C.GenesisUTxOKey) <-
maybeReadAs (C.AsVerificationKey C.AsGenesisUTxOKey) w1VKeyFile
mGenesisSKey :: Maybe (C.SigningKey C.GenesisUTxOKey) <-
maybeReadAs (C.AsSigningKey C.AsGenesisUTxOKey) w1SKeyFile
-- PaymentKey comes from cardano-cli
-- (the likely type for a locally created wallet)
mPaymentVKey :: Maybe (C.VerificationKey C.PaymentKey) <-
maybeReadAs (C.AsVerificationKey C.AsPaymentKey) w1VKeyFile
mPaymentSKey :: Maybe (C.SigningKey C.PaymentKey) <-
maybeReadAs (C.AsSigningKey C.AsPaymentKey) w1SKeyFile
let
vKey :: C.VerificationKey C.PaymentKey =
maybe (fromJust mPaymentVKey) C.castVerificationKey mGenesisVKey
sKey :: C.SigningKey C.PaymentKey =
maybe (fromJust mPaymentSKey) C.castSigningKey mGenesisSKey
address = makeAddress (Left vKey) networkId
pure (sKey, vKey, address)
w1
:: (MonadIO m, MonadTest m)
=> -- => Either (LocalNodeOptions era) (TestnetOptions era)
FilePath
-> C.NetworkId
-> m (C.SigningKey C.PaymentKey, C.Address C.ShelleyAddr)
w1 tempAbsPath networkId =
(\(sKey, _, address) -> (sKey, address)) <$> w1All tempAbsPath networkId
data TestnetStakePool = TestnetStakePool
{ stakePoolSKey :: C.SigningKey C.StakePoolKey
, stakePoolVKey :: C.VerificationKey C.StakePoolKey
, stakePoolVKeyHash :: C.Hash C.StakePoolKey
, stakePoolKeyHash :: C.Hash C.StakeKey
, stakePoolVrfHash :: C.Hash C.VrfKey
, stakePoolPoolKeyHash :: KeyHash 'StakePool StandardCrypto
}
deriving (Show)
pool1All
:: (MonadIO m, MonadTest m)
=> FilePath
-> m TestnetStakePool
pool1All tempAbsPath = do
let pool1SKeyFile = C.File $ tempAbsPath </> "pools/cold1.skey"
mPool1SKey :: Maybe (C.SigningKey C.StakePoolKey) <-
maybeReadAs (C.AsSigningKey C.AsStakePoolKey) pool1SKeyFile
let pool1SKey = fromJust mPool1SKey
let pool1VerificationKeyFile = C.File $ tempAbsPath </> "pools/cold1.vkey"
mPool1VKey :: Maybe (C.VerificationKey C.StakePoolKey) <-
maybeReadAs (C.AsVerificationKey C.AsStakePoolKey) pool1VerificationKeyFile
let pool1VKey = fromJust mPool1VKey
pool1VKeyHash = C.verificationKeyHash pool1VKey
C.StakePoolKeyHash pool1StakePoolKeyHash = pool1VKeyHash
let pool1StakingRewardsFile =
C.File $ tempAbsPath </> "pools/staking-reward1.vkey"
mPool1StakingRewards :: Maybe (C.VerificationKey C.StakeKey) <-
maybeReadAs (C.AsVerificationKey C.AsStakeKey) pool1StakingRewardsFile
let pool1StakeKeyHash = C.verificationKeyHash (fromJust mPool1StakingRewards)
let pool1VrfKeyFile = C.File $ tempAbsPath </> "pools/vrf1.vkey"
mPool1VrfKey :: Maybe (C.VerificationKey C.VrfKey) <-
maybeReadAs (C.AsVerificationKey C.AsVrfKey) pool1VrfKeyFile
let pool1VrfKeyHash = C.verificationKeyHash (fromJust mPool1VrfKey)
pure $
TestnetStakePool
pool1SKey
pool1VKey
pool1VKeyHash
pool1StakeKeyHash
pool1VrfKeyHash
pool1StakePoolKeyHash
pool1UnregCert
:: (MonadIO m, MonadTest m)
=> ConwayEraOnwards era
-> C.EpochNo
-> FilePath
-> m (C.Certificate era)
pool1UnregCert ceo epochNo tempAbsPath = do
pool1Data <- pool1All tempAbsPath
let retireReqs =
C.StakePoolRetirementRequirementsConwayOnwards
ceo
(stakePoolVKeyHash pool1Data)
epochNo
pure $ C.makeStakePoolRetirementCertificate retireReqs
pool1Voter
:: (MonadIO m, MonadTest m)
=> C.ConwayEraOnwards era
-> FilePath
-> m (Voter (EraCrypto (C.ShelleyLedgerEra era)))
pool1Voter ceo tempAbsPath =
pure
. StakePoolVoter
. C.conwayEraOnwardsConstraints ceo
. stakePoolPoolKeyHash
=<< pool1All tempAbsPath