Skip to content

Commit

Permalink
Update tests for governance drep hash checks
Browse files Browse the repository at this point in the history
  • Loading branch information
palas committed Oct 3, 2024
1 parent 685c5fd commit 2b400b5
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 29 deletions.
106 changes: 83 additions & 23 deletions cardano-cli/test/cardano-cli-golden/Test/Golden/Governance/DRep.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{- HLINT ignore "Use camelCase" -}

module Test.Golden.Governance.DRep where
Expand All @@ -17,13 +18,17 @@ import System.Posix.Files (fileMode, getFileStatus)
#endif

import Test.Cardano.CLI.Util (FileSem, bracketSem, execCardanoCLI, newFileSem,
noteInputFile, noteTempFile, propertyOnce)
noteInputFile, noteTempFile, propertyOnce, expectFailure, execCardanoCLIWithEnvVars)

import Hedgehog
import qualified Hedgehog as H
import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.File as H
import qualified Hedgehog.Extras.Test.Golden as H
import Control.Monad.Trans.Control (MonadBaseControl)
import Control.Monad.IO.Class (MonadIO)
import Control.Monad.Catch (MonadCatch)
import Test.Cardano.CLI.Hash (exampleAnchorDataHash, exampleAnchorDataIpfsHash, serveFilesWhile, exampleAnchorDataPathGolden)

-- | Semaphore protecting against locked file error, when running properties concurrently.
drepRetirementCertSem :: FileSem
Expand All @@ -35,6 +40,11 @@ drepRegistrationCertSem :: FileSem
drepRegistrationCertSem = newFileSem "test/cardano-cli-golden/files/golden/governance/drep/drep_registration_certificate.json"
{-# NOINLINE drepRegistrationCertSem #-}

-- | Semaphore protecting against locked file error, when running properties concurrently.
drepRegistrationCertSem2 :: FileSem
drepRegistrationCertSem2 = newFileSem "test/cardano-cli-golden/files/golden/governance/drep/drep_registration_certificate2.json"
{-# NOINLINE drepRegistrationCertSem2 #-}

hprop_golden_governanceDRepKeyGen :: Property
hprop_golden_governanceDRepKeyGen =
propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
Expand Down Expand Up @@ -204,29 +214,50 @@ hprop_golden_governance_drep_metadata_hash_cip119 = propertyOnce . H.moduleWorks

H.diffFileVsGoldenFile outputDRepMetadataHashCip119 goldenDRepMetadataHashCip119

hprop_golden_governance_drep_registration_certificate_vkey_file_wrong_hash_fails :: Property
hprop_golden_governance_drep_registration_certificate_vkey_file_wrong_hash_fails =
propertyOnce . expectFailure . H.moduleWorkspace "tmp" $ \tempDir ->
base_golden_governance_drep_registration_certificate_vkey_file
('a' : drop 1 exampleAnchorDataHash)
tempDir

hprop_golden_governance_drep_registration_certificate_vkey_file :: Property
hprop_golden_governance_drep_registration_certificate_vkey_file = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
hprop_golden_governance_drep_registration_certificate_vkey_file =
propertyOnce . H.moduleWorkspace "tmp" $ \tempDir ->
base_golden_governance_drep_registration_certificate_vkey_file exampleAnchorDataHash tempDir

base_golden_governance_drep_registration_certificate_vkey_file
:: (MonadBaseControl IO m, MonadTest m, MonadIO m, MonadCatch m) => String -> FilePath -> m ()
base_golden_governance_drep_registration_certificate_vkey_file hash tempDir = do
drepVKeyFile <- noteInputFile "test/cardano-cli-golden/files/input/drep.vkey"
H.noteShow_ drepRegistrationCertSem

outFile <- H.noteTempFile tempDir "drep-reg-cert.txt"

void $ execCardanoCLI
[ "conway", "governance", "drep", "registration-certificate"
, "--drep-verification-key-file", drepVKeyFile
, "--key-reg-deposit-amt", "0"
, "--drep-metadata-url", "dummy-url"
, "--drep-metadata-hash", "52e69500a92d80f2126c836a4903dc582006709f004cf7a28ed648f732dff8d2"
, "--out-file", outFile
]
let relativeUrl = ["ipfs", exampleAnchorDataIpfsHash]
serveFilesWhile
[(relativeUrl, exampleAnchorDataPathGolden)]
( \port -> do
void $
execCardanoCLIWithEnvVars
[("IPFS_GATEWAY_URI", "http://localhost:" ++ show port ++ "/")]
[ "conway", "governance", "drep", "registration-certificate"
, "--drep-verification-key-file", drepVKeyFile
, "--key-reg-deposit-amt", "0"
, "--drep-metadata-url", "ipfs://" ++ exampleAnchorDataIpfsHash
, "--drep-metadata-hash", hash
, "--check-drep-metadata-hash"
, "--out-file", outFile
]
)

bracketSem drepRegistrationCertSem $
H.diffFileVsGoldenFile outFile

hprop_golden_governance_drep_registration_certificate_id_hex :: Property
hprop_golden_governance_drep_registration_certificate_id_hex = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
idFile <- H.readFile "test/cardano-cli-golden/files/input/drep.id.hex"
H.noteShow_ drepRegistrationCertSem
H.noteShow_ drepRegistrationCertSem2

outFile <- H.noteTempFile tempDir "drep-reg-cert.txt"

Expand All @@ -236,16 +267,17 @@ hprop_golden_governance_drep_registration_certificate_id_hex = propertyOnce . H.
, "--key-reg-deposit-amt", "0"
, "--drep-metadata-url", "dummy-url"
, "--drep-metadata-hash", "52e69500a92d80f2126c836a4903dc582006709f004cf7a28ed648f732dff8d2"
, "--trust-drep-metadata-hash"
, "--out-file", outFile
]

bracketSem drepRegistrationCertSem $
bracketSem drepRegistrationCertSem2 $
H.diffFileVsGoldenFile outFile

hprop_golden_governance_drep_registration_certificate_id_bech32 :: Property
hprop_golden_governance_drep_registration_certificate_id_bech32 = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
idFile <- H.readFile "test/cardano-cli-golden/files/input/drep.id.bech32"
H.noteShow_ drepRegistrationCertSem
H.noteShow_ drepRegistrationCertSem2

outFile <- H.noteTempFile tempDir "drep-reg-cert.txt"

Expand All @@ -255,10 +287,11 @@ hprop_golden_governance_drep_registration_certificate_id_bech32 = propertyOnce .
, "--key-reg-deposit-amt", "0"
, "--drep-metadata-url", "dummy-url"
, "--drep-metadata-hash", "52e69500a92d80f2126c836a4903dc582006709f004cf7a28ed648f732dff8d2"
, "--trust-drep-metadata-hash"
, "--out-file", outFile
]

