Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove use of ScriptWitnessFiles in spending scripts #1025

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ index-state:
packages:
cardano-cli

program-options
ghc-options: -Werror
-- program-options
-- ghc-options: -Werror

package cryptonite
-- Using RDRAND instead of /dev/urandom as an entropy source for key
Expand Down
2 changes: 2 additions & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ library
Cardano.CLI.EraBased.Run.Transaction
Cardano.CLI.EraBased.Script.Mint.Read
Cardano.CLI.EraBased.Script.Mint.Types
Cardano.CLI.EraBased.Script.Spend.Read
Cardano.CLI.EraBased.Script.Spend.Types
Cardano.CLI.EraBased.Transaction.HashCheck
Cardano.CLI.Helpers
Cardano.CLI.IO.Lazy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import Cardano.Api.Ledger (Coin)
import Cardano.Api.Shelley

import Cardano.CLI.EraBased.Script.Mint.Types
import qualified Cardano.CLI.EraBased.Script.Spend.Types as PlutusSpend
import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Governance

Expand All @@ -49,7 +50,7 @@ data TransactionBuildRawCmdArgs era = TransactionBuildRawCmdArgs
{ eon :: !(ShelleyBasedEra era)
, mScriptValidity :: !(Maybe ScriptValidity)
-- ^ Mark script as expected to pass or fail validation
, txIns :: ![(TxIn, Maybe (ScriptWitnessFiles WitCtxTxIn))]
, txIns :: ![(TxIn, Maybe PlutusSpend.CliSpendScriptRequirements)]
-- ^ Transaction inputs with optional spending scripts
, readOnlyRefIns :: ![TxIn]
-- ^ Read only reference inputs
Expand Down Expand Up @@ -96,7 +97,7 @@ data TransactionBuildCmdArgs era = TransactionBuildCmdArgs
-- ^ Mark script as expected to pass or fail validation
, mOverrideWitnesses :: !(Maybe Word)
-- ^ Override the required number of tx witnesses
, txins :: ![(TxIn, Maybe (ScriptWitnessFiles WitCtxTxIn))]
, txins :: ![(TxIn, Maybe PlutusSpend.CliSpendScriptRequirements)]
-- ^ Transaction inputs with optional spending scripts
, readOnlyReferenceInputs :: ![TxIn]
-- ^ Read only reference inputs
Expand Down Expand Up @@ -144,7 +145,7 @@ data TransactionBuildEstimateCmdArgs era = TransactionBuildEstimateCmdArgs
, mByronWitnesses :: !(Maybe Int)
, protocolParamsFile :: !ProtocolParamsFile
, totalUTxOValue :: !Value
, txins :: ![(TxIn, Maybe (ScriptWitnessFiles WitCtxTxIn))]
, txins :: ![(TxIn, Maybe PlutusSpend.CliSpendScriptRequirements)]
-- ^ Transaction inputs with optional spending scripts
, readOnlyReferenceInputs :: ![TxIn]
-- ^ Read only reference inputs
Expand Down
137 changes: 108 additions & 29 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import Cardano.Api
import qualified Cardano.Api.Ledger as L
import qualified Cardano.Api.Network as Consensus
import Cardano.CLI.EraBased.Script.Spend.Types (CliSpendScriptRequirements)
import qualified Cardano.CLI.EraBased.Script.Spend.Types as PlutusSpend
import Cardano.Api.Shelley

import Cardano.CLI.Environment (EnvCli (..), envCliAnyEon)
Expand Down Expand Up @@ -1029,6 +1031,33 @@
)
)



pPlutusSpendingScriptWitness
:: ShelleyBasedEra era
-> BalanceTxExecUnits
-- ^ Use the @execution-units@ flag.
-> String
-- ^ Script flag prefix
-> Maybe String
-> String
-> Parser CliSpendScriptRequirements
pPlutusSpendingScriptWitness sbe autoBalanceExecUnits scriptFlagPrefix scriptFlagPrefixDeprecated help =
PlutusSpend.createPlutusScriptFromCliArgs
<$> pScriptFor
(scriptFlagPrefix ++ "-script-file")
((++ "-script-file") <$> scriptFlagPrefixDeprecated)
("The file containing the script to witness " ++ help)
<*>
( optional ((,,)
<$> pScriptDatumOrFileSpendingCip69 sbe scriptFlagPrefix
<*> pScriptRedeemerOrFile scriptFlagPrefix
<*> ( case autoBalanceExecUnits of
AutoBalance -> pure (ExecutionUnits 0 0)
ManualBalance -> pExecutionUnits scriptFlagPrefix
))
)
Comment on lines +1052 to +1059

Check notice

Code scanning / HLint

Redundant bracket Note

cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs:(1052,10)-(1059,10): Suggestion: Redundant bracket
  
Found:
  PlutusSpend.createPlutusScriptFromCliArgs
    <$>
      pScriptFor
        (scriptFlagPrefix ++ "-script-file")
        ((++ "-script-file") <$> scriptFlagPrefixDeprecated)
        ("The file containing the script to witness " ++ help)
    <>
      (optional
         ((,,) <$> pScriptDatumOrFileSpendingCip69 sbe scriptFlagPrefix
            <> pScriptRedeemerOrFile scriptFlagPrefix
            <>
              (case autoBalanceExecUnits of
                 AutoBalance -> pure (ExecutionUnits 0 0)
                 ManualBalance -> pExecutionUnits scriptFlagPrefix)))
  
Perhaps:
  PlutusSpend.createPlutusScriptFromCliArgs
    <$>
      pScriptFor
        (scriptFlagPrefix ++ "-script-file")
        ((++ "-script-file") <$> scriptFlagPrefixDeprecated)
        ("The file containing the script to witness " ++ help)
    <>
      optional
        ((,,) <$> pScriptDatumOrFileSpendingCip69 sbe scriptFlagPrefix
           <> pScriptRedeemerOrFile scriptFlagPrefix
           <>
             (case autoBalanceExecUnits of
                AutoBalance -> pure (ExecutionUnits 0 0)
                ManualBalance -> pExecutionUnits scriptFlagPrefix))

pScriptWitnessFiles
:: forall witctx era
. ShelleyBasedEra era
Expand All @@ -1048,20 +1077,14 @@
("The file containing the script to witness " ++ help)
<*> optional
( (,,)
<$> cip69Modification sbe
<$> cip69Modification sbe witctx scriptFlagPrefix
<*> pScriptRedeemerOrFile scriptFlagPrefix
<*> ( case autoBalanceExecUnits of
AutoBalance -> pure (ExecutionUnits 0 0)
ManualBalance -> pExecutionUnits scriptFlagPrefix
)
)
where
cip69Modification :: ShelleyBasedEra era -> Parser (ScriptDatumOrFile witctx)
cip69Modification =
caseShelleyToBabbageOrConwayEraOnwards
(const $ pScriptDatumOrFile scriptFlagPrefix witctx)
(const $ pScriptDatumOrFileCip69 scriptFlagPrefix witctx)

toScriptWitnessFiles
:: ScriptFile
-> Maybe
Expand All @@ -1073,6 +1096,14 @@
toScriptWitnessFiles sf Nothing = SimpleScriptWitnessFile sf
toScriptWitnessFiles sf (Just (d, r, e)) = PlutusScriptWitnessFiles sf d r e


cip69Modification :: ShelleyBasedEra era -> WitCtx witctx -> String -> Parser (ScriptDatumOrFile witctx)
cip69Modification sbe witctx scriptFlagPrefix =
caseShelleyToBabbageOrConwayEraOnwards
(const $ pScriptDatumOrFile scriptFlagPrefix witctx)
(const $ pScriptDatumOrFileCip69 scriptFlagPrefix witctx)
sbe

pExecutionUnits :: String -> Parser ExecutionUnits
pExecutionUnits scriptFlagPrefix =
fmap (uncurry ExecutionUnits) $
Expand Down Expand Up @@ -1138,6 +1169,56 @@
, Opt.help "Inline datum present at transaction input."
]

pScriptDatumOrFileSpendingNotOptional :: String -> Parser PlutusSpend.ScriptDatumOrFileSpending
pScriptDatumOrFileSpendingNotOptional scriptFlagPrefix =
asum
[ PlutusSpend.PotentialDatum . Just <$> (pScriptDataOrFile
(scriptFlagPrefix ++ "-datum")
"The script datum."
"The script datum file.")
Comment on lines +1175 to +1178

Check notice

Code scanning / HLint

Redundant bracket Note

cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs:(1175,49)-(1178,39): Suggestion: Redundant bracket
  
Found:
  PlutusSpend.PotentialDatum . Just
    <$>
      (pScriptDataOrFile
         (scriptFlagPrefix ++ "-datum") "The script datum."
         "The script datum file.")
  
Perhaps:
  PlutusSpend.PotentialDatum . Just
    <$>
      pScriptDataOrFile
        (scriptFlagPrefix ++ "-datum") "The script datum."
        "The script datum file."
, pInlineDatumPresent
]
where
pInlineDatumPresent :: Parser PlutusSpend.ScriptDatumOrFileSpending
pInlineDatumPresent =
flag' PlutusSpend.InlineDatum $
mconcat
[ long (scriptFlagPrefix ++ "-inline-datum-present")
, Opt.help "Inline datum present at transaction input."
]

pScriptDatumOrFileSpendingCip69 :: ShelleyBasedEra era -> String -> Parser PlutusSpend.ScriptDatumOrFileSpending
pScriptDatumOrFileSpendingCip69 sbe scriptFlagPrefix =
caseShelleyToBabbageOrConwayEraOnwards
(const $ datumMandatory)

Check notice

Code scanning / HLint

Redundant $ Note

cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs:1193:12: Suggestion: Redundant $
  
Found:
  const $ datumMandatory
  
Perhaps:
  const datumMandatory
(const $ datumOptional)

Check notice

Code scanning / HLint

Redundant $ Note

cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs:1194:12: Suggestion: Redundant $
  
Found:
  const $ datumOptional
  
Perhaps:
  const datumOptional
sbe
where
datumMandatory =
asum
[ PlutusSpend.PotentialDatum . Just <$> (pScriptDataOrFile
(scriptFlagPrefix ++ "-datum")
"The script datum."
"The script datum file.")
Comment on lines +1199 to +1202

Check notice

Code scanning / HLint

Redundant bracket Note

cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs:(1199,49)-(1202,39): Suggestion: Redundant bracket
  
Found:
  PlutusSpend.PotentialDatum . Just
    <$>
      (pScriptDataOrFile
         (scriptFlagPrefix ++ "-datum") "The script datum."
         "The script datum file.")
  
Perhaps:
  PlutusSpend.PotentialDatum . Just
    <$>
      pScriptDataOrFile
        (scriptFlagPrefix ++ "-datum") "The script datum."
        "The script datum file."
, pInlineDatumPresent
]
datumOptional = asum
[ PlutusSpend.PotentialDatum <$> optional (pScriptDataOrFile
(scriptFlagPrefix ++ "-datum")
"The script datum."
"The script datum file.")
, pInlineDatumPresent
]
pInlineDatumPresent :: Parser PlutusSpend.ScriptDatumOrFileSpending
pInlineDatumPresent =
flag' PlutusSpend.InlineDatum $
mconcat
[ long (scriptFlagPrefix ++ "-inline-datum-present")
, Opt.help "Inline datum present at transaction input."
]



pScriptDataOrFile
:: String
-- ^ data flag prefix
Expand Down Expand Up @@ -1935,7 +2016,7 @@
pTxIn
:: ShelleyBasedEra era
-> BalanceTxExecUnits
-> Parser (TxIn, Maybe (ScriptWitnessFiles WitCtxTxIn))
-> Parser (TxIn, Maybe PlutusSpend.CliSpendScriptRequirements)
pTxIn sbe balance =
(,)
<$> Opt.option
Expand All @@ -1944,44 +2025,43 @@
<> Opt.metavar "TX-IN"
<> Opt.help "TxId#TxIx"
)
<*> optional
( pPlutusReferenceScriptWitness sbe balance
<*>
( optional (pPlutusReferenceScriptWitness sbe balance
<|> pSimpleReferenceSpendingScriptWitess
<|> pEmbeddedPlutusScriptWitness
<|> pOnDiskPlutusScriptWitness)
)

Check notice

Code scanning / HLint

Redundant bracket Note

cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs:(2029,7)-(2032,7): Suggestion: Redundant bracket
  
Found:
  (,)
    <$>
      Opt.option
        (readerFromParsecParser parseTxIn)
        (Opt.long "tx-in" <> Opt.metavar "TX-IN" <> Opt.help "TxId#TxIx")
    <>
      (optional
         (pPlutusReferenceScriptWitness sbe balance
            <|> pSimpleReferenceSpendingScriptWitess
            <|> pOnDiskPlutusScriptWitness))
  
Perhaps:
  (,)
    <$>
      Opt.option
        (readerFromParsecParser parseTxIn)
        (Opt.long "tx-in" <> Opt.metavar "TX-IN" <> Opt.help "TxId#TxIx")
    <>
      optional
        (pPlutusReferenceScriptWitness sbe balance
           <|> pSimpleReferenceSpendingScriptWitess
           <|> pOnDiskPlutusScriptWitness)
where
pSimpleReferenceSpendingScriptWitess :: Parser (ScriptWitnessFiles WitCtxTxIn)
pSimpleReferenceSpendingScriptWitess :: Parser CliSpendScriptRequirements
pSimpleReferenceSpendingScriptWitess =
createSimpleReferenceScriptWitnessFiles
PlutusSpend.createSimpleReferenceScriptFromCliArgs
<$> pReferenceTxIn "simple-script-" "simple"
where
createSimpleReferenceScriptWitnessFiles
:: TxIn
-> ScriptWitnessFiles WitCtxTxIn
createSimpleReferenceScriptWitnessFiles refTxIn =
let simpleLang = AnyScriptLanguage SimpleScriptLanguage
in SimpleReferenceScriptWitnessFiles refTxIn simpleLang

-- TODO: Left off here. You can keep the era switching behaviour
-- for now but use createPlutusReferenceScriptFromCliArgs. Resolve
-- type errors and pScriptDatumOrFile issue. You likely need
-- a newtype that represents the possibility of a datum, no datum
-- or inline datum. The other contructors will eventually become irrelevant
-- once you remove the script witness files type
pPlutusReferenceScriptWitness
:: ShelleyBasedEra era -> BalanceTxExecUnits -> Parser (ScriptWitnessFiles WitCtxTxIn)
:: ShelleyBasedEra era -> BalanceTxExecUnits -> Parser CliSpendScriptRequirements
pPlutusReferenceScriptWitness sbe' autoBalanceExecUnits =
caseShelleyToBabbageOrConwayEraOnwards
( const $
PlutusReferenceScriptWitnessFiles
PlutusSpend.createPlutusReferenceScriptFromCliArgs
<$> pReferenceTxIn "spending-" "plutus"
<*> pPlutusScriptLanguage "spending-"
<*> pScriptDatumOrFile "spending-reference-tx-in" WitCtxTxIn
<*> pScriptDatumOrFileSpendingNotOptional "spending-reference-tx-in"
<*> pScriptRedeemerOrFile "spending-reference-tx-in"
<*> ( case autoBalanceExecUnits of
AutoBalance -> pure (ExecutionUnits 0 0)
ManualBalance -> pExecutionUnits "spending-reference-tx-in"
)
)
( const $
PlutusReferenceScriptWitnessFiles
PlutusSpend.createPlutusReferenceScriptFromCliArgs
<$> pReferenceTxIn "spending-" "plutus"
<*> pPlutusScriptLanguage "spending-"
<*> pScriptDatumOrFileCip69 "spending-reference-tx-in" WitCtxTxIn
<*> pScriptDatumOrFileSpendingCip69 sbe "spending-reference-tx-in"
<*> pScriptRedeemerOrFile "spending-reference-tx-in"
<*> ( case autoBalanceExecUnits of
AutoBalance -> pure (ExecutionUnits 0 0)
Expand All @@ -1990,11 +2070,10 @@
)
sbe'

pEmbeddedPlutusScriptWitness :: Parser (ScriptWitnessFiles WitCtxTxIn)
pEmbeddedPlutusScriptWitness =
pScriptWitnessFiles
pOnDiskPlutusScriptWitness :: Parser CliSpendScriptRequirements
pOnDiskPlutusScriptWitness =
pPlutusSpendingScriptWitness
sbe
WitCtxTxIn
balance
"tx-in"
(Just "txin")
Expand Down
Loading
Loading