From 32cbe41fbecdd84bacf62813eff2b83ff5550942 Mon Sep 17 00:00:00 2001 From: Duncan Coutts Date: Fri, 27 Dec 2019 17:23:39 +0100 Subject: [PATCH] Add and use compactTxOut{Address,Value} projections For when we want just one component of the CompactTxOut without uncompacting the other (e.g. computing the utxo balance). --- cardano-ledger/src/Cardano/Chain/UTxO/Compact.hs | 10 ++++++++++ cardano-ledger/src/Cardano/Chain/UTxO/UTxO.hs | 4 +--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cardano-ledger/src/Cardano/Chain/UTxO/Compact.hs b/cardano-ledger/src/Cardano/Chain/UTxO/Compact.hs index 1df96fc1..d427b50c 100644 --- a/cardano-ledger/src/Cardano/Chain/UTxO/Compact.hs +++ b/cardano-ledger/src/Cardano/Chain/UTxO/Compact.hs @@ -28,6 +28,8 @@ module Cardano.Chain.UTxO.Compact , CompactTxOut(..) , toCompactTxOut , fromCompactTxOut + , compactTxOutAddress + , compactTxOutValue ) where @@ -41,6 +43,7 @@ import qualified Data.ByteArray as BA (convert) import qualified Data.ByteString.Lazy as BSL (fromStrict, toStrict) import Cardano.Binary (FromCBOR(..), ToCBOR(..), encodeListLen, enforceSize) +import Cardano.Chain.Common (Address) import Cardano.Chain.Common.Compact (CompactAddress, fromCompactAddress, toCompactAddress) import Cardano.Chain.Common.Lovelace @@ -259,3 +262,10 @@ toCompactLovelace = CompactLovelace . fromIntegral . lovelaceToNatural fromCompactLovelace :: CompactLovelace -> Lovelace fromCompactLovelace (CompactLovelace n) = naturalToLovelace (fromIntegral n) + +compactTxOutAddress :: CompactTxOut -> Address +compactTxOutAddress (CompactTxOut addr _) = fromCompactAddress addr + +compactTxOutValue :: CompactTxOut -> Lovelace +compactTxOutValue (CompactTxOut _ lovelace) = fromCompactLovelace lovelace + diff --git a/cardano-ledger/src/Cardano/Chain/UTxO/UTxO.hs b/cardano-ledger/src/Cardano/Chain/UTxO/UTxO.hs index 57201cdb..09777b2d 100644 --- a/cardano-ledger/src/Cardano/Chain/UTxO/UTxO.hs +++ b/cardano-ledger/src/Cardano/Chain/UTxO/UTxO.hs @@ -55,6 +55,7 @@ import Cardano.Chain.UTxO.Compact , fromCompactTxOut , toCompactTxIn , toCompactTxOut + , compactTxOutValue ) import Cardano.Crypto (hash) @@ -137,9 +138,6 @@ concat = foldM union empty balance :: UTxO -> Lovelace balance = foldMap compactTxOutValue . unUTxO - where - compactTxOutValue :: CompactTxOut -> Lovelace - compactTxOutValue = txOutValue . fromCompactTxOut (<|) :: Set TxIn -> UTxO -> UTxO (<|) inputs = UTxO . flip M.restrictKeys compactInputs . unUTxO