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

Poll bitcoind at startup instead of trying only once #2739

Merged
merged 3 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions eclair-core/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ eclair {
// - ignore: eclair will leave these utxos locked and start
startup-locked-utxos-behavior = "stop"
final-pubkey-refresh-delay = 3 seconds
// If true, eclair will poll bitcoind for 30 seconds at start-up before giving up.
wait-for-bitcoind-up = true
sstone marked this conversation as resolved.
Show resolved Hide resolved
}

node-alias = "eclair"
Expand Down
57 changes: 37 additions & 20 deletions eclair-core/src/main/scala/fr/acinq/eclair/Setup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,9 @@ class Setup(val datadir: File,
case "password" => BitcoinJsonRPCAuthMethod.UserPassword(config.getString("bitcoind.rpcuser"), config.getString("bitcoind.rpcpassword"))
}

val bitcoinClient = new BasicBitcoinJsonRPCClient(
rpcAuthMethod = rpcAuthMethod,
host = config.getString("bitcoind.host"),
port = config.getInt("bitcoind.rpcport"),
wallet = wallet)
case class BitcoinStatus(version: Int, chainHash: ByteVector32, initialBlockDownload: Boolean, verificationProgress: Double, blockCount: Long, headerCount: Long, unspentAddresses: List[String])

val future = for {
def getBitcoinStatus(bitcoinClient: BasicBitcoinJsonRPCClient): Future[BitcoinStatus] = for {
json <- bitcoinClient.invoke("getblockchaininfo").recover { case e => throw BitcoinRPCConnectionException(e) }
// Make sure wallet support is enabled in bitcoind.
wallets <- bitcoinClient.invoke("listwallets").recover { case e => throw BitcoinWalletDisabledException(e) }
Expand All @@ -165,20 +161,41 @@ class Setup(val datadir: File,
case "signet" => bitcoinClient.invoke("getrawtransaction", "ff1027486b628b2d160859205a3401fb2ee379b43527153b0b50a92c17ee7955") // coinbase of #5000
case "regtest" => Future.successful(())
}
} yield (progress, ibd, chainHash, bitcoinVersion, unspentAddresses, blocks, headers)
// blocking sanity checks
val (progress, initialBlockDownload, chainHash, bitcoinVersion, unspentAddresses, blocks, headers) = await(future, 30 seconds, "bitcoind did not respond after 30 seconds")
logger.info(s"bitcoind version=$bitcoinVersion")
assert(bitcoinVersion >= 230000, "Eclair requires Bitcoin Core 23.0 or higher")
assert(unspentAddresses.forall(address => !isPay2PubkeyHash(address)), "Your wallet contains non-segwit UTXOs. You must send those UTXOs to a bech32 address to use Eclair (check out our README for more details).")
if (chainHash != Block.RegtestGenesisBlock.hash) {
assert(!initialBlockDownload, s"bitcoind should be synchronized (initialblockdownload=$initialBlockDownload)")
assert(progress > 0.999, s"bitcoind should be synchronized (progress=$progress)")
assert(headers - blocks <= 1, s"bitcoind should be synchronized (headers=$headers blocks=$blocks)")
} yield BitcoinStatus(bitcoinVersion, chainHash, ibd, progress, blocks, headers, unspentAddresses)

def pollBitcoinStatus(bitcoinClient: BasicBitcoinJsonRPCClient): Future[BitcoinStatus] = {
getBitcoinStatus(bitcoinClient).transformWith {
case Success(status) => Future.successful(status)
case Failure(e) =>
logger.warn(s"failed to connect to bitcoind (${e.getMessage}), retrying...")
after(5 seconds) {
pollBitcoinStatus(bitcoinClient)
}
}
}

val bitcoinClient = new BasicBitcoinJsonRPCClient(
rpcAuthMethod = rpcAuthMethod,
host = config.getString("bitcoind.host"),
port = config.getInt("bitcoind.rpcport"),
wallet = wallet
)
val bitcoinStatus = if (config.getBoolean("bitcoind.wait-for-bitcoind-up")) {
await(pollBitcoinStatus(bitcoinClient), 30 seconds, "bitcoind wasn't ready after 30 seconds")
} else {
await(getBitcoinStatus(bitcoinClient), 30 seconds, "bitcoind did not respond after 30 seconds")
}
logger.info(s"bitcoind version=${bitcoinStatus.version}")
assert(bitcoinStatus.version >= 240100, "Eclair requires Bitcoin Core 24.1 or higher")
assert(bitcoinStatus.unspentAddresses.forall(address => !isPay2PubkeyHash(address)), "Your wallet contains non-segwit UTXOs. You must send those UTXOs to a bech32 address to use Eclair (check out our README for more details).")
if (bitcoinStatus.chainHash != Block.RegtestGenesisBlock.hash) {
assert(!bitcoinStatus.initialBlockDownload, s"bitcoind should be synchronized (initialblockdownload=${bitcoinStatus.initialBlockDownload})")
assert(bitcoinStatus.verificationProgress > 0.999, s"bitcoind should be synchronized (progress=${bitcoinStatus.verificationProgress})")
assert(bitcoinStatus.headerCount - bitcoinStatus.blockCount <= 1, s"bitcoind should be synchronized (headers=${bitcoinStatus.headerCount} blocks=${bitcoinStatus.blockCount})")
}
logger.info(s"current blockchain height=$blocks")
blockHeight.set(blocks)
(bitcoinClient, chainHash)
logger.info(s"current blockchain height=${bitcoinStatus.blockCount}")
blockHeight.set(bitcoinStatus.blockCount)
(bitcoinClient, bitcoinStatus.chainHash)
}

val instanceId = UUID.randomUUID()
Expand Down Expand Up @@ -234,7 +251,7 @@ class Setup(val datadir: File,
blockchain.Monitoring.Metrics.FeeratesPerByte.withTag(blockchain.Monitoring.Tags.Priority, blockchain.Monitoring.Tags.Priorities.Fast).update(feeratesPerKw.get.fast.toLong.toDouble)
blockchain.Monitoring.Metrics.FeeratesPerByte.withTag(blockchain.Monitoring.Tags.Priority, blockchain.Monitoring.Tags.Priorities.Fastest).update(feeratesPerKw.get.fastest.toLong.toDouble)
system.eventStream.publish(CurrentFeerates(feeratesPerKw.get))
logger.info(s"current feeratesPerKB=${feeratesPerKB} feeratesPerKw=${feeratesPerKw.get}")
logger.info(s"current feeratesPerKB=$feeratesPerKB feeratesPerKw=${feeratesPerKw.get}")
feeratesRetrieved.trySuccess(Done)
case Failure(exception) =>
logger.warn(s"cannot retrieve feerates: ${exception.getMessage}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package fr.acinq.eclair.integration

import akka.testkit.TestProbe
import com.typesafe.config.ConfigFactory
import com.typesafe.config.{Config, ConfigFactory}
import fr.acinq.eclair.blockchain.bitcoind.BitcoindService.BitcoinReq
import fr.acinq.eclair.blockchain.bitcoind.rpc.{Error, JsonRPCError}
import fr.acinq.eclair.{BitcoinDefaultWalletException, BitcoinWalletDisabledException, BitcoinWalletNotLoadedException, TestUtils}
Expand All @@ -30,41 +30,49 @@ import scala.jdk.CollectionConverters._

class StartupIntegrationSpec extends IntegrationSpec {

private def createConfig(wallet_opt: Option[String]): Config = {
val defaultConfig = ConfigFactory.parseMap(Map("eclair.bitcoind.wait-for-bitcoind-up" -> "false", "eclair.server.port" -> TestUtils.availablePort).asJava).withFallback(withStaticRemoteKey).withFallback(commonConfig)
wallet_opt match {
case Some(wallet) => ConfigFactory.parseMap(Map("eclair.bitcoind.wallet" -> wallet).asJava).withFallback(defaultConfig)
case None => defaultConfig.withoutPath("eclair.bitcoind.wallet")
}
}

test("no bitcoind wallet configured and one wallet loaded") {
instantiateEclairNode("A", ConfigFactory.parseMap(Map("eclair.server.port" -> TestUtils.availablePort).asJava).withFallback(withStaticRemoteKey).withFallback(commonConfig).withoutPath("eclair.bitcoind.wallet"))
instantiateEclairNode("A", createConfig(wallet_opt = None))
}

test("no bitcoind wallet configured and two wallets loaded") {
val sender = TestProbe()
sender.send(bitcoincli, BitcoinReq("createwallet", ""))
sender.send(bitcoincli, BitcoinReq("createwallet", "other_wallet"))
sender.expectMsgType[Any]
val thrown = intercept[BitcoinDefaultWalletException] {
instantiateEclairNode("C", ConfigFactory.parseMap(Map("eclair.server.port" -> TestUtils.availablePort).asJava).withFallback(withStaticRemoteKey).withFallback(commonConfig).withoutPath("eclair.bitcoind.wallet"))
instantiateEclairNode("C", createConfig(wallet_opt = None))
}
assert(thrown == BitcoinDefaultWalletException(List(defaultWallet, "")))
assert(thrown == BitcoinDefaultWalletException(List(defaultWallet, "other_wallet")))
}

test("explicit bitcoind wallet configured and two wallets loaded") {
val sender = TestProbe()
sender.send(bitcoincli, BitcoinReq("createwallet", ""))
sender.send(bitcoincli, BitcoinReq("createwallet", "other_wallet"))
sender.expectMsgType[Any]
instantiateEclairNode("D", ConfigFactory.parseMap(Map("eclair.server.port" -> TestUtils.availablePort).asJava).withFallback(withStaticRemoteKey).withFallback(commonConfig))
instantiateEclairNode("D", createConfig(wallet_opt = Some(defaultWallet)))
}

test("explicit bitcoind wallet configured but not loaded") {
val sender = TestProbe()
sender.send(bitcoincli, BitcoinReq("createwallet", ""))
sender.send(bitcoincli, BitcoinReq("createwallet", "other_wallet"))
sender.expectMsgType[Any]
val thrown = intercept[BitcoinWalletNotLoadedException] {
instantiateEclairNode("E", ConfigFactory.parseMap(Map("eclair.bitcoind.wallet" -> "notloaded", "eclair.server.port" -> TestUtils.availablePort).asJava).withFallback(withStaticRemoteKey).withFallback(commonConfig))
instantiateEclairNode("E", createConfig(wallet_opt = Some("not_loaded")))
}
assert(thrown == BitcoinWalletNotLoadedException("notloaded", List(defaultWallet, "")))
assert(thrown == BitcoinWalletNotLoadedException("not_loaded", List(defaultWallet, "other_wallet")))
}

test("bitcoind started with wallets disabled") {
restartBitcoind(startupFlags = "-disablewallet", loadWallet = false)
val thrown = intercept[BitcoinWalletDisabledException] {
instantiateEclairNode("F", ConfigFactory.load().getConfig("eclair").withFallback(withStaticRemoteKey).withFallback(commonConfig))
instantiateEclairNode("F", createConfig(wallet_opt = None))
}
assert(thrown == BitcoinWalletDisabledException(e = JsonRPCError(Error(-32601, "Method not found"))))
}
Expand Down