コード例 #1
0
ファイル: P2PJob.cs プロジェクト: BitNumbers/FiiiCoin
        private void receivedBlocksMessage(string address, int port, BlocksMsg msg)
        {
            foreach (var block in msg.Blocks)
            {
                this.saveBlockToDB(block);

                if (this.blocksInSynchronizing.ContainsKey(block.Header.Height))
                {
                    blocksInSynchronizing.Remove(block.Header.Height);
                }

                if (this.newBlocksInDownloading.Contains(block.Header.Height))
                {
                    var nodes = this.p2pComponent.GetNodes();

                    //Broadcast to other node
                    foreach (var node in nodes)
                    {
                        if (node.IsConnected && !node.IsTrackerServer && node.IP != address)
                        {
                            var cmd = P2PCommand.CreateCommand(CommandNames.Block.NewBlock, msg);
                            this.p2pComponent.SendCommand(node.IP, node.Port, cmd);
                        }
                    }

                    newBlocksInDownloading.Remove(block.Header.Height);
                }
            }
        }
コード例 #2
0
        private void receivedGetBlocks(string address, int port, GetBlocksMsg msg, int nonce)
        {
            List <BlockMsg> blocks;

            try
            {
                blocks = this.blockComponent.GetBlockMsgByHeights(msg.Heights);
            }
            catch (Exception ex)
            {
                LogHelper.Error("GetBlocksMsg Message is Empty");
                throw;
            }

            if (blocks.Count > 0)
            {
                int maxLength   = 1000 * 1024; //max 100KB;
                int totalLength = 0;
                var payload     = new BlocksMsg();

                foreach (var block in blocks)
                {
                    totalLength += block.Serialize().Length;

                    if (payload.Blocks.Count == 0 || totalLength <= maxLength)
                    {
                        payload.Blocks.Add(block);
                    }
                    else
                    {
                        break;
                    }
                }

                try
                {
                    var cmd = P2PCommand.CreateCommand(this.Identity.ToString(), CommandNames.Block.Blocks, nonce, payload);
                    this.p2pComponent.SendCommand(address, port, cmd);
                }
                catch (Exception ex)
                {
                    LogHelper.Error("GetBlocksMsg Result is Empty");
                }
            }
            else
            {
                var cmd = P2PCommand.CreateCommand(this.Identity.ToString(), CommandNames.Other.NotFound, nonce, null);
                this.p2pComponent.SendCommand(address, port, cmd);
            }
        }
コード例 #3
0
ファイル: P2PJob.cs プロジェクト: BitNumbers/FiiiCoin
        private void receivedGetBlocks(string address, int port, GetBlocksMsg msg, int nonce)
        {
            var blocks = this.blockComponent.GetBlockMsgByHeights(msg.Heights);

            if (blocks.Count > 0)
            {
                var payload = new BlocksMsg();
                payload.Blocks.AddRange(blocks);

                var cmd = P2PCommand.CreateCommand(CommandNames.Block.Blocks, nonce, payload);
                this.p2pComponent.SendCommand(address, port, cmd);
            }
            else
            {
                var cmd = P2PCommand.CreateCommand(CommandNames.Other.NotFound, nonce, null);
                this.p2pComponent.SendCommand(address, port, cmd);
            }
        }
コード例 #4
0
        private void receivedBlocksMessage(string address, int port, BlocksMsg msg)
        {
            this.syncManager.CloseGetBlocksTask(address, port);

            foreach (var block in msg.Blocks)
            {
                try
                {
                    if (!this.tempBlockList.Any(t => t.Header.Hash == block.Header.Hash) &&
                        block.Header.Hash == block.Header.GetHash() && block.Header.Height > GlobalParameters.LocalHeight)
                    {
                        this.tempBlockList.Add(block);
                    }
                }
                catch
                {
                    LogHelper.Error(string.Format("Error IP at {0}:{1}", address, port));
                }

                if (this.newBlocksInDownloading.Contains(block.Header.Height))
                {
                    var nodes  = this.p2pComponent.GetNodes();
                    var newMsg = new NewBlockMsg();
                    newMsg.Header = block.Header;

                    //Broadcast to other node
                    foreach (var node in nodes)
                    {
                        if (node.IsConnected && !node.IsTrackerServer && node.IP != address)
                        {
                            var cmd = P2PCommand.CreateCommand(this.Identity.ToString(), CommandNames.Block.NewBlock, newMsg);
                            this.p2pComponent.SendCommand(node.IP, node.Port, cmd);
                        }
                    }

                    newBlocksInDownloading.Remove(block.Header.Height);
                }
            }
        }
コード例 #5
0
        //private void processLongTimeCommand()
        //{
        //    LogHelper.Warn("Thread : In to thread threadProcessLongTimeCommand's process action : processLongTimeCommand");
        //    P2PState state = default(P2PState);

        //    //System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
        //    while (this.isRunning)
        //    {
        //        if (longTimeCommandQueue.TryDequeue(out state))
        //        {
        //            if(state != null)
        //            {
        //                try
        //                {
        //                    //stopwatch.Reset();
        //                    //stopwatch.Start();
        //                    int index = 0;
        //                    switch (state.Command.CommandName)
        //                    {
        //                        case CommandNames.Block.GetHeaders:
        //                            var getHeadersMsg = new GetHeadersMsg();
        //                            getHeadersMsg.Deserialize(state.Command.Payload, ref index);
        //                            this.receivedGetHeaders(state.IP, state.Port, getHeadersMsg, state.Command.Nonce);
        //                            break;
        //                        case CommandNames.Block.GetBlocks:
        //                            var getBlocksMsg = new GetBlocksMsg();
        //                            getBlocksMsg.Deserialize(state.Command.Payload, ref index);
        //                            this.receivedGetBlocks(state.IP, state.Port, getBlocksMsg, state.Command.Nonce);
        //                            break;
        //                        default:
        //                            break;
        //                    }
        //                    //stopwatch.Stop();
        //                    //LogHelper.Warn($"processLongTimeCommand : {state.Command.CommandName}  -- {stopwatch.ElapsedMilliseconds}  , longTimeCommandQueue count = {longTimeCommandQueue.Count()}" );

        //                }
        //                catch (Exception ex)
        //                {
        //                    LogHelper.Error(ex.ToString());
        //                    //LogHelper.Warn($"processLongTimeCommand : {state.Command.CommandName}  -- {stopwatch.ElapsedMilliseconds}  , longTimeCommandQueue count = {longTimeCommandQueue.Count()} -- Exception msg: {ex.ToString()} ");
        //                }

        //            }
        //            Thread.Sleep(10);
        //        }
        //        else
        //        {
        //            Thread.Sleep(50);
        //        }

        //    }
        //    LogHelper.Warn("Thread : out of thread threadProcessLongTimeCommand's process action : processLongTimeCommand");
        //}

        #endregion

        private void dataReceived(P2PState state)
        {
            int index = 0;

            switch (state.Command.CommandName)
            {
            case CommandNames.Transaction.GetTxPool:
                this.receivedGetTransactionPool(state.IP, state.Port, state.Command.Nonce);
                break;

            case CommandNames.Transaction.TxPool:
                var txPoolMsg = new TxPoolMsg();
                txPoolMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedTransacitonPoolMessage(state.IP, state.Port, txPoolMsg);
                break;

            case CommandNames.Transaction.GetTx:
                var getTxMsg = new GetTxsMsg();
                getTxMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedGetTransaction(state.IP, state.Port, getTxMsg, state.Command.Nonce);
                break;

            case CommandNames.Transaction.Tx:
                var txsMsg = new TxsMsg();
                txsMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedTransactionMessage(state.IP, state.Port, txsMsg);
                break;

            case CommandNames.Transaction.NewTx:
                var newTxMsg = new NewTxMsg();
                newTxMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedNewTransactionMessage(state.IP, state.Port, newTxMsg);
                break;

            case CommandNames.Block.GetHeight:
                this.receivedGetHeight(state.IP, state.Port, state.Command.Nonce);
                break;

            case CommandNames.Block.Height:
                var heightMsg = new HeightMsg();
                heightMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedHeightMessage(state.IP, state.Port, heightMsg);
                break;

            case CommandNames.Block.GetHeaders:
                var getHeadersMsg = new GetHeadersMsg();
                getHeadersMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedGetHeaders(state.IP, state.Port, getHeadersMsg, state.Command.Nonce);
                //longTimeCommandQueue.Enqueue(state);
                break;

            case CommandNames.Block.Headers:
                var headersMsg = new HeadersMsg();
                headersMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedHeadersMessage(state.IP, state.Port, headersMsg);
                break;

            case CommandNames.Block.GetBlocks:
                var getBlocksMsg = new GetBlocksMsg();
                getBlocksMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedGetBlocks(state.IP, state.Port, getBlocksMsg, state.Command.Nonce);
                //longTimeCommandQueue.Enqueue(state);
                break;

            case CommandNames.Block.Blocks:
                var blocksMsg = new BlocksMsg();
                blocksMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedBlocksMessage(state.IP, state.Port, blocksMsg);
                break;

            case CommandNames.Block.NewBlock:
                var newBlockMsg = new NewBlockMsg();
                newBlockMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedNewBlockMessage(state.IP, state.Port, newBlockMsg, state.Command.Nonce);
                break;

            case CommandNames.MiningPool.GetMiningPools:
                this.receivedGetMiningPoolsMessage(state.IP, state.Port);
                break;

            case CommandNames.MiningPool.MiningPools:
                var miningPoolMsg = new MiningPoolMsg();
                miningPoolMsg.Deserialize(state.Command.Payload, ref index);
                receivedMiningPoolsMessage(miningPoolMsg);
                break;

            case CommandNames.MiningPool.NewMiningPool:
                var newMiningPoolMsg = new NewMiningPoolMsg();
                newMiningPoolMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedNewMiningPoolMessage(state, newMiningPoolMsg);
                break;

            case CommandNames.Other.Reject:
            case CommandNames.Other.NotFound:
            default:
                break;
            }
        }
