Skip to content

Commit

Permalink
Refactored PBSince
Browse files Browse the repository at this point in the history
  • Loading branch information
xrtm000 committed Nov 7, 2023
1 parent d5b9aa3 commit b12a5c2
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ final case class TransactionJsonSerializer(blockchain: Blockchain, commonApi: Co
tx.assetFee._1.maybeBase58Repr.foreach(gen.writeStringField("feeAssetId", _))
gen.writeNumberField("timestamp", tx.timestamp, numbersAsString)
gen.writeNumberField("version", tx.version, numbersAsString)
if (tx.asInstanceOf[PBSince].isProtobufVersion) gen.writeNumberField("chainId", tx.chainId, numbersAsString)
if (PBSince.affects(tx)) gen.writeNumberField("chainId", tx.chainId, numbersAsString)
gen.writeStringField("sender", tx.sender.toAddress(tx.chainId).toString)
gen.writeStringField("senderPublicKey", tx.sender.toString)
gen.writeArrayField("proofs")(gen => tx.proofs.proofs.foreach(p => gen.writeString(p.toString)))
Expand Down Expand Up @@ -312,7 +312,7 @@ final case class TransactionJsonSerializer(blockchain: Blockchain, commonApi: Co
tx.assetFee._1.maybeBase58Repr.foreach(gen.writeStringField("feeAssetId", _))
gen.writeNumberField("timestamp", tx.timestamp, numbersAsString)
gen.writeNumberField("version", tx.version, numbersAsString)
if (tx.isProtobufVersion) gen.writeNumberField("chainId", tx.chainId, numbersAsString)
if (PBSince.affects(tx)) gen.writeNumberField("chainId", tx.chainId, numbersAsString)
gen.writeStringField("bytes", EthEncoding.toHexString(tx.bytes()))
gen.writeStringField("sender", tx.senderAddress().toString)
gen.writeStringField("senderPublicKey", tx.signerPublicKey().toString)
Expand Down
26 changes: 9 additions & 17 deletions node/src/main/scala/com/wavesplatform/database/package.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.wavesplatform

import java.nio.ByteBuffer
import java.util
import java.util.Map as JMap
import com.google.common.base.Charsets.UTF_8
import com.google.common.collect.{Interners, Maps}
import com.google.common.io.ByteStreams.{newDataInput, newDataOutput}
Expand All @@ -28,22 +25,17 @@ import com.wavesplatform.state.StateHash.SectionId
import com.wavesplatform.state.reader.LeaseDetails
import com.wavesplatform.transaction.Asset.IssuedAsset
import com.wavesplatform.transaction.lease.LeaseTransaction
import com.wavesplatform.transaction.{
EthereumTransaction,
GenesisTransaction,
PBSince,
PaymentTransaction,
Transaction,
TransactionParsers,
TxValidationError
}
import com.wavesplatform.transaction.{EthereumTransaction, GenesisTransaction, PBSince, PaymentTransaction, Transaction, TransactionParsers, TxValidationError, VersionedTransaction}
import com.wavesplatform.utils.*
import monix.eval.Task
import monix.reactive.Observable
import org.rocksdb.*
import sun.nio.ch.Util
import supertagged.TaggedType

import java.nio.ByteBuffer
import java.util
import java.util.Map as JMap
import scala.annotation.tailrec
import scala.collection.mutable.ArrayBuffer
import scala.collection.{View, mutable}
Expand Down Expand Up @@ -645,11 +637,11 @@ package object database {
def writeTransaction(v: (TxMeta, Transaction)): Array[Byte] = {
val (m, tx) = v
val ptx = tx match {
case lps: PBSince if !lps.isProtobufVersion => TD.LegacyBytes(ByteString.copyFrom(tx.bytes()))
case _: GenesisTransaction => TD.LegacyBytes(ByteString.copyFrom(tx.bytes()))
case _: PaymentTransaction => TD.LegacyBytes(ByteString.copyFrom(tx.bytes()))
case et: EthereumTransaction => TD.EthereumTransaction(ByteString.copyFrom(et.bytes()))
case _ => TD.WavesTransaction(PBTransactions.protobuf(tx))
case lps: PBSince with VersionedTransaction if !PBSince.affects(lps) => TD.LegacyBytes(ByteString.copyFrom(tx.bytes()))
case _: GenesisTransaction => TD.LegacyBytes(ByteString.copyFrom(tx.bytes()))
case _: PaymentTransaction => TD.LegacyBytes(ByteString.copyFrom(tx.bytes()))
case et: EthereumTransaction => TD.EthereumTransaction(ByteString.copyFrom(et.bytes()))
case _ => TD.WavesTransaction(PBTransactions.protobuf(tx))
}
pb.TransactionData(ptx, m.status.protobuf, m.spentComplexity).toByteArray
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ object CommonValidation {
case v: VersionedTransaction if !versionIsCorrect(v) && blockchain.isFeatureActivated(LightNode) =>
Left(UnsupportedTypeAndVersion(v.tpe.id.toByte, v.version))

case p: PBSince if p.isProtobufVersion =>
case p: PBSince with VersionedTransaction if PBSince.affects(p) =>
activationBarrier(BlockchainFeatures.BlockV5)

case v: VersionedTransaction if !versionIsCorrect(v) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ object FeeValidation {
case tx: DataTransaction =>
val payloadLength =
if (blockchain.isFeatureActivated(BlockchainFeatures.RideV6)) DataTxValidator.realUserPayloadSize(tx.data)
else if (tx.isProtobufVersion) tx.protoDataPayload.length
else if (PBSince.affects(tx)) tx.protoDataPayload.length
else if (blockchain.isFeatureActivated(BlockchainFeatures.SmartAccounts)) tx.bodyBytes().length
else tx.bytes().length

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ object InvokeDiffsCommon {
tx.enableEmptyKeys || dataEntries.forall(_.key.nonEmpty),
(), {
val versionInfo = tx.root match {
case s: PBSince => s" in tx version >= ${s.protobufVersion}"
case s: PBSince => s" in tx version >= ${PBSince.version(s)}"
case _ => ""
}
s"Empty keys aren't allowed$versionInfo"
Expand Down
27 changes: 13 additions & 14 deletions node/src/main/scala/com/wavesplatform/transaction/PBSince.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.wavesplatform.transaction

import com.wavesplatform.transaction.PBSince.*
sealed trait PBSince

import scala.annotation.nowarn
object PBSince {
trait V1 extends PBSince
trait V2 extends PBSince
trait V3 extends PBSince

sealed trait PBSince { self: Transaction & VersionedTransaction =>
lazy val protobufVersion: TxVersion = this match {
case _: V1 => TxVersion.V1
case _: V2 => TxVersion.V2: @nowarn // scalac: unreachable code
case _: V3 => TxVersion.V3
}
final def isProtobufVersion: Boolean = self.version >= protobufVersion
}
def version(tx: PBSince): TxVersion =
tx match {
case _: V1 => TxVersion.V1
case _: V2 => TxVersion.V2
case _: V3 => TxVersion.V3
}

object PBSince {
trait V1 extends PBSince { self: Transaction & VersionedTransaction => }
trait V2 extends PBSince { self: Transaction & VersionedTransaction => }
trait V3 extends PBSince { self: Transaction & VersionedTransaction => }
def affects(tx: PBSince & VersionedTransaction): Boolean =
tx.version >= version(tx)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ object BaseTxJson {
case v: VersionedTransaction => Json.obj("version" -> v.version)
case _ => Json.obj()
}) ++ (tx match {
case pbs: PBSince if pbs.isProtobufVersion => Json.obj("chainId" -> tx.chainId)
case _ => Json.obj()
case pbs: PBSince with VersionedTransaction if PBSince.affects(pbs) => Json.obj("chainId" -> tx.chainId)
case _ => Json.obj()
}) ++ (tx match {
case p: ProvenTransaction =>
Json.obj(
"sender" -> p.sender.toAddress(p.chainId),
"sender" -> p.sender.toAddress(p.chainId),
"senderPublicKey" -> p.sender,
"proofs" -> JsArray(p.proofs.proofs.map(p => JsString(p.toString)))
) ++ (tx match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import java.nio.charset.StandardCharsets.UTF_8
import com.google.common.primitives.{Bytes, Longs, Shorts}
import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.account.AddressScheme
import com.wavesplatform.serialization._
import com.wavesplatform.serialization.*
import com.wavesplatform.state.DataEntry.Type
import com.wavesplatform.state.{BinaryDataEntry, BooleanDataEntry, DataEntry, IntegerDataEntry, StringDataEntry}
import com.wavesplatform.transaction.{DataTransaction, TxPositiveAmount, TxVersion}
import com.wavesplatform.transaction.{DataTransaction, PBSince, TxPositiveAmount, TxVersion}
import com.wavesplatform.utils.StringBytes
import play.api.libs.json.{JsObject, Json}

Expand Down Expand Up @@ -51,7 +51,7 @@ object DataTxSerializer {
}

def toBytes(tx: DataTransaction): Array[Byte] =
if (tx.isProtobufVersion) PBTransactionSerializer.bytes(tx)
if (PBSince.affects(tx)) PBTransactionSerializer.bytes(tx)
else Bytes.concat(Array(0: Byte), this.bodyBytes(tx), tx.proofs.bytes())

def parseBytes(bytes: Array[Byte]): Try[DataTransaction] = Try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.wavesplatform.transaction.serialization.impl

import java.nio.ByteBuffer

import com.google.common.primitives.{Bytes, Longs}
import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.common.utils.*
Expand All @@ -11,7 +10,7 @@ import com.wavesplatform.lang.v1.serialization.SerdeV1
import com.wavesplatform.serialization.*
import com.wavesplatform.transaction.smart.InvokeScriptTransaction
import com.wavesplatform.transaction.smart.InvokeScriptTransaction.Payment
import com.wavesplatform.transaction.{Asset, TxPositiveAmount, TxVersion}
import com.wavesplatform.transaction.{Asset, PBSince, TxPositiveAmount, TxVersion}
import play.api.libs.json.{JsArray, JsObject, JsString, Json}

import scala.util.Try
Expand Down Expand Up @@ -69,7 +68,7 @@ object InvokeScriptTxSerializer {
}

def toBytes(tx: InvokeScriptTransaction): Array[Byte] =
if (tx.isProtobufVersion) PBTransactionSerializer.bytes(tx)
if (PBSince.affects(tx)) PBTransactionSerializer.bytes(tx)
else Bytes.concat(Array(0: Byte), this.bodyBytes(tx), tx.proofs.bytes())

def parseBytes(bytes: Array[Byte]): Try[InvokeScriptTransaction] = Try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import java.nio.ByteBuffer
import com.google.common.primitives.{Bytes, Longs, Shorts}
import com.wavesplatform.account.AddressScheme
import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.serialization._
import com.wavesplatform.transaction.{TxNonNegativeAmount, TxPositiveAmount, TxVersion}
import com.wavesplatform.serialization.*
import com.wavesplatform.transaction.{PBSince, TxNonNegativeAmount, TxPositiveAmount, TxVersion}
import com.wavesplatform.transaction.transfer.MassTransferTransaction
import com.wavesplatform.transaction.transfer.MassTransferTransaction.{ParsedTransfer, Transfer}
import com.wavesplatform.utils.byteStrFormat
Expand Down Expand Up @@ -51,7 +51,7 @@ object MassTransferTxSerializer {
}

def toBytes(tx: MassTransferTransaction): Array[Byte] =
if (tx.isProtobufVersion) PBTransactionSerializer.bytes(tx)
if (PBSince.affects(tx)) PBTransactionSerializer.bytes(tx)
else Bytes.concat(this.bodyBytes(tx), tx.proofs.bytes()) // No zero mark

def parseBytes(bytes: Array[Byte]): Try[MassTransferTransaction] = Try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.nio.ByteBuffer
import com.google.common.primitives.{Bytes, Longs}
import com.wavesplatform.account.AddressScheme
import com.wavesplatform.serialization.{ByteBufferOps, Deser}
import com.wavesplatform.transaction.{TxPositiveAmount, TxVersion}
import com.wavesplatform.transaction.{PBSince, TxPositiveAmount, TxVersion}
import com.wavesplatform.transaction.assets.SetAssetScriptTransaction
import play.api.libs.json.{JsObject, Json}

Expand Down Expand Up @@ -38,7 +38,7 @@ object SetAssetScriptTxSerializer {
}

def toBytes(tx: SetAssetScriptTransaction): Array[Byte] =
if (tx.isProtobufVersion) PBTransactionSerializer.bytes(tx)
if (PBSince.affects(tx)) PBTransactionSerializer.bytes(tx)
else Bytes.concat(Array(0: Byte), this.bodyBytes(tx), tx.proofs.bytes())

def parseBytes(bytes: Array[Byte]): Try[SetAssetScriptTransaction] = Try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.nio.ByteBuffer
import com.google.common.primitives.{Bytes, Longs}
import com.wavesplatform.account.AddressScheme
import com.wavesplatform.serialization.{ByteBufferOps, Deser}
import com.wavesplatform.transaction.{TxPositiveAmount, TxVersion}
import com.wavesplatform.transaction.{PBSince, TxPositiveAmount, TxVersion}
import com.wavesplatform.transaction.smart.SetScriptTransaction
import play.api.libs.json.{JsObject, Json}

Expand Down Expand Up @@ -36,7 +36,7 @@ object SetScriptTxSerializer {
}

def toBytes(tx: SetScriptTransaction): Array[Byte] = {
if (tx.isProtobufVersion) PBTransactionSerializer.bytes(tx)
if (PBSince.affects(tx)) PBTransactionSerializer.bytes(tx)
else Bytes.concat(Array(0: Byte), this.bodyBytes(tx), tx.proofs.bytes())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.nio.ByteBuffer
import com.google.common.primitives.{Bytes, Longs}
import com.wavesplatform.account.AddressScheme
import com.wavesplatform.serialization.ByteBufferOps
import com.wavesplatform.transaction.{TxPositiveAmount, TxVersion}
import com.wavesplatform.transaction.{PBSince, TxPositiveAmount, TxVersion}
import com.wavesplatform.transaction.assets.SponsorFeeTransaction
import play.api.libs.json.{JsObject, Json}

Expand Down Expand Up @@ -38,7 +38,7 @@ object SponsorFeeTxSerializer {
}

def toBytes(tx: SponsorFeeTransaction): Array[Byte] = {
if (tx.isProtobufVersion) PBTransactionSerializer.bytes(tx)
if (PBSince.affects(tx)) PBTransactionSerializer.bytes(tx)
else Bytes.concat(Array(0: Byte, tx.tpe.id.toByte, tx.version), this.bodyBytes(tx), tx.proofs.bytes()) // [typeId, version] appears twice
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ package com.wavesplatform.transaction.validation.impl
import cats.data.ValidatedNel
import com.wavesplatform.lang.ValidationError
import com.wavesplatform.transaction.TxValidationError.{GenericError, OrderValidationError}
import com.wavesplatform.transaction.TxVersion
import com.wavesplatform.transaction.assets.exchange.{ExchangeTransaction, Order, OrderType}
import com.wavesplatform.transaction.validation.TxValidator
import com.wavesplatform.transaction.{PBSince, TxVersion}

object ExchangeTxValidator extends TxValidator[ExchangeTransaction] {
override def validate(tx: ExchangeTransaction): ValidatedNel[ValidationError, ExchangeTransaction] = {
import tx._
import tx.*

V.seq(tx)(
V.cond(sellMatcherFee <= Order.MaxAmount, GenericError("sellMatcherFee too large")),
V.cond(buyMatcherFee <= Order.MaxAmount, GenericError("buyMatcherFee too large")),
V.cond(fee.value <= Order.MaxAmount, GenericError("fee too large")),
V.cond(isProtobufVersion || order1.orderType == OrderType.BUY, GenericError("order1 should have OrderType.BUY")),
V.cond(PBSince.affects(tx) || order1.orderType == OrderType.BUY, GenericError("order1 should have OrderType.BUY")),
V.cond(buyOrder.orderType == OrderType.BUY, GenericError("buyOrder should has OrderType.BUY")),
V.cond(sellOrder.orderType == OrderType.SELL, GenericError("sellOrder should has OrderType.SELL")),
V.cond(buyOrder.matcherPublicKey == sellOrder.matcherPublicKey, GenericError("buyOrder.matcher should be the same as sellOrder.matcher")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package com.wavesplatform.transaction.validation.impl

import cats.data.NonEmptyList
import cats.data.Validated.{Invalid, Valid}
import cats.syntax.either._
import cats.syntax.either.*
import com.wavesplatform.lang.v1.compiler.Terms.FUNCTION_CALL
import com.wavesplatform.lang.v1.{ContractLimits, FunctionHeader}
import com.wavesplatform.protobuf.transaction.PBTransactions
import com.wavesplatform.transaction.PBSince
import com.wavesplatform.transaction.TxValidationError.{GenericError, NonPositiveAmount}
import com.wavesplatform.transaction.smart.InvokeScriptTransaction
import com.wavesplatform.transaction.smart.InvokeScriptTransaction.Payment
import com.wavesplatform.transaction.validation.{TxValidator, ValidatedNV, ValidatedV}
import com.wavesplatform.utils._
import com.wavesplatform.utils.*

import scala.util.Try

Expand All @@ -27,7 +28,7 @@ object InvokeScriptTxValidator extends TxValidator[InvokeScriptTransaction] {

def checkLength: Either[GenericError, Unit] = {
val length =
if (tx.isProtobufVersion)
if (PBSince.affects(tx))
PBTransactions.toPBInvokeScriptData(tx.dApp, tx.funcCallOpt, tx.payments).toByteArray.length
else
tx.bytes().length
Expand Down
Loading

0 comments on commit b12a5c2

Please sign in to comment.