Skip to content

Commit

Permalink
Merge pull request #1206 from input-output-hk/lc/outputsTxBodyL
Browse files Browse the repository at this point in the history
Replace use of outputs' with view outputsTxBodyL
  • Loading branch information
locallycompact committed Dec 12, 2023
2 parents ea51f67 + 10e5994 commit 6ba4954
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 0 deletions hydra-cardano-api/hydra-cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ library
, cardano-ledger-mary >=1.3.1 && <1.4
, cardano-ledger-shelley >=1.7 && <1.8
, containers
, lens
, plutus-ledger-api >=1.15.0.1 && <1.16
, QuickCheck
, serialise
Expand Down
4 changes: 3 additions & 1 deletion hydra-cardano-api/src/Hydra/Cardano/Api/UTxO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import Hydra.Cardano.Api.TxIn (fromLedgerTxIn, toLedgerTxIn, txIns')
import Hydra.Cardano.Api.TxOut (fromLedgerTxOut, toLedgerTxOut)

import Cardano.Api.UTxO qualified as UTxO
import Cardano.Ledger.Api (outputsTxBodyL)
import Cardano.Ledger.Babbage.TxBody qualified as Ledger
import Cardano.Ledger.BaseTypes qualified as Ledger
import Cardano.Ledger.Shelley.UTxO qualified as Ledger
import Cardano.Ledger.TxIn qualified as Ledger
import Control.Lens ((^.))
import Data.Foldable (toList)
import Data.Map qualified as Map
import Data.Maybe (mapMaybe)
Expand All @@ -26,7 +28,7 @@ renderUTxO =
-- outputs they correspond to.
utxoFromTx :: Tx Era -> UTxO
utxoFromTx (Tx body@(ShelleyTxBody _ ledgerBody _ _ _ _) _) =
let txOuts = toList $ Ledger.outputs' ledgerBody
let txOuts = toList $ ledgerBody ^. outputsTxBodyL
txIns =
[ Ledger.TxIn (toLedgerTxId $ getTxId body) ix
| ix <- [Ledger.TxIx 0 .. toEnum (length txOuts)]
Expand Down
1 change: 0 additions & 1 deletion hydra-cardano-api/src/Hydra/Cardano/Api/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Cardano.Ledger.Core (getMinCoinTxOut)
import Cardano.Ledger.Mary.Value qualified as Ledger
import Data.Word (Word64)
import Hydra.Cardano.Api.CtxUTxO (ToUTxOContext (..))
import Hydra.Cardano.Api.Hash (unsafeScriptHashFromBytes)
import Hydra.Cardano.Api.MaryEraOnwards (maryEraOnwards)
import Hydra.Cardano.Api.PolicyId (fromPlutusCurrencySymbol)
import PlutusLedgerApi.V1.Value (flattenValue)
Expand Down
4 changes: 2 additions & 2 deletions hydra-node/src/Hydra/Chain/Direct/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Cardano.Ledger.Alonzo.TxWits (AlonzoTxWits (..), RdmrPtr (RdmrPtr), Redee
import Cardano.Ledger.Api (TransactionScriptFailure, ensureMinCoinTxOut, evalTxExUnits, outputsTxBodyL, ppMaxTxExUnitsL, ppPricesL)
import Cardano.Ledger.Babbage.Tx (body, getLanguageView, hashScriptIntegrity, wits)
import Cardano.Ledger.Babbage.Tx qualified as Babbage
import Cardano.Ledger.Babbage.TxBody (BabbageTxBody (..), outputs', spendInputs')
import Cardano.Ledger.Babbage.TxBody (BabbageTxBody (..), spendInputs')
import Cardano.Ledger.Babbage.TxBody qualified as Babbage
import Cardano.Ledger.Babbage.UTxO (getReferenceScripts)
import Cardano.Ledger.BaseTypes qualified as Ledger
Expand Down Expand Up @@ -193,7 +193,7 @@ applyTxs txs isOurs utxo =
let txId = getTxId tx
modify (`Map.withoutKeys` spendInputs' (body tx))
let indexedOutputs =
let outs = toList $ outputs' (body tx)
let outs = toList $ body tx ^. outputsTxBodyL
maxIx = fromIntegral $ length outs
in zip [Ledger.TxIx ix | ix <- [0 .. maxIx]] outs
forM_ indexedOutputs $ \(ix, out@(Babbage.BabbageTxOut addr _ _ _)) ->
Expand Down
4 changes: 3 additions & 1 deletion hydra-node/src/Hydra/Ledger/Cardano/Json.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Cardano.Ledger.Allegra.Scripts qualified as Ledger
import Cardano.Ledger.Alonzo.Scripts qualified as Ledger
import Cardano.Ledger.Alonzo.TxAuxData qualified as Ledger
import Cardano.Ledger.Alonzo.TxWits qualified as Ledger
import Cardano.Ledger.Api (outputsTxBodyL)
import Cardano.Ledger.Babbage.Tx qualified as Ledger
import Cardano.Ledger.Babbage.TxBody qualified as Ledger
import Cardano.Ledger.BaseTypes (StrictMaybe (..), isSJust)
Expand All @@ -41,6 +42,7 @@ import Cardano.Ledger.SafeHash qualified as Ledger
import Cardano.Ledger.Shelley.API qualified as Ledger
import Cardano.Ledger.Shelley.TxCert qualified as Ledger
import Codec.Binary.Bech32 qualified as Bech32
import Control.Lens ((^.))
import Data.Aeson (
FromJSONKey (fromJSONKey),
FromJSONKeyFunction (FromJSONKeyTextParser),
Expand Down Expand Up @@ -217,7 +219,7 @@ instance ToJSON (Ledger.BabbageTxBody LedgerEra) where
[ onlyIf (const True) "inputs" (Set.map fromLedgerTxIn (Ledger.spendInputs' b))
, onlyIf (not . null) "collateral" (Set.map fromLedgerTxIn (Ledger.collateralInputs' b))
, onlyIf (not . null) "referenceInputs" (Set.map fromLedgerTxIn (Ledger.referenceInputs' b))
, onlyIf (const True) "outputs" (fromLedgerTxOut <$> Ledger.outputs' b)
, onlyIf (const True) "outputs" (fromLedgerTxOut <$> b ^. outputsTxBodyL)
, onlyIf isSJust "collateralReturn" (fromLedgerTxOut <$> Ledger.collateralReturn' b)
, onlyIf isSJust "totalCollateral" (Ledger.totalCollateral' b)
, onlyIf (not . null) "certificates" (Ledger.certs' b)
Expand Down
4 changes: 3 additions & 1 deletion hydra-node/test/Hydra/Chain/Direct/Contract/Mutation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,14 @@ import Cardano.Api.UTxO qualified as UTxO
import Cardano.Ledger.Alonzo.Scripts qualified as Ledger
import Cardano.Ledger.Alonzo.Scripts.Data qualified as Ledger
import Cardano.Ledger.Alonzo.TxWits qualified as Ledger
import Cardano.Ledger.Api (outputsTxBodyL)
import Cardano.Ledger.Babbage.TxBody qualified as Ledger
import Cardano.Ledger.Binary (mkSized)
import Cardano.Ledger.Core qualified as Ledger
import Cardano.Ledger.Credential (Credential (..))
import Cardano.Ledger.Mary.Value qualified as Ledger
import Control.Exception (assert)
import Control.Lens ((^.))
import Data.Map qualified as Map
import Data.Sequence.Strict qualified as StrictSeq
import Data.Set qualified as Set
Expand Down Expand Up @@ -660,7 +662,7 @@ alterTxOuts fn tx =

ledgerOutputs' = StrictSeq.fromList . map (mkSized ledgerEraVersion . toLedgerTxOut . toCtxUTxOTxOut) $ outputs'

outputs' = fn . fmap fromLedgerTxOut . toList $ Ledger.outputs' ledgerBody
outputs' = fn . fmap fromLedgerTxOut . toList $ ledgerBody ^. outputsTxBodyL

scriptData' = ensureDatums outputs' scriptData

Expand Down
12 changes: 6 additions & 6 deletions hydra-node/test/Hydra/Chain/Direct/WalletSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Test.Hydra.Prelude

import Cardano.Ledger.Api (bodyTxL, coinTxOutL, outputsTxBodyL)
import Cardano.Ledger.Babbage.Tx (AlonzoTx (..))
import Cardano.Ledger.Babbage.TxBody (BabbageTxBody (..), BabbageTxOut (..), outputs')
import Cardano.Ledger.Babbage.TxBody (BabbageTxBody (..), BabbageTxOut (..))
import Cardano.Ledger.BaseTypes qualified as Ledger
import Cardano.Ledger.Binary (mkSized)
import Cardano.Ledger.Coin (Coin (..))
Expand All @@ -16,7 +16,7 @@ import Cardano.Ledger.SafeHash qualified as SafeHash
import Cardano.Ledger.Shelley.API qualified as Ledger
import Cardano.Ledger.Val (Val (..), invert)
import Control.Concurrent (newEmptyMVar, putMVar, takeMVar)
import Control.Lens ((.~), (<>~), (^.))
import Control.Lens (view, (.~), (<>~), (^.))
import Control.Tracer (nullTracer)
import Data.Map.Strict qualified as Map
import Data.Sequence.Strict qualified as StrictSeq
Expand Down Expand Up @@ -322,9 +322,9 @@ genTxsSpending utxo = scale (round @Double . sqrt . fromIntegral) $ do

genUTxO :: Gen (Map TxIn TxOut)
genUTxO = do
tx <- arbitrary `suchThat` (Prelude.not . Prelude.null . outputs' . body)
tx <- arbitrary `suchThat` (Prelude.not . Prelude.null . view outputsTxBodyL . body)
txIn <- toLedgerTxIn <$> genTxIn
let txOut = scaleAda $ Prelude.head $ toList $ outputs' $ body tx
let txOut = scaleAda $ Prelude.head $ toList $ body tx ^. outputsTxBodyL
pure $ Map.singleton txIn txOut
where
scaleAda :: TxOut -> TxOut
Expand Down Expand Up @@ -354,7 +354,7 @@ allTxIns txs =

allTxOuts :: [Tx LedgerEra] -> [TxOut]
allTxOuts txs =
toList $ mconcat (outputs' . body <$> txs)
toList $ mconcat (view outputsTxBodyL . body <$> txs)

isOurs :: Map TxIn TxOut -> Address -> Bool
isOurs utxo addr =
Expand Down Expand Up @@ -390,4 +390,4 @@ knownInputBalance utxo = foldMap resolve . toList . btbInputs . body
-- | NOTE: This does not account for deposits
outputBalance :: Tx LedgerEra -> Value LedgerEra
outputBalance =
foldMap getValue . outputs' . body
foldMap getValue . view outputsTxBodyL . body

0 comments on commit 6ba4954

Please sign in to comment.