From e79fd44e2f83c404219e164d25dbee24d9c36160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20=C5=9Al=C4=85ski?= Date: Thu, 28 Jan 2021 14:00:50 +0100 Subject: [PATCH] etcm-546 fixed unsafe usage of maxBy which could throw --- src/main/scala/io/iohk/ethereum/ledger/BlockQueue.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/scala/io/iohk/ethereum/ledger/BlockQueue.scala b/src/main/scala/io/iohk/ethereum/ledger/BlockQueue.scala index 28ace795e2..e9f45c72d1 100644 --- a/src/main/scala/io/iohk/ethereum/ledger/BlockQueue.scala +++ b/src/main/scala/io/iohk/ethereum/ledger/BlockQueue.scala @@ -144,7 +144,7 @@ class BlockQueue(blockchain: Blockchain, val maxQueuedBlockNumberAhead: Int, val * @return Best leaf from the affected subtree */ private def updateChainWeights(ancestor: ByteString): Option[Leaf] = { - blocks.get(ancestor).flatMap(_.weight).map { weight => + blocks.get(ancestor).flatMap(_.weight).flatMap { weight => parentToChildren.get(ancestor) match { case Some(children) if children.nonEmpty => @@ -152,10 +152,10 @@ class BlockQueue(blockchain: Blockchain, val maxQueuedBlockNumberAhead: Int, val .flatMap(blocks.get) .map(qb => qb.copy(weight = Some(weight.increase(qb.block.header)))) updatedChildren.foreach(qb => blocks += qb.block.header.hash -> qb) - updatedChildren.flatMap(qb => updateChainWeights(qb.block.header.hash)).maxBy(_.weight) + updatedChildren.flatMap(qb => updateChainWeights(qb.block.header.hash)).maxByOption(_.weight) case _ => - Leaf(ancestor, weight) + Some(Leaf(ancestor, weight)) } } }