Implement cleanup of dlcWallet/wallet DbAppConfig threadpools (#4557)

* Implement cleanup of dlcWallet/wallet DbAppConfig threadpools

* Cleanup

* Remove uncessary walletHolder.start()
This commit is contained in:
Chris Stewart 2022-08-01 06:55:02 -05:00 committed by GitHub
parent 603b7e0aea
commit f487c1270e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 142 additions and 10 deletions

View File

@ -101,6 +101,10 @@ class BitcoinSServerMain(override val serverArgParser: ServerArgParser)(implicit
mempoolPollingCancellableOpt.foreach(_.cancel())
for {
_ <- conf.stop()
_ <- walletLoaderApiOpt match {
case Some(l) => l.stop()
case None => Future.unit
}
_ <- serverBindingsOpt match {
case Some(bindings) => bindings.stop()
case None => Future.unit
@ -143,10 +147,12 @@ class BitcoinSServerMain(override val serverArgParser: ServerArgParser)(implicit
for {
node <- nodeF
} yield {
DLCWalletNeutrinoBackendLoader(walletHolder,
chainApi,
nodeApi = node,
feeRateApi = feeProvider)
val l = DLCWalletNeutrinoBackendLoader(walletHolder,
chainApi,
nodeApi = node,
feeRateApi = feeProvider)
walletLoaderApiOpt = Some(l)
l
}
}
@ -292,6 +298,9 @@ class BitcoinSServerMain(override val serverArgParser: ServerArgParser)(implicit
private var mempoolPollingCancellableOpt: Option[BitcoindPollingCancellabe] =
None
/** The wallet loader that is being used for our wallet. */
private[this] var walletLoaderApiOpt: Option[DLCWalletLoaderApi] = None
/** Start the bitcoin-s wallet server with a bitcoind backend
* @param startedTorConfigF a future that is completed when tor is fully started
* @return
@ -352,10 +361,15 @@ class BitcoinSServerMain(override val serverArgParser: ServerArgParser)(implicit
bitcoind <- bitcoindF
nodeApi <- nodeApiF
feeProvider <- feeProviderF
} yield DLCWalletBitcoindBackendLoader(walletHolder,
bitcoind,
nodeApi,
feeProvider)
} yield {
val l = DLCWalletBitcoindBackendLoader(walletHolder,
bitcoind,
nodeApi,
feeProvider)
walletLoaderApiOpt = Some(l)
l
}
}
val walletF: Future[(WalletHolder, WalletAppConfig, DLCAppConfig)] = {

View File

@ -6,6 +6,7 @@ import org.bitcoins.core.api.chain.ChainQueryApi
import org.bitcoins.core.api.dlc.wallet.DLCNeutrinoHDWalletApi
import org.bitcoins.core.api.feeprovider.FeeRateApi
import org.bitcoins.core.api.node.NodeApi
import org.bitcoins.core.util.StartStopAsync
import org.bitcoins.crypto.AesPassword
import org.bitcoins.dlc.wallet.DLCAppConfig
import org.bitcoins.node.NodeCallbacks
@ -21,8 +22,9 @@ import scala.concurrent.{ExecutionContext, Future}
/** A trait used to help load a different load and discard the current wallet in memory
* This trait encapsulates the heavy lifting done in the 'loadwallet' RPC command
*/
sealed trait DLCWalletLoaderApi extends Logging {
sealed trait DLCWalletLoaderApi extends Logging with StartStopAsync[Unit] {
override def start(): Future[Unit] = Future.unit
protected def conf: BitcoinSAppConfig
implicit protected def system: ActorSystem
@ -56,7 +58,6 @@ sealed trait DLCWalletLoaderApi extends Logging {
_ <- stoppedCallbacksF
(walletConfig, dlcConfig) <- updateWalletConfigs(walletName,
Some(aesPasswordOpt))
.recover { case _: Throwable => (conf.walletConf, conf.dlcConf) }
_ <- {
if (walletHolder.isInitialized) {
walletHolder
@ -116,6 +117,25 @@ case class DLCWalletNeutrinoBackendLoader(
import system.dispatcher
implicit private val nodeConf = conf.nodeConf
private[this] var currentWalletAppConfigOpt: Option[WalletAppConfig] = None
private[this] var currentDLCAppConfigOpt: Option[DLCAppConfig] = None
override def stop(): Future[Unit] = {
val walletStopF = currentWalletAppConfigOpt match {
case Some(w) => w.stop()
case None => Future.unit
}
val dlcStopF = currentDLCAppConfigOpt match {
case Some(d) => d.stop()
case None => Future.unit
}
for {
_ <- walletStopF
_ <- dlcStopF
} yield ()
}
override def load(
walletNameOpt: Option[String],
aesPasswordOpt: Option[AesPassword]): Future[
@ -130,6 +150,8 @@ case class DLCWalletNeutrinoBackendLoader(
walletNameOpt = walletNameOpt,
aesPasswordOpt = aesPasswordOpt
)
_ <- stopOldWalletAppConfig(walletConfig)
_ <- stopOldDLCAppConfig(dlcConfig)
_ <- walletHolder.replaceWallet(dlcWallet)
nodeCallbacks <-
CallbackUtil.createNeutrinoNodeCallbacksForWallet(walletHolder)
@ -137,6 +159,43 @@ case class DLCWalletNeutrinoBackendLoader(
_ <- updateWalletName(walletNameOpt)
} yield (walletHolder, walletConfig, dlcConfig)
}
private def stopOldWalletAppConfig(
newWalletConfig: WalletAppConfig): Future[Unit] = {
currentWalletAppConfigOpt match {
case Some(current) =>
//stop the old config
current
.stop()
.map(_ => {
currentWalletAppConfigOpt = Some(newWalletConfig)
})
case None =>
for {
_ <- conf.walletConf.stop()
} yield {
currentWalletAppConfigOpt = Some(newWalletConfig)
}
}
}
private def stopOldDLCAppConfig(newDlcConfig: DLCAppConfig): Future[Unit] = {
currentDLCAppConfigOpt match {
case Some(current) =>
//stop the old config
current
.stop()
.map(_ => {
currentDLCAppConfigOpt = Some(newDlcConfig)
})
case None =>
for {
_ <- conf.walletConf.stop()
} yield {
currentDLCAppConfigOpt = Some(newDlcConfig)
}
}
}
}
case class DLCWalletBitcoindBackendLoader(
@ -150,6 +209,26 @@ case class DLCWalletBitcoindBackendLoader(
import system.dispatcher
implicit private val nodeConf = conf.nodeConf
private[this] var currentWalletAppConfigOpt: Option[WalletAppConfig] = None
private[this] var currentDLCAppConfigOpt: Option[DLCAppConfig] = None
override def stop(): Future[Unit] = {
val walletStopF = currentWalletAppConfigOpt match {
case Some(w) => w.stop()
case None => Future.unit
}
val dlcStopF = currentDLCAppConfigOpt match {
case Some(d) => d.stop()
case None => Future.unit
}
for {
_ <- walletStopF
_ <- dlcStopF
} yield ()
}
override def load(
walletNameOpt: Option[String],
aesPasswordOpt: Option[AesPassword]): Future[
@ -163,10 +242,49 @@ case class DLCWalletBitcoindBackendLoader(
walletNameOpt = walletNameOpt,
aesPasswordOpt = aesPasswordOpt)
_ <- stopOldWalletAppConfig(walletConfig)
_ <- stopOldDLCAppConfig(dlcConfig)
nodeCallbacks <- CallbackUtil.createBitcoindNodeCallbacksForWallet(
walletHolder)
_ = nodeConf.addCallbacks(nodeCallbacks)
_ <- walletHolder.replaceWallet(dlcWallet)
} yield (walletHolder, walletConfig, dlcConfig)
}
private def stopOldWalletAppConfig(
newWalletConfig: WalletAppConfig): Future[Unit] = {
currentWalletAppConfigOpt match {
case Some(current) =>
//stop the old config
current
.stop()
.map(_ => {
currentWalletAppConfigOpt = Some(newWalletConfig)
})
case None =>
for {
_ <- conf.walletConf.stop()
} yield {
currentWalletAppConfigOpt = Some(newWalletConfig)
}
}
}
private def stopOldDLCAppConfig(newDlcConfig: DLCAppConfig): Future[Unit] = {
currentDLCAppConfigOpt match {
case Some(current) =>
//stop the old config
current
.stop()
.map(_ => {
currentDLCAppConfigOpt = Some(newDlcConfig)
})
case None =>
for {
_ <- conf.walletConf.stop()
} yield {
currentDLCAppConfigOpt = Some(newDlcConfig)
}
}
}
}