|
| 1 | +{-# LANGUAGE TypeApplications #-} |
| 2 | +{-# LANGUAGE NoImplicitPrelude #-} |
| 3 | + |
| 4 | +module Cardano.DbSync.Gen ( |
| 5 | + -- * Config/Api Type generators |
| 6 | + syncNodeParams, |
| 7 | + syncNodeConfig, |
| 8 | + syncInsertConfig, |
| 9 | + shelleyConfig, |
| 10 | + multiAssetConfig, |
| 11 | + metadataConfig, |
| 12 | + plutusConfig, |
| 13 | + multiAssetPolicy, |
| 14 | + metadataKey, |
| 15 | + scriptHash, |
| 16 | + addr, |
| 17 | + |
| 18 | + -- * Utility generators |
| 19 | + hashBlake2b_256, |
| 20 | + filePath, |
| 21 | + protocolVersion, |
| 22 | + triggerHardFork, |
| 23 | +) where |
| 24 | + |
| 25 | +import qualified Cardano.BM.Configuration.Model as Logging |
| 26 | +import Cardano.Chain.Update (ProtocolVersion (..)) |
| 27 | +import Cardano.Crypto (RequiresNetworkMagic (..)) |
| 28 | +import Cardano.Crypto.Hash (Blake2b_256 (), Hash ()) |
| 29 | +import Cardano.Crypto.Hash.Class (HashAlgorithm (..), hashFromBytes) |
| 30 | +import Cardano.Db (PGPassSource (..)) |
| 31 | +import Cardano.DbSync |
| 32 | +import Cardano.DbSync.Config.Types |
| 33 | +import Cardano.Ledger.Slot (EpochNo (..)) |
| 34 | +import Cardano.Prelude |
| 35 | +import Data.ByteString.Short (ShortByteString (), toShort) |
| 36 | +import Data.Maybe (fromJust) |
| 37 | +import Hedgehog |
| 38 | +import qualified Hedgehog.Gen as Gen |
| 39 | +import qualified Hedgehog.Range as Range |
| 40 | +import Ouroboros.Consensus.Cardano.CanHardFork (TriggerHardFork (..)) |
| 41 | + |
| 42 | +syncNodeParams :: MonadGen m => m SyncNodeParams |
| 43 | +syncNodeParams = |
| 44 | + SyncNodeParams |
| 45 | + <$> (ConfigFile <$> filePath) |
| 46 | + <*> (SocketPath <$> filePath) |
| 47 | + <*> Gen.maybe (LedgerStateDir <$> filePath) |
| 48 | + <*> (MigrationDir <$> filePath) |
| 49 | + <*> Gen.constant PGPassDefaultEnv |
| 50 | + <*> Gen.bool |
| 51 | + <*> Gen.bool |
| 52 | + <*> Gen.bool |
| 53 | + <*> Gen.bool |
| 54 | + <*> Gen.bool |
| 55 | + <*> Gen.bool |
| 56 | + <*> Gen.bool |
| 57 | + <*> Gen.bool |
| 58 | + <*> Gen.bool |
| 59 | + <*> Gen.bool |
| 60 | + <*> Gen.bool |
| 61 | + <*> Gen.list (Range.linear 0 10) (Gen.word64 (Range.linear minBound maxBound)) |
| 62 | + <*> Gen.bool |
| 63 | + <*> Gen.bool |
| 64 | + <*> Gen.bool |
| 65 | + <*> Gen.bool |
| 66 | + <*> Gen.bool |
| 67 | + <*> Gen.bool |
| 68 | + <*> Gen.bool |
| 69 | + <*> Gen.bool |
| 70 | + <*> Gen.bool |
| 71 | + <*> Gen.bool |
| 72 | + <*> Gen.bool |
| 73 | + <*> Gen.word64 (Range.linear 0 1000) |
| 74 | + <*> Gen.word64 (Range.linear 0 1000) |
| 75 | + <*> pure Nothing |
| 76 | + |
| 77 | +syncNodeConfig :: Logging.Configuration -> Gen SyncNodeConfig |
| 78 | +syncNodeConfig loggingCfg = |
| 79 | + SyncNodeConfig |
| 80 | + <$> (NetworkName <$> Gen.text (Range.linear 0 100) Gen.alpha) |
| 81 | + <*> pure loggingCfg |
| 82 | + <*> (NodeConfigFile <$> filePath) |
| 83 | + <*> Gen.constant SyncProtocolCardano |
| 84 | + <*> Gen.element [RequiresNoMagic, RequiresMagic] |
| 85 | + <*> Gen.bool |
| 86 | + <*> Gen.bool |
| 87 | + <*> Gen.int (Range.linear 0 10000) |
| 88 | + <*> Gen.maybe (Gen.double (Range.linearFrac 0 1)) |
| 89 | + <*> (GenesisFile <$> filePath) |
| 90 | + <*> (GenesisHashByron <$> Gen.text (Range.linear 0 100) Gen.alphaNum) |
| 91 | + <*> (GenesisFile <$> filePath) |
| 92 | + <*> (GenesisHashShelley . fromJust <$> hashBlake2b_256) |
| 93 | + <*> (GenesisFile <$> filePath) |
| 94 | + <*> (GenesisHashAlonzo . fromJust <$> hashBlake2b_256) |
| 95 | + <*> Gen.maybe (GenesisFile <$> filePath) |
| 96 | + <*> Gen.maybe (GenesisHashConway . fromJust <$> hashBlake2b_256) |
| 97 | + <*> protocolVersion |
| 98 | + <*> triggerHardFork |
| 99 | + <*> triggerHardFork |
| 100 | + <*> triggerHardFork |
| 101 | + <*> triggerHardFork |
| 102 | + <*> triggerHardFork |
| 103 | + <*> triggerHardFork |
| 104 | + <*> syncInsertConfig |
| 105 | + |
| 106 | +syncInsertConfig :: Gen SyncInsertConfig |
| 107 | +syncInsertConfig = |
| 108 | + SyncInsertConfig |
| 109 | + <$> Gen.element [TxOutEnable, TxOutDisable, TxOutConsumed, TxOutPrune, TxOutBootstrap] |
| 110 | + <*> Gen.element [LedgerEnable, LedgerDisable, LedgerIgnore] |
| 111 | + <*> shelleyConfig |
| 112 | + <*> multiAssetConfig |
| 113 | + <*> metadataConfig |
| 114 | + <*> plutusConfig |
| 115 | + <*> (GovernanceConfig <$> Gen.bool) |
| 116 | + <*> (OffchainPoolDataConfig <$> Gen.bool) |
| 117 | + <*> Gen.element [JsonTypeText, JsonTypeJsonb, JsonTypeDisable] |
| 118 | + |
| 119 | +shelleyConfig :: Gen ShelleyInsertConfig |
| 120 | +shelleyConfig = do |
| 121 | + Gen.choice |
| 122 | + [ pure ShelleyEnable |
| 123 | + , pure ShelleyDisable |
| 124 | + , ShelleyStakeAddrs <$> Gen.nonEmpty (Range.linear 1 5) addr |
| 125 | + ] |
| 126 | + |
| 127 | +multiAssetConfig :: Gen MultiAssetConfig |
| 128 | +multiAssetConfig = |
| 129 | + Gen.choice |
| 130 | + [ pure MultiAssetEnable |
| 131 | + , pure MultiAssetDisable |
| 132 | + , MultiAssetPolicies <$> Gen.nonEmpty (Range.linear 1 5) multiAssetPolicy |
| 133 | + ] |
| 134 | + |
| 135 | +metadataConfig :: Gen MetadataConfig |
| 136 | +metadataConfig = |
| 137 | + Gen.choice |
| 138 | + [ pure MetadataEnable |
| 139 | + , pure MetadataDisable |
| 140 | + , MetadataKeys <$> Gen.nonEmpty (Range.linear 1 5) metadataKey |
| 141 | + ] |
| 142 | + |
| 143 | +plutusConfig :: Gen PlutusConfig |
| 144 | +plutusConfig = |
| 145 | + Gen.choice |
| 146 | + [ pure PlutusEnable |
| 147 | + , pure PlutusDisable |
| 148 | + , PlutusScripts <$> Gen.nonEmpty (Range.linear 1 5) scriptHash |
| 149 | + ] |
| 150 | + |
| 151 | +addr :: Gen ShortByteString |
| 152 | +addr = toShort <$> Gen.utf8 (Range.linear 1 5) Gen.unicode |
| 153 | + |
| 154 | +multiAssetPolicy :: Gen ShortByteString |
| 155 | +multiAssetPolicy = toShort <$> Gen.utf8 (Range.linear 1 5) Gen.unicode |
| 156 | + |
| 157 | +metadataKey :: Gen Word |
| 158 | +metadataKey = Gen.word (Range.linear minBound maxBound) |
| 159 | + |
| 160 | +scriptHash :: Gen ShortByteString |
| 161 | +scriptHash = toShort <$> Gen.utf8 (Range.linear 1 5) Gen.unicode |
| 162 | + |
| 163 | +hashBlake2b_256 :: MonadGen m => m (Maybe (Hash Blake2b_256 ByteString)) |
| 164 | +hashBlake2b_256 = serialiseHash <$> Gen.bytes (Range.linear 0 100) |
| 165 | + where |
| 166 | + serialiseHash :: ByteString -> Maybe (Hash Blake2b_256 ByteString) |
| 167 | + serialiseHash = hashFromBytes . digest (Proxy @Blake2b_256) |
| 168 | + |
| 169 | +filePath :: MonadGen m => m FilePath |
| 170 | +filePath = Gen.string (Range.linear 0 100) Gen.unicode |
| 171 | + |
| 172 | +protocolVersion :: MonadGen m => m ProtocolVersion |
| 173 | +protocolVersion = ProtocolVersion <$> word16 <*> word16 <*> word8 |
| 174 | + where |
| 175 | + word16 = Gen.word16 (Range.linear minBound maxBound) |
| 176 | + word8 = Gen.word8 (Range.linear minBound maxBound) |
| 177 | + |
| 178 | +triggerHardFork :: MonadGen m => m TriggerHardFork |
| 179 | +triggerHardFork = |
| 180 | + Gen.choice |
| 181 | + [ Gen.constant TriggerHardForkNotDuringThisExecution |
| 182 | + , TriggerHardForkAtEpoch . EpochNo <$> Gen.word64 (Range.linear minBound maxBound) |
| 183 | + , TriggerHardForkAtVersion <$> Gen.word16 (Range.linear minBound maxBound) |
| 184 | + ] |
0 commit comments