Skip to content

Commit

Permalink
Implement EqRaw type class and require it for all memoized types
Browse files Browse the repository at this point in the history
* Provide `EqRaw` instances for `TxCert`, `TxAuxData`, `TxWits`,
  `Script, `TxBody` and `Tx` for each era that defines one
* Require and provide `ToExpr` instances for `TxCert`, `TxAuxData`,
  `Script` and `TxBody`
* Simplify `specTxAuxDataUpgrade` test
  • Loading branch information
lehins committed Aug 23, 2023
1 parent 38a312a commit aeca1ff
Show file tree
Hide file tree
Showing 50 changed files with 295 additions and 82 deletions.
5 changes: 3 additions & 2 deletions eras/allegra/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Version history for `cardano-ledger-allegra`

## 1.2.1.2
## 1.2.2.0

*
* Add `EqRaw` instance for `Timelock`, `AllegraTxAuxData` and `AllegraTxBody`
* Add `ToExpr` instance for `AllegraTxAuxData`

## 1.2.1.1

Expand Down
6 changes: 3 additions & 3 deletions eras/allegra/impl/cardano-ledger-allegra.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.0
name: cardano-ledger-allegra
version: 1.2.1.2
version: 1.2.2.0
license: Apache-2.0
maintainer: [email protected]
author: IOHK
Expand Down Expand Up @@ -52,8 +52,8 @@ library
bytestring,
cardano-crypto-class,
cardano-ledger-binary >=1.0,
cardano-ledger-core >=1.5 && <1.7,
cardano-ledger-shelley >=1.5 && <1.6,
cardano-ledger-core >=1.6 && <1.7,
cardano-ledger-shelley >=1.5.1 && <1.6,
cardano-strict-containers,
cardano-slotting,
cborg,
Expand Down
4 changes: 4 additions & 0 deletions eras/allegra/impl/src/Cardano/Ledger/Allegra/Scripts.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import Cardano.Ledger.Core
import Cardano.Ledger.Crypto (Crypto, HASH)
import Cardano.Ledger.Keys (KeyHash (..), KeyRole (Witness))
import Cardano.Ledger.MemoBytes (
EqRaw (..),
Mem,
MemoBytes (Memo),
Memoized (..),
Expand Down Expand Up @@ -211,6 +212,9 @@ instance Crypto c => EraScript (AllegraEra c) where
phaseScript PhaseOneRep timelock = Just (Phase1Script timelock)
phaseScript PhaseTwoRep _ = Nothing

instance EqRaw (Timelock era) where
eqRaw = eqTimelockRaw

deriving via
Mem TimelockRaw era
instance
Expand Down
5 changes: 5 additions & 0 deletions eras/allegra/impl/src/Cardano/Ledger/Allegra/TxAuxData.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import Cardano.Ledger.Core (
import Cardano.Ledger.Crypto (Crypto (HASH))
import Cardano.Ledger.Hashes (EraIndependentTxAuxData)
import Cardano.Ledger.MemoBytes (
EqRaw,
Mem,
MemoBytes,
MemoHashIndex,
Expand Down Expand Up @@ -107,6 +108,8 @@ newtype AllegraTxAuxData era = AuxiliaryDataWithBytes (MemoBytes AllegraTxAuxDat
deriving (Generic)
deriving newtype (Eq, ToCBOR, SafeToHash)

instance ToExpr (AllegraTxAuxData era)

instance Memoized AllegraTxAuxData where
type RawType AllegraTxAuxData = AllegraTxAuxDataRaw

Expand All @@ -123,6 +126,8 @@ deriving newtype instance Era era => NoThunks (AllegraTxAuxData era)

deriving newtype instance NFData (AllegraTxAuxData era)

instance EqRaw (AllegraTxAuxData era)

pattern AllegraTxAuxData ::
Era era =>
Map Word64 Metadatum ->
Expand Down
5 changes: 5 additions & 0 deletions eras/allegra/impl/src/Cardano/Ledger/Allegra/TxBody.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import Cardano.Ledger.Compactible (Compactible (..))
import Cardano.Ledger.Core
import Cardano.Ledger.Crypto (Crypto, StandardCrypto)
import Cardano.Ledger.MemoBytes (
EqRaw,
Mem,
MemoBytes,
MemoHashIndex,
Expand Down Expand Up @@ -354,3 +355,7 @@ instance Crypto c => AllegraEraTxBody (AllegraEra c) where
lensMemoRawType atbrValidityInterval $
\txBodyRaw vldt -> txBodyRaw {atbrValidityInterval = vldt}
{-# INLINEABLE vldtTxBodyL #-}

instance
(Era era, Eq (PParamsUpdate era), Eq (TxOut era), Eq (TxCert era)) =>
EqRaw (AllegraTxBody era)
1 change: 0 additions & 1 deletion eras/allegra/impl/src/Cardano/Ledger/Allegra/TxWits.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
module Test.Cardano.Ledger.Allegra.BinarySpec (spec) where

import Cardano.Ledger.Allegra
import Cardano.Ledger.Allegra.TxAuxData
import Test.Cardano.Ledger.Allegra.Arbitrary ()
import Test.Cardano.Ledger.Common
import Test.Cardano.Ledger.Core.Binary as UpgradeSpec

spec :: Spec
spec = specUpgrade @Allegra @AllegraTxAuxData True
spec = specUpgrade @Allegra True
7 changes: 5 additions & 2 deletions eras/alonzo/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Version history for `cardano-ledger-alonzo`

## 1.4.0.1
## 1.4.1.0

*
* Made `isPlutusScript` more general.
* Add `alonzoEqTxRaw` and `alonzoEqTxWitsRaw`
* Add `EqRaw` instance for `AlonzoScript`, `AlonzoTxWits`, `AlonzoTxAuxData`,
`AlonzoTxBody` and `AlonzoTx`

## 1.4.0.0

Expand Down
4 changes: 2 additions & 2 deletions 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.4.0.1
version: 1.4.1.0
license: Apache-2.0
maintainer: [email protected]
author: IOHK
Expand Down Expand Up @@ -67,7 +67,7 @@ library
cardano-ledger-allegra >=1.1,
cardano-crypto-class,
cardano-ledger-binary >=1.0.1,
cardano-ledger-core >=1.5 && <1.7,
cardano-ledger-core >=1.6 && <1.7,
cardano-ledger-mary >=1.1,
cardano-ledger-shelley ^>=1.5,
cardano-slotting,
Expand Down
12 changes: 9 additions & 3 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Scripts.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ import Cardano.Ledger.Language (
mkLanguageEnum,
nonNativeLanguages,
)
import Cardano.Ledger.MemoBytes (EqRaw (..))
import Cardano.Ledger.SafeHash (SafeToHash (..))
import Cardano.Ledger.Shelley.Scripts (nativeMultiSigTag)
import Cardano.Ledger.TreeDiff (Expr (App), ToExpr (..), defaultExprViaShow)
Expand Down Expand Up @@ -202,9 +203,11 @@ type instance SomeScript 'PhaseOne (AlonzoEra c) = Timelock (AlonzoEra c)

type instance SomeScript 'PhaseTwo (AlonzoEra c) = (Language, ShortByteString)

isPlutusScript :: AlonzoScript era -> Bool
isPlutusScript (PlutusScript _) = True
isPlutusScript (TimelockScript _) = False
isPlutusScript :: forall era. EraScript era => Script era -> Bool
isPlutusScript x =
case phaseScript @era PhaseTwoRep x of
Just _ -> True
Nothing -> False

instance Crypto c => EraScript (AlonzoEra c) where
type Script (AlonzoEra c) = AlonzoScript (AlonzoEra c)
Expand All @@ -222,6 +225,9 @@ instance Crypto c => EraScript (AlonzoEra c) where
PlutusScript (Plutus PlutusV2 _) -> "\x02"
PlutusScript (Plutus PlutusV3 _) -> "\x03"

instance EqRaw (AlonzoScript era) where
eqRaw = eqAlonzoScriptRaw

instance Era era => ToJSON (AlonzoScript era) where
toJSON = String . serializeAsHexText

Expand Down
10 changes: 10 additions & 0 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ module Cardano.Ledger.Alonzo.Tx (
-- Other
toCBORForSizeComputation,
toCBORForMempoolSubmission,
alonzoEqTxRaw,
)
where

Expand Down Expand Up @@ -130,7 +131,9 @@ import Cardano.Ledger.Core
import Cardano.Ledger.Crypto (Crypto (HASH), StandardCrypto)
import Cardano.Ledger.Language (nonNativeLanguages)
import Cardano.Ledger.Mary.Value (AssetName, MaryValue (..), MultiAsset (..), PolicyID (..))
import Cardano.Ledger.MemoBytes (EqRaw (..))
import Cardano.Ledger.SafeHash (HashAnnotated, SafeToHash (..), hashAnnotated)
import Cardano.Ledger.Shelley.Tx (shelleyEqTxRaw)
import Cardano.Ledger.Shelley.TxBody (Withdrawals (..), unWithdrawals)
import Cardano.Ledger.TxIn (TxIn (..))
import qualified Cardano.Ledger.UTxO as Shelley
Expand Down Expand Up @@ -194,6 +197,9 @@ instance Crypto c => EraTx (AlonzoEra c) where
getMinFeeTx = alonzoMinFeeTx
{-# INLINE getMinFeeTx #-}

instance (Tx era ~ AlonzoTx era, AlonzoEraTx era) => EqRaw (AlonzoTx era) where
eqRaw = alonzoEqTxRaw

class (EraTx era, AlonzoEraTxBody era, AlonzoEraTxWits era) => AlonzoEraTx era where
isValidTxL :: Lens' (Tx era) IsValid

Expand Down Expand Up @@ -599,3 +605,7 @@ isTwoPhaseScriptAddressFromMap hashScriptMap addr =
Just hash -> any ok hashScriptMap
where
ok script = hashScript @era script == hash && not (isNativeScript @era script)

alonzoEqTxRaw :: AlonzoEraTx era => Tx era -> Tx era -> Bool
alonzoEqTxRaw tx1 tx2 =
shelleyEqTxRaw tx1 tx2 && (tx1 ^. isValidTxL == tx2 ^. isValidTxL)
3 changes: 3 additions & 0 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxAuxData.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import Cardano.Ledger.Core
import Cardano.Ledger.Crypto (Crypto (HASH))
import Cardano.Ledger.Language (Language (..), Plutus (..), guardPlutus)
import Cardano.Ledger.MemoBytes (
EqRaw,
Mem,
MemoBytes (..),
MemoHashIndex,
Expand Down Expand Up @@ -237,6 +238,8 @@ instance Memoized AlonzoTxAuxData where

instance ToExpr (AlonzoTxAuxData era)

instance EqRaw (AlonzoTxAuxData era)

type AuxiliaryData era = AlonzoTxAuxData era

{-# DEPRECATED AuxiliaryData "Use `AlonzoTxAuxData` instead" #-}
Expand Down
5 changes: 5 additions & 0 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxBody.hs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ import Cardano.Ledger.Crypto
import Cardano.Ledger.Keys (KeyHash (..), KeyRole (..))
import Cardano.Ledger.Mary.Value (MaryValue (MaryValue), MultiAsset (..), policies, policyID)
import Cardano.Ledger.MemoBytes (
EqRaw,
Mem,
MemoBytes,
MemoHashIndex,
Expand Down Expand Up @@ -404,6 +405,10 @@ scriptIntegrityHash' = atbrScriptIntegrityHash . getMemoRawType

txnetworkid' = atbrTxNetworkId . getMemoRawType

instance
(Era era, Eq (PParamsUpdate era), Eq (TxOut era), Eq (TxCert era)) =>
EqRaw (AlonzoTxBody era)

--------------------------------------------------------------------------------
-- Serialisation
--------------------------------------------------------------------------------
Expand Down
14 changes: 13 additions & 1 deletion eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxWits.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module Cardano.Ledger.Alonzo.TxWits (
hashDataTxWitsL,
unTxDats,
nullDats,
alonzoEqTxWitsRaw,
)
where

Expand Down Expand Up @@ -75,16 +76,18 @@ import Cardano.Ledger.Keys (KeyRole (Witness))
import Cardano.Ledger.Keys.Bootstrap (BootstrapWitness)
import Cardano.Ledger.Language (Language (..), Plutus (..), guardPlutus)
import Cardano.Ledger.MemoBytes (
EqRaw (..),
Mem,
MemoBytes,
Memoized (..),
eqRawType,
getMemoRawType,
lensMemoRawType,
mkMemoized,
)
import Cardano.Ledger.SafeHash (SafeToHash (..))
import Cardano.Ledger.Shelley.TxBody (WitVKey)
import Cardano.Ledger.Shelley.TxWits (keyBy)
import Cardano.Ledger.Shelley.TxWits (keyBy, shelleyEqTxWitsRaw)
import Control.DeepSeq (NFData)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
Expand Down Expand Up @@ -434,6 +437,9 @@ instance (EraScript (AlonzoEra c), Crypto c) => AlonzoEraTxWits (AlonzoEra c) wh
rdmrsTxWitsL = rdmrsAlonzoTxWitsL
{-# INLINE rdmrsTxWitsL #-}

instance (TxWits era ~ AlonzoTxWits era, AlonzoEraTxWits era) => EqRaw (AlonzoTxWits era) where
eqRaw = alonzoEqTxWitsRaw

-- | This is a convenience Lens that will hash the `Data` when it is being added to the
-- `TxWits`. See `datsTxWitsL` for a version that aloows setting `TxDats` instead.
hashDataTxWitsL :: AlonzoEraTxWits era => Lens (TxWits era) (TxWits era) (TxDats era) [Data era]
Expand Down Expand Up @@ -603,3 +609,9 @@ deriving via
instance
(EraScript era, Script era ~ AlonzoScript era) =>
DecCBOR (Annotator (AlonzoTxWits era))

alonzoEqTxWitsRaw :: AlonzoEraTxWits era => TxWits era -> TxWits era -> Bool
alonzoEqTxWitsRaw txWits1 txWits2 =
shelleyEqTxWitsRaw txWits1 txWits2
&& eqRawType (txWits1 ^. datsTxWitsL) (txWits2 ^. datsTxWitsL)
&& eqRawType (txWits1 ^. rdmrsTxWitsL) (txWits2 ^. rdmrsTxWitsL)
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ spec :: Spec
spec =
-- Scripts are not upgradeable from Mary through their CBOR instances, since Mary had no
-- concept of a prefix.
specUpgrade @Alonzo @AlonzoTxAuxData False
specUpgrade @Alonzo False
4 changes: 2 additions & 2 deletions eras/babbage/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Version history for `cardano-ledger-babbage`

## 1.4.3.1
## 1.4.4.0

*
* Add `EqRaw` instance for `BabbageTxBody`

## 1.4.3.0

Expand Down
6 changes: 3 additions & 3 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.4.3.1
version: 1.4.4.0
license: Apache-2.0
maintainer: [email protected]
author: IOHK
Expand Down Expand Up @@ -60,9 +60,9 @@ library
cardano-crypto-class,
cardano-data >=1.0,
cardano-ledger-allegra >=1.1,
cardano-ledger-alonzo ^>=1.4,
cardano-ledger-alonzo ^>=1.4.1,
cardano-ledger-binary >=1.0,
cardano-ledger-core >=1.5 && <1.7,
cardano-ledger-core >=1.6 && <1.7,
cardano-ledger-mary >=1.1,
cardano-ledger-shelley ^>=1.5,
cardano-slotting,
Expand Down
12 changes: 5 additions & 7 deletions eras/babbage/impl/src/Cardano/Ledger/Babbage/Scripts.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ where

import Cardano.Ledger.Allegra.Scripts (Timelock)
import Cardano.Ledger.Alonzo.Language
import Cardano.Ledger.Alonzo.Scripts (AlonzoScript (..), translateAlonzoScript)
import Cardano.Ledger.Alonzo.Scripts (
AlonzoScript (..),
isPlutusScript,
translateAlonzoScript,
)
import Cardano.Ledger.Babbage.Era
import Cardano.Ledger.Core
import Cardano.Ledger.Crypto
Expand Down Expand Up @@ -51,9 +55,3 @@ instance Crypto c => EraScript (BabbageEra c) where
phaseScript PhaseOneRep (TimelockScript s) = Just (Phase1Script s)
phaseScript PhaseTwoRep (PlutusScript plutus) = Just (Phase2Script plutus)
phaseScript _ _ = Nothing

isPlutusScript :: forall era. EraScript era => Script era -> Bool
isPlutusScript x =
case phaseScript @era PhaseTwoRep x of
Just _ -> True
Nothing -> False
2 changes: 1 addition & 1 deletion eras/babbage/impl/src/Cardano/Ledger/Babbage/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ instance Crypto c => EraTx (BabbageEra c) where
sizeTxF = sizeAlonzoTxF
{-# INLINE sizeTxF #-}

validateScript (Phase1Script script) tx = validateTimelock @(BabbageEra c) script tx
validateScript (Phase1Script script) = validateTimelock @(BabbageEra c) script
{-# INLINE validateScript #-}

getMinFeeTx = alonzoMinFeeTx
Expand Down
5 changes: 5 additions & 0 deletions eras/babbage/impl/src/Cardano/Ledger/Babbage/TxBody.hs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ import Cardano.Ledger.Crypto
import Cardano.Ledger.Keys (KeyHash (..), KeyRole (..))
import Cardano.Ledger.Mary.Value (MaryValue (MaryValue), MultiAsset, policies, policyID)
import Cardano.Ledger.MemoBytes (
EqRaw,
Mem,
MemoBytes,
MemoHashIndex,
Expand Down Expand Up @@ -471,6 +472,10 @@ instance Crypto c => BabbageEraTxBody (BabbageEra c) where
allSizedOutputsTxBodyF = allSizedOutputsBabbageTxBodyF
{-# INLINE allSizedOutputsTxBodyF #-}

instance
(Era era, Eq (PParamsUpdate era), Eq (TxOut era), Eq (TxCert era)) =>
EqRaw (BabbageTxBody era)

deriving newtype instance
(Era era, Eq (TxOut era), Eq (TxCert era), Eq (PParamsUpdate era)) =>
Eq (BabbageTxBody era)
Expand Down
Loading

0 comments on commit aeca1ff

Please sign in to comment.