Skip to content

Commit

Permalink
Remove rewinding from execLookupPactTxs. Make doLookupSuccessful use …
Browse files Browse the repository at this point in the history
…current blockheight
  • Loading branch information
Evgenii Akentev committed Aug 23, 2023
1 parent 4fc31a7 commit 0dc79b0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 28 deletions.
2 changes: 1 addition & 1 deletion bench/Chainweb/Pact/Backend/Bench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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]])
25 changes: 7 additions & 18 deletions src/Chainweb/Pact/Backend/RelationalCheckpointer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -299,36 +299,25 @@ 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
return $ HashMap.fromList (zip (V.toList hashes) r)
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
Expand Down
2 changes: 1 addition & 1 deletion src/Chainweb/Pact/Backend/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ::
Expand Down
20 changes: 13 additions & 7 deletions src/Chainweb/Pact/PactService.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
--
Expand Down
2 changes: 1 addition & 1 deletion src/Chainweb/Pact/PactService/ExecBlock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0dc79b0

Please sign in to comment.