Skip to content

Commit

Permalink
Fixed: server-connected nodes were not tested by nodeSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
zilm13 committed Feb 8, 2017
1 parent 6697fd1 commit 9e40463
Showing 1 changed file with 42 additions and 26 deletions.
68 changes: 42 additions & 26 deletions ethereumj-core/src/main/java/org/ethereum/sync/SyncPool.java
Expand Up @@ -215,44 +215,51 @@ synchronized void logActivePeers() {
}
}

private void fillUp() {
int lackSize = config.maxActivePeers() - channelManager.getActivePeers().size();
if(lackSize <= 0) return;
class NodeSelector implements Functional.Predicate<NodeHandler> {
BigInteger lowerDifficulty;
Set<String> nodesInUse;

final Set<String> nodesInUse = nodesInUse();
nodesInUse.add(Hex.toHexString(config.nodeId())); // exclude home node
public NodeSelector(BigInteger lowerDifficulty) {
this.lowerDifficulty = lowerDifficulty;
}

class NodeSelector implements Functional.Predicate<NodeHandler> {
BigInteger lowerDifficulty;
public NodeSelector(BigInteger lowerDifficulty, Set<String> nodesInUse) {
this.lowerDifficulty = lowerDifficulty;
this.nodesInUse = nodesInUse;
}

public NodeSelector(BigInteger lowerDifficulty) {
this.lowerDifficulty = lowerDifficulty;
@Override
public boolean test(NodeHandler handler) {
if (nodesInUse != null && nodesInUse.contains(handler.getNode().getHexId())) {
return false;
}

@Override
public boolean test(NodeHandler handler) {
if (nodesInUse.contains(handler.getNode().getHexId())) {
return false;
}

if (handler.getNodeStatistics().isPredefined()) return true;
if (handler.getNodeStatistics().isPredefined()) return true;

if (nodesSelector != null && !nodesSelector.test(handler)) return false;
if (nodesSelector != null && !nodesSelector.test(handler)) return false;

if (lowerDifficulty.compareTo(BigInteger.ZERO) > 0 && handler.getNodeStatistics().getEthTotalDifficulty() == null) {
return false;
}
if (lowerDifficulty.compareTo(BigInteger.ZERO) > 0 &&
handler.getNodeStatistics().getEthTotalDifficulty() == null) {
return false;
}

if (handler.getNodeStatistics().getReputation() < 100) return false;
if (handler.getNodeStatistics().getReputation() < 100) return false;

return handler.getNodeStatistics().getEthTotalDifficulty().compareTo(lowerDifficulty) >= 0;
}
return handler.getNodeStatistics().getEthTotalDifficulty().compareTo(lowerDifficulty) >= 0;
}
}

private void fillUp() {
int lackSize = config.maxActivePeers() - channelManager.getActivePeers().size();
if(lackSize <= 0) return;

final Set<String> nodesInUse = nodesInUse();
nodesInUse.add(Hex.toHexString(config.nodeId())); // exclude home node

List<NodeHandler> newNodes;
newNodes = nodeManager.getNodes(new NodeSelector(lowerUsefulDifficulty), lackSize);
newNodes = nodeManager.getNodes(new NodeSelector(lowerUsefulDifficulty, nodesInUse), lackSize);
if (lackSize > 0 && newNodes.isEmpty()) {
newNodes = nodeManager.getNodes(new NodeSelector(BigInteger.ZERO), lackSize);
newNodes = nodeManager.getNodes(new NodeSelector(BigInteger.ZERO, nodesInUse), lackSize);
}

if (logger.isTraceEnabled()) {
Expand All @@ -265,7 +272,16 @@ public boolean test(NodeHandler handler) {
}

private synchronized void prepareActive() {
List<Channel> active = new ArrayList<>(channelManager.getActivePeers());
List<Channel> managerActive = new ArrayList<>(channelManager.getActivePeers());

// Filtering out with nodeSelector because server-connected nodes were not tested
NodeSelector nodeSelector = new NodeSelector(BigInteger.ZERO);
List<Channel> active = new ArrayList<>();
for (Channel channel : managerActive) {
if (nodeSelector.test(nodeManager.getNodeHandler(channel.getNode()))) {
active.add(channel);
}
}

if (active.isEmpty()) return;

Expand Down

0 comments on commit 9e40463

Please sign in to comment.