0% found this document useful (0 votes)
21 views8 pages

Bitcion's Block Rewarding System Mechanism 2023179047

Uploaded by

Yogesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views8 pages

Bitcion's Block Rewarding System Mechanism 2023179047

Uploaded by

Yogesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

BITCOIN’S BLOCK REWARD MECHANISM

As a ASSIGNMENT in

(CA3005) BLOCKCHAIN TECHNOLOGIES

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:

 Index: The block's position in the chain.

 Previous Hash: The hash of the previous block.

 Transaction Fees: The fees collected from the transactions included in


the block.

 Miner's Address: The address of the miner who mined the block.

 Block Reward: The reward given to the miner for mining the block.

 Timestamp: The time at which the block was mined.

 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:

 Initializes the blockchain with a genesis block.

 Maintains the current block reward, which starts at 50 BTC.


 Tracks the number of blocks mined and reduces the block reward after
every 210,000 blocks (halving event).

 Uses the mine_new_block() method to simulate mining and reward


distribution, adding transaction fees to the miner's total reward.

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.

4. Mining Process and Halving:

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

INITIAL_REWARD = 50.0 # Initial block reward in bitcoins

HALVING_INTERVAL = 210000 # Number of blocks after which the reward is halved

BLOCK_TIME = 10 # Block time in minutes

MAX_BLOCKS = 630000 # Simulating up to 3 halving events


class Block:

def __init__(self, index, previous_hash, transaction_fees, miner_address, block_reward):

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()

def mine_block(self, difficulty):

while not self.hash.startswith('0' * difficulty):

self.nonce += 1

self.hash = self.compute_hash()

class Blockchain:

def __init__(self, difficulty):

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 = Block(0, "0", 0, "genesis_miner", self.current_block_reward)

genesis_block.mine_block(self.difficulty)

self.chain.append(genesis_block)

def get_last_block(self):

return self.chain[-1

def mine_new_block(self, transaction_fees, miner_address):

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

total_reward = self.current_block_reward + transaction_fees

print(f"Block {new_block.index} mined by {miner_address}. Reward: {total_reward}


BTC")

if self.blocks_mined % HALVING_INTERVAL == 0:

self.current_block_reward /= 2

print(f"Block reward halved to {self.current_block_reward} BTC at block


{self.blocks_mined}")

def print_chain(self):

for block in self.chain:

print(f"Block {block.index}:")

print(f" Hash: {block.hash}")

print(f" Previous Hash: {block.previous_hash}")

print(f" Miner: {block.miner_address}")

print(f" Block Reward: {block.block_reward} BTC")

print(f" Transaction Fees: {block.transaction_fees} BTC\n")

blockchain = Blockchain(difficulty=2)

for i in range(1, 51):

transaction_fees = round(0.1 + 0.9 * (i % 10) / 10, 2)

miner_address = f"miner_{i % 5 + 1}" # Simulate different miners

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.

You might also like