From 30e10451c4c071413bb0083ea57c5021d606c384 Mon Sep 17 00:00:00 2001 From: gagarin55 Date: Wed, 8 Jun 2016 21:00:17 +0300 Subject: [PATCH] /waves/external-payment return error if account balance not enough --- changelog.md | 2 + src/main/scala/scorex/waves/Application.scala | 48 ------------------- .../scorex/waves/http/WavesApiRoute.scala | 46 +++++++++--------- .../transaction/WavesTransactionModule.scala | 12 +++-- 4 files changed, 35 insertions(+), 73 deletions(-) diff --git a/changelog.md b/changelog.md index ddf8a330e0c..7b15fde340c 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,8 @@ **0.2.0-RC4** * Fixed issue with incorrect Handshake +* Balance with confirmations is the minimum balance +* /waves/external-payment returns error if account balance invalid **0.2.0-RC3** diff --git a/src/main/scala/scorex/waves/Application.scala b/src/main/scala/scorex/waves/Application.scala index b714440c653..a1d9997080a 100644 --- a/src/main/scala/scorex/waves/Application.scala +++ b/src/main/scala/scorex/waves/Application.scala @@ -91,52 +91,4 @@ object Application extends App with ScorexLogging { if (application.wallet.privateKeyAccounts().isEmpty) application.wallet.generateNewAccounts(1) - - def testingScript(application: Application): Unit = { - log.info("Going to execute testing scenario") - log.info("Current state is:" + application.blockStorage.state) - val wallet = application.wallet - - if (wallet.privateKeyAccounts().isEmpty) { - wallet.generateNewAccounts(3) - log.info("Generated Accounts:\n" + wallet.privateKeyAccounts().toList.map(_.address).mkString("\n")) - } - - log.info("Executing testing scenario with accounts" + - s"(${wallet.privateKeyAccounts().size}) : " - + wallet.privateKeyAccounts().mkString(" ")) - - require(wallet.privateKeyAccounts().nonEmpty) - - Thread.sleep(3.seconds.toMillis) - - val genesisBlock = application.blockStorage.history.genesis - val genesisAccs = genesisBlock.transactions.flatMap(_ match { - case gtx: GenesisTransaction => - Some(gtx.recipient) - case _ => - log.error("Non-genesis tx in the genesis block!") - None - }) - - def genPayment(recipient: Option[Account] = None, amtOpt: Option[Long] = None): Option[Transaction] = { - val pkAccs = wallet.privateKeyAccounts().ensuring(_.nonEmpty) - val senderAcc = pkAccs(Random.nextInt(pkAccs.size)) - val senderBalance = application.blockStorage.state.asInstanceOf[BalanceSheet].generationBalance(senderAcc) - val recipientAcc = recipient.getOrElse(pkAccs(Random.nextInt(pkAccs.size))) - val fee = Random.nextInt(5).toLong + 1 - if (senderBalance - fee > 0) { - val amt = amtOpt.getOrElse(Math.abs(Random.nextLong() % (senderBalance - fee))) - Some(application.transactionModule.createPayment(senderAcc, recipientAcc, amt, fee)) - } else None - } - - log.info("Generate 200 transactions") - (1 to 200) foreach (_ => genPayment()) - - (1 to Int.MaxValue).foreach { _ => - Thread.sleep(Random.nextInt(5.seconds.toMillis.toInt)) - log.info(s"Payment created: ${genPayment()}") - } - } } diff --git a/src/main/scala/scorex/waves/http/WavesApiRoute.scala b/src/main/scala/scorex/waves/http/WavesApiRoute.scala index f3bd320ca9f..eff612434b5 100644 --- a/src/main/scala/scorex/waves/http/WavesApiRoute.scala +++ b/src/main/scala/scorex/waves/http/WavesApiRoute.scala @@ -67,26 +67,28 @@ case class WavesApiRoute(override val application: Application)(implicit val con case err: JsError => WrongJson.json case JsSuccess(payment: ExternalPayment, _) => - val tx = transactionModule.broadcastPayment(payment) - if (!tx.signatureValid) - InvalidSignature.json - else { - tx.validate match { - case ValidationResult.ValidateOke => - tx.json + val txTry = transactionModule.broadcastPayment(payment) + if (txTry.isSuccess) { + val tx = txTry.get + if (!tx.signatureValid) + InvalidSignature.json + else { + tx.validate match { + case ValidationResult.ValidateOke => + tx.json - case ValidationResult.InvalidAddress => - InvalidAddress.json + case ValidationResult.InvalidAddress => + InvalidAddress.json - case ValidationResult.NegativeAmount => - NegativeAmount.json + case ValidationResult.NegativeAmount => + NegativeAmount.json - case ValidationResult.NegativeFee => - NegativeFee.json - - case ValidationResult.NoBalance => - NoBalance.json + case ValidationResult.NegativeFee => + NegativeFee.json + } } + } else { + NoBalance.json } } }.getOrElse(WrongJson.json).toString @@ -96,11 +98,11 @@ case class WavesApiRoute(override val application: Application)(implicit val con } } -// -// // Workaround to show datatype of post request without using it in another route -// // Related: https://github.com/swagger-api/swagger-core/issues/606 -// // Why is this still showing even though it's set to hidden? See https://github.com/martypitt/swagger-springmvc/issues/447 -// @ApiOperation(value = "IGNORE", notes = "", hidden = true, httpMethod = "GET", response = classOf[Payment]) -// protected def paymentModel = Unit + // + // // Workaround to show datatype of post request without using it in another route + // // Related: https://github.com/swagger-api/swagger-core/issues/606 + // // Why is this still showing even though it's set to hidden? See https://github.com/martypitt/swagger-springmvc/issues/447 + // @ApiOperation(value = "IGNORE", notes = "", hidden = true, httpMethod = "GET", response = classOf[Payment]) + // protected def paymentModel = Unit } \ No newline at end of file diff --git a/src/main/scala/scorex/waves/transaction/WavesTransactionModule.scala b/src/main/scala/scorex/waves/transaction/WavesTransactionModule.scala index a95c2072d96..c6f235d498a 100644 --- a/src/main/scala/scorex/waves/transaction/WavesTransactionModule.scala +++ b/src/main/scala/scorex/waves/transaction/WavesTransactionModule.scala @@ -1,6 +1,7 @@ package scorex.waves.transaction import scorex.account.{Account, PrivateKeyAccount, PublicKeyAccount} +import scorex.api.http.NoBalance import scorex.app.Application import scorex.block.BlockField import scorex.crypto.encode.Base58 @@ -8,6 +9,8 @@ import scorex.settings.Settings import scorex.transaction._ import scorex.utils.NTP +import scala.util.{Failure, Success, Try} + /** * Waves Transaction Module */ @@ -24,16 +27,19 @@ class WavesTransactionModule(implicit override val settings: TransactionSettings /** * Publish signed payment transaction which generated outside node */ - def broadcastPayment(externalPayment: ExternalPayment): PaymentTransaction = { + def broadcastPayment(externalPayment: ExternalPayment): Try[PaymentTransaction] = { val time = externalPayment.timestamp val sigBytes = Base58.decode(externalPayment.signature).get val senderPubKey = Base58.decode(externalPayment.senderPublicKey).get val recipientAccount = new Account(externalPayment.recipient) val payment = new PaymentTransaction(new PublicKeyAccount(senderPubKey), recipientAccount, externalPayment.amount, externalPayment.fee, time, sigBytes) - if (blockStorage.state.isValid(payment)) + if (blockStorage.state.isValid(payment)) { onNewOffchainTransaction(payment) - payment + Success(payment) + } else { + Failure(new Exception(NoBalance.message)) + } } override def genesisData: BlockField[SimpleTransactionModule.StoredInBlock] = {