Skip to content

Commit

Permalink
WIP: make coverFee_ polymorphic on era
Browse files Browse the repository at this point in the history
  • Loading branch information
ch1bo committed Aug 6, 2024
1 parent 5098f51 commit 1a0b956
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
2 changes: 1 addition & 1 deletion hydra-cardano-api/src/Hydra/Cardano/Api/TxOut.hs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ isScriptTxOut script txOut =
-- * Type Conversions

-- | Convert a cardano-ledger 'TxOut' into a cardano-api 'TxOut'
fromLedgerTxOut :: Ledger.TxOut (ShelleyLedgerEra Era) -> TxOut ctx Era
fromLedgerTxOut :: IsShelleyBasedEra era => Ledger.TxOut (ShelleyLedgerEra era) -> TxOut ctx era
fromLedgerTxOut =
fromShelleyTxOut shelleyBasedEra

Expand Down
53 changes: 24 additions & 29 deletions hydra-node/src/Hydra/Chain/Direct/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Cardano.Api.UTxO (UTxO)
import Cardano.Api.UTxO qualified as UTxO
import Cardano.Crypto.Hash.Class
import Cardano.Ledger.Address qualified as Ledger
import Cardano.Ledger.Alonzo.Plutus.Context (ContextError)
import Cardano.Ledger.Alonzo.Plutus.Context (ContextError, EraPlutusContext)
import Cardano.Ledger.Alonzo.Scripts (
AlonzoEraScript (..),
AlonzoPlutusPurpose (AlonzoSpending),
Expand All @@ -25,11 +25,15 @@ import Cardano.Ledger.Alonzo.TxWits (
txdats,
txscripts,
)
import Cardano.Ledger.Alonzo.UTxO (AlonzoScriptsNeeded)
import Cardano.Ledger.Api (
AlonzoEraTx,
Babbage,
Conway,
EraCrypto,
PParams,
TransactionScriptFailure,
Tx,
bodyTxL,
collateralInputsTxBodyL,
ensureMinCoinTxOut,
Expand All @@ -45,6 +49,7 @@ import Cardano.Ledger.Api (
scriptIntegrityHashTxBodyL,
witsTxL,
)
import Cardano.Ledger.Api.UTxO (ScriptsNeeded)
import Cardano.Ledger.Babbage.Tx (body, getLanguageView, hashScriptIntegrity, wits)
import Cardano.Ledger.Babbage.Tx qualified as Babbage
import Cardano.Ledger.Babbage.TxBody qualified as Babbage
Expand Down Expand Up @@ -242,7 +247,7 @@ data ErrCoverFee
= ErrNotEnoughFunds ChangeError
| ErrNoFuelUTxOFound
| ErrUnknownInput {input :: TxIn}
| ErrScriptExecutionFailed {scriptFailure :: (PlutusPurpose AsIx LedgerEra, TransactionScriptFailure LedgerEra)}
| ErrScriptExecutionFailed {scriptFailure :: Text} -- FIXME: try to avoid Text
| ErrTranslationError (ContextError LedgerEra)
deriving stock (Show)

Expand All @@ -259,13 +264,14 @@ data SomePParams
--
-- XXX: All call sites of this function use cardano-api types
coverFee_ ::
Core.PParams LedgerEra ->
EraCrypto era ~ StandardCrypto =>
PParams era ->
SystemStart ->
EpochInfo (Either Text) ->
Map TxIn TxOut ->
Map TxIn TxOut ->
Babbage.AlonzoTx LedgerEra ->
Either ErrCoverFee (Babbage.AlonzoTx LedgerEra)
Map TxIn (Ledger.TxOut era) ->
Map TxIn (Ledger.TxOut era) ->
Babbage.AlonzoTx era ->
Either ErrCoverFee (Babbage.AlonzoTx era)
coverFee_ pparams systemStart epochInfo lookupUTxO walletUTxO partialTx@Babbage.AlonzoTx{body, wits} = do
(feeTxIn, feeTxOut) <- findUTxOToPayFees walletUTxO

Expand Down Expand Up @@ -314,15 +320,7 @@ coverFee_ pparams systemStart epochInfo lookupUTxO walletUTxO partialTx@Babbage.

-- Compute fee using a body with selected txOut to pay fees (= full change)
-- and an aditional witness (we will sign this tx later)
let fee = case pparams of
(pp :: Ledger.PParams Babbage) -> estimateMinFeeTx pp costingTx additionalWitnesses 0 0
(conwayPParams :: Ledger.PParams Conway) ->
let newTx = case upgradeTx costingTx of
-- TODO: Proper error
Left e -> error $ show e
Right tx -> tx
in estimateMinFeeTx conwayPParams newTx additionalWitnesses 0 0

let fee = estimateMinFeeTx pparams costingTx additionalWitnesses 0 0
costingTx =
unbalancedTx
& bodyTxL . outputsTxBodyL %~ (|> feeTxOut)
Expand Down Expand Up @@ -353,7 +351,6 @@ coverFee_ pparams systemStart epochInfo lookupUTxO walletUTxO partialTx@Babbage.
getAdaValue (Babbage.BabbageTxOut _ value _ _) =
coin value

resolveInput :: TxIn -> Either ErrCoverFee TxOut
resolveInput i = do
case Map.lookup i (lookupUTxO <> walletUTxO) of
Nothing -> Left $ ErrUnknownInput i
Expand Down Expand Up @@ -381,7 +378,7 @@ coverFee_ pparams systemStart epochInfo lookupUTxO walletUTxO partialTx@Babbage.
changeOut = totalIn <> invert totalOut
refScript = SNothing

adjustRedeemers :: Set TxIn -> Set TxIn -> Map (PlutusPurpose AsIx LedgerEra) ExUnits -> Redeemers LedgerEra -> Redeemers LedgerEra
adjustRedeemers :: Set TxIn -> Set TxIn -> Map (PlutusPurpose AsIx era) ExUnits -> Redeemers era -> Redeemers era
adjustRedeemers initialInputs finalInputs estimatedCosts (Redeemers initialRedeemers) =
Redeemers $ Map.fromList $ map adjustOne $ Map.toList initialRedeemers
where
Expand All @@ -397,7 +394,7 @@ coverFee_ pparams systemStart epochInfo lookupUTxO walletUTxO partialTx@Babbage.
_ ->
(ptr, (d, executionUnitsFor ptr))

executionUnitsFor :: PlutusPurpose AsIx LedgerEra -> ExUnits
executionUnitsFor :: PlutusPurpose AsIx era -> ExUnits
executionUnitsFor ptr =
let ExUnits maxMem maxCpu = pparams ^. ppMaxTxExUnitsL
ExUnits totalMem totalCpu = foldMap identity estimatedCosts
Expand All @@ -406,7 +403,7 @@ coverFee_ pparams systemStart epochInfo lookupUTxO walletUTxO partialTx@Babbage.
(floor (maxMem * approxMem % totalMem))
(floor (maxCpu * approxCpu % totalCpu))

findLargestUTxO :: Map TxIn TxOut -> Maybe (TxIn, TxOut)
findLargestUTxO :: Map TxIn (Ledger.TxOut era) -> Maybe (TxIn, Ledger.TxOut era)
findLargestUTxO utxo =
maxLovelaceUTxO apiUtxo
where
Expand All @@ -423,24 +420,22 @@ findLargestUTxO utxo =
-- elements to it like change outputs or script integrity hash may increase that
-- cost a little.
estimateScriptsCost ::
-- FIXME: avoid these constraints
(AlonzoEraTx era, EraPlutusContext era, ScriptsNeeded era ~ AlonzoScriptsNeeded era, EraCrypto era ~ StandardCrypto) =>
-- | Protocol parameters
Core.PParams LedgerEra ->
Core.PParams era ->
-- | Start of the blockchain, for converting slots to UTC times
SystemStart ->
-- | Information about epoch sizes, for converting slots to UTC times
EpochInfo (Either Text) ->
-- | A UTXO needed to resolve inputs
Map TxIn TxOut ->
Map TxIn (Ledger.TxOut era) ->
-- | The pre-constructed transaction
Babbage.AlonzoTx LedgerEra ->
Either ErrCoverFee (Map (PlutusPurpose AsIx LedgerEra) ExUnits)
Tx era ->
Either ErrCoverFee (Map (PlutusPurpose AsIx era) ExUnits)
estimateScriptsCost pparams systemStart epochInfo utxo tx = do
Map.traverseWithKey (\ptr -> left $ ErrScriptExecutionFailed . (ptr,)) result
Map.traverseWithKey (\ptr -> left $ ErrScriptExecutionFailed . show) result
where
result ::
Map
(AlonzoPlutusPurpose AsIx LedgerEra)
(Either (TransactionScriptFailure Babbage) ExUnits)
result =
evalTxExUnits
pparams
Expand Down

0 comments on commit 1a0b956

Please sign in to comment.