Skip to content
Open
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
4 changes: 3 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
[submodule "config"]
path = config
url = https://github.com/input-output-hk/cardano-configurations.git
# TODO: pinned to the head of https://github.com/IntersectMBO/ogmios/pull/45 (guard clause origin);
# move it to the merge commit on master once the PR merges.
[submodule "test/ogmios"]
path = test/vectors/ogmios
url = https://github.com/cardanosolutions/ogmios.git
url = https://github.com/IntersectMBO/ogmios.git
[submodule "test/vectors/hydra"]
path = test/vectors/hydra
url = https://github.com/input-output-hk/hydra.git
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

#### Added

- 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.
- Kupo now indexes Dijkstra blocks, both when it is connected directly to cardano-node and through Ogmios. Through Ogmios, it needs an Ogmios that serialises Dijkstra transactions in full (https://github.com/IntersectMBO/ogmios/pull/22 and https://github.com/IntersectMBO/ogmios/pull/24); with an older Ogmios, Kupo stops with an error at the first Dijkstra block that holds transactions. Through Ogmios, Kupo also stops at a native script with a guard clause when Ogmios does not say whether the guard is a key or a script: Kupo reads that from the clause's `origin` field, or from the script's CBOR when Ogmios runs with `--include-script-cbor`.

- 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.
- Kupo now indexes Dijkstra sub-transactions, 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.

Expand Down Expand Up @@ -35,6 +35,8 @@

- Kupo now returns datums and redeemers from Babbage and Conway transaction witnesses with their on-chain bytes, as it already does for inline datums and other eras. Earlier versions returned a re-encoding of the same value, which cannot be used as-is to spend an output locked by that datum's hash. Databases built by earlier versions keep the re-encoded bytes.

- Through an Ogmios that runs with `--include-script-cbor`, Kupo now stores native scripts with their on-chain bytes instead of a re-encoding of their JSON.

- Kupo now limits the statistics scan of SQLite's `PRAGMA optimize`, which it runs at start-up, after `kupo copy` and on every garbage collection pass. On large databases the unbounded scan could take hours and stall chain sync. See https://github.com/CardanoSolutions/kupo/issues/209.

#### Removed
Expand Down
6 changes: 3 additions & 3 deletions src/Kupo/Data/Cardano.hs
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,15 @@ class HasTransactionId (BlockBody block) => IsBlock (block :: Type) where
-- | Raised on a Dijkstra block whose sub-transactions Kupo cannot index.
data UnsupportedSubTransactions
= UnverifiableSubTransactions !Point
-- ^ A Dijkstra block whose transactions Kupo cannot inspect.
-- ^ A Dijkstra block whose transactions Ogmios sent without their bodies.
deriving (Show)

instance Exception UnsupportedSubTransactions where
displayException = \case
UnverifiableSubTransactions point ->
"Ogmios does not serialise Dijkstra transactions yet, so Kupo cannot index the block at slot "
"This Ogmios does not serialise Dijkstra transactions in full, so Kupo cannot index the block at slot "
<> toString (slotNoToText (getPointSlotNo point))
<> ". Connect Kupo directly to cardano-node to index Dijkstra blocks."
<> ". Upgrade Ogmios, or connect Kupo directly to cardano-node."

instance IsBlock Void where
type BlockBody Void = Void
Expand Down
1 change: 1 addition & 0 deletions src/Kupo/Data/Cardano/NativeScript.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Kupo.Data.Cardano.NativeScript
, pattern Ledger.Shelley.RequireMOf
, pattern Ledger.Allegra.RequireTimeExpire
, pattern Ledger.Allegra.RequireTimeStart
, pattern Ledger.Dijkstra.RequireGuard
, KeyHash(..)
) where

Expand Down
6 changes: 5 additions & 1 deletion src/Kupo/Data/Hydra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ mkHydraBlock number txs = do
, PartialBlock
{ blockPoint = BlockPoint slotNo headerHash
, blockBody = toList txs
, isDijkstraBlock = False
, hasTransactionStubs = False
}
)

Expand Down Expand Up @@ -179,6 +179,8 @@ decodeGenesisTxForUTxO id indexOutputs = do
, datums = mempty
, scripts = mempty
, metadata = Nothing
, appliedSubTransactions = []
, unappliedSubTransactions = []
}

