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.addPeerDiscovery(new DnsDiscovery(params));

        Peer peer;
        int i = 0;
        while (true)
                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();
            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.

1 Answer 1


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.