Last active
August 29, 2015 14:03
-
-
Save afk11/bbd9657b91e9a68d054f to your computer and use it in GitHub Desktop.
Print the time to wait before each pool has < 1% probability of producing a longer chain, based on current pool hashrates.
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
<?php | |
// Average block time in minutes. | |
$avg_block_time = file_get_contents("https://blockchain.info/q/interval")/60; | |
// Pools page.. | |
$pools_page = file_get_contents("https://blockchain.info/pools?timespan=4days"); | |
// Obtain the JSON string blockchain embeds in their page for the graph | |
$lines = explode("\n",$pools_page); | |
foreach($lines as $i => $l) { | |
if($i == 16){ | |
$len = strlen($l); | |
$start = 568; | |
$from_end = 6; | |
$delta = $len-($start+$from_end); | |
$str = "{\"data\"".str_replace("'",'"',substr($l, $start, $delta))."}"; | |
$pool_hashrate = json_decode($str); | |
} | |
} | |
echo "Average block time: {$avg_block_time} minutes<br /><br />"; | |
foreach($pool_hashrate->data as $pool) { | |
$blocks = prob($pool[1]); | |
$time = ($avg_block_time)*$blocks; | |
echo "{$pool[0]} has {$pool[1]}% of the hashrate: <br />\nWait {$blocks} blocks, or {$time} minutes for 99% certainty a double spend cannot occur<br /><br />"; | |
} | |
// Taken from bitcoin whitepaper | |
function AttackerSuccessProbability($ratio, $blocks_deep) { | |
$q = $ratio; | |
$z = $blocks_deep; | |
$p = 1.0 - $q; | |
$sum = 1.0; | |
$lambda = $z * ($q/$p); | |
for($k = 0; $k <= $z; $k++) { | |
$poisson = exp((0-$lambda)); | |
for($i = 1; $i <= $k; $i++) | |
$poisson *= $lambda / $i; | |
$sum -= $poisson * (1 - pow($q/$p, ($z - $k))); | |
} | |
return number_format($sum,7); | |
} | |
function prob($percentage_hash) { | |
// Convert % hashrate to ratio. | |
$ratio = $percentage_hash/100; | |
$i = 0; | |
$a = AttackerSuccessProbability($ratio, $i); | |
// Loop until probability finally is greater than 0.01 | |
while($a > 0.01) { | |
$i++; | |
$a = AttackerSuccessProbability($ratio, $i); | |
} | |
// Return number of blocks taken to reach this probability, at this hashrate | |
return $i; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment