Refactor waiting for bitcoind lost blocks into helper method, add context (#4602)

This commit is contained in:
Chris Stewart 2022-08-12 12:08:27 -05:00 committed by GitHub
parent 9fd9cf1ee4
commit b0831f26a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -24,7 +24,7 @@ import org.bitcoins.core.api.node.{
NodeApi,
NodeType
}
import org.bitcoins.core.api.wallet.NeutrinoHDWalletApi
import org.bitcoins.core.api.wallet.{NeutrinoHDWalletApi, WalletApi}
import org.bitcoins.core.util.TimeUtil
import org.bitcoins.dlc.node.DLCNode
import org.bitcoins.dlc.node.config.DLCNodeAppConfig
@ -540,18 +540,7 @@ class BitcoinSServerMain(override val serverArgParser: ServerArgParser)(implicit
wallet: NeutrinoHDWalletApi,
chainCallbacksOpt: Option[ChainCallbacks]): Future[BitcoindSyncState] = {
val f = for {
_ <- AsyncUtil.retryUntilSatisfiedF(
conditionF = { () =>
for {
bitcoindHeight <- bitcoind.getBlockCount
walletStateOpt <- wallet.getSyncDescriptorOpt()
} yield walletStateOpt.forall(bitcoindHeight >= _.height)
},
// retry for approximately 2 hours
mode = Exponential,
interval = 1.second,
maxTries = 12
)
_ <- handlePotentialBitcoindLostBlock(bitcoind, wallet)
syncF = BitcoindRpcBackendUtil.syncWalletToBitcoind(
bitcoind,
wallet,
@ -590,6 +579,27 @@ class BitcoinSServerMain(override val serverArgParser: ServerArgParser)(implicit
f
}
/** Surprisingly on some OSes like umbrel bitcoind can lose blocks during the shutdown process
* This means next time we boot up, our wallet will have more blocks than bitcoind!
* Eventually bitcoind will synchrnoize with the network. This waits until bitcoind is synced
*/
private def handlePotentialBitcoindLostBlock(
bitcoind: BitcoindRpcClient,
wallet: WalletApi): Future[Unit] = {
AsyncUtil.retryUntilSatisfiedF(
conditionF = { () =>
for {
bitcoindHeight <- bitcoind.getBlockCount
walletStateOpt <- wallet.getSyncDescriptorOpt()
} yield walletStateOpt.forall(bitcoindHeight >= _.height)
},
// retry for approximately 2 hours
mode = Exponential,
interval = 1.second,
maxTries = 12
)
}
/** Builds a websocket queue that you can feed elements to.
* The Source can be wired up with Directives.handleWebSocketMessages
* to create a flow that emits websocket messages