Skip to content

Commit

Permalink
Merge pull request #1840 from ergoplatform/flatmap
Browse files Browse the repository at this point in the history
Performance optimizations in checking tree operations
  • Loading branch information
kushti authored Sep 21, 2022
2 parents 2dc1bbd + 8c862ca commit e54d86c
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/main/scala/org/ergoplatform/nodeView/state/UtxoState.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.ergoplatform.nodeView.state

import java.io.File
import cats.Traverse
import org.ergoplatform.ErgoBox
import org.ergoplatform.ErgoLikeContext.Height
import org.ergoplatform.modifiers.history.header.Header
Expand Down Expand Up @@ -76,7 +75,6 @@ class UtxoState(override val persistentProver: PersistentBatchAVLProver[Digest32
headerId: ModifierId,
expectedDigest: ADDigest,
currentStateContext: ErgoStateContext): Try[Unit] = {
import cats.implicits._
val createdOutputs = transactions.flatMap(_.outputs).map(o => (ByteArrayWrapper(o.id), o)).toMap

def checkBoxExistence(id: ErgoBox.BoxId): Try[ErgoBox] = createdOutputs
Expand All @@ -87,13 +85,23 @@ class UtxoState(override val persistentProver: PersistentBatchAVLProver[Digest32
val txProcessing = ErgoState.execTransactions(transactions, currentStateContext)(checkBoxExistence)
if (txProcessing.isValid) {
log.debug(s"Cost of block $headerId (${currentStateContext.currentHeight}): ${txProcessing.payload.getOrElse(0)}")
val resultTry =
ErgoState.stateChanges(transactions).map { stateChanges =>
val mods = stateChanges.operations
Traverse[List].sequence(mods.map(persistentProver.performOneOperation).toList).map(_ => ())
val blockOpsTry = ErgoState.stateChanges(transactions).flatMap { stateChanges =>
val operations = stateChanges.operations
var opsResult: Try[Unit] = Success(())
operations.foreach { op =>
if (opsResult.isSuccess) {
persistentProver.performOneOperation(op) match {
case Success(_) =>
case Failure(t) =>
log.error(s"Operation $op failed during $headerId transactions validation")
opsResult = Failure(t)
}
}
}
opsResult
}
ModifierValidator(stateContext.validationSettings)
.validateNoFailure(fbOperationFailed, resultTry, Transaction.ModifierTypeId)
.validateNoFailure(fbOperationFailed, blockOpsTry, Transaction.ModifierTypeId)
.validateEquals(fbDigestIncorrect, expectedDigest, persistentProver.digest, headerId, Header.modifierTypeId)
.result
.toTry
Expand Down

0 comments on commit e54d86c

Please sign in to comment.