Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 905a65b

Browse files
ffakenzlocallycompact
authored andcommittedMar 22, 2024
Upgrade to hydra-0.16
Hydra 0.16 drops JSON encoding of transactions in favour of a CBOR representation only. This updates the hydra submodule to provide a golden test file in the new format. Co-Authored by Daniel Firth <[email protected]>
1 parent b12414a commit 905a65b

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed
 

‎CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#### Added
44

5+
* Compatibility with hydra-node 0.16.0.
6+
57
- A new mode `--read-only` which can be used to boot-up an HTTP server with only read access to the underlying database. This option comes as an alternative to the other options for chain producers (e.g. `--node-socket` and `--node-config`). The replica can only reply successfully to GET queries with the exception of queries under `/metadata`. The latter must go through the master server.
68

79
- Automatic restart and setup indexes when `--defer-db-indexes` is provided and the tip of the chain is reached.

‎kupo.cabal

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/Kupo/Data/Hydra.hs

+45-12
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,25 @@ import Cardano.Crypto.Hash
99
, hashToBytes
1010
, hashWith
1111
)
12+
import Cardano.Ledger.Allegra.Scripts (translateTimelock)
13+
import Cardano.Ledger.Alonzo.Scripts (AlonzoScript (..))
14+
import Cardano.Ledger.Alonzo.TxWits (unTxDats)
15+
import Cardano.Ledger.Api (
16+
outputsTxBodyL,
17+
inputsTxBodyL,
18+
datsTxWitsL,
19+
scriptTxWitsL,
20+
witsTxL,
21+
bodyTxL
22+
)
23+
import Cardano.Ledger.Binary (decodeFullAnnotator)
24+
import Cardano.Ledger.Block (txid)
25+
import Cardano.Ledger.Plutus.Data (translateDatum, dataToBinaryData, upgradeData)
1226
import Cardano.Ledger.SafeHash
1327
( unsafeMakeSafeHash
1428
)
1529
import Data.Aeson
16-
( (.!=)
17-
, (.:)
30+
( (.:)
1831
, (.:?)
1932
)
2033
import Kupo.Data.Cardano
@@ -57,7 +70,9 @@ import Kupo.Data.PartialBlock
5770
( PartialBlock (..)
5871
, PartialTransaction (..)
5972
)
60-
73+
import qualified Cardano.Ledger.Api as LedgerApi
74+
import qualified Cardano.Ledger.Babbage.TxBody as Babbage
75+
import qualified Cardano.Ledger.Core as Ledger
6176
import qualified Codec.CBOR.Decoding as Cbor
6277
import qualified Codec.CBOR.Read as Cbor
6378
import qualified Data.Aeson.Key as Key
@@ -156,15 +171,21 @@ decodeGenesisTxForUTxO id indexOutputs = do
156171

157172
decodePartialTransaction :: Json.Value -> Json.Parser PartialTransaction
158173
decodePartialTransaction = Json.withObject "PartialTransaction" $ \o -> do
159-
id <- o .: "id" >>= decodeTransactionId
174+
hexText <- o .: "cborHex"
175+
176+
bytes <- decodeBase16' hexText
160177

161-
body <- o .: "body"
162-
inputs <- body .: "inputs" >>= traverse decodeInput
163-
outputs <- body .:? "outputs" .!= [] >>= traverse decodeOutput
178+
tx <- case decodeFullAnnotator (Ledger.eraProtVerLow @(BabbageEra StandardCrypto)) "foo" decCBOR (fromStrict bytes) of
179+
Left e -> fail $ show e
180+
Right tx -> pure tx
164181

165-
wits <- o.: "witnesses"
166-
datums <- wits .:? "datums" .!= Json.Object mempty >>= decodeDatums
167-
scripts <- wits .:? "scripts" .!= Json.Object mempty >>= decodeScripts
182+
let body' = tx ^. bodyTxL
183+
let inputs = toList (body' ^. inputsTxBodyL)
184+
let outputs = map convertOutput $ toList (body' ^. outputsTxBodyL)
185+
let wits' = tx ^. witsTxL
186+
let scripts = Map.map convertScript (wits' ^. scriptTxWitsL)
187+
let datums = Map.map convertData $ unTxDats (wits' ^. datsTxWitsL)
188+
let id = txid body'
168189

169190
-- TODO
170191
-- This is 'acceptable' for now because:
@@ -185,6 +206,18 @@ decodePartialTransaction = Json.withObject "PartialTransaction" $ \o -> do
185206
, scripts
186207
, metadata
187208
}
209+
where
210+
convertOutput :: Babbage.BabbageTxOut (BabbageEra StandardCrypto) -> Output
211+
convertOutput (Babbage.BabbageTxOut addr val datum maybeScript) =
212+
(Babbage.BabbageTxOut addr val (translateDatum datum) (convertScript <$> maybeScript))
213+
214+
convertData :: LedgerApi.Data (BabbageEra StandardCrypto) -> BinaryData
215+
convertData = dataToBinaryData . upgradeData
216+
217+
convertScript :: AlonzoScript (BabbageEra StandardCrypto) -> Script
218+
convertScript = \case
219+
TimelockScript timelock -> TimelockScript (translateTimelock timelock)
220+
PlutusScript script -> PlutusScript script
188221

189222
decodeDatums :: Json.Value -> Json.Parser (Map DatumHash BinaryData)
190223
decodeDatums = Json.withObject "Datums" $
@@ -333,7 +366,7 @@ decodeValue
333366
:: Json.Value
334367
-> Json.Parser Value
335368
decodeValue = Json.withObject "Value" $ \o -> do
336-
coins <- o .: "lovelace"
369+
coins <- o .:? "lovelace"
337370
assets <- KeyMap.foldrWithKey
338371
(\k v accum ->
339372
if k == "lovelace" then accum else do
@@ -344,7 +377,7 @@ decodeValue = Json.withObject "Value" $ \o -> do
344377
)
345378
(pure mempty)
346379
o
347-
pure (unsafeValueFromList coins assets)
380+
pure (unsafeValueFromList (maybe 0 (\x -> x) coins) assets)
348381
where
349382
decodeAssets
350383
:: ByteString

‎test/vectors/hydra

Submodule hydra updated from 3d825b2 to 89f8fb5

0 commit comments

Comments
 (0)
Please sign in to comment.