/
InMode.hs
344 lines (278 loc) · 13.4 KB
/
InMode.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
-- | Transactions in the context of a consensus mode, and other types used in
-- the transaction submission protocol.
--
module Cardano.Api.InMode (
-- * Transaction in a consensus mode
TxInMode(..),
fromConsensusGenTx,
toConsensusGenTx,
-- * Transaction id in a consensus mode
TxIdInMode(..),
toConsensusTxId,
-- * Transaction validation errors
TxValidationError(..),
TxValidationErrorInMode(..),
fromConsensusApplyTxErr,
) where
import Prelude
import Data.SOP.Strict (NS (S, Z))
import qualified Ouroboros.Consensus.Byron.Ledger as Consensus
import qualified Ouroboros.Consensus.Cardano.Block as Consensus
import qualified Ouroboros.Consensus.HardFork.Combinator as Consensus
import Ouroboros.Consensus.HardFork.Combinator.AcrossEras (EraMismatch)
import qualified Ouroboros.Consensus.HardFork.Combinator.Degenerate as Consensus
import qualified Ouroboros.Consensus.Ledger.SupportsMempool as Consensus
import qualified Ouroboros.Consensus.Ledger.SupportsProtocol as Consensus
import qualified Ouroboros.Consensus.Protocol.TPraos as TPraos
import qualified Ouroboros.Consensus.Shelley.HFEras as Consensus
import qualified Ouroboros.Consensus.Shelley.Ledger as Consensus
import qualified Ouroboros.Consensus.TypeFamilyWrappers as Consensus
import Cardano.Api.Eras
import Cardano.Api.Modes
import Cardano.Api.Tx
import Cardano.Api.TxBody
-- ----------------------------------------------------------------------------
-- Transactions in the context of a consensus mode
--
-- | A 'Tx' in one of the eras supported by a given protocol mode.
--
-- For multi-era modes such as the 'CardanoMode' this type is a sum of the
-- different transaction types for all the eras. It is used in the
-- LocalTxSubmission protocol.
--
data TxInMode mode where
-- | Everything we consider a normal transaction.
--
TxInMode :: Tx era -> EraInMode era mode -> TxInMode mode
-- | Byron has various things we can post to the chain which are not
-- actually transactions. This covers: update proposals, votes and
-- delegation certs.
--
TxInByronSpecial :: Consensus.GenTx Consensus.ByronBlock
-> EraInMode ByronEra mode -> TxInMode mode
deriving instance Show (TxInMode mode)
fromConsensusGenTx
:: ConsensusBlockForMode mode ~ block
=> ConsensusMode mode -> Consensus.GenTx block -> TxInMode mode
fromConsensusGenTx ByronMode (Consensus.HardForkGenTx (Consensus.OneEraGenTx (Z tx'))) =
TxInByronSpecial tx' ByronEraInByronMode
fromConsensusGenTx ShelleyMode (Consensus.HardForkGenTx (Consensus.OneEraGenTx (Z tx'))) =
let Consensus.ShelleyTx _txid shelleyEraTx = tx'
in TxInMode (ShelleyTx ShelleyBasedEraShelley shelleyEraTx) ShelleyEraInShelleyMode
fromConsensusGenTx CardanoMode (Consensus.HardForkGenTx (Consensus.OneEraGenTx (Z tx'))) =
TxInByronSpecial tx' ByronEraInCardanoMode
fromConsensusGenTx CardanoMode (Consensus.HardForkGenTx (Consensus.OneEraGenTx (S (Z tx')))) =
let Consensus.ShelleyTx _txid shelleyEraTx = tx'
in TxInMode (ShelleyTx ShelleyBasedEraShelley shelleyEraTx) ShelleyEraInCardanoMode
fromConsensusGenTx CardanoMode (Consensus.HardForkGenTx (Consensus.OneEraGenTx (S (S (Z tx'))))) =
let Consensus.ShelleyTx _txid shelleyEraTx = tx'
in TxInMode (ShelleyTx ShelleyBasedEraAllegra shelleyEraTx) AllegraEraInCardanoMode
fromConsensusGenTx CardanoMode (Consensus.HardForkGenTx (Consensus.OneEraGenTx (S (S (S (Z tx')))))) =
let Consensus.ShelleyTx _txid shelleyEraTx = tx'
in TxInMode (ShelleyTx ShelleyBasedEraMary shelleyEraTx) MaryEraInCardanoMode
fromConsensusGenTx CardanoMode (Consensus.HardForkGenTx (Consensus.OneEraGenTx (S (S (S (S (Z tx'))))))) =
let Consensus.ShelleyTx _txid shelleyEraTx = tx'
in TxInMode (ShelleyTx ShelleyBasedEraAlonzo shelleyEraTx) AlonzoEraInCardanoMode
fromConsensusGenTx CardanoMode (Consensus.HardForkGenTx (Consensus.OneEraGenTx (S (S (S (S (S (Z tx')))))))) =
let Consensus.ShelleyTx _txid shelleyEraTx = tx'
in TxInMode (ShelleyTx ShelleyBasedEraBabbage shelleyEraTx) BabbageEraInCardanoMode
toConsensusGenTx :: ConsensusBlockForMode mode ~ block
=> TxInMode mode
-> Consensus.GenTx block
toConsensusGenTx (TxInMode (ByronTx tx) ByronEraInByronMode) =
Consensus.HardForkGenTx (Consensus.OneEraGenTx (Z tx'))
where
tx' = Consensus.ByronTx (Consensus.byronIdTx tx) tx
toConsensusGenTx (TxInMode (ByronTx tx) ByronEraInCardanoMode) =
Consensus.HardForkGenTx (Consensus.OneEraGenTx (Z tx'))
where
tx' = Consensus.ByronTx (Consensus.byronIdTx tx) tx
--TODO: add the above as mkByronTx to the consensus code,
-- matching mkShelleyTx below
toConsensusGenTx (TxInByronSpecial gtx ByronEraInByronMode) =
Consensus.HardForkGenTx (Consensus.OneEraGenTx (Z gtx))
toConsensusGenTx (TxInByronSpecial gtx ByronEraInCardanoMode) =
Consensus.HardForkGenTx (Consensus.OneEraGenTx (Z gtx))
toConsensusGenTx (TxInMode (ShelleyTx _ tx) ShelleyEraInShelleyMode) =
Consensus.HardForkGenTx (Consensus.OneEraGenTx (Z tx'))
where
tx' = Consensus.mkShelleyTx tx
toConsensusGenTx (TxInMode (ShelleyTx _ tx) ShelleyEraInCardanoMode) =
Consensus.HardForkGenTx (Consensus.OneEraGenTx (S (Z tx')))
where
tx' = Consensus.mkShelleyTx tx
toConsensusGenTx (TxInMode (ShelleyTx _ tx) AllegraEraInCardanoMode) =
Consensus.HardForkGenTx (Consensus.OneEraGenTx (S (S (Z tx'))))
where
tx' = Consensus.mkShelleyTx tx
toConsensusGenTx (TxInMode (ShelleyTx _ tx) MaryEraInCardanoMode) =
Consensus.HardForkGenTx (Consensus.OneEraGenTx (S (S (S (Z tx')))))
where
tx' = Consensus.mkShelleyTx tx
toConsensusGenTx (TxInMode (ShelleyTx _ tx) AlonzoEraInCardanoMode) =
Consensus.HardForkGenTx (Consensus.OneEraGenTx (S (S (S (S (Z tx'))))))
where
tx' = Consensus.mkShelleyTx tx
toConsensusGenTx (TxInMode (ShelleyTx _ tx) BabbageEraInCardanoMode) =
Consensus.HardForkGenTx (Consensus.OneEraGenTx (S (S (S (S (S (Z tx')))))))
where
tx' = Consensus.mkShelleyTx tx
-- ----------------------------------------------------------------------------
-- Transaction ids in the context of a consensus mode
--
-- | A 'TxId' in one of the eras supported by a given protocol mode.
--
-- For multi-era modes such as the 'CardanoMode' this type is a sum of the
-- different transaction types for all the eras. It is used in the
-- LocalTxMonitoring protocol.
--
data TxIdInMode mode where
TxIdInMode :: TxId -> EraInMode era mode -> TxIdInMode mode
toConsensusTxId
:: ConsensusBlockForMode mode ~ block
=> TxIdInMode mode -> Consensus.TxId (Consensus.GenTx block)
toConsensusTxId (TxIdInMode txid ByronEraInByronMode) =
Consensus.HardForkGenTxId . Consensus.OneEraGenTxId . Z $ Consensus.WrapGenTxId txid'
where
txid' :: Consensus.TxId (Consensus.GenTx Consensus.ByronBlock)
txid' = Consensus.ByronTxId $ toByronTxId txid
toConsensusTxId (TxIdInMode t ShelleyEraInShelleyMode) =
Consensus.HardForkGenTxId $ Consensus.OneEraGenTxId $ Z (Consensus.WrapGenTxId txid')
where
txid' :: Consensus.TxId (Consensus.GenTx Consensus.StandardShelleyBlock)
txid' = Consensus.ShelleyTxId $ toShelleyTxId t
toConsensusTxId (TxIdInMode txid ByronEraInCardanoMode) =
Consensus.HardForkGenTxId . Consensus.OneEraGenTxId . Z $ Consensus.WrapGenTxId txid'
where
txid' :: Consensus.TxId (Consensus.GenTx Consensus.ByronBlock)
txid' = Consensus.ByronTxId $ toByronTxId txid
toConsensusTxId (TxIdInMode txid ShelleyEraInCardanoMode) =
Consensus.HardForkGenTxId (Consensus.OneEraGenTxId (S (Z (Consensus.WrapGenTxId txid'))))
where
txid' :: Consensus.TxId (Consensus.GenTx Consensus.StandardShelleyBlock)
txid' = Consensus.ShelleyTxId $ toShelleyTxId txid
toConsensusTxId (TxIdInMode txid AllegraEraInCardanoMode) =
Consensus.HardForkGenTxId (Consensus.OneEraGenTxId (S (S (Z (Consensus.WrapGenTxId txid')))))
where
txid' :: Consensus.TxId (Consensus.GenTx Consensus.StandardAllegraBlock)
txid' = Consensus.ShelleyTxId $ toShelleyTxId txid
toConsensusTxId (TxIdInMode txid MaryEraInCardanoMode) =
Consensus.HardForkGenTxId (Consensus.OneEraGenTxId (S (S (S (Z (Consensus.WrapGenTxId txid'))))))
where
txid' :: Consensus.TxId (Consensus.GenTx Consensus.StandardMaryBlock)
txid' = Consensus.ShelleyTxId $ toShelleyTxId txid
toConsensusTxId (TxIdInMode txid AlonzoEraInCardanoMode) =
Consensus.HardForkGenTxId (Consensus.OneEraGenTxId (S (S (S (S (Z (Consensus.WrapGenTxId txid')))))))
where
txid' :: Consensus.TxId (Consensus.GenTx Consensus.StandardAlonzoBlock)
txid' = Consensus.ShelleyTxId $ toShelleyTxId txid
toConsensusTxId (TxIdInMode txid BabbageEraInCardanoMode) =
Consensus.HardForkGenTxId (Consensus.OneEraGenTxId (S (S (S (S (S (Z (Consensus.WrapGenTxId txid'))))))))
where
txid' :: Consensus.TxId (Consensus.GenTx Consensus.StandardBabbageBlock)
txid' = Consensus.ShelleyTxId $ toShelleyTxId txid
-- ----------------------------------------------------------------------------
-- Transaction validation errors in the context of eras and consensus modes
--
-- | The transaction validations errors that can occur from trying to submit a
-- transaction to a local node. The errors are specific to an era.
--
data TxValidationError era where
ByronTxValidationError
:: Consensus.ApplyTxErr Consensus.ByronBlock
-> TxValidationError ByronEra
ShelleyTxValidationError
:: ShelleyBasedEra era
-> Consensus.ApplyTxErr (Consensus.ShelleyBlock (ConsensusProtocol era) (ShelleyLedgerEra era))
-> TxValidationError era
-- The GADT in the ShelleyTxValidationError case requires a custom instance
instance Show (TxValidationError era) where
showsPrec p (ByronTxValidationError err) =
showParen (p >= 11)
( showString "ByronTxValidationError "
. showsPrec 11 err
)
showsPrec p (ShelleyTxValidationError ShelleyBasedEraShelley err) =
showParen (p >= 11)
( showString "ShelleyTxValidationError ShelleyBasedEraShelley "
. showsPrec 11 err
)
showsPrec p (ShelleyTxValidationError ShelleyBasedEraAllegra err) =
showParen (p >= 11)
( showString "ShelleyTxValidationError ShelleyBasedEraAllegra "
. showsPrec 11 err
)
showsPrec p (ShelleyTxValidationError ShelleyBasedEraMary err) =
showParen (p >= 11)
( showString "ShelleyTxValidationError ShelleyBasedEraMary "
. showsPrec 11 err
)
showsPrec p (ShelleyTxValidationError ShelleyBasedEraAlonzo err) =
showParen (p >= 11)
( showString "ShelleyTxValidationError ShelleyBasedEraAlonzo "
. showsPrec 11 err
)
showsPrec p (ShelleyTxValidationError ShelleyBasedEraBabbage err) =
showParen (p >= 11)
( showString "ShelleyTxValidationError ShelleyBasedEraBabbage "
. showsPrec 11 err
)
-- | A 'TxValidationError' in one of the eras supported by a given protocol
-- mode.
--
-- This is used in the LocalStateQuery protocol.
--
data TxValidationErrorInMode mode where
TxValidationErrorInMode :: TxValidationError era
-> EraInMode era mode
-> TxValidationErrorInMode mode
TxValidationEraMismatch :: EraMismatch
-> TxValidationErrorInMode mode
deriving instance Show (TxValidationErrorInMode mode)
fromConsensusApplyTxErr :: ConsensusBlockForMode mode ~ block
=> Consensus.LedgerSupportsProtocol
(Consensus.ShelleyBlock
(TPraos.TPraos Consensus.StandardCrypto)
(Consensus.ShelleyEra Consensus.StandardCrypto))
=> ConsensusMode mode
-> Consensus.ApplyTxErr block
-> TxValidationErrorInMode mode
fromConsensusApplyTxErr ByronMode (Consensus.DegenApplyTxErr err) =
TxValidationErrorInMode
(ByronTxValidationError err)
ByronEraInByronMode
fromConsensusApplyTxErr ShelleyMode (Consensus.DegenApplyTxErr err) =
TxValidationErrorInMode
(ShelleyTxValidationError ShelleyBasedEraShelley err)
ShelleyEraInShelleyMode
fromConsensusApplyTxErr CardanoMode (Consensus.ApplyTxErrByron err) =
TxValidationErrorInMode
(ByronTxValidationError err)
ByronEraInCardanoMode
fromConsensusApplyTxErr CardanoMode (Consensus.ApplyTxErrShelley err) =
TxValidationErrorInMode
(ShelleyTxValidationError ShelleyBasedEraShelley err)
ShelleyEraInCardanoMode
fromConsensusApplyTxErr CardanoMode (Consensus.ApplyTxErrAllegra err) =
TxValidationErrorInMode
(ShelleyTxValidationError ShelleyBasedEraAllegra err)
AllegraEraInCardanoMode
fromConsensusApplyTxErr CardanoMode (Consensus.ApplyTxErrMary err) =
TxValidationErrorInMode
(ShelleyTxValidationError ShelleyBasedEraMary err)
MaryEraInCardanoMode
fromConsensusApplyTxErr CardanoMode (Consensus.ApplyTxErrAlonzo err) =
TxValidationErrorInMode
(ShelleyTxValidationError ShelleyBasedEraAlonzo err)
AlonzoEraInCardanoMode
fromConsensusApplyTxErr CardanoMode (Consensus.ApplyTxErrBabbage err) =
TxValidationErrorInMode
(ShelleyTxValidationError ShelleyBasedEraBabbage err)
BabbageEraInCardanoMode
fromConsensusApplyTxErr CardanoMode (Consensus.ApplyTxErrWrongEra err) =
TxValidationEraMismatch err