Fix max by for getBlockCount (#1951)

* Fix max by for getBlockCount

* Add helper func to package
This commit is contained in:
Ben Carman 2020-09-01 19:38:15 -05:00 committed by GitHub
parent cc2a118ff0
commit 1670902e75
2 changed files with 23 additions and 1 deletions

View File

@ -260,7 +260,9 @@ case class BlockHeaderDAO()(implicit
/** Returns the block height of the block with the most work from our database */
def bestHeight: Future[Int] = {
chainTips.map(_.maxBy(_.chainWork).height)
chainTips.map { tips =>
tips.maxByOption(_.chainWork).map(_.height).getOrElse(0)
}
}
private val maxWorkQuery: profile.ProfileAction[

View File

@ -8,9 +8,29 @@ import org.bitcoins.core.wallet.fee.SatoshisPerKiloByte
import scodec.bits._
import scala.annotation.tailrec
import scala.math.Ordering
package object core {
implicit class seqUtil[T](private val seq: Seq[T]) extends AnyVal {
def maxByOption[B](f: T => B)(implicit cmp: Ordering[B]): Option[T] = {
if (seq.isEmpty) {
None
} else {
Some(seq.maxBy(f))
}
}
def minByOption[B](f: T => B)(implicit cmp: Ordering[B]): Option[T] = {
if (seq.isEmpty) {
None
} else {
Some(seq.minBy(f))
}
}
}
implicit val satoshisPerKiloByteOrdering: Ordering[SatoshisPerKiloByte] =
new Ordering[SatoshisPerKiloByte] {