How to design the basic structure of blockchain?

Adam Chen Longhui
3 min readDec 18, 2023

--

This article aims to introduce the basic terminologies of blockchain and to create a basic structure of blockchain using python.

What is blockchain?

A blockchain is a linked list, where each block contains data and a hash of the previous block, thereby creating a chain.

What is a block?

A block is a piece of data that has a unique hash value. If the content of the block changes, the hash value will change as well, and this is a fundamental property used to secure the blockchain

Creating a class for block

We can create a block using a ‘Block’ class and the ‘hashlib’ library.

import hashlib

class Block:
def __init__(self, index, transactions, timestamp, previous_hash):
self.index = index
self.transactions = transactions
self.timestamp = timestamp
self.previous_hash = previous_hash
self.hash = self.calculate_hash()

def calculate_hash(self):
block_string = f"{self.index}{self.transactions}{self.timestamp}{self.previous_hash}"
return hashlib.sha256(block_string.encode()).hexdigest()

A block will have the following attributes: index, transactions, timestamp, previous_hash

  • index: order of transaction (e.g. 1,2,..)
  • transactions: detail of the transaction (e.g. A sold 1 coin to B for $1)
  • timestamp : current time
  • previous_hash : hash value of the predecessor

‘calculate_hash’ is a method to create a unique hash value for this block. It uses the ‘hashlib’ library and the message digest algorithm ‘SHA256’ which is a popular cryptographic hashing functions to check data integrity.

What is hashing?

  • Hashing is like creating a unique id (known as hash value) for this block, any change to the block will change the hash value
  • Hashing in cryptocurrency refers to the process of taking an input (or ‘message’) and using a cryptographic hash function to produce an output, known as a hash value or hash code.

Creating a class for blockchain

We can create a class for blockchain, which is used to manage the chain of blocks created

class Blockchain:
def __init__(self):
self.chain = []
self.create_genesis_block()

def create_genesis_block(self):
genesis_block = Block(0, [], time.time(), '0')
self.chain.append(genesis_block)

def add_block(self, block):
block.previous_hash = self.chain[-1].hash # create attribute
block.hash = block.calculate_hash() # create attribute
self.chain.append(block)

def is_valid(self):
for i in range(1, len(self.chain)):
current = self.chain[i]
previous = self.chain[i-1]

if current.hash != current.calculate_hash():
return False
if current.previous_hash != previous.hash:
return False
return True

‘self.create_genesis_block()’ create a genesis block, which is the first block in the chain, and upon which more blocks are added

What is genesis block?

  • ‘self.create_genesis_block()’ create a genesis block, which is the first block in the chain, and upon which more blocks are added
  • the details of the genesis block are hardcoded into the blockchain’s software. Its previous hash value is often set to a default value, like ‘0’
  • integrity and security of blockchain is based on immutability of the genesis block. Genesis block cannot be changed as changing it will invalidate the entire blockchain
  • In blockchain, each block make reference to the hash of its predecessor block, linking all the way back to genesis block. Any change to the genesis block will change its hash value, hence making all blocks invalid because their references (previous hash values) would no longer match

Testing blockchain

blockchain = Blockchain()
blockchain.add_block(Block(1, ['Transaction 1'], time.time(), blockchain.chain[-1].hash))
blockchain.add_block(Block(2, ['Transaction 2'], time.time(), blockchain.chain[-1].hash))

print('Is blockchain valid? ', blockchain.is_valid())

for block in blockchain.chain:
print(f'Block {block.index}: {block.hash}')

########################

Is blockchain valid? True
Block 0: 5f484566721504458bf8dbdaebd1b22446302519d09e2db7c7a8a796530fd33f
Block 1: de1b63addfe83b096b8a82b7183eca3151942d3556023635a4e68f6f886c097b
Block 2: af0012e6c694ad3bbc5f61403b7f261458970078c7b3a657dc7001769ee2bc02

We create a blockchain object and added 2 blocks

  • Block 0 is the genesis block with hash value ‘5f484566721504458bf8dbdaebd1b22446302519d09e2db7c7a8a796530fd33f’
  • Block 1 is the first block added, with transaction detail as ‘Transaction 1’, its hash value is ‘de1b63addfe83b096b8a82b7183eca3151942d3556023635a4e68f6f886c097b’
  • Block 2 is the second block added, with transaction detail as ‘Transaction 2’, its hash value is ‘af0012e6c694ad3bbc5f61403b7f261458970078c7b3a657dc7001769ee2bc02’

The python code ‘(1) Create Basic Blockchain.ipynb’ is located at https://github.com/adam5644/Quant_Projects

--

--