TeachMeBitcoin

Custom Python Soft Fork Simulator

From TeachMeBitcoin, the free encyclopedia Reading time: 3 min

Custom Python Soft Fork Simulator

A Soft Fork works by tightening the rules. In this simulation, we will model a "New Rule" node that enforces a stricter transaction format, while an "Old Rule" node remains oblivious to the change but still accepts the new blocks.

The Python Soft Fork Simulator

import hashlib

class Transaction:
    def __init__(self, sender, amount, metadata=""):
        self.sender = sender
        self.amount = amount
        self.metadata = metadata # New rule: must not be empty!

class Node:
    def __init__(self, name, enforce_new_rules=False):
        self.name = name
        self.enforce_new_rules = enforce_new_rules
        self.blockchain = []

    def validate_tx(self, tx):
        # Old nodes allow empty metadata
        # New nodes REJECT empty metadata (Rule Tightening)
        if self.enforce_new_rules and not tx.metadata:
            return False
        return True

    def receive_block(self, block):
        for tx in block:
            if not self.validate_tx(tx):
                print(f"[!] {self.name}: REJECTED block. TX with empty metadata is invalid!")
                return False

        self.blockchain.append(block)
        print(f"[+] {self.name}: ACCEPTED block with {len(block)} transactions.")
        return True

# --- Simulation ---

# 1. Initialize Nodes
old_node = Node("Old Node (v0.1)", enforce_new_rules=False)
new_node = Node("New Node (v0.2)", enforce_new_rules=True)

# 2. A miner produces a block following NEW RULES (stricter)
print("--- Round 1: Validating a 'Strict' Block ---")
strict_tx = Transaction("Alice", 1.0, metadata="SECURE_ID_123")
block1 = [strict_tx]

old_node.receive_block(block1)
new_node.receive_block(block1)

# 3. A miner produces a block following OLD RULES (loose)
print("\n--- Round 2: Validating a 'Loose' Block (Soft Fork Violation) ---")
loose_tx = Transaction("Bob", 2.0, metadata="") # Empty metadata
block2 = [loose_tx]

old_node.receive_block(block2) # This will be accepted (loose rules)
new_node.receive_block(block2) # This will be REJECTED (strict rules)

# 4. Summary
print("\n--- Final Status ---")
print(f"Old Node Chain Height: {len(old_node.blockchain)}")
print(f"New Node Chain Height: {len(new_node.blockchain)}")

How to Run the Simulator

  1. Ensure you have Python 3 installed.
  2. Copy the code into a file named soft_fork_sim.py.
  3. Run it using python3 soft_fork_sim.py.

Technical Takeaways

  1. Forward Compatibility: The old_node successfully accepted the "Strict" block because it didn't violate its original loose rules.
  2. Stricter Validation: The new_node rejected the "Loose" block because it was enforcing a new requirement (metadata must exist).
  3. Miner Enforcement: In a real soft fork, as long as >50% of miners run the new_node logic, they will only build on "Strict" blocks. The old_node will follow them because the "Strict" blocks are valid and have the most proof of work.

Congratulations! You have completed the Soft Fork module. You now understand how Bitcoin upgrades its engine while the car is still driving at 100mph.

☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!