decodePartialTransaction :: Json.Value -> Json.Parser PartialTransaction
Expand Down Expand Up @@ -225,6 +227,8 @@ decodePartialTransaction = Json.withObject "PartialTransaction" $ \o -> do
(unRedeemers $ wits' ^. rdmrsTxWitsL)
, scripts = Map.map fromConwayScript (wits' ^. scriptTxWitsL)
, metadata = Nothing
, appliedSubTransactions = []
, unappliedSubTransactions = []
}

decodeInput
Expand Down
64 changes: 48 additions & 16 deletions src/Kupo/Data/Ogmios.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import Kupo.Data.Cardano
, pattern GenesisTip
, pattern RequireAllOf
, pattern RequireAnyOf
, pattern RequireGuard
, pattern RequireMOf
, pattern RequireSignature
, pattern RequireTimeExpire
Expand Down Expand Up @@ -97,6 +98,8 @@ import Ouroboros.Consensus.HardFork.Combinator
( OneEraHash (..)
)

import qualified Cardano.Ledger.Credential as Ledger
import qualified Cardano.Ledger.Hashes as Ledger
import qualified Cardano.Ledger.Shelley.TxAuxData as Ledger
import qualified Data.Aeson.Encoding as Json
import qualified Data.Aeson.Key as Key
Expand All @@ -122,6 +125,7 @@ data ScriptPurpose
| Withdraw
| Vote
| Propose
| Guard
deriving (Eq)

-- Encoders
Expand Down Expand Up @@ -199,19 +203,26 @@ decodeBlock = Json.withObject "Block" $ \o -> do
slot <- o .:? "slot" >>= \case
Just slot -> pure slot
Nothing -> o .: "height"
isDijkstraBlock <- (== Just ("dijkstra" :: Text)) <$> o .:? "era"
-- Ogmios serialises Dijkstra transactions as stubs, which do not decode.
txs <- if isDijkstraBlock then pure [] else o .:? "transactions" .!= []
blockBody <- traverse decodePartialTransaction txs
pure PartialBlock { blockPoint = BlockPoint (SlotNo slot) id, blockBody, isDijkstraBlock }
era <- o .:? "era"
txs <- o .:? "transactions" .!= []
-- Older Ogmios versions send each Dijkstra transaction with only its id and 'spends'.
let hasTransactionStubs = era == Just ("dijkstra" :: Text) && any (not . KeyMap.member "inputs") txs
blockBody <- if hasTransactionStubs then pure [] else traverse (decodePartialTransaction . Json.Object) txs
pure PartialBlock { blockPoint = BlockPoint (SlotNo slot) id, blockBody, hasTransactionStubs }

decodePartialTransaction
:: Json.Value
-> Json.Parser PartialTransaction
decodePartialTransaction = Json.withObject "PartialTransaction" $ \o -> do
id <- o .: "id" >>= decodeTransactionId

inputSource <- o .: "spends"
decodeTransaction inputSource o

decodeTransaction
:: Json.Key
-> Json.Object
-> Json.Parser PartialTransaction
decodeTransaction inputSource o = do
id <- o .: "id" >>= decodeTransactionId

inputs <- o .:? inputSource .!= [] >>= traverse decodeInput

Expand All @@ -228,14 +239,16 @@ decodePartialTransaction = Json.withObject "PartialTransaction" $ \o -> do

scripts <- o .:? "scripts" .!= Json.Object mempty >>= decodeScripts

outputs <- case Key.toText inputSource of
subTransactions <- o .:? "subTransactions" .!= [] >>= traverse decodeSubTransaction

(outputs, appliedSubTransactions, unappliedSubTransactions) <- case Key.toText inputSource of
"collaterals" -> do
outs <- o .:? "outputs" .!= ([] :: [Json.Value])
colReturn <- o .:? "collateralReturn" >>= traverse decodeOutput
pure $ withReferences (fromIntegral $ length outs) id (maybeToList colReturn)
pure (withReferences (fromIntegral $ length outs) id (maybeToList colReturn), [], subTransactions)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reference scripts carried only in the outputs of discarded sub-transactions are missed here. When the parent fails phase-2 validation, matchBlock collects these sub-transactions through witnessedScripts. For PartialBlock, that returns only the transaction's scripts map, whereas Ogmios puts output reference scripts in outputs[].script. The direct-node implementation also collects scripts from those outputs.

If the block otherwise matches (for example, through its collateral return), /scripts/<hash> can therefore return nothing through Ogmios while returning the script through the node. Please merge output reference scripts into the collected scripts for sub-transactions and add a comparison test where the script appears only in a discarded sub-transaction's output. The current golden transaction has no output reference scripts, so it does not exercise this case.

"inputs" -> do
outs <- o .:? "outputs" .!= [] >>= traverse decodeOutput
pure $ withReferences 0 id outs
pure (withReferences 0 id outs, subTransactions, [])
(_ :: Text) -> do
fail "unrecognized input source"

Expand All @@ -247,8 +260,18 @@ decodePartialTransaction = Json.withObject "PartialTransaction" $ \o -> do
, spendRedeemers
, scripts
, metadata
, appliedSubTransactions
, unappliedSubTransactions
}

