Skip to content

Commit

Permalink
Refactor WavesSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirlogachev committed Nov 18, 2024
1 parent c547ac9 commit f3f7eec
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,33 +244,31 @@ private[settings] object BlockchainType {
}

object BlockchainSettings {
def fromRootConfig(config: Config): BlockchainSettings = fromConfig(config.getConfig("waves.blockchain"))
def fromRootConfig(config: Config): BlockchainSettings =
ConfigSource.fromConfig(config).at("waves.blockchain").loadOrThrow[BlockchainSettings]

def fromConfig(config: Config): BlockchainSettings = {
val blockchainType = config.getString("type").toUpperCase
val (addressSchemeCharacter, functionalitySettings, genesisSettings, rewardsSettings) = blockchainType match {
case BlockchainType.STAGENET =>
('S', FunctionalitySettings.STAGENET, GenesisSettings.STAGENET, RewardsSettings.STAGENET)
case BlockchainType.TESTNET =>
('T', FunctionalitySettings.TESTNET, GenesisSettings.TESTNET, RewardsSettings.TESTNET)
case BlockchainType.MAINNET =>
('W', FunctionalitySettings.MAINNET, GenesisSettings.MAINNET, RewardsSettings.MAINNET)
case _ => // Custom
// Note: Mind the imperative approach to reading the config here. Be careful when refactoring.
val networkId = config.getString(s"custom.address-scheme-character").charAt(0)
val configSource = ConfigSource.fromConfig(config)
val functionality: FunctionalitySettings = configSource.at("custom.functionality").loadOrThrow[FunctionalitySettings]
val genesis = configSource.at("custom.genesis").loadOrThrow[GenesisSettings]
val rewards = configSource.at("custom.rewards").loadOrThrow[RewardsSettings]
require(functionality.minBlockTime <= genesis.averageBlockDelay, "minBlockTime should be <= averageBlockDelay")
(networkId, functionality, genesis, rewards)
}
implicit val configReader: ConfigReader[BlockchainSettings] = ConfigReader.fromCursor(cur =>
for {
objCur <- cur.asObjectCursor
blockchainTypeString <- objCur.atKey("type").flatMap(_.asString).map(_.toUpperCase)
(addressSchemeCharacter, functionalitySettings, genesisSettings, rewardsSettings) <- blockchainTypeString match {
case BlockchainType.STAGENET => Right(('S', FunctionalitySettings.STAGENET, GenesisSettings.STAGENET, RewardsSettings.STAGENET))
case BlockchainType.TESTNET => Right(('T', FunctionalitySettings.TESTNET, GenesisSettings.TESTNET, RewardsSettings.TESTNET))
case BlockchainType.MAINNET => Right(('W', FunctionalitySettings.MAINNET, GenesisSettings.MAINNET, RewardsSettings.MAINNET))
case _ =>
// Custom
for {
customObjCur <- objCur.atKey("custom").flatMap(_.asObjectCursor)
networkId <- customObjCur.atKey("address-scheme-character").flatMap(_.asString).map(_.charAt(0))
functionality <- customObjCur.atKey("functionality").flatMap(ConfigReader[FunctionalitySettings].from)
genesis <- customObjCur.atKey("genesis").flatMap(ConfigReader[GenesisSettings].from)
rewards <- customObjCur.atKey("rewards").flatMap(ConfigReader[RewardsSettings].from)
} yield {
require(functionality.minBlockTime <= genesis.averageBlockDelay, "minBlockTime should be <= averageBlockDelay")
(networkId, functionality, genesis, rewards)
}
}

BlockchainSettings(
addressSchemeCharacter = addressSchemeCharacter,
functionalitySettings = functionalitySettings,
genesisSettings = genesisSettings,
rewardsSettings = rewardsSettings
)
}
} yield BlockchainSettings(addressSchemeCharacter, functionalitySettings, genesisSettings, rewardsSettings)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ object WavesSettings {
val wavesConfigSource = ConfigSource.fromConfig(waves)

val directory = wavesConfigSource.at("directory").loadOrThrow[String]
val enableLightMode = wavesConfigSource.at("enable-light-mode").loadOrThrow[Boolean]
val ntpServer = wavesConfigSource.at("ntp-server").loadOrThrow[String]
val maxTxErrorLogSize = wavesConfigSource.at("max-tx-error-log-size").loadOrThrow[Int]
val dbSettings = wavesConfigSource.at("db").loadOrThrow[DBSettings]
val extensions = wavesConfigSource.at("extensions").loadOrThrow[Seq[String]]
val extensionsShutdownTimeout = wavesConfigSource.at("extensions-shutdown-timeout").loadOrThrow[FiniteDuration]
val networkSettings = wavesConfigSource.at("network").loadOrThrow[NetworkSettings]
val walletSettings = wavesConfigSource.at("wallet").loadOrThrow[WalletSettings]
val blockchainSettings = BlockchainSettings.fromConfig(waves.getConfig("blockchain"))
val blockchainSettings = wavesConfigSource.at("blockchain").loadOrThrow[BlockchainSettings]
val minerSettings = wavesConfigSource.at("miner").loadOrThrow[MinerSettings]
val restAPISettings = wavesConfigSource.at("rest-api").loadOrThrow[RestAPISettings]
val synchronizationSettings = wavesConfigSource.at("synchronization").loadOrThrow[SynchronizationSettings]
val utxSettings = wavesConfigSource.at("utx").loadOrThrow[UtxSettings]
val featuresSettings = wavesConfigSource.at("features").loadOrThrow[FeaturesSettings]
val rewardsSettings = wavesConfigSource.at("rewards").loadOrThrow[RewardsVotingSettings]
val metrics = ConfigSource.fromConfig(rootConfig).at("metrics").loadOrThrow[Metrics.Settings] // TODO: Move to waves section
val enableLightMode = wavesConfigSource.at("enable-light-mode").loadOrThrow[Boolean]

WavesSettings(
directory,
Expand Down

0 comments on commit f3f7eec

Please sign in to comment.