Skip to content

Commit

Permalink
Do not allow submitting transactions older than current node era
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Oct 10, 2023
1 parent 8946a00 commit 667b925
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ library
Cardano.CLI.Types.Errors.ItnKeyConversionError
Cardano.CLI.Types.Errors.KeyCmdError
Cardano.CLI.Types.Errors.NodeCmdError
Cardano.CLI.Types.Errors.NodeEraMismatchError
Cardano.CLI.Types.Errors.ProtocolParamsError
Cardano.CLI.Types.Errors.QueryCmdError
Cardano.CLI.Types.Errors.QueryCmdLocalStateQueryError
Expand Down
13 changes: 6 additions & 7 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Cardano.CLI.Json.Friendly (friendlyTxBodyJson, friendlyTxBodyYa
import Cardano.CLI.Read
import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Errors.BootstrapWitnessError
import Cardano.CLI.Types.Errors.NodeEraMismatchError
import Cardano.CLI.Types.Errors.TxCmdError
import Cardano.CLI.Types.Errors.TxValidationError
import Cardano.CLI.Types.Governance
Expand Down Expand Up @@ -150,7 +151,7 @@ runTxBuildCmd
era socketPath consensusModeParams@(AnyConsensusModeParams cModeParams) nid
mScriptValidity mOverrideWits txins readOnlyRefIns reqSigners txinsc mReturnColl mTotCollateral txouts
changeAddr mValue mLowBound mUpperBound certs wdrls metadataSchema scriptFiles metadataFiles mUpProp
conwayVotes newProposals outputOptions = do
conwayVotes newProposals outputOptions = cardanoEraConstraints era $ do

-- The user can specify an era prior to the era that the node is currently in.
-- We cannot use the user specified era to construct a query against a node because it may differ
Expand Down Expand Up @@ -242,15 +243,13 @@ runTxBuildCmd
& onLeft (left . TxCmdQueryConvenienceError . AcqFailure)
& onLeft (left . TxCmdQueryConvenienceError . QceUnsupportedNtcVersion)

(nodeEraUTxO, _, eraHistory, systemStart, _, _, _) <-
(txEraUtxo, _, eraHistory, systemStart, _, _, _) <-
lift (executeLocalStateQueryExpr localNodeConnInfo Nothing (queryStateForBalancedTx nodeEra allTxInputs []))
& onLeft (left . TxCmdQueryConvenienceError . AcqFailure)
& onLeft (left . TxCmdQueryConvenienceError)

-- Why do we cast the era? The user can specify an era prior to the era that the node is currently in.
-- We cannot use the user specified era to construct a query against a node because it may differ
-- from the node's era and this will result in the 'QueryEraMismatch' failure.
txEraUtxo <- cardanoEraConstraints era $ pure (eraCast era nodeEraUTxO) & onLeft (left . TxCmdTxEraCastErr)
Refl <- testEquality era nodeEra
& hoistMaybe (TxCmdTxNodeEraMismatchError $ NodeEraMismatchError era nodeEra)

scriptExecUnitsMap <-
firstExceptT TxCmdTxExecUnitsErr $ hoistEither
Expand Down Expand Up @@ -559,7 +558,7 @@ runTxBuild
& onLeft (left . TxCmdQueryConvenienceError . QceUnsupportedNtcVersion)

Refl <- testEquality era nodeEra
& hoistMaybe (TxCmdTxEraCastErr $ EraCastError ("nodeEra" :: Text) era nodeEra)
& hoistMaybe (TxCmdTxNodeEraMismatchError $ NodeEraMismatchError era nodeEra)

let certs =
case validatedTxCerts of
Expand Down
13 changes: 13 additions & 0 deletions cardano-cli/src/Cardano/CLI/Types/Errors/NodeEraMismatchError.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{-# LANGUAGE GADTs #-}

module Cardano.CLI.Types.Errors.NodeEraMismatchError
( NodeEraMismatchError(..)
) where

import Cardano.Api

data NodeEraMismatchError = forall era nodeEra.
NodeEraMismatchError
{ era :: !(CardanoEra era)
, nodeEra :: !(CardanoEra nodeEra)
}
12 changes: 8 additions & 4 deletions cardano-cli/src/Cardano/CLI/Types/Errors/TxCmdError.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Cardano.Api.Shelley
import Cardano.CLI.Read
import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Errors.BootstrapWitnessError
import Cardano.CLI.Types.Errors.NodeEraMismatchError
import Cardano.CLI.Types.Errors.ProtocolParamsError
import Cardano.CLI.Types.Errors.TxValidationError
import Cardano.CLI.Types.Output
Expand Down Expand Up @@ -68,7 +69,7 @@ data TxCmdError
| TxCmdPParamExecutionUnitsNotAvailable
| TxCmdPlutusScriptsRequireCardanoMode
| TxCmdProtocolParametersNotPresentInTxBody
| TxCmdTxEraCastErr EraCastError
| TxCmdTxNodeEraMismatchError !NodeEraMismatchError
| TxCmdQueryConvenienceError !QueryConvenienceError
| TxCmdQueryNotScriptLocked !ScriptLockedTxInsError
| TxCmdScriptDataError !ScriptDataError
Expand Down Expand Up @@ -167,9 +168,12 @@ renderTxCmdError err =
[ "Execution units not available in the protocol parameters. This is "
, "likely due to not being in the Alonzo era"
]
TxCmdTxEraCastErr (EraCastError value fromEra toEra) ->
"Transactions can only be produced in the same era as the node. Mismatched eras of "
<> textShow value <> ". Requested era: " <> renderEra (AnyCardanoEra toEra) <> ", node era: " <> renderEra (AnyCardanoEra fromEra) <> "."
TxCmdTxNodeEraMismatchError (NodeEraMismatchError nodeEra valueEra) ->
cardanoEraConstraints nodeEra $ cardanoEraConstraints valueEra $ mconcat
[ "Transactions can only be produced in the same era as the node. Requested era: "
, renderEra (AnyCardanoEra valueEra) <> ", node era: "
, renderEra (AnyCardanoEra nodeEra) <> "."
]
TxCmdQueryConvenienceError e ->
renderQueryConvenienceError e
TxCmdQueryNotScriptLocked e ->
Expand Down

0 comments on commit 667b925

Please sign in to comment.