static public void Test() { CalculatePower calculate = new CalculatePower(); Block blk = new Block(); blk.prehash = "b6b67d3d8b83f4885620ccd45d1af81d5690a056de2aba8ddf899fba8088b75d"; double diff_max = 0; for (int jj = 0; jj < 100; jj++) { for (int ii = 0; ii < 1000 * 1000; ii++) { string random = RandomHelper.RandUInt64().ToString("x"); string hash = blk.ToHash(random); double diff = Helper.GetDiff(hash); if (diff > diff_max) { diff_max = diff; blk.hash = hash; blk.random = random; } } double value1 = calculate.Power(blk.GetDiff()); string value2 = CalculatePower.GetPowerCompany(value1); Log.Info($"\n PowerCompany {blk.GetDiff()} \n {value2} \n {blk.hash}"); } }
static public void Test() { // { Block blk = new Block(); blk.prehash = "b6b67d3d8b83f4885620ccd45d1af81d5690a056de2aba8ddf899fba8088b75d"; { blk.hash = "000d24c333b6ba99f18e3c3ca33a1b5a388e562fdd38c1ce0a459f15139e0f5e"; double value1 = CalculatePower.Power(blk.GetDiff()); string value2 = CalculatePower.GetPowerCompany(value1); Log.Info($"\n PowerCompany {blk.GetDiff()} \n {value2} \n {blk.hash}"); } { blk.hash = "0e7adeae5ba12d1b14af8a45aaced269e6523621e225e7fd8c6628bdcdbf76db"; double value1 = CalculatePower.Power(blk.GetDiff()); string value2 = CalculatePower.GetPowerCompany(value1); Log.Info($"\n PowerCompany {blk.GetDiff()} \n {value2} \n {blk.hash}"); } } // 2 { CalculatePower calculate = new CalculatePower(); Block blk = new Block(); blk.prehash = "b6b67d3d8b83f4885620ccd45d1af81d5690a056de2aba8ddf899fba8088b75d"; double diff_max = 0; for (int jj = 0; jj < 100; jj++) { for (int ii = 0; ii < 1000 * 1000; ii++) { string random = RandomHelper.RandUInt64().ToString("x"); string hash = blk.ToHash(random); double diff = Helper.GetDiff(hash); if (diff > diff_max) { diff_max = diff; blk.hash = hash; blk.random = random; } } double value1 = CalculatePower.Power(blk.GetDiff()); string value2 = CalculatePower.GetPowerCompany(value1); Log.Info($"\n PowerCompany {blk.GetDiff()} \n {value2} \n {blk.hash}"); } } }
static public void Test() { // { Block blk = new Block(); blk.prehash = "b6b67d3d8b83f4885620ccd45d1af81d5690a056de2aba8ddf899fba8088b75d"; { blk.hash = "000000c09983d8950d8d0dce9ab5e9039fade2590d25f361ee0f9c1047832ceb"; double value1 = CalculatePower.Power(blk.GetDiff()); string value2 = CalculatePower.GetPowerCompany(value1); Log.Info($"\n PowerCompany {blk.GetDiff()} \n {value2} \n {blk.hash}"); } { blk.hash = "0000003e833bc8b524922d9400e8b489eb1fe753d35efde0d5e2ae5ee430dfbe"; double value1 = CalculatePower.Power(blk.GetDiff()); string value2 = CalculatePower.GetPowerCompany(value1); Log.Info($"\n PowerCompany {blk.GetDiff()} \n {value2} \n {blk.hash}"); } } // 2 { CalculatePower calculate = new CalculatePower(); Block blk = new Block(); blk.prehash = "b6b67d3d8b83f4885620ccd45d1af81d5690a056de2aba8ddf899fba8088b75d"; double diff_max = 0; for (int jj = 0; jj < 100; jj++) { for (int ii = 0; ii < 1000 * 1000; ii++) { string random = RandomHelper.RandUInt64().ToString("x"); string hash = blk.ToHash(random); double diff = Helper.GetDiff(hash); if (diff > diff_max) { diff_max = diff; blk.hash = hash; blk.random = random; } } double value1 = CalculatePower.Power(blk.GetDiff()); string value2 = CalculatePower.GetPowerCompany(value1); Log.Info($"\n PowerCompany {blk.GetDiff()} \n {value2} \n {blk.hash}"); } } }
public MinerView GetMinerTop(long minerIndex, long minerColumn) { minerColumn = Math.Min(minerColumn, 100); var minerView = new MinerView(); if (httpPool != null) { Dictionary <string, MinerTask> miners = httpPool.GetMinerRewardMin(out long miningHeight); if (miners != null) { var minerList = miners.Values.OrderByDescending((x) => { double.TryParse(x.power_average, out double power_average); return(power_average); }).ToList(); for (var ii = 0; ii < minerColumn; ii++) { if ((minerIndex + ii) >= minerList.Count) { break; } var miner = minerList[(int)minerIndex + ii]; if (string.IsNullOrEmpty(miner.power_cur)) { miner.power_cur = CalculatePower.GetPowerCompany(CalculatePower.Power(miner.diff)); } var minerdata = new MinerViewData(); minerdata.number = $"{miner.address}_{ miner.number}"; minerdata.lasttime = miner.time; minerdata.power_cur = miner.power_cur; double.TryParse(miner.power_average, out double power_average); minerdata.power_average = CalculatePower.GetPowerCompany(power_average); minerView.miners.Add(minerdata); } } } return(minerView); }
public void TestOnSubmit(HttpMessage httpMessage) { if (httpMessage.map["cmd"].ToLower() != "submit") { return; } var resultMap = JsonHelper.FromJson <Dictionary <string, string> >(httpMessage.result); resultMap.TryGetValue("hashmining", out string hashmining); if (Pool.registerPool || string.IsNullOrEmpty(hashmining)) { return; } string address = httpMessage.map["address"]; string number = httpMessage.map["number"]; for (int i = 0; i < 20000; i++) { MinerTask minerTask = NewNextTakID(height, address, number); if (minerTask != null) { string randomTemp; randomTemp = minerTask.taskid + System.Guid.NewGuid().ToString("N").Substring(0, 13); string hash = BlockDag.ToHash(minerTask.height, hashmining, randomTemp); double diff = Helper.GetDiff(hash); double power = CalculatePower.Power(diff); minerTask.diff = diff; minerTask.random.Add(randomTemp); minerTask.power = power; minerTask.power_average = power.ToString(); } } }
public MinerView GetMinerView(string address, long transferIndex, long transferColumn, long minerIndex, long minerColumn) { if (getMinerViewTimePass.IsPassSet()) { getMinerViewCache.Clear(); } if (getMinerViewCache.TryGetValue($"{address}_{transferIndex}_{transferColumn}_{minerIndex}_{minerColumn}", out MinerView minerViewLast)) { return(minerViewLast); } transferColumn = Math.Min(transferColumn, 100); minerColumn = Math.Min(minerColumn, 100); var minerView = new MinerView(); minerView.address = address; Dictionary <string, BlockSub> minerTransferCache = null; using (DbSnapshot snapshot = PoolDBStore.GetSnapshot()) { var str = snapshot.Get($"Pool_Cache_MinerReward"); if (!string.IsNullOrEmpty(str)) { minerTransferCache = JsonHelper.FromJson <Dictionary <string, BlockSub> >(str); } } var transfers_cur = minerTransferCache?.Values.FirstOrDefault(c => c.addressOut == address); if (transfers_cur != null) { minerView.amount_cur = transfers_cur.amount; minerView.share = transfers_cur.type; } // 交易确认 using (DbSnapshot snapshot = PoolDBStore.GetSnapshot()) { int TopIndex = snapshot.Queue.GetTopIndex($"Pool_MT_{address}"); for (int ii = 1; ii <= (int)transferColumn; ii++) { var value = snapshot.Queue.Get($"Pool_MT_{address}", TopIndex - (int)transferIndex - ii); if (!string.IsNullOrEmpty(value)) { var transfer = JsonHelper.FromJson <BlockSub>(value); if (transfer != null) { minerView.transfers.Add(transfer); } } } foreach (var transfer in minerView.transfers) { // 节点使用自己的地址挖矿 if (transfer.addressIn == transfer.addressOut) { transfer.hash = transfer.addressIn; } else { transfer.hash = TransferProcess.GetMinerTansfer(snapshot, transfer.data); } } } var miners = httpPool.GetMinerRewardMin(out long miningHeight); var minerList = miners?.Values.Where((x) => x.address == address).ToList(); double totalPower = 0L; if (minerList != null) { minerList.Sort((MinerTask a, MinerTask b) => { return(a.number.CompareTo(b.number)); }); // 当前页矿机算力 for (var ii = 0; ii < minerColumn; ii++) { if ((minerIndex + ii) >= minerList.Count) { break; } var miner = minerList[(int)minerIndex + ii]; if (string.IsNullOrEmpty(miner.power_cur)) { miner.power_cur = CalculatePower.GetPowerCompany(CalculatePower.Power(miner.diff)); } var minerdata = new MinerViewData(); minerdata.number = miner.number; minerdata.lasttime = miner.time; minerdata.power_cur = miner.power_cur; double.TryParse(miner.power_average, out double power_average); minerdata.power_average = CalculatePower.GetPowerCompany(power_average); minerView.miners.Add(minerdata); } // 当前总算力 for (var ii = 0; ii < minerList.Count; ii++) { var miner = minerList[ii]; if (double.TryParse(miner.power_average, out double power_average)) { totalPower += power_average; } } minerView.totalMiners = minerList.Count; } minerView.totalPower = CalculatePower.GetPowerCompany(totalPower); getMinerViewCache.Add($"{address}_{transferIndex}_{transferColumn}_{minerIndex}_{minerColumn}", minerView); return(minerView); }
// Miner reward, only after confirming that it cannot be rolled back public Dictionary <string, BlockSub> GetMinerReward_PPLNS(long minHeight, long maxHeight) { Dictionary <string, BlockSub> minerTransfer = new Dictionary <string, BlockSub>(); if (httpPool != null) { string addressIn = Wallet.GetWallet().GetCurWallet().ToAddress(); for (long rewardheight = minHeight; rewardheight < maxHeight; rewardheight++) { Dictionary <string, MinerTask> miners = null; using (DbSnapshot snapshot = PoolDBStore.GetSnapshot()) { string json = snapshot.Get("Pool_H2_" + rewardheight); if (!string.IsNullOrEmpty(json)) { miners = JsonHelper.FromJson <Dictionary <string, MinerTask> >(json); } } if (miners != null) { var mcblk = GetMcBlock(rewardheight); if (mcblk == null) { throw new Exception($"GetMcBlock({rewardheight}) return null"); } if (mcblk != null && mcblk.Address == ownerAddress) { double reward = Consensus.GetReward(rewardheight); reward = reward * (1.0f - GetServiceFee()); var miner = miners.Values.FirstOrDefault(c => c.random.IndexOf(mcblk.random) != -1); if (miner == null) { continue; } // Total power double powerSum = 0; var Values = miners.Values.ToList(); for (var ii = 0; ii < Values.Count; ii++) { var dic = Values[ii]; if (string.IsNullOrEmpty(dic.address)) { continue; } double power = CalculatePower.Power(dic.diff); if (power < HttpPool.ignorePower) { continue; } powerSum += power; } // Reward for participation BlockSub transfer = null; for (var ii = 0; ii < Values.Count; ii++) { var dic = Values[ii]; if (string.IsNullOrEmpty(dic.address)) { continue; } double power = CalculatePower.Power(dic.diff); double pay = Math.Round(power * reward / powerSum, 8); if (minerTransfer.TryGetValue(dic.address, out transfer)) { if (power < HttpPool.ignorePower) { transfer.height += 1; // 这里表示无效提交数 continue; } transfer.nonce += 1; // 这里表示有效提交数 transfer.amount = BigHelper.Add(transfer.amount, pay.ToString("N8")); } else if (pay >= 0.002) { transfer = new BlockSub(); transfer.nonce = 1; // 这里表示有效提交数 transfer.addressIn = addressIn; transfer.addressOut = dic.address; transfer.amount = BigHelper.Sub(pay.ToString("N8"), "0.002"); // 扣除交易手续费 transfer.data = CryptoHelper.Sha256($"{mcblk.hash}_{maxHeight}_{ownerAddress}_{dic.address}_MinerReward"); if (power < HttpPool.ignorePower) { transfer.height += 1; // 这里表示无效提交数 continue; } minerTransfer.Add(transfer.addressOut, transfer); } } } } } // 有效提交次数越多收益越高 var totalAmount1 = "0"; // 总账1 var totalAmount2 = "0"; // 总账2 foreach (var transfer in minerTransfer.Values) { try { totalAmount1 = BigHelper.Add(totalAmount1, transfer.amount); var totalSubmit = transfer.nonce + transfer.height; var share = (float)transfer.nonce / (float)totalSubmit; transfer.type = $"{Math.Round(share * 100, 2)}%"; // 有效提交百分比 share *= share; transfer.remarks = BigHelper.Mul(share.ToString(), transfer.amount); totalAmount2 = BigHelper.Add(totalAmount2, transfer.remarks); } catch (Exception) { transfer.type = "0%"; transfer.remarks = "0"; } } var totalAmount3 = "0"; // 总账3 foreach (var transfer in minerTransfer.Values) { try { transfer.amount = BigHelper.Div(BigHelper.Mul(transfer.remarks, totalAmount1), totalAmount2); totalAmount3 = BigHelper.Add(totalAmount3, transfer.amount); } catch (Exception) { } } //if (BigHelper.Greater(BigHelper.Abs(BigHelper.Sub(totalAmount1, totalAmount3)), "0.002", true)) //{ // Log.Warning($"|totalAmount1 - totalAmount3| > 0.002 {BigHelper.Sub(totalAmount1, totalAmount3)}"); //} } return(minerTransfer); }
public MinerView GetMinerView(string address, long transferIndex, long transferColumn, long minerIndex, long minerColumn) { transferColumn = Math.Min(transferColumn, 100); minerColumn = Math.Min(minerColumn, 100); var minerView = new MinerView(); minerView.address = address; var transfers_cur = MinerReward_PPLNS(false)?.Values.FirstOrDefault(c => c.addressOut == address); if (transfers_cur != null) { minerView.amount_cur = transfers_cur.amount; } using (DbSnapshot snapshot = PoolDBStore.GetSnapshot()) { int TopIndex = snapshot.Queue.GetTopIndex($"Pool_MT_{address}"); for (int ii = 1; ii <= (int)transferColumn; ii++) { var value = snapshot.Queue.Get($"Pool_MT_{address}", TopIndex - (int)transferIndex - ii); if (!string.IsNullOrEmpty(value)) { var transfer = JsonHelper.FromJson <BlockSub>(value); if (transfer != null) { minerView.transfers.Add(transfer); } } } foreach (var transfer in minerView.transfers) { // 节点使用自己的地址挖矿 if (transfer.addressIn == transfer.addressOut) { transfer.hash = transfer.addressIn; } else { transfer.hash = transferProcess.GetMinerTansfer(snapshot, transfer.data); } } } var miners = httpPool.GetMinerReward(out long miningHeight); var minerList = miners?.Values.Where((x) => x.address == address).ToList(); double totalPower = 0L; if (minerList != null) { minerList.Sort((MinerTask a, MinerTask b) => { return(a.number.CompareTo(b.number)); }); for (var ii = 0; ii < minerColumn; ii++) { if ((minerIndex + ii) >= minerList.Count) { break; } var miner = minerList[(int)minerIndex + ii]; if (string.IsNullOrEmpty(miner.power_cur)) { miner.power_cur = CalculatePower.GetPowerCompany(CalculatePower.Power(miner.diff)); } var minerdata = new MinerViewData(); minerdata.number = miner.number; minerdata.lasttime = miner.time; minerdata.power_cur = miner.power_cur; double.TryParse(miner.power_average, out double power_average); minerdata.power_average = CalculatePower.GetPowerCompany(power_average); minerView.miners.Add(minerdata); totalPower += power_average; } minerView.totalMiners = minerList.Count; } minerView.totalPower = CalculatePower.GetPowerCompany(totalPower); return(minerView); }
public async void Run() { Program.DisbleQuickEditMode(); Console.Clear(); Console.CursorVisible = false; Console.Title = $" address:{address},thread:{thread}, number:{number}, poolUrl:{poolUrl}"; Log.Info($"start mining..."); await Task.Delay(1000); //创建后台工作线程 for (int ii = 0; ii < thread; ii++) { System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Mining)); thread.IsBackground = true;//设置为后台线程 thread.Start(this); } HttpMessage quest = new HttpMessage(); quest.map = new Dictionary <string, string>(); while (true) { try { if (timePassInfo.IsPassSet()) { //string hash = CryptoHelper.Sha256(hashmining + random); string hash = BlockDag.ToHash(height, hashmining, random); Log.Info($"\n height:{height}, taskid:{taskid}, random:{random}, diff:{diff_max}, power:{calculatePower.GetPower()} hash:{hash}"); } quest.map.Clear(); quest.map.Add("cmd", "submit"); quest.map.Add("height", height.ToString()); quest.map.Add("address", address); quest.map.Add("number", number); quest.map.Add("random", random); quest.map.Add("taskid", taskid); quest.map.Add("average", calculatePower.GetPowerDouble().ToString()); HttpMessage result = null; try { result = await ComponentNetworkHttp.Query($"http://{poolUrl}/mining", quest); } catch (Exception) { if (timePassDebug.IsPassSet()) { Log.Warning($"\n Unable to open the network connection http://{poolUrl}/mining"); } } if (result != null && result.map != null) { if (result.map.ContainsKey("taskid")) { if (result.map.ContainsKey("number")) { number = result.map["number"]; Console.Title = $" address:{address},thread:{thread}, number:{number}, poolUrl:{poolUrl}"; } long.TryParse(result.map["height"], out long tempheight); taskid = result.map["taskid"]; string temphash = result.map["hashmining"]; if (temphash == null || temphash == "" || temphash != hashmining) { if (diff_max != 0) { calculatePower.InsertPower(CalculatePower.Power(diff_max)); } diff_max = 0; hashmining = temphash; height = tempheight; random = ""; result.map.TryGetValue("power", out poolPower); changeCallback?.Invoke(); } } } } catch (Exception e) { Log.Error(e); } await Task.Delay(intervalTime); } }
public MinerView GetMinerView(string address, long transferIndex, long transferColumn, long minerIndex, long minerColumn) { var minerView = new MinerView(); minerView.address = address; var transfers_cur = MinerReward_PPLNS(false)?.Values.FirstOrDefault(c => c.addressOut == address); if (transfers_cur != null) { minerView.amount_cur = transfers_cur.amount; } long counted = 0; using (DbSnapshot snapshot = PoolDBStore.GetSnapshot()) { string str_Counted = snapshot.Get("Pool_Counted"); if (long.TryParse(str_Counted, out counted)) { counted = counted - transferIndex; for (int i = 0; i < transferColumn; i++) { string str_MT = snapshot.Get($"Pool_MT_{counted-i}"); Dictionary <string, BlockSub> minerTransfer = null; if (!string.IsNullOrEmpty(str_MT)) { minerTransfer = JsonHelper.FromJson <Dictionary <string, BlockSub> >(str_MT); if (minerTransfer != null) { var transfer = minerTransfer?.Values.FirstOrDefault(c => c.addressOut == address); if (transfer != null) { string str_MR = snapshot.Get($"Pool_MR_{counted - i}"); MinerRewardDB minerRewardLast = null; if (!string.IsNullOrEmpty(str_MR)) { minerRewardLast = JsonHelper.FromJson <MinerRewardDB>(str_MR); } long.TryParse(minerRewardLast.time, out long time); transfer.depend = new DateTime(time).ToString("yyyy-MM-dd HH:mm:ss"); minerView.transfers.Add(transfer); } } } } } } // Is the query successful using (var dbSnapshot = PoolDBStore.GetSnapshot(0)) { foreach (var transfer in minerView.transfers) { transfer.hash = transferProcess.GetMinerTansfer(dbSnapshot, transfer.data); } } var miners = httpPool.GetMinerReward(out long miningHeight); var minerList = miners?.Values.Where((x) => x.address == address).ToList(); double totalPower = 0L; if (minerList != null) { minerList.Sort((MinerTask a, MinerTask b) => { return(a.number.CompareTo(b.number)); }); for (var ii = 0; ii < minerColumn; ii++) { if ((minerIndex + ii) >= minerList.Count) { break; } var miner = minerList[(int)minerIndex + ii]; if (string.IsNullOrEmpty(miner.power_cur)) { miner.power_cur = CalculatePower.GetPowerCompany(CalculatePower.Power(miner.diff)); } var minerdata = new MinerViewData(); minerdata.number = miner.number; minerdata.lasttime = miner.time; minerdata.power_cur = miner.power_cur; double.TryParse(miner.power_average, out double power_average); minerdata.power_average = CalculatePower.GetPowerCompany(power_average); minerView.miners.Add(minerdata); totalPower += power_average; } minerView.totalMiners = minerList.Count; } minerView.totalPower = CalculatePower.GetPowerCompany(totalPower); return(minerView); }
// http://127.0.0.1:8088/mining?cmd=Submit public void OnSubmit(HttpMessage httpMessage) { // submit Dictionary <string, string> map = new Dictionary <string, string>(); try { map.Add("report", "refuse"); // 版本检查 string version = httpMessage.map["version"]; if (version != Miner.version) { map.Remove("report"); map.Add("report", "error"); map.Add("tips", $"Miner.version: {Miner.version}"); httpMessage.result = JsonHelper.ToJson(map); return; } // 矿池登记检查 if (Pool.registerPool && HttpPoolRelay.poolIpAddress.IndexOf(httpMessage.request.RemoteEndPoint.Address.ToString()) == -1) { map.Add("tips", "need register Pool"); httpMessage.result = JsonHelper.ToJson(map); return; } long minerHeight = long.Parse(httpMessage.map["height"]); string address = httpMessage.map["address"]; string number = httpMessage.map["number"]; MinerTask minerTask = GetMyTaskID(minerHeight, address, number, httpMessage.map["taskid"]); if (minerHeight == height && !string.IsNullOrEmpty(hashmining) && minerTask != null && TimeHelper.time - minerTask.time > 0.1) { minerTask.time = TimeHelper.time; string random = httpMessage.map["random"]; string hash = BlockDag.ToHash(height, hashmining, random); double diff = Helper.GetDiff(hash); double power = CalculatePower.Power(diff); if (power >= HttpPool.ignorePower && diff > minerTask.diff && !string.IsNullOrEmpty(random) && (Pool.registerPool || random.IndexOf(minerTask.taskid) == 0)) { ///Log.Info($"OnSubmit: Height:{height} A:{address} P:{power} H:{hash}"); minerTask.diff = diff; minerTask.random.Add(random); minerTask.power = power; minerTask.power_average = httpMessage.map["average"]; map.Remove("report"); map.Add("report", "accept"); } } if (minerHeight != height && !string.IsNullOrEmpty(hashmining)) { bool limitNewNext = true; // 矿池人数限制 if (!Miners.TryGetValue(height - 1, out Dictionary <string, MinerTask> list_1) || list_1.Count < minerLimit || list_1.TryGetValue($"{address}_{number}", out MinerTask temp)) { limitNewNext = false; } if (Miners.TryGetValue(height, out Dictionary <string, MinerTask> list_0) && list_0.Count >= minerLimit) { limitNewNext = true; } if (!limitNewNext) { MinerTask newMinerTask = NewNextTakID(height, address, number); // new task map.Add("height", height.ToString()); map.Add("hashmining", hashmining); map.Add("taskid", newMinerTask.taskid); map.Add("power", minerTask?.power.ToString()); map.Add("nodeTime", GetNodeTime().ToString()); if (newMinerTask.number != number) { map.Add("number", newMinerTask.number); } } else { map.Add("minerLimit", minerLimit.ToString()); } } } catch (Exception) { map.Remove("report"); map.Add("report", "error"); } httpMessage.result = JsonHelper.ToJson(map); }