Skip to content

Commit

Permalink
Merge pull request #758 from IntersectMBO/smelc/write-protocol-parame…
Browse files Browse the repository at this point in the history
…ters-use-ledger-encoding

query protocol-parameters: use ledger JSON encoding, not API one
  • Loading branch information
smelc authored May 28, 2024
2 parents adbaac2 + 87134c3 commit b08f10c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 36 deletions.
1 change: 1 addition & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ library
, cardano-crypto-wrapper ^>= 1.5.1
, cardano-data >= 1.1
, cardano-git-rev ^>= 0.2.2
, cardano-ledger-api
, cardano-ledger-byron >= 1.0.1.0
, cardano-ping ^>= 0.2.0.13
, cardano-prelude
Expand Down
10 changes: 6 additions & 4 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Genesis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1176,9 +1176,11 @@ readConwayGenesis fpath = do

--TODO: eliminate this and get only the necessary params, and get them in a more
-- helpful way rather than requiring them as a local file.
readProtocolParameters :: ProtocolParamsFile
-> ExceptT ProtocolParamsError IO ProtocolParameters
readProtocolParameters (ProtocolParamsFile fpath) = do
readProtocolParameters :: ()
=> ShelleyBasedEra era
-> ProtocolParamsFile
-> ExceptT ProtocolParamsError IO (L.PParams (ShelleyLedgerEra era))
readProtocolParameters sbe (ProtocolParamsFile fpath) = do
pparams <- handleIOExceptT (ProtocolParamsErrorFile . FileIOError fpath) $ LBS.readFile fpath
firstExceptT (ProtocolParamsErrorJSON fpath . Text.pack) . hoistEither $
Aeson.eitherDecode' pparams
shelleyBasedEraConstraints sbe $ Aeson.eitherDecode' pparams
5 changes: 3 additions & 2 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,16 @@ runQueryProtocolParametersCmd
. newExceptT $ executeQueryAnyMode localNodeConnInfo qInMode
writeProtocolParameters sbe mOutFile pp
where
-- TODO: Conway era - use ledger PParams JSON
writeProtocolParameters
:: ShelleyBasedEra era
-> Maybe (File () Out)
-> L.PParams (ShelleyLedgerEra era)
-> ExceptT QueryCmdError IO ()
writeProtocolParameters sbe mOutFile' pparams =
firstExceptT QueryCmdWriteFileError . newExceptT
$ writeLazyByteStringOutput mOutFile' $ encodePretty $ fromLedgerPParams sbe pparams
$ writeLazyByteStringOutput mOutFile'
$ shelleyBasedEraConstraints sbe
$ encodePretty pparams

-- | Calculate the percentage sync rendered as text.
percentage
Expand Down
43 changes: 13 additions & 30 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import Cardano.CLI.Types.Errors.TxCmdError
import Cardano.CLI.Types.Errors.TxValidationError
import Cardano.CLI.Types.Output (renderScriptCosts)
import Cardano.CLI.Types.TxFeature
import qualified Cardano.Ledger.Api.PParams as L
import qualified Ouroboros.Network.Protocol.LocalStateQuery.Type as Consensus
import qualified Ouroboros.Network.Protocol.LocalTxSubmission.Client as Net.Tx

Expand Down Expand Up @@ -283,9 +284,7 @@ runTransactionBuildEstimateCmd
, txBodyOutFile
} = do
let sbe = maryEraOnwardsToShelleyBasedEra eon
legacyPParams <- firstExceptT TxCmdProtocolParamsError $ readProtocolParameters protocolParamsFile
ledgerPParams <- hoistEither . first TxCmdProtocolParamsConverstionError
$ convertToLedgerProtocolParameters sbe legacyPParams
ledgerPParams <- firstExceptT TxCmdProtocolParamsError $ readProtocolParameters sbe protocolParamsFile
inputsAndMaybeScriptWits <- firstExceptT TxCmdScriptWitnessError
$ readScriptWitnessFiles sbe txins
certFilesAndMaybeScriptWits <- firstExceptT TxCmdScriptWitnessError
Expand Down Expand Up @@ -336,7 +335,7 @@ runTransactionBuildEstimateCmd

txBodyContent <- hoistEither $ constructTxBodyContent
sbe mScriptValidity
(Just $ unLedgerProtocolParameters ledgerPParams)
(Just ledgerPParams)
inputsAndMaybeScriptWits
readOnlyRefIns
filteredTxinsc
Expand Down Expand Up @@ -365,7 +364,7 @@ runTransactionBuildEstimateCmd

BalancedTxBody _ balancedTxBody _ _ <-
hoistEither $ first TxCmdFeeEstimationError $
estimateBalancedTxBody eon txBodyContent (unLedgerProtocolParameters ledgerPParams) poolsToDeregister
estimateBalancedTxBody eon txBodyContent ledgerPParams poolsToDeregister
stakeCredentialsToDeregisterMap drepsToDeregisterMap
pScriptExecUnits totCol shelleyWitnesses (fromMaybe 0 mByronWitnesses)
(maybe 0 unReferenceScriptSize totalReferenceScriptSize) (anyAddressInShelleyBasedEra sbe changeAddr)
Expand Down Expand Up @@ -503,15 +502,10 @@ runTransactionBuildRawCmd
mapM (readFileScriptInAnyLang . unFile) scriptFiles
txAuxScripts <- hoistEither $ first TxCmdAuxScriptsValidationError $ validateTxAuxScripts eon scripts

-- TODO: Conway era - update readProtocolParameters to rely on L.PParams JSON instances
pparams <- forM mProtocolParamsFile $ \ppf ->
firstExceptT TxCmdProtocolParamsError (readProtocolParameters ppf)
firstExceptT TxCmdProtocolParamsError (readProtocolParameters eon ppf)

mLedgerPParams <- case pparams of
Nothing -> return Nothing
Just pp -> do
ledgerpp <- firstExceptT TxCmdProtocolParamsConverstionError . hoistEither $ convertToLedgerProtocolParameters eon pp
return $ Just ledgerpp
let mLedgerPParams = LedgerProtocolParameters <$> pparams

txUpdateProposal <- case mUpdateProprosalFile of
Just (Featured w (Just updateProposalFile)) ->
Expand Down Expand Up @@ -1239,32 +1233,23 @@ runTransactionCalculateMinFeeCmd
unwitnessed <-
firstExceptT TxCmdTextEnvCddlError . newExceptT
$ readFileTxBody txbodyFile
pparams <-
firstExceptT TxCmdProtocolParamsError
$ readProtocolParameters protocolParamsFile

let nShelleyKeyWitW32 = fromIntegral nShelleyKeyWitnesses

InAnyShelleyBasedEra sbe txbody <- pure $ unIncompleteCddlTxBody unwitnessed

lpparams <- getLedgerPParams sbe pparams
lpparams <-
firstExceptT TxCmdProtocolParamsError
$ readProtocolParameters sbe protocolParamsFile

let shelleyfee = evaluateTransactionFee sbe lpparams txbody nShelleyKeyWitW32 0 sReferenceScript

let byronfee = calculateByronWitnessFees (protocolParamTxFeePerByte pparams) nByronKeyWitnesses
let byronfee = shelleyBasedEraConstraints sbe $ calculateByronWitnessFees (lpparams ^. L.ppMinFeeAL) nByronKeyWitnesses

let L.Coin fee = shelleyfee + byronfee

liftIO $ putStrLn $ (show fee :: String) <> " Lovelace"

getLedgerPParams :: forall era. ()
=> ShelleyBasedEra era
-> ProtocolParameters
-> ExceptT TxCmdError IO (L.PParams (ShelleyLedgerEra era))
getLedgerPParams sbe pparams =
firstExceptT TxCmdProtocolParamsConverstionError $
hoistEither $ toLedgerPParams sbe pparams

-- Extra logic to handle byron witnesses.
-- TODO: move this to Cardano.API.Fee.evaluateTransactionFee.
calculateByronWitnessFees :: ()
Expand Down Expand Up @@ -1317,14 +1302,12 @@ runTransactionCalculateMinValueCmd
, protocolParamsFile
, txOut
} = do
pp <- firstExceptT TxCmdProtocolParamsError (readProtocolParameters protocolParamsFile)
pp <- firstExceptT TxCmdProtocolParamsError (readProtocolParameters eon protocolParamsFile)
out <- toTxOutInShelleyBasedEra eon txOut
-- TODO: shouldn't we just require shelley based era here instead of error-ing for byron?

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

runTransactionPolicyIdCmd :: ()
Expand Down

0 comments on commit b08f10c

Please sign in to comment.