Skip to content

Commit 0e04cf4

Browse files
authored
Merge pull request #4916 from IntersectMBO/td/use-memobytes
Use `MemoBytes` to represent memoized types
2 parents 2363a49 + 63d5cf9 commit 0e04cf4

File tree

5 files changed

+118
-127
lines changed

5 files changed

+118
-127
lines changed

libs/cardano-ledger-core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 1.17.0.0
44

5+
* Add `BoootstrapWitnessRaw` type
56
* Add `EraStake`, `CanGetInstantStake`, `CanSetInstantStake` , `snapShotFromInstantStake`, `resolveActiveInstantStakeCredentials`
67
* Add boolean argument to `fromCborRigorousBothAddr` for lenient `Ptr` decoding
78
* Add `ToCBOR` and `FromCBOR` instances for:

libs/cardano-ledger-core/src/Cardano/Ledger/Keys/Bootstrap.hs

Lines changed: 49 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
{-# LANGUAGE OverloadedStrings #-}
88
{-# LANGUAGE PatternSynonyms #-}
99
{-# LANGUAGE PolyKinds #-}
10+
{-# LANGUAGE RecordWildCards #-}
1011
{-# LANGUAGE ScopedTypeVariables #-}
1112
{-# LANGUAGE StandaloneDeriving #-}
1213
{-# LANGUAGE TypeApplications #-}
1314
{-# LANGUAGE TypeFamilies #-}
14-
{-# LANGUAGE TypeOperators #-}
1515
{-# LANGUAGE UndecidableInstances #-}
16+
{-# LANGUAGE ViewPatterns #-}
1617

1718
module Cardano.Ledger.Keys.Bootstrap (
1819
BootstrapWitness (
@@ -22,6 +23,7 @@ module Cardano.Ledger.Keys.Bootstrap (
2223
bwChainCode,
2324
bwAttributes
2425
),
26+
BootstrapWitnessRaw,
2527
ChainCode (..),
2628
bootstrapWitKeyHash,
2729
unpackByronVKey,
@@ -41,11 +43,9 @@ import Cardano.Ledger.Binary (
4143
Annotator,
4244
DecCBOR (..),
4345
EncCBOR (..),
44-
annotatorSlice,
4546
byronProtVer,
4647
decodeRecordNamed,
4748
encodeListLen,
48-
serialize,
4949
serialize',
5050
)
5151
import Cardano.Ledger.Binary.Crypto (
@@ -62,41 +62,68 @@ import Cardano.Ledger.Keys.Internal (
6262
)
6363
import Cardano.Ledger.MemoBytes (
6464
EqRaw (..),
65-
MemoBytes (Memo),
66-
decodeMemoized,
65+
Mem,
66+
MemoBytes,
67+
Memoized (..),
68+
getMemoRawType,
69+
mkMemoized,
6770
)
6871
import Control.DeepSeq (NFData)
6972
import Data.ByteString (ByteString)
70-
import qualified Data.ByteString.Lazy as LBS
71-
import qualified Data.ByteString.Short as SBS
7273
import Data.Coerce (coerce)
7374
import Data.Maybe (fromMaybe)
7475
import Data.Ord (comparing)
7576
import Data.Proxy (Proxy (..))
7677
import GHC.Generics (Generic)
77-
import NoThunks.Class (AllowThunksIn (..), NoThunks (..))
78+
import NoThunks.Class (NoThunks (..))
7879
import Quiet
7980

8081
newtype ChainCode = ChainCode {unChainCode :: ByteString}
8182
deriving (Eq, Generic)
8283
deriving (Show) via Quiet ChainCode
8384
deriving newtype (NoThunks, EncCBOR, DecCBOR, NFData)
8485

85-
data BootstrapWitness = BootstrapWitness'
86-
{ bwKey' :: !(VKey 'Witness)
87-
, bwSig' :: !(SignedDSIGN DSIGN (Hash HASH EraIndependentTxBody))
88-
, bwChainCode' :: !ChainCode
89-
, bwAttributes' :: !ByteString
90-
, bwBytes :: LBS.ByteString
86+
data BootstrapWitnessRaw = BootstrapWitnessRaw
87+
{ bwrKey :: !(VKey 'Witness)
88+
, bwrSignature :: !(SignedDSIGN DSIGN (Hash HASH EraIndependentTxBody))
89+
, bwrChainCode :: !ChainCode
90+
, bwrAttributes :: !ByteString
9191
}
9292
deriving (Generic, Show, Eq)
9393

94-
instance NFData BootstrapWitness
94+
instance NFData BootstrapWitnessRaw
95+
instance NoThunks BootstrapWitnessRaw
96+
97+
instance EncCBOR BootstrapWitnessRaw where
98+
encCBOR cwr@(BootstrapWitnessRaw _ _ _ _) =
99+
let BootstrapWitnessRaw {..} = cwr
100+
in encodeListLen 4
101+
<> encCBOR bwrKey
102+
<> encodeSignedDSIGN bwrSignature
103+
<> encCBOR bwrChainCode
104+
<> encCBOR bwrAttributes
105+
106+
instance DecCBOR BootstrapWitnessRaw where
107+
decCBOR =
108+
decodeRecordNamed "BootstrapWitnessRaw" (const 4) $
109+
BootstrapWitnessRaw <$> decCBOR <*> decodeSignedDSIGN <*> decCBOR <*> decCBOR
110+
111+
instance DecCBOR (Annotator BootstrapWitnessRaw) where
112+
decCBOR = pure <$> decCBOR
113+
114+
newtype BootstrapWitness = BootstrapWitnessConstr (MemoBytes BootstrapWitnessRaw)
115+
deriving (Generic)
116+
deriving newtype (Show, Eq, NFData, NoThunks, Plain.ToCBOR, DecCBOR)
117+
118+
instance Memoized BootstrapWitness where
119+
type RawType BootstrapWitness = BootstrapWitnessRaw
120+
121+
instance EncCBOR BootstrapWitness
95122

96123
deriving via
97-
(AllowThunksIn '["bwBytes"] BootstrapWitness)
124+
Mem BootstrapWitnessRaw
98125
instance
99-
NoThunks BootstrapWitness
126+
DecCBOR (Annotator BootstrapWitness)
100127

101128
pattern BootstrapWitness ::
102129
VKey 'Witness ->
@@ -105,55 +132,17 @@ pattern BootstrapWitness ::
105132
ByteString ->
106133
BootstrapWitness
107134
pattern BootstrapWitness {bwKey, bwSig, bwChainCode, bwAttributes} <-
108-
BootstrapWitness' bwKey bwSig bwChainCode bwAttributes _
135+
( getMemoRawType ->
136+
BootstrapWitnessRaw bwKey bwSig bwChainCode bwAttributes
137+
)
109138
where
110-
BootstrapWitness key sig cc attributes =
111-
let bytes =
112-
serialize byronProtVer $
113-
encodeListLen 4
114-
<> encCBOR key
115-
<> encodeSignedDSIGN sig
116-
<> encCBOR cc
117-
<> encCBOR attributes
118-
in BootstrapWitness' key sig cc attributes bytes
119-
139+
BootstrapWitness bwKey bwSig bwChainCode bwAttributes =
140+
mkMemoized minBound $ BootstrapWitnessRaw bwKey bwSig bwChainCode bwAttributes
120141
{-# COMPLETE BootstrapWitness #-}
121142

122143
instance Ord BootstrapWitness where
123144
compare = comparing bootstrapWitKeyHash
124145

125-
instance Plain.ToCBOR BootstrapWitness where
126-
toCBOR = Plain.encodePreEncoded . LBS.toStrict . bwBytes
127-
128-
-- | Encodes memoized bytes created upon construction.
129-
instance EncCBOR BootstrapWitness
130-
131-
instance DecCBOR (Annotator BootstrapWitness) where
132-
decCBOR = annotatorSlice $
133-
decodeRecordNamed "BootstrapWitness" (const 4) $ do
134-
key <- decCBOR
135-
sig <- decodeSignedDSIGN
136-
cc <- decCBOR
137-
attributes <- decCBOR
138-
pure . pure $ BootstrapWitness' key sig cc attributes
139-
140-
data BootstrapWitnessRaw
141-
= BootstrapWitnessRaw
142-
!(VKey 'Witness)
143-
!(SignedDSIGN DSIGN (Hash HASH EraIndependentTxBody))
144-
!ChainCode
145-
!ByteString
146-
147-
instance DecCBOR BootstrapWitnessRaw where
148-
decCBOR =
149-
decodeRecordNamed "BootstrapWitnessRaw" (const 4) $
150-
BootstrapWitnessRaw <$> decCBOR <*> decodeSignedDSIGN <*> decCBOR <*> decCBOR
151-
152-
instance DecCBOR BootstrapWitness where
153-
decCBOR = do
154-
Memo (BootstrapWitnessRaw k s c a) bs <- decodeMemoized (decCBOR @BootstrapWitnessRaw)
155-
pure $ BootstrapWitness' k s c a (LBS.fromStrict (SBS.fromShort bs))
156-
157146
-- | Rebuild the addrRoot of the corresponding address.
158147
bootstrapWitKeyHash ::
159148
BootstrapWitness ->

libs/cardano-ledger-core/testlib/Test/Cardano/Ledger/TreeDiff.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ instance ToExpr CostModels
112112
instance ToExpr (WitVKey kr)
113113

114114
-- Keys/Bootstrap
115+
instance ToExpr BootstrapWitnessRaw
115116
instance ToExpr BootstrapWitness
116117

117118
instance ToExpr ChainCode

libs/cardano-protocol-tpraos/CHANGELOG.md

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

33
## 1.4.0.0
44

5+
* Deprecated `bHeaderSize`
6+
* Add `SafeToHash` and `HashAnnotated` instances for `BHeader`
57
* Add `DecCBOR` instance for `OCert`
68
* Add `DecCBOR` instance for `BHeader`
79
* Converted `CertState` to a type family

0 commit comments

Comments
 (0)