Bitcion's Block Rewarding System Mechanism 2023179047
Bitcion's Block Rewarding System Mechanism 2023179047
As a ASSIGNMENT in
Submitted by
YOGESHWARAN R (2023179047)
INTRODUCTION
This project implements Bitcoin’s block reward mechanism using Python. It simulates
block mining, where miners are rewarded with block rewards and transaction fees. It follows
Bitcoin's halving schedule, in which the block reward is reduced by half after every 210,000
blocks. The simulation provides insights into how the decreasing block reward affects the
long-term security of the Bitcoin network.
OBJECTIVE
To simulate block mining, where miners receive block rewards and transaction fees.
To implement Bitcoin's halving event, reducing rewards by half every 210,000 blocks.
To understand how the decreasing block reward influences the profitability of mining
and network security over time.
To analyze how transaction fees become more significant as block rewards decrease.
THINGS TO KNOW
Block Reward: The reward given to a miner for successfully mining a block. In Bitcoin, this
reward halves approximately every four years (210,000 blocks).
Halving Event: A programmed event in Bitcoin where the block reward is reduced by 50%
after every 210,000 blocks mined. This ensures that the total supply of Bitcoin is capped at 21
million.
Transaction Fees: Additional fees included in each block, which are also awarded to the
miner. As block rewards decrease, these fees become a more significant part of the miner's
reward.
Proof of Work (PoW): The process of validating a block by solving a computational puzzle
(mining). The block is considered valid when its hash begins with a specific number of
leading zeros, determined by the difficulty level.
Blockchain: A public ledger of blocks, each containing transactions and other details. The
blocks are linked cryptographically, with each new block containing the hash of the previous
one.
PROCEDURE
1. Block Class:
o The Block class defines the structure of a block in the blockchain. Each
block has the following attributes:
Miner's Address: The address of the miner who mined the block.
Block Reward: The reward given to the miner for mining the block.
Nonce: A number that is changed to find a valid hash for the block.
o The compute_hash() method calculates the hash of the block, combining its
data.
o The mine_block() method implements the proof of work algorithm. The block
is mined when its hash begins with a number of leading zeros, based on the
difficulty level.
2. Blockchain Class:
o The Blockchain class is responsible for managing the chain of blocks. It:
3. Mining Simulation:
o The blockchain simulates the mining process, where different miners mine
blocks and are rewarded with block rewards plus transaction fees.
o Random transaction fees are generated for each block, and different miners
(simulated by miner_address) are rewarded.
o Every 210,000 blocks, the block reward is halved, mimicking Bitcoin’s real-
world behavior.
o During mining, if the number of blocks reaches the halving interval (210,000
blocks), the block reward is halved, decreasing the miner's reward.
o The process repeats until the maximum number of blocks is reached or the
halving event occurs multiple times.
CODE
import time
import hashlib
self.index = index
self.previous_hash = previous_hash
self.transaction_fees = transaction_fees
self.miner_address = miner_address
self.block_reward = block_reward
self.timestamp = time.time()
self.nonce = 0
self.hash = self.compute_hash()
def compute_hash(self):
block_data = f"{self.index}{self.previous_hash}{self.transaction_fees}
{self.block_reward}{self.timestamp}{self.nonce}"
return hashlib.sha256(block_data.encode()).hexdigest()
self.nonce += 1
self.hash = self.compute_hash()
class Blockchain:
self.chain = []
self.difficulty = difficulty
self.current_block_reward = INITIAL_REWARD
self.blocks_mined = 0
self.create_genesis_block()
def create_genesis_block(self):
genesis_block.mine_block(self.difficulty)
self.chain.append(genesis_block)
def get_last_block(self):
return self.chain[-1
previous_block = self.get_last_block()
new_block = Block(
index=previous_block.index + 1,
previous_hash=previous_block.hash,
transaction_fees=transaction_fees,
miner_address=miner_address,
block_reward=self.current_block_reward
new_block.mine_block(self.difficulty)
self.chain.append(new_block)
self.blocks_mined += 1
if self.blocks_mined % HALVING_INTERVAL == 0:
self.current_block_reward /= 2
def print_chain(self):
print(f"Block {block.index}:")
blockchain = Blockchain(difficulty=2)
blockchain.mine_new_block(transaction_fees, miner_address)
blockchain.print_chain()
OUTPUT
OBSERVATION
The simulation successfully mines blocks and rewards miners based on block rewards
and transaction fees.
Every 210,000 blocks, the block reward is halved. After three halving events, the
block reward decreases significantly, mimicking Bitcoin’s real-world halving
mechanism.
Observation: As block rewards decrease, miners will rely more on transaction fees to
stay profitable. This mechanism ensures a controlled release of new bitcoins into
circulation, but the decreasing rewards may pose a challenge to long-term network
security if transaction fees don’t compensate miners adequately.