Skip to content

Commit

Permalink
Merge pull request #839 from MrStahlfelge/fix-838
Browse files Browse the repository at this point in the history
ErgoBoxCandidate.tokens does not account for same token multiple times in array
  • Loading branch information
aslesarenko authored Nov 7, 2022
2 parents 32a3554 + 0c2ba58 commit 5fe2c7f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 13 additions & 8 deletions sigmastate/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

/**
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
Expand All @@ -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)
Expand Down

0 comments on commit 5fe2c7f

Please sign in to comment.