Skip to content
Advertisement

How can I get notified when money has been sent to a particular Bitcoin address on a local regtest network?

I want to programmatically detect whenever someone sends Bitcoin to some address. This happens on a local testnet which I start using this docker-compose.yml file.

Once the local testnet runs, I create a new address using

JavaScript

Let’s say it returns 2N23tWAFEtBtTgxNjBNmnwzsiPdLcNek181.

I put this address into the following Java code:

JavaScript

Then I start the Java application with this class.

Then I send some test Bitcoin to the address in question:

JavaScript

If I go to http://localhost:3002/tx/068c377bab961356ad9a3919229a764aa929711c68aefd5dbd4c7c348eef3406, I see that the transaction details.

Screenshot of the transaction page

However, the breakpoint in the listener (onCoinsReceived method) never activates.

How do I need to modify my code and/or the commands I use to send test BTC so that whenever money is received by that account, onCoinsReceived method is called? Is there a place where I can tell Wallet or NetworkParameters that I want to connect to localhost?

I am using version 0.15.10 of bitcoinj-core.

Update 1:

I modified docker-compose.yml and added following port mappings:

JavaScript

Then I rewrote the init method so that I can connect to localhost and specify the port:

JavaScript

LocalTestNetParams allows to specify the port:

JavaScript

I tried all of the aforementioned ports in netParams.setPort(50001);.

In all cases I get the following messages after kit.awaitRunning();:

JavaScript

10.10.1.218 seems to be generated by InetAddress.getLocalHost() in org.bitcoinj.kits.WalletAppKit#connectToLocalHost:

JavaScript

Update 1:

I tried to use network_mode: "host".

If I add it to node as in

JavaScript

I get the following error when I run docker-compose up -d:

JavaScript

If I add it to electrumx part as in

JavaScript

I get another error:

JavaScript

Update 2:

If I comment out port bindings as in

JavaScript

and run docker-compose up -d I get

JavaScript

Update 3: I assume that the root of the error is that in my Java code I try to connect to the ElectrumX server instead of the actual Bitcoin node (node in docker-compose.yml).

Update 4:

I changed docker-compose.yml as follows:

JavaScript

Now I am getting different errors (full log available here):

JavaScript

Update 5:

Someone suggested (in a now removed comment) that in the output of the application there is this Peer does not support bloom filtering message:

JavaScript

So I tried to fork the original image and change the bitcoin.conf file to enable Bloom filtering:

JavaScript

When I run docker build -t mentiflectax/bitcoind-custom-regtest:latest . I get the following error message (part of remaining output can be found here):

JavaScript

Update 6: The correct port seems to be 19000.

If I use port 19001, I get following errors after kit.awaitRunning():

JavaScript

Full log output is available here.

Advertisement

Answer

I haven’t tested your full setup with electrumx and the ethereum stuff present in your docker-compose file, but regarding your problem, the following steps worked properly, and I think it will do as well in your complete setup.

I ran with docker a bitcoin node based in the ulamlabs/bitcoind-custom-regtest:latest image you provided:

JavaScript

As you can see, I exposed the image internal port 19000 as the default port for RegTestParams, 18444. From the point of view of our client, with this setup, basically it will look like as if we were running the bitcoin daemon in the host. Using your LocalTestNetParams class and providing the port 19000 as you indicated should do the trick as well.

Then, according to the feedback you provided in the question, I manually edited the daemon configuration of the bitcoin node in /root/.bitcoin/bitcoin.conf using bash and vi:

JavaScript

And included the following configuration:

JavaScript

After restart the container, I got a new address:

JavaScript

Let’s assume that the new address is the one you provided in the question:

JavaScript

Then, as suggested in the Bitcoin documentation, in order to avoid an insufficient funds error, I generated 101 blocks to this address:

JavaScript

I used generatetoaddress and not generate because since Bitcoin 0.19.0 the option is no longer valid.

Next, I prepared a simple Java program, based in the information you provided and this example from the Bitcoinj library documentation:

JavaScript

I used a simple while loop to keep the problem running; of course, if will be probably unnecessary in an actual setup as it seems you are using Spring Boot.

Then, if you send some bitcoins to this address:

JavaScript

The listener should be invoked:

JavaScript

There is still a problem you mentioned that I haven’t had the opportunity to test, and it is creating a new Docker image in which the peerbloomfilters configuration would be configured properly without modifying the actual container state. I think the compilation problem you indicated could be related to this issue, basically, that the container didn’t have enough resources to perform the process. If you are using macOS and Docker for Mac, try tweaking the amount of memory available to your containers, it may be of help. A change in the base alpine image used can motivate the problem also. I will try digging into the issue as well.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement