Skip to content

Commit

Permalink
Rename new http server types
Browse files Browse the repository at this point in the history
  • Loading branch information
v0d1ch authored and ffakenz committed Jun 5, 2023
1 parent ce7d003 commit 2d86642
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 38 deletions.
8 changes: 4 additions & 4 deletions hydra-cluster/src/Hydra/Cluster/Scenarios.hs
Expand Up @@ -12,7 +12,7 @@ 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.RestServer (RestClientInput (..), RestServerOutput (..))
import Hydra.API.RestServer (DraftCommitTxRequest (..), DraftCommitTxResponse (..))
import Hydra.Cardano.Api (
Lovelace,
TxId,
Expand Down Expand Up @@ -168,20 +168,20 @@ singlePartyCommitsFromExternal tracer workDir node@RunningNode{networkId} hydraS
headId <- waitMatch 600 n1 $ headIsInitializingWith (Set.fromList [alice])

-- Request to build a draft commit tx from hydra-node
let clientPayload = DraftCommitTx @Tx utxoToCommit
let clientPayload = DraftCommitTxRequest @Tx utxoToCommit

response <-
runReq defaultHttpConfig $
req
POST
(http "127.0.0.1" /: "commit")
(ReqBodyJson clientPayload)
(Proxy :: Proxy (JsonResponse (RestServerOutput Tx)))
(Proxy :: Proxy (JsonResponse (DraftCommitTxResponse Tx)))
(port $ 4000 + hydraNodeId)

responseStatusCode response `shouldBe` 200

let DraftedCommitTx commitTx = responseBody response
let DraftCommitTxResponse commitTx = responseBody response

-- sign and submit the tx with our external user key
let signedCommitTx = signWith externalSk commitTx
Expand Down
16 changes: 8 additions & 8 deletions hydra-node/json-schemas/api.yaml
Expand Up @@ -120,14 +120,14 @@ channels:
operationId: restServerOutput
message:
oneOf:
- $ref: "#/components/messages/RestServerOutput"
- $ref: "#/components/messages/DraftCommitTxResponse"

publish:
summary: Requests sent to the Hydra node.
operationId: restClientInput
message:
oneOf:
- $ref: "#/components/messages/RestClientInput"
- $ref: "#/components/messages/DraftCommitTxRequest"
components:
messages:
########
Expand Down Expand Up @@ -249,8 +249,8 @@ components:
type: string
enum: ["GetUTxO"]

RestClientInput:
title: RestClientInput
DraftCommitTxRequest:
title: DraftCommitTxRequest
description: |
Provide a utxo that will be used to draft a commit transaction that will be sent back to the user.
payload:
Expand All @@ -261,7 +261,7 @@ components:
properties:
tag:
type: string
enum: ["RestClientInput"]
enum: ["DraftCommitTxRequest"]
utxo:
$ref: "#/components/schemas/UTxO"

Expand Down Expand Up @@ -747,8 +747,8 @@ components:
timestamp:
$ref: "#/components/schemas/UTCTime"

RestServerOutput:
title: RestServerOutput
DraftCommitTxResponse:
title: DraftCommitTxResponse
description: |
Emitted by the server after drafting a commit transaction with the user provided utxo. Transaction returned to the user is in it's cbor representation encoded as Base16.
payload:
Expand All @@ -759,7 +759,7 @@ components:
properties:
tag:
type: string
enum: ["RestServerOutput"]
enum: ["DraftCommitTxResponse"]
commitTx:
$ref: "#/components/schemas/RawTransaction"

Expand Down
38 changes: 19 additions & 19 deletions hydra-node/src/Hydra/API/RestServer.hs
Expand Up @@ -9,56 +9,56 @@ import Data.Aeson (Value (String), object, withObject, (.:), (.=))
import qualified Data.ByteString.Base16 as Base16
import Hydra.Ledger (IsTx, UTxOType)

newtype RestServerOutput tx = DraftedCommitTx
newtype DraftCommitTxResponse tx = DraftCommitTxResponse
{ commitTx :: tx
}
deriving (Generic)

deriving stock instance IsTx tx => Eq (RestServerOutput tx)
deriving stock instance IsTx tx => Show (RestServerOutput tx)
deriving stock instance IsTx tx => Eq (DraftCommitTxResponse tx)
deriving stock instance IsTx tx => Show (DraftCommitTxResponse tx)

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

instance
(IsTx tx, FromCBOR tx) =>
FromJSON (RestServerOutput tx)
FromJSON (DraftCommitTxResponse tx)
where
parseJSON = withObject "RestServerOutput" $ \o -> do
parseJSON = withObject "DraftCommitTxResponse" $ \o -> do
tag <- o .: "tag"
case tag :: Text of
"RestServerOutput" -> do
"DraftCommitTxResponse" -> 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
Right v -> pure $ DraftCommitTxResponse v
_ -> fail "Expected tag to be PubKeyHash"

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

shrink = \case
DraftedCommitTx xs -> DraftedCommitTx <$> shrink xs
DraftCommitTxResponse xs -> DraftCommitTxResponse <$> shrink xs

newtype RestClientInput tx = DraftCommitTx
newtype DraftCommitTxRequest tx = DraftCommitTxRequest
{ utxo :: UTxOType tx
}
deriving (Generic)

deriving newtype instance IsTx tx => Eq (RestClientInput tx)
deriving newtype instance IsTx tx => Show (RestClientInput tx)
deriving anyclass instance IsTx tx => ToJSON (RestClientInput tx)
deriving anyclass instance IsTx tx => FromJSON (RestClientInput tx)
deriving newtype instance IsTx tx => Eq (DraftCommitTxRequest tx)
deriving newtype instance IsTx tx => Show (DraftCommitTxRequest tx)
deriving anyclass instance IsTx tx => ToJSON (DraftCommitTxRequest tx)
deriving anyclass instance IsTx tx => FromJSON (DraftCommitTxRequest tx)

instance Arbitrary (UTxOType tx) => Arbitrary (RestClientInput tx) where
instance Arbitrary (UTxOType tx) => Arbitrary (DraftCommitTxRequest tx) where
arbitrary = genericArbitrary

shrink = \case
DraftCommitTx xs -> DraftCommitTx <$> shrink xs
DraftCommitTxRequest xs -> DraftCommitTxRequest <$> shrink xs
6 changes: 3 additions & 3 deletions hydra-node/src/Hydra/API/Server.hs
Expand Up @@ -16,7 +16,7 @@ import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Lazy as LBS
import Hydra.API.ClientInput (ClientInput)
import Hydra.API.Projection (Projection (..), mkProjection)
import Hydra.API.RestServer (RestClientInput (utxo), RestServerOutput (..))
import Hydra.API.RestServer (DraftCommitTxRequest (..), DraftCommitTxResponse (..))
import Hydra.API.ServerOutput (
HeadStatus (Idle),
OutputFormat (..),
Expand Down Expand Up @@ -346,7 +346,7 @@ handleDraftCommitUtxo ::
(Response -> IO ResponseReceived) ->
IO ResponseReceived
handleDraftCommitUtxo directChain tracer body reqMethod reqPaths respond = do
case Aeson.eitherDecode' body :: Either String (RestClientInput tx) of
case Aeson.eitherDecode' body :: Either String (DraftCommitTxRequest tx) of
Left err -> respond $ responseLBS status400 [] (show err)
Right requestInput -> do
traceWith tracer $
Expand All @@ -363,6 +363,6 @@ handleDraftCommitUtxo directChain tracer body reqMethod reqPaths respond = do
case eCommitTx of
Left err -> responseLBS status400 [] (show err)
Right commitTx ->
responseLBS status200 [] (Aeson.encode $ DraftedCommitTx commitTx)
responseLBS status200 [] (Aeson.encode $ DraftCommitTxResponse commitTx)
where
Chain{draftTx} = directChain
7 changes: 3 additions & 4 deletions hydra-node/test/Hydra/LoggingSpec.hs
Expand Up @@ -7,8 +7,7 @@ import Test.Hydra.Prelude

import Data.Aeson (object, (.=))
import Data.Aeson.Lens (key)
import Hydra.API.ClientInput (RestClientInput)
import Hydra.API.ServerOutput (RestServerOutput)
import Hydra.API.RestServer (DraftCommitTxRequest, DraftCommitTxResponse)
import Hydra.JSONSchema (SpecificationSelector, prop_specIsComplete, prop_validateToJSON, withJsonSpecifications)
import Hydra.Ledger.Cardano (Tx)
import Hydra.Logging (Envelope (..), Verbosity (Verbose), traceWith, withTracer)
Expand All @@ -33,8 +32,8 @@ spec = do
conjoin
[ prop_validateToJSON @(Envelope (HydraLog Tx ())) (dir </> "logs.yaml") "messages" (dir </> "HydraLog")
, prop_specIsComplete @(HydraLog Tx ()) (dir </> "logs.yaml") apiSpecificationSelector
, prop_specIsComplete @(RestClientInput Tx) (dir </> "logs.yaml") apiSpecificationSelector
, prop_specIsComplete @(RestServerOutput Tx) (dir </> "logs.yaml") apiSpecificationSelector
, prop_specIsComplete @(DraftCommitTxResponse Tx) (dir </> "logs.yaml") apiSpecificationSelector
, prop_specIsComplete @(DraftCommitTxRequest Tx) (dir </> "logs.yaml") apiSpecificationSelector
]

apiSpecificationSelector :: SpecificationSelector
Expand Down

0 comments on commit 2d86642

Please sign in to comment.