void Tell_Request_BlockHeight(IModulePipeline remote)
        {
            var dict = new MessagePackObjectDictionary();

            dict["cmd"] = (UInt16)CmdList.Request_BlockHeight;
            remote.Tell(new MessagePackObject(dict));
        }
        void OnRecv_Request_Block(IModulePipeline from, MessagePackObjectDictionary dict)
        {
            var index       = dict["blockIndex"].AsUInt64();
            var blockheader = this.blockChain.GetBlockHeader(index);

            Tell_Response_Block(from, blockheader);
        }
        void OnRecv_Response_Block(IModulePipeline from, MessagePackObjectDictionary dict)
        {
            var header = dict["blockHeader"].AsBinary();

            if (header != null)
            {
                var index = this.GetLastIndex();
                var head  = SerializeHelper.DeserializeWithBinary <BlockHeader>(header);
                if (head.blockType == BlockType.TxData)
                {
                    var list = SerializeHelper.DeserializeWithBinary <List <Hash256> >(head.TxidsHash);
                    logger.Info($"---------------OnRecv_Response_Block-----block:[{index}]------------");
                    for (int i = 0; i < list.Count; i++)
                    {
                        logger.Info($" index={i}    txid={list[i].ToString()}");
                    }
                    logger.Info("--------------------------------------------------------------------");
                    foreach (var item in list)
                    {
                        Tell_Request_Tx(from, item);
                    }
                }
                var block = new Block();
                block.index            = BitConverter.GetBytes(index);
                block.header           = new BlockHeader(head.blockType);
                block.header.TxidsHash = head.TxidsHash;
                this.blockChain.SaveBlock(block, this.blockIndex);
            }
        }
        void OnRecv_Response_PeerList(IModulePipeline from, MessagePackObjectDictionary dict)
        {
            var nodes = dict["nodes"].AsList();

            foreach (var n in nodes)
            {
                var        subobj  = n.AsDictionary();
                CanLinkObj canlink = new CanLinkObj();
                canlink.fromType  = LinkFromEnum.ResponsePeers;
                canlink.from      = from.system.Remote;
                canlink.ID        = subobj["id"].AsBinary();
                canlink.remote    = IPEndPoint.Parse(subobj["pubep"].AsString());
                canlink.PublicKey = subobj["pubkey"].AsBinary();

                if (this.listCanlink.Contains(canlink))//检查我的连接列表
                {
                    var link = this.listCanlink.Getqueue(canlink.remote.ToString());
                    link.ID        = canlink.ID;
                    link.PublicKey = canlink.PublicKey;
                }
                else
                {
                    this.listCanlink.Enqueue(canlink);
                }
            }
        }
        void OnRecv_Response_ProvedRelay(IModulePipeline from, MessagePackObjectDictionary dict)
        {
            var pubep       = dict["pubep"].AsString();
            var provedpubep = dict["provedpubep"].AsString();
            var isProved    = dict["isProved"].AsBoolean();

            if (pubep.Contains("$"))
            {
                var url  = pubep.Substring(0, pubep.IndexOf("$"));
                var link = this.linkNodes.GetLinkNode(url);
                if (link != null)
                {
                    var subPubep = pubep.Substring(pubep.IndexOf("$"));
                    Tell_Response_ProvedRelay(link.remoteNode, subPubep, provedpubep);
                }
            }
            else if (!string.IsNullOrEmpty(provedpubep) || isProved)
            {
                var link = this.linkNodes.GetLinkNode(pubep);
                if (link != null)
                {
                    Tell_Response_Iamhere(link.remoteNode, provedpubep);
                }
            }
        }
        /// <summary>
        /// 交易业务说明
        /// SendRaw是将交易数据从 任意节点(任意节点包括共识节点) 流转到 一个共识节点的过程
        ///
        /// 1.流转说明
        /// 如果SendRaw发起者就是共识节点,则不需要再流转了,直接处理交易
        /// 如果不是,只流转给自己连接的节点中plevel 小于等于自己的,这个没见到
        /// 2.sendraw参数
        /// SendRaw(byte[] message,SignData)
        /// 发送交易只需要两个数据,一个 bytearray,一个signdata,一定是byte[],不要整什么MessagePackObjectList
        /// signdata参考neo的最简形态,我们只支持最简的 txpool.TransactionSign 写在那里的
        /// 3.流转验证
        /// 每一次流转,都要检查sendraw 数据对不对
        /// signdata里面的vscript 包含公钥,iscript包含签名数据,执行ecc验签,不通过不转发,还记录本地黑名单,第二次收到,都不需要验证
        ///
        /// 4.txid
        /// 交易的id ,就是交易message 的 hash256,保持和neo兼容
        ///
        /// 5.共识节点收到交易的处理
        /// 交易不是块,和块没有关系
        /// 先忽略共识过程,系统就一个共识节点,自己就是议长。议长干的第一件事是构造一个统一的交易内存池
        /// 收到交易,只需要存在内存里
        /// 我们先假设所有共识节点有同样的交易index,交易的index是由议长分配的,议长分配完id,告知所有的共识节点
        ///
        /// 当前共识节点收到交易,给他分配一个index,就存在自己的内存池里,不需要存数据库。
        /// 分配了index的交易就可以全网广播,>=plevel
        /// 然后开一个定时器,定时从自己的内存池里挑一些交易,组装成块,广播
        ///
        /// block 的header 包括当前块所包含的交易的hash
        /// block 的body 就是 所有的包含的交易的 message 和 signdata
        /// 只需要广播block的header,block的body 各个节点自己就可以组装
        ///
        /// 6.错过广播
        /// 错过广播是很正常的,所以每个节点有一个高度设计,就是block的index
        /// 可以找任意高度大于自己的节点索要指定的block header(by block index or block id) 和 指定的hash(by txid)
        ///
        /// 7.数据存储
        /// 对共识节点,仅当组装成块的时候,一次性写入 block header、block涉及的交易、当前高度,用db 的 writebatch 方式
        /// 刚收到的交易不写数据库,仅有随块一起写入的,并且已写入的交易,内存池里就不必保持了。
        /// 但是所有的txid->block index 的映射,内存池里要保持
        /// </summary>
        /// <param name="from"></param>
        /// <param name="dict"></param>
        //static DateTime start;
        //static int recvcount = 0;
        void OnRecv_Post_SendRaw(IModulePipeline from, MessagePackObjectDictionary dict)
        {
            //if(recvcount == 0 )
            //{
            //    start = DateTime.Now;
            //}
            //recvcount++;
            logger.Info($"------OnRecv_Post_SendRaw  From:{from?.system?.Remote?.ToString()??"Local"} -------");
            //验证交易合法性,合法就收
            var  signData = SerializeHelper.DeserializeWithBinary <TransactionSign>(dict["signData"].AsBinary());
            bool sign     = Helper_NEO.VerifySignature(dict["message"].AsBinary(), signData.IScript, signData.VScript);

            if (!sign)
            {
                logger.Info($"------OnRecv_Post_SendRaw  sign error -------");
                return;
            }
            //if (recvcount % 20 == 0)
            //{
            //    var end = DateTime.Now;
            //    logger.Info("recv count:" + recvcount + " span=" + (end - start).TotalMilliseconds + "  txpool.discard"+ TXPool.txpoolcount);
            //}

            //收到消息后要么转发,要么保存
            if (this.isProved)
            {
                Transaction trans = new Transaction();
                trans.Index    = this.txpool.MaxTransactionID;
                trans.message  = dict["message"].AsBinary();
                trans.signdata = signData;
                lock (blockTimerLock)
                {
                    this.txpool.AddTx(trans);
                }
                //向内存池保存完,向全网广播这个交易
                foreach (var item in this.linkNodes)
                {
                    if (item.Value.hadJoin)
                    {
                        Tell_BoardCast_Tx(item.Value.remoteNode, dict["message"].AsBinary(), dict["signData"].AsBinary());
                    }
                }
            }
            else
            {
                //只流转给非记账节点,按照优先级,小于等于自己的其中一个
                LinkObj minLink = null;
                foreach (var item in this.linkNodes)
                {
                    if ((minLink == null || item.Value.pLevel < minLink.pLevel) && (item.Value.hadJoin))
                    {
                        minLink = item.Value;
                    }
                }
                if (minLink != null)
                {
                    Tell_SendRaw(minLink.remoteNode, dict["message"].AsBinary(), dict["signData"].AsBinary());
                }
            }
        }
        void Tell_BoardCast_LosePlevel(IModulePipeline remote)
        {
            var dict = new MessagePackObjectDictionary();

            dict["cmd"] = (UInt16)CmdList.BoardCast_LosePlevel;
            remote.Tell(new MessagePackObject(dict));
        }
        void Tell_Response_BlockHeight(IModulePipeline remote)
        {
            var dict = new MessagePackObjectDictionary();

            dict["cmd"]        = (UInt16)CmdList.Response_BlockHeight;
            dict["blockIndex"] = this.blockIndex;
            remote.Tell(new MessagePackObject(dict));
        }
        void Tell_Request_Block(IModulePipeline remote, ulong blockIndex)
        {
            var dict = new MessagePackObjectDictionary();

            dict["cmd"]        = (UInt16)CmdList.Request_Block;
            dict["blockIndex"] = blockIndex;
            remote.Tell(new MessagePackObject(dict));
        }
        void Tell_Response_Block(IModulePipeline remote, byte[] header)
        {
            var dict = new MessagePackObjectDictionary();

            dict["cmd"]         = (UInt16)CmdList.Response_Block;
            dict["blockHeader"] = header;
            remote.Tell(new MessagePackObject(dict));
        }
        void Tell_Response_Tx(IModulePipeline remote, byte[] tx)
        {
            var dict = new MessagePackObjectDictionary();

            dict["cmd"] = (UInt16)CmdList.Response_Tx;
            dict["tx"]  = tx;
            remote.Tell(new MessagePackObject(dict));
        }
        void Tell_BoradCast_PeerState(IModulePipeline remote)
        {
            var dict = new MessagePackObjectDictionary();

            dict["cmd"]    = (UInt16)CmdList.BoradCast_PeerState;
            dict["plevel"] = this.pLevel;//告诉对方我的优先级
            remote.Tell(new MessagePackObject(dict));
        }
        void Tell_Response_plevel(IModulePipeline remote)
        {
            var dict = new MessagePackObjectDictionary();

            dict["cmd"]    = (UInt16)CmdList.Response_Plevel;
            dict["plevel"] = this.pLevel;
            remote.Tell(new MessagePackObject(dict));
        }
Ejemplo n.º 14
0
 public override void OnTell(IModulePipeline from, byte[] data)
 {
     if (from != null && from.system.Remote != null)//从远程投递而来
     {
         Console.WriteLine("Hello get from:" + from.system.Remote + " // " + from.path);
         from.Tell(global::System.Text.Encoding.UTF8.GetBytes("hihihihi"));
     }
     Console.WriteLine("Hello get info=" + global::System.Text.Encoding.UTF8.GetString(data));
 }
        void Tell_Response_Iamhere(IModulePipeline remote, string provedpubep)
        {
            var dict = new MessagePackObjectDictionary();

            dict["cmd"]         = (UInt16)CmdList.Response_Iamhere;
            dict["provedpubep"] = provedpubep;
            dict["isProved"]    = this.isProved;
            remote.Tell(new MessagePackObject(dict));
        }
        void OnRecv_Request_ConnectTo(IModulePipeline from, MessagePackObjectDictionary dict)
        {
            var endpointstr = dict["endpoint"].AsString();

            if (IPEndPoint.TryParse(endpointstr, out IPEndPoint endpoint))
            {
                this.ConnectOne(endpoint);
            }
        }
        void Tell_BoardCast_Tx(IModulePipeline remote, byte[] message, byte[] signData)
        {
            var dict = new MessagePackObjectDictionary();

            dict["cmd"]      = (UInt16)CmdList.BoardCast_Tx;
            dict["message"]  = message;
            dict["signData"] = signData;
            remote.Tell(new MessagePackObject(dict));
        }
Ejemplo n.º 18
0
 public override void OnTell(IModulePipeline from, byte[] data)
 {
     if (from != null && from.system.Remote != null)//从远程投递而来
     {
         UInt64 peerid = from.system.PeerID;
         //Console.WriteLine("peerid:"+from.system.PeerID);
         NetMessage netMessage = server.Process(peerid, data);
         from.Tell(netMessage.ToBytes());
     }
 }
        void Tell_Request_ProvePeer(IModulePipeline remote, byte[] addinfo, byte[] signdata)
        {
            var dict = new MessagePackObjectDictionary();

            dict["cmd"]      = (UInt16)CmdList.Request_ProvePeer;
            dict["pubkey"]   = this.pubkey;
            dict["addinfo"]  = addinfo;
            dict["signdata"] = signdata;
            remote.Tell(new MessagePackObject(dict));
        }
        //private IModulePipeline observer;
        //void OnRecv_IamObserver(IModulePipeline from, MessagePackObjectDictionary dict)
        //{
        //    observer = from;
        //}

        void OnRecv_Response_plevel(IModulePipeline from, MessagePackObjectDictionary dict)
        {
            var id     = from.system.PeerID;
            var plevel = dict["plevel"].AsInt32();

            if (this.linkNodes.TryGetValue(id, out LinkObj obj))
            {
                logger.Info(" plevel:" + plevel + "from:" + obj.publicEndPoint);
            }
        }
        void Tell_SendRaw(IModulePipeline remote, byte[] message, byte[] signData)
        {
            logger.Info($"------Tell_SendRaw  To:{remote.ToString()}-------");
            var dict = new MessagePackObjectDictionary();

            dict["cmd"]      = (UInt16)CmdList.Post_SendRaw;
            dict["message"]  = message;
            dict["signData"] = signData;
            remote.Tell(new MessagePackObject(dict));
        }
        void OnRecv_SendMsg(IModulePipeline from, MessagePackObjectDictionary dict)
        {
            var msg         = dict["msg"];
            var endpointStr = dict["target"].AsString();

            if (this.linkIDs.TryGetValue(endpointStr, out ulong peer))
            {
                this.linkNodes[peer].remoteNode.Tell(msg);
            }
        }
