@@ -101,15 +101,16 @@ type IsPoolMember = PoolKeyHash -> Bool
101
101
insertShelleyBlock ::
102
102
(MonadBaseControl IO m , MonadIO m ) =>
103
103
SyncEnv ->
104
+ [BlockGroupedData ] ->
104
105
Bool ->
105
106
Bool ->
106
107
Bool ->
107
108
Generic. Block ->
108
109
SlotDetails ->
109
110
IsPoolMember ->
110
111
ApplyResult ->
111
- ReaderT SqlBackend m (Either SyncNodeError () )
112
- insertShelleyBlock syncEnv shouldLog withinTwoMins withinHalfHour blk details isMember applyResult = do
112
+ ReaderT SqlBackend m (Either SyncNodeError [ BlockGroupedData ] )
113
+ insertShelleyBlock syncEnv groupsPrev shouldLog withinTwoMins withinHalfHour blk details isMember applyResult = do
113
114
runExceptT $ do
114
115
pbid <- case Generic. blkPreviousHash blk of
115
116
Nothing -> liftLookupFail (renderErrorMessage (Generic. blkEra blk)) DB. queryGenesis -- this is for networks that fork from Byron on epoch 0.
@@ -141,12 +142,11 @@ insertShelleyBlock syncEnv shouldLog withinTwoMins withinHalfHour blk details is
141
142
142
143
let zippedTx = zip [0 .. ] (Generic. blkTxs blk)
143
144
144
- txsPrepared <- foldAndAccM (prepareTx syncEnv blkId applyResult) zippedTx
145
+ txsPrepared <- foldAndAccM (prepareTx syncEnv txOutPrev blkId applyResult) zippedTx
145
146
txIds <- lift $ DB. insertManyTx (ptrTxDb <$> txsPrepared)
146
- let txInserter = insertTx syncEnv blkId isMember (sdEpochNo details) (Generic. blkSlotNo blk) applyResult
147
+ let txInserter = insertTx syncEnv txOutPrev blkId isMember (sdEpochNo details) (Generic. blkSlotNo blk) applyResult
147
148
let newZip = zipWith3 (\ tx txId ptr -> (txId, tx, ptr)) (Generic. blkTxs blk) txIds txsPrepared
148
149
blockGroupedData <- foldM txInserter mempty newZip
149
- minIds <- insertBlockGroupedData syncEnv blockGroupedData
150
150
151
151
-- now that we've inserted the Block and all it's txs lets cache what we'll need
152
152
-- when we later update the epoch values.
@@ -158,15 +158,12 @@ insertShelleyBlock syncEnv shouldLog withinTwoMins withinHalfHour blk details is
158
158
EpochBlockDiff
159
159
{ ebdBlockId = blkId
160
160
, ebdTime = sdSlotTime details
161
- , ebdFees = groupedTxFees blockGroupedData
161
+ , ebdFees = sum (ptrFees <$> txsPrepared)
162
162
, ebdEpochNo = unEpochNo (sdEpochNo details)
163
- , ebdOutSum = fromIntegral $ groupedTxOutSum blockGroupedData
163
+ , ebdOutSum = sum ( fromIntegral . ptrOutSum <$> txsPrepared)
164
164
, ebdTxCount = fromIntegral $ length (Generic. blkTxs blk)
165
165
}
166
166
167
- when withinHalfHour $
168
- insertReverseIndex blkId minIds
169
-
170
167
liftIO $ do
171
168
let epoch = unEpochNo epochNo
172
169
slotWithinEpoch = unEpochSlot (sdEpochSlot details)
@@ -208,9 +205,20 @@ insertShelleyBlock syncEnv shouldLog withinTwoMins withinHalfHour blk details is
208
205
when (ioOffChainPoolData iopts)
209
206
. lift
210
207
$ insertOffChainPoolResults tracer (envOffChainPoolResultQueue syncEnv)
208
+
209
+ if withinHalfHour then do
210
+ unless (null groupsPrev) $
211
+ void $ insertBlockGroupedData syncEnv $ mconcat $ reverse groupsPrev
212
+ minIds <- insertBlockGroupedData syncEnv blockGroupedData
213
+ insertReverseIndex blkId minIds
214
+ pure []
215
+ else do
216
+ pure $ blockGroupedData : groupsPrev
211
217
where
212
218
iopts = getInsertOptions syncEnv
213
219
220
+ txOutPrev = fmap fst . groupedTxOut <$> groupsPrev
221
+
214
222
logger :: Trace IO a -> a -> IO ()
215
223
logger
216
224
| shouldLog = logInfo
@@ -273,12 +281,13 @@ data PrepareTxRes = PrepareTxRes
273
281
prepareTx ::
274
282
(MonadBaseControl IO m , MonadIO m ) =>
275
283
SyncEnv ->
284
+ [[ExtendedTxOut ]] ->
276
285
DB. BlockId ->
277
286
ApplyResult ->
278
287
[(ByteString , Generic. TxOut )] ->
279
288
(Word64 , Generic. Tx ) ->
280
289
ExceptT SyncNodeError (ReaderT SqlBackend m ) (PrepareTxRes , [(ByteString , Generic. TxOut )])
281
- prepareTx syncEnv blkId applyResult blockTxOuts (blockIndex, tx) = do
290
+ prepareTx syncEnv txOutPrev blkId applyResult blockTxOuts (blockIndex, tx) = do
282
291
let ! txHash = Generic. txHash tx
283
292
let ! mdeposits = if not (Generic. txValidContract tx) then Just (Coin 0 ) else lookupDepositsMap txHash (apDepositsMap applyResult)
284
293
let ! outSum = fromIntegral $ unCoin $ Generic. txOutSum tx
@@ -293,7 +302,7 @@ prepareTx syncEnv blkId applyResult blockTxOuts (blockIndex, tx) = do
293
302
pure (resolvedInputsDB, fees, unCoin <$> mdeposits)
294
303
(_, Nothing ) -> do
295
304
-- Nothing in fees means a phase 2 failure
296
- (resolvedInsFull, amounts) <- splitLast <$> mapM (resolveTxInputsValue blockTxOuts) (Generic. txInputs tx)
305
+ (resolvedInsFull, amounts) <- splitLast <$> mapM (resolveTxInputsValue txOutPrev blockTxOuts) (Generic. txInputs tx)
297
306
let ! inSum = sum $ map unDbLovelace $ catMaybes amounts
298
307
! diffSum = if inSum >= outSum then inSum - outSum else 0
299
308
! fees = maybe diffSum (fromIntegral . unCoin) (Generic. txFees tx)
@@ -318,6 +327,7 @@ prepareTx syncEnv blkId applyResult blockTxOuts (blockIndex, tx) = do
318
327
insertTx ::
319
328
(MonadBaseControl IO m , MonadIO m ) =>
320
329
SyncEnv ->
330
+ [[ExtendedTxOut ]] ->
321
331
DB. BlockId ->
322
332
IsPoolMember ->
323
333
EpochNo ->
@@ -326,17 +336,17 @@ insertTx ::
326
336
BlockGroupedData ->
327
337
(DB. TxId , Generic. Tx , PrepareTxRes ) ->
328
338
ExceptT SyncNodeError (ReaderT SqlBackend m ) BlockGroupedData
329
- insertTx syncEnv blkId isMember epochNo slotNo applyResult grouped (txId, tx, ptr) = do
339
+ insertTx syncEnv txOutPrev blkId isMember epochNo slotNo applyResult grouped (txId, tx, ptr) = do
330
340
let ! txHash = Generic. txHash tx
331
341
disInOut <- liftIO $ getDisableInOutState syncEnv
332
342
if not (Generic. txValidContract tx)
333
343
then do
334
344
! txOutsGrouped <- mapM (prepareTxOut tracer cache iopts (txId, txHash)) (Generic. txOutputs tx)
335
345
336
- ! txIns <- mapM (prepareTxIn txId ( fst <$> groupedTxOut grouped) Map. empty) (ptrResolvedTxIn ptr)
346
+ ! txIns <- mapM (prepareTxIn txId groups Map. empty) (ptrResolvedTxIn ptr)
337
347
-- There is a custom semigroup instance for BlockGroupedData which uses addition for the values `fees` and `outSum`.
338
348
-- Same happens bellow on last line of this function.
339
- pure (grouped <> BlockGroupedData txIns txOutsGrouped [] [] (ptrFees ptr) (ptrOutSum ptr) )
349
+ pure (grouped <> BlockGroupedData txIns txOutsGrouped [] [] )
340
350
else do
341
351
-- The following operations only happen if the script passes stage 2 validation (or the tx has
342
352
-- no script).
@@ -346,7 +356,7 @@ insertTx syncEnv blkId isMember epochNo slotNo applyResult grouped (txId, tx, pt
346
356
Map. fromList
347
357
<$> whenFalseMempty
348
358
(ioPlutusExtra iopts)
349
- (mapM (insertRedeemer tracer disInOut ( fst <$> groupedTxOut grouped) txId) (Generic. txRedeemer tx))
359
+ (mapM (insertRedeemer tracer disInOut groups txId) (Generic. txRedeemer tx))
350
360
351
361
when (ioPlutusExtra iopts) $ do
352
362
mapM_ (insertDatum tracer cache txId) (Generic. txData tx)
@@ -390,13 +400,15 @@ insertTx syncEnv blkId isMember epochNo slotNo applyResult grouped (txId, tx, pt
390
400
mapM_ (insertGovActionProposal cache blkId txId (getGovExpiresAt applyResult epochNo)) $ zip [0 .. ] (Generic. txProposalProcedure tx)
391
401
mapM_ (insertVotingProcedures tracer cache txId) (Generic. txVotingProcedure tx)
392
402
393
- ! txIns <- mapM (prepareTxIn txId ( fst <$> groupedTxOut grouped) redeemers) (ptrResolvedTxIn ptr)
394
- pure (grouped <> BlockGroupedData txIns txOutsGrouped txMetadata maTxMint (ptrFees ptr) (ptrOutSum ptr) )
403
+ ! txIns <- mapM (prepareTxIn txId groups redeemers) (ptrResolvedTxIn ptr)
404
+ pure (grouped <> BlockGroupedData txIns txOutsGrouped txMetadata maTxMint)
395
405
where
396
406
tracer = getTrace syncEnv
397
407
cache = envCache syncEnv
398
408
iopts = getInsertOptions syncEnv
399
409
410
+ groups = (fst <$> groupedTxOut grouped) : txOutPrev
411
+
400
412
prepareTxOut ::
401
413
(MonadBaseControl IO m , MonadIO m ) =>
402
414
Trace IO Text ->
@@ -481,15 +493,15 @@ insertCollateralTxOut tracer cache iopts (txId, _txHash) (Generic.TxOut index ad
481
493
prepareTxIn ::
482
494
Monad m =>
483
495
DB. TxId ->
484
- [ExtendedTxOut ] ->
496
+ [[ ExtendedTxOut ] ] ->
485
497
Map Word64 DB. RedeemerId ->
486
498
(Generic. TxIn , Maybe DB. TxId , Either Generic. TxIn DB. TxOutId ) ->
487
499
ExceptT SyncNodeError m ExtendedTxIn
488
500
prepareTxIn txInId groupedOutputs redeemers (txIn, mtxOutId, mTxOutId) = do
489
501
txOutId <- liftLookupFail " resolveScriptHash" $
490
502
case mtxOutId of
491
503
Just txOutId -> pure $ Right txOutId
492
- Nothing -> case resolveInMemory txIn groupedOutputs of
504
+ Nothing -> case resolveInMemoryMany txIn groupedOutputs of
493
505
Nothing -> pure $ Left $ DB. DbLookupTxHash (Generic. txInHash txIn)
494
506
Just txOut -> pure $ Right $ DB. txOutTxId $ etoTxOut txOut
495
507
let txInDB =
@@ -1138,7 +1150,7 @@ insertRedeemer ::
1138
1150
(MonadBaseControl IO m , MonadIO m ) =>
1139
1151
Trace IO Text ->
1140
1152
Bool ->
1141
- [ExtendedTxOut ] ->
1153
+ [[ ExtendedTxOut ] ] ->
1142
1154
DB. TxId ->
1143
1155
(Word64 , Generic. TxRedeemer ) ->
1144
1156
ExceptT SyncNodeError (ReaderT SqlBackend m ) (Word64 , DB. RedeemerId )
0 commit comments