Move getBestFilterHeader() use the best chain by chainwork for determining the fitler header (#1964)

This commit is contained in:
Chris Stewart 2020-09-02 19:41:52 -05:00 committed by GitHub
parent f5fcab9580
commit 8b76acd042

View File

@ -390,25 +390,22 @@ case class ChainHandler(
/** @inheritdoc */
override def getBestFilterHeader(): Future[Option[CompactFilterHeaderDb]] = {
val bestFilterHeadersInChains: Vector[
Future[Option[CompactFilterHeaderDb]]] = {
blockchains.map { blockchain =>
filterHeaderDAO.getBestFilterHeaderForHeaders(blockchain.toVector)
val bestFilterHeadersInChain: Future[Option[CompactFilterHeaderDb]] = {
val bestChainOpt = blockchains.maxByOption(_.tip.chainWork)
bestChainOpt match {
case Some(bestChain) =>
filterHeaderDAO.getBestFilterHeaderForHeaders(bestChain.toVector)
case None => Future.successful(None)
}
}
val filterHeadersOptF: Future[Vector[Option[CompactFilterHeaderDb]]] = {
Future.sequence(bestFilterHeadersInChains)
}
for {
filterHeaders <- filterHeadersOptF
flattened = filterHeaders.flatten
filterHeaderOpt <- bestFilterHeadersInChain
result <-
if (flattened.isEmpty) {
if (filterHeaderOpt.isEmpty) {
bestFilterHeaderSearch()
} else {
Future.successful(flattened.maxByOption(_.height))
Future.successful(filterHeaderOpt)
}
} yield {
result