Skip to content

Commit 95aa81f

Browse files
committed
Don't use separate handle field to check deposits
Signed-off-by: Sasha Bogicevic <[email protected]>
1 parent a6704f8 commit 95aa81f

File tree

9 files changed

+21
-35
lines changed

9 files changed

+21
-35
lines changed

hydra-node/src/Hydra/API/HTTPServer.hs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import Data.Text (pack)
1313
import Hydra.API.APIServerLog (APIServerLog (..), Method (..), PathInfo (..))
1414
import Hydra.API.ClientInput (ClientInput (..))
1515
import Hydra.API.ServerOutput (CommitInfo (..), getConfirmedSnapshot, getSeenSnapshot, getSnapshotUtxo)
16-
import Hydra.Cardano.Api (LedgerEra, Tx)
16+
import Hydra.Cardano.Api (
17+
LedgerEra,
18+
Tx,
19+
)
1720
import Hydra.Chain (Chain (..), PostTxError (..), draftCommitTx)
1821
import Hydra.Chain.ChainState (
1922
IsChainState,
@@ -236,29 +239,20 @@ handleDraftCommitUtxo env pparams directChain getCommitInfo body = do
236239
SimpleCommitRequest{utxoToCommit} -> do
237240
let blueprintTx = txSpendingUTxO utxoToCommit
238241
draftCommit headId utxoToCommit blueprintTx
239-
IncrementalCommit headId ->
242+
IncrementalCommit headId -> do
240243
case someCommitRequest of
241-
FullCommitRequest{blueprintTx, utxo} ->
242-
checkDeposit' utxo $
243-
deposit headId CommitBlueprintTx{blueprintTx, lookupUTxO = utxo}
244+
FullCommitRequest{blueprintTx, utxo} -> do
245+
deposit headId CommitBlueprintTx{blueprintTx, lookupUTxO = utxo}
244246
SimpleCommitRequest{utxoToCommit} ->
245-
checkDeposit' utxoToCommit $
246-
deposit headId CommitBlueprintTx{blueprintTx = txSpendingUTxO utxoToCommit, lookupUTxO = utxoToCommit}
247+
deposit headId CommitBlueprintTx{blueprintTx = txSpendingUTxO utxoToCommit, lookupUTxO = utxoToCommit}
247248
CannotCommit -> pure $ responseLBS status500 [] (Aeson.encode (FailedToDraftTxNotInitializing :: PostTxError tx))
248249
where
249-
checkDeposit' utxo cont = do
250-
depositCheck <- checkDeposit pparams utxo
251-
case depositCheck of
252-
Left e -> pure $ responseLBS status400 jsonContent (Aeson.encode $ toJSON e)
253-
Right _ -> cont
254-
255250
deposit headId commitBlueprint = do
256251
-- NOTE: Three times deposit period means we have one deposit period time to
257252
-- increment because a deposit only activates after one deposit period and
258253
-- expires one deposit period before deadline.
259-
--
260254
deadline <- addUTCTime (3 * toNominalDiffTime depositPeriod) <$> getCurrentTime
261-
draftDepositTx headId commitBlueprint deadline <&> \case
255+
draftDepositTx headId pparams commitBlueprint deadline <&> \case
262256
Left e -> responseLBS status400 jsonContent (Aeson.encode $ toJSON e)
263257
Right depositTx -> okJSON $ DraftCommitTxResponse depositTx
264258

@@ -271,12 +265,11 @@ handleDraftCommitUtxo env pparams directChain getCommitInfo body = do
271265
CommittedTooMuchADAForMainnet _ _ -> badRequest e
272266
UnsupportedLegacyOutput _ -> badRequest e
273267
CannotFindOwnInitial _ -> badRequest e
274-
DepositTooLow _ _ -> badRequest e
275268
_ -> responseLBS status500 [] (Aeson.encode $ toJSON e)
276269
Right commitTx ->
277270
okJSON $ DraftCommitTxResponse commitTx
278271

279-
Chain{draftCommitTx, draftDepositTx, checkDeposit} = directChain
272+
Chain{draftCommitTx, draftDepositTx} = directChain
280273

281274
Environment{depositPeriod} = env
282275

hydra-node/src/Hydra/Chain.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ data Chain tx m = Chain
271271
, draftDepositTx ::
272272
MonadThrow m =>
273273
HeadId ->
274+
PParams LedgerEra ->
274275
CommitBlueprintTx tx ->
275276
UTCTime ->
276277
m (Either (PostTxError tx) tx)
@@ -284,7 +285,6 @@ data Chain tx m = Chain
284285
--
285286
-- XXX: While technically they could be any of 'PostTxError tx', only
286287
-- `FailedToPostTx` errors are expected here.
287-
, checkDeposit :: MonadThrow m => PParams LedgerEra -> UTxOType tx -> m (Either (PostTxError tx) ())
288288
}
289289

290290
data ChainEvent tx

hydra-node/src/Hydra/Chain/Direct/Handlers.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import Hydra.Cardano.Api (
2727
getChainPoint,
2828
getTxBody,
2929
getTxId,
30+
liftEither,
3031
shelleyBasedEra,
3132
throwError,
3233
)
@@ -178,12 +179,13 @@ mkChain tracer queryTimeHandle wallet ctx LocalChainState{getLatest} submitTx =
178179
let CommitBlueprintTx{lookupUTxO} = commitBlueprintTx
179180
traverse (finalizeTx wallet ctx spendableUTxO lookupUTxO) $
180181
commit' ctx headId spendableUTxO commitBlueprintTx
181-
, draftDepositTx = \headId commitBlueprintTx deadline -> do
182+
, draftDepositTx = \headId pparams commitBlueprintTx deadline -> do
182183
let CommitBlueprintTx{lookupUTxO} = commitBlueprintTx
183184
ChainStateAt{spendableUTxO} <- atomically getLatest
184185
TimeHandle{currentPointInTime} <- queryTimeHandle
185186
-- XXX: What an error handling mess
186187
runExceptT $ do
188+
liftEither $ rejectLowDeposits pparams lookupUTxO
187189
(currentSlot, currentTime) <- case currentPointInTime of
188190
Left failureReason -> throwError FailedToConstructDepositTx{failureReason}
189191
Right (s, t) -> pure (s, t)
@@ -200,12 +202,10 @@ mkChain tracer queryTimeHandle wallet ctx LocalChainState{getLatest} submitTx =
200202
, -- Submit a cardano transaction to the cardano-node using the
201203
-- LocalTxSubmission protocol.
202204
submitTx
203-
, -- Check if deposit value is higher than 'minUTxOValue'. Throw 'DepositTooLow' if
204-
-- the provided UTxO value is too low.
205-
checkDeposit = \pparams userUTxO -> pure $ rejectLowDeposits pparams userUTxO
206205
}
207206

208207
-- Check each UTxO entry against the minADAUTxO value.
208+
-- Throws 'DepositTooLow' exception.
209209
rejectLowDeposits :: PParams LedgerEra -> UTxO.UTxO -> Either (PostTxError Tx) ()
210210
rejectLowDeposits pparams utxo = do
211211
let insAndOuts = UTxO.toList utxo

hydra-node/src/Hydra/Chain/Offline.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,8 @@ withOfflineChain config party otherParties chainStateHistory callback action = d
8686
{ mkChainState = initialChainState
8787
, submitTx = const $ pure ()
8888
, draftCommitTx = \_ _ -> pure $ Left FailedToDraftTxNotInitializing
89-
, draftDepositTx = \_ _ _ -> pure $ Left FailedToConstructDepositTx{failureReason = "not implemented"}
89+
, draftDepositTx = \_ _ _ _ -> pure $ Left FailedToConstructDepositTx{failureReason = "not implemented"}
9090
, postTx = const $ pure ()
91-
, checkDeposit = \_ _ -> pure $ Right ()
9291
}
9392

9493
initializeOfflineHead = do

hydra-node/test/Hydra/API/HTTPServerSpec.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import Hydra.Cardano.Api (
2525
renderTxIn,
2626
serialiseToTextEnvelope,
2727
)
28-
import Hydra.Chain (Chain (draftCommitTx), PostTxError (..), checkDeposit, draftDepositTx)
28+
import Hydra.Chain (Chain (draftCommitTx), PostTxError (..), draftDepositTx)
2929
import Hydra.Chain.Direct.Handlers (rejectLowDeposits)
3030
import Hydra.HeadLogic.State (ClosedState (..), HeadState (..), SeenSnapshot (..))
3131
import Hydra.HeadLogicSpec (inIdleState)
@@ -463,8 +463,7 @@ apiServerSpec = do
463463
let failingChainHandle postTxError =
464464
dummyChainHandle
465465
{ draftCommitTx = \_ _ -> pure $ Left postTxError
466-
, draftDepositTx = \_ _ _ -> pure $ Left postTxError
467-
, checkDeposit = \_ _ -> pure $ Left postTxError
466+
, draftDepositTx = \_ _ _ _ -> pure $ Left postTxError
468467
}
469468

470469
prop "reject deposits with less than min ADA" $ do

hydra-node/test/Hydra/API/ServerSpec.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import Hydra.API.ServerOutput (InvalidInput (..), input)
3131
import Hydra.API.ServerOutputFilter (ServerOutputFilter (..))
3232
import Hydra.Chain (
3333
Chain (Chain),
34-
checkDeposit,
3534
draftCommitTx,
3635
draftDepositTx,
3736
mkChainState,
@@ -381,7 +380,6 @@ dummyChainHandle =
381380
, draftCommitTx = \_ -> error "unexpected call to draftCommitTx"
382381
, draftDepositTx = \_ -> error "unexpected call to draftDepositTx"
383382
, submitTx = \_ -> error "unexpected call to submitTx"
384-
, checkDeposit = \_ -> error "unexpected call to checkDeposit"
385383
}
386384

387385
allowEverythingServerOutputFilter :: ServerOutputFilter tx

hydra-node/test/Hydra/BehaviorSpec.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,6 @@ simulatedChainAndNetwork initialChainState = do
10851085
, draftCommitTx = \_ -> error "unexpected call to draftCommitTx"
10861086
, draftDepositTx = \_ -> error "unexpected call to draftDepositTx"
10871087
, submitTx = \_ -> error "unexpected call to submitTx"
1088-
, checkDeposit = \_ -> error "unexpected call to checkDeposit"
10891088
}
10901089
mockNetwork = createMockNetwork draftNode nodes
10911090
mockServer = Server{sendMessage = const $ pure ()}

hydra-node/test/Hydra/Model/MockChain.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ import Hydra.Tx.Party (Party (..), deriveParty, getParty)
8282
import Hydra.Tx.ScriptRegistry (registryUTxO)
8383
import Hydra.Tx.Snapshot (ConfirmedSnapshot (..))
8484
import Hydra.Tx.Utils (verificationKeyToOnChainId)
85-
import Test.Hydra.Tx.Fixture (testNetworkId)
85+
import Test.Hydra.Tx.Fixture (defaultPParams, testNetworkId)
8686
import Test.Hydra.Tx.Gen (genScriptRegistry, genTxOutAdaOnly)
8787
import Test.QuickCheck (getPositive)
8888

@@ -215,7 +215,7 @@ mockChainAndNetwork tr seedKeys commits = do
215215
readTVarIO nodes >>= \case
216216
[] -> error "simulateDeposit: no MockHydraNode"
217217
(MockHydraNode{node = HydraNode{oc = Chain{submitTx, draftDepositTx}}} : _) ->
218-
draftDepositTx headId (mkSimpleBlueprintTx utxoToDeposit) deadline >>= \case
218+
draftDepositTx headId defaultPParams (mkSimpleBlueprintTx utxoToDeposit) deadline >>= \case
219219
Left e -> throwIO e
220220
Right tx -> submitTx tx $> txId tx
221221

hydra-node/test/Hydra/NodeSpec.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,8 @@ mockChain =
345345
{ mkChainState = error "mockChain: unexpected mkChainState"
346346
, postTx = \_ -> pure ()
347347
, draftCommitTx = \_ _ -> failure "mockChain: unexpected draftCommitTx"
348-
, draftDepositTx = \_ _ _ -> failure "mockChain: unexpected draftDepositTx"
348+
, draftDepositTx = \_ _ _ _ -> failure "mockChain: unexpected draftDepositTx"
349349
, submitTx = \_ -> failure "mockChain: unexpected submitTx"
350-
, checkDeposit = \_ _ -> failure "mockChain: unexpected checkDeposit"
351350
}
352351

353352
mockSink :: Monad m => EventSink a m
@@ -503,6 +502,5 @@ throwExceptionOnPostTx exception node =
503502
, draftCommitTx = \_ -> error "draftCommitTx not implemented"
504503
, draftDepositTx = \_ -> error "draftDepositTx not implemented"
505504
, submitTx = \_ -> error "submitTx not implemented"
506-
, checkDeposit = \_ _ -> error "checkDeposit not implemented"
507505
}
508506
}

0 commit comments

Comments
 (0)