From 26c7d8227d2683ae96dd4401b3545c1bef2bac89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enis=20Bayramo=C4=9Flu?= Date: Fri, 3 Mar 2023 12:21:01 +0100 Subject: [PATCH] Retreive continuation history recursively --- haskell-src/chainweb-data.cabal | 1 + haskell-src/lib/ChainwebDb/Queries.hs | 35 ++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/haskell-src/chainweb-data.cabal b/haskell-src/chainweb-data.cabal index 5fd77e64..8efde53b 100644 --- a/haskell-src/chainweb-data.cabal +++ b/haskell-src/chainweb-data.cabal @@ -87,6 +87,7 @@ library , optparse-applicative >=0.14 && <0.17 , servant-client , servant-openapi3 + , vector , yet-another-logger if flag(ghc-flags) diff --git a/haskell-src/lib/ChainwebDb/Queries.hs b/haskell-src/lib/ChainwebDb/Queries.hs index ed687450..54afb44d 100644 --- a/haskell-src/lib/ChainwebDb/Queries.hs +++ b/haskell-src/lib/ChainwebDb/Queries.hs @@ -22,6 +22,7 @@ import Data.Functor ((<&>)) import Data.Maybe (maybeToList) import Data.Text (Text) import Data.Time +import Data.Vector (Vector) import Database.Beam hiding (insert) import Database.Beam.Postgres import Database.Beam.Postgres.Syntax @@ -97,6 +98,33 @@ toDbTxSummary Transaction{..} = DbTxSummary , dtsGoodResult = _tx_goodResult } +data ContinuationHistoryF f = ContinuationHistory + { chCode :: C f (Maybe Text) + , chSteps :: C f (Vector Text) + } deriving (Generic, Beamable) + +type ContinuationHistory = ContinuationHistoryF Identity + +deriving instance Show ContinuationHistory + +joinContinuationHistory :: PgExpr s (Maybe (DbHash TxHash)) -> + Q Postgres ChainwebDataDb s (ContinuationHistoryF (PgExpr s)) +joinContinuationHistory pactIdExp = pgUnnest $ (customExpr_ $ \pactId -> + "LATERAL ( " <> + "WITH RECURSIVE transactionSteps AS ( " <> + "SELECT DISTINCT ON (depth) tInner.code, tInner.pactid, 1 AS depth, tInner.requestkey " <> + "FROM transactions AS tInner " <> + "WHERE (tInner.requestkey) = " <> pactId <> + "UNION ALL " <> + "SELECT DISTINCT ON (depth) tInner.code, tInner.pactid, tRec.depth + 1, tInner.requestkey " <> + "FROM transactions AS tInner " <> + "INNER JOIN transactionSteps AS tRec ON tRec.pactid = tInner.requestkey " <> + ")" <> + "SELECT (array_agg(code) FILTER (WHERE code IS NOT NULL))[1] as code " <> + ", array_agg(requestkey ORDER BY depth) as steps " <> + "FROM transactionSteps " <> + ")") pactIdExp + txSearchSource :: Text -> HeightRangeParams -> @@ -104,11 +132,10 @@ txSearchSource :: txSearchSource search hgtRange = do txMerged <- do tx <- all_ $ _cddb_transactions database - initTx <- leftJoin_' (all_ $ _cddb_transactions database) $ \tx2 -> - just_ (_tx_requestKey tx2) ==?. _tx_pactId tx + contHist <- joinContinuationHistory (_tx_pactId tx) let codeMerged = coalesce_ - [ just_ (_tx_code tx) - , _tx_code initTx + [ just_ $ _tx_code tx + , just_ $ chCode contHist ] nothing_ return $ tx { _tx_code = codeMerged }