<p>Ok figured it out. The memory pool only holds transactions that have not been included in a block. The reason it stops working after a few minutes is the transaction is included in a block, and therefore the peer does not have it in its memory pool. </p>

<p>It seems I will need a different approach.</p>

CC BY-SA 3.0
2014-01-07T08:11:18.530Z
2014-01-07T08:11:18.530Z
Woozer
<p>I am trying to write an application that queries bitcoin nodes for the results of a particular transaction. I am attempting to imitate FetchTransactions.java, one of the examples that comes with bitcoinj. </p>

<p>Sometimes my requests go through to the peers, no problem. Other times, they get cancelled. I figured maybe I was stressing some of the peers, so I decided to loop through all the peers I could find and ask. Now I can't seem to get any peers to answer my request. I think I'm missing some basic network etiquette or something. Here's the relevant code:</p>

<pre><code>        NetworkParameters params = getNetworkParameters();
        PeerGroup peerGroup = getPeerGroup();
        peerGroup.startAndWait();
        peerGroup.addPeerDiscovery(new DnsDiscovery(params));
        peerGroup.setMaxConnections(100);

        peerGroup.waitForPeers(3).get();
        Peer peer;
        int i = 0;
        while (true)
        {
            try
            {
                while (i &gt;= peerGroup.numConnectedPeers())
                {
                    peerGroup.waitForPeers(peerGroup.numConnectedPeers() + 10);
                    System.out.println("Waiting to connect to more peers.");
                }

                System.out.println("Attempting to connect to peer " + i);
                System.out.println("numConnectedPeers = " + peerGroup.numConnectedPeers());
                peer = peerGroup.getConnectedPeers().get(i++);                      

                Sha256Hash txHash = new Sha256Hash(hash);
                ListenableFuture&lt;Transaction&gt; future = peer.getPeerMempoolTransaction(txHash);
                System.out.println("Waiting for node to send us the requested transaction: " + txHash);
                tx = future.get();
                System.out.println(tx);
                peerGroup.stopAndWait();
                break;
            }
            catch (CancellationException e)
            {
                System.out.println("Task was cancelled. Trying again.");
            }

        }
</code></pre>

<p>Also, when it hits 75 peers and tries to wait for more peers, it returns immediately. I don't understand why it doesn't wait until it gets more peers before returning, since the source code of PeerGroup.java indicates it should only return when it has as many peers as specified in the argument. </p>

<p>Also it has never worked on the testnet: requests for a transaction have always been cancelled on the testnet. </p>

<p>Anyways, any tips in the right direction would be much appreciated. </p>

<network><bitcoinj><nodes>
Why do my futures keep getting cancelled using BitcoinJ?
Woozer
# Why do my futures keep getting cancelled using BitcoinJ?

(Asked by: username_0 on 2014-01-04)

I am trying to write an application that queries bitcoin nodes for the results of a particular transaction. I am attempting to imitate FetchTransactions.java, one of the examples that comes with bitcoinj.

Sometimes my requests go through to the peers, no problem. Other times, they get cancelled. I figured maybe I was stressing some of the peers, so I decided to loop through all the peers I could find and ask. Now I can't seem to get any peers to answer my request. I think I'm missing some basic network etiquette or something. Here's the relevant code:

```
        NetworkParameters params = getNetworkParameters();
        PeerGroup peerGroup = getPeerGroup();
        peerGroup.startAndWait();
        peerGroup.addPeerDiscovery(new DnsDiscovery(params));
        peerGroup.setMaxConnections(100);

        peerGroup.waitForPeers(3).get();
        Peer peer;
        int i = 0;
        while (true)
        {
            try
            {
                while (i >= peerGroup.numConnectedPeers())
                {
                    peerGroup.waitForPeers(peerGroup.numConnectedPeers() + 10);
                    System.out.println("Waiting to connect to more peers.");
                }

                System.out.println("Attempting to connect to peer " + i);
                System.out.println("numConnectedPeers = " + peerGroup.numConnectedPeers());
                peer = peerGroup.getConnectedPeers().get(i++);                      

                Sha256Hash txHash = new Sha256Hash(hash);
                ListenableFuture<Transaction> future = peer.getPeerMempoolTransaction(txHash);
                System.out.println("Waiting for node to send us the requested transaction: " + txHash);
                tx = future.get();
                System.out.println(tx);
                peerGroup.stopAndWait();
                break;
            }
            catch (CancellationException e)
            {
                System.out.println("Task was cancelled. Trying again.");
            }

        }

```

Also, when it hits 75 peers and tries to wait for more peers, it returns immediately. I don't understand why it doesn't wait until it gets more peers before returning, since the source code of PeerGroup.java indicates it should only return when it has as many peers as specified in the argument.

Also it has never worked on the testnet: requests for a transaction have always been cancelled on the testnet.

Anyways, any tips in the right direction would be much appreciated.

**Tags:** network, bitcoinj, nodes


---

## 1 answers

### Answer by username_0 on 2014-01-07. Score: 2 | Accepted answer

Ok figured it out. The memory pool only holds transactions that have not been included in a block. The reason it stops working after a few minutes is the transaction is included in a block, and therefore the peer does not have it in its memory pool.

It seems I will need a different approach.


---

