Skip to content

Commit c587974

Browse files
committed
take ChainOutOfSync type out from the server output hierarchy
1 parent 67d97c8 commit c587974

File tree

5 files changed

+44
-21
lines changed

5 files changed

+44
-21
lines changed

hydra-node/golden/ServerOutput/ChainOutOfSync.json renamed to hydra-node/golden/ChainOutOfSync/ChainOutOfSync.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
{
44
"syncedStatus": {
55
"point": {
6-
"blockHash": "0101000000000000000000000000010001000100010000010100010101010000",
7-
"slot": 0,
6+
"blockHash": "0001000001000100010000010000010000000100010001010100010001010001",
7+
"slot": 1,
88
"tag": "ChainPoint"
99
},
1010
"tip": {
11-
"blockHash": "0101000001000101010001000101000001010101010101000101010101010001",
12-
"slot": 1,
11+
"blockHash": "0101000101000100000101010101010000010100000001010101010001000001",
12+
"slot": 0,
1313
"tag": "ChainPoint"
1414
}
1515
},
1616
"tag": "ChainOutOfSync"
1717
}
1818
],
19-
"seed": -429184662
19+
"seed": -455573634
2020
}

hydra-node/json-schemas/api.yaml

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ channels:
168168
* DraftCommitTxResponse - 200 ok
169169
* CommittedTooMuchADAForMainnet - 400 bad request
170170
* UnsupportedLegacyOutput - 400 bad request
171+
* ChainOutOfSync - 503 service unavailable
171172
message:
172173
$ref: "api.yaml#/components/messages/DraftCommitTxResponse"
173174
bindings:
@@ -227,6 +228,7 @@ channels:
227228
Possible responses of this endpoint are:
228229
* 200 OK
229230
* 400 Bad Request
231+
* 503 Service Unavailable
230232
message:
231233
payload:
232234
type: string
@@ -333,6 +335,7 @@ channels:
333335
Possible responses of this endpoint are:
334336
* String - 200 OK
335337
* String - 400 bad request
338+
* ChainOutOfSync - 503 service unavailable
336339
message:
337340
payload:
338341
$ref: "api.yaml#/components/messages/DecommitResponse"
@@ -389,6 +392,10 @@ channels:
389392
summary: 400 Bad Request
390393
payload:
391394
$ref: "api.yaml#/components/schemas/PostTxError"
395+
- name: ChainOutOfSync
396+
summary: 503 Service Unavailable
397+
payload:
398+
$ref: "api.yaml#/components/schemas/ChainOutOfSync"
392399
bindings:
393400
http:
394401
type: response
@@ -450,6 +457,10 @@ channels:
450457
tag:
451458
type: string
452459
enum: ["SubmitTxSubmitted"]
460+
- name: ChainOutOfSync
461+
summary: 503 Service Unavailable
462+
payload:
463+
$ref: "api.yaml#/components/schemas/ChainOutOfSync"
453464
bindings:
454465
http:
455466
type: response
@@ -1804,19 +1815,13 @@ components:
18041815
type: object
18051816
required:
18061817
- tag
1807-
- seq
18081818
- syncedStatus
1809-
- timestamp
18101819
properties:
18111820
tag:
18121821
type: string
18131822
enum: ["ChainOutOfSync"]
1814-
seq:
1815-
$ref: "api.yaml#/components/schemas/SequenceNumber"
18161823
syncedStatus:
18171824
$ref: "api.yaml#/components/schemas/SyncedStatus"
1818-
timestamp:
1819-
$ref: "api.yaml#/components/schemas/UTCTime"
18201825

18211826
# END OF SERVER OUTPUT SCHEMAS
18221827

@@ -3582,7 +3587,7 @@ components:
35823587
properties:
35833588
point:
35843589
oneOf:
3585-
- $ref: "api.yaml#/components/schemas/ChainPoint"
3586-
- type: "null"
3590+
- $ref: "api.yaml#/components/schemas/ChainPoint"
3591+
- type: "null"
35873592
tip:
35883593
$ref: "api.yaml#/components/schemas/ChainPoint"

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
module Hydra.API.ServerOutput where
66

77
import Control.Lens ((.~))
8-
import Data.Aeson (Value (..), defaultOptions, encode, genericParseJSON, genericToJSON, omitNothingFields, tagSingleConstructors, withObject, (.:))
8+
import Data.Aeson (Value (..), defaultOptions, encode, genericParseJSON, genericToJSON, object, omitNothingFields, tagSingleConstructors, withObject, (.:), (.=))
99
import Data.Aeson.KeyMap qualified as KeyMap
1010
import Data.Aeson.Lens (atKey, key)
1111
import Data.ByteString.Lazy qualified as LBS
@@ -142,6 +142,21 @@ data InvalidInput = InvalidInput
142142
deriving instance ToJSON InvalidInput
143143
deriving instance FromJSON InvalidInput
144144

145+
newtype ChainOutOfSync = ChainOutOfSync {syncedStatus :: SyncedStatus}
146+
deriving newtype (Eq, Show)
147+
deriving (Generic)
148+
deriving newtype (Arbitrary)
149+
150+
instance ToJSON ChainOutOfSync where
151+
toJSON (ChainOutOfSync s) =
152+
object ["tag" .= String "ChainOutOfSync", "syncedStatus" .= s]
153+
154+
instance FromJSON ChainOutOfSync where
155+
parseJSON = withObject "ChainOutOfSync" $ \o ->
156+
ChainOutOfSync <$> o .: "syncedStatus"
157+
158+
instance ToADTArbitrary ChainOutOfSync
159+
145160
data ServerOutput tx
146161
= NetworkConnected
147162
| NetworkDisconnected
@@ -216,7 +231,6 @@ data ServerOutput tx
216231
-- Any signing round has been discarded, and the snapshot leader has changed accordingly.
217232
SnapshotSideLoaded {headId :: HeadId, snapshotNumber :: SnapshotNumber}
218233
| EventLogRotated {checkpoint :: NodeState tx}
219-
| ChainOutOfSync {syncedStatus :: SyncedStatus}
220234
deriving stock (Generic)
221235

222236
deriving stock instance IsChainState tx => Eq (ServerOutput tx)
@@ -290,7 +304,6 @@ prepareServerOutput config response =
290304
PeerDisconnected{} -> encodedResponse
291305
SnapshotSideLoaded{} -> encodedResponse
292306
EventLogRotated{} -> encodedResponse
293-
ChainOutOfSync{} -> encodedResponse
294307
where
295308
encodedResponse = encode response
296309

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ import Hydra.API.APIServerLog (APIServerLog (..))
1717
import Hydra.API.ClientInput (ClientInput)
1818
import Hydra.API.Projection (Projection (..))
1919
import Hydra.API.ServerOutput (
20+
ChainOutOfSync (..),
2021
ClientMessage,
2122
Greetings (..),
2223
HeadStatus (..),
2324
InvalidInput (..),
2425
NetworkInfo,
25-
ServerOutput (ChainOutOfSync),
2626
ServerOutputConfig (..),
2727
TimedServerOutput (..),
2828
WithAddressedTx (..),
@@ -85,9 +85,9 @@ wsApp env party tracer history callback nodeStateP networkInfoP responseChannel
8585
-- we notify clients every time the chain is out of sync
8686
-- as their inputs will get rejected
8787
_ <- forkLabelled "ws-check-sync-status" $ forever $ do
88-
synced <- chainSyncedStatus
89-
unless (status synced) $ do
90-
let output :: ServerOutput tx = ChainOutOfSync synced
88+
syncedStatus <- chainSyncedStatus
89+
unless (status syncedStatus) $ do
90+
let output = ChainOutOfSync syncedStatus
9191
sendTextData con (Aeson.encode output)
9292
-- check every second
9393
-- TODO! configure threadDelay

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Test.Hydra.Prelude
66
import Control.Lens (toListOf, (^.))
77
import Data.Aeson (Value)
88
import Data.Aeson.Lens (key, values, _Array)
9-
import Hydra.API.ServerOutput (ClientMessage, Greetings (..), ServerOutput, TimedServerOutput)
9+
import Hydra.API.ServerOutput (ChainOutOfSync (..), ClientMessage, Greetings (..), ServerOutput, TimedServerOutput)
1010
import Hydra.Chain.Direct.State ()
1111
import Hydra.JSONSchema (prop_specIsComplete, prop_validateJSONSchema)
1212
import Hydra.Ledger.Cardano (Tx)
@@ -22,6 +22,7 @@ spec :: Spec
2222
spec = parallel $ do
2323
roundtripAndGoldenADTSpecsWithSettings defaultSettings{sampleSize = 1} $ Proxy @(MinimumSized (ServerOutput Tx))
2424
roundtripAndGoldenADTSpecsWithSettings defaultSettings{sampleSize = 1} $ Proxy @(MinimumSized (Greetings Tx))
25+
roundtripAndGoldenADTSpecsWithSettings defaultSettings{sampleSize = 1} $ Proxy @(MinimumSized ChainOutOfSync)
2526

2627
-- NOTE: Kupo and maybe other downstream projects use this file as a test vector.
2728
it "golden SnapshotConfirmed is good" $ do
@@ -42,6 +43,8 @@ spec = parallel $ do
4243
key "components" . key "schemas" . key "Greetings"
4344
, prop_validateJSONSchema @(ClientMessage Tx) "api.json" $
4445
key "components" . key "schemas" . key "ClientMessage"
46+
, prop_validateJSONSchema @ChainOutOfSync "api.json" $
47+
key "components" . key "schemas" . key "ChainOutOfSync"
4548
]
4649

4750
-- XXX: This seems no to be working? Adding a new message does not lead to a failure here
@@ -53,4 +56,6 @@ spec = parallel $ do
5356
key "channels" . key "/" . key "subscribe" . key "message"
5457
, prop_specIsComplete @(ClientMessage Tx) "api.json" $
5558
key "channels" . key "/" . key "subscribe" . key "message"
59+
, prop_specIsComplete @ChainOutOfSync "api.json" $
60+
key "channels" . key "/" . key "subscribe" . key "message"
5661
]

0 commit comments

Comments
 (0)