private void ProcessException(PeerConnection peer, Messages.MineralMessage message, System.Exception exception) { string reason_message = ""; ReasonCode code = ReasonCode.Unknown; if (exception is P2pException) { P2pException.ErrorType type = ((P2pException)exception).Type; switch (type) { case P2pException.ErrorType.BAD_TRX: code = ReasonCode.BadTx; break; case P2pException.ErrorType.BAD_BLOCK: code = ReasonCode.BadBlock; break; case P2pException.ErrorType.NO_SUCH_MESSAGE: case P2pException.ErrorType.MESSAGE_WITH_WRONG_LENGTH: case P2pException.ErrorType.BAD_MESSAGE: code = ReasonCode.BadProtocol; break; case P2pException.ErrorType.SYNC_FAILED: code = ReasonCode.SyncFail; break; case P2pException.ErrorType.UNLINK_BLOCK: code = ReasonCode.Unlinkable; break; default: code = ReasonCode.Unknown; break; } reason_message = string.Format("Message from {0} process failed, {1} \n type: {2}, detail: {3}.", peer.Address, message, type, exception.Message); Logger.Error(reason_message); } else { code = ReasonCode.Unknown; reason_message = string.Format("Message from {0} process failed, {1}", peer.Address, message); Logger.Error(reason_message); } peer.Disconnect(code, reason_message); }
private static void HandleTransaction(object state) { object[] parameter = state as object[]; PeerConnection peer = (PeerConnection)parameter[0]; TransactionMessage message = (TransactionMessage)parameter[1]; if (peer.IsDisconnect) { Logger.Warning( string.Format("Drop tx {0} from {1}, peer is disconnect.", message.MessageId, peer.Address)); return; } if (Manager.Instance.AdvanceService.GetMessage(new Item(message.MessageId, InventoryType.Trx)) != null) { return; } try { Manager.Instance.NetDelegate.PushTransaction(message.Transaction); Manager.Instance.AdvanceService.Broadcast(message); } catch (P2pException e) { string reason = string.Format("Tx {0} from peer {1} process failed. type: {2}, reason: {3}", message.MessageId.ToString(), peer.Address.ToString(), e.Type.ToString(), e.Message.ToString()); Logger.Warning(reason); if (e.Type == P2pException.ErrorType.BAD_TRX) { peer.Disconnect(ReasonCode.BadTx, reason); } } catch { Logger.Error( string.Format("Tx {0} from peer {1} process failed.", message.MessageId.ToString(), peer.Address.ToString())); } }
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)); } }