Skip to content

Commit ed81f4f

Browse files
committed
Switch to JSON datasets again
This keeps the cardano-cli style transaction envelopes for transactions with essentially base16 encoded tx data, but uses JSON otherwise. This allows easier introspection (title, description, number of txs) of individual parts of a dataset.
1 parent d8d2f93 commit ed81f4f

File tree

7 files changed

+34
-54
lines changed

7 files changed

+34
-54
lines changed

.github/workflows/ci-nix.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ jobs:
153153
options: '-o $(pwd)/../benchmarks/ledger-bench.html'
154154
- package: hydra-cluster
155155
bench: bench-e2e
156-
options: 'datasets datasets/3-nodes.cbor datasets/1-node.cbor --output-directory $(pwd)/../benchmarks --timeout 1000s'
156+
options: 'datasets datasets/1-node.json datasets/3-nodes.json --output-directory $(pwd)/../benchmarks --timeout 1000s'
157157
- package: plutus-merkle-tree
158158
bench: on-chain-cost
159159
options: '$(pwd)/../benchmarks'

hydra-cluster/bench/Main.hs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,17 @@ import Test.Hydra.Prelude
88
import Bench.EndToEnd (bench)
99
import Bench.Options (Options (..), benchOptionsParser)
1010
import Bench.Summary (Summary (..), markdownReport, textReport)
11-
import Cardano.Binary (decodeFull, serialize)
12-
import Data.Aeson (eitherDecodeFileStrict')
13-
import Data.ByteString.Base16 qualified as Base16
14-
import Data.ByteString.Lazy qualified as LBS
11+
import Data.Aeson (eitherDecodeFileStrict', encodeFile)
1512
import Hydra.Cardano.Api (
1613
ShelleyBasedEra (..),
1714
ShelleyGenesis (..),
1815
fromLedgerPParams,
1916
)
2017
import Hydra.Generator (Dataset (..), generateConstantUTxODataset)
21-
import Options.Applicative (
22-
execParser,
23-
)
18+
import Options.Applicative (execParser)
2419
import System.Directory (createDirectoryIfMissing, doesDirectoryExist)
2520
import System.Environment (withArgs)
26-
import System.FilePath (takeDirectory, takeFileName, (</>))
21+
import System.FilePath (takeFileName, (</>))
2722
import Test.HUnit.Lang (formatFailureReason)
2823
import Test.QuickCheck (generate, getSize, scale)
2924

@@ -53,12 +48,12 @@ main =
5348
Right shelleyGenesis ->
5449
pure $ fromLedgerPParams ShelleyBasedEraShelley (sgProtocolParams shelleyGenesis)
5550
dataset <- generateConstantUTxODataset pparams (fromIntegral clusterSize) numberOfTxs
56-
let datasetPath = workDir </> "dataset.cbor"
51+
let datasetPath = workDir </> "dataset.json"
5752
saveDataset datasetPath dataset
5853
run outputDirectory timeoutSeconds startingNodeId [datasetPath]
5954

6055
replay outputDirectory timeoutSeconds startingNodeId benchDir = do
61-
let datasetPath = benchDir </> "dataset.cbor"
56+
let datasetPath = benchDir </> "dataset.json"
6257
putStrLn $ "Replaying single dataset from work directory: " <> datasetPath
6358
run outputDirectory timeoutSeconds startingNodeId [datasetPath]
6459

@@ -83,13 +78,12 @@ main =
8378
loadDataset :: FilePath -> IO Dataset
8479
loadDataset f = do
8580
putStrLn $ "Reading dataset from: " <> f
86-
readFileBS f >>= either (die . show) pure . (decodeFull . LBS.fromStrict . Base16.decodeLenient)
81+
eitherDecodeFileStrict' f >>= either (die . show) pure
8782

8883
saveDataset :: FilePath -> Dataset -> IO ()
8984
saveDataset f dataset = do
9085
putStrLn $ "Writing dataset to: " <> f
91-
createDirectoryIfMissing True $ takeDirectory f
92-
writeFileBS f $ Base16.encode $ LBS.toStrict $ serialize dataset
86+
encodeFile f dataset
9387

9488
data BenchmarkFailed
9589
= TestFailed HUnitFailure

hydra-cluster/datasets/1-node.cbor

Lines changed: 0 additions & 1 deletion
This file was deleted.

hydra-cluster/datasets/3-nodes.cbor

Lines changed: 0 additions & 1 deletion
This file was deleted.

hydra-cluster/hydra-cluster.cabal

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ library
8585
build-depends:
8686
, aeson
8787
, async
88-
, base >=4.7 && <5
88+
, base >=4.7 && <5
8989
, bytestring
9090
, cardano-slotting
9191
, containers
@@ -103,7 +103,6 @@ library
103103
, lens
104104
, lens-aeson
105105
, optparse-applicative
106-
, plutus-ledger-api:plutus-ledger-api-testlib >=1.9.1.0
107106
, process
108107
, QuickCheck
109108
, req
@@ -164,6 +163,7 @@ test-suite tests
164163
, directory
165164
, filepath
166165
, hspec
166+
, hspec-golden-aeson
167167
, http-client
168168
, http-conduit
169169
, hydra-cardano-api
@@ -202,9 +202,6 @@ benchmark bench-e2e
202202
build-depends:
203203
, aeson
204204
, base >=4.7 && <5
205-
, base16-bytestring
206-
, bytestring
207-
, cardano-binary
208205
, containers
209206
, directory
210207
, filepath

hydra-cluster/src/Hydra/Generator.hs

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Hydra.Prelude hiding (size)
66
import Cardano.Api.UTxO qualified as UTxO
77
import CardanoClient (mkGenesisTx)
88
import Control.Monad (foldM)
9+
import Data.Aeson (object, withObject, (.:), (.=))
910
import Data.Default (def)
1011
import Hydra.Cluster.Fixture (Actor (Faucet), availableInitialFunds)
1112
import Hydra.Cluster.Util (keysFor)
@@ -26,18 +27,7 @@ data Dataset = Dataset
2627
, description :: Maybe Text
2728
}
2829
deriving stock (Show, Generic)
29-
30-
instance ToCBOR Dataset where
31-
toCBOR Dataset{fundingTransaction, clientDatasets, title, description} =
32-
mconcat
33-
[ toCBOR fundingTransaction
34-
, toCBOR clientDatasets
35-
, toCBOR title
36-
, toCBOR description
37-
]
38-
39-
instance FromCBOR Dataset where
40-
fromCBOR = Dataset <$> fromCBOR <*> fromCBOR <*> fromCBOR <*> fromCBOR
30+
deriving anyclass (ToJSON, FromJSON)
4131

4232
instance Arbitrary Dataset where
4333
arbitrary = sized $ \n -> do
@@ -52,36 +42,35 @@ data ClientKeys = ClientKeys
5242
}
5343
deriving stock (Show)
5444

