@@ -21,6 +21,7 @@ import Hydra.Chain.Direct.State ()
21
21
import Hydra.HeadLogic.State (HeadState (.. ))
22
22
import Hydra.Ledger (ValidationError (.. ))
23
23
import Hydra.Logging (Tracer , traceWith )
24
+ import Hydra.Node.ApiTransactionTimeout (ApiTransactionTimeout (.. ))
24
25
import Hydra.Node.DepositPeriod (toNominalDiffTime )
25
26
import Hydra.Node.Environment (Environment (.. ))
26
27
import Hydra.Tx (CommitBlueprintTx (.. ), ConfirmedSnapshot , IsTx (.. ), Snapshot (.. ), UTxOType )
@@ -124,45 +125,45 @@ instance (Arbitrary tx, Arbitrary (UTxOType tx), IsTx tx) => Arbitrary (SideLoad
124
125
shrink = \ case
125
126
SideLoadSnapshotRequest snapshot -> SideLoadSnapshotRequest <$> shrink snapshot
126
127
127
- -- | Request to submit a Hydra transaction to the head
128
+ -- | Request to submit a transaction to the head
128
129
newtype SubmitHydraTxRequest tx = SubmitHydraTxRequest
129
130
{ submitHydraTx :: tx
130
131
}
131
132
deriving newtype (Eq , Show , Arbitrary )
132
133
deriving newtype (ToJSON , FromJSON )
133
134
134
- -- | Response for Hydra transaction submission
135
+ -- | Response for transaction submission
135
136
data SubmitHydraTxResponse
136
137
= -- | Transaction was included in a confirmed snapshot
137
- SubmitHydraTxConfirmed Integer
138
+ SubmitTxConfirmed Integer
138
139
| -- | Transaction was rejected due to validation errors
139
- SubmitHydraTxInvalidResponse Text
140
+ SubmitTxInvalidResponse Text
140
141
| -- | Transaction was accepted but not yet confirmed
141
- SubmitHydraTxSubmitted
142
+ SubmitTxSubmitted
142
143
deriving stock (Eq , Show , Generic )
143
144
144
145
instance ToJSON SubmitHydraTxResponse where
145
146
toJSON = \ case
146
- SubmitHydraTxConfirmed snapshotNumber ->
147
+ SubmitTxConfirmed snapshotNumber ->
147
148
object
148
- [ " tag" .= Aeson. String " SubmitHydraTxConfirmed "
149
+ [ " tag" .= Aeson. String " SubmitTxConfirmed "
149
150
, " snapshotNumber" .= snapshotNumber
150
151
]
151
- SubmitHydraTxInvalidResponse validationError ->
152
+ SubmitTxInvalidResponse validationError ->
152
153
object
153
- [ " tag" .= Aeson. String " SubmitHydraTxInvalid "
154
+ [ " tag" .= Aeson. String " SubmitTxInvalid "
154
155
, " validationError" .= validationError
155
156
]
156
- SubmitHydraTxSubmitted -> object [" tag" .= Aeson. String " SubmitHydraTxSubmitted " ]
157
+ SubmitTxSubmitted -> object [" tag" .= Aeson. String " SubmitTxSubmitted " ]
157
158
158
159
instance FromJSON SubmitHydraTxResponse where
159
- parseJSON = withObject " SubmitHydraTxResponse " $ \ o -> do
160
+ parseJSON = withObject " SubmitTxResponse " $ \ o -> do
160
161
tag <- o .: " tag"
161
162
case tag :: Text of
162
- " SubmitHydraTxConfirmed " -> SubmitHydraTxConfirmed <$> o .: " snapshotNumber"
163
- " SubmitHydraTxInvalid " -> SubmitHydraTxInvalidResponse <$> o .: " validationError"
164
- " SubmitHydraTxSubmitted " -> pure SubmitHydraTxSubmitted
165
- _ -> fail " Expected tag to be SubmitHydraTxConfirmed, SubmitHydraTxInvalid , or SubmitHydraTxSubmitted "
163
+ " SubmitTxConfirmed " -> SubmitTxConfirmed <$> o .: " snapshotNumber"
164
+ " SubmitTxInvalid " -> SubmitTxInvalidResponse <$> o .: " validationError"
165
+ " SubmitTxSubmitted " -> pure SubmitTxSubmitted
166
+ _ -> fail " Expected tag to be SubmitTxConfirmed, SubmitTxInvalid , or SubmitTxSubmitted "
166
167
167
168
instance Arbitrary SubmitHydraTxResponse where
168
169
arbitrary = genericArbitrary
@@ -187,7 +188,7 @@ httpApp ::
187
188
-- | Callback to yield a 'ClientInput' to the main event loop.
188
189
(ClientInput tx -> IO () ) ->
189
190
-- | Timeout for transaction submission
190
- NominalDiffTime ->
191
+ ApiTransactionTimeout ->
191
192
-- | Channel to listen for events
192
193
TChan (Either (TimedServerOutput tx ) (ClientMessage tx )) ->
193
194
Application
@@ -377,7 +378,7 @@ handleSubmitHydraTx ::
377
378
forall tx .
378
379
IsChainState tx =>
379
380
(ClientInput tx -> IO () ) ->
380
- NominalDiffTime ->
381
+ ApiTransactionTimeout ->
381
382
TChan (Either (TimedServerOutput tx ) (ClientMessage tx )) ->
382
383
LBS. ByteString ->
383
384
IO Response
@@ -390,15 +391,18 @@ handleSubmitHydraTx putClientInput apiTransactionTimeout responseChannel body =
390
391
putClientInput (NewTx submitHydraTx)
391
392
392
393
let txid = txId submitHydraTx
393
- result <- timeout (realToFrac apiTransactionTimeout) (waitForTransactionResult txid)
394
+ result <-
395
+ timeout
396
+ (realToFrac (apiTransactionTimeoutNominalDiffTime apiTransactionTimeout))
397
+ (waitForTransactionResult txid)
394
398
395
399
case result of
396
- Just (SubmitHydraTxConfirmed snapshotNumber) ->
397
- pure $ responseLBS status200 jsonContent (Aeson. encode $ SubmitHydraTxConfirmed snapshotNumber)
398
- Just (SubmitHydraTxInvalidResponse validationError) ->
399
- pure $ responseLBS status400 jsonContent (Aeson. encode $ SubmitHydraTxInvalidResponse validationError)
400
- Just SubmitHydraTxSubmitted ->
401
- pure $ responseLBS status202 jsonContent (Aeson. encode SubmitHydraTxSubmitted )
400
+ Just (SubmitTxConfirmed snapshotNumber) ->
401
+ pure $ responseLBS status200 jsonContent (Aeson. encode $ SubmitTxConfirmed snapshotNumber)
402
+ Just (SubmitTxInvalidResponse validationError) ->
403
+ pure $ responseLBS status400 jsonContent (Aeson. encode $ SubmitTxInvalidResponse validationError)
404
+ Just SubmitTxSubmitted ->
405
+ pure $ responseLBS status202 jsonContent (Aeson. encode SubmitTxSubmitted )
402
406
Nothing ->
403
407
-- Timeout occurred - return 202 Accepted with timeout info
404
408
pure $
@@ -407,30 +411,29 @@ handleSubmitHydraTx putClientInput apiTransactionTimeout responseChannel body =
407
411
jsonContent
408
412
( Aeson. encode $
409
413
object
410
- [ " tag" .= Aeson. String " SubmitHydraTxSubmitted "
414
+ [ " tag" .= Aeson. String " SubmitTxSubmitted "
411
415
, " timeout" .= Aeson. String (" Transaction submission timed out after " <> pack (show apiTransactionTimeout) <> " seconds" )
412
416
]
413
417
)
414
418
where
415
419
-- Wait for transaction result by listening to events
416
420
waitForTransactionResult :: TxIdType tx -> IO SubmitHydraTxResponse
417
- waitForTransactionResult txid = do
418
- go
421
+ waitForTransactionResult txid = go
419
422
where
420
423
go = do
421
424
event <- atomically $ readTChan responseChannel
422
425
case event of
423
426
Left (TimedServerOutput {output}) -> case output of
424
427
TxValid {transactionId}
425
428
| transactionId == txid ->
426
- pure SubmitHydraTxSubmitted
429
+ pure SubmitTxSubmitted
427
430
TxInvalid {transaction, validationError = ValidationError reason}
428
431
| txId transaction == txid ->
429
- pure $ SubmitHydraTxInvalidResponse reason
432
+ pure $ SubmitTxInvalidResponse reason
430
433
SnapshotConfirmed {snapshot} ->
431
434
-- Check if the transaction is in the confirmed snapshot
432
435
if txid `elem` map txId (confirmed snapshot)
433
- then pure $ SubmitHydraTxConfirmed (fromIntegral $ number snapshot)
436
+ then pure $ SubmitTxConfirmed (fromIntegral $ number snapshot)
434
437
else go
435
438
_ -> go
436
439
Right _ -> go
0 commit comments