|
| 1 | +{-# LANGUAGE DerivingVia #-} |
| 2 | +{-# LANGUAGE NamedFieldPuns #-} |
| 3 | +{-# LANGUAGE NumericUnderscores #-} |
| 4 | +{-# LANGUAGE OverloadedStrings #-} |
| 5 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 6 | +{-# LANGUAGE TypeApplications #-} |
| 7 | + |
| 8 | +module Cardano.Testnet.Test.Gov.TransactionBuildWrongHash |
| 9 | + ( hprop_transaction_build_wrong_hash |
| 10 | + ) where |
| 11 | + |
| 12 | +import Cardano.Api as Api |
| 13 | + |
| 14 | +import Cardano.Testnet |
| 15 | + |
| 16 | +import Prelude |
| 17 | + |
| 18 | +import Control.Monad |
| 19 | +import Data.Default.Class |
| 20 | +import qualified Data.Text as Text |
| 21 | +import GHC.IO.Exception (ExitCode (ExitFailure)) |
| 22 | +import System.Directory (makeAbsolute) |
| 23 | +import System.FilePath ((</>)) |
| 24 | + |
| 25 | +import Test.Cardano.CLI.Hash (serveFilesWhile, tamperBase16Hash) |
| 26 | +import Testnet.Components.Query |
| 27 | +import Testnet.Process.Cli.Keys |
| 28 | +import Testnet.Process.Run (addEnvVarsToConfig, execCli', execCliAny, mkExecConfig) |
| 29 | +import Testnet.Property.Util (integrationRetryWorkspace) |
| 30 | +import Testnet.Start.Types |
| 31 | +import Testnet.Types |
| 32 | + |
| 33 | +import Hedgehog |
| 34 | +import qualified Hedgehog as H |
| 35 | +import qualified Hedgehog.Extras as H |
| 36 | + |
| 37 | +-- | Execute me with: |
| 38 | +-- @DISABLE_RETRIES=1 cabal test cardano-testnet-test --test-options '-p "/Transaction Build Wrong Hash/'@ |
| 39 | +hprop_transaction_build_wrong_hash :: Property |
| 40 | +hprop_transaction_build_wrong_hash = integrationRetryWorkspace 2 "wrong-hash" $ \tempAbsBasePath' -> H.runWithDefaultWatchdog_ $ do |
| 41 | + |
| 42 | + conf@Conf { tempAbsPath } <- H.noteShowM $ mkConf tempAbsBasePath' |
| 43 | + let tempAbsPath' = unTmpAbsPath tempAbsPath |
| 44 | + tempBaseAbsPath = makeTmpBaseAbsPath tempAbsPath |
| 45 | + |
| 46 | + work <- H.createDirectoryIfMissing $ tempAbsPath' </> "work" |
| 47 | + |
| 48 | + let ceo = ConwayEraOnwardsConway |
| 49 | + sbe = conwayEraOnwardsToShelleyBasedEra ceo |
| 50 | + asbe = AnyShelleyBasedEra sbe |
| 51 | + eraName = eraToString sbe |
| 52 | + fastTestnetOptions = def { cardanoNodeEra = asbe } |
| 53 | + shelleyOptions = def { genesisEpochLength = 200 } |
| 54 | + |
| 55 | + TestnetRuntime |
| 56 | + { testnetMagic |
| 57 | + , testnetNodes |
| 58 | + , wallets=wallet0:wallet1:_ |
| 59 | + , configurationFile |
| 60 | + } |
| 61 | + <- cardanoTestnetDefault fastTestnetOptions shelleyOptions conf |
| 62 | + |
| 63 | + node <- H.headM testnetNodes |
| 64 | + poolSprocket1 <- H.noteShow $ nodeSprocket node |
| 65 | + execConfig <- mkExecConfig tempBaseAbsPath poolSprocket1 testnetMagic |
| 66 | + let socketPath = nodeSocketPath node |
| 67 | + |
| 68 | + epochStateView <- getEpochStateView configurationFile socketPath |
| 69 | + |
| 70 | + H.note_ $ "Sprocket: " <> show poolSprocket1 |
| 71 | + H.note_ $ "Abs path: " <> tempAbsBasePath' |
| 72 | + H.note_ $ "Socketpath: " <> unFile socketPath |
| 73 | + H.note_ $ "Foldblocks config file: " <> unFile configurationFile |
| 74 | + |
| 75 | + gov <- H.createDirectoryIfMissing $ work </> "governance" |
| 76 | + |
| 77 | + let proposalAnchorDataIpfsHash = "QmexFJuEn5RtnHEqpxDcqrazdHPzAwe7zs2RxHLfMH5gBz" |
| 78 | + proposalAnchorFile <- H.noteM $ liftIO $ makeAbsolute $ "test" </> "cardano-testnet-test" </> "files" </> "sample-proposal-anchor" |
| 79 | + |
| 80 | + infoActionFp <- H.note $ work </> gov </> "info.action" |
| 81 | + |
| 82 | + proposalAnchorDataHash <- execCli' execConfig |
| 83 | + [ "hash", "anchor-data", "--file-binary", proposalAnchorFile |
| 84 | + ] |
| 85 | + |
| 86 | + let stakeVkeyFp = gov </> "stake.vkey" |
| 87 | + stakeSKeyFp = gov </> "stake.skey" |
| 88 | + stakeCertFp = gov </> "stake.regcert" |
| 89 | + stakeKeys = KeyPair { verificationKey = File stakeVkeyFp |
| 90 | + , signingKey = File stakeSKeyFp |
| 91 | + } |
| 92 | + |
| 93 | + cliStakeAddressKeyGen stakeKeys |
| 94 | + |
| 95 | + -- Register stake address |
| 96 | + |
| 97 | + void $ execCli' execConfig |
| 98 | + [ eraName, "stake-address", "registration-certificate" |
| 99 | + , "--stake-verification-key-file", stakeVkeyFp |
| 100 | + , "--key-reg-deposit-amt", show @Int 0 -- TODO: why this needs to be 0???? |
| 101 | + , "--out-file", stakeCertFp |
| 102 | + ] |
| 103 | + |
| 104 | + stakeCertTxBodyFp <- H.note $ work </> "stake.registration.txbody" |
| 105 | + stakeCertTxSignedFp <- H.note $ work </> "stake.registration.tx" |
| 106 | + |
| 107 | + txin1 <- findLargestUtxoForPaymentKey epochStateView sbe wallet0 |
| 108 | + |
| 109 | + void $ execCli' execConfig |
| 110 | + [ eraName, "transaction", "build" |
| 111 | + , "--change-address", Text.unpack $ paymentKeyInfoAddr wallet0 |
| 112 | + , "--tx-in", Text.unpack $ renderTxIn txin1 |
| 113 | + , "--tx-out", Text.unpack (paymentKeyInfoAddr wallet1) <> "+" <> show @Int 10_000_000 |
| 114 | + , "--certificate-file", stakeCertFp |
| 115 | + , "--witness-override", show @Int 2 |
| 116 | + , "--out-file", stakeCertTxBodyFp |
| 117 | + ] |
| 118 | + |
| 119 | + void $ execCli' execConfig |
| 120 | + [ eraName, "transaction", "sign" |
| 121 | + , "--tx-body-file", stakeCertTxBodyFp |
| 122 | + , "--signing-key-file", signingKeyFp $ paymentKeyInfoPair wallet0 |
| 123 | + , "--signing-key-file", stakeSKeyFp |
| 124 | + , "--out-file", stakeCertTxSignedFp |
| 125 | + ] |
| 126 | + |
| 127 | + void $ execCli' execConfig |
| 128 | + [ eraName, "transaction", "submit" |
| 129 | + , "--tx-file", stakeCertTxSignedFp |
| 130 | + ] |
| 131 | + |
| 132 | + let relativeUrl = ["ipfs", proposalAnchorDataIpfsHash] |
| 133 | + |
| 134 | + txbodyFp <- H.note $ work </> "tx.body" |
| 135 | + |
| 136 | + tamperedHash <- H.evalMaybe $ tamperBase16Hash proposalAnchorDataHash |
| 137 | + |
| 138 | + -- Create temporary HTTP server with files required by the call to `cardano-cli` |
| 139 | + -- In this case, the server emulates an IPFS gateway |
| 140 | + serveFilesWhile |
| 141 | + [(relativeUrl, proposalAnchorFile)] |
| 142 | + ( \port -> do |
| 143 | + let execConfig' = addEnvVarsToConfig execConfig [("IPFS_GATEWAY_URI", "http://localhost:" ++ show port ++ "/")] |
| 144 | + |
| 145 | + void $ |
| 146 | + execCli' |
| 147 | + execConfig' |
| 148 | + [ eraName, "governance", "action", "create-info" |
| 149 | + , "--testnet" |
| 150 | + , "--governance-action-deposit", show @Int 1_000_000 -- TODO: Get this from the node |
| 151 | + , "--deposit-return-stake-verification-key-file", stakeVkeyFp |
| 152 | + , "--anchor-url", "ipfs://" ++ proposalAnchorDataIpfsHash |
| 153 | + , "--anchor-data-hash", tamperedHash |
| 154 | + , "--out-file", infoActionFp |
| 155 | + ] |
| 156 | + |
| 157 | + txin2 <- findLargestUtxoForPaymentKey epochStateView sbe wallet1 |
| 158 | + |
| 159 | + (exitCode, _, stderrOutput) <- |
| 160 | + execCliAny |
| 161 | + execConfig' |
| 162 | + [ eraName, "transaction", "build" |
| 163 | + , "--change-address", Text.unpack $ paymentKeyInfoAddr wallet1 |
| 164 | + , "--tx-in", Text.unpack $ renderTxIn txin2 |
| 165 | + , "--tx-out", Text.unpack (paymentKeyInfoAddr wallet0) <> "+" <> show @Int 5_000_000 |
| 166 | + , "--proposal-file", infoActionFp |
| 167 | + , "--out-file", txbodyFp |
| 168 | + ] |
| 169 | + |
| 170 | + exitCode H.=== ExitFailure 1 |
| 171 | + |
| 172 | + H.note_ stderrOutput |
| 173 | + |
| 174 | + H.assert ("Hashes do not match!" `Text.isInfixOf` Text.pack stderrOutput) |
| 175 | + ) |
0 commit comments