mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-01-18 13:24:25 +01:00
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:
parent
d3af9c2ccb
commit
170242812c
@ -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()
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user