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

Initial Byron isolation 1 of n #450

Closed
wants to merge 7 commits into from
Closed
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
5 changes: 5 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ write-ghc-environment-files: always
-- IMPORTANT
-- Do NOT add more source-repository-package stanzas here unless they are strictly
-- temporary! Please read the section in CONTRIBUTING about updating dependencies.
source-repository-package
type: git
location: https://github.com/input-output-hk/cardano-api.git
tag: dd496b21d0e2bb41d73465798c562add40e5cc9e
subdir: cardano-api
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Byron/Parsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ parseTxOut =
pLovelaceTxOut l =
if l > (maxBound :: Word64)
then error $ show l <> " lovelace exceeds the Word64 upper bound"
else TxOutValueByron ByronEraOnlyByron . Lovelace $ toInteger l
else TxOutValueByron . Lovelace $ toInteger l

readerFromAttoParser :: Atto.Parser a -> Opt.ReadM a
readerFromAttoParser p =
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Byron/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ nodeSubmitTx nodeSocketPath network gentx = do
localNodeNetworkId = network,
localConsensusModeParams = CardanoModeParams (EpochSlots 21600)
}
res <- liftIO $ submitTxToNodeLocal connctInfo (TxInByronSpecial ByronEraOnlyByron gentx)
res <- liftIO $ submitTxToNodeLocal connctInfo (TxInByronSpecial gentx)
case res of
Net.Tx.SubmitSuccess -> liftIO $ Text.putStrLn "Transaction successfully submitted."
Net.Tx.SubmitFail reason ->
Expand Down
8 changes: 4 additions & 4 deletions cardano-cli/src/Cardano/CLI/EraBased/Commands/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ data TransactionBuildRawCmdArgs era = TransactionBuildRawCmdArgs
-- ^ Read only reference inputs
, txInsCollateral :: ![TxIn]
-- ^ Transaction inputs for collateral, only key witnesses, no scripts.
, mReturnCollateral :: !(Maybe TxOutAnyEra)
, mReturnCollateral :: !(Maybe TxOutShelleyBasedEra)
-- ^ Return collateral
, mTotalCollateral :: !(Maybe Lovelace)
-- ^ Total collateral
Expand Down Expand Up @@ -98,7 +98,7 @@ data TransactionBuildCmdArgs era = TransactionBuildCmdArgs
-- ^ Required signers
, txinsc :: ![TxIn]
-- ^ Transaction inputs for collateral, only key witnesses, no scripts.
, mReturnCollateral :: !(Maybe TxOutAnyEra)
, mReturnCollateral :: !(Maybe TxOutShelleyBasedEra)
-- ^ Return collateral
, mTotalCollateral :: !(Maybe Lovelace)
-- ^ Total collateral
Expand Down Expand Up @@ -168,9 +168,9 @@ data TransactionCalculateMinFeeCmdArgs = TransactionCalculateMinFeeCmdArgs
} deriving Show

data TransactionCalculateMinValueCmdArgs era = TransactionCalculateMinValueCmdArgs
{ eon :: !(CardanoEra era)
{ eon :: !(ShelleyBasedEra era)
, protocolParamsFile :: !ProtocolParamsFile
, txOut :: !TxOutAnyEra
, txOut :: !TxOutShelleyBasedEra
} deriving Show

newtype TransactionHashScriptDataCmdArgs = TransactionHashScriptDataCmdArgs
Expand Down
36 changes: 34 additions & 2 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1848,9 +1848,9 @@ pTxInCollateral =
<> Opt.help "TxId#TxIx"
)

