Skip to content

Commit

Permalink
Retreive continuation history recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
enobayram committed Mar 3, 2023
1 parent ed7f8b0 commit 26c7d82
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions haskell-src/chainweb-data.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ library
, optparse-applicative >=0.14 && <0.17
, servant-client
, servant-openapi3
, vector
, yet-another-logger

if flag(ghc-flags)
Expand Down
35 changes: 31 additions & 4 deletions haskell-src/lib/ChainwebDb/Queries.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -97,18 +98,44 @@ 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 ->
Q Postgres ChainwebDataDb s (FilterMarked DbTxSummaryT (PgExpr s))
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 }
Expand Down

0 comments on commit 26c7d82

Please sign in to comment.