1
00:00:04,675 --> 00:00:07,180
All right,
welcome to the fifth lecture on Bitcoin.

2
00:00:07,180 --> 00:00:09,530
Today is going to be all about mining.

3
00:00:09,530 --> 00:00:13,110
So to recap,
what did we learn about the miners so far?

4
00:00:13,110 --> 00:00:16,180
Well, we've heard about the miners quite
a bit and we know that Bitcoin depends on

5
00:00:16,180 --> 00:00:19,220
the miners pretty heavily to perform
a couple of important roles.

6
00:00:19,220 --> 00:00:22,890
Of course they validate every transaction,
they build all the blocks, and

7
00:00:22,890 --> 00:00:25,320
we know that the miners earn some reward.

8
00:00:25,320 --> 00:00:26,440
Who are the miners?

9
00:00:26,440 --> 00:00:27,480
How do they get into this?

10
00:00:27,480 --> 00:00:28,370
How do they operate?

11
00:00:28,370 --> 00:00:30,030
What's the business model like for miners?

12
00:00:30,030 --> 00:00:31,770
What impact are they
having on the environment?

13
00:00:31,770 --> 00:00:34,175
We're going to get to all of those
questions in today's lecture.

14
00:00:38,280 --> 00:00:42,072
So we'll start by talking about what
Bitcoin miners actually have to do in

15
00:00:42,072 --> 00:00:43,119
order to be miners.

16
00:00:44,460 --> 00:00:48,450
And maybe before we get into that,
we'll talk about some historical miners.

17
00:00:48,450 --> 00:00:51,590
So if you're thinking about
becoming a Bitcoin miner,

18
00:00:51,590 --> 00:00:55,280
I wouldn't want to
completely discourage you.

19
00:00:55,280 --> 00:00:59,300
But I'd be remiss not to tell you that
this is not a get rich quick scheme.

20
00:00:59,300 --> 00:01:01,530
This is a very long haul.

21
00:01:01,530 --> 00:01:04,129
So looking back at some
historical gold rushes.

22
00:01:05,300 --> 00:01:10,020
They're full of stories of young people
rushing off hoping to find fortune.

23
00:01:10,020 --> 00:01:12,480
A lot of them losing
everything that they have,

24
00:01:12,480 --> 00:01:15,900
some of them really striking it rich.

25
00:01:15,900 --> 00:01:17,910
But a lot of hardship along the way.

26
00:01:17,910 --> 00:01:22,426
This is probably my favorite example from
the Klondike gold rush in Alaska in 1898.

27
00:01:23,440 --> 00:01:25,970
This gold rush all happened within
the matter of six months, so

28
00:01:25,970 --> 00:01:30,580
there was a huge rush to get there for
the one summer mining season.

29
00:01:30,580 --> 00:01:33,240
And all of these people
are trudging up this

30
00:01:33,240 --> 00:01:36,740
snowy mountain pass with all of
their equipment at the same time.

31
00:01:36,740 --> 00:01:38,930
Doesn't look like
the easiest way to get rich.

32
00:01:38,930 --> 00:01:42,240
And I'll argue that Bitcoin
mining is starting to look

33
00:01:42,240 --> 00:01:43,600
like a similar proposition.

34
00:01:46,530 --> 00:01:51,020
So what do you have to do to be
a Bitcoin miner in six easy steps?

35
00:01:51,020 --> 00:01:55,210
Well, you join the network,
you become a Bitcoin node, you listen for

36
00:01:55,210 --> 00:01:57,460
all of the transactions that
people are broadcasting.

37
00:01:57,460 --> 00:02:00,240
Of course you have to validate them.

38
00:02:00,240 --> 00:02:02,340
You listen for
new blocks that people have found,

39
00:02:02,340 --> 00:02:05,660
you maintain a view of
the current block chain.

40
00:02:05,660 --> 00:02:07,320
Course you want to validate
all the blocks and

41
00:02:07,320 --> 00:02:09,170
all the transactions
that are in those blocks.

42
00:02:10,870 --> 00:02:13,930
You start to assemble new valid
blocks based on the transactions that

43
00:02:13,930 --> 00:02:15,260
you're hearing.

44
00:02:15,260 --> 00:02:18,040
Then you have to work really
hard in step four here

45
00:02:18,040 --> 00:02:20,750
to find a nonce that will make
your block considered valid.

46
00:02:20,750 --> 00:02:24,520
That's the really difficult computational
step where all the difficulty really

47
00:02:24,520 --> 00:02:26,420
happens for the miners.

48
00:02:26,420 --> 00:02:29,010
Finally, if you're lucky enough
to find a block, you have to

49
00:02:29,010 --> 00:02:32,720
hope that all of the other miners accept
your block that they validate it and start

50
00:02:32,720 --> 00:02:37,860
mining on top of it, and that they don't
accept some competitor's block instead.

51
00:02:37,860 --> 00:02:40,490
And if all of that happens, in step six,

52
00:02:40,490 --> 00:02:45,038
you finally get to profit, currently to
the tune of over 25 Bitcoins per block.

53
00:02:45,038 --> 00:02:46,840
Worth about $15,000 US.

54
00:02:46,840 --> 00:02:52,580
And so a couple of these steps,
all of the validation in particular,

55
00:02:52,580 --> 00:02:54,910
are what's actually useful
to the Bitcoin network.

56
00:02:55,920 --> 00:03:00,590
So that's why we have mining at all,
miners perform this validation step.

57
00:03:00,590 --> 00:03:01,850
The rest of it, the profit,

58
00:03:01,850 --> 00:03:06,570
and the race to find blocks, that's all
just incentive to encourage the miners

59
00:03:06,570 --> 00:03:10,020
to do the validation which is necessary
for Bitcoin to function as a currency.

60
00:03:10,020 --> 00:03:14,112
What does the process look like for
finding a valid block?

61
00:03:14,112 --> 00:03:18,570
Well we'll have to look back to the data
structure that we introduced two lectures

62
00:03:18,570 --> 00:03:23,880
ago, and recall that there are two
main hash base data structures here.

63
00:03:23,880 --> 00:03:27,590
There's the block chain where each block
header points to the previous block header

64
00:03:27,590 --> 00:03:29,060
in the chain.

65
00:03:29,060 --> 00:03:31,710
And then within each block
there's this Merkle tree,

66
00:03:31,710 --> 00:03:36,520
this hash based binary tree of all of the
transactions included within that block.

67
00:03:38,180 --> 00:03:42,490
So the first thing that you do as a miner
is you assemble all of the transactions

68
00:03:42,490 --> 00:03:45,200
that you have from your pending
transaction pool into this tree.

69
00:03:46,340 --> 00:03:49,460
You create a block with the right header
that points to the previous block.

70
00:03:50,860 --> 00:03:54,420
And then, you have to start searching over
this nonce field to try to have the hash

71
00:03:54,420 --> 00:03:57,830
of the block header start with
the required number of zeros.

72
00:03:57,830 --> 00:04:02,060
So, you may start with a nonce of
all zeros, that's a 32 bit integer.

73
00:04:02,060 --> 00:04:05,829
And when you try that,
you'll get a hash that's incorrect.

74
00:04:07,570 --> 00:04:10,090
So you'll say, okay,
let's move onto the next nonce,

75
00:04:10,090 --> 00:04:12,570
nonce number one,
the hash is also incorrect.

76
00:04:14,050 --> 00:04:18,220
Step forward one more time,
try hash two, also incorrect.

77
00:04:18,220 --> 00:04:22,090
So you can see where this is going,
and a lot of cases

78
00:04:22,090 --> 00:04:26,470
you'll try every single possible value for
that 32 bit integer.

79
00:04:26,470 --> 00:04:28,240
And in no case will the hash be correct.

80
00:04:29,520 --> 00:04:31,920
So, at this point you're going
to have to make further changes.

81
00:04:34,270 --> 00:04:37,510
And you'll notice that there's this
parameter in the coin base transaction and

82
00:04:37,510 --> 00:04:41,270
remember that the coin base transaction
is where the miners are actually

83
00:04:41,270 --> 00:04:44,660
minting new coins and
claiming them for themselves.

84
00:04:44,660 --> 00:04:46,990
There's also this extra
nonce parameter in there.

85
00:04:48,060 --> 00:04:52,210
So after you've exhausted all possible
nonces in the block header, for

86
00:04:52,210 --> 00:04:57,640
the extra nonce in the coin
base transaction of all zeroes,

87
00:04:57,640 --> 00:05:00,820
you'll step the extra nonce in
the coin based transaction up to one,

88
00:05:00,820 --> 00:05:04,190
and then you'll start searching nonces
in the block header once again.

89
00:05:07,090 --> 00:05:09,890
And it's important to realize that
when you change that one parameter in

90
00:05:09,890 --> 00:05:15,270
the coinbase transaction, the entire
Merkle tree of transactions has to change.

91
00:05:15,270 --> 00:05:18,310
So that change will
propagate all the way up.

92
00:05:18,310 --> 00:05:21,210
Changing the extra nonce in
the coinbase transaction is much

93
00:05:21,210 --> 00:05:24,430
expensive than changing the nonce
in the header, for that reason.

94
00:05:24,430 --> 00:05:26,910
So you do that as the outer loop,
and the inner loop,

95
00:05:26,910 --> 00:05:30,090
where you're really working hardest
changing the nonce in the block header.

96
00:05:33,322 --> 00:05:33,960
Okay.

97
00:05:33,960 --> 00:05:36,220
But once again,
our first try didn't work here.

98
00:05:36,220 --> 00:05:38,990
So we'll have to keep stepping that nonce.

99
00:05:38,990 --> 00:05:42,220
And again, this is a really
difficult computation, so the vast,

100
00:05:42,220 --> 00:05:44,469
vast majority of nonces that
you try aren't going to work.

101
00:05:45,950 --> 00:05:50,420
But eventually, if you stay at it long
enough, you'll find the right combination

102
00:05:50,420 --> 00:05:54,390
of the extra nonce in the coin-based
transaction, and the nonce in the header.

103
00:05:54,390 --> 00:05:57,760
And you'll find a block with a hash
that starts with enough zeros to be

104
00:05:57,760 --> 00:05:59,260
considered valid.

105
00:05:59,260 --> 00:06:01,700
And this is when you want to announce
it as quickly as you can and

106
00:06:01,700 --> 00:06:02,959
hope that you can profit from it.

107
00:06:06,390 --> 00:06:09,520
So I said it was difficult,
exactly how difficult is it?

108
00:06:10,800 --> 00:06:14,740
Well as of today,
this is the mining difficulty target, so

109
00:06:14,740 --> 00:06:18,249
the hash of any value block has
to be below this value here.

110
00:06:20,580 --> 00:06:25,350
And this is a 256-bit hashout, but
we're using shaw256, the hash function,

111
00:06:25,350 --> 00:06:27,370
which is appropriately named.

112
00:06:27,370 --> 00:06:29,090
It's 256 bits.

113
00:06:29,090 --> 00:06:33,440
Currently at least the first 64
bits of the hash of any valid

114
00:06:33,440 --> 00:06:34,920
block have to be set to zero.

115
00:06:38,700 --> 00:06:43,417
Overall, the current difficulty
is about two to the 66 which is

116
00:06:43,417 --> 00:06:45,060
a really huge number.

117
00:06:45,060 --> 00:06:48,240
If we try to write it out in decimal,
that's what we get.

118
00:06:48,240 --> 00:06:54,220
That's 84 quintillion and this number is
so big, it's difficult to even imagine.

119
00:06:54,220 --> 00:06:55,740
One approximation for

120
00:06:55,740 --> 00:07:00,070
it that you can think about is it's about
the population of the Earth squared.

121
00:07:01,310 --> 00:07:05,820
So basically if every person on Earth
was themselves their own planet Earth

122
00:07:05,820 --> 00:07:07,760
with seven billion people on it,

123
00:07:07,760 --> 00:07:10,470
the total number of people
would be close to this number.

124
00:07:11,630 --> 00:07:15,670
Or another way to think about it is
if all seven billion people on Earth

125
00:07:15,670 --> 00:07:16,850
shook hands with each other.

126
00:07:18,250 --> 00:07:21,460
Fewer than to 2 to the 66 total
handshakes would have to take place.

127
00:07:21,460 --> 00:07:25,715
So this is an incomprehensibly
huge number.

128
00:07:30,110 --> 00:07:31,670
So where did this number come from?

129
00:07:31,670 --> 00:07:33,240
How is the mining difficult set?

130
00:07:34,510 --> 00:07:37,350
It's actually chosen
again every two weeks,

131
00:07:37,350 --> 00:07:41,200
based on how efficient the miners
were over the previous two weeks.

132
00:07:41,200 --> 00:07:45,625
So you simply take the amount of time that
it took the miners to find the previous

133
00:07:45,625 --> 00:07:47,990
2,016 blocks, and

134
00:07:47,990 --> 00:07:53,480
you divide that amount of time by two
weeks, and then you multiply that ratio

135
00:07:53,480 --> 00:07:58,050
by whatever the previous difficulty value
was to get the next difficulty value.

136
00:07:58,050 --> 00:08:02,660
So you're just scaling it to keep
a constant property true, which is that

137
00:08:02,660 --> 00:08:08,050
block should be found by the network,
on average, about once every 10 minutes.

138
00:08:08,050 --> 00:08:12,210
That's the fixed constant that was chosen
at the beginning of time for Bitcoin and

139
00:08:12,210 --> 00:08:16,290
every two weeks the difficulty is reset
to ensure that property is maintained.

140
00:08:19,200 --> 00:08:22,809
So you can see that over time
the mining difficulty keeps increasing.

141
00:08:24,050 --> 00:08:28,120
It's not necessarily a steady linear
increase or an exponential increase.

142
00:08:28,120 --> 00:08:32,120
It depends on activity in the market, how
many new miners are getting into the game.

143
00:08:32,120 --> 00:08:36,330
Which may be affected by the current
exchange rate of Bitcoin.

144
00:08:36,330 --> 00:08:39,520
But generally more and
more hash power comes online.

145
00:08:39,520 --> 00:08:43,589
More people are hashing, blocks are found
faster, and the difficulty is adjusted up.

146
00:08:44,910 --> 00:08:48,010
So that it, again,
takes ten minutes to find blocks.

147
00:08:48,010 --> 00:08:52,260
And you can see that,
in the red line on the graph here,

148
00:08:52,260 --> 00:08:54,530
there's a step function of difficulty.

149
00:08:54,530 --> 00:08:57,950
Even though the overall network
hash rate is going smoothly,

150
00:08:57,950 --> 00:09:01,120
because you only reset every two
weeks the difficulty steps up.

151
00:09:05,210 --> 00:09:09,080
Another way to view this is to look at how
long it takes to find a block on average.

152
00:09:10,360 --> 00:09:13,660
So here you can see a graph of,

153
00:09:13,660 --> 00:09:19,000
over time how many seconds elapsed between
consecutive blocks in the block chain.

154
00:09:21,510 --> 00:09:25,140
You can see that this gradually goes
down and then it jumps up again,

155
00:09:25,140 --> 00:09:26,820
and then gradually goes down.

156
00:09:27,960 --> 00:09:32,990
Of course, what's happening there is that
every two weeks, the difficulty resets and

157
00:09:32,990 --> 00:09:36,660
the average block time goes back up to
about ten minutes and then more and

158
00:09:36,660 --> 00:09:38,410
more power goes online.

159
00:09:38,410 --> 00:09:41,710
The difficulty stays the same, so
the miners are finding blocks faster and

160
00:09:41,710 --> 00:09:42,810
faster.

161
00:09:42,810 --> 00:09:43,850
And then two weeks passed.

162
00:09:43,850 --> 00:09:45,990
The difficulty resets back
up to ten minutes and

163
00:09:45,990 --> 00:09:47,540
the process starts all over again.

164
00:09:49,520 --> 00:09:54,182
So in actuality, even though the goal was
for a block to be found every ten minutes,

165
00:09:54,182 --> 00:09:57,579
on average it's more close to
about every nine minutes, and

166
00:09:57,579 --> 00:10:01,999
at the end of the two week cycle, it will
get down to about every eight minutes.

