Filter transactions for onTransactionProcessed (#3990)

This commit is contained in:
rorp 2022-01-18 07:52:41 -08:00 committed by GitHub
parent ee0d62c5b8
commit 21c97bba12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 3 deletions

View File

@ -1,6 +1,8 @@
package org.bitcoins.wallet
import org.bitcoins.asyncutil.AsyncUtil
import org.bitcoins.core.api.wallet.db.SpendingInfoDb
import org.bitcoins.core.currency.Satoshis
import org.bitcoins.core.protocol.BitcoinAddress
import org.bitcoins.core.protocol.blockchain.{Block, RegTestNetChainParams}
import org.bitcoins.core.protocol.transaction.{EmptyTransaction, Transaction}
@ -8,6 +10,7 @@ import org.bitcoins.testkit.wallet.BitcoinSWalletTest
import org.bitcoins.testkit.wallet.FundWalletUtil.FundedWallet
import org.scalatest.FutureOutcome
import scala.concurrent.duration.DurationInt
import scala.concurrent.{Future, Promise}
class WalletCallbackTest extends BitcoinSWalletTest {
@ -62,9 +65,48 @@ class WalletCallbackTest extends BitcoinSWalletTest {
val wallet = fundedWallet.wallet
for {
_ <- wallet.processTransaction(EmptyTransaction, None)
address <- wallet.getNewAddress()
tx <- wallet.sendToAddress(address, Satoshis(1000), None)
_ <- wallet.processTransaction(tx, None)
result <- resultP.future
} yield assert(result == EmptyTransaction)
} yield assert(result == tx)
}
it must "verify OnTransactionProcessed callbacks are not executed for a transaction unrelated to the wallet" in {
fundedWallet: FundedWallet =>
val resultP: Promise[Transaction] = Promise()
val callback: OnTransactionProcessed = (tx: Transaction) => {
Future {
resultP.success(tx)
()
}
}
val callbacks = WalletCallbacks.onTransactionProcessed(callback)
fundedWallet.wallet.walletConfig.addCallbacks(callbacks)
val wallet = fundedWallet.wallet
// a random testnet transaction
val tx = Transaction.fromHex(
"""02000000000101c2cb8b4d16d2a111cfd2f44e674a89327cfb2dcad5828ec9ad12edb3972b2c
|b20100000000feffffff023ef929e900000000160014a5f44222c5859b388f513f07c96bdf69
|8a5a6bfd87c71d00000000001600145543e613b22f2393e76510cede73952405a5c9b9024730
|440220348dc443d9a0cc6b5365d7ef8d62e1ca4d890c6f4d817a0fb0f48ff36b97e08702201d
|77554641889932523e7d103385d99834cb9f29328ce11282ccbe218acf56440121028bb78dbe
|0ea469c97061b8dcc870ec25d5abcd938f19ec17e32422f8f318fa251b992000""".stripMargin)
for {
txno <- wallet.listTransactions().map(_.size)
_ <- wallet.processTransaction(tx, None)
_ <- AsyncUtil.nonBlockingSleep(50.millis)
txs <- wallet.listTransactions()
} yield {
assert(txs.size == txno)
assert(!resultP.isCompleted)
}
}
it must "verify OnTransactionBroadcast callbacks are executed" in {

View File

@ -375,7 +375,11 @@ private[bitcoins] trait TransactionProcessing extends WalletLogger {
logger.info(
s"Finished processing ${outgoing.length} spent outputs, it took=${TimeUtil.currentEpochMs - spentStart}ms")
}
_ <- walletCallbacks.executeOnTransactionProcessed(logger, transaction)
_ <-
// only notify about our transactions
if (incoming.nonEmpty || outgoing.nonEmpty)
walletCallbacks.executeOnTransactionProcessed(logger, transaction)
else Future.unit
} yield {
ProcessTxResult(incoming, outgoing)
}