Skip to content

Commit

Permalink
[alonzo] Redeemers
Browse files Browse the repository at this point in the history
  • Loading branch information
teodanciu committed Jan 29, 2025
1 parent 0a26a78 commit 0c97dae
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxWits.hs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ instance Memoized (Redeemers era) where
-- Since the 'Redeemers' exist outside of the transaction body,
-- this is how we ensure that they are not manipulated.
newtype Redeemers era = RedeemersConstr (MemoBytes (RedeemersRaw era))
deriving newtype (Generic, ToCBOR, SafeToHash, Typeable)
deriving newtype (Generic, ToCBOR, SafeToHash, Typeable, DecCBOR)

deriving newtype instance AlonzoEraScript era => Eq (Redeemers era)
deriving newtype instance AlonzoEraScript era => NFData (Redeemers era)
Expand Down Expand Up @@ -610,6 +610,40 @@ instance AlonzoEraScript era => DecCBOR (Annotator (RedeemersRaw era)) where
{-# INLINE decodeElement #-}
{-# INLINE decCBOR #-}

instance AlonzoEraScript era => DecCBOR (RedeemersRaw era) where
decCBOR =
ifDecoderVersionAtLeast
(natVersion @9)
( peekTokenType >>= \case
TypeMapLenIndef -> decodeMapRedeemers
TypeMapLen -> decodeMapRedeemers
_ -> decodeListRedeemers
)
(RedeemersRaw . Map.fromList <$> decodeList decodeElement)
where
decodeMapRedeemers :: Decoder s (RedeemersRaw era)
decodeMapRedeemers =
RedeemersRaw . Map.fromList . NE.toList <$> do
(_, xs) <- decodeListLikeWithCount decodeMapLenOrIndef (:) $ \_ -> do
ptr <- decCBOR
(annData, exUnits) <- decCBOR
pure (ptr, (annData, exUnits))
case NE.nonEmpty xs of
Nothing -> fail "Expected redeemers map to be non-empty"
Just neList -> pure $ NE.reverse neList
decodeListRedeemers :: Decoder s (RedeemersRaw era)
decodeListRedeemers =
RedeemersRaw . Map.fromList . NE.toList
<$> decodeNonEmptyList decodeElement
decodeElement :: Decoder s (PlutusPurpose AsIx era, (Data era, ExUnits))
decodeElement = do
decodeRecordNamed
"Redeemer"
(\(rdmrPtr, _) -> fromIntegral (listLen rdmrPtr) + 2)
$ (,) <$> decCBORGroup <*> ((,) <$> decCBOR <*> decCBOR)
{-# INLINE decodeElement #-}
{-# INLINE decCBOR #-}

-- | Encodes memoized bytes created upon construction.
instance AlonzoEraScript era => EncCBOR (Redeemers era)

Expand Down

0 comments on commit 0c97dae

Please sign in to comment.