Skip to content

Commit 87f061a

Browse files
committed
Implement Enc/DecCBOR for PoolParams explicitly
rather than via `Enc/DecCBORGroup`
1 parent f776625 commit 87f061a

File tree

3 files changed

+68
-50
lines changed

3 files changed

+68
-50
lines changed

eras/shelley/impl/src/Cardano/Ledger/Shelley/TxCert.hs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,8 @@ module Cardano.Ledger.Shelley.TxCert (
7777
import Cardano.Ledger.BaseTypes (invalidKey, kindObject)
7878
import Cardano.Ledger.Binary (
7979
DecCBOR (decCBOR),
80-
DecCBORGroup (..),
8180
Decoder,
8281
EncCBOR (..),
83-
EncCBORGroup (..),
8482
Encoding,
8583
FromCBOR (..),
8684
ToCBOR (..),
@@ -90,7 +88,6 @@ import Cardano.Ledger.Binary (
9088
decodeWord,
9189
encodeListLen,
9290
encodeWord8,
93-
listLenInt,
9491
peekTokenType,
9592
)
9693
import Cardano.Ledger.Coin (Coin (..), DeltaCoin)
@@ -105,7 +102,12 @@ import Cardano.Ledger.Internal.Era (AllegraEra, AlonzoEra, BabbageEra, MaryEra)
105102
import Cardano.Ledger.Keys (asWitness)
106103
import Cardano.Ledger.Shelley.Era (ShelleyEra)
107104
import Cardano.Ledger.Shelley.PParams ()
108-
import Cardano.Ledger.State (PoolParams (..))
105+
import Cardano.Ledger.State (
106+
PoolParams (..),
107+
decCBORGroupPoolParams,
108+
encCBORGroupPoolParams,
109+
poolParamsCount,
110+
)
109111
import Cardano.Ledger.Val ((<+>), (<×>))
110112
import Control.DeepSeq (NFData (..), rwhnf)
111113
import Data.Aeson (ToJSON (..), (.=))
@@ -434,9 +436,9 @@ encodeShelleyDelegCert = \case
434436
encodePoolCert :: PoolCert -> Encoding
435437
encodePoolCert = \case
436438
RegPool poolParams ->
437-
encodeListLen (1 + listLen poolParams)
439+
encodeListLen (1 + fromIntegral poolParamsCount)
438440
<> encodeWord8 3
439-
<> encCBORGroup poolParams
441+
<> encCBORGroupPoolParams poolParams
440442
RetirePool vk epoch ->
441443
encodeListLen 3
442444
<> encodeWord8 4
@@ -504,8 +506,8 @@ shelleyTxCertDelegDecoder = \case
504506
poolTxCertDecoder :: EraTxCert era => Word -> Decoder s (Int, TxCert era)
505507
poolTxCertDecoder = \case
506508
3 -> do
507-
group <- decCBORGroup
508-
pure (1 + listLenInt group, RegPoolTxCert group)
509+
group <- decCBORGroupPoolParams
510+
pure (1 + fromIntegral poolParamsCount, RegPoolTxCert group)
509511
4 -> do
510512
a <- decCBOR
511513
b <- decCBOR

libs/cardano-ledger-core/CHANGELOG.md

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

33
## 1.19.0.0
44

5+
* Add `poolParamsCount`, `decCBORGroupPoolParams`, `encCBORGroupPoolParams` to `StakePool` module
6+
* Remove `DecCBORGroup` and `EncCBORGroup` instances for `PoolParams`
57
* Remove the `UMap` module and the `umap` benchmarks cabal target.
68
* Export `dRepToText`
79
* Deprecated `bheader` and `bbody`

libs/cardano-ledger-core/src/Cardano/Ledger/State/StakePool.hs

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
{-# LANGUAGE FlexibleContexts #-}
66
{-# LANGUAGE LambdaCase #-}
77
{-# LANGUAGE OverloadedStrings #-}
8+
{-# LANGUAGE RecordWildCards #-}
89
{-# LANGUAGE StandaloneDeriving #-}
910
{-# LANGUAGE TypeApplications #-}
1011
{-# LANGUAGE TypeFamilies #-}
@@ -26,6 +27,9 @@
2627
module Cardano.Ledger.State.StakePool (
2728
-- * Stake Pool State
2829
StakePoolState (..),
30+
decCBORGroupPoolParams,
31+
encCBORGroupPoolParams,
32+
poolParamsCount,
2933

3034
-- * Lenses
3135
spsVrfL,
@@ -68,12 +72,11 @@ import Cardano.Ledger.BaseTypes (
6872
strictMaybeToMaybe,
6973
)
7074
import Cardano.Ledger.Binary (
71-
CBORGroup (..),
7275
DecCBOR (..),
73-
DecCBORGroup (..),
7476
DecShareCBOR (..),
77+
Decoder,
7578
EncCBOR (..),
76-
EncCBORGroup (..),
79+
Encoding,
7780
decodeNullMaybe,
7881
decodeRecordNamed,
7982
decodeRecordSum,
@@ -381,8 +384,6 @@ data PoolParams = PoolParams
381384
, ppMetadata :: !(StrictMaybe PoolMetadata)
382385
}
383386
deriving (Show, Generic, Eq, Ord)
384-
deriving (EncCBOR) via CBORGroup PoolParams
385-
deriving (DecCBOR) via CBORGroup PoolParams
386387

387388
ppVrfL :: Lens' PoolParams (VRFVerKeyHash 'StakePoolVRF)
388389
ppVrfL = lens ppVrf (\pp u -> pp {ppVrf = u})
@@ -452,40 +453,53 @@ data SizeOfPoolRelays = SizeOfPoolRelays
452453
instance EncCBOR SizeOfPoolRelays where
453454
encCBOR = error "The `SizeOfPoolRelays` type cannot be encoded!"
454455

455-
instance EncCBORGroup PoolParams where
456-
encCBORGroup poolParams =
457-
encCBOR (ppId poolParams)
458-
<> encCBOR (ppVrf poolParams)
459-
<> encCBOR (ppPledge poolParams)
460-
<> encCBOR (ppCost poolParams)
461-
<> encCBOR (ppMargin poolParams)
462-
<> encCBOR (ppRewardAccount poolParams)
463-
<> encCBOR (ppOwners poolParams)
464-
<> encCBOR (ppRelays poolParams)
465-
<> encodeNullMaybe encCBOR (strictMaybeToMaybe (ppMetadata poolParams))
466-
listLen _ = 9
467-
listLenBound _ = 9
468-
469-
instance DecCBORGroup PoolParams where
470-
decCBORGroup = do
471-
hk <- decCBOR
472-
vrf <- decCBOR
473-
pledge <- decCBOR
474-
cost <- decCBOR
475-
margin <- decCBOR
476-
ra <- decCBOR
477-
owners <- decCBOR
478-
relays <- decCBOR
479-
md <- decodeNullMaybe decCBOR
480-
pure $
481-
PoolParams
482-
{ ppId = hk
483-
, ppVrf = vrf
484-
, ppPledge = pledge
485-
, ppCost = cost
486-
, ppMargin = margin
487-
, ppRewardAccount = ra
488-
, ppOwners = owners
489-
, ppRelays = relays
490-
, ppMetadata = maybeToStrictMaybe md
491-
}
456+
instance EncCBOR PoolParams where
457+
encCBOR poolParams =
458+
encodeListLen poolParamsCount
459+
<> encCBORGroupPoolParams poolParams
460+
461+
instance DecCBOR PoolParams where
462+
decCBOR =
463+
decodeRecordNamed
464+
"CBORGroup"
465+
(const (fromIntegral poolParamsCount))
466+
decCBORGroupPoolParams
467+
468+
poolParamsCount :: Word
469+
poolParamsCount = 9
470+
471+
encCBORGroupPoolParams :: PoolParams -> Encoding
472+
encCBORGroupPoolParams PoolParams {..} =
473+
encCBOR ppId
474+
<> encCBOR ppVrf
475+
<> encCBOR ppPledge
476+
<> encCBOR ppCost
477+
<> encCBOR ppMargin
478+
<> encCBOR ppRewardAccount
479+
<> encCBOR ppOwners
480+
<> encCBOR ppRelays
481+
<> encodeNullMaybe encCBOR (strictMaybeToMaybe ppMetadata)
482+
483+
decCBORGroupPoolParams :: Decoder s PoolParams
484+
decCBORGroupPoolParams = do
485+
hk <- decCBOR
486+
vrf <- decCBOR
487+
pledge <- decCBOR
488+
cost <- decCBOR
489+
margin <- decCBOR
490+
ra <- decCBOR
491+
owners <- decCBOR
492+
relays <- decCBOR
493+
md <- decodeNullMaybe decCBOR
494+
pure $
495+
PoolParams
496+
{ ppId = hk
497+
, ppVrf = vrf
498+
, ppPledge = pledge
499+
, ppCost = cost
500+
, ppMargin = margin
501+
, ppRewardAccount = ra
502+
, ppOwners = owners
503+
, ppRelays = relays
504+
, ppMetadata = maybeToStrictMaybe md
505+
}

0 commit comments

Comments
 (0)