Skip to content

Commit

Permalink
Add test: compatible shelley transaction signed-transaction results i…
Browse files Browse the repository at this point in the history
…n different witnesses than legacy command
  • Loading branch information
carbolymer committed Feb 10, 2025
1 parent 1bc8d05 commit 2200a91
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 25 deletions.
23 changes: 10 additions & 13 deletions cardano-cli/src/Cardano/CLI/Compatible/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}

-- TODO split this into separate modules
module Cardano.CLI.Compatible.Transaction
( CompatibleTransactionCmds (..)
, CompatibleTransactionError (..)
Expand Down Expand Up @@ -293,8 +294,8 @@ runCompatibleTransactionCmd

proposalsRefInputs =
[ refInput
| ProposalProcedures _ (TxProposalProcedures _ (BuildTxWith proposalMap)) <- [protocolUpdates]
, sWit <- Map.elems proposalMap
| ProposalProcedures _ (TxProposalProcedures proposalMap) <- [protocolUpdates]
, BuildTxWith (Just sWit) <- toList proposalMap
, refInput <- maybeToList $ getScriptWitnessReferenceInput sWit
]

Expand All @@ -303,24 +304,20 @@ runCompatibleTransactionCmd
certsRefInputs <> votesRefInputs <> proposalsRefInputs
let txCerts = mkTxCertificates sbe certsAndMaybeScriptWits

-- this body is only for witnesses
apiTxBody <-
firstExceptT CompatibleTxBodyError $
hoistEither $
createTransactionBody sbe $
defaultTxBodyContent sbe
& setTxIns (map (,BuildTxWith (KeyWitness KeyWitnessForSpending)) ins)
& setTxOuts allOuts
& setTxFee (TxFeeExplicit sbe fee)
& setTxCertificates txCerts
& setTxInsReference validatedRefInputs
-- TODO avoid creating body twice
signedTx <-
firstExceptT CompatiblePParamsConversionError . hoistEither $
createCompatibleSignedTx sbe ins allOuts [] fee protocolUpdates votes txCerts

let apiTxBody = getTxBody signedTx

let (sksByron, sksShelley) = partitionSomeWitnesses $ map categoriseSomeSigningWitness sks

byronWitnesses <-
firstExceptT CompatibleBootstrapWitnessError . hoistEither $
mkShelleyBootstrapWitnesses sbe mNetworkId apiTxBody sksByron

-- TODO this body contains witnesses, so if
let newShelleyKeyWits = map (makeShelleyKeyWitness sbe apiTxBody) sksShelley
allKeyWits = newShelleyKeyWits ++ byronWitnesses

Expand Down
6 changes: 3 additions & 3 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1422,9 +1422,9 @@ runTransactionSignCmd
runTransactionSignCmd
Cmd.TransactionSignCmdArgs
{ txOrTxBodyFile = txOrTxBody
, witnessSigningData = witnessSigningData
, mNetworkId = mNetworkId
, outTxFile = outTxFile
, witnessSigningData
, mNetworkId
, outTxFile
} = do
sks <- forM witnessSigningData $ \d ->
lift (readWitnessSigningData d)
Expand Down
3 changes: 0 additions & 3 deletions cardano-cli/src/Cardano/CLI/Orphans.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,3 @@ instance ToJSON HashableScriptData where
[ "hash" .= hashScriptDataBytes hsd
, "json" .= scriptDataToJsonDetailedSchema hsd
]

-- TODO move LocalNodeConnectInfo instances to cardano-api
deriving instance Show LocalNodeConnectInfo
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,26 @@ import Control.Monad.IO.Class
import Data.Aeson (Value)
import qualified Data.Aeson as A
import Data.Char (toLower)
import Data.Functor
import Data.String (IsString (..))
import GHC.Stack

import Test.Cardano.CLI.Util

import Hedgehog
import qualified Hedgehog as H
import qualified Hedgehog.Extras as H
import qualified Hedgehog.Extras.Test.Golden as H

inputDir :: FilePath
inputDir = "test/cardano-cli-test/files/input/shelley/transaction"
inputDir = "test/cardano-cli-test/files/input/"

-- | Execute me with:
-- @cabal test cardano-cli-test --test-options '-p "/conway transaction build one voter many votes/"'@
hprop_compatible_conway_transaction_build_one_voter_many_votes :: Property
hprop_compatible_conway_transaction_build_one_voter_many_votes = propertyOnce $ H.moduleWorkspace "tmp" $ \tempDir -> do
refOutFile <- H.noteTempFile tempDir "reference_tx.traw"
outFile <- H.noteTempFile tempDir "tx.traw"
refOutFile <- H.noteTempFile tempDir "reference_tx.txbody.json"
outFile <- H.noteTempFile tempDir "txbody.tx.json"
let eraName = map toLower . docToString $ pretty ConwayEra

let args =
Expand All @@ -37,9 +40,9 @@ hprop_compatible_conway_transaction_build_one_voter_many_votes = propertyOnce $
, "--fee"
, "178569"
, "--certificate-file"
, "test/cardano-cli-test/files/input/certificate/stake-address-registration.json"
, inputDir <> "certificate/stake-address-registration.json"
, "--certificate-script-file"
, "test/cardano-cli-test/files/input/plutus/v1-always-succeeds.plutus"
, inputDir <> "plutus/v1-always-succeeds.plutus"
, "--certificate-redeemer-value"
, "0"
, "--certificate-execution-units"
Expand Down Expand Up @@ -73,6 +76,162 @@ hprop_compatible_conway_transaction_build_one_voter_many_votes = propertyOnce $

assertTxFilesEqual refOutFile outFile

hprop_compatible_shelley_create_update_proposal :: Property
hprop_compatible_shelley_create_update_proposal = propertyOnce $ H.moduleWorkspace "tmp" $ \tempDir -> do
refOutFile <- H.noteTempFile tempDir "ref_update-proposal_allegra.proposal"
outFile <- H.noteTempFile tempDir "update_proposal_allegra.proposal"
let eraName = map toLower . docToString $ pretty ShelleyEra

let args =
[ "--epoch"
, "1"
, "--genesis-verification-key-file"
, inputDir <> "genesis1.vkey"
, "--protocol-major-version"
, "3"
, "--protocol-minor-version"
, "0"
]

-- reference transaction
_ <-
execCardanoCLI $
[ "legacy"
, "governance"
, "create-update-proposal"
]
<> args
<> [ "--out-file"
, refOutFile
]

-- tested compatible transaction
_ <-
execCardanoCLI $
[ "compatible"
, eraName
, "governance"
, "action"
, "create-protocol-parameters-update"
]
<> args
<> [ "--out-file"
, outFile
]

H.diffFileVsGoldenFile outFile refOutFile

hprop_compatible_shelley_transaction :: Property
hprop_compatible_shelley_transaction = propertyOnce $ H.moduleWorkspace "tmp" $ \tempDir -> do
refOutFile <- H.noteTempFile tempDir "reference_tx.tx.json"
outFile <- H.noteTempFile tempDir "tx.tx.json"
let eraName = map toLower . docToString $ pretty ShelleyEra

let args =
[ "--fee"
, "5000000"
, "--tx-in"
, "596e9836a4f42661d66deb7993e4e5da310b688e85facc50fee2462e611a0c94#0"
, "--tx-out"
, "2657WMsDfac7RXyZU5nkYxPvZAh7u96FN4cp6w6581zJUR4vKUr3kofjd8MuFghFS+35999999995000000"
, "--update-proposal-file"
, inputDir <> "update-proposal-allegra.proposal"
]

-- reference transaction
void . execCardanoCLI $
[ eraName
, "transaction"
, "build-raw"
]
<> args
<> [ "--out-file"
, refOutFile
]

-- tested compatible transaction
void . execCardanoCLI $
[ "compatible"
, eraName
, "transaction"
, "signed-transaction"
]
<> args
<> [ "--out-file"
, outFile
]

assertTxFilesEqual refOutFile outFile

hprop_compatible_shelley_signed_transaction :: Property
hprop_compatible_shelley_signed_transaction = propertyOnce $ H.moduleWorkspace "tmp" $ \tempDir -> do
refOutFile <- H.noteTempFile tempDir "reference_tx.tx.json"
refTxBody <- H.noteTempFile tempDir "reference_tx.txbody.json"
outFile <- H.noteTempFile tempDir "tx.tx.json"
let eraName = map toLower . docToString $ pretty ShelleyEra

let args =
[ "--fee"
, "5000000"
, "--tx-in"
, "596e9836a4f42661d66deb7993e4e5da310b688e85facc50fee2462e611a0c94#0"
, "--tx-out"
, "2657WMsDfac7RXyZU5nkYxPvZAh7u96FN4cp6w6581zJUR4vKUr3kofjd8MuFghFS+35999999995000000"
, "--update-proposal-file"
, inputDir <> "update-proposal-allegra.proposal"
]
signArgs =
[ "--signing-key-file"
, inputDir <> "delegate1.skey"
, "--signing-key-file"
, inputDir <> "genesis1.skey"
, "--signing-key-file"
, inputDir <> "payment-keys.000-converted.skey"
, "--testnet-magic"
, "42"
]

-- reference transaction
void . execCardanoCLI $
[ eraName
, "transaction"
, "build-raw"
]
<> args
<> [ "--out-file"
, refTxBody
]

-- sign reference transaction
void . execCardanoCLI $
[ eraName
, "transaction"
, "sign"
]
<> signArgs
<> [ "--tx-body-file"
, refTxBody
, "--out-file"
, refOutFile
]

-- tested compatible transaction
void . execCardanoCLI $
[ "compatible"
, eraName
, "transaction"
, "signed-transaction"
]
<> args
<> signArgs
<> [ "--out-file"
, outFile
]

assertTxFilesEqual refOutFile outFile

H.failure

assertTxFilesEqual
:: forall m
. (HasCallStack, MonadIO m, MonadTest m, MonadCatch m)
Expand All @@ -97,6 +256,6 @@ assertTxFilesEqual f1 f2 = withFrozenCallStack $ do
[ "debug"
, "transaction"
, "view"
, "--tx-body-file"
, "--tx-file"
, f
]
5 changes: 5 additions & 0 deletions cardano-cli/test/cardano-cli-test/files/input/delegate1.skey
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "GenesisDelegateSigningKey_ed25519",
"description": "Genesis delegate operator key",
"cborHex": "5820423d671214097da94e309969f6847bd9d3134b8f45cee3c3d77a175f67a7e2ab"
}
5 changes: 5 additions & 0 deletions cardano-cli/test/cardano-cli-test/files/input/genesis1.skey
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "GenesisSigningKey_ed25519",
"description": "Genesis Signing Key",
"cborHex": "5820e657de1f4f98d87c64e2eedef8b2b590342b8f16d77ab043faec2cfb16420a50"
}
5 changes: 5 additions & 0 deletions cardano-cli/test/cardano-cli-test/files/input/genesis1.vkey
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "GenesisVerificationKey_ed25519",
"description": "Genesis Verification Key",
"cborHex": "5820da706def2349274e5ccaac07b7ab4d8aa807ef22a3971a6775a65b6cfd4717f7"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "PaymentSigningKeyByron_ed25519_bip32",
"description": "",
"cborHex": "588070806d8da0a77c8a1cd139bddf3d52c361c4c9d554433ca04491d8f69ef6ef4856a061841d5ca8e0aab6aac6eed0e004a65adc21f1df36836fb8d02a60784eee294e3192b8ecdd9b8d3a74c182171accaffa4e059b31be6a781aab34d0c912e26cec5da9274e8b6a970f740a30d571d66d2b28a57c7266294b663d72e4c7fc0f"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "Witnessed Tx ShelleyEra",
"description": "Ledger Cddl Format",
"cborHex": "83a50081825820596e9836a4f42661d66deb7993e4e5da310b688e85facc50fee2462e611a0c9400018182582f82d818582583581cdcfd403bb421d9b9c910e1e2b59326a14f45c97aa519daf1ee07e1eda10242182a001a58090edd1b007fe5cf2b9db4c0021a004c4b40031bffffffffffffffff0682a1581c570604ad450a68074e535f8b7d839151c013fd3e6c4eb68018c8b11aa10e82030001a200828258206535cb2b2f145da7f69ac7d7e3488d3b5b76320e400fc863ddaa5815f5eae497584053066e578a8afe78b51b55aa26f16ba4132ba95ae2a3c56f20273f414002c7a2c9b7d3a749ad7f385f5768699b7f61a57b176d87084111ad852cf6fefae7b002825820fbf4ed8c2aab6438216de9d90fac47589668a88297ca74660551e70d469ffaf358409e59ab82aebfff125559f17b614657a6d7abac51e85c8d410fce68be364d5c59ee8959979468ec1de0586d0ddb979bf3430830cbcd9f87b3b9b9e698a5ca280e0281845820294e3192b8ecdd9b8d3a74c182171accaffa4e059b31be6a781aab34d0c912e2584021325e4c2eab3b8d6ff8adfef5c2f288e82f691902ab7bf42dee957ef0888f345c8263e84c99eb255333e8e6d72ec06656fc1f9500e40e9b904575a94b7f9e0358206cec5da9274e8b6a970f740a30d571d66d2b28a57c7266294b663d72e4c7fc0f45a10242182af6"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "UpdateProposalShelley",
"description": "",
"cborHex": "82a1581c29a791a82b48398c90acedc363c1588590d787e94c32fa82db89d681981a8182030080808080808080808080808080808080a08080808080808001"
}

0 comments on commit 2200a91

Please sign in to comment.