Skip to content

Commit

Permalink
Alter to/from json instances for RestServerOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
v0d1ch authored and ffakenz committed Jun 5, 2023
1 parent 5e13eac commit 0ff1809
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions hydra-cluster/hydra-cluster.cabal
Expand Up @@ -127,6 +127,7 @@ library
, cardano-ledger-core
, cardano-ledger-shelley
, cardano-slotting
, cborg
, containers
, contra-tracer
, data-default
Expand Down
3 changes: 2 additions & 1 deletion hydra-cluster/src/Hydra/Cluster/Scenarios.hs
Expand Up @@ -12,7 +12,8 @@ import Data.Aeson (Value, object, (.=))
import Data.Aeson.Lens (key, _JSON)
import Data.Aeson.Types (parseMaybe)
import qualified Data.Set as Set
import Hydra.API.Server (RestClientInput (..), RestServerOutput (DraftedCommitTx))
import Hydra.API.ClientInput (RestClientInput (..))
import Hydra.API.ServerOutput (RestServerOutput (DraftedCommitTx))
import Hydra.Cardano.Api (
Lovelace,
TxId,
Expand Down
3 changes: 2 additions & 1 deletion hydra-node/src/Hydra/API/Server.hs
Expand Up @@ -21,6 +21,7 @@ import Hydra.API.Projection (Projection (..), mkProjection)
import Hydra.API.ServerOutput (
HeadStatus (Idle),
OutputFormat (..),
RestServerOutput (DraftedCommitTx),
ServerOutput (Greetings, InvalidInput),
ServerOutputConfig (..),
TimedServerOutput (..),
Expand Down Expand Up @@ -235,7 +236,7 @@ runAPIServer host port party tracer history chain callback headStatusP snapshotU
case eCommitTx of
Left err -> responseLBS status400 [] (show err)
Right commitTx -> do
let encodedRestOutput = Aeson.encode commitTx
let encodedRestOutput = Aeson.encode $ DraftedCommitTx commitTx
responseLBS status200 [] encodedRestOutput
_ -> do
traceWith tracer $
Expand Down
29 changes: 26 additions & 3 deletions hydra-node/src/Hydra/API/ServerOutput.hs
Expand Up @@ -2,9 +2,10 @@

module Hydra.API.ServerOutput where

import Cardano.Binary (serialize')
import Cardano.Binary (decodeFull', serialize')
import Control.Lens ((.~))
import Data.Aeson (Value (..), defaultOptions, encode, genericParseJSON, genericToJSON, omitNothingFields, withObject, (.:))
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.KeyMap as KeyMap
import Data.Aeson.Lens (atKey, key)
import qualified Data.ByteString.Base16 as Base16
Expand Down Expand Up @@ -155,8 +156,30 @@ newtype RestServerOutput tx = DraftedCommitTx

deriving stock instance IsTx tx => Eq (RestServerOutput tx)
deriving stock instance IsTx tx => Show (RestServerOutput tx)
deriving newtype instance IsTx tx => ToJSON (RestServerOutput tx)
deriving newtype instance IsTx tx => FromJSON (RestServerOutput tx)

instance (IsTx tx, ToCBOR tx) => ToJSON (RestServerOutput tx) where
toJSON (DraftedCommitTx tx) =
Aeson.object
[ "tag" Aeson..= Aeson.String "RestServerOutput"
, "commitTx" Aeson..= (String . decodeUtf8 . Base16.encode $ serialize' tx)
]

instance
(IsTx tx, FromCBOR tx) =>
FromJSON (RestServerOutput tx)
where
parseJSON = withObject "RestServerOutput" $ \o -> do
tag <- o .: "tag"
case tag :: Text of
"RestServerOutput" -> do
encodedTx :: Text <- o .: "commitTx"
case Base16.decode $ encodeUtf8 encodedTx of
Left e -> fail e
Right commitTx ->
case decodeFull' commitTx of
Left err -> fail $ show err
Right v -> pure $ DraftedCommitTx v
_ -> fail "Expected tag to be PubKeyHash"

instance IsTx tx => Arbitrary (RestServerOutput tx) where
arbitrary = genericArbitrary
Expand Down

0 comments on commit 0ff1809

Please sign in to comment.