5.4 Creating a Chain and Validating Integrity

Sho Shimoda
2 min readDec 11, 2023

--

Part of “Mastering Blockchain Technology with C#: From Concepts to Implementation

In Section 5.4, “Creating a Chain and Validating Integrity,” we will focus on developing the chain of blocks and ensuring the integrity of our Blockchain. This step is crucial as it involves implementing mechanisms to maintain the security and validity of the entire chain.

Extending the Blockchain Class

After defining the basic structure of a Block, the next step is to create a series of these blocks — the Blockchain. Each block in the chain must be securely linked to its predecessor, ensuring that any alteration of data within a block is detected.

Here’s how we can extend our Blockchain class to add new blocks and validate the chain:

public class Blockchain
{
public IList<Block> Chain { get; private set; }

public Blockchain()
{
InitializeChain();
AddGenesisBlock();
}

public void AddBlock(Block newBlock)
{
Block latestBlock = GetLatestBlock();
newBlock.Index = latestBlock.Index + 1;
newBlock.PreviousHash = latestBlock.Hash;
newBlock.Hash = newBlock.CalculateHash();
Chain.Add(newBlock);
}

private Block GetLatestBlock()
{
return Chain.Last();
}

public bool IsValid()
{
for (int i = 1; i < Chain.Count; i++)
{
Block currentBlock = Chain[i];
Block previousBlock = Chain[i - 1];

if (currentBlock.Hash != currentBlock.CalculateHash())
{
return false;
}

if (currentBlock.PreviousHash != previousBlock.Hash)
{
return false;
}
}
return true;
}
}

Explanation of the Code

  • AddBlock Method: This method adds a new block to the chain. It sets the new block’s index to the next number in the sequence, calculates the new block’s hash, and links it to the previous block by setting the PreviousHash property.
  • GetLatestBlock Method: Retrieves the most recent block in the chain, which is used to link the new block.
  • IsValid Method: This method is crucial for maintaining the integrity of the Blockchain. It iterates through each block in the chain and verifies two conditions:
  • The hash of each block is still valid.
  • Each block points to the correct previous block (via the PreviousHash).

If any of these conditions fail, the method returns false, indicating that the chain has been tampered with.

Creating and Adding Blocks

With this setup, you can now create and add new blocks to the Blockchain. For example:

Blockchain bitcoinBlockchain = new Blockchain();
bitcoinBlockchain.AddBlock(new Block(bitcoinBlockchain.GetLatestBlock().Index + 1, bitcoinBlockchain.GetLatestBlock().Hash, "Block 2 Data"));
bitcoinBlockchain.AddBlock(new Block(bitcoinBlockchain.GetLatestBlock().Index + 1, bitcoinBlockchain.GetLatestBlock().Hash, "Block 3 Data"));

By the end of this section, you’ll have a functional Blockchain where you can add new blocks and verify the chain’s integrity. This forms the core of your Blockchain application, setting the stage for more advanced features like implementing consensus mechanisms and handling transactions, which we will explore in the subsequent sections.

Next: 5.5 Implementing Transactions and a Basic Consensus Algorithm

--

--

Sho Shimoda

CEO/CTO of {RECEIPT}ROLLER. We offer easy digital receipt solutions for all POS and e-commerce, eliminating paper waste.