From 0dc79b093a1e1360401855673f0a6b0df9c542a4 Mon Sep 17 00:00:00 2001 From: Evgenii Akentev Date: Wed, 23 Aug 2023 14:38:47 +0400 Subject: [PATCH] Remove rewinding from execLookupPactTxs. Make doLookupSuccessful use current blockheight --- bench/Chainweb/Pact/Backend/Bench.hs | 2 +- .../Pact/Backend/RelationalCheckpointer.hs | 25 ++++++------------- src/Chainweb/Pact/Backend/Types.hs | 2 +- src/Chainweb/Pact/PactService.hs | 20 +++++++++------ src/Chainweb/Pact/PactService/ExecBlock.hs | 2 +- 5 files changed, 23 insertions(+), 28 deletions(-) diff --git a/bench/Chainweb/Pact/Backend/Bench.hs b/bench/Chainweb/Pact/Backend/Bench.hs index 9d8854e66f..e362969874 100644 --- a/bench/Chainweb/Pact/Backend/Bench.hs +++ b/bench/Chainweb/Pact/Backend/Bench.hs @@ -414,4 +414,4 @@ cpBenchLookupProcessedTx transactionCount cp = C.env (setup' cp) $ \ ~(ut) -> go Checkpointer{..} (NoopNFData _) = do _cpRestore (Just (BlockHeight 2, hash02)) >>= \case PactDbEnv' _ -> - _cpLookupProcessedTx Nothing (V.fromList [Pact.TypedHash "" | _ <- [1..transactionCount]]) + _cpLookupProcessedTx (BlockHeight 2) Nothing (V.fromList [Pact.TypedHash "" | _ <- [1..transactionCount]]) diff --git a/src/Chainweb/Pact/Backend/RelationalCheckpointer.hs b/src/Chainweb/Pact/Backend/RelationalCheckpointer.hs index a426d243cc..8a037aa1fd 100644 --- a/src/Chainweb/Pact/Backend/RelationalCheckpointer.hs +++ b/src/Chainweb/Pact/Backend/RelationalCheckpointer.hs @@ -299,26 +299,16 @@ doRegisterSuccessful :: Db logger -> PactHash -> IO () doRegisterSuccessful dbenv (TypedHash hash) = runBlockEnv dbenv (indexPactTransaction $ BS.fromShort hash) -doLookupSuccessful :: Db logger -> Maybe ConfirmationDepth -> V.Vector PactHash -> IO (HashMap.HashMap PactHash (T2 BlockHeight BlockHash)) -doLookupSuccessful dbenv confDepth hashes = runBlockEnv dbenv $ do +doLookupSuccessful :: Db logger -> BlockHeight -> Maybe ConfirmationDepth -> V.Vector PactHash -> IO (HashMap.HashMap PactHash (T2 BlockHeight BlockHash)) +doLookupSuccessful dbenv (BlockHeight currentBh) confDepth hashes = runBlockEnv dbenv $ do withSavepoint DbTransaction $ do r <- callDb "doLookupSuccessful" $ \db -> do - let - currentHeightQ = "SELECT blockheight FROM BlockHistory \ - \ ORDER BY blockheight DESC LIMIT 1" - -- if there is a confirmation depth, we get the current height and calculate -- the block height, to look for the transactions in range [0, current block height - confirmation depth] - blockheight <- case confDepth of - Nothing -> pure Nothing - Just (ConfirmationDepth cd) -> do - currentHeight <- qry_ db currentHeightQ [RInt] - case currentHeight of - [[SInt bh]] -> pure $ Just (bh - fromIntegral cd) - _ -> fail "impossible" - let - blockheightval = maybe [] (\bh -> [SInt bh]) blockheight + blockheightval = case confDepth of + Nothing -> [SInt $ fromIntegral currentBh] + Just (ConfirmationDepth cd) -> [SInt $ fromIntegral $ currentBh - cd] qvals = [ SBlob (BS.fromShort hash) | (TypedHash hash) <- V.toList hashes ] ++ blockheightval qry db qtext qvals [RInt, RBlob] >>= mapM go @@ -326,9 +316,8 @@ doLookupSuccessful dbenv confDepth hashes = runBlockEnv dbenv $ do where qtext = "SELECT blockheight, hash FROM \ \TransactionIndex INNER JOIN BlockHistory \ - \USING (blockheight) WHERE txhash IN (" <> hashesParams <> ")" - <> maybe "" (const " AND blockheight <= ?") confDepth - <> ";" + \USING (blockheight) WHERE txhash IN (" <> hashesParams <> ") \ + \AND blockheight <= ?;" hashesParams = Utf8 $ intercalate "," [ "?" | _ <- V.toList hashes] go ((SInt h):(SBlob blob):_) = do diff --git a/src/Chainweb/Pact/Backend/Types.hs b/src/Chainweb/Pact/Backend/Types.hs index 4aa790d836..e31336d3d9 100644 --- a/src/Chainweb/Pact/Backend/Types.hs +++ b/src/Chainweb/Pact/Backend/Types.hs @@ -312,7 +312,7 @@ data Checkpointer logger = Checkpointer , _cpRegisterProcessedTx :: !(P.PactHash -> IO ()) , _cpLookupProcessedTx :: - !(Maybe ConfirmationDepth -> Vector P.PactHash -> IO (HashMap P.PactHash (T2 BlockHeight BlockHash))) + !(BlockHeight -> Maybe ConfirmationDepth -> Vector P.PactHash -> IO (HashMap P.PactHash (T2 BlockHeight BlockHash))) , _cpGetBlockHistory :: !(BlockHeader -> Domain RowKey RowData -> IO BlockTxHistory) , _cpGetHistoricalLookup :: diff --git a/src/Chainweb/Pact/PactService.hs b/src/Chainweb/Pact/PactService.hs index b5eed2b2e4..a450784a41 100644 --- a/src/Chainweb/Pact/PactService.hs +++ b/src/Chainweb/Pact/PactService.hs @@ -163,7 +163,12 @@ withPactService ver cid chainwebLogger bhDb pdb sqlenv config act = , _psBlockGasLimit = _pactBlockGasLimit config , _psChainId = cid } - !pst = PactServiceState Nothing mempty initialParentHeader P.noSPVSupport + !pst = PactServiceState + { _psStateValidated = Nothing + , _psInitCache = mempty + , _psParentHeader = initialParentHeader + , _psSpvSupport = P.noSPVSupport + } runPactServiceM pst pse $ do -- If the latest header that is stored in the checkpointer was on an @@ -867,12 +872,13 @@ execLookupPactTxs execLookupPactTxs restorePoint confDepth txs = pactLabel "execLookupPactTxs" $ do if V.null txs then return mempty else go where - go = getCheckpointer >>= \(!cp) -> case restorePoint of - NoRewind _ -> - liftIO $! _cpLookupProcessedTx cp confDepth txs - DoRewind parent -> withDiscardedBatch $ do - withCheckpointerRewind Nothing (Just $ ParentHeader parent) "lookupPactTxs" $ \_ -> - liftIO $ Discard <$> _cpLookupProcessedTx cp confDepth txs + go = getCheckpointer >>= \(!cp) -> do + currHeight <- case restorePoint of + NoRewind _ -> do + parent <- use psParentHeader + pure $ _blockHeight $ _parentHeader parent + DoRewind parent -> pure $ _blockHeight parent + liftIO $! _cpLookupProcessedTx cp currHeight confDepth txs -- | Modified table gas module with free module loads -- diff --git a/src/Chainweb/Pact/PactService/ExecBlock.hs b/src/Chainweb/Pact/PactService/ExecBlock.hs index e3600ca427..6fafcab0a9 100644 --- a/src/Chainweb/Pact/PactService/ExecBlock.hs +++ b/src/Chainweb/Pact/PactService/ExecBlock.hs @@ -226,7 +226,7 @@ validateChainwebTxs logger v cid cp txValidationTime bh txs doBuyGas checkUnique :: ChainwebTransaction -> IO (Either InsertError ChainwebTransaction) checkUnique t = do - found <- HashMap.lookup (P._cmdHash t) <$> _cpLookupProcessedTx cp Nothing (V.singleton $ P._cmdHash t) + found <- HashMap.lookup (P._cmdHash t) <$> _cpLookupProcessedTx cp bh Nothing (V.singleton $ P._cmdHash t) case found of Nothing -> pure $ Right t Just _ -> pure $ Left InsertErrorDuplicate