Skip to content

Commit

Permalink
fix the merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
lvs007 committed Sep 24, 2019
2 parents 98546d4 + f38a369 commit a66335e
Show file tree
Hide file tree
Showing 78 changed files with 3,383 additions and 1,835 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ dependencies {
// http
compile 'org.eclipse.jetty:jetty-server:9.4.11.v20180605'
compile 'org.eclipse.jetty:jetty-servlet:9.4.11.v20180605'
compile 'com.alibaba:fastjson:1.2.47'
compile 'com.alibaba:fastjson:1.2.60'
compile group: 'org.apache.httpcomponents', name: 'httpasyncclient', version: '4.1.1'
// end http

Expand Down
43 changes: 3 additions & 40 deletions src/main/java/org/tron/common/overlay/discover/DiscoverTask.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
/*
* Copyright (c) [2016] [ <ether.camp> ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*/

package org.tron.common.overlay.discover;

import java.util.ArrayList;
Expand All @@ -24,18 +6,17 @@
import org.tron.common.overlay.discover.node.Node;
import org.tron.common.overlay.discover.node.NodeManager;
import org.tron.common.overlay.discover.table.KademliaOptions;
import org.tron.common.overlay.discover.table.NodeEntry;

@Slf4j(topic = "discover")
public class DiscoverTask implements Runnable {

NodeManager nodeManager;
private NodeManager nodeManager;

byte[] nodeId;
private byte[] nodeId;

public DiscoverTask(NodeManager nodeManager) {
this.nodeManager = nodeManager;
nodeId = nodeManager.getPublicHomeNode().getId();
this.nodeId = nodeManager.getPublicHomeNode().getId();
}

@Override
Expand All @@ -47,12 +28,6 @@ public synchronized void discover(byte[] nodeId, int round, List<Node> prevTried

try {
if (round == KademliaOptions.MAX_STEPS) {
logger.debug("Node table contains [{}] peers", nodeManager.getTable().getNodesCount());
logger.debug("{}", String
.format("(KademliaOptions.MAX_STEPS) Terminating discover after %d rounds.", round));
logger.trace("{}\n{}",
String.format("Nodes discovered %d ", nodeManager.getTable().getNodesCount()),
dumpNodes());
return;
}

Expand All @@ -74,11 +49,6 @@ public synchronized void discover(byte[] nodeId, int round, List<Node> prevTried
}

if (tried.isEmpty()) {
logger.debug("{}",
String.format("(tried.isEmpty()) Terminating discover after %d rounds.", round));
logger.trace("{}\n{}",
String.format("Nodes discovered %d ", nodeManager.getTable().getNodesCount()),
dumpNodes());
return;
}

Expand All @@ -90,11 +60,4 @@ public synchronized void discover(byte[] nodeId, int round, List<Node> prevTried
}
}

private String dumpNodes() {
String ret = "";
for (NodeEntry entry : nodeManager.getTable().getAllNodes()) {
ret += " " + entry.getNode() + "\n";
}
return ret;
}
}
10 changes: 1 addition & 9 deletions src/main/java/org/tron/common/overlay/discover/RefreshTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.tron.common.overlay.discover;

import java.util.ArrayList;
import java.util.Random;
import lombok.extern.slf4j.Slf4j;
import org.tron.common.overlay.discover.node.Node;
import org.tron.common.overlay.discover.node.NodeManager;
Expand All @@ -31,15 +30,8 @@ public RefreshTask(NodeManager nodeManager) {
super(nodeManager);
}

public static byte[] getNodeId() {
Random gen = new Random();
byte[] id = new byte[64];
gen.nextBytes(id);
return id;
}

@Override
public void run() {
discover(getNodeId(), 0, new ArrayList<Node>());
discover(Node.getNodeId(), 0, new ArrayList<>());
}
}
66 changes: 38 additions & 28 deletions src/main/java/org/tron/common/overlay/discover/node/Node.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
/*
* Copyright (c) [2016] [ <ether.camp> ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*/

package org.tron.common.overlay.discover.node;

import static org.tron.common.crypto.Hash.sha3;

import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Random;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.spongycastle.util.encoders.Hex;
import org.tron.common.crypto.ECKey;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.Utils;
import org.tron.core.config.args.Args;

public class Node implements Serializable {

Expand All @@ -39,6 +22,14 @@ public class Node implements Serializable {

private int port;

@Getter
private int bindPort;

@Setter
private int p2pVersion;

private int reputation = 0;

private boolean isFakeNodeId = false;

public int getReputation() {
Expand All @@ -49,8 +40,6 @@ public void setReputation(int reputation) {
this.reputation = reputation;
}

private int reputation = 0;

public static Node instanceOf(String addressOrEnode) {
try {
URI uri = new URI(addressOrEnode);
Expand All @@ -61,10 +50,8 @@ public static Node instanceOf(String addressOrEnode) {
// continue
}

final ECKey generatedNodeKey = ECKey.fromPrivate(sha3(addressOrEnode.getBytes()));
final String generatedNodeId = Hex.toHexString(generatedNodeKey.getNodeId());
final String generatedNodeId = Hex.toHexString(getNodeId());
final Node node = new Node("enode://" + generatedNodeId + "@" + addressOrEnode);
node.isFakeNodeId = true;
return node;
}

Expand All @@ -84,6 +71,8 @@ public Node(String enodeURL) {
this.id = Hex.decode(uri.getUserInfo());
this.host = uri.getHost();
this.port = uri.getPort();
this.bindPort = uri.getPort();
this.isFakeNodeId = true;
} catch (URISyntaxException e) {
throw new RuntimeException("expecting URL in the format enode://PUBKEY@HOST:PORT", e);
}
Expand All @@ -95,6 +84,20 @@ public Node(byte[] id, String host, int port) {
}
this.host = host;
this.port = port;
this.isFakeNodeId = true;
}

public Node(byte[] id, String host, int port, int bindPort) {
if (id != null) {
this.id = id.clone();
}
this.host = host;
this.port = port;
this.bindPort = bindPort;
}

public boolean isConnectible() {
return port == bindPort && p2pVersion == Args.getInstance().getNodeP2pVersion();
}

public String getHexId() {
Expand All @@ -110,11 +113,11 @@ public boolean isDiscoveryNode() {
}

public byte[] getId() {
return id == null ? id : id.clone();
return id;
}

public void setId(byte[] id) {
this.id = id == null ? null : id.clone();
this.id = id;
}

public String getHost() {
Expand All @@ -136,6 +139,13 @@ public String getIdString() {
return new String(id);
}

public static byte[] getNodeId() {
Random gen = new Random();
byte[] id = new byte[64];
gen.nextBytes(id);
return id;
}

@Override
public String toString() {
return "Node{" + " host='" + host + '\'' + ", port=" + port
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ public void handlePing(PingMessage msg) {
if (!nodeManager.getTable().getNode().equals(node)) {
sendPong(msg.getTimestamp());
}
if (msg.getVersion() != Args.getInstance().getNodeP2pVersion()) {
node.setP2pVersion(msg.getVersion());
if (!node.isConnectible()) {
changeState(State.NonActive);
} else if (state.equals(State.NonActive) || state.equals(State.Dead)) {
changeState(State.Discovered);
Expand All @@ -204,7 +205,8 @@ public void handlePong(PongMessage msg) {
getNodeStatistics().discoverMessageLatency.add(System.currentTimeMillis() - pingSent);
getNodeStatistics().lastPongReplyTime.set(System.currentTimeMillis());
node.setId(msg.getFrom().getId());
if (msg.getVersion() != Args.getInstance().getNodeP2pVersion()) {
node.setP2pVersion(msg.getVersion());
if (!node.isConnectible()) {
changeState(State.NonActive);
} else {
changeState(State.Alive);
Expand Down
Loading

0 comments on commit a66335e

Please sign in to comment.