1+ {-# LANGUAGE BangPatterns #-}
12{-# LANGUAGE DataKinds #-}
23{-# LANGUAGE DeriveAnyClass #-}
34{-# LANGUAGE DeriveGeneric #-}
1213{-# LANGUAGE StandaloneDeriving #-}
1314{-# LANGUAGE TypeApplications #-}
1415{-# LANGUAGE TypeFamilies #-}
15- {-# LANGUAGE TypeOperators #-}
1616{-# LANGUAGE UndecidableInstances #-}
1717{-# LANGUAGE UndecidableSuperClasses #-}
1818{-# LANGUAGE ViewPatterns #-}
@@ -82,11 +82,10 @@ import Data.Aeson (ToJSON (..), object, (.=))
8282import qualified Data.Aeson as Aeson (Value (Null , String ))
8383import Data.Bits
8484import Data.Maybe (fromMaybe )
85- import Data.Typeable (Proxy (.. ), (:~:) ( Refl ) )
85+ import Data.Typeable (Proxy (.. ))
8686import Data.Word
8787import GHC.Generics (Generic )
8888import GHC.Stack (HasCallStack )
89- import GHC.TypeLits
9089import Lens.Micro
9190import NoThunks.Class (InspectHeapNamed (.. ), NoThunks )
9291
@@ -114,19 +113,20 @@ data DataHash32
114113decodeAddress28 ::
115114 Credential 'Staking ->
116115 Addr28Extra ->
117- Maybe Addr
118- decodeAddress28 stakeRef (Addr28Extra a b c d) = do
119- Refl <- sameNat (Proxy @ (SizeHash ADDRHASH )) (Proxy @ 28 )
120- let network = if d `testBit` 1 then Mainnet else Testnet
121- paymentCred =
122- if d `testBit` 0
123- then KeyHashObj (KeyHash addrHash)
124- else ScriptHashObj (ScriptHash addrHash)
125- addrHash :: Hash ADDRHASH a
126- addrHash =
127- hashFromPackedBytes $
128- PackedBytes28 a b c (fromIntegral (d `shiftR` 32 ))
129- pure $! Addr network paymentCred (StakeRefBase stakeRef)
116+ Addr
117+ decodeAddress28 stakeRef (Addr28Extra a b c d) =
118+ let
119+ network = if d `testBit` 1 then Mainnet else Testnet
120+ paymentCred =
121+ if d `testBit` 0
122+ then KeyHashObj (KeyHash addrHash)
123+ else ScriptHashObj (ScriptHash addrHash)
124+ addrHash :: Hash ADDRHASH a
125+ addrHash =
126+ hashFromPackedBytes $
127+ PackedBytes28 a b c (fromIntegral (d `shiftR` 32 ))
128+ in
129+ Addr network paymentCred (StakeRefBase stakeRef)
130130{-# INLINE decodeAddress28 #-}
131131
132132data AlonzoTxOut era
@@ -155,16 +155,11 @@ deriving instance Generic (AlonzoTxOut era)
155155instance NFData (AlonzoTxOut era ) where
156156 rnf = rwhnf
157157
158- addressErrorMsg :: String
159- addressErrorMsg = " Impossible: Compacted an address of non-standard size"
160- {-# NOINLINE addressErrorMsg #-}
161-
162158decodeDataHash32 ::
163159 DataHash32 ->
164- Maybe DataHash
160+ DataHash
165161decodeDataHash32 (DataHash32 a b c d) = do
166- Refl <- sameNat (Proxy @ (SizeHash HASH )) (Proxy @ 32 )
167- Just $! unsafeMakeSafeHash $ hashFromPackedBytes $ PackedBytes32 a b c d
162+ unsafeMakeSafeHash $ hashFromPackedBytes $ PackedBytes32 a b c d
168163
169164viewCompactTxOut ::
170165 Val (Value era ) =>
@@ -173,15 +168,17 @@ viewCompactTxOut ::
173168viewCompactTxOut txOut = case txOut of
174169 TxOutCompact' addr val -> (addr, val, SNothing )
175170 TxOutCompactDH' addr val dh -> (addr, val, SJust dh)
176- TxOut_AddrHash28_AdaOnly stakeRef addr28Extra adaVal
177- | Just addr <- decodeAddress28 stakeRef addr28Extra ->
178- (compactAddr addr, injectCompact adaVal, SNothing )
179- | otherwise -> error addressErrorMsg
180- TxOut_AddrHash28_AdaOnly_DataHash32 stakeRef addr28Extra adaVal dataHash32
181- | Just addr <- decodeAddress28 stakeRef addr28Extra
182- , Just dh <- decodeDataHash32 dataHash32 ->
183- (compactAddr addr, injectCompact adaVal, SJust dh)
184- | otherwise -> error addressErrorMsg
171+ TxOut_AddrHash28_AdaOnly stakeRef addr28Extra adaVal ->
172+ let
173+ ! addr = decodeAddress28 stakeRef addr28Extra
174+ in
175+ (compactAddr addr, injectCompact adaVal, SNothing )
176+ TxOut_AddrHash28_AdaOnly_DataHash32 stakeRef addr28Extra adaVal dataHash32 ->
177+ let
178+ ! addr = decodeAddress28 stakeRef addr28Extra
179+ ! dh = decodeDataHash32 dataHash32
180+ in
181+ (compactAddr addr, injectCompact adaVal, SJust dh)
185182
186183viewTxOut ::
187184 Val (Value era ) =>
@@ -195,15 +192,15 @@ viewTxOut (TxOutCompactDH' bs c dh) = (addr, val, SJust dh)
195192 where
196193 addr = decompactAddr bs
197194 val = fromCompact c
198- viewTxOut (TxOut_AddrHash28_AdaOnly stakeRef addr28Extra adaVal)
199- | Just addr <- decodeAddress28 stakeRef addr28Extra =
200- (addr, inject (fromCompact adaVal), SNothing )
201- viewTxOut (TxOut_AddrHash28_AdaOnly_DataHash32 stakeRef addr28Extra adaVal dataHash32)
202- | Just addr <- decodeAddress28 stakeRef addr28Extra
203- , Just dh <- decodeDataHash32 dataHash32 =
204- (addr, inject (fromCompact adaVal), SJust dh)
205- viewTxOut TxOut_AddrHash28_AdaOnly {} = error addressErrorMsg
206- viewTxOut TxOut_AddrHash28_AdaOnly_DataHash32 {} = error addressErrorMsg
195+ viewTxOut (TxOut_AddrHash28_AdaOnly stakeRef addr28Extra adaVal) =
196+ let ! addr = decodeAddress28 stakeRef addr28Extra
197+ in (addr, inject (fromCompact adaVal), SNothing )
198+ viewTxOut (TxOut_AddrHash28_AdaOnly_DataHash32 stakeRef addr28Extra adaVal dataHash32) =
199+ let
200+ ! addr = decodeAddress28 stakeRef addr28Extra
201+ ! dh = decodeDataHash32 dataHash32
202+ in
203+ (addr, inject (fromCompact adaVal), SJust dh)
207204
208205instance (Era era , Val (Value era )) => Show (AlonzoTxOut era ) where
209206 show = show . viewTxOut -- FIXME: showing tuple is ugly
@@ -213,7 +210,7 @@ deriving via InspectHeapNamed "AlonzoTxOut" (AlonzoTxOut era) instance NoThunks
213210encodeAddress28 ::
214211 Network ->
215212 PaymentCredential ->
216- Maybe ( SizeHash ADDRHASH :~: 28 , Addr28Extra )
213+ Addr28Extra
217214encodeAddress28 network paymentCred = do
218215 let networkBit, payCredTypeBit :: Word64
219216 networkBit =
@@ -226,26 +223,28 @@ encodeAddress28 network paymentCred = do
226223 ScriptHashObj {} -> 0
227224 encodeAddr ::
228225 Hash ADDRHASH a ->
229- Maybe ( SizeHash ADDRHASH :~: 28 , Addr28Extra )
226+ Addr28Extra
230227 encodeAddr h = do
231- refl@ Refl <- sameNat (Proxy @ (SizeHash ADDRHASH )) (Proxy @ 28 )
232228 case hashToPackedBytes h of
233229 PackedBytes28 a b c d ->
234230 let d' = (fromIntegral d `shiftL` 32 ) .|. networkBit .|. payCredTypeBit
235- in Just (refl, Addr28Extra a b c d')
236- _ -> Nothing
231+ in Addr28Extra a b c d'
232+ _ ->
233+ -- This case should never match, but if it does, we output garbage
234+ Addr28Extra 0xaa 0xaa 0xaa 0xaa
237235 case paymentCred of
238236 KeyHashObj (KeyHash addrHash) -> encodeAddr addrHash
239237 ScriptHashObj (ScriptHash addrHash) -> encodeAddr addrHash
240238
241239encodeDataHash32 ::
242240 DataHash ->
243- Maybe ( SizeHash HASH :~: 32 , DataHash32 )
241+ DataHash32
244242encodeDataHash32 dataHash = do
245- refl@ Refl <- sameNat (Proxy @ (SizeHash HASH )) (Proxy @ 32 )
246243 case hashToPackedBytes (extractHash dataHash) of
247- PackedBytes32 a b c d -> Just (refl, DataHash32 a b c d)
248- _ -> Nothing
244+ PackedBytes32 a b c d -> DataHash32 a b c d
245+ _ ->
246+ -- This case should never match, but if it does, we output garbage
247+ DataHash32 0xaa 0xaa 0xaa 0xaa
249248
250249getAdaOnly ::
251250 forall era .
@@ -269,15 +268,17 @@ pattern AlonzoTxOut addr vl dh <-
269268 where
270269 AlonzoTxOut (Addr network paymentCred stakeRef) vl SNothing
271270 | StakeRefBase stakeCred <- stakeRef
272- , Just adaCompact <- getAdaOnly (Proxy @ era ) vl
273- , Just ( Refl , addr28Extra) <- encodeAddress28 network paymentCred =
274- TxOut_AddrHash28_AdaOnly stakeCred addr28Extra adaCompact
271+ , Just adaCompact <- getAdaOnly (Proxy @ era ) vl =
272+ let addr28Extra = encodeAddress28 network paymentCred
273+ in TxOut_AddrHash28_AdaOnly stakeCred addr28Extra adaCompact
275274 AlonzoTxOut (Addr network paymentCred stakeRef) vl (SJust dh)
276275 | StakeRefBase stakeCred <- stakeRef
277- , Just adaCompact <- getAdaOnly (Proxy @ era ) vl
278- , Just (Refl , addr28Extra) <- encodeAddress28 network paymentCred
279- , Just (Refl , dataHash32) <- encodeDataHash32 dh =
280- TxOut_AddrHash28_AdaOnly_DataHash32 stakeCred addr28Extra adaCompact dataHash32
276+ , Just adaCompact <- getAdaOnly (Proxy @ era ) vl =
277+ let
278+ addr28Extra = encodeAddress28 network paymentCred
279+ dataHash32 = encodeDataHash32 dh
280+ in
281+ TxOut_AddrHash28_AdaOnly_DataHash32 stakeCred addr28Extra adaCompact dataHash32
281282 AlonzoTxOut addr vl mdh =
282283 let v = fromMaybe (error $ " Illegal value in TxOut: " ++ show vl) $ toCompact vl
283284 a = compactAddr addr
@@ -440,15 +441,12 @@ mkTxOutCompact addr cAddr cVal mdh
440441
441442getAlonzoTxOutDataHash ::
442443 forall era .
443- HasCallStack =>
444444 AlonzoTxOut era ->
445445 StrictMaybe DataHash
446446getAlonzoTxOutDataHash = \ case
447447 TxOutCompactDH' _ _ dh -> SJust dh
448448 TxOut_AddrHash28_AdaOnly_DataHash32 _ _ _ dh ->
449- case decodeDataHash32 dh of
450- Nothing -> error $ " Impossible: Compacted a DataHash of non-standard size: " ++ show dh
451- Just dataHash -> SJust dataHash
449+ SJust $! decodeDataHash32 dh
452450 _ -> SNothing
453451
454452getAlonzoTxOutEitherAddr ::
@@ -457,12 +455,10 @@ getAlonzoTxOutEitherAddr ::
457455getAlonzoTxOutEitherAddr = \ case
458456 TxOutCompact' cAddr _ -> Right cAddr
459457 TxOutCompactDH' cAddr _ _ -> Right cAddr
460- TxOut_AddrHash28_AdaOnly stakeRef addr28Extra _
461- | Just addr <- decodeAddress28 stakeRef addr28Extra -> Left addr
462- | otherwise -> error addressErrorMsg
463- TxOut_AddrHash28_AdaOnly_DataHash32 stakeRef addr28Extra _ _
464- | Just addr <- decodeAddress28 stakeRef addr28Extra -> Left addr
465- | otherwise -> error addressErrorMsg
458+ TxOut_AddrHash28_AdaOnly stakeRef addr28Extra _ ->
459+ Left $! decodeAddress28 stakeRef addr28Extra
460+ TxOut_AddrHash28_AdaOnly_DataHash32 stakeRef addr28Extra _ _ ->
461+ Left $! decodeAddress28 stakeRef addr28Extra
466462
467463-- | Compute an estimate of the size of storing one UTxO entry.
468464-- This function implements the UTxO entry size estimate done by scaledMinDeposit in the ShelleyMA era
0 commit comments