Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

- Kupo now indexes Dijkstra blocks when it is connected directly to cardano-node. With Ogmios, Kupo stops with an error at the first Dijkstra block, because Ogmios does not serialise Dijkstra transactions yet.

- A Dijkstra transaction that carries sub-transactions is not indexed yet. Kupo stops with an error that names the transaction.
- Kupo now indexes Dijkstra sub-transactions when it is connected directly to cardano-node, instead of stopping at the first transaction that carries them. Kupo records the outputs that each sub-transaction creates and the inputs that it spends under the sub-transaction's own id, with the index of its top-level transaction in the block. A `*@<transaction id>` pattern matches the outputs of that transaction only, so the id of a top-level transaction does not match the outputs of its sub-transactions. When a top-level transaction fails phase-2 validation, its sub-transactions create and spend nothing.

- Each match returned by `/matches` now carries `sub_transaction_index` and `top_level_transaction_id`. For an output created by a Dijkstra sub-transaction, they give the position of the sub-transaction among those of its top-level transaction, starting at 0, and the id of that top-level transaction; `transaction_id` stays the id of the sub-transaction itself. Both are `null` for an output of a top-level transaction. The `spent_at` object of a match gains `top_level_transaction_id`: the id of the top-level transaction when a sub-transaction spent the output, `null` otherwise. Existing fields keep their meaning, and `transaction_index` is still the position of the top-level transaction in the block.

- `/matches` accepts a new `top_level_transaction_id` query filter. It returns the outputs created by the Dijkstra sub-transactions of that top-level transaction, and not the outputs of the top-level transaction itself, which `*@<transaction id>` and `transaction_id` still return. Like `transaction_id`, it cannot be combined with `policy_id`, `asset_name` or `output_index`, nor given twice, and `transaction_id` and `top_level_transaction_id` exclude each other; Kupo then answers with a 400 error. The `transaction_id` filter and the `*@<transaction id>` and `<output index>@<transaction id>` patterns accept the id of a sub-transaction.

- `/metadata/{slot}` includes the metadata of Dijkstra sub-transactions. Each entry gains `transaction_id`, the id of the transaction that carries the metadata, and `top_level_transaction_id`, the id of the top-level transaction for a sub-transaction and `null` otherwise. The metadata of the sub-transactions of a transaction comes first, in the order the ledger applies them, followed by the metadata of the transaction itself. The `transaction_id` filter accepts the id of a sub-transaction. The sub-transactions of a transaction that fails phase-2 validation have no entry.

- Kupo now records database migrations in a `migrations` table, and no longer runs heavy or breaking migrations silently at startup:
- When the database needs a blocking migration, Kupo refuses to start and exits with code 3. Run `kupo db migrate`, then start Kupo again.
Expand All @@ -27,6 +33,8 @@

- Fixed the input index and the redeemer reported in `spent_at` (`input_index` and `redeemer`) for transactions that spend two or more inputs. Since 2.10.0, Kupo numbered the inputs from the last one, so the index and the redeemer did not match the ledger. They now follow the ledger's input order. For the spends recorded before the upgrade, Kupo registers a data repair, and warns at every start until it is applied. If the index matches `*`, was synchronised from origin and was never pruned with `--prune-utxo`, run `kupo db migrate --confirm-complete-index` to apply it; otherwise, resynchronise.

- At startup, Kupo adds three empty columns to the `inputs` table of an existing database, to prepare the indexing of Dijkstra sub-transactions. The change is automatic and instant, and needs no action.

#### Removed

N/A
Expand Down
76 changes: 69 additions & 7 deletions db/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,62 @@
## Overview

<p align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="./schema-dark.png">
<img alt="Database schema." src="./schema-light.png">
</picture>
</p>
The diagram below is maintained by hand and must be updated with each migration.

