/
Query.hs
601 lines (483 loc) · 24 KB
/
Query.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
-- The Shelley ledger uses promoted data kinds which we have to use, but we do
-- not export any from this API. We also use them unticked as nature intended.
{-# LANGUAGE DataKinds #-}
{-# OPTIONS_GHC -Wno-unticked-promoted-constructors #-}
-- | Queries from local clients to the node.
--
module Cardano.Api.Query (
-- * Queries
QueryInMode(..),
QueryInEra(..),
QueryInShelleyBasedEra(..),
UTxO(..),
-- * Internal conversion functions
toConsensusQuery,
fromConsensusQueryResult,
-- * Wrapper types used in queries
SerialisedDebugLedgerState(..),
ProtocolState(..),
DebugLedgerState(..),
EraHistory(..),
SlotsInEpoch(..),
SlotsToEpochEnd(..),
slotToEpoch,
LedgerState(..),
getProgress,
) where
import Data.Aeson (ToJSON (..), object, (.=))
import Data.Bifunctor (bimap)
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Maybe (mapMaybe)
import Data.SOP.Strict (SListI)
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Typeable
import Prelude
import Ouroboros.Network.Protocol.LocalStateQuery.Client (Some (..))
import qualified Ouroboros.Consensus.HardFork.Combinator as Consensus
import Ouroboros.Consensus.HardFork.Combinator.AcrossEras (EraMismatch)
import qualified Ouroboros.Consensus.HardFork.Combinator.AcrossEras as Consensus
import qualified Ouroboros.Consensus.HardFork.Combinator.Degenerate as Consensus
import qualified Ouroboros.Consensus.HardFork.Combinator.PartialConfig as PC
import qualified Ouroboros.Consensus.HardFork.History as History
import qualified Ouroboros.Consensus.Ledger.Query as Ledger
import Ouroboros.Consensus.BlockchainTime.WallClock.Types
( RelativeTime, SlotLength, SystemStart )
import qualified Ouroboros.Consensus.Byron.Ledger as Consensus
import Ouroboros.Consensus.Cardano.Block (StandardCrypto, LedgerState(..))
import qualified Ouroboros.Consensus.Cardano.Block as Consensus
import qualified Ouroboros.Consensus.Shelley.Ledger as Consensus
import Ouroboros.Network.Block (Serialised)
import Cardano.Binary
import qualified Cardano.Chain.Update.Validation.Interface as Byron.Update
import qualified Cardano.Ledger.Core as Core
import qualified Cardano.Ledger.Era as Ledger
import qualified Cardano.Ledger.Shelley.Constraints as Shelley
import qualified Shelley.Spec.Ledger.API as Shelley
import qualified Shelley.Spec.Ledger.LedgerState as Shelley
import qualified Shelley.Spec.Ledger.PParams as Shelley
import Cardano.Api.Address
import Cardano.Api.Block
import Cardano.Api.Certificate
import Cardano.Api.Eras
import Cardano.Api.KeysShelley
import Cardano.Api.Modes
import Cardano.Api.NetworkId
import Cardano.Api.Orphans ()
import Cardano.Api.ProtocolParameters
import Cardano.Api.TxBody
import Cardano.Api.Value
import Data.Word (Word64)
import qualified Ouroboros.Consensus.HardFork.History.Qry as Qry
-- ----------------------------------------------------------------------------
-- Queries
--
data QueryInMode mode result where
QueryCurrentEra
:: ConsensusModeIsMultiEra mode
-> QueryInMode mode AnyCardanoEra
QueryInEra
:: EraInMode era mode
-> QueryInEra era result
-> QueryInMode mode (Either EraMismatch result)
QueryEraHistory
:: ConsensusModeIsMultiEra mode
-> QueryInMode mode (EraHistory mode)
QuerySystemStart
:: ConsensusModeIsMultiEra mode
-> QueryInMode mode SystemStart
QueryPartialLedgerConfig
:: ConsensusModeIsMultiEra mode
-> QueryInMode mode (PC.WrapPartialLedgerConfig (ConsensusBlockForMode mode))
data EraHistory mode where
EraHistory
:: ConsensusBlockForMode mode ~ Consensus.HardForkBlock xs
=> ConsensusMode mode
-> History.Interpreter xs
-> EraHistory mode
-- data PartialLedgerConfig mode where
-- PartialLedgerConfig
-- :: ConsensusMode mode
-- -> PC.PartialLedgerConfig xs
-- -> PartialLedgerConfig mode
getProgress :: SlotNo -> EraHistory mode -> Either Qry.PastHorizonException (RelativeTime, SlotLength)
getProgress slotNo (EraHistory _ interpreter) = Qry.interpretQuery interpreter (Qry.slotToWallclock slotNo)
--TODO: add support for these
-- QueryEraStart :: ConsensusModeIsMultiEra mode
-- -> EraInMode era mode
-- -> QueryInMode mode (Maybe EraStart)
newtype SlotsInEpoch = SlotsInEpoch Word64
newtype SlotsToEpochEnd = SlotsToEpochEnd Word64
slotToEpoch :: SlotNo -> EraHistory mode -> Either Qry.PastHorizonException (EpochNo, SlotsInEpoch, SlotsToEpochEnd)
slotToEpoch slotNo (EraHistory _ interpreter) = case Qry.interpretQuery interpreter (Qry.slotToEpoch slotNo) of
Right (epochNumber, slotsInEpoch, slotsToEpochEnd) -> Right (epochNumber, SlotsInEpoch slotsInEpoch, SlotsToEpochEnd slotsToEpochEnd)
Left e -> Left e
deriving instance Show (QueryInMode mode result)
data QueryInEra era result where
QueryByronUpdateState :: QueryInEra ByronEra ByronUpdateState
QueryInShelleyBasedEra :: ShelleyBasedEra era
-> QueryInShelleyBasedEra era result
-> QueryInEra era result
deriving instance Show (QueryInEra era result)
data QueryInShelleyBasedEra era result where
QueryChainPoint
:: QueryInShelleyBasedEra era ChainPoint
QueryEpoch
:: QueryInShelleyBasedEra era EpochNo
QueryGenesisParameters
:: QueryInShelleyBasedEra era GenesisParameters
QueryProtocolParameters
:: QueryInShelleyBasedEra era ProtocolParameters
QueryProtocolParametersUpdate
:: QueryInShelleyBasedEra era
(Map (Hash GenesisKey) ProtocolParametersUpdate)
QueryStakeDistribution
:: QueryInShelleyBasedEra era (Map (Hash StakePoolKey) Rational)
QueryUTxO
:: Maybe (Set AddressAny)
-> QueryInShelleyBasedEra era (UTxO era)
QueryStakeAddresses
:: Set StakeCredential
-> NetworkId
-> QueryInShelleyBasedEra era (Map StakeAddress Lovelace,
Map StakeAddress PoolId)
-- TODO: Need to update ledger-specs dependency to access RewardProvenance
-- QueryPoolRanking
-- :: QueryInShelleyBasedEra era RewardProvenance
QueryDebugLedgerState
:: QueryInShelleyBasedEra era (SerialisedDebugLedgerState era)
QueryProtocolState
:: QueryInShelleyBasedEra era (ProtocolState era)
deriving instance Show (QueryInShelleyBasedEra era result)
-- ----------------------------------------------------------------------------
-- Wrapper types used in queries
--
--TODO: provide appropriate instances for these types as needed, e.g. JSON
newtype ByronUpdateState = ByronUpdateState Byron.Update.State
deriving Show
newtype UTxO era = UTxO (Map TxIn (TxOut era))
instance IsCardanoEra era => ToJSON (UTxO era) where
toJSON (UTxO m) = toJSON m
newtype SerialisedDebugLedgerState era
= SerialisedDebugLedgerState (Serialised (Shelley.NewEpochState (ShelleyLedgerEra era)))
data DebugLedgerState era where
DebugLedgerState :: ShelleyLedgerEra era ~ ledgerera => Shelley.NewEpochState ledgerera -> DebugLedgerState era
instance (Typeable era, Shelley.TransLedgerState FromCBOR (ShelleyLedgerEra era)) => FromCBOR (DebugLedgerState era) where
fromCBOR = DebugLedgerState <$> (fromCBOR :: Decoder s (Shelley.NewEpochState (ShelleyLedgerEra era)))
-- TODO: Shelley based era class!
instance ( IsShelleyBasedEra era
, ShelleyLedgerEra era ~ ledgerera
, Consensus.ShelleyBasedEra ledgerera
, ToJSON (Core.PParams ledgerera)
, ToJSON (Shelley.PParamsDelta ledgerera)
, ToJSON (Core.TxOut ledgerera)) => ToJSON (DebugLedgerState era) where
toJSON (DebugLedgerState newEpochS) = object [ "lastEpoch" .= Shelley.nesEL newEpochS
, "blocksBefore" .= Shelley.nesBprev newEpochS
, "blocksCurrent" .= Shelley.nesBcur newEpochS
, "stateBefore" .= Shelley.nesEs newEpochS
, "possibleRewardUpdate" .= Shelley.nesRu newEpochS
, "stakeDistrib" .= Shelley.nesPd newEpochS
]
newtype ProtocolState era
= ProtocolState (Serialised (Shelley.ChainDepState (Ledger.Crypto (ShelleyLedgerEra era))))
toShelleyAddrSet :: CardanoEra era
-> Set AddressAny
-> Set (Shelley.Addr Consensus.StandardCrypto)
toShelleyAddrSet era =
Set.fromList
. map toShelleyAddr
-- Ignore any addresses that are not appropriate for the era,
-- e.g. Shelley addresses in the Byron era, as these would not
-- appear in the UTxO anyway.
. mapMaybe (anyAddressInEra era)
. Set.toList
fromUTxO
:: ShelleyLedgerEra era ~ ledgerera
=> ShelleyBasedEra era
-> Shelley.UTxO ledgerera
-> UTxO era
fromUTxO eraConversion utxo =
case eraConversion of
ShelleyBasedEraShelley ->
let Shelley.UTxO sUtxo = utxo
in UTxO . Map.fromList . map (bimap fromShelleyTxIn fromShelleyTxOut) $ Map.toList sUtxo
ShelleyBasedEraAllegra ->
let Shelley.UTxO sUtxo = utxo
in UTxO . Map.fromList . map (bimap fromShelleyTxIn (fromTxOut ShelleyBasedEraAllegra)) $ Map.toList sUtxo
ShelleyBasedEraMary ->
let Shelley.UTxO sUtxo = utxo
in UTxO . Map.fromList . map (bimap fromShelleyTxIn (fromTxOut ShelleyBasedEraMary)) $ Map.toList sUtxo
fromShelleyPoolDistr :: Shelley.PoolDistr StandardCrypto
-> Map (Hash StakePoolKey) Rational
fromShelleyPoolDistr =
--TODO: write an appropriate property to show it is safe to use
-- Map.fromListAsc or to use Map.mapKeysMonotonic
Map.fromList
. map (bimap StakePoolKeyHash Shelley.individualPoolStake)
. Map.toList
. Shelley.unPoolDistr
fromShelleyDelegations :: Map (Shelley.Credential Shelley.Staking StandardCrypto)
(Shelley.KeyHash Shelley.StakePool StandardCrypto)
-> Map StakeCredential PoolId
fromShelleyDelegations =
--TODO: write an appropriate property to show it is safe to use
-- Map.fromListAsc or to use Map.mapKeysMonotonic
-- In this case it may not be: the Ord instances for Shelley.Credential
-- do not match the one for StakeCredential
Map.fromList
. map (bimap fromShelleyStakeCredential StakePoolKeyHash)
. Map.toList
fromShelleyRewardAccounts :: Shelley.RewardAccounts Consensus.StandardCrypto
-> Map StakeCredential Lovelace
fromShelleyRewardAccounts =
--TODO: write an appropriate property to show it is safe to use
-- Map.fromListAsc or to use Map.mapKeysMonotonic
Map.fromList
. map (bimap fromShelleyStakeCredential fromShelleyLovelace)
. Map.toList
-- ----------------------------------------------------------------------------
-- Conversions of queries into the consensus types.
--
toConsensusQuery :: forall mode block result.
ConsensusBlockForMode mode ~ block
=> QueryInMode mode result
-> Some (Ledger.Query block)
toConsensusQuery (QueryPartialLedgerConfig CardanoModeIsMultiEra) =
Some Ledger.GetPartialLedgerConfig
toConsensusQuery (QueryEraHistory CardanoModeIsMultiEra) =
Some (Ledger.BlockQuery (Consensus.QueryHardFork Consensus.GetInterpreter))
toConsensusQuery (QueryCurrentEra CardanoModeIsMultiEra) =
Some (Ledger.BlockQuery (Consensus.QueryHardFork Consensus.GetCurrentEra))
toConsensusQuery (QuerySystemStart CardanoModeIsMultiEra) =
Some (Ledger.BlockQuery (Consensus.QueryHardFork Consensus.GetSystemStart))
toConsensusQuery (QueryInEra ByronEraInByronMode QueryByronUpdateState) =
Some (Ledger.BlockQuery (Consensus.DegenQuery Consensus.GetUpdateInterfaceState))
toConsensusQuery (QueryInEra ByronEraInCardanoMode QueryByronUpdateState) =
Some (Ledger.BlockQuery (Consensus.QueryIfCurrentByron Consensus.GetUpdateInterfaceState))
toConsensusQuery (QueryInEra erainmode (QueryInShelleyBasedEra era q)) =
case erainmode of
ByronEraInByronMode -> case era of {}
ShelleyEraInShelleyMode -> toConsensusQueryShelleyBased erainmode q
ByronEraInCardanoMode -> case era of {}
ShelleyEraInCardanoMode -> toConsensusQueryShelleyBased erainmode q
AllegraEraInCardanoMode -> toConsensusQueryShelleyBased erainmode q
MaryEraInCardanoMode -> toConsensusQueryShelleyBased erainmode q
toConsensusQueryShelleyBased
:: forall era ledgerera mode block xs result.
ConsensusBlockForEra era ~ Consensus.ShelleyBlock ledgerera
=> Ledger.Crypto ledgerera ~ Consensus.StandardCrypto
=> ConsensusBlockForMode mode ~ block
=> block ~ Consensus.HardForkBlock xs
=> EraInMode era mode
-> QueryInShelleyBasedEra era result
-> Some (Ledger.Query block)
toConsensusQueryShelleyBased erainmode QueryChainPoint =
Some (Ledger.BlockQuery (consensusQueryInEraInMode erainmode Consensus.GetLedgerTip))
toConsensusQueryShelleyBased erainmode QueryEpoch =
Some (Ledger.BlockQuery (consensusQueryInEraInMode erainmode Consensus.GetEpochNo))
toConsensusQueryShelleyBased erainmode QueryGenesisParameters =
Some (Ledger.BlockQuery (consensusQueryInEraInMode erainmode Consensus.GetGenesisConfig))
toConsensusQueryShelleyBased erainmode QueryProtocolParameters =
Some (Ledger.BlockQuery (consensusQueryInEraInMode erainmode Consensus.GetCurrentPParams))
toConsensusQueryShelleyBased erainmode QueryProtocolParametersUpdate =
Some (Ledger.BlockQuery (consensusQueryInEraInMode erainmode Consensus.GetProposedPParamsUpdates))
toConsensusQueryShelleyBased erainmode QueryStakeDistribution =
Some (Ledger.BlockQuery (consensusQueryInEraInMode erainmode Consensus.GetStakeDistribution))
toConsensusQueryShelleyBased erainmode (QueryUTxO Nothing) =
Some (Ledger.BlockQuery (consensusQueryInEraInMode erainmode Consensus.GetUTxO))
toConsensusQueryShelleyBased erainmode (QueryUTxO (Just addrs)) =
Some (Ledger.BlockQuery (consensusQueryInEraInMode erainmode (Consensus.GetFilteredUTxO addrs')))
where
addrs' :: Set (Shelley.Addr Consensus.StandardCrypto)
addrs' = toShelleyAddrSet (eraInModeToEra erainmode) addrs
toConsensusQueryShelleyBased erainmode (QueryStakeAddresses creds _nId) =
Some (Ledger.BlockQuery (consensusQueryInEraInMode erainmode
(Consensus.GetFilteredDelegationsAndRewardAccounts creds')))
where
creds' :: Set (Shelley.Credential Shelley.Staking StandardCrypto)
creds' = Set.map toShelleyStakeCredential creds
toConsensusQueryShelleyBased erainmode QueryDebugLedgerState =
Some (Ledger.BlockQuery (consensusQueryInEraInMode erainmode (Consensus.GetCBOR Consensus.DebugNewEpochState)))
toConsensusQueryShelleyBased erainmode QueryProtocolState =
Some (Ledger.BlockQuery (consensusQueryInEraInMode erainmode (Consensus.GetCBOR Consensus.DebugChainDepState)))
consensusQueryInEraInMode
:: forall era mode erablock modeblock result result' xs.
ConsensusBlockForEra era ~ erablock
=> ConsensusBlockForMode mode ~ modeblock
=> modeblock ~ Consensus.HardForkBlock xs
=> Consensus.HardForkQueryResult xs result ~ result'
=> EraInMode era mode
-> Consensus.BlockQuery erablock result
-> Consensus.BlockQuery modeblock result'
consensusQueryInEraInMode ByronEraInByronMode = Consensus.DegenQuery
consensusQueryInEraInMode ShelleyEraInShelleyMode = Consensus.DegenQuery
consensusQueryInEraInMode ByronEraInCardanoMode = Consensus.QueryIfCurrentByron
consensusQueryInEraInMode ShelleyEraInCardanoMode = Consensus.QueryIfCurrentShelley
consensusQueryInEraInMode AllegraEraInCardanoMode = Consensus.QueryIfCurrentAllegra
consensusQueryInEraInMode MaryEraInCardanoMode = Consensus.QueryIfCurrentMary
-- ----------------------------------------------------------------------------
-- Conversions of query results from the consensus types.
--
fromConsensusQueryResult :: forall mode block result result'.
ConsensusBlockForMode mode ~ block
=> QueryInMode mode result
-> Ledger.Query block result'
-> result'
-> result
fromConsensusQueryResult (QueryPartialLedgerConfig CardanoModeIsMultiEra) q' r' =
case q' of
Ledger.GetPartialLedgerConfig -> PC.WrapPartialLedgerConfig r'
_ -> fromConsensusQueryResultMismatch
fromConsensusQueryResult (QueryEraHistory CardanoModeIsMultiEra) q' r' =
case q' of
Ledger.BlockQuery (Consensus.QueryHardFork Consensus.GetInterpreter) -> EraHistory CardanoMode r'
_ -> fromConsensusQueryResultMismatch
fromConsensusQueryResult (QuerySystemStart CardanoModeIsMultiEra) q' r' =
case q' of
Ledger.BlockQuery (Consensus.QueryHardFork Consensus.GetSystemStart) -> r'
_ -> fromConsensusQueryResultMismatch
fromConsensusQueryResult (QueryCurrentEra CardanoModeIsMultiEra) q' r' =
case q' of
Ledger.BlockQuery (Consensus.QueryHardFork Consensus.GetCurrentEra) ->
anyEraInModeToAnyEra (fromConsensusEraIndex CardanoMode r')
_ -> fromConsensusQueryResultMismatch
fromConsensusQueryResult (QueryInEra ByronEraInByronMode
QueryByronUpdateState) q' r' =
case (q', r') of
(Ledger.BlockQuery (Consensus.DegenQuery Consensus.GetUpdateInterfaceState),
Consensus.DegenQueryResult r'') ->
Right (ByronUpdateState r'')
_ -> fromConsensusQueryResultMismatch
fromConsensusQueryResult (QueryInEra ByronEraInCardanoMode
QueryByronUpdateState) q' r' =
case q' of
Ledger.BlockQuery (Consensus.QueryIfCurrentByron Consensus.GetUpdateInterfaceState) ->
bimap fromConsensusEraMismatch ByronUpdateState r'
_ -> fromConsensusQueryResultMismatch
fromConsensusQueryResult (QueryInEra ByronEraInByronMode
(QueryInShelleyBasedEra era _)) _ _ =
case era of {}
fromConsensusQueryResult (QueryInEra ShelleyEraInShelleyMode
(QueryInShelleyBasedEra _era q)) q' r' =
case (q', r') of
(Ledger.BlockQuery (Consensus.DegenQuery q''), Consensus.DegenQueryResult r'') ->
Right (fromConsensusQueryResultShelleyBased ShelleyBasedEraShelley q q'' r'')
_ -> fromConsensusQueryResultMismatch
fromConsensusQueryResult (QueryInEra ByronEraInCardanoMode
(QueryInShelleyBasedEra era _)) _ _ =
case era of {}
fromConsensusQueryResult (QueryInEra ShelleyEraInCardanoMode
(QueryInShelleyBasedEra _era q)) q' r' =
case q' of
Ledger.BlockQuery (Consensus.QueryIfCurrentShelley q'') ->
bimap fromConsensusEraMismatch
(fromConsensusQueryResultShelleyBased ShelleyBasedEraShelley q q'')
r'
_ -> fromConsensusQueryResultMismatch
fromConsensusQueryResult (QueryInEra AllegraEraInCardanoMode
(QueryInShelleyBasedEra _era q)) q' r' =
case q' of
Ledger.BlockQuery (Consensus.QueryIfCurrentAllegra q'') ->
bimap fromConsensusEraMismatch
(fromConsensusQueryResultShelleyBased ShelleyBasedEraAllegra q q'')
r'
_ -> fromConsensusQueryResultMismatch
fromConsensusQueryResult (QueryInEra MaryEraInCardanoMode
(QueryInShelleyBasedEra _era q)) q' r' =
case q' of
Ledger.BlockQuery (Consensus.QueryIfCurrentMary q'') ->
bimap fromConsensusEraMismatch
(fromConsensusQueryResultShelleyBased ShelleyBasedEraMary q q'')
r'
_ -> fromConsensusQueryResultMismatch
fromConsensusQueryResultShelleyBased
:: forall era ledgerera result result'.
ShelleyLedgerEra era ~ ledgerera
=> Shelley.PParams ledgerera ~ Core.PParams ledgerera
=> Shelley.PParamsDelta ledgerera ~ Shelley.PParamsUpdate ledgerera
=> Consensus.ShelleyBasedEra ledgerera
=> Ledger.Crypto ledgerera ~ Consensus.StandardCrypto
=> ShelleyBasedEra era
-> QueryInShelleyBasedEra era result
-> Consensus.BlockQuery (Consensus.ShelleyBlock ledgerera) result'
-> result'
-> result
fromConsensusQueryResultShelleyBased _ QueryChainPoint q' point =
case q' of
Consensus.GetLedgerTip -> fromConsensusPoint point
_ -> fromConsensusQueryResultMismatch
fromConsensusQueryResultShelleyBased _ QueryEpoch q' epoch =
case q' of
Consensus.GetEpochNo -> epoch
_ -> fromConsensusQueryResultMismatch
fromConsensusQueryResultShelleyBased _ QueryGenesisParameters q' r' =
case q' of
Consensus.GetGenesisConfig -> fromShelleyGenesis
(Consensus.getCompactGenesis r')
_ -> fromConsensusQueryResultMismatch
fromConsensusQueryResultShelleyBased _ QueryProtocolParameters q' r' =
case q' of
Consensus.GetCurrentPParams -> fromShelleyPParams r'
_ -> fromConsensusQueryResultMismatch
fromConsensusQueryResultShelleyBased _ QueryProtocolParametersUpdate q' r' =
case q' of
Consensus.GetProposedPParamsUpdates -> fromShelleyProposedPPUpdates r'
_ -> fromConsensusQueryResultMismatch
fromConsensusQueryResultShelleyBased _ QueryStakeDistribution q' r' =
case q' of
Consensus.GetStakeDistribution -> fromShelleyPoolDistr r'
_ -> fromConsensusQueryResultMismatch
fromConsensusQueryResultShelleyBased shelleyBasedEra' (QueryUTxO Nothing) q' utxo' =
case q' of
Consensus.GetUTxO -> fromUTxO shelleyBasedEra' utxo'
_ -> fromConsensusQueryResultMismatch
fromConsensusQueryResultShelleyBased shelleyBasedEra' (QueryUTxO Just{}) q' utxo' =
case q' of
Consensus.GetFilteredUTxO{} -> fromUTxO shelleyBasedEra' utxo'
_ -> fromConsensusQueryResultMismatch
fromConsensusQueryResultShelleyBased _ (QueryStakeAddresses _ nId) q' r' =
case q' of
Consensus.GetFilteredDelegationsAndRewardAccounts{}
-> let (delegs, rwaccs) = r'
in ( Map.mapKeys (makeStakeAddress nId) $ fromShelleyRewardAccounts rwaccs
, Map.mapKeys (makeStakeAddress nId) $ fromShelleyDelegations delegs
)
_ -> fromConsensusQueryResultMismatch
fromConsensusQueryResultShelleyBased _ QueryDebugLedgerState{} q' r' =
case q' of
Consensus.GetCBOR Consensus.DebugNewEpochState -> SerialisedDebugLedgerState r'
_ -> fromConsensusQueryResultMismatch
fromConsensusQueryResultShelleyBased _ QueryProtocolState q' r' =
case q' of
Consensus.GetCBOR Consensus.DebugChainDepState -> ProtocolState r'
_ -> fromConsensusQueryResultMismatch
-- | This should /only/ happen if we messed up the mapping in 'toConsensusQuery'
-- and 'fromConsensusQueryResult' so they are inconsistent with each other.
--
-- If we do encounter this error it means that 'toConsensusQuery' maps a
-- API query constructor to a certain consensus query constructor but that
-- 'fromConsensusQueryResult' apparently expects a different pairing.
--
-- For example, imagine if 'toConsensusQuery would (incorrectly) map
-- 'QueryChainPoint' to 'Consensus.GetEpochNo' but 'fromConsensusQueryResult'
-- (correctly) expected to find 'Consensus.GetLedgerTip'. This mismatch would
-- trigger this error.
--
-- Such mismatches should be preventable with an appropriate property test.
--
fromConsensusQueryResultMismatch :: a
fromConsensusQueryResultMismatch =
error "fromConsensusQueryResult: internal query mismatch"
fromConsensusEraMismatch :: SListI xs
=> Consensus.MismatchEraInfo xs -> EraMismatch
fromConsensusEraMismatch = Consensus.mkEraMismatch