Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v5.0.11-RC' into todo-v5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
aslesarenko committed Aug 28, 2023
2 parents 49104aa + 98c6f06 commit 6f322a1
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ trait Header {
/** Hash of ADProofs for transactions in a block */
def ADProofsRoot: Coll[Byte] // Digest32. Can we build AvlTree out of it?

/** AvlTree) of a state after block application */
/** AvlTree of a state after block application */
def stateRoot: AvlTree

/** Root hash (for a Merkle tree) of transactions in a block. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,10 @@ sealed class MapSigmaValidationSettings(private val map: Map[Short, (ValidationR
}

override def equals(obj: Any): Boolean = (this eq obj.asInstanceOf[AnyRef]) || (obj match {
case that: MapSigmaValidationSettings =>
map.size == that.map.size &&
map.iterator.forall { case (id, v) => that.map.get(id).exists(_ == v) }
case that: MapSigmaValidationSettings => map == that.map
case _ => false
})

override def hashCode(): Int = map.toIndexedSeq.sortBy(_._1).hashCode()
override def hashCode(): Int = map.hashCode()
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,37 @@ import org.ergoplatform.sdk

import scala.scalajs.js.UndefOr
import scala.scalajs.js.annotation.JSExportTopLevel
import org.ergoplatform.sdk.Iso._

import scala.scalajs.js

/** JS exported version of the [[sdk.BlockchainParameters]] class with the same fields.
* 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
* @see sdk.BlockchainParameters
*/
@JSExportTopLevel("BlockchainParameters")
class BlockchainParameters(
storageFeeFactor: Int,
minValuePerByte: Int,
maxBlockSize: Int,
tokenAccessCost: Int,
inputCost: Int,
dataInputCost: Int,
outputCost: Int,
maxBlockCost: Int,
_softForkStartingHeight: UndefOr[Int],
_softForkVotesCollected: UndefOr[Int],
blockVersion: Byte
) extends sdk.BlockchainParameters(
storageFeeFactor, minValuePerByte, maxBlockSize, tokenAccessCost, inputCost, dataInputCost,
outputCost, maxBlockCost,
Isos.isoUndefOr[Int, Int](identityIso).to(_softForkStartingHeight),
Isos.isoUndefOr[Int, Int](identityIso).to(_softForkVotesCollected), blockVersion
)
val storageFeeFactor: Int,
val minValuePerByte: Int,
val maxBlockSize: Int,
val tokenAccessCost: Int,
val inputCost: Int,
val dataInputCost: Int,
val outputCost: Int,
val maxBlockCost: Int,
val softForkStartingHeight: UndefOr[Int],
val softForkVotesCollected: UndefOr[Int],
val blockVersion: Byte
) extends js.Object
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package org.ergoplatform.sdk.js
import scala.scalajs.js
import scala.scalajs.js.annotation.JSExportTopLevel

/** Equivalent of [[org.ergoplatform.sdk.wallet.protocol.context.BlockchainStateContext]] available from JS. */
/** Equivalent of [[org.ergoplatform.sdk.wallet.protocol.context.BlockchainStateContext]] available from JS.
* @param sigmaLastHeaders fixed number (10 in Ergo) of last block headers
* @param previousStateDigest hex of UTXO set digest from a last header (of sigmaLastHeaders)
* @param sigmaPreHeader returns pre-header (header without certain fields) of the current block
*/
@JSExportTopLevel("BlockchainStateContext")
class BlockchainStateContext(
val sigmaLastHeaders: js.Array[Header],
val previousStateDigest: String,
val sigmaPreHeader: PreHeader
)
) extends js.Object
32 changes: 29 additions & 3 deletions sdk/js/src/main/scala/org/ergoplatform/sdk/js/Header.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,48 @@ package org.ergoplatform.sdk.js
import scala.scalajs.js
import scala.scalajs.js.annotation.JSExportTopLevel

/** Equivalent of [[special.sigma.Header]] available from JS. */
/** Equivalent of [[special.sigma.Header]] available from JS.
* Represents data of the block header available in Sigma propositions.
*/
@JSExportTopLevel("Header")
class Header(
/** Hex representation of ModifierId of this Header */
val id: String,
/** Block version, to be increased on every soft and hardfork. */
val version: Byte,
/** Hex representation of ModifierId of the parent block */
val parentId: String,
/** Hex hash of ADProofs for transactions in a block */
val ADProofsRoot: String,
/** AvlTree of a state after block application */
val stateRoot: AvlTree,
/** Hex of root hash (for a Merkle tree) of transactions in a block. */
val transactionsRoot: String,
/** Block timestamp (in milliseconds since beginning of Unix Epoch) */
val timestamp: js.BigInt,
/** Current difficulty in a compressed view.
* NOTE: actually it is unsigned Int */
val nBits: js.BigInt,
/** Block height */
val height: Int,
/** Hex of root hash of extension section */
val extensionRoot: String,
val minerPk: String,
val powOnetimePk: String,

/** Miner public key (hex of EC Point). Should be used to collect block rewards.
* Part of Autolykos solution.
*/
val minerPk: GroupElement,

/** One-time public key (hex of EC Point). Prevents revealing of miners secret. */
val powOnetimePk: GroupElement,

/** Hex of nonce bytes */
val powNonce: String,

/** Distance between pseudo-random number, corresponding to nonce `powNonce` and a secret,
* corresponding to `minerPk`. The lower `powDistance` is, the harder it was to find this solution. */
val powDistance: js.BigInt,

/** Miner votes for changing system parameters. */
val votes: String
) extends js.Object
71 changes: 56 additions & 15 deletions sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.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._
import org.ergoplatform.sdk.JavaHelpers.UniversalConverter
import org.ergoplatform.sdk.{ExtendedInputBox, Iso}
import org.ergoplatform.sdk.{Iso, ExtendedInputBox}
import org.ergoplatform.sdk.wallet.protocol.context
import org.ergoplatform._
import scalan.RType
Expand All @@ -11,20 +11,19 @@ import scorex.util.ModifierId
import scorex.util.encode.Base16
import sigmastate.Values.{Constant, GroupElementConstant}
import sigmastate.eval.Extensions.ArrayOps
import sigmastate.eval.{CAvlTree, CBigInt, CHeader, CPreHeader, Colls, Digest32Coll, Evaluation}
import sigmastate.eval.{CBigInt, Digest32Coll, Evaluation, CAvlTree, Colls, CGroupElement, CPreHeader, CHeader}
import sigmastate.fleetSdkCommon.distEsmTypesBoxesMod.Box
import sigmastate.fleetSdkCommon.distEsmTypesCommonMod.HexString
import sigmastate.fleetSdkCommon.distEsmTypesRegistersMod.NonMandatoryRegisters
import sigmastate.fleetSdkCommon.distEsmTypesTokenMod.TokenAmount
import sigmastate.fleetSdkCommon.distEsmTypesTransactionsMod.{SignedTransaction, UnsignedTransaction}
import sigmastate.fleetSdkCommon.{distEsmTypesBoxesMod => boxesMod, distEsmTypesCommonMod => commonMod, distEsmTypesContextExtensionMod => contextExtensionMod, distEsmTypesInputsMod => inputsMod, distEsmTypesProverResultMod => proverResultMod, distEsmTypesRegistersMod => registersMod, distEsmTypesTokenMod => tokenMod}
import sigmastate.fleetSdkCommon.distEsmTypesTransactionsMod.{UnsignedTransaction, SignedTransaction}
import sigmastate.fleetSdkCommon.{distEsmTypesProverResultMod => proverResultMod, distEsmTypesContextExtensionMod => contextExtensionMod, distEsmTypesInputsMod => inputsMod, distEsmTypesBoxesMod => boxesMod, distEsmTypesCommonMod => commonMod, distEsmTypesRegistersMod => registersMod, distEsmTypesTokenMod => tokenMod}
import sigmastate.interpreter.{ContextExtension, ProverResult}
import sigmastate.serialization.{ErgoTreeSerializer, ValueSerializer}
import sigmastate.{AvlTreeData, AvlTreeFlags, SType}
import sigmastate.{AvlTreeData, SType, AvlTreeFlags}
import special.collection.Coll
import special.collection.Extensions.CollBytesOps
import special.sigma
import special.sigma.GroupElement

import java.math.BigInteger
import scala.collection.immutable.ListMap
Expand Down Expand Up @@ -57,17 +56,26 @@ object Isos {
override def from(x: Coll[Byte]): String = x.toHex
}

val isoStringToGroupElement: Iso[String, GroupElement] = new Iso[String, GroupElement] {
override def to(x: String): GroupElement = {
val isoStringToGroupElement: Iso[String, sigma.GroupElement] = new Iso[String, sigma.GroupElement] {
override def to(x: String): sigma.GroupElement = {
val bytes = Base16.decode(x).get
ValueSerializer.deserialize(bytes).asInstanceOf[GroupElementConstant].value
}
override def from(x: GroupElement): String = {
override def from(x: sigma.GroupElement): String = {
val bytes = ValueSerializer.serialize(GroupElementConstant(x))
Base16.encode(bytes)
}
}

val isoGroupElement: Iso[GroupElement, special.sigma.GroupElement] = new Iso[GroupElement, special.sigma.GroupElement] {
override def to(x: GroupElement): sigma.GroupElement = {
CGroupElement(x.point)
}
override def from(x: sigma.GroupElement): GroupElement = {
new GroupElement(x.asInstanceOf[CGroupElement].wrappedValue)
}
}

implicit val isoBoxId: Iso[boxesMod.BoxId, ErgoBox.BoxId] = new Iso[boxesMod.BoxId, ErgoBox.BoxId] {
override def to(x: boxesMod.BoxId): ErgoBox.BoxId = ADKey @@@ isoStringToArray.to(x)

Expand Down Expand Up @@ -124,8 +132,8 @@ object Isos {
nBits = isoBigIntToLong.to(a.nBits),
height = a.height,
extensionRoot = isoStringToColl.to(a.extensionRoot),
minerPk = isoStringToGroupElement.to(a.minerPk),
powOnetimePk = isoStringToGroupElement.to(a.powOnetimePk),
minerPk = isoGroupElement.to(a.minerPk),
powOnetimePk = isoGroupElement.to(a.powOnetimePk),
powNonce = isoStringToColl.to(a.powNonce),
powDistance = isoBigInt.to(a.powDistance),
votes = isoStringToColl.to(a.votes)
Expand All @@ -144,8 +152,8 @@ object Isos {
nBits = isoBigIntToLong.from(header.nBits),
height = header.height,
extensionRoot = isoStringToColl.from(header.extensionRoot),
minerPk = isoStringToGroupElement.from(header.minerPk),
powOnetimePk = isoStringToGroupElement.from(header.powOnetimePk),
minerPk = isoGroupElement.from(header.minerPk),
powOnetimePk = isoGroupElement.from(header.powOnetimePk),
powNonce = isoStringToColl.from(header.powNonce),
powDistance = isoBigInt.from(header.powDistance),
votes = isoStringToColl.from(header.votes)
Expand All @@ -161,7 +169,7 @@ object Isos {
timestamp = isoBigIntToLong.to(a.timestamp),
nBits = isoBigIntToLong.to(a.nBits),
height = a.height,
minerPk = isoStringToGroupElement.to(a.minerPk),
minerPk = isoGroupElement.to(a.minerPk),
votes = isoStringToColl.to(a.votes)
)
}
Expand All @@ -173,12 +181,45 @@ object Isos {
timestamp = isoBigIntToLong.from(header.timestamp),
nBits = isoBigIntToLong.from(header.nBits),
height = header.height,
minerPk = isoStringToGroupElement.from(header.minerPk),
minerPk = isoGroupElement.from(header.minerPk),
votes = isoStringToColl.from(header.votes)
)
}
}

val isoBlockchainParameters: Iso[BlockchainParameters, sdk.BlockchainParameters] = new Iso[BlockchainParameters, sdk.BlockchainParameters] {
override def to(a: BlockchainParameters): sdk.BlockchainParameters = {
sdk.BlockchainParameters(
storageFeeFactor = a.storageFeeFactor,
minValuePerByte = a.minValuePerByte,
maxBlockSize = a.maxBlockSize,
tokenAccessCost = a.tokenAccessCost,
inputCost = a.inputCost,
dataInputCost = a.dataInputCost,
outputCost = a.outputCost,
maxBlockCost = a.maxBlockCost,
softForkStartingHeight = Isos.isoUndefOr[Int, Int](Iso.identityIso).to(a.softForkStartingHeight),
softForkVotesCollected = Isos.isoUndefOr[Int, Int](Iso.identityIso).to(a.softForkVotesCollected),
blockVersion = a.blockVersion
)
}
override def from(b: sdk.BlockchainParameters): BlockchainParameters = {
new BlockchainParameters(
storageFeeFactor = b.storageFeeFactor,
minValuePerByte = b.minValuePerByte,
maxBlockSize = b.maxBlockSize,
tokenAccessCost = b.tokenAccessCost,
inputCost = b.inputCost,
dataInputCost = b.dataInputCost,
outputCost = b.outputCost,
maxBlockCost = b.maxBlockCost,
softForkStartingHeight = Isos.isoUndefOr[Int, Int](Iso.identityIso).from(b.softForkStartingHeight),
softForkVotesCollected = Isos.isoUndefOr[Int, Int](Iso.identityIso).from(b.softForkVotesCollected),
blockVersion = b.blockVersion
)
}
}

implicit val isoBlockchainStateContext: Iso[BlockchainStateContext, context.BlockchainStateContext] = new Iso[BlockchainStateContext, context.BlockchainStateContext] {
override def to(a: BlockchainStateContext): context.BlockchainStateContext = {
context.BlockchainStateContext(
Expand Down
15 changes: 13 additions & 2 deletions sdk/js/src/main/scala/org/ergoplatform/sdk/js/PreHeader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@ package org.ergoplatform.sdk.js
import scala.scalajs.js
import scala.scalajs.js.annotation.JSExportTopLevel

/** Equivalent of [[special.sigma.PreHeader]] available from JS. */
/** Equivalent of [[special.sigma.PreHeader]] available from JS.
* Only header fields that can be predicted by a miner.
*/
@JSExportTopLevel("PreHeader")
class PreHeader(
/** Block version, to be increased on every soft and hardfork. */
val version: Byte,
/** Hex of id of parent block */
val parentId: String,
/** Block timestamp (in milliseconds since beginning of Unix Epoch) */
val timestamp: js.BigInt,

/** Current difficulty in a compressed view.
* NOTE: actually it is unsigned integer */
val nBits: js.BigInt,
/** Block height */
val height: Int,
val minerPk: String,
/** Miner public key (hex of EC Point). Should be used to collect block rewards. */
val minerPk: GroupElement,
/** Hex of miner votes bytes for changing system parameters. */
val votes: String
) extends js.Object
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import sigmastate.eval.SigmaDsl
*/
@JSExportTopLevel("ProverBuilder")
class ProverBuilder(parameters: BlockchainParameters, network: Byte) extends js.Object {
val _builder = new sdk.ProverBuilder(parameters, network)
val _builder = new sdk.ProverBuilder(Isos.isoBlockchainParameters.to(parameters), network)

/** Configure this builder to use the given seed when building a new prover.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package org.ergoplatform.sdk.js

import org.ergoplatform.sdk

import scala.scalajs.js
import scala.scalajs.js.annotation.JSExportTopLevel

/** Equivalent of [[sdk.ReducedTransaction]] available from JS. */
@JSExportTopLevel("ReducedTransaction")
class ReducedTransaction(private[js] val _tx: sdk.ReducedTransaction) {
class ReducedTransaction(val _tx: sdk.ReducedTransaction) extends js.Object {
/** Serialized bytes of this transaction in hex format. */
def toHex: String = _tx.toHex
}

@JSExportTopLevel("ReducedTransactionObj")
object ReducedTransaction {
object ReducedTransaction extends js.Object {
/** Creates a [[ReducedTransaction]] from serialized bytes in hex format. */
def fromHex(hex: String): ReducedTransaction = {
val tx = sdk.ReducedTransaction.fromHex(hex)
Expand Down

This file was deleted.

Loading

0 comments on commit 6f322a1

Please sign in to comment.