Skip to content
This repository has been archived by the owner on Feb 9, 2021. It is now read-only.

Commit

Permalink
Add and use compactTxOut{Address,Value} projections
Browse files Browse the repository at this point in the history
For when we want just one component of the CompactTxOut without
uncompacting the other (e.g. computing the utxo balance).
  • Loading branch information
dcoutts committed Dec 28, 2019
1 parent 8ab5f15 commit 32cbe41
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 10 additions & 0 deletions cardano-ledger/src/Cardano/Chain/UTxO/Compact.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ module Cardano.Chain.UTxO.Compact
, CompactTxOut(..)
, toCompactTxOut
, fromCompactTxOut
, compactTxOutAddress
, compactTxOutValue
)
where

Expand All @@ -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
Expand Down Expand Up @@ -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

4 changes: 1 addition & 3 deletions cardano-ledger/src/Cardano/Chain/UTxO/UTxO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import Cardano.Chain.UTxO.Compact
, fromCompactTxOut
, toCompactTxIn
, toCompactTxOut
, compactTxOutValue
)
import Cardano.Crypto (hash)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 32cbe41

Please sign in to comment.