Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract classes for BlockchainParameters and BlockchainStateContext #922

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions sdk/js/src/test/scala/org/ergoplatform/sdk/js/IsosSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -27,7 +41,7 @@ case class BlockchainParameters(
softForkStartingHeight: Option[Int],
softForkVotesCollected: Option[Int],
blockVersion: Byte
)
) extends BlockchainParameters

/** Global parameters used by SDK */
object BlockchainParameters {
Expand Down
25 changes: 8 additions & 17 deletions sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -369,37 +369,33 @@ 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,
"outputs" -> tx.outputCandidates.asJson
"outputs" -> tx.outputs.asJson
)
})

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)
})

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,
"outputs" -> tx.outputCandidates.asJson
"outputs" -> tx.outputs.asJson
)
})

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)
Expand All @@ -411,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
/** Blockchain context used in tx signing. */
abstract class BlockchainStateContext {
/** Fixed number (10 in Ergo) of last block headers. */
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: special.sigma.PreHeader
}

/** Blockchain context used in tx signing. */
case class CBlockchainStateContext(
sigmaLastHeaders: Coll[special.sigma.Header],
previousStateDigest: Coll[Byte],
sigmaPreHeader: special.sigma.PreHeader
)
) extends BlockchainStateContext
Loading