Ejemplo n.º 23
0
        public override void OnTell(IModulePipeline from, MessagePackObject?obj)
        {
            var dict = obj.Value.AsDictionary();
            var cmd  = dict["cmd"].AsUInt16();

            if (cmd == CMDID_RPC)
            {
                var id = dict["id"].AsInt32();
                this.recvRPC[id] = obj;
            }
        }
        void OnRecv_Response_Iamhere(IModulePipeline from, MessagePackObjectDictionary dict)
        {
            var link = this.linkNodes[from.system.PeerID];

            link.provedPubep = dict["provedpubep"].AsString();
            link.isProved    = dict["isProved"].AsBoolean();
            if (!ContainsRemote(link.publicEndPoint))
            {
                this.provedNodes[from.system.PeerID] = link;
            }
        }
        void Tell_Request_PeerList(IModulePipeline remote)
        {
            if (!this.beEnableQueryPeers)
            {
                return;
            }
            var dict = new MessagePackObjectDictionary();

            dict["cmd"] = (UInt16)CmdList.Request_PeerList;
            remote.Tell(new MessagePackObject(dict));
        }
        void Tell_ReqJoinPeer(IModulePipeline remote)
        {
            var dict = new MessagePackObjectDictionary();

            dict["cmd"]   = (UInt16)CmdList.Request_JoinPeer;
            dict["id"]    = this.guid.data;
            dict["pubep"] = this.config.PublicEndPoint.ToString();
            //Console.WriteLine("Tell_ReqJoinPeer----->:"+ dict["pubep"]);
            dict["chaininfo"] = chainHash.data;
            remote.Tell(new MessagePackObject(dict));
        }
Ejemplo n.º 27
0
 public static void Tell(this IModulePipeline pipeline, Newtonsoft.Json.Linq.JToken json)
 {
     if (pipeline.IsLocal)
     {
         pipeline.TellLocalObj(json);
     }
     else
     {
         pipeline.Tell(Pack(json).ToArray());
     }
 }
Ejemplo n.º 28
0
 public static void Tell(this IModulePipeline pipeline, MessagePackObject?obj)
 {
     if (pipeline.IsLocal)
     {
         pipeline.TellLocalObj(obj);
     }
     else
     {
         pipeline.Tell(Pack(obj).ToArray());
     }
 }
Ejemplo n.º 29
0
        public override void OnTell(IModulePipeline from, byte[] data)
        {
            BaseMsg msg = MsgHelper.DecodeMessage(data);

            if (msg != null)
            {
                if (this.actionFactory.ContainsKey(msg.msgtype))
                {
                    this.actionFactory[msg.msgtype].handle(from, msg);
                }
            }
        }
        void Tell_ResponseAcceptJoin(IModulePipeline remote)
        {
            var link = this.linkNodes[remote.system.PeerID];

            link.CheckInfo = Guid.NewGuid().ToByteArray();
            var dict = new MessagePackObjectDictionary();

            dict["cmd"]       = (UInt16)CmdList.Response_AcceptJoin;
            dict["checkinfo"] = link.CheckInfo;
            dict["plevel"]    = this.pLevel;//告诉对方我的优先级
            //选个挑战信息
            remote.Tell(new MessagePackObject(dict));
        }