Skip to content

Commit f7e8fdc

Browse files
Change TransactionEnvelope to Transaction and only use Transaction type.
Remove ToJSON/FromJSON AlonzoTx instances
1 parent b7ba6ad commit f7e8fdc

File tree

7 files changed

+40
-111
lines changed

7 files changed

+40
-111
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ changes.
4242

4343
- Adapt cardano client and the chain-sync client to survive after the fork to Conway era.
4444

45+
- **BREAKING** Transaction serialization on hydra-node api and persisted data changed.
46+
4547
## [0.14.0] - 2023-12-04
4648

4749
- **BREAKING** Multiple changes to the Hydra Head protocol on-chain:

hydra-cluster/src/Hydra/Cluster/Scenarios.hs

+2-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ import Hydra.Cardano.Api (
4848
mkVkAddress,
4949
selectLovelace,
5050
signTx,
51-
toLedgerTx,
5251
toScriptData,
5352
writeFileTextEnvelope,
5453
pattern ReferenceScriptNone,
@@ -523,12 +522,12 @@ canSubmitTransactionThroughAPI tracer workDir node hydraScriptsTxId =
523522
Left e -> failure $ show e
524523
Right body -> do
525524
let unsignedTx = makeSignedTransaction [] body
526-
let unsignedRequest = toJSON $ toLedgerTx unsignedTx
525+
let unsignedRequest = toJSON unsignedTx
527526
sendRequest hydraNodeId unsignedRequest
528527
`shouldThrow` expectErrorStatus 400 (Just "MissingVKeyWitnessesUTXOW")
529528

530529
let signedTx = signTx cardanoBobSk unsignedTx
531-
let signedRequest = toJSON $ toLedgerTx signedTx
530+
let signedRequest = toJSON signedTx
532531
(sendRequest hydraNodeId signedRequest <&> responseBody)
533532
`shouldReturn` TransactionSubmitted
534533
where

hydra-node/golden/Tx BabbageEra.json

+16-6
Large diffs are not rendered by default.

hydra-node/json-schemas/api.yaml

+2-4
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ channels:
161161
Cardano transaction to be submitted to the L1 network.
162162
Accepts transactions encoded as Base16 CBOR string, TextEnvelope type or JSON.
163163
payload:
164-
oneOf:
165-
- $ref: "api.yaml#/components/schemas/Transaction"
164+
$ref: "api.yaml#/components/schemas/Transaction"
166165
bindings:
167166
http:
168167
type: response
@@ -239,8 +238,7 @@ components:
239238
type: string
240239
enum: ["NewTx"]
241240
transaction:
242-
oneOf:
243-
- $ref: "api.yaml#/components/schemas/Transaction"
241+
$ref: "api.yaml#/components/schemas/Transaction"
244242

245243
Close:
246244
title: Close

hydra-node/src/Hydra/Ledger/Cardano.hs

+17-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import Hydra.Prelude
1212
import Hydra.Cardano.Api hiding (initialLedgerState)
1313
import Hydra.Ledger.Cardano.Builder
1414

15+
import Data.Aeson (object, (.:), (.=))
16+
import Data.Aeson qualified as Aeson
1517
import Cardano.Api.UTxO (fromPairs, pairs)
1618
import Cardano.Api.UTxO qualified as UTxO
1719
import Cardano.Crypto.DSIGN qualified as CC
@@ -50,6 +52,9 @@ import Test.QuickCheck (
5052
suchThat,
5153
vectorOf,
5254
)
55+
import qualified Data.ByteString.Base16 as Base16
56+
import Data.Aeson.Types (withObject)
57+
import Data.Aeson ((.:?))
5358

5459
-- * Ledger
5560

@@ -125,10 +130,20 @@ instance FromCBOR Tx where
125130
(pure . fromLedgerTx)
126131

127132
instance ToJSON Tx where
128-
toJSON = toJSON . toLedgerTx
133+
toJSON tx = object [ "cborHex" .= (Aeson.String $ decodeUtf8 $ Base16.encode $ serialiseToCBOR tx) ]
129134

130135
instance FromJSON Tx where
131-
parseJSON = fmap fromLedgerTx . parseJSON
136+
parseJSON =
137+
withObject "Tx" $ \o -> do
138+
let TextEnvelopeType envelopeType = textEnvelopeType (proxyToAsType (Proxy @Tx ))
139+
hexText <- o .: "cborHex"
140+
(o .:? "type") >>= \case
141+
Nothing -> pure ()
142+
Just x -> guard (envelopeType == x)
143+
bytes <- decodeBase16 hexText
144+
case deserialiseFromCBOR (proxyToAsType (Proxy @Tx)) bytes of
145+
Left e -> fail $ show e
146+
Right x -> pure x
132147

133148
instance Arbitrary Tx where
134149
-- TODO: shrinker!

hydra-node/src/Hydra/Ledger/Cardano/Json.hs

-95
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,17 @@ module Hydra.Ledger.Cardano.Json where
1111
import Hydra.Cardano.Api
1212
import Hydra.Prelude
1313

14-
import Cardano.Binary (serialize)
1514
import Cardano.Ledger.Api (Babbage)
16-
import Cardano.Ledger.Api qualified as Ledger
1715
import Cardano.Ledger.Api.Era (eraProtVerLow)
1816
import Cardano.Ledger.Babbage.PParams (BabbagePParams (..))
1917
import Cardano.Ledger.Babbage.PParams qualified as Ledger
20-
import Cardano.Ledger.Babbage.Tx qualified as Ledger
21-
import Cardano.Ledger.Binary (
22-
DecCBOR,
23-
EncCBOR,
24-
decCBOR,
25-
decodeFullAnnotator,
26-
decodeFullDecoder,
27-
)
28-
import Cardano.Ledger.Binary.Decoding (Annotator)
2918
import Cardano.Ledger.Shelley.API qualified as Ledger
3019
import Data.Aeson (
31-
Value (String),
32-
withObject,
33-
withText,
3420
(.!=),
3521
(.:),
3622
(.:?),
3723
)
3824
import Data.Aeson qualified as Aeson
39-
import Data.Aeson.Types (
40-
Parser,
41-
)
42-
import Data.ByteString.Base16 qualified as Base16
4325

4426
-- XXX: Maybe use babbagePParamsHKDPairs?
4527
instance FromJSON (Ledger.BabbagePParams Identity era) where
@@ -92,80 +74,3 @@ instance FromJSON (Ledger.BabbagePParams Identity era) where
9274
.: "collateralPercentage"
9375
<*> obj
9476
.: "maxCollateralInputs"
95-
96-
--
97-
-- AlonzoTx
98-
--
99-
100-
instance
101-
( EncCBOR (Ledger.TxBody era)
102-
, EncCBOR (Ledger.TxWits era)
103-
, EncCBOR (Ledger.TxAuxData era)
104-
, Ledger.EraTxBody era
105-
, Ledger.Era era
106-
) =>
107-
ToJSON (Ledger.AlonzoTx era)
108-
where
109-
toJSON = Aeson.String . decodeUtf8 . Base16.encode . toStrict . serialize
110-
111-
instance
112-
( DecCBOR (Annotator (Ledger.TxBody era))
113-
, DecCBOR (Annotator (Ledger.TxWits era))
114-
, DecCBOR (Annotator (Ledger.TxAuxData era))
115-
, Ledger.Era era
116-
) =>
117-
FromJSON (Ledger.AlonzoTx era)
118-
where
119-
parseJSON value =
120-
-- We accept transactions in three forms:
121-
--
122-
-- (1) As a JSON 'text-envelope', which is a format defined and produced by
123-
-- the cardano-cli, wrapping base16-encoded strings as JSON objects with
124-
-- tags.
125-
parseAsEnvelopedBase16CBOR value
126-
-- (2) As base16 string representing a CBOR-serialized transaction, since
127-
-- this is the most common medium of exchange used for transactions.
128-
<|> parseHexEncodedCborAnnotated @era "Tx" value
129-
where
130-
parseAsEnvelopedBase16CBOR =
131-
withObject "Tx" $ \o -> do
132-
let TextEnvelopeType envelopeType = textEnvelopeType (proxyToAsType (Proxy @Tx))
133-
str <- o .: "cborHex"
134-
guard . (== envelopeType) =<< (o .: "type")
135-
parseHexEncodedCborAnnotated @era "Tx" (String str)
136-
137-
-- | Parse a hex-encoded CBOR value in given 'era'.
138-
parseHexEncodedCbor ::
139-
forall era a.
140-
(Ledger.Era era, DecCBOR a) =>
141-
Text ->
142-
Aeson.Value ->
143-
Parser a
144-
parseHexEncodedCbor lbl =
145-
withText (toString lbl) $ \t ->
146-
case Base16.decode $ encodeUtf8 t of
147-
Left e -> fail $ "failed to decode from base16: " <> show e
148-
Right bs ->
149-
case decodeFullDecoder version lbl decCBOR (fromStrict bs) of
150-
Left err -> fail $ show err
151-
Right v -> pure v
152-
where
153-
version = Ledger.eraProtVerLow @era
154-
155-
-- | Parse a hex-encoded, annotated CBOR value in given 'era'.
156-
parseHexEncodedCborAnnotated ::
157-
forall era a.
158-
(Ledger.Era era, DecCBOR (Annotator a)) =>
159-
Text ->
160-
Aeson.Value ->
161-
Parser a
162-
parseHexEncodedCborAnnotated lbl =
163-
withText (toString lbl) $ \t ->
164-
case Base16.decode $ encodeUtf8 t of
165-
Left e -> fail $ "failed to decode from base16: " <> show e
166-
Right bs ->
167-
case decodeFullAnnotator version lbl decCBOR (fromStrict bs) of
168-
Left err -> fail $ show err
169-
Right v -> pure v
170-
where
171-
version = Ledger.eraProtVerLow @era

hydra-node/test/Hydra/API/HTTPServerSpec.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ spec = do
6464
Error e -> counterexample (toString $ toText e) $ property False
6565
prop "accepts json encoded transaction" $
6666
forAll (arbitrary @Tx) $ \tx ->
67-
let json = toJSON (toLedgerTx tx)
67+
let json = toJSON tx
6868
in case fromJSON @(SubmitTxRequest Tx) json of
6969
Success{} -> property True
7070
Error e -> counterexample (toString $ toText e) $ property False

0 commit comments

Comments
 (0)