Custom Python Attacker Simulator
From TeachMeBitcoin, the free encyclopedia
Reading time: 3 min
Building an Attacker Lead-Time Simulator in Python
To visualize the probability of a successful 51% attack, we can build a simulator that tracks the "Lead Time." This is the number of blocks an attacker can find privately compared to the honest network. We will use this to calculate the success rate for a Double Spend attack given a certain number of confirmations.
1. The Attacker Lead-Time Simulator Source Code
Save the following Python script as attacker_lead_sim.py.
import random
def simulate_attack_race(attacker_hashrate_pct, confirmations_required, max_steps=1000):
"""
Simulates a race between an attacker and the honest network.
"""
q = attacker_hashrate_pct / 100.0
p = 1.0 - q
# The attacker starts z blocks behind the honest network's head
# We assume the merchant waits for 'confirmations_required' blocks.
honest_blocks = confirmations_required
attacker_blocks = 0
steps = 0
while steps < max_steps:
# Who finds the next block?
if random.random() < q:
attacker_blocks += 1
else:
honest_blocks += 1
# If attacker overtakes the honest network
if attacker_blocks >= honest_blocks:
return True, steps
steps += 1
return False, steps
def run_simulation_suite(hashrates, confs, trials=1000):
print(f"{'Hashrate %':<12} | {'Confirms':<10} | {'Success Rate %':<15}")
print("-" * 45)
for h in hashrates:
for c in confs:
successes = 0
for _ in range(trials):
success, _ = simulate_attack_race(h, c)
if success:
successes += 1
rate = (successes / trials) * 100
print(f"{h:<12}% | {c:<10} | {rate:<15.2f}%")
if __name__ == "__main__":
# Test different scenarios
hashrate_scenarios = [10, 30, 48, 51, 55]
confirmation_scenarios = [3, 6, 12]
run_simulation_suite(hashrate_scenarios, confirmation_scenarios)
️ 2. Analyzing the Data
When you run this script, you will notice three distinct regimes of network security:
- Low Hashrate ($q < 30\%$): The success rate is effectively 0% for 6+ confirmations. The math of exponential decay ($q/p)^z$ is working perfectly to protect the network.
- Near-Majority ($q = 48\%$): Even with 48% hashrate, an attacker still has a low success rate (~10-20% for 6 confirms). They are fighting against the statistical weight of the honest majority.
- The Threshold ($q \ge 51\%$): Once the hashrate crosses 51%, the success rate jumps significantly. For $q=55\%$, an attacker will succeed in reversing the chain almost 100% of the time given enough steps.
Key Takeaways
- Confirmations are the Lever: Notice how moving from 3 to 12 confirmations drastically reduces the success rate for an attacker with 48% hashrate, but has no effect on an attacker with 55% hashrate.
- The 51% Limit: This simulation proves that Bitcoin's security model is binary. If the honest majority holds, the network is secure. If they lose the majority, the "Time-Wait" defense eventually fails.
☕ 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