Bitcoin protocol from the inside. How does it works? part 1: message header

in #bitcoin8 years ago

Mainstream

Recently bitcoin and other altcoins due to high value increase became very popular in the mainstream. It's no more just a geek toy, but commonly accepted payment method. However not many people know how does it works. I think its worth to know it from the inside to understand what happend to your money, why it's worth so much and why it can be only bigger. The main feature of bitcoin is its dispersion, p2p model without central server, so noone can easily stop it nor can lock your money. Lots of nodes means that if you want to to join to network you just need to connect to ANY node.
btc.png

Communication

Bitcoin node DNS

How to get an node adress?

dns.png The easiest way is just connecting with special DNS (domain name server) which will return addresses of few randomly selected active nodes. You can find a list of DNS here: https://en.bitcoin.it/wiki/Satoshi_Client_Node_Discovery#DNS_Addresses

Basics of communication in bitcoin network:

The basic bitcoin port is 8333 in which an app is waiting for incomming connection. The network protocol bitcoin use is TCP.
Bitcoin network have it's own communication protocol to exchange prepared messages. Some of the messages can be an sending transaction, active node listening, or transaction details.

Header:

Every single message in network need an header to let application filter packets that doesn't belongs to bitcoin network, then recognize incomming transaction, interpret it, check the checksum to make sure everything is allright
Every header contains 4 elements:

field 1: magic

4 bytes inherent in any header to ensure if its a Bitcoin packet. Actuall value is 0xD9B4BEF9 for bitcoin network and 0xDAB5BFFA for bitcoin testnet.

field 2: command

12 bytes of ASCII that represents name of message. Thanks to it we know how to interpret bytes after header. It's allways 12 bytes, if name it's shorter theres added proper amount of zeros.

field 3: length

4 bytes that say how many bytes after the header - how big is the message.

field 4: checksum

first 4 bytes of message checksum. Used alghoritm: SHA-256

What's next

That's how a message header looks like, to initate a cennection you need to build a "version" message which I will show in next post.

sources:

Thanks for reading, feel free to ask, and give me a feedback if you would like to see more technical posts like this

Sort:  

I'm sure it'll be a good series, keep posting.