I modified one of Bitcoinj's examples
<https://github.com/bitcoinj/bitcoinj/blob/master/examples/src/main/java/org/bitcoinj/examples/FetchTransactions.java>
to
fetch a certain transaction from my local node's (using -regtest) mempool.
I created a transaction on my node:
./bitcoin-cli -regtest listtransactions
{
"account": "",
"address": "2Mvsc4i3pnmkeBmpx7WMffuHhoe4zxn4ZJ4",
"category": "send",
"amount": -10.00000000,
"label": "",
"vout": 0,
"fee": -0.00003760,
"confirmations": 0,
"trusted": true,
"txid": "
b03afb0ffcf57b7ff71a331c4b28c6511e512cb360704d20ec3ecb6a9acb114b",
"walletconflicts": [
],
"time": 1526671857,
"timereceived": 1526671857,
"bip125-replaceable": "no",
"abandoned": false
}
And see that the transaction is indeed in the mempool
./bitcoin-cli -regtest getrawmempool
[
"b03afb0ffcf57b7ff71a331c4b28c6511e512cb360704d20ec3ecb6a9acb114b"
]
I then fetch the transactions
public class FetchTransactions {
public static void main(String[] args) throws Exception {
BriefLogFormatter.init();
System.out.println("Connecting to node");
final NetworkParameters params = RegTestParams.get();
BlockStore blockStore = new MemoryBlockStore(params);
BlockChain chain = new BlockChain(params, blockStore);
PeerGroup peerGroup = new PeerGroup(params, chain);
peerGroup.start();
Peer localHostPeer = peerGroup.connectToLocalHost();
peerGroup.waitForPeers(1).get();
System.out.println("got peer");
String txId = args[0];
Sha256Hash txHash = Sha256Hash.wrap(txId);
PeerAddress localHostPeerAddress = localHostPeer.getAddress();
TxConfidenceTable table = Context.get().getConfidenceTable();
TransactionConfidence confidence = table.seen(txHash,
localHostPeerAddress);
TransactionConfidence confidence1 = table.get(txHash);
System.out.println(confidence);
System.out.println(confidence1);
ListenableFuture<Transaction> future =
localHostPeer.getPeerMempoolTransaction(txHash);
System.out.println("Waiting for node to send us the requested
transaction: " + txHash);
Transaction tx = future.get();
System.out.println(tx);
System.out.println("Waiting for node to send us the dependencies ...");
List<Transaction> deps = localHostPeer.downloadDependencies(tx).get();
for (Transaction dep : deps) {
System.out.println("Got dependency " + dep.getHashAsString());
}
System.out.println("Done.");
peerGroup.stop();
}
}
And get this output:
Connecting to node
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further
details.
got peer
Seen by 1 peer (most recently: 2018-05-18T20:14:31Z). Pending/unconfirmed.
Seen by 1 peer (most recently: 2018-05-18T20:14:31Z). Pending/unconfirmed.
Waiting for node to send us the requested transaction:
b03afb0ffcf57b7ff71a331c4b28c6511e512cb360704d20ec3ecb6a9acb114b
Exception in thread "main" java.util.concurrent.CancellationException: Task
was cancelled.
at
com.google.common.util.concurrent.AbstractFuture.cancellationExceptionWithCause(AbstractFuture.java:392)
at
com.google.common.util.concurrent.AbstractFuture$Sync.getValue(AbstractFuture.java:306)
at
com.google.common.util.concurrent.AbstractFuture$Sync.get(AbstractFuture.java:286)
at
com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:116)
at org.ian.bitcoin.main.FetchTransactions.main(FetchTransactions.java:73) FAILS
ON Transaction tx = future.get();
Caused by: java.util.concurrent.CancellationException: Future.cancel() was
called.
at
com.google.common.util.concurrent.AbstractFuture$Sync.complete(AbstractFuture.java:378)
at
com.google.common.util.concurrent.AbstractFuture$Sync.cancel(AbstractFuture.java:355)
at
com.google.common.util.concurrent.AbstractFuture.cancel(AbstractFuture.java:131)
at org.bitcoinj.core.Peer.processNotFoundMessage(Peer.java:642)
at org.bitcoinj.core.Peer.processMessage(Peer.java:487)
at
org.bitcoinj.core.PeerSocketHandler.receiveBytes(PeerSocketHandler.java:184)
at org.bitcoinj.net.ConnectionHandler.handleKey(ConnectionHandler.java:223)
at org.bitcoinj.net.NioClientManager.handleKey(NioClientManager.java:86)
at org.bitcoinj.net.NioClientManager.run(NioClientManager.java:122)
at
com.google.common.util.concurrent.AbstractExecutionThreadService$1$2.run(AbstractExecutionThreadService.java:60)
at com.google.common.util.concurrent.Callables$3.run(Callables.java:95)
at
org.bitcoinj.utils.ContextPropagatingThreadFactory$1.run(ContextPropagatingThreadFactory.java:49)
at java.lang.Thread.run(Thread.java:748)
Process finished with exit code 1
I don't understand how I can find my transaction in the TxConfidenceTable but
when I got to fetch it I get a NotFoundMessage message meaning the
transaction could not be found in my node's mempool. Where am I going wrong
here?
--
You received this message because you are subscribed to the Google Groups
"bitcoinj" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.