Fix maxBy() exception in the case of empty Blockchain in ChainHandler… (#1934)

* Fix maxBy() exception in the case of empty Blockchain in ChainHandler.bestFilterHeaderSearch()

* Add another unit test to CompactFilterHeaderDAO for coverage
This commit is contained in:
Chris Stewart 2020-08-29 15:29:09 -05:00 committed by GitHub
parent d3af9c2ccb
commit 170242812c
2 changed files with 38 additions and 5 deletions

View File

@ -54,6 +54,29 @@ class CompactFilterHeaderDAOTest extends ChainDbUnitTest {
}
}
it must "find filters between height" in { filterHeaderDAO =>
val blockHeaderDAO = BlockHeaderDAO()
val blockHeaderDb =
BlockHeaderHelper.buildNextHeader(ChainTestUtil.regTestGenesisHeaderDb)
val blockHeaderDbF = blockHeaderDAO.create(blockHeaderDb)
val filterHeaderDb1F = for {
blockHeaderDb <- blockHeaderDbF
} yield {
randomFilterHeader(blockHeaderDb)
}
val createdF = filterHeaderDb1F.flatMap(filterHeaderDAO.create)
for {
headerDb <- createdF
fromDbVec <-
filterHeaderDAO.getBetweenHeights(headerDb.height, headerDb.height)
} yield {
assert(fromDbVec.length == 1)
assert(fromDbVec.head == headerDb)
}
}
it must "get the best filter header that has a block header associated with it" in {
filterHeaderDAO =>
val blockHeaderDAO = BlockHeaderDAO()

View File

@ -410,7 +410,7 @@ case class ChainHandler(
val bestFilterHeaderOptF = filterHeaderDAO.getBestFilterHeader
//get best blockchain around our latest filter header
val blockchainF: Future[Blockchain] = {
val blockchainOptF: Future[Option[Blockchain]] = {
for {
bestFilterHeaderOpt <- bestFilterHeaderOptF
blockchains <- {
@ -429,15 +429,25 @@ case class ChainHandler(
}
}
} yield {
blockchains.maxBy(_.tip.chainWork)
if (blockchains.isEmpty) {
None
} else {
Some(blockchains.maxBy(_.tip.chainWork))
}
}
}
val filterHeadersOptF: Future[Option[CompactFilterHeaderDb]] = {
for {
blockchain <- blockchainF
bestHeadersForChain <-
filterHeaderDAO.getBestFilterHeaderForHeaders(blockchain.toVector)
blockchainOpt <- blockchainOptF
bestHeadersForChainFOpt = {
blockchainOpt.map(b =>
filterHeaderDAO.getBestFilterHeaderForHeaders(b.toVector))
}
bestHeadersForChain <- bestHeadersForChainFOpt match {
case Some(f) => f
case None => Future.successful(None)
}
} yield bestHeadersForChain
}