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

Removing unused code #2 #946

Merged
merged 3 commits into from
Jan 5, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,6 @@ class CoreByteReader(val r: Reader, val maxTreeDepth: Int = CoreSerializer.MaxTr
positionLmt = v
}

private var _complexity: Int = 0
/** Helper property which is used to accumulate complexity during parsing. */
@inline final def complexity: Int = _complexity
@inline final def complexity_=(v: Int): Unit = {
_complexity = v
}

@inline final def addComplexity(delta: Int): Unit = {
_complexity += delta
}

private var _wasDeserialize: Boolean = false
/** Helper property which is used to track deserialization operations during parsing. */
@inline final def wasDeserialize: Boolean = _wasDeserialize
Expand Down
157 changes: 0 additions & 157 deletions data/shared/src/main/scala/sigma/ast/ComplexityTable.scala

This file was deleted.

26 changes: 2 additions & 24 deletions data/shared/src/main/scala/sigma/ast/ErgoTree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package sigma.ast
import scorex.util.encode.Base16
import sigma.VersionContext
import sigma.ast.ErgoTree.{HeaderType, substConstants}
import sigma.ast.SigmaPropConstant
import sigma.ast.syntax._
import sigma.data.{CSigmaProp, SigmaBoolean}
import sigma.kiama.rewriting.Rewriter.{everywherebu, strategy}
Expand Down Expand Up @@ -63,13 +62,6 @@ case class UnparsedErgoTree(bytes: mutable.WrappedArray[Byte], error: Validation
* instead of some Constant nodes. Otherwise, it may not contain placeholders.
* It is possible to have both constants and placeholders in the tree, but for every placeholder
* there should be a constant in `constants` array.
* @param givenComplexity structural complexity of the tree or 0 if is not specified at construction time.
* Access to this private value is provided via `complexity` property.
* In case of 0, the complexity is computed using ErgoTree deserializer, which can do this.
* When specified it should be computed as the sum of complexity values taken
* from ComplexityTable for all tree nodes. It approximates the time needed to process
* the tree by sigma compiler to obtain cost formula. Overly complex trees can thus
* be rejected even before compiler starts working.
* @param propositionBytes original bytes of this tree from which it has been deserialized.
* If null then the bytes are not provided, and will be lazily generated when `bytes`
* method is called.
Expand All @@ -88,7 +80,6 @@ case class ErgoTree private[sigma](
header: HeaderType,
constants: IndexedSeq[Constant[SType]],
root: Either[UnparsedErgoTree, SigmaPropValue],
private val givenComplexity: Int,
private val propositionBytes: Array[Byte],
private val givenDeserialize: Option[Boolean],
private val givenIsUsingBlockchainContext: Option[Boolean]
Expand All @@ -98,9 +89,9 @@ case class ErgoTree private[sigma](
constants: IndexedSeq[Constant[SType]],
root: Either[UnparsedErgoTree, SigmaPropValue]) =
this(
header, constants, root, 0,
header, constants, root,
propositionBytes = DefaultSerializer.serializeErgoTree(
ErgoTree(header, constants, root, 0, null, None, None)
ErgoTree(header, constants, root, null, None, None)
),
givenDeserialize = None,
givenIsUsingBlockchainContext = None
Expand Down Expand Up @@ -140,19 +131,6 @@ case class ErgoTree private[sigma](
/** Hexadecimal encoded string of ErgoTree.bytes. */
final def bytesHex: String = Base16.encode(bytes)

private var _complexity: Int = givenComplexity

/** Structural complexity estimation of this tree.
*
* @see ComplexityTable
*/
lazy val complexity: Int = {
if (_complexity == 0) {
_complexity = DefaultSerializer.deserializeErgoTree(bytes).complexity
}
_complexity
}

private[sigma] var _hasDeserialize: Option[Boolean] = givenDeserialize

/** Returns true if the tree contains at least one deserialization operation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import sigma.ast.{EvaluatedValue, SType}
import sigma.interpreter.ContextExtension.VarBinding
import sigma.serialization.{SigmaByteReader, SigmaByteWriter, SigmaSerializer}

import scala.collection.mutable

/**
* User-defined variables to be put into context.
* Each variable is identified by `id: Byte` and can be accessed from a script
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sigma.serialization

import org.ergoplatform.validation.ValidationRules.{CheckDeserializedScriptIsSigmaProp, CheckHeaderSizeBit}
import sigma.ast.{ComplexityTable, Constant, DeserializationSigmaBuilder, ErgoTree, SType, SubstConstants, UnparsedErgoTree}
import sigma.ast.{Constant, DeserializationSigmaBuilder, ErgoTree, SType, SubstConstants, UnparsedErgoTree}
import sigma.ast.syntax.ValueOps
import sigma.ast.ErgoTree.{EmptyConstants, HeaderType}
import sigma.util.safeNewArray
Expand Down Expand Up @@ -135,9 +135,7 @@ class ErgoTreeSerializer {
private[sigma] def deserializeErgoTree(r: SigmaByteReader, maxTreeSizeBytes: Int, checkType: Boolean): ErgoTree = {
val startPos = r.position
val previousPositionLimit = r.positionLimit
val previousComplexity = r.complexity
r.positionLimit = r.position + maxTreeSizeBytes
r.complexity = 0
val (h, sizeOpt) = deserializeHeaderAndSize(r)
val bodyPos = r.position
val tree = try {
Expand Down Expand Up @@ -165,15 +163,13 @@ class ErgoTreeSerializer {
}

r.constantStore = previousConstantStore
val complexity = r.complexity

// now we know the end position of propositionBytes, read them all at once into array
val treeSize = r.position - startPos
r.position = startPos
val propositionBytes = r.getBytes(treeSize)

new ErgoTree(
h, cs, Right(root.asSigmaProp), complexity,
h, cs, Right(root.asSigmaProp),
propositionBytes, Some(hasDeserialize), Some(isUsingBlockchainContext))
}
catch {
Expand All @@ -188,16 +184,14 @@ class ErgoTreeSerializer {
val numBytes = bodyPos - startPos + treeSize
r.position = startPos
val bytes = r.getBytes(numBytes)
val complexity = ComplexityTable.OpCodeComplexity(Constant.opCode)
new ErgoTree(ErgoTree.DefaultHeader, EmptyConstants, Left(UnparsedErgoTree(bytes, ve)), complexity, bytes, None, None)
new ErgoTree(ErgoTree.DefaultHeader, EmptyConstants, Left(UnparsedErgoTree(bytes, ve)), bytes, None, None)
case None =>
throw new SerializerException(
s"Cannot handle ValidationException, ErgoTree serialized without size bit.", Some(ve))
}
}
finally {
r.positionLimit = previousPositionLimit
r.complexity = previousComplexity
}
tree
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sigma.serialization

import sigma.ast.syntax._
import sigma.ast.{ComplexityTable, MethodCall, SContextMethods, SMethod, SType, STypeSubst, Value, ValueCompanion}
import sigma.ast.{MethodCall, SContextMethods, SMethod, SType, STypeSubst, Value, ValueCompanion}
import sigma.util.safeNewArray
import SigmaByteWriter._
import debox.cfor
Expand All @@ -25,8 +25,6 @@ case class MethodCallSerializer(cons: (Value[SType], SMethod, IndexedSeq[Value[S
w.putValues(mc.args, argsInfo, argsItemInfo)
}

override def getComplexity: Int = 0 // because we add it explicitly in parse below

/** The SMethod instances in STypeCompanions may have type STypeIdent in methods types,
* but a valid ErgoTree should have SMethod instances specialized for specific types
* of `obj` and `args` using `specializeFor`.
Expand All @@ -45,8 +43,6 @@ case class MethodCallSerializer(cons: (Value[SType], SMethod, IndexedSeq[Value[S
val obj = r.getValue()
val args = r.getValues()
val method = SMethod.fromIds(typeId, methodId)
val complexity = ComplexityTable.MethodCallComplexity.getOrElse((typeId, methodId), ComplexityTable.MinimalComplexity)
r.addComplexity(complexity)
val nArgs = args.length

val types: Seq[SType] =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package sigma.serialization

import sigma.ast.{ComplexityTable, EmptySubst, SType, STypeSubst}
import sigma.ast.{EmptySubst, SType, STypeSubst}
import sigma.serialization.CoreByteWriter.{ArgInfo, DataInfo}
import sigma.ast._
import sigma.ast.syntax.SValue
Expand All @@ -9,7 +9,6 @@ import SigmaByteWriter._
case class PropertyCallSerializer(cons: (Value[SType], SMethod, IndexedSeq[Value[SType]], STypeSubst) => Value[SType])
extends ValueSerializer[MethodCall] {
override def opDesc: ValueCompanion = PropertyCall
override def getComplexity: Int = 0 // because we add it explicitly in parse below
val typeCodeInfo: DataInfo[Byte] = ArgInfo("typeCode", "type of the method (see Table~\\ref{table:predeftypes})")
val methodCodeInfo: DataInfo[Byte] = ArgInfo("methodCode", "a code of the property")
val objInfo: DataInfo[SValue] = ArgInfo("obj", "receiver object of this property call")
Expand All @@ -25,8 +24,6 @@ case class PropertyCallSerializer(cons: (Value[SType], SMethod, IndexedSeq[Value
val methodId = r.getByte()
val obj = r.getValue()
val method = SMethod.fromIds(typeId, methodId)
val complexity = ComplexityTable.MethodCallComplexity.getOrElse((typeId, methodId), ComplexityTable.MinimalComplexity)
r.addComplexity(complexity)
val specMethod = method.specializeFor(obj.tpe, SType.EmptySeq)
cons(obj, specMethod, Value.EmptySeq, EmptySubst)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@ import sigma.util.Extensions.toUByte
import sigma.serialization.ValueCodes.OpCode
import sigma.serialization.transformers._
import sigma.serialization.trees.{QuadrupleSerializer, Relation2Serializer}
import ComplexityTable._
import sigma.utils.SparseArrayContainer

import scala.collection.mutable

abstract class ValueSerializer[V <: Value[SType]] extends SigmaSerializer[Value[SType], V] {

def getComplexity: Int = OpCodeComplexity.getOrElse(opCode, MinimalComplexity)
lazy val complexity: Int = getComplexity

def opDesc: ValueCompanion
/** Code of the corresponding tree node (Value.opCode) which is used to lookup this serizalizer
* during deserialization. It is emitted immediately before the body of this node in serialized byte array. */
Expand Down Expand Up @@ -391,13 +387,11 @@ object ValueSerializer extends SigmaSerializerCompanion[Value[SType]] {
val firstByte = toUByte(r.peekByte())
val v = if (firstByte <= LastConstantCode) {
// look ahead byte tell us this is going to be a Constant
r.addComplexity(constantSerializer.complexity)
constantSerializer.deserialize(r)
}
else {
val opCode = r.getByte().asInstanceOf[OpCode]
val ser = getSerializer(opCode)
r.addComplexity(ser.complexity)
ser.parse(r)
}
r.level = r.level - 1
Expand Down
Loading
Loading