Skip to content

Commit

Permalink
Parse transactions in Ledger CDDL format
Browse files Browse the repository at this point in the history
cardano-cli is now using the CDDL format for transactions and our tests
were still assuming the old TextEnvelope format which is not any more
parseable (when using serialiseToTextEnvelope).

Seems like CDDL format is what is going to be supported in the future so
let's use that in our tests to make sure produced json is compatible
with cardano-cli.
  • Loading branch information
v0d1ch committed May 13, 2024
1 parent e278c4c commit a3dabaa
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion hydra-cluster/test/Test/Hydra/Cluster/CardanoCliSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Test.QuickCheck (generate)
spec :: Spec
spec =
describe "cardano-cli" $ do
it "cardano-cli can accept a draft commit tx in text-envelope format" $
it "cardano-cli can accept a draft commit tx" $
withTempDir "hydra-cluster" $ \tmpDir -> do
let txFile = tmpDir </> "tx.raw"
draftCommitResponse <- DraftCommitTxResponse <$> generate (arbitrary :: Gen Tx)
Expand Down
2 changes: 1 addition & 1 deletion hydra-node/src/Hydra/Ledger/Cardano.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import Codec.CBOR.Decoding qualified as CBOR
import Codec.CBOR.Encoding qualified as CBOR
import Control.Lens (set)
import Control.Monad (foldM)
import Data.Aeson (object, (.:), (.:?), (.=))
import Data.Aeson (object, (.:), (.=))
import Data.Aeson qualified as Aeson
import Data.Aeson.Types (withObject)
import Data.ByteString qualified as BS
Expand Down
23 changes: 15 additions & 8 deletions hydra-node/test/Hydra/API/ClientInputSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ module Hydra.API.ClientInputSpec where
import Hydra.Prelude
import Test.Hydra.Prelude

import Data.Aeson (Result (..), fromJSON)
import Control.Lens ((^.))
import Data.Aeson.Lens (key, _String)
import Hydra.API.ClientInput (ClientInput)
import Hydra.Cardano.Api (serialiseToTextEnvelope)
import Hydra.Cardano.Api (ShelleyBasedEra (..), deserialiseTxLedgerCddl, serialiseTxLedgerCddl)
import Hydra.Ledger.Cardano (Tx)
import Hydra.Ledger.Simple (SimpleTx)
import Test.Aeson.GenericSpecs (
Settings (..),
defaultSettings,
roundtripAndGoldenSpecsWithSettings,
)
import Test.QuickCheck (counterexample, forAll, property)
import Test.QuickCheck (counterexample, forAll, (.&&.), (===))

spec :: Spec
spec = parallel $ do
Expand All @@ -26,11 +27,17 @@ spec = parallel $ do

describe "FromJSON (ValidatedTx era)" $ do
prop "accepts transactions produced via cardano-cli" $
forAll (arbitrary @Tx) $ \tx ->
let envelope = toJSON $ serialiseToTextEnvelope (Just "Tx Babbage") tx
in case fromJSON @Tx envelope of
Success{} -> property True
Error e -> counterexample (toString $ toText e) $ property False
forAll arbitrary $ \tx -> do
let transactionCDDL = serialiseTxLedgerCddl ShelleyBasedEraBabbage tx
let transaction = toJSON tx
let deserialisedTx = deserialiseTxLedgerCddl ShelleyBasedEraBabbage transactionCDDL
let getKey txJson k = txJson ^. key k . _String
( deserialisedTx == Right tx
.&&. toJSON transactionCDDL `getKey` "cborHex" === transaction `getKey` "cborHex"
.&&. toJSON transactionCDDL `getKey` "type" === transaction `getKey` "type"
)
& counterexample ("Transaction JSON: " <> show transaction)
& counterexample ("Transaction CDDL JSON: " <> show (toJSON transactionCDDL))

settings :: Settings
settings =
Expand Down
7 changes: 4 additions & 3 deletions hydra-node/test/Hydra/API/HTTPServerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import Data.Aeson.Lens (key, nth)
import Hydra.API.HTTPServer (DraftCommitTxRequest (..), DraftCommitTxResponse (..), SubmitTxRequest (..), TransactionSubmitted, httpApp)
import Hydra.API.ServerSpec (dummyChainHandle)
import Hydra.Cardano.Api (
ShelleyBasedEra (..),
fromLedgerPParams,
serialiseToTextEnvelope,
serialiseTxLedgerCddl,
shelleyBasedEra,
)
import Hydra.Chain (Chain (draftCommitTx), PostTxError (..))
Expand Down Expand Up @@ -77,9 +78,9 @@ spec = do
in case fromJSON @(SubmitTxRequest Tx) json of
Success{} -> property True
Error e -> counterexample (toString $ toText e) $ property False
prop "accepts transaction encoded as TextEnvelope" $
prop "accepts transaction encoded as Ledger CDDL" $
forAll (arbitrary @Tx) $ \tx ->
let json = toJSON $ serialiseToTextEnvelope Nothing tx
let json = toJSON $ serialiseTxLedgerCddl ShelleyBasedEraBabbage tx
in case fromJSON @(SubmitTxRequest Tx) json of
Success{} -> property True
Error e -> counterexample (toString $ toText e) $ property False
Expand Down

0 comments on commit a3dabaa

Please sign in to comment.