Skip to content

Commit

Permalink
Merge pull request #4627 from IntersectMBO/td/fix-reg-txcert-pattern
Browse files Browse the repository at this point in the history
Fix Conway implementation of RegTxCert and UnRegTxCert
  • Loading branch information
teodanciu authored Sep 13, 2024
2 parents db39c75 + 85aee1a commit 492d54c
Show file tree
Hide file tree
Showing 16 changed files with 189 additions and 66 deletions.
14 changes: 11 additions & 3 deletions eras/alonzo/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# Version history for `cardano-ledger-alonzo`

## 1.10.2.1

*
## 1.11.0.0

* Add `ProtVer` argument to functions in `EraPlutusTxInfo` class:
* `toPlutusTxCert` of
* `toPlutusScriptPurpose`
* `toPlutusArgs`
* Add `ProtVer` argument to `TxInfo` functions:
* `transTxBodyCerts`
* `transPlutusPurpose`
* `toPlutusV1Args`
* `toLegacyPlutusArgs`

## 1.10.2.0

Expand Down
2 changes: 1 addition & 1 deletion eras/alonzo/impl/cardano-ledger-alonzo.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.0
name: cardano-ledger-alonzo
version: 1.10.2.1
version: 1.11.0.0
license: Apache-2.0
maintainer: [email protected]
author: IOHK
Expand Down
11 changes: 9 additions & 2 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Plutus/Context.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,15 @@ data LedgerTxInfo era = LedgerTxInfo
}

class (PlutusLanguage l, EraPlutusContext era) => EraPlutusTxInfo (l :: Language) era where
toPlutusTxCert :: proxy l -> TxCert era -> Either (ContextError era) (PlutusTxCert l)
toPlutusTxCert ::
proxy l ->
ProtVer ->
TxCert era ->
Either (ContextError era) (PlutusTxCert l)

toPlutusScriptPurpose ::
proxy l ->
ProtVer ->
PlutusPurpose AsIxItem era ->
Either (ContextError era) (PlutusScriptPurpose l)

