Skip to content

Commit ff526df

Browse files
authored
Merge pull request #4811 from IntersectMBO/lehins/use-mempack-newest
Integration of MemPack
2 parents 4ee11aa + e98520f commit ff526df

File tree

49 files changed

+516
-62
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+516
-62
lines changed

.github/workflows/haskell.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ jobs:
8787
uses: input-output-hk/actions/haskell@latest
8888
with:
8989
ghc-version: ${{ matrix.ghc }}
90-
cabal-version: 3.12.1.0
90+
cabal-version: 3.14
9191

9292
- name: Configure to use libsodium
9393
run: |
@@ -266,7 +266,7 @@ jobs:
266266
uses: input-output-hk/actions/haskell@latest
267267
with:
268268
ghc-version: ${{ matrix.ghc }}
269-
cabal-version: 3.12.1.0
269+
cabal-version: 3.14
270270

271271
- name: Set up Ruby 2.7
272272
if: contains(fromJson(env.packages-with-ruby-cddl-tests), matrix.package)
@@ -432,7 +432,7 @@ jobs:
432432
uses: input-output-hk/actions/haskell@latest
433433
with:
434434
ghc-version: 9.10.1
435-
cabal-version: 3.12.1.0
435+
cabal-version: 3.14
436436

437437
- name: Install gen-hie if not cached
438438
if: steps.cache-gen-hie.outputs.cache-hit != 'true'

eras/allegra/impl/CHANGELOG.md

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

33
## 1.7.0.0
44

5+
* Add `MemPack` instance for `Timelock`
56
* Remove deprecated `AuxiliaryData` type synonym
67
* Deprecate `Allegra` type synonym
78
* Remove crypto parametrization from `AllegraEra`

eras/allegra/impl/cardano-ledger-allegra.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ library
7474
cborg,
7575
containers,
7676
deepseq,
77+
mempack,
7778
microlens,
7879
nothunks,
7980
small-steps >=1.1,

eras/allegra/impl/src/Cardano/Ledger/Allegra/Scripts.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ import Cardano.Ledger.Shelley.Scripts (
8787
pattern RequireMOf,
8888
pattern RequireSignature,
8989
)
90+
import Data.MemPack
9091

9192
import Cardano.Slotting.Slot (SlotNo (..))
9293
import Control.DeepSeq (NFData (..))
@@ -209,7 +210,7 @@ instance Era era => DecCBOR (Annotator (TimelockRaw era)) where
209210

210211
newtype Timelock era = TimelockConstr (MemoBytes TimelockRaw era)
211212
deriving (Eq, Generic)
212-
deriving newtype (ToCBOR, NoThunks, NFData, SafeToHash)
213+
deriving newtype (ToCBOR, NoThunks, NFData, SafeToHash, MemPack)
213214

214215
instance Era era => EncCBOR (Timelock era)
215216

eras/alonzo/impl/CHANGELOG.md

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

33
## 1.13.0.0
44

5+
* Add `MemPack` instance for `Addr28Extra`, `DataHash32`, `AlonzoTxOut` and `PlutusScript AlonzoEra`
56
* Deprecate `hashAlonzoTxAuxData`
67
* Stop re-exporting deprecated `AuxiliaryDataHash` from `Cardano.Ledger.Alonzo.TxAuxData`
78
* Deprecate `Alonzo` type synonym

eras/alonzo/impl/cardano-ledger-alonzo.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ library
8989
containers,
9090
data-default,
9191
deepseq,
92+
mempack,
9293
microlens,
9394
mtl,
9495
nothunks,

eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Scripts.hs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ import qualified Data.ByteString as BS
114114
import Data.Kind (Type)
115115
import qualified Data.Map.Strict as Map
116116
import Data.Maybe (fromJust, isJust)
117+
import Data.MemPack
117118
import Data.Typeable
118119
import Data.Word (Word16, Word32, Word8)
119120
import GHC.Generics (Generic)
@@ -417,6 +418,21 @@ data AlonzoScript era
417418
| PlutusScript !(PlutusScript era)
418419
deriving (Generic)
419420

421+
instance (Era era, MemPack (PlutusScript era)) => MemPack (AlonzoScript era) where
422+
packedByteCount = \case
423+
TimelockScript script -> packedTagByteCount + packedByteCount script
424+
PlutusScript script -> packedTagByteCount + packedByteCount script
425+
packM = \case
426+
TimelockScript script -> packTagM 0 >> packM script
427+
PlutusScript script -> packTagM 1 >> packM script
428+
{-# INLINE packM #-}
429+
unpackM =
430+
unpackTagM >>= \case
431+
0 -> TimelockScript <$> unpackM
432+
1 -> PlutusScript <$> unpackM
433+
n -> unknownTagM @(AlonzoScript era) n
434+
{-# INLINE unpackM #-}
435+
420436
deriving instance Eq (PlutusScript era) => Eq (AlonzoScript era)
421437

422438
instance (Era era, NoThunks (PlutusScript era)) => NoThunks (AlonzoScript era)
@@ -531,6 +547,23 @@ instance Eq (PlutusScript era) => EqRaw (AlonzoScript era) where
531547
instance AlonzoEraScript era => ToJSON (AlonzoScript era) where
532548
toJSON = String . serializeAsHexText
533549

550+
-- | It might seem that this instance unnecessarily utilizes a zero Tag, but it is needed for
551+
-- forward compatibility with plutus scripts from future eras.
552+
--
553+
-- That being said, currently this instance is not used at all, since reference scripts where
554+
-- introduced in Babbage era and `MemPack` for now is only used for `TxOut`s
555+
instance MemPack (PlutusScript AlonzoEra) where
556+
packedByteCount = \case
557+
AlonzoPlutusV1 script -> packedTagByteCount + packedByteCount script
558+
packM = \case
559+
AlonzoPlutusV1 script -> packTagM 0 >> packM script
560+
{-# INLINE packM #-}
561+
unpackM =
562+
unpackTagM >>= \case
563+
0 -> AlonzoPlutusV1 <$> unpackM
564+
n -> unknownTagM @(PlutusScript AlonzoEra) n
565+
{-# INLINE unpackM #-}
566+
534567
--------------------------------------------------------------------------------
535568
-- Serialisation
536569
--------------------------------------------------------------------------------

eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxOut.hs

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,14 @@ import Cardano.Ledger.Binary (
6262
FromCBOR (..),
6363
Interns,
6464
ToCBOR (..),
65+
TokenType (..),
6566
cborError,
6667
decodeBreakOr,
6768
decodeListLenOrIndef,
69+
decodeMemPack,
6870
encodeListLen,
6971
interns,
72+
peekTokenType,
7073
)
7174
import Cardano.Ledger.Coin (Coin (..))
7275
import Cardano.Ledger.Compactible
@@ -77,11 +80,12 @@ import Cardano.Ledger.Shelley.Core
7780
import qualified Cardano.Ledger.Shelley.TxOut as Shelley
7881
import Cardano.Ledger.Val (Val (..))
7982
import Control.DeepSeq (NFData (..), rwhnf)
80-
import Control.Monad (guard, (<$!>))
83+
import Control.Monad (guard)
8184
import Data.Aeson (ToJSON (..), object, (.=))
8285
import qualified Data.Aeson as Aeson (Value (Null, String))
8386
import Data.Bits
8487
import Data.Maybe (fromMaybe)
88+
import Data.MemPack
8589
import Data.Typeable (Proxy (..), (:~:) (Refl))
8690
import Data.Word
8791
import GHC.Generics (Generic)
@@ -103,6 +107,13 @@ data Addr28Extra
103107
{-# UNPACK #-} !Word64 -- Payment Addr (32bits) + ... + 0/1 for Testnet/Mainnet + 0/1 Script/Pubkey
104108
deriving (Eq, Show, Generic, NoThunks)
105109

110+
instance MemPack Addr28Extra where
111+
packedByteCount _ = 32
112+
packM (Addr28Extra w0 w1 w2 w3) = packM w0 >> packM w1 >> packM w2 >> packM w3
113+
{-# INLINE packM #-}
114+
unpackM = Addr28Extra <$> unpackM <*> unpackM <*> unpackM <*> unpackM
115+
{-# INLINE unpackM #-}
116+
106117
data DataHash32
107118
= DataHash32
108119
{-# UNPACK #-} !Word64 -- DataHash
@@ -111,6 +122,13 @@ data DataHash32
111122
{-# UNPACK #-} !Word64 -- DataHash
112123
deriving (Eq, Show, Generic, NoThunks)
113124

125+
instance MemPack DataHash32 where
126+
packedByteCount _ = 32
127+
packM (DataHash32 w0 w1 w2 w3) = packM w0 >> packM w1 >> packM w2 >> packM w3
128+
{-# INLINE packM #-}
129+
unpackM = DataHash32 <$> unpackM <*> unpackM <*> unpackM <*> unpackM
130+
{-# INLINE unpackM #-}
131+
114132
decodeAddress28 ::
115133
Credential 'Staking ->
116134
Addr28Extra ->
@@ -147,6 +165,42 @@ data AlonzoTxOut era
147165
{-# UNPACK #-} !(CompactForm Coin) -- Ada value
148166
{-# UNPACK #-} !DataHash32
149167

168+
-- | This instance is backwards compatible in binary representation with TxOut instances for all
169+
-- previous era
170+
instance (Era era, MemPack (CompactForm (Value era))) => MemPack (AlonzoTxOut era) where
171+
packedByteCount = \case
172+
TxOutCompact' cAddr cValue ->
173+
packedTagByteCount + packedByteCount cAddr + packedByteCount cValue
174+
TxOutCompactDH' cAddr cValue dataHash ->
175+
packedTagByteCount + packedByteCount cAddr + packedByteCount cValue + packedByteCount dataHash
176+
TxOut_AddrHash28_AdaOnly cred addr28 cCoin ->
177+
packedTagByteCount + packedByteCount cred + packedByteCount addr28 + packedByteCount cCoin
178+
TxOut_AddrHash28_AdaOnly_DataHash32 cred addr28 cCoin dataHash32 ->
179+
packedTagByteCount
180+
+ packedByteCount cred
181+
+ packedByteCount addr28
182+
+ packedByteCount cCoin
183+
+ packedByteCount dataHash32
184+
{-# INLINE packedByteCount #-}
185+
packM = \case
186+
TxOutCompact' cAddr cValue ->
187+
packTagM 0 >> packM cAddr >> packM cValue
188+
TxOutCompactDH' cAddr cValue dataHash ->
189+
packTagM 1 >> packM cAddr >> packM cValue >> packM dataHash
190+
TxOut_AddrHash28_AdaOnly cred addr28 cCoin ->
191+
packTagM 2 >> packM cred >> packM addr28 >> packM cCoin
192+
TxOut_AddrHash28_AdaOnly_DataHash32 cred addr28 cCoin dataHash32 ->
193+
packTagM 3 >> packM cred >> packM addr28 >> packM cCoin >> packM dataHash32
194+
{-# INLINE packM #-}
195+
unpackM =
196+
unpackTagM >>= \case
197+
0 -> TxOutCompact' <$> unpackM <*> unpackM
198+
1 -> TxOutCompactDH' <$> unpackM <*> unpackM <*> unpackM
199+
2 -> TxOut_AddrHash28_AdaOnly <$> unpackM <*> unpackM <*> unpackM
200+
3 -> TxOut_AddrHash28_AdaOnly_DataHash32 <$> unpackM <*> unpackM <*> unpackM <*> unpackM
201+
n -> unknownTagM @(AlonzoTxOut era) n
202+
{-# INLINE unpackM #-}
203+
150204
deriving stock instance (Eq (Value era), Compactible (Value era)) => Eq (AlonzoTxOut era)
151205

152206
deriving instance Generic (AlonzoTxOut era)
@@ -365,10 +419,15 @@ instance (Era era, Val (Value era)) => DecCBOR (AlonzoTxOut era) where
365419
Just _ -> cborError $ DecoderErrorCustom "txout" "wrong number of terms in txout"
366420
{-# INLINEABLE decCBOR #-}
367421

368-
instance (Era era, Val (Value era)) => DecShareCBOR (AlonzoTxOut era) where
422+
instance (Era era, Val (Value era), MemPack (CompactForm (Value era))) => DecShareCBOR (AlonzoTxOut era) where
369423
type Share (AlonzoTxOut era) = Interns (Credential 'Staking)
370424
decShareCBOR credsInterns = do
371-
internAlonzoTxOut (interns credsInterns) <$!> decCBOR
425+
txOut <-
426+
peekTokenType >>= \case
427+
TypeBytes -> decodeMemPack
428+
TypeBytesIndef -> decodeMemPack
429+
_ -> decCBOR
430+
pure $! internAlonzoTxOut (interns credsInterns) txOut
372431
{-# INLINEABLE decShareCBOR #-}
373432

374433
internAlonzoTxOut ::

eras/babbage/impl/CHANGELOG.md

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

33
## 1.11.0.0
44

5+
* Add `MemPack` instance for `BabbageTxOut` and `PlutusScript BabbageEra`
56
* Deprecate `Babbage` type synonym
67
* Remove crypto parametrization from `BabbageEra`
78

eras/babbage/impl/cardano-ledger-babbage.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ library
8484
cardano-strict-containers,
8585
containers,
8686
deepseq,
87+
mempack,
8788
microlens,
8889
nothunks,
8990
plutus-ledger-api >=1.33,

0 commit comments

Comments
 (0)