Skip to content

Commit 496ce57

Browse files
authored
Merge pull request #4981 from IntersectMBO/nm/4567-move-compactcoin-addsub
Move add/sum functions for `CompactCoin` to `Cardano.Ledger.Coin`
2 parents 298cae3 + 359e640 commit 496ce57

File tree

7 files changed

+46
-26
lines changed

7 files changed

+46
-26
lines changed

eras/conway/impl/src/Cardano/Ledger/Conway/Governance/DRepPulser.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import Cardano.Ledger.Binary.Coders (
5858
(!>),
5959
(<!),
6060
)
61-
import Cardano.Ledger.Coin (Coin (..))
61+
import Cardano.Ledger.Coin (Coin (..), addCompactCoin)
6262
import Cardano.Ledger.Conway.Era (ConwayRATIFY)
6363
import Cardano.Ledger.Conway.Governance.Internal
6464
import Cardano.Ledger.Conway.Governance.Procedures (GovActionState)
@@ -214,19 +214,19 @@ computeDRepDistr instantStake regDReps proposalDeposits poolDistr dRepDistr =
214214
let instantStakeCredentials = instantStake ^. instantStakeCredentialsL
215215
stake = fromMaybe (CompactCoin 0) $ Map.lookup stakeCred instantStakeCredentials
216216
mProposalDeposit = Map.lookup stakeCred proposalDeposits
217-
stakeAndDeposits = maybe stake (addCompact stake) mProposalDeposit
217+
stakeAndDeposits = maybe stake (addCompactCoin stake) mProposalDeposit
218218
in case umElemDelegations umElem of
219219
Nothing -> (drepAccum, poolAccum)
220220
Just (RewardDelegationSPO spo _r) ->
221221
( drepAccum
222222
, addToPoolDistr spo mProposalDeposit poolAccum
223223
)
224224
Just (RewardDelegationDRep drep r) ->
225-
( addToDRepDistr drep (addCompact stakeAndDeposits r) drepAccum
225+
( addToDRepDistr drep (addCompactCoin stakeAndDeposits r) drepAccum
226226
, poolAccum
227227
)
228228
Just (RewardDelegationBoth spo drep r) ->
229-
( addToDRepDistr drep (addCompact stakeAndDeposits r) drepAccum
229+
( addToDRepDistr drep (addCompactCoin stakeAndDeposits r) drepAccum
230230
, addToPoolDistr spo mProposalDeposit poolAccum
231231
)
232232
addToPoolDistr spo mProposalDeposit distr = fromMaybe distr $ do
@@ -237,7 +237,7 @@ computeDRepDistr instantStake regDReps proposalDeposits poolDistr dRepDistr =
237237
& poolDistrDistrL %~ Map.insert spo (ips & individualTotalPoolStakeL <>~ proposalDeposit)
238238
& poolDistrTotalL <>~ proposalDeposit
239239
addToDRepDistr drep ccoin distr =
240-
let updatedDistr = Map.insertWith addCompact drep ccoin distr
240+
let updatedDistr = Map.insertWith addCompactCoin drep ccoin distr
241241
in case drep of
242242
DRepAlwaysAbstain -> updatedDistr
243243
DRepAlwaysNoConfidence -> updatedDistr

eras/conway/impl/src/Cardano/Ledger/Conway/Governance/Proposals.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ import Cardano.Ledger.Binary (
131131
decodeListLikeWithCountT,
132132
decodeRecordNamedT,
133133
)
134-
import Cardano.Ledger.Coin (Coin, CompactForm (CompactCoin))
134+
import Cardano.Ledger.Coin (Coin, CompactForm (CompactCoin), addCompactCoin)
135+
import Cardano.Ledger.Compactible (toCompact)
135136
import Cardano.Ledger.Conway.Governance.Procedures
136137
import Cardano.Ledger.Core
137138
import Cardano.Ledger.Credential (Credential)
138-
import Cardano.Ledger.UMap (addCompact, toCompact)
139139
import Control.DeepSeq (NFData)
140140
import Control.Exception (assert)
141141
import Control.Monad (unless)
@@ -545,7 +545,7 @@ proposalsDeposits =
545545
F.foldl'
546546
( \gasMap gas ->
547547
Map.insertWith
548-
addCompact
548+
addCompactCoin
549549
(gas ^. gasReturnAddrL . rewardAccountCredentialL)
550550
(fromMaybe (CompactCoin 0) $ toCompact $ gas ^. gasDepositL)
551551
gasMap

eras/shelley/test-suite/test/Test/Cardano/Ledger/Shelley/Examples/Combinators.hs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ import Cardano.Ledger.BaseTypes (
5757
(⭒),
5858
)
5959
import Cardano.Ledger.Block (Block (..), bheader)
60-
import Cardano.Ledger.Coin (Coin (..))
60+
import Cardano.Ledger.Coin (
61+
Coin (..),
62+
CompactForm (CompactCoin),
63+
addCompactCoin,
64+
compactCoinOrError,
65+
)
6166
import Cardano.Ledger.Credential (Credential (..), Ptr)
6267
import Cardano.Ledger.Hashes (GenDelegPair, GenDelegs (..))
6368
import Cardano.Ledger.PoolParams (PoolParams (..))
@@ -186,7 +191,7 @@ feesAndDeposits ppEx newFees stakes pools cs = cs {chainNes = nes'}
186191
newcount = F.foldl' accum 0 pools
187192
accum n x = if Map.member (ppId x) (psDeposits pstate) then (n :: Integer) else n + 1
188193
newDeposits =
189-
Map.fromList (map (\cred -> (cred, UM.compactCoinOrError (ppEx ^. ppKeyDepositL))) stakes)
194+
Map.fromList (map (\cred -> (cred, compactCoinOrError (ppEx ^. ppKeyDepositL))) stakes)
190195
newPools = Map.fromList (map (\p -> (ppId p, ppEx ^. ppPoolDepositL)) pools)
191196
dpstate' =
192197
mkShelleyCertState
@@ -222,7 +227,7 @@ feesAndKeyRefund newFees key cs = cs {chainNes = nes'}
222227
es' = es {esLState = ls'}
223228
nes' = nes {nesEs = es'}
224229
dpstate' = certState & certDStateL . dsUnifiedL %~ (UM.adjust zeroD key . RewDepUView)
225-
zeroD (RDPair x _) = RDPair x (UM.CompactCoin 0)
230+
zeroD (RDPair x _) = RDPair x (CompactCoin 0)
226231

227232
-- | = Update the UTxO
228233
--
@@ -273,7 +278,7 @@ newStakeCred cred ptr cs = cs {chainNes = nes'}
273278
ds
274279
{ dsUnified =
275280
let um0 = dsUnified ds
276-
um1 = UM.insert cred (UM.RDPair (UM.CompactCoin 0) (UM.CompactCoin 0)) (RewDepUView um0)
281+
um1 = UM.insert cred (UM.RDPair (CompactCoin 0) (CompactCoin 0)) (RewDepUView um0)
277282
um2 = (PtrUView um1 UM. (ptr, cred))
278283
in um2
279284
}
@@ -469,7 +474,7 @@ reapPool pool cs = cs {chainNes = nes'}
469474
Just (UM.RDPair ccoin dep) ->
470475
( UM.insert'
471476
rewardAddr
472-
(UM.RDPair (UM.addCompact ccoin (UM.compactCoinOrError (pp ^. ppPoolDepositL))) dep)
477+
(UM.RDPair (addCompactCoin ccoin (compactCoinOrError (pp ^. ppPoolDepositL))) dep)
473478
(rewards ds)
474479
, Coin 0
475480
)
@@ -542,7 +547,7 @@ applyMIR pot newrewards cs = cs {chainNes = nes'}
542547
ds = dps ^. certDStateL
543548
ds' =
544549
ds
545-
{ dsUnified = rewards ds UM.∪+ Map.map UM.compactCoinOrError newrewards
550+
{ dsUnified = rewards ds UM.∪+ Map.map compactCoinOrError newrewards
546551
, dsIRewards = emptyInstantaneousRewards
547552
}
548553
dps' = dps & certDStateL .~ ds'

libs/cardano-ledger-core/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## 1.18.0.0
44

5+
* Add `addCompactCoin` to `Cardano.Ledger.Coin` and deprecate `Cardano.Ledger.UMap.addCompact`
6+
in its favor
7+
* Move `sumCompactCoin` to `Cardano.Ledger.Coin`
58
* Add `eraDecoderWithBytes`
69
* Move `Annotator` instances to `testlib`
710
* Expose `MkData` constructor.

libs/cardano-ledger-core/src/Cardano/Ledger/Coin.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ module Cardano.Ledger.Coin (
2424
integerToWord64,
2525
decodePositiveCoin,
2626
compactCoinOrError,
27+
addCompactCoin,
28+
sumCompactCoin,
2729
-- NonZero helpers
2830
toCompactCoinNonZero,
2931
unCoinNonZero,
@@ -54,6 +56,7 @@ import Cardano.Ledger.Compactible
5456
import Control.DeepSeq (NFData)
5557
import Data.Aeson (FromJSON, ToJSON)
5658
import Data.Coerce (coerce)
59+
import qualified Data.Foldable as F (foldl') -- Drop this when ghc >= 9.10
5760
import Data.Group (Abelian, Group (..))
5861
import Data.MemPack
5962
import Data.Monoid (Sum (..))
@@ -173,6 +176,12 @@ instance EncCBOR (CompactForm DeltaCoin) where
173176
instance DecCBOR (CompactForm DeltaCoin) where
174177
decCBOR = CompactDeltaCoin <$> decCBOR
175178

179+
addCompactCoin :: CompactForm Coin -> CompactForm Coin -> CompactForm Coin
180+
addCompactCoin (CompactCoin x) (CompactCoin y) = CompactCoin (x + y)
181+
182+
sumCompactCoin :: Foldable t => t (CompactForm Coin) -> CompactForm Coin
183+
sumCompactCoin = F.foldl' addCompactCoin (CompactCoin 0)
184+
176185
-- ================================
177186

178187
decodePositiveCoin :: String -> Decoder s Coin

libs/cardano-ledger-core/src/Cardano/Ledger/UMap.hs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,13 @@ where
119119

120120
import Cardano.Ledger.BaseTypes (strictMaybe)
121121
import Cardano.Ledger.Binary
122-
import Cardano.Ledger.Coin (Coin (..), CompactForm (CompactCoin), compactCoinOrError)
122+
import Cardano.Ledger.Coin (
123+
Coin (..),
124+
CompactForm (CompactCoin),
125+
addCompactCoin,
126+
compactCoinOrError,
127+
sumCompactCoin,
128+
)
123129
import Cardano.Ledger.Compactible (Compactible (..))
124130
import Cardano.Ledger.Credential (Credential (..), Ptr, mkPtrNormalized)
125131
import Cardano.Ledger.DRep (DRep)
@@ -926,7 +932,7 @@ unionRewAgg view m = Map.foldlWithKey' accum (unUView view) m
926932
where
927933
accum umap key ccoin = adjust combine key (RewDepUView umap)
928934
where
929-
combine (RDPair r d) = RDPair (addCompact r ccoin) d
935+
combine (RDPair r d) = RDPair (addCompactCoin r ccoin) d
930936
(∪+) = unionRewAgg
931937

932938
-- | Add the deposit from the `Map` on the right side to the deposit in the `UView` on the left.
@@ -937,7 +943,7 @@ unionKeyDeposits view m = unUView $ Map.foldlWithKey' accum view m
937943
accum vw key ccoin = insertWith' combine key (RDPair (CompactCoin 0) ccoin) vw
938944
-- If the key isn't present in the `UMap` the combining function is ignored
939945
-- and the new `RDPair` is inserted in the `UMap`. Ref: haddock for `insertWith'`.
940-
combine (RDPair r d) (RDPair _ newD) = RDPair r (addCompact d newD)
946+
combine (RDPair r d) (RDPair _ newD) = RDPair r (addCompactCoin d newD)
941947

942948
-- | Delete all keys in the given `Set` from the domain of the given map-like `UView`.
943949
--
@@ -1083,17 +1089,15 @@ unify rd ptr sPool dRep = um4
10831089
um4 = unUView $ Map.foldlWithKey' (\um k v -> insert' k v um) (PtrUView um3) ptr
10841090

10851091
addCompact :: CompactForm Coin -> CompactForm Coin -> CompactForm Coin
1086-
addCompact (CompactCoin x) (CompactCoin y) = CompactCoin (x + y)
1087-
1088-
sumCompactCoin :: Foldable t => t (CompactForm Coin) -> CompactForm Coin
1089-
sumCompactCoin = foldl' addCompact (CompactCoin 0)
1092+
addCompact = addCompactCoin
1093+
{-# DEPRECATED addCompact "In favor of `Cardano.Ledger.Coin.addCompactCoin`" #-}
10901094

10911095
sumRewardsUView :: UView k RDPair -> CompactForm Coin
10921096
sumRewardsUView = foldl' accum (CompactCoin 0)
10931097
where
1094-
accum ans (RDPair r _) = addCompact ans r
1098+
accum ans (RDPair r _) = addCompactCoin ans r
10951099

10961100
sumDepositUView :: UView k RDPair -> CompactForm Coin
10971101
sumDepositUView = foldl' accum (CompactCoin 0)
10981102
where
1099-
accum ans (RDPair _ d) = addCompact ans d
1103+
accum ans (RDPair _ d) = addCompactCoin ans d

libs/cardano-ledger-core/test/Test/Cardano/Ledger/UMapSpec.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
module Test.Cardano.Ledger.UMapSpec where
88

99
import Cardano.Ledger.BaseTypes (StrictMaybe (SJust, SNothing))
10-
import Cardano.Ledger.Coin (Coin, CompactForm)
10+
import Cardano.Ledger.Coin (Coin, CompactForm, addCompactCoin)
1111
import Cardano.Ledger.Compactible (fromCompact)
1212
import Cardano.Ledger.Credential (Credential, Ptr)
1313
import Cardano.Ledger.DRep (DRep)
@@ -18,7 +18,6 @@ import Cardano.Ledger.UMap (
1818
UMElem (UMElem),
1919
UMap (UMap, umElems, umPtrs),
2020
UView (DRepUView, PtrUView, RewDepUView, SPoolUView),
21-
addCompact,
2221
compactRewardMap,
2322
dRepMap,
2423
delete,
@@ -216,7 +215,7 @@ oldUnionRewAgg ::
216215
addC :: CompactForm Coin -> StrictMaybe RDPair -> StrictMaybe RDPair
217216
addC newR = \case
218217
SNothing -> SNothing
219-
SJust (RDPair r d) -> SJust $ RDPair (addCompact r newR) d
218+
SJust (RDPair r d) -> SJust $ RDPair (addCompactCoin r newR) d
220219

221220
spec :: Spec
222221
spec = do

0 commit comments

Comments
 (0)