public async void Run() { await Task.Delay(1000); HttpMessage quest = new HttpMessage(); quest.map = new Dictionary <string, string>(); while (true) { 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); HttpMessage result = await ComponentNetworkHttp.Query(url, quest); string rel = result.map["rel"]; if (rel != "ok") { 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.Insert(diff_max); } diff_max = 0; hashmining = temphash; height = tempheight; random = ""; } } await Task.Delay(1000); } }
public bool ApplyHeight(BlockChain blockChain) { if (blockChain == null) { return(false); } // 应用高度不是下一高度 long.TryParse(levelDBStore.Get("UndoHeight"), out transferHeight); if (blockChain.height - 1 != transferHeight) { return(false); } Block mcblk = blockChain.GetMcBlock(); if (mcblk == null) { return(false); } Block preblk = BlockChainHelper.GetMcBlock(transferHeight); if (mcblk.prehash != preblk.hash) { return(false); } LuaVMEnv luaVMEnv = Entity.Root.GetComponent <LuaVMEnv>(); ruleInfos = luaVMEnv.GetRules(consAddress, transferHeight); // -------------------------------------------------- calculatePower.Insert(mcblk); //Log.Debug($"ApplyHeight={blockChain.height}, {calculatePower.GetPower()}, {mcblk.GetDiff()}"); using (DbSnapshot dbSnapshot = levelDBStore.GetSnapshotUndo(blockChain.height)) { blockChain.Apply(dbSnapshot); ApplyReward(dbSnapshot, mcblk); if (GetRuleCount(mcblk.height - 1, 2) >= 2 && BlockChainHelper.IsIrreversible(this, mcblk)) { dbSnapshot.Add("2F1Height", mcblk.height.ToString()); } // 连接块交易 for (int ii = 0; ii < mcblk.linksblk.Count; ii++) { Block linkblk = blockMgr.GetBlock(mcblk.linksblk[ii]); if (linkblk == null) { return(false); } if (linkblk.height != 1) { for (int jj = 0; jj < linkblk.linkstran.Count; jj++) { if (!ApplyTransfer(dbSnapshot, linkblk.linkstran[jj], linkblk.height)) { return(false); } if (!ApplyContract(dbSnapshot, linkblk.linkstran[jj], linkblk.height)) { return(false); } } } } dbSnapshot.Commit(); } ruleInfos = luaVMEnv.GetRules(consAddress, mcblk.height, true); return(true); }
public async void Run() { //Entity.Root.GetComponent<LevelDBStore>().Undo(100); await Task.Delay(3000); long.TryParse(levelDBStore.Get("UndoHeight"), out height); Log.Debug($"Rule.Run at height {height}"); Wallet wallet = Wallet.GetWallet(); P2P_NewBlock p2p_NewBlock = new P2P_NewBlock() { networkID = NodeManager.networkIDBase }; Block blk = null; Block preblk = null; TimePass timePass = new TimePass(1); bool bBlkMining = true; if (httpRule == null) { broadcasttime = pooltime * 5 / 15; // 不挖矿就提前广播块 } state = 0xff; while (true) { try { if (state < 0xff) { await Task.Delay(1000); } long time = (nodeManager.GetNodeTime() / 1000) % pooltime; if (blk == null && time < broadcasttime && timePass.IsPassSet()) { preblk = GetLastMcBlock(); if (consensus.IsRule(preblk.height + 1, wallet.GetCurWallet().ToAddress())) { blk = CreateBlock(preblk, ref bBlkMining); if (blk != null && bBlkMining) { diff_max = 0; hashmining = blk.ToHashMining(); httpRule?.SetMinging(blk.height, hashmining, consensus.calculatePower.GetPower()); } } else { long.TryParse(levelDBStore.Get("UndoHeight"), out height); await Task.Delay(1000); } } if (blk != null && httpRule == null) { // 挖矿 if (httpRule == null && bBlkMining) { Mining(blk, hashmining); } } // 广播块 if (blk != null && time >= broadcasttime) { if (bBlkMining) { if (httpRule != null) { Dictionary <string, MinerTask> miners = httpRule.GetMiner(blk.height); if (miners != null) { double diff = miners.Values.Max(t => t.diff); var miner = miners.Values.FirstOrDefault(c => c.diff == diff); if (miner != null && miner.random != null && miner.random.Count > 0) { blk.random = miner.random[miner.random.Count - 1]; } httpRule.SetMinging(blk.height, "", consensus.calculatePower.GetPower()); } } height = blk.height; blk.hash = blk.ToHash(); blk.sign = blk.ToSign(Wallet.GetWallet().GetCurWallet()); blockMgr.AddBlock(blk); } p2p_NewBlock.block = JsonHelper.ToJson(blk); p2p_NewBlock.ipEndPoint = networkInner.ipEndPoint.ToString(); nodeManager.Broadcast(p2p_NewBlock, blk); relayNetwork?.Broadcast(p2p_NewBlock); Log.Debug($"Rule.Broadcast H:{blk.height} Mining:{bBlkMining} hash:{blk.hash} T:{blk.linkstran.Count}"); calculatePower.Insert(blk); hashmining = ""; diff_max = 0; blk = null; } await Task.Delay(10); } catch (Exception) { await Task.Delay(1000); } } }
public async void Run() { Program.DisbleQuickEditMode(); Console.Clear(); Console.CursorVisible = false; Console.Title = $" address:{address} number:{number} poolUrl:{poolUrl}"; await Task.Delay(1000); //创建后台工作线程 for (int ii = 0; ii < 16; 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 (timePass.IsPassSet()) { string hash = CryptoHelper.Sha256(hashmining + random); Log.Debug($"\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); HttpMessage result = await ComponentNetworkHttp.Query(poolUrl, quest); if (result.map != null) { if (result.map.ContainsKey("taskID")) { 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.Insert(diff_max); } diff_max = 0; hashmining = temphash; height = tempheight; random = ""; } } } } catch (Exception e) { Log.Error(e); } await Task.Delay(1000); } }
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); 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.Insert(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 async void Run() { //Entity.Root.GetComponent<LevelDBStore>().Undo(100); await Task.Delay(3000); long.TryParse(levelDBStore.Get("UndoHeight"), out height); Log.Debug($"Rule.Run at height {height}"); Wallet wallet = Wallet.GetWallet(); P2P_NewBlock p2p_NewBlock = new P2P_NewBlock(); Block blk = null; Block preblk = null; TimePass timePass = new TimePass(1); var httpRule = Entity.Root.GetComponent <HttpPool>(); state = 0xff; while (true) { try { if (state < 0xff) { await Task.Delay(1000); } long time = (nodeManager.GetNodeTime() / 1000) % pooltime; if (blk == null && time < broadcasttime && timePass.IsPassSet()) { // 找到最新的Mc块 preblk = GetLastMcBlock(); // 是否在裁决节点列表中 if (consensus.IsRule(preblk.height + 1, wallet.GetCurWallet().ToAddress())) { // 生成主块 blk = CreateBlock(preblk); diff_max = 0; hashmining = blk.ToHashMining(); httpRule?.SetMinging(blk.height, hashmining, consensus.calculatePower.GetPower()); } else { long.TryParse(levelDBStore.Get("UndoHeight"), out height); await Task.Delay(1000); } } if (blk != null && httpRule == null) { // 挖矿 这里是提交算力 by sxh Mining(blk, hashmining); } // 广播块 if (blk != null && time >= broadcasttime) { if (httpRule != null) { Dictionary <string, MinerTask> miners = httpRule.GetMiner(blk.height); if (miners != null) { double diff = miners.Values.Max(t => t.diff); var miner = miners.Values.First(c => c.diff == diff); blk.random = miner.random; httpRule?.SetMinging(blk.height, "", consensus.calculatePower.GetPower()); } } height = blk.height; blk.hash = blk.ToHash(); blk.sign = blk.ToSign(Wallet.GetWallet().GetCurWallet()); blockMgr.AddBlock(blk); p2p_NewBlock.block = JsonHelper.ToJson(blk); p2p_NewBlock.ipEndPoint = networkInner.ipEndPoint.ToString(); nodeManager.Broadcast(p2p_NewBlock); diff_max = 0; Log.Debug($"Rule.Broadcast {blk.height} {blk.hash}"); calculatePower.Insert(blk); blk = null; hashmining = ""; } await Task.Delay(10); } catch (Exception) { await Task.Delay(1000); } } }