Skip to content

Commit

Permalink
Use path instead of query string for recover
Browse files Browse the repository at this point in the history
  • Loading branch information
v0d1ch committed Sep 25, 2024
1 parent f09f7d5 commit 8c21c54
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 44 deletions.
2 changes: 1 addition & 1 deletion hydra-cluster/hydra-cluster.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ library
, aeson
, async
, base >=4.7 && <5
, base16-bytestring
, bytestring
, cardano-slotting
, containers
Expand All @@ -95,6 +94,7 @@ library
, directory
, filepath
, http-conduit
, http-types
, hydra-cardano-api
, hydra-node
, hydra-prelude
Expand Down
6 changes: 3 additions & 3 deletions hydra-cluster/src/Hydra/Cluster/Scenarios.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import Data.Aeson.Lens (key, values, _JSON)
import Data.Aeson.Types (parseMaybe)
import Data.ByteString (isInfixOf)
import Data.ByteString qualified as B
import Data.ByteString.Base16 qualified as Base16
import Data.ByteString.Char8 qualified as BSC
import Data.List qualified as List
import Data.Set qualified as Set
Expand Down Expand Up @@ -102,6 +101,7 @@ import Network.HTTP.Req (
(/:),
)
import Network.HTTP.Simple (getResponseBody, httpJSON, setRequestBodyJSON)
import Network.HTTP.Types (urlEncode)
import System.Directory (removeDirectoryRecursive)
import System.FilePath ((</>))
import Test.Hydra.Tx.Gen (genKeyPair)
Expand Down Expand Up @@ -702,11 +702,11 @@ canRecoverDeposit tracer workDir node hydraScriptsTxId =
(selectLovelace . balance <$> queryUTxOFor networkId nodeSocket QueryTip walletVk)
`shouldReturn` 0

let queryStr = BSC.unpack $ Base16.encode $ encodeUtf8 $ "\"" <> renderTxIn (fst . List.head . UTxO.pairs $ utxoFromTx tx) <> "\""
let path = BSC.unpack $ urlEncode False $ encodeUtf8 $ "\"" <> renderTxIn (fst . List.head . UTxO.pairs $ utxoFromTx tx) <> "\""

recoverResp <-
-- TODO: use slash instead of question mark to utilize the path instead of query params
parseUrlThrow ("DELETE " <> hydraNodeBaseUrl n1 <> "/commits?" <> queryStr)
parseUrlThrow ("DELETE " <> hydraNodeBaseUrl n1 <> "/commits/" <> path)
>>= httpJSON

(getResponseBody recoverResp :: String) `shouldBe` "OK"
Expand Down
75 changes: 35 additions & 40 deletions hydra-node/src/Hydra/API/HTTPServer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import Hydra.Prelude
import Cardano.Ledger.Core (PParams)
import Data.Aeson (KeyValue ((.=)), object, withObject, (.:))
import Data.Aeson qualified as Aeson
import Data.ByteString.Base16 qualified as Base16
import Data.ByteString.Lazy qualified as LBS
import Data.ByteString.Short ()
import Data.Text (pack)
Expand All @@ -30,13 +29,12 @@ import Hydra.Tx (
IsTx (..),
UTxOType,
)
import Network.HTTP.Types (QueryItem, status200, status400, status404, status500)
import Network.HTTP.Types (status200, status400, status404, status500)
import Network.Wai (
Application,
Request (pathInfo, requestMethod),
Response,
consumeRequestBodyStrict,
queryString,
rawPathInfo,
responseLBS,
)
Expand Down Expand Up @@ -152,33 +150,33 @@ httpApp tracer directChain pparams getCommitInfo getConfirmedUTxO putClientInput
, path = PathInfo $ rawPathInfo request
}
case (requestMethod request, pathInfo request) of
("GET", ["snapshot", "utxo"]) ->
-- XXX: Should ensure the UTxO is of the right head and the head is still
-- open. This is something we should fix on the "read model" side of the
-- server.
getConfirmedUTxO >>= \case
Nothing -> respond notFound
Just utxo -> respond $ okJSON utxo
("POST", ["commit"]) ->
consumeRequestBodyStrict request
>>= handleDraftCommitUtxo directChain getCommitInfo
>>= respond
("DELETE", ["commits"]) ->
consumeRequestBodyStrict request
>>= handleRecoverCommitUtxo directChain putClientInput (queryString request)
>>= respond
("POST", ["decommit"]) ->
consumeRequestBodyStrict request
>>= handleDecommit putClientInput
>>= respond
("GET", ["protocol-parameters"]) ->
respond . responseLBS status200 [] . Aeson.encode $ pparams
("POST", ["cardano-transaction"]) ->
consumeRequestBodyStrict request
>>= handleSubmitUserTx directChain
>>= respond
_ ->
respond $ responseLBS status400 [] "Resource not found"
("GET", ["snapshot", "utxo"]) ->
-- XXX: Should ensure the UTxO is of the right head and the head is still
-- open. This is something we should fix on the "read model" side of the
-- server.
getConfirmedUTxO >>= \case
Nothing -> respond notFound
Just utxo -> respond $ okJSON utxo
("POST", ["commit"]) ->
consumeRequestBodyStrict request
>>= handleDraftCommitUtxo directChain getCommitInfo
>>= respond
("DELETE", ["commits", _]) ->
consumeRequestBodyStrict request
>>= handleRecoverCommitUtxo directChain putClientInput (last . fromList $ pathInfo request)
>>= respond
("POST", ["decommit"]) ->
consumeRequestBodyStrict request
>>= handleDecommit putClientInput
>>= respond
("GET", ["protocol-parameters"]) ->
respond . responseLBS status200 [] . Aeson.encode $ pparams
("POST", ["cardano-transaction"]) ->
consumeRequestBodyStrict request
>>= handleSubmitUserTx directChain
>>= respond
_ ->
respond $ responseLBS status400 [] "Resource not found"

-- * Handlers

Expand Down Expand Up @@ -245,11 +243,11 @@ handleRecoverCommitUtxo ::
IsChainState tx =>
Chain tx IO ->
(ClientInput tx -> IO ()) ->
[QueryItem] ->
Text ->
LBS.ByteString ->
IO Response
handleRecoverCommitUtxo directChain putClientInput recoverQuery _body = do
case checkRecover recoverQuery of
handleRecoverCommitUtxo directChain putClientInput recoverPath _body = do
case parseTxInFromPath recoverPath of
Left err -> pure err
Right recoverTxIn ->
draftRecoverTx recoverTxIn >>= \case
Expand All @@ -258,13 +256,10 @@ handleRecoverCommitUtxo directChain putClientInput recoverQuery _body = do
putClientInput Recover{recoverTx}
pure $ responseLBS status200 [] (Aeson.encode $ Aeson.String "OK")
where
checkRecover query =
case Base16.decode $ foldMap fst query of
Left e -> Left (responseLBS status400 [] (Aeson.encode $ Aeson.String $ "Cannot recover funds. Failed to decode TxIn string: " <> pack e))
Right txInStr ->
case Aeson.eitherDecode (fromStrict txInStr) :: Either String TxIn of
Left e -> Left (responseLBS status400 [] (Aeson.encode $ Aeson.String $ "Cannot recover funds. Failed to parse TxIn: " <> pack e))
Right txIn -> Right txIn
parseTxInFromPath txInStr =
case Aeson.eitherDecode (encodeUtf8 txInStr) :: Either String TxIn of
Left e -> Left (responseLBS status400 [] (Aeson.encode $ Aeson.String $ "Cannot recover funds. Failed to parse TxIn: " <> pack e))
Right txIn -> Right txIn

Chain{draftRecoverTx} = directChain

Expand Down

0 comments on commit 8c21c54

Please sign in to comment.