-
Notifications
You must be signed in to change notification settings - Fork 721
/
ProposeNewConstitution.hs
408 lines (340 loc) · 14.5 KB
/
ProposeNewConstitution.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
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
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Cardano.Testnet.Test.LedgerEvents.Gov.ProposeNewConstitution
( hprop_ledger_events_propose_new_constitution
) where
import Cardano.Api
import Cardano.Api.Shelley
import Cardano.Testnet
import Prelude
import qualified Cardano.Ledger.Conway.Governance as Ledger
import Control.Monad
import Control.Monad.Trans.Except
import Control.Monad.Trans.Except.Extra
import qualified Data.Map.Strict as Map
import Data.String
import qualified Data.Text as Text
import Data.Word
import GHC.IO.Exception (IOException)
import GHC.Stack (HasCallStack, callStack)
import Lens.Micro
import System.FilePath ((</>))
import Hedgehog
import qualified Hedgehog.Extras as H
import qualified Hedgehog.Extras.Stock.IO.Network.Sprocket as IO
import qualified Testnet.Process.Cli as P
import qualified Testnet.Process.Run as H
import Testnet.Components.SPO
import qualified Testnet.Property.Utils as H
import Testnet.Runtime
newtype AdditionalCatcher
= IOE IOException
deriving Show
hprop_ledger_events_propose_new_constitution :: Property
hprop_ledger_events_propose_new_constitution = H.integrationRetryWorkspace 2 "propose-new-constitution" $ \tempAbsBasePath' -> do
-- Start a local test net
conf@Conf { tempAbsPath } <- H.noteShowM $ mkConf tempAbsBasePath'
let tempAbsPath' = unTmpAbsPath tempAbsPath
tempBaseAbsPath = makeTmpBaseAbsPath tempAbsPath
work <- H.createDirectoryIfMissing $ tempAbsPath' </> "work"
let sbe = ShelleyBasedEraConway
era = toCardanoEra sbe
cEra = AnyCardanoEra era
fastTestnetOptions = cardanoDefaultTestnetOptions
{ cardanoEpochLength = 100
, cardanoSlotLength = 0.1
, cardanoNodeEra = cEra
}
testnetRuntime@TestnetRuntime
{ testnetMagic
, poolNodes
, wallets
}
<- cardanoTestnet fastTestnetOptions conf
poolNode1 <- H.headM poolNodes
poolSprocket1 <- H.noteShow $ nodeSprocket $ poolRuntime poolNode1
execConfig <- H.mkExecConfig tempBaseAbsPath poolSprocket1
let socketName' = IO.sprocketName poolSprocket1
socketBase = IO.sprocketBase poolSprocket1 -- /tmp
socketPath = socketBase </> socketName'
H.note_ $ "Sprocket: " <> show poolSprocket1
H.note_ $ "Abs path: " <> tempAbsBasePath'
H.note_ $ "Socketpath: " <> socketPath
H.note_ $ "Foldblocks config file: " <> configurationFile testnetRuntime
-- Create Conway constitution
gov <- H.createDirectoryIfMissing $ work </> "governance"
proposalAnchorFile <- H.note $ work </> gov </> "sample-proposFal-anchor"
consitutionFile <- H.note $ work </> gov </> "sample-constitution"
constitutionActionFp <- H.note $ work </> gov </> "constitution.action"
H.writeFile proposalAnchorFile "dummy anchor data"
H.writeFile consitutionFile "dummy constitution data"
constitutionHash <- H.execCli' execConfig
[ "conway", "governance"
, "hash", "--file-text", consitutionFile
]
proposalAnchorDataHash <- H.execCli' execConfig
[ "conway", "governance"
, "hash", "--file-text", proposalAnchorFile
]
let stakeVkeyFp = gov </> "stake.vkey"
stakeSKeyFp = gov </> "stake.skey"
_ <- P.cliStakeAddressKeyGen tempAbsPath'
$ P.KeyNames { P.verificationKeyFile = stakeVkeyFp
, P.signingKeyFile = stakeSKeyFp
}
let drepVkeyFp :: Int -> FilePath
drepVkeyFp n = gov </> "drep-keys" <>"drep" <> show n <> ".vkey"
drepSKeyFp :: Int -> FilePath
drepSKeyFp n = gov </> "drep-keys" <>"drep" <> show n <> ".skey"
-- Create DReps -- TODO: Refactor with shelleyKeyGen
forM_ [1..3] $ \n -> do
H.execCli' execConfig
[ "conway", "governance", "drep", "key-gen"
, "--verification-key-file", drepVkeyFp n
, "--signing-key-file", drepSKeyFp n
]
-- Create Drep registration certificates
let drepCertFile :: Int -> FilePath
drepCertFile n = gov </> "drep-keys" <>"drep" <> show n <> ".regcert"
forM_ [1..3] $ \n -> do
H.execCli' execConfig
[ "conway", "governance", "drep", "registration-certificate"
, "--drep-verification-key-file", drepVkeyFp n
, "--key-reg-deposit-amt", show @Int 0
, "--out-file", drepCertFile n
]
-- Retrieve UTxOs for registration submission
void $ H.execCli' execConfig
[ "conway", "query", "utxo"
, "--address", Text.unpack $ paymentKeyInfoAddr $ head wallets
, "--cardano-mode"
, "--testnet-magic", show @Int testnetMagic
, "--out-file", work </> "utxo-1.json"
]
utxo1Json <- H.leftFailM . H.readJsonFile $ work </> "utxo-1.json"
UTxO utxo1 <- H.noteShowM $ H.noteShowM $ decodeEraUTxO sbe utxo1Json
txin1 <- H.noteShow =<< H.headM (Map.keys utxo1)
drepRegTxbodyFp <- H.note $ work </> "drep.registration.txbody"
drepRegTxSignedFp <- H.note $ work </> "drep.registration.tx"
void $ H.execCli' execConfig
[ "conway", "transaction", "build"
, "--testnet-magic", show @Int testnetMagic
, "--change-address", Text.unpack $ paymentKeyInfoAddr $ head wallets
, "--tx-in", Text.unpack $ renderTxIn txin1
, "--tx-out", Text.unpack (paymentKeyInfoAddr (wallets !! 1)) <> "+" <> show @Int 5_000_000
, "--certificate-file", drepCertFile 1
, "--certificate-file", drepCertFile 2
, "--certificate-file", drepCertFile 3
, "--witness-override", show @Int 4
, "--out-file", drepRegTxbodyFp
]
void $ H.execCli' execConfig
[ "conway", "transaction", "sign"
, "--testnet-magic", show @Int testnetMagic
, "--tx-body-file", drepRegTxbodyFp
, "--signing-key-file", paymentSKey $ paymentKeyInfoPair $ head wallets
, "--signing-key-file", drepSKeyFp 1
, "--signing-key-file", drepSKeyFp 2
, "--signing-key-file", drepSKeyFp 3
, "--out-file", drepRegTxSignedFp
]
void $ H.execCli' execConfig
[ "conway", "transaction", "submit"
, "--testnet-magic", show @Int testnetMagic
, "--tx-file", drepRegTxSignedFp
]
-- Create constitution proposal
void $ H.execCli' execConfig
[ "conway", "governance", "action", "create-constitution"
, "--testnet"
, "--governance-action-deposit", show @Int 0 -- TODO: Get this from the node
, "--deposit-return-stake-verification-key-file", stakeVkeyFp
, "--anchor-url", "https://tinyurl.com/3wrwb2as"
, "--anchor-data-hash", proposalAnchorDataHash
, "--constitution-url", "https://tinyurl.com/2pahcy6z"
, "--constitution-hash", constitutionHash
, "--out-file", constitutionActionFp
]
txbodyFp <- H.note $ work </> "tx.body"
txbodySignedFp <- H.note $ work </> "tx.body.signed"
void $ H.execCli' execConfig
[ "conway", "query", "utxo"
, "--address", Text.unpack $ paymentKeyInfoAddr $ wallets !! 1
, "--cardano-mode"
, "--testnet-magic", show @Int testnetMagic
, "--out-file", work </> "utxo-2.json"
]
utxo2Json <- H.leftFailM . H.readJsonFile $ work </> "utxo-2.json"
UTxO utxo2 <- H.noteShowM $ H.noteShowM $ decodeEraUTxO sbe utxo2Json
txin2 <- H.noteShow =<< H.headM (Map.keys utxo2)
void $ H.execCli' execConfig
[ "conway", "transaction", "build"
, "--testnet-magic", show @Int testnetMagic
, "--change-address", Text.unpack $ paymentKeyInfoAddr $ wallets !! 1
, "--tx-in", Text.unpack $ renderTxIn txin2
, "--tx-out", Text.unpack (paymentKeyInfoAddr (head wallets)) <> "+" <> show @Int 5_000_000
, "--proposal-file", constitutionActionFp
, "--out-file", txbodyFp
]
void $ H.execCli' execConfig
[ "conway", "transaction", "sign"
, "--testnet-magic", show @Int testnetMagic
, "--tx-body-file", txbodyFp
, "--signing-key-file", paymentSKey $ paymentKeyInfoPair $ wallets !! 1
, "--out-file", txbodySignedFp
]
void $ H.execCli' execConfig
[ "conway", "transaction", "submit"
, "--testnet-magic", show @Int testnetMagic
, "--tx-file", txbodySignedFp
]
txidString <- mconcat . lines <$> H.execCli' execConfig
[ "transaction", "txid"
, "--tx-file", txbodySignedFp
]
!propSubmittedResult
<- runExceptT $ handleIOExceptT IOE
$ runExceptT $ foldBlocks
(File $ configurationFile testnetRuntime)
(File socketPath)
FullValidation
Nothing -- Initial accumulator state
(foldBlocksCheckProposalWasSubmitted (fromString txidString))
newProposalEvents <- case propSubmittedResult of
Left (IOE e) ->
H.failMessage callStack
$ "foldBlocksCheckProposalWasSubmitted failed with: " <> show e
Right (Left e) ->
H.failMessage callStack
$ "foldBlocksCheckProposalWasSubmitted failed with: " <> Text.unpack (renderFoldBlocksError e)
Right (Right events) -> return events
governanceActionIndex <- retrieveGovernanceActionIndex newProposalEvents
let voteFp :: Int -> FilePath
voteFp n = work </> gov </> "vote-" <> show n
-- Proposal was successfully submitted, now we vote on the proposal and confirm it was ratified
forM_ [1..3] $ \n -> do
H.execCli' execConfig
[ "conway", "governance", "vote", "create"
, "--yes"
, "--governance-action-tx-id", txidString
, "--governance-action-index", show @Word32 governanceActionIndex
, "--drep-verification-key-file", drepVkeyFp n
, "--out-file", voteFp n
]
-- We need more UTxOs
void $ H.execCli' execConfig
[ "conway", "query", "utxo"
, "--address", Text.unpack $ paymentKeyInfoAddr $ head wallets
, "--cardano-mode"
, "--testnet-magic", show @Int testnetMagic
, "--out-file", work </> "utxo-3.json"
]
utxo3Json <- H.leftFailM . H.readJsonFile $ work </> "utxo-3.json"
UTxO utxo3 <- H.noteShowM $ H.noteShowM $ decodeEraUTxO sbe utxo3Json
txin3 <- H.noteShow =<< H.headM (Map.keys utxo3)
voteTxFp <- H.note $ work </> gov </> "vote.tx"
voteTxBodyFp <- H.note $ work </> gov </> "vote.txbody"
-- Submit votes
void $ H.execCli' execConfig
[ "conway", "transaction", "build"
, "--testnet-magic", show @Int testnetMagic
, "--change-address", Text.unpack $ paymentKeyInfoAddr $ head wallets
, "--tx-in", Text.unpack $ renderTxIn txin3
, "--tx-out", Text.unpack (paymentKeyInfoAddr (wallets !! 1)) <> "+" <> show @Int 3_000_000
, "--vote-file", voteFp 1
, "--vote-file", voteFp 2
, "--vote-file", voteFp 3
, "--witness-override", show @Int 4
, "--out-file", voteTxBodyFp
]
void $ H.execCli' execConfig
[ "conway", "transaction", "sign"
, "--testnet-magic", show @Int testnetMagic
, "--tx-body-file", voteTxBodyFp
, "--signing-key-file", paymentSKey $ paymentKeyInfoPair $ head wallets
, "--signing-key-file", drepSKeyFp 1
, "--signing-key-file", drepSKeyFp 2
, "--signing-key-file", drepSKeyFp 3
, "--out-file", voteTxFp
]
void $ H.execCli' execConfig
[ "conway", "transaction", "submit"
, "--testnet-magic", show @Int testnetMagic
, "--tx-file", voteTxFp
]
-- We check that constitution was succcessfully ratified
!eConstitutionAdopted
<- runExceptT $ handleIOExceptT IOE
$ runExceptT $ foldBlocks
(File $ configurationFile testnetRuntime)
(File socketPath)
FullValidation
[] -- Initial accumulator state
(foldBlocksCheckConstitutionWasRatified constitutionHash)
case eConstitutionAdopted of
Left (IOE e) ->
H.failMessage callStack
$ "foldBlocksCheckConstitutionWasRatified failed with: " <> show e
Right (Left e) ->
H.failMessage callStack
$ "foldBlocksCheckConstitutionWasRatified failed with: " <> Text.unpack (renderFoldBlocksError e)
Right (Right _events) -> success
foldBlocksCheckProposalWasSubmitted
:: TxId -- TxId of submitted tx
-> Env
-> LedgerState
-> [LedgerEvent]
-> BlockInMode -- Block i
-> Maybe LedgerEvent -- ^ Accumulator at block i - 1
-> IO (Maybe LedgerEvent, FoldStatus) -- ^ Accumulator at block i and fold status
foldBlocksCheckProposalWasSubmitted txid _ _ allEvents _ _ = do
let newGovProposal = filter (filterNewGovProposals txid) allEvents
if null newGovProposal
then return (Nothing, ContinueFold)
else return (Just $ head newGovProposal , StopFold)
retrieveGovernanceActionIndex
:: (HasCallStack, MonadTest m)
=> Maybe LedgerEvent -> m Word32
retrieveGovernanceActionIndex mEvent = do
case mEvent of
Nothing -> H.failMessage callStack "retrieveGovernanceActionIndex: No new governance proposals found"
Just (NewGovernanceProposals _ (AnyProposals props)) ->
-- In this test there will only be one
let govActionStates = [i
| Ledger.GovActionIx i <- map Ledger.gaidGovActionIx . Map.keys $ Ledger.proposalsGovActionStates props
]
in return $ head govActionStates
Just unexpectedEvent ->
H.failMessage callStack
$ mconcat ["retrieveGovernanceActionIndex: Expected NewGovernanceProposals, got: "
, show unexpectedEvent
]
filterNewGovProposals :: TxId -> LedgerEvent -> Bool
filterNewGovProposals txid (NewGovernanceProposals eventTxId (AnyProposals props)) =
let _govActionStates = Ledger.proposalsGovActionStates props
in fromShelleyTxId eventTxId == txid
filterNewGovProposals _ _ = False
foldBlocksCheckConstitutionWasRatified
:: String -- submitted constitution hash
-> Env
-> LedgerState
-> [LedgerEvent]
-> BlockInMode -- Block i
-> [LedgerEvent] -- ^ Accumulator at block i - 1
-> IO ([LedgerEvent], FoldStatus) -- ^ Accumulator at block i and fold status
foldBlocksCheckConstitutionWasRatified submittedConstitutionHash _ _ allEvents _ _ =
if any (filterRatificationState submittedConstitutionHash) allEvents
then return (allEvents , StopFold)
else return ([], ContinueFold)
filterRatificationState
:: String -- ^ Submitted constitution anchor hash
-> LedgerEvent
-> Bool
filterRatificationState c (EpochBoundaryRatificationState (AnyRatificationState rState)) =
let constitutionAnchorHash = Ledger.anchorDataHash $ Ledger.constitutionAnchor (rState ^. Ledger.rsEnactStateL . Ledger.ensConstitutionL)
in Text.pack c == renderSafeHashAsHex constitutionAnchorHash
filterRatificationState _ _ = False