Bitcoin miner programming
Instead, the ledger is broken up into blocks: Every block includes a reference to the block that came before it, and you can follow the links backward from the most recent block to the very first block, when bitcoin creator Satoshi Nakamoto conjured the first bitcoins into existence. Every 10 minutes miners add a new block, growing the chain like an expanding pearl necklace.
Generally speaking, every bitcoin miner has a copy of the entire block chain on her computer. If she shuts her computer down and stops mining for a while, when she starts back up, her machine will send a message to other miners requesting the blocks that were created in her absence. No one person or computer has responsibility for these block chain updates; no miner has special status. The updates, like the authentication of new blocks, are provided by the network of bitcoin miners at large.
Bitcoin also relies on cryptography. The computational problem is different for every block in the chain, and it involves a particular kind of algorithm called a hash function. Like any function, a cryptographic hash function takes an input—a string of numbers and letters—and produces an output.
But there are three things that set cryptographic hash functions apart:. The hash function that bitcoin relies on—called SHA, and developed by the US National Security Agency—always produces a string that is 64 characters long.
You could run your name through that hash function, or the entire King James Bible. Think of it like mixing paint. If you substitute light pink paint for regular pink paint in the example above, the result is still going to be pretty much the same purple , just a little lighter. But with hashes, a slight variation in the input results in a completely different output:. The proof-of-work problem that miners have to solve involves taking a hash of the contents of the block that they are working on—all of the transactions, some meta-data like a timestamp , and the reference to the previous block—plus a random number called a nonce.
Their goal is to find a hash that has at least a certain number of leading zeroes. That constraint is what makes the problem more or less difficult. More leading zeroes means fewer possible solutions, and more time required to solve the problem.
Every 2, blocks roughly two weeks , that difficulty is reset. If it took miners less than 10 minutes on average to solve those 2, blocks, then the difficulty is automatically increased. If it took longer, then the difficulty is decreased. Miners search for an acceptable hash by choosing a nonce, running the hash function, and checking.
When a miner is finally lucky enough to find a nonce that works, and wins the block, that nonce gets appended to the end of the block, along with the resulting hash. Her first step would be to go in and change the record for that transaction. Then, because she had modified the block, she would have to solve a new proof-of-work problem—find a new nonce—and do all of that computational work, all over again. Again, due to the unpredictable nature of hash functions, making the slightest change to the original block means starting the proof of work from scratch.
But unless the hacker has more computing power at her disposal than all other bitcoin miners combined, she could never catch up. She would always be at least six blocks behind, and her alternative chain would obviously be a counterfeit. She has to find a new one. The code that makes bitcoin mining possible is completely open-source, and developed by volunteers. But the force that really makes the entire machine go is pure capitalistic competition.
Every miner right now is racing to solve the same block simultaneously, but only the winner will get the prize. In a sense, everybody else was just burning electricity.
Yet their presence in the network is critical. But it also solves another problem. It distributes new bitcoins in a relatively fair way—only those people who dedicate some effort to making bitcoin work get to enjoy the coins as they are created. But because mining is a competitive enterprise, miners have come up with ways to gain an edge.
One obvious way is by pooling resources. Your machine, right now, is actually working as part of a bitcoin mining collective that shares out the computational load. Your computer is not trying to solve the block, at least not immediately. It is chipping away at a cryptographic problem, using the input at the top of the screen and combining it with a nonce, then taking the hash to try to find a solution.
Solving that problem is a lot easier than solving the block itself, but doing so gets the pool closer to finding a winning nonce for the block. Think of it as work done by groups of people to find large prime numbers or trying keys to decrypt a file. In fact, many wager that the DDOS attacks on many bitcoin-related services are direct action by hackers to inject instability in order to reduce the price. As it stands, mining solo is very nearly deprecated.
The process of finding blocks is now so popular and the difficulty of finding a block so high that it could take over three years to generate any coins. While you could simply set a machine aside and have it run the algorithms endlessly, the energy cost and equipment deprecation will eventually cost more than the actual bitcoins are worth.
Pooled mining, however, is far more lucrative. While this is simplified, it is basically how the system works. You work for shares in a block and when complete you get a percentage of the block based on the number of workers alongside you, less fees. The astute among you will note that I probably used twice that amount of electricity. My buddy Tom explained how to set up a pooled mining account so I thought it would be interesting to share the instructions.
You can either store your wallet locally or store it online. Wallets require you to use or download a fairly large blockchain file — about 6GB — so downloading and updating a local wallet may be a non-starter.
There is no preferred wallet type and there are obvious trade-offs to both. Privacy advocates would probably say a local wallet is best. You can download a local wallet here but make sure you keep a copy of your data backed up.
This, without the period, is a direct way to send bitcoins to your wallet. Make a note of your address. In Coinbase, the wallet address found under linked accounts. To mine in a pool you have to work with a group of other miners on available blocks. You can also try guilds like BTC Guild as well as a number of other options. Pools with fewer users could also have a slower discovery time but pools with many users usually result in smaller payments.
However, as one pool owner, Slush, notes:. First, create a pool login. The workers are sub-accounts with their own passwords and are usually identified by [yourlogin]. I have three workers running, currently — one on my iMac and two on my old PC.