Expand All @@ -85,6 +90,7 @@ class (PlutusLanguage l, EraPlutusContext era) => EraPlutusTxInfo (l :: Language

toPlutusArgs ::
proxy l ->
ProtVer ->
PlutusTxInfo l ->
PlutusPurpose AsIxItem era ->
Maybe (Data era) ->
Expand Down Expand Up @@ -129,7 +135,8 @@ toPlutusWithContext script scriptHash plutusPurpose lti (redeemerData, exUnits)
maybeSpendingDatum =
getSpendingDatum (ltiUTxO lti) (ltiTx lti) (hoistPlutusPurpose toAsItem plutusPurpose)
txInfo <- toPlutusTxInfo proxy lti
plutusArgs <- toPlutusArgs proxy txInfo plutusPurpose maybeSpendingDatum redeemerData
plutusArgs <-
toPlutusArgs proxy (ltiProtVer lti) txInfo plutusPurpose maybeSpendingDatum redeemerData
pure $
PlutusWithContext
{ pwcProtocolVersion = pvMajor (ltiProtVer lti)
Expand Down
26 changes: 15 additions & 11 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Plutus/TxInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,17 @@ import NoThunks.Class (NoThunks)
import qualified PlutusLedgerApi.V1 as PV1

instance Crypto c => EraPlutusTxInfo 'PlutusV1 (AlonzoEra c) where
toPlutusTxCert _ = pure . transTxCert
toPlutusTxCert _ _ = pure . transTxCert

toPlutusScriptPurpose proxy = transPlutusPurpose proxy . hoistPlutusPurpose toAsItem
toPlutusScriptPurpose proxy pv = transPlutusPurpose proxy pv . hoistPlutusPurpose toAsItem

toPlutusTxInfo proxy LedgerTxInfo {ltiProtVer, ltiEpochInfo, ltiSystemStart, ltiUTxO, ltiTx} = do
timeRange <-
transValidityInterval ltiTx ltiProtVer ltiEpochInfo ltiSystemStart (txBody ^. vldtTxBodyL)
txInsMaybes <- forM (Set.toList (txBody ^. inputsTxBodyL)) $ \txIn -> do
txOut <- transLookupTxOut ltiUTxO txIn
pure $ PV1.TxInInfo (transTxIn txIn) <$> transTxOut txOut
txCerts <- transTxBodyCerts proxy txBody
txCerts <- transTxBodyCerts proxy ltiProtVer txBody
Right $
PV1.TxInfo
{ -- A mistake was made in Alonzo of filtering out Byron addresses, so we need to
Expand All @@ -135,25 +135,27 @@ instance Crypto c => EraPlutusTxInfo 'PlutusV1 (AlonzoEra c) where
toPlutusV1Args ::
EraPlutusTxInfo 'PlutusV1 era =>
proxy 'PlutusV1 ->
ProtVer ->
PV1.TxInfo ->
PlutusPurpose AsIxItem era ->
Maybe (Data era) ->
Data era ->
Either (ContextError era) (PlutusArgs 'PlutusV1)
toPlutusV1Args proxy txInfo scriptPurpose maybeSpendingData redeemerData =
toPlutusV1Args proxy pv txInfo scriptPurpose maybeSpendingData redeemerData =
PlutusV1Args
<$> toLegacyPlutusArgs proxy (PV1.ScriptContext txInfo) scriptPurpose maybeSpendingData redeemerData
<$> toLegacyPlutusArgs proxy pv (PV1.ScriptContext txInfo) scriptPurpose maybeSpendingData redeemerData

toLegacyPlutusArgs ::
EraPlutusTxInfo l era =>
proxy l ->
ProtVer ->
(PlutusScriptPurpose l -> PlutusScriptContext l) ->
PlutusPurpose AsIxItem era ->
Maybe (Data era) ->
Data era ->
Either (ContextError era) (LegacyPlutusArgs l)
toLegacyPlutusArgs proxy mkScriptContext scriptPurpose maybeSpendingData redeemerData = do
scriptContext <- mkScriptContext <$> toPlutusScriptPurpose proxy scriptPurpose
toLegacyPlutusArgs proxy pv mkScriptContext scriptPurpose maybeSpendingData redeemerData = do
scriptContext <- mkScriptContext <$> toPlutusScriptPurpose proxy pv scriptPurpose
let redeemer = getPlutusData redeemerData
pure $ case maybeSpendingData of
Nothing -> LegacyPlutusArgs2 redeemer scriptContext
Expand Down Expand Up @@ -257,10 +259,11 @@ transTxBodyId txBody = PV1.TxId (transSafeHash (hashAnnotated txBody))
transTxBodyCerts ::
(EraPlutusTxInfo l era, EraTxBody era) =>
proxy l ->
ProtVer ->
TxBody era ->
Either (ContextError era) [PlutusTxCert l]
transTxBodyCerts proxy txBody =
mapM (toPlutusTxCert proxy) $ F.toList (txBody ^. certsTxBodyL)
transTxBodyCerts proxy pv txBody =
mapM (toPlutusTxCert proxy pv) $ F.toList (txBody ^. certsTxBodyL)

transWithdrawals :: Withdrawals c -> Map.Map PV1.StakingCredential Integer
transWithdrawals (Withdrawals mp) = Map.foldlWithKey' accum Map.empty mp
Expand Down Expand Up @@ -348,11 +351,12 @@ transTxCertCommon = \case
transPlutusPurpose ::
(EraPlutusTxInfo l era, PlutusTxCert l ~ PV1.DCert) =>
proxy l ->
ProtVer ->
AlonzoPlutusPurpose AsItem era ->
Either (ContextError era) PV1.ScriptPurpose
transPlutusPurpose proxy = \case
transPlutusPurpose proxy pv = \case
AlonzoSpending (AsItem txIn) -> pure $ PV1.Spending (transTxIn txIn)
AlonzoMinting (AsItem policyId) -> pure $ PV1.Minting (transPolicyID policyId)
AlonzoCertifying (AsItem txCert) -> PV1.Certifying <$> toPlutusTxCert proxy txCert
AlonzoCertifying (AsItem txCert) -> PV1.Certifying <$> toPlutusTxCert proxy pv txCert
AlonzoRewarding (AsItem rewardAccount) ->
pure $ PV1.Rewarding (PV1.StakingHash (transRewardAccount rewardAccount))
6 changes: 4 additions & 2 deletions eras/babbage/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Version history for `cardano-ledger-babbage`

## 1.9.0.1
## 1.10.0.0

*
* Add `ProtVer` argument to `TxInfo` functions:
* `transTxRedeemers`
* `toPlutusV2Args`

## 1.9.0.0

Expand Down
4 changes: 2 additions & 2 deletions eras/babbage/impl/cardano-ledger-babbage.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.0
name: cardano-ledger-babbage
version: 1.9.0.1
version: 1.10.0.0
license: Apache-2.0
maintainer: [email protected]
author: IOHK
Expand Down Expand Up @@ -73,7 +73,7 @@ library
cardano-crypto-class,
cardano-data >=1.2,
cardano-ledger-allegra ^>=1.6,
cardano-ledger-alonzo >=1.9 && <1.11,
cardano-ledger-alonzo >=1.11 && <1.12,
cardano-ledger-binary >=1.3,
cardano-ledger-core ^>=1.15,
cardano-ledger-mary ^>=1.7,
Expand Down
37 changes: 23 additions & 14 deletions eras/babbage/impl/src/Cardano/Ledger/Babbage/TxInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ import Cardano.Ledger.Babbage.Core
import Cardano.Ledger.Babbage.Era (BabbageEra)
import Cardano.Ledger.Babbage.Scripts (PlutusScript (..))
import Cardano.Ledger.Babbage.UTxO ()
import Cardano.Ledger.BaseTypes (Inject (..), StrictMaybe (..), isSJust, kindObject)
import Cardano.Ledger.BaseTypes (
Inject (..),
ProtVer,
StrictMaybe (..),
isSJust,
kindObject,
)
import Cardano.Ledger.Binary (DecCBOR (..), EncCBOR (..))
import Cardano.Ledger.Binary.Coders (
Decode (..),
Expand Down Expand Up @@ -184,14 +190,15 @@ transRedeemerPtr ::
, Inject (BabbageContextError era) (ContextError era)
) =>
proxy l ->
ProtVer ->
TxBody era ->
(PlutusPurpose AsIx era, (Data era, ExUnits)) ->
Either (ContextError era) (PlutusScriptPurpose l, PV2.Redeemer)
transRedeemerPtr proxy txBody (ptr, (d, _)) =
transRedeemerPtr proxy pv txBody (ptr, (d, _)) =
case redeemerPointerInverse txBody ptr of
SNothing -> Left $ inject $ RedeemerPointerPointsToNothing ptr
SJust sp -> do
plutusScriptPurpose <- toPlutusScriptPurpose proxy sp
plutusScriptPurpose <- toPlutusScriptPurpose proxy pv sp
Right (plutusScriptPurpose, transRedeemer d)

-- | Translate all `Redeemers` from within a `Tx` into a Map from a `PlutusScriptPurpose`
Expand All @@ -204,12 +211,13 @@ transTxRedeemers ::
, Inject (BabbageContextError era) (ContextError era)
) =>
proxy l ->
ProtVer ->
Tx era ->
Either (ContextError era) (PV2.Map (PlutusScriptPurpose l) PV2.Redeemer)
transTxRedeemers proxy tx =
transTxRedeemers proxy pv tx =
PV2.unsafeFromList
<$> mapM
(transRedeemerPtr proxy (tx ^. bodyTxL))
(transRedeemerPtr proxy pv (tx ^. bodyTxL))
(Map.toList (unRedeemers $ tx ^. witsTxL . rdmrsTxWitsL))

instance Crypto c => EraPlutusContext (BabbageEra c) where
Expand Down Expand Up @@ -288,9 +296,9 @@ instance ToJSON (PlutusPurpose AsIx era) => ToJSON (BabbageContextError era) whe
<> T.intercalate ", " (map txInToText (Set.toList txIns))

instance Crypto c => EraPlutusTxInfo 'PlutusV1 (BabbageEra c) where
toPlutusTxCert _ = pure . Alonzo.transTxCert
toPlutusTxCert _ _ = pure . Alonzo.transTxCert

toPlutusScriptPurpose proxy = Alonzo.transPlutusPurpose proxy . hoistPlutusPurpose toAsItem
toPlutusScriptPurpose proxy pv = Alonzo.transPlutusPurpose proxy pv . hoistPlutusPurpose toAsItem

toPlutusTxInfo proxy LedgerTxInfo {ltiProtVer, ltiEpochInfo, ltiSystemStart, ltiUTxO, ltiTx} = do
let refInputs = txBody ^. referenceInputsTxBodyL
Expand All @@ -304,7 +312,7 @@ instance Crypto c => EraPlutusTxInfo 'PlutusV1 (BabbageEra c) where
(transTxOutV1 . TxOutFromOutput)
[minBound ..]
(F.toList (txBody ^. outputsTxBodyL))
txCerts <- Alonzo.transTxBodyCerts proxy txBody
txCerts <- Alonzo.transTxBodyCerts proxy ltiProtVer txBody
pure
PV1.TxInfo
{ PV1.txInfoInputs = inputs
Expand All @@ -324,9 +332,9 @@ instance Crypto c => EraPlutusTxInfo 'PlutusV1 (BabbageEra c) where
toPlutusArgs = Alonzo.toPlutusV1Args

instance Crypto c => EraPlutusTxInfo 'PlutusV2 (BabbageEra c) where
toPlutusTxCert _ = pure . Alonzo.transTxCert
toPlutusTxCert _ _ = pure . Alonzo.transTxCert

toPlutusScriptPurpose proxy = Alonzo.transPlutusPurpose proxy . hoistPlutusPurpose toAsItem
toPlutusScriptPurpose proxy pv = Alonzo.transPlutusPurpose proxy pv . hoistPlutusPurpose toAsItem

toPlutusTxInfo proxy LedgerTxInfo {ltiProtVer, ltiEpochInfo, ltiSystemStart, ltiUTxO, ltiTx} = do
timeRange <-
Expand All @@ -338,8 +346,8 @@ instance Crypto c => EraPlutusTxInfo 'PlutusV2 (BabbageEra c) where
(transTxOutV2 . TxOutFromOutput)
[minBound ..]
(F.toList (txBody ^. outputsTxBodyL))
txCerts <- Alonzo.transTxBodyCerts proxy txBody
plutusRedeemers <- transTxRedeemers proxy ltiTx
txCerts <- Alonzo.transTxBodyCerts proxy ltiProtVer txBody
plutusRedeemers <- transTxRedeemers proxy ltiProtVer ltiTx
pure
PV2.TxInfo
{ PV2.txInfoInputs = inputs
Expand All @@ -363,11 +371,12 @@ instance Crypto c => EraPlutusTxInfo 'PlutusV2 (BabbageEra c) where
toPlutusV2Args ::
EraPlutusTxInfo 'PlutusV2 era =>
proxy 'PlutusV2 ->
ProtVer ->
PV2.TxInfo ->
PlutusPurpose AsIxItem era ->
Maybe (Data era) ->
Data era ->
Either (ContextError era) (PlutusArgs 'PlutusV2)
toPlutusV2Args proxy txInfo scriptPurpose maybeSpendingData redeemerData =
toPlutusV2Args proxy pv txInfo scriptPurpose maybeSpendingData redeemerData =
PlutusV2Args
<$> toLegacyPlutusArgs proxy (PV2.ScriptContext txInfo) scriptPurpose maybeSpendingData redeemerData
<$> toLegacyPlutusArgs proxy pv (PV2.ScriptContext txInfo) scriptPurpose maybeSpendingData redeemerData
4 changes: 2 additions & 2 deletions eras/babbage/test-suite/cardano-ledger-babbage-test.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.0
name: cardano-ledger-babbage-test
version: 1.2.0.4
version: 1.2.0.5
license: Apache-2.0
maintainer: [email protected]
author: IOHK
Expand Down Expand Up @@ -35,7 +35,7 @@ library
cardano-ledger-binary:{cardano-ledger-binary, testlib} >=1.0,
cardano-ledger-alonzo:{cardano-ledger-alonzo, testlib} >=1.9,
cardano-ledger-alonzo-test >=1.1,
cardano-ledger-babbage:{cardano-ledger-babbage, testlib} >=1.9 && <1.10,
cardano-ledger-babbage:{cardano-ledger-babbage, testlib} >=1.9 && <1.11,
cardano-ledger-core:{cardano-ledger-core, testlib} >=1.11,
cardano-ledger-shelley-ma-test >=1.1,
cardano-ledger-mary >=1.4,
Expand Down
5 changes: 5 additions & 0 deletions eras/conway/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## 1.17.0.0

* Add `ProtVer` argument to `TxInfo` functions:
* `transTxCert`
* `transScriptPurpose`
* `transPlutusPurposeV1V2`
* `toPlutusV3Args`
* Changed `ConwayWdrlNotDelegatedToDRep` to wrap `NonEmpty KeyHash`
* Removed `DRepAlreadyRegisteredForStakeKeyDELEG`
* Add `showGovActionType`, `acceptedByEveryone`
Expand Down
6 changes: 4 additions & 2 deletions eras/conway/impl/cardano-ledger-conway.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ library
cardano-data >=1.2.3,
cardano-ledger-binary ^>=1.4,
cardano-ledger-allegra ^>=1.6,
cardano-ledger-alonzo ^>=1.10,
cardano-ledger-babbage ^>=1.9,
cardano-ledger-alonzo ^>=1.11,
cardano-ledger-babbage ^>=1.10,
cardano-ledger-core ^>=1.15,
cardano-ledger-mary ^>=1.7,
cardano-ledger-shelley ^>=1.14,
Expand Down Expand Up @@ -195,6 +195,7 @@ test-suite tests
Test.Cardano.Ledger.Conway.GenesisSpec
Test.Cardano.Ledger.Conway.GovActionReorderSpec
Test.Cardano.Ledger.Conway.Plutus.PlutusSpec
Test.Cardano.Ledger.Conway.TxInfoSpec
Paths_cardano_ledger_conway

default-language: Haskell2010
Expand All @@ -221,4 +222,5 @@ test-suite tests
containers,
data-default-class,
microlens,
plutus-ledger-api,
testlib
4 changes: 2 additions & 2 deletions eras/conway/impl/src/Cardano/Ledger/Conway/TxCert.hs
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ instance Crypto c => EraTxCert (ConwayEra c) where
instance Crypto c => ShelleyEraTxCert (ConwayEra c) where
mkRegTxCert c = ConwayTxCertDeleg $ ConwayRegCert c SNothing

getRegTxCert (ConwayTxCertDeleg (ConwayRegCert c _)) = Just c
getRegTxCert (ConwayTxCertDeleg (ConwayRegCert c SNothing)) = Just c
getRegTxCert _ = Nothing

mkUnRegTxCert c = ConwayTxCertDeleg $ ConwayUnRegCert c SNothing

getUnRegTxCert (ConwayTxCertDeleg (ConwayUnRegCert c _)) = Just c
getUnRegTxCert (ConwayTxCertDeleg (ConwayUnRegCert c SNothing)) = Just c
getUnRegTxCert _ = Nothing

mkDelegStakeTxCert c kh = ConwayTxCertDeleg $ ConwayDelegCert c (DelegStake kh)
Expand Down
Loading

0 comments on commit 492d54c

Please sign in to comment.