Skip to content

Commit 2ade2a5

Browse files
Move instances from #3 into new orphans module
1 parent 627bdcb commit 2ade2a5

File tree

6 files changed

+44
-43
lines changed

6 files changed

+44
-43
lines changed

ouroboros-consensus-diffusion/app/conformance-test-runner/Main.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import Ouroboros.Network.PeerSelection.LedgerPeers (RelayAccessPoint (..), UseLe
2323
import Ouroboros.Network.PeerSelection.State.LocalRootPeers (HotValency (..), WarmValency (..))
2424
import Server (run)
2525
import System.Environment (getArgs)
26+
import Test.Consensus.OrphanInstances ()
2627
import Test.Consensus.PointSchedule (PointSchedule (..))
2728
import Test.Consensus.PointSchedule.Peers (PeerId (..), Peers (Peers), getPeerIds)
2829

ouroboros-consensus-diffusion/ouroboros-consensus-diffusion.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ library unstable-consensus-conformance-testlib
239239
Test.Consensus.HardFork.Combinator.B
240240
Test.Consensus.Network.AnchoredFragment.Extras
241241
Test.Consensus.Node
242+
Test.Consensus.OrphanInstances
242243
Test.Consensus.PeerSimulator.BlockFetch
243244
Test.Consensus.PeerSimulator.CSJInvariants
244245
Test.Consensus.PeerSimulator.ChainSync
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{-# LANGUAGE DeriveGeneric #-}
2+
{-# LANGUAGE StandaloneDeriving #-}
3+
{-# OPTIONS_GHC -Wno-orphans #-}
4+
5+
module Test.Consensus.OrphanInstances () where
6+
7+
import Control.Monad.Class.MonadTime.SI (Time)
8+
import Data.Aeson
9+
( FromJSON
10+
, ToJSON (toEncoding)
11+
, defaultOptions
12+
, genericToEncoding
13+
)
14+
import GHC.Generics
15+
import Test.Consensus.PointSchedule (PointSchedule (..))
16+
import Test.Consensus.PointSchedule.Peers (PeerId, Peers (..))
17+
import Test.Consensus.PointSchedule.SinglePeer (SchedulePoint (..))
18+
19+
deriving instance Generic (PointSchedule blk)
20+
instance ToJSON blk => ToJSON (PointSchedule blk) where
21+
toEncoding = genericToEncoding defaultOptions
22+
instance FromJSON blk => FromJSON (PointSchedule blk)
23+
24+
instance ToJSON Time where
25+
toEncoding = genericToEncoding defaultOptions
26+
instance FromJSON Time
27+
28+
instance ToJSON PeerId where
29+
toEncoding = genericToEncoding defaultOptions
30+
instance FromJSON PeerId
31+
32+
deriving instance Generic (Peers a)
33+
instance ToJSON a => ToJSON (Peers a) where
34+
toEncoding = genericToEncoding defaultOptions
35+
instance FromJSON a => FromJSON (Peers a)
36+
37+
deriving instance Generic (SchedulePoint blk)
38+
instance ToJSON blk => ToJSON (SchedulePoint blk) where
39+
toEncoding = genericToEncoding defaultOptions
40+
instance FromJSON blk => FromJSON (SchedulePoint blk)

ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/PointSchedule.hs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{-# LANGUAGE DataKinds #-}
2-
{-# LANGUAGE DeriveGeneric #-}
32
{-# LANGUAGE DuplicateRecordFields #-}
43
{-# LANGUAGE FlexibleContexts #-}
54
{-# LANGUAGE FlexibleInstances #-}
@@ -10,10 +9,6 @@
109
{-# LANGUAGE PolyKinds #-}
1110
{-# LANGUAGE RankNTypes #-}
1211
{-# LANGUAGE ScopedTypeVariables #-}
13-
-- Orphan instances for 'Time' were introduced temporarily (no pun intended)
14-
-- to implement generic 'ToJSON' and 'FromJSON' instances for 'PointSchedule'.
15-
-- Once implemented by hand, they should be removed.
16-
{-# OPTIONS_GHC -Worphans #-}
1712

1813
-- | Data types and generators for point schedules.
1914
--
@@ -66,15 +61,13 @@ import Control.Monad.Class.MonadTime.SI
6661
, diffTime
6762
)
6863
import Control.Monad.ST (ST)
69-
import Data.Aeson (ToJSON(toEncoding), FromJSON, genericToEncoding, defaultOptions)
7064
import Data.Bifunctor (first)
7165
import Data.Functor (($>))
7266
import Data.List (mapAccumL, partition, scanl')
7367
import qualified Data.Map.Strict as Map
7468
import Data.Maybe (catMaybes, fromMaybe, mapMaybe)
7569
import Data.Time (DiffTime)
7670
import Data.Word (Word64)
77-
import GHC.Generics
7871
import Network.TypedProtocol
7972
import Ouroboros.Consensus.Block.Abstract (withOriginToMaybe)
8073
import Ouroboros.Consensus.Ledger.SupportsProtocol
@@ -220,21 +213,6 @@ data PointSchedule blk = PointSchedule
220213
-- If no point in the schedule is larger than 'psMinEndTime',
221214
-- the simulation will still run until this time is reached.
222215
}
223-
deriving (Generic)
224-
225-
instance ToJSON blk => ToJSON (PointSchedule blk) where
226-
toEncoding = genericToEncoding defaultOptions
227-
228-
instance FromJSON blk => FromJSON (PointSchedule blk)
229-
230-
-- | TODO(xavier): Remove orphan instance after writing the 'PointSchedule'
231-
-- instance above by hand.
232-
instance ToJSON Time where
233-
toEncoding = genericToEncoding defaultOptions
234-
235-
-- | TODO(xavier): Remove orphan instance after writing the 'PointSchedule'
236-
-- instance above by hand.
237-
instance FromJSON Time
238216

239217
-- | List of all blocks appearing in the schedules.
240218
peerSchedulesBlocks :: Peers (PeerSchedule blk) -> [blk]

ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/PointSchedule/Peers.hs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ module Test.Consensus.PointSchedule.Peers
3939
, updatePeer
4040
) where
4141

42-
import Data.Aeson (ToJSON(toEncoding), FromJSON, genericToEncoding, defaultOptions)
4342
import Data.Hashable (Hashable)
4443
import Data.Map.Strict (Map)
4544
import qualified Data.Map.Strict as Map
@@ -79,11 +78,6 @@ instance CondenseList PeerId where
7978

8079
instance Hashable PeerId
8180

82-
instance ToJSON PeerId where
83-
toEncoding = genericToEncoding defaultOptions
84-
85-
instance FromJSON PeerId
86-
8781
-- | General-purpose functor associated with a peer.
8882
data Peer a
8983
= Peer
@@ -117,12 +111,7 @@ data Peers a = Peers
117111
{ honestPeers :: Map Int a
118112
, adversarialPeers :: Map Int a
119113
}
120-
deriving (Eq, Show, Generic)
121-
122-
instance ToJSON a => ToJSON (Peers a) where
123-
toEncoding = genericToEncoding defaultOptions
124-
125-
instance FromJSON a => FromJSON (Peers a)
114+
deriving (Eq, Show)
126115

127116
-- | Variant of 'honestPeers' that returns a map with 'PeerId's as keys.
128117
honestPeers' :: Peers a -> Map PeerId a

ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/PointSchedule/SinglePeer.hs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{-# LANGUAGE DeriveGeneric #-}
21
-- | This module contains functions for generating random point schedules.
32
--
43
-- A point schedule is a set of tables, having one table per simulated peer.
@@ -100,12 +99,10 @@ module Test.Consensus.PointSchedule.SinglePeer
10099
import Cardano.Slotting.Slot (WithOrigin (At, Origin), withOrigin)
101100
import Control.Arrow (second)
102101
import Control.Monad.Class.MonadTime.SI (Time)
103-
import Data.Aeson (ToJSON(toEncoding), FromJSON, genericToEncoding, defaultOptions)
104102
import Data.List (mapAccumL)
105103
import Data.Time.Clock (DiffTime)
106104
import Data.Vector (Vector)
107105
import qualified Data.Vector as Vector
108-
import GHC.Generics
109106
import qualified Ouroboros.Network.AnchoredFragment as AF
110107
import Ouroboros.Network.Block (BlockNo (unBlockNo), blockSlot)
111108
import qualified System.Random.Stateful as R (StatefulGen)
@@ -121,12 +118,7 @@ data SchedulePoint blk
121118
= ScheduleTipPoint (WithOrigin blk)
122119
| ScheduleHeaderPoint (WithOrigin blk)
123120
| ScheduleBlockPoint (WithOrigin blk)
124-
deriving (Eq, Show, Generic)
125-
126-
instance ToJSON blk => ToJSON (SchedulePoint blk) where
127-
toEncoding = genericToEncoding defaultOptions
128-
129-
instance FromJSON blk => FromJSON (SchedulePoint blk)
121+
deriving (Eq, Show)
130122

131123
scheduleTipPoint :: blk -> SchedulePoint blk
132124
scheduleTipPoint = ScheduleTipPoint . At

0 commit comments

Comments
 (0)