pReturnCollateral :: Parser TxOutAnyEra
pReturnCollateral :: Parser TxOutShelleyBasedEra
pReturnCollateral =
Opt.option (readerFromParsecParser parseTxOutAnyEra)
Opt.option (readerFromParsecParser parseTxOutShelleyBasedEra)
( mconcat
[ Opt.long "tx-out-return-collateral"
, Opt.metavar "ADDRESS VALUE"
Expand Down Expand Up @@ -1899,6 +1899,19 @@ pTxOut =
<*> pTxOutDatum
<*> pRefScriptFp

pTxOutShelleyBased :: Parser TxOutShelleyBasedEra
pTxOutShelleyBased =
Opt.option (readerFromParsecParser parseTxOutShelleyBasedEra)
( Opt.long "tx-out"
<> Opt.metavar "ADDRESS VALUE"
-- TODO alonzo: Update the help text to describe the new syntax as well.
<> Opt.help "The transaction output as ADDRESS VALUE where ADDRESS is \
\the Bech32-encoded address followed by the value in \
\the multi-asset syntax (including simply Lovelace)."
)
<*> pTxOutDatum
<*> pRefScriptFp

pTxOutDatum :: Parser TxOutDatumAnyEra
pTxOutDatum =
pTxOutDatumByHashOnly
Expand Down Expand Up @@ -2998,6 +3011,25 @@ pDRepActivity =
, Opt.help "TODO"
]

parseTxOutShelleyBasedEra
:: Parsec.Parser (TxOutDatumAnyEra -> ReferenceScriptAnyEra -> TxOutShelleyBasedEra)
parseTxOutShelleyBasedEra = do
addr <- parseShelleyAddress
Parsec.spaces
-- Accept the old style of separating the address and value in a
-- transaction output:
Parsec.option () (Parsec.char '+' >> Parsec.spaces)
val <- parseValue
return (TxOutShelleyBasedEra addr val)

parseShelleyAddress :: Parsec.Parser (Address ShelleyAddr)
parseShelleyAddress = do
str <- lexPlausibleAddressString
case deserialiseAddress AsShelleyAddress str of
Nothing -> fail $ "invalid address: " <> Text.unpack str
Just addr -> pure addr


parseTxOutAnyEra
:: Parsec.Parser (TxOutDatumAnyEra -> ReferenceScriptAnyEra -> TxOutAnyEra)
parseTxOutAnyEra = do
Expand Down
25 changes: 15 additions & 10 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,17 @@ pTransactionCmds era envCli =
$ subParser "calculate-min-fee"
$ Opt.info (pTransactionCalculateMinFee envCli)
$ Opt.progDesc "Calculate the minimum fee for a transaction."
, Just
$ subParser "calculate-min-required-utxo"
$ Opt.info (pTransactionCalculateMinReqUTxO era)
$ Opt.progDesc "Calculate the minimum required UTxO for a transaction output."
, Just
$ pCalculateMinRequiredUtxoBackwardCompatible era
, caseByronOrShelleyBasedEra
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The goal is to not need caseByronOrShelleyBasedEra in the future. Required byron based functionality will be hard coded and available under the byron command group.

(const Nothing)
(\sbe -> Just $ subParser "calculate-min-required-utxo"
$ Opt.info (pTransactionCalculateMinReqUTxO sbe)
$ Opt.progDesc "Calculate the minimum required UTxO for a transaction output."
)
era
, caseByronOrShelleyBasedEra
(const Nothing)
(Just . pCalculateMinRequiredUtxoBackwardCompatible)
era
, Just
$ subParser "hash-script-data"
$ Opt.info pTxHashScriptData
Expand All @@ -99,12 +104,12 @@ pTransactionCmds era envCli =
]

-- Backwards compatible parsers
calcMinValueInfo :: CardanoEra era -> ParserInfo (TransactionCmds era)
calcMinValueInfo :: ShelleyBasedEra era -> ParserInfo (TransactionCmds era)
calcMinValueInfo era =
Opt.info (pTransactionCalculateMinReqUTxO era)
$ Opt.progDesc "DEPRECATED: Use 'calculate-min-required-utxo' instead."

