utils.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import random
  2. import math
  3. import numpy as np
  4. from core.constants import *
  5. # naive factorial
  6. def fact(n, hp=False):
  7. assert (n>0)
  8. n = int(n)
  9. if n==1:
  10. return Num(1) if hp else 1
  11. elif n==2:
  12. return Num(2) if hp else 2
  13. else:
  14. return (Num(n) if hp else n)* fact(n-1, hp)
  15. """
  16. approximate ouroboros phi function
  17. all inputs to this function are integers
  18. @param sigmas: n sigmas of n-term approximation of phi target function
  19. @param stake: stakeholder stake
  20. @returns: target value T
  21. """
  22. def approx_target_in_zk(sigmas, stake):
  23. # both sigma_1, sigma_2 are constants, if f is a constant.
  24. # if f is constant then sigma_12, sigma_2
  25. # this dictates that tuning need to be hardcoded,
  26. # secondly the reward, or at least the total stake in the network,
  27. # can't be anonymous, should be public.
  28. T = 0
  29. for i, sigma in enumerate(sigmas):
  30. try:
  31. T += Num(sigma)*Num(stake)**(i+1)
  32. except Exception as e:
  33. T +=0
  34. return-1*T
  35. def rnd(hp=False):
  36. return Num(random.random()) if hp else random.random()
  37. def lottery(T, hp=False, log=False):
  38. y = rnd(hp) * (L_HP if hp else L)
  39. if log:
  40. lottery_line = str(y)+","+str(T)+"\n"
  41. with open("/tmp/sim_lottery_history.log", "a+") as f:
  42. f.write(lottery_line)
  43. won = y < T if y is not None and T is not None else False
  44. return won, y