From d98325ea0a5707ccfe896d552404b59b0b8e4f6f Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Mon, 25 Sep 2023 16:43:05 +0200 Subject: [PATCH 1/5] ergotree-version: abstract classes for BlockchainParameters and BlockchainStateContext --- .../scala/org/ergoplatform/sdk/js/Isos.scala | 4 +- .../org/ergoplatform/sdk/js/IsosSpec.scala | 4 +- .../sdk/BlockchainParameters.scala | 44 ++++++++++++------- .../context/BlockchainStateContext.scala | 25 ++++++----- 4 files changed, 48 insertions(+), 29 deletions(-) diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala index a317e3ab29..56df9cd2c9 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala @@ -189,7 +189,7 @@ object Isos { val isoBlockchainParameters: Iso[BlockchainParameters, sdk.BlockchainParameters] = new Iso[BlockchainParameters, sdk.BlockchainParameters] { override def to(a: BlockchainParameters): sdk.BlockchainParameters = { - sdk.BlockchainParameters( + sdk.CBlockchainParameters( storageFeeFactor = a.storageFeeFactor, minValuePerByte = a.minValuePerByte, maxBlockSize = a.maxBlockSize, @@ -222,7 +222,7 @@ object Isos { implicit val isoBlockchainStateContext: Iso[BlockchainStateContext, context.BlockchainStateContext] = new Iso[BlockchainStateContext, context.BlockchainStateContext] { override def to(a: BlockchainStateContext): context.BlockchainStateContext = { - context.BlockchainStateContext( + context.CBlockchainStateContext( sigmaLastHeaders = isoArrayToColl(isoHeader).to(a.sigmaLastHeaders), previousStateDigest = isoStringToColl.to(a.previousStateDigest), sigmaPreHeader = isoPreHeader.to(a.sigmaPreHeader) diff --git a/sdk/js/src/test/scala/org/ergoplatform/sdk/js/IsosSpec.scala b/sdk/js/src/test/scala/org/ergoplatform/sdk/js/IsosSpec.scala index 47eedf2706..3e27513f02 100644 --- a/sdk/js/src/test/scala/org/ergoplatform/sdk/js/IsosSpec.scala +++ b/sdk/js/src/test/scala/org/ergoplatform/sdk/js/IsosSpec.scala @@ -2,7 +2,7 @@ package org.ergoplatform.sdk.js import org.ergoplatform.ErgoBox.{AdditionalRegisters, BoxId, TokenId} import org.ergoplatform._ -import org.ergoplatform.sdk.wallet.protocol.context.BlockchainStateContext +import org.ergoplatform.sdk.wallet.protocol.context.{BlockchainStateContext, CBlockchainStateContext} import org.ergoplatform.sdk.{ExtendedInputBox, Iso} import org.scalacheck.{Arbitrary, Gen} import org.scalatest.matchers.should.Matchers @@ -30,7 +30,7 @@ class IsosSpec extends AnyPropSpec with Matchers with ObjectGenerators with Sca stateRoot <- avlTreeGen headers <- headersGen(stateRoot) preHeader <- preHeaderGen(headers.headOption.map(_.id).getOrElse(modifierIdBytesGen.sample.get)) - } yield BlockchainStateContext( + } yield CBlockchainStateContext( sigmaLastHeaders = Colls.fromItems(headers:_*), previousStateDigest = stateRoot.digest, sigmaPreHeader = preHeader diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/BlockchainParameters.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/BlockchainParameters.scala index 4a1014b112..6f799ff8d8 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/BlockchainParameters.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/BlockchainParameters.scala @@ -1,21 +1,35 @@ package org.ergoplatform.sdk /** Blockchain parameters re-adjustable via miners voting and voting-related data. - * All these fields are included into extension section of a first block of a voting epoch. - * - * @param storageFeeFactor cost of storing 1 byte in UTXO for four years, in nanoErgs - * @param minValuePerByte cost of a transaction output, in computation unit - * @param maxBlockSize max block size, in bytes - * @param tokenAccessCost cost of a token contained in a transaction, in computation unit - * @param inputCost cost of a transaction input, in computation unit - * @param dataInputCost cost of a transaction data input, in computation unit - * @param outputCost cost of a transaction output, in computation unit - * @param maxBlockCost computation units limit per block - * @param softForkStartingHeight height when voting for a soft-fork had been started - * @param softForkVotesCollected votes for soft-fork collected in previous epochs - * @param blockVersion Protocol version activated on the network + * All these parameters are included into extension section of a first block of a voting epoch. */ -case class BlockchainParameters( +abstract class BlockchainParameters { + /** Cost of storing 1 byte in UTXO for four years, in nanoErgs. */ + def storageFeeFactor: Int + /** Cost of a transaction output, in computation unit. */ + def minValuePerByte: Int + /** Max block size, in bytes. */ + def maxBlockSize: Int + /** Cost of a token contained in a transaction, in computation unit. */ + def tokenAccessCost: Int + /** Cost of a transaction input, in computation unit. */ + def inputCost: Int + /** Cost of a transaction data input, in computation unit. */ + def dataInputCost: Int + /** Cost of a transaction output, in computation unit. */ + def outputCost: Int + /** Computation units limit per block. */ + def maxBlockCost: Int + /** Height when voting for a soft-fork had been started. */ + def softForkStartingHeight: Option[Int] + /** Votes for soft-fork collected in previous epochs. */ + def softForkVotesCollected: Option[Int] + /** Protocol version activated on the network. */ + def blockVersion: Byte +} + +/** Concete implementation of blockchain parameters. */ +case class CBlockchainParameters( storageFeeFactor: Int, minValuePerByte: Int, maxBlockSize: Int, @@ -27,7 +41,7 @@ case class BlockchainParameters( softForkStartingHeight: Option[Int], softForkVotesCollected: Option[Int], blockVersion: Byte -) +) extends BlockchainParameters /** Global parameters used by SDK */ object BlockchainParameters { diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/protocol/context/BlockchainStateContext.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/protocol/context/BlockchainStateContext.scala index 5c8d97df4f..e2fc88eaf9 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/protocol/context/BlockchainStateContext.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/protocol/context/BlockchainStateContext.scala @@ -2,14 +2,19 @@ package org.ergoplatform.sdk.wallet.protocol.context import special.collection.Coll -/** Blockchain context used in tx signing. - * - * @param sigmaLastHeaders fixed number (10 in Ergo) of last block headers - * @param previousStateDigest UTXO set digest from a last header (of sigmaLastHeaders) - * @param sigmaPreHeader returns pre-header (header without certain fields) of the current block - */ -case class BlockchainStateContext( - sigmaLastHeaders: Coll[special.sigma.Header], +/** Blockchain context used in tx signing. */ +abstract class BlockchainStateContext { + /** Fixed number (10 in Ergo) of last block headers. */ + def sigmaLastHeaders: Coll[sigma.Header] + /** UTXO set digest from a last header (of sigmaLastHeaders). */ + def previousStateDigest: Coll[Byte] + /** Returns pre-header (header without certain fields) of the current block. */ + def sigmaPreHeader: sigma.PreHeader +} + +/** Blockchain context used in tx signing. */ +case class CBlockchainStateContext( + sigmaLastHeaders: Coll[sigma.Header], previousStateDigest: Coll[Byte], - sigmaPreHeader: special.sigma.PreHeader -) + sigmaPreHeader: sigma.PreHeader +) extends BlockchainStateContext From dca61811bd8dc839b97178d9c862d65c210c3af2 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Tue, 26 Sep 2023 22:38:21 +0200 Subject: [PATCH 2/5] v5.0.11-fix1: abstract classes for BlockchainParameters and BlockchainStateContext --- .../wallet/protocol/context/BlockchainStateContext.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/protocol/context/BlockchainStateContext.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/protocol/context/BlockchainStateContext.scala index e2fc88eaf9..985a4acf8d 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/protocol/context/BlockchainStateContext.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/protocol/context/BlockchainStateContext.scala @@ -5,16 +5,16 @@ import special.collection.Coll /** Blockchain context used in tx signing. */ abstract class BlockchainStateContext { /** Fixed number (10 in Ergo) of last block headers. */ - def sigmaLastHeaders: Coll[sigma.Header] + def sigmaLastHeaders: Coll[special.sigma.Header] /** UTXO set digest from a last header (of sigmaLastHeaders). */ def previousStateDigest: Coll[Byte] /** Returns pre-header (header without certain fields) of the current block. */ - def sigmaPreHeader: sigma.PreHeader + def sigmaPreHeader: special.sigma.PreHeader } /** Blockchain context used in tx signing. */ case class CBlockchainStateContext( - sigmaLastHeaders: Coll[sigma.Header], + sigmaLastHeaders: Coll[special.sigma.Header], previousStateDigest: Coll[Byte], - sigmaPreHeader: sigma.PreHeader + sigmaPreHeader: special.sigma.PreHeader ) extends BlockchainStateContext From 770d297b53529f1cbd2a3a4074d4f3ddd0b74042 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Thu, 28 Sep 2023 11:55:37 +0200 Subject: [PATCH 3/5] v5.0.11-fix1: fix issue with tx decoders --- .../src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala index 52e1565ca3..0806e78098 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala @@ -379,8 +379,7 @@ trait JsonCodecs { implicit val ergoLikeTransactionDecoder: Decoder[ErgoLikeTransaction] = Decoder.instance({ implicit cursor => for { - t <- cursor.downField("type").as[String] - inputs <- {require(t == "ELT"); cursor.downField("inputs").as[IndexedSeq[Input]] } + inputs <- cursor.downField("inputs").as[IndexedSeq[Input]] dataInputs <- cursor.downField("dataInputs").as[IndexedSeq[DataInput]] outputs <- cursor.downField("outputs").as[IndexedSeq[ErgoBoxCandidate]] } yield new ErgoLikeTransaction(inputs, dataInputs, outputs) @@ -398,8 +397,7 @@ trait JsonCodecs { implicit val unsignedErgoLikeTransactionDecoder: Decoder[UnsignedErgoLikeTransaction] = Decoder.instance({ implicit cursor => for { - t <- cursor.downField("type").as[String] - inputs <- {require(t == "UELT"); cursor.downField("inputs").as[IndexedSeq[UnsignedInput]] } + inputs <- cursor.downField("inputs").as[IndexedSeq[UnsignedInput]] dataInputs <- cursor.downField("dataInputs").as[IndexedSeq[DataInput]] outputs <- cursor.downField("outputs").as[IndexedSeq[ErgoBoxCandidate]] } yield new UnsignedErgoLikeTransaction(inputs, dataInputs, outputs) From 7de88b03806c588d46487e0873ea9648cbbc0c65 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Thu, 28 Sep 2023 12:52:12 +0200 Subject: [PATCH 4/5] v5.0.11-fix1: rollback changes in decoders --- .../scala/org/ergoplatform/sdk/JsonCodecs.scala | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala index 0806e78098..d4a3c08ea6 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala @@ -369,7 +369,6 @@ trait JsonCodecs { implicit val ergoLikeTransactionEncoder: Encoder[ErgoLikeTransaction] = Encoder.instance({ tx => Json.obj( - "type" -> "ELT".asJson, // ErgoLikeTransaction "id" -> tx.id.asJson, "inputs" -> tx.inputs.asJson, "dataInputs" -> tx.dataInputs.asJson, @@ -387,7 +386,6 @@ trait JsonCodecs { implicit val unsignedErgoLikeTransactionEncoder: Encoder[UnsignedErgoLikeTransaction] = Encoder.instance({ tx => Json.obj( - "type" -> "UELT".asJson, // UnsignedErgoLikeTransaction "id" -> tx.id.asJson, "inputs" -> tx.inputs.asJson, "dataInputs" -> tx.dataInputs.asJson, @@ -409,15 +407,10 @@ trait JsonCodecs { case t => throw new SigmaException(s"Don't know how to encode transaction $t") }) - implicit val ergoLikeTransactionTemplateDecoder: Decoder[ErgoLikeTransactionTemplate[_ <: UnsignedInput]] = Decoder.instance({ implicit cursor => - for { - t <- cursor.downField("type").as[String] - tx <- t match { - case "ELT" => ergoLikeTransactionDecoder(cursor) - case "UELT" => unsignedErgoLikeTransactionDecoder(cursor) - } - } yield tx - }) + implicit val ergoLikeTransactionTemplateDecoder: Decoder[ErgoLikeTransactionTemplate[_ <: UnsignedInput]] = { + ergoLikeTransactionDecoder.asInstanceOf[Decoder[ErgoLikeTransactionTemplate[_ <: UnsignedInput]]] or + unsignedErgoLikeTransactionDecoder.asInstanceOf[Decoder[ErgoLikeTransactionTemplate[_ <: UnsignedInput]]] + } implicit val sigmaValidationSettingsEncoder: Encoder[SigmaValidationSettings] = Encoder.instance({ v => SigmaValidationSettingsSerializer.toBytes(v).asJson From 4c5386420fb0bf014a8b228825bd6aa04c189dc3 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Thu, 28 Sep 2023 13:54:04 +0200 Subject: [PATCH 5/5] v5.0.11-fix1: rollback changes in decoders (2) --- .../src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala index d4a3c08ea6..008f71bb68 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala @@ -372,7 +372,7 @@ trait JsonCodecs { "id" -> tx.id.asJson, "inputs" -> tx.inputs.asJson, "dataInputs" -> tx.dataInputs.asJson, - "outputs" -> tx.outputCandidates.asJson + "outputs" -> tx.outputs.asJson ) }) @@ -389,7 +389,7 @@ trait JsonCodecs { "id" -> tx.id.asJson, "inputs" -> tx.inputs.asJson, "dataInputs" -> tx.dataInputs.asJson, - "outputs" -> tx.outputCandidates.asJson + "outputs" -> tx.outputs.asJson ) })