Skip to content

Commit

Permalink
Remove usages of Empty and Solo.
Browse files Browse the repository at this point in the history
These types were introduced because we parameterized the **container type**
for change in `TxFeeAndChange`:

```hs
data TxFeeAndChange f = TxFeeAndChange
    { fee :: Coin
    , change :: f Coin
    }
```

This required us to instantiate `f` with types of kind `* -> *`.

But later on we parameterized the type of `change` so that it can be any type
of kind `*`:

```hs
data TxFeeAndChange change = TxFeeAndChange
    { fee :: Coin
    , change :: change
    }
```

Hence we can make the following simplifications:

  - Empty Void -> ()
  - Solo  Coin -> Coin

And therefore we can remove the `Empty` and `Solo` types.

In response to review discussion:

#3238 (comment)
  • Loading branch information
jonathanknowles committed Apr 20, 2022
1 parent 498f731 commit b6d2189
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 39 deletions.
22 changes: 0 additions & 22 deletions lib/core/src/Cardano/Wallet/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ module Cardano.Wallet.Util
-- * HTTP(S) URIs
, uriToText
, parseURI

-- * Containers
, Empty (..)
, Solo (..)
) where

import Prelude
Expand Down Expand Up @@ -173,21 +169,3 @@ parseURI (T.unpack -> uri) = runIdentity $ runExceptT $ do
Left "URI must not contain a path/query/fragment."
_ -> Right uri'
either (throwE . TextDecodingError) pure res

--------------------------------------------------------------------------------
-- Containers
--------------------------------------------------------------------------------

-- | A container that always has no elements.
--
-- This type is the conceptual opposite of 'NonEmpty'.
--
data Empty a = Empty
deriving (Eq, Foldable, Functor, Ord, Show, Traversable)

-- | A container that always has exactly one element.
--
-- This type is equivalent to the singleton tuple.
--
data Solo a = Solo {unSolo :: a}
deriving (Eq, Foldable, Functor, Ord, Show, Traversable)
32 changes: 15 additions & 17 deletions lib/shelley/src/Cardano/Wallet/Shelley/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ import Cardano.Wallet.Transaction
, withdrawalToCoin
)
import Cardano.Wallet.Util
( Empty (..), Solo (..), internalError, modifyM )
( internalError, modifyM )
import Codec.Serialise
( deserialiseOrFail )
import Control.Arrow
Expand Down Expand Up @@ -231,8 +231,6 @@ import Data.Set
( Set )
import Data.Type.Equality
( type (==) )
import Data.Void
( Void )
import Data.Word
( Word16, Word64, Word8 )
import GHC.Generics
Expand Down Expand Up @@ -1525,22 +1523,22 @@ distributeSurplusDelta feePolicy surplus (TxFeeAndChange fee change) =
case listToMaybe change of
Just firstChange ->
distributeSurplusDeltaWithOneChangeCoin feePolicy surplus
(TxFeeAndChange fee (Solo firstChange))
(TxFeeAndChange fee firstChange)
<&> mapTxFeeAndChange id
((: replicate (length change - 1) (Coin 0)) . unSolo)
((: replicate (length change - 1) (Coin 0)))
Nothing ->
burnSurplusAsFees feePolicy surplus
(TxFeeAndChange fee Empty)
(TxFeeAndChange fee ())
<&> mapTxFeeAndChange id
(\Empty -> [])
(\() -> [])

distributeSurplusDeltaWithOneChangeCoin
:: FeePolicy
-> Coin -- ^ Surplus to distribute
-> TxFeeAndChange (Solo Coin)
-> Either ErrMoreSurplusNeeded (TxFeeAndChange (Solo Coin))
-> TxFeeAndChange Coin
-> Either ErrMoreSurplusNeeded (TxFeeAndChange Coin)
distributeSurplusDeltaWithOneChangeCoin
feePolicy surplus fc@(TxFeeAndChange fee0 (Solo change0)) =
feePolicy surplus fc@(TxFeeAndChange fee0 change0) =
let
-- We calculate the maximum possible fee increase, by assuming the
-- **entire** surplus is added to the change.
Expand All @@ -1551,15 +1549,15 @@ distributeSurplusDeltaWithOneChangeCoin
Just extraChange ->
Right $ TxFeeAndChange
{ fee = extraFee
, change = Solo extraChange
, change = extraChange
}
Nothing ->
-- The fee increase from adding the surplus to the change was
-- greater than the surplus itself. This could happen if the
-- surplus is small.
burnSurplusAsFees feePolicy surplus
(mapTxFeeAndChange id (const Empty) fc)
<&> mapTxFeeAndChange id (\Empty -> Solo (Coin 0))
(mapTxFeeAndChange id (const ()) fc)
<&> mapTxFeeAndChange id (\() -> Coin 0)
where
-- Increasing the fee may itself increase the fee. If that is the case, this
-- function will increase the fee further. The process repeats until the fee
Expand Down Expand Up @@ -1609,13 +1607,13 @@ distributeSurplusDeltaWithOneChangeCoin
burnSurplusAsFees
:: FeePolicy
-> Coin -- Surplus
-> TxFeeAndChange (Empty Void)
-> Either ErrMoreSurplusNeeded (TxFeeAndChange (Empty Void))
burnSurplusAsFees feePolicy surplus (TxFeeAndChange fee0 _) =
-> TxFeeAndChange ()
-> Either ErrMoreSurplusNeeded (TxFeeAndChange ())
burnSurplusAsFees feePolicy surplus (TxFeeAndChange fee0 ()) =
case costOfBurningSurplus `Coin.subtract` surplus of
Just shortfall -> Left $ ErrMoreSurplusNeeded shortfall
Nothing ->
Right $ TxFeeAndChange surplus Empty
Right $ TxFeeAndChange surplus ()
where
costOfBurningSurplus = costOfIncreasingCoin feePolicy fee0 surplus

Expand Down

0 comments on commit b6d2189

Please sign in to comment.