-
Notifications
You must be signed in to change notification settings - Fork 211
/
Compatibility.hs
2159 lines (1971 loc) · 75.5 KB
/
Compatibility.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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
-- Orphan instances for {Encode,Decode}Address until we get rid of the
-- Jörmungandr dual support.
{-# OPTIONS_GHC -fno-warn-orphans #-}
-- |
-- Copyright: © 2020 IOHK
-- License: Apache-2.0
--
-- Conversion functions and static chain settings for Shelley.
module Cardano.Wallet.Shelley.Compatibility
( CardanoBlock
, StandardCrypto
, StandardShelley
-- * Protocol Parameters
, NetworkId (..)
, NodeToClientVersionData
, nodeToClientVersions
-- * Node Connection
, localNodeConnectInfo
-- * Genesis
, emptyGenesis
-- * Eras
, AnyCardanoEra (..)
, AnyShelleyBasedEra (..)
, CardanoEra (..)
, ShelleyBasedEra (..)
, shelleyBasedToCardanoEra
, shelleyToCardanoEra
, getShelleyBasedEra
-- * Conversions
, toCardanoHash
, unsealShelleyTx
, toPoint
, fromPoint
, toCardanoTxId
, toCardanoTxIn
, toCardanoUTxO
, fromCardanoTxIn
, fromCardanoTxOut
, fromCardanoWdrls
, cardanoCertKeysForWitnesses
, toCardanoTxOut
, toCardanoLovelace
, toStakeKeyRegCert
, toStakeKeyDeregCert
, toStakePoolDlgCert
, toLedgerStakeCredential
, fromStakeCredential
, toShelleyCoin
, fromShelleyCoin
, toHDPayloadAddress
, toCardanoStakeCredential
, toCardanoValue
, fromCardanoValue
, fromCardanoLovelace
, rewardAccountFromAddress
, fromShelleyPParams
, fromAllegraPParams
, fromMaryPParams
, fromAlonzoPParams
, fromBabbagePParams
, fromConwayPParams
, fromLedgerExUnits
, toLedgerExUnits
, fromCardanoAddress
, toSystemStart
, toScriptPurpose
, fromShelleyTxIn
, toCostModelsAsArray
, toCardanoPolicyId
, toCardanoSimpleScript
, toCardanoSimpleScriptV1
, fromCardanoSimpleScript
-- * Address encoding
, shelleyEncodeAddress
, shelleyEncodeStakeAddress
, shelleyDecodeAddress
, shelleyDecodeStakeAddress
-- * Unsafe conversions
, unsafeLovelaceToWalletCoin
, unsafeValueToLovelace
-- ** Assessing sizes of token bundles
, tokenBundleSizeAssessor
, computeTokenBundleSerializedLengthBytes
-- ** Stake pools
, fromPoolId
, fromPoolDistr
, fromNonMyopicMemberRewards
, optimumNumberOfPools
, getProducer
, fromBlockNo
, fromCardanoBlock
, toCardanoEra
, toCardanoBlockHeader
, toShelleyBlockHeader
, toBabbageBlockHeader
, toConwayBlockHeader
, fromShelleyHash
, fromShelleyTxOut
, fromCardanoHash
, fromChainHash
, fromPrevHash
, fromGenesisData
, fromTip
, fromTip'
, toTip
, fromShelleyBlock
, fromAllegraBlock
, slottingParametersFromGenesis
, fromMaryBlock
, fromAlonzoBlock
, fromBabbageBlock
, fromConwayBlock
, getBabbageProducer
, getConwayProducer
-- * Internal Conversions
, decentralizationLevelFromPParams
-- * Utilities
, inspectAddress
, invertUnitInterval
, interval0
, interval1
, getScriptIntegrityHash
, numberOfTransactionsInBlock
, encodeAddress
, decodeAddress
, encodeStakeAddress
, decodeStakeAddress
) where
import Prelude
import Cardano.Address
( unsafeMkAddress )
import Cardano.Address.Derivation
( XPub, xpubPublicKey )
import Cardano.Address.Script
( KeyHash (..), KeyRole (..), Script (..) )
import Cardano.Api
( AllegraEra
, AlonzoEra
, AnyCardanoEra (..)
, AsType (..)
, BabbageEra
, CardanoEra (..)
, CardanoEraStyle (..)
, CardanoMode
, ConsensusModeParams (CardanoModeParams)
, ConwayEra
, EraInMode (..)
, InAnyCardanoEra (..)
, IsCardanoEra (..)
, LocalNodeConnectInfo (LocalNodeConnectInfo)
, MaryEra
, NetworkId
, ShelleyEra
, TxInMode (..)
, cardanoEraStyle
)
import Cardano.Api.Shelley
( InAnyShelleyBasedEra (..)
, IsShelleyBasedEra (..)
, ShelleyBasedEra (..)
, ShelleyGenesis (..)
)
import Cardano.Binary
( serialize' )
import Cardano.Chain.Block
( ABlockOrBoundary (ABOBBlock, ABOBBoundary), blockTxPayload )
import Cardano.Chain.UTxO
( unTxPayload )
import Cardano.Crypto.Hash.Class
( Hash (UnsafeHash), hashToBytes )
import Cardano.Launcher.Node
( CardanoNodeConn, nodeSocketFile )
import Cardano.Ledger.BaseTypes
( strictMaybeToMaybe, urlToText )
import Cardano.Ledger.Era
( Era (..) )
import Cardano.Ledger.Serialization
( ToCBORGroup )
import Cardano.Pool.Metadata.Types
( StakePoolMetadataHash (..), StakePoolMetadataUrl (..) )
import Cardano.Pool.Types
( PoolId (..), PoolOwner (..) )
import Cardano.Slotting.Slot
( EpochNo (..), EpochSize (..) )
import Cardano.Slotting.Time
( SystemStart (..) )
import Cardano.Wallet.Byron.Compatibility
( fromByronBlock, fromTxAux, maryTokenBundleMaxSize, toByronBlockHeader )
import Cardano.Wallet.Primitive.Types
( ChainPoint (..)
, PoolCertificate
, PoolRegistrationCertificate (..)
, ProtocolParameters (txParameters)
, TokenBundleMaxSize (..)
, TxParameters (getTokenBundleMaxSize)
)
import Cardano.Wallet.Primitive.Types.MinimumUTxO
( minimumUTxOForShelleyBasedEra )
import Cardano.Wallet.Primitive.Types.Tx.Constraints
( TokenBundleSizeAssessment (..), TokenBundleSizeAssessor (..) )
import Cardano.Wallet.Read.NetworkId
( SNetworkId (..) )
import Cardano.Wallet.Read.Primitive.Tx.Allegra
( fromAllegraTx )
import Cardano.Wallet.Read.Primitive.Tx.Alonzo
( fromAlonzoTx )
import Cardano.Wallet.Read.Primitive.Tx.Babbage
( fromBabbageTx )
import Cardano.Wallet.Read.Primitive.Tx.Conway
( fromConwayTx )
import Cardano.Wallet.Read.Primitive.Tx.Features.Fee
( fromShelleyCoin )
import Cardano.Wallet.Read.Primitive.Tx.Features.Inputs
( fromShelleyTxIn )
import Cardano.Wallet.Read.Primitive.Tx.Features.Outputs
( fromCardanoValue, fromShelleyAddress, fromShelleyTxOut )
import Cardano.Wallet.Read.Primitive.Tx.Mary
( fromMaryTx )
import Cardano.Wallet.Read.Primitive.Tx.Shelley
( fromShelleyTx )
import Cardano.Wallet.Read.Tx.Hash
( fromShelleyTxId )
import Cardano.Wallet.Transaction
( WitnessCountCtx (..) )
import Cardano.Wallet.Unsafe
( unsafeIntToWord, unsafeMkPercentage )
import Cardano.Wallet.Util
( internalError, tina )
import Codec.Binary.Bech32
( dataPartFromBytes, dataPartToBytes )
import Control.Applicative
( Const (..), (<|>) )
import Control.Arrow
( left )
import Control.Monad
( when, (>=>) )
import Crypto.Hash.Utils
( blake2b224 )
import Data.Array
( Array )
import Data.Bifunctor
( bimap )
import Data.Binary.Get
( runGetOrFail )
import Data.Binary.Put
( putByteString, putWord8, runPut )
import Data.Bits
( (.&.), (.|.) )
import Data.ByteString
( ByteString )
import Data.ByteString.Base58
( bitcoinAlphabet, encodeBase58 )
import Data.ByteString.Short
( fromShort, toShort )
import Data.Coerce
( coerce )
import Data.Either.Extra
( eitherToMaybe )
import Data.Foldable
( toList )
import Data.IntCast
( intCast, intCastMaybe )
import Data.List
( unzip6 )
import Data.Map.Strict
( Map )
import Data.Maybe
( fromMaybe, isJust, mapMaybe )
import Data.Quantity
( Percentage, Quantity (..), mkPercentage )
import Data.Text
( Text )
import Data.Text.Class
( TextDecodingError (..) )
import Data.Type.Equality
( (:~:) (..), testEquality )
import Data.Typeable
( Typeable )
import Data.Word
( Word16, Word32, Word8 )
import Fmt
( Buildable (..), Builder, (+|), (+||), (||+) )
import GHC.Records
( HasField (..) )
import GHC.Stack
( HasCallStack )
import Numeric.Natural
( Natural )
import Ouroboros.Consensus.Byron.Ledger
( byronBlockRaw )
import Ouroboros.Consensus.Cardano.Block
( CardanoBlock
, CardanoEras
, HardForkBlock (..)
, StandardAllegra
, StandardAlonzo
, StandardBabbage
, StandardConway
, StandardMary
, StandardShelley
)
import Ouroboros.Consensus.HardFork.Combinator.AcrossEras
( OneEraHash (..) )
import Ouroboros.Consensus.HardFork.History.Summary
( Bound (..) )
import Ouroboros.Consensus.Shelley.Eras
( StandardCrypto )
import Ouroboros.Consensus.Shelley.Ledger
( ShelleyCompatible, ShelleyHash (..) )
import Ouroboros.Consensus.Shelley.Ledger.Block
( ShelleyBlock (..) )
import Ouroboros.Network.Block
( BlockNo (..), ChainHash, Point (..), Tip (..), getTipPoint )
import Ouroboros.Network.NodeToClient
( ConnectionId (..)
, LocalAddress (..)
, NodeToClientVersion (..)
, NodeToClientVersionData
)
import Ouroboros.Network.Point
( WithOrigin (..) )
import qualified Cardano.Address as CA
import qualified Cardano.Address.Style.Shelley as CA
import qualified Cardano.Api as Cardano
import qualified Cardano.Api.Byron as Cardano
( Tx (ByronTx) )
import qualified Cardano.Api.Shelley as Cardano
import qualified Cardano.Byron.Codec.Cbor as CBOR
import qualified Cardano.Chain.Common as Byron
import qualified Cardano.Crypto.Hash as Crypto
import qualified Cardano.Ledger.Address as SL
import qualified Cardano.Ledger.Alonzo as Alonzo
import qualified Cardano.Ledger.Alonzo.Language as Alonzo
import qualified Cardano.Ledger.Alonzo.PParams as Alonzo
import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo
import qualified Cardano.Ledger.Alonzo.Tx as Alonzo
import qualified Cardano.Ledger.Alonzo.TxSeq as Alonzo
import qualified Cardano.Ledger.Babbage as Babbage
import qualified Cardano.Ledger.Babbage.PParams as Babbage
import qualified Cardano.Ledger.Babbage.Tx as Babbage hiding
( ScriptIntegrityHash )
import qualified Cardano.Ledger.Babbage.TxBody as Babbage
import qualified Cardano.Ledger.BaseTypes as SL
import qualified Cardano.Ledger.Conway as Conway
import qualified Cardano.Ledger.Conway.PParams as Conway
import qualified Cardano.Ledger.Credential as SL
import qualified Cardano.Ledger.Crypto as SL
import qualified Cardano.Ledger.Era as Ledger.Era
import qualified Cardano.Ledger.Keys as Ledger
import qualified Cardano.Ledger.Mary as Mary
import qualified Cardano.Ledger.Mary.Value as SL
import qualified Cardano.Ledger.SafeHash as SafeHash
import qualified Cardano.Ledger.Shelley as SL hiding
( Value )
import qualified Cardano.Ledger.Shelley as Shelley
import qualified Cardano.Ledger.Shelley.API as SL
import qualified Cardano.Ledger.Shelley.API as SLAPI
import qualified Cardano.Ledger.Shelley.BlockChain as SL
import qualified Cardano.Ledger.ShelleyMA as MA
import qualified Cardano.Protocol.TPraos.BHeader as SL
import qualified Cardano.Wallet.Primitive.Types as W
import qualified Cardano.Wallet.Primitive.Types.Address as W
import qualified Cardano.Wallet.Primitive.Types.Coin as Coin
import qualified Cardano.Wallet.Primitive.Types.Coin as W
import qualified Cardano.Wallet.Primitive.Types.Hash as W
import qualified Cardano.Wallet.Primitive.Types.Redeemer as W
import qualified Cardano.Wallet.Primitive.Types.RewardAccount as W
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as TokenBundle
import qualified Cardano.Wallet.Primitive.Types.TokenPolicy as W
import qualified Cardano.Wallet.Primitive.Types.TokenQuantity as W
import qualified Cardano.Wallet.Primitive.Types.Tx.Constraints as W
import qualified Cardano.Wallet.Primitive.Types.Tx.SealedTx as W
( SealedTx, cardanoTxIdeallyNoLaterThan )
import qualified Cardano.Wallet.Primitive.Types.Tx.Tx as W
( Tx (..) )
import qualified Cardano.Wallet.Primitive.Types.Tx.TxIn as W
( TxIn (TxIn) )
import qualified Cardano.Wallet.Primitive.Types.Tx.TxOut as W
( TxOut (TxOut) )
import qualified Cardano.Wallet.Primitive.Types.UTxO as W
import qualified Cardano.Wallet.Write.ProtocolParameters as Write
import qualified Cardano.Wallet.Write.Tx as Write
import qualified Codec.Binary.Bech32 as Bech32
import qualified Codec.Binary.Bech32.TH as Bech32
import qualified Codec.CBOR.Decoding as CBOR
import qualified Data.Aeson as Aeson
import qualified Data.Array as Array
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Short as SBS
import qualified Data.ListMap as ListMap
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
import qualified Data.Text.Encoding as T
import qualified Ouroboros.Consensus.Protocol.Praos as Consensus
import qualified Ouroboros.Consensus.Protocol.Praos.Header as Consensus
import qualified Ouroboros.Consensus.Protocol.TPraos as Consensus
import qualified Ouroboros.Consensus.Shelley.Ledger as O
import qualified Ouroboros.Consensus.Shelley.Protocol.Abstract as Consensus
import qualified Ouroboros.Network.Block as O
import qualified Ouroboros.Network.Point as Point
--------------------------------------------------------------------------------
--
-- Chain Parameters
-- NOTE
-- For MainNet and TestNet, we can get away with empty genesis blocks with
-- the following assumption:
--
-- - Users won't ever restore a wallet that has genesis UTxO.
--
-- This assumption is _true_ for any user using HD wallets (sequential or
-- random) which means, any user of cardano-wallet.
emptyGenesis :: W.GenesisParameters -> W.Block
emptyGenesis gp = W.Block
{ transactions = []
, delegations = []
, header = W.BlockHeader
{ slotNo =
W.SlotNo 0
, blockHeight =
Quantity 0
, headerHash =
coerce $ W.getGenesisBlockHash gp
, parentHeaderHash =
Nothing
}
}
--------------------------------------------------------------------------------
--
-- Network Parameters
-- | The protocol client version. Distinct from the codecs version.
nodeToClientVersions :: [NodeToClientVersion]
nodeToClientVersions = [NodeToClientV_13]
--------------------------------------------------------------------------------
--
-- Type Conversions
toCardanoHash :: W.Hash "BlockHeader" -> OneEraHash (CardanoEras sc)
toCardanoHash (W.Hash bytes) =
OneEraHash $ toShort bytes
toPoint :: W.ChainPoint -> O.Point (CardanoBlock sc)
toPoint ChainPointAtGenesis = O.GenesisPoint
toPoint (ChainPoint slot h) = O.BlockPoint slot (toCardanoHash h)
fromPoint :: O.Point (CardanoBlock sc) -> W.ChainPoint
fromPoint O.GenesisPoint = ChainPointAtGenesis
fromPoint (O.BlockPoint slot h) = ChainPoint slot (fromCardanoHash h)
toCardanoBlockHeader
:: W.GenesisParameters
-> CardanoBlock StandardCrypto
-> W.BlockHeader
toCardanoBlockHeader gp = \case
BlockByron blk ->
toByronBlockHeader gp blk
BlockShelley blk ->
toShelleyBlockHeader (W.getGenesisBlockHash gp) blk
BlockAllegra blk ->
toShelleyBlockHeader (W.getGenesisBlockHash gp) blk
BlockMary blk ->
toShelleyBlockHeader (W.getGenesisBlockHash gp) blk
BlockAlonzo blk ->
toShelleyBlockHeader (W.getGenesisBlockHash gp) blk
BlockBabbage blk ->
toBabbageBlockHeader (W.getGenesisBlockHash gp) blk
BlockConway blk ->
toBabbageBlockHeader (W.getGenesisBlockHash gp) blk
toShelleyBlockHeader
:: (ShelleyCompatible (Consensus.TPraos StandardCrypto) era)
=> W.Hash "Genesis"
-> ShelleyBlock (Consensus.TPraos StandardCrypto) era
-> W.BlockHeader
toShelleyBlockHeader genesisHash blk =
let
ShelleyBlock (SL.Block header _txSeq) _headerHash = blk
in
W.BlockHeader
{ slotNo =
Consensus.pHeaderSlot header
, blockHeight =
fromBlockNo $ Consensus.pHeaderBlock header
, headerHash =
fromShelleyHash $ Consensus.pHeaderHash header
, parentHeaderHash = Just $
fromPrevHash (coerce genesisHash) $
Consensus.pHeaderPrevHash header
}
toBabbageBlockHeader
:: (ShelleyCompatible (Consensus.Praos StandardCrypto) era)
=> W.Hash "Genesis"
-> ShelleyBlock (Consensus.Praos StandardCrypto) era
-> W.BlockHeader
toBabbageBlockHeader genesisHash blk =
W.BlockHeader
{ slotNo = Consensus.pHeaderSlot header
, blockHeight = fromBlockNo $ Consensus.pHeaderBlock header
, headerHash = fromShelleyHash $ Consensus.pHeaderHash header
, parentHeaderHash = Just $ fromPrevHash (coerce genesisHash) $
Consensus.pHeaderPrevHash header
}
where
ShelleyBlock (SL.Block header _txSeq) _headerHash = blk
toConwayBlockHeader
:: (ShelleyCompatible (Consensus.Praos StandardCrypto) era)
=> W.Hash "Genesis"
-> ShelleyBlock (Consensus.Praos StandardCrypto) era
-> W.BlockHeader
toConwayBlockHeader genesisHash blk =
W.BlockHeader
{ slotNo = Consensus.pHeaderSlot header
, blockHeight = fromBlockNo $ Consensus.pHeaderBlock header
, headerHash = fromShelleyHash $ Consensus.pHeaderHash header
, parentHeaderHash = Just $ fromPrevHash (coerce genesisHash) $
Consensus.pHeaderPrevHash header
}
where
ShelleyBlock (SL.Block header _txSeq) _headerHash = blk
getProducer
:: (Era era, ToCBORGroup (Ledger.Era.TxSeq era))
=> ShelleyBlock (Consensus.TPraos StandardCrypto) era -> PoolId
getProducer (ShelleyBlock (SL.Block (SL.BHeader header _) _) _) =
fromPoolKeyHash $ SL.hashKey (SL.bheaderVk header)
getBabbageProducer
:: (Era era, ToCBORGroup (Ledger.Era.TxSeq era))
=> ShelleyBlock (Consensus.Praos StandardCrypto) era -> PoolId
getBabbageProducer (ShelleyBlock (SL.Block (Consensus.Header header _) _) _) =
fromPoolKeyHash $ SL.hashKey (Consensus.hbVk header)
getConwayProducer
:: (Era era, ToCBORGroup (Ledger.Era.TxSeq era))
=> ShelleyBlock (Consensus.Praos StandardCrypto) era -> PoolId
getConwayProducer (ShelleyBlock (SL.Block (Consensus.Header header _) _) _) =
fromPoolKeyHash $ SL.hashKey (Consensus.hbVk header)
fromCardanoBlock
:: W.GenesisParameters
-> CardanoBlock StandardCrypto
-> W.Block
fromCardanoBlock gp = \case
BlockByron blk ->
fromByronBlock gp blk
BlockShelley blk ->
fst $ fromShelleyBlock gp blk
BlockAllegra blk ->
fst $ fromAllegraBlock gp blk
BlockMary blk ->
fst $ fromMaryBlock gp blk
BlockAlonzo blk ->
fst $ fromAlonzoBlock gp blk
BlockBabbage blk ->
fst $ fromBabbageBlock gp blk
BlockConway blk ->
fst $ fromConwayBlock gp blk
numberOfTransactionsInBlock
:: CardanoBlock StandardCrypto -> (Int, (Quantity "block" Word32, O.SlotNo))
numberOfTransactionsInBlock = \case
BlockByron byb -> transactionsByron byb
BlockShelley shb -> transactions shb
BlockAllegra shb -> transactions shb
BlockMary shb -> transactions shb
BlockAlonzo shb -> transactionsAlonzo shb
BlockBabbage shb -> transactionsBabbage shb
BlockConway shb -> transactionsConway shb
where
transactions
(ShelleyBlock
(SL.Block (SL.BHeader header _) (SL.ShelleyTxSeq txs'))
_
) =
( length txs'
, (fromBlockNo $ SL.bheaderBlockNo header, SL.bheaderSlotNo header)
)
transactionsAlonzo
(ShelleyBlock
(SL.Block (SL.BHeader header _) (Alonzo.AlonzoTxSeq txs'))
_
) =
( length txs'
, (fromBlockNo $ SL.bheaderBlockNo header, SL.bheaderSlotNo header)
)
transactionsBabbage
:: ShelleyBlock
(Consensus.Praos StandardCrypto)
(Babbage.BabbageEra StandardCrypto)
-> (Int, (Quantity "block" Word32, O.SlotNo))
transactionsBabbage
(ShelleyBlock
(SL.Block (Consensus.Header header _)
(Alonzo.AlonzoTxSeq txs')) _) =
( length txs'
, ( fromBlockNo $ Consensus.hbBlockNo header
, Consensus.hbSlotNo header
)
)
transactionsByron blk =
(, (fromBlockNo $ O.blockNo blk, O.blockSlot blk)) $
case byronBlockRaw blk of
ABOBBlock blk' ->
length $ fromTxAux <$> unTxPayload (blockTxPayload blk')
ABOBBoundary _ ->
0
transactionsConway
:: ShelleyBlock
(Consensus.Praos StandardCrypto)
(Conway.ConwayEra StandardCrypto)
-> (Int, (Quantity "block" Word32, O.SlotNo))
transactionsConway
(ShelleyBlock
(SL.Block (Consensus.Header header _)
(Alonzo.AlonzoTxSeq txs')) _) =
( length txs'
, ( fromBlockNo $ Consensus.hbBlockNo header
, Consensus.hbSlotNo header
)
)
toCardanoEra :: CardanoBlock c -> AnyCardanoEra
toCardanoEra = \case
BlockByron{} -> AnyCardanoEra ByronEra
BlockShelley{} -> AnyCardanoEra ShelleyEra
BlockAllegra{} -> AnyCardanoEra AllegraEra
BlockMary{} -> AnyCardanoEra MaryEra
BlockAlonzo{} -> AnyCardanoEra AlonzoEra
BlockBabbage{} -> AnyCardanoEra BabbageEra
BlockConway{} -> AnyCardanoEra ConwayEra
fromShelleyBlock
:: W.GenesisParameters
-> ShelleyBlock
(Consensus.TPraos StandardCrypto)
(SL.ShelleyEra StandardCrypto)
-> (W.Block, [PoolCertificate])
fromShelleyBlock gp blk@(ShelleyBlock (SL.Block _ (SL.ShelleyTxSeq txs')) _) =
let
(txs, certs, _, _, _, _) = unzip6 $ map fromShelleyTx $ toList txs'
certs' = mconcat certs
in
( W.Block
{ header = toShelleyBlockHeader (W.getGenesisBlockHash gp) blk
, transactions = txs
, delegations = toDelegationCertificates certs'
}
, toPoolCertificates certs'
)
fromAllegraBlock
:: W.GenesisParameters
-> ShelleyBlock
(Consensus.TPraos StandardCrypto)
(MA.ShelleyMAEra 'MA.Allegra StandardCrypto)
-> (W.Block, [PoolCertificate])
fromAllegraBlock gp blk@(ShelleyBlock (SL.Block _ (SL.ShelleyTxSeq txs')) _) =
let
(txs, certs, _, _, _, _) = unzip6 $ map fromAllegraTx $ toList txs'
certs' = mconcat certs
in
( W.Block
{ header = toShelleyBlockHeader (W.getGenesisBlockHash gp) blk
, transactions = txs
, delegations = toDelegationCertificates certs'
}
, toPoolCertificates certs'
)
fromMaryBlock
:: W.GenesisParameters
-> ShelleyBlock
(Consensus.TPraos StandardCrypto)
(MA.ShelleyMAEra 'MA.Mary StandardCrypto)
-> (W.Block, [PoolCertificate])
fromMaryBlock gp blk@(ShelleyBlock (SL.Block _ (SL.ShelleyTxSeq txs')) _) =
let
(txs, certs, _, _, _, _) = unzip6 $
map (`fromMaryTx` AnyWitnessCountCtx) $ toList txs'
certs' = mconcat certs
in
( W.Block
{ header = toShelleyBlockHeader (W.getGenesisBlockHash gp) blk
, transactions = txs
, delegations = toDelegationCertificates certs'
}
, toPoolCertificates certs'
)
-- TODO: We could use the cardano-api `Block` pattern to very elegently get the
-- header and txs of any era block.
--
-- We would need to remove the previous block hash from our `W.BlockHeader`,
-- which shouldn't be needed modulo some hacks w.r.t. the genesis point which
-- would need to be cleaned up too. We probably will need to use `Point block`,
-- in all chain followers (including the DBLayer).
fromAlonzoBlock
:: ShelleyCompatible
(Consensus.TPraos StandardCrypto)
(Alonzo.AlonzoEra StandardCrypto)
=> W.GenesisParameters
-> ShelleyBlock
(Consensus.TPraos StandardCrypto)
(Alonzo.AlonzoEra StandardCrypto)
-> (W.Block, [PoolCertificate])
fromAlonzoBlock gp blk@(ShelleyBlock (SL.Block _ txSeq) _) =
let
Alonzo.AlonzoTxSeq txs' = txSeq
(txs, certs, _, _, _, _) = unzip6 $
map (`fromAlonzoTx` AnyWitnessCountCtx) $ toList txs'
certs' = mconcat certs
in
( W.Block
{ header = toShelleyBlockHeader (W.getGenesisBlockHash gp) blk
, transactions = txs
, delegations = toDelegationCertificates certs'
}
, toPoolCertificates certs'
)
fromBabbageBlock
:: W.GenesisParameters
-> ShelleyBlock
(Consensus.Praos StandardCrypto)
(Babbage.BabbageEra StandardCrypto)
-> (W.Block, [PoolCertificate])
fromBabbageBlock gp blk@(ShelleyBlock (SL.Block _ txSeq) _) =
let
Alonzo.AlonzoTxSeq txs' = txSeq
(txs, certs, _, _, _, _) = unzip6 $
map (`fromBabbageTx` AnyWitnessCountCtx) $ toList txs'
certs' = mconcat certs
in
( W.Block
{ header = toBabbageBlockHeader (W.getGenesisBlockHash gp) blk
, transactions = txs
, delegations = toDelegationCertificates certs'
}
, toPoolCertificates certs'
)
fromConwayBlock
:: W.GenesisParameters
-> ShelleyBlock
(Consensus.Praos StandardCrypto)
(Conway.ConwayEra StandardCrypto)
-> (W.Block, [PoolCertificate])
fromConwayBlock gp blk@(ShelleyBlock (SL.Block _ txSeq) _) =
let
Alonzo.AlonzoTxSeq txs' = txSeq
(txs, certs, _, _, _, _) = unzip6 $
map (`fromConwayTx` AnyWitnessCountCtx) $ toList txs'
certs' = mconcat certs
in
( W.Block
{ header = toConwayBlockHeader (W.getGenesisBlockHash gp) blk
, transactions = txs
, delegations = toDelegationCertificates certs'
}
, toPoolCertificates certs'
)
fromShelleyHash :: ShelleyHash crypto -> W.Hash "BlockHeader"
fromShelleyHash (ShelleyHash h) = W.Hash (hashToBytes h)
fromCardanoHash :: O.HeaderHash (CardanoBlock sc) -> W.Hash "BlockHeader"
fromCardanoHash = W.Hash . fromShort . getOneEraHash
fromPrevHash
:: W.Hash "BlockHeader"
-> SL.PrevHash crypto
-> W.Hash "BlockHeader"
fromPrevHash genesisHash = \case
SL.GenesisHash -> genesisHash
SL.BlockHash (SL.HashHeader h) -> W.Hash (hashToBytes h)
fromChainHash
:: W.Hash "Genesis"
-> ChainHash (CardanoBlock sc)
-> W.Hash "BlockHeader"
fromChainHash genesisHash = \case
O.GenesisHash -> coerce genesisHash
O.BlockHash (OneEraHash h) -> W.Hash $ fromShort h
-- FIXME unsafe conversion (Word64 -> Word32)
fromBlockNo :: BlockNo -> Quantity "block" Word32
fromBlockNo (BlockNo h) = Quantity (fromIntegral h)
fromTip' :: W.GenesisParameters -> Tip (CardanoBlock sc) -> W.BlockHeader
fromTip' gp = fromTip (W.getGenesisBlockHash gp)
fromTip
:: W.Hash "Genesis"
-> Tip (CardanoBlock sc)
-> W.BlockHeader
fromTip genesisHash tip = case getPoint (getTipPoint tip) of
Origin -> W.BlockHeader
{ slotNo = W.SlotNo 0
, blockHeight = Quantity 0
, headerHash = coerce genesisHash
, parentHeaderHash = Nothing
}
At blk -> W.BlockHeader
{ slotNo = Point.blockPointSlot blk
, blockHeight = fromBlockNo $ getLegacyTipBlockNo tip
, headerHash = fromCardanoHash $ Point.blockPointHash blk
-- TODO: parentHeaderHash could be removed.
, parentHeaderHash = Just $ W.Hash "parentHeaderHash - unused in Shelley"
}
where
-- TODO: This function was marked deprecated in ouroboros-network.
-- It is wrong, because `Origin` doesn't have a block number.
-- We should remove it.
getLegacyTipBlockNo t = case O.getTipBlockNo t of
Origin -> BlockNo 0
At x -> x
toTip :: W.Hash "Genesis" -> W.BlockHeader -> Tip (CardanoBlock sc)
toTip genesisHash (W.BlockHeader sl bl h _)
| h == (coerce genesisHash) = O.TipGenesis
| otherwise = O.Tip sl
(toCardanoHash h)
(BlockNo $ fromIntegral $ getQuantity bl)
-- NOTE: Unsafe conversion from Natural -> Word16
fromMaxSize :: Natural -> Quantity "byte" Word16
fromMaxSize = Quantity . fromIntegral
fromShelleyPParams
:: W.EraInfo Bound
-> Shelley.ShelleyPParams StandardShelley
-> W.ProtocolParameters
fromShelleyPParams eraInfo pp =
W.ProtocolParameters
{ decentralizationLevel =
decentralizationLevelFromPParams pp
, txParameters =
txParametersFromPParams
maryTokenBundleMaxSize (W.ExecutionUnits 0 0) pp
, desiredNumberOfStakePools =
desiredNumberOfStakePoolsFromPParams pp
, minimumUTxO =
minimumUTxOForShelleyBasedEra ShelleyBasedEraShelley pp
, stakeKeyDeposit = stakeKeyDepositFromPParams pp
, eras = fromBoundToEpochNo <$> eraInfo
-- Collateral inputs were not supported or required in Shelley:
, maximumCollateralInputCount = 0
, minimumCollateralPercentage = 0
, executionUnitPrices = Nothing
, currentLedgerProtocolParameters = Write.InNonRecentEraShelley
}
fromAllegraPParams
:: W.EraInfo Bound
-> Shelley.ShelleyPParams StandardAllegra
-> W.ProtocolParameters
fromAllegraPParams eraInfo pp =
W.ProtocolParameters
{ decentralizationLevel =
decentralizationLevelFromPParams pp
, txParameters =
txParametersFromPParams
maryTokenBundleMaxSize (W.ExecutionUnits 0 0) pp
, desiredNumberOfStakePools =
desiredNumberOfStakePoolsFromPParams pp
, minimumUTxO =
minimumUTxOForShelleyBasedEra ShelleyBasedEraAllegra pp
, stakeKeyDeposit = stakeKeyDepositFromPParams pp
, eras = fromBoundToEpochNo <$> eraInfo
-- Collateral inputs were not supported or required in Allegra:
, maximumCollateralInputCount = 0
, minimumCollateralPercentage = 0
, executionUnitPrices = Nothing
, currentLedgerProtocolParameters = Write.InNonRecentEraAllegra
}
fromMaryPParams
:: W.EraInfo Bound
-> Mary.ShelleyPParams StandardMary
-> W.ProtocolParameters
fromMaryPParams eraInfo pp =
W.ProtocolParameters
{ decentralizationLevel =
decentralizationLevelFromPParams pp
, txParameters =
txParametersFromPParams
maryTokenBundleMaxSize (W.ExecutionUnits 0 0) pp
, desiredNumberOfStakePools =
desiredNumberOfStakePoolsFromPParams pp
, minimumUTxO =
minimumUTxOForShelleyBasedEra ShelleyBasedEraMary pp
, stakeKeyDeposit = stakeKeyDepositFromPParams pp
, eras = fromBoundToEpochNo <$> eraInfo
-- Collateral inputs were not supported or required in Mary:
, maximumCollateralInputCount = 0
, minimumCollateralPercentage = 0
, executionUnitPrices = Nothing
, currentLedgerProtocolParameters = Write.InNonRecentEraMary
}
fromBoundToEpochNo :: Bound -> W.EpochNo
fromBoundToEpochNo (Bound _relTime _slotNo (EpochNo e)) =
W.EpochNo $ fromIntegral e
fromAlonzoPParams
:: HasCallStack
=> W.EraInfo Bound
-> Alonzo.AlonzoPParams StandardAlonzo
-> W.ProtocolParameters
fromAlonzoPParams eraInfo pp =
W.ProtocolParameters
{ decentralizationLevel =
decentralizationLevelFromPParams pp
, txParameters = txParametersFromPParams
(W.TokenBundleMaxSize $ W.TxSize $ Alonzo._maxValSize pp)
(fromLedgerExUnits (getField @"_maxTxExUnits" pp))
pp
, desiredNumberOfStakePools =
desiredNumberOfStakePoolsFromPParams pp
, minimumUTxO =
minimumUTxOForShelleyBasedEra ShelleyBasedEraAlonzo pp
, stakeKeyDeposit = stakeKeyDepositFromPParams pp
, eras = fromBoundToEpochNo <$> eraInfo
, maximumCollateralInputCount = unsafeIntToWord $
Alonzo._maxCollateralInputs pp
, minimumCollateralPercentage =
Alonzo._collateralPercentage pp
, executionUnitPrices =
Just $ executionUnitPricesFromPParams pp
, currentLedgerProtocolParameters = Write.InNonRecentEraAlonzo
}
fromBabbagePParams
:: HasCallStack
=> W.EraInfo Bound
-> Babbage.BabbagePParams StandardBabbage
-> W.ProtocolParameters
fromBabbagePParams eraInfo pp =
W.ProtocolParameters
{ decentralizationLevel =
decentralizationLevelFromPParams pp
, txParameters = txParametersFromPParams
(W.TokenBundleMaxSize $ W.TxSize $ Babbage._maxValSize pp)
(fromLedgerExUnits (getField @"_maxTxExUnits" pp))
pp
, desiredNumberOfStakePools =
desiredNumberOfStakePoolsFromPParams pp
, minimumUTxO =
minimumUTxOForShelleyBasedEra ShelleyBasedEraBabbage pp
, stakeKeyDeposit = stakeKeyDepositFromPParams pp
, eras = fromBoundToEpochNo <$> eraInfo
, maximumCollateralInputCount = unsafeIntToWord $
Babbage._maxCollateralInputs pp
, minimumCollateralPercentage =