Skip to content

Commit 4891b76

Browse files
committed
Merge remote-tracking branch 'origin/master' into develop
2 parents 7ee8a07 + 564864a commit 4891b76

95 files changed

Lines changed: 5545 additions & 305 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎build.gradle‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ dependencies {
151151
compile 'com.googlecode.cqengine:cqengine:2.12.4'
152152
compile group: 'com.google.api.grpc', name: 'googleapis-common-protos', version: '0.0.3'
153153

154+
// http
155+
compile 'org.eclipse.jetty:jetty-server:9.4.11.v20180605'
156+
compile 'org.eclipse.jetty:jetty-servlet:9.4.11.v20180605'
157+
compile 'com.alibaba:fastjson:1.2.47'
158+
// end http
154159
}
155160

156161
check.dependsOn 'lint'

‎src/main/java/org/tron/common/net/udp/handler/PacketDecoder.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class PacketDecoder extends MessageToMessageDecoder<DatagramPacket> {
3535
public void decode(ChannelHandlerContext ctx, DatagramPacket packet, List<Object> out) throws Exception {
3636
ByteBuf buf = packet.content();
3737
int length = buf.readableBytes();
38-
if (length > maxSize) {
38+
if (length <= 1 || length >= maxSize) {
3939
logger.error("UDP rcv bad packet, from {} length = {}", ctx.channel().remoteAddress(), length);
4040
return;
4141
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,6 @@ public void run() {
138138
for (Node node : bootNodes) {
139139
getNodeHandler(node);
140140
}
141-
142-
143141
}
144142
}
145143

@@ -227,14 +225,13 @@ public void handleEvent(UdpEvent udpEvent) {
227225
Message m = udpEvent.getMessage();
228226
InetSocketAddress sender = udpEvent.getAddress();
229227

230-
Node n = new Node(m.getFrom().getId(), sender.getHostString(), sender.getPort());
228+
Node n = new Node(m.getNodeId(), sender.getHostString(), sender.getPort());
231229

232230
if (inboundOnlyFromKnownNodes && !hasNodeHandler(n)) {
233231
logger.debug("Inbound packet from unknown peer {}.", sender.getAddress());
234232
return;
235233
}
236234
NodeHandler nodeHandler = getNodeHandler(n);
237-
nodeHandler.setSourceNode(m.getFrom());
238235

239236
switch (m.getType()) {
240237
case DISCOVER_PING:

‎src/main/java/org/tron/common/overlay/discover/node/NodeStatistics.java‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ public NodeStatistics(Node node) {
8585
discoverMessageLatency = new SimpleStatter(node.getIdString());
8686
}
8787

88-
private int getSessionReputation() {
89-
return getSessionFairReputation() + (isPredefined ? REPUTATION_PREDEFINED : 0);
90-
}
91-
9288
private int getSessionFairReputation() {
9389
int discoverReput = 0;
9490

@@ -129,7 +125,14 @@ private int getSessionFairReputation() {
129125
}
130126

131127
public int getReputation() {
132-
return isReputationPenalized() ? 0 : persistedReputation / 2 + getSessionReputation();
128+
int score = 0;
129+
if (!isReputationPenalized()){
130+
score += persistedReputation / 2 + getSessionFairReputation();
131+
}
132+
if (isPredefined){
133+
score += REPUTATION_PREDEFINED;
134+
}
135+
return score;
133136
}
134137

135138
public ReasonCode getDisconnectReason() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public synchronized Node addNode(Node n) {
6161
NodeEntry e = new NodeEntry(node.getId(), n);
6262
if (nodes.contains(e)) {
6363
nodes.forEach(nodeEntry -> {
64-
if (nodeEntry.equals(e)){
64+
if (nodeEntry.equals(e)) {
6565
nodeEntry.touch();
6666
}
6767
});

‎src/main/java/org/tron/common/overlay/message/P2pMessageFactory.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@
2020

2121
import org.apache.commons.lang3.ArrayUtils;
2222
import org.tron.core.exception.P2pException;
23+
import org.tron.core.exception.P2pException.TypeEnum;
2324
import org.tron.core.net.message.MessageTypes;
2425

2526
public class P2pMessageFactory extends MessageFactory {
2627

2728
@Override
2829
public P2pMessage create(byte[] data) throws Exception{
30+
if (data.length <= 1){
31+
throw new P2pException(TypeEnum.MESSAGE_WITH_WRONG_LENGTH, "len=" + data.length);
32+
}
2933
try {
3034
byte type = data[0];
3135
byte[] rawData = ArrayUtils.subarray(data, 1, data.length);

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

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,25 @@
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-
*/
181
package org.tron.common.overlay.server;
192

203
import static org.tron.protos.Protocol.ReasonCode.DUPLICATE_PEER;
214
import static org.tron.protos.Protocol.ReasonCode.TOO_MANY_PEERS;
5+
import static org.tron.protos.Protocol.ReasonCode.TOO_MANY_PEERS_WITH_SAME_IP;
226
import static org.tron.protos.Protocol.ReasonCode.UNKNOWN;
237

248
import com.google.common.cache.Cache;
259
import com.google.common.cache.CacheBuilder;
26-
import io.netty.util.internal.ConcurrentSet;
2710
import java.net.InetAddress;
2811
import java.net.InetSocketAddress;
2912
import java.util.Collection;
30-
import java.util.List;
3113
import java.util.Map;
32-
import java.util.Set;
3314
import java.util.concurrent.ConcurrentHashMap;
3415
import java.util.concurrent.TimeUnit;
16+
import lombok.Getter;
3517
import org.slf4j.Logger;
3618
import org.slf4j.LoggerFactory;
3719
import org.springframework.beans.factory.annotation.Autowired;
3820
import org.springframework.stereotype.Component;
3921
import org.tron.common.overlay.client.PeerClient;
4022
import org.tron.common.overlay.discover.node.Node;
41-
import org.tron.common.overlay.discover.node.NodeManager;
4223
import org.tron.core.config.args.Args;
4324
import org.tron.core.db.ByteArrayWrapper;
4425
import org.tron.protos.Protocol.ReasonCode;
@@ -59,11 +40,14 @@ public class ChannelManager {
5940
private Cache<InetAddress, ReasonCode> recentlyDisconnected = CacheBuilder.newBuilder().maximumSize(1000)
6041
.expireAfterWrite(30, TimeUnit.SECONDS).recordStats().build();
6142

43+
@Getter
6244
private Map<InetAddress, Node> trustPeers = new ConcurrentHashMap();
6345

6446
private Args args = Args.getInstance();
6547

66-
private int maxActivePeers = args.getNodeMaxActiveNodes() > 0 ? args.getNodeMaxActiveNodes() : 30;
48+
private int maxActivePeers = args.getNodeMaxActiveNodes();
49+
50+
private int getMaxActivePeersWithSameIp = args.getNodeMaxActiveNodesWithSameIp();
6751

6852
private PeerServer peerServer;
6953

@@ -77,12 +61,12 @@ private ChannelManager(final PeerServer peerServer, final PeerClient peerClient)
7761
this.peerServer = peerServer;
7862
this.peerClient = peerClient;
7963

80-
if (args.getNodeListenPort() > 0) {
64+
if (this.args.getNodeListenPort() > 0) {
8165
new Thread(() -> peerServer.start(Args.getInstance().getNodeListenPort()),
8266
"PeerServerThread").start();
8367
}
8468

85-
for (Node node : args.getTrustNodes()){
69+
for (Node node : args.getPassiveNodes()){
8670
trustPeers.put(new InetSocketAddress(node.getHost(), node.getPort()).getAddress() , node);
8771
}
8872
logger.info("Trust peer size {}", trustPeers.size());
@@ -138,6 +122,11 @@ public synchronized boolean processPeer(Channel peer) {
138122
peer.disconnect(TOO_MANY_PEERS);
139123
return false;
140124
}
125+
126+
if (getConnectionNum(peer.getInetAddress()) >= getMaxActivePeersWithSameIp){
127+
peer.disconnect(TOO_MANY_PEERS_WITH_SAME_IP);
128+
return false;
129+
}
141130
}
142131

143132
if (activePeers.containsKey(peer.getNodeIdWrapper())) {
@@ -155,6 +144,16 @@ public synchronized boolean processPeer(Channel peer) {
155144
return true;
156145
}
157146

147+
public int getConnectionNum(InetAddress inetAddress){
148+
int cnt = 0;
149+
for (Channel channel: activePeers.values()){
150+
if (channel.getInetAddress().equals(inetAddress)){
151+
cnt++;
152+
}
153+
}
154+
return cnt;
155+
}
156+
158157
public Collection<Channel> getActivePeers() {
159158
return activePeers.values();
160159
}
@@ -167,10 +166,6 @@ public Cache<InetAddress, ReasonCode> getBadPeers(){
167166
return this.badPeers;
168167
}
169168

170-
public Map<InetAddress, Node> getTrustPeers(){
171-
return trustPeers;
172-
}
173-
174169
public void close() {
175170
peerServer.close();
176171
peerClient.close();

‎src/main/java/org/tron/common/overlay/server/HandshakeHandler.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.netty.buffer.ByteBuf;
2121
import io.netty.channel.ChannelHandlerContext;
2222
import io.netty.handler.codec.ByteToMessageDecoder;
23+
import java.net.InetAddress;
2324
import java.net.InetSocketAddress;
2425
import java.util.Arrays;
2526
import java.util.List;
@@ -125,7 +126,8 @@ private void sendHelloMsg(ChannelHandlerContext ctx, long time){
125126
private void handleHelloMsg(ChannelHandlerContext ctx, HelloMessage msg) {
126127
if (remoteId.length != 64) {
127128
channel.initNode(msg.getFrom().getId(), msg.getFrom().getPort());
128-
if (!syncPool.isCanConnect()) {
129+
InetAddress address = ((InetSocketAddress)ctx.channel().remoteAddress()).getAddress();
130+
if (!channelManager.getTrustPeers().keySet().contains(address) && !syncPool.isCanConnect()) {
129131
channel.disconnect(ReasonCode.TOO_MANY_PEERS);
130132
return;
131133
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ public class SyncPool {
7474

7575
private Args args = Args.getInstance();
7676

77-
private int maxActiveNodes = args.getNodeMaxActiveNodes() > 0 ? args.getNodeMaxActiveNodes() : 30;
77+
private int maxActiveNodes = args.getNodeMaxActiveNodes();
78+
79+
private int getMaxActivePeersWithSameIp = args.getNodeMaxActiveNodesWithSameIp();
7880

7981
private ScheduledExecutorService poolLoopExecutor = Executors.newSingleThreadScheduledExecutor();
8082

@@ -244,6 +246,9 @@ public boolean test(NodeHandler handler) {
244246
if (channelManager.getBadPeers().getIfPresent(inetAddress) != null) {
245247
return false;
246248
}
249+
if (channelManager.getConnectionNum(inetAddress) >= getMaxActivePeersWithSameIp){
250+
return false;
251+
}
247252

248253
if (nodeHandlerCache.getIfPresent(handler) != null) {
249254
return false;

‎src/main/java/org/tron/common/utils/Sha256Hash.java‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ public class Sha256Hash implements Serializable, Comparable<Sha256Hash> {
4545

4646
private final byte[] bytes;
4747

48-
private long blockNum;
49-
50-
5148
private byte[] generateBlockId(long blockNum, Sha256Hash blockHash) {
5249
byte[] numBytes = Longs.toByteArray(blockNum);
5350
byte[] hash = blockHash.getBytes();
@@ -62,22 +59,16 @@ private byte[] generateBlockId(long blockNum, byte[] blockHash) {
6259
return hash;
6360
}
6461

65-
public long getBlockNum(){
66-
return blockNum;
67-
}
68-
6962
public Sha256Hash(long num, byte[] hash) {
7063
byte[] rawHashBytes = this.generateBlockId(num, hash);
7164
checkArgument(rawHashBytes.length == LENGTH);
7265
this.bytes = rawHashBytes;
73-
this.blockNum = num;
7466
}
7567

7668
public Sha256Hash(long num, Sha256Hash hash) {
7769
byte[] rawHashBytes = this.generateBlockId(num, hash);
7870
checkArgument(rawHashBytes.length == LENGTH);
7971
this.bytes = rawHashBytes;
80-
this.blockNum = num;
8172
}
8273

8374
/**

0 commit comments

Comments
 (0)