Skip to content

Commit

Permalink
Merge pull request #192 from ergoplatform/appkit-5.0
Browse files Browse the repository at this point in the history
Migrate to sigma v5.0
  • Loading branch information
aslesarenko authored Nov 14, 2022
2 parents 890f27d + eedc109 commit c89e5e3
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 19 deletions.
3 changes: 3 additions & 0 deletions MIGRATION
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[5.0.0]
- block version now required to construct ColdErgoClient

[4.0.12]
- getBoxById(String boxId) was replaced by getBoxById(String boxId, boolean findInPool, boolean findInSpent)
the old behaviour (returning only confirmed unspent boxes) can be achieved by calling
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ class ColdErgoClient(networkType: NetworkType, params: BlockchainParameters) ext
/**
* Convenience constructor for giving maxBlockCost
*/
def this(networkType: NetworkType, maxBlockCost: Int) {
def this(networkType: NetworkType, maxBlockCost: Int, blockVersion: Byte) {
this(networkType, new NodeInfoParameters(
new NodeInfo().parameters(new client.Parameters()
.maxBlockCost(Integer.valueOf(maxBlockCost)))))
.maxBlockCost(Integer.valueOf(maxBlockCost))
.blockVersion(Integer.valueOf(blockVersion)))))
}

override def execute[T](action: function.Function[BlockchainContext, T]): T = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ErgoAuthSpec extends PropSpec with Matchers with ScalaCheckDrivenPropertyC
// EIP-28: "the wallet app adds some own bytes to the obtained message from ErgoAuthRequest"
val signedMessage = new String(Random.randomBytes(16)) + requestedMessage +
new String(Random.randomBytes(32))
val signature = new ColdErgoClient(address.getNetworkType, Parameters.ColdClientMaxBlockCost)
val signature = new ColdErgoClient(address.getNetworkType, Parameters.ColdClientMaxBlockCost, Parameters.ColdClientBlockVersion)
.execute { ctx: BlockchainContext =>

val prover = ctx.newProverBuilder().withMnemonic(mnemonic, SecretString.empty(), false).build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.ergoplatform.appkit.InputBoxesSelectionException.{InputBoxLimitExceededException, NotEnoughCoinsForChangeException, NotEnoughErgsException}
import org.ergoplatform.appkit.JavaHelpers._
import org.ergoplatform.appkit.examples.RunMockedScala.data
import org.ergoplatform.appkit.impl.{Eip4TokenBuilder, ErgoTreeContract}
import org.ergoplatform.appkit.testing.AppkitTesting
import org.ergoplatform.explorer.client.model.{Items, TokenInfo}
Expand All @@ -23,7 +22,6 @@ import java.math.BigInteger
import java.util
import java.util.Arrays
import java.util.function.Consumer
import scala.collection.JavaConversions

class TxBuilderSpec extends PropSpec with Matchers
with ScalaCheckDrivenPropertyChecks
Expand Down Expand Up @@ -308,7 +306,7 @@ class TxBuilderSpec extends PropSpec with Matchers
// the only necessary parameter can either be hard-coded or passed
// together with ReducedTransaction
val maxBlockCost = Parameters.ColdClientMaxBlockCost
val coldClient = new ColdErgoClient(NetworkType.MAINNET, maxBlockCost)
val coldClient = new ColdErgoClient(NetworkType.MAINNET, maxBlockCost, Parameters.ColdClientBlockVersion)

coldClient.execute { ctx: BlockchainContext =>
// test that context is cold
Expand Down
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ assemblyMergeStrategy in assembly := {

lazy val allConfigDependency = "compile->compile;test->test"

val sigmaStateVersion = "4.0.7"
val ergoWalletVersion = "4.0.104"
val sigmaStateVersion = "5.0.2"
val ergoWalletVersion = "5.0.2"
lazy val sigmaState = ("org.scorexfoundation" %% "sigma-state" % sigmaStateVersion).force()
.exclude("ch.qos.logback", "logback-classic")
.exclude("org.scorexfoundation", "scrypto")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import java.util.{Objects, List => JList}
import org.ergoplatform.wallet.secrets.ExtendedSecretKey
import sigmastate.basics.{SigmaProtocolCommonInput, DiffieHellmanTupleProverInput, SigmaProtocol, SigmaProtocolPrivateInput}
import org.ergoplatform._
import org.ergoplatform.appkit.JavaHelpers.{TokenColl, subtractTokenColls}
import org.ergoplatform.appkit.JavaHelpers.TokenColl
import org.ergoplatform.appkit.ReducedInputData.createReductionResult
import org.ergoplatform.utils.ArithUtils
import org.ergoplatform.wallet.protocol.context.{ErgoLikeStateContext, ErgoLikeParameters, TransactionContext}
import sigmastate.Values.{SigmaBoolean, ErgoTree}

import scala.util.Try
import sigmastate.eval.CompiletimeIRContext
import sigmastate.interpreter.Interpreter.{ReductionResult, ScriptEnv}
import sigmastate.interpreter.Interpreter.{ReductionResult, JitReductionResult, ScriptEnv, AotReductionResult, FullReductionResult}
import sigmastate.interpreter.{Interpreter, CostedProverResult, ContextExtension, ProverInterpreter, HintsBag}
import sigmastate.lang.exceptions.CostLimitException
import sigmastate.serialization.SigmaSerializer
Expand All @@ -26,6 +27,8 @@ import sigmastate.utils.Helpers._ // for Scala 2.11
import sigmastate.utils.{SigmaByteWriter, SigmaByteReader}
import spire.syntax.all.cfor
import scalan.util.Extensions.LongOps
import sigmastate.VersionContext
import sigmastate.VersionContext.JitActivationVersion

import scala.collection.mutable

Expand Down Expand Up @@ -306,6 +309,22 @@ case class TokenBalanceException(
*/
case class ReducedInputData(reductionResult: ReductionResult, extension: ContextExtension)

object ReducedInputData {
/** Creates [[ReductionResult]] for the given blockVersion.
*
* @param blockVersion version of Ergo protocol (stored in block header)
* @param sb sigma proposition (typically result of script reduction)
* @param cost cost accumulated during reduction
*/
def createReductionResult(blockVersion: Byte, sb: SigmaBoolean, cost: Long): ReductionResult = {
val scriptVersion = blockVersion - 1 // convert to script version
if (scriptVersion >= JitActivationVersion)
FullReductionResult(null, JitReductionResult(sb, cost))
else
FullReductionResult(AotReductionResult(sb, cost), null)
}
}

/** Represent `reduced` transaction, i.e. unsigned transaction where each unsigned input
* is augmented with [[ReducedInputData]] which contains a script reduction result.
* After an unsigned transaction is reduced it can be signed without context.
Expand Down Expand Up @@ -356,13 +375,24 @@ object ReducedErgoLikeTransactionSerializer extends SigmaSerializer[ReducedErgoL
val cost = r.getULong()
val input = tx.inputs(i)
val extension = input.extension
reducedInputs(i) = ReducedInputData(ReductionResult(sb, cost), extension)
val currentBlockVersion: Byte = (VersionContext.current.activatedVersion + 1).toByte
val reductionResult = createReductionResult(currentBlockVersion, sb, cost)
reducedInputs(i) = ReducedInputData(reductionResult, extension)
unsignedInputs(i) = new UnsignedInput(input.boxId, extension)
}

val unsignedTx = UnsignedErgoLikeTransaction(unsignedInputs, tx.dataInputs, tx.outputCandidates)
ReducedErgoLikeTransaction(unsignedTx, reducedInputs)
}

/** Parses the [[ReducedErgoLikeTransaction]] using the given blockVersion.
* @param blockVersion version of Ergo protocol to use during parsing.
*/
def parse(r: SigmaByteReader, blockVersion: Byte): ReducedErgoLikeTransaction = {
val scriptVersion = (blockVersion - 1).toByte
VersionContext.withVersions(scriptVersion, scriptVersion) {
parse(r)
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ object JavaHelpers {
*/
def substituteErgoTreeConstants(ergoTreeBytes: Array[Byte], positions: Array[Int], newValues: Array[ErgoValue[_]]): ErgoTree = {
val (newBytes, _) = ErgoTreeSerializer.DefaultSerializer.substituteConstants(
ergoTreeBytes, positions, newValues.map(Iso.isoErgoValueToSValue.to))
ergoTreeBytes, positions, newValues.map(v => Iso.isoErgoValueToSValue.to(v).asInstanceOf[Constant[SType]]))
ErgoTreeSerializer.DefaultSerializer.deserializeErgoTree(newBytes)
}

Expand Down
5 changes: 5 additions & 0 deletions common/src/main/java/org/ergoplatform/appkit/Parameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public class Parameters {
* Max block cost for Cold Client
*/
public static final int ColdClientMaxBlockCost = 1000000;

/**
* Activated version for Cold Client
*/
public static final byte ColdClientBlockVersion = 2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ private Signature() {
* @return whether signature is valid or not
*/
public static boolean verifySignature(SigmaProp sigmaProp, byte[] message, byte[] signature) {
return SigmaPropInterpreter.verifySignature(sigmaProp.getSigmaBoolean(), message, signature);
return SigmaPropInterpreter.verifySignature(sigmaProp.getSigmaBoolean(), message, signature, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ErgoTreeTemplateSpec extends TestingBase

assertExceptionThrown(
template.applyParameters(ErgoValue.of(1.toByte)), // invalid type of ErgoValue (should be Int)
exceptionLike[AssertionError]()
exceptionLike[IllegalArgumentException]("expected new constant to have the same SInt$ tpe, got SByte$")
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package org.ergoplatform.appkit

import org.ergoplatform.UnsignedErgoLikeTransaction
import org.ergoplatform.appkit.ReducedInputData.createReductionResult
import org.scalacheck.Gen
import org.scalatest.{PropSpec, Assertion, Matchers}
import org.scalatest.{Assertion, Matchers}
import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks
import sigmastate.CrossVersionProps
import sigmastate.interpreter.ContextExtension
import sigmastate.interpreter.Interpreter.ReductionResult
import sigmastate.serialization.SigmaSerializer
import sigmastate.serialization.generators.ObjectGenerators

class ReducedErgoLikeTransactionSpec extends PropSpec
class ReducedErgoLikeTransactionSpec extends CrossVersionProps
with Matchers with ScalaCheckDrivenPropertyChecks with ObjectGenerators {

def reducedInputDataGen(extension: ContextExtension): Gen[ReducedInputData] = for {
sb <- sigmaBooleanGen
cost <- Gen.choose(10L, 1000L)
} yield
ReducedInputData(ReductionResult(sb, cost), extension)
ReducedInputData(createReductionResult(activatedVersionInTests, sb, cost), extension)

def reducedErgoLikeTransactionGen(
unsignedTx: UnsignedErgoLikeTransaction): Gen[ReducedErgoLikeTransaction] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import org.ergoplatform.appkit.SignedTransaction;
import org.ergoplatform.appkit.BlockchainParameters;

import scala.Function0;
import sigmastate.Values;
import sigmastate.VersionContext$;
import sigmastate.serialization.SigmaSerializer$;
import sigmastate.utils.SigmaByteReader;

Expand Down Expand Up @@ -43,7 +45,7 @@ public NetworkType getNetworkType() {
@Override
public ReducedTransaction parseReducedTransaction(byte[] txBytes) {
SigmaByteReader r = SigmaSerializer$.MODULE$.startReader(txBytes, 0);
ReducedErgoLikeTransaction tx = ReducedErgoLikeTransactionSerializer$.MODULE$.parse(r);
ReducedErgoLikeTransaction tx = ReducedErgoLikeTransactionSerializer$.MODULE$.parse(r, getParameters().getBlockVersion());
int cost = (int)r.getUInt(); // TODO use java7.compat.Math.toIntExact when it will available in Sigma
return new ReducedTransactionImpl(this, tx, cost);
}
Expand Down

0 comments on commit c89e5e3

Please sign in to comment.