All the recent media attention on Bitcoin inspired me to learn how Bitcoin really works, right down to the bytes flowing through the network. I wanted to get a hands-on understanding of the Bitcoin protocol. My goal was to use the Bitcoin system directly: create a Bitcoin transaction manually, feed it into ripemd 160 bitcoin miner system as hex data, and see how it gets processed.
This turned out to be considerably harder than I expected, but I learned a lot in the process and hopefully you will find it interesting. If you like this article, check out my mining article too. This blog post starts with a quick overview of Bitcoin and then jumps into the low-level details: creating a Bitcoin address, making a transaction, signing the transaction, feeding the transaction into the peer-to-peer network, and observing the results. To simplify slightly, bitcoins consist of entries in a distributed database that keeps track of the ownership of bitcoins. Unlike a bank, bitcoins are not tied to users or accounts. In a transaction, the owner of some bitcoins transfers ownership to a new address.
Bitcoin mining is the process that puts transactions into a block, to make sure everyone has a consistent view of the transaction log. Finding this solution generates a mined block, which becomes part of the official block chain. Mining is also the mechanism for new bitcoins to enter the system. When a block is successfully mined, new bitcoins are generated in the block and paid to the miner. In addition, the miner gets any fees associated with the transactions in the block.
Because of this, mining is very competitive with many people attempting to mine blocks. The difficulty and competitiveness of mining is a key part of Bitcoin security, since it ensures that nobody can flood the system with bad blocks. Instead, Bitcoin runs on a peer-to-peer network. If you run a Bitcoin client, you become part of that network.
The nodes on the network exchange transactions, blocks, and addresses of other peers with each other. When you first connect to the network, your client downloads the blockchain from some random node or nodes. In turn, your client may provide data to other nodes. When you create a Bitcoin transaction, you send it to some peer, who sends it to other peers, and so on, until it reaches the entire network. Miners pick up your transaction, generate a mined block containing your transaction, and send this mined block to peers.
Eventually your client will receive the block and your client shows that the transaction was processed. Bitcoin uses digital signatures to ensure that only the owner of bitcoins can spend them. The owner of a Bitcoin address has the private key associated with the address. To spend bitcoins, they sign the transaction with this private key, which proves they are the owner. It’s somewhat like signing a physical check to make it valid.
A public key is associated with each Bitcoin address, and anyone can use it to verify the digital signature. Blocks and transactions are identified by a 256-bit cryptographic hash of their contents. This hash value is used in multiple places in the Bitcoin protocol. In addition, finding a special hash is the difficult task in mining a block. Bitcoins do not really look like this.