/** * Returns an array list of peers that implement the given protocol version or better. */ public List<Peer> findPeersOfAtLeastVersion(long protocolVersion) { lock.lock(); try { ArrayList<Peer> results = new ArrayList<Peer>(peers.size()); for (Peer peer : peers) if (peer.getPeerVersionMessage().clientVersion >= protocolVersion) results.add(peer); return results; } finally { lock.unlock(); } }
/** * Returns an array list of peers that implement the given protocol version or better. */ public List<Peer> findPeersOfAtLeastVersion(long protocolVersion) { lock.lock(); try { ArrayList<Peer> results = new ArrayList<Peer>(peers.size()); for (Peer peer : peers) if (peer.getPeerVersionMessage().clientVersion >= protocolVersion) results.add(peer); return results; } finally { lock.unlock(); } }
/** * Returns an array list of peers that match the requested service bit mask. */ public List<Peer> findPeersWithServiceMask(int mask) { lock.lock(); try { ArrayList<Peer> results = new ArrayList<Peer>(peers.size()); for (Peer peer : peers) if ((peer.getPeerVersionMessage().localServices & mask) == mask) results.add(peer); return results; } finally { lock.unlock(); } }
/** * Returns an array list of peers that match the requested service bit mask. */ public List<Peer> findPeersWithServiceMask(int mask) { lock.lock(); try { ArrayList<Peer> results = new ArrayList<Peer>(peers.size()); for (Peer peer : peers) if ((peer.getPeerVersionMessage().localServices & mask) == mask) results.add(peer); return results; } finally { lock.unlock(); } }
/** * Returns an array list of peers that implement the given protocol version or better. */ public List<Peer> findPeersOfAtLeastVersion(long protocolVersion) { lock.lock(); try { ArrayList<Peer> results = new ArrayList<Peer>(peers.size()); for (Peer peer : peers) if (peer.getPeerVersionMessage().clientVersion >= protocolVersion) results.add(peer); return results; } finally { lock.unlock(); } }
/** * Returns an array list of peers that match the requested service bit mask. */ public List<Peer> findPeersWithServiceMask(int mask) { lock.lock(); try { ArrayList<Peer> results = new ArrayList<Peer>(peers.size()); for (Peer peer : peers) if ((peer.getPeerVersionMessage().localServices & mask) == mask) results.add(peer); return results; } finally { lock.unlock(); } }
/** * Returns an array list of peers that implement the given protocol version or better. */ public List<Peer> findPeersOfAtLeastVersion(long protocolVersion) { lock.lock(); try { ArrayList<Peer> results = new ArrayList<Peer>(peers.size()); for (Peer peer : peers) if (peer.getPeerVersionMessage().clientVersion >= protocolVersion) results.add(peer); return results; } finally { lock.unlock(); } }
/** * Returns an array list of peers that match the requested service bit mask. */ public List<Peer> findPeersWithServiceMask(int mask) { lock.lock(); try { ArrayList<Peer> results = new ArrayList<Peer>(peers.size()); for (Peer peer : peers) if ((peer.getPeerVersionMessage().localServices & mask) == mask) results.add(peer); return results; } finally { lock.unlock(); } }
@Override public void serialize(Peer value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeStartObject(); jgen.writeStringField("socketAddress", value.getAddress().toSocketAddress().toString()); jgen.writeNumberField("remoteVersion", value.getPeerVersionMessage().clientVersion); jgen.writeNumberField("bestHeight", value.getPeerVersionMessage().bestHeight); jgen.writeEndObject(); } }
/** * The minimum P2P protocol version that is accepted. If the peer speaks a protocol version lower than this, it * will be disconnected. * @return true if the peer was disconnected as a result */ public boolean setMinProtocolVersion(int minProtocolVersion) { this.vMinProtocolVersion = minProtocolVersion; VersionMessage ver = getPeerVersionMessage(); if (ver != null && ver.clientVersion < minProtocolVersion) { log.warn("{}: Disconnecting due to new min protocol version {}, got: {}", this, minProtocolVersion, ver.clientVersion); close(); return true; } return false; }
/** * The minimum P2P protocol version that is accepted. If the peer speaks a protocol version lower than this, it * will be disconnected. * @return true if the peer was disconnected as a result */ public boolean setMinProtocolVersion(int minProtocolVersion) { this.vMinProtocolVersion = minProtocolVersion; VersionMessage ver = getPeerVersionMessage(); if (ver != null && ver.clientVersion < minProtocolVersion) { log.warn("{}: Disconnecting due to new min protocol version {}, got: {}", this, minProtocolVersion, ver.clientVersion); close(); return true; } return false; }
/** * The minimum P2P protocol version that is accepted. If the peer speaks a protocol version lower than this, it * will be disconnected. * @return true if the peer was disconnected as a result */ public boolean setMinProtocolVersion(int minProtocolVersion) { this.vMinProtocolVersion = minProtocolVersion; VersionMessage ver = getPeerVersionMessage(); if (ver != null && ver.clientVersion < minProtocolVersion) { log.warn("{}: Disconnecting due to new min protocol version {}, got: {}", this, minProtocolVersion, ver.clientVersion); close(); return true; } return false; }
/** * The minimum P2P protocol version that is accepted. If the peer speaks a protocol version lower than this, it * will be disconnected. * @return true if the peer was disconnected as a result */ public boolean setMinProtocolVersion(int minProtocolVersion) { this.vMinProtocolVersion = minProtocolVersion; VersionMessage ver = getPeerVersionMessage(); if (ver != null && ver.clientVersion < minProtocolVersion) { log.warn("{}: Disconnecting due to new min protocol version {}, got: {}", this, minProtocolVersion, ver.clientVersion); close(); return true; } return false; }
@Override public void onPeerConnected(Peer p, int peerCount) { // Check the chain height it claims to have. VersionMessage ver = peer.getPeerVersionMessage(); long nodeHeight = ver.bestHeight; synchronized (lock) { long diff = bestHeight[0] - nodeHeight; if (diff > 0) { System.out.println("Node is behind by " + diff + " blocks: " + addr); } else if (diff == 0) { System.out.println("Node " + addr + " has " + nodeHeight + " blocks"); bestHeight[0] = nodeHeight; } else if (diff < 0) { System.out.println("Node is ahead by " + Math.abs(diff) + " blocks: " + addr); bestHeight[0] = nodeHeight; } } // Now finish the future and close the connection future.set(null); peer.close(); } });
/** * Sends a query to the remote peer asking for the unspent transaction outputs (UTXOs) for the given outpoints. * The result should be treated only as a hint: it's possible for the returned outputs to be fictional and not * exist in any transaction, and it's possible for them to be spent the moment after the query returns. * <b>Most peers do not support this request. You will need to connect to Bitcoin XT peers if you want * this to work.</b> * * @param includeMempool If true (the default) the results take into account the contents of the memory pool too. * @throws ProtocolException if this peer doesn't support the protocol. */ public ListenableFuture<UTXOsMessage> getUTXOs(List<TransactionOutPoint> outPoints, boolean includeMempool) { lock.lock(); try { VersionMessage peerVer = getPeerVersionMessage(); if (peerVer.clientVersion < GetUTXOsMessage.MIN_PROTOCOL_VERSION) throw new ProtocolException("Peer does not support getutxos protocol version"); if ((peerVer.localServices & GetUTXOsMessage.SERVICE_FLAGS_REQUIRED) != GetUTXOsMessage.SERVICE_FLAGS_REQUIRED) throw new ProtocolException("Peer does not support getutxos protocol flag: find Bitcoin XT nodes."); SettableFuture<UTXOsMessage> future = SettableFuture.create(); // Add to the list of in flight requests. if (getutxoFutures == null) getutxoFutures = new LinkedList<SettableFuture<UTXOsMessage>>(); getutxoFutures.add(future); sendMessage(new GetUTXOsMessage(params, outPoints, includeMempool)); return future; } finally { lock.unlock(); } }
@Override public void onPeerConnected(Peer peer, int peerCount) { if (!peer.getPeerVersionMessage().subVer.contains("Satoshi")) { System.out.println(); System.out.println("************************************************************************************************************************\n" + "WARNING: You appear to be using this to test an alternative implementation with full validation rules. You should go\n" + "think hard about what you're doing. Seriously, no one has gotten even close to correctly reimplementing Bitcoin\n" + "consensus rules, despite serious investment in trying. It is a huge task and the slightest difference is a huge bug.\n" + "Instead, go work on making Bitcoin Core consensus rules a shared library and use that. Seriously, you wont get it right,\n" + "and starting with this tester as a way to try to do so will simply end in pain and lost coins.\n" + "************************************************************************************************************************"); System.out.println(); } log.info("bitcoind connected"); // Make sure bitcoind has no blocks bitcoind.setDownloadParameters(0, false); bitcoind.startBlockChainDownload(); connectedFuture.set(null); } });
@Override public void run() { try { if (getPingIntervalMsec() <= 0) { ListenableScheduledFuture<?> task = vPingTask; if (task != null) { task.cancel(false); vPingTask = null; } return; // Disabled. } for (Peer peer : getConnectedPeers()) { if (peer.getPeerVersionMessage().clientVersion < params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.PONG)) continue; peer.ping(); } } catch (Throwable e) { log.error("Exception in ping loop", e); // The executor swallows exceptions :( } } }, getPingIntervalMsec(), getPingIntervalMsec(), TimeUnit.MILLISECONDS);
@Override public void run() { try { if (getPingIntervalMsec() <= 0) { ListenableScheduledFuture<?> task = vPingTask; if (task != null) { task.cancel(false); vPingTask = null; } return; // Disabled. } for (Peer peer : getConnectedPeers()) { if (peer.getPeerVersionMessage().clientVersion < params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.PONG)) continue; peer.ping(); } } catch (Throwable e) { log.error("Exception in ping loop", e); // The executor swallows exceptions :( } } }, getPingIntervalMsec(), getPingIntervalMsec(), TimeUnit.MILLISECONDS);
@Override public void run() { try { if (getPingIntervalMsec() <= 0) { ListenableScheduledFuture<?> task = vPingTask; if (task != null) { task.cancel(false); vPingTask = null; } return; // Disabled. } for (Peer peer : getConnectedPeers()) { if (peer.getPeerVersionMessage().clientVersion < params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.PONG)) continue; peer.ping(); } } catch (Throwable e) { log.error("Exception in ping loop", e); // The executor swallows exceptions :( } } }, getPingIntervalMsec(), getPingIntervalMsec(), TimeUnit.MILLISECONDS);
@Override public void run() { try { if (getPingIntervalMsec() <= 0) { ListenableScheduledFuture<?> task = vPingTask; if (task != null) { task.cancel(false); vPingTask = null; } return; // Disabled. } for (Peer peer : getConnectedPeers()) { if (peer.getPeerVersionMessage().clientVersion < params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.PONG)) continue; peer.ping(); } } catch (Throwable e) { log.error("Exception in ping loop", e); // The executor swallows exceptions :( } } }, getPingIntervalMsec(), getPingIntervalMsec(), TimeUnit.MILLISECONDS);