Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration/CLI preparation for HFC #188

Merged
merged 4 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions cardano-db-sync-extended/app/cardano-db-sync-extended.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import Cardano.Prelude

import Cardano.Db (MigrationDir (..))
import Cardano.DbSync (ConfigFile (..), DbSyncNodeParams (..), GenesisFile (..),
SocketPath (..), runDbSyncNode)
import Cardano.DbSync (ConfigFile (..), DbSyncNodeParams (..), SocketPath (..),
runDbSyncNode)
import Cardano.DbSync.Plugin.Extended (extendedDbSyncNodePlugin)

import Cardano.Slotting.Slot (SlotNo (..))
Expand All @@ -30,7 +30,6 @@ pCommandLine :: Parser DbSyncNodeParams
pCommandLine =
DbSyncNodeParams
<$> pConfigFile
<*> pGenesisFile
<*> pSocketPath
<*> pMigrationDir
<*> optional pSlotNo
Expand All @@ -44,15 +43,6 @@ pConfigFile =
<> Opt.metavar "FILEPATH"
)

pGenesisFile :: Parser GenesisFile
pGenesisFile =
GenesisFile <$> Opt.strOption
( Opt.long "genesis-file"
<> Opt.help "Path to the genesis JSON file"
<> Opt.completer (Opt.bashCompleter "file")
<> Opt.metavar "FILEPATH"
)

pMigrationDir :: Parser MigrationDir
pMigrationDir =
MigrationDir <$> Opt.strOption
Expand Down
14 changes: 2 additions & 12 deletions cardano-db-sync/app/cardano-db-sync.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import Cardano.Prelude

import Cardano.Db (MigrationDir (..))
import Cardano.DbSync (ConfigFile (..), DbSyncNodeParams (..), GenesisFile (..),
SocketPath (..), defDbSyncNodePlugin, runDbSyncNode)
import Cardano.DbSync (ConfigFile (..), DbSyncNodeParams (..), SocketPath (..),
defDbSyncNodePlugin, runDbSyncNode)

import Cardano.Slotting.Slot (SlotNo (..))

Expand All @@ -29,7 +29,6 @@ pCommandLine :: Parser DbSyncNodeParams
pCommandLine =
DbSyncNodeParams
<$> pConfigFile
<*> pGenesisFile
<*> pSocketPath
<*> pMigrationDir
<*> optional pSlotNo
Expand All @@ -43,15 +42,6 @@ pConfigFile =
<> Opt.metavar "FILEPATH"
)

pGenesisFile :: Parser GenesisFile
pGenesisFile =
GenesisFile <$> Opt.strOption
( Opt.long "genesis-file"
<> Opt.help "Path to the genesis JSON file"
<> Opt.completer (Opt.bashCompleter "file")
<> Opt.metavar "FILEPATH"
)

pMigrationDir :: Parser MigrationDir
pMigrationDir =
MigrationDir <$> Opt.strOption
Expand Down
4 changes: 3 additions & 1 deletion cardano-db-sync/src/Cardano/DbSync.hs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ runDbSyncNode plugin enp =
Nothing -> pure ()

orDie renderDbSyncNodeError $ do
genCfg <- readGenesisConfig enp enc
genCfg <- readGenesisConfig enc
logProtocolMagic trce $ genesisProtocolMagic genCfg

-- If the DB is empty it will be inserted, otherwise it will be validated (to make
Expand All @@ -150,6 +150,8 @@ runDbSyncNode plugin enp =
GenesisShelley sCfg ->
runDbSyncNodeNodeClient (ShelleyEnv $ Shelley.sgNetworkId sCfg)
iomgr trce plugin shelleyCodecConfig networkMagic (enpSocketPath enp)
GenesisCardano _bCfg _sCfg ->
panic "Cardano.DbSync.runDbSyncNode: GenesisCardano"


shelleyCodecConfig :: CodecConfig ShelleyBlock
Expand Down
35 changes: 31 additions & 4 deletions cardano-db-sync/src/Cardano/DbSync/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
{-# LANGUAGE ScopedTypeVariables #-}

module Cardano.DbSync.Config
( DbSyncNodeConfig
( DbSyncProtocol (..)
, DbSyncNodeConfig
, GenesisFile (..)
, GenesisHash (..)
, GenDbSyncNodeConfig (..)
, NetworkName (..)
Expand All @@ -20,7 +22,7 @@ import Cardano.Crypto (RequiresNetworkMagic (..))
import Cardano.Prelude

import Data.Aeson (FromJSON (..), Object, Value (..), (.:))
import Data.Aeson.Types (Parser)
import Data.Aeson.Types (Parser, typeMismatch)
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Char8 as BS
import Data.Text (Text)
Expand All @@ -29,17 +31,30 @@ import qualified Data.Yaml as Yaml

import Cardano.DbSync.Util

data DbSyncProtocol
= DbSyncProtocolByron
| DbSyncProtocolShelley
| DbSyncProtocolCardano

type DbSyncNodeConfig = GenDbSyncNodeConfig Logging.Configuration

data GenDbSyncNodeConfig a = GenDbSyncNodeConfig
{ encNetworkName :: !NetworkName
, encLoggingConfig :: !a
, encGenesisHash :: !GenesisHash
, encProtocol :: !DbSyncProtocol
, encByronGenesisFile :: !GenesisFile
, encByronGenesisHash :: !GenesisHash
, encShelleyGenesisFile :: !GenesisFile
, encShelleyGenesisHash :: !GenesisHash
, encEnableLogging :: !Bool
, encEnableMetrics :: !Bool
, encRequiresNetworkMagic :: !RequiresNetworkMagic
}

newtype GenesisFile = GenesisFile
{ unGenesisFile :: FilePath
}

newtype GenesisHash = GenesisHash
{ unGenesisHash :: Text
}
Expand Down Expand Up @@ -77,7 +92,19 @@ parseGenDbSyncNodeConfig o =
GenDbSyncNodeConfig
<$> fmap NetworkName (o .: "NetworkName")
<*> parseJSON (Object o)
<*> fmap GenesisHash (o .: "GenesisHash")
<*> o .: "Protocol"
<*> fmap GenesisFile (o .: "ByronGenesisFile")
<*> fmap GenesisHash (o .: "ByronGenesisHash")
<*> fmap GenesisFile (o .: "ShelleyGenesisFile")
<*> fmap GenesisHash (o .: "ShelleyGenesisHash")
<*> o .: "EnableLogging"
<*> o .: "EnableLogMetrics"
<*> o .: "RequiresNetworkMagic"

instance FromJSON DbSyncProtocol where
parseJSON o =
case o of
String "Byron" -> pure DbSyncProtocolByron
String "Shelley" -> pure DbSyncProtocolShelley
String "Cardano" -> pure DbSyncProtocolCardano
x -> typeMismatch "Protocol" x
52 changes: 26 additions & 26 deletions cardano-db-sync/src/Cardano/DbSync/Era.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

module Cardano.DbSync.Era
( GenesisEra (..)
, genesisEnv
, genesisNetworkMagic
, genesisProtocolMagic
, insertValidateGenesisDist
Expand All @@ -26,13 +25,11 @@ import Cardano.DbSync.Config
import qualified Cardano.DbSync.Era.Byron.Genesis as Byron
import qualified Cardano.DbSync.Era.Shelley.Genesis as Shelley
import Cardano.DbSync.Error
import Cardano.DbSync.Types

import Cardano.Config.Shelley.Orphans ()

import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.Except (ExceptT)
import Control.Monad.Trans.Except.Extra (firstExceptT, hoistEither, newExceptT, runExceptT)
import Control.Monad.Trans.Except.Extra (firstExceptT, hoistEither, newExceptT)

import qualified Data.Aeson as Aeson
import qualified Data.Text as Text
Expand All @@ -47,27 +44,25 @@ import qualified Shelley.Spec.Ledger.Genesis as Shelley
data GenesisEra
= GenesisByron !Byron.Config
| GenesisShelley !(ShelleyGenesis TPraosStandardCrypto)
-- | GenesisCardano !Byron.Config !(ShelleyGenesis TPraosStandardCrypto)
| GenesisCardano !Byron.Config !(ShelleyGenesis TPraosStandardCrypto)


genesisEnv :: GenesisEra -> DbSyncEnv
genesisEnv ge =
case ge of
GenesisByron _ -> ByronEnv
GenesisShelley sCfg -> ShelleyEnv $ Shelley.sgNetworkId sCfg

genesisNetworkMagic :: GenesisEra -> NetworkMagic
genesisNetworkMagic ge =
case ge of
GenesisByron bg ->
NetworkMagic $ unProtocolMagicId (Byron.configProtocolMagicId bg)
GenesisShelley sg -> NetworkMagic $ Shelley.sgNetworkMagic sg
GenesisShelley sg ->
NetworkMagic $ Shelley.sgNetworkMagic sg
GenesisCardano _bg sg ->
NetworkMagic $ Shelley.sgNetworkMagic sg

genesisProtocolMagic :: GenesisEra -> ProtocolMagic
genesisProtocolMagic ge =
case ge of
GenesisByron bg -> Byron.configProtocolMagic bg
GenesisShelley sg -> mkShelleyProtocolMagic sg
GenesisCardano _ sg -> mkShelleyProtocolMagic sg
where
mkShelleyProtocolMagic :: ShelleyGenesis TPraosStandardCrypto -> ProtocolMagic
mkShelleyProtocolMagic sg =
Expand All @@ -88,33 +83,38 @@ insertValidateGenesisDist trce nname genCfg =
Byron.insertValidateGenesisDist trce (unNetworkName nname) bCfg
GenesisShelley sCfg ->
Shelley.insertValidateGenesisDist trce (unNetworkName nname) sCfg
GenesisCardano bCfg sCfg -> do
Byron.insertValidateGenesisDist trce (unNetworkName nname) bCfg
Shelley.insertValidateGenesisDist trce (unNetworkName nname) sCfg

-- -----------------------------------------------------------------------------

readGenesisConfig
:: DbSyncNodeParams -> DbSyncNodeConfig
:: DbSyncNodeConfig
-> ExceptT DbSyncNodeError IO GenesisEra
readGenesisConfig enp enc = do
res <- liftIO $ runExceptT (readByronGenesisConfig enp enc)
case res of
Right gb -> pure $ GenesisByron gb
Left _ -> GenesisShelley <$> readShelleyGenesisConfig enp enc

readGenesisConfig enc =
case encProtocol enc of
DbSyncProtocolByron ->
GenesisByron <$> readByronGenesisConfig enc
DbSyncProtocolShelley ->
GenesisShelley <$> readShelleyGenesisConfig enc
DbSyncProtocolCardano ->
GenesisCardano <$> readByronGenesisConfig enc <*> readShelleyGenesisConfig enc

readByronGenesisConfig
:: DbSyncNodeParams -> DbSyncNodeConfig
:: DbSyncNodeConfig
-> ExceptT DbSyncNodeError IO Byron.Config
readByronGenesisConfig enp enc = do
let file = unGenesisFile $ enpGenesisFile enp
readByronGenesisConfig enc = do
let file = unGenesisFile $ encByronGenesisFile enc
genHash <- firstExceptT NEError
. hoistEither $ decodeAbstractHash (unGenesisHash $ encGenesisHash enc)
. hoistEither $ decodeAbstractHash (unGenesisHash $ encByronGenesisHash enc)
firstExceptT (NEByronConfig file)
$ Byron.mkConfigFromFile (encRequiresNetworkMagic enc) file genHash

readShelleyGenesisConfig
:: DbSyncNodeParams -> DbSyncNodeConfig
:: DbSyncNodeConfig
-> ExceptT DbSyncNodeError IO (ShelleyGenesis TPraosStandardCrypto)
readShelleyGenesisConfig enp _enc = do
let file = unGenesisFile $ enpGenesisFile enp
readShelleyGenesisConfig enc = do
let file = unGenesisFile $ encShelleyGenesisFile enc
firstExceptT (NEShelleyConfig file . Text.pack)
. newExceptT $ Aeson.eitherDecodeFileStrict' file
6 changes: 0 additions & 6 deletions cardano-db-sync/src/Cardano/DbSync/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module Cardano.DbSync.Types
, ConfigFile (..)
, DbSyncEnv (..)
, DbSyncNodeParams (..)
, GenesisFile (..)
, ShelleyAddress
, ShelleyBlock
, ShelleyDCert
Expand Down Expand Up @@ -60,7 +59,6 @@ newtype ConfigFile = ConfigFile
-- | The product type of all command line arguments
data DbSyncNodeParams = DbSyncNodeParams
{ enpConfigFile :: !ConfigFile
, enpGenesisFile :: !GenesisFile
, enpSocketPath :: !SocketPath
, enpMigrationDir :: !MigrationDir
, enpMaybeRollback :: !(Maybe SlotNo)
Expand All @@ -70,10 +68,6 @@ data DbSyncEnv
= ByronEnv
| ShelleyEnv !Shelley.Network

newtype GenesisFile = GenesisFile
{ unGenesisFile :: FilePath
}

type ShelleyAddress = Shelley.Addr Shelley.TPraosStandardCrypto
type ShelleyBlock = Shelley.ShelleyBlock Shelley.TPraosStandardCrypto
type ShelleyDCert = Shelley.DCert Shelley.TPraosStandardCrypto
Expand Down
12 changes: 10 additions & 2 deletions config/mainnet-config.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# Explorer DB Node configuration

NetworkName: mainnet
RequiresNetworkMagic: RequiresNoMagic
GenesisHash: 5f20df933584822601f9e3f8c024eb5eb252fe8cefb24d1317dc3d432e940ebb
EnableLogMetrics: False
EnableLogging: True

Protocol: Byron

RequiresNetworkMagic: RequiresNoMagic

ByronGenesisFile: ../cardano-node/mainnet-genesis.json
ByronGenesisHash: 5f20df933584822601f9e3f8c024eb5eb252fe8cefb24d1317dc3d432e940ebb

ShelleyGenesisFile: not/applicable
ShelleyGenesisHash: not/applicable

# ------------------------------------------------------------------------------
# Logging configuration follows.

Expand Down
11 changes: 10 additions & 1 deletion config/testnet-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

NetworkName: testnet
RequiresNetworkMagic: RequiresMagic
GenesisHash: 96fceff972c2c06bd3bb5243c39215333be6d56aaf4823073dca31afe5038471
EnableLogMetrics: False
EnableLogging: True

Protocol: Byron

RequiresNetworkMagic: RequiresMagic

ByronGenesisFile: ../cardano-node/configuration/defaults/byron-testnet/genesis.json
ByronGenesisHash: 96fceff972c2c06bd3bb5243c39215333be6d56aaf4823073dca31afe5038471

ShelleyGenesisFile: not/applicable
ShelleyGenesisHash: not/applicable

# ------------------------------------------------------------------------------
# Logging configuration follows.

Expand Down
Loading