Skip to content

Commit 957370a

Browse files
authored
Merge pull request #4938 from IntersectMBO/lehins/remove-bytes-memoization-from-witvkey
Remove redundant bytes memoization from `WitVKey` and `BootstrapWitness`
2 parents 9a21512 + 24e31ee commit 957370a

File tree

12 files changed

+77
-178
lines changed

12 files changed

+77
-178
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,9 @@ pattern ShelleyTxWits {addrWits, scriptWits, bootWits} <-
216216

217217
shelleyEqTxWitsRaw :: EraTxWits era => TxWits era -> TxWits era -> Bool
218218
shelleyEqTxWitsRaw txWits1 txWits2 =
219-
liftEq eqRaw (txWits1 ^. addrTxWitsL) (txWits2 ^. addrTxWitsL)
219+
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-api/CHANGELOG.md

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

77
## 1.11.0.0
88

9+
* Remove `witVKeyBytes`
910
* Converted `CertState` to a type family
1011
* Expose new `TxAuxDataHash` and deprecate old `AuxiliaryDataHash`
1112
* Stop re-exporting `Crypto` and `StandardCrypto`, since they have been moved to `cardano-protocol-tpraos`

libs/cardano-ledger-api/src/Cardano/Ledger/Api/Tx/Wits.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ module Cardano.Ledger.Api.Tx.Wits (
99

1010
-- *** WitVKey
1111
WitVKey (WitVKey),
12-
witVKeyBytes,
1312
witVKeyHash,
1413

1514
-- ** Byron address witness
@@ -64,4 +63,4 @@ import Cardano.Ledger.Conway.Scripts (ConwayPlutusPurpose (..))
6463
import Cardano.Ledger.Core (EraTxWits (..), hashScriptTxWitsL)
6564
import Cardano.Ledger.Keys (KeyRole (Witness))
6665
import Cardano.Ledger.Keys.Bootstrap (BootstrapWitness)
67-
import Cardano.Ledger.Keys.WitVKey (WitVKey (WitVKey), witVKeyBytes, witVKeyHash)
66+
import Cardano.Ledger.Keys.WitVKey (WitVKey (WitVKey), witVKeyHash)

libs/cardano-ledger-conformance/src/Test/Cardano/Ledger/Conformance/SpecTranslate/Conway/Base.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ import Control.Monad.Except (MonadError (..))
9090
import Control.State.Transition.Extended (STS (..))
9191
import Data.Bifunctor (Bifunctor (..))
9292
import Data.Bitraversable (bimapM)
93-
import Data.Data (Typeable)
9493
import Data.Default (Default (..))
9594
import Data.Foldable (Foldable (..))
9695
import Data.List (sortOn)
@@ -460,7 +459,7 @@ instance DSIGNAlgorithm v => SpecTranslate ctx (SignedDSIGN v a) where
460459

461460
toSpecRep (SignedDSIGN x) = pure $ signatureToInteger x
462461

463-
instance Typeable k => SpecTranslate ctx (WitVKey k) where
462+
instance SpecTranslate ctx (WitVKey k) where
464463
type SpecRep (WitVKey k) = (SpecRep (VKey k), Integer)
465464

466465
toSpecRep (WitVKey vk sk) = toSpecRep (vk, sk)

libs/cardano-ledger-core/CHANGELOG.md

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

33
## 1.18.0.0
44

5+
* Rename `wvkSig` to `wvkSignature`
6+
* Remove `eqBootstrapWitnessRaw` and `BootstrapWitnessRaw`
7+
* Rename `bwSig` to `bwSignature` for `BootstrapWitness`
8+
* Remove `witVKeyBytes` and `eqWitVKeyRaw`
9+
* Remove `EqRaw` instance for `WitVKey`
510
* Replace `Block'` constructor with `Block`
611
* Remove patterns: `Block`, `UnserialisedBlock` and `UnsafeUnserialisedBlock`
712
* Add ` EncCBORGroup (TxSeq era)` and `EncCBOR h` constraints to `EncCBOR` and `ToCBOR` instances for `Block`

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

0 commit comments

Comments
 (0)