Skip to content

Index Dijkstra sub-transactions - #19

Merged
carbolymer merged 10 commits into
masterfrom
mgalazyn/feature/sub-transactions
Nov 23, 2026
Merged

carbolymer merged 10 commits into
masterfrom
mgalazyn/feature/sub-transactions

Conversation

@carbolymer

@carbolymer carbolymer commented Nov 16, 2026 •

Copy link
Copy Markdown

Part of #12.

Summary

Kupo refused Dijkstra blocks that contain sub-transactions (CIP-118). When it is connected directly to cardano-node, it now indexes the outputs and spends of each sub-transaction, and reports them through /matches and /metadata. The Ogmios path still refuses such blocks, see #21, which is blocked by IntersectMBO/ogmios#21.

Stack

This PR is based on #23 (cardano-config layout of the node configuration), which is on #18 (CHaP bump), which is on master. The base branch is mgalazyn/feature/cardano-config-envelope. The 10 commits on top of it are new in this PR.

Changes

One bullet per commit, in order.

  • Store sub-transactions in new columns of the inputs table: migration v2.12.0/003 adds three nullable columns to inputs (sub_transaction_index, top_level_transaction_id, spent_by_top_level_transaction_id). It is automatic and instant, existing rows keep NULL, and no resync is needed. IsBlock gains subTransactions and discardedSubTransactions, matchBlock fills the columns, and the database layer writes and reads them. No block lists sub-transactions yet, so the index does not change.
  • Order inputs by sub-transaction within a transaction: within one transaction, the outputs of its sub-transactions come before those of the top-level transaction, by sub-transaction index, in both directions. This is the order in which the ledger applies them.
  • Read the sub-transactions of Dijkstra transactions: Transaction gains SubTransactionDijkstra, and the Block instance lists the sub-transactions of a phase-2 valid transaction in ledger order, with their inputs, outputs, datums, scripts, metadata and spend redeemers. A phase-2 invalid parent lists none as applied, so its sub-transactions create and spend nothing and add no metadata, but it lists them as discarded, so their datums and scripts are still collected, as for an invalid top-level transaction. Kupo still refuses such blocks at this commit.
  • Index Dijkstra sub-transactions instead of refusing them: blocks from cardano-node that carry sub-transactions no longer stop Kupo. The refusal stays for Dijkstra blocks from Ogmios. The library no longer uses cardano-data.
  • Report the sub-transaction of a match in the matches API: each match gains sub_transaction_index and top_level_transaction_id, null for outputs of top-level transactions. spent_at gains top_level_transaction_id, null unless a sub-transaction spent the output (spent_at itself is null for an unspent output).
  • Filter matches by the top-level transaction of their sub-transaction: GET /matches?top_level_transaction_id=<T> returns only the outputs created by the sub-transactions of T, not T's own. Together with policy_id, asset_name, transaction_id or output_index, or when given twice, it is rejected with 400 invalidMatchFilter. Tests also cover the transaction_id filter and the transaction id patterns with the id of a sub-transaction.
  • Serve the metadata of sub-transactions from /metadata: entries gain transaction_id and top_level_transaction_id (null for top-level transactions). The metadata of the sub-transactions of a transaction comes before that of the transaction itself. The transaction_id filter accepts the id of a sub-transaction.
  • Document the sub-transaction columns of the inputs table: the new columns are described in db/README.md.
  • Test Dijkstra blocks with sub-transactions through the block instance: the tests build a real Dijkstra Block, with a header signed by keys derived from fixed seeds, and check its point, its fold over the top-level transactions, the sub-transactions it lists, that it passes checkSubTransactions, and what matchBlock reads from it.
  • Draw the database schema as a Mermaid diagram: a Mermaid erDiagram in db/README.md replaces the two schema PNGs, which were drawn by hand and no longer matched the schema. It also adds the inputsByDatumHash and policiesByPolicyId indexes and drops a NOT NULL on payment_credential that the migration never declared.

The API documentation (docs/api/nightly.yaml) and the changelog describe the new fields and the filter.

Remaining work

  • Ledger extraction: yield sub-transactions from Dijkstra blocks. The sub-transactions of a phase-2-invalid parent create and spend nothing, but their datums and scripts are collected.
  • Matching and ingestion that fill the new columns.
  • /matches JSON fields and a top_level_transaction_id filter.
  • /metadata entries with transaction_id and top_level_transaction_id.
  • API docs.
  • End-to-end run against a Dijkstra devnet, run by hand on a cardano-testnet 11.2.0 devnet (see Verification), tracked in Get Kupo ready for the Dijkstra hard fork #13.
  • A test that decodes a real Dijkstra block from CBOR. No fixture exists yet.
  • Database round trip and HTTP tests through the real Dijkstra block type; they use a test-only block type today.
  • The Ogmios path, tracked in Index Dijkstra sub-transactions on the Ogmios path #21.

The unsupported Postgres backend is out of scope.

Verification

At the tip, the builds of kupo and kupo-tests pass with no GHC warnings, and the unit tests run 448 examples with 0 failures. After the rebase onto master, only the tip was rebuilt and tested.

I also ran Kupo at the tip of this branch by hand against a Dijkstra devnet made with cardano-testnet from the cardano-node 11.2.0 release (cardano-cli 11.6.0.0), raised to protocol version 12 by governance votes. I submitted a transaction with one sub-transaction, built with the cardano-cli dijkstra commands, with metadata on both the sub-transaction and its parent, and it landed in a block. Kupo followed the chain past that block to the tip with no errors. /matches gave the sub-transaction's output a sub_transaction_index of 0 and the parent id in top_level_transaction_id, and both fields are null for the parent's output. The top_level_transaction_id filter returned only the sub-transaction's output. spent_at.top_level_transaction_id is the parent id for the output the sub-transaction spent and null for the one the parent spent. With order=oldest_first the sub-transaction's output comes before the parent's, and /metadata for that slot lists the sub-transaction's entry first, with the parent id, then the parent's, with null. The node configuration was flattened with jq first, as described in #22. A phase-2-invalid parent with sub-transactions was not covered on the devnet.

@carbolymer
carbolymer added this pull request to stack #20 November 16, 2026 09:28
@carbolymer carbolymer self-assigned this Nov 16, 2026
@carbolymer carbolymer changed the title Mgalazyn/feature/sub transactions Index Dijkstra sub-transactions Nov 16, 2026
@carbolymer
carbolymer force-pushed the mgalazyn/feature/sub-transactions branch 2 times, most recently from 0588cfa to 766bd93 Compare November 16, 2026 13:11
@carbolymer
carbolymer force-pushed the mgalazyn/feature/bump-chap-deps branch from 2892161 to bd9a974 Compare November 16, 2026 14:01
@carbolymer
carbolymer force-pushed the mgalazyn/feature/sub-transactions branch from 766bd93 to 6da14c2 Compare November 16, 2026 14:01
@carbolymer
carbolymer removed this pull request from stack #20 November 16, 2026 14:03
@carbolymer
carbolymer changed the base branch from mgalazyn/feature/bump-chap-deps to mgalazyn/feature/cardano-config-envelope November 16, 2026 14:06
@carbolymer
carbolymer marked this pull request as ready for review November 17, 2026 14:10
Comment thread src/Kupo/Data/Pattern.hs Outdated
let pt = getPoint blk in foldBlock (fnBatch pt) mempty blk
where
-- Sub-transactions share the transaction index of their top-level transaction.
-- Of the discarded ones, only the datums and scripts are collected.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also explain why we collect these datums and scripts. A phase-2-invalid transaction is still included on-chain, so it can reveal the datum or script behind a hash even though its normal inputs are not spent and its normal outputs are not created. Those bytes remain useful for lookup by hash. The same rationale applies to discarded sub-transactions; documenting it here would make this behavior easier to understand.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: the comment now says that a discarded (phase-2 invalid) transaction or sub-transaction still puts its datums and scripts on chain, so only those are collected, for lookup by hash.

Comment thread src/Kupo/Data/Cardano.hs
Ledger.Phase2Invalid ->
[]
SubTransactionDijkstra{} ->
[]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment explaining why this returns an empty list: CIP-118 forbids sub-transactions from containing further sub-transactions, so this case has no children to return. For example: -- Sub-transactions cannot themselves contain sub-transactions.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

[spent0, spent1, subOutput0, subOutput1, output] <- generate (vectorOf 5 genOutput)
let funding = mkTransaction [fundingInput] [spent0, spent1]
let sub0 = mkTransaction [mkOutputReference (topLevelId funding) 0] [subOutput0]
let sub1 = mkTransaction [mkOutputReference (topLevelId funding) 1, subInput] [subOutput1]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extend the regression coverage with two cases:

  • One sub-transaction creates an output that a later sub-transaction or the parent spends. Both sub-transactions here spend outputs from an earlier block, so this does not yet exercise spending within a batch.
  • An output is spent by a sub-transaction, that block is rolled back, and an ordinary transaction then spends the output. Check that /matches reports the replacement spender and spent_at.top_level_transaction_id is null.

I did not find a failure in these paths, but these tests would give useful coverage of the new behavior.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added both:

  • spends the outputs that sub-transactions create within the same block: one sub-transaction creates an output, a later one spends it and creates another, and the parent spends that. All three outputs are indexed, and each spend reports its spender and top-level transaction id.
  • reports an ordinary spender after a spending sub-transaction is rolled back: after the rollback, /matches reports the ordinary transaction as spender, with spent_at.top_level_transaction_id null.

Neither found a failure.

@carbolymer
carbolymer force-pushed the mgalazyn/feature/cardano-config-envelope branch from 3058ac6 to 6b5b1d3 Compare November 23, 2026 19:47
@carbolymer
carbolymer force-pushed the mgalazyn/feature/sub-transactions branch 4 times, most recently from ecd67c8 to a158f69 Compare November 23, 2026 20:51
@carbolymer
carbolymer force-pushed the mgalazyn/feature/cardano-config-envelope branch from 6b5b1d3 to b4445d5 Compare November 23, 2026 20:51
Base automatically changed from mgalazyn/feature/cardano-config-envelope to master November 23, 2026 21:02
The inputs table gains three nullable columns. Top-level transactions
leave all three empty.

IsBlock gains subTransactions, which lists the sub-transactions of a
top-level transaction, and discardedSubTransactions, which lists those
that the ledger discards with a phase-2 invalid one, both none by
default. matchBlock matches the former with the transaction index of
their top-level transaction, records the top-level transaction id and
the sub-transaction position on their outputs, and the top-level
transaction id on their spends. Of the latter, it collects only the
datums and scripts, as it does for a phase-2 invalid top-level
transaction. The database layer writes and reads the three
sub-transaction columns.

No block lists sub-transactions yet, so the index does not change.
Outputs of sub-transactions come before those of the top-level transaction, by sub-transaction index, in both directions.
Transaction gains SubTransactionDijkstra. The Block instance of IsBlock
lists the sub-transactions of a phase-2 valid Dijkstra transaction in
ledger order, and reads their inputs, outputs, datums, scripts,
metadata and spend redeemers from their own bodies and witness sets. A
phase-2 invalid transaction lists none, because the ledger then applies
none of them; it lists them as discarded instead, so that their datums
and scripts are still collected.

Kupo still refuses blocks that carry sub-transactions, so the index
does not change.
Blocks from cardano-node that carry sub-transactions no longer stop
Kupo: it indexes the outputs and spends of each sub-transaction. The
refusal stays for Dijkstra blocks from Ogmios, whose transactions Kupo
cannot inspect.

Without the refusal, the library no longer uses cardano-data.
Each match gains sub_transaction_index and top_level_transaction_id, null
for outputs of top-level transactions, and spent_at gains
top_level_transaction_id, null unless a sub-transaction spent the output.
GET /matches accepts top_level_transaction_id, which returns the outputs
created by the sub-transactions of that transaction and not its own. The
filter excludes policy_id, transaction_id and output_index. Tests also
cover the transaction_id filter and the transaction id patterns with the
id of a sub-transaction.
/metadata/{slot} lists the metadata of the sub-transactions of a Dijkstra
transaction before that of the transaction itself, and names the
transaction of each entry with transaction_id and top_level_transaction_id.
The transaction_id filter accepts the id of a sub-transaction.
The sub-transaction tests read Dijkstra transactions through a test-only
block type. These tests build a real Dijkstra Block, with a header signed
by keys derived from fixed seeds, and check its point, its fold over the
top-level transactions, the sub-transactions it lists, that it passes
checkSubTransactions, and what matchBlock reads from it.
The schema images were drawn by hand, no script produced them, and they
no longer matched the schema. A Mermaid erDiagram in db/README.md
replaces them; GitHub renders it in light and dark themes. It shows the
tables after all migrations, their primary keys, the foreign key from
policies to inputs, and the lookups that queries make by value.

The complete definition also gains the inputsByDatumHash and
policiesByPolicyId indexes that Kupo creates, and no longer declares
payment_credential NOT NULL, which its migration never did.
@carbolymer
carbolymer force-pushed the mgalazyn/feature/sub-transactions branch from a158f69 to eb4be63 Compare November 23, 2026 21:04
@carbolymer
carbolymer merged commit e9ef0be into master Nov 23, 2026
7 checks passed
@carbolymer
carbolymer deleted the mgalazyn/feature/sub-transactions branch November 23, 2026 21:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants