All right, welcome to the 5th lecture on Bitcoin. Today is gonna be all about mining. So to recap, what have we learned about the miners so far? Well, we've heard about the miners quite a bit. And we know that Bitcoin depends on the miners pretty heavily to perform a couple of important roles. Of course, they validate every transaction, they build all the blocks and we know that the minors earn some reward. Who are the minors? How do they get into this? How do they operate? What's the business model like for minors? What impact are they having on the environment? We're gonna get to all those questions in today's lecture. So we'll start by talking about what Bitcoin minors actually have to do in order to be minors. And maybe before we get into that, we'll talk about some historical miners. So, if you're thinking about becoming a Bitcoin miner, I wouldn't wanna completely discourage you, but I'd be remiss not to tell you that this is not a get rich quick scheme, this is a very long haul. So looking back at some historical gold rushes, they're full of stories of young people rushing off hoping to find fortune. A lot of them losing everything that they have. Some of them really striking it rich. But a lot of hardship along the way. This is probably my favorite example from the Klondike gold rush in Alaska in 1898. This gold rush all happened within a matter of six months, so there was a huge rush to get there for the one summer mining season. And all of these people are trudging up this snowy mountain pass with all of their equipment at the same time. Doesn't look like the easiest way to get rich, and I'll argue that Bitcoin mining is starting to look as like a similar proposition. So what do you have to do to be a Bitcoin miner in six easy steps? Well, you join the network, you become a Bitcoin node, you listen for all of the transactions that people are broadcasting, of course, you have to validate them, you listen for new blocks that people have found, you maintain a view of the current block chain. Of course, you wanna validate all the blocks and all the transactions that are in those blocks. You start to assemble new valid blocks based on the transactions that you're hearing. Then you have to work really hard in step four here to find a nonce that will make your block considered valid. That's the really difficult computational step where all the difficulty really happens for the minors. Finally, if you're lucky enough to find a block, you have to hope that all of the other miners accept your block, that they validate it and start mining on top of it, and that they don't accept some competitor's block instead. And if all that happens in step 6 you finally get to profit, currently to the tune of over 25 Bitcoins per block. Worth about $15,000 US. And so a couple of these steps, all of the validation in particular, are what's actually useful to the Bitcoin network. So that's why we have mining at all. Miners perform this validation step. The rest of it, the profit and the race to find blocks, that's all just incentive to encourage the miners to do the validation which is necessary for Bitcoin to function as a currency. So what does the process look like for finding a valid block? Well, we'll have to look back to the data structure that we introduced two lectures ago, and recall that there are two main hash-based data structures here. There's the block chain, where each block header points to the previous block header in the chain. And then within each block, there's this merkle tree, this hash-based binary tree of all of the transactions included within that block. So the first thing that you do as a miner is you assemble all of the transactions that you have from your pending transaction pool into this tree. You create a block with the right header that points to the previous block, and then you have to start searching over this nonce field to try to have the hash of the block header start with the required number of zeroes. So you may start a nonce of all zeroes that's a 32-bit integer, and when you try that you'll get a hash that's incorrect. So you'll say, okay, let's move on to the next nonce, nonce number one, the hash is also incorrect. Step forward one more time, try hash two, also incorrect. So you can see where this is going, and in a lot of cases, you'll try every single possible value for that 32-bit integer, and in no case will the hash be correct. So at this point you're gonna have to make further changes. And you'll notice that there's this parameter in the coinbase transaction. And remember that the coinbase transaction is where the miners are actually minting new coins and claiming them for themselves. There's also this extra nonce parameter in there. So after you've exhausted all possible nonces in the block header for the extra nonce in the coinbase transaction of all zeroes. You'll step the extra nonce in the coinbase transaction up to one, and then you'll start searching nonces in the block header once again. And it's important to realize that when you change that one parameter in the coinbase transaction, the entire merkle tree of transactions has to change. So that change will propagate all the way up. Changing the extra nonce in the coinbase transaction is much more expensive than changing the nonce in the header for that reason, so you do that as the outer loop. And the inner loop where you're really working hard is changing the nonce in the block header. Okay, but once again our first try didn't work here, so we'll have to keep stepping that nonce. And again, this is a really difficult computation, so the vast, vast majority of nonces that you try aren't gonna work. But eventually, if you stay at it long enough, you'll find the right combination of the extra nonce in the coinbase transaction and the nonce in the header, and you'll find a block with a hash that starts with enough zeros to be considered valid. And this is when you wanna announce it as quickly as you can and hope that you can profit from it. So I said it was difficult. Exactly how difficult is it? Well, as of today, this is the mining difficulty target. So the hash of any valid block has to be below this value here. And this is a 256 bit hash output, we're using Sha 256 the hash function which is appropriately named its 256 bits, currently at least the first 64 bits of the hash of any valid block have to be set to 0. Overall, the current difficulty is about 2 to the 66, which is a really huge number. If we try to write it out in decimal, that's what we get. That's 84 quintillion. And this number is so big it's difficult to even imagine. One approximation for it that you can think about is its about the population of the Earth squared. So basically, if every person on Earth was themselves their own planet Earth, with seven billion people on it, the total number of people would be close to this number. Or another way to think about it is that if all seven billion people on Earth shook hands with each other. Fewer than 2 to the 66 total handshakes would have to take place. So this is a incomprehensibly huge number. So where did this number come from? How is the mining difficult set? It's actually chosen again, every two weeks based on how efficient the miners were over the previous two weeks. So you simply take the amount of time that it took the miners to find the previous 2016 blocks and you divide that amount of time by two weeks. And then you multiply that ratio by whatever the previous difficulty value was to get the next difficulty value. So you're just scaling it to keep a constant property true, which is that blocks should be found by the network on average, about once every ten minutes. That's the fixed constant that was chosen at the beginning of time for Bitcoin, and every two weeks the difficulty is reset to ensure that, that property is maintained. So you can see that over time the mining difficulty keeps increasing. It's not necessarily a steady linear increase or an exponential increase. It depends on activity in the market. How many new miners are getting into the game, which may be affected by the current exchange rate of Bitcoin. But generally more and more hash power comes online. More people are hashing, blocks are found faster, and the difficulty is adjusted up, so that it again takes ten minutes to find blocks, and you can see that in the red line on the graph here. There's a step function of difficulty even though the overall network hash rate is growing smoothly, because you only reset every two weeks, the difficulty steps out. Another way to view this is to look at how long it takes to find a block on average. So here you can see a graph of over time, how many seconds elapsed between consecutive blocks in the block chain. And you can see that this gradually goes down, and then it jumps up again, and then gradually goes down. And of course, what's happening there is that every two weeks the difficulty resets and the average block time goes back up to about ten minutes, and then more and more power goes online, the difficulty stays the same, so the miners are finding blocks faster and faster. And then two weeks pass, the difficulty resets back up to ten minutes and the process starts all over again. So in actuality, even though the goal was for a block to be found every ten minutes, on average it's more close to about every nine minutes, and at the end of the two week cycle, it will get down to about every eight minutes.