55-
instance Arbitrary ClientKeys where
56-
arbitrary = ClientKeys <$> genSigningKey <*> genSigningKey
57-
58-
instance ToCBOR ClientKeys where
59-
toCBOR ClientKeys{signingKey, externalSigningKey} =
60-
mconcat
61-
[ toCBOR signingKey
62-
, toCBOR externalSigningKey
45+
instance ToJSON ClientKeys where
46+
toJSON ClientKeys{signingKey, externalSigningKey} =
47+
object
48+
[ "signingKey" .= serialiseToTextEnvelope (Just "signingKey") signingKey
49+
, "externalSigningKey" .= serialiseToTextEnvelope (Just "externalSigningKey") externalSigningKey
6350
]
6451

65-
instance FromCBOR ClientKeys where
66-
fromCBOR = ClientKeys <$> fromCBOR <*> fromCBOR
52+
instance FromJSON ClientKeys where
53+
parseJSON =
54+
withObject "ClientKeys" $ \o ->
55+
ClientKeys
56+
<$> (decodeSigningKey =<< o .: "signingKey")
57+
<*> (decodeSigningKey =<< o .: "externalSigningKey")
58+
where
59+
decodeSigningKey v = do
60+
envelope <- parseJSON v
61+
deserialiseFromTextEnvelope (AsSigningKey AsPaymentKey) envelope
62+
& either (fail . show) pure
63+
64+
instance Arbitrary ClientKeys where
65+
arbitrary = ClientKeys <$> genSigningKey <*> genSigningKey
6766

6867
data ClientDataset = ClientDataset
6968
{ clientKeys :: ClientKeys
7069
, initialUTxO :: UTxO
7170
, txSequence :: [Tx]
7271
}
7372
deriving stock (Show, Generic)
74-
75-
instance ToCBOR ClientDataset where
76-
toCBOR ClientDataset{clientKeys, initialUTxO, txSequence} =
77-
mconcat
78-
[ toCBOR clientKeys
79-
, toCBOR initialUTxO
80-
, toCBOR txSequence
81-
]
82-
83-
instance FromCBOR ClientDataset where
84-
fromCBOR = ClientDataset <$> fromCBOR <*> fromCBOR <*> fromCBOR
73+
deriving anyclass (ToJSON, FromJSON)
8574

8675
defaultProtocolParameters :: ProtocolParameters
8776
defaultProtocolParameters = fromLedgerPParams ShelleyBasedEraShelley def

hydra-cluster/test/Test/GeneratorSpec.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import Hydra.Ledger.Cardano.Configuration (
2323
LedgerEnv,
2424
newLedgerEnv,
2525
)
26+
import Test.Aeson.GenericSpecs (roundtripSpecs)
2627
import Test.QuickCheck (
2728
Positive (Positive),
2829
Property,
@@ -33,6 +34,7 @@ import Test.QuickCheck (
3334

3435
spec :: Spec
3536
spec = parallel $ do
37+
roundtripSpecs (Proxy @Dataset)
3638
prop "generates a Dataset that keeps UTXO constant" prop_keepsUTxOConstant
3739

3840
prop_keepsUTxOConstant :: Property

0 commit comments

Comments
 (0)