/
Insert.hs
1188 lines (1106 loc) · 46.2 KB
/
Insert.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 BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Cardano.DbSync.Era.Shelley.Insert (
insertShelleyBlock,
-- These are exported for data in Shelley Genesis
insertPoolRegister,
insertStakeRegistration,
insertDelegation,
insertStakeAddressRefIfMissing,
mkAdaPots,
) where
import Cardano.Api (SerialiseAsCBOR (..))
import Cardano.Api.Shelley (
TxMetadataValue (..),
makeTransactionMetadata,
metadataValueToJsonNoSchema,
)
import Cardano.BM.Trace (Trace, logDebug, logInfo, logWarning)
import Cardano.Crypto.Hash (hashToBytes)
import qualified Cardano.Crypto.Hashing as Crypto
import Cardano.Db (DbLovelace (..), DbWord64 (..), PoolUrl (..))
import qualified Cardano.Db as DB
import Cardano.DbSync.Api
import Cardano.DbSync.Cache
import qualified Cardano.DbSync.Era.Shelley.Generic as Generic
import Cardano.DbSync.Era.Shelley.Generic.ParamProposal
import Cardano.DbSync.Era.Shelley.Insert.Epoch
import Cardano.DbSync.Era.Shelley.Insert.Grouped
import Cardano.DbSync.Era.Shelley.Offline
import Cardano.DbSync.Era.Shelley.Query
import Cardano.DbSync.Era.Util (liftLookupFail, safeDecodeToJson)
import Cardano.DbSync.Error
import Cardano.DbSync.Types
import Cardano.DbSync.Util
import qualified Cardano.Ledger.Address as Ledger
import Cardano.Ledger.Alonzo.Language (Language)
import qualified Cardano.Ledger.Alonzo.Scripts as Ledger
import Cardano.Ledger.BaseTypes (getVersion, strictMaybeToMaybe)
import qualified Cardano.Ledger.BaseTypes as Ledger
import Cardano.Ledger.Coin (Coin (..))
import qualified Cardano.Ledger.Coin as Ledger
import qualified Cardano.Ledger.Credential as Ledger
import Cardano.Ledger.Keys
import qualified Cardano.Ledger.Keys as Ledger
import Cardano.Ledger.Mary.Value (AssetName (..), MultiAsset (..), PolicyID (..))
import qualified Cardano.Ledger.Shelley.API.Wallet as Shelley
import qualified Cardano.Ledger.Shelley.TxBody as Shelley
import Cardano.Prelude
import Cardano.Slotting.Block (BlockNo (..))
import Cardano.Slotting.Slot (EpochNo (..), EpochSize (..), SlotNo (..))
import Control.Monad.Trans.Control (MonadBaseControl)
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Lazy.Char8 as LBS
import Data.Either.Extra (eitherToMaybe)
import Data.Group (invert)
import qualified Data.Map.Strict as Map
import qualified Data.Strict.Maybe as Strict
import qualified Data.Text.Encoding as Text
import Database.Persist.Sql (SqlBackend)
import Ouroboros.Consensus.Cardano.Block (StandardCrypto)
{- HLINT ignore "Reduce duplication" -}
type IsPoolMember = PoolKeyHash -> Bool
insertShelleyBlock ::
(MonadBaseControl IO m, MonadIO m) =>
SyncEnv ->
Bool ->
Bool ->
Bool ->
Generic.Block ->
SlotDetails ->
IsPoolMember ->
Strict.Maybe Generic.NewEpoch ->
Generic.StakeSliceRes ->
ReaderT SqlBackend m (Either SyncNodeError ())
insertShelleyBlock syncEnv shouldLog withinTwoMins withinHalfHour blk details isMember mNewEpoch stakeSlice = do
runExceptT $ do
pbid <- case Generic.blkPreviousHash blk of
Nothing -> liftLookupFail (renderErrorMessage (Generic.blkEra blk)) DB.queryGenesis -- this is for networks that fork from Byron on epoch 0.
Just pHash -> queryPrevBlockWithCache (renderErrorMessage (Generic.blkEra blk)) cache pHash
mPhid <- lift $ queryPoolKeyWithCache cache CacheNew $ coerceKeyRole $ Generic.blkSlotLeader blk
slid <- lift . DB.insertSlotLeader $ Generic.mkSlotLeader (Generic.unKeyHashRaw $ Generic.blkSlotLeader blk) (eitherToMaybe mPhid)
blkId <-
lift . insertBlockAndCache cache $
DB.Block
{ DB.blockHash = Generic.blkHash blk
, DB.blockEpochNo = Just $ unEpochNo (sdEpochNo details)
, DB.blockSlotNo = Just $ unSlotNo (Generic.blkSlotNo blk)
, DB.blockEpochSlotNo = Just $ unEpochSlot (sdEpochSlot details)
, DB.blockBlockNo = Just $ unBlockNo (Generic.blkBlockNo blk)
, DB.blockPreviousId = Just pbid
, DB.blockSlotLeaderId = slid
, DB.blockSize = Generic.blkSize blk
, DB.blockTime = sdSlotTime details
, DB.blockTxCount = fromIntegral $ length (Generic.blkTxs blk)
, DB.blockProtoMajor = getVersion $ Ledger.pvMajor (Generic.blkProto blk)
, DB.blockProtoMinor = fromIntegral $ Ledger.pvMinor (Generic.blkProto blk)
, -- Shelley specific
DB.blockVrfKey = Just $ Generic.blkVrfKey blk
, DB.blockOpCert = Just $ Generic.blkOpCert blk
, DB.blockOpCertCounter = Just $ Generic.blkOpCertCounter blk
}
let zippedTx = zip [0 ..] (Generic.blkTxs blk)
let txInserter = insertTx tracer cache (getInsertOptions syncEnv) (getNetwork syncEnv) isMember blkId (sdEpochNo details) (Generic.blkSlotNo blk)
grouped <- foldM (\grouped (idx, tx) -> txInserter idx tx grouped) mempty zippedTx
minIds <- insertBlockGroupedData tracer grouped
when withinHalfHour $
insertReverseIndex blkId minIds
liftIO $ do
let epoch = unEpochNo (sdEpochNo details)
slotWithinEpoch = unEpochSlot (sdEpochSlot details)
when (withinTwoMins && slotWithinEpoch /= 0 && unBlockNo (Generic.blkBlockNo blk) `mod` 20 == 0) $ do
logInfo tracer $
mconcat
[ renderInsertName (Generic.blkEra blk)
, ": continuing epoch "
, textShow epoch
, " (slot "
, textShow slotWithinEpoch
, "/"
, textShow (unEpochSize $ sdEpochSize details)
, ")"
]
logger tracer $
mconcat
[ renderInsertName (Generic.blkEra blk)
, ": epoch "
, textShow (unEpochNo $ sdEpochNo details)
, ", slot "
, textShow (unSlotNo $ Generic.blkSlotNo blk)
, ", block "
, textShow (unBlockNo $ Generic.blkBlockNo blk)
, ", hash "
, renderByteArray (Generic.blkHash blk)
]
whenStrictJust mNewEpoch $ \newEpoch -> do
insertOnNewEpoch tracer blkId (Generic.blkSlotNo blk) (sdEpochNo details) newEpoch
insertStakeSlice syncEnv stakeSlice
when (ioOfflineData iopts && unBlockNo (Generic.blkBlockNo blk) `mod` offlineModBase == 0)
. lift
$ do
insertOfflineResults tracer (envOfflineResultQueue syncEnv)
loadOfflineWorkQueue tracer (envOfflineWorkQueue syncEnv)
where
iopts = getInsertOptions syncEnv
logger :: Trace IO a -> a -> IO ()
logger
| shouldLog = logInfo
| withinTwoMins = logInfo
| unBlockNo (Generic.blkBlockNo blk) `mod` 5000 == 0 = logInfo
| otherwise = logDebug
renderInsertName :: Generic.BlockEra -> Text
renderInsertName eraName =
mconcat ["Insert ", textShow eraName, " Block"]
renderErrorMessage :: Generic.BlockEra -> Text
renderErrorMessage eraName =
case eraName of
Generic.Shelley -> "insertShelleyBlock"
other -> mconcat ["insertShelleyBlock(", textShow other, ")"]
offlineModBase :: Word64
offlineModBase = if withinTwoMins then 10 else 2000
tracer :: Trace IO Text
tracer = getTrace syncEnv
cache :: Cache
cache = envCache syncEnv
-- -----------------------------------------------------------------------------
insertOnNewEpoch ::
(MonadBaseControl IO m, MonadIO m) =>
Trace IO Text ->
DB.BlockId ->
SlotNo ->
EpochNo ->
Generic.NewEpoch ->
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertOnNewEpoch tracer blkId slotNo epochNo newEpoch = do
whenStrictJust (Generic.euProtoParams epochUpdate) $ \params ->
insertEpochParam tracer blkId epochNo params (Generic.euNonce epochUpdate)
whenStrictJust (Generic.neAdaPots newEpoch) $ \pots ->
insertPots blkId slotNo epochNo pots
where
epochUpdate :: Generic.EpochUpdate
epochUpdate = Generic.neEpochUpdate newEpoch
-- -----------------------------------------------------------------------------
insertTx ::
(MonadBaseControl IO m, MonadIO m) =>
Trace IO Text ->
Cache ->
InsertOptions ->
Ledger.Network ->
IsPoolMember ->
DB.BlockId ->
EpochNo ->
SlotNo ->
Word64 ->
Generic.Tx ->
BlockGroupedData ->
ExceptT SyncNodeError (ReaderT SqlBackend m) BlockGroupedData
insertTx tracer cache iopts network isMember blkId epochNo slotNo blockIndex tx grouped = do
let !outSum = fromIntegral $ unCoin $ Generic.txOutSum tx
!withdrawalSum = fromIntegral $ unCoin $ Generic.txWithdrawalSum tx
!resolvedInputs <- mapM (resolveTxInputs (fst <$> groupedTxOut grouped)) (Generic.txInputs tx)
let !inSum = sum $ map (unDbLovelace . forth4) resolvedInputs
let diffSum = if inSum >= outSum then inSum - outSum else 0
let !fees = maybe diffSum (fromIntegral . unCoin) (Generic.txFees tx)
let !txHash = Generic.txHash tx
-- Insert transaction and get txId from the DB.
!txId <-
lift . DB.insertTx $
DB.Tx
{ DB.txHash = txHash
, DB.txBlockId = blkId
, DB.txBlockIndex = blockIndex
, DB.txOutSum = DB.DbLovelace outSum
, DB.txFee = DB.DbLovelace $ fromIntegral fees
, DB.txDeposit =
if not (Generic.txValidContract tx)
then 0
else fromIntegral (inSum + withdrawalSum) - fromIntegral (outSum + fees)
, DB.txSize = Generic.txSize tx
, DB.txInvalidBefore = DbWord64 . unSlotNo <$> Generic.txInvalidBefore tx
, DB.txInvalidHereafter = DbWord64 . unSlotNo <$> Generic.txInvalidHereafter tx
, DB.txValidContract = Generic.txValidContract tx
, DB.txScriptSize = sum $ Generic.txScriptSizes tx
}
if not (Generic.txValidContract tx)
then do
!txOutsGrouped <- mapM (prepareTxOut tracer cache iopts (txId, txHash)) (Generic.txOutputs tx)
let !txIns = map (prepareTxIn txId Map.empty) resolvedInputs
pure $ grouped <> BlockGroupedData txIns txOutsGrouped [] []
else do
-- The following operations only happen if the script passes stage 2 validation (or the tx has
-- no script).
!txOutsGrouped <- mapM (prepareTxOut tracer cache iopts (txId, txHash)) (Generic.txOutputs tx)
!redeemers <- Map.fromList <$> mapM (insertRedeemer tracer (fst <$> groupedTxOut grouped) txId) (Generic.txRedeemer tx)
when (ioPlutusExtra iopts) $
mapM_ (insertDatum tracer cache txId) (Generic.txData tx)
mapM_ (insertCollateralTxIn tracer txId) (Generic.txCollateralInputs tx)
mapM_ (insertReferenceTxIn tracer txId) (Generic.txReferenceInputs tx)
mapM_ (insertCollateralTxOut tracer cache iopts (txId, txHash)) (Generic.txCollateralOutputs tx)
txMetadata <-
whenFalseMempty (ioMetadata iopts) $
prepareTxMetadata tracer txId (Generic.txMetadata tx)
mapM_ (insertCertificate tracer cache isMember network blkId txId epochNo slotNo redeemers) $ Generic.txCertificates tx
mapM_ (insertWithdrawals tracer cache txId redeemers) $ Generic.txWithdrawals tx
mapM_ (insertParamProposal tracer blkId txId) $ Generic.txParamProposal tx
maTxMint <-
whenFalseMempty (ioMetadata iopts) $
prepareMaTxMint tracer cache txId $
Generic.txMint tx
when (ioPlutusExtra iopts) $
mapM_ (insertScript tracer txId) $
Generic.txScripts tx
mapM_ (insertExtraKeyWitness tracer txId) $ Generic.txExtraKeyWitnesses tx
let !txIns = map (prepareTxIn txId redeemers) resolvedInputs
pure $ grouped <> BlockGroupedData txIns txOutsGrouped txMetadata maTxMint
prepareTxOut ::
(MonadBaseControl IO m, MonadIO m) =>
Trace IO Text ->
Cache ->
InsertOptions ->
(DB.TxId, ByteString) ->
Generic.TxOut ->
ExceptT SyncNodeError (ReaderT SqlBackend m) (ExtendedTxOut, [MissingMaTxOut])
prepareTxOut tracer cache iopts (txId, txHash) (Generic.TxOut index addr addrRaw value maMap mScript dt) = do
mSaId <- lift $ insertStakeAddressRefIfMissing tracer cache txId addr
mDatumId <-
whenFalseEmpty (ioPlutusExtra iopts) Nothing $
Generic.whenInlineDatum dt $
insertDatum tracer cache txId
mScriptId <-
whenFalseEmpty (ioPlutusExtra iopts) Nothing $
whenMaybe mScript $
insertScript tracer txId
let !txOut =
DB.TxOut
{ DB.txOutTxId = txId
, DB.txOutIndex = index
, DB.txOutAddress = Generic.renderAddress addr
, DB.txOutAddressRaw = addrRaw
, DB.txOutAddressHasScript = hasScript
, DB.txOutPaymentCred = Generic.maybePaymentCred addr
, DB.txOutStakeAddressId = mSaId
, DB.txOutValue = Generic.coinToDbLovelace value
, DB.txOutDataHash = Generic.dataHashToBytes <$> Generic.getTxOutDatumHash dt
, DB.txOutInlineDatumId = mDatumId
, DB.txOutReferenceScriptId = mScriptId
}
let !eutxo = ExtendedTxOut txHash txOut
!maTxOuts <- whenFalseMempty (ioMultiAssets iopts) $ prepareMaTxOuts tracer cache maMap
pure (eutxo, maTxOuts)
where
hasScript :: Bool
hasScript = maybe False Generic.hasCredScript (Generic.getPaymentCred addr)
insertCollateralTxOut ::
(MonadBaseControl IO m, MonadIO m) =>
Trace IO Text ->
Cache ->
InsertOptions ->
(DB.TxId, ByteString) ->
Generic.TxOut ->
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertCollateralTxOut tracer cache iopts (txId, _txHash) (Generic.TxOut index addr addrRaw value maMap mScript dt) = do
mSaId <- lift $ insertStakeAddressRefIfMissing tracer cache txId addr
mDatumId <-
whenFalseEmpty (ioPlutusExtra iopts) Nothing $
Generic.whenInlineDatum dt $
insertDatum tracer cache txId
mScriptId <-
whenFalseEmpty (ioPlutusExtra iopts) Nothing $
whenMaybe mScript $
insertScript tracer txId
_ <-
lift . DB.insertCollateralTxOut $
DB.CollateralTxOut
{ DB.collateralTxOutTxId = txId
, DB.collateralTxOutIndex = index
, DB.collateralTxOutAddress = Generic.renderAddress addr
, DB.collateralTxOutAddressRaw = addrRaw
, DB.collateralTxOutAddressHasScript = hasScript
, DB.collateralTxOutPaymentCred = Generic.maybePaymentCred addr
, DB.collateralTxOutStakeAddressId = mSaId
, DB.collateralTxOutValue = Generic.coinToDbLovelace value
, DB.collateralTxOutDataHash = Generic.dataHashToBytes <$> Generic.getTxOutDatumHash dt
, DB.collateralTxOutMultiAssetsDescr = textShow maMap
, DB.collateralTxOutInlineDatumId = mDatumId
, DB.collateralTxOutReferenceScriptId = mScriptId
}
pure ()
where
-- TODO: Is there any reason to add new tables for collateral multi-assets/multi-asset-outputs
hasScript :: Bool
hasScript = maybe False Generic.hasCredScript (Generic.getPaymentCred addr)
prepareTxIn ::
DB.TxId ->
Map Word64 DB.RedeemerId ->
(Generic.TxIn, DB.TxId, Either Generic.TxIn DB.TxOutId, DbLovelace) ->
ExtendedTxIn
prepareTxIn txInId redeemers (txIn, txOutId, mTxOutId, _lovelace) =
ExtendedTxIn
{ etiTxIn = txInDB
, etiTxOutId = mTxOutId
}
where
txInDB = DB.TxIn
{ DB.txInTxInId = txInId
, DB.txInTxOutId = txOutId
, DB.txInTxOutIndex = fromIntegral $ Generic.txInIndex txIn
, DB.txInRedeemerId = mlookup (Generic.txInRedeemerIndex txIn) redeemers
}
insertCollateralTxIn ::
(MonadBaseControl IO m, MonadIO m) =>
Trace IO Text ->
DB.TxId ->
Generic.TxIn ->
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertCollateralTxIn _tracer txInId (Generic.TxIn txId index _) = do
txOutId <- liftLookupFail "insertCollateralTxIn" $ DB.queryTxId txId
void . lift . DB.insertCollateralTxIn $
DB.CollateralTxIn
{ DB.collateralTxInTxInId = txInId
, DB.collateralTxInTxOutId = txOutId
, DB.collateralTxInTxOutIndex = fromIntegral index
}
insertReferenceTxIn ::
(MonadBaseControl IO m, MonadIO m) =>
Trace IO Text ->
DB.TxId ->
Generic.TxIn ->
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertReferenceTxIn _tracer txInId (Generic.TxIn txId index _) = do
txOutId <- liftLookupFail "insertReferenceTxIn" $ DB.queryTxId txId
void . lift . DB.insertReferenceTxIn $
DB.ReferenceTxIn
{ DB.referenceTxInTxInId = txInId
, DB.referenceTxInTxOutId = txOutId
, DB.referenceTxInTxOutIndex = fromIntegral index
}
insertCertificate ::
(MonadBaseControl IO m, MonadIO m) =>
Trace IO Text ->
Cache ->
IsPoolMember ->
Ledger.Network ->
DB.BlockId ->
DB.TxId ->
EpochNo ->
SlotNo ->
Map Word64 DB.RedeemerId ->
Generic.TxCertificate ->
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertCertificate tracer cache isMember network blkId txId epochNo slotNo redeemers (Generic.TxCertificate ridx idx cert) =
case cert of
Shelley.DCertDeleg deleg -> insertDelegCert tracer cache network txId idx mRedeemerId epochNo slotNo deleg
Shelley.DCertPool pool -> insertPoolCert tracer cache isMember network epochNo blkId txId idx pool
Shelley.DCertMir mir -> insertMirCert tracer cache network txId idx mir
Shelley.DCertGenesis _gen -> do
-- TODO : Low priority
liftIO $ logWarning tracer "insertCertificate: Unhandled DCertGenesis certificate"
pure ()
where
mRedeemerId = mlookup ridx redeemers
insertPoolCert ::
(MonadBaseControl IO m, MonadIO m) =>
Trace IO Text ->
Cache ->
IsPoolMember ->
Ledger.Network ->
EpochNo ->
DB.BlockId ->
DB.TxId ->
Word16 ->
Shelley.PoolCert StandardCrypto ->
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertPoolCert tracer cache isMember network epoch blkId txId idx pCert =
case pCert of
Shelley.RegPool pParams -> insertPoolRegister tracer cache isMember network epoch blkId txId idx pParams
Shelley.RetirePool keyHash epochNum -> insertPoolRetire txId cache epochNum idx keyHash
insertDelegCert ::
(MonadBaseControl IO m, MonadIO m) =>
Trace IO Text ->
Cache ->
Ledger.Network ->
DB.TxId ->
Word16 ->
Maybe DB.RedeemerId ->
EpochNo ->
SlotNo ->
Shelley.DelegCert StandardCrypto ->
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertDelegCert _tracer cache network txId idx mRedeemerId epochNo slotNo dCert =
case dCert of
Shelley.RegKey cred -> insertStakeRegistration epochNo txId idx $ Generic.annotateStakingCred network cred
Shelley.DeRegKey cred -> insertStakeDeregistration cache network epochNo txId idx mRedeemerId cred
Shelley.Delegate (Shelley.Delegation cred poolkh) -> insertDelegation cache network epochNo slotNo txId idx mRedeemerId cred poolkh
insertPoolRegister ::
(MonadBaseControl IO m, MonadIO m) =>
Trace IO Text ->
Cache ->
IsPoolMember ->
Ledger.Network ->
EpochNo ->
DB.BlockId ->
DB.TxId ->
Word16 ->
Shelley.PoolParams StandardCrypto ->
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertPoolRegister _tracer cache isMember network (EpochNo epoch) blkId txId idx params = do
poolHashId <- lift $ insertPoolKeyWithCache cache CacheNew (Shelley.ppId params)
mdId <- case strictMaybeToMaybe $ Shelley.ppMetadata params of
Just md -> Just <$> insertMetaDataRef poolHashId txId md
Nothing -> pure Nothing
epochActivationDelay <- mkEpochActivationDelay poolHashId
saId <- lift $ insertStakeAddressWithCache cache CacheNew txId (adjustNetworkTag $ Shelley.ppRewardAcnt params)
poolUpdateId <-
lift . DB.insertPoolUpdate $
DB.PoolUpdate
{ DB.poolUpdateHashId = poolHashId
, DB.poolUpdateCertIndex = idx
, DB.poolUpdateVrfKeyHash = hashToBytes (Shelley.ppVrf params)
, DB.poolUpdatePledge = Generic.coinToDbLovelace (Shelley.ppPledge params)
, DB.poolUpdateRewardAddrId = saId
, DB.poolUpdateActiveEpochNo = epoch + epochActivationDelay
, DB.poolUpdateMetaId = mdId
, DB.poolUpdateMargin = realToFrac $ Ledger.unboundRational (Shelley.ppMargin params)
, DB.poolUpdateFixedCost = Generic.coinToDbLovelace (Shelley.ppCost params)
, DB.poolUpdateRegisteredTxId = txId
}
mapM_ (insertPoolOwner cache network poolUpdateId txId) $ toList (Shelley.ppOwners params)
mapM_ (insertPoolRelay poolUpdateId) $ toList (Shelley.ppRelays params)
where
mkEpochActivationDelay :: MonadIO m => DB.PoolHashId -> ExceptT SyncNodeError (ReaderT SqlBackend m) Word64
mkEpochActivationDelay poolHashId =
if isMember (Shelley.ppId params)
then pure 3
else do
-- if the pool is not registered at the end of the previous block, check for
-- other registrations at the current block. If this is the first registration
-- then it's +2, else it's +3.
otherUpdates <- lift $ queryPoolUpdateByBlock blkId poolHashId
pure $ if otherUpdates then 3 else 2
-- Ignore the network in the `RewardAcnt` and use the provided one instead.
-- This is a workaround for https://github.com/input-output-hk/cardano-db-sync/issues/546
adjustNetworkTag :: Ledger.RewardAcnt StandardCrypto -> Ledger.RewardAcnt StandardCrypto
adjustNetworkTag (Shelley.RewardAcnt _ cred) = Shelley.RewardAcnt network cred
insertPoolRetire ::
(MonadBaseControl IO m, MonadIO m) =>
DB.TxId ->
Cache ->
EpochNo ->
Word16 ->
Ledger.KeyHash 'Ledger.StakePool StandardCrypto ->
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertPoolRetire txId cache epochNum idx keyHash = do
poolId <- liftLookupFail "insertPoolRetire" $ queryPoolKeyWithCache cache CacheNew keyHash
void . lift . DB.insertPoolRetire $
DB.PoolRetire
{ DB.poolRetireHashId = poolId
, DB.poolRetireCertIndex = idx
, DB.poolRetireAnnouncedTxId = txId
, DB.poolRetireRetiringEpoch = unEpochNo epochNum
}
insertMetaDataRef ::
(MonadBaseControl IO m, MonadIO m) =>
DB.PoolHashId ->
DB.TxId ->
Shelley.PoolMetadata ->
ExceptT SyncNodeError (ReaderT SqlBackend m) DB.PoolMetadataRefId
insertMetaDataRef poolId txId md =
lift . DB.insertPoolMetadataRef $
DB.PoolMetadataRef
{ DB.poolMetadataRefPoolId = poolId
, DB.poolMetadataRefUrl = PoolUrl $ Ledger.urlToText (Shelley.pmUrl md)
, DB.poolMetadataRefHash = Shelley.pmHash md
, DB.poolMetadataRefRegisteredTxId = txId
}
insertStakeAddressWithCache ::
(MonadBaseControl IO m, MonadIO m) =>
Cache ->
CacheNew ->
DB.TxId ->
Shelley.RewardAcnt StandardCrypto ->
ReaderT SqlBackend m DB.StakeAddressId
insertStakeAddressWithCache cache cacheNew txId rewardAddr = do
eiAddrId <- queryRewardAccountWithCacheRetBs cache cacheNew rewardAddr
case eiAddrId of
Left (_err, bs) -> insertStakeAddress txId rewardAddr (Just bs)
Right addrId -> pure addrId
-- If the address already esists in the table, it will not be inserted again (due to
-- the uniqueness constraint) but the function will return the 'StakeAddressId'.
insertStakeAddress ::
(MonadBaseControl IO m, MonadIO m) =>
DB.TxId ->
Shelley.RewardAcnt StandardCrypto ->
Maybe ByteString ->
ReaderT SqlBackend m DB.StakeAddressId
insertStakeAddress _txId rewardAddr stakeCredBs =
DB.insertStakeAddress $
DB.StakeAddress
{ DB.stakeAddressHashRaw = addrBs
, DB.stakeAddressView = Generic.renderRewardAcnt rewardAddr
, DB.stakeAddressScriptHash = Generic.getCredentialScriptHash $ Ledger.getRwdCred rewardAddr
}
where
addrBs = fromMaybe (Ledger.serialiseRewardAcnt rewardAddr) stakeCredBs
-- | Insert a stake address if it is not already in the `stake_address` table. Regardless of
-- whether it is newly inserted or it is already there, we retrun the `StakeAddressId`.
insertStakeAddressRefIfMissing ::
(MonadBaseControl IO m, MonadIO m) =>
Trace IO Text ->
Cache ->
DB.TxId ->
Ledger.Addr StandardCrypto ->
ReaderT SqlBackend m (Maybe DB.StakeAddressId)
insertStakeAddressRefIfMissing _trce cache txId addr =
case addr of
Ledger.AddrBootstrap {} -> pure Nothing
Ledger.Addr nw _pcred sref ->
case sref of
Ledger.StakeRefBase cred -> do
Just <$> insertStakeAddressWithCache cache DontCacheNew txId (Shelley.RewardAcnt nw cred)
Ledger.StakeRefPtr ptr -> do
queryStakeRefPtr ptr
Ledger.StakeRefNull -> pure Nothing
insertPoolOwner ::
(MonadBaseControl IO m, MonadIO m) =>
Cache ->
Ledger.Network ->
DB.PoolUpdateId ->
DB.TxId ->
Ledger.KeyHash 'Ledger.Staking StandardCrypto ->
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertPoolOwner cache network poolUpdateId txId skh = do
saId <- lift $ insertStakeAddressWithCache cache CacheNew txId (Shelley.RewardAcnt network (Ledger.KeyHashObj skh))
void . lift . DB.insertPoolOwner $
DB.PoolOwner
{ DB.poolOwnerAddrId = saId
, DB.poolOwnerPoolUpdateId = poolUpdateId
}
insertStakeRegistration ::
(MonadBaseControl IO m, MonadIO m) =>
EpochNo ->
DB.TxId ->
Word16 ->
Shelley.RewardAcnt StandardCrypto ->
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertStakeRegistration epochNo txId idx rewardAccount = do
-- We by-pass the cache here It's likely it won't hit.
-- We don't store to the cache yet, since there are many addrresses
-- which are registered and never used.
saId <- lift $ insertStakeAddress txId rewardAccount Nothing
void . lift . DB.insertStakeRegistration $
DB.StakeRegistration
{ DB.stakeRegistrationAddrId = saId
, DB.stakeRegistrationCertIndex = idx
, DB.stakeRegistrationEpochNo = unEpochNo epochNo
, DB.stakeRegistrationTxId = txId
}
insertStakeDeregistration ::
(MonadBaseControl IO m, MonadIO m) =>
Cache ->
Ledger.Network ->
EpochNo ->
DB.TxId ->
Word16 ->
Maybe DB.RedeemerId ->
StakeCred ->
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertStakeDeregistration cache network epochNo txId idx mRedeemerId cred = do
scId <- liftLookupFail "insertStakeDeregistration" $ queryStakeAddrWithCache cache EvictAndReturn network cred
void . lift . DB.insertStakeDeregistration $
DB.StakeDeregistration
{ DB.stakeDeregistrationAddrId = scId
, DB.stakeDeregistrationCertIndex = idx
, DB.stakeDeregistrationEpochNo = unEpochNo epochNo
, DB.stakeDeregistrationTxId = txId
, DB.stakeDeregistrationRedeemerId = mRedeemerId
}
insertDelegation ::
(MonadBaseControl IO m, MonadIO m) =>
Cache ->
Ledger.Network ->
EpochNo ->
SlotNo ->
DB.TxId ->
Word16 ->
Maybe DB.RedeemerId ->
StakeCred ->
Ledger.KeyHash 'Ledger.StakePool StandardCrypto ->
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertDelegation cache network (EpochNo epoch) slotNo txId idx mRedeemerId cred poolkh = do
addrId <- liftLookupFail "insertDelegation" $ queryStakeAddrWithCache cache CacheNew network cred
poolHashId <- liftLookupFail "insertDelegation" $ queryPoolKeyWithCache cache CacheNew poolkh
void . lift . DB.insertDelegation $
DB.Delegation
{ DB.delegationAddrId = addrId
, DB.delegationCertIndex = idx
, DB.delegationPoolHashId = poolHashId
, DB.delegationActiveEpochNo = epoch + 2 -- The first epoch where this delegation is valid.
, DB.delegationTxId = txId
, DB.delegationSlotNo = unSlotNo slotNo
, DB.delegationRedeemerId = mRedeemerId
}
insertMirCert ::
(MonadBaseControl IO m, MonadIO m) =>
Trace IO Text ->
Cache ->
Ledger.Network ->
DB.TxId ->
Word16 ->
Shelley.MIRCert StandardCrypto ->
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertMirCert _tracer cache network txId idx mcert = do
case Shelley.mirPot mcert of
Shelley.ReservesMIR ->
case Shelley.mirRewards mcert of
Shelley.StakeAddressesMIR rwds -> mapM_ insertMirReserves $ Map.toList rwds
Shelley.SendToOppositePotMIR xfrs -> insertPotTransfer (Ledger.toDeltaCoin xfrs)
Shelley.TreasuryMIR -> do
case Shelley.mirRewards mcert of
Shelley.StakeAddressesMIR rwds -> mapM_ insertMirTreasury $ Map.toList rwds
Shelley.SendToOppositePotMIR xfrs -> insertPotTransfer (invert $ Ledger.toDeltaCoin xfrs)
where
insertMirReserves ::
(MonadBaseControl IO m, MonadIO m) =>
(StakeCred, Ledger.DeltaCoin) ->
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertMirReserves (cred, dcoin) = do
addrId <- lift . insertStakeAddressWithCache cache CacheNew txId $ Generic.annotateStakingCred network cred
void . lift . DB.insertReserve $
DB.Reserve
{ DB.reserveAddrId = addrId
, DB.reserveCertIndex = idx
, DB.reserveTxId = txId
, DB.reserveAmount = DB.deltaCoinToDbInt65 dcoin
}
insertMirTreasury ::
(MonadBaseControl IO m, MonadIO m) =>
(StakeCred, Ledger.DeltaCoin) ->
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertMirTreasury (cred, dcoin) = do
addrId <- lift . insertStakeAddressWithCache cache CacheNew txId $ Generic.annotateStakingCred network cred
void . lift . DB.insertTreasury $
DB.Treasury
{ DB.treasuryAddrId = addrId
, DB.treasuryCertIndex = idx
, DB.treasuryTxId = txId
, DB.treasuryAmount = DB.deltaCoinToDbInt65 dcoin
}
insertPotTransfer ::
(MonadBaseControl IO m, MonadIO m) =>
Ledger.DeltaCoin ->
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertPotTransfer dcoinTreasury =
void . lift . DB.insertPotTransfer $
DB.PotTransfer
{ DB.potTransferCertIndex = idx
, DB.potTransferTreasury = DB.deltaCoinToDbInt65 dcoinTreasury
, DB.potTransferReserves = DB.deltaCoinToDbInt65 (invert dcoinTreasury)
, DB.potTransferTxId = txId
}
insertWithdrawals ::
(MonadBaseControl IO m, MonadIO m) =>
Trace IO Text ->
Cache ->
DB.TxId ->
Map Word64 DB.RedeemerId ->
Generic.TxWithdrawal ->
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertWithdrawals _tracer cache txId redeemers txWdrl = do
addrId <-
liftLookupFail "insertWithdrawals" $
queryRewardAccountWithCache cache CacheNew $
Generic.txwRewardAccount txWdrl
void . lift . DB.insertWithdrawal $
DB.Withdrawal
{ DB.withdrawalAddrId = addrId
, DB.withdrawalTxId = txId
, DB.withdrawalAmount = Generic.coinToDbLovelace $ Generic.txwAmount txWdrl
, DB.withdrawalRedeemerId = mlookup (Generic.txwRedeemerIndex txWdrl) redeemers
}
insertPoolRelay ::
(MonadBaseControl IO m, MonadIO m) =>
DB.PoolUpdateId ->
Shelley.StakePoolRelay ->
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertPoolRelay updateId relay =
void . lift . DB.insertPoolRelay $
case relay of
Shelley.SingleHostAddr mPort mIpv4 mIpv6 ->
DB.PoolRelay -- An IPv4 and/or IPv6 address
{ DB.poolRelayUpdateId = updateId
, DB.poolRelayIpv4 = textShow <$> strictMaybeToMaybe mIpv4
, DB.poolRelayIpv6 = textShow <$> strictMaybeToMaybe mIpv6
, DB.poolRelayDnsName = Nothing
, DB.poolRelayDnsSrvName = Nothing
, DB.poolRelayPort = Ledger.portToWord16 <$> strictMaybeToMaybe mPort
}
Shelley.SingleHostName mPort name ->
DB.PoolRelay -- An A or AAAA DNS record
{ DB.poolRelayUpdateId = updateId
, DB.poolRelayIpv4 = Nothing
, DB.poolRelayIpv6 = Nothing
, DB.poolRelayDnsName = Just (Ledger.dnsToText name)
, DB.poolRelayDnsSrvName = Nothing
, DB.poolRelayPort = Ledger.portToWord16 <$> strictMaybeToMaybe mPort
}
Shelley.MultiHostName name ->
DB.PoolRelay -- An SRV DNS record
{ DB.poolRelayUpdateId = updateId
, DB.poolRelayIpv4 = Nothing
, DB.poolRelayIpv6 = Nothing
, DB.poolRelayDnsName = Nothing
, DB.poolRelayDnsSrvName = Just (Ledger.dnsToText name)
, DB.poolRelayPort = Nothing
}
insertParamProposal ::
(MonadBaseControl IO m, MonadIO m) =>
Trace IO Text ->
DB.BlockId ->
DB.TxId ->
ParamProposal ->
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertParamProposal _tracer blkId txId pp = do
cmId <- maybe (pure Nothing) (fmap Just . insertCostModel blkId) (pppCostmdls pp)
void . lift . DB.insertParamProposal $
DB.ParamProposal
{ DB.paramProposalRegisteredTxId = txId
, DB.paramProposalEpochNo = unEpochNo $ pppEpochNo pp
, DB.paramProposalKey = pppKey pp
, DB.paramProposalMinFeeA = fromIntegral <$> pppMinFeeA pp
, DB.paramProposalMinFeeB = fromIntegral <$> pppMinFeeB pp
, DB.paramProposalMaxBlockSize = fromIntegral <$> pppMaxBlockSize pp
, DB.paramProposalMaxTxSize = fromIntegral <$> pppMaxTxSize pp
, DB.paramProposalMaxBhSize = fromIntegral <$> pppMaxBhSize pp
, DB.paramProposalKeyDeposit = Generic.coinToDbLovelace <$> pppKeyDeposit pp
, DB.paramProposalPoolDeposit = Generic.coinToDbLovelace <$> pppPoolDeposit pp
, DB.paramProposalMaxEpoch = unEpochNo <$> pppMaxEpoch pp
, DB.paramProposalOptimalPoolCount = fromIntegral <$> pppOptimalPoolCount pp
, DB.paramProposalInfluence = fromRational <$> pppInfluence pp
, DB.paramProposalMonetaryExpandRate = Generic.unitIntervalToDouble <$> pppMonetaryExpandRate pp
, DB.paramProposalTreasuryGrowthRate = Generic.unitIntervalToDouble <$> pppTreasuryGrowthRate pp
, DB.paramProposalDecentralisation = Generic.unitIntervalToDouble <$> pppDecentralisation pp
, DB.paramProposalEntropy = Generic.nonceToBytes =<< pppEntropy pp
, DB.paramProposalProtocolMajor = getVersion . Ledger.pvMajor <$> pppProtocolVersion pp
, DB.paramProposalProtocolMinor = fromIntegral . Ledger.pvMinor <$> pppProtocolVersion pp
, DB.paramProposalMinUtxoValue = Generic.coinToDbLovelace <$> pppMinUtxoValue pp
, DB.paramProposalMinPoolCost = Generic.coinToDbLovelace <$> pppMinPoolCost pp
, -- New for Alonzo
DB.paramProposalCoinsPerUtxoSize = Generic.coinToDbLovelace <$> pppCoinsPerUtxo pp
, DB.paramProposalCostModelId = cmId
, DB.paramProposalPriceMem = realToFrac <$> pppPriceMem pp
, DB.paramProposalPriceStep = realToFrac <$> pppPriceStep pp
, DB.paramProposalMaxTxExMem = DbWord64 <$> pppMaxTxExMem pp
, DB.paramProposalMaxTxExSteps = DbWord64 <$> pppMaxTxExSteps pp
, DB.paramProposalMaxBlockExMem = DbWord64 <$> pppMaxBlockExMem pp
, DB.paramProposalMaxBlockExSteps = DbWord64 <$> pppMaxBlockExSteps pp
, DB.paramProposalMaxValSize = DbWord64 . fromIntegral <$> pppMaxValSize pp
, DB.paramProposalCollateralPercent = fromIntegral <$> pppCollateralPercentage pp
, DB.paramProposalMaxCollateralInputs = fromIntegral <$> pppMaxCollateralInputs pp
}
insertRedeemer ::
(MonadBaseControl IO m, MonadIO m) =>
Trace IO Text ->
[ExtendedTxOut] ->
DB.TxId ->
(Word64, Generic.TxRedeemer) ->
ExceptT SyncNodeError (ReaderT SqlBackend m) (Word64, DB.RedeemerId)
insertRedeemer tracer groupedOutputs txId (rix, redeemer) = do
tdId <- insertRedeemerData tracer txId $ Generic.txRedeemerData redeemer
scriptHash <- findScriptHash
rid <-
lift . DB.insertRedeemer $
DB.Redeemer
{ DB.redeemerTxId = txId
, DB.redeemerUnitMem = Generic.txRedeemerMem redeemer
, DB.redeemerUnitSteps = Generic.txRedeemerSteps redeemer
, DB.redeemerFee = DB.DbLovelace . fromIntegral . unCoin <$> Generic.txRedeemerFee redeemer
, DB.redeemerPurpose = mkPurpose $ Generic.txRedeemerPurpose redeemer
, DB.redeemerIndex = Generic.txRedeemerIndex redeemer
, DB.redeemerScriptHash = scriptHash
, DB.redeemerRedeemerDataId = tdId
}
pure (rix, rid)
where
mkPurpose :: Ledger.Tag -> DB.ScriptPurpose
mkPurpose tag =
case tag of
Ledger.Spend -> DB.Spend
Ledger.Mint -> DB.Mint
Ledger.Cert -> DB.Cert
Ledger.Rewrd -> DB.Rewrd
findScriptHash ::
(MonadBaseControl IO m, MonadIO m) =>
ExceptT SyncNodeError (ReaderT SqlBackend m) (Maybe ByteString)
findScriptHash =
case Generic.txRedeemerScriptHash redeemer of
Nothing -> pure Nothing
Just (Right bs) -> pure $ Just bs
Just (Left txIn) -> resolveScriptHash groupedOutputs txIn
insertDatum ::
(MonadBaseControl IO m, MonadIO m) =>
Trace IO Text ->
Cache ->
DB.TxId ->
Generic.PlutusData ->
ExceptT SyncNodeError (ReaderT SqlBackend m) DB.DatumId
insertDatum tracer cache txId txd = do
mDatumId <- lift $ queryDatum cache $ Generic.txDataHash txd
case mDatumId of
Just datumId -> pure datumId
Nothing -> do
value <- safeDecodeToJson tracer "insertDatum" $ Generic.txDataValue txd
lift $
insertDatumAndCache cache (Generic.txDataHash txd) $
DB.Datum
{ DB.datumHash = Generic.dataHashToBytes $ Generic.txDataHash txd
, DB.datumTxId = txId
, DB.datumValue = value
, DB.datumBytes = Generic.txDataBytes txd
}
insertRedeemerData ::
(MonadBaseControl IO m, MonadIO m) =>
Trace IO Text ->
DB.TxId ->
Generic.PlutusData ->
ExceptT SyncNodeError (ReaderT SqlBackend m) DB.RedeemerDataId
insertRedeemerData tracer txId txd = do
mRedeemerDataId <- lift $ DB.queryRedeemerData $ Generic.dataHashToBytes $ Generic.txDataHash txd
case mRedeemerDataId of
Just redeemerDataId -> pure redeemerDataId
Nothing -> do
value <- safeDecodeToJson tracer "insertRedeemerData" $ Generic.txDataValue txd
lift . DB.insertRedeemerData $
DB.RedeemerData
{ DB.redeemerDataHash = Generic.dataHashToBytes $ Generic.txDataHash txd
, DB.redeemerDataTxId = txId
, DB.redeemerDataValue = value
, DB.redeemerDataBytes = Generic.txDataBytes txd
}
prepareTxMetadata ::
(MonadBaseControl IO m, MonadIO m) =>
Trace IO Text ->
DB.TxId ->
Maybe (Map Word64 TxMetadataValue) ->
ExceptT SyncNodeError (ReaderT SqlBackend m) [DB.TxMetadata]
prepareTxMetadata tracer txId mmetadata =
case mmetadata of
Nothing -> pure []
Just metadata -> mapM prepare $ Map.toList metadata
where
prepare ::
(MonadBaseControl IO m, MonadIO m) =>
(Word64, TxMetadataValue) ->
ExceptT SyncNodeError (ReaderT SqlBackend m) DB.TxMetadata
prepare (key, md) = do
let jsonbs = LBS.toStrict $ Aeson.encode (metadataValueToJsonNoSchema md)
singleKeyCBORMetadata = serialiseToCBOR $ makeTransactionMetadata (Map.singleton key md)
mjson <- safeDecodeToJson tracer "prepareTxMetadata" jsonbs
pure
DB.TxMetadata
{ DB.txMetadataKey = DbWord64 key
, DB.txMetadataJson = mjson
, DB.txMetadataBytes = singleKeyCBORMetadata
, DB.txMetadataTxId = txId
}
insertCostModel ::
(MonadBaseControl IO m, MonadIO m) =>
DB.BlockId ->
Map Language Ledger.CostModel ->
ExceptT SyncNodeError (ReaderT SqlBackend m) DB.CostModelId
insertCostModel _blkId cms =
lift . DB.insertCostModel $
DB.CostModel
{ DB.costModelHash = Crypto.abstractHashToBytes $ Crypto.serializeCborHash $ Ledger.CostModels cms mempty mempty
, DB.costModelCosts = Text.decodeUtf8 $ LBS.toStrict $ Aeson.encode cms
}
insertEpochParam ::
(MonadBaseControl IO m, MonadIO m) =>
Trace IO Text ->
DB.BlockId ->
EpochNo ->