Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: btc_artist on February 09, 2012, 11:56:01 PM



Title: Can't seem to build the version message correctly to talk to a bitcoin peer
Post by: btc_artist on February 09, 2012, 11:56:01 PM
So I'm trying to learn more about the Bitcoin protocol, and I'm writing some code to try to talk with a Bitcoin instance as a peer. My problem is that Bitcoin won't respond to any of the messages I try to send.  Here's what I have:

An example of the raw "version" message I'm sending:
Code:
F9BEB4D976657273696F6E000000000055000000B4C3000001000000000000009E5A344F00000000010000000000000000000000000000000000FFFF7F000010208D010000000000000000000000000000000000FFFF7F00001061A8CB0170F938EC13C700D6880200

The above message broken apart:
Code:
Start    : F9 BE B4 D9
Command  : 76 65 72 73 69 6F 6E 00 00 00 00 00
Length   : 55 00 00 00
Checksum :
Payload  :
    Version          : B4 C3 00 00
    Services         : 01 00 00 00 00 00 00 00
    Timestamp        : 9E 5A 34 4F 00 00 00 00
    RecipientAddress :
        Services : 01 00 00 00 00 00 00 00
        Ip       : 00 00 00 00 00 00 00 00 00 00 FF FF 7F 00 00 10
        Port     : 20 8D
    SenderAddress    :
        Services : 01 00 00 00 00 00 00 00
        Ip       : 00 00 00 00 00 00 00 00 00 00 FF FF 7F 00 00 10
        Port     : 61 A8
    Nonce            : CB 01 70 F9 38 EC 13 C7
    SubVersion       : 00
    LastBlock        : D6 88 02 00


Here's what I get in the Bitcoin debug.log file:
Code:
accepted connection 127.0.0.1:63714
...
PROCESSMESSAGE MESSAGESTART NOT FOUND
...
socket no message in first 60 seconds, 1 0
disconnecting node 127.0.0.1:63714

"PROCESSMESSAGE MESSAGESTART NOT FOUND" comes from main.cpp around line 2342.
"socket no message in first 60 seconds" comes from net.cpp, right near the end.

I've tried to follow the spec as outlined here: https://en.bitcoin.it/wiki/Protocol_specification (particularly the "Message structure", "Network address" and "Version" sections), but when I send the message, I never get a response, and Bitcoin complains about the already mentioned errors (above) in the log file.

Any ideas as to what I'm doing wrong in building the message?


Title: Re: Can't seem to build the version message correctly to talk to a bitcoin peer
Post by: btc_artist on February 10, 2012, 04:53:04 AM
Solved the first part of my problem.  I no longer get the "PROCESSMESSAGE MESSAGESTART NOT FOUND" error in the debug.log file, but when I send the "version" message to bitcoin, it doesn't respond with anything, and in the debug file, it closes the connection with "socket no message in first 60 seconds".

Any ideas?  Thanks in advance.

Edit: Note, I get a socket error in my code saying: "An existing connection was forcibly closed by the remote host".


Title: Re: Can't seem to build the version message correctly to talk to a bitcoin peer
Post by: theymos on February 10, 2012, 05:02:56 AM
I think you're sending an empty vector as the checksum, but there should be no checksum field at all. (For 10 more days, at least.)


Title: Re: Can't seem to build the version message correctly to talk to a bitcoin peer
Post by: btc_artist on February 10, 2012, 05:14:28 AM
Hmmm, am I?

I have the Checksum field in my debugging output, but as far as I can tell, I'm not sending it. In the raw data, I have the 4 bytes for length "55 00 00 00", then it goes directly to version, which is "B4 C3 00 00".

Thanks for the response.

EDIT: It just occurred to me that I might not be using the correct protocol.  I'm using TCP, but I can't find anywhere that it is actually specified.


Title: Re: Can't seem to build the version message correctly to talk to a bitcoin peer
Post by: theymos on February 10, 2012, 05:24:18 AM
Ah, you're right. I miscounted the bytes.

Bitcoin does use TCP.

Solved the first part of my problem.  I no longer get the "PROCESSMESSAGE MESSAGESTART NOT FOUND" error in the debug.log file

What did you change to fix it?


Title: Re: Can't seem to build the version message correctly to talk to a bitcoin peer
Post by: btc_artist on February 10, 2012, 05:44:07 AM
Solved the first part of my problem.  I no longer get the "PROCESSMESSAGE MESSAGESTART NOT FOUND" error in the debug.log file

What did you change to fix it?
I made the elementary mistake of sending the message as ASCII characters 0-9A-F representing the hex-encoded message, instead of the actual binary hex message.  After changing that, Bitcoin stopped complaining about "MESSAGESTART NOT FOUND".

I still can't get bitcoin to respond to my message though, and it closes the socket one minute later with the "socket no message in first 60 seconds" error.

I thought after it received my initial "version" message, it would respond in kind?


Title: Re: Can't seem to build the version message correctly to talk to a bitcoin peer
Post by: btc_artist on February 10, 2012, 06:12:00 AM
This is strange... if I duplicate my entire binary "version" message, and and send two copies of it together over the socket, then I get different output in the debug.log file.

Code:
accepted connection 127.0.0.1:52191
...
version message: version 50100, blocks=17777
...
PROCESSMESSAGE MESSAGESTART NOT FOUND
...
socket no message in first 60 seconds, 1 0
disconnecting node 127.0.0.1:52191

Whereas, if I don't duplicate it, I only get:

Code:
accepted connection 127.0.0.1:52191
...
socket no message in first 60 seconds, 1 0
disconnecting node 127.0.0.1:52191

The "version message: version 50100, blocks=17777" is from the message I sent.

Very strange.

EDIT: Yup, if I duplicate the message and send it double, then I immediately start getting version, verack, inv, addr messages back.


Title: Re: Can't seem to build the version message correctly to talk to a bitcoin peer
Post by: btc_artist on February 10, 2012, 03:58:09 PM
Update:  It's not the duplicated message that's making it work.  The full message I'm sending is 105 bytes long.  If I add at least 71 bytes of any data to the end of it then it works. 70 bytes or less and it doesn't work. 71 bytes or more (for a total message length of 176 bytes or more written to the socket) and it works.  The extra data can be anything (letters, numbers, null byte, etc) and it works.

I still can't explain why this is.... :(

Update #2: Finally figured this out.  There was a bug in my code where some code was getting called twice, which caused the payload length (in the header) to get calculated wrong.  After fixing that, the extra bytes are no longer needed.

Thanks for your help theymos!


Title: Re: Can't seem to build the version message correctly to talk to a bitcoin peer
Post by: moneyart on September 27, 2015, 09:28:09 AM
Can you explain how you are sending the version message. I also want to do this but I have no clue. Do you use the addnode or connect command?