/
Protocol.hs
119 lines (98 loc) · 3.74 KB
/
Protocol.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
module Cardano.Api.Protocol
(
-- * The enumeration of supported protocols
Protocol(..)
, MockProtocol(..)
-- * Node client support
-- | Support for the context needed to run a client of a node that is using
-- a protocol.
, mkNodeClientProtocol
, SomeNodeClientProtocol(..)
) where
import Cardano.Prelude
import Control.Monad.Fail (fail)
import Data.Aeson
import Cardano.Chain.Slotting (EpochSlots(..))
import Cardano.Api.Protocol.Types
import Cardano.Api.Protocol.Byron
import Cardano.Api.Protocol.Cardano
import Cardano.Api.Protocol.Shelley
import qualified Ouroboros.Consensus.Cardano as Consensus
data Protocol = MockProtocol !MockProtocol
| ByronProtocol
| ShelleyProtocol
| CardanoProtocol
deriving (Eq, Show, Generic)
instance FromJSON Protocol where
parseJSON =
withText "Protocol" $ \str -> case str of
-- The new names
"MockBFT" -> pure (MockProtocol MockBFT)
"MockPBFT" -> pure (MockProtocol MockPBFT)
"MockPraos" -> pure (MockProtocol MockPraos)
"Byron" -> pure ByronProtocol
"Shelley" -> pure ShelleyProtocol
"Cardano" -> pure CardanoProtocol
-- The old names
"BFT" -> pure (MockProtocol MockBFT)
--"MockPBFT" -- same as new name
"Praos" -> pure (MockProtocol MockPraos)
"RealPBFT" -> pure ByronProtocol
"TPraos" -> pure ShelleyProtocol
_ -> fail $ "Parsing of Protocol failed. "
<> show str <> " is not a valid protocol"
deriving instance NFData Protocol
deriving instance NoUnexpectedThunks Protocol
data MockProtocol = MockBFT
| MockPBFT
| MockPraos
deriving (Eq, Show, Generic)
deriving instance NFData MockProtocol
deriving instance NoUnexpectedThunks MockProtocol
mkNodeClientProtocol :: Protocol -> SomeNodeClientProtocol
mkNodeClientProtocol protocol =
case protocol of
{-
--TODO
-- Mock protocols
NodeProtocolConfigurationMock config ->
case npcMockProtocol config of
BFT -> mkNodeClientProtocolMockBFT
MockPBFT -> mkNodeClientProtocolMockPBFT
Praos -> mkNodeClientProtocolMockPraos
-}
MockProtocol _ ->
panic "TODO: mkNodeClientProtocol NodeProtocolConfigurationMock"
-- Real protocols
ByronProtocol ->
mkSomeNodeClientProtocolByron
--TODO: this is only the correct value for mainnet
-- not for Byron testnets. This value is needed because
-- to decode legacy EBBs one needs to know how many
-- slots there are per-epoch. This info comes from
-- the genesis file, but we don't have that in the
-- client case.
(EpochSlots 21600)
(Consensus.SecurityParam 2160)
ShelleyProtocol ->
mkSomeNodeClientProtocolShelley
CardanoProtocol ->
mkSomeNodeClientProtocolCardano
--TODO: this is only the correct value for mainnet
-- not for Byron testnets. This value is needed because
-- to decode legacy EBBs one needs to know how many
-- slots there are per-epoch. This info comes from
-- the genesis file, but we don't have that in the
-- client case.
(EpochSlots 21600)
(Consensus.SecurityParam 2160)