/** * Returns most commonly reported chain height from the given list of {@link Peer}s. * If multiple heights are tied, the highest is returned. If no peers are connected, returns zero. */ public static int getMostCommonChainHeight(final List<Peer> peers) { if (peers.isEmpty()) return 0; List<Integer> heights = new ArrayList<>(peers.size()); for (Peer peer : peers) heights.add((int) peer.getBestHeight()); return Utils.maxOfMostFreq(heights); }
/** * Returns most commonly reported chain height from the given list of {@link Peer}s. * If multiple heights are tied, the highest is returned. If no peers are connected, returns zero. */ public static int getMostCommonChainHeight(final List<Peer> peers) { if (peers.isEmpty()) return 0; List<Integer> heights = new ArrayList<Integer>(peers.size()); for (Peer peer : peers) heights.add((int) peer.getBestHeight()); return Utils.maxOfMostFreq(heights); }
/** * Returns most commonly reported chain height from the given list of {@link Peer}s. * If multiple heights are tied, the highest is returned. If no peers are connected, returns zero. */ public static int getMostCommonChainHeight(final List<Peer> peers) { if (peers.isEmpty()) return 0; List<Integer> heights = new ArrayList<>(peers.size()); for (Peer peer : peers) heights.add((int) peer.getBestHeight()); return Utils.maxOfMostFreq(heights); }
/** * Returns most commonly reported chain height from the given list of {@link Peer}s. * If multiple heights are tied, the highest is returned. If no peers are connected, returns zero. */ public static int getMostCommonChainHeight(final List<Peer> peers) { if (peers.isEmpty()) return 0; List<Integer> heights = new ArrayList<Integer>(peers.size()); for (Peer peer : peers) heights.add((int) peer.getBestHeight()); return Utils.maxOfMostFreq(heights); }
/** * Returns the difference between our best chain height and the peers, which can either be positive if we are * behind the peer, or negative if the peer is ahead of us. */ public int getPeerBlockHeightDifference() { checkNotNull(blockChain, "No block chain configured"); // Chain will overflow signed int blocks in ~41,000 years. int chainHeight = (int) getBestHeight(); // chainHeight should not be zero/negative because we shouldn't have given the user a Peer that is to another // client-mode node, nor should it be unconnected. If that happens it means the user overrode us somewhere or // there is a bug in the peer management code. checkState(params.allowEmptyPeerChain() || chainHeight > 0, "Connected to peer with zero/negative chain height", chainHeight); return chainHeight - blockChain.getBestChainHeight(); }
/** * Returns the difference between our best chain height and the peers, which can either be positive if we are * behind the peer, or negative if the peer is ahead of us. */ public int getPeerBlockHeightDifference() { checkNotNull(blockChain, "No block chain configured"); // Chain will overflow signed int blocks in ~41,000 years. int chainHeight = (int) getBestHeight(); // chainHeight should not be zero/negative because we shouldn't have given the user a Peer that is to another // client-mode node, nor should it be unconnected. If that happens it means the user overrode us somewhere or // there is a bug in the peer management code. checkState(params.allowEmptyPeerChain() || chainHeight > 0, "Connected to peer with zero/negative chain height", chainHeight); return chainHeight - blockChain.getBestChainHeight(); }
@Override public void onChainDownloadStarted(Peer peer, int blocksLeft) { if (blocksLeft > 0 && originalBlocksLeft == -1) startDownload(blocksLeft); // Only mark this the first time, because this method can be called more than once during a chain download // if we switch peers during it. if (originalBlocksLeft == -1) originalBlocksLeft = blocksLeft; else log.info("Chain download switched to {}", peer); if (blocksLeft == 0) { doneDownload(); future.set(peer.getBestHeight()); } }
@Override public void onChainDownloadStarted(Peer peer, int blocksLeft) { if (blocksLeft > 0 && originalBlocksLeft == -1) startDownload(blocksLeft); // Only mark this the first time, because this method can be called more than once during a chain download // if we switch peers during it. if (originalBlocksLeft == -1) originalBlocksLeft = blocksLeft; else log.info("Chain download switched to {}", peer); if (blocksLeft == 0) { doneDownload(); future.set(peer.getBestHeight()); } }
@Override public void onChainDownloadStarted(Peer peer, int blocksLeft) { if (blocksLeft > 0 && originalBlocksLeft == -1) startDownload(blocksLeft); // Only mark this the first time, because this method can be called more than once during a chain download // if we switch peers during it. if (originalBlocksLeft == -1) originalBlocksLeft = blocksLeft; else log.info("Chain download switched to {}", peer); if (blocksLeft == 0) { doneDownload(); future.set(peer.getBestHeight()); } }
/** * Returns the difference between our best chain height and the peers, which can either be positive if we are * behind the peer, or negative if the peer is ahead of us. */ public int getPeerBlockHeightDifference() { checkNotNull(blockChain, "No block chain configured"); // Chain will overflow signed int blocks in ~41,000 years. int chainHeight = (int) getBestHeight(); // chainHeight should not be zero/negative because we shouldn't have given the user a Peer that is to another // client-mode node, nor should it be unconnected. If that happens it means the user overrode us somewhere or // there is a bug in the peer management code. checkState(params.allowEmptyPeerChain() || chainHeight > 0, "Connected to peer with zero/negative chain height", chainHeight); return chainHeight - blockChain.getBestChainHeight(); }
@Override public void onChainDownloadStarted(Peer peer, int blocksLeft) { if (blocksLeft > 0 && originalBlocksLeft == -1) startDownload(blocksLeft); // Only mark this the first time, because this method can be called more than once during a chain download // if we switch peers during it. if (originalBlocksLeft == -1) originalBlocksLeft = blocksLeft; else log.info("Chain download switched to {}", peer); if (blocksLeft == 0) { doneDownload(); future.set(peer.getBestHeight()); } }
/** * Returns the difference between our best chain height and the peers, which can either be positive if we are * behind the peer, or negative if the peer is ahead of us. */ public int getPeerBlockHeightDifference() { checkNotNull(blockChain, "No block chain configured"); // Chain will overflow signed int blocks in ~41,000 years. int chainHeight = (int) getBestHeight(); // chainHeight should not be zero/negative because we shouldn't have given the user a Peer that is to another // client-mode node, nor should it be unconnected. If that happens it means the user overrode us somewhere or // there is a bug in the peer management code. checkState(params.allowEmptyPeerChain() || chainHeight > 0, "Connected to peer with zero/negative chain height", chainHeight); return chainHeight - blockChain.getBestChainHeight(); }
@Override public void onBlocksDownloaded(Peer peer, Block block, @Nullable FilteredBlock filteredBlock, int blocksLeft) { if (caughtUp) return; if (blocksLeft == 0) { caughtUp = true; doneDownload(); future.set(peer.getBestHeight()); } if (blocksLeft < 0 || originalBlocksLeft <= 0) return; double pct = 100.0 - (100.0 * (blocksLeft / (double) originalBlocksLeft)); if ((int) pct != lastPercent) { progress(pct, blocksLeft, new Date(block.getTimeSeconds() * 1000)); lastPercent = (int) pct; } }
@Override public void onBlocksDownloaded(Peer peer, Block block, @Nullable FilteredBlock filteredBlock, int blocksLeft) { if (caughtUp) return; if (blocksLeft == 0) { caughtUp = true; doneDownload(); future.set(peer.getBestHeight()); } if (blocksLeft < 0 || originalBlocksLeft <= 0) return; double pct = 100.0 - (100.0 * (blocksLeft / (double) originalBlocksLeft)); if ((int) pct != lastPercent) { progress(pct, blocksLeft, new Date(block.getTimeSeconds() * 1000)); lastPercent = (int) pct; } }
@Override public void onBlocksDownloaded(Peer peer, Block block, @Nullable FilteredBlock filteredBlock, int blocksLeft) { if (caughtUp) return; if (blocksLeft == 0) { caughtUp = true; doneDownload(); future.set(peer.getBestHeight()); } if (blocksLeft < 0 || originalBlocksLeft <= 0) return; double pct = 100.0 - (100.0 * (blocksLeft / (double) originalBlocksLeft)); if ((int) pct != lastPercent) { progress(pct, blocksLeft, new Date(block.getTimeSeconds() * 1000)); lastPercent = (int) pct; } }
List<Peer> candidates = new ArrayList<Peer>(); for (Peer peer : peers) { if (peer.getBestHeight() == mostCommonChainHeight) candidates.add(peer);
List<Peer> candidates = new ArrayList<>(); for (Peer peer : peers) { if (peer.getBestHeight() == mostCommonChainHeight) candidates.add(peer);
List<Peer> candidates = new ArrayList<>(); for (Peer peer : peers) { if (peer.getBestHeight() == mostCommonChainHeight) candidates.add(peer);
@Override public void onBlocksDownloaded(Peer peer, Block block, @Nullable FilteredBlock filteredBlock, int blocksLeft) { if (caughtUp) return; if (blocksLeft == 0) { caughtUp = true; if (lastPercent != 100) { lastPercent = 100; progress(lastPercent, blocksLeft, new Date(block.getTimeSeconds() * 1000)); } doneDownload(); future.set(peer.getBestHeight()); return; } if (blocksLeft < 0 || originalBlocksLeft <= 0) return; double pct = 100.0 - (100.0 * (blocksLeft / (double) originalBlocksLeft)); if ((int) pct != lastPercent) { progress(pct, blocksLeft, new Date(block.getTimeSeconds() * 1000)); lastPercent = (int) pct; } }
return peer.getPeerVersionMessage().subVer; case CHAIN_HEIGHT: return peer.getBestHeight(); case PING_TIME: case LAST_PING_TIME: