@@ -59,6 +59,7 @@ import Cardano.Ledger.BaseTypes (ProtVer)
5959import Cardano.Ledger.Binary (
6060 Annotator (.. ),
6161 DecCBOR (.. ),
62+ Decoder ,
6263 EncCBOR (.. ),
6364 ToCBOR ,
6465 TokenType (.. ),
@@ -183,16 +184,10 @@ getAlonzoTxAuxDataScripts AlonzoTxAuxData {atadTimelock = timelocks, atadPlutus
183184
184185instance Era era => DecCBOR (Annotator (AlonzoTxAuxDataRaw era )) where
185186 decCBOR =
186- peekTokenType >>= \ case
187- TypeMapLen -> decodeShelley
188- TypeMapLen64 -> decodeShelley
189- TypeMapLenIndef -> decodeShelley
190- TypeListLen -> decodeShelleyMA
191- TypeListLen64 -> decodeShelleyMA
192- TypeListLenIndef -> decodeShelleyMA
193- TypeTag -> decodeAlonzo
194- TypeTag64 -> decodeAlonzo
195- _ -> fail " Failed to decode AlonzoTxAuxData"
187+ decodeTxAuxDataByTokenType @ (Annotator (AlonzoTxAuxDataRaw era ))
188+ decodeShelley
189+ decodeAllegra
190+ decodeAlonzo
196191 where
197192 decodeShelley =
198193 decode
@@ -201,7 +196,7 @@ instance Era era => DecCBOR (Annotator (AlonzoTxAuxDataRaw era)) where
201196 <*! Ann (Emit StrictSeq. empty)
202197 <*! Ann (Emit Map. empty)
203198 )
204- decodeShelleyMA =
199+ decodeAllegra =
205200 decode
206201 ( Ann (RecD AlonzoTxAuxDataRaw )
207202 <*! Ann From
@@ -214,13 +209,6 @@ instance Era era => DecCBOR (Annotator (AlonzoTxAuxDataRaw era)) where
214209 TagD 259 $
215210 SparseKeyed " AlonzoTxAuxData" (pure emptyAuxData) auxDataField []
216211
217- addPlutusScripts lang scripts ad =
218- case NE. nonEmpty scripts of
219- Nothing -> ad
220- Just neScripts ->
221- -- Avoid leaks by deepseq, since non empty list is lazy.
222- neScripts `deepseq` ad {atadrPlutus = Map. insert lang neScripts $ atadrPlutus ad}
223-
224212 auxDataField :: Word -> Field (Annotator (AlonzoTxAuxDataRaw era ))
225213 auxDataField 0 = fieldA (\ x ad -> ad {atadrMetadata = x}) From
226214 auxDataField 1 =
@@ -232,6 +220,53 @@ instance Era era => DecCBOR (Annotator (AlonzoTxAuxDataRaw era)) where
232220 auxDataField 4 = fieldA (addPlutusScripts PlutusV3 ) (D (guardPlutus PlutusV3 >> decCBOR))
233221 auxDataField n = field (\ _ t -> t) (Invalid n)
234222
223+ instance Era era => DecCBOR (AlonzoTxAuxDataRaw era ) where
224+ decCBOR =
225+ decodeTxAuxDataByTokenType @ (AlonzoTxAuxDataRaw era )
226+ decodeShelley
227+ decodeAllegra
228+ decodeAlonzo
229+ where
230+ decodeShelley =
231+ decode
232+ (Emit AlonzoTxAuxDataRaw <! From <! Emit StrictSeq. empty <! Emit Map. empty)
233+ decodeAllegra =
234+ decode
235+ (RecD AlonzoTxAuxDataRaw <! From <! From <! Emit Map. empty)
236+ decodeAlonzo =
237+ decode $
238+ TagD 259 $
239+ SparseKeyed " AlonzoTxAuxData" emptyAuxData auxDataField []
240+
241+ auxDataField :: Word -> Field (AlonzoTxAuxDataRaw era )
242+ auxDataField 0 = field (\ x ad -> ad {atadrMetadata = x}) From
243+ auxDataField 1 = field (\ x ad -> ad {atadrTimelock = atadrTimelock ad <> x}) From
244+ auxDataField 2 = field (addPlutusScripts PlutusV1 ) (D (guardPlutus PlutusV1 >> decCBOR))
245+ auxDataField 3 = field (addPlutusScripts PlutusV2 ) (D (guardPlutus PlutusV2 >> decCBOR))
246+ auxDataField 4 = field (addPlutusScripts PlutusV3 ) (D (guardPlutus PlutusV3 >> decCBOR))
247+ auxDataField n = field (\ _ t -> t) (Invalid n)
248+
249+ decodeTxAuxDataByTokenType :: forall t s . Decoder s t -> Decoder s t -> Decoder s t -> Decoder s t
250+ decodeTxAuxDataByTokenType decodeShelley decodeAllegra decodeAlonzo =
251+ peekTokenType >>= \ case
252+ TypeMapLen -> decodeShelley
253+ TypeMapLen64 -> decodeShelley
254+ TypeMapLenIndef -> decodeShelley
255+ TypeListLen -> decodeAllegra
256+ TypeListLen64 -> decodeAllegra
257+ TypeListLenIndef -> decodeAllegra
258+ TypeTag -> decodeAlonzo
259+ TypeTag64 -> decodeAlonzo
260+ _ -> fail " Failed to decode AlonzoTxAuxData"
261+
262+ addPlutusScripts :: Language -> [PlutusBinary ] -> AlonzoTxAuxDataRaw era -> AlonzoTxAuxDataRaw era
263+ addPlutusScripts lang scripts ad =
264+ case NE. nonEmpty scripts of
265+ Nothing -> ad
266+ Just neScripts ->
267+ -- Avoid leaks by deepseq, since non empty list is lazy.
268+ neScripts `deepseq` ad {atadrPlutus = Map. insert lang neScripts $ atadrPlutus ad}
269+
235270emptyAuxData :: AlonzoTxAuxDataRaw era
236271emptyAuxData = AlonzoTxAuxDataRaw mempty mempty mempty
237272
@@ -240,7 +275,7 @@ emptyAuxData = AlonzoTxAuxDataRaw mempty mempty mempty
240275
241276newtype AlonzoTxAuxData era = AlonzoTxAuxDataConstr (MemoBytes (AlonzoTxAuxDataRaw era ))
242277 deriving (Generic )
243- deriving newtype (ToCBOR , SafeToHash )
278+ deriving newtype (ToCBOR , SafeToHash , DecCBOR )
244279
245280instance Memoized (AlonzoTxAuxData era ) where
246281 type RawType (AlonzoTxAuxData era ) = AlonzoTxAuxDataRaw era
0 commit comments