pCalculateMinRequiredUtxoBackwardCompatible :: CardanoEra era -> Parser (TransactionCmds era)
pCalculateMinRequiredUtxoBackwardCompatible :: ShelleyBasedEra era -> Parser (TransactionCmds era)
pCalculateMinRequiredUtxoBackwardCompatible era =
Opt.subparser
$ Opt.command "calculate-min-value" (calcMinValueInfo era) <> Opt.internal
Expand Down Expand Up @@ -275,12 +280,12 @@ pTransactionCalculateMinFee envCli =
<*> pTxShelleyWitnessCount
<*> pTxByronWitnessCount

pTransactionCalculateMinReqUTxO :: CardanoEra era -> Parser (TransactionCmds era)
pTransactionCalculateMinReqUTxO :: ShelleyBasedEra era -> Parser (TransactionCmds era)
pTransactionCalculateMinReqUTxO era =
fmap TransactionCalculateMinValueCmd $
TransactionCalculateMinValueCmdArgs era
<$> pProtocolParamsFile
<*> pTxOut
<*> pTxOutShelleyBased

pTxHashScriptData :: Parser (TransactionCmds era)
pTxHashScriptData =
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ printUtxo sbe txInOutTuple =

printableValue :: TxOutValue era -> Text
printableValue = \case
TxOutValueByron _ (Lovelace i) -> Text.pack $ show i
TxOutValueByron (Lovelace i) -> Text.pack $ show i
TxOutValueShelleyBased sbe2 val -> renderValue $ Api.fromLedgerValue sbe2 val

runQueryStakePoolsCmd :: ()
Expand Down
132 changes: 91 additions & 41 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
Expand Down Expand Up @@ -164,7 +165,7 @@ runTransactionBuildCmd
_ -> pure TxUpdateProposalNone

requiredSigners <- mapM (firstExceptT TxCmdRequiredSignerError . newExceptT . readRequiredSigner) reqSigners
mReturnCollateral <- forM mReturnColl $ toTxOutInAnyEra era
mReturnCollateral <- forM mReturnColl $ toTxOutInShelleyBasedEra eon

txOuts <- mapM (toTxOutInAnyEra era) txouts

Expand Down Expand Up @@ -319,7 +320,11 @@ runTransactionBuildRawCmd
_ -> pure TxUpdateProposalNone

requiredSigners <- mapM (firstExceptT TxCmdRequiredSignerError . newExceptT . readRequiredSigner) reqSigners
mReturnCollateral <- forM mReturnColl $ toTxOutInAnyEra eon

mReturnCollateral <- forEraInEon eon (pure Nothing) $ \sbe ->
forM mReturnColl $ toTxOutInShelleyBasedEra sbe

-- NB: We need to be able to construct txs in Byron to other Byron addresses
txOuts <- mapM (toTxOutInAnyEra eon) txouts

-- the same collateral input can be used for several plutus scripts
Expand Down Expand Up @@ -690,6 +695,14 @@ toAddressInAnyEra era addrAny = runExcept $ do
pure (AddressInEra (ShelleyAddressInEra sbe) sAddr)


toAddressInShelleyBasedEra
:: ShelleyBasedEra era
-> Address ShelleyAddr
-> Either TxCmdError (AddressInEra era)
toAddressInShelleyBasedEra sbe sAddr = runExcept $
pure (AddressInEra (ShelleyAddressInEra sbe) sAddr)


lovelaceToCoin :: Lovelace -> Ledger.Coin
lovelaceToCoin (Lovelace ll) = Ledger.Coin ll

