Introduction
In my previous article, I talked about Proof of Work. Proof of Work requires a lot of computing time, in turn, it uses a lot of electricity. One example is that the power consumed by the entire Bitcoin network in November 2017 is higher than that of Ireland. All the nodes in the Bitcoin network are volunteers that they “donate” their computing time to the network. The question is why people want to connect their computer to a Blockchain network and use their computing time to help with Proof of Work? The answer is an award.
Please check previous articles,
Hash, Mining, and Award
The most popular Blockchain network at this moment is Bitcoin. It is a digital currency that has no central authority. Proof of Work allows Bitcoin to maintain security and validity without a middleman, bank. However, the price is a lot of computing time, therefore a lot of electricity. The computing time used in Proof of Work is to fulfill cryptographic requirements. The cryptographic requirements are in place for data validation. More specifically, a hash of a block is generated for validating the block by any nodes. The SHA256 hash algorithm guarantees that the generated hash code is completely random. It is chosen by Blockchain as the hash algorithm. To generate a hash is trivial, but to generate a specific hash is difficult and time-consuming.
The goal is to find a hash that matches with a dictated difficulty. The only way to find the right hash is the brute force approach, which means try it again and again. The finding hash process is done by the entire network. Every node in the network competes with other nodes to find the right hash and to get the award. The award is a new transaction that gives a certain amount of digital currency to the node who found a right hash and added a new block. This process is called mining and computers which help to find a new hash code and add a new block are called a miner. The awarded currency is a new currency in circulation. So, the ming process is also a currency creating process in Blockchain.
Transaction
Before we update our Blockchain to provide award feature, we want to refactor our code to have a clearly defined transaction in place

A new Class, Transaction, is created to contain information that was contained in JSON format.
Transaction
- public class Transaction
- {
- public string FromAddress { get; set; }
- public string ToAddress { get; set; }
- public int Amount { get; set; }
- public Transaction(string fromAddress, string toAddress, int amount)
- {
- FromAddress = fromAddress;
- ToAddress = toAddress;
- Amount = amount;
- }
- }
After added Transaction class, we can update our Block to replace Data with Transactions. One misunderstanding I had before was that one Blockchain can only contain one transaction. Actually, one block can contain many transactions. Therefore, we uses a collection to store transaction data.
- public IList<Transaction> Transactions { get; set; }
The CalculateHash method is also updated to use Transactions instead of Data to get hash of a block.
- public string CalculateHash()
- {
- SHA256 sha256 = SHA256.Create();
- byte[] inputBytes = Encoding.ASCII.GetBytes($"{TimeStamp}-{PreviousHash ?? ""}-{JsonConvert.SerializeObject(Transactions)}-{Nonce}");
- byte[] outputBytes = sha256.ComputeHash(inputBytes);
- return Convert.ToBase64String(outputBytes);
- }
Blockchain
The new block generating process is a time-consuming process. But, a transaction can be submitted anytime, so we need to have a place to store transactions before they are processed. Therefore, we added a new field, PendingTransactions, to store newly added transactions.
- IList<Transaction> PendingTransactions = new List<Transaction>();
We added a CreateTransaction method to add a new transaction to the PendingTransaction collection.
- public void CreateTransaction(Transaction transaction)
- {
- PendingTransactions.Add(transaction);
- }





Radek WysockiPosted Feb 18, 2021, 9:58 PM
Forgot to write up the CreateGenesisBlock() change
Warit TaveekarnPosted May 8, 2020, 2:03 PM
Absolutely perfect!, good for starting to understand how the BlockChain works in action. Many Thanks to your article.
宝泽 韩Posted Jan 2, 2020, 2:31 AM
Thanks very much. I have read several books about blockchain. But your articles give me more understanding.
Tran Van NhatPosted Jul 19, 2018, 11:13 PM
Hello henry. I am learning Bloackchain, i don't know blockchain project structure. I hoping you can post new document by help. Best regard
KeithPosted Jun 20, 2018, 1:54 PM
Thanks for posting this series - :) - I was just wondering, did you purposely leave the "GetBalance" stuff from the series to allow us to refactor it ourselves our was accidentally left out?
Tomasz ZyskPosted Jun 15, 2018, 4:06 PM
Great series, can't wait for the next part. It feels as though you missed some parts of code and explanation in this part comparing to the previous ones. Had to follow both sample and article (it's not really any issue, just saying). @EDIT I've just noticed you already added next part, just haven't linked it in this article. (Also, how one does put new line in comments here :|)