@@ -26,9 +26,10 @@ import PSR.Events.Interface (
2626import PSR.Metrics qualified as Metrics
2727import PSR.Storage.SQLite.Instances ()
2828import PSR.Storage.SQLite.Utils
29+ import PSR.Types (BlockStatus )
2930
30- getEvents :: Metrics. Summary -> Pool Connection -> EventFilterParams -> IO [Event ]
31- getEvents getEvents_select pool EventFilterParams {.. } =
31+ getEvents :: Metrics. Summary -> Pool Connection -> Int -> EventFilterParams -> IO [Event ]
32+ getEvents getEvents_select pool confirmationDepth EventFilterParams {.. } =
3233 withResource pool $ \ conn -> withTransaction conn $ do
3334 let
3435 -- see `docs/specification.md` for default values
@@ -80,15 +81,20 @@ getEvents getEvents_select pool EventFilterParams{..} =
8081 " SELECT b.slot_no, b.hash, b.block_no, \
8182 \ CASE \
8283 \ WHEN ec.block_hash IS NOT NULL THEN 'execution' \
83- \ WHEN c .block_hash IS NOT NULL THEN 'rollback' \
84+ \ WHEN re .block_hash IS NOT NULL THEN 'rollback' \
8485 \ WHEN s.block_hash IS NOT NULL THEN 'selection' \
8586 \ END, \
86- \ COALESCE(ee.created_at, c .created_at, s.created_at), \
87+ \ COALESCE(ee.created_at, re .created_at, s.created_at), \
8788 \ json(ee.trace_logs), \
88- \ c.blocks_cancelled , \
89+ \ rb.block_hashes , \
8990 \ ee.eval_error, \
9091 \ ee.exec_budget_cpu, \
9192 \ ee.exec_budget_mem, \
93+ \ CASE \
94+ \ WHEN rb.block_hashes IS NOT NULL THEN 'cancelled' \
95+ \ WHEN b_max.max_slot_no IS NOT NULL AND (b_max.max_slot_no - :confirmation_depth) > b.slot_no THEN 'committed' \
96+ \ ELSE 'unknown'\
97+ \ END,\
9298 \ ec.transaction_hash, \
9399 \ ec.target_script_hash, \
94100 \ ec.target_script_name, \
@@ -103,20 +109,28 @@ getEvents getEvents_select pool EventFilterParams{..} =
103109 \ ec.exec_budget_max_mem, \
104110 \ cmp.params \
105111 \ FROM block b \
106- \ LEFT JOIN execution_context ec ON ec.block_hash = b.hash \
107- \ LEFT JOIN execution_event ee ON ee.context_id = ec.context_id \
112+ \ LEFT JOIN execution_context ec ON ec.block_hash = b.hash \
113+ \ LEFT JOIN execution_event ee ON ee.context_id = ec.context_id \
108114 \ LEFT JOIN cost_model_params cmp ON cmp.params_id = ec.cost_model_params_id \
109- \ LEFT JOIN rollback_event c ON c.block_hash = b.hash \
110- \ LEFT JOIN selection_event s ON s.block_hash = b.hash "
115+ \ LEFT JOIN rollback_event re ON re.block_hash = b.hash \
116+ \ LEFT JOIN \
117+ \(SELECT event_id, string_agg(block_hash, ' ') AS block_hashes FROM rollback_block) \
118+ \ rb ON re.event_id = rb.event_id \
119+ \ LEFT JOIN selection_event s ON s.block_hash = b.hash \
120+ \ JOIN (SELECT max(slot_no) as max_slot_no from block) b_max"
111121 <> whereQuery
112- <> " ORDER BY COALESCE(ee.created_at, c .created_at, s.created_at) ASC \
122+ <> " ORDER BY COALESCE(ee.created_at, re .created_at, s.created_at) ASC \
113123 \ LIMIT :limit \
114124 \ OFFSET :offset"
115125
116- parameters = whereParams <> [" :limit" := limitParameter, " :offset" := offsetParameter]
117-
118- rows <- queryNamed getEvents_select conn eventsQuery parameters
119- pure $ rowToEvent <$> rows
126+ parameters =
127+ whereParams
128+ <> [ " :limit" := limitParameter
129+ , " :offset" := offsetParameter
130+ , " :confirmation_depth" := confirmationDepth
131+ ]
132+ fmap rowToEvent
133+ <$> queryNamed getEvents_select conn eventsQuery parameters
120134 where
121135 rowToEvent ::
122136 ( ( SlotNo
@@ -129,6 +143,7 @@ getEvents getEvents_select pool EventFilterParams{..} =
129143 , Maybe EvalError
130144 , Maybe Integer
131145 , Maybe Integer
146+ , BlockStatus
132147 )
133148 :. Maybe ExecutionContext
134149 ) ->
@@ -144,6 +159,7 @@ getEvents getEvents_select pool EventFilterParams{..} =
144159 , evalError
145160 , mExBudgetCpu
146161 , mExBudgetMem
162+ , blockStatus
147163 )
148164 :. mExecutionContext
149165 ) =
@@ -161,7 +177,7 @@ getEvents getEvents_select pool EventFilterParams{..} =
161177 context <- mExecutionContext
162178 blockNo <- mBlockNo
163179 pure $ ExecutionPayload blockNo $ ExecutionEventPayload {.. }
164- Rollback -> pure $ RollbackPayload (maybe [] id blocksCancelled)
180+ Rollback -> pure $ RollbackPayload (fromMaybe [] blocksCancelled)
165181 Selection -> SelectionPayload <$> mBlockNo
166182 in
167183 Event {.. }
0 commit comments