CancelledKeyException when trying to connect to a local regtest net

130 views
Skip to first unread message

Dmitrii Pisarenko

unread,
Nov 6, 2021, 5:29:09 PM11/6/21
to bitcoinj
Hi!

I want to write a piece of code that will observe a particular address. I am using a local testnet, therefore the first step is to connect to it.

The testnet is created using docker-compose.yml:

version: '3'

services:
  node:
    image: ulamlabs/bitcoind-custom-regtest:latest
    ports:
      - "19001:19001"
      - "19000:19000"
      - "28332:28332"


I wrote this:

final WalletAppKit kit = new WalletAppKit(netParams, new File("."), "_minimalCryptoExchangeBtcWallet");
kit.connectToLocalHost();
kit.setAutoSave(true);
kit.startAsync();
kit.awaitRunning();


After kit.awaitRunning(); I am getting the error

java.nio.channels.CancelledKeyException: null
    at java.base/sun.nio.ch.SelectionKeyImpl.ensureValid(SelectionKeyImpl.java:71)
    at java.base/sun.nio.ch.SelectionKeyImpl.readyOps(SelectionKeyImpl.java:130)
    at java.base/java.nio.channels.SelectionKey.isWritable(SelectionKey.java:377)
    at org.bitcoinj.net.ConnectionHandler.handleKey(ConnectionHandler.java:244)
    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:66)
    at com.google.common.util.concurrent.Callables$4.run(Callables.java:119)
    at org.bitcoinj.utils.ContextPropagatingThreadFactory$1.run(ContextPropagatingThreadFactory.java:51)
at java.base/java.lang.Thread.run(Thread.java:830)


How can I fix this?

Thanks in advance

Dmitrii Pisarenko

P. S.: This question is also available on StackOverflow:


Andreas Schildbach

unread,
Nov 7, 2021, 12:49:58 PM11/7/21
to bitc...@googlegroups.com
The CancelledKeyException isn't a bad sign. It just happens if some peer
remotely disconnects. PeerGroup will connect the next peer in place.

I don't see the code that actually observes the address. Can you show it?
> --
> 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 bitcoinj+u...@googlegroups.com
> <mailto:bitcoinj+u...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/bitcoinj/904f8072-e0a0-4945-a69a-ed131f287a24n%40googlegroups.com
> <https://groups.google.com/d/msgid/bitcoinj/904f8072-e0a0-4945-a69a-ed131f287a24n%40googlegroups.com?utm_medium=email&utm_source=footer>.

Dmitrii Pisarenko

unread,
Nov 8, 2021, 2:32:59 AM11/8/21
to bitcoinj
Hello, Andreas!

Thanks for your answer.

> The CancelledKeyException isn't a bad sign. It just happens if some peer 
> remotely disconnects.

This code is supposed to connect to a locally running regtest network. If that network disconnects regularly, this is probably an error in the configuration of the test network, isn't it?

Below (and at https://github.com/ulamlabs/bitcoind-custom-regtest/blob/master/bitcoin.conf ) you can find the bitcoin.conf file being used. Do you see anything there that may make the peer inside this network disconnect all the time (and lead to CancelledKeyException)?

# testnet-box functionality
regtest=1
dnsseed=0
upnp=0

# always run a server, even with bitcoin-qt
server=1
rpcallowip=0.0.0.0/0
zmqpubrawtx=tcp://0.0.0.0:28332
zmqpubhashblock=tcp://0.0.0.0:28332
whitelist=0.0.0.0/0
txindex=1
addressindex=1
timestampindex=1
spentindex=1
uacomment=bitcore
rpcworkqueue=128
dbcache=16384

rpcuser=test
rpcpassword=test

# listen on different ports than default testnet
[regtest]
rpcbind=0.0.0.0
port=19000
rpcport=19001

> I don't see the code that actually observes the address. Can you show it? 

Sure, here it is:


public void init() {
        BriefLogFormatter.init();
        final LocalTestNetParams netParams = new LocalTestNetParams();
        netParams.setPort(CUR_PORT);


        try {
            final WalletAppKit kit = new WalletAppKit(netParams, new File("."), "_minimalCryptoExchangeBtcWallet");
            kit.connectToLocalHost();
            kit.setAutoSave(false);
            kit.startAsync();
            kit.awaitRunning();

            System.out.println("Port works");


            kit.peerGroup().addPeerDiscovery(new DnsDiscovery(netParams));


            kit.wallet().addWatchedAddress(Address.fromString(netParams, "2N23tWAFEtBtTgxNjBNmnwzsiPdLcNek181"));

            kit.wallet().addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
                @Override
                public void onCoinsReceived(final Wallet wallet, final Transaction transaction, final Coin prevBalance, final Coin newBalance) {
                    System.out.println("Heyo!");
                }
            });
        }
        catch (Exception exception) {
            exception.printStackTrace();
        }
    }

The method that is supposed to be called whenever money has been sent to 2N23tWAFEtBtTgxNjBNmnwzsiPdLcNek181 is onCoinsReceived.

Please note that until now I never got past kit.awaitRunning(). I terminated the application after that message. So I haven't tested the code after kit.awaitRunning() yet.

Feedback regarding errors in this code is highly appreciated as I never worked with Bitcoin/BitcoinJ before.

Thanks!

Dmitrii
Reply all
Reply to author
Forward
0 new messages