Peer extends PeerSocketHandler { public void addDisconnectedEventListener(PeerDisconnectedEventListener listener) { addDisconnectedEventListener(Threading.USER_THREAD, listener); } Peer(NetworkParameters params, VersionMessage ver, @Nullable AbstractBlockChain chain, PeerAddress remoteAddress);  Peer(NetworkParameters params, VersionMessage ver, PeerAddress remoteAddress,
                @Nullable AbstractBlockChain chain);  Peer(NetworkParameters params, VersionMessage ver, PeerAddress remoteAddress,
                @Nullable AbstractBlockChain chain, int downloadTxDependencyDepth);  Peer(NetworkParameters params, AbstractBlockChain blockChain, PeerAddress peerAddress, String thisSoftwareName, String thisSoftwareVersion); @Deprecated @SuppressWarnings("deprecation") void addEventListener(AbstractPeerEventListener listener); @Deprecated void addEventListener(AbstractPeerEventListener listener, Executor executor); @Deprecated void removeEventListener(AbstractPeerEventListener listener); void addBlocksDownloadedEventListener(BlocksDownloadedEventListener listener); void addBlocksDownloadedEventListener(Executor executor, BlocksDownloadedEventListener listener); void addChainDownloadStartedEventListener(ChainDownloadStartedEventListener listener); void addChainDownloadStartedEventListener(Executor executor, ChainDownloadStartedEventListener listener); void addConnectedEventListener(PeerConnectedEventListener listener); void addConnectedEventListener(Executor executor, PeerConnectedEventListener listener); void addDisconnectedEventListener(PeerDisconnectedEventListener listener); void addDisconnectedEventListener(Executor executor, PeerDisconnectedEventListener listener); void addGetDataEventListener(GetDataEventListener listener); void addGetDataEventListener(Executor executor, GetDataEventListener listener); void addOnTransactionBroadcastListener(OnTransactionBroadcastListener listener); void addOnTransactionBroadcastListener(Executor executor, OnTransactionBroadcastListener listener); void addPreMessageReceivedEventListener(PreMessageReceivedEventListener listener); void addPreMessageReceivedEventListener(Executor executor, PreMessageReceivedEventListener listener); boolean removeBlocksDownloadedEventListener(BlocksDownloadedEventListener listener); boolean removeChainDownloadStartedEventListener(ChainDownloadStartedEventListener listener); boolean removeConnectedEventListener(PeerConnectedEventListener listener); boolean removeDisconnectedEventListener(PeerDisconnectedEventListener listener); boolean removeGetDataEventListener(GetDataEventListener listener); boolean removeOnTransactionBroadcastListener(OnTransactionBroadcastListener listener); boolean removePreMessageReceivedEventListener(PreMessageReceivedEventListener listener); @Override String toString(); @Override void connectionClosed(); @Override void connectionOpened(); ListenableFuture<Peer> getConnectionOpenFuture(); ListenableFuture<Peer> getVersionHandshakeFuture(); ListenableFuture<List<Transaction>> downloadDependencies(Transaction tx); @SuppressWarnings("unchecked") // The 'unchecked conversion' warning being suppressed here comes from the sendSingleGetData() formally returning // ListenableFuture instead of ListenableFuture<Block>. This is okay as sendSingleGetData() actually returns // ListenableFuture<Block> in this context. Note that sendSingleGetData() is also used for Transactions. ListenableFuture<Block> getBlock(Sha256Hash blockHash); @SuppressWarnings("unchecked") // The 'unchecked conversion' warning being suppressed here comes from the sendSingleGetData() formally returning // ListenableFuture instead of ListenableFuture<Transaction>. This is okay as sendSingleGetData() actually returns // ListenableFuture<Transaction> in this context. Note that sendSingleGetData() is also used for Blocks. ListenableFuture<Transaction> getPeerMempoolTransaction(Sha256Hash hash); ListenableFuture<AddressMessage> getAddr(); void setDownloadParameters(long secondsSinceEpoch, boolean useFilteredBlocks); void addWallet(Wallet wallet); void removeWallet(Wallet wallet); void startBlockChainDownload(); ListenableFuture<Long> ping(); long getLastPingTime(); long getPingTime(); int getPeerBlockHeightDifference(); boolean isDownloadData(); void setDownloadData(boolean downloadData); VersionMessage getPeerVersionMessage(); VersionMessage getVersionMessage(); long getBestHeight(); boolean setMinProtocolVersion(int minProtocolVersion); void setBloomFilter(BloomFilter filter); void setBloomFilter(BloomFilter filter, boolean andQueryMemPool); BloomFilter getBloomFilter(); ListenableFuture<UTXOsMessage> getUTXOs(List<TransactionOutPoint> outPoints); ListenableFuture<UTXOsMessage> getUTXOs(List<TransactionOutPoint> outPoints, boolean includeMempool); boolean isDownloadTxDependencies(); void setDownloadTxDependencies(boolean enable); void setDownloadTxDependencies(int depth);  }
@Test public void badMessage() throws Exception { final SettableFuture<Void> result = SettableFuture.create(); Threading.uncaughtExceptionHandler = new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread thread, Throwable throwable) { result.setException(throwable); } }; connect(); final SettableFuture<Void> peerDisconnected = SettableFuture.create(); writeTarget.peer.addDisconnectedEventListener(new PeerDisconnectedEventListener() { @Override public void onPeerDisconnected(Peer p, int peerCount) { peerDisconnected.set(null); } }); final NetworkParameters params = TestNet3Params.get(); MessageSerializer serializer = params.getDefaultSerializer(); ByteArrayOutputStream out = new ByteArrayOutputStream(); serializer.serialize("inv", new InventoryMessage(PARAMS) { @Override public void bitcoinSerializeToStream(OutputStream stream) throws IOException { addItem(new InventoryItem(InventoryItem.Type.Transaction, Sha256Hash.of(new byte[]{1}))); addItem(new InventoryItem(InventoryItem.Type.Transaction, Sha256Hash.of(new byte[]{2}))); addItem(new InventoryItem(InventoryItem.Type.Transaction, Sha256Hash.of(new byte[]{3}))); ByteArrayOutputStream bos = new ByteArrayOutputStream(); super.bitcoinSerializeToStream(bos); byte[] bits = bos.toByteArray(); bits = Arrays.copyOf(bits, bits.length / 2); stream.write(bits); } }.bitcoinSerialize(), out); writeTarget.writeTarget.writeBytes(out.toByteArray()); try { result.get(1, TimeUnit.SECONDS); fail(); } catch (ExecutionException e) { assertTrue(e.getCause() instanceof ProtocolException); } peerDisconnected.get(); try { peer.writeTarget.writeBytes(new byte[1]); fail(); } catch (IOException e) { assertTrue((e.getCause() != null && e.getCause() instanceof CancelledKeyException) || (e instanceof SocketException && e.getMessage().equals("Socket is closed"))); } }