Skip to content

Commit

Permalink
Generalize and test also "no" and "abstain" votes
Browse files Browse the repository at this point in the history
  • Loading branch information
palas committed Mar 27, 2024
1 parent 275bfaa commit 74dfb81
Showing 1 changed file with 43 additions and 27 deletions.
Expand Up @@ -73,13 +73,23 @@ hprop_ledger_events_propose_new_constitution = H.integrationWorkspace "propose-n

work <- H.createDirectoryIfMissing $ tempAbsPath' </> "work"

-- Generate model for votes
let allVotes :: [(String, Int)]
allVotes = voteList [("yes", 4), ("no", 3), ("abstain", 2)]
annotateShow allVotes

let numVotes :: Int
numVotes = length allVotes
annotateShow numVotes

let sbe = ShelleyBasedEraConway
era = toCardanoEra sbe
cEra = AnyCardanoEra era
fastTestnetOptions = cardanoDefaultTestnetOptions
{ cardanoEpochLength = 100
, cardanoSlotLength = 0.1
, cardanoNodeEra = cEra
, cardanoNumDReps = numVotes
}

TestnetRuntime
Expand Down Expand Up @@ -140,8 +150,9 @@ hprop_ledger_events_propose_new_constitution = H.integrationWorkspace "propose-n

-- Create Drep registration certificates
let drepCertFile :: Int -> FilePath
drepCertFile n = gov </> "drep-keys" <>"drep" <> show n <> ".regcert"
forM_ [1..3] $ \n -> do
drepCertFile n = gov </> "drep-keys" <> "drep" <> show n <> ".regcert"

forM_ allVotes $ \(_, n) -> do
H.execCli' execConfig
[ "conway", "governance", "drep", "registration-certificate"
, "--drep-verification-key-file", drepVkeyFp n
Expand All @@ -150,31 +161,27 @@ hprop_ledger_events_propose_new_constitution = H.integrationWorkspace "propose-n
]

-- Retrieve UTxOs for registration submission
txin1 <- findLargestUtxoForPaymentKey epochStateView sbe $ wallets !! 0
regTxIn1 <- findLargestUtxoForPaymentKey epochStateView sbe $ wallets !! 0

drepRegTxbodyFp <- H.note $ work </> "drep.registration.txbody"
drepRegTxSignedFp <- H.note $ work </> "drep.registration.tx"

void $ H.execCli' execConfig
void $ H.execCli' execConfig $
[ "conway", "transaction", "build"
, "--change-address", Text.unpack $ paymentKeyInfoAddr $ wallets !! 0
, "--tx-in", Text.unpack $ renderTxIn txin1
, "--tx-in", Text.unpack $ renderTxIn regTxIn1
, "--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
] ++ (concat [["--certificate-file", drepCertFile n] | (_, n) <- allVotes]) ++
[ "--witness-override", show @Int (numVotes + 1)
, "--out-file", drepRegTxbodyFp
]

void $ H.execCli' execConfig
void $ H.execCli' execConfig $
[ "conway", "transaction", "sign"
, "--tx-body-file", drepRegTxbodyFp
, "--signing-key-file", paymentSKey $ paymentKeyInfoPair $ wallets !! 0
, "--signing-key-file", drepSKeyFp 1
, "--signing-key-file", drepSKeyFp 2
, "--signing-key-file", drepSKeyFp 3
, "--out-file", drepRegTxSignedFp
] ++ (concat [["--signing-key-file", drepSKeyFp n] | (_, n) <- allVotes]) ++
[ "--out-file", drepRegTxSignedFp
]

void $ H.execCli' execConfig
Expand Down Expand Up @@ -253,10 +260,10 @@ hprop_ledger_events_propose_new_constitution = H.integrationWorkspace "propose-n
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
forM_ allVotes $ \(vote, n) -> do
H.execCli' execConfig
[ "conway", "governance", "vote", "create"
, "--yes"
, "--" ++ vote
, "--governance-action-tx-id", txidString
, "--governance-action-index", show @Word32 governanceActionIndex
, "--drep-verification-key-file", drepVkeyFp n
Expand All @@ -271,26 +278,22 @@ hprop_ledger_events_propose_new_constitution = H.integrationWorkspace "propose-n
voteTxBodyFp <- H.note $ work </> gov </> "vote.txbody"

-- Submit votes
void $ H.execCli' execConfig
void $ H.execCli' execConfig $
[ "conway", "transaction", "build"
, "--change-address", Text.unpack $ paymentKeyInfoAddr $ wallets !! 0
, "--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
] ++ (concat [["--vote-file", voteFp n] | (_, n) <- allVotes]) ++
[ "--witness-override", show @Int (numVotes + 1)
, "--out-file", voteTxBodyFp
]

void $ H.execCli' execConfig
void $ H.execCli' execConfig $
[ "conway", "transaction", "sign"
, "--tx-body-file", voteTxBodyFp
, "--signing-key-file", paymentSKey $ paymentKeyInfoPair $ wallets !! 0
, "--signing-key-file", drepSKeyFp 1
, "--signing-key-file", drepSKeyFp 2
, "--signing-key-file", drepSKeyFp 3
, "--out-file", voteTxFp
] ++ (concat [["--signing-key-file", drepSKeyFp n] | (_, n) <- allVotes]) ++
[ "--out-file", voteTxFp
]

void $ H.execCli' execConfig
Expand Down Expand Up @@ -319,6 +322,8 @@ hprop_ledger_events_propose_new_constitution = H.integrationWorkspace "propose-n
, "--out-file", finalGovState
]

-- Tally registered votes

finalGovFileBS <- liftIO $ LBS.readFile finalGovState

votes <- H.nothingFail $ do (Aeson.Object jsonValue) <- Aeson.decode finalGovFileBS
Expand All @@ -327,13 +332,24 @@ hprop_ledger_events_propose_new_constitution = H.integrationWorkspace "propose-n
(Aeson.Object votes) <- proposal KeyMap.!? "dRepVotes"
KeyMap.foldl' extractVote (Just []) votes

length (filter (== "VoteYes") votes) === 3
length (filter (== "VoteYes") votes) === 4
length (filter (== "VoteNo") votes) === 3
length (filter (== "Abstain") votes) === 2
length votes === numVotes

where
extractVote :: Maybe [String] -> Aeson.Value -> Maybe [String]
extractVote (Just acc) (Aeson.String x) = Just $ unpack x:acc
extractVote _ _ = Nothing

voteList :: [(a, Int)] -> [(a, Int)]
voteList l = go 1 l
where
go :: Int -> [(a, Int)] -> [(a, Int)]
go _ [] = []
go n ((s, m):r) | m > 0 = (s, n):go (n + 1) ((s, m - 1):r)
| otherwise = go n r


retrieveGovernanceActionIndex
:: (HasCallStack, MonadTest m)
Expand Down

0 comments on commit 74dfb81

Please sign in to comment.