Skip to content

Commit

Permalink
Add test to check transaction build fails with wrong hash
Browse files Browse the repository at this point in the history
  • Loading branch information
palas committed Oct 24, 2024
1 parent 0712366 commit fe9d98a
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 0 deletions.
1 change: 1 addition & 0 deletions cardano-testnet/cardano-testnet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ test-suite cardano-testnet-test
Cardano.Testnet.Test.Gov.ProposeNewConstitution
Cardano.Testnet.Test.Gov.ProposeNewConstitutionSPO
Cardano.Testnet.Test.Gov.GovActionTimeout
Cardano.Testnet.Test.Gov.TransactionBuildWrongHash
Cardano.Testnet.Test.Gov.TreasuryDonation
Cardano.Testnet.Test.Gov.TreasuryGrowth
Cardano.Testnet.Test.Gov.TreasuryWithdrawal
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module Cardano.Testnet.Test.Gov.TransactionBuildWrongHash
( hprop_transaction_build_wrong_hash
) where

import Cardano.Api as Api

import Cardano.Testnet

import Prelude

import Control.Monad
import Data.Default.Class
import qualified Data.Text as Text
import GHC.IO.Exception (ExitCode (ExitFailure))
import System.Directory (makeAbsolute)
import System.FilePath ((</>))

