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

Improved EthereumTransferSmartTest #3866

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
}
}

Expand Down Expand Up @@ -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[?]])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(_ =>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.*
Expand Down Expand Up @@ -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()

Expand All @@ -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()
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions node/src/test/scala/com/wavesplatform/utils/EthHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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 =
Expand Down