From 5f1a74b1accd5332a15b0953b8a2ded2520bbdd9 Mon Sep 17 00:00:00 2001 From: "Nastya.Urlapova" Date: Thu, 1 Mar 2018 18:06:41 +0300 Subject: [PATCH 1/3] Rewrite AliasTransactionSuite in sync way JIRA: https://wavesplatform.atlassian.net/browse/NODE-576 --- .../wavesplatform/it/api/SyncHttpApi.scala | 9 + .../transactions/AliasTransactionSuite.scala | 197 ------------------ .../transactions/AliasTransactionSuite.scala | 141 +++++++++++++ 3 files changed, 150 insertions(+), 197 deletions(-) delete mode 100644 it/src/test/scala/com/wavesplatform/it/async/transactions/AliasTransactionSuite.scala create mode 100644 it/src/test/scala/com/wavesplatform/it/sync/transactions/AliasTransactionSuite.scala diff --git a/it/src/main/scala/com/wavesplatform/it/api/SyncHttpApi.scala b/it/src/main/scala/com/wavesplatform/it/api/SyncHttpApi.scala index e5fc8f17e95..cc794ad6927 100644 --- a/it/src/main/scala/com/wavesplatform/it/api/SyncHttpApi.scala +++ b/it/src/main/scala/com/wavesplatform/it/api/SyncHttpApi.scala @@ -4,9 +4,11 @@ import akka.http.scaladsl.model.StatusCodes import com.wavesplatform.it.Node import org.asynchttpclient.Response import org.scalatest.{Assertion, Assertions, Matchers} +import play.api.libs.json.Json.parse import play.api.libs.json.Writes import scorex.api.http.assets.SignedMassTransferRequest import scorex.transaction.assets.MassTransferTransaction.Transfer +import com.wavesplatform.it.RequestErrorAssert.ErrorMessage import scala.concurrent.duration._ import scala.concurrent.{Await, Future} @@ -20,6 +22,13 @@ object SyncHttpApi extends Assertions{ case _ => Assertions.fail(s"Expecting bad request") } + def assertBadRequestAndMessage2[R](f: => R, errorMessage: String): Assertion = Try(f) match { + case Failure(UnexpectedStatusCodeException(_, statusCode, responseBody)) => + Assertions.assert(statusCode == StatusCodes.BadRequest.intValue && parse(responseBody).as[ErrorMessage].message.contains(errorMessage)) + case Failure(e) => Assertions.fail(e) + case _ => Assertions.fail(s"Expecting bad request") + } + implicit class NodeExtSync(n: Node) extends Assertions with Matchers { import com.wavesplatform.it.api.AsyncHttpApi.{NodeAsyncHttpApi => async} diff --git a/it/src/test/scala/com/wavesplatform/it/async/transactions/AliasTransactionSuite.scala b/it/src/test/scala/com/wavesplatform/it/async/transactions/AliasTransactionSuite.scala deleted file mode 100644 index 93d52e80229..00000000000 --- a/it/src/test/scala/com/wavesplatform/it/async/transactions/AliasTransactionSuite.scala +++ /dev/null @@ -1,197 +0,0 @@ -package com.wavesplatform.it.transactions - -import com.wavesplatform.it.api.AsyncHttpApi._ -import com.wavesplatform.it.util._ -import org.scalatest.prop.TableDrivenPropertyChecks - -import scala.concurrent.Await -import scala.concurrent.duration._ - -class AliasTransactionSuite extends BaseTransactionSuite with TableDrivenPropertyChecks { - - private val waitCompletion = 2.minutes - private val aliasFee = 1.waves - private val transferFee = 1.waves - private val leasingFee = 0.001.waves - private val transferAmount = 1.waves - - test("Able to send money to an alias") { - val alias = "test_alias" - - val f = for { - (balance, effectiveBalance) <- notMiner.accountBalances(firstAddress) - aliasTxId <- sender.createAlias(firstAddress, alias, aliasFee).map(_.id) - - - _ <- nodes.waitForHeightAraiseAndTxPresent(aliasTxId) - - _ <- notMiner.assertBalances(firstAddress, balance - aliasFee, effectiveBalance - aliasFee) - transferId <- sender.transfer(firstAddress - , s"alias:${sender.settings.blockchainSettings.addressSchemeCharacter}:$alias" - , transferAmount, transferFee).map(_.id) - - _ <- nodes.waitForHeightAraiseAndTxPresent(transferId) - - _ <- notMiner.assertBalances(firstAddress - , balance - transferFee - aliasFee - , effectiveBalance - transferFee - aliasFee) - } yield succeed - - Await.result(f, waitCompletion) - } - - test("Not able to create same aliases to same address") { - val alias = "test_alias2" - val f = for { - (balance, effectiveBalance) <- notMiner.accountBalances(firstAddress) - aliasTxId <- sender.createAlias(firstAddress, alias, aliasFee).map(_.id) - - _ <- nodes.waitForHeightAraiseAndTxPresent(aliasTxId) - newBalance = balance - aliasFee - newEffectiveBalance = effectiveBalance - aliasFee - - _ <- notMiner.assertBalances(firstAddress, newBalance, newEffectiveBalance) - _ <- assertBadRequest(sender.createAlias(firstAddress, alias, aliasFee)) - _ <- notMiner.assertBalances(firstAddress, newBalance, newEffectiveBalance) - } yield succeed - - Await.result(f, waitCompletion) - } - - test("Not able to create aliases to other addresses") { - val alias = "test_alias3" - - val f = for { - (balance, effectiveBalance) <- notMiner.accountBalances(firstAddress) - - aliasTxId <- sender.createAlias(firstAddress, alias, aliasFee).map(_.id) - - _ <- nodes.waitForHeightAraiseAndTxPresent(aliasTxId) - _ <- assertBadRequestAndMessage(sender.createAlias(secondAddress, alias, aliasFee), "already in the state") - _ <- notMiner.assertBalances(firstAddress, balance - aliasFee, effectiveBalance - aliasFee) - } yield succeed - - Await.result(f, waitCompletion) - } - - test("Able to create several different aliases to same addresses") { - val firstAlias = "test_alias4" - val secondAlias = "test_alias5" - - val f = for { - - (balance, effectiveBalance) <- notMiner.accountBalances(secondAddress) - - aliasFirstTxId <- sender.createAlias(secondAddress, firstAlias, aliasFee).map(_.id) - _ <- nodes.waitForHeightAraiseAndTxPresent(aliasFirstTxId) - - newBalance = balance - aliasFee - newEffectiveBalance = effectiveBalance - aliasFee - - _ <- notMiner.assertBalances(secondAddress, newBalance, newEffectiveBalance) - - aliasSecondTxId <- sender.createAlias(secondAddress, secondAlias, aliasFee).map(_.id) - - _ <- nodes.waitForHeightAraiseAndTxPresent(aliasSecondTxId) - _ <- notMiner.assertBalances(secondAddress, newBalance - aliasFee, newEffectiveBalance - aliasFee) - - aliasesList <- sender.aliasByAddress(secondAddress) - - } yield { - aliasesList should contain allElementsOf Seq(firstAlias, secondAlias) - .map(s => s"alias:${sender.settings.blockchainSettings.addressSchemeCharacter}:$s") - } - - Await.result(f, waitCompletion) - } - - test("Able to get address by alias") { - val alias = "test_alias_6" - val f = for { - balance <- notMiner.accountBalance(firstAddress) - aliasFirstTxId <- sender.createAlias(firstAddress, alias, aliasFee).map(_.id) - _ <- nodes.waitForHeightAraiseAndTxPresent(aliasFirstTxId) - addressByAlias <- sender.addressByAlias(alias).map(_.address) - } yield { - addressByAlias should be(firstAddress) - } - - Await.result(f, waitCompletion) - } - - val aliases_names = - Table("aliasName", - "aaaa", - "sixteen_chars_al", - "....", - "1234567890123456", - "@.@-@_@") - - aliases_names.foreach { alias => - test(s"create alias named $alias") { - val f = for { - (balance, effectiveBalance) <- notMiner.accountBalances(secondAddress) - aliasTxId <- sender.createAlias(secondAddress, alias, aliasFee).map(_.id) - _ <- nodes.waitForHeightAraiseAndTxPresent(aliasTxId) - _ <- notMiner.assertBalances(secondAddress, balance - aliasFee, effectiveBalance - aliasFee) - - } yield succeed - - Await.result(f, waitCompletion) - } - - } - - val invalid_aliases_names = - Table(("aliasName", "message"), - ("", "Alias '' length should be between 4 and 30"), - ("abc", "Alias 'abc' length should be between 4 and 30"), - (null, "failed to parse json message"), - ("morethen_thirtycharactersinline", "Alias 'morethen_thirtycharactersinline' length should be between 4 and 30"), - ("~!|#$%^&*()_+=\";:/?><|\\][{}", "Alias should contain only following characters: -.0123456789@_abcdefghijklmnopqrstuvwxyz"), - ("multilnetest\ntest", "Alias should contain only following characters: -.0123456789@_abcdefghijklmnopqrstuvwxyz"), - ("UpperCaseAliase", "Alias should contain only following characters: -.0123456789@_abcdefghijklmnopqrstuvwxyz")) - - forAll(invalid_aliases_names) { (alias: String, message: String) => - test(s"Not able to create alias named $alias") { - val f = for { - _ <- assertBadRequestAndMessage(sender.createAlias(secondAddress, alias, aliasFee), message) - } yield succeed - - Await.result(f, waitCompletion) - } - } - - test("Able to lease by alias") { - val thirdAddressAlias = "leasing_alias" - val buildedThirdAddressAlias = s"alias:${sender.settings.blockchainSettings.addressSchemeCharacter}:$thirdAddressAlias" - - val f = for { - ((firstAddressBalance, firstAddressEffectiveBalance), (thirdAddressBalance, thirdAddressEffectiveBalance)) <- notMiner.accountBalances(firstAddress) - .zip(notMiner.accountBalances(thirdAddress)) - - aliasTxId <- sender.createAlias(thirdAddress, thirdAddressAlias, aliasFee).map(_.id) - _ <- nodes.waitForHeightAraiseAndTxPresent(aliasTxId) - - //lease maximum value, to pass next test - leasingAmount = firstAddressBalance - leasingFee - 0.5.waves - - leasingTx <- sender.lease(firstAddress, buildedThirdAddressAlias, leasingAmount, leasingFee).map(_.id) - _ <- nodes.waitForHeightAraiseAndTxPresent(leasingTx) - - _ <- notMiner.assertBalances(firstAddress, firstAddressBalance - leasingFee, firstAddressEffectiveBalance - leasingAmount - leasingFee) - .zip(notMiner.assertBalances(thirdAddress, thirdAddressBalance - aliasFee, thirdAddressEffectiveBalance - aliasFee + leasingAmount)) - } yield succeed - Await.result(f, waitCompletion) - } - - //previous test should not be commented to run this one - test("Not able to create aliase when insufficient funds") { - val alias = "test_alias7" - val f = for { - _ <- assertBadRequestAndMessage(sender.createAlias(firstAddress, alias, aliasFee), - "State check failed. Reason: negative effective balance") - } yield succeed - Await.result(f, waitCompletion) - } -} \ No newline at end of file diff --git a/it/src/test/scala/com/wavesplatform/it/sync/transactions/AliasTransactionSuite.scala b/it/src/test/scala/com/wavesplatform/it/sync/transactions/AliasTransactionSuite.scala new file mode 100644 index 00000000000..b0870bc91cd --- /dev/null +++ b/it/src/test/scala/com/wavesplatform/it/sync/transactions/AliasTransactionSuite.scala @@ -0,0 +1,141 @@ +package com.wavesplatform.it.sync.transactions + +import com.wavesplatform.it.api.SyncHttpApi._ +import com.wavesplatform.it.transactions.BaseTransactionSuite +import com.wavesplatform.it.util._ +import org.scalatest.prop.TableDrivenPropertyChecks + + +class AliasTransactionSuite extends BaseTransactionSuite with TableDrivenPropertyChecks { + + private val transferFee = 1.waves + private val leasingFee = 0.001.waves + private val transferAmount = 1.waves + + test("Able to send money to an alias") { + val alias = "test_alias" + val (balance1, eff1) = notMiner.accountBalances(firstAddress) + + val aliasFee = calcAliasFee(firstAddress, alias) + notMiner.assertBalances(firstAddress, balance1 - aliasFee, eff1 - aliasFee) + + val aliasFull = fullAliasByAddress(firstAddress, alias) + + val transferId = sender.transfer(firstAddress + , aliasFull + , transferAmount + , transferFee).id + + nodes.waitForHeightAraiseAndTxPresent(transferId) + notMiner.assertBalances(firstAddress + , balance1 - transferFee - aliasFee + , eff1 - transferFee - aliasFee) + } + + test("Not able to create same aliases to same address") { + val alias = "test_alias2" + val (balance1, eff1) = notMiner.accountBalances(firstAddress) + val aliasFee = calcAliasFee(firstAddress, alias) + notMiner.assertBalances(firstAddress, balance1 - aliasFee, eff1 - aliasFee) + + assertBadRequest2(sender.createAlias(firstAddress, alias, transferFee)) + notMiner.assertBalances(firstAddress, balance1 - aliasFee, eff1 - aliasFee) + } + + test("Not able to create aliases to other addresses") { + val alias = "test_alias3" + + val (balance1, eff1) = notMiner.accountBalances(firstAddress) + val aliasFee = calcAliasFee(firstAddress, alias) + assertBadRequestAndMessage2(sender.createAlias(secondAddress, alias, transferFee), "already in the state") + notMiner.assertBalances(firstAddress, balance1 - aliasFee, eff1 - aliasFee) + } + + test("Able to create several different aliases to same addresses") { + val firstAlias = "test_alias4" + val secondAlias = "test_alias5" + + val (balance1, eff1) = notMiner.accountBalances(secondAddress) + + val aliasFeeFirstAlias = calcAliasFee(secondAddress, firstAlias) + notMiner.assertBalances(secondAddress, balance1 - aliasFeeFirstAlias, eff1 - aliasFeeFirstAlias) + + val aliasFeeSecondAlias = calcAliasFee(secondAddress, secondAlias) + notMiner.assertBalances(secondAddress, balance1 - aliasFeeFirstAlias - aliasFeeSecondAlias, eff1 - aliasFeeFirstAlias - aliasFeeSecondAlias) + + val aliasesList = sender.aliasByAddress(secondAddress) + aliasesList should contain allElementsOf Seq(fullAliasByAddress(secondAddress, firstAlias), fullAliasByAddress(secondAddress, secondAlias)) + + } + + val aliases_names = + Table("aliasName", + "aaaa", + "sixteen_chars_al", + "....", + "1234567890123456", + "@.@-@_@") + + aliases_names.foreach { alias => + test(s"create alias named $alias") { + val (balance1, eff1) = notMiner.accountBalances(secondAddress) + val aliasFee = calcAliasFee(secondAddress, alias) + notMiner.assertBalances(secondAddress, balance1 - aliasFee, eff1 - aliasFee) + } + } + + val invalid_aliases_names = + Table(("aliasName", "message"), + ("", "Alias '' length should be between 4 and 30"), + ("abc", "Alias 'abc' length should be between 4 and 30"), + (null, "failed to parse json message"), + ("morethen_thirtycharactersinline", "Alias 'morethen_thirtycharactersinline' length should be between 4 and 30"), + ("~!|#$%^&*()_+=\";:/?><|\\][{}", "Alias should contain only following characters: -.0123456789@_abcdefghijklmnopqrstuvwxyz"), + ("multilnetest\ntest", "Alias should contain only following characters: -.0123456789@_abcdefghijklmnopqrstuvwxyz"), + ("UpperCaseAliase", "Alias should contain only following characters: -.0123456789@_abcdefghijklmnopqrstuvwxyz")) + + forAll(invalid_aliases_names) { (alias: String, message: String) => + test(s"Not able to create alias named $alias") { + assertBadRequestAndMessage2(sender.createAlias(secondAddress, alias, transferFee), message) + } + } + + test("Able to lease by alias") { + val thirdAddressAlias = "leasing_alias" + + val (balance1, eff1) = notMiner.accountBalances(firstAddress) + val (balance3, eff3) = notMiner.accountBalances(thirdAddress) + + val aliasFee = calcAliasFee(thirdAddress, thirdAddressAlias) + val aliasFull = fullAliasByAddress(thirdAddress, thirdAddressAlias) + //lease maximum value, to pass next thirdAddress + val leasingAmount = balance1 - leasingFee - 0.5.waves + + val leasingTx = sender.lease(firstAddress, aliasFull, leasingAmount, leasingFee).id + nodes.waitForHeightAraiseAndTxPresent(leasingTx) + + notMiner.assertBalances(firstAddress, balance1 - leasingFee, eff1 - leasingAmount - leasingFee) + notMiner.assertBalances(thirdAddress, balance3 - aliasFee, eff3 - aliasFee + leasingAmount) + + } + + //previous test should not be commented to run this one + test("Not able to create aliase when insufficient funds") { + val alias = "test_alias7" + assertBadRequestAndMessage2(sender.createAlias(firstAddress, alias, transferFee), + "State check failed. Reason: negative effective balance") + } + + private def calcAliasFee(address: String, alias: String): Long = { + if (!sender.aliasByAddress(address).exists(_.endsWith(alias))){ + val aliasId = sender.createAlias(address, alias, transferFee).id + nodes.waitForHeightAraiseAndTxPresent(aliasId) + transferFee + } else 0 + } + + private def fullAliasByAddress(address: String, alias: String): String = { + sender.aliasByAddress(address).find(_.endsWith(alias)).get + } + +} From 241064021c7ed577a486f36da2e09ce046ead5be Mon Sep 17 00:00:00 2001 From: Nastya Urlapova Date: Fri, 2 Mar 2018 14:03:09 +0300 Subject: [PATCH 2/3] Update AliasTransactionSuite.scala Was tested on Testnet --- .../transactions/AliasTransactionSuite.scala | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/it/src/test/scala/com/wavesplatform/it/sync/transactions/AliasTransactionSuite.scala b/it/src/test/scala/com/wavesplatform/it/sync/transactions/AliasTransactionSuite.scala index b0870bc91cd..286bccdb566 100644 --- a/it/src/test/scala/com/wavesplatform/it/sync/transactions/AliasTransactionSuite.scala +++ b/it/src/test/scala/com/wavesplatform/it/sync/transactions/AliasTransactionSuite.scala @@ -4,6 +4,8 @@ import com.wavesplatform.it.api.SyncHttpApi._ import com.wavesplatform.it.transactions.BaseTransactionSuite import com.wavesplatform.it.util._ import org.scalatest.prop.TableDrivenPropertyChecks +import scala.util.Random + class AliasTransactionSuite extends BaseTransactionSuite with TableDrivenPropertyChecks { @@ -33,7 +35,7 @@ class AliasTransactionSuite extends BaseTransactionSuite with TableDrivenPropert } test("Not able to create same aliases to same address") { - val alias = "test_alias2" + val alias = randomAlias() val (balance1, eff1) = notMiner.accountBalances(firstAddress) val aliasFee = calcAliasFee(firstAddress, alias) notMiner.assertBalances(firstAddress, balance1 - aliasFee, eff1 - aliasFee) @@ -43,7 +45,7 @@ class AliasTransactionSuite extends BaseTransactionSuite with TableDrivenPropert } test("Not able to create aliases to other addresses") { - val alias = "test_alias3" + val alias = randomAlias() val (balance1, eff1) = notMiner.accountBalances(firstAddress) val aliasFee = calcAliasFee(firstAddress, alias) @@ -52,8 +54,8 @@ class AliasTransactionSuite extends BaseTransactionSuite with TableDrivenPropert } test("Able to create several different aliases to same addresses") { - val firstAlias = "test_alias4" - val secondAlias = "test_alias5" + val firstAlias = randomAlias() + val secondAlias = randomAlias() val (balance1, eff1) = notMiner.accountBalances(secondAddress) @@ -69,12 +71,11 @@ class AliasTransactionSuite extends BaseTransactionSuite with TableDrivenPropert } val aliases_names = - Table("aliasName", - "aaaa", - "sixteen_chars_al", - "....", - "1234567890123456", - "@.@-@_@") + Table(s"aliasName${randomAlias()}", + s"aaaa${randomAlias()}", + s"....${randomAlias()}", + s"1234567890.${randomAlias()}", + s"@.@-@_@${randomAlias()}") aliases_names.foreach { alias => test(s"create alias named $alias") { @@ -101,7 +102,7 @@ class AliasTransactionSuite extends BaseTransactionSuite with TableDrivenPropert } test("Able to lease by alias") { - val thirdAddressAlias = "leasing_alias" + val thirdAddressAlias = randomAlias() val (balance1, eff1) = notMiner.accountBalances(firstAddress) val (balance3, eff3) = notMiner.accountBalances(thirdAddress) @@ -121,7 +122,7 @@ class AliasTransactionSuite extends BaseTransactionSuite with TableDrivenPropert //previous test should not be commented to run this one test("Not able to create aliase when insufficient funds") { - val alias = "test_alias7" + val alias = randomAlias() assertBadRequestAndMessage2(sender.createAlias(firstAddress, alias, transferFee), "State check failed. Reason: negative effective balance") } @@ -137,5 +138,9 @@ class AliasTransactionSuite extends BaseTransactionSuite with TableDrivenPropert private def fullAliasByAddress(address: String, alias: String): String = { sender.aliasByAddress(address).find(_.endsWith(alias)).get } + + private def randomAlias(): String = { + s"testalias.${Random.alphanumeric take 9 mkString}".toLowerCase + } } From f9a46eafd5202c1c95979ee5d1a036cdfe04b3ab Mon Sep 17 00:00:00 2001 From: Nastya Urlapova Date: Fri, 2 Mar 2018 14:04:11 +0300 Subject: [PATCH 3/3] Update AliasTransactionSuite.scala --- .../it/sync/transactions/AliasTransactionSuite.scala | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/it/src/test/scala/com/wavesplatform/it/sync/transactions/AliasTransactionSuite.scala b/it/src/test/scala/com/wavesplatform/it/sync/transactions/AliasTransactionSuite.scala index 286bccdb566..38006349da2 100644 --- a/it/src/test/scala/com/wavesplatform/it/sync/transactions/AliasTransactionSuite.scala +++ b/it/src/test/scala/com/wavesplatform/it/sync/transactions/AliasTransactionSuite.scala @@ -7,7 +7,6 @@ import org.scalatest.prop.TableDrivenPropertyChecks import scala.util.Random - class AliasTransactionSuite extends BaseTransactionSuite with TableDrivenPropertyChecks { private val transferFee = 1.waves @@ -15,7 +14,7 @@ class AliasTransactionSuite extends BaseTransactionSuite with TableDrivenPropert private val transferAmount = 1.waves test("Able to send money to an alias") { - val alias = "test_alias" + val alias = randomAlias() val (balance1, eff1) = notMiner.accountBalances(firstAddress) val aliasFee = calcAliasFee(firstAddress, alias)