From e5e6554581975b23d35667e082d5c5a3d288eafb Mon Sep 17 00:00:00 2001 From: Benjamin Schulte Date: Thu, 3 Nov 2022 10:12:53 +0100 Subject: [PATCH 1/2] Fix ErgoBoxCandidate.tokens does not account for same token multiple times in array, closes #838 --- .../org/ergoplatform/ErgoBoxCandidate.scala | 21 ++++++++++++------- .../ErgoLikeTransactionSpec.scala | 2 ++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/sigmastate/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala b/sigmastate/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala index bf13b8d9c8..003bfbe977 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 */ + 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) From 0c2ba58b3d30ac168418d1f5eeda4c898e003d9b Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Fri, 4 Nov 2022 09:52:47 +0100 Subject: [PATCH 2/2] fix-838: comments on consensus + formatting --- .../scala/org/ergoplatform/ErgoBoxCandidate.scala | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sigmastate/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala b/sigmastate/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala index 003bfbe977..9fa6cafaf0 100644 --- a/sigmastate/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala +++ b/sigmastate/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala @@ -100,14 +100,14 @@ 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, merged into a Map */ + /** 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)) - } + additionalTokens.foreach { case (id, amount) => + val mId = bytesToId(id) + merged.put(mId, java7.compat.Math.addExact(merged.getOrElse(mId, 0L), amount)) } merged.toMap }