Skip to content

Commit

Permalink
Move update(Sealed)Tx out of TxLayer
Browse files Browse the repository at this point in the history
  • Loading branch information
Anviking committed Mar 22, 2023
1 parent 3700fe8 commit e7fefb4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 38 deletions.
32 changes: 26 additions & 6 deletions lib/wallet/src/Cardano/Wallet/Shelley/Transaction.hs
Expand Up @@ -37,7 +37,8 @@ module Cardano.Wallet.Shelley.Transaction
-- * Updating SealedTx
, TxUpdate (..)
, noTxUpdate
, updateSealedTx
, updateTx
, TxFeeUpdate (..)

-- * For balancing (To be moved)
, maxScriptExecutionCost
Expand Down Expand Up @@ -690,8 +691,6 @@ newTransactionLayer networkId = TransactionLayer
, constraints = \era pp -> txConstraints era pp (txWitnessTagFor @k)

, decodeTx = _decodeSealedTx

, updateTx = updateSealedTx
}

_decodeSealedTx
Expand Down Expand Up @@ -725,14 +724,35 @@ mkDelegationCertificates da accXPub =
Quit -> [toStakeKeyDeregCert accXPub]


-- | Describes modifications that can be made to a `Tx` using `updateTx`.
data TxUpdate = TxUpdate
{ extraInputs :: [(TxIn, TxOut)]
, extraCollateral :: [TxIn]
-- ^ Only used in the Alonzo era and later. Will be silently ignored in
-- previous eras.
, extraOutputs :: [TxOut]
, extraInputScripts :: [Script KeyHash]
, feeUpdate :: TxFeeUpdate
-- ^ Set a new fee or use the old one.
}

-- | For testing that
-- @
-- forall tx. updateSealedTx noTxUpdate tx
-- forall tx. updateTx noTxUpdate tx
-- == Right tx or Left
-- @
noTxUpdate :: TxUpdate
noTxUpdate = TxUpdate [] [] [] [] UseOldTxFee

-- | Method to use when updating the fee of a transaction.
data TxFeeUpdate = UseOldTxFee
-- ^ Instead of updating the fee, just use the old fee of the
-- Tx (no-op for fee update).
| UseNewTxFee Coin
-- ^ Specify a new fee to use instead.
deriving (Eq, Show)


-- Used to add inputs and outputs when balancing a transaction.
--
-- If the transaction contains existing key witnesses, it will return `Left`,
Expand All @@ -745,12 +765,12 @@ noTxUpdate = TxUpdate [] [] [] [] UseOldTxFee
--
-- To avoid the need for `ledger -> wallet` conversions, this function can only
-- be used to *add* tx body content.
updateSealedTx
updateTx
:: forall era. Cardano.IsShelleyBasedEra era
=> Cardano.Tx era
-> TxUpdate
-> Either ErrUpdateSealedTx (Cardano.Tx era)
updateSealedTx (Cardano.Tx body existingKeyWits) extraContent = do
updateTx (Cardano.Tx body existingKeyWits) extraContent = do
-- NOTE: The script witnesses are carried along with the cardano-api
-- `anyEraBody`.
body' <- modifyTxBody extraContent body
Expand Down
29 changes: 0 additions & 29 deletions lib/wallet/src/Cardano/Wallet/Transaction.hs
Expand Up @@ -31,8 +31,6 @@ module Cardano.Wallet.Transaction
, defaultTransactionCtx
, Withdrawal (..)
, withdrawalToCoin
, TxUpdate (..)
, TxFeeUpdate(..)
, TokenMapWithScripts (..)
, emptyTokenMapWithScripts
, AnyExplicitScript (..)
Expand Down Expand Up @@ -249,33 +247,6 @@ data TransactionLayer k ktype tx = TransactionLayer
, WitnessCount
)
-- ^ Decode an externally-created transaction.

, updateTx
:: forall era. Cardano.IsShelleyBasedEra era
=> Cardano.Tx era
-> TxUpdate
-> Either ErrUpdateSealedTx (Cardano.Tx era)
-- ^ Update tx by adding additional inputs and outputs
}

-- | Method to use when updating the fee of a transaction.
data TxFeeUpdate = UseOldTxFee
-- ^ Instead of updating the fee, just use the old fee of the
-- Tx (no-op for fee update).
| UseNewTxFee Coin
-- ^ Specify a new fee to use instead.
deriving (Eq, Show)

-- | Describes modifications that can be made to a `Tx` using `updateTx`.
data TxUpdate = TxUpdate
{ extraInputs :: [(TxIn, TxOut)]
, extraCollateral :: [TxIn]
-- ^ Only used in the Alonzo era and later. Will be silently ignored in
-- previous eras.
, extraOutputs :: [TxOut]
, extraInputScripts :: [Script KeyHash]
, feeUpdate :: TxFeeUpdate
-- ^ Set a new fee or use the old one.
}

type TxValidityInterval = (Maybe SlotNo, SlotNo)
Expand Down
6 changes: 3 additions & 3 deletions lib/wallet/src/Cardano/Wallet/Write/Tx/Balance.hs
Expand Up @@ -82,6 +82,7 @@ import Cardano.Wallet.Shelley.Compatibility.Ledger
( toWalletCoin )
import Cardano.Wallet.Shelley.Transaction
( KeyWitnessCount (..)
, TxFeeUpdate (..)
, TxUpdate (..)
, assignScriptRedeemers
, distributeSurplus
Expand All @@ -96,7 +97,6 @@ import Cardano.Wallet.Transaction
, TransactionCtx (..)
, TransactionLayer (..)
, TxFeeAndChange (..)
, TxFeeUpdate (UseNewTxFee)
, WitnessCount (verificationKey)
, WitnessCountCtx (..)
, defaultTransactionCtx
Expand Down Expand Up @@ -766,7 +766,7 @@ balanceTransactionWithSelectionStrategyAndNoZeroAdaAdjustment
let minfee = toWalletCoin $ Write.Tx.evaluateMinimumFee
(recentEra @era) ledgerPP (Write.Tx.fromCardanoTx tx) witCount
let update = TxUpdate [] [] [] [] (UseNewTxFee minfee)
tx' <- left ErrBalanceTxUpdateError $ updateTx txLayer tx update
tx' <- left ErrBalanceTxUpdateError $ updateTx tx update
let balance = txBalance tx'
let minfee' = Cardano.Lovelace $ fromIntegral $ W.unCoin minfee
return (balance, minfee', witCount)
Expand Down Expand Up @@ -820,7 +820,7 @@ balanceTransactionWithSelectionStrategyAndNoZeroAdaAdjustment
:: TxUpdate
-> ExceptT ErrBalanceTx m (Cardano.Tx era)
assembleTransaction update = ExceptT . pure $ do
tx' <- left ErrBalanceTxUpdateError $ updateTx txLayer partialTx update
tx' <- left ErrBalanceTxUpdateError $ updateTx partialTx update
left ErrBalanceTxAssignRedeemers $ assignScriptRedeemers
nodePParams ti combinedUTxO redeemers tx'

Expand Down

0 comments on commit e7fefb4

Please sign in to comment.