コード例 #6
0
ファイル: P2PJob.cs プロジェクト: BitNumbers/FiiiCoin
        private void dataReceived(P2PState state)
        {
            int index = 0;

            switch (state.Command.CommandName)
            {
            //case CommandNames.P2P.GetAddr:
            //    this.getAddrMsgHandle(state);
            //    break;
            //case CommandNames.P2P.Addr:
            //    this.addrMsgHandle(state);
            //    break;
            //case CommandNames.P2P.Heartbeat:
            //    this.heartbeatMsgHandle(state);
            //    break;
            case CommandNames.Transaction.GetTxPool:
                this.receivedGetTransactionPool(state.IP, state.Port, state.Command.Nonce);
                break;

            case CommandNames.Transaction.TxPool:
                var txPoolMsg = new TxPoolMsg();
                txPoolMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedTransacitonPoolMessage(state.IP, state.Port, txPoolMsg);
                break;

            case CommandNames.Transaction.GetTx:
                var getTxMsg = new GetTxsMsg();
                getTxMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedGetTransaction(state.IP, state.Port, getTxMsg, state.Command.Nonce);
                break;

            case CommandNames.Transaction.Tx:
                var txsMsg = new TxsMsg();
                txsMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedTransactionMessage(state.IP, state.Port, txsMsg);
                break;

            case CommandNames.Transaction.NewTx:
                var newTxMsg = new NewTxMsg();
                newTxMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedNewTransactionMessage(state.IP, state.Port, newTxMsg);
                break;

            case CommandNames.Block.GetHeight:
                this.receivedGetHeight(state.IP, state.Port, state.Command.Nonce);
                break;

            case CommandNames.Block.Height:
                var heightMsg = new HeightMsg();
                heightMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedHeightMessage(state.IP, state.Port, heightMsg);
                break;

            case CommandNames.Block.GetHeaders:
                var getHeadersMsg = new GetHeadersMsg();
                getHeadersMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedGetHeaders(state.IP, state.Port, getHeadersMsg, state.Command.Nonce);
                break;

            case CommandNames.Block.Headers:
                var headersMsg = new HeadersMsg();
                headersMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedHeadersMessage(state.IP, state.Port, headersMsg);
                break;

            case CommandNames.Block.GetBlocks:
                var getBlocksMsg = new GetBlocksMsg();
                getBlocksMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedGetBlocks(state.IP, state.Port, getBlocksMsg, state.Command.Nonce);
                break;

            case CommandNames.Block.Blocks:
                var blocksMsg = new BlocksMsg();
                blocksMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedBlocksMessage(state.IP, state.Port, blocksMsg);
                break;

            case CommandNames.Block.NewBlock:
                var newBlockMsg = new NewBlockMsg();
                newBlockMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedNewBlockMessage(state.IP, state.Port, newBlockMsg, state.Command.Nonce);
                break;

            case CommandNames.Other.Reject:

                break;

            case CommandNames.Other.NotFound:
                break;

            default:
                break;
            }
        }