Skip to content

Commit

Permalink
Merge pull request #622 from ScorexFoundation/serializer-opt
Browse files Browse the repository at this point in the history
Optimization of serialization
  • Loading branch information
aslesarenko authored Jan 17, 2020
2 parents 911a1a2 + 829f5da commit abab425
Show file tree
Hide file tree
Showing 84 changed files with 604 additions and 428 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ lazy val rootSettings = Seq(
)

def runErgoTask(task: String, sigmastateVersion: String, log: Logger): Unit = {
val ergoBranch = "sigma-core-opt"
val ergoBranch = "master"
val sbtEnvVars = Seq("BUILD_ENV" -> "test", "SIGMASTATE_VERSION" -> sigmastateVersion)

log.info(s"Testing current build in Ergo (branch $ergoBranch):")
Expand Down Expand Up @@ -353,7 +353,7 @@ commands += Command.command("ergoItTest") { state =>
}

def runSpamTestTask(task: String, sigmastateVersion: String, log: Logger): Unit = {
val spamBranch = "master"
val spamBranch = "revert-23-revert-22-serialize-opt"
val envVars = Seq("SIGMASTATE_VERSION" -> sigmastateVersion,
"SPECIAL_VERSION" -> specialVersion,
// SSH_SPAM_REPO_KEY should be set (see Jenkins Credentials Binding Plugin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class P2PKAddress(val pubkey: ProveDlog,
override val script: ErgoTree = {
// NOTE: we don't segregate constants because the whole tree is single constant
// and we want different addreses of this type to have different `script` values
ErgoTree(ErgoTree.DefaultHeader, IndexedSeq.empty, SigmaPropConstant(pubkey))
ErgoTree(ErgoTree.DefaultHeader, ErgoTree.EmptyConstants, SigmaPropConstant(pubkey))
}

override def equals(obj: Any): Boolean = obj match {
Expand Down
15 changes: 12 additions & 3 deletions sigmastate/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class ErgoBoxCandidate(val value: Long,

object ErgoBoxCandidate {
val UndefinedBoxRef: Coll[Byte] = Array.fill(34)(0: Byte).toColl

/** @hotspot don't beautify the code */
object serializer extends SigmaSerializer[ErgoBoxCandidate, ErgoBoxCandidate] {

def serializeBodyWithIndexedDigests(obj: ErgoBoxCandidate,
Expand All @@ -90,7 +92,14 @@ object ErgoBoxCandidate {
w.putBytes(DefaultSerializer.serializeErgoTree(obj.ergoTree))
w.putUInt(obj.creationHeight)
w.putUByte(obj.additionalTokens.size)
obj.additionalTokens.foreach { case (id, amount) =>

val unzipped = Colls.unzip(obj.additionalTokens)
val ids = unzipped._1
val amounts = unzipped._2
val limit = ids.length
cfor(0)(_ < limit, _ + 1) { i =>
val id = ids(i)
val amount = amounts(i)
if (tokensInTx.isDefined) {
val tokenIndex = tokensInTx.get.indexWhere(v => util.Arrays.equals(v, id), 0)
if (tokenIndex == -1) sys.error(s"failed to find token id ($id) in tx's digest index")
Expand All @@ -100,6 +109,7 @@ object ErgoBoxCandidate {
}
w.putULong(amount)
}

val nRegs = obj.additionalRegisters.keys.size
if (nRegs + ErgoBox.startingNonMandatoryIndex > 255)
sys.error(s"The number of non-mandatory indexes $nRegs exceeds ${255 - ErgoBox.startingNonMandatoryIndex} limit.")
Expand All @@ -108,7 +118,7 @@ object ErgoBoxCandidate {
// this convention allows to save 1 bite for each register
val startReg = ErgoBox.startingNonMandatoryIndex
val endReg = ErgoBox.startingNonMandatoryIndex + nRegs - 1
for (regId <- startReg to endReg) {
cfor(startReg: Int)(_ <= endReg, _ + 1) { regId =>
val reg = ErgoBox.findRegisterByIndex(regId.toByte).get
obj.get(reg) match {
case Some(v) =>
Expand All @@ -124,7 +134,6 @@ object ErgoBoxCandidate {
serializeBodyWithIndexedDigests(obj, None, w)
}

/** @hotspot don't beautify the code */
def parseBodyWithIndexedDigests(digestsInTx: Option[Coll[TokenId]], r: SigmaByteReader): ErgoBoxCandidate = {
val previousPositionLimit = r.positionLimit
r.positionLimit = r.position + ErgoBox.MaxBoxSize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ object ErgoScriptPredef {
// first segregated constant is delta, so key is second constant
val positions = IntArrayConstant(Array[Int](1))
val minerPubkeySigmaProp = CreateProveDlog(DecodePoint(minerPkBytesVal))
val newVals = Values.ConcreteCollection(Vector[SigmaPropValue](minerPubkeySigmaProp), SSigmaProp)
val newVals = Values.ConcreteCollection(Array[SigmaPropValue](minerPubkeySigmaProp), SSigmaProp)
SubstConstants(genericMinerPropBytes, positions, newVals)
}

Expand Down
Loading

0 comments on commit abab425

Please sign in to comment.