Skip to content

Commit

Permalink
Merge pull request #646 from ScorexFoundation/add-ergoboxassets-trait
Browse files Browse the repository at this point in the history
introduce ErgoBoxAssets
  • Loading branch information
aslesarenko authored Mar 28, 2020
2 parents abab425 + 2dcd9c8 commit 9ca6465
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ ergo-tests/

# spam test's checkout location
spam-tests/

# metals, bloop, vscode
.bloop/
.metals/
.vscode/
project/metals.sbt
17 changes: 17 additions & 0 deletions sigmastate/src/main/scala/org/ergoplatform/ErgoBoxAssets.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.ergoplatform

import scorex.util.ModifierId

trait ErgoBoxAssets {
def value: Long
def tokens: Map[ModifierId, Long]
}

final case class ErgoBoxAssetsHolder(
val value: Long,
val tokens: Map[ModifierId, Long]
) extends ErgoBoxAssets

object ErgoBoxAssetsHolder {
def apply(value: Long): ErgoBoxAssetsHolder = ErgoBoxAssetsHolder(value, Map())
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.util
import org.ergoplatform.ErgoBox._
import org.ergoplatform.settings.ErgoAlgos
import scorex.crypto.hash.Digest32
import scorex.util.ModifierId
import scorex.util.{bytesToId, idToBytes, ModifierId}
import sigmastate.Values._
import sigmastate._
import sigmastate.SType.AnyOps
Expand Down Expand Up @@ -39,7 +39,8 @@ class ErgoBoxCandidate(val value: Long,
val ergoTree: ErgoTree,
val creationHeight: Int,
val additionalTokens: Coll[(TokenId, Long)] = Colls.emptyColl,
val additionalRegisters: Map[NonMandatoryRegisterId, _ <: EvaluatedValue[_ <: SType]] = Map()) {
val additionalRegisters: Map[NonMandatoryRegisterId, _ <: EvaluatedValue[_ <: SType]] = Map())
extends ErgoBoxAssets {

def proposition: BoolValue = ergoTree.toProposition(ergoTree.isConstantSegregation).asBoolValue

Expand Down Expand Up @@ -77,6 +78,12 @@ class ErgoBoxCandidate(val value: Long,
override def toString: Idn = s"ErgoBoxCandidate($value, $ergoTree," +
s"tokens: (${additionalTokens.map(t => ErgoAlgos.encode(t._1) + ":" + t._2).toArray.mkString(", ")}), " +
s"$additionalRegisters, creationHeight: $creationHeight)"

lazy val tokens: Map[ModifierId, Long] =
additionalTokens
.toArray
.map(t => bytesToId(t._1) -> t._2)
.toMap
}

object ErgoBoxCandidate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ object ErgoLikeTransactionSerializer extends SigmaSerializer[ErgoLikeTransaction
for (input <- tx.dataInputs) {
w.putBytes(input.boxId)
}
// serialize distinct ids of tokens in transaction outputs
// Serialize distinct ids of tokens in transaction outputs.
// This optimization is crucial to allow up to MaxTokens (== 255) in a box.
// Without it total size of all token ids 255 * 32 = 8160,
// way beyond MaxBoxSize (== 4K)
val tokenIds = tx.outputCandidates.toColl
.flatMap(box => box.additionalTokens.map(t => t._1))

Expand Down

0 comments on commit 9ca6465

Please sign in to comment.