bsv-p2p
bsv-p2p copied to clipboard
Support to regtest environment
I've created a small program to connect to libsv/regtest-stack node. I've used Regtest's magic number in the constructor call.
I've tried to listen block event, but it is not fired. The events that are generated are "inv" "and block_hashes" only when i generate new blocks in the node. Is it that ok?
In the constants.js file there are no entries for Regtest magic_nums, I think it would be good to have Regtest in those constant objects.
I just added a REGTEST ticker for the network magic const https://github.com/kevinejohn/bsv-p2p/blob/b55e925bbe22bd17e8b4f4fb67869bccccc55826/src/config.ts#L7
I also just pushed a change that may fix the block event issue you're having. Try out v1.1.4+ and let me know if it works. You can call this to auto download all newly announced blocks on the network:
peer.fetchNewBlocks((hashes) => hashes)
Kevin
- don't you have to add a REGTEST entry into the VERSIONS object?
- peer.fetchMempoolTxs((txids) => txids); had the same issue? I didn't try it yet.
after updating the library, it fails to connect ` bsv-p2p: Connecting to localhost:18333 node_modules/bsv-p2p/lib/index.js:338 bsv-p2p: Connected to localhost:18333 node_modules/bsv-p2p/lib/index.js:345 bsv-p2p: Sent message version 98 bytes node_modules/bsv-p2p/lib/index.js:68 bsv-p2p: Socket disconnected localhost:18333 node_modules/bsv-p2p/lib/index.js:366 bsv-p2p: Disconnected from localhost:18333 node_modules/bsv-p2p/lib/index.js:427
/home/mhamra/bsv/prueba/node_modules/bsv-p2p/src/index.ts:477
reject(Error(disconnected (end)
)); node_modules/source-map-support/source-map-support.js:496
^
Error: disconnected (end) (/home/mhamra/bsv/prueba/node_modules/bsv-p2p/src/index.ts:477:18)
at Socket.
If there isn't a ticker match for version or user_agent then it defaults to this below. You can also set it yourself in the constructor.
https://github.com/kevinejohn/bsv-p2p/blob/312dc97084193c2e1208ebb4fc25da6d8cd899c6/src/config.ts#L17
It is possible that libsv uses a different regtest magic than the original Bitcoin Core magic. It's also possible that it doesn't accept the version message sent and disconnects after seeing it. Worth reading the version message of that node and copying it: peer.on("version", ({ version }) => console.log(version))
The strange thing is that my program was connecting ok and suddenly stopped working. I was working in another computer and it happened the same. In the node log I see this message:
[net] connection from 172.23.0.1:47244 dropped (banned)
I've changed the bantime parameter to 1, set debug to 1 and restarted the node. Now the log shows:
2022-11-03 22:06:20 [net] Added connection peer=2 2022-11-03 22:06:20 [net] connection from 172.24.0.1:47472 accepted 2022-11-03 22:06:20 [msghand] PROCESSMESSAGE: INVALID MESSAGESTART version peer=2 2022-11-03 22:06:20 [net] disconnecting peer=2 2022-11-03 22:06:20 [net] closing GENERAL stream to peer=2 2022-11-03 22:06:20 [net] Removing peer=2
Do you know how can i know what the node is doing? any debug flag or something like that?
After reading bitcoin node C++ source code, I've found that the magic number are inverted.
This const works for regtest:
const magic = Buffer.from("dab5bffa", "hex");
But this one:
REGTEST: Buffer.from("fabfb5da", "hex")
NO!!!!!
I've also noted that all magic numbers (STN, TEST and MAIN) are written in the same way in the c++ source code. Is it possible that the library should reverse all the bytes of the magic number when serialize the messages?
I also just pushed a change that may fix the block event issue you're having. Try out v1.1.4+ and let me know if it works. You can call this to auto download all newly announced blocks on the network:
peer.fetchNewBlocks((hashes) => hashes)
when I generate a new block from the node dashboard, the 'block' event it is not triggered. The event that is fired is 'block_chunk'.
if in that event I call
peer.getBlock(hash.toString("hex"));
The 'transactions' event gets triggered. Block event is never fired.
All the magic const's use the "Sent over wire as" format which if you used the ticker = 'REGTEST'
it should have worked: https://en.bitcoin.it/wiki/Protocol_documentation#Message_structure
The 'block' event is only called if the constructor param stream = false
. It is not recommended though since it is memory dependent on the block size. Use 'block_chunk' and 'transactions' events depending what you are needing it for. See the README on how to use them.
The REGTEST ticker is now fixed in v1.1.5+
https://github.com/kevinejohn/bsv-p2p/pull/6