Skip to content

Commit 86737b1

Browse files
committed
Replace Timelock with NativeScript in AllegraTxWitsRaw
1 parent 25bfd99 commit 86737b1

File tree

13 files changed

+76
-49
lines changed

13 files changed

+76
-49
lines changed

eras/allegra/impl/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 1.8.0.0
44

5+
* Add `upgradeNativeScript` method to `AllegraEraScript`
56
* Remove `TriesToForgeADA`
67
* Change the type of `actualSize` and `PParameterMaxValue` fields in `OutputTooBigUTxO` to `Int`
78
* Added `COMPLETE` pragma for `TxCert AllegraEra`

eras/allegra/impl/src/Cardano/Ledger/Allegra/Scripts.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ showTimelock (RequireMOf m xs) = "(MOf " ++ show m ++ " " ++ F.foldl' accum ")"
462462
showTimelock (RequireSignature hash) = "(Signature " ++ show hash ++ ")"
463463
showTimelock _ = error "Impossible: All NativeScripts should have been accounted for"
464464

465+
-- | Check the equality of two underlying types, while ignoring their binary
466+
-- representation, which `Eq` instance normally does. This is used for testing.
465467
eqTimelockRaw :: Timelock era -> Timelock era -> Bool
466468
eqTimelockRaw t1 t2 = go (getMemoRawType t1) (getMemoRawType t2)
467469
where

eras/allegra/impl/src/Cardano/Ledger/Allegra/TxAuxData.hs

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ module Cardano.Ledger.Allegra.TxAuxData (
2323
AllegraTxAuxDataRaw (..),
2424
metadataAllegraTxAuxDataL,
2525
AllegraEraTxAuxData (..),
26-
timelockScriptsAllegraTxAuxDataL,
26+
nativeScriptsAllegraTxAuxDataL,
2727
) where
2828

2929
import Cardano.Ledger.Allegra.Era (AllegraEra)
30-
import Cardano.Ledger.Allegra.Scripts (Timelock)
30+
import Cardano.Ledger.Allegra.Scripts (AllegraEraScript)
3131
import Cardano.Ledger.Binary (
3232
Annotator,
3333
DecCBOR (..),
@@ -45,7 +45,7 @@ import Cardano.Ledger.MemoBytes (
4545
getMemoRawType,
4646
getMemoSafeHash,
4747
lensMemoRawType,
48-
mkMemoizedEra,
48+
mkMemoized,
4949
)
5050
import Cardano.Ledger.Shelley.Core
5151
import Cardano.Ledger.Shelley.TxAuxData (Metadatum, validMetadatum)
@@ -63,6 +63,7 @@ import Control.DeepSeq (NFData, deepseq)
6363
import Data.Map.Strict (Map)
6464
import Data.Sequence.Strict (StrictSeq)
6565
import qualified Data.Sequence.Strict as StrictSeq
66+
import Data.Typeable (Typeable)
6667
import Data.Word (Word64)
6768
import GHC.Generics (Generic)
6869
import Lens.Micro (Lens')
@@ -74,16 +75,18 @@ import NoThunks.Class (NoThunks)
7475
data AllegraTxAuxDataRaw era = AllegraTxAuxDataRaw
7576
{ atadrMetadata :: !(Map Word64 Metadatum)
7677
-- ^ Structured transaction metadata
77-
, atadrNativeScripts :: !(StrictSeq (Timelock era))
78+
, atadrNativeScripts :: !(StrictSeq (NativeScript era))
7879
-- ^ Pre-images of script hashes found within the TxBody, but which are not
7980
-- required as witnesses. Examples include:
8081
-- - Token policy IDs appearing in transaction outputs
8182
-- - Pool reward account registrations
8283
}
83-
deriving (Generic, Eq)
84+
deriving (Generic)
85+
86+
deriving instance Eq (NativeScript era) => Eq (AllegraTxAuxDataRaw era)
8487

8588
class EraTxAuxData era => AllegraEraTxAuxData era where
86-
timelockScriptsTxAuxDataL :: Lens' (TxAuxData era) (StrictSeq (Timelock era))
89+
nativeScriptsTxAuxDataL :: Lens' (TxAuxData era) (StrictSeq (NativeScript era))
8790

8891
instance EraTxAuxData AllegraEra where
8992
type TxAuxData AllegraEra = AllegraTxAuxData AllegraEra
@@ -95,77 +98,88 @@ instance EraTxAuxData AllegraEra where
9598
validateTxAuxData _ (AllegraTxAuxData md as) = as `deepseq` all validMetadatum md
9699

97100
metadataAllegraTxAuxDataL ::
98-
forall era. Era era => Lens' (AllegraTxAuxData era) (Map Word64 Metadatum)
101+
forall era.
102+
( Era era
103+
, EncCBOR (NativeScript era)
104+
) =>
105+
Lens' (AllegraTxAuxData era) (Map Word64 Metadatum)
99106
metadataAllegraTxAuxDataL =
100107
lensMemoRawType @era atadrMetadata $
101108
\txAuxDataRaw md -> txAuxDataRaw {atadrMetadata = md}
102109

103110
instance AllegraEraTxAuxData AllegraEra where
104-
timelockScriptsTxAuxDataL = timelockScriptsAllegraTxAuxDataL
111+
nativeScriptsTxAuxDataL = nativeScriptsAllegraTxAuxDataL
105112

106-
timelockScriptsAllegraTxAuxDataL ::
113+
nativeScriptsAllegraTxAuxDataL ::
107114
forall era.
108-
Era era =>
109-
Lens' (AllegraTxAuxData era) (StrictSeq (Timelock era))
110-
timelockScriptsAllegraTxAuxDataL =
115+
(Era era, EncCBOR (NativeScript era)) =>
116+
Lens' (AllegraTxAuxData era) (StrictSeq (NativeScript era))
117+
nativeScriptsAllegraTxAuxDataL =
111118
lensMemoRawType @era atadrNativeScripts $
112119
\txAuxDataRaw ts -> txAuxDataRaw {atadrNativeScripts = ts}
113120

114-
deriving instance Show (AllegraTxAuxDataRaw era)
121+
deriving instance Show (NativeScript era) => Show (AllegraTxAuxDataRaw era)
115122

116-
deriving instance Era era => NoThunks (AllegraTxAuxDataRaw era)
123+
deriving instance (Era era, NoThunks (NativeScript era)) => NoThunks (AllegraTxAuxDataRaw era)
117124

118-
instance NFData (AllegraTxAuxDataRaw era)
125+
instance NFData (NativeScript era) => NFData (AllegraTxAuxDataRaw era)
119126

120127
newtype AllegraTxAuxData era = MkAlegraTxAuxData (MemoBytes (AllegraTxAuxDataRaw era))
121128
deriving (Generic)
122-
deriving newtype (Eq, ToCBOR, SafeToHash)
129+
deriving newtype (ToCBOR, SafeToHash)
130+
131+
deriving instance Eq (NativeScript era) => Eq (AllegraTxAuxData era)
123132

124133
instance Memoized (AllegraTxAuxData era) where
125134
type RawType (AllegraTxAuxData era) = AllegraTxAuxDataRaw era
126135

127136
deriving via
128137
(Mem (AllegraTxAuxDataRaw era))
129138
instance
130-
Era era => DecCBOR (Annotator (AllegraTxAuxData era))
139+
AllegraEraScript era => DecCBOR (Annotator (AllegraTxAuxData era))
131140

132141
type instance MemoHashIndex (AllegraTxAuxDataRaw era) = EraIndependentTxAuxData
133142

134143
instance HashAnnotated (AllegraTxAuxData era) EraIndependentTxAuxData where
135144
hashAnnotated = getMemoSafeHash
136145

137-
deriving newtype instance Show (AllegraTxAuxData era)
146+
deriving newtype instance Show (NativeScript era) => Show (AllegraTxAuxData era)
138147

139-
deriving newtype instance Era era => NoThunks (AllegraTxAuxData era)
148+
deriving newtype instance (Era era, NoThunks (NativeScript era)) => NoThunks (AllegraTxAuxData era)
140149

141-
deriving newtype instance NFData (AllegraTxAuxData era)
150+
deriving newtype instance NFData (NativeScript era) => NFData (AllegraTxAuxData era)
142151

143-
instance EqRaw (AllegraTxAuxData era)
152+
instance Eq (NativeScript era) => EqRaw (AllegraTxAuxData era)
144153

145154
pattern AllegraTxAuxData ::
146155
forall era.
147-
Era era =>
156+
( Era era
157+
, EncCBOR (NativeScript era)
158+
) =>
148159
Map Word64 Metadatum ->
149-
StrictSeq (Timelock era) ->
160+
StrictSeq (NativeScript era) ->
150161
AllegraTxAuxData era
151162
pattern AllegraTxAuxData blob sp <- (getMemoRawType -> AllegraTxAuxDataRaw blob sp)
152163
where
153-
AllegraTxAuxData blob sp = mkMemoizedEra @era $ AllegraTxAuxDataRaw blob sp
164+
AllegraTxAuxData blob sp = mkMemoized (eraProtVerLow @era) $ AllegraTxAuxDataRaw blob sp
154165

155166
{-# COMPLETE AllegraTxAuxData #-}
156167

157168
--------------------------------------------------------------------------------
158169
-- Serialisation
159170
--------------------------------------------------------------------------------
160171

161-
instance Era era => EncCBOR (AllegraTxAuxDataRaw era) where
172+
instance (Era era, EncCBOR (NativeScript era)) => EncCBOR (AllegraTxAuxDataRaw era) where
162173
encCBOR (AllegraTxAuxDataRaw blob sp) =
163174
encode (Rec AllegraTxAuxDataRaw !> To blob !> To sp)
164175

165176
-- | Encodes memoized bytes created upon construction.
166177
instance Era era => EncCBOR (AllegraTxAuxData era)
167178

168-
instance Era era => DecCBOR (Annotator (AllegraTxAuxDataRaw era)) where
179+
instance
180+
(Era era, Typeable (NativeScript era), DecCBOR (Annotator (NativeScript era))) =>
181+
DecCBOR (Annotator (AllegraTxAuxDataRaw era))
182+
where
169183
decCBOR =
170184
peekTokenType >>= \case
171185
TypeMapLen -> decodeFromMap

eras/allegra/impl/testlib/Test/Cardano/Ledger/Allegra/Binary/Annotator.hs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ import Test.Cardano.Ledger.Shelley.Binary.Annotator
2929

3030
deriving newtype instance DecCBOR (TxBody AllegraEra)
3131

32-
instance Era era => DecCBOR (AllegraTxAuxDataRaw era) where
32+
instance
33+
( Era era
34+
, AllegraEraScript era
35+
, DecCBOR (NativeScript era)
36+
) =>
37+
DecCBOR (AllegraTxAuxDataRaw era)
38+
where
3339
decCBOR =
3440
peekTokenType >>= \case
3541
TypeMapLen -> decodeFromMap
@@ -50,10 +56,11 @@ instance Era era => DecCBOR (AllegraTxAuxDataRaw era) where
5056
decode
5157
( RecD AllegraTxAuxDataRaw
5258
<! From
53-
<! From
59+
<! From -- TODO is this supposed to decode an annotator instead?
5460
)
5561

56-
deriving newtype instance Era era => DecCBOR (AllegraTxAuxData era)
62+
deriving newtype instance
63+
(AllegraEraScript era, DecCBOR (NativeScript era)) => DecCBOR (AllegraTxAuxData era)
5764

5865
instance Era era => DecCBOR (TimelockRaw era) where
5966
decCBOR = decode $ Summands "TimelockRaw" $ \case

eras/allegra/impl/testlib/Test/Cardano/Ledger/Allegra/TreeDiff.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ instance ToExpr (TimelockRaw era)
3030
instance ToExpr (Timelock era)
3131

3232
-- TxAuxData
33-
instance ToExpr (AllegraTxAuxDataRaw era)
33+
instance ToExpr (NativeScript era) => ToExpr (AllegraTxAuxDataRaw era)
3434

35-
instance ToExpr (AllegraTxAuxData era)
35+
instance ToExpr (NativeScript era) => ToExpr (AllegraTxAuxData era)
3636

3737
-- TxBody
3838
instance

eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxAuxData.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module Cardano.Ledger.Alonzo.TxAuxData (
3939
validateAlonzoTxAuxData,
4040
getAlonzoTxAuxDataScripts,
4141
metadataAlonzoTxAuxDataL,
42-
timelockScriptsAlonzoTxAuxDataL,
42+
nativeScriptsAlonzoTxAuxDataL,
4343
plutusScriptsAllegraTxAuxDataL,
4444
addPlutusScripts,
4545
decodeTxAuxDataByTokenType,
@@ -306,12 +306,12 @@ validateAlonzoTxAuxData pv auxData@AlonzoTxAuxData {atadMetadata = metadata} =
306306
&& all (validScript pv) (getAlonzoTxAuxDataScripts auxData)
307307

308308
instance AllegraEraTxAuxData AlonzoEra where
309-
timelockScriptsTxAuxDataL = timelockScriptsAlonzoTxAuxDataL
309+
nativeScriptsTxAuxDataL = nativeScriptsAlonzoTxAuxDataL
310310

311-
timelockScriptsAlonzoTxAuxDataL ::
311+
nativeScriptsAlonzoTxAuxDataL ::
312312
forall era.
313313
(Era era, EncCBOR (NativeScript era)) => Lens' (AlonzoTxAuxData era) (StrictSeq (NativeScript era))
314-
timelockScriptsAlonzoTxAuxDataL =
314+
nativeScriptsAlonzoTxAuxDataL =
315315
lensMemoRawType @era atadrNativeScripts $
316316
\txAuxDataRaw ts -> txAuxDataRaw {atadrNativeScripts = ts}
317317

eras/babbage/impl/src/Cardano/Ledger/Babbage/TxAuxData.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import Cardano.Ledger.Alonzo.Core
77
import Cardano.Ledger.Alonzo.TxAuxData (
88
AlonzoTxAuxData (..),
99
metadataAlonzoTxAuxDataL,
10+
nativeScriptsAlonzoTxAuxDataL,
1011
plutusScriptsAllegraTxAuxDataL,
11-
timelockScriptsAlonzoTxAuxDataL,
1212
validateAlonzoTxAuxData,
1313
)
1414
import Cardano.Ledger.Babbage.Era
@@ -23,7 +23,7 @@ instance EraTxAuxData BabbageEra where
2323
validateTxAuxData = validateAlonzoTxAuxData
2424

2525
instance AllegraEraTxAuxData BabbageEra where
26-
timelockScriptsTxAuxDataL = timelockScriptsAlonzoTxAuxDataL
26+
nativeScriptsTxAuxDataL = nativeScriptsAlonzoTxAuxDataL
2727

2828
instance AlonzoEraTxAuxData BabbageEra where
2929
plutusScriptsTxAuxDataL = plutusScriptsAllegraTxAuxDataL

eras/conway/impl/src/Cardano/Ledger/Conway/TxAuxData.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import Cardano.Ledger.Alonzo.Core
77
import Cardano.Ledger.Alonzo.TxAuxData (
88
AlonzoTxAuxData (..),
99
metadataAlonzoTxAuxDataL,
10+
nativeScriptsAlonzoTxAuxDataL,
1011
plutusScriptsAllegraTxAuxDataL,
11-
timelockScriptsAlonzoTxAuxDataL,
1212
validateAlonzoTxAuxData,
1313
)
1414
import Cardano.Ledger.Conway.Era
@@ -24,7 +24,7 @@ instance EraTxAuxData ConwayEra where
2424
validateTxAuxData = validateAlonzoTxAuxData
2525

2626
instance AllegraEraTxAuxData ConwayEra where
27-
timelockScriptsTxAuxDataL = timelockScriptsAlonzoTxAuxDataL
27+
nativeScriptsTxAuxDataL = nativeScriptsAlonzoTxAuxDataL
2828

2929
instance AlonzoEraTxAuxData ConwayEra where
3030
plutusScriptsTxAuxDataL = plutusScriptsAllegraTxAuxDataL

eras/dijkstra/src/Cardano/Ledger/Dijkstra/TxAuxData.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ module Cardano.Ledger.Dijkstra.TxAuxData () where
66
import Cardano.Ledger.Alonzo.TxAuxData (
77
AlonzoTxAuxData (..),
88
metadataAlonzoTxAuxDataL,
9+
nativeScriptsAlonzoTxAuxDataL,
910
plutusScriptsAllegraTxAuxDataL,
10-
timelockScriptsAlonzoTxAuxDataL,
1111
validateAlonzoTxAuxData,
1212
)
1313
import Cardano.Ledger.Conway.Core (
@@ -28,7 +28,7 @@ instance EraTxAuxData DijkstraEra where
2828
validateTxAuxData = validateAlonzoTxAuxData
2929

3030
instance AllegraEraTxAuxData DijkstraEra where
31-
timelockScriptsTxAuxDataL = timelockScriptsAlonzoTxAuxDataL
31+
nativeScriptsTxAuxDataL = nativeScriptsAlonzoTxAuxDataL
3232

3333
instance AlonzoEraTxAuxData DijkstraEra where
3434
plutusScriptsTxAuxDataL = plutusScriptsAllegraTxAuxDataL

eras/mary/impl/src/Cardano/Ledger/Mary/TxAuxData.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Cardano.Ledger.Allegra.TxAuxData (
1515
AllegraEraTxAuxData (..),
1616
AllegraTxAuxData (..),
1717
metadataAllegraTxAuxDataL,
18-
timelockScriptsAllegraTxAuxDataL,
18+
nativeScriptsAllegraTxAuxDataL,
1919
)
2020
import Cardano.Ledger.Core
2121
import Cardano.Ledger.Mary.Era (MaryEra)
@@ -35,4 +35,4 @@ instance EraTxAuxData MaryEra where
3535
validateTxAuxData _ (AllegraTxAuxData md as) = as `deepseq` all validMetadatum md
3636

3737
instance AllegraEraTxAuxData MaryEra where
38-
timelockScriptsTxAuxDataL = timelockScriptsAllegraTxAuxDataL
38+
nativeScriptsTxAuxDataL = nativeScriptsAllegraTxAuxDataL

0 commit comments

Comments
 (0)