Skip to content

Commit

Permalink
Merge branch 'v5.0.2' of github.com:ergoplatform/ergo into v5.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Nov 1, 2022
2 parents bbcb430 + 061b786 commit 21834fd
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 26 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ val circeVersion = "0.13.0"
val akkaVersion = "2.6.10"
val akkaHttpVersion = "10.2.4"

val sigmaStateVersion = "5.0.0"
val sigmaStateVersion = "5.0.1"

// for testing current sigmastate build (see sigmastate-ergo-it jenkins job)
val effectiveSigmaStateVersion = Option(System.getenv().get("SIGMASTATE_VERSION")).getOrElse(sigmaStateVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ trait BoxSelector extends ScorexLogging {
*/
def reemissionAmount[T <: ErgoBoxAssets](boxes: Seq[T]): Long = {
reemissionDataOpt.map { reemissionData =>
boxes
.flatMap(_.tokens.get(reemissionData.reemissionTokenId))
.sum
boxes.foldLeft(0L) { case (sum, b) => sum + b.tokens.getOrElse(reemissionData.reemissionTokenId, 0L) }
}.getOrElse(0L)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class DefaultBoxSelector(override val reemissionDataOpt: Option[ReemissionData])
targetBoxAssets: TokensMap,
reemissionAmt: Long): Either[BoxSelectionError, Seq[ErgoBoxAssets]] = {
AssetUtils.subtractAssetsMut(foundBoxAssets, targetBoxAssets)
val changeBoxesAssets: Seq[mutable.Map[ModifierId, Long]] = foundBoxAssets.grouped(MaxAssetsPerBox).toSeq
val changeBoxesAssets: Seq[mutable.Map[ModifierId, Long]] = foundBoxAssets.grouped(MaxAssetsPerBox).toIndexedSeq
val changeBalance = foundBalance - targetBalance
//at least a minimum amount of ERG should be assigned per a created box
if (changeBoxesAssets.size * MinBoxValue > changeBalance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.ergoplatform.wallet.transactions.TransactionBuilder._

import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.mutable.ListBuffer

/**
* A box selector which is parameterized by maximum number of inputs a transaction can have, and optimal number of inputs.
Expand Down Expand Up @@ -84,7 +85,7 @@ class ReplaceCompactCollectBoxSelector(maxInputs: Int,
targetBalance: Long,
targetAssets: TokensMap
): Either[BoxSelectionError, Seq[ErgoBoxAssets]] = {
val compactedBalance = boxes.map(b => BoxSelector.valueOf(b, reemissionDataOpt)).sum
val compactedBalance = boxes.foldLeft(0L) { case (sum, b) => sum + BoxSelector.valueOf(b, reemissionDataOpt) }
val compactedAssets = mutable.Map[ModifierId, Long]()
AssetUtils.mergeAssetsMut(compactedAssets, boxes.map(_.tokens): _*)
val ra = reemissionAmount(boxes)
Expand All @@ -108,14 +109,16 @@ class ReplaceCompactCollectBoxSelector(maxInputs: Int,
targetBalance: Long,
targetAssets: TokensMap): Either[BoxSelectionError, BoxSelectionResult[T]] = {
val boxes = bsr.boxes
val diff = boxes.map(b => BoxSelector.valueOf(b,reemissionDataOpt)).sum - targetBalance
val diff = boxes.foldLeft(0L) { case (sum, b) => sum + BoxSelector.valueOf(b, reemissionDataOpt) } - targetBalance

val boxesToThrowAway = boxes.filter(!_.tokens.keySet.exists(tid => targetAssets.keySet.contains(tid)))
val sorted = boxesToThrowAway.sortBy(b => BoxSelector.valueOf(b, reemissionDataOpt))
val targetAssetsKeys = targetAssets.keySet
val sortedBoxesToThrowAway =
boxes.filter(!_.tokens.keySet.exists(tid => targetAssetsKeys.contains(tid)))
.sortBy(b => BoxSelector.valueOf(b, reemissionDataOpt))

if (diff >= BoxSelector.valueOf(sorted.head, reemissionDataOpt)) {
if (diff >= BoxSelector.valueOf(sortedBoxesToThrowAway.head, reemissionDataOpt)) {
var thrownValue = 0L
val thrownBoxes = sorted.takeWhile { b =>
val thrownBoxes = sortedBoxesToThrowAway.takeWhile { b =>
thrownValue = thrownValue + BoxSelector.valueOf(b, reemissionDataOpt)
thrownValue <= diff
}
Expand All @@ -135,31 +138,33 @@ class ReplaceCompactCollectBoxSelector(maxInputs: Int,
val boxesToThrowAway = bsr.boxes.filter(!_.tokens.keySet.exists(tid => targetAssets.keySet.contains(tid)))
val sorted = boxesToThrowAway.sortBy(b => BoxSelector.valueOf(b, reemissionDataOpt))

type BoxesToAdd = Seq[T]
type BoxesToDrop = Seq[T]
type Operations = (BoxesToAdd, BoxesToDrop)
val boxesToAdd = ListBuffer.empty[T]
val boxesToDrop = mutable.HashSet.empty[T]

@tailrec
def replaceStep(candidates: Seq[T], toDrop: Seq[T], currentOps: Operations): Operations = {
def replaceStep(candidates: Seq[T], toDrop: Seq[T]): Unit = {
candidates match {
case Seq() => currentOps
case Seq() => ()
case Seq(cand)
if BoxSelector.valueOf(cand, reemissionDataOpt) <=
toDrop.headOption.map(b => BoxSelector.valueOf(b, reemissionDataOpt)).getOrElse(Long.MaxValue) =>
currentOps
()
case Seq(cand, cs@_*) =>
var collected = 0L
val (dropped, remain) = toDrop.partition { b =>
val candValue = BoxSelector.valueOf(cand, reemissionDataOpt)
val (dropped, remain) = toDrop.span { b =>
collected = collected + BoxSelector.valueOf(b, reemissionDataOpt)
collected <= BoxSelector.valueOf(cand, reemissionDataOpt)
collected <= candValue
}
replaceStep(cs, remain, (currentOps._1 :+ cand, currentOps._2 ++ dropped))
boxesToAdd += cand
boxesToDrop ++= dropped
replaceStep(cs, remain)
}
}

val (toAdd, toDrop) = replaceStep(bigBoxes, sorted, (Seq(), Seq()))
if (toAdd.nonEmpty) {
val compactedBoxes = bsr.boxes.filter(b => !toDrop.contains(b)) ++ toAdd
replaceStep(bigBoxes, sorted)
if (boxesToAdd.nonEmpty) {
val compactedBoxes = bsr.boxes.filter(b => !boxesToDrop.contains(b)) ++ boxesToAdd
calcChange(compactedBoxes, targetBalance, targetAssets)
.mapRight(changeBoxes => BoxSelectionResult(compactedBoxes, changeBoxes))
} else {
Expand All @@ -170,5 +175,5 @@ class ReplaceCompactCollectBoxSelector(maxInputs: Int,
}

object ReplaceCompactCollectBoxSelector {
final case class MaxInputsExceededError(message: String) extends BoxSelectionError
final case class MaxInputsExceededError(message: String) extends BoxSelectionError
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ trait ErgoWalletSupport extends ScorexLogging {
}
val inputBoxes = selectionResult.boxes.toIndexedSeq
new UnsignedErgoTransaction(
inputBoxes.map(_.box.id).map(id => new UnsignedInput(id)),
inputBoxes.map(tx => new UnsignedInput(tx.box.id)),
dataInputs,
(payTo ++ changeBoxCandidates).toIndexedSeq
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ object ApiRejectionHandler {
}
.handle { case ValidationRejection(msg, _) => ApiError.BadRequest(msg) }
.handle { case x => ApiError.InternalError(s"Unhandled rejection: $x") }
.handleNotFound { ApiError.NotExists("The requested resource could not be found.") }
.handleNotFound { ApiError.BadRequest("The requested resource/endpoint could not be found.") }
.result()
}

0 comments on commit 21834fd

Please sign in to comment.