Advertisement
Guest User

ShelvedPayouts

a guest
Oct 21st, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.46 KB | None | 0 0
  1. //mikefdm 2013
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.IO;
  8.  
  9. namespace ShelvedShares
  10. {
  11.     class Program
  12.     {
  13.         //входные параметры
  14.         static int TestDays = 90;                            //дней в одном тесте (сначала майнинг, потом ожидание выплат)
  15.         static int MiningDays = 30;                          //дней майнинга в одном тесте
  16.         static int TestCount = 1000;                         //кол-во тестов
  17.         static double MyGH = 1500.0;                         //своя мощность в GH/s
  18.         static double PoolGH = 24140.2;                      //мощность пула в GH/s
  19.         static double PoolShelvedSharesCost = 1000;          //стоимость отложенных шар - "долг"
  20.         static double Diff = 267731249.482421;               //сложность
  21.  
  22.         static int TestSec;
  23.         static int MiningSec;
  24.         static double DiffGH;
  25.  
  26.         const double Eps = 1e-8;
  27.  
  28.         const int MaxStackDepth = 1000;
  29.         const double GHPerShare = 4.294967296;
  30.  
  31.         static double[] StackPoolSharesCount = new double[MaxStackDepth];
  32.         static double[] StackMySharesCount = new double[MaxStackDepth];
  33.         static double[] StackShareCost = new double[MaxStackDepth];
  34.  
  35.         static bool ParseArgs(string[] args)
  36.         {
  37.             if (args.Count() != 7) return false;
  38.  
  39.             bool res =
  40.                 int.TryParse(args[0], out TestDays) |
  41.                 int.TryParse(args[1], out MiningDays) |
  42.                 int.TryParse(args[2], out TestCount) |
  43.                 double.TryParse(args[3], out MyGH) |
  44.                 double.TryParse(args[4], out PoolGH) |
  45.                 double.TryParse(args[5], out PoolShelvedSharesCost) |
  46.                 double.TryParse(args[6], out Diff);
  47.             return res;
  48.         }
  49.  
  50.         static int Main(string[] args)
  51.         {
  52.             if (args.Count() > 0)
  53.             {
  54.                 if (!ParseArgs(args))
  55.                 {
  56.                     Console.WriteLine("Usage: ShelvedShares.exe TestDays MiningDays TestCount MyGH PoolGH PoolShelvedSharesCost Diff");
  57.                     return 1;
  58.                 }
  59.             }
  60.             else
  61.             {
  62.                 Console.WriteLine("Usage: ShelvedShares.exe TestDays MiningDays TestCount MyGH PoolGH PoolShelvedSharesCost Diff");
  63.                 Console.WriteLine("Using defaults");
  64.             }
  65.  
  66.             TestSec = TestDays * 24 * 60 * 60;
  67.             MiningSec = MiningDays * 24 * 60 * 60;
  68.             DiffGH = Diff * GHPerShare / 600.0;
  69.  
  70.             int seed = 0;//3737661;
  71.             double PercAll = 0.0;
  72.             double ShelvedSharesCostAll = 0.0;
  73.             double MyShelvedSharesCostAll = 0.0;
  74.             double LuckAll = 0.0;
  75.  
  76.             double PercAvg = 0.0;
  77.             double ShelvedSharesCostAvg;
  78.             double MyShelvedSharesCostAvg;
  79.             double LuckAvg;
  80.  
  81.             for (int CurTest = 1; CurTest <= TestCount; ++CurTest)
  82.             {
  83.                 double ShelvedSharesCost;
  84.                 double MyShelvedSharesCost;
  85.                 double Luck;
  86.                 double CurPerc = RunTest(seed, out ShelvedSharesCost, out MyShelvedSharesCost, out Luck);
  87.  
  88.                 PercAll += CurPerc;
  89.                 ShelvedSharesCostAll += ShelvedSharesCost;
  90.                 MyShelvedSharesCostAll += MyShelvedSharesCost;
  91.                 LuckAll += Luck;
  92.  
  93.                 PercAvg = PercAll / CurTest;
  94.                 ShelvedSharesCostAvg = ShelvedSharesCostAll / CurTest;
  95.                 MyShelvedSharesCostAvg = MyShelvedSharesCostAll / CurTest;
  96.                 LuckAvg = LuckAll / CurTest;
  97.  
  98.                 Console.WriteLine("Test #{0}  Tot: {1:f3}%  Shelved My/Pool: {2:f3}/{3:f3}  Luck: {4:f3}%", CurTest,
  99.                     PercAvg * 100.0, MyShelvedSharesCostAvg, ShelvedSharesCostAvg, LuckAvg * 100.0);
  100.  
  101.                 if (seed != 0) ++seed;
  102.             }
  103.  
  104.             bool ok = false;
  105.             for (int i = 0; i < 100; ++i)
  106.             {
  107.                 try
  108.                 {
  109.                     StreamWriter sw = new StreamWriter("results.txt", true);
  110.                     sw.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}",
  111.                         TestDays, MiningDays, TestCount, MyGH, PoolGH, PoolShelvedSharesCost, Diff, PercAvg);
  112.                     sw.Close();
  113.  
  114.                     ok = true;
  115.                     break;
  116.                 }
  117.                 catch (Exception) { }
  118.                 System.Threading.Thread.Sleep(100);
  119.             }
  120.             if (!ok)
  121.             {
  122.                 Console.WriteLine("Saving failed");
  123.                 return 1;
  124.             }
  125.             return 0;
  126.         }
  127.  
  128.         static double RunTest(int seed, out double ShelvedSharesCost, out double MyShelvedSharesCost, out double Luck)
  129.         {
  130.             Random Rand;
  131.             if (seed == 0)
  132.                 Rand = new Random();
  133.             else
  134.                 Rand = new Random(seed);
  135.  
  136.             double ProbFindBlockPerSec = PoolGH / DiffGH / 10.0 / 60.0;
  137.  
  138.             double CurShareCost = 25.0 / 600.0 / (DiffGH / GHPerShare);
  139.  
  140.             int StackDepth = 1;
  141.             StackPoolSharesCount[0] = 1;
  142.             StackMySharesCount[0] = 0;
  143.             StackShareCost[0] = PoolShelvedSharesCost;
  144.  
  145.             double MySharesCount = 0.0;
  146.             double PoolSharesCount = 0.0;
  147.  
  148.             double Sec = 0;
  149.             double BlockTimeAvg = 0;
  150.             double BlockCount = 0;
  151.  
  152.             double PoolAvailBTC = 0.0;
  153.  
  154.             double POutPool = 0.0;
  155.             double POutPPS = 0.0;
  156.  
  157.             double MySharesPerSec = MyGH / GHPerShare;
  158.             double PoolSharesPerSec = PoolGH / GHPerShare;
  159.             double POutPPSPerSec = MyGH / GHPerShare * CurShareCost;
  160.  
  161.             for (int i = 0; i < TestSec; ++i)
  162.             {
  163.                 ++Sec;
  164.  
  165.                 if (i < MiningSec)
  166.                 {
  167.                     POutPPS += POutPPSPerSec;
  168.                     MySharesCount += MySharesPerSec;
  169.                 }
  170.                 PoolSharesCount += PoolSharesPerSec;
  171.  
  172.                 bool Found = Rand.NextDouble() < ProbFindBlockPerSec;
  173.  
  174.                 if (Found || i == TestSec - 1)
  175.                 {
  176.                     if (StackDepth == MaxStackDepth)
  177.                     {
  178.                         throw new Exception("MaxStackDepth");
  179.                     }
  180.  
  181.                     StackPoolSharesCount[StackDepth] = PoolSharesCount;
  182.                     StackMySharesCount[StackDepth] = MySharesCount;
  183.                     StackShareCost[StackDepth] = CurShareCost;
  184.                     ++StackDepth;
  185.  
  186.                     PoolSharesCount = 0;
  187.                     MySharesCount = 0;
  188.  
  189.                     if (Found)
  190.                     {
  191.                         ++BlockCount;
  192.                         PoolAvailBTC += 25.0;
  193.                     }
  194.  
  195.                     while (PoolAvailBTC > Eps && StackDepth > 0)
  196.                     {
  197.                         double AllSharesCost = StackPoolSharesCount[StackDepth - 1] * StackShareCost[StackDepth - 1];
  198.                         double PercToPay = PoolAvailBTC / AllSharesCost;
  199.                         if (PercToPay > 1.0)
  200.                             PercToPay = 1.0;
  201.                         PoolAvailBTC -= AllSharesCost * PercToPay;
  202.                         POutPool += StackMySharesCount[StackDepth - 1] * PercToPay * StackShareCost[StackDepth - 1];
  203.                         StackMySharesCount[StackDepth - 1] *= (1.0 - PercToPay);
  204.                         StackPoolSharesCount[StackDepth - 1] *= (1.0 - PercToPay);
  205.                         if (PercToPay >= 1.0 - Eps)
  206.                             --StackDepth;
  207.                     }
  208.  
  209.                 }
  210.  
  211.             }
  212.  
  213.             ShelvedSharesCost = -PoolAvailBTC;
  214.             MyShelvedSharesCost = 0;
  215.             for (int i = 0; i < StackDepth; ++i)
  216.             {
  217.                 ShelvedSharesCost += StackPoolSharesCount[i] * StackShareCost[i];
  218.                 MyShelvedSharesCost += StackMySharesCount[i] * StackShareCost[i];
  219.             }
  220.  
  221.             if (BlockCount == 0)
  222.                 Luck = 0.0;
  223.             else
  224.             {
  225.                 BlockTimeAvg = Sec / BlockCount;
  226.                 Luck = (1.0 / ProbFindBlockPerSec) / BlockTimeAvg;
  227.             }
  228.  
  229.             return POutPool / POutPPS;
  230.         }
  231.     }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement