Esempio n. 1
0
        private Message Create(byte type, byte[] packed)
        {
            MessageTypes.MsgType received = MessageTypes.FromByte(type);
            if (received == MessageTypes.MsgType.LAST)
            {
                throw new P2pException(P2pException.ErrorType.NO_SUCH_MESSAGE, "type=" + type + ", len=" + packed.Length);
            }

            switch (received)
            {
            case MessageTypes.MsgType.TX:
                return(new TransactionMessage(packed));

            case MessageTypes.MsgType.BLOCK:
                return(new BlockMessage(packed));

            case MessageTypes.MsgType.TXS:
                return(new TransactionsMessage(packed));

            case MessageTypes.MsgType.BLOCKS:
                return(new BlocksMessage(packed));

            case MessageTypes.MsgType.INVENTORY:
                return(new InventoryMessage(packed));

            case MessageTypes.MsgType.FETCH_INV_DATA:
                return(new FetchInventoryDataMessage(packed));

            case MessageTypes.MsgType.SYNC_BLOCK_CHAIN:
                return(new SyncBlockChainMessage(packed));

            case MessageTypes.MsgType.BLOCK_CHAIN_INVENTORY:
                return(new ChainInventoryMessage(packed));

            case MessageTypes.MsgType.ITEM_NOT_FOUND:
                return(new ItemNotFoundMessage());

            case MessageTypes.MsgType.FETCH_BLOCK_HEADERS:
                return(new FetchBlockHeadersMessage(packed));

            case MessageTypes.MsgType.TX_INVENTORY:
                return(new TransactionInventoryMessage(packed));

            default:
                throw new P2pException(
                          P2pException.ErrorType.NO_SUCH_MESSAGE, received.ToString() + ", len=" + packed.Length);
            }
        }
Esempio n. 2
0
        private P2pMessage Create(byte type, byte[] raw_data)
        {
            P2pMessage result = null;

            MessageTypes.MsgType message_type = MessageTypes.FromByte(type);

            if (message_type == MessageTypes.MsgType.LAST)
            {
                throw new P2pException(P2pException.ErrorType.NO_SUCH_MESSAGE, "type = " + type + ", len = " + raw_data.Length);
            }

            switch (message_type)
            {
            case MessageTypes.MsgType.P2P_HELLO:
            {
                result = new HelloMessage(type, raw_data);
            }
            break;

            case MessageTypes.MsgType.P2P_DISCONNECT:
            {
                result = new DisconnectMessage(type, raw_data);
            }
            break;

            case MessageTypes.MsgType.P2P_PING:
            {
                result = new PingMessage(type, raw_data);
            }
            break;

            case MessageTypes.MsgType.P2P_PONG:
            {
                result = new PongMessage(type, raw_data);
            }
            break;

            default:
            {
                throw new P2pException(P2pException.ErrorType.NO_SUCH_MESSAGE, message_type.ToString());
            }
            }

            return(result);
        }
        private void Check(PeerConnection peer, FetchInventoryDataMessage message)
        {
            MessageTypes.MsgType type = message.InventoryMessageType;

            if (type == MessageTypes.MsgType.TX)
            {
                foreach (SHA256Hash hash in message.GetHashList())
                {
                    if (peer.GetInventorySpread(new Item(hash, InventoryType.Trx)) == null)
                    {
                        throw new P2pException(P2pException.ErrorType.BAD_MESSAGE, "not spread inventory : " + hash);
                    }
                }

                int fetch_count = peer.NodeStatistics.MessageStatistics.MineralInTrxFetchInvDataElement.GetCount(10);
                int max_count   = Manager.Instance.AdvanceService.TxCount.GetCount(60);
                if (fetch_count > max_count)
                {
                    throw new P2pException(
                              P2pException.ErrorType.BAD_MESSAGE, "maxCount: " + max_count + ", fetchCount: " + fetch_count);
                }
            }
            else
            {
                bool is_advance = true;
                foreach (SHA256Hash hash in message.GetHashList())
                {
                    if (peer.GetInventorySpread(new Item(hash, InventoryType.Block)) == null)
                    {
                        is_advance = false;
                        break;
                    }
                }

                if (is_advance)
                {
                    MessageCount out_advance_block = peer.NodeStatistics.MessageStatistics.MineralOutAdvBlock;
                    out_advance_block.Add(message.GetHashList().Count);

                    int out_block_count_1min = out_advance_block.GetCount(60);
                    int produced_block_2min  = 120000 / Parameter.ChainParameters.BLOCK_PRODUCED_INTERVAL;
                    if (out_block_count_1min > produced_block_2min)
                    {
                        throw new P2pException(
                                  P2pException.ErrorType.BAD_MESSAGE,
                                  "producedBlockIn2min: " + produced_block_2min + ", outBlockCountIn1min: " + out_block_count_1min);
                    }
                }
                else
                {
                    if (!peer.IsNeedSyncUs)
                    {
                        throw new P2pException(
                                  P2pException.ErrorType.BAD_MESSAGE, "no need sync");
                    }

                    foreach (SHA256Hash hash in message.GetHashList())
                    {
                        long block_num     = new BlockId(hash).Num;
                        long min_block_num = peer.LastSyncBlockId.Num - 2 * Parameter.NodeParameters.SYNC_FETCH_BATCH_NUM;

                        if (block_num < min_block_num)
                        {
                            throw new P2pException(
                                      P2pException.ErrorType.BAD_MESSAGE, "minBlockNum: " + min_block_num + ", blockNum: " + block_num);
                        }

                        if (peer.GetSyncBlockId(hash) != null)
                        {
                            throw new P2pException(
                                      P2pException.ErrorType.BAD_MESSAGE, new BlockId(hash).GetString() + " is exist");
                        }

                        peer.AddSyncBlockId(hash, Helper.CurrentTimeMillis());
                    }
                }
            }
        }