Skip to content

Commit

Permalink
Small refactor and haddocks
Browse files Browse the repository at this point in the history
  • Loading branch information
v0d1ch authored and ffakenz committed Jun 5, 2023
1 parent 6cbc2dc commit c9bb964
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions hydra-node/src/Hydra/API/Server.hs
@@ -1,4 +1,3 @@
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
Expand Down Expand Up @@ -216,28 +215,10 @@ runAPIServer host port party tracer history chain callback headStatusP snapshotU
withPingThread con 30 (pure ()) $
race_ (receiveInputs con) (sendOutputs chan con outConfig)

-- Hydra HTTP server
httpApp directChain req respond =
case (requestMethod req, pathInfo req) of
("POST", ["commit"]) -> do
body <- getWaiRequestBody req
case Aeson.eitherDecode' (fromStrict body) :: Either String (RestClientInput tx) of
Left err -> respond $ responseLBS status400 [] (show err)
Right requestInput -> do
traceWith tracer $
APIRestInputReceived
{ method = decodeUtf8 $ requestMethod req
, paths = pathInfo req
, requestInputBody = Just $ toJSON requestInput
}
let Chain{draftTx} = directChain
let userUtxo = utxo requestInput
eCommitTx <- draftTx userUtxo
respond $
case eCommitTx of
Left err -> responseLBS status400 [] (show err)
Right commitTx -> do
let encodedRestOutput = Aeson.encode $ DraftedCommitTx commitTx
responseLBS status200 [] encodedRestOutput
("POST", ["commit"]) -> handleDraftCommitUtxo directChain req respond
_ -> do
traceWith tracer $
APIRestInputReceived
Expand Down Expand Up @@ -282,13 +263,17 @@ runAPIServer host port party tracer history chain callback headStatusP snapshotU
let k = [queryKey|tx-output|]
v = [queryValue|cbor|]
queryP = QueryParam k v
in if queryP `elem` qp then OutputCBOR else OutputJSON
in case queryP `elem` qp of
True -> OutputCBOR
False -> OutputJSON

decideOnUTxODisplay qp =
let k = [queryKey|snapshot-utxo|]
v = [queryValue|no|]
queryP = QueryParam k v
in if queryP `elem` qp then WithoutUTxO else WithUTxO
in case queryP `elem` qp of
True -> WithoutUTxO
False -> WithUTxO

shouldNotServeHistory qp =
flip any qp $ \case
Expand Down Expand Up @@ -333,6 +318,28 @@ runAPIServer host port party tracer history chain callback headStatusP snapshotU
let encodeAndReverse xs serverOutput = Aeson.encode serverOutput : xs
sendTextDatas con $ foldl' encodeAndReverse [] hist

-- Handle user requests to obtain a draft commit tx
handleDraftCommitUtxo directChain req respond = do
body <- getWaiRequestBody req
case Aeson.eitherDecode' (fromStrict body) :: Either String (RestClientInput tx) of
Left err -> respond $ responseLBS status400 [] (show err)
Right requestInput -> do
traceWith tracer $
APIRestInputReceived
{ method = decodeUtf8 $ requestMethod req
, paths = pathInfo req
, requestInputBody = Just $ toJSON requestInput
}
let Chain{draftTx} = directChain
let userUtxo = utxo requestInput
eCommitTx <- draftTx userUtxo
respond $
case eCommitTx of
Left err -> responseLBS status400 [] (show err)
Right commitTx -> do
let encodedRestOutput = Aeson.encode $ DraftedCommitTx commitTx
responseLBS status200 [] encodedRestOutput

data RunServerException = RunServerException
{ ioException :: IOException
, host :: IP
Expand All @@ -342,6 +349,7 @@ data RunServerException = RunServerException

instance Exception RunServerException

-- | Helper function to grab all of the request body contents
getWaiRequestBody :: Request -> IO ByteString
getWaiRequestBody request = BS.concat <$> getChunks
where
Expand Down

0 comments on commit c9bb964

Please sign in to comment.