Expand All @@ -698,10 +711,10 @@ toTxOutValueInAnyEra
-> Value
-> Either TxCmdError (TxOutValue era)
toTxOutValueInAnyEra era val =
caseByronOrShelleyBasedEra
(\w ->
caseByronOrShelleyBasedEra
(const $
case valueToLovelace val of
Just l -> return (TxOutValueByron w l)
Just l -> return (TxOutValueByron l)
Nothing -> txFeatureMismatchPure era TxFeatureMultiAssetOutputs
)
(\sbe ->
Expand All @@ -715,7 +728,46 @@ toTxOutValueInAnyEra era val =
sbe
)
era
toTxOutValueInShelleyBasedEra
:: ShelleyBasedEra era
-> Value
-> Either TxCmdError (TxOutValue era)
toTxOutValueInShelleyBasedEra sbe val =
caseShelleyToAllegraOrMaryEraOnwards
(\_ -> case valueToLovelace val of
Just l -> return (TxOutValueShelleyBased sbe $ lovelaceToCoin l)
Nothing -> txFeatureMismatchPure (toCardanoEra sbe) TxFeatureMultiAssetOutputs
)
(\w -> return (TxOutValueShelleyBased sbe (toLedgerValue w val))
)
sbe


toTxOutInShelleyBasedEra
:: ShelleyBasedEra era
-> TxOutShelleyBasedEra
-> ExceptT TxCmdError IO (TxOut CtxTx era)
toTxOutInShelleyBasedEra era (TxOutShelleyBasedEra addr' val' mDatumHash refScriptFp) = do
addr <- hoistEither $ toAddressInShelleyBasedEra era addr'
val <- hoistEither $ toTxOutValueInShelleyBasedEra era val'

datum <-
caseShelleyToMaryOrAlonzoEraOnwards
(const (pure TxOutDatumNone))
(\wa -> toTxAlonzoDatum wa mDatumHash)
era

refScript <- inEonForEra
(pure ReferenceScriptNone)
(\wb -> getReferenceScript wb refScriptFp)
(toCardanoEra era)

pure $ TxOut addr val datum refScript


-- TODO: toTxOutInAnyEra eventually will not be needed because
-- byron related functionality will be treated
-- separately
toTxOutInAnyEra :: CardanoEra era
-> TxOutAnyEra
-> ExceptT TxCmdError IO (TxOut CtxTx era)
Expand All @@ -735,37 +787,35 @@ toTxOutInAnyEra era (TxOutAnyEra addr' val' mDatumHash refScriptFp) = do
(const (pure ReferenceScriptNone))
(\wb -> getReferenceScript wb refScriptFp)
era

pure $ TxOut addr val datum refScript

where
getReferenceScript :: ()
=> BabbageEraOnwards era
-> ReferenceScriptAnyEra
-> ExceptT TxCmdError IO (ReferenceScript era)
getReferenceScript w = \case
ReferenceScriptAnyEraNone -> return ReferenceScriptNone
ReferenceScriptAnyEra fp -> ReferenceScript w <$> firstExceptT TxCmdScriptFileError (readFileScriptInAnyLang fp)

toTxAlonzoDatum :: ()
=> AlonzoEraOnwards era
-> TxOutDatumAnyEra
-> ExceptT TxCmdError IO (TxOutDatum CtxTx era)
toTxAlonzoDatum supp cliDatum =
case cliDatum of
TxOutDatumByNone -> pure TxOutDatumNone
TxOutDatumByHashOnly h -> pure (TxOutDatumHash supp h)
TxOutDatumByHashOf sDataOrFile -> do
sData <- firstExceptT TxCmdScriptDataError $ readScriptDataOrFile sDataOrFile
pure (TxOutDatumHash supp $ hashScriptDataBytes sData)
TxOutDatumByValue sDataOrFile -> do
sData <- firstExceptT TxCmdScriptDataError $ readScriptDataOrFile sDataOrFile
pure (TxOutDatumInTx supp sData)
TxOutInlineDatumByValue sDataOrFile -> do
let cEra = alonzoEraOnwardsToCardanoEra supp
forEraInEon cEra (txFeatureMismatch cEra TxFeatureInlineDatums) $ \babbageOnwards -> do
sData <- firstExceptT TxCmdScriptDataError $ readScriptDataOrFile sDataOrFile
pure $ TxOutDatumInline babbageOnwards sData
getReferenceScript :: ()
=> BabbageEraOnwards era
-> ReferenceScriptAnyEra
-> ExceptT TxCmdError IO (ReferenceScript era)
getReferenceScript w = \case
ReferenceScriptAnyEraNone -> return ReferenceScriptNone
ReferenceScriptAnyEra fp -> ReferenceScript w <$> firstExceptT TxCmdScriptFileError (readFileScriptInAnyLang fp)

toTxAlonzoDatum :: ()
=> AlonzoEraOnwards era
-> TxOutDatumAnyEra
-> ExceptT TxCmdError IO (TxOutDatum CtxTx era)
toTxAlonzoDatum supp cliDatum =
case cliDatum of
TxOutDatumByNone -> pure TxOutDatumNone
TxOutDatumByHashOnly h -> pure (TxOutDatumHash supp h)
TxOutDatumByHashOf sDataOrFile -> do
sData <- firstExceptT TxCmdScriptDataError $ readScriptDataOrFile sDataOrFile
pure (TxOutDatumHash supp $ hashScriptDataBytes sData)
TxOutDatumByValue sDataOrFile -> do
sData <- firstExceptT TxCmdScriptDataError $ readScriptDataOrFile sDataOrFile
pure (TxOutDatumInTx supp sData)
TxOutInlineDatumByValue sDataOrFile -> do
let cEra = alonzoEraOnwardsToCardanoEra supp
forEraInEon cEra (txFeatureMismatch cEra TxFeatureInlineDatums) $ \babbageOnwards -> do
sData <- firstExceptT TxCmdScriptDataError $ readScriptDataOrFile sDataOrFile
pure $ TxOutDatumInline babbageOnwards sData

-- TODO: Currently we specify the policyId with the '--mint' option on the cli
-- and we added a separate '--policy-id' parser that parses the policy id for the
Expand Down Expand Up @@ -1023,14 +1073,14 @@ runTransactionCalculateMinValueCmd
, txOut
} = do
pp <- firstExceptT TxCmdProtocolParamsError (readProtocolParameters protocolParamsFile)
out <- toTxOutInAnyEra eon txOut
out <- toTxOutInShelleyBasedEra eon txOut
-- TODO: shouldn't we just require shelley based era here instead of error-ing for byron?
forEraInEon eon (error "runTransactionCalculateMinValueCmd: Byron era not implemented yet") $ \sbe -> do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Byron was never supported

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO can be removed now

