@@ -101,15 +101,16 @@ type IsPoolMember = PoolKeyHash -> Bool
101101insertShelleyBlock ::
102102 (MonadBaseControl IO m , MonadIO m ) =>
103103 SyncEnv ->
104+ [BlockGroupedData ] ->
104105 Bool ->
105106 Bool ->
106107 Bool ->
107108 Generic. Block ->
108109 SlotDetails ->
109110 IsPoolMember ->
110111 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
113114 runExceptT $ do
114115 pbid <- case Generic. blkPreviousHash blk of
115116 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
141142
142143 let zippedTx = zip [0 .. ] (Generic. blkTxs blk)
143144
144- txsPrepared <- foldAndAccM (prepareTx syncEnv blkId applyResult) zippedTx
145+ txsPrepared <- foldAndAccM (prepareTx syncEnv txOutPrev blkId applyResult) zippedTx
145146 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
147148 let newZip = zipWith3 (\ tx txId ptr -> (txId, tx, ptr)) (Generic. blkTxs blk) txIds txsPrepared
148149 blockGroupedData <- foldM txInserter mempty newZip
149- minIds <- insertBlockGroupedData syncEnv blockGroupedData
150150
151151 -- now that we've inserted the Block and all it's txs lets cache what we'll need
152152 -- when we later update the epoch values.
@@ -164,9 +164,6 @@ insertShelleyBlock syncEnv shouldLog withinTwoMins withinHalfHour blk details is
164164 , ebdTxCount = fromIntegral $ length (Generic. blkTxs blk)
165165 }
166166
167- when withinHalfHour $
168- insertReverseIndex blkId minIds
169-
170167 liftIO $ do
171168 let epoch = unEpochNo epochNo
172169 slotWithinEpoch = unEpochSlot (sdEpochSlot details)
@@ -208,9 +205,20 @@ insertShelleyBlock syncEnv shouldLog withinTwoMins withinHalfHour blk details is
208205 when (ioOffChainPoolData iopts)
209206 . lift
210207 $ 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
211217 where
212218 iopts = getInsertOptions syncEnv
213219
220+ txOutPrev = fmap fst . groupedTxOut <$> groupsPrev
221+
214222 logger :: Trace IO a -> a -> IO ()
215223 logger
216224 | shouldLog = logInfo
@@ -273,12 +281,13 @@ data PrepareTxRes = PrepareTxRes
273281prepareTx ::
274282 (MonadBaseControl IO m , MonadIO m ) =>
275283 SyncEnv ->
284+ [[ExtendedTxOut ]] ->
276285 DB. BlockId ->
277286 ApplyResult ->
278287 [(ByteString , Generic. TxOut )] ->
279288 (Word64 , Generic. Tx ) ->
280289 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
282291 let ! txHash = Generic. txHash tx
283292 let ! mdeposits = if not (Generic. txValidContract tx) then Just (Coin 0 ) else lookupDepositsMap txHash (apDepositsMap applyResult)
284293 let ! outSum = fromIntegral $ unCoin $ Generic. txOutSum tx
@@ -293,7 +302,7 @@ prepareTx syncEnv blkId applyResult blockTxOuts (blockIndex, tx) = do
293302 pure (resolvedInputsDB, fees, unCoin <$> mdeposits)
294303 (_, Nothing ) -> do
295304 -- 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)
297306 let ! inSum = sum $ map unDbLovelace $ catMaybes amounts
298307 ! diffSum = if inSum >= outSum then inSum - outSum else 0
299308 ! fees = maybe diffSum (fromIntegral . unCoin) (Generic. txFees tx)
@@ -318,6 +327,7 @@ prepareTx syncEnv blkId applyResult blockTxOuts (blockIndex, tx) = do
318327insertTx ::
319328 (MonadBaseControl IO m , MonadIO m ) =>
320329 SyncEnv ->
330+ [[ExtendedTxOut ]] ->
321331 DB. BlockId ->
322332 IsPoolMember ->
323333 EpochNo ->
@@ -326,14 +336,14 @@ insertTx ::
326336 BlockGroupedData ->
327337 (DB. TxId , Generic. Tx , PrepareTxRes ) ->
328338 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
330340 let ! txHash = Generic. txHash tx
331341 disInOut <- liftIO $ getDisableInOutState syncEnv
332342 if not (Generic. txValidContract tx)
333343 then do
334344 ! txOutsGrouped <- mapM (prepareTxOut tracer cache iopts (txId, txHash)) (Generic. txOutputs tx)
335345
336- ! txIns <- mapM (prepareTxIn txId ( fst <$> groupedTxOut grouped) Map. empty) (ptrResolvedTxIn ptr)
346+ ! txIns <- mapM (prepareTxIn txId groups Map. empty) (ptrResolvedTxIn ptr)
337347 -- There is a custom semigroup instance for BlockGroupedData which uses addition for the values `fees` and `outSum`.
338348 -- Same happens bellow on last line of this function.
339349 pure (grouped <> BlockGroupedData txIns txOutsGrouped [] [] (ptrFees ptr) (ptrOutSum ptr))
@@ -346,7 +356,7 @@ insertTx syncEnv blkId isMember epochNo slotNo applyResult grouped (txId, tx, pt
346356 Map. fromList
347357 <$> whenFalseMempty
348358 (ioPlutusExtra iopts)
349- (mapM (insertRedeemer tracer disInOut ( fst <$> groupedTxOut grouped) txId) (Generic. txRedeemer tx))
359+ (mapM (insertRedeemer tracer disInOut groups txId) (Generic. txRedeemer tx))
350360
351361 when (ioPlutusExtra iopts) $ do
352362 mapM_ (insertDatum tracer cache txId) (Generic. txData tx)
@@ -390,13 +400,15 @@ insertTx syncEnv blkId isMember epochNo slotNo applyResult grouped (txId, tx, pt
390400 mapM_ (insertGovActionProposal cache blkId txId (getGovExpiresAt applyResult epochNo)) $ zip [0 .. ] (Generic. txProposalProcedure tx)
391401 mapM_ (insertVotingProcedures tracer cache txId) (Generic. txVotingProcedure tx)
392402
393- ! txIns <- mapM (prepareTxIn txId ( fst <$> groupedTxOut grouped) redeemers) (ptrResolvedTxIn ptr)
403+ ! txIns <- mapM (prepareTxIn txId groups redeemers) (ptrResolvedTxIn ptr)
394404 pure (grouped <> BlockGroupedData txIns txOutsGrouped txMetadata maTxMint (ptrFees ptr) (ptrOutSum ptr))
395405 where
396406 tracer = getTrace syncEnv
397407 cache = envCache syncEnv
398408 iopts = getInsertOptions syncEnv
399409
410+ groups = (fst <$> groupedTxOut grouped) : txOutPrev
411+
400412prepareTxOut ::
401413 (MonadBaseControl IO m , MonadIO m ) =>
402414 Trace IO Text ->
@@ -481,15 +493,15 @@ insertCollateralTxOut tracer cache iopts (txId, _txHash) (Generic.TxOut index ad
481493prepareTxIn ::
482494 Monad m =>
483495 DB. TxId ->
484- [ExtendedTxOut ] ->
496+ [[ ExtendedTxOut ] ] ->
485497 Map Word64 DB. RedeemerId ->
486498 (Generic. TxIn , Maybe DB. TxId , Either Generic. TxIn DB. TxOutId ) ->
487499 ExceptT SyncNodeError m ExtendedTxIn
488500prepareTxIn txInId groupedOutputs redeemers (txIn, mtxOutId, mTxOutId) = do
489501 txOutId <- liftLookupFail " resolveScriptHash" $
490502 case mtxOutId of
491503 Just txOutId -> pure $ Right txOutId
492- Nothing -> case resolveInMemory txIn groupedOutputs of
504+ Nothing -> case resolveInMemoryMany txIn groupedOutputs of
493505 Nothing -> pure $ Left $ DB. DbLookupTxHash (Generic. txInHash txIn)
494506 Just txOut -> pure $ Right $ DB. txOutTxId $ etoTxOut txOut
495507 let txInDB =
@@ -1138,7 +1150,7 @@ insertRedeemer ::
11381150 (MonadBaseControl IO m , MonadIO m ) =>
11391151 Trace IO Text ->
11401152 Bool ->
1141- [ExtendedTxOut ] ->
1153+ [[ ExtendedTxOut ] ] ->
11421154 DB. TxId ->
11431155 (Word64 , Generic. TxRedeemer ) ->
11441156 ExceptT SyncNodeError (ReaderT SqlBackend m ) (Word64 , DB. RedeemerId )
0 commit comments