Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: Coding Enthusiast on December 11, 2016, 09:23:31 AM



Title: Is there a way to check if my Message is valid (Version handshake,...)
Post by: Coding Enthusiast on December 11, 2016, 09:23:31 AM
Since I am learning I wanted to know if there there is a way to check if the Message I have generated is valid before sending it out?

I am talking about Messages that you send to a node in order to establish communication, starting with version.

Example based on Docs (https://bitcoin.org/en/developer-reference#version)
Quote
My header:
f9beb4d9
76657273696f6e0000000000
cc0000006a000000
3fd3a680


My message:
7e110100
0000000000000000
fa084d5800000000
0000000000000000
00000000000000000000ffff52a1377f
208d
0000000000000000
00000000000000000000ffff7f000001
208d
fa084d5800000000
2814 2f437573746f6d436c69656e743a302e392e302f
00000000
00


A couple of questions regarding this:
1) Why should I even provide "addr_recv services" field? And does it matter if I provide 0 or 1?
2) Will receiving node reject "addr_recv IP address" field if it is 127.0.0.1 instead of its own IP or a false IP?
3) "addr_trans services" Why am I telling this again if it is identical to "services" above?
4) I don't get usage of "nonce" when I have already provided "timestamp" which will change every time it is generated and it will be bigger. And can I use the same thing again here?
5) What is the criteria to get banned by a node? For instance will I be banned if I send a wrong message on first attempt? Also will I receive any reply if my message contains errors or will it be ignored?


Title: Re: Is there a way to check if my Message is valid (Version handshake,...)
Post by: achow101 on December 11, 2016, 03:59:21 PM
1) Why should I even provide "addr_recv services" field? And does it matter if I provide 0 or 1?
I don't think it matters.

2) Will receiving node reject "addr_recv IP address" field if it is 127.0.0.1 instead of its own IP or a false IP?
It shouldn't, but you should still attempt to provide the correct IP.

3) "addr_trans services" Why am I telling this again if it is identical to "services" above?
Because....

4) I don't get usage of "nonce" when I have already provided "timestamp" which will change every time it is generated and it will be bigger. And can I use the same thing again here?
Code:
A random nonce which can help a node detect a connection to itself. If the nonce is 0, the nonce field is ignored. If the nonce is anything else, a node should terminate the connection on receipt of a version message with a nonce it previously sent.
Just use 0.

5) What is the criteria to get banned by a node? For instance will I be banned if I send a wrong message on first attempt? Also will I receive any reply if my message contains errors or will it be ignored?
You won't get banned on the first time that you try. Just don't send bad messages too many times too quickly and you should be fine.

You should test this against your own local node and you can set it to never ban the node that you are testing.


You should use wireshark to see the messages actually sent by nodes so that you can check your own message and see how the messages are usually done.


Title: Re: Is there a way to check if my Message is valid (Version handshake,...)
Post by: valley365 on December 12, 2016, 09:30:00 AM
if you create message on your own (i.e. not using bitcoin framework), then you need to make sure the message starts with particular sequence which is defined by the pchMessageStart array. For bitcoin it is defined as:
        pchMessageStart[0] = 0xf9;
        pchMessageStart[1] = 0xbe;
        pchMessageStart[2] = 0xb4;
        pchMessageStart[3] = 0xd9;

other coins such as litecoin have different definition, that's how the coin recognize its own message.


Title: Re: Is there a way to check if my Message is valid (Version handshake,...)
Post by: Coding Enthusiast on December 12, 2016, 02:25:39 PM
@valley365
Sorry I thought my question was clear that I am creating the message on my own based on the documentation not with any software or library out there. And that example in OP is the message I have generated with my own code.


I will update this topic, right now I apparently have some troubles with my understanding of TCP ;D and how to use a freaking Socket!


Title: Re: Is there a way to check if my Message is valid (Version handshake,...)
Post by: Foxpup on December 12, 2016, 03:11:34 PM
@valley365
Sorry I thought my question was clear that I am creating the message on my own based on the documentation not with any software or library out there. And that example in OP is the message I have generated with my own code.
Your question was perfectly clear; he's just posting nonsense for his sig campaign. I already reported it.


Title: Re: Is there a way to check if my Message is valid (Version handshake,...)
Post by: Coding Enthusiast on December 14, 2016, 06:35:35 PM
As an update I want to say the Wireshark helped a lot. My mistake in my message was the length fields which I forgot to cut in half (hex length/2) and also it was interesting to find out that the receiving node returns a reply even to messages with error like mine with a reject message saying error parsing message
https://bitcoin.org/en/developer-reference#reject

Now I've got to figure out how to stack these send and received on top of each other using threading.

Something weird: the node that I sent the wrong message to replied with the following messages respectively!
  • reject
  • ping
  • getheaders
  • addr
  • inv
the weird part is that it skips version and verack!
Until both peers have exchanged version messages, no other messages will be accepted.
I guess that only goes for "accepting" not "sending"?!