Fix bug where it prevented us from rescanning an empty wallet (#4632)

* Fix bug where it prevented us from rescanning an empty wallet with a seed to try and recover funds

* Fix unit test to not expect isEmpty() wallet calls
This commit is contained in:
Chris Stewart 2022-08-20 17:03:22 -05:00 committed by GitHub
parent eb1327824b
commit b18da7ac2b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 42 deletions

View file

@ -1706,9 +1706,6 @@ class RoutesSpec extends AnyWordSpec with ScalatestRouteTest with MockFactory {
.expects()
.returning(100)
.atLeastOnce()
(mockWalletApi.isEmpty: () => Future[Boolean])
.expects()
.returning(Future.successful(false))
(mockWalletApi
.rescanNeutrinoWallet(_: Option[BlockStamp],
_: Option[BlockStamp],
@ -1729,9 +1726,6 @@ class RoutesSpec extends AnyWordSpec with ScalatestRouteTest with MockFactory {
responseAs[String] == """{"result":"Rescan started.","error":null}""")
}
(mockWalletApi.isEmpty: () => Future[Boolean])
.expects()
.returning(Future.successful(false))
(mockWalletApi
.rescanNeutrinoWallet(_: Option[BlockStamp],
_: Option[BlockStamp],
@ -1762,9 +1756,6 @@ class RoutesSpec extends AnyWordSpec with ScalatestRouteTest with MockFactory {
}
(mockWalletApi.isEmpty: () => Future[Boolean])
.expects()
.returning(Future.successful(false))
(mockWalletApi
.rescanNeutrinoWallet(_: Option[BlockStamp],
_: Option[BlockStamp],
@ -1792,9 +1783,6 @@ class RoutesSpec extends AnyWordSpec with ScalatestRouteTest with MockFactory {
responseAs[String] == """{"result":"Rescan started.","error":null}""")
}
(mockWalletApi.isEmpty: () => Future[Boolean])
.expects()
.returning(Future.successful(false))
(mockWalletApi
.rescanNeutrinoWallet(_: Option[BlockStamp],
_: Option[BlockStamp],
@ -1858,9 +1846,6 @@ class RoutesSpec extends AnyWordSpec with ScalatestRouteTest with MockFactory {
responseAs[String] == s"""{"result":null,"error":"Expected a positive integer (data: -1)"}""")
}
(mockWalletApi.isEmpty: () => Future[Boolean])
.expects()
.returning(Future.successful(false))
(mockWalletApi
.rescanNeutrinoWallet(_: Option[BlockStamp],
_: Option[BlockStamp],

View file

@ -1083,35 +1083,29 @@ case class WalletRoutes(loadWalletApi: DLCWalletLoaderApi)(implicit
private def handleRescan(rescan: Rescan): Future[String] = {
if (loadWalletApi.isRescanStateEmpty) {
val res = for {
empty <- wallet.isEmpty()
rescanState <- {
if (empty) {
//if wallet is empty, just return Done immediately
Future.successful(RescanState.RescanDone)
} else {
rescanStateOpt match {
case Some(rescanState) =>
val stateF: Future[RescanState] = rescanState match {
case started: RescanState.RescanStarted =>
if (started.isStopped) {
//means rescan is done, reset the variable
rescanStateOpt = Some(RescanDone)
Future.successful(RescanDone)
} else {
//do nothing, we don't want to reset/stop a rescan that is running
Future.successful(started)
}
case RescanState.RescanDone =>
//if the previous rescan is done, start another rescan
startRescan(rescan)
case RescanState.RescanAlreadyStarted =>
Future.successful(RescanState.RescanAlreadyStarted)
}
rescanStateOpt match {
case Some(rescanState) =>
val stateF: Future[RescanState] = rescanState match {
case started: RescanState.RescanStarted =>
if (started.isStopped) {
//means rescan is done, reset the variable
rescanStateOpt = Some(RescanDone)
Future.successful(RescanDone)
} else {
//do nothing, we don't want to reset/stop a rescan that is running
Future.successful(started)
}
case RescanState.RescanDone =>
//if the previous rescan is done, start another rescan
startRescan(rescan)
case RescanState.RescanAlreadyStarted =>
Future.successful(RescanState.RescanAlreadyStarted)
}
stateF
case None =>
startRescan(rescan)
}
stateF
case None =>
startRescan(rescan)
}
}
_ = loadWalletApi.setRescanState(rescanState)