Skip to content

Commit

Permalink
Connect chain handle to draft deposit tx
Browse files Browse the repository at this point in the history
Rename draftIncrementalCommitTx to draftDepositTx

Signed-off-by: Sasha Bogicevic <[email protected]>
  • Loading branch information
v0d1ch committed Sep 5, 2024
1 parent 9ccdbf7 commit 7ea2e9f
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 30 deletions.
21 changes: 8 additions & 13 deletions hydra-node/src/Hydra/API/HTTPServer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -225,23 +225,18 @@ handleDraftCommitUtxo directChain getCommitInfo putClientInput body = do
let blueprintTx = txSpendingUTxO utxoToCommit
draftCommit headId utxoToCommit blueprintTx
_ -> pure $ responseLBS status400 [] (Aeson.encode $ Aeson.String "Invalid request: expected a FullCommitRequest or SimpleCommitRequest")
IncrementalCommit -> do
IncrementalCommit headId -> do
case someCommitRequest of
IncrementalCommitDepositRequest{utxo, deadline} -> do
incrementalCommit utxo deadline
IncrementalCommitDepositRequest{utxo, deadline} -> deposit headId utxo deadline
_ -> pure $ responseLBS status400 [] (Aeson.encode $ Aeson.String "Invalid request: expected a IncrementalCommitDepositRequest or IncrementalCommitRecoverRequest")
-- XXX: This is not really an internal server error
-- FIXME: FailedToDraftTxNotInitializing is not a good name and it's not a PostTxError. Should create an error type somewhere in Hydra.API for this.
CannotCommit -> pure $ responseLBS status500 [] (Aeson.encode (FailedToDraftTxNotInitializing :: PostTxError tx))
where
incrementalCommit utxo deadline = do
putClientInput Commit{utxo}
-- TODO:
-- - Wait synchronysly
-- - Get a snapshot confirmed
-- - Construct incrementTx
-- - Return it
error "Not done yet"
deposit headId utxo deadline = do
draftDepositTx headId utxo deadline <&> \case
Left e -> responseLBS status500 [] (Aeson.encode $ toJSON e)
Right depositTx -> okJSON $ DraftCommitTxResponse depositTx

draftCommit headId lookupUTxO blueprintTx =
draftCommitTx headId CommitBlueprintTx{lookupUTxO, blueprintTx} <&> \case
Expand All @@ -254,7 +249,7 @@ handleDraftCommitUtxo directChain getCommitInfo putClientInput body = do
_ -> responseLBS status500 [] (Aeson.encode $ toJSON e)
Right commitTx ->
okJSON $ DraftCommitTxResponse commitTx
Chain{draftCommitTx} = directChain
Chain{draftCommitTx, draftDepositTx} = directChain

-- | Handle request to recover a pending deposit.
handleRecoverCommitUtxo ::
Expand All @@ -275,7 +270,7 @@ handleRecoverCommitUtxo directChain getCommitInfo putClientInput recoverQuery bo
pure $ responseLBS status400 [] (Aeson.encode $ Aeson.String $ pack err)
Right someCommitRequest ->
getCommitInfo >>= \case
IncrementalCommit -> do
IncrementalCommit _ -> do
case someCommitRequest of
IncrementalCommitRecoverRequest{utxo, deadline} -> do
recoverDeposit utxo deadline
Expand Down
4 changes: 2 additions & 2 deletions hydra-node/src/Hydra/API/ServerOutput.hs
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,15 @@ instance Arbitrary HeadStatus where
data CommitInfo
= CannotCommit
| NormalCommit HeadId
| IncrementalCommit
| IncrementalCommit HeadId

-- | Projection to obtain 'CommitInfo' needed to draft commit transactions.
-- NOTE: We only want to project 'HeadId' when the Head is in the 'Initializing'
-- state since this is when Head parties need to commit some funds.
projectCommitInfo :: CommitInfo -> ServerOutput tx -> CommitInfo
projectCommitInfo commitInfo = \case
HeadIsInitializing{headId} -> NormalCommit headId
HeadIsOpen{} -> IncrementalCommit
HeadIsOpen{headId} -> IncrementalCommit headId
HeadIsAborted{} -> CannotCommit
HeadIsClosed{} -> CannotCommit
_other -> commitInfo
Expand Down
9 changes: 5 additions & 4 deletions hydra-node/src/Hydra/Chain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,14 @@ data Chain tx m = Chain
-- ^ Create a commit transaction using user provided utxos (zero or many) and
-- a _blueprint_ transaction which spends these outputs.
-- Errors are handled at the call site.
, draftIncrementalCommitTx ::
, draftDepositTx ::
MonadThrow m =>
HeadId ->
CommitBlueprintTx tx ->
UTxOType tx ->
UTCTime ->
m (Either (PostTxError tx) tx)
-- ^ Create a **incremental** commit transaction using user provided utxos (zero or many) and
-- a _blueprint_ transaction which spends these outputs.
-- ^ Create a deposit transaction using user provided utxos (zero or many) and
-- a deadline for their inclusion into L2.
-- Errors are handled at the call site.
, submitTx :: MonadThrow m => tx -> m ()
-- ^ Submit a cardano transaction.
Expand Down
10 changes: 5 additions & 5 deletions hydra-node/src/Hydra/Chain/Direct/Handlers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import Hydra.Chain.Direct.State (
decrement,
fanout,
getKnownUTxO,
incrementalCommit,
initialize,
)
import Hydra.Chain.Direct.TimeHandle (TimeHandle (..))
Expand Down Expand Up @@ -171,11 +170,12 @@ mkChain tracer queryTimeHandle wallet ctx LocalChainState{getLatest} submitTx =
commit' ctx headId spendableUTxO commitBlueprintTx
, -- Handle that creates a draft **incremental** commit tx using the user utxo and a _blueprint_ transaction.
-- Possible errors are handled at the api server level.
draftIncrementalCommitTx = \headId commitBlueprintTx -> do
draftDepositTx = \headId utxo deadline -> do
ChainStateAt{spendableUTxO} <- atomically getLatest
let CommitBlueprintTx{lookupUTxO} = commitBlueprintTx
traverse (finalizeTx wallet ctx spendableUTxO lookupUTxO) $
incrementalCommit ctx headId spendableUTxO commitBlueprintTx
traverse (finalizeTx wallet ctx spendableUTxO utxo) $
-- TODO: merge a PR that contains depositTx
-- depositTx networkId headId utxo deadline
undefined
, -- Submit a cardano transaction to the cardano-node using the
-- LocalTxSubmission protocol.
submitTx
Expand Down
2 changes: 1 addition & 1 deletion hydra-node/src/Hydra/Chain/Offline.hs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ withOfflineChain nodeId OfflineChainConfig{ledgerGenesisFile, initialUTxOFile} p
Chain
{ submitTx = const $ pure ()
, draftCommitTx = \_ _ -> pure $ Left FailedToDraftTxNotInitializing
, draftIncrementalCommitTx = \_ _ -> pure $ Left FailedToDraftTxNotInitializing
, draftDepositTx = \_ _ _ -> pure $ Left FailedToDraftTxNotInitializing
, postTx = const $ pure ()
}

Expand Down
4 changes: 2 additions & 2 deletions hydra-node/test/Hydra/API/ServerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import Hydra.API.ServerOutput (ServerOutput (..), TimedServerOutput (..), genTim
import Hydra.Chain (
Chain (Chain),
draftCommitTx,
draftIncrementalCommitTx,
draftDepositTx,
postTx,
submitTx,
)
Expand Down Expand Up @@ -373,7 +373,7 @@ dummyChainHandle =
Chain
{ postTx = \_ -> error "unexpected call to postTx"
, draftCommitTx = \_ -> error "unexpected call to draftCommitTx"
, draftIncrementalCommitTx = \_ -> error "unexpected call to draftIncrementalCommitTx"
, draftDepositTx = \_ -> error "unexpected call to draftDepositTx"
, submitTx = \_ -> error "unexpected call to submitTx"
}

Expand Down
2 changes: 1 addition & 1 deletion hydra-node/test/Hydra/BehaviorSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ simulatedChainAndNetwork initialChainState = do
threadDelay blockTime
createAndYieldEvent nodes history localChainState $ toOnChainTx now tx
, draftCommitTx = \_ -> error "unexpected call to draftCommitTx"
, draftIncrementalCommitTx = \_ -> error "unexpected call to draftIncrementalCommitTx"
, draftDepositTx = \_ -> error "unexpected call to draftIncrementalCommitTx"
, submitTx = \_ -> error "unexpected call to submitTx"
}
mockNetwork = createMockNetwork draftNode nodes
Expand Down
4 changes: 2 additions & 2 deletions hydra-node/test/Hydra/NodeSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ mockChain =
Chain
{ postTx = \_ -> pure ()
, draftCommitTx = \_ _ -> failure "mockChain: unexpected draftCommitTx"
, draftIncrementalCommitTx = \_ _ -> failure "mockChain: unexpected draftIncrementalCommitTx"
, draftDepositTx = \_ _ _ -> failure "mockChain: unexpected draftDepositTx"
, submitTx = \_ -> failure "mockChain: unexpected submitTx"
}

Expand Down Expand Up @@ -456,7 +456,7 @@ throwExceptionOnPostTx exception node =
Chain
{ postTx = \_ -> throwIO exception
, draftCommitTx = \_ -> error "draftCommitTx not implemented"
, draftIncrementalCommitTx = \_ -> error "draftIncrementalCommitTx not implemented"
, draftDepositTx = \_ -> error "draftDepositTx not implemented"
, submitTx = \_ -> error "submitTx not implemented"
}
}

0 comments on commit 7ea2e9f

Please sign in to comment.