import Test.Cardano.CLI.Hash (serveFilesWhile, tamperBase16Hash)
import Testnet.Components.Query
import Testnet.Process.Cli.Keys
import Testnet.Process.Run (addEnvVarsToConfig, execCli', execCliAny, mkExecConfig)
import Testnet.Property.Util (integrationRetryWorkspace)
import Testnet.Start.Types
import Testnet.Types

import Hedgehog
import qualified Hedgehog as H
import qualified Hedgehog.Extras as H

-- | Execute me with:
-- @DISABLE_RETRIES=1 cabal test cardano-testnet-test --test-options '-p "/Transaction Build Wrong Hash/'@
hprop_transaction_build_wrong_hash :: Property
hprop_transaction_build_wrong_hash = integrationRetryWorkspace 2 "wrong-hash" $ \tempAbsBasePath' -> H.runWithDefaultWatchdog_ $ do

conf@Conf { tempAbsPath } <- H.noteShowM $ mkConf tempAbsBasePath'
let tempAbsPath' = unTmpAbsPath tempAbsPath
tempBaseAbsPath = makeTmpBaseAbsPath tempAbsPath

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

let ceo = ConwayEraOnwardsConway
sbe = conwayEraOnwardsToShelleyBasedEra ceo
asbe = AnyShelleyBasedEra sbe
eraName = eraToString sbe
fastTestnetOptions = def { cardanoNodeEra = asbe }
shelleyOptions = def { genesisEpochLength = 200 }

TestnetRuntime
{ testnetMagic
, testnetNodes
, wallets=wallet0:wallet1:_
, configurationFile
}
<- cardanoTestnetDefault fastTestnetOptions shelleyOptions conf

node <- H.headM testnetNodes
poolSprocket1 <- H.noteShow $ nodeSprocket node
execConfig <- mkExecConfig tempBaseAbsPath poolSprocket1 testnetMagic
let socketPath = nodeSocketPath node

epochStateView <- getEpochStateView configurationFile socketPath

H.note_ $ "Sprocket: " <> show poolSprocket1
H.note_ $ "Abs path: " <> tempAbsBasePath'
H.note_ $ "Socketpath: " <> unFile socketPath
H.note_ $ "Foldblocks config file: " <> unFile configurationFile

gov <- H.createDirectoryIfMissing $ work </> "governance"

let proposalAnchorDataIpfsHash = "QmexFJuEn5RtnHEqpxDcqrazdHPzAwe7zs2RxHLfMH5gBz"
proposalAnchorFile <- H.noteM $ liftIO $ makeAbsolute $ "test" </> "cardano-testnet-test" </> "files" </> "sample-proposal-anchor"

infoActionFp <- H.note $ work </> gov </> "info.action"

proposalAnchorDataHash <- execCli' execConfig
[ "hash", "anchor-data", "--file-binary", proposalAnchorFile
]

let stakeVkeyFp = gov </> "stake.vkey"
stakeSKeyFp = gov </> "stake.skey"
stakeCertFp = gov </> "stake.regcert"
stakeKeys = KeyPair { verificationKey = File stakeVkeyFp
, signingKey = File stakeSKeyFp
}

cliStakeAddressKeyGen stakeKeys

-- Register stake address

void $ execCli' execConfig
[ eraName, "stake-address", "registration-certificate"
, "--stake-verification-key-file", stakeVkeyFp
, "--key-reg-deposit-amt", show @Int 0 -- TODO: why this needs to be 0????
, "--out-file", stakeCertFp
]

stakeCertTxBodyFp <- H.note $ work </> "stake.registration.txbody"
stakeCertTxSignedFp <- H.note $ work </> "stake.registration.tx"

txin1 <- findLargestUtxoForPaymentKey epochStateView sbe wallet0

void $ execCli' execConfig
[ eraName, "transaction", "build"
, "--change-address", Text.unpack $ paymentKeyInfoAddr wallet0
, "--tx-in", Text.unpack $ renderTxIn txin1
, "--tx-out", Text.unpack (paymentKeyInfoAddr wallet1) <> "+" <> show @Int 10_000_000
, "--certificate-file", stakeCertFp
, "--witness-override", show @Int 2
, "--out-file", stakeCertTxBodyFp
]

void $ execCli' execConfig
[ eraName, "transaction", "sign"
, "--tx-body-file", stakeCertTxBodyFp
, "--signing-key-file", signingKeyFp $ paymentKeyInfoPair wallet0
, "--signing-key-file", stakeSKeyFp
, "--out-file", stakeCertTxSignedFp
]

void $ execCli' execConfig
[ eraName, "transaction", "submit"
, "--tx-file", stakeCertTxSignedFp
]

let relativeUrl = ["ipfs", proposalAnchorDataIpfsHash]

txbodyFp <- H.note $ work </> "tx.body"

tamperedHash <- H.evalMaybe $ tamperBase16Hash proposalAnchorDataHash

-- Create temporary HTTP server with files required by the call to `cardano-cli`
-- In this case, the server emulates an IPFS gateway
serveFilesWhile
[(relativeUrl, proposalAnchorFile)]
( \port -> do
let execConfig' = addEnvVarsToConfig execConfig [("IPFS_GATEWAY_URI", "http://localhost:" ++ show port ++ "/")]

void $
execCli'
execConfig'
[ eraName, "governance", "action", "create-info"
, "--testnet"
, "--governance-action-deposit", show @Int 1_000_000 -- TODO: Get this from the node
, "--deposit-return-stake-verification-key-file", stakeVkeyFp
, "--anchor-url", "ipfs://" ++ proposalAnchorDataIpfsHash
, "--anchor-data-hash", tamperedHash
, "--out-file", infoActionFp
]

txin2 <- findLargestUtxoForPaymentKey epochStateView sbe wallet1

(exitCode, _, stderrOutput) <-
execCliAny
execConfig'
[ eraName, "transaction", "build"
, "--change-address", Text.unpack $ paymentKeyInfoAddr wallet1
, "--tx-in", Text.unpack $ renderTxIn txin2
, "--tx-out", Text.unpack (paymentKeyInfoAddr wallet0) <> "+" <> show @Int 5_000_000
, "--proposal-file", infoActionFp
, "--out-file", txbodyFp
]

exitCode H.=== ExitFailure 1

H.note_ stderrOutput

H.assert ("Hashes do not match!" `Text.isInfixOf` Text.pack stderrOutput)
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import qualified Cardano.Testnet.Test.Gov.GovActionTimeout as Gov
import qualified Cardano.Testnet.Test.Gov.InfoAction as LedgerEvents
import qualified Cardano.Testnet.Test.Gov.PParamChangeFailsSPO as Gov
import qualified Cardano.Testnet.Test.Gov.ProposeNewConstitution as Gov
import qualified Cardano.Testnet.Test.Gov.TransactionBuildWrongHash as WrongHash
import qualified Cardano.Testnet.Test.Gov.TreasuryDonation as Gov
import qualified Cardano.Testnet.Test.Gov.TreasuryWithdrawal as Gov
import qualified Cardano.Testnet.Test.Node.Shutdown
Expand Down Expand Up @@ -67,6 +68,7 @@ tests = do
, ignoreOnMacAndWindows "Treasury Withdrawal" Gov.hprop_ledger_events_treasury_withdrawal
, ignoreOnWindows "PParam change fails for SPO" Gov.hprop_check_pparam_fails_spo
, ignoreOnWindows "InfoAction" LedgerEvents.hprop_ledger_events_info_action
, ignoreOnWindows "Transaction Build Wrong Hash" WrongHash.hprop_transaction_build_wrong_hash
]
, T.testGroup "Plutus"
[ ignoreOnWindows "PlutusV3" Cardano.Testnet.Test.Cli.Conway.Plutus.hprop_plutus_v3]
Expand Down

0 comments on commit fe9d98a

Please sign in to comment.