bracketSem drepRegistrationCertSem $
bracketSem drepRegistrationCertSem2 $
H.diffFileVsGoldenFile outFile

hprop_golden_governance_drep_registration_certificate_script_hash :: Property
Expand All @@ -273,30 +306,54 @@ hprop_golden_governance_drep_registration_certificate_script_hash = propertyOnce
, "--key-reg-deposit-amt", "0"
, "--drep-metadata-url", "dummy-url"
, "--drep-metadata-hash", "52e69500a92d80f2126c836a4903dc582006709f004cf7a28ed648f732dff8d2"
, "--trust-drep-metadata-hash"
, "--out-file", outFile
]

H.diffFileVsGoldenFile outFile goldenFile

-- | Execute me with:
-- @cabal test cardano-cli-golden --test-options '-p "/golden governance drep update certificate vkey file wrong hash/"'@
hprop_golden_governance_drep_update_certificate_vkey_file_wrong_hash_fails :: Property
hprop_golden_governance_drep_update_certificate_vkey_file_wrong_hash_fails =
propertyOnce . expectFailure . H.moduleWorkspace "tmp" $ \tempDir ->
base_golden_governance_drep_update_certificate_vkey_file
('a' : drop 1 exampleAnchorDataHash)
tempDir

-- | Execute me with:
-- @cabal test cardano-cli-golden --test-options '-p "/golden governance drep update certificate vkey file/"'@
hprop_golden_governance_drep_update_certificate_vkey_file :: Property
hprop_golden_governance_drep_update_certificate_vkey_file = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
hprop_golden_governance_drep_update_certificate_vkey_file =
propertyOnce . H.moduleWorkspace "tmp" $ \tempDir ->
base_golden_governance_drep_update_certificate_vkey_file exampleAnchorDataHash tempDir

