Beispiel #1
0
        private void SendPiece(PeerConnection peer, BlockRequest request)
        {
            long dataOffset = application.Metainfo.PieceSize * request.PieceIndex + request.Offset;

            byte[] data = application.DataHandler.ReadBlockData(dataOffset, request.Length);

            peer.SendMessage(new PieceMessage(request.ToBlock(data)));
        }
        public void ProcessMessage(PeerConnection peer, Messages.MineralMessage message)
        {
            SyncBlockChainMessage sync_message = (SyncBlockChainMessage)message;

            Check(peer, sync_message);

            long           remain_num = 0;
            List <BlockId> ids        = GetLostBlockIds(sync_message.Ids);

            if (ids.Count == 1)
            {
                peer.IsNeedSyncUs = false;
            }
            else
            {
                peer.IsNeedSyncUs = true;
                remain_num        = Manager.Instance.NetDelegate.HeadBlockId.Num - ids.Last().Num;
            }

            peer.LastSyncBlockId = ids.Last();
            peer.RemainNum       = remain_num;
            peer.SendMessage(new ChainInventoryMessage(ids, remain_num));
        }
Beispiel #3
0
        public void SyncNext(PeerConnection peer)
        {
            try
            {
                if (!peer.SyncChainRequest.Equals(default(KeyValuePair <Deque <BlockId>, long>)))
                {
                    Logger.Warning(
                        string.Format("Peer {0} is in sync.",
                                      peer.Node.Host));
                    return;
                }

                List <BlockId> chain_summary = GetBlockChainSummary(peer);
                peer.SyncChainRequest = new KeyValuePair <Deque <BlockId>, long>(new Deque <BlockId>(chain_summary), Helper.CurrentTimeMillis());
                peer.SendMessage(new SyncBlockChainMessage(chain_summary));
            }
            catch (System.Exception e)
            {
                Logger.Error(
                    string.Format("Peer {0} sync failed", peer.Address, e));

                peer.Disconnect(Protocol.ReasonCode.SyncFail, e.Message);
            }
        }
        public void ProcessMessage(PeerConnection peer, Messages.MineralMessage message)
        {
            FetchInventoryDataMessage fetch_message = (FetchInventoryDataMessage)message;

            Check(peer, fetch_message);

            InventoryType      type         = fetch_message.InventoryType;
            List <Transaction> transactions = new List <Transaction>();

            int size = 0;

            foreach (SHA256Hash hash in fetch_message.GetHashList())
            {
                Item item = new Item(hash, type);

                Message msg = Manager.Instance.AdvanceService.GetMessage(item);
                if (msg == null)
                {
                    try
                    {
                        msg = Manager.Instance.NetDelegate.GetData(hash, type);
                    }
                    catch (System.Exception e)
                    {
                        Logger.Error(
                            string.Format("Fetch item {0} failed. reason: {1}",
                                          item,
                                          hash,
                                          e.Message));
                        peer.Disconnect(ReasonCode.FetchFail, e.Message);

                        return;
                    }
                }

                if (type == InventoryType.Block)
                {
                    BlockId block_id = ((BlockMessage)msg).Block.Id;
                    if (peer.BlockBothHave.Num < block_id.Num)
                    {
                        peer.BlockBothHave = block_id;
                    }

                    peer.SendMessage(msg);
                }
                else
                {
                    transactions.Add(((TransactionMessage)msg).Transaction.Instance);
                    size += ((TransactionMessage)msg).Transaction.Instance.CalculateSize();

                    if (size > MAX_SIZE)
                    {
                        peer.SendMessage(new TransactionsMessage(transactions));
                        transactions = new List <Transaction>();
                        size         = 0;
                    }
                }
            }
            if (transactions.Count > 0)
            {
                peer.SendMessage(new TransactionsMessage(transactions));
            }
        }