diff --git a/sigmastate/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala b/sigmastate/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala index bf13b8d9c8..9fa6cafaf0 100644 --- a/sigmastate/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala +++ b/sigmastate/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala @@ -3,7 +3,7 @@ package org.ergoplatform import java.util import org.ergoplatform.ErgoBox._ import org.ergoplatform.settings.ErgoAlgos -import scorex.util.{bytesToId, ModifierId} +import scorex.util.{ModifierId, bytesToId} import sigmastate.Values._ import sigmastate._ import sigmastate.SType.AnyOps @@ -16,7 +16,7 @@ import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer import sigmastate.util.safeNewArray import spire.syntax.all.cfor -import scala.collection.immutable +import scala.collection.{immutable, mutable} import scala.runtime.ScalaRunTime /** @@ -100,12 +100,17 @@ class ErgoBoxCandidate(val value: Long, s"tokens: (${additionalTokens.map(t => ErgoAlgos.encode(t._1) + ":" + t._2).toArray.mkString(", ")}), " + s"$additionalRegisters, creationHeight: $creationHeight)" - /** Additional tokens stored in the box. */ - lazy val tokens: Map[ModifierId, Long] = - additionalTokens - .toArray - .map(t => bytesToId(t._1) -> t._2) - .toMap + /** Additional tokens stored in the box, merged into a Map. + * This method is not used in ErgoTree and serialization, not part of consensus. + */ + lazy val tokens: Map[ModifierId, Long] = { + val merged = new mutable.HashMap[ModifierId, Long] + additionalTokens.foreach { case (id, amount) => + val mId = bytesToId(id) + merged.put(mId, java7.compat.Math.addExact(merged.getOrElse(mId, 0L), amount)) + } + merged.toMap + } } object ErgoBoxCandidate { diff --git a/sigmastate/src/test/scala/org/ergoplatform/ErgoLikeTransactionSpec.scala b/sigmastate/src/test/scala/org/ergoplatform/ErgoLikeTransactionSpec.scala index ab27179bec..5de774def8 100644 --- a/sigmastate/src/test/scala/org/ergoplatform/ErgoLikeTransactionSpec.scala +++ b/sigmastate/src/test/scala/org/ergoplatform/ErgoLikeTransactionSpec.scala @@ -46,6 +46,7 @@ class ErgoLikeTransactionSpec extends SigmaDslTesting { 100, Coll( Digest32 @@ (ErgoAlgos.decodeUnsafe(token1)) -> 10000000L, + Digest32 @@ (ErgoAlgos.decodeUnsafe(token1)) -> 500L, Digest32 @@ (ErgoAlgos.decodeUnsafe(token2)) -> 500L ) ) @@ -70,6 +71,7 @@ class ErgoLikeTransactionSpec extends SigmaDslTesting { b1.tokens shouldBe expectedTokens b1_clone.tokens shouldBe expectedTokens b3.tokens shouldBe expectedTokens + b2.tokens shouldBe Map[ModifierId, Long](ModifierId @@ token1 -> 10000500L, ModifierId @@ token2 -> 500L) assertResult(true)(b1.hashCode() == b1.hashCode()) assertResult(true)(b1 == b1)