From d98325ea0a5707ccfe896d552404b59b0b8e4f6f Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Mon, 25 Sep 2023 16:43:05 +0200 Subject: [PATCH] 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