Skip to content

Commit fde4cf8

Browse files
Add instances needed to serialize a GenesisTest
1 parent 2ade2a5 commit fde4cf8

File tree

1 file changed

+129
-14
lines changed

1 file changed

+129
-14
lines changed
Lines changed: 129 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,155 @@
11
{-# LANGUAGE DeriveGeneric #-}
2+
{-# LANGUAGE DerivingVia #-}
3+
{-# LANGUAGE FlexibleContexts #-}
4+
{-# LANGUAGE OverloadedStrings #-}
25
{-# LANGUAGE StandaloneDeriving #-}
6+
{-# LANGUAGE UndecidableInstances #-}
37
{-# OPTIONS_GHC -Wno-orphans #-}
48

9+
-- | This module contains (orphan) instances to serialize a 'GenesisTest' for
10+
-- the @consensus-test-runner@ input test file.
11+
--
12+
-- Types with 'Generic' instances can derive 'ToJSON' and 'FromJSON'
13+
-- automatically; in turn, to standalone derive a 'Generic' instance, all of
14+
-- its constructors must in scope. Because of this, opaque data type instances
15+
-- need to be written manually.
16+
--
17+
-- Note that most of the constraints in this instance declarations are caused
18+
-- by the abstraction of the block type parameter.
519
module Test.Consensus.OrphanInstances () where
620

21+
import Cardano.Slotting.Time (SlotLength)
722
import Control.Monad.Class.MonadTime.SI (Time)
823
import Data.Aeson
9-
( FromJSON
10-
, ToJSON (toEncoding)
24+
( FromJSON (parseJSON)
25+
, KeyValue ((.=))
26+
, ToJSON (toEncoding, toJSON)
1127
, defaultOptions
1228
, genericToEncoding
29+
, object
30+
, withObject
31+
, (.:)
32+
)
33+
import GHC.Generics (Generic)
34+
import Ouroboros.Consensus.Block.Abstract (GenesisWindow (..))
35+
import Ouroboros.Consensus.Config (SecurityParam (..))
36+
import Ouroboros.Network.AnchoredFragment (Anchor)
37+
import Ouroboros.Network.AnchoredSeq
38+
( Anchorable (..)
39+
, AnchoredSeq (..)
40+
, fromOldestFirst
41+
, toOldestFirst
42+
)
43+
import Ouroboros.Network.Block (HasHeader, HeaderHash)
44+
import Test.Consensus.BlockTree (BlockTree (..), BlockTreeBranch (..))
45+
import Test.Consensus.PointSchedule
46+
( BlockFetchTimeout (..)
47+
, CSJParams (..)
48+
, ChainSyncTimeout (..)
49+
, ForecastRange (..)
50+
, GenesisTest (..)
51+
, LoPBucketParams (..)
52+
, PointSchedule (..)
1353
)
14-
import GHC.Generics
15-
import Test.Consensus.PointSchedule (PointSchedule (..))
1654
import Test.Consensus.PointSchedule.Peers (PeerId, Peers (..))
1755
import Test.Consensus.PointSchedule.SinglePeer (SchedulePoint (..))
56+
import Test.Ouroboros.Consensus.ChainGenerator.Params (Delta (..))
1857

19-
deriving instance Generic (PointSchedule blk)
20-
instance ToJSON blk => ToJSON (PointSchedule blk) where
58+
-- * Target instances
59+
60+
deriving instance Generic (GenesisTest blk schedule)
61+
instance
62+
(ToJSON (HeaderHash blk), HasHeader blk, ToJSON schedule, ToJSON blk) =>
63+
ToJSON (GenesisTest blk schedule)
64+
where
2165
toEncoding = genericToEncoding defaultOptions
66+
instance
67+
(FromJSON (HeaderHash blk), HasHeader blk, FromJSON schedule, FromJSON blk) =>
68+
FromJSON (GenesisTest blk schedule)
69+
70+
-- ** 'GenesisTest' field instances
71+
72+
instance ToJSON SecurityParam
73+
instance FromJSON SecurityParam
74+
75+
deriving instance Generic GenesisWindow
76+
instance ToJSON GenesisWindow
77+
instance FromJSON GenesisWindow
78+
79+
deriving instance Generic ForecastRange
80+
instance ToJSON ForecastRange
81+
instance FromJSON ForecastRange
82+
83+
deriving instance Generic Delta
84+
instance ToJSON Delta
85+
instance FromJSON Delta
86+
87+
deriving instance Generic (BlockTree blk)
88+
instance (ToJSON (HeaderHash blk), HasHeader blk, ToJSON blk) => ToJSON (BlockTree blk)
89+
instance (FromJSON (HeaderHash blk), HasHeader blk, FromJSON blk) => FromJSON (BlockTree blk)
90+
91+
deriving instance Generic ChainSyncTimeout
92+
instance ToJSON ChainSyncTimeout
93+
instance FromJSON ChainSyncTimeout
94+
95+
deriving instance Generic BlockFetchTimeout
96+
instance ToJSON BlockFetchTimeout
97+
instance FromJSON BlockFetchTimeout
98+
99+
deriving instance Generic LoPBucketParams
100+
instance ToJSON LoPBucketParams
101+
instance FromJSON LoPBucketParams
102+
103+
deriving instance Generic CSJParams
104+
instance ToJSON CSJParams
105+
instance FromJSON CSJParams
106+
107+
instance ToJSON SlotLength
108+
instance FromJSON SlotLength
109+
110+
deriving instance Generic (PointSchedule blk)
111+
instance ToJSON blk => ToJSON (PointSchedule blk)
22112
instance FromJSON blk => FromJSON (PointSchedule blk)
23113

24-
instance ToJSON Time where
25-
toEncoding = genericToEncoding defaultOptions
114+
-- *** 'BlockTree' field related instances
115+
116+
-- | The underlying 'AnchoredFragment' type of a 'BlockTreeBranch' is
117+
-- responsible for the 'HeaderHash' and 'HasHeader' constraints; the former is
118+
-- a type family, which is justifies the need for @UndecidableInstances@ unless
119+
-- a concrete block type is picked.
120+
deriving instance Generic (BlockTreeBranch blk)
121+
122+
instance (ToJSON (HeaderHash blk), HasHeader blk, ToJSON blk) => ToJSON (BlockTreeBranch blk)
123+
instance (FromJSON (HeaderHash blk), HasHeader blk, FromJSON blk) => FromJSON (BlockTreeBranch blk)
124+
125+
instance (ToJSON a, ToJSON b) => ToJSON (AnchoredSeq v a b) where
126+
toJSON anchoredSeq =
127+
object
128+
[ "anchor" .= anchor anchoredSeq
129+
, "sequence" .= toOldestFirst anchoredSeq
130+
]
131+
132+
instance (Anchorable v a b, FromJSON a, FromJSON b) => FromJSON (AnchoredSeq v a b) where
133+
parseJSON = withObject "AnchoredSeq" $ \obj -> do
134+
a <- obj .: "anchor"
135+
s <- obj .: "sequence"
136+
pure $ fromOldestFirst a s
137+
138+
instance ToJSON (HeaderHash blk) => ToJSON (Anchor blk)
139+
instance FromJSON (HeaderHash blk) => FromJSON (Anchor blk)
140+
141+
-- *** 'PointSchedule' field related instances
142+
143+
instance ToJSON Time
26144
instance FromJSON Time
27145

28-
instance ToJSON PeerId where
29-
toEncoding = genericToEncoding defaultOptions
146+
instance ToJSON PeerId
30147
instance FromJSON PeerId
31148

32149
deriving instance Generic (Peers a)
33-
instance ToJSON a => ToJSON (Peers a) where
34-
toEncoding = genericToEncoding defaultOptions
150+
instance ToJSON a => ToJSON (Peers a)
35151
instance FromJSON a => FromJSON (Peers a)
36152

37153
deriving instance Generic (SchedulePoint blk)
38-
instance ToJSON blk => ToJSON (SchedulePoint blk) where
39-
toEncoding = genericToEncoding defaultOptions
154+
instance ToJSON blk => ToJSON (SchedulePoint blk)
40155
instance FromJSON blk => FromJSON (SchedulePoint blk)

0 commit comments

Comments
 (0)