firstExceptT TxCmdPParamsErr . hoistEither
$ checkProtocolParameters sbe pp
pp' <- hoistEither . first TxCmdProtocolParamsConverstionError $ toLedgerPParams sbe pp
let minValue = calculateMinimumUTxO sbe out pp'
liftIO . IO.print $ minValue

firstExceptT TxCmdPParamsErr . hoistEither
$ checkProtocolParameters eon pp
pp' <- hoistEither . first TxCmdProtocolParamsConverstionError $ toLedgerPParams eon pp
let minValue = calculateMinimumUTxO eon out pp'
liftIO . IO.print $ minValue

runTransactionPolicyIdCmd :: ()
=> Cmd.TransactionPolicyIdCmdArgs
Expand Down
3 changes: 1 addition & 2 deletions cardano-cli/src/Cardano/CLI/Json/Friendly.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
Expand Down Expand Up @@ -566,7 +565,7 @@ friendlyMintValue = \case

friendlyTxOutValue :: TxOutValue era -> Aeson.Value
friendlyTxOutValue = \case
TxOutValueByron _ lovelace -> friendlyLovelace $ toShelleyLovelace lovelace
TxOutValueByron lovelace -> friendlyLovelace $ toShelleyLovelace lovelace
TxOutValueShelleyBased sbe v -> friendlyLedgerValue sbe v

friendlyLedgerValue :: ()
Expand Down
Loading
Loading