Beispiel #1
0
        public Dictionary <string, BlockSub> MinerReward(bool saveDB = true)
        {
            long counted   = 0;
            long minHeight = -1;
            long maxHeight = -1;

            using (DbSnapshot snapshot = PoolDBStore.GetSnapshot(0, true))
            {
                string str_Counted = snapshot.Get("Pool_Counted");
                long.TryParse(str_Counted, out counted);
                string        str_MR          = snapshot.Get($"Pool_MR_{str_Counted}");
                MinerRewardDB minerRewardLast = null;
                if (!string.IsNullOrEmpty(str_MR))
                {
                    minerRewardLast = JsonHelper.FromJson <MinerRewardDB>(str_MR);
                }
                if (minerRewardLast == null)
                {
                    if (httpPool.height <= 2) // 还没有获取到最新高度
                    {
                        return(null);
                    }
                    snapshot.Add("Pool_Counted", "0");
                    var minerRewardNew = new MinerRewardDB();
                    minerRewardNew.counted   = 0;
                    minerRewardNew.minHeight = httpPool.height;
                    minerRewardNew.maxHeight = httpPool.height;
                    minerRewardNew.time      = TimeHelper.time.ToString();
                    snapshot.Add($"Pool_MR_{0}", JsonHelper.ToJson(minerRewardNew));
                    snapshot.Commit();
                    return(null);
                }
                minHeight = minerRewardLast.maxHeight;

                string json = snapshot.Get("Pool_H_Miner");
                if (!string.IsNullOrEmpty(json))
                {
                    long.TryParse(json, out maxHeight);
                }
            }

            // 超过一天的数据忽略
            if (maxHeight - minHeight > 5760)
            {
                Log.Warning($"MinerReward maxHeight - minHeight > 5760, {maxHeight} - {minHeight} = {maxHeight - minHeight}");
            }

            // 设置步长
            if (maxHeight - minHeight >= RewardInterval)
            {
                maxHeight = minHeight + RewardInterval;
                runSleep  = saveDB ? 10 : runSleep; // 加快处理速度
            }

            // 缓存===========================================================================
            Dictionary <string, BlockSub> minerTransferCache = null;

            if (MinerReward_TimePass.IsPassSet())
            {
                minerTransferCache = MinerReward(false);
                if (minerTransferCache != null)
                {
                    using (DbSnapshot snapshot = PoolDBStore.GetSnapshot(0, true))
                    {
                        snapshot.Add($"Pool_Cache_MinerReward", JsonHelper.ToJson(minerTransferCache));
                        snapshot.Commit();
                    }
                }
            }
            // ===============================================================================

            if (maxHeight - minHeight < RewardInterval && saveDB)
            {
                runSleep = 7500;
                return(null);
            }

            Dictionary <string, BlockSub> minerTransfer = null;

            if (style == "PPLNS")
            {
                minerTransfer = minerTransferCache ?? GetMinerReward_PPLNS(minHeight, maxHeight);
            }
            if (style == "SOLO")
            {
                minerTransfer = minerTransferCache ?? GetMinerReward_SOLO(minHeight, maxHeight);
            }

            if (saveDB)
            {
                foreach (var it in minerTransfer)
                {
                    transferProcess.AddTransferHandle(it.Value.addressIn, it.Value.addressOut, it.Value.amount, it.Value.data);
                }

                using (DbSnapshot snapshot = PoolDBStore.GetSnapshot(0, true))
                {
                    counted += 1;
                    snapshot.Add("Pool_Counted", counted.ToString());
                    var minerRewardNew = new MinerRewardDB();
                    minerRewardNew.counted   = counted;
                    minerRewardNew.minHeight = minHeight;
                    minerRewardNew.maxHeight = maxHeight;
                    minerRewardNew.time      = DateTime.Now.Ticks.ToString();
                    snapshot.Add($"Pool_MR_{counted}", JsonHelper.ToJson(minerRewardNew));

                    // Pool_MT
                    var depend = new DateTime(DateTime.Now.Ticks).ToString("yyyy-MM-dd HH:mm:ss");
                    foreach (var it in minerTransfer)
                    {
                        it.Value.depend = depend;
                        snapshot.Queue.Push($"Pool_MT_{it.Value.addressOut}", JsonHelper.ToJson(it.Value));
                    }
                    snapshot.Commit();
                }
            }

            return(minerTransfer);
        }
Beispiel #2
0
        public Dictionary <string, BlockSub> MinerReward_PPLNS(bool saveDB = true)
        {
            var today = DateTime.UtcNow.ToString("yyyy-MM-dd");

            long counted   = 0;
            long minHeight = -1;
            long maxHeight = -1;

            using (DbSnapshot snapshot = PoolDBStore.GetSnapshot())
            {
                string str_Counted = snapshot.Get("Pool_Counted");
                long.TryParse(str_Counted, out counted);
                string        str_MR          = snapshot.Get($"Pool_MR_{str_Counted}");
                MinerRewardDB minerRewardLast = null;
                if (!string.IsNullOrEmpty(str_MR))
                {
                    minerRewardLast = JsonHelper.FromJson <MinerRewardDB>(str_MR);
                }
                if (minerRewardLast == null)
                {
                    snapshot.Add("Pool_Counted", "0");
                    var minerRewardNew = new MinerRewardDB();
                    minerRewardNew.counted   = 0;
                    minerRewardNew.minHeight = httpPool.height;
                    minerRewardNew.maxHeight = httpPool.height;
                    minerRewardNew.time      = TimeHelper.time.ToString();
                    snapshot.Add($"Pool_MR_{0}", JsonHelper.ToJson(minerRewardNew));
                    snapshot.Commit();
                    return(null);
                }
                minHeight = minerRewardLast.maxHeight;

                string json = snapshot.Get("Pool_H_Miner");
                if (!string.IsNullOrEmpty(json))
                {
                    long.TryParse(json, out maxHeight);
                }
            }

            if (maxHeight - minHeight < RewardInterval && saveDB)
            {
                return(null);
            }

            var minerTransfer = MinerReward_PPLNS(today, minHeight, maxHeight);

            if (saveDB)
            {
                using (DbSnapshot snapshot = PoolDBStore.GetSnapshot())
                {
                    foreach (var it in minerTransfer)
                    {
                        transferProcess.AddTransferHandle(it.Value.addressIn, it.Value.addressOut, it.Value.amount, it.Value.data);
                    }

                    counted += 1;
                    snapshot.Add("Pool_Counted", counted.ToString());
                    var minerRewardNew = new MinerRewardDB();
                    minerRewardNew.counted   = counted;
                    minerRewardNew.minHeight = minHeight;
                    minerRewardNew.maxHeight = maxHeight;
                    minerRewardNew.time      = DateTime.UtcNow.Ticks.ToString();
                    snapshot.Add($"Pool_MR_{counted}", JsonHelper.ToJson(minerRewardNew));

                    // Pool_MT
                    var depend = new DateTime(DateTime.UtcNow.Ticks, DateTimeKind.Utc).ToString("yyyy-MM-dd HH:mm:ss");
                    foreach (var it in minerTransfer)
                    {
                        it.Value.depend = depend;
                        snapshot.Queue.Push($"Pool_MT_{it.Value.addressOut}", JsonHelper.ToJson(it.Value));
                    }

                    snapshot.Commit();
                }

                transferProcess.SaveTransferToDB();
            }

            return(minerTransfer);
        }