```mermaid
erDiagram
inputs {
BLOB ext_output_reference PK
TEXT address
BLOB value
BLOB datum_info "nullable"
BLOB script_hash "nullable"
INTEGER created_at
INTEGER spent_at "nullable"
BLOB spent_by "nullable"
BLOB spent_with "nullable"
INTEGER sub_transaction_index "nullable"
BLOB top_level_transaction_id "nullable"
BLOB spent_by_top_level_transaction_id "nullable"
BLOB output_reference UK "virtual"
BLOB output_index "virtual"
BLOB transaction_index "virtual"
BLOB datum_hash "virtual, nullable"
TEXT payment_credential "virtual, nullable"
}
policies {
BLOB output_reference PK, FK
BLOB policy_id PK
}
checkpoints {
BLOB header_hash
INTEGER slot_no PK
}
binary_data {
BLOB binary_data_hash PK
BLOB binary_data
}
scripts {
BLOB script_hash PK
BLOB script
}
patterns {
TEXT pattern PK
}
migrations {
TEXT id PK
TEXT state
INTEGER cutoff_slot "nullable"
BLOB cursor "nullable"
TEXT kupo_version "nullable"
INTEGER registered_at
INTEGER applied_at "nullable"
}
inputs ||--o{ policies : "output_reference, ON DELETE CASCADE"
checkpoints ||..o{ inputs : "created_at, lookup, no FK"
checkpoints |o..o{ inputs : "spent_at, lookup, no FK"
binary_data |o..o{ inputs : "datum_hash, lookup, no FK"
scripts |o..o{ inputs : "script_hash, lookup, no FK"
```

<details>
<summary>See complete definition</summary>
Expand All @@ -21,19 +72,23 @@ CREATE TABLE `inputs` (
`spent_at` INTEGER,
`spent_by` BLOB,
`spent_with` BLOB,
`sub_transaction_index` INTEGER,
`top_level_transaction_id` BLOB,
`spent_by_top_level_transaction_id` BLOB,

`output_reference` BLOB NOT NULL GENERATED ALWAYS AS (substr(`ext_output_reference`, 1, 34)) VIRTUAL,
`output_index` BLOB NOT NULL GENERATED ALWAYS AS (substr(`ext_output_reference`, -4, 2)) VIRTUAL,
`transaction_index` BLOB NOT NULL GENERATED ALWAYS AS (substr(`ext_output_reference`, -2)) VIRTUAL,
`datum_hash` BLOB GENERATED ALWAYS AS (substr(`datum_info`, 2)) VIRTUAL,
`payment_credential` TEXT COLLATE NOCASE NOT NULL GENERATED ALWAYS AS (substr(`address`, -56)) VIRTUAL,
`payment_credential` TEXT COLLATE NOCASE GENERATED ALWAYS AS (substr(`address`, -56)) VIRTUAL,

PRIMARY KEY (`ext_output_reference`)
);

CREATE UNIQUE INDEX `inputsByOutputReference` ON `inputs` (`output_reference`);

CREATE INDEX `inputsByAddress` ON `inputs` (`address` COLLATE NOCASE);
CREATE INDEX `inputsByDatumHash` ON `inputs` (`datum_hash`);
CREATE INDEX `inputsByPaymentCredential` ON `inputs` (`payment_credential` COLLATE NOCASE);
CREATE INDEX `inputsByCreatedAt` ON `inputs` (`created_at`);
CREATE INDEX `inputsBySpentAt` ON `inputs` (`spent_at`);
Expand All @@ -48,6 +103,8 @@ CREATE TABLE `policies` (
ON DELETE CASCADE
);

CREATE INDEX `policiesByPolicyId` ON `policies` (`policy_id`);

CREATE TABLE `checkpoints` (
`header_hash` BLOB NOT NULL,
`slot_no` INTEGER PRIMARY KEY NOT NULL
Expand Down Expand Up @@ -116,6 +173,11 @@ Kupo refuses a database whose registry holds an id that it does not know, becaus
- On a database upgraded from `version=9` or older, which holds no such spends, record the repair as skipped instead.
- The repair needs an index that matches `*`, was synchronised from origin and was never pruned with `--prune-utxo`: an interrupted prune can leave only the last inputs of a transaction, which look like a complete set. Resynchronise any other index.

### Migration to `version=13`

- Add `sub_transaction_index` and `top_level_transaction_id` columns to `inputs`. For an output created by a Dijkstra sub-transaction, they hold the position of the sub-transaction among those of its top-level transaction, starting at 0, and the id of the top-level transaction; `ext_output_reference` then holds the id of the sub-transaction itself. Both are `NULL` for an output of a top-level transaction.
- Add a `spent_by_top_level_transaction_id` column to `inputs`. When a Dijkstra sub-transaction spends the output, it holds the id of the top-level transaction, and `spent_by` holds the id of the sub-transaction. It is `NULL` otherwise.

<p align="right"><code>v2.10.0</code></p>
<hr/>

Expand Down
Binary file removed db/schema-dark.png
Binary file not shown.
Binary file removed db/schema-light.png
Binary file not shown.
3 changes: 3 additions & 0 deletions db/v2.12.0/003.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE inputs ADD COLUMN sub_transaction_index INTEGER;
ALTER TABLE inputs ADD COLUMN top_level_transaction_id BLOB;
ALTER TABLE inputs ADD COLUMN spent_by_top_level_transaction_id BLOB;
65 changes: 63 additions & 2 deletions docs/api/nightly.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -894,10 +894,21 @@ components:
type: object
additionalProperties: false
required:
- transaction_id
- top_level_transaction_id
- hash
- schema
- raw
properties:
transaction_id:
$ref: "#/components/schemas/TransactionId"

top_level_transaction_id:
description: The id of the top-level transaction carrying the sub-transaction that holds this metadata. `null` when the metadata belongs to a top-level transaction.
oneOf:
- $ref: "#/components/schemas/TransactionId"
- type: "null"

hash:
type: string
description: A blake2b-256 hash digest of the raw serialized data
Expand Down Expand Up @@ -1201,6 +1212,7 @@ components:
- slot_no
- header_hash
- transaction_id
- top_level_transaction_id
- input_index
- redeemer
properties:
Expand All @@ -1212,6 +1224,11 @@ components:
oneOf:
- $ref: "#/components/schemas/TransactionId"
- type: "null"
top_level_transaction_id:
description: The id of the top-level transaction carrying the sub-transaction that spent this output. `null` unless the spending transaction is a sub-transaction, in which case `transaction_id` is the id of the sub-transaction itself.
oneOf:
- $ref: "#/components/schemas/TransactionId"
- type: "null"
input_index:
oneOf:
- $ref: "#/components/schemas/InputIndex"
Expand All @@ -1221,6 +1238,12 @@ components:
- $ref: "#/components/schemas/BinaryData"
- type: "null"

SubTransactionIndex:
type: integer
description: The position of a sub-transaction among the sub-transactions of its top-level transaction, starting at 0.
minimum: 0
example: 1

TransactionId:
type: string
description: A blake2b-256 hash digest of a transaction body.
Expand Down Expand Up @@ -1297,6 +1320,8 @@ components:
- transaction_index
- transaction_id
- output_index
- sub_transaction_index
- top_level_transaction_id
- address
- value
- datum_hash
Expand All @@ -1310,6 +1335,16 @@ components:
$ref: "#/components/schemas/TransactionId"
output_index:
$ref: "#/components/schemas/OutputIndex"
sub_transaction_index:
description: The position of the sub-transaction that created this output among the sub-transactions of its top-level transaction, starting at 0. `null` for an output of a top-level transaction.
oneOf:
- $ref: "#/components/schemas/SubTransactionIndex"
- type: "null"
top_level_transaction_id:
description: The id of the top-level transaction carrying the sub-transaction that created this output. `null` for an output of a top-level transaction. For an output of a sub-transaction, `transaction_id` is the id of the sub-transaction itself.
oneOf:
- $ref: "#/components/schemas/TransactionId"
- type: "null"
address:
$ref: "#/components/schemas/Address"
value:
Expand Down Expand Up @@ -1349,6 +1384,8 @@ components:
- transaction_index
- transaction_id
- output_index
- sub_transaction_index
- top_level_transaction_id
- address
- value
- datum_hash
Expand All @@ -1362,6 +1399,16 @@ components:
$ref: "#/components/schemas/TransactionId"
output_index:
$ref: "#/components/schemas/OutputIndex"
sub_transaction_index:
description: The position of the sub-transaction that created this output among the sub-transactions of its top-level transaction, starting at 0. `null` for an output of a top-level transaction.
oneOf:
- $ref: "#/components/schemas/SubTransactionIndex"
- type: "null"
top_level_transaction_id:
description: The id of the top-level transaction carrying the sub-transaction that created this output. `null` for an output of a top-level transaction. For an output of a sub-transaction, `transaction_id` is the id of the sub-transaction itself.
oneOf:
- $ref: "#/components/schemas/TransactionId"
- type: "null"
address:
$ref: "#/components/schemas/Address"
value:
Expand Down Expand Up @@ -1792,6 +1839,17 @@ components:
description: |
A query flag (i.e. `?strict`) to only look for checkpoints that strictly match the provided slot. The behavior otherwise is to look for the largest nearest slot smaller or equal to the one provided.

top-level-transaction-id:
name: top_level_transaction_id
in: query
required: false
schema:
$ref: "#/components/schemas/TransactionId"
description: |
Filters results by top-level transaction id to retrieve the outputs created by the sub-transactions of that transaction. Outputs created by the top-level transaction itself are not returned; match them with a `*@<transaction id>` pattern or the `transaction_id` filter.
Cannot be combined with `policy_id`, `asset_name`, `transaction_id` or `output_index`, nor given more than once; the server then answers with a 400 error.
Like the other query filters, it is applied after the pattern has selected its matches.

transaction-id: &transaction-id
name: transaction_id
in: query
Expand All @@ -1800,6 +1858,7 @@ components:
$ref: "#/components/schemas/TransactionId"
description: |
Filters results by transaction id to retrieve items originating from that transaction only.
The id of a Dijkstra sub-transaction is accepted too; it selects the outputs created by that sub-transaction.
Useful when combined with a pattern as path-parameter.

> <sup><strong>WARNING</strong></sup> <br/>
Expand Down Expand Up @@ -1877,6 +1936,7 @@ paths:
- $ref: "#/components/parameters/asset-name"
- $ref: "#/components/parameters/transaction-id"
- $ref: "#/components/parameters/output-index"
- $ref: "#/components/parameters/top-level-transaction-id"
responses:
200:
description: OK
Expand Down Expand Up @@ -1924,6 +1984,7 @@ paths:
- $ref: "#/components/parameters/asset-name"
- $ref: "#/components/parameters/transaction-id"
- $ref: "#/components/parameters/output-index"
- $ref: "#/components/parameters/top-level-transaction-id"
responses:
200:
description: OK
Expand Down Expand Up @@ -2251,11 +2312,11 @@ paths:
description: |
Retrieve all metadata data seen in a block at the given slot, possibly filtered by transaction id.

Metadata are ordered according to their respective transaction's order in the block.
Metadata are ordered according to their respective transaction's order in the block. The metadata of the sub-transactions of a Dijkstra transaction come first, in the order the ledger applies them, followed by the metadata of the transaction itself. Each entry names the transaction that carries the metadata. The sub-transactions of a transaction that fails phase-2 validation are not applied and have no entry.
parameters:
- <<: *transaction-id
description: |
Filters results by transaction id to retrieve items originating from that transaction only.
Filters results by transaction id to retrieve items originating from that transaction only. The id of a sub-transaction selects the metadata of that sub-transaction alone.
responses:
200:
description: OK
Expand Down
10 changes: 9 additions & 1 deletion kupo.cabal

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ library:
- bytestring
- cardano-crypto-class
- cardano-crypto-wrapper
- cardano-data
- cardano-ledger-allegra
- cardano-ledger-alonzo
- cardano-ledger-api
Expand Down Expand Up @@ -134,6 +133,10 @@ tests:
- base
- bytestring
- cardano-crypto-class
- cardano-ledger-alonzo
- cardano-ledger-core
- cardano-ledger-dijkstra
- cardano-protocol
- containers
- contra-tracer
- directory
Expand All @@ -145,8 +148,10 @@ tests:
- io-classes
- io-sim
- kupo
- lens
- lens-aeson
- openapi3
- ouroboros-consensus:cardano
- process
- QuickCheck
- quickcheck-state-machine
Expand Down
2 changes: 1 addition & 1 deletion src/Kupo/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ rollForwardAll tr inputManagement notifyTip Database{..} patterns blks = do
onSpentInputs
:: Tip
-> SlotNo
-> Map (TransactionId, SlotNo) [(Pattern, InputIndex, Maybe BinaryData)]
-> Map (TransactionId, Maybe TransactionId, SlotNo) [(Pattern, InputIndex, Maybe BinaryData)]
-> DBTransaction m Int
onSpentInputs = case inputManagement of
MarkSpentInputs ->
Expand Down
Loading
Loading