main.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import math
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import random
  5. L = 28948022309329048855892746252171976963363056481941560715954676764349967630337
  6. def target(f, rel_stake):
  7. T = L * (1 - (1-f)**rel_stake)
  8. return T
  9. def approx_target_in_zk(sigma_1, sigma_2, stake):
  10. # both sigma_1, sigma_2 are constants, if f is a constant.
  11. # if f is constant then sigma_12, sigma_2
  12. # this dictates that tuning need to be hardcoded,
  13. # secondly the reward, or at least the total stake in the network,
  14. # can't be anonymous, should be public.
  15. T = sigma_1 * stake + sigma_2*stake**2
  16. return T
  17. def approx_target(f, stake, Sigma):
  18. x = (1-f)
  19. c = math.log(x)
  20. k = L*c
  21. kp = k*c
  22. kpp = kp/2
  23. # approx sigma
  24. sigma_1 = -1 * k/Sigma
  25. sigma_2 = -1 * kpp/(Sigma**2)
  26. # sigma is in Z
  27. sigma_2 = int(sigma_2)
  28. sigma_1 = int(sigma_1)
  29. stake = int(stake)
  30. return approx_target_in_zk(sigma_1, sigma_2, stake)
  31. f = 0.5
  32. # let's assume stakeholde having 1% of the stake, 1/100.
  33. # each iteration increases stake by value 1.
  34. TOTAL = 10000
  35. S = []
  36. stake = 0
  37. T = []
  38. T_approx = []
  39. for i in range(TOTAL):
  40. if random.random()>=0.9:
  41. stake+=1
  42. S+=[(stake, i+1.0)]
  43. t = target(f, stake/(i+1.0))
  44. T+=[t]
  45. t_approx = approx_target(f, stake, (i+1.0))
  46. T_approx+=[t_approx]
  47. plt.plot(T)
  48. plt.plot(T_approx)
  49. plt.legend(["target", "approximation"])
  50. plt.savefig('plot.png')