Skip to content

Commit d633ab8

Browse files
committed
etcm-546 fixed unsafe usage of maxBy which could throw
1 parent 2beeac1 commit d633ab8

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/main/scala/io/iohk/ethereum/ledger/BlockQueue.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,18 @@ class BlockQueue(blockchain: Blockchain, val maxQueuedBlockNumberAhead: Int, val
144144
* @return Best leaf from the affected subtree
145145
*/
146146
private def updateChainWeights(ancestor: ByteString): Option[Leaf] = {
147-
blocks.get(ancestor).flatMap(_.weight).map { weight =>
147+
blocks.get(ancestor).flatMap(_.weight).flatMap { weight =>
148148
parentToChildren.get(ancestor) match {
149149

150150
case Some(children) if children.nonEmpty =>
151151
val updatedChildren = children
152152
.flatMap(blocks.get)
153153
.map(qb => qb.copy(weight = Some(weight.increase(qb.block.header))))
154154
updatedChildren.foreach(qb => blocks += qb.block.header.hash -> qb)
155-
updatedChildren.flatMap(qb => updateChainWeights(qb.block.header.hash)).maxBy(_.weight)
155+
updatedChildren.flatMap(qb => updateChainWeights(qb.block.header.hash)).maxByOption(_.weight)
156156

157157
case _ =>
158-
Leaf(ancestor, weight)
158+
Some(Leaf(ancestor, weight))
159159
}
160160
}
161161
}

0 commit comments

Comments
 (0)