-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.hs
More file actions
365 lines (331 loc) · 11.4 KB
/
Copy pathMain.hs
File metadata and controls
365 lines (331 loc) · 11.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
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
module Main (main) where
-------------------------------------------------------------------------------
-- Imports
-------------------------------------------------------------------------------
import Cardano.Api (IsPlutusScriptLanguage)
import Control.Monad (when)
import Data.Function ((&))
import Data.String (IsString (..))
import Data.Time.Clock.POSIX (getPOSIXTime)
import Export (writePlutusScript)
import Onchain.V2.Escrow (EscrowParams (..))
import Onchain.V2.Simple (CompiledCodeLang)
import Options.Applicative hiding (str)
import Populate
import Streamly.Console.Stdio qualified as Console
import Streamly.FileSystem.FileIO qualified as File
import Streamly.FileSystem.Path qualified as Path
import Streamly.System.Command qualified as Cmd
import Streamly.Unicode.String (str)
import System.Environment (setEnv)
import System.FilePath ((</>))
import System.IO (BufferMode (..), hSetBuffering, stderr, stdout)
import Onchain.V2.Debug qualified as DebugV2
import Onchain.V2.Release qualified as ReleaseV2
import Onchain.V3.Debug qualified as DebugV3
import Onchain.V3.Release qualified as ReleaseV3
-------------------------------------------------------------------------------
-- CLI
-------------------------------------------------------------------------------
data PopulateCommand
= PCTriggerTest
| PCEscrow
| PCFanout
data Command
= StartLocalTestnet
| Clean
| Populate PopulateCommand
| TriggerRollback
| Setup
| SyncNode2
populateCommandParser :: Parser PopulateCommand
populateCommandParser =
hsubparser
( command
"trigger-test"
( info
(pure PCTriggerTest)
(progDesc "Run the trigger test")
)
<> command
"escrow"
( info
(pure PCEscrow)
(progDesc "Run the escrow scenario")
)
<> command
"fanout"
( info
(pure PCFanout)
(progDesc "Run the fanout scenario")
)
)
commandParser :: Parser Command
commandParser =
hsubparser
( command
"start-local-testnet"
( info
(pure StartLocalTestnet)
(progDesc "Start the local development network")
)
<> command
"clean"
( info
(pure Clean)
(progDesc "Clean all local state")
)
<> command
"populate"
( info
(Populate <$> populateCommandParser)
(progDesc "Populate the local network with initial data")
)
<> command
"setup"
( info
(pure Setup)
(progDesc "Setup the initial config files")
)
<> command
"trigger-rollback"
( info
(pure TriggerRollback)
(progDesc "Trigger rollback")
)
<> command
"sync-node2"
( info
(pure SyncNode2)
(progDesc "Sync Node 2")
)
)
opts :: ParserInfo Command
opts =
info
(commandParser <**> helper)
( fullDesc
<> progDesc "Local development management tool"
)
--------------------------------------------------------------------------------
-- Create config
--------------------------------------------------------------------------------
-- TODO: Abstract escrow into v2 and v3.
tracingYamlContents :: String -> IO String
tracingYamlContents scriptsDirName = do
let scriptsDirPath = env_LOCAL_CONFIG_DIR </> scriptsDirName
policyPolicyId <- getPolicyId $ scriptsDirPath </> "policy.plutus"
validatorPolicyId <- getPolicyId $ scriptsDirPath </> "validator.plutus"
policyDebugPolicyId <- getPolicyId $ scriptsDirPath </> "policy-debug.plutus"
validatorDebugPolicyId <- getPolicyId $ scriptsDirPath </> "validator-debug.plutus"
-- NOTE: This seems hacky but I can't think of a clean way to abstract
-- common functionality.
let validatorPrefix =
if policyPolicyId == validatorPolicyId
then ""
else
[str|
- hash: "#{validatorPolicyId}"
shadows:
|]
pure
[str|
- hash: "#{policyPolicyId}"
shadows:
- name: "Local Policy"
hash: "#{policyDebugPolicyId}"
source:
file_path: "#{scriptsDirName}/policy-debug.plutus"
#{validatorPrefix}
- name: "Local Validator"
hash: "#{validatorDebugPolicyId}"
source:
file_path: "#{scriptsDirName}/validator-debug.plutus"
- name: "Always failing script"
hash: "793f8c8cffba081b2a56462fc219cc8fe652d6a338b62c7b134876e7"
source:
inline:
type: "PlutusScriptV2"
description: "Always failing script"
cborHex: "4e4d01000033222220051200120011"
|]
escrowYamlContents :: IO String
escrowYamlContents = do
escrowPolicyId <- getPolicyId $ env_LOCAL_CONFIG_DIR </> "escrow.plutus"
escrowDebugPolicyId <- getPolicyId $ env_LOCAL_CONFIG_DIR </> "escrow-debug.plutus"
pure
[str|
- hash: "#{escrowPolicyId}"
shadows:
- name: "Escrow"
hash: "#{escrowDebugPolicyId}"
source:
file_path: "./escrow-debug.plutus"
|]
createScriptsYaml :: IO ()
createScriptsYaml = do
tracingV2 <- tracingYamlContents "tracing-plutus-v2"
tracingV3 <- tracingYamlContents "tracing-plutus-v3"
escrowV2 <- escrowYamlContents
writeFile
(env_LOCAL_CONFIG_DIR </> "scripts.yaml")
[str|
running_mode: sync_initial_local_state
confirmation_depth: 100
scripts:
#{tracingV2}
#{tracingV3}
#{escrowV2}
|]
createTracingConfig ::
(IsPlutusScriptLanguage lang) =>
String ->
CompiledCodeLang lang pol ->
CompiledCodeLang lang val ->
CompiledCodeLang lang pol ->
CompiledCodeLang lang val ->
IO ()
createTracingConfig scriptsDirName relP relV debP debV = do
runCmd_ [str|mkdir -p #{env_LOCAL_CONFIG_DIR}/#{scriptsDirName}|]
let scriptsDirPath = env_LOCAL_CONFIG_DIR </> scriptsDirName
writePlutusScript (scriptsDirPath </> "policy.plutus") relP
writePlutusScript (scriptsDirPath </> "validator.plutus") relV
writePlutusScript (scriptsDirPath </> "policy-debug.plutus") debP
writePlutusScript (scriptsDirPath </> "validator-debug.plutus") debV
buildStakeAddress
[ opt "stake-script-file" (scriptsDirPath </> "policy.plutus")
, opt "out-file" (scriptsDirPath </> "script.stake.addr")
]
-- 400_000 here comes from:
-- cardano-cli conway query protocol-parameters
-- \ --testnet-magic 42
-- \ --socket-path "devnet-env/socket/node1/sock"
-- \ | jq .stakeAddressDeposit
genRegCertStakeAddress
[ opt "stake-script-file" (scriptsDirPath </> "policy.plutus")
, opt "out-file" (scriptsDirPath </> "registration.cert")
, opt "key-reg-deposit-amt" (400_000 :: Int)
]
genDeregCertStakeAddress
[ opt "stake-script-file" (scriptsDirPath </> "policy.plutus")
, opt "out-file" (scriptsDirPath </> "deregistration.cert")
, opt "key-reg-deposit-amt" (400_000 :: Int)
]
createEscrowConfig :: IO ()
createEscrowConfig = do
-- Config for Escrow
alice <- mkWallet env_LOCAL_CONFIG_DIR "alice"
bob <- mkWallet env_LOCAL_CONFIG_DIR "bob"
alicePkh <- fromString <$> walletKeyHash alice
bobPkh <- fromString <$> walletKeyHash bob
now <- getPOSIXTime
let deadline = floor $ (now + 30) * 1000
let escrowParams = EscrowParams alicePkh bobPkh deadline
writePlutusScript
(env_LOCAL_CONFIG_DIR </> "escrow.plutus")
(ReleaseV2.escrowValidator escrowParams)
writePlutusScript
(env_LOCAL_CONFIG_DIR </> "escrow-debug.plutus")
(DebugV2.escrowValidator escrowParams)
-- Finally create the scripts.yaml file
createScriptsYaml
createConfig :: IO ()
createConfig = do
runCmd_ [str|mkdir -p #{env_LOCAL_CONFIG_DIR}|]
createTracingConfig
"tracing-plutus-v3"
ReleaseV3.tracingScript
ReleaseV3.tracingScript
DebugV3.tracingScript
DebugV3.tracingScript
createTracingConfig
"tracing-plutus-v2"
ReleaseV2.tracingPolicy
ReleaseV2.tracingValidator
DebugV2.tracingPolicy
DebugV2.tracingValidator
createEscrowConfig
--------------------------------------------------------------------------------
-- Main
--------------------------------------------------------------------------------
changeSecurityParam :: Int -> IO ()
changeSecurityParam i = do
-- NOTE: Using jq here fails because of runCmd. Need to investigate this
-- later.
-- TODO: Use jq instead of sed
updateAndCheck
shelly
[str|s/"securityParam": [0-9]*/"securityParam": #{secParam}/|]
".securityParam"
updateAndCheck byron [str|s/"k": [0-9]*/"k": #{secParam}/|] ".protocolConsts.k"
where
updateAndCheck fp sedQ jqQ = do
runCmd_ [str|sed -i '#{sedQ}' #{fp}|]
fpP <- Path.fromString fp
updated <-
File.readChunks fpP
& Cmd.pipeChunks [str|jq -r "#{jqQ}"|]
& firstNonEmptyLine "changeSecurityParam.updateAndCheck"
when (updated /= secParam) . error $
"changeSecurityParam: Unable to change security param: " ++ fp
shelly = env_TESTNET_WORK_DIR </> "shelley-genesis.json"
byron = env_TESTNET_WORK_DIR </> "byron-genesis.json"
secParam = show i
createTestnetConfig :: IO ()
createTestnetConfig = do
runCmd
"cardano-testnet create-env"
[ opt "num-pool-nodes" env_CARDANO_TESTNET_NUM_NODES
, flg "enable-new-epoch-state-logging"
, opt "output" env_TESTNET_WORK_DIR
, opt "testnet-magic" env_CARDANO_TESTNET_MAGIC
]
& Console.putChunks
changeSecurityParam 100
updateNode2Topology
startLocalTestnet :: IO ()
startLocalTestnet = do
runCmd
"cardano-testnet cardano"
[ opt "num-pool-nodes" env_CARDANO_TESTNET_NUM_NODES
, flg "enable-new-epoch-state-logging"
, opt "node-env" env_TESTNET_WORK_DIR
, opt "testnet-magic" env_CARDANO_TESTNET_MAGIC
]
& Console.putChunks
clean :: IO ()
clean = do
runCmd_ [str|rm -rf #{env_LOCAL_CONFIG_DIR}|]
runCmd_ [str|rm -rf #{env_TESTNET_WORK_DIR}|]
runCmd_ [str|rm -rf #{env_POPULATE_WORK_DIR}|]
runCmd_ [str|sh -c 'mv #{db} #{dbPrev} || true'|]
runCmd_ [str|sh -c 'mv #{ev} #{evPrev} || true'|]
where
db = "plutus-script-reexecutor.db"
dbPrev = "plutus-script-reexecutor.prev.db"
ev = "events.log"
evPrev = "events.prev.log"
setup :: IO ()
setup = do
clean
createConfig
createTestnetConfig
main :: IO ()
main = do
hSetBuffering stdout LineBuffering
hSetBuffering stderr LineBuffering
cmd <- execParser opts
setEnv "CARDANO_CLI" "cardano-cli"
setEnv "CARDANO_NODE" "cardano-node"
case cmd of
StartLocalTestnet -> startLocalTestnet
Clean -> clean
Populate PCTriggerTest -> testScriptTrigger
Populate PCEscrow -> escrow
Populate PCFanout -> runFanout
TriggerRollback -> triggerRollback
SyncNode2 -> syncNode2
Setup -> setup