Last active
July 28, 2021 00:24
-
-
Save Rucknium/be6ee0dc2290e12f221105623ad9cd16 to your computer and use it in GitHub Desktop.
Simulates the probability of a double-spend attack succeeding over a 10 minute interval, with 10 minute vs 1 minute target block interval times
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simulations carried out by the R statistical language: https://www.r-project.org/ | |
set.seed(314) | |
# Set the random seed to make the results reproducible | |
current.target.block.time <- 10 | |
modified.target.block.time <- 1 | |
hashpower.honest.miners <- 0.70 | |
hashpower.malicious.miner <- 0.30 | |
n.simulations <- 1000000 | |
# One million simulations | |
normal.operations <- rexp(n.simulations) * current.target.block.time / (hashpower.honest.miners + hashpower.malicious.miner) | |
# Draw n.simulations from an exponential distribution, scaled by block time and hashpower | |
mean(normal.operations) | |
# 10.00411 | |
# Mean block time is 10.00411 minutes | |
honest.blocks.attack.current <- rexp(n.simulations) * current.target.block.time / hashpower.honest.miners | |
malicious.blocks.attack.current <- rexp(n.simulations) * current.target.block.time / hashpower.malicious.miner | |
# Now, split the hashpower by honest and malicious miners | |
mean(honest.blocks.attack.current) | |
# 14.32075 | |
# Mean time between blocks found by the honest miners is 14.32075 | |
mean(malicious.blocks.attack.current) | |
# 33.2966 | |
# Mean time between blocks found by the malicious miner is 33.2966 | |
mean(malicious.blocks.attack.current < honest.blocks.attack.current) | |
# 0.300555 | |
# On average, the malicious miner finds a block before the honest miners in 30.0555% of cases | |
########################################################################################## | |
# Now, simulate a one-minute block time | |
# How often will the malicious miner find 10 blocks before the honest miners find 10 blocks? | |
########################################################################################## | |
honest.blocks.sum.v <- vector("numeric", length = n.simulations) | |
malicious.blocks.sum.v <- vector("numeric", length = n.simulations) | |
for (i in seq_len(n.simulations)) { | |
honest.blocks.attack.modified <- rexp(current.target.block.time) * modified.target.block.time / hashpower.honest.miners | |
malicious.blocks.attack.modified <- rexp(current.target.block.time) * modified.target.block.time / hashpower.malicious.miner | |
# Draw 10 values from an exponential distribution for both honest and malicious miners | |
honest.blocks.sum.v[i] <- sum(honest.blocks.attack.modified) | |
malicious.blocks.sum.v[i] <- sum(malicious.blocks.attack.modified) | |
# Take the sums. This is the total time it took each set of miners to find 10 blocks | |
} | |
mean(malicious.blocks.sum.v < honest.blocks.sum.v) | |
# 0.032736 | |
########################################################################################## | |
# KEY RESULT: | |
# In 3.2736% of cases, the malicious miner finds 10 blocks before the honest miners finds 10 blocks | |
########################################################################################## | |
mean(honest.blocks.sum.v/current.target.block.time) | |
# 1.429406 | |
# Mean time between blocks found by the honest miners is 1.429406 minutes | |
mean(malicious.blocks.sum.v/current.target.block.time) | |
# 3.334569 | |
# Mean time between blocks found by the malicious miner is 3.334569 minutes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment