An education effort from
Blockchain Training
an education effort from
An education effort from
Building a Blockchain
Lesson 05
An education effort from
Blockchain
At this point we have built a basic Blockchain in JavaScript and implemented proof-
of-work to protect it from spammers and attackers.
We still have a couple of things to fix:
● Our blockchain can only store 1 transaction in a block
● There are no rewards for miners.
An education effort from
Restructuring Block Class
The first step is to restructure the
Block Class and the
calculateHash() function. We need
to include a transactions field
instead of the data one and the
index field can be removed since
it doesn't make sense with our
implementation.
An education effort from
Transaction Class
On each block we should be able to store multiple transactions. For this we will
have to define a Transaction class. In this case it will be very simple, containing a
sender (fromAddress) a receiver (toAddress) and an amount. You can add more
fields to a transaction, but these are the minimum.
An education effort from
Adjust the Blockchain class
The first thing that we need is a place to store pending transactions.
As you know, blockchains create blocks on a steady interval thanks to the proof-of-
work algorithm. However, it should be possible to submit new transactions in
between the creation of two blocks.
To do that, the Blockchain’s constructor needs some changes to include a place to
store pending transactions.
An education effort from
Adjust the Blockchain class
This is how the Blockchain class constructor will look. Notice that there is a
property to define the reward for the miner:
An education effort from
Review addBlock() method
This method needs to be rewritten completely, and a new method
createTransaction will be use instead:
An education effort from
Block Mining
A new method
minePendingTransactions()
will help us mine a new
block with all the pending
transactions while sending
the reward to the miner.
An education effort from
Block Mining
Note that this implementation takes all the pending transactions and adds them to a
block.
The mininghRewardAddress argument can be passed along with the wallet
address. Once a block is successfully mined, a new transaction with the reward will
be created.
An education effort from
Address Balance
We need to
include a new
method to
check the
balances of
the addresses
on the
blockchain
An education effort from
Run the application
By including this code we
can test our application
and creating 2 new
transaction in pending
state. We will have to start
the miner to get them
confirmed.
An education effort from
Run the application
While adding the following code we will be starting the miner and showing the
whole chain:
An education effort from
Run the application
We can check the balance for the test address with the following code:
However if you look closely at the code, you'll see that the system creates a new
block and then adds your mining rewards as a new pending transaction. That
transaction will be included in the next block. Until the miner is started again the
reward will not be received.
An education effort from
Run the application
Let's start the miner again and run the application to receive the reward and check
the current chain status.
An education effort from
Results
Here you have the whole terminal
output:
An education effort from
Lesson Review
While this implementation is far from complete. It is capable of storing multiple
transactions in a block and of giving rewards to miners. A basic example of how to
implement blockchain and moreover how to build a basic cryptocurrency.
The complete project code can be found in the following Git repository:
https://github.com/juliomacr/blockchain-demo

Building a blockchain part 3

  • 1.
    An education effortfrom Blockchain Training an education effort from
  • 2.
    An education effortfrom Building a Blockchain Lesson 05
  • 3.
    An education effortfrom Blockchain At this point we have built a basic Blockchain in JavaScript and implemented proof- of-work to protect it from spammers and attackers. We still have a couple of things to fix: ● Our blockchain can only store 1 transaction in a block ● There are no rewards for miners.
  • 4.
    An education effortfrom Restructuring Block Class The first step is to restructure the Block Class and the calculateHash() function. We need to include a transactions field instead of the data one and the index field can be removed since it doesn't make sense with our implementation.
  • 5.
    An education effortfrom Transaction Class On each block we should be able to store multiple transactions. For this we will have to define a Transaction class. In this case it will be very simple, containing a sender (fromAddress) a receiver (toAddress) and an amount. You can add more fields to a transaction, but these are the minimum.
  • 6.
    An education effortfrom Adjust the Blockchain class The first thing that we need is a place to store pending transactions. As you know, blockchains create blocks on a steady interval thanks to the proof-of- work algorithm. However, it should be possible to submit new transactions in between the creation of two blocks. To do that, the Blockchain’s constructor needs some changes to include a place to store pending transactions.
  • 7.
    An education effortfrom Adjust the Blockchain class This is how the Blockchain class constructor will look. Notice that there is a property to define the reward for the miner:
  • 8.
    An education effortfrom Review addBlock() method This method needs to be rewritten completely, and a new method createTransaction will be use instead:
  • 9.
    An education effortfrom Block Mining A new method minePendingTransactions() will help us mine a new block with all the pending transactions while sending the reward to the miner.
  • 10.
    An education effortfrom Block Mining Note that this implementation takes all the pending transactions and adds them to a block. The mininghRewardAddress argument can be passed along with the wallet address. Once a block is successfully mined, a new transaction with the reward will be created.
  • 11.
    An education effortfrom Address Balance We need to include a new method to check the balances of the addresses on the blockchain
  • 12.
    An education effortfrom Run the application By including this code we can test our application and creating 2 new transaction in pending state. We will have to start the miner to get them confirmed.
  • 13.
    An education effortfrom Run the application While adding the following code we will be starting the miner and showing the whole chain:
  • 14.
    An education effortfrom Run the application We can check the balance for the test address with the following code: However if you look closely at the code, you'll see that the system creates a new block and then adds your mining rewards as a new pending transaction. That transaction will be included in the next block. Until the miner is started again the reward will not be received.
  • 15.
    An education effortfrom Run the application Let's start the miner again and run the application to receive the reward and check the current chain status.
  • 16.
    An education effortfrom Results Here you have the whole terminal output:
  • 17.
    An education effortfrom Lesson Review While this implementation is far from complete. It is capable of storing multiple transactions in a block and of giving rewards to miners. A basic example of how to implement blockchain and moreover how to build a basic cryptocurrency. The complete project code can be found in the following Git repository: https://github.com/juliomacr/blockchain-demo