Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # CSW vs. Peter Rizun bet demo by awemany
  3. # Note this discretizes time into seconds for easier understanding. This will
  4. # however-so-slightly bias the calculations in here, due to coincidences etc.
  5.  
  6. import numpy as np
  7.  
  8. MEAN_BLOCK_TIME = 600
  9.  
  10. minute = 60
  11.  
  12. T0 = 10000
  13. Tmax = 20000
  14.  
  15. def blockTimes(tmax, hpf):
  16. """ Create a boolean array indexed by time (in seconds) where,
  17. 'block found' is marked with 'true' and 'no block found' is marked with false.
  18. hpf is hash power fraction. """
  19. r = np.zeros(tmax, bool)
  20.  
  21. nblocks = int(hpf * tmax / MEAN_BLOCK_TIME) # number of blocks expected
  22.  
  23. n=0
  24. while n < nblocks:
  25. time = np.random.randint(low = 0,
  26. high = tmax)
  27. r[time] = True
  28. n+=1
  29.  
  30. return r
  31.  
  32. def blockTimesButNotIn(tmax, tleft, tright, hpf):
  33. """ Like blockTimes(..) above, but will exclude blocks from being produced in the interval [tleft, tright). """
  34. r = np.zeros(tmax, bool)
  35.  
  36. nblocks = int(hpf * (tmax-(tright-tleft)) / MEAN_BLOCK_TIME) # number of blocks expected
  37.  
  38. n = 0
  39. while n < nblocks:
  40. time = np.random.randint(low = 0,
  41. high = tmax)
  42.  
  43. if time < tleft or time > tright:
  44. r[time] = True
  45. n+=1
  46.  
  47. return r
  48.  
  49. def nextBlockAfter(B, t):
  50. """ Returns number of seconds block comes in after t. Assumes a block comes
  51. after time t, might fail (very unlikely for large number of blocks). """
  52. return np.nonzero(B[t:])[0][0]
  53.  
  54.  
  55. # Scenario:
  56. # Normal operation, generate blocks every 10min and look for the delta time
  57. # Calculate delta time of next block after T0 for 10000 runs
  58. delta_times = [nextBlockAfter(blockTimes(Tmax, 1.0), T0) for _ in range(10000)]
  59.  
  60. # Scenario:
  61. # Hashing with 2/3rds of hash power. A priori condition that no block happened
  62. # in interval [T0, T0 + 10minutes).
  63. # Calculate delta time of next block after T0 for 10000 runs
  64. x_delta_times = [nextBlockAfter(blockTimesButNotIn(Tmax, T0, T0+10*minute, 2/3.), T0) for _ in range(10000)]
  65.  
  66. print "Mean delta time for block after T0:", np.mean(delta_times)
  67. print "Mean delta time for block after T0, with T0..T0+600s excluded:", np.mean(x_delta_times)
  68.  
  69. # Results: CSW is wrong and PeterR is right. 600s and 1500s (10min and 25min from T0)
  70.  
  71.  
  72. # optional part: histograms (change if to 0 if you don't care)
  73. if 1:
  74. from matplotlib import pyplot as plt
  75. plt.hist(x_delta_times, bins=100)
  76. plt.hist(delta_times, bins=100)
  77. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement