Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 committed Nov 21, 2024
1 parent 6c4869f commit abc1f5e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 35 deletions.
18 changes: 3 additions & 15 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1539,7 +1539,6 @@ pPlutusStakeReferenceScriptWitnessFilesVotingProposing prefix autoBalanceExecUni
AutoBalance -> pure (ExecutionUnits 0 0)
ManualBalance -> pExecutionUnits $ prefix ++ "reference-tx-in"
)
<*> pure Nothing

pPlutusStakeReferenceScriptWitnessFiles
:: String
Expand All @@ -1556,7 +1555,6 @@ pPlutusStakeReferenceScriptWitnessFiles prefix autoBalanceExecUnits =
AutoBalance -> pure (ExecutionUnits 0 0)
ManualBalance -> pExecutionUnits $ prefix ++ "reference-tx-in"
)
<*> pure Nothing

pPlutusScriptLanguage :: String -> Parser AnyPlutusScriptVersion
pPlutusScriptLanguage prefix = plutusP prefix PlutusScriptV2 "v2" <|> plutusP prefix PlutusScriptV3 "v3"
Expand Down Expand Up @@ -1947,14 +1945,14 @@ pTxIn sbe balance =
-> ScriptWitnessFiles WitCtxTxIn
createSimpleReferenceScriptWitnessFiles refTxIn =
let simpleLang = AnyScriptLanguage SimpleScriptLanguage
in SimpleReferenceScriptWitnessFiles refTxIn simpleLang Nothing
in SimpleReferenceScriptWitnessFiles refTxIn simpleLang

pPlutusReferenceScriptWitness
:: ShelleyBasedEra era -> BalanceTxExecUnits -> Parser (ScriptWitnessFiles WitCtxTxIn)
pPlutusReferenceScriptWitness sbe' autoBalanceExecUnits =
caseShelleyToBabbageOrConwayEraOnwards
( const $
createPlutusReferenceScriptWitnessFiles
PlutusReferenceScriptWitnessFiles
<$> pReferenceTxIn "spending-" "plutus"
<*> pPlutusScriptLanguage "spending-"
<*> pScriptDatumOrFile "spending-reference-tx-in" WitCtxTxIn
Expand All @@ -1965,7 +1963,7 @@ pTxIn sbe balance =
)
)
( const $
createPlutusReferenceScriptWitnessFiles
PlutusReferenceScriptWitnessFiles
<$> pReferenceTxIn "spending-" "plutus"
<*> pPlutusScriptLanguage "spending-"
<*> pScriptDatumOrFileCip69 "spending-reference-tx-in" WitCtxTxIn
Expand All @@ -1976,16 +1974,6 @@ pTxIn sbe balance =
)
)
sbe'
where
createPlutusReferenceScriptWitnessFiles
:: TxIn
-> AnyPlutusScriptVersion
-> ScriptDatumOrFile WitCtxTxIn
-> ScriptRedeemerOrFile
-> ExecutionUnits
-> ScriptWitnessFiles WitCtxTxIn
createPlutusReferenceScriptWitnessFiles refIn sLang sDatum sRedeemer execUnits =
PlutusReferenceScriptWitnessFiles refIn sLang sDatum sRedeemer execUnits Nothing

pEmbeddedPlutusScriptWitness :: Parser (ScriptWitnessFiles WitCtxTxIn)
pEmbeddedPlutusScriptWitness =
Expand Down
25 changes: 16 additions & 9 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ import Data.Function ((&))
import qualified Data.List as List
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe (catMaybes, fromMaybe, mapMaybe)
import Data.Maybe (catMaybes, fromMaybe, mapMaybe, maybeToList)
import Data.Set (Set)
import qualified Data.Set as Set
import qualified Data.Text as Text
Expand Down Expand Up @@ -1248,9 +1248,9 @@ getAllReferenceInputs
:: ScriptWitness witctx era -> Maybe TxIn
getReferenceInput sWit =
case sWit of
PlutusScriptWitness _ _ (PReferenceScript refIn _) _ _ _ -> Just refIn
PlutusScriptWitness _ _ (PReferenceScript refIn) _ _ _ -> Just refIn
PlutusScriptWitness _ _ PScript{} _ _ _ -> Nothing
SimpleScriptWitness _ (SReferenceScript refIn _) -> Just refIn
SimpleScriptWitness _ (SReferenceScript refIn) -> Just refIn
SimpleScriptWitness _ SScript{} -> Nothing

toAddressInAnyEra
Expand Down Expand Up @@ -1403,19 +1403,26 @@ createTxMintValue era (val, scriptWitnesses) =
caseShelleyToAllegraOrMaryEraOnwards
(const (txFeatureMismatchPure (toCardanoEra era) TxFeatureMintValue))
( \w -> do
-- The set of policy ids for which we need witnesses:
let witnessesNeededSet :: Set PolicyId
witnessesNeededSet =
fromList [pid | (AssetId pid _, _) <- toList val]
let policiesWithAssets :: [(PolicyId, AssetName, Quantity)]
policiesWithAssets = [(pid, assetName, quantity) | (AssetId pid assetName, quantity) <- toList val]
-- The set of policy ids for which we need witnesses:
witnessesNeededSet :: Set PolicyId
witnessesNeededSet = fromList [pid | (pid, _, _) <- policiesWithAssets]

let witnessesProvidedMap :: Map PolicyId (ScriptWitness WitCtxMint era)
witnessesProvidedMap = fromList $ [(polid, sWit) | MintScriptWitnessWithPolicyId polid sWit <- scriptWitnesses]
witnessesProvidedSet = Map.keysSet witnessesProvidedMap

policiesWithWitnesses =
Map.fromListWith
(<>)
[ (pid, [(assetName, quantity, BuildTxWith witness)])
| (pid, assetName, quantity) <- policiesWithAssets
, witness <- maybeToList $ Map.lookup pid witnessesProvidedMap
]
-- Check not too many, nor too few:
validateAllWitnessesProvided witnessesNeededSet witnessesProvidedSet
validateNoUnnecessaryWitnesses witnessesNeededSet witnessesProvidedSet
return (TxMintValue w val (BuildTxWith witnessesProvidedMap))
pure $ TxMintValue w policiesWithWitnesses
)
era
where
Expand Down
4 changes: 2 additions & 2 deletions cardano-cli/src/Cardano/CLI/EraBased/Script/Mint/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ readMintScriptWitness sbe (OnDiskSimpleRefScript (SimpleRefScriptCliArgs refTxIn
MintScriptWitnessWithPolicyId polId $
SimpleScriptWitness
(sbeToSimpleScriptLangInEra sbe)
(SReferenceScript refTxIn $ Just $ unPolicyId polId)
(SReferenceScript refTxIn)
readMintScriptWitness
sbe
( OnDiskPlutusRefScript
(PlutusRefScriptCliArgs refTxIn anyPlutusScriptVersion redeemerFile execUnits polId)
) = do
case anyPlutusScriptVersion of
AnyPlutusScriptVersion lang -> do
let pScript = PReferenceScript refTxIn $ Just $ unPolicyId polId
let pScript = PReferenceScript refTxIn
redeemer <-
-- TODO: Implement a new error type to capture this. FileError is not representative of cases
-- where we do not have access to the script.
Expand Down
4 changes: 3 additions & 1 deletion cardano-cli/src/Cardano/CLI/EraBased/Script/Mint/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ where

import Cardano.Api

import Cardano.CLI.Read
import Cardano.CLI.Types.Common (ScriptDataOrFile)
import Cardano.CLI.Types.Errors.PlutusScriptDecodeError
import Cardano.CLI.Types.Errors.ScriptDataError
import Cardano.CLI.Types.Errors.ScriptDecodeError

-- We always need the policy id when constructing a transaction that mints.
-- In the case of reference scripts, the user currently must provide the policy id (script hash)
Expand Down
6 changes: 2 additions & 4 deletions cardano-cli/src/Cardano/CLI/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,6 @@ readScriptWitness
datumOrFile
redeemerOrFile
execUnits
mPid
) = do
caseShelleyToAlonzoOrBabbageEraOnwards
( const $
Expand All @@ -381,7 +380,7 @@ readScriptWitness
PlutusScriptWitness
sLangInEra
version
(PReferenceScript refTxIn (unPolicyId <$> mPid))
(PReferenceScript refTxIn)
datum
redeemer
execUnits
Expand All @@ -397,7 +396,6 @@ readScriptWitness
( SimpleReferenceScriptWitnessFiles
refTxIn
anyScrLang@(AnyScriptLanguage anyScriptLanguage)
mPid
) = do
caseShelleyToAlonzoOrBabbageEraOnwards
( const $
Expand All @@ -411,7 +409,7 @@ readScriptWitness
case languageOfScriptLanguageInEra sLangInEra of
SimpleScriptLanguage ->
return . SimpleScriptWitness sLangInEra $
SReferenceScript refTxIn (unPolicyId <$> mPid)
SReferenceScript refTxIn
PlutusScriptLanguage{} ->
error "readScriptWitness: Should not be possible to specify a plutus script"
Nothing ->
Expand Down
3 changes: 0 additions & 3 deletions cardano-cli/src/Cardano/CLI/Types/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -421,14 +421,11 @@ data ScriptWitnessFiles witctx where
-> ScriptDatumOrFile witctx
-> ScriptRedeemerOrFile
-> ExecutionUnits
-> Maybe PolicyId
-- ^ For minting reference scripts
-> ScriptWitnessFiles witctx
SimpleReferenceScriptWitnessFiles
:: TxIn
-> AnyScriptLanguage
-> Maybe PolicyId
-- ^ For minting reference scripts
-> ScriptWitnessFiles witctx

deriving instance Show (ScriptWitnessFiles witctx)
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Types/Output.hs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ renderScriptCosts (UTxO utxo) eUnitPrices scriptMapping executionCostMapping =
Left err -> Left (PlutusScriptCostErrExecError sWitInd (Just scriptHash) err) : accum
-- TODO: Create a new sum type to encapsulate the fact that we can also
-- have a txin and render the txin in the case of reference scripts.
Just (AnyScriptWitness (PlutusScriptWitness _ _ (PReferenceScript refTxIn _) _ _ _)) ->
Just (AnyScriptWitness (PlutusScriptWitness _ _ (PReferenceScript refTxIn) _ _ _)) ->
case Map.lookup refTxIn utxo of
Nothing -> Left (PlutusScriptCostErrRefInputNotInUTxO refTxIn) : accum
Just (TxOut _ _ _ refScript) ->
Expand Down

0 comments on commit abc1f5e

Please sign in to comment.