Skip to content
10 changes: 7 additions & 3 deletions app/src/main/kotlin/maru/app/MaruApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ import org.hyperledger.besu.plugin.services.MetricsSystem
import org.web3j.protocol.Web3j
import tech.pegasys.teku.ethereum.executionclient.web3j.Web3JClient

interface LongRunningCloseable :
LongRunningService,
AutoCloseable

class MaruApp(
val config: MaruConfig,
val beaconGenesisConfig: ForksSchedule,
Expand All @@ -58,7 +62,7 @@ class MaruApp(
private val apiServer: ApiServer,
private val syncStatusProvider: SyncStatusProvider,
private val syncControllerManager: LongRunningService,
) : AutoCloseable {
) : LongRunningCloseable {
private val log: Logger = LogManager.getLogger(this.javaClass)

private fun getPrivateKeyWithoutPrefix() = Crypto.privateKeyBytesWithoutPrefix(privateKeyProvider())
Expand Down Expand Up @@ -112,7 +116,7 @@ class MaruApp(
privateKeyWithoutPrefix = getPrivateKeyWithoutPrefix(),
)

fun start() {
override fun start() {
if (finalizationProvider is LineaFinalizationProvider) {
start("Finalization Provider", finalizationProvider::start)
}
Expand All @@ -128,7 +132,7 @@ class MaruApp(
log.info("Maru is up")
}

fun stop() {
override fun stop() {
stop("Sync service", syncControllerManager::stop)
stop("P2P Network") { p2pNetwork.stop().get() }
if (finalizationProvider is LineaFinalizationProvider) {
Expand Down
58 changes: 31 additions & 27 deletions app/src/main/kotlin/maru/app/MaruAppCli.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,22 @@ enum class Network(
descriptionHeading = "%nDescription:%n%n",
optionListHeading = "%nOptions:%n",
footerHeading = "%n",
mixinStandardHelpOptions = true,
)
class MaruAppCli(
private val dryRun: Boolean = false,
private val maruAppFactory: MaruAppFactoryCreator = MaruAppFactory(),
) : Callable<Int> {
private val log = LogManager.getLogger(this.javaClass)

@Option(
names = ["--config"],
paramLabel = "CONFIG.toml,CONFIG.overrides.toml",
description = ["Configuration files"],
paramLabel = "CONFIG.toml",
description = [
"Comma-separated list of configuration files or in multiple options" +
" e.g. \"--config=CONFIG.toml --config=CONFIG.overrides.toml\"",
],
arity = "1..*",
split = ",",
required = true,
)
val configFiles: List<File>? = null
Expand Down Expand Up @@ -116,33 +121,32 @@ class MaruAppCli(
log.info("Using the genesis file of the named network \"${genesisOptions!!.network!!.networkNameInKebab}\"")
}

if (!dryRun) {
val parsedAppConfig = MaruConfigLoader.loadAppConfigs(configFiles)
val parsedBeaconGenesisConfig = MaruConfigLoader.loadGenesisConfig(genesisOptions!!.genesisFile!!)
val app =
MaruAppFactory()
.create(
config = parsedAppConfig.domainFriendly(),
beaconGenesisConfig = parsedBeaconGenesisConfig.domainFriendly(),
)
app.start()
val parsedAppConfig = MaruConfigLoader.loadAppConfigs(configFiles)
val parsedBeaconGenesisConfig = MaruConfigLoader.loadGenesisConfig(genesisOptions!!.genesisFile!!)

Runtime
.getRuntime()
.addShutdownHook(
Thread {
app.stop()
if (LogManager.getContext() is LoggerContext) {
// Disable log4j auto shutdown hook is not used otherwise
// Messages in App.stop won't appear in the logs
Configurator.shutdown(LogManager.getContext() as LoggerContext)
}
},
)
}
val app =
maruAppFactory.create(
config = parsedAppConfig.domainFriendly(),
beaconGenesisConfig = parsedBeaconGenesisConfig.domainFriendly(),
)
app.start()

Runtime
.getRuntime()
.addShutdownHook(
Thread {
app.stop()
app.close()
if (LogManager.getContext() is LoggerContext) {
// Disable log4j auto shutdown hook is not used otherwise
// Messages in App.stop won't appear in the logs
Configurator.shutdown(LogManager.getContext() as LoggerContext)
}
},
)

return 0
}

private fun validateFileCanRead(file: File): Boolean = dryRun || file.canRead()
private fun validateFileCanRead(file: File): Boolean = file.canRead()
}
34 changes: 30 additions & 4 deletions app/src/main/kotlin/maru/app/MaruAppFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ import tech.pegasys.teku.ethereum.executionclient.web3j.Web3JClient
import tech.pegasys.teku.networking.p2p.network.config.GeneratingFilePrivateKeySource
import org.hyperledger.besu.plugin.services.MetricsSystem as BesuMetricsSystem

class MaruAppFactory {
private val log = LogManager.getLogger(this.javaClass)

interface MaruAppFactoryCreator {
fun create(
config: MaruConfig,
beaconGenesisConfig: ForksSchedule,
Expand All @@ -107,7 +105,35 @@ class MaruAppFactory {
P2PState,
() -> SyncStatusProvider,
) -> P2PNetworkImpl = ::P2PNetworkImpl,
): MaruApp {
): LongRunningCloseable
}

class MaruAppFactory : MaruAppFactoryCreator {
private val log = LogManager.getLogger(this.javaClass)

override fun create(
config: MaruConfig,
beaconGenesisConfig: ForksSchedule,
clock: Clock,
overridingP2PNetwork: P2PNetwork?,
overridingFinalizationProvider: FinalizationProvider?,
overridingLineaContractClient: LineaRollupSmartContractClientReadOnly?,
overridingApiServer: ApiServer?,
p2pNetworkFactory: (
ByteArray,
P2PConfig,
UInt,
SerDe<SealedBeaconBlock>,
MetricsFacade,
BesuMetricsSystem,
StatusManager,
BeaconChain,
ForkPeeringManager,
() -> Boolean,
P2PState,
() -> SyncStatusProvider,
) -> P2PNetworkImpl,
): LongRunningCloseable {
log.info("configs={}", config)
log.info("beaconGenesisConfig={}", beaconGenesisConfig)

Expand Down
Loading