From d160119e4771dadbb068d362a18279083a786b28 Mon Sep 17 00:00:00 2001 From: "Nastya.Urlapova" Date: Thu, 5 Apr 2018 13:12:21 +0300 Subject: [PATCH] NODE-674: LeasingTransaction to sync way of implementation --- .../LeasingTransactionsSuite.scala | 183 ------------------ .../LeasingTransactionsSuite.scala | 130 +++++++++++++ 2 files changed, 130 insertions(+), 183 deletions(-) delete mode 100644 it/src/test/scala/com/wavesplatform/it/async/transactions/LeasingTransactionsSuite.scala create mode 100644 it/src/test/scala/com/wavesplatform/it/sync/transactions/LeasingTransactionsSuite.scala diff --git a/it/src/test/scala/com/wavesplatform/it/async/transactions/LeasingTransactionsSuite.scala b/it/src/test/scala/com/wavesplatform/it/async/transactions/LeasingTransactionsSuite.scala deleted file mode 100644 index bff5e40bc10..00000000000 --- a/it/src/test/scala/com/wavesplatform/it/async/transactions/LeasingTransactionsSuite.scala +++ /dev/null @@ -1,183 +0,0 @@ -package com.wavesplatform.it.async.transactions - -import com.wavesplatform.it.api.AsyncHttpApi._ -import com.wavesplatform.it.api._ -import com.wavesplatform.it.transactions.BaseTransactionSuite -import com.wavesplatform.it.util._ -import org.scalatest.CancelAfterFailure -import play.api.libs.json.Json -import scorex.transaction.lease.LeaseTransaction - -import scala.concurrent.Await -import scala.concurrent.Future.traverse -import scala.concurrent.duration._ - -class LeasingTransactionsSuite extends BaseTransactionSuite with CancelAfterFailure { - - private val waitCompletion = 2.minutes - private val defaultFee = 2.waves - private val leasingAmount = 5.waves - - test("leasing waves decreases lessor's eff.b. and increases lessee's eff.b.; lessor pays fee") { - val f = for { - height <- traverse(nodes)(_.height).map(_.max) - _ <- traverse(nodes)(_.waitForHeight(height + 1)) - ((firstBalance, firstEffBalance), (secondBalance, secondEffBalance)) <- notMiner - .accountBalances(firstAddress) - .zip(notMiner.accountBalances(secondAddress)) - - createdLeaseTxId <- sender.lease(firstAddress, secondAddress, leasingAmount, fee = defaultFee).map(_.id) - _ <- nodes.waitForHeightAriseAndTxPresent(createdLeaseTxId) - _ <- notMiner - .assertBalances(firstAddress, firstBalance - defaultFee, firstEffBalance - leasingAmount - defaultFee) - .zip(notMiner.assertBalances(secondAddress, secondBalance, secondEffBalance + leasingAmount)) - } yield succeed - - Await.result(f, waitCompletion) - } - - test("can not make leasing without having enough waves") { - val f = for { - fb <- traverse(nodes)(_.height).map(_.min) - - ((firstBalance, firstEffBalance), (secondBalance, secondEffBalance)) <- notMiner - .accountBalances(firstAddress) - .zip(notMiner.accountBalances(secondAddress)) - - //secondAddress effective balance more than general balance - leaseFailureAssertion <- assertBadRequest(sender.lease(secondAddress, firstAddress, secondBalance + 1.waves, defaultFee)) - - _ <- traverse(nodes)(_.waitForHeight(fb + 2)) - - _ <- notMiner - .assertBalances(firstAddress, firstBalance, firstEffBalance) - .zip(notMiner.assertBalances(secondAddress, secondBalance, secondEffBalance)) - } yield leaseFailureAssertion - - Await.result(f, waitCompletion) - } - - test("can not make leasing without having enough waves for fee") { - val f = for { - fb <- traverse(nodes)(_.height).map(_.min) - - ((firstBalance, firstEffBalance), (secondBalance, secondEffBalance)) <- notMiner - .accountBalances(firstAddress) - .zip(notMiner.accountBalances(secondAddress)) - - transferFailureAssertion <- assertBadRequest(sender.lease(firstAddress, secondAddress, firstEffBalance, fee = defaultFee)) - - _ <- traverse(nodes)(_.waitForHeight(fb + 2)) - - _ <- notMiner - .assertBalances(firstAddress, firstBalance, firstEffBalance) - .zip(notMiner.assertBalances(secondAddress, secondBalance, secondEffBalance)) - } yield transferFailureAssertion - - Await.result(f, waitCompletion) - } - - test("lease cancellation reverts eff.b. changes; lessor pays fee for both lease and cancellation") { - import LeaseTransaction.Status._ - def status(txId: String) = sender.get(s"/transactions/info/$txId").map { r => - (Json.parse(r.getResponseBody) \ "status").as[String] - } - def activeLeases(address: String) = sender.get(s"/leasing/active/$address").as[Seq[Transaction]] - - val f = for { - ((firstBalance, firstEffBalance), (secondBalance, secondEffBalance)) <- notMiner - .accountBalances(firstAddress) - .zip(notMiner.accountBalances(secondAddress)) - - createdLeaseTxId <- sender.lease(firstAddress, secondAddress, leasingAmount, fee = defaultFee).map(_.id) - - _ <- nodes.waitForHeightAriseAndTxPresent(createdLeaseTxId) - _ <- notMiner - .assertBalances(firstAddress, firstBalance - defaultFee, firstEffBalance - leasingAmount - defaultFee) - .zip(notMiner.assertBalances(secondAddress, secondBalance, secondEffBalance + leasingAmount)) - - status1 <- status(createdLeaseTxId) - _ = assert(status1 == Active) - - leases0 <- activeLeases(secondAddress) - _ = assert(leases0.forall(!_.sender.contains(secondAddress))) - - leases1 <- activeLeases(firstAddress) - _ = assert(leases1.exists(_.id == createdLeaseTxId)) - - createdCancelLeaseTxId <- sender.cancelLease(firstAddress, createdLeaseTxId, fee = defaultFee).map(_.id) - _ <- nodes.waitForHeightAriseAndTxPresent(createdCancelLeaseTxId) - _ <- notMiner - .assertBalances(firstAddress, firstBalance - 2 * defaultFee, firstEffBalance - 2 * defaultFee) - .zip(notMiner.assertBalances(secondAddress, secondBalance, secondEffBalance)) - - status2 <- status(createdLeaseTxId) - _ = assert(status2 == Canceled) - - leases2 <- activeLeases(firstAddress) - _ = assert(leases2.forall(_.id != createdLeaseTxId)) - _ = assert(leases2.size == leases1.size - 1) - } yield succeed - - Await.result(f, waitCompletion) - } - - test("lease cancellation can be done only once") { - val f = for { - ((firstBalance, firstEffBalance), (secondBalance, secondEffBalance)) <- notMiner - .accountBalances(firstAddress) - .zip(notMiner.accountBalances(secondAddress)) - - createdLeasingTxId <- sender.lease(firstAddress, secondAddress, leasingAmount, fee = defaultFee).map(_.id) - _ <- nodes.waitForHeightAriseAndTxPresent(createdLeasingTxId) - _ <- notMiner - .assertBalances(firstAddress, firstBalance - defaultFee, firstEffBalance - leasingAmount - defaultFee) - .zip(notMiner.assertBalances(secondAddress, secondBalance, secondEffBalance + leasingAmount)) - - createdCancelLeaseTxId <- sender.cancelLease(firstAddress, createdLeasingTxId, fee = defaultFee).map(_.id) - _ <- nodes.waitForHeightAriseAndTxPresent(createdCancelLeaseTxId) - _ <- assertBadRequest(sender.cancelLease(firstAddress, createdLeasingTxId, fee = defaultFee).map(_.id)) - _ <- notMiner - .assertBalances(firstAddress, firstBalance - 2 * defaultFee, firstEffBalance - 2 * defaultFee) - .zip(notMiner.assertBalances(secondAddress, secondBalance, secondEffBalance)) - } yield succeed - - Await.result(f, waitCompletion) - } - - test("only sender can cancel lease transaction") { - val f = for { - ((firstBalance, firstEffBalance), (secondBalance, secondEffBalance)) <- notMiner - .accountBalances(firstAddress) - .zip(notMiner.accountBalances(secondAddress)) - - createdLeaseTxId <- sender.lease(firstAddress, secondAddress, leasingAmount, fee = defaultFee).map(_.id) - - height <- traverse(nodes)(_.height).map(_.max) - _ <- traverse(nodes)(_.waitForHeight(height + 1)) - _ <- traverse(nodes)(_.waitForTransaction(createdLeaseTxId)) - - _ <- notMiner - .assertBalances(firstAddress, firstBalance - defaultFee, firstEffBalance - leasingAmount - defaultFee) - .zip(notMiner.assertBalances(secondAddress, secondBalance, secondEffBalance + leasingAmount)) - - _ <- assertBadRequest(sender.cancelLease(thirdAddress, createdLeaseTxId, fee = defaultFee)) - } yield succeed - - Await.result(f, waitCompletion) - } - - test("can not make leasing without having enough your waves to self") { - val f = for { - fb <- traverse(nodes)(_.height).map(_.min) - - (firstBalance, firstEffBalance) <- notMiner.accountBalances(firstAddress) - transferFailureAssertion <- assertBadRequest(sender.lease(firstAddress, firstAddress, firstBalance + 1.waves, fee = defaultFee)) - _ <- traverse(nodes)(_.waitForHeight(fb + 2)) - _ <- notMiner.assertBalances(firstAddress, firstBalance, firstEffBalance) - } yield transferFailureAssertion - - Await.result(f, waitCompletion) - } - -} diff --git a/it/src/test/scala/com/wavesplatform/it/sync/transactions/LeasingTransactionsSuite.scala b/it/src/test/scala/com/wavesplatform/it/sync/transactions/LeasingTransactionsSuite.scala new file mode 100644 index 00000000000..f279ab676dc --- /dev/null +++ b/it/src/test/scala/com/wavesplatform/it/sync/transactions/LeasingTransactionsSuite.scala @@ -0,0 +1,130 @@ +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.CancelAfterFailure +import play.api.libs.json.Json +import scorex.transaction.lease.LeaseTransaction + +class LeasingTransactionsSuite extends BaseTransactionSuite with CancelAfterFailure { + + private val defaultFee = 2.waves + private val leasingAmount = 5.waves + + test("leasing waves decreases lessor's eff.b. and increases lessee's eff.b.; lessor pays fee") { + val (balance1, eff1) = notMiner.accountBalances(firstAddress) + val (balance2, eff2) = notMiner.accountBalances(secondAddress) + + val createdLeaseTxId = sender.lease(firstAddress, secondAddress, leasingAmount, leasingFee = defaultFee).id + nodes.waitForHeightAriseAndTxPresent(createdLeaseTxId) + + notMiner.assertBalances(firstAddress, balance1 - defaultFee, eff1 - leasingAmount - defaultFee) + notMiner.assertBalances(secondAddress, balance2, eff2 + leasingAmount) + + } + + test("can not make leasing without having enough balance") { + val (balance1, eff1) = notMiner.accountBalances(firstAddress) + val (balance2, eff2) = notMiner.accountBalances(secondAddress) + + //secondAddress effective balance more than general balance + assertBadRequestAndResponse(sender.lease(secondAddress, firstAddress, balance2 + 1.waves, defaultFee), "Reason: Cannot lease more than own") + nodes.waitForHeightArise() + + notMiner.assertBalances(firstAddress, balance1, eff1) + notMiner.assertBalances(secondAddress, balance2, eff2) + } + + test("can not make leasing without having enough waves for fee") { + val (balance1, eff1) = notMiner.accountBalances(firstAddress) + val (balance2, eff2) = notMiner.accountBalances(secondAddress) + + assertBadRequestAndResponse(sender.lease(firstAddress, secondAddress, balance1, defaultFee), "Reason: Cannot lease more than own") + nodes.waitForHeightArise() + + notMiner.assertBalances(firstAddress, balance1, eff1) + notMiner.assertBalances(secondAddress, balance2, eff2) + } + + test("lease cancellation reverts eff.b. changes; lessor pays fee for both lease and cancellation") { + import LeaseTransaction.Status._ + + def getStatus(txId: String): String = { + val r = sender.get(s"/transactions/info/$txId") + (Json.parse(r.getResponseBody) \ "status").as[String] + } + + val (balance1, eff1) = notMiner.accountBalances(firstAddress) + val (balance2, eff2) = notMiner.accountBalances(secondAddress) + + val createdLeaseTxId = sender.lease(firstAddress, secondAddress, leasingAmount, defaultFee).id + nodes.waitForHeightAriseAndTxPresent(createdLeaseTxId) + + notMiner.assertBalances(firstAddress, balance1 - defaultFee, eff1 - leasingAmount - defaultFee) + notMiner.assertBalances(secondAddress, balance2, eff2 + leasingAmount) + + val status1 = getStatus(createdLeaseTxId) + status1 shouldBe Active + + val activeLeases = sender.activeLeases(secondAddress) + assert(activeLeases.forall(!_.sender.contains(secondAddress))) + + val leases1 = sender.activeLeases(firstAddress) + assert(leases1.exists(_.id == createdLeaseTxId)) + + val createdCancelLeaseTxId = sender.cancelLease(firstAddress, createdLeaseTxId, defaultFee).id + nodes.waitForHeightAriseAndTxPresent(createdCancelLeaseTxId) + + notMiner.assertBalances(firstAddress, balance1 - 2 * defaultFee, eff1 - 2 * defaultFee) + notMiner.assertBalances(secondAddress, balance2, eff2) + + val status2 = getStatus(createdLeaseTxId) + status2 shouldBe Canceled + + val leases2 = sender.activeLeases(firstAddress) + assert(leases2.forall(_.id != createdLeaseTxId)) + + leases2.size shouldBe leases1.size - 1 + } + + test("lease cancellation can be done only once") { + val (balance1, eff1) = notMiner.accountBalances(firstAddress) + val (balance2, eff2) = notMiner.accountBalances(secondAddress) + + val createdLeasingTxId = sender.lease(firstAddress, secondAddress, leasingAmount, defaultFee).id + nodes.waitForHeightAriseAndTxPresent(createdLeasingTxId) + + notMiner.assertBalances(firstAddress, balance1 - defaultFee, eff1 - leasingAmount - defaultFee) + notMiner.assertBalances(secondAddress, balance2, eff2 + leasingAmount) + + val createdCancelLeaseTxId = sender.cancelLease(firstAddress, createdLeasingTxId, defaultFee).id + nodes.waitForHeightAriseAndTxPresent(createdCancelLeaseTxId) + + assertBadRequestAndResponse(sender.cancelLease(firstAddress, createdLeasingTxId, defaultFee), "Reason: Cannot cancel already cancelled lease") + + notMiner.assertBalances(firstAddress, balance1 - 2 * defaultFee, eff1 - 2 * defaultFee) + notMiner.assertBalances(secondAddress, balance2, eff2) + } + + test("only sender can cancel lease transaction") { + val (balance1, eff1) = notMiner.accountBalances(firstAddress) + val (balance2, eff2) = notMiner.accountBalances(secondAddress) + + val createdLeaseTxId = sender.lease(firstAddress, secondAddress, leasingAmount, leasingFee = defaultFee).id + nodes.waitForHeightAriseAndTxPresent(createdLeaseTxId) + + notMiner.assertBalances(firstAddress, balance1 - defaultFee, eff1 - leasingAmount - defaultFee) + notMiner.assertBalances(secondAddress, balance2, eff2 + leasingAmount) + + assertBadRequestAndResponse(sender.cancelLease(thirdAddress, createdLeaseTxId, defaultFee), "LeaseTransaction was leased by other sender") + } + + test("can not make leasing without having enough your waves to self") { + val (balance1, eff1) = notMiner.accountBalances(firstAddress) + assertBadRequestAndResponse(sender.lease(firstAddress, firstAddress, balance1 + 1.waves, defaultFee), "Transaction to yourself") + nodes.waitForHeightArise() + + notMiner.assertBalances(firstAddress, balance1, eff1) + } +}