base_golden_governance_drep_update_certificate_vkey_file
:: (MonadBaseControl IO m, MonadTest m, MonadIO m, MonadCatch m) => String -> FilePath -> m ()
base_golden_governance_drep_update_certificate_vkey_file hash tempDir = do
drepVKeyFile <- noteInputFile "test/cardano-cli-golden/files/input/drep.vkey"
goldenFile <- H.note "test/cardano-cli-golden/files/golden/governance/drep/drep_update_certificate.json"

outFile <- H.noteTempFile tempDir "drep-upd-cert.txt"

void $ execCardanoCLI
[ "conway", "governance", "drep", "update-certificate"
, "--drep-verification-key-file", drepVKeyFile
, "--drep-metadata-url", "dummy-url"
, "--drep-metadata-hash", "52e69500a92d80f2126c836a4903dc582006709f004cf7a28ed648f732dff8d2"
, "--out-file", outFile
]
let relativeUrl = ["ipfs", exampleAnchorDataIpfsHash]
serveFilesWhile
[(relativeUrl, exampleAnchorDataPathGolden)]
( \port -> do
void $
execCardanoCLIWithEnvVars
[("IPFS_GATEWAY_URI", "http://localhost:" ++ show port ++ "/")]
[ "conway", "governance", "drep", "update-certificate"
, "--drep-verification-key-file", drepVKeyFile
, "--drep-metadata-url", "ipfs://" ++ exampleAnchorDataIpfsHash
, "--drep-metadata-hash", hash
, "--check-drep-metadata-hash"
, "--out-file", outFile
]
)

H.diffFileVsGoldenFile outFile goldenFile

-- | Execute me with:
-- @cabal test cardano-cli-golden --test-options '-p "/golden governance drep update certificate script hash/"'@
hprop_golden_governance_drep_update_certificate_script_hash :: Property
Expand All @@ -309,6 +366,9 @@ hprop_golden_governance_drep_update_certificate_script_hash = propertyOnce . H.m
, "--drep-script-hash", "8f33600845940d65bdbc7ea7a247a7997aa8558649128fa82c4c0468"
, "--drep-metadata-url", "https://raw.githubusercontent.com/cardano-foundation/CIPs/master/CIP-0119/examples/drep.jsonld"
, "--drep-metadata-hash", "fecc1773db89b45557d82e07719c275f6877a6cadfd2469f4dc5a7df5b38b4a4"
, "--trust-drep-metadata-hash" -- This is to avoid connecting to GitHub during tests, but it is actually a
-- really good test to run it with "--check-drep-metadata-hash" here,
-- because the URL and hash are correct.
, "--out-file", outFile
]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"type": "CertificateConway",
"description": "DRep Key Registration Certificate",
"cborHex": "84108200581ceb09d5556a8bce421074e394d02c79ced96741657b4cf7ca8995294d00826964756d6d792d75726c582052e69500a92d80f2126c836a4903dc582006709f004cf7a28ed648f732dff8d2"
"cborHex": "84108200581ceb09d5556a8bce421074e394d02c79ced96741657b4cf7ca8995294d00827835697066733a2f2f516d624c354542464a4c66384464506b5741736b47334575696e39744859386e617151324a446f486e574848584a5820de38a4f5b8b9d8372386cc923bad19d1a0662298cf355bbe947e5eedf127fa9c"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "CertificateConway",
"description": "DRep Key Registration Certificate",
"cborHex": "84108200581ceb09d5556a8bce421074e394d02c79ced96741657b4cf7ca8995294d00826964756d6d792d75726c582052e69500a92d80f2126c836a4903dc582006709f004cf7a28ed648f732dff8d2"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"type": "CertificateConway",
"description": "DRep Update Certificate",
"cborHex": "83128200581ceb09d5556a8bce421074e394d02c79ced96741657b4cf7ca8995294d826964756d6d792d75726c582052e69500a92d80f2126c836a4903dc582006709f004cf7a28ed648f732dff8d2"
"cborHex": "83128200581ceb09d5556a8bce421074e394d02c79ced96741657b4cf7ca8995294d827835697066733a2f2f516d624c354542464a4c66384464506b5741736b47334575696e39744859386e617151324a446f486e574848584a5820de38a4f5b8b9d8372386cc923bad19d1a0662298cf355bbe947e5eedf127fa9c"
}
10 changes: 8 additions & 2 deletions cardano-cli/test/cardano-cli-golden/files/golden/help.cli
Original file line number Diff line number Diff line change
Expand Up @@ -6786,7 +6786,10 @@ Usage: cardano-cli conway governance drep registration-certificate
)
--key-reg-deposit-amt NATURAL
[--drep-metadata-url TEXT
--drep-metadata-hash HASH]
--drep-metadata-hash HASH
( --check-drep-metadata-hash
| --trust-drep-metadata-hash
)]
--out-file FILEPATH

Create a registration certificate.
Expand All @@ -6809,7 +6812,10 @@ Usage: cardano-cli conway governance drep update-certificate
| --drep-key-hash HASH
)
[--drep-metadata-url TEXT
--drep-metadata-hash HASH]
--drep-metadata-hash HASH
( --check-drep-metadata-hash
| --trust-drep-metadata-hash
)]
--out-file FILEPATH

Create a DRep update certificate.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ Usage: cardano-cli conway governance drep registration-certificate
)
--key-reg-deposit-amt NATURAL
[--drep-metadata-url TEXT
--drep-metadata-hash HASH]
--drep-metadata-hash HASH
( --check-drep-metadata-hash
| --trust-drep-metadata-hash
)]
--out-file FILEPATH

Create a registration certificate.
Expand All @@ -25,5 +28,12 @@ Available options:
--drep-metadata-url TEXT DRep anchor URL
--drep-metadata-hash HASH
DRep anchor data hash.
--check-drep-metadata-hash
Check the DRep metadata hash (from
--drep-metadata-hash) by downloading DRep metadata
data (from --drep-metadata-url).
--trust-drep-metadata-hash
Do not check the DRep metadata hash (from
--drep-metadata-hash) and trust it is correct.
--out-file FILEPATH The output file.
-h,--help Show this help text
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ Usage: cardano-cli conway governance drep update-certificate
| --drep-key-hash HASH
)
[--drep-metadata-url TEXT
--drep-metadata-hash HASH]
--drep-metadata-hash HASH
( --check-drep-metadata-hash
| --trust-drep-metadata-hash
)]
--out-file FILEPATH

Create a DRep update certificate.
Expand All @@ -22,5 +25,12 @@ Available options:
--drep-metadata-url TEXT DRep anchor URL
--drep-metadata-hash HASH
DRep anchor data hash.
--check-drep-metadata-hash
Check the DRep metadata hash (from
--drep-metadata-hash) by downloading DRep metadata
data (from --drep-metadata-url).
--trust-drep-metadata-hash
Do not check the DRep metadata hash (from
--drep-metadata-hash) and trust it is correct.
--out-file FILEPATH The output file.
-h,--help Show this help text
2 changes: 2 additions & 0 deletions cardano-cli/test/cardano-cli-test/Test/Cli/Governance/DRep.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ hprop_governance_drep_registration_certificate_script_hash =
, metadataUrl
, "--drep-metadata-hash"
, "52e69500a92d80f2126c836a4903dc582006709f004cf7a28ed648f732dff8d2"
, "--trust-drep-metadata-hash"
, "--out-file"
, outFile
]
Expand Down Expand Up @@ -79,6 +80,7 @@ hprop_governance_drep_update_certificate_vkey_file =
, metadataUrl
, "--drep-metadata-hash"
, "52e69500a92d80f2126c836a4903dc582006709f004cf7a28ed648f732dff8d2"
, "--trust-drep-metadata-hash"
, "--out-file"
, outFile
]

0 comments on commit 2b400b5

Please sign in to comment.