Skip to content

Commit

Permalink
Use consistent namespace for Cardano.Api.ShelleyLedgerEra.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanknowles committed Nov 16, 2023
1 parent e9e67fa commit 9b79ff4
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 81 deletions.
88 changes: 47 additions & 41 deletions lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Balance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ import Internal.Cardano.Write.Tx
, RecentEra (..)
, RecentEraConstraints
, RecentEraLedgerConstraints
, ShelleyLedgerEra
, Tx
, TxBody
, TxIn
Expand Down Expand Up @@ -361,19 +360,19 @@ instance Buildable (BuildableInAnyEra a) where
--
data ErrBalanceTxInsufficientCollateralError era =
ErrBalanceTxInsufficientCollateralError
{ largestCombinationAvailable :: UTxO (ShelleyLedgerEra era)
{ largestCombinationAvailable :: UTxO (CardanoApi.ShelleyLedgerEra era)
-- ^ The largest available combination of pure ada UTxOs.
, minimumCollateralAmount :: Coin
-- ^ The minimum quantity of ada necessary for collateral.
}
deriving Generic

deriving instance
RecentEraLedgerConstraints (ShelleyLedgerEra era) =>
RecentEraLedgerConstraints (CardanoApi.ShelleyLedgerEra era) =>
Eq (ErrBalanceTxInsufficientCollateralError era)

deriving instance
RecentEraLedgerConstraints (ShelleyLedgerEra era) =>
RecentEraLedgerConstraints (CardanoApi.ShelleyLedgerEra era) =>
Show (ErrBalanceTxInsufficientCollateralError era)

-- | Indicates that there was not enough ada available to create change outputs.
Expand Down Expand Up @@ -419,7 +418,8 @@ data ErrBalanceTxAssetsInsufficientError = ErrBalanceTxAssetsInsufficientError

data ErrBalanceTxInternalError era
= RecentEraConstraints era =>
ErrUnderestimatedFee Coin (Tx (ShelleyLedgerEra era)) KeyWitnessCount
ErrUnderestimatedFee
Coin (Tx (CardanoApi.ShelleyLedgerEra era)) KeyWitnessCount
| ErrFailedBalancing Value

deriving instance Eq (ErrBalanceTxInternalError era)
Expand All @@ -433,15 +433,19 @@ data ErrBalanceTx era
| ErrBalanceTxExistingCollateral
| ErrBalanceTxExistingTotalCollateral
| ErrBalanceTxExistingReturnCollateral
| RecentEraLedgerConstraints (ShelleyLedgerEra era)
| RecentEraLedgerConstraints (CardanoApi.ShelleyLedgerEra era)
=> ErrBalanceTxInsufficientCollateral
(ErrBalanceTxInsufficientCollateralError era)
| ErrBalanceTxConflictingNetworks
| ErrBalanceTxAssignRedeemers ErrAssignRedeemers
| ErrBalanceTxInternalError (ErrBalanceTxInternalError era)
| RecentEraLedgerConstraints (ShelleyLedgerEra era)
=> ErrBalanceTxInputResolutionConflicts
(NonEmpty (TxOut (ShelleyLedgerEra era), TxOut (ShelleyLedgerEra era)))
| RecentEraLedgerConstraints (CardanoApi.ShelleyLedgerEra era) =>
ErrBalanceTxInputResolutionConflicts
(NonEmpty
( TxOut (CardanoApi.ShelleyLedgerEra era)
, TxOut (CardanoApi.ShelleyLedgerEra era)
)
)
| ErrBalanceTxUnresolvedInputs (NonEmpty TxIn)
| ErrBalanceTxOutputError ErrBalanceTxOutputError
| ErrBalanceTxUnableToCreateChange ErrBalanceTxUnableToCreateChangeError
Expand Down Expand Up @@ -469,23 +473,23 @@ deriving instance Show (ErrBalanceTx era)
-- and instead adjust the existing redeemer indexes ourselves when balancing,
-- even though they are in an "unordered" set.
data PartialTx era = PartialTx
{ tx :: Tx (ShelleyLedgerEra era)
, inputs :: UTxO (ShelleyLedgerEra era)
{ tx :: Tx (CardanoApi.ShelleyLedgerEra era)
, inputs :: UTxO (CardanoApi.ShelleyLedgerEra era)
-- ^ NOTE: Can we rename this to something better? Perhaps 'extraUTxO'?
, redeemers :: [Redeemer]
}
deriving Generic

deriving instance
RecentEraLedgerConstraints (ShelleyLedgerEra era) =>
RecentEraLedgerConstraints (CardanoApi.ShelleyLedgerEra era) =>
Eq (PartialTx era)

deriving instance
RecentEraLedgerConstraints (ShelleyLedgerEra era) =>
RecentEraLedgerConstraints (CardanoApi.ShelleyLedgerEra era) =>
Show (PartialTx era)

instance
RecentEraLedgerConstraints (ShelleyLedgerEra era) =>
RecentEraLedgerConstraints (CardanoApi.ShelleyLedgerEra era) =>
Buildable (PartialTx era)
where
build (PartialTx tx (UTxO ins) redeemers)
Expand All @@ -497,18 +501,18 @@ instance
where
inF = build . show

txF :: Tx (ShelleyLedgerEra era) -> Builder
txF :: Tx (CardanoApi.ShelleyLedgerEra era) -> Builder
txF tx' = pretty $ pShow tx'

data UTxOIndex era = UTxOIndex
{ walletUTxO :: !W.UTxO
, walletUTxOIndex :: !(UTxOIndex.UTxOIndex WalletUTxO)
, ledgerUTxO :: !(UTxO (ShelleyLedgerEra era))
, ledgerUTxO :: !(UTxO (CardanoApi.ShelleyLedgerEra era))
}

constructUTxOIndex
:: forall era. IsRecentEra era
=> UTxO (ShelleyLedgerEra era)
=> UTxO (CardanoApi.ShelleyLedgerEra era)
-> UTxOIndex era
constructUTxOIndex ledgerUTxO =
UTxOIndex {walletUTxO, walletUTxOIndex, ledgerUTxO}
Expand All @@ -520,14 +524,14 @@ constructUTxOIndex ledgerUTxO =
fromWalletUTxO
:: RecentEra era
-> W.UTxO
-> UTxO (ShelleyLedgerEra era)
-> UTxO (CardanoApi.ShelleyLedgerEra era)
fromWalletUTxO era (W.UTxO m) = withConstraints era $ UTxO
$ Map.mapKeys Convert.toLedger
$ Map.map (toLedgerTxOut era) m

toWalletUTxO
:: RecentEra era
-> UTxO (ShelleyLedgerEra era)
-> UTxO (CardanoApi.ShelleyLedgerEra era)
-> W.UTxO
toWalletUTxO era (UTxO m) = withConstraints era $ W.UTxO
$ Map.mapKeys Convert.toWallet
Expand Down Expand Up @@ -872,8 +876,8 @@ balanceTransactionWithSelectionStrategyAndNoZeroAdaAdjustment

guardTxSize
:: KeyWitnessCount
-> Tx (ShelleyLedgerEra era)
-> ExceptT (ErrBalanceTx era) m (Tx (ShelleyLedgerEra era))
-> Tx (CardanoApi.ShelleyLedgerEra era)
-> ExceptT (ErrBalanceTx era) m (Tx (CardanoApi.ShelleyLedgerEra era))
guardTxSize witCount tx =
withConstraints era $ do
let maxSize = W.TxSize (pp ^. ppMaxTxSizeL)
Expand All @@ -882,8 +886,8 @@ balanceTransactionWithSelectionStrategyAndNoZeroAdaAdjustment
pure tx

guardTxBalanced
:: Tx (ShelleyLedgerEra era)
-> ExceptT (ErrBalanceTx era) m (Tx (ShelleyLedgerEra era))
:: Tx (CardanoApi.ShelleyLedgerEra era)
-> ExceptT (ErrBalanceTx era) m (Tx (CardanoApi.ShelleyLedgerEra era))
guardTxBalanced tx = do
let bal = txBalance tx
if bal == mempty
Expand All @@ -892,14 +896,14 @@ balanceTransactionWithSelectionStrategyAndNoZeroAdaAdjustment
$ ErrBalanceTxInternalError
$ ErrFailedBalancing bal

txBalance :: Tx (ShelleyLedgerEra era) -> Value
txBalance :: Tx (CardanoApi.ShelleyLedgerEra era) -> Value
txBalance
= withConstraints era
. evaluateTransactionBalance era pp combinedUTxO
. txBody era

balanceAfterSettingMinFee
:: Tx (ShelleyLedgerEra era)
:: Tx (CardanoApi.ShelleyLedgerEra era)
-> ExceptT (ErrBalanceTx era) m
(CardanoApi.Value, CardanoApi.Lovelace, KeyWitnessCount)
balanceAfterSettingMinFee tx = ExceptT . pure $ do
Expand Down Expand Up @@ -928,7 +932,7 @@ balanceTransactionWithSelectionStrategyAndNoZeroAdaAdjustment
-- NOTE: Representing the wallet utxo as a @CardanoApi.UTxO@ will not make
-- this check easier, even if it may be useful in other regards.
guardWalletUTxOConsistencyWith
:: UTxO (ShelleyLedgerEra era)
:: UTxO (CardanoApi.ShelleyLedgerEra era)
-> ExceptT (ErrBalanceTx era) m ()
guardWalletUTxOConsistencyWith u' = do
let W.UTxO u = toWalletUTxO (recentEra @era) u'
Expand All @@ -945,7 +949,7 @@ balanceTransactionWithSelectionStrategyAndNoZeroAdaAdjustment
$ withConstraints era
$ ErrBalanceTxInputResolutionConflicts (c :| cs)

combinedUTxO :: UTxO (ShelleyLedgerEra era)
combinedUTxO :: UTxO (CardanoApi.ShelleyLedgerEra era)
combinedUTxO = withConstraints era $ mconcat
-- The @CardanoApi.UTxO@ can contain strictly more information than
-- @W.UTxO@. Therefore we make the user-specified @inputUTxO@ to take
Expand All @@ -964,22 +968,22 @@ balanceTransactionWithSelectionStrategyAndNoZeroAdaAdjustment
$ outputs era
$ txBody era tx
where
fromLedgerTxOut :: TxOut (ShelleyLedgerEra era) -> W.TxOut
fromLedgerTxOut :: TxOut (CardanoApi.ShelleyLedgerEra era) -> W.TxOut
fromLedgerTxOut o = case era of
RecentEraBabbage -> Convert.fromBabbageTxOut o
RecentEraConway -> Convert.fromConwayTxOut o

assembleTransaction
:: TxUpdate
-> ExceptT (ErrBalanceTx era) m (Tx (ShelleyLedgerEra era))
-> ExceptT (ErrBalanceTx era) m (Tx (CardanoApi.ShelleyLedgerEra era))
assembleTransaction update = ExceptT . pure $ do
tx' <- left ErrBalanceTxUpdateError $ updateTx era partialTx update
left ErrBalanceTxAssignRedeemers $
assignScriptRedeemers
era pp timeTranslation combinedUTxO redeemers tx'

guardExistingCollateral
:: Tx (ShelleyLedgerEra era)
:: Tx (CardanoApi.ShelleyLedgerEra era)
-> ExceptT (ErrBalanceTx era) m ()
guardExistingCollateral tx = withConstraints era $ do
-- Coin selection does not support pre-defining collateral. In Sep 2021
Expand All @@ -991,7 +995,7 @@ balanceTransactionWithSelectionStrategyAndNoZeroAdaAdjustment
throwE ErrBalanceTxExistingCollateral

guardExistingTotalCollateral
:: Tx (ShelleyLedgerEra era)
:: Tx (CardanoApi.ShelleyLedgerEra era)
-> ExceptT (ErrBalanceTx era) m ()
guardExistingTotalCollateral tx = withConstraints era $ do
let totColl = tx ^. (bodyTxL . totalCollateralTxBodyL)
Expand All @@ -1000,7 +1004,7 @@ balanceTransactionWithSelectionStrategyAndNoZeroAdaAdjustment
SJust _ -> throwE ErrBalanceTxExistingTotalCollateral

guardExistingReturnCollateral
:: Tx (ShelleyLedgerEra era)
:: Tx (CardanoApi.ShelleyLedgerEra era)
-> ExceptT (ErrBalanceTx era) m ()
guardExistingReturnCollateral tx = withConstraints era $ do
let collRet = tx ^. (bodyTxL . collateralReturnTxBodyL)
Expand Down Expand Up @@ -1115,7 +1119,7 @@ selectAssets era (ProtocolParameters pp) utxoAssumptions outs redeemers
=> RecentEra era
-> W.Address
-> W.TokenBundle
-> TxOut (ShelleyLedgerEra era)
-> TxOut (CardanoApi.ShelleyLedgerEra era)
mkLedgerTxOut txOutEra address bundle =
case txOutEra of
RecentEraBabbage -> Convert.toBabbageTxOut txOut
Expand Down Expand Up @@ -1271,9 +1275,9 @@ newtype ErrUpdateSealedTx
-- be used to *add* tx body content.
updateTx
:: forall era. RecentEra era
-> Tx (ShelleyLedgerEra era)
-> Tx (CardanoApi.ShelleyLedgerEra era)
-> TxUpdate
-> Either ErrUpdateSealedTx (Tx (ShelleyLedgerEra era))
-> Either ErrUpdateSealedTx (Tx (CardanoApi.ShelleyLedgerEra era))
updateTx era tx extraContent = withConstraints era $ do
let tx' = tx
& over bodyTxL (modifyShelleyTxBody extraContent era)
Expand All @@ -1292,7 +1296,9 @@ updateTx era tx extraContent = withConstraints era $ do
TxUpdate _ _ _ extraInputScripts _ = extraContent

extraInputScripts'
:: Map (ScriptHash StandardCrypto) (Script (ShelleyLedgerEra era))
:: Map
(ScriptHash StandardCrypto)
(Script (CardanoApi.ShelleyLedgerEra era))
extraInputScripts' = withConstraints era $
Map.fromList $ map (pairWithHash . convert) extraInputScripts
where
Expand All @@ -1302,16 +1308,16 @@ updateTx era tx extraContent = withConstraints era $ do
toLedgerScript
:: CA.Script CA.KeyHash
-> RecentEra era
-> Core.Script (ShelleyLedgerEra era)
-> Core.Script (CardanoApi.ShelleyLedgerEra era)
toLedgerScript s = \case
RecentEraBabbage -> TimelockScript $ Convert.toLedgerTimelockScript s
RecentEraConway -> TimelockScript $ Convert.toLedgerTimelockScript s

modifyShelleyTxBody
:: forall era. TxUpdate
-> RecentEra era
-> TxBody (ShelleyLedgerEra era)
-> TxBody (ShelleyLedgerEra era)
-> TxBody (CardanoApi.ShelleyLedgerEra era)
-> TxBody (CardanoApi.ShelleyLedgerEra era)
modifyShelleyTxBody txUpdate era = withConstraints era $
over feeTxBodyL modifyFee
. over outputsTxBodyL
Expand Down Expand Up @@ -1551,15 +1557,15 @@ toLedgerTxOut
:: HasCallStack
=> RecentEra era
-> W.TxOut
-> TxOut (ShelleyLedgerEra era)
-> TxOut (CardanoApi.ShelleyLedgerEra era)
toLedgerTxOut txOutEra txOut =
case txOutEra of
RecentEraBabbage -> Convert.toBabbageTxOut txOut
RecentEraConway -> Convert.toConwayTxOut txOut

toWalletTxOut
:: RecentEra era
-> TxOut (ShelleyLedgerEra era)
-> TxOut (CardanoApi.ShelleyLedgerEra era)
-> W.TxOut
toWalletTxOut RecentEraBabbage = Convert.fromBabbageTxOut
toWalletTxOut RecentEraConway = Convert.fromConwayTxOut
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ import Data.IntCast
import Internal.Cardano.Write.Tx
( PParams
, RecentEra
, ShelleyLedgerEra
, Value
, Version
, withConstraints
)

import qualified Cardano.Api.Shelley as CardanoApi
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as W
( TokenBundle
)
Expand All @@ -53,7 +53,7 @@ import qualified Data.ByteString.Lazy as BL
--
mkTokenBundleSizeAssessor
:: RecentEra era
-> PParams (ShelleyLedgerEra era)
-> PParams (CardanoApi.ShelleyLedgerEra era)
-> TokenBundleSizeAssessor
mkTokenBundleSizeAssessor era pp = TokenBundleSizeAssessor $ \tb ->
if computeTokenBundleSerializedLengthBytes tb ver > maxValSize
Expand Down
Loading

0 comments on commit 9b79ff4

Please sign in to comment.