-- | A sub-transaction has no 'spends': the validity of its top-level transaction decides
-- whether the ledger applies it.
decodeSubTransaction
:: Json.Value
-> Json.Parser PartialTransaction
decodeSubTransaction =
Json.withObject "SubTransaction" (decodeTransaction "inputs")

decodeAddress
:: Text
-> Json.Parser Address
Expand Down Expand Up @@ -324,17 +347,18 @@ decodeScript
-> Json.Parser Script
decodeScript = Json.withObject "Script" $ \o -> do
o .: "language" >>= \case
"native" -> decodeNative =<< o .: "json"
"plutus:v1" -> decodePlutus "01" =<< o .: "cbor"
"plutus:v2" -> decodePlutus "02" =<< o .: "cbor"
"plutus:v3" -> decodePlutus "03" =<< o .: "cbor"
"plutus:v4" -> decodePlutus "04" =<< o .: "cbor"
-- Ogmios includes the CBOR of native scripts only when run with --include-script-cbor.
"native" -> (decodeTagged "00" =<< o .: "cbor") <|> (decodeNative =<< o .: "json")
"plutus:v1" -> decodeTagged "01" =<< o .: "cbor"
"plutus:v2" -> decodeTagged "02" =<< o .: "cbor"
"plutus:v3" -> decodeTagged "03" =<< o .: "cbor"
"plutus:v4" -> decodeTagged "04" =<< o .: "cbor"
(_ :: Text) -> fail "unrecognized script language"
where
decodeNative =
fmap fromNativeScript . decodeNativeScript

decodePlutus tag bytes = do
decodeTagged tag bytes = do
case scriptFromBytes <$> decodeBase16 (encodeUtf8 @Text (tag <> bytes)) of
Right (Just s) ->
pure s
Expand Down Expand Up @@ -365,6 +389,13 @@ decodeNativeScript o = do
RequireTimeExpire . SlotNo <$> (o .: "slot")
"after" -> do
RequireTimeStart . SlotNo <$> (o .: "slot")
-- Older Ogmios versions give a bare hash, with no 'origin' to tell a key from a script.
"guard" -> do
let credential = o .: "origin" >>= \case
"verificationKey" -> Ledger.KeyHashObj . KeyHash <$> (o .: "from" >>= decodeHash)
"script" -> Ledger.ScriptHashObj . Ledger.ScriptHash <$> (o .: "from" >>= decodeHash)
(_ :: Text) -> fail "unrecognized guard origin"
RequireGuard <$> (credential <|> fail "cannot decode a native script guard clause without its CBOR or a known origin: upgrade Ogmios, or run it with --include-script-cbor")
(_ :: Text) ->
fail "unrecognized native script clause"

Expand Down Expand Up @@ -413,6 +444,7 @@ decodeScriptPurpose = \case
"withdraw" -> pure Withdraw
"vote" -> pure Vote
"propose" -> pure Propose
"guard" -> pure Guard
purpose -> fail $ "unknown script purpose: " ++ toString purpose

decodeDatumHash
Expand Down
23 changes: 16 additions & 7 deletions src/Kupo/Data/PartialBlock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ import qualified Data.Set as Set
data PartialBlock = PartialBlock
{ blockPoint :: !Point
, blockBody :: ![PartialTransaction]
-- ^ The decoded transactions, empty for a Dijkstra block.
, isDijkstraBlock :: !Bool
-- ^ Whether this is a Dijkstra block whose transactions were not decoded.
-- Ogmios sends Dijkstra transactions without their bodies, and Hydra
-- decodes Conway transactions only.
-- ^ The decoded transactions, empty when 'hasTransactionStubs'.
, hasTransactionStubs :: !Bool
-- ^ Whether Ogmios sent the Dijkstra transactions of this block without
-- their bodies, as Ogmios versions before full Dijkstra support do.
} deriving (Eq, Show)

-- | A partial transaction, analogous to 'PartialBlock', trimmed down to the
Expand All @@ -53,6 +52,10 @@ data PartialTransaction = PartialTransaction
, spendRedeemers :: !(Map InputIndex BinaryData)
, scripts :: !(Map ScriptHash Script)
, metadata :: !(Maybe (MetadataHash, Metadata))
, appliedSubTransactions :: ![PartialTransaction]
-- ^ The Dijkstra sub-transactions of a phase-2 valid transaction, in the order the ledger applies them.
, unappliedSubTransactions :: ![PartialTransaction]
-- ^ The Dijkstra sub-transactions of a phase-2 invalid transaction, which the ledger discards.
} deriving (Eq, Show)

instance HasTransactionId PartialTransaction where
Expand All @@ -70,10 +73,16 @@ instance IsBlock PartialBlock where
foldBlock fn result =
foldrWithIndex fn result . blockBody

checkSubTransactions PartialBlock{blockPoint, isDijkstraBlock} =
when isDijkstraBlock $
checkSubTransactions PartialBlock{blockPoint, hasTransactionStubs} =
when hasTransactionStubs $
Left (UnverifiableSubTransactions blockPoint)

subTransactions =
appliedSubTransactions

discardedSubTransactions =
unappliedSubTransactions

mapMaybeOutputs fn tx =
let meta = maybe emptyMetadata snd (metadata tx)
in mapMaybe (\outs -> uncurry fn outs meta) (outputs tx)
Expand Down
4 changes: 3 additions & 1 deletion test/Test/Kupo/App/HttpSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,10 @@ newStubbedApplication defaultPatterns = do
, datums = mempty
, spendRedeemers = mempty
, metadata
, appliedSubTransactions = []
, unappliedSubTransactions = []
}
pure $ PartialBlock { blockPoint, blockBody, isDijkstraBlock = False }
pure $ PartialBlock { blockPoint, blockBody, hasTransactionStubs = False }
)
patternsVar
healthStub
Expand Down
14 changes: 12 additions & 2 deletions test/Test/Kupo/AppSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ genContinuingBlock utxo txs previousTip = do
blockPoint <- BlockPoint (next (getPointSlotNo previousTip)) <$> genHeaderHash
(txs', utxo') <- runStateT (genTransactionSublist txs) utxo
blockBody <- (txs' <>) <$> evalStateT genPartialTransactions utxo'
pure PartialBlock { blockPoint, blockBody, isDijkstraBlock = False }
pure PartialBlock { blockPoint, blockBody, hasTransactionStubs = False }

-- | Generate a list of transactions using and modifying a given utxo. The
-- generation stops when there's no more utxos or, arbitrarily (1 chance out of
Expand Down Expand Up @@ -778,7 +778,17 @@ genPartialTransaction = do
replicateM nScripts (lift genScript)
& fmap (foldMap (\v -> Map.singleton (hashScript v) v))
metadata <- lift $ oneof [pure Nothing, Just <$> genMetadata]
pure PartialTransaction { id, inputs, outputs, datums, spendRedeemers, scripts, metadata }
pure PartialTransaction
{ id
, inputs
, outputs
, datums
, spendRedeemers
, scripts
, metadata
, appliedSubTransactions = []
, unappliedSubTransactions = []
}
where
maxNumberOfInputs = 3
maxNumberOfOutputs = 3
Expand Down
Loading
Loading