From 76fb527dcffe770409702f950f70af72b8034bdc Mon Sep 17 00:00:00 2001 From: Artyom Sayadyan Date: Tue, 8 Aug 2023 23:10:12 +0300 Subject: [PATCH] Improved EthereumTransferSmartTest --- .../state/diffs/EthereumTransactionDiff.scala | 4 +- .../transaction/EthereumTransaction.scala | 9 +++- .../com/wavesplatform/utx/UtxPoolImpl.scala | 2 +- .../smart/eth/EthereumTransferSmartTest.scala | 42 +++++++++---------- .../com/wavesplatform/utils/EthHelpers.scala | 4 +- 5 files changed, 31 insertions(+), 30 deletions(-) diff --git a/node/src/main/scala/com/wavesplatform/state/diffs/EthereumTransactionDiff.scala b/node/src/main/scala/com/wavesplatform/state/diffs/EthereumTransactionDiff.scala index b0ef7d65a1..03f320b7cc 100644 --- a/node/src/main/scala/com/wavesplatform/state/diffs/EthereumTransactionDiff.scala +++ b/node/src/main/scala/com/wavesplatform/state/diffs/EthereumTransactionDiff.scala @@ -19,7 +19,7 @@ object EthereumTransactionDiff { val resultEi = e.payload match { case et: EthereumTransaction.Transfer => for { - _ <- if (blockchain.isFeatureActivated(BlockRewardDistribution)) et.check(e.underlying.getData) else Right(()) + _ <- if (blockchain.isFeatureActivated(BlockRewardDistribution)) et.checkAsset(e.underlying.getData) else Right(()) assetId <- et.tryResolveAsset(blockchain) } yield Diff( ethereumTransactionMeta = Map( @@ -60,7 +60,7 @@ object EthereumTransactionDiff { case et: EthereumTransaction.Transfer => for { _ <- checkLeadingZeros(e, blockchain) - _ <- TracedResult { if (blockchain.isFeatureActivated(BlockRewardDistribution)) et.check(e.underlying.getData) else Right(()) } + _ <- TracedResult { if (blockchain.isFeatureActivated(BlockRewardDistribution)) et.checkAsset(e.underlying.getData) else Right(()) } asset <- TracedResult(et.tryResolveAsset(blockchain)) transfer <- TracedResult(et.toTransferLike(e, blockchain)) assetDiff <- TransactionDiffer.assetsVerifierDiff(blockchain, transfer, verify = true, Diff(), Int.MaxValue, enableExecutionLog) diff --git a/node/src/main/scala/com/wavesplatform/transaction/EthereumTransaction.scala b/node/src/main/scala/com/wavesplatform/transaction/EthereumTransaction.scala index 2dc3050523..56230be700 100644 --- a/node/src/main/scala/com/wavesplatform/transaction/EthereumTransaction.scala +++ b/node/src/main/scala/com/wavesplatform/transaction/EthereumTransaction.scala @@ -160,8 +160,12 @@ object EthereumTransaction { amount <- TxPositiveAmount(amount)(TxValidationError.NonPositiveAmount(amount, asset.maybeBase58Repr.getOrElse("waves"))) } yield tx.toTransferLike(amount, recipient, asset) - def check(data: String) = { - Either.cond(tokenAddress.isEmpty || EthEncoding.cleanHexPrefix(data).length == 136, (), GenericError("unconsumed bytes remaining")) + def checkAsset(data: String): Either[GenericError, Unit] = { + Either.cond( + tokenAddress.isEmpty || EthEncoding.cleanHexPrefix(data).length == AssetDataLength, + (), + GenericError("unconsumed bytes remaining") + ) } } @@ -190,6 +194,7 @@ object EthereumTransaction { val GasPrice: BigInteger = Convert.toWei("10", Convert.Unit.GWEI).toBigInteger val AmountMultiplier = 10000000000L + val AssetDataLength = 136 private val decodeMethod = { val m = classOf[TypeDecoder].getDeclaredMethod("decode", classOf[String], classOf[Int], classOf[Class[?]]) diff --git a/node/src/main/scala/com/wavesplatform/utx/UtxPoolImpl.scala b/node/src/main/scala/com/wavesplatform/utx/UtxPoolImpl.scala index fddb1dd670..5d5eeefca7 100644 --- a/node/src/main/scala/com/wavesplatform/utx/UtxPoolImpl.scala +++ b/node/src/main/scala/com/wavesplatform/utx/UtxPoolImpl.scala @@ -151,7 +151,7 @@ case class UtxPoolImpl( case EthereumTransaction(inv: EthereumTransaction.Invocation, _, _, _) => inv.decodeFuncCall(blockchain, true) case et @ EthereumTransaction(tr: EthereumTransaction.Transfer, _, _, _) => - tr.check(et.underlying.getData) + tr.checkAsset(et.underlying.getData) case _ => Right(()) }) .flatMap(_ => diff --git a/node/src/test/scala/com/wavesplatform/state/diffs/smart/eth/EthereumTransferSmartTest.scala b/node/src/test/scala/com/wavesplatform/state/diffs/smart/eth/EthereumTransferSmartTest.scala index dc4e4dd592..058ebf2108 100644 --- a/node/src/test/scala/com/wavesplatform/state/diffs/smart/eth/EthereumTransferSmartTest.scala +++ b/node/src/test/scala/com/wavesplatform/state/diffs/smart/eth/EthereumTransferSmartTest.scala @@ -1,9 +1,11 @@ package com.wavesplatform.state.diffs.smart.eth import com.wavesplatform.account.Address +import com.wavesplatform.block.Block.ProtoBlockVersion import com.wavesplatform.common.state.ByteStr import com.wavesplatform.common.utils.EitherExt2 import com.wavesplatform.db.WithDomain +import com.wavesplatform.db.WithState.AddrWithBalance import com.wavesplatform.lang.directives.DirectiveDictionary import com.wavesplatform.lang.directives.values.{StdLibVersion, V3, V6} import com.wavesplatform.lang.v1.compiler.TestCompiler @@ -16,10 +18,11 @@ import com.wavesplatform.transaction.Asset.{IssuedAsset, Waves} import com.wavesplatform.transaction.assets.IssueTransaction import com.wavesplatform.transaction.smart.SetScriptTransaction import com.wavesplatform.transaction.transfer.TransferTransaction -import com.wavesplatform.transaction.{ERC20Address, EthereumTransaction, GenesisTransaction, TxHelpers} +import com.wavesplatform.transaction.{ERC20Address, EthTxGenerator, EthereumTransaction, TxHelpers} import com.wavesplatform.utils.EthHelpers import scala.collection.immutable.VectorMap +import scala.math.Ordering.Implicits.infixOrderingOps class EthereumTransferSmartTest extends PropSpec with WithDomain with EthHelpers { import DomainPresets.* @@ -73,32 +76,28 @@ class EthereumTransferSmartTest extends PropSpec with WithDomain with EthHelpers val recipient = RandomKeyPair() val issue = IssueTransaction.selfSigned(2.toByte, recipient, "Asset", "", ENOUGH_AMT, 8, true, None, 1.waves, ts).explicitGet() - val asset = IssuedAsset(issue.id()) for { - version <- DirectiveDictionary[StdLibVersion].all.init - token <- Seq(None, Some(ERC20Address(asset.id.take(20)))) + version <- DirectiveDictionary[StdLibVersion].all + asset <- Seq(Waves, IssuedAsset(issue.id())) } { - val transfer = EthereumTransaction.Transfer(token, transferAmount, recipient.toAddress) - val ethTransfer = EthereumTransaction(transfer, TestEthRawTransaction, TestEthSignature, 'T'.toByte) + val ethTransfer = EthTxGenerator.generateEthTransfer(TxHelpers.defaultEthSigner, recipient.toAddress, transferAmount, asset) val ethSender = ethTransfer.senderAddress() - val preTransfer = + val transferIssuedAsset = TransferTransaction.selfSigned(2.toByte, recipient, ethSender, asset, ENOUGH_AMT, Waves, 0.001.waves, ByteStr.empty, ts).explicitGet() - val genesis1 = GenesisTransaction.create(ethSender, ENOUGH_AMT, ts).explicitGet() - val genesis2 = GenesisTransaction.create(recipient.toAddress, ENOUGH_AMT, ts).explicitGet() - val function = if (version >= V3) "transferTransactionById" else "transactionById" - val verifier = Some(accountScript(version, function, ethTransfer, token.map(_ => asset), recipient.toAddress)) + val verifier = Some(accountScript(version, function, ethTransfer, asset.fold(None: Option[IssuedAsset])(Some(_)), recipient.toAddress)) val setVerifier = () => SetScriptTransaction.selfSigned(1.toByte, recipient, verifier, 0.01.waves, ts).explicitGet() - withDomain(RideV6) { d => - d.appendBlock(genesis1, genesis2, issue, preTransfer, setVerifier()) - d.appendBlock(ethTransfer) + withDomain(settingsForRide(version.max(V6)), Seq(AddrWithBalance(ethSender), AddrWithBalance(recipient.toAddress))) { d => + if (asset != Waves) d.appendBlock(issue, transferIssuedAsset) + d.appendBlock(setVerifier()) + d.appendBlock(ProtoBlockVersion, ethTransfer) - val transferPortfolio = if (token.isEmpty) Portfolio.waves(transferAmount) else Portfolio.build(asset, transferAmount) + val transferPortfolio = Portfolio.build(asset, transferAmount) d.liquidDiff.portfolios(recipient.toAddress) shouldBe transferPortfolio - d.liquidDiff.portfolios(ethSender) shouldBe Portfolio.waves(-ethTransfer.underlying.getGasPrice.longValue()).minus(transferPortfolio) + d.liquidDiff.portfolios(ethSender) shouldBe Portfolio.waves(-ethTransfer.underlying.getGasLimit.longValue()).minus(transferPortfolio) d.appendBlock() @@ -124,10 +123,7 @@ class EthereumTransferSmartTest extends PropSpec with WithDomain with EthHelpers val dummyEthTransfer = EthereumTransaction(dummyTransfer, TestEthRawTransaction, TestEthSignature, 'T'.toByte) // needed to pass into asset script val ethSender = dummyEthTransfer.senderAddress() - val genesis1 = TxHelpers.genesis(ethSender, ENOUGH_AMT) - val genesis2 = TxHelpers.genesis(recipient.toAddress, ENOUGH_AMT) - - DirectiveDictionary[StdLibVersion].all.init + DirectiveDictionary[StdLibVersion].all .foreach { version => val script = assetScript(version, dummyEthTransfer, recipient.toAddress) val issue = IssueTransaction.selfSigned(2.toByte, recipient, "Asset", "", ENOUGH_AMT, 8, true, Some(script), 1.waves, ts).explicitGet() @@ -136,9 +132,9 @@ class EthereumTransferSmartTest extends PropSpec with WithDomain with EthHelpers val preTransfer = TransferTransaction.selfSigned(2.toByte, recipient, ethSender, asset, ENOUGH_AMT, Waves, 0.005.waves, ByteStr.empty, ts).explicitGet() - withDomain(RideV6) { d => - d.appendBlock(genesis1, genesis2, issue, preTransfer) - d.appendBlock(ethTransfer) + withDomain(settingsForRide(version.max(V6)), Seq(AddrWithBalance(ethSender), AddrWithBalance(recipient.toAddress))) { d => + d.appendBlock(issue, preTransfer) + d.appendBlock(ProtoBlockVersion, ethTransfer) d.liquidDiff.errorMessage(ethTransfer.id()) shouldBe None d.liquidDiff.portfolios(recipient.toAddress) shouldBe Portfolio.build(asset, transferAmount) diff --git a/node/src/test/scala/com/wavesplatform/utils/EthHelpers.scala b/node/src/test/scala/com/wavesplatform/utils/EthHelpers.scala index ac7fb154d2..bda5ba8d23 100644 --- a/node/src/test/scala/com/wavesplatform/utils/EthHelpers.scala +++ b/node/src/test/scala/com/wavesplatform/utils/EthHelpers.scala @@ -3,6 +3,7 @@ package com.wavesplatform.utils import java.math.BigInteger import com.wavesplatform.account.{Address, PublicKey} import com.wavesplatform.common.state.ByteStr +import com.wavesplatform.transaction.EthereumTransaction.AssetDataLength import com.wavesplatform.transaction.{EthTxGenerator, EthereumTransaction, TxHelpers} import com.wavesplatform.transaction.assets.exchange.OrderAuthentication import org.web3j.crypto.{Bip32ECKeyPair, RawTransaction, SignedRawTransaction} @@ -25,8 +26,7 @@ trait EthHelpers { EthereumTransaction.GasPrice, EthereumTransaction.GasPrice, EthEncoding.toHexString(new Array[Byte](20)), - (BigInt(123) * EthereumTransaction.AmountMultiplier).bigInteger, - "" + "0x" + "1" * AssetDataLength ) val TestEthSignature: SignatureData =