1
1
{-# LANGUAGE FlexibleContexts #-}
2
2
{-# LANGUAGE OverloadedStrings #-}
3
+ {-# LANGUAGE RecordWildCards #-}
3
4
{-# LANGUAGE NoImplicitPrelude #-}
4
5
5
6
module Cardano.DbSync.Era.Universal.Insert.Grouped (
@@ -17,7 +18,7 @@ module Cardano.DbSync.Era.Universal.Insert.Grouped (
17
18
import qualified Data.List as List
18
19
import qualified Data.Text as Text
19
20
20
- import Cardano.BM.Trace (Trace , logWarning )
21
+ import Cardano.BM.Trace (logWarning )
21
22
import Cardano.Db (DbLovelace (.. ), MinIds (.. ))
22
23
import qualified Cardano.Db as DB
23
24
import qualified Cardano.Db.Schema.Variants.TxOutAddress as VA
@@ -26,6 +27,7 @@ import Cardano.DbSync.Api
26
27
import Cardano.DbSync.Api.Types (InsertOptions (.. ), SyncEnv (.. ), SyncOptions (.. ))
27
28
import Cardano.DbSync.Cache (queryTxIdWithCacheEither )
28
29
import qualified Cardano.DbSync.Era.Shelley.Generic as Generic
30
+ import Cardano.DbSync.Era.Shelley.Generic.Util (unTxHash )
29
31
import Cardano.DbSync.Era.Shelley.Query
30
32
import Cardano.Prelude
31
33
@@ -98,9 +100,18 @@ insertBlockGroupedData syncEnv grouped = do
98
100
then pure []
99
101
else DB. insertBulkTxIn $ etiTxIn <$> groupedTxIn grouped
100
102
whenConsumeOrPruneTxOut syncEnv $ do
103
+ -- Resolve remaining inputs
101
104
etis <- resolveRemainingInputs (groupedTxIn grouped) $ zip txOutIds (fst <$> groupedTxOut grouped)
102
- updateTuples <- mapM (prepareUpdates tracer) etis
103
- DB. updateListTxOutConsumedByTxId $ catMaybes updateTuples
105
+ -- Categorise resolved inputs for bulk vs individual processing
106
+ let (hashBasedUpdates, idBasedUpdates, failedInputs) = categorizeResolvedInputs etis
107
+ -- Bulk process hash-based updates
108
+ unless (null hashBasedUpdates) $ DB. updateConsumedByTxHashBulk txOutVariantType hashBasedUpdates
109
+ -- Individual process ID-based updates
110
+ unless (null idBasedUpdates) $ do
111
+ DB. updateListTxOutConsumedByTxId idBasedUpdates
112
+ -- Log failures
113
+ mapM_ (liftIO . logWarning tracer . (" Failed to find output for " <> ) . Text. pack . show ) failedInputs
114
+
104
115
void . DB. insertBulkTxMetadata removeJsonbFromSchema $ groupedTxMetadata grouped
105
116
void . DB. insertBulkMaTxMint $ groupedTxMint grouped
106
117
pure $ makeMinId txInIds txOutIds maTxOutIds
@@ -109,6 +120,24 @@ insertBlockGroupedData syncEnv grouped = do
109
120
txOutVariantType = getTxOutVariantType syncEnv
110
121
removeJsonbFromSchema = ioRemoveJsonbFromSchema $ soptInsertOptions $ envOptions syncEnv
111
122
123
+ categorizeResolvedInputs :: [ExtendedTxIn ] -> ([DB. BulkConsumedByHash ], [(DB. TxOutIdW , DB. TxId )], [ExtendedTxIn ])
124
+ categorizeResolvedInputs etis =
125
+ let (hashBased, idBased, failed) = foldr categorizeOne ([] , [] , [] ) etis
126
+ in (hashBased, idBased, failed)
127
+ where
128
+ categorizeOne ExtendedTxIn {.. } (hAcc, iAcc, fAcc) =
129
+ case etiTxOutId of
130
+ Right txOutId ->
131
+ (hAcc, (txOutId, DB. txInTxInId etiTxIn) : iAcc, fAcc)
132
+ Left genericTxIn ->
133
+ let bulkData =
134
+ DB. BulkConsumedByHash
135
+ { bchTxHash = unTxHash (Generic. txInTxId genericTxIn)
136
+ , bchOutputIndex = Generic. txInIndex genericTxIn
137
+ , bchConsumingTxId = DB. txInTxInId etiTxIn
138
+ }
139
+ in (bulkData : hAcc, iAcc, fAcc)
140
+
112
141
makeMinId :: [DB. TxInId ] -> [DB. TxOutIdW ] -> [DB. MaTxOutIdW ] -> DB. MinIdsWrapper
113
142
makeMinId txInIds txOutIds maTxOutIds =
114
143
case txOutVariantType of
@@ -148,17 +177,6 @@ mkmaTxOuts _txOutVariantType (txOutId, mmtos) = mkmaTxOut <$> mmtos
148
177
, VA. maTxOutAddressTxOutId = txOutId'
149
178
}
150
179
151
- prepareUpdates ::
152
- MonadIO m =>
153
- Trace IO Text ->
154
- ExtendedTxIn ->
155
- m (Maybe (DB. TxOutIdW , DB. TxId ))
156
- prepareUpdates trce eti = case etiTxOutId eti of
157
- Right txOutId -> pure $ Just (txOutId, DB. txInTxInId (etiTxIn eti))
158
- Left _ -> do
159
- liftIO $ logWarning trce $ " Failed to find output for " <> Text. pack (show eti)
160
- pure Nothing
161
-
162
180
insertReverseIndex ::
163
181
MonadIO m =>
164
182
DB. BlockId ->
0 commit comments