Skip to content

Commit 40ce84f

Browse files
authored
Merge pull request #1036 from tronprotocol/revert-1023-w_p2p
Revert "p2p: add trust nodes & active nodes function and solving eclipse attack"
2 parents 4b5f223 + 9c6f3a2 commit 40ce84f

File tree

9 files changed

+48
-82
lines changed

9 files changed

+48
-82
lines changed

src/main/java/org/tron/common/overlay/discover/node/NodeHandler.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public enum State {
8787
private NodeStatistics nodeStatistics;
8888
private NodeHandler replaceCandidate;
8989
private volatile boolean waitForPong = false;
90-
private volatile boolean waitForNeighbors = false;
9190
private volatile int pingTrials = 3;
9291
private long pingSent;
9392

@@ -205,11 +204,6 @@ public void handlePong(PongMessage msg) {
205204
}
206205

207206
public void handleNeighbours(NeighborsMessage msg) {
208-
if (!waitForNeighbors){
209-
logger.warn("Receive neighbors from {} without send find nodes.", node.getHost());
210-
return;
211-
}
212-
waitForNeighbors = false;
213207
getNodeStatistics().discoverInNeighbours.add();
214208
for (Node n : msg.getNodes()) {
215209
if (!nodeManager.getPublicHomeNode().getHexId().equals(n.getHexId())) {
@@ -275,7 +269,6 @@ public void sendNeighbours(List<Node> neighbours) {
275269
}
276270

277271
public void sendFindNode(byte[] target) {
278-
waitForNeighbors = true;
279272
Message findNode = new FindNodeMessage(nodeManager.getPublicHomeNode(), target);
280273
sendMessage(findNode);
281274
getNodeStatistics().discoverOutFind.add();

src/main/java/org/tron/common/overlay/discover/node/NodeManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ public void run() {
138138
for (Node node : bootNodes) {
139139
getNodeHandler(node);
140140
}
141+
142+
for (Node node : args.getNodeActive()) {
143+
getNodeHandler(node).getNodeStatistics().setPredefined(true);
144+
}
141145
}
142146
}
143147

src/main/java/org/tron/common/overlay/discover/table/NodeEntry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ public class NodeEntry {
3333
public NodeEntry(Node n) {
3434
this.node = n;
3535
this.ownerId = n.getId();
36-
entryId = n.getHost();
36+
entryId = n.toString();
3737
distance = distance(ownerId, n.getId());
3838
touch();
3939
}
4040

4141
public NodeEntry(byte[] ownerId, Node n) {
4242
this.node = n;
4343
this.ownerId = ownerId;
44-
entryId = n.getHost();
44+
entryId = n.toString();
4545
distance = distance(ownerId, n.getId());
4646
touch();
4747
}

src/main/java/org/tron/common/overlay/discover/table/NodeTable.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,6 @@ public final void initialize() {
5959

6060
public synchronized Node addNode(Node n) {
6161
NodeEntry e = new NodeEntry(node.getId(), n);
62-
if (nodes.contains(e)) {
63-
nodes.forEach(nodeEntry -> {
64-
if (nodeEntry.equals(e)) {
65-
nodeEntry.touch();
66-
}
67-
});
68-
return null;
69-
}
7062
NodeEntry lastSeen = buckets[getBucketId(e)].addNode(e);
7163
if (lastSeen != null) {
7264
return lastSeen.getNode();

src/main/java/org/tron/common/overlay/server/ChannelManager.java

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*
2+
* Copyright (c) [2016] [ <ether.camp> ]
3+
* This file is part of the ethereumJ library.
4+
*
5+
* The ethereumJ library is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* The ethereumJ library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
17+
*/
118
package org.tron.common.overlay.server;
219

320
import static org.tron.protos.Protocol.ReasonCode.DUPLICATE_PEER;
@@ -7,7 +24,6 @@
724
import com.google.common.cache.Cache;
825
import com.google.common.cache.CacheBuilder;
926
import java.net.InetAddress;
10-
import java.net.InetSocketAddress;
1127
import java.util.Collection;
1228
import java.util.Map;
1329
import java.util.concurrent.ConcurrentHashMap;
@@ -17,7 +33,6 @@
1733
import org.springframework.beans.factory.annotation.Autowired;
1834
import org.springframework.stereotype.Component;
1935
import org.tron.common.overlay.client.PeerClient;
20-
import org.tron.common.overlay.discover.node.Node;
2136
import org.tron.core.config.args.Args;
2237
import org.tron.core.db.ByteArrayWrapper;
2338
import org.tron.protos.Protocol.ReasonCode;
@@ -38,8 +53,6 @@ public class ChannelManager {
3853
private Cache<InetAddress, ReasonCode> recentlyDisconnected = CacheBuilder.newBuilder().maximumSize(1000)
3954
.expireAfterWrite(30, TimeUnit.SECONDS).recordStats().build();
4055

41-
private Map<InetAddress, Node> trustPeers = new ConcurrentHashMap();
42-
4356
private Args args = Args.getInstance();
4457

4558
private int maxActivePeers = args.getNodeMaxActiveNodes() > 0 ? args.getNodeMaxActiveNodes() : 30;
@@ -60,11 +73,6 @@ private ChannelManager(final PeerServer peerServer, final PeerClient peerClient)
6073
new Thread(() -> peerServer.start(Args.getInstance().getNodeListenPort()),
6174
"PeerServerThread").start();
6275
}
63-
64-
for (Node node : args.getTrustNodes()){
65-
trustPeers.put(new InetSocketAddress(node.getHost(), node.getPort()).getAddress() , node);
66-
}
67-
logger.info("Trust peer size {}", trustPeers.size());
6876
}
6977

7078
public void processDisconnect(Channel channel, ReasonCode reason){
@@ -102,21 +110,19 @@ public void notifyDisconnect(Channel channel) {
102110

103111
public synchronized boolean processPeer(Channel peer) {
104112

105-
if (!trustPeers.containsKey(peer.getInetAddress())){
106-
if (recentlyDisconnected.getIfPresent(peer) != null){
107-
logger.info("Peer {} recently disconnected.", peer.getInetAddress());
108-
return false;
109-
}
113+
if (recentlyDisconnected.getIfPresent(peer) != null){
114+
logger.info("Peer {} recently disconnected.", peer.getInetAddress());
115+
return false;
116+
}
110117

111-
if (badPeers.getIfPresent(peer) != null) {
112-
peer.disconnect(peer.getNodeStatistics().getDisconnectReason());
113-
return false;
114-
}
118+
if (badPeers.getIfPresent(peer) != null) {
119+
peer.disconnect(peer.getNodeStatistics().getDisconnectReason());
120+
return false;
121+
}
115122

116-
if (!peer.isActive() && activePeers.size() >= maxActivePeers) {
117-
peer.disconnect(TOO_MANY_PEERS);
118-
return false;
119-
}
123+
if (!peer.isActive() && activePeers.size() >= maxActivePeers) {
124+
peer.disconnect(TOO_MANY_PEERS);
125+
return false;
120126
}
121127

122128
if (activePeers.containsKey(peer.getNodeIdWrapper())) {

src/main/java/org/tron/common/overlay/server/SyncPool.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import com.google.common.cache.Cache;
2121
import com.google.common.cache.CacheBuilder;
22-
import com.google.common.collect.Lists;
2322
import java.net.InetAddress;
2423
import java.util.ArrayList;
2524
import java.util.Collections;
@@ -32,6 +31,8 @@
3231
import java.util.concurrent.TimeUnit;
3332
import java.util.concurrent.atomic.AtomicInteger;
3433
import java.util.function.Predicate;
34+
35+
import com.google.common.collect.Lists;
3536
import org.slf4j.Logger;
3637
import org.slf4j.LoggerFactory;
3738
import org.springframework.beans.factory.annotation.Autowired;
@@ -51,10 +52,8 @@ public class SyncPool {
5152
public static final Logger logger = LoggerFactory.getLogger("SyncPool");
5253

5354
private static final double factor = 0.4;
54-
private static final double activeFactor = 0.2;
5555

56-
private final List<PeerConnection> activePeers = Collections
57-
.synchronizedList(new ArrayList<PeerConnection>());
56+
private final List<PeerConnection> activePeers = Collections.synchronizedList(new ArrayList<PeerConnection>());
5857
private final AtomicInteger passivePeersCount = new AtomicInteger(0);
5958
private final AtomicInteger activePeersCount = new AtomicInteger(0);
6059

@@ -88,10 +87,6 @@ public void init(PeerConnectionDelegate peerDel) {
8887

8988
peerClient = ctx.getBean(PeerClient.class);
9089

91-
for (Node node : args.getActiveNodes()) {
92-
nodeManager.getNodeHandler(node).getNodeStatistics().setPredefined(true);
93-
}
94-
9590
poolLoopExecutor.scheduleWithFixedDelay(() -> {
9691
try {
9792
fillUp();
@@ -109,11 +104,8 @@ public void init(PeerConnectionDelegate peerDel) {
109104
}
110105

111106
private void fillUp() {
112-
int lackSize = Math.max((int) (maxActiveNodes * factor) - activePeers.size(),
113-
(int) (maxActiveNodes * activeFactor - activePeersCount.get()));
114-
if (lackSize <= 0) {
115-
return;
116-
}
107+
int lackSize = (int) (maxActiveNodes * factor) - activePeers.size();
108+
if(lackSize <= 0) return;
117109

118110
final Set<String> nodesInUse = new HashSet<>();
119111
channelManager.getActivePeers().forEach(channel -> nodesInUse.add(channel.getPeerId()));
@@ -197,7 +189,7 @@ public synchronized void onDisconnect(Channel peer) {
197189
}
198190

199191
public boolean isCanConnect() {
200-
if (passivePeersCount.get() >= maxActiveNodes * (1 - activeFactor)) {
192+
if (activePeers.size() >= maxActiveNodes) {
201193
return false;
202194
}
203195
return true;

src/main/java/org/tron/core/config/args/Args.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,7 @@ public class Args {
121121

122122
@Getter
123123
@Setter
124-
private List<Node> activeNodes;
125-
126-
@Getter
127-
@Setter
128-
private List<Node> trustNodes;
124+
private List<Node> nodeActive;
129125

130126
@Getter
131127
@Setter
@@ -272,8 +268,7 @@ public static void clearParam() {
272268
INSTANCE.nodeDiscoveryEnable = false;
273269
INSTANCE.nodeDiscoveryPersist = false;
274270
INSTANCE.nodeConnectionTimeout = 0;
275-
INSTANCE.activeNodes = Collections.emptyList();
276-
INSTANCE.trustNodes = Collections.emptyList();
271+
INSTANCE.nodeActive = Collections.emptyList();
277272
INSTANCE.nodeChannelReadTimeout = 0;
278273
INSTANCE.nodeMaxActiveNodes = 0;
279274
INSTANCE.minParticipationRate = 0;
@@ -407,9 +402,7 @@ public static void setParam(final String[] args, final String confFileName) {
407402
config.hasPath("node.connection.timeout") ? config.getInt("node.connection.timeout") * 1000
408403
: 0;
409404

410-
INSTANCE.activeNodes = getNodes(config, "active.node");
411-
412-
INSTANCE.trustNodes = getNodes(config, "trust.node");
405+
INSTANCE.nodeActive = nodeActive(config);
413406

414407
INSTANCE.nodeChannelReadTimeout =
415408
config.hasPath("node.channel.read.timeout") ? config.getInt("node.channel.read.timeout")
@@ -551,12 +544,12 @@ public String getOutputDirectory() {
551544
return this.outputDirectory;
552545
}
553546

554-
private static List<Node> getNodes(final com.typesafe.config.Config config, String path) {
555-
if (!config.hasPath(path)) {
547+
private static List<Node> nodeActive(final com.typesafe.config.Config config) {
548+
if (!config.hasPath("node.active")) {
556549
return Collections.EMPTY_LIST;
557550
}
558551
List<Node> ret = new ArrayList<>();
559-
List<String> list = config.getStringList(path);
552+
List<String> list = config.getStringList("node.active");
560553
for (String configString : list) {
561554
Node n = Node.instanceOf(configString);
562555
ret.add(n);

src/main/resources/config.conf

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ storage {
3131
// writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B
3232
// cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B
3333
// maxOpenFiles = 100
34-
// },
34+
// },
3535
// {
3636
// name = "account-index",
3737
// path = "storage_directory_test",
@@ -123,20 +123,6 @@ node {
123123

124124
}
125125

126-
active.node = [
127-
# Active establish connection in any case
128-
# Sample entries:
129-
# "ip:port",
130-
# "ip:port"
131-
]
132-
133-
trust.node = [
134-
# Passive accept connection in any case
135-
# Sample entries:
136-
# "ip:port",
137-
# "ip:port"
138-
]
139-
140126
seed.node = {
141127
# List of the seed nodes
142128
# Seed nodes are stable full nodes

src/test/java/org/tron/core/config/args/ArgsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void get() {
6262
Assert.assertEquals("46.168.1.1", args.getNodeExternalIp());
6363
Assert.assertEquals(18888, args.getNodeListenPort());
6464
Assert.assertEquals(2000, args.getNodeConnectionTimeout());
65-
Assert.assertEquals(0, args.getActiveNodes().size());
65+
Assert.assertEquals(0, args.getNodeActive().size());
6666
Assert.assertEquals(30, args.getNodeMaxActiveNodes());
6767
Assert.assertEquals(43, args.getNodeP2pVersion());
6868
//Assert.assertEquals(30, args.getSyncNodeCount());

0 commit comments

Comments
 (0)