From 06b8375c81ee95a0224cd9fca7ab2697eae0cb6c Mon Sep 17 00:00:00 2001 From: Torkel Rogstad Date: Thu, 20 Jun 2019 11:27:43 +0200 Subject: [PATCH] Add ProcessTransactionTest --- .../wallet/ProcessTransactionTest.scala | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 wallet-test/src/test/scala/org/bitcoins/wallet/ProcessTransactionTest.scala diff --git a/wallet-test/src/test/scala/org/bitcoins/wallet/ProcessTransactionTest.scala b/wallet-test/src/test/scala/org/bitcoins/wallet/ProcessTransactionTest.scala new file mode 100644 index 0000000000..dddd343233 --- /dev/null +++ b/wallet-test/src/test/scala/org/bitcoins/wallet/ProcessTransactionTest.scala @@ -0,0 +1,94 @@ +package org.bitcoins.wallet + +import org.bitcoins.testkit.wallet.BitcoinSWalletTest +import org.scalatest.FutureOutcome +import org.bitcoins.core.currency._ +import scala.concurrent.Future +import org.scalatest.compatible.Assertion +import org.bitcoins.wallet.api.UnlockedWalletApi +import org.bitcoins.rpc.client.common.BitcoindRpcClient + +class ProcessTransactionTest extends BitcoinSWalletTest { + override type FixtureParam = WalletWithBitcoind + + def withFixture(test: OneArgAsyncTest): FutureOutcome = { + withNewWalletAndBitcoind(test) + } + + behavior of "Wallet.processTransaction" + + /** Verifies that executing the given action doesn't change wallet state */ + private def checkUtxosAndBalance( + wallet: UnlockedWalletApi, + bitcoind: BitcoindRpcClient)(action: => Future[_]): Future[Assertion] = + for { + oldUtxos <- wallet.listUtxos() + oldUnconfirmed <- wallet.getUnconfirmedBalance() + oldConfirmed <- wallet.getBalance() + _ <- action // by name + newUtxos <- wallet.listUtxos() + newUnconfirmed <- wallet.getUnconfirmedBalance() + newConfirmed <- wallet.getBalance() + + } yield { + assert(oldConfirmed == newConfirmed) + assert(oldUnconfirmed == oldUnconfirmed) + assert(oldUtxos == newUtxos) + } + + it must "not change state when processing the same transaction twice" in { + walletAndBitcoind => + val WalletWithBitcoind(wallet, bitcoind) = walletAndBitcoind + + for { + address <- wallet.getNewAddress() + tx <- bitcoind + .sendToAddress(address, 1.bitcoin) + .flatMap(bitcoind.getRawTransactionRaw(_)) + + _ <- wallet.processTransaction(tx, confirmations = 0) + oldBalance <- wallet.getBalance() + oldUnconfirmed <- wallet.getUnconfirmedBalance() + + // repeating the action should not make a difference + _ <- checkUtxosAndBalance(wallet, bitcoind) { + wallet.processTransaction(tx, confirmations = 0) + } + + _ <- wallet.processTransaction(tx, confirmations = 3) + newBalance <- wallet.getBalance() + newUnconfirmed <- wallet.getUnconfirmedBalance() + + // repeating the action should not make a difference + _ <- checkUtxosAndBalance(wallet, bitcoind) { + wallet.processTransaction(tx, confirmations = 3) + } + } yield { + assert(newBalance != oldBalance) + assert(newUnconfirmed != oldUnconfirmed) + } + } + + it must "not change state when processing an unrelated transaction" in { + walletAndBitcoind => + val WalletWithBitcoind(wallet, bitcoind) = walletAndBitcoind + + for { + unrelated <- bitcoind.getNewAddress + .flatMap(bitcoind.sendToAddress(_, 1.bitcoin)) + .flatMap(bitcoind.getRawTransactionRaw(_)) + + _ <- checkUtxosAndBalance(wallet, bitcoind) { + wallet.processTransaction(unrelated, confirmations = 4) + } + + balance <- wallet.getBalance() + unconfirmed <- wallet.getUnconfirmedBalance() + + } yield { + assert(balance == 0.sats) + assert(unconfirmed == 0.sats) + } + + } +}