-
Notifications
You must be signed in to change notification settings - Fork 156
/
Gen.hs
228 lines (205 loc) · 6.75 KB
/
Gen.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Cardano.DbSync.Gen (
-- * Config/Api Type generators
syncNodeParams,
syncPreConfig,
syncNodeConfig,
syncInsertConfig,
syncInsertOptions,
shelleyConfig,
multiAssetConfig,
metadataConfig,
plutusConfig,
multiAssetPolicy,
metadataKey,
scriptHash,
addr,
networkName,
-- * Utility generators
hashBlake2b_256,
filePath,
protocolVersion,
triggerHardFork,
) where
import qualified Cardano.BM.Configuration.Model as Logging (Configuration ())
import qualified Cardano.BM.Data.Configuration as Logging
import qualified Cardano.BM.Data.Severity as Logging
import Cardano.Chain.Update (ProtocolVersion (..))
import Cardano.Crypto (RequiresNetworkMagic (..))
import Cardano.Crypto.Hash (Blake2b_256 (), Hash ())
import Cardano.Crypto.Hash.Class (HashAlgorithm (..), hashFromBytes)
import Cardano.Db (PGPassSource (..))
import Cardano.DbSync
import Cardano.DbSync.Config.Types
import Cardano.Ledger.Slot (EpochNo (..))
import Cardano.Prelude
import Data.ByteString.Short (ShortByteString (), toShort)
import Data.Maybe (fromJust)
import Hedgehog
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
import Ouroboros.Consensus.Cardano.CanHardFork (TriggerHardFork (..))
syncPreConfig :: Gen SyncPreConfig
syncPreConfig =
SyncPreConfig
<$> networkName
<*> pure loggingRepresentation
<*> (NodeConfigFile <$> filePath)
<*> Gen.bool
<*> Gen.bool
<*> Gen.int (Range.linear 0 10000)
<*> syncInsertConfig
<*> Gen.bool
syncNodeParams :: MonadGen m => m SyncNodeParams
syncNodeParams =
SyncNodeParams
<$> (ConfigFile <$> filePath)
<*> (SocketPath <$> filePath)
<*> Gen.maybe (LedgerStateDir <$> filePath)
<*> (MigrationDir <$> filePath)
<*> Gen.constant PGPassDefaultEnv
<*> Gen.bool
<*> Gen.bool
<*> Gen.bool
<*> Gen.bool
<*> Gen.bool
<*> Gen.bool
<*> Gen.word64 (Range.linear 0 1000)
<*> Gen.word64 (Range.linear 0 1000)
<*> pure Nothing
syncNodeConfig :: Logging.Configuration -> Gen SyncNodeConfig
syncNodeConfig loggingCfg =
SyncNodeConfig
<$> networkName
<*> pure loggingCfg
<*> (NodeConfigFile <$> filePath)
<*> Gen.constant SyncProtocolCardano
<*> Gen.element [RequiresNoMagic, RequiresMagic]
<*> Gen.bool
<*> Gen.bool
<*> Gen.int (Range.linear 0 10000)
<*> Gen.maybe (Gen.double (Range.linearFrac 0 1))
<*> (GenesisFile <$> filePath)
<*> (GenesisHashByron <$> Gen.text (Range.linear 0 100) Gen.alphaNum)
<*> (GenesisFile <$> filePath)
<*> (GenesisHashShelley . fromJust <$> hashBlake2b_256)
<*> (GenesisFile <$> filePath)
<*> (GenesisHashAlonzo . fromJust <$> hashBlake2b_256)
<*> Gen.maybe (GenesisFile <$> filePath)
<*> Gen.maybe (GenesisHashConway . fromJust <$> hashBlake2b_256)
<*> protocolVersion
<*> triggerHardFork
<*> triggerHardFork
<*> triggerHardFork
<*> triggerHardFork
<*> triggerHardFork
<*> triggerHardFork
<*> syncInsertOptions
syncInsertConfig :: Gen SyncInsertConfig
syncInsertConfig =
Gen.choice
[ pure FullInsertOptions
, pure OnlyUTxOInsertOptions
, pure OnlyGovInsertOptions
, pure DisableAllInsertOptions
, SyncInsertConfig <$> syncInsertOptions
]
syncInsertOptions :: Gen SyncInsertOptions
syncInsertOptions =
SyncInsertOptions
<$> txOutConfig
<*> Gen.element [LedgerEnable, LedgerDisable, LedgerIgnore]
<*> shelleyConfig
<*> pure (RewardsConfig True)
<*> multiAssetConfig
<*> metadataConfig
<*> plutusConfig
<*> (GovernanceConfig <$> Gen.bool)
<*> (OffchainPoolDataConfig <$> Gen.bool)
<*> Gen.element [JsonTypeText, JsonTypeJsonb, JsonTypeDisable]
txOutConfig :: Gen TxOutConfig
txOutConfig =
Gen.choice
[ pure TxOutEnable
, pure TxOutDisable
, TxOutConsumed <$> (ForceTxIn <$> Gen.bool)
, TxOutPrune <$> (ForceTxIn <$> Gen.bool)
, TxOutBootstrap <$> (ForceTxIn <$> Gen.bool)
]
shelleyConfig :: Gen ShelleyInsertConfig
shelleyConfig = do
Gen.choice
[ pure ShelleyEnable
, pure ShelleyDisable
, ShelleyStakeAddrs <$> Gen.nonEmpty (Range.linear 1 5) addr
]
multiAssetConfig :: Gen MultiAssetConfig
multiAssetConfig =
Gen.choice
[ pure MultiAssetEnable
, pure MultiAssetDisable
, MultiAssetPolicies <$> Gen.nonEmpty (Range.linear 1 5) multiAssetPolicy
]
metadataConfig :: Gen MetadataConfig
metadataConfig =
Gen.choice
[ pure MetadataEnable
, pure MetadataDisable
, MetadataKeys <$> Gen.nonEmpty (Range.linear 1 5) metadataKey
]
plutusConfig :: Gen PlutusConfig
plutusConfig =
Gen.choice
[ pure PlutusEnable
, pure PlutusDisable
, PlutusScripts <$> Gen.nonEmpty (Range.linear 1 5) scriptHash
]
addr :: Gen ShortByteString
addr = toShort <$> Gen.utf8 (Range.linear 1 5) Gen.unicode
multiAssetPolicy :: Gen ShortByteString
multiAssetPolicy = toShort <$> Gen.utf8 (Range.linear 1 5) Gen.unicode
metadataKey :: Gen Word
metadataKey = Gen.word (Range.linear minBound maxBound)
scriptHash :: Gen ShortByteString
scriptHash = toShort <$> Gen.utf8 (Range.linear 1 5) Gen.unicode
networkName :: Gen NetworkName
networkName = NetworkName <$> Gen.text (Range.linear 0 100) Gen.alpha
hashBlake2b_256 :: MonadGen m => m (Maybe (Hash Blake2b_256 ByteString))
hashBlake2b_256 = serialiseHash <$> Gen.bytes (Range.linear 0 100)
where
serialiseHash :: ByteString -> Maybe (Hash Blake2b_256 ByteString)
serialiseHash = hashFromBytes . digest (Proxy @Blake2b_256)
filePath :: MonadGen m => m FilePath
filePath = Gen.string (Range.linear 0 100) Gen.unicode
protocolVersion :: MonadGen m => m ProtocolVersion
protocolVersion = ProtocolVersion <$> word16 <*> word16 <*> word8
where
word16 = Gen.word16 (Range.linear minBound maxBound)
word8 = Gen.word8 (Range.linear minBound maxBound)
triggerHardFork :: MonadGen m => m TriggerHardFork
triggerHardFork =
Gen.choice
[ Gen.constant TriggerHardForkNotDuringThisExecution
, TriggerHardForkAtEpoch . EpochNo <$> Gen.word64 (Range.linear minBound maxBound)
, TriggerHardForkAtVersion <$> Gen.word16 (Range.linear minBound maxBound)
]
-- | @Logging.Representation@ is not useful for our testing, so we just generate a minimal example
loggingRepresentation :: Logging.Representation
loggingRepresentation =
Logging.Representation
{ Logging.minSeverity = Logging.Info
, Logging.rotation = Nothing
, Logging.setupScribes = []
, Logging.defaultScribes = []
, Logging.setupBackends = []
, Logging.defaultBackends = []
, Logging.hasEKG = Nothing
, Logging.hasGraylog = Nothing
, Logging.hasPrometheus = Nothing
, Logging.hasGUI = Nothing
, Logging.traceForwardTo = Nothing
, Logging.forwardDelay = Nothing
, Logging.traceAcceptAt = Nothing
, Logging.options = mempty
}