mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2024-11-19 01:40:55 +01:00
Remove BlockHeader.getBlockchainsFrom(), rework type signature for Bl… (#2431)
* Remove BlockHeader.getBlockchainsFrom(), rework type signature for BlockHeader.getBlockchainFrom() to return Future[Option[Blockchain]] * fix nits
This commit is contained in:
parent
af89c04d15
commit
936d4e4911
@ -104,14 +104,19 @@ class BitcoinPowTest extends ChainDbUnitTest {
|
|||||||
val nestedAssertions: Vector[Future[Assertion]] = {
|
val nestedAssertions: Vector[Future[Assertion]] = {
|
||||||
iterator.map { height =>
|
iterator.map { height =>
|
||||||
val blockF = blockHeaderDAO.getAtHeight(height + 1).map(_.head)
|
val blockF = blockHeaderDAO.getAtHeight(height + 1).map(_.head)
|
||||||
val blockchainF =
|
val blockchainOptF: Future[Option[Blockchain]] =
|
||||||
blockF.flatMap(b => blockHeaderDAO.getBlockchainFrom(b))
|
blockF.flatMap(b => blockHeaderDAO.getBlockchainFrom(b))
|
||||||
for {
|
|
||||||
blockchain <- blockchainF
|
blockchainOptF.map {
|
||||||
nextTip = blockchain.head
|
case Some(blockchain) =>
|
||||||
chain = Blockchain.fromHeaders(blockchain.tail.toVector)
|
val chain = Blockchain.fromHeaders(blockchain.tail.toVector)
|
||||||
nextNBits = Pow.getNetworkWorkRequired(nextTip.blockHeader, chain)
|
val nextTip = blockchain.tip
|
||||||
} yield assert(nextNBits == nextTip.nBits)
|
val nextNBits =
|
||||||
|
Pow.getNetworkWorkRequired(nextTip.blockHeader, chain)
|
||||||
|
assert(nextNBits == nextTip.nBits)
|
||||||
|
case None =>
|
||||||
|
fail(s"Chain not found best on header at height=$height")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Future
|
Future
|
||||||
|
@ -330,8 +330,8 @@ case class BlockHeaderDAO()(implicit
|
|||||||
ec: ExecutionContext): Future[Vector[Blockchain]] = {
|
ec: ExecutionContext): Future[Vector[Blockchain]] = {
|
||||||
val chainTipsF = chainTips
|
val chainTipsF = chainTips
|
||||||
chainTipsF.flatMap { tips =>
|
chainTipsF.flatMap { tips =>
|
||||||
val nestedFuture: Vector[Future[Vector[Blockchain]]] = tips.map { tip =>
|
val nestedFuture: Vector[Future[Option[Blockchain]]] = tips.map { tip =>
|
||||||
getBlockchainsFrom(tip)
|
getBlockchainFrom(tip)
|
||||||
}
|
}
|
||||||
Future.sequence(nestedFuture).map(_.flatten)
|
Future.sequence(nestedFuture).map(_.flatten)
|
||||||
}
|
}
|
||||||
@ -339,20 +339,16 @@ case class BlockHeaderDAO()(implicit
|
|||||||
|
|
||||||
/** Retrieves a blockchain with the best tip being the given header */
|
/** Retrieves a blockchain with the best tip being the given header */
|
||||||
def getBlockchainFrom(header: BlockHeaderDb)(implicit
|
def getBlockchainFrom(header: BlockHeaderDb)(implicit
|
||||||
ec: ExecutionContext): Future[Blockchain] = {
|
ec: ExecutionContext): Future[Option[Blockchain]] = {
|
||||||
val diffInterval = appConfig.chain.difficultyChangeInterval
|
val diffInterval = appConfig.chain.difficultyChangeInterval
|
||||||
val height = Math.max(0, header.height - diffInterval)
|
val height = Math.max(0, header.height - diffInterval)
|
||||||
val headersF = getBetweenHeights(from = height, to = header.height)
|
val blockchainsF =
|
||||||
headersF.map(headers =>
|
getBlockchainsBetweenHeights(from = height, to = header.height)
|
||||||
Blockchain.fromHeaders(headers.sortBy(_.height)(Ordering.Int.reverse)))
|
|
||||||
}
|
|
||||||
|
|
||||||
def getBlockchainsFrom(header: BlockHeaderDb)(implicit
|
for {
|
||||||
ec: ExecutionContext): Future[Vector[Blockchain]] = {
|
blockchains <- blockchainsF
|
||||||
val diffInterval = appConfig.chain.difficultyChangeInterval
|
blockchainOpt = blockchains.find(_.tip == header)
|
||||||
val height = Math.max(0, header.height - diffInterval)
|
} yield blockchainOpt
|
||||||
|
|
||||||
getBlockchainsBetweenHeights(from = height, to = header.height)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@tailrec
|
@tailrec
|
||||||
|
Loading…
Reference in New Issue
Block a user