Implemented the ability to fetch unconfirmed transactions (#3429)

* relay flag in version message made configurable

* modified test classes as well

* Update configuration.md

* reverted few files and cleaned up format

* fixed mistake

* enabled neutrino node to receive transactions as a callback

* added txid to log message

* changed txId to txIdBE

* unit test added for checking if we're getting transaction as a callback
This commit is contained in:
Shruthii RG 2021-07-23 23:31:07 +05:30 committed by GitHub
parent 1032669f21
commit e2c2798e60
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 1 deletions

View file

@ -219,6 +219,7 @@ class BitcoinSServerMain(override val serverArgParser: ServerArgParser)(implicit
nodeConf: NodeAppConfig,
ec: ExecutionContext): Future[NodeCallbacks] = {
lazy val onTx: OnTxReceived = { tx =>
logger.info(s"Receiving transaction txid=${tx.txIdBE.hex} as a callback")
wallet.processTransaction(tx, blockHashOpt = None).map(_ => ())
}
lazy val onCompactFilters: OnCompactFiltersReceived = { blockFilters =>
@ -243,7 +244,8 @@ class BitcoinSServerMain(override val serverArgParser: ServerArgParser)(implicit
onBlockHeadersReceived = Vector(onHeaders)))
case NodeType.NeutrinoNode =>
Future.successful(
NodeCallbacks(onBlockReceived = Vector(onBlock),
NodeCallbacks(onTxReceived = Vector(onTx),
onBlockReceived = Vector(onBlock),
onCompactFiltersReceived = Vector(onCompactFilters),
onBlockHeadersReceived = Vector(onHeaders)))
case NodeType.FullNode =>

View file

@ -0,0 +1,73 @@
package org.bitcoins.node.networking.peer
import org.bitcoins.core.currency.BitcoinsInt
import org.bitcoins.core.p2p.TransactionMessage
import org.bitcoins.core.protocol.transaction.Transaction
import org.bitcoins.node.OnTxReceived
import org.bitcoins.node.NodeCallbacks
import org.bitcoins.server.BitcoinSAppConfig
import org.bitcoins.testkit.BitcoinSTestAppConfig
import org.bitcoins.testkit.node.{
NeutrinoNodeFundedWalletBitcoind,
NodeTestWithCachedBitcoindNewest
}
import org.scalatest.{FutureOutcome, Outcome}
import scala.concurrent.{Future, Promise}
class DataMessageHandlerNeutrinoNodesTest
extends NodeTestWithCachedBitcoindNewest {
/** Wallet config with data directory set to user temp directory */
override protected def getFreshConfig: BitcoinSAppConfig =
BitcoinSTestAppConfig.getNeutrinoWithEmbeddedDbTestConfig(pgUrl)
override type FixtureParam = NeutrinoNodeFundedWalletBitcoind
def withFixture(test: OneArgAsyncTest): FutureOutcome = {
val outcomeF: Future[Outcome] = for {
bitcoind <- cachedBitcoindWithFundsF
outcome = withNeutrinoNodeFundedWalletBitcoind(
test = test,
bip39PasswordOpt = getBIP39PasswordOpt(),
bitcoind = bitcoind
)(system, getFreshConfig)
f <- outcome.toFuture
} yield f
new FutureOutcome(outcomeF)
}
it must "verify OnTxReceived callbacks are executed" in {
param: FixtureParam =>
val NeutrinoNodeFundedWalletBitcoind(node, _, bitcoind, _) = param
val resultP: Promise[Transaction] = Promise()
val callback: OnTxReceived = (tx: Transaction) => {
Future {
resultP.success(tx)
()
}
}
val sender = node.peerMsgSenders(0)
for {
txId <- bitcoind.sendToAddress(junkAddress, 1.bitcoin)
tx <- bitcoind.getRawTransactionRaw(txId)
payload = TransactionMessage(tx)
nodeCallbacks = NodeCallbacks.onTxReceived(callback)
_ = node.nodeAppConfig.addCallbacks(nodeCallbacks)
dataMessageHandler =
DataMessageHandler(genesisChainApi)(node.executionContext,
node.nodeAppConfig,
node.chainConfig)
_ <- dataMessageHandler.handleDataPayload(payload, sender)
result <- resultP.future
} yield assert(result == tx)
}
}

View file

@ -86,6 +86,7 @@ object BitcoinSTestAppConfig {
|bitcoin-s {
| node {
| mode = neutrino
| relay = true
| }
| proxy.enabled = ${torEnabled}
|}
@ -103,6 +104,7 @@ object BitcoinSTestAppConfig {
|bitcoin-s {
| node {
| mode = neutrino
| relay = true
| }
| proxy.enabled = ${torEnabled}
|}