Mining rewards & transactions - Blockchain in Javascript (part 3) - YouTube

Channel: Simply Explained

[0]
Hey everyone, my name is Xavier
[2]
and in this video, we will continue working
[4]
on our little JavaScript blockchain implementation.
[7]
In the last two videos, we created our blockchain
[9]
and we secured it with the proof-of-work algorithm.
[13]
In this video, I'm going to expand it a little bit
[15]
and make it a simple cryptocurrency.
[18]
To do that, we're going to do two things.
[20]
Number one, we're gonna make it
[22]
so that a block can contain multiple transactions
[24]
and number two, we're gonna add rewards for miners.
[28]
Now why would we wanna do that?
[29]
Well when you start a cryptocurrency,
[31]
you need to have virtual money or coins.
[34]
And if you don't introduce them somewhere in the system,
[37]
no one can get any coins.
[39]
Mining rewards,
[40]
steadily introduce new coins into the system.
[43]
So, let's get going.
[45]
Here I've opened up the source code of the last two videos
[48]
in Visual Studio Code,
[49]
and I'm going to start by removing
[51]
all of our test code near the bottom.
[55]
We're gonna re-factor a bunch of this stuff
[57]
so this won't work anyway.
[58]
I'm just gonna keep the sajeevCoin instance.
[62]
Next, I'm going to modify the block class a bit,
[65]
so that it supports multiple transactions.
[67]
So instead of receiving a data field,
[70]
I'm gonna say that it receives transactions.
[73]
That makes a little bit more sense.
[75]
And we're gonna say this dot transactions
[78]
equals transactions.
[80]
I'm also going to remove the index argument right here
[83]
because I've added that in the first video
[85]
but it's actually not useful in a blockchain.
[88]
The order of blocks
[89]
is determined by their position in the area,
[91]
and not by the index that we can pass here.
[95]
So when I remove index from the constructor block here,
[98]
I also have to remove it from our genesis block down here.
[103]
All right, so this transaction argument
[105]
will get an array of transactions.
[108]
So, let's define what a transaction looks like
[111]
by creating a new class for a transaction.
[115]
And I'm gonna say that a transaction receives
[117]
a fromAddress, a toAddress and an amount.
[122]
This is pretty straightforward,
[123]
a transaction always comes from someone,
[126]
it goes to someone and it carries a certain amount of coins.
[129]
And then we're just going to assign each of these.
[132]
So fromAddress equals fromAddress,
[134]
this dot toAdress equals toAddress,
[137]
this dot amount equals amount.
[141]
With that out the way,
[142]
we can start working on our main blockchain class.
[144]
There are a few things that we have to do here.
[146]
We have to add a mining reward.
[148]
We need a place to store pending transactions
[151]
and we need a new method to mine a new block
[153]
for the pending transactions.
[156]
Now you might ask yourself,
[157]
what is this pending transaction stuff and why do I need it?
[160]
Well it's because we only create blocks
[162]
on a specific interval.
[164]
In Bitcoin's case, proof-of-work algorithm
[167]
makes sure that there's only one block created
[169]
every 10 minutes.
[171]
All the transactions that are made in between blocks
[174]
are temporarily stored in this pendingTransactionsarray,
[177]
so that they can be included in the next block.
[180]
So let's start with simple things first.
[182]
I'm going to add a new property to our blockchain class
[186]
for our pendingTransactions.
[187]
So I'm gonna call it this dot pendingTransactions
[190]
equals an empty array.
[193]
And them I'm also going to define a property
[195]
that will control how much coins the miners get as a reward.
[199]
So I'm gonna say, this dot miningReward equals,
[203]
let's say, 100 coins if you successfully mine a new block.
[208]
I'm also going to reduce the difficulty a bit
[210]
so that it's just faster to test
[212]
and then we can increase again when we're done
[214]
with our changes.
[217]
Now that we have these changes,
[218]
we're gonna work on our new mining method.
[220]
So, this was our old mining method,
[223]
the addBlock method, and I'm gonna replace that
[225]
by minePendingTransactions.
[230]
Now minePendingTransactions, this will receive an address.
[234]
It will receive a miningRewarAddress.
[240]
So when a miner calls this method
[243]
it will pass along it's wallet address,
[246]
and say, hey if I successfully mined this block
[249]
then send the reward to this address.
[252]
Now, inside this method,
[253]
we will start by creating a new block.
[256]
So I'm gonna say let block equals eight new Block.
[260]
That requires a timestamp, so I'm gonna say Date dot now.
[264]
And then it requires some transaction,
[266]
so we're gonna pass along this dot pendingTransactions.
[269]
We're gonna give it all of our pending transactions
[272]
that are currently stored in our blockchain.
[274]
Now in real world cryptocurrencies,
[276]
like again, for instance, Bitcoin,
[278]
adding all the pendingTransactionsto a block
[280]
isn't possible
[281]
because there are way too many pending transactions
[284]
and because the block size cannot increase one megabyte.
[287]
So instead,
[288]
miners get to choose which transactions they include
[291]
and which they don't.
[292]
But we're not going to worry about this
[294]
in our simple example,
[295]
but just be aware that in reality,
[297]
miners have to pick the transactions
[299]
that they want to include.
[301]
So after creating the block, let's mine it.
[304]
We're gonna say block dot minedBlock.
[306]
And we're going to mine it with the difficulty
[308]
that is currently set within our blockchain,
[311]
right now it's two.
[313]
And if that's done,
[314]
we're gonna to log something to the console.
[316]
We're gonna say block successfully mined.
[322]
And then we're going to add it to our chain.
[324]
So we're going to say this dot chain dot push our block.
[330]
And the last two things that we have to do
[332]
is reset the pendingTransactions array
[335]
and create a new transaction to give the miner his reward.
[339]
Now we can do this all in one go.
[341]
We can just say that this dot pendingTransactions
[344]
equals an empty array,
[345]
that will reset the pending transactions.
[348]
But we can also just define
[349]
an area which is one new element.
[351]
So we can say new Transaction.
[354]
The fromAddress, we're gonna say no.
[356]
Because there isn't really a fromAddress in a mine reward.
[359]
It just comes out of this system,
[361]
it's the algorithm that gives it to you.
[363]
We're going to send it to the miningRewardAddress,
[367]
and how much coins are we going to send
[369]
this dot miningRewards.
[373]
Now, in the previous video, some people pointed out,
[376]
that they can change this code right here
[378]
and give themselves more coins.
[381]
And that's true, you really can do that.
[383]
However, cryptocurrencies are powered
[385]
by a peer-to-peer network and other nodes in the network
[388]
won't accept your attempts of fooling them,
[390]
and instead, they will just ignore you.
[393]
So that's something that I wanted to address
[394]
from the previous videos.
[396]
Now to continue,
[397]
I'm also going to create a simple method
[399]
called createTransaction.
[402]
And this will receive a transaction and all this will do
[405]
is add this to the pendingTransactions area.
[409]
So we're going to push it on to that area.
[412]
All right, now the final thing that I'm going to do
[414]
is I'm going to create a method,
[416]
that checks the balance of an address.
[419]
Many people thing that if you send some bitcoins around,
[422]
they actually move away from your wallets balance
[425]
to someone else's balance.
[427]
But in reality, you don't really have a balance.
[430]
The transaction is just stored on the blockchain,
[433]
and if you ask for your balance,
[434]
you have to go through all the transactions
[436]
that involve your address and calculate it that way.
[440]
So let's create a method for this.
[441]
We gonna call this getBalanceOfAddress,
[446]
and it will of course receive an address
[448]
that we have to check its balance.
[450]
We're going to say that the balance starts at zero.
[454]
And then we're going to loop over all the blocks
[456]
in our blockchain.
[457]
So we're gonna say for constant block of this dot chain.
[461]
And because our blocks now contain multiple transactions,
[464]
we're going to say loop over each transaction
[466]
of the transactions in side this block.
[470]
And then we have to write a bit of logic.
[474]
If you are the fromAddress,
[476]
that means that you've transferred money
[477]
away from your wallet to someone else,
[479]
so we have to reduce your balance.
[482]
And if you are the toAddress,
[484]
then you were the receiver of a transaction
[486]
and we have to increase your balance.
[488]
So, we're going to say,
[489]
if transaction fromAddress equals the address
[494]
that was given here and its address,
[499]
then we're going to reduce your balance
[502]
with the amount of the transaction.
[507]
And if the toAddress equals the address that you gave us,
[513]
then we're going to increase your balance
[517]
with the amount of the transaction.
[521]
And when we're done looping over all the blocks
[523]
and all the transactions,
[524]
we're just gonna return the balance that you have.
[529]
Alright, so now it's finally time to test our code.
[532]
I'm gonna scroll down
[533]
and the first thing that I'm going to do
[535]
is I'm going to create some transactions.
[538]
I'm going to savjeeCoin dot createTransaction.
[543]
And we're going to make a new Transaction object here.
[546]
new Transaction.
[547]
This has a fromAddress,
[549]
I'm just going to give it random strings here address one,
[552]
to address two, and it will send address two,
[555]
let's say 100 coins.
[557]
And then we're going to repeat that.
[558]
And then we're going to say that address two, for instance,
[562]
gives address one 50 points back, something like that.
[566]
Now in reality address one and address two
[569]
would be the public key of someone's wallet.
[572]
After we've created these new transactions,
[574]
they will be pending.
[575]
They will be in the pendingTransactions area.
[577]
So we have to start the miner
[579]
to actually create a block for them
[580]
and store them on our blockchain.
[582]
So I'm going to say console dot log,
[585]
new line Starting the miner
[589]
and then I'm going to say savjeeCoin
[590]
dot minependingTransactions.
[593]
And this method expects a miningRewardAddress.
[596]
In other words, it wants to know,
[597]
where it should send the miningRewards to.
[600]
So, I'm just going to create another fake address.
[602]
I'm gonna say Xavier's address.
[605]
And after that is done,
[606]
we're going to check my balance.
[608]
So we're going to say console log Balance of Xavier is,
[614]
and then we're going to say, savjeeCoin dot
[618]
getBalanceOfAddress, xaviers address.
[623]
All right, so let's save the file.
[625]
And let's open the terminal and run it
[627]
to see if it actually works.
[628]
So I'm going to say node source main dot js.
[633]
And yeah, kind of works starting the miner, mined the block,
[637]
but then it says my balance still zero.
[640]
Well, actually, that's correct,
[642]
because in our mining method if you remember,
[645]
after a block has been mined,
[647]
we create a new Transaction to give you your miningReward,
[650]
but that one is added to the pendingTransactions array.
[654]
So the miningReward will only be sent
[656]
when the next block is mined.
[658]
So, lets mine another one then.
[661]
We say console dot log.
[664]
Uh oops, kind of put that...
[667]
Just copy it and put it there.
[669]
So storing the miner again,
[673]
and then we're gonna check my balance again, right there.
[680]
Save it.
[682]
Run it again.
[684]
And sure enough the second time,
[686]
it increased our balance to 100.
[688]
But in mining our second block,
[690]
I also get a new reward
[692]
which is again in the pendingTransaction state
[694]
and will again be included in the next block that is mined.
[698]
So, that concludes this video.
[700]
We've transformed our block chain
[702]
into a very simple cryptocurrency,
[704]
where a block can contain multiple transactions,
[707]
and where miners get 100 coins in miningRewards.
[711]
Now I want to point out that this is a simplified version
[714]
of a cryptocurrency and it's for education purposes.
[717]
Don't try to make your own cryptocurrency with this.
[720]
So that's it for now.
[721]
If you haven't subscribed already,
[723]
make sure you hit the subscribe button
[724]
and also like this video.
[727]
Thank you very much for watching,
[728]
and I'll see you in the next video.