public async void OnTransferAsync(BlockSub transfer) { Consensus consensus = Entity.Root.GetComponent <Consensus>(); Q2P_Transfer q2p_Transfer = new Q2P_Transfer(); q2p_Transfer.transfer = JsonHelper.ToJson(transfer); var networkInner = Entity.Root.GetComponent <ComponentNetworkInner>(); var nodeList = Entity.Root.GetComponent <NodeManager>().GetNodeRandomList(); // 遍历node提交交易,直到找个一个可以出块的节点 for (int i = 0; i < nodeList.Count; i++) { var node = nodeList[i]; Session session = await networkInner.Get(NetworkHelper.ToIPEndPoint(node.ipEndPoint)); if (session != null && session.IsConnect()) { var r2p_Transfer = (R2P_Transfer)await session.Query(q2p_Transfer, 5); if (r2p_Transfer != null && r2p_Transfer.rel != "-1") { break; } } } }
public async Task <string> QueryPrehashmkl(long syncHeight, string ipEndPoint = null) { Q2P_Prehashmkl q2p_PreHash = new Q2P_Prehashmkl(); q2p_PreHash.ActorId = nodeManager.GetMyNodeId(); q2p_PreHash.height = syncHeight; Session session = null; if (ipEndPoint != null && ipEndPoint != "") { session = await networkInner.Get(NetworkHelper.ToIPEndPoint(ipEndPoint)); } if (session != null && !session.IsConnect()) { session = null; } if (session == null) { NodeManager.NodeData node = nodeManager.GetRandomNode(); if (node != null) { session = await networkInner.Get(NetworkHelper.ToIPEndPoint(node.ipEndPoint)); } } if (session != null) { R2P_Prehashmkl r2p_Prehashmkl = (R2P_Prehashmkl)await session.Query(q2p_PreHash); return(r2p_Prehashmkl != null ? r2p_Prehashmkl.prehashmkl : ""); } return(""); }
// Broadcast to my Kademlia public async void Broadcast2Kad(IMessage message) { NodeData nodeSelf = nodes.Find((n) => { return(n.nodeId == StringHelper.HashCode(networkInner.ipEndPoint.ToString())); }); if (nodeSelf == null) { return; } List <NodeData> result = GetkList(nodeSelf.kIndex); for (int i = 0; i < result.Count; i++) { NodeData node = result[i]; if (node.nodeId != nodeSelf.nodeId) // ignore self { Session session = await networkInner.Get(NetworkHelper.ToIPEndPoint(node.ipEndPoint)); if (session != null && session.IsConnect()) { session.Send(message); } } } }
public async ETTask <Session> OnTransferAsync2(Transfer transfer, Session session2) { Consensus consensus = Entity.Root.GetComponent <Consensus>(); Q2P_Transfer q2p_Transfer = new Q2P_Transfer(); q2p_Transfer.transfer = JsonHelper.ToJson(transfer); var networkInner = Entity.Root.GetComponent <ComponentNetworkInner>(); var nodeList = Entity.Root.GetComponent <NodeManager>().GetNodeList(); // 遍历node提交交易,直到找个一个可以出块的节点 int start = RandomHelper.Random(); for (int i = start; i < start + nodeList.Count; i++) { var node = nodeList[i % nodeList.Count]; Session session = session2 ?? await networkInner.Get(NetworkHelper.ToIPEndPoint(node.ipEndPoint)); if (session != null && session.IsConnect()) { var r2p_Transfer = (R2P_Transfer)await session.Query(q2p_Transfer, 5); if (r2p_Transfer != null && r2p_Transfer.rel != "-1") { return(session); } } } await Task.Delay(10); return(null); }
public async void Broadcast(IMessage message) { // 向所在桶广播 Broadcast2Kad(message); // 获取广播列表 List <NodeData> result = GetBroadcastNode(); // 剔除自己所在桶 NodeData nodeSelf = nodes.Find((n) => { return(n.nodeId == StringHelper.HashCode(networkInner.ipEndPoint.ToString())); }); if (nodeSelf != null) { NodeData nodeIgnore = result.Find((n) => { return(n.kIndex == nodeSelf.kIndex); }); if (nodeIgnore != null) { result.Remove(nodeIgnore); } } // 开始广播 for (int i = 0; i < result.Count; i++) { NodeData node = result[i]; Session session = await networkInner.Get(NetworkHelper.ToIPEndPoint(node.ipEndPoint)); if (session != null && session.IsConnect()) { session.Send(message); } } }
public async override void Start() { networkInner.ipEndPoint = NetworkHelper.ToIPEndPoint(GetIpV4() + ":" + networkInner.ipEndPoint.Port); Log.Info($"Node:{networkInner.ipEndPoint.ToString()}"); string tmp = Program.jdNode["NodeSessions"].ToString(); List <string> list = JsonHelper.FromJson <List <string> >(tmp); Q2P_New_Node new_Node = new Q2P_New_Node(); new_Node.ActorId = GetMyNodeId(); new_Node.address = Wallet.GetWallet().GetCurWallet().ToAddress(); new_Node.ipEndPoint = networkInner.ipEndPoint.ToString(); Log.Debug($"NodeManager.Start"); while (true && list.Count > 0) { try { for (int ii = 0; ii < list.Count; ii++) { bool bResponse = false; new_Node.HashCode = StringHelper.HashCode(JsonHelper.ToJson(nodes)); new_Node.sendTime = TimeHelper.Now(); Session session = await networkInner.Get(NetworkHelper.ToIPEndPoint(list[0])); if (session != null && session.IsConnect()) { //Log.Debug($"NodeSessions connect " + r2P_New_Node.ActorId); //session.Send(new_Node); R2P_New_Node r2P_New_Node = (R2P_New_Node)await session.Query(new_Node, 0.3f); if (r2P_New_Node != null) { if (r2P_New_Node.Nodes != "") { nodes = JsonHelper.FromJson <List <NodeData> >(r2P_New_Node.Nodes); long timeNow = TimeHelper.Now(); nodeTimeOffset = (timeNow - new_Node.sendTime) / 2 + r2P_New_Node.nodeTime - timeNow; } bResponse = true; } } if (bResponse) { break; } } // 等待5秒后关闭连接 await Task.Delay(5 * 1000); } catch (Exception) { await Task.Delay(5 * 1000); } } }
public async void Broadcast2Kad(int kIndex, IMessage message) { List <NodeData> result = GetkList(kIndex); for (int i = 0; i < result.Count; i++) { NodeData node = result[i]; Session session = await networkInner.Get(NetworkHelper.ToIPEndPoint(node.ipEndPoint)); if (session != null && session.IsConnect()) { session.Send(message); } } }
public async void Broadcast(P2P_NewBlock p2p_Block, Block block) { var myNodeId = GetMyNodeId(); for (int i = 0; i < nodes.Count; i++) { NodeData node = nodes[i]; if (node.nodeId != myNodeId) { Session session = await networkInner.Get(NetworkHelper.ToIPEndPoint(node.ipEndPoint)); if (session != null && session.IsConnect()) { session.Send(p2p_Block); } } } }
public async Task <Block> QueryBlock(string hash, string ipEndPoint = null) { Q2P_Block q2p_Block = new Q2P_Block(); q2p_Block.ActorId = nodeManager.GetMyNodeId(); q2p_Block.hash = hash; Session session = null; if (ipEndPoint != null && ipEndPoint != "") { session = await networkInner.Get(NetworkHelper.ToIPEndPoint(ipEndPoint)); } if (session != null && !session.IsConnect()) { session = null; } Block blk = null; //if (session == null) { NodeManager.NodeData node = nodeManager.GetRandomNode(); if (node != null) { session = await networkInner.Get(NetworkHelper.ToIPEndPoint(node.ipEndPoint)); } } if (session != null) { R2P_Block r2p_Block = (R2P_Block)await session.Query(q2p_Block); if (r2p_Block != null && r2p_Block.block != "") { blk = JsonHelper.FromJson <Block>(r2p_Block.block); } } return(blk); }
public async Task <string> QueryMcBlockHash(long height, string ipEndPoint = null) { Q2P_McBlockHash q2p_mcBlockHash = new Q2P_McBlockHash(); q2p_mcBlockHash.ActorId = nodeManager.GetMyNodeId(); q2p_mcBlockHash.height = height; Session session = null; if (ipEndPoint != null && ipEndPoint != "") { session = await networkInner.Get(NetworkHelper.ToIPEndPoint(ipEndPoint)); } if (session != null && !session.IsConnect()) { session = null; } string hash = ""; if (session == null) { NodeManager.NodeData node = nodeManager.GetRandomNode(); if (node != null) { session = await networkInner.Get(NetworkHelper.ToIPEndPoint(node.ipEndPoint)); } } if (session != null) { R2P_McBlockHash r2p_McBlock = (R2P_McBlockHash)await session.Query(q2p_mcBlockHash); if (r2p_McBlock != null) { hash = r2p_McBlock.hash; } } return(hash); }
public async Task <List <string> > QueryBeLinkHash(string hash, string ipEndPoint = null) { Q2P_BeLinkHash q2p_BeLinkHash = new Q2P_BeLinkHash(); q2p_BeLinkHash.ActorId = nodeManager.GetMyNodeId(); q2p_BeLinkHash.hash = hash; Session session = null; if (ipEndPoint != null && ipEndPoint != "") { session = await networkInner.Get(NetworkHelper.ToIPEndPoint(ipEndPoint)); } if (session != null && !session.IsConnect()) { session = null; } if (session == null) { NodeManager.NodeData node = nodeManager.GetRandomNode(); if (node != null) { session = await networkInner.Get(NetworkHelper.ToIPEndPoint(node.ipEndPoint)); } } if (session != null) { R2P_BeLinkHash r2p_BeLinkHash = (R2P_BeLinkHash)await session.Query(q2p_BeLinkHash); if (r2p_BeLinkHash != null && r2p_BeLinkHash.hashs != null && r2p_BeLinkHash.hashs != "") { return(JsonHelper.FromJson <List <string> >(r2p_BeLinkHash.hashs)); } } return(null); }
public async void Run(bool bRun) { await Task.Delay(1 * 1000); List <string> list = JsonHelper.FromJson <List <string> >(Program.jdNode["NodeSessions"].ToString()); // Get Internet IP try { Session session = await networkInner.Get(NetworkHelper.ToIPEndPoint(list[0])); Q2P_IP_INFO qIPNode = new Q2P_IP_INFO(); R2P_IP_INFO rIPNode = (R2P_IP_INFO)await session.Query(qIPNode, 0.3f); networkInner.ipEndPoint = NetworkHelper.ToIPEndPoint(GetIpV4() + ":" + networkInner.ipEndPoint.Port); if (rIPNode != null) { networkInner.ipEndPoint = NetworkHelper.ToIPEndPoint(rIPNode.address + ":" + networkInner.ipEndPoint.Port); } } catch (Exception) { } Log.Info($"NodeManager {networkInner.ipEndPoint.ToString()}"); Log.Info($"NodeSessions {list[0]}"); // Q2P_New_Node new_Node = new Q2P_New_Node(); new_Node.ActorId = GetMyNodeId(); new_Node.address = Wallet.GetWallet().GetCurWallet().ToAddress(); new_Node.ipEndPoint = networkInner.ipEndPoint.ToString(); long state = 0; var consensus = Entity.Root.GetComponent <Consensus>(); if (consensus != null) { state |= consensus.transferShow ? EnumState.transferShow : 0; state |= consensus.openSyncFast ? EnumState.openSyncFast : 0; } state |= Entity.Root.GetComponentInChild <RelayNetwork>() != null ? EnumState.RelayNetwork : 0; new_Node.state = state; new_Node.version = BlockMgr.networkID; while (bRun && list.Count > 0) { try { for (int ii = 0; ii < list.Count; ii++) { bool bResponse = false; new_Node.HashCode = StringHelper.HashCode(JsonHelper.ToJson(nodes)); Session session = await networkInner.Get(NetworkHelper.ToIPEndPoint(list[ii])); if (session != null && session.IsConnect()) { //Log.Debug($"NodeSessions connect " + r2P_New_Node.ActorId); //session.Send(new_Node); long sendTime = TimeHelper.Now(); R2P_New_Node r2P_New_Node = (R2P_New_Node)await session.Query(new_Node, 3f); if (r2P_New_Node != null) { long timeNow = TimeHelper.Now(); nodeTimeOffset = (timeNow - sendTime) / 2 + r2P_New_Node.nodeTime - timeNow; if (!string.IsNullOrEmpty(r2P_New_Node.Nodes)) { nodes = JsonHelper.FromJson <List <NodeData> >(r2P_New_Node.Nodes); } if (!string.IsNullOrEmpty(r2P_New_Node.Message)) { Log.Warning($"NodeSessions: {r2P_New_Node.Message}"); } bResponse = true; } } if (bResponse) { break; } } // 等待5秒后关闭连接 await Task.Delay(5 * 1000); } catch (Exception) { await Task.Delay(5 * 1000); } } }