Advertisement
Guest User

Simulator # 11

a guest
Apr 25th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "flag"
  5. "fmt"
  6. "time"
  7. "math"
  8. "math/rand"
  9. "math/big"
  10. "os"
  11. "strconv"
  12. )
  13.  
  14. var block = 4 //block count ( for genesis block = 0 )
  15.  
  16. var pi = float64(3.1415926535897932384626433832795)
  17.  
  18. var DCoef = new(big.Int)
  19. var BCoef = new(big.Int)
  20.  
  21.  
  22. //var coef = float64(0.43429448190325182765112891891661)
  23. var coef = float64(4.3429448190325182765112891891661)
  24.  
  25. var year = 262980
  26.  
  27. //var percent_year = 0.0314159
  28. var percent_year = 0.005
  29.  
  30. var percent_day_multi = math.Pow(1+percent_year, float64(1.0/365.0))
  31. var percent = (percent_day_multi - 1) / 720.0
  32.  
  33. var Coin = int64(1000000)
  34. var Cent = int64(10000)
  35.  
  36. var mplus, wMplus, sMplus float64
  37.  
  38. var stakeTgtSpacing = 600
  39. var tgtTimeSpan = int64(7 * 24 * 60 * 60)
  40.  
  41. var maxWreward = float64(99.99)
  42. //var wDiff = float64(1.0)
  43. var sDiff = 0.0001
  44.  
  45.  
  46. //blockchain index for 200 years
  47. var pindex = make([]int64, 52596006)
  48. var targets = make([]big.Int, 52596006)
  49.  
  50. var supply = 0.0 //moneysupply
  51. var fees = 0.0
  52.  
  53. var wHour = 29 //PoW blocks' rate per 1 hour
  54. var shour = 1 // PoS blocks' rate per 1 hour
  55.  
  56. var maxTgt = "26959535291011309493156476344723991336010898738574164086137773096960"
  57. var Limit = "26959123914871979191645937602428351998384653054607755691171935944704"
  58.  
  59.  
  60.  
  61. func getWdiff() float64 {
  62.  
  63. actSpacing := pindex[block - 1] - pindex[block - 2]
  64. tgtSpacing := 61 * (1 + pindex[block - 1] - pindex[block - 2])
  65. // tgtSpacing := int64(600 * (1 + 1))
  66.  
  67. if tgtSpacing==0 { tgtSpacing = 1 }
  68. interval := int64(tgtTimeSpan / tgtSpacing)
  69. // fmt.Println("Interval=", interval)
  70. bits := new(big.Int)
  71. bits = &targets[block - 1]
  72. // fmt.Println("Bits_0=", bits.String())
  73. bits.Mul(bits, big.NewInt((interval + int64(1)) * tgtSpacing + actSpacing + actSpacing))
  74. bits.Div(bits, big.NewInt((interval - int64(1)) * tgtSpacing))
  75.  
  76. bitsTest := new(big.Int)
  77. if block > 95 {
  78. bitsTest.Sub(bits, &targets[block - 60])
  79. // fmt.Println("BitsTest=", bitsTest.String(), bitsTest.Sign())
  80. cmp := bitsTest.Cmp(&targets[block - 30])
  81. if cmp == 1 {
  82. // bits.Add(&targets[block - 10], bits)
  83. bits.Div(bits, big.NewInt(2))
  84. // bits.Mul(bits, big.NewInt(2))
  85. fmt.Println("B/2 ====", bits.String())
  86. }
  87. }
  88. if bits.Cmp(&targets[0])==1 { bits = &targets[0] }
  89. // fmt.Println("Bits_3=", bits.String())
  90. targets[block].Set(bits)
  91.  
  92. diff := new(big.Int)
  93. // if block > 3 {
  94. diff.Div(&targets[0], bits)
  95. // diff.Set(&targets[0])
  96. // } else {
  97. // return float64(1.0)
  98. // }
  99. // fmt.Println("diff=", diff.String())
  100. // return float64(diff.Int64()) / float64(bits.Int64())
  101. return float64(diff.Int64())
  102. }
  103.  
  104. //nth root function
  105. func root(a float64, n int) float64 {
  106. n1 := n - 1
  107. n1f, rn := float64(n1), 1/float64(n)
  108. x, x0 := 1., 0.
  109. for {
  110. potx, t2 := 1/x, a
  111. for b := n1; b > 0; b >>= 1 {
  112. if b&1 == 1 {
  113. t2 *= potx
  114. }
  115. potx *= potx
  116. }
  117. x0, x = x, rn*(n1f*x+t2)
  118. if math.Abs(x-x0)*1e15 < x {
  119. break
  120. }
  121. }
  122. return x
  123. }
  124.  
  125. func main() {
  126. flag.Parse()
  127. wReward := 0.0
  128. // sReward := 0.0
  129. // suppDelta := 0.0
  130. supplyPrev := 0.000000001
  131. targets[0].SetString(maxTgt, 10)
  132. targets[1].SetString(maxTgt, 10)
  133. targets[2].SetString(maxTgt, 10)
  134. targets[3].SetString(Limit, 10)
  135. targets[4].SetString(Limit, 10)
  136. // fmt.Println(targets[3].String())
  137. fl, _ := strconv.ParseInt(flag.Arg(0), 10, 8)
  138.  
  139. //years to mine in blocks
  140. eoTime := year*int(fl) + 2
  141.  
  142. pindex[0] = time.Now().Unix()
  143. pindex[1] = pindex[0] + 120
  144. pindex[2] = pindex[1] + 120
  145. pindex[3] = pindex[2] + 120
  146. pindex[4] = pindex[3] + 120
  147. // fmt.Println("started at : ", pindex[0])
  148.  
  149. rnd := rand.New(rand.NewSource((int64(time.Now().Nanosecond()))))
  150. for {
  151. if block < 10000000000 {
  152. wDiff := getWdiff()
  153. // fmt.Println("diff=", strconv.FormatFloat(wDiff, 'f', -1, 64))
  154.  
  155. // wReward = math.Log(wDiff) - (supply * percent)
  156. // wReward = maxWreward / root(wDiff, 6)
  157. // wReward = root(wDiff, 3) + root(wDiff, 6)// + (supply * percent)
  158.  
  159. // wReward = root(wDiff, 2) - (supply * percent)
  160. // wReward = root(wDiff, 2) - (supply * percent) + 3 * root(float64(block), 2)
  161. // wReward = root(wDiff, 2) + math.Sqrt(float64(1 / block))
  162.  
  163. // wReward = math.Sqrt(float64(9999 * block)) / 5000.0
  164. // wReward = math.Sqrt(wDiff) - root(float64(block), 2) / 100.0
  165.  
  166. // wReward = wDiff / 2.0
  167. wReward = math.Sqrt(wDiff) + (supply * percent)
  168.  
  169. // wReward = (2.0 * math.Sqrt(wDiff) + wDiff / float64(block * block))/2.0
  170.  
  171.  
  172. // fmt.Println("PoW bounty: ", wReward, "wDiff=", wDiff)
  173. // if block > 10 {
  174. // wDiff = (1.005*wDiff + rnd.Float64()*111111.0 + 1.0) / 2
  175. // }
  176. // if wDiff > 64000.0 {
  177. // wDiff = 63999.0
  178. // }
  179. block++
  180. freq := 121//average time between blocks
  181. // if wDiff > 63300000.0 { freq = 119 }
  182. if wDiff > 670.0 { freq = 119 }
  183. after := rnd.Int63n(240) - int64(freq)//power input point ;)
  184.  
  185. pindex[block] = pindex[block - 1] + after
  186. // fmt.Println(block, pindex[block] - pindex[block-1])
  187. }
  188. // if block >= 1000000 { wReward = 0.0 }
  189. txFees := rnd.Float64() / 1.35
  190. if txFees > 0.66 {
  191. txFees = 0.02
  192. }
  193. // coinAge := rnd.Int63n(89970) + 30 //pseudorandom coin_age in days
  194. // sReward := float64((33*coinAge)/12053) * 0.01
  195.  
  196. // fmt.Println("PoS: ", sReward, "Fees=", txFees)
  197. // sRewardAndFees := sReward + txFees
  198.  
  199. // block++
  200. // fmt.Println("")
  201. supply = supply + wReward// + sReward
  202. fees = fees + txFees
  203. if (block % year) == 0 {
  204. supplyDelta := (supply / supplyPrev - 1.0) * 100
  205. fmt.Println("coinsupply= ", strconv.FormatFloat(supply, 'f', -1, 64), " at year #", (block / year))
  206. fmt.Println("+ from prev. year=", supplyDelta, "%")
  207. // fmt.Println("tx_fees =", fees)
  208. fmt.Println("PoW bounty: ", wReward, "Diff=", getWdiff())
  209. // fmt.Println("PoS: ", sReward, "Fees=", txFees)
  210. fmt.Println("==========================================================")
  211. supplyPrev = supply
  212. }
  213. if block > eoTime {
  214. break
  215. }
  216. }
  217.  
  218. os.Exit(0)
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement