-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathTypes.hs
135 lines (118 loc) · 3.75 KB
/
Types.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
module Types (
AppM (AppM),
ClusterStartupFailureReason (
ClusterIsRunningAlready,
NegativeLovelaces,
NodeConfigNotFound
),
Env (Env, status, options),
ErrorMessage,
Lovelace (unLovelace),
PlutipServerError (PlutipServerError),
PrivateKey,
ServerOptions (ServerOptions, nodeLogs, port),
StartClusterRequest (StartClusterRequest, keysToGenerate, slotLength, epochSize),
StartClusterResponse (
ClusterStartupSuccess,
ClusterStartupFailure
),
ClusterStartupParameters (
ClusterStartupParameters,
keysDirectory,
nodeSocketPath,
privateKeys,
nodeConfigPath
),
StopClusterRequest (StopClusterRequest),
StopClusterResponse (StopClusterSuccess, StopClusterFailure),
) where
import Cardano.Ledger.Slot (EpochSize)
import Control.Concurrent.MVar (MVar)
import Control.Monad.Catch (Exception, MonadThrow)
import Control.Monad.IO.Class (MonadIO)
import Control.Monad.Reader (MonadReader, ReaderT)
import Data.Aeson (FromJSON, ToJSON, parseJSON)
import Data.Kind (Type)
import Data.Text (Text)
import Data.Time (NominalDiffTime)
import GHC.Generics (Generic)
import Network.Wai.Handler.Warp (Port)
import Test.Plutip.Internal.BotPlutusInterface.Wallet (BpiWallet)
import Test.Plutip.Internal.LocalCluster (ClusterStatus)
import Test.Plutip.Internal.Types (ClusterEnv)
import UnliftIO.STM (TVar)
-- TVar is used for signaling by 'startCluster'/'stopCluster' (STM is used
-- for blocking).
-- MVar is used by plutip-server to store current TVar (we allow maximum of one
-- cluster at any given moment).
-- This MVar is used by start/stop handlers.
-- The payload of ClusterStatus is irrelevant.
type ClusterStatusRef = MVar (TVar (ClusterStatus (ClusterEnv, [BpiWallet])))
data Env = Env
{ status :: ClusterStatusRef
, options :: ServerOptions
}
data ServerOptions = ServerOptions
{ port :: Port
, nodeLogs :: Maybe FilePath
}
deriving stock (Generic)
newtype AppM (a :: Type) = AppM (ReaderT Env IO a)
deriving newtype
( Functor
, Applicative
, Monad
, MonadIO
, MonadReader Env
, MonadThrow
)
data PlutipServerError
= PlutipServerError
deriving stock (Show, Eq, Generic)
deriving anyclass (FromJSON, ToJSON)
instance Exception PlutipServerError
type ErrorMessage = Text
newtype Lovelace = Lovelace {unLovelace :: Integer}
deriving stock (Show, Eq, Generic)
deriving newtype (ToJSON, Num)
instance FromJSON Lovelace where
parseJSON json = do
value <- parseJSON json
if value < 0
then fail "Lovelace value must not be negative"
else pure $ Lovelace value
data StartClusterRequest = StartClusterRequest
{ slotLength :: NominalDiffTime
, epochSize :: EpochSize
, -- | Lovelace amounts for each UTXO of each wallet
keysToGenerate :: [[Lovelace]]
}
deriving stock (Show, Eq, Generic)
deriving anyclass (FromJSON, ToJSON)
-- CborHex
type PrivateKey = Text
data ClusterStartupFailureReason
= ClusterIsRunningAlready
| NegativeLovelaces
| NodeConfigNotFound
deriving stock (Show, Eq, Generic)
deriving anyclass (FromJSON, ToJSON)
data ClusterStartupParameters = ClusterStartupParameters
{ privateKeys :: [PrivateKey]
, nodeSocketPath :: FilePath
, nodeConfigPath :: FilePath
, keysDirectory :: FilePath
}
deriving stock (Show, Eq, Generic)
deriving anyclass (FromJSON, ToJSON)
data StartClusterResponse
= ClusterStartupFailure ClusterStartupFailureReason
| ClusterStartupSuccess ClusterStartupParameters
deriving stock (Show, Eq, Generic)
deriving anyclass (FromJSON, ToJSON)
data StopClusterRequest = StopClusterRequest
deriving stock (Show, Eq, Generic)
deriving anyclass (FromJSON, ToJSON)
data StopClusterResponse = StopClusterSuccess | StopClusterFailure ErrorMessage
deriving stock (Show, Eq, Generic)
deriving anyclass (FromJSON, ToJSON)