@@ -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 )
7174import Cardano.Ledger.Coin (Coin (.. ))
7275import Cardano.Ledger.Compactible
@@ -77,11 +80,12 @@ import Cardano.Ledger.Shelley.Core
7780import qualified Cardano.Ledger.Shelley.TxOut as Shelley
7881import Cardano.Ledger.Val (Val (.. ))
7982import Control.DeepSeq (NFData (.. ), rwhnf )
80- import Control.Monad (guard , (<$!>) )
83+ import Control.Monad (guard )
8184import Data.Aeson (ToJSON (.. ), object , (.=) )
8285import qualified Data.Aeson as Aeson (Value (Null , String ))
8386import Data.Bits
8487import Data.Maybe (fromMaybe )
88+ import Data.MemPack
8589import Data.Typeable (Proxy (.. ), (:~:) (Refl ))
8690import Data.Word
8791import 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+
106117data 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+
114132decodeAddress28 ::
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+
150204deriving stock instance (Eq (Value era ), Compactible (Value era )) => Eq (AlonzoTxOut era )
151205
152206deriving 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
374433internAlonzoTxOut ::
0 commit comments