Skip to content

Instantly share code, notes, and snippets.

@kristovatlas
Last active December 27, 2017 22:40
Show Gist options
  • Save kristovatlas/8dcc02513f65a96cd6f3b3abcfa79d6a to your computer and use it in GitHub Desktop.
Save kristovatlas/8dcc02513f65a96cd6f3b3abcfa79d6a to your computer and use it in GitHub Desktop.
simulating the broadcasting of Bitcoin transactions and first-hop reception by sybil attacker
import itertools
from random import randint
#p = 0.95
OTHER_NODES = 10000
MY_NODES = 4410
MY_CONNECTIONS = 8 * MY_NODES #my nodes are malicious and make 8 connections per peer
ALL_NODES = OTHER_NODES + MY_CONNECTIONS
NUM_BROADCASTED = 2
TRIALS = 1000000
TRIALS_WON = 0
TRIALS_LOST = 0
for _ in itertools.repeat(None, TRIALS):
for x in itertools.repeat(None, NUM_BROADCASTED):
select = randint(1, ALL_NODES)
win = False
#print select
if select <= MY_CONNECTIONS:
TRIALS_WON += 1
#print "won!"
win = True
break
if not win:
TRIALS_LOST += 1
#print "lost!"
print "Stats for {0} trials: ".format(TRIALS)
print "\t # trials in which I was one of the first-hop broadcasts: {0}".format(TRIALS_WON)
print "\t # trials in which I wasn't one of the first-hop broadcasts: {0}".format(TRIALS_LOST)
print "\t Fraction my nodes: {0}".format(float(TRIALS_WON)/(TRIALS_WON+TRIALS_LOST))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment