Skip to content

Commit

Permalink
Add golden test for stake pool metadata hash command
Browse files Browse the repository at this point in the history
  • Loading branch information
intricate committed Jul 3, 2020
1 parent 588f13a commit de4b9ea
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 8 deletions.
1 change: 1 addition & 0 deletions cardano-cli/cardano-cli.cabal
Expand Up @@ -205,6 +205,7 @@ test-suite cardano-cli-pioneers
Test.CLI.Shelley.TextEnvelope.Golden.Tx.TxBody
Test.CLI.Shelley.TextEnvelope.Golden.Tx.Witness
Test.ITN
Test.Metadata
Test.OptParse
Test.Pioneers.Exercise1
Test.Pioneers.Exercise2
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Shelley/Commands.hs
Expand Up @@ -162,7 +162,7 @@ data PoolCmd
-- ^ Epoch in which to retire the stake pool. --TODO: Double check this
OutputFile
| PoolGetId VerificationKeyFile
| PoolMetaDataHash PoolMetaDataFile
| PoolMetaDataHash PoolMetaDataFile (Maybe OutputFile)
deriving (Eq, Show)


Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs
Expand Up @@ -392,7 +392,7 @@ pPoolCmd =
pId = PoolGetId <$> pVerificationKeyFile Output

pPoolMetaDataHashSubCmd :: Parser PoolCmd
pPoolMetaDataHashSubCmd = PoolMetaDataHash <$> pPoolMetaDataFile
pPoolMetaDataHashSubCmd = PoolMetaDataHash <$> pPoolMetaDataFile <*> pMaybeOutputFile


pQueryCmd :: Parser QueryCmd
Expand Down
17 changes: 12 additions & 5 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Pool.hs
Expand Up @@ -18,7 +18,7 @@ import qualified Data.ByteString.Char8 as BS

import Cardano.Api (StakePoolMetadataValidationError, Network(..),
decodeAndValidateStakePoolMetadata, renderStakePoolMetadataValidationError)
import Cardano.Api.TextView (TextViewTitle (..))
import Cardano.Api.TextView (TextViewTitle (..), textShow)
import Cardano.Api.Typed (AsType (..), Error (..), FileError (..), Key (..),
Lovelace, StakeCredential (..), StakePoolMetadataReference (..),
StakePoolParameters (..), StakePoolRelay (..), TextEnvelopeError,
Expand All @@ -39,6 +39,7 @@ import qualified Cardano.Crypto.Hash.Class as Crypto
data ShelleyPoolCmdError
= ShelleyPoolReadFileError !(FileError TextEnvelopeError)
| ShelleyPoolWriteFileError !(FileError ())
| ShelleyPoolWriteMetaDataHashError !FilePath !IOException
| ShelleyPoolMetaDataValidationError !StakePoolMetadataValidationError
deriving Show

Expand All @@ -47,6 +48,8 @@ renderShelleyPoolCmdError err =
case err of
ShelleyPoolReadFileError fileErr -> Text.pack (displayError fileErr)
ShelleyPoolWriteFileError fileErr -> Text.pack (displayError fileErr)
ShelleyPoolWriteMetaDataHashError fp ioException ->
"Error writing stake pool metadata hash at: " <> textShow fp <> " Error: " <> textShow ioException
ShelleyPoolMetaDataValidationError validationErr ->
"Error validating stake pool metadata: " <> renderStakePoolMetadataValidationError validationErr

Expand All @@ -58,7 +61,7 @@ runPoolCmd (PoolRegistrationCert sPvkey vrfVkey pldg pCost pMrgn rwdVerFp ownerV
runPoolCmd (PoolRetirementCert sPvkeyFp retireEpoch outfp) =
runStakePoolRetirementCert sPvkeyFp retireEpoch outfp
runPoolCmd (PoolGetId sPvkey) = runPoolId sPvkey
runPoolCmd (PoolMetaDataHash poolMdFile) = runPoolMetaDataHash poolMdFile
runPoolCmd (PoolMetaDataHash poolMdFile mOutFile) = runPoolMetaDataHash poolMdFile mOutFile
runPoolCmd cmd = liftIO $ putStrLn $ "runPoolCmd: " ++ show cmd


Expand Down Expand Up @@ -191,13 +194,17 @@ runPoolId (VerificationKeyFile vkeyPath) = do
$ readFileTextEnvelope (AsVerificationKey AsStakePoolKey) vkeyPath
liftIO $ BS.putStrLn $ serialiseToRawBytesHex (verificationKeyHash stakePoolVerKey)

runPoolMetaDataHash :: PoolMetaDataFile -> ExceptT ShelleyPoolCmdError IO ()
runPoolMetaDataHash (PoolMetaDataFile poolMDPath) = do
runPoolMetaDataHash :: PoolMetaDataFile -> Maybe OutputFile -> ExceptT ShelleyPoolCmdError IO ()
runPoolMetaDataHash (PoolMetaDataFile poolMDPath) mOutFile = do
metaDataBytes <- handleIOExceptT (ShelleyPoolReadFileError . FileIOError poolMDPath) $
BS.readFile poolMDPath
_ <- firstExceptT ShelleyPoolMetaDataValidationError
. hoistEither
$ decodeAndValidateStakePoolMetadata metaDataBytes
let metaDataHash :: Crypto.Hash Crypto.Blake2b_256 ByteString
metaDataHash = Crypto.hashRaw (\x -> x) metaDataBytes
liftIO $ BS.putStrLn (Crypto.getHashBytesAsHex metaDataHash)
case mOutFile of
Nothing -> liftIO $ BS.putStrLn (Crypto.getHashBytesAsHex metaDataHash)
Just (OutputFile fpath) ->
handleIOExceptT (ShelleyPoolWriteMetaDataHashError fpath)
$ BS.writeFile fpath (Crypto.getHashBytesAsHex metaDataHash)
54 changes: 54 additions & 0 deletions cardano-cli/test/Test/Metadata.hs
@@ -0,0 +1,54 @@
{-# LANGUAGE OverloadedStrings #-}

module Test.Metadata
( tests
) where

import Cardano.Prelude

import Hedgehog (Property, (===))
import qualified Hedgehog as H

import Test.OptParse

golden_stakePoolMetadataHash :: Property
golden_stakePoolMetadataHash =
propertyOnce $ do
let stakePoolMetadataFp = "stake-pool-metadata.json"
outputStakePoolMetadataHashFp = "stake-pool-metadata-hash.txt"
allFiles = [stakePoolMetadataFp, outputStakePoolMetadataHashFp]

-- Write the example stake pool metadata to disk
liftIO $ writeFile stakePoolMetadataFp exampleStakePoolMetadata
assertFilesExist [stakePoolMetadataFp]

-- Hash the stake pool metadata
execCardanoCLIParser
allFiles
$ evalCardanoCLIParser [ "shelley","stake-pool","metadata-hash"
, "--pool-metadata-file", stakePoolMetadataFp
, "--out-file", outputStakePoolMetadataHashFp
]

-- Check for existence of the stake pool metadata hash file.
assertFilesExist [outputStakePoolMetadataHashFp]

-- Check that the stake pool metadata hash file content is correct.
expectedStakePoolMetadataHash <- liftIO $
readFile "test/Test/golden/shelley/stake_pool_metadata/stake_pool_metadata_hash"
actualStakePoolMetadataHash <- liftIO $ readFile outputStakePoolMetadataHashFp
expectedStakePoolMetadataHash === actualStakePoolMetadataHash

liftIO $ fileCleanup allFiles

H.success
where
exampleStakePoolMetadata :: Text
exampleStakePoolMetadata = "{\"homepage\":\"https://iohk.io\",\"name\":\"Genesis Pool C\",\"ticker\":\"GPC\",\"description\":\"Lorem Ipsum Dolor Sit Amet.\"}"

tests :: IO Bool
tests =
H.checkSequential
$ H.Group "Stake pool metadata"
[ ("golden_stakePoolMetadataHash", golden_stakePoolMetadataHash)
]
@@ -0,0 +1 @@
8241de08075886a7d09c847c9bbd1719459dac0bd0a2f085e673611ebb9a5965
4 changes: 3 additions & 1 deletion cardano-cli/test/cardano-cli-pioneers.hs
Expand Up @@ -6,11 +6,12 @@ import System.IO (BufferMode (..))
import qualified System.IO as IO

import qualified Test.CLI.Shelley.TextEnvelope.Golden.Tests
import qualified Test.ITN
import qualified Test.Metadata
import qualified Test.Pioneers.Exercise1
import qualified Test.Pioneers.Exercise2
import qualified Test.Pioneers.Exercise3
import qualified Test.Pioneers.Exercise4
import qualified Test.ITN

main :: IO ()
main = do
Expand All @@ -27,4 +28,5 @@ main = do
, Test.Pioneers.Exercise4.tests

, Test.ITN.tests
, Test.Metadata.tests
]

0 comments on commit de4b9ea

Please sign in to comment.