5.3 Building a Basic Block Structure

Sho Shimoda
2 min readDec 11, 2023

--

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

In this section, 5.3 “Building a Basic Block Structure,” we will take a step-by-step approach to create the fundamental building block of our Blockchain — the Block. This section is crucial as it lays the groundwork for the entire Blockchain system.

Designing the Block Class

A block in a Blockchain typically contains a set of transactions, its own hash (a unique identifier), the hash of the previous block (to maintain the chain), a timestamp, and other relevant data.

Here’s a basic example of what the Block class in C# might look like:

using System;
using System.Security.Cryptography;
using System.Text;

public class Block
{
public int Index { get; private set; }
public DateTime TimeStamp { get; private set; }
public string PreviousHash { get; private set; }
public string Hash { get; private set; }
public string Data { get; private set; }

public Block(int index, string previousHash, string data)
{
Index = index;
TimeStamp = DateTime.UtcNow;
PreviousHash = previousHash;
Data = data;
Hash = CalculateHash();
}

public string CalculateHash()
{
using (SHA256 sha256 = SHA256.Create())
{
string rawData = $"{Index}{TimeStamp}{PreviousHash}{Data}";
byte[] bytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(rawData));
return BitConverter.ToString(bytes).Replace("-", "").ToLower();
}
}
}

Explanation of the Code

  • Index: It’s a unique value to identify each block. It’s typically a sequence number.
  • TimeStamp: The date and time when the block was created.
  • PreviousHash: The hash of the previous block in the chain. This is what creates the link in the Blockchain.
  • Hash: A unique identifier for the current block, calculated based on the block’s content.
  • Data: The actual data stored in the block. In the case of cryptocurrencies, this would be transaction details.

The CalculateHash method generates a hash value for the block. It uses SHA256, a cryptographic hash function, to create a hash from the block's index, timestamp, previous hash, and data. This hash ensures the integrity of the block and the chain.

Building the First Block — The Genesis Block

The first block in any Blockchain is known as the Genesis Block. It’s special because it does not have a preceding block. Here’s how you might initialize the Genesis Block:

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

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

private void InitializeChain()
{
Chain = new List<Block>();
}

private void AddGenesisBlock()
{
Chain.Add(new Block(0, "0", "Genesis Block"));
}
}

By completing this section, you’ll understand the structure and function of a block within a Blockchain. You will also have laid the foundation for your Blockchain system by coding the Genesis Block. In the upcoming sections, we’ll expand on this foundation, adding more blocks and introducing key concepts like chain integrity and consensus algorithms.

Next: 5.4 Creating a Chain and Validating Integrity

--

--

Sho Shimoda

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