1
1
{-# LANGUAGE DataKinds #-}
2
2
{-# LANGUAGE DeriveGeneric #-}
3
- {-# LANGUAGE DerivingStrategies #-}
3
+ {-# LANGUAGE DerivingVia #-}
4
+ {-# LANGUAGE FlexibleContexts #-}
4
5
{-# LANGUAGE FlexibleInstances #-}
5
6
{-# LANGUAGE LambdaCase #-}
7
+ {-# LANGUAGE MultiParamTypeClasses #-}
8
+ {-# LANGUAGE OverloadedStrings #-}
9
+ {-# LANGUAGE PatternSynonyms #-}
10
+ {-# LANGUAGE QuantifiedConstraints #-}
11
+ {-# LANGUAGE RankNTypes #-}
12
+ {-# LANGUAGE StandaloneDeriving #-}
6
13
{-# LANGUAGE TypeApplications #-}
7
14
{-# LANGUAGE TypeFamilies #-}
8
15
{-# LANGUAGE UndecidableInstances #-}
16
+ {-# LANGUAGE ViewPatterns #-}
9
17
{-# OPTIONS_GHC -Wno-orphans #-}
10
18
11
- module Cardano.Ledger.Dijkstra.Scripts (PlutusScript (.. )) where
19
+ module Cardano.Ledger.Dijkstra.Scripts (
20
+ PlutusScript (.. ),
21
+ DijkstraPlutusPurpose (.. ),
22
+ pattern GuardingPurpose ,
23
+ ) where
12
24
13
25
import Cardano.Ledger.Address (RewardAccount )
14
26
import Cardano.Ledger.Allegra.Scripts (
@@ -32,16 +44,34 @@ import Cardano.Ledger.Alonzo (AlonzoScript)
32
44
import Cardano.Ledger.Alonzo.Scripts (
33
45
AlonzoEraScript (.. ),
34
46
AlonzoScript (.. ),
47
+ AsItem ,
35
48
AsIx (.. ),
49
+ AsIxItem ,
36
50
alonzoScriptPrefixTag ,
37
51
)
52
+ import Cardano.Ledger.BaseTypes (Inject (.. ), kindObject )
53
+ import Cardano.Ledger.Binary (
54
+ CBORGroup (.. ),
55
+ DecCBOR (.. ),
56
+ DecCBORGroup (.. ),
57
+ EncCBOR (.. ),
58
+ EncCBORGroup (.. ),
59
+ decodeWord8 ,
60
+ encodeWord8 ,
61
+ )
38
62
import Cardano.Ledger.Conway.Governance (ProposalProcedure , Voter )
39
63
import Cardano.Ledger.Conway.Scripts (
40
64
ConwayEraScript (.. ),
41
65
ConwayPlutusPurpose (.. ),
42
66
PlutusScript (.. ),
43
67
)
44
- import Cardano.Ledger.Core (EraScript (.. ), EraTxCert (.. ), SafeToHash (.. ), ScriptHash )
68
+ import Cardano.Ledger.Core (
69
+ EraPParams ,
70
+ EraScript (.. ),
71
+ EraTxCert (.. ),
72
+ SafeToHash (.. ),
73
+ ScriptHash ,
74
+ )
45
75
import Cardano.Ledger.Dijkstra.Era (DijkstraEra )
46
76
import Cardano.Ledger.Dijkstra.PParams ()
47
77
import Cardano.Ledger.Dijkstra.TxCert ()
@@ -50,8 +80,10 @@ import Cardano.Ledger.Plutus (Language (..), Plutus, SLanguage (..), plutusSLang
50
80
import Cardano.Ledger.Shelley.Scripts (ShelleyEraScript (.. ))
51
81
import Cardano.Ledger.TxIn (TxIn )
52
82
import Control.DeepSeq (NFData (.. ), rwhnf )
83
+ import Data.Aeson (KeyValue (.. ), ToJSON (.. ))
53
84
import Data.MemPack (MemPack (.. ), packTagM , packedTagByteCount , unknownTagM , unpackTagM )
54
- import Data.Word (Word32 )
85
+ import Data.Typeable (Proxy (.. ), Typeable )
86
+ import Data.Word (Word16 , Word32 , Word8 )
55
87
import GHC.Generics (Generic )
56
88
import NoThunks.Class (NoThunks )
57
89
@@ -65,6 +97,124 @@ data DijkstraPlutusPurpose f era
65
97
| DijkstraGuarding ! (f Word32 ScriptHash )
66
98
deriving (Generic )
67
99
100
+ instance Inject (ConwayPlutusPurpose f era ) (DijkstraPlutusPurpose f era ) where
101
+ inject = \ case
102
+ ConwaySpending p -> DijkstraSpending p
103
+ ConwayMinting p -> DijkstraMinting p
104
+ ConwayCertifying p -> DijkstraCertifying p
105
+ ConwayRewarding p -> DijkstraRewarding p
106
+ ConwayVoting p -> DijkstraVoting p
107
+ ConwayProposing p -> DijkstraProposing p
108
+
109
+ deriving via
110
+ CBORGroup (DijkstraPlutusPurpose f era)
111
+ instance
112
+ ( Typeable f
113
+ , EraPParams era
114
+ , forall a b . (DecCBOR a , DecCBOR b ) => DecCBOR (f a b )
115
+ , forall a b . (EncCBOR a , EncCBOR b ) => EncCBOR (f a b )
116
+ , EraTxCert era
117
+ ) =>
118
+ DecCBOR (DijkstraPlutusPurpose f era )
119
+
120
+ deriving via
121
+ CBORGroup (DijkstraPlutusPurpose f era)
122
+ instance
123
+ ( Typeable f
124
+ , EraPParams era
125
+ , forall a b . (EncCBOR a , EncCBOR b ) => EncCBOR (f a b )
126
+ , EraTxCert era
127
+ ) =>
128
+ EncCBOR (DijkstraPlutusPurpose f era )
129
+
130
+ instance
131
+ ( Typeable f
132
+ , EraPParams era
133
+ , forall a b . (DecCBOR a , DecCBOR b ) => DecCBOR (f a b )
134
+ , DecCBOR (TxCert era )
135
+ ) =>
136
+ DecCBORGroup (DijkstraPlutusPurpose f era )
137
+ where
138
+ decCBORGroup =
139
+ decodeWord8 >>= \ case
140
+ 0 -> DijkstraSpending <$> decCBOR
141
+ 1 -> DijkstraMinting <$> decCBOR
142
+ 2 -> DijkstraCertifying <$> decCBOR
143
+ 3 -> DijkstraRewarding <$> decCBOR
144
+ 4 -> DijkstraVoting <$> decCBOR
145
+ 5 -> DijkstraProposing <$> decCBOR
146
+ 6 -> DijkstraGuarding <$> decCBOR
147
+ n -> fail $ " Unexpected tag for DijkstraPlutusPurpose: " <> show n
148
+
149
+ instance
150
+ ( Typeable f
151
+ , EraPParams era
152
+ , forall a b . (EncCBOR a , EncCBOR b ) => EncCBOR (f a b )
153
+ , EncCBOR (TxCert era )
154
+ ) =>
155
+ EncCBORGroup (DijkstraPlutusPurpose f era )
156
+ where
157
+ listLen _ = 2
158
+ listLenBound _ = 2
159
+ encCBORGroup = \ case
160
+ DijkstraSpending p -> encodeWord8 0 <> encCBOR p
161
+ DijkstraMinting p -> encodeWord8 1 <> encCBOR p
162
+ DijkstraCertifying p -> encodeWord8 2 <> encCBOR p
163
+ DijkstraRewarding p -> encodeWord8 3 <> encCBOR p
164
+ DijkstraVoting p -> encodeWord8 4 <> encCBOR p
165
+ DijkstraProposing p -> encodeWord8 5 <> encCBOR p
166
+ DijkstraGuarding p -> encodeWord8 6 <> encCBOR p
167
+ encodedGroupSizeExpr size_ _proxy =
168
+ encodedSizeExpr size_ (Proxy @ Word8 ) + encodedSizeExpr size_ (Proxy @ Word16 )
169
+
170
+ instance
171
+ ( forall a b . (ToJSON a , ToJSON b ) => ToJSON (f a b )
172
+ , ToJSON (TxCert era )
173
+ , EraPParams era
174
+ ) =>
175
+ ToJSON (DijkstraPlutusPurpose f era )
176
+ where
177
+ toJSON = \ case
178
+ DijkstraSpending n -> kindObjectWithValue " DijkstraSpending" n
179
+ DijkstraMinting n -> kindObjectWithValue " DijkstraMinting" n
180
+ DijkstraCertifying n -> kindObjectWithValue " DijkstraCertifying" n
181
+ DijkstraRewarding n -> kindObjectWithValue " DijkstraRewarding" n
182
+ DijkstraVoting n -> kindObjectWithValue " DijkstraVoting" n
183
+ DijkstraProposing n -> kindObjectWithValue " DijkstraProposing" n
184
+ DijkstraGuarding n -> kindObjectWithValue " DijkstraGuarding" n
185
+ where
186
+ kindObjectWithValue name n = kindObject name [" value" .= n]
187
+
188
+ deriving instance (EraTxCert era , EraPParams era ) => Eq (DijkstraPlutusPurpose AsItem era )
189
+
190
+ deriving instance (EraTxCert era , EraPParams era ) => Eq (DijkstraPlutusPurpose AsIx era )
191
+
192
+ deriving instance (EraTxCert era , EraPParams era ) => Eq (DijkstraPlutusPurpose AsIxItem era )
193
+
194
+ instance (EraPParams era , NFData (TxCert era )) => NFData (DijkstraPlutusPurpose AsItem era )
195
+
196
+ instance (EraPParams era , NFData (TxCert era )) => NFData (DijkstraPlutusPurpose AsIx era )
197
+
198
+ instance (EraPParams era , NFData (TxCert era )) => NFData (DijkstraPlutusPurpose AsIxItem era )
199
+
200
+ instance (EraPParams era , NoThunks (TxCert era )) => NoThunks (DijkstraPlutusPurpose AsItem era )
201
+
202
+ instance (EraPParams era , NoThunks (TxCert era )) => NoThunks (DijkstraPlutusPurpose AsIx era )
203
+
204
+ instance (EraPParams era , NoThunks (TxCert era )) => NoThunks (DijkstraPlutusPurpose AsIxItem era )
205
+
206
+ deriving instance (EraPParams era , EraTxCert era ) => Ord (DijkstraPlutusPurpose AsItem era )
207
+
208
+ deriving instance (EraPParams era , EraTxCert era ) => Ord (DijkstraPlutusPurpose AsIx era )
209
+
210
+ deriving instance (EraPParams era , EraTxCert era ) => Ord (DijkstraPlutusPurpose AsIxItem era )
211
+
212
+ deriving instance (EraPParams era , EraTxCert era ) => Show (DijkstraPlutusPurpose AsItem era )
213
+
214
+ deriving instance (EraPParams era , EraTxCert era ) => Show (DijkstraPlutusPurpose AsIx era )
215
+
216
+ deriving instance (EraPParams era , EraTxCert era ) => Show (DijkstraPlutusPurpose AsIxItem era )
217
+
68
218
instance EraScript DijkstraEra where
69
219
type Script DijkstraEra = AlonzoScript DijkstraEra
70
220
type NativeScript DijkstraEra = Timelock DijkstraEra
@@ -119,7 +269,7 @@ instance AlonzoEraScript DijkstraEra where
119
269
| DijkstraPlutusV4 ! (Plutus 'PlutusV4)
120
270
deriving (Eq , Ord , Show , Generic )
121
271
122
- type PlutusPurpose f DijkstraEra = ConwayPlutusPurpose f DijkstraEra
272
+ type PlutusPurpose f DijkstraEra = DijkstraPlutusPurpose f DijkstraEra
123
273
124
274
eraMaxLanguage = PlutusV3
125
275
@@ -136,51 +286,41 @@ instance AlonzoEraScript DijkstraEra where
136
286
withPlutusScript (DijkstraPlutusV4 plutus) f = f plutus
137
287
138
288
hoistPlutusPurpose f = \ case
139
- ConwaySpending x -> ConwaySpending $ f x
140
- ConwayMinting x -> ConwayMinting $ f x
141
- ConwayCertifying x -> ConwayCertifying $ f x
142
- ConwayRewarding x -> ConwayRewarding $ f x
143
- ConwayVoting x -> ConwayVoting $ f x
144
- ConwayProposing x -> ConwayProposing $ f x
289
+ DijkstraSpending x -> DijkstraSpending $ f x
290
+ DijkstraMinting x -> DijkstraMinting $ f x
291
+ DijkstraCertifying x -> DijkstraCertifying $ f x
292
+ DijkstraRewarding x -> DijkstraRewarding $ f x
293
+ DijkstraVoting x -> DijkstraVoting $ f x
294
+ DijkstraProposing x -> DijkstraProposing $ f x
295
+ DijkstraGuarding x -> DijkstraGuarding $ f x
145
296
146
- mkSpendingPurpose = ConwaySpending
297
+ mkSpendingPurpose = DijkstraSpending
147
298
148
- toSpendingPurpose (ConwaySpending i) = Just i
299
+ toSpendingPurpose (DijkstraSpending i) = Just i
149
300
toSpendingPurpose _ = Nothing
150
301
151
- mkMintingPurpose = ConwayMinting
302
+ mkMintingPurpose = DijkstraMinting
152
303
153
- toMintingPurpose (ConwayMinting i) = Just i
304
+ toMintingPurpose (DijkstraMinting i) = Just i
154
305
toMintingPurpose _ = Nothing
155
306
156
- mkCertifyingPurpose = ConwayCertifying
307
+ mkCertifyingPurpose = DijkstraCertifying
157
308
158
- toCertifyingPurpose (ConwayCertifying i) = Just i
309
+ toCertifyingPurpose (DijkstraCertifying i) = Just i
159
310
toCertifyingPurpose _ = Nothing
160
311
161
- mkRewardingPurpose = ConwayRewarding
312
+ mkRewardingPurpose = DijkstraRewarding
162
313
163
- toRewardingPurpose (ConwayRewarding i) = Just i
314
+ toRewardingPurpose (DijkstraRewarding i) = Just i
164
315
toRewardingPurpose _ = Nothing
165
316
166
317
upgradePlutusPurposeAsIx = \ case
167
- ConwaySpending (AsIx ix) -> ConwaySpending (AsIx ix)
168
- ConwayMinting (AsIx ix) -> ConwayMinting (AsIx ix)
169
- ConwayCertifying (AsIx ix) -> ConwayCertifying (AsIx ix)
170
- ConwayRewarding (AsIx ix) -> ConwayRewarding (AsIx ix)
171
- ConwayVoting (AsIx ix) -> ConwayVoting (AsIx ix)
172
- ConwayProposing (AsIx ix) -> ConwayProposing (AsIx ix)
173
-
174
- instance ConwayEraScript DijkstraEra where
175
- mkVotingPurpose = ConwayVoting
176
-
177
- toVotingPurpose (ConwayVoting i) = Just i
178
- toVotingPurpose _ = Nothing
179
-
180
- mkProposingPurpose = ConwayProposing
181
-
182
- toProposingPurpose (ConwayProposing i) = Just i
183
- toProposingPurpose _ = Nothing
318
+ ConwaySpending (AsIx ix) -> DijkstraSpending (AsIx ix)
319
+ ConwayMinting (AsIx ix) -> DijkstraMinting (AsIx ix)
320
+ ConwayCertifying (AsIx ix) -> DijkstraCertifying (AsIx ix)
321
+ ConwayRewarding (AsIx ix) -> DijkstraRewarding (AsIx ix)
322
+ ConwayVoting (AsIx ix) -> DijkstraVoting (AsIx ix)
323
+ ConwayProposing (AsIx ix) -> DijkstraProposing (AsIx ix)
184
324
185
325
instance ShelleyEraScript DijkstraEra where
186
326
mkRequireSignature = mkRequireSignatureTimelock
@@ -201,3 +341,30 @@ instance AllegraEraScript DijkstraEra where
201
341
202
342
mkTimeExpire = mkTimeExpireTimelock
203
343
getTimeExpire = getTimeExpireTimelock
344
+
345
+ instance ConwayEraScript DijkstraEra where
346
+ mkVotingPurpose = DijkstraVoting
347
+
348
+ toVotingPurpose (DijkstraVoting i) = Just i
349
+ toVotingPurpose _ = Nothing
350
+
351
+ mkProposingPurpose = DijkstraProposing
352
+
353
+ toProposingPurpose (DijkstraProposing i) = Just i
354
+ toProposingPurpose _ = Nothing
355
+
356
+ class DijkstraEraScript era where
357
+ mkGuardingPurpose :: f Word32 ScriptHash -> PlutusPurpose f era
358
+ toGuardingPurpose :: PlutusPurpose f era -> Maybe (f Word32 ScriptHash )
359
+
360
+ instance DijkstraEraScript DijkstraEra where
361
+ mkGuardingPurpose = DijkstraGuarding
362
+
363
+ toGuardingPurpose (DijkstraGuarding i) = Just i
364
+ toGuardingPurpose _ = Nothing
365
+
366
+ pattern GuardingPurpose ::
367
+ DijkstraEraScript era => f Word32 ScriptHash -> PlutusPurpose f era
368
+ pattern GuardingPurpose c <- (toGuardingPurpose -> Just c)
369
+ where
370
+ GuardingPurpose c = mkGuardingPurpose c
0 commit comments