Skip to content

Commit 64f8040

Browse files
committed
Remove redundant memoization of bytes for BootstrapWitness
1 parent b4db229 commit 64f8040

File tree

7 files changed

+39
-101
lines changed

7 files changed

+39
-101
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ shelleyEqTxWitsRaw :: EraTxWits era => TxWits era -> TxWits era -> Bool
218218
shelleyEqTxWitsRaw txWits1 txWits2 =
219219
txWits1 ^. addrTxWitsL == txWits2 ^. addrTxWitsL
220220
&& liftEq eqRaw (txWits1 ^. scriptTxWitsL) (txWits2 ^. scriptTxWitsL)
221-
&& liftEq eqRaw (txWits1 ^. bootAddrTxWitsL) (txWits2 ^. bootAddrTxWitsL)
221+
&& txWits1 ^. bootAddrTxWitsL == txWits2 ^. bootAddrTxWitsL
222222

223223
instance EraScript era => DecCBOR (Annotator (ShelleyTxWitsRaw era)) where
224224
decCBOR = decodeWits

libs/cardano-ledger-core/CHANGELOG.md

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

33
## 1.18.0.0
44

5+
* Remove `eqBootstrapWitnessRaw` and `BootstrapWitnessRaw`
6+
* Rename `bwSig` to `bwSignature` for `BootstrapWitness`
57
* Remove `witVKeyBytes` and `eqWitVKeyRaw`
68
* Remove `EqRaw` instance for `WitVKey`
79
* Replace `Block'` constructor with `Block`
@@ -10,8 +12,6 @@
1012

1113
## 1.17.0.0
1214

13-
* Remove `eqBootstrapWitnessRaw` and `BootstrapWitnessRaw`
14-
* Rename `bwSig` to `bwSignature` for `BootstrapWitness`
1515
* Add `BoootstrapWitnessRaw` type
1616
* Add `EraStake`, `CanGetInstantStake`, `CanSetInstantStake` , `snapShotFromInstantStake`, `resolveActiveInstantStakeCredentials`
1717
* Add boolean argument to `fromCborRigorousBothAddr` for lenient `Ptr` decoding

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

Lines changed: 32 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -5,70 +5,50 @@
55
{-# LANGUAGE FlexibleInstances #-}
66
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
77
{-# LANGUAGE OverloadedStrings #-}
8-
{-# LANGUAGE PatternSynonyms #-}
98
{-# LANGUAGE PolyKinds #-}
109
{-# LANGUAGE RecordWildCards #-}
1110
{-# LANGUAGE ScopedTypeVariables #-}
12-
{-# LANGUAGE StandaloneDeriving #-}
1311
{-# LANGUAGE TypeApplications #-}
1412
{-# LANGUAGE TypeFamilies #-}
1513
{-# LANGUAGE UndecidableInstances #-}
16-
{-# LANGUAGE ViewPatterns #-}
1714

1815
module Cardano.Ledger.Keys.Bootstrap (
19-
BootstrapWitness (
20-
BootstrapWitness,
21-
bwKey,
22-
bwSig,
23-
bwChainCode,
24-
bwAttributes
25-
),
26-
BootstrapWitnessRaw,
16+
BootstrapWitness (..),
2717
ChainCode (..),
2818
bootstrapWitKeyHash,
2919
unpackByronVKey,
3020
makeBootstrapWitness,
3121
verifyBootstrapWit,
32-
eqBootstrapWitnessRaw,
3322
)
3423
where
3524

3625
import qualified Cardano.Chain.Common as Byron
3726
import Cardano.Crypto.DSIGN (SignedDSIGN (..))
3827
import qualified Cardano.Crypto.DSIGN as DSIGN
28+
import qualified Cardano.Crypto.DSIGN.Class as C
3929
import qualified Cardano.Crypto.Hash as Hash
4030
import qualified Cardano.Crypto.Signing as Byron
4131
import qualified Cardano.Crypto.Wallet as WC
4232
import Cardano.Ledger.Binary (
4333
Annotator,
4434
DecCBOR (..),
4535
EncCBOR (..),
46-
byronProtVer,
36+
)
37+
import Cardano.Ledger.Binary.Plain (
38+
FromCBOR (..),
39+
ToCBOR (..),
4740
decodeRecordNamed,
4841
encodeListLen,
4942
serialize',
5043
)
51-
import Cardano.Ledger.Binary.Crypto (
52-
decodeSignedDSIGN,
53-
encodeSignedDSIGN,
54-
)
55-
import qualified Cardano.Ledger.Binary.Plain as Plain
5644
import Cardano.Ledger.Hashes (ADDRHASH, EraIndependentTxBody, HASH, Hash, KeyHash (..))
5745
import Cardano.Ledger.Keys.Internal (
5846
DSIGN,
5947
KeyRole (..),
6048
VKey (..),
6149
verifySignedDSIGN,
6250
)
63-
import Cardano.Ledger.MemoBytes (
64-
EqRaw (..),
65-
Mem,
66-
MemoBytes,
67-
Memoized (..),
68-
getMemoRawType,
69-
mkMemoized,
70-
)
71-
import Control.DeepSeq (NFData)
51+
import Control.DeepSeq (NFData (..), rwhnf)
7252
import Data.ByteString (ByteString)
7353
import Data.Coerce (coerce)
7454
import Data.Maybe (fromMaybe)
@@ -81,65 +61,40 @@ import Quiet
8161
newtype ChainCode = ChainCode {unChainCode :: ByteString}
8262
deriving (Eq, Generic)
8363
deriving (Show) via Quiet ChainCode
84-
deriving newtype (NoThunks, EncCBOR, DecCBOR, NFData)
64+
deriving newtype (NoThunks, ToCBOR, FromCBOR, EncCBOR, DecCBOR, NFData)
8565

86-
data BootstrapWitnessRaw = BootstrapWitnessRaw
87-
{ bwrKey :: !(VKey 'Witness)
88-
, bwrSignature :: !(SignedDSIGN DSIGN (Hash HASH EraIndependentTxBody))
89-
, bwrChainCode :: !ChainCode
90-
, bwrAttributes :: !ByteString
66+
data BootstrapWitness = BootstrapWitness
67+
{ bwKey :: !(VKey 'Witness)
68+
, bwSignature :: !(SignedDSIGN DSIGN (Hash HASH EraIndependentTxBody))
69+
, bwChainCode :: !ChainCode
70+
, bwAttributes :: !ByteString
9171
}
9272
deriving (Generic, Show, Eq)
9373

94-
instance NFData BootstrapWitnessRaw
95-
instance NoThunks BootstrapWitnessRaw
74+
instance NFData BootstrapWitness where
75+
rnf = rwhnf
76+
77+
instance NoThunks BootstrapWitness
9678

97-
instance EncCBOR BootstrapWitnessRaw where
98-
encCBOR cwr@(BootstrapWitnessRaw _ _ _ _) =
99-
let BootstrapWitnessRaw {..} = cwr
79+
instance ToCBOR BootstrapWitness where
80+
toCBOR cwr@(BootstrapWitness _ _ _ _) =
81+
let BootstrapWitness {..} = cwr
10082
in encodeListLen 4
101-
<> encCBOR bwrKey
102-
<> encodeSignedDSIGN bwrSignature
103-
<> encCBOR bwrChainCode
104-
<> encCBOR bwrAttributes
83+
<> toCBOR bwKey
84+
<> C.encodeSignedDSIGN bwSignature
85+
<> toCBOR bwChainCode
86+
<> toCBOR bwAttributes
87+
instance EncCBOR BootstrapWitness
10588

106-
instance DecCBOR BootstrapWitnessRaw where
107-
decCBOR =
89+
instance FromCBOR BootstrapWitness where
90+
fromCBOR =
10891
decodeRecordNamed "BootstrapWitnessRaw" (const 4) $
109-
BootstrapWitnessRaw <$> decCBOR <*> decodeSignedDSIGN <*> decCBOR <*> decCBOR
92+
BootstrapWitness <$> fromCBOR <*> C.decodeSignedDSIGN <*> fromCBOR <*> fromCBOR
93+
instance DecCBOR BootstrapWitness
11094

111-
instance DecCBOR (Annotator BootstrapWitnessRaw) where
95+
instance DecCBOR (Annotator BootstrapWitness) where
11296
decCBOR = pure <$> decCBOR
11397

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
122-
123-
deriving via
124-
Mem BootstrapWitnessRaw
125-
instance
126-
DecCBOR (Annotator BootstrapWitness)
127-
128-
pattern BootstrapWitness ::
129-
VKey 'Witness ->
130-
SignedDSIGN DSIGN (Hash HASH EraIndependentTxBody) ->
131-
ChainCode ->
132-
ByteString ->
133-
BootstrapWitness
134-
pattern BootstrapWitness {bwKey, bwSig, bwChainCode, bwAttributes} <-
135-
( getMemoRawType ->
136-
BootstrapWitnessRaw bwKey bwSig bwChainCode bwAttributes
137-
)
138-
where
139-
BootstrapWitness bwKey bwSig bwChainCode bwAttributes =
140-
mkMemoized minBound $ BootstrapWitnessRaw bwKey bwSig bwChainCode bwAttributes
141-
{-# COMPLETE BootstrapWitness #-}
142-
14398
instance Ord BootstrapWitness where
14499
compare = comparing bootstrapWitKeyHash
145100

@@ -195,7 +150,7 @@ verifyBootstrapWit txbodyHash witness =
195150
verifySignedDSIGN
196151
(bwKey witness)
197152
txbodyHash
198-
(coerce . bwSig $ witness)
153+
(coerce $ bwSignature witness)
199154

200155
coerceSignature :: WC.XSignature -> DSIGN.SigDSIGN DSIGN.Ed25519DSIGN
201156
coerceSignature sig =
@@ -208,7 +163,7 @@ makeBootstrapWitness ::
208163
Byron.Attributes Byron.AddrAttributes ->
209164
BootstrapWitness
210165
makeBootstrapWitness txBodyHash byronSigningKey addrAttributes =
211-
BootstrapWitness vk signature cc (serialize' byronProtVer addrAttributes)
166+
BootstrapWitness vk signature cc (serialize' addrAttributes)
212167
where
213168
(vk, cc) = unpackByronVKey $ Byron.toVerification byronSigningKey
214169
signature =
@@ -217,13 +172,3 @@ makeBootstrapWitness txBodyHash byronSigningKey addrAttributes =
217172
(mempty :: ByteString)
218173
(Byron.unSigningKey byronSigningKey)
219174
(Hash.hashToBytes txBodyHash)
220-
221-
eqBootstrapWitnessRaw :: BootstrapWitness -> BootstrapWitness -> Bool
222-
eqBootstrapWitnessRaw bw1 bw2 =
223-
bwKey bw1 == bwKey bw2
224-
&& bwSig bw1 == bwSig bw2
225-
&& bwChainCode bw1 == bwChainCode bw2
226-
&& bwAttributes bw1 == bwAttributes bw2
227-
228-
instance EqRaw BootstrapWitness where
229-
eqRaw = eqBootstrapWitnessRaw

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ spec =
5757
BootstrapWitness
5858
{ bwKey = shelleyVKey
5959
, bwChainCode = chainCode
60-
, bwSig = sig
60+
, bwSignature = sig
6161
, bwAttributes = serialize' byronProtVer $ Byron.addrAttributes byronAddr
6262
}
6363
pure $

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,10 @@ instance Arbitrary ChainCode where
370370
instance Arbitrary BootstrapWitness where
371371
arbitrary = do
372372
bwKey <- arbitrary
373-
bwSig <- arbitrary
373+
bwSignature <- arbitrary
374374
bwChainCode <- arbitrary
375375
bwAttributes <- arbitrary
376-
pure $ BootstrapWitness {bwKey, bwSig, bwChainCode, bwAttributes}
376+
pure $ BootstrapWitness {bwKey, bwSignature, bwChainCode, bwAttributes}
377377

378378
instance Arbitrary GenDelegPair where
379379
arbitrary = GenDelegPair <$> arbitrary <*> arbitrary

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

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

114114
-- Keys/Bootstrap
115-
instance ToExpr BootstrapWitnessRaw
116115
instance ToExpr BootstrapWitness
117116

118117
instance ToExpr ChainCode

libs/cardano-ledger-test/src/Test/Cardano/Ledger/Generic/PrettyCore.hs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ import Data.Sequence.Strict (StrictSeq)
285285
import Data.Set (Set)
286286
import qualified Data.Set as Set
287287
import Data.Text (Text, pack)
288-
import Data.Typeable (Typeable)
289288
import qualified Data.VMap as VMap
290289
import Data.Void (Void, absurd)
291290
import Data.Word (Word16, Word32, Word64, Word8)
@@ -2876,12 +2875,7 @@ pcWitVKey _p (WitVKey vk@(VKey x) sig) =
28762875
hash = pcKeyHash (hashKey vk)
28772876
sigstring = show sig
28782877

2879-
instance
2880-
( Reflect era
2881-
, Typeable keyrole
2882-
) =>
2883-
PrettyA (WitVKey keyrole)
2884-
where
2878+
instance Reflect era => PrettyA (WitVKey keyrole) where
28852879
prettyA = pcWitVKey @era reify
28862880

28872881
-- =====================================

0 commit comments

Comments
 (0)