Advertisement
Guest User

Untitled

a guest
Jan 7th, 2018
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. import random
  2.  
  3. # score1 is the number of blocks mined by the attacker
  4. # score2 is the amount of blocks mined by the network
  5. # p is the probability of mining (set to 1 here)
  6. # compute_power is the computation power of the attacker
  7. # confirmation_level is amount of blocks needed for a transaction to be considered confirmed
  8. # let_go is when the attacker give up
  9.  
  10. def test2(p, compute_power, confirmation_level, let_go):
  11.     score1 = score2 = 0
  12.     while True:
  13.         if random.random() <= compute_power:
  14.             # Just for clarity
  15.             if random.random() <= p:
  16.                 score1 += 1
  17.         else:
  18.             # Just for clarity
  19.             if random.random() <= p:
  20.                 score2 += 1
  21.  
  22.  
  23.         if (score1 >= score2) and (score2 >= confirmation_level):
  24.             return True, score1, score2
  25.  
  26.         if score2 >= score1 + let_go:
  27.             return False, score1, score2
  28.  
  29.  
  30. def metatest(amount = 100):
  31.     counter = 0
  32.     for i in xrange(amount):
  33.         if test2(1, 0.3, 6, 90)[0]:
  34.             counter += 1
  35.  
  36.     return counter/float(amount) # , counter
  37.  
  38.  
  39.  
  40. # OUTPUT:
  41. # >>> metatest()
  42. # 0.15
  43. # >>>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement