Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
352f4c3
mdy
lvs007 May 9, 2018
f1d3c47
mdy
lvs007 May 9, 2018
0cd8904
Merge branch 'develop' into modify-score
lvs007 May 9, 2018
64b19ec
mdy
lvs007 May 9, 2018
064cdae
mdy
lvs007 May 9, 2018
d0326de
Merge branch 'develop' into modify-score
lvs007 May 9, 2018
fb840b3
modify the reason code
lvs007 May 9, 2018
86e6f76
modify the version
lvs007 May 9, 2018
d9f7e97
Merge branch 'develop' into modify-score
lvs007 May 9, 2018
2c3a06f
add validUrl, url need 1 to 256 bytes
zergweak May 9, 2018
51fea0a
Merge branch 'develop' into develop-evan-witnesscreate
zergweak May 9, 2018
10198bb
modify addressValid validAccountName validUrl
zergweak May 9, 2018
adfe1b7
revert config
sean-liu55 May 10, 2018
ada33da
Merge pull request #679 from tronprotocol/modify-score
wubin12 May 10, 2018
4784f69
add trxs msg
xxo1shine May 10, 2018
7573f61
Merge branch 'develop' of https://github.com/tronprotocol/java-tron i…
xxo1shine May 10, 2018
7d77812
add the disconnect notice
lvs007 May 10, 2018
0f6f48b
Merge branch 'develop' into modify-score
lvs007 May 10, 2018
400972e
Merge pull request #688 from tronprotocol/feature/revert_config
zergweak May 10, 2018
c192b6b
Merge branch 'develop' into modify-score
lvs007 May 10, 2018
be94157
modify the score compute way
lvs007 May 10, 2018
ed1ad2d
add trx msg handle
xxo1shine May 10, 2018
c865d72
modify the disconnect times count way
lvs007 May 10, 2018
13258e9
Merge pull request #689 from tronprotocol/modify-score
wubin12 May 10, 2018
03d3e3c
mdf TransactionsMessage
xxo1shine May 10, 2018
89325f7
Merge pull request #683 from tronprotocol/develop-evan-witnesscreate
renchenchang May 10, 2018
c178db4
fix createTime error
sean-liu55 May 10, 2018
1e21b3f
fix test conflict
sean-liu55 May 10, 2018
617aa84
fix count disconnect times
lvs007 May 10, 2018
a32147c
Merge pull request #692 from tronprotocol/feature/fix_createTimeError
nanfengpo May 10, 2018
7e12187
Merge pull request #693 from tronprotocol/modify-score
wubin12 May 10, 2018
301c42f
Merge branch 'develop' of https://github.com/tronprotocol/java-tron i…
xxo1shine May 10, 2018
227e894
mdf tron msg
xxo1shine May 10, 2018
ead30cf
Merge pull request #691 from tronprotocol/p2p_trx
lvs007 May 10, 2018
0c926e5
modify_transfer_asset_bandwidth
nanfengpo May 10, 2018
1ebb7e8
modify_transfer_asset_bandwidth
nanfengpo May 10, 2018
9301c0a
Merge branch 'develop' into feature/modify_transfer_asset_bandwidth
nanfengpo May 10, 2018
c1d92f2
mdf msg queue
xxo1shine May 10, 2018
ceeebfd
mdf msg queue
xxo1shine May 10, 2018
c3aebe3
Merge pull request #695 from tronprotocol/p2p_mq
lvs007 May 10, 2018
0e841b4
mdf channel exception process
xxo1shine May 10, 2018
f24d35b
Merge pull request #696 from tronprotocol/p2p_ep
lvs007 May 10, 2018
f5e5c61
Merge pull request #694 from tronprotocol/feature/modify_transfer_ass…
sean-liu55 May 10, 2018
47f63fe
add OPERATING_TIME_INTERVAL parameter
sean-liu55 May 10, 2018
27ab5ef
Merge pull request #698 from tronprotocol/feature/add_dynamic_parameter
nanfengpo May 10, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/main/java/org/tron/common/overlay/client/PeerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption;
import io.netty.channel.DefaultMessageSizeEstimator;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.tron.common.overlay.discover.Node;
import org.tron.common.overlay.discover.NodeHandler;
import org.tron.common.overlay.message.ReasonCode;
import org.tron.common.overlay.server.TronChannelInitializer;
import org.tron.core.config.args.Args;
import org.tron.core.net.node.NodeImpl;

import java.io.IOException;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

@Component
public class PeerClient {

Expand Down Expand Up @@ -54,6 +56,20 @@ public void connect(String host, int port, String remoteId) {
}
}

public ChannelFuture connectAsync(NodeHandler nodeHandler, boolean discoveryMode) {
Node node = nodeHandler.getNode();
return connectAsync(node.getHost(), node.getPort(), node.getHexId(), discoveryMode)
.addListener((ChannelFutureListener) future -> {
if (!future.isSuccess()) {
logger.error("connect to {}:{} fail,cause:{}", node.getHost(), node.getPort(),
future.cause().getMessage());
nodeHandler.getNodeStatistics().nodeDisconnectedLocal(ReasonCode.CONNECT_FAIL);
nodeHandler.getNodeStatistics().notifyDisconnect();
future.channel().close();
}
});
}

public ChannelFuture connectAsync(String host, int port, String remoteId, boolean discoveryMode) {

logger.info("connect peer {} {} {}", host, port, remoteId);
Expand Down
41 changes: 32 additions & 9 deletions src/main/java/org/tron/common/overlay/discover/NodeStatistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import static java.lang.Math.min;

import java.util.concurrent.atomic.AtomicLong;

import org.tron.common.overlay.message.ReasonCode;

public class NodeStatistics {

public final static int REPUTATION_PREDEFINED = 100000;
public final static long TOO_MANY_PEERS_PENALIZE_TIMEOUT = 60 * 1000;
private static final long CLEAR_CYCLE_TIME = 60 * 60 * 1000;

public class StatHandler {

Expand Down Expand Up @@ -92,7 +92,7 @@ private int getSessionFairReputation() {
int discoverReput = 0;

discoverReput +=
min(discoverInPong.get(), 1) * (discoverOutPing.get() == discoverInPong.get() ? 50 : 1);
min(discoverInPong.get(), 1) * (discoverOutPing.get() == discoverInPong.get() ? 51 : 1);
discoverReput += min(discoverInNeighbours.get(), 10) * 10;
discoverReput += min(discoverInFind.get(), 50);

Expand All @@ -117,7 +117,10 @@ private int getSessionFairReputation() {
}
}
}
return discoverReput + 10 * reput;
int score =
discoverReput + 10 * reput - (int) Math.pow(2, disconnectTimes) * (disconnectTimes > 0 ? 10
: 0);
return score > 0 ? score : 0;
}

public int getReputation() {
Expand Down Expand Up @@ -146,7 +149,15 @@ public boolean isReputationPenalized() {
return true;
}

return tronLastLocalDisconnectReason == ReasonCode.NULL_IDENTITY ||
if (lastDisconnectedTime > 0
&& (System.currentTimeMillis() - lastDisconnectedTime) > CLEAR_CYCLE_TIME) {
tronLastLocalDisconnectReason = null;
tronLastRemoteDisconnectReason = null;
disconnectTimes = 0;
persistedReputation = 0;
}

if (tronLastLocalDisconnectReason == ReasonCode.NULL_IDENTITY ||
tronLastRemoteDisconnectReason == ReasonCode.NULL_IDENTITY ||
tronLastLocalDisconnectReason == ReasonCode.INCOMPATIBLE_PROTOCOL ||
tronLastRemoteDisconnectReason == ReasonCode.INCOMPATIBLE_PROTOCOL ||
Expand All @@ -163,26 +174,38 @@ public boolean isReputationPenalized() {
tronLastLocalDisconnectReason == ReasonCode.INCOMPATIBLE_VERSION ||
tronLastRemoteDisconnectReason == ReasonCode.INCOMPATIBLE_VERSION ||
tronLastLocalDisconnectReason == ReasonCode.INCOMPATIBLE_CHAIN ||
tronLastRemoteDisconnectReason == ReasonCode.INCOMPATIBLE_CHAIN;
tronLastRemoteDisconnectReason == ReasonCode.INCOMPATIBLE_CHAIN ||
tronLastRemoteDisconnectReason == ReasonCode.SYNC_FAIL ||
tronLastLocalDisconnectReason == ReasonCode.SYNC_FAIL) {
persistedReputation = 0;
return true;
}
return false;
}

public boolean isPenalized() {
return tronLastLocalDisconnectReason == ReasonCode.NULL_IDENTITY ||
tronLastRemoteDisconnectReason == ReasonCode.NULL_IDENTITY ||
tronLastLocalDisconnectReason == ReasonCode.BAD_PROTOCOL ||
tronLastRemoteDisconnectReason == ReasonCode.BAD_PROTOCOL;
tronLastRemoteDisconnectReason == ReasonCode.NULL_IDENTITY ||
tronLastLocalDisconnectReason == ReasonCode.BAD_PROTOCOL ||
tronLastRemoteDisconnectReason == ReasonCode.BAD_PROTOCOL ||
tronLastLocalDisconnectReason == ReasonCode.SYNC_FAIL ||
tronLastRemoteDisconnectReason == ReasonCode.SYNC_FAIL;
}

public void nodeDisconnectedRemote(ReasonCode reason) {
lastDisconnectedTime = System.currentTimeMillis();
tronLastRemoteDisconnectReason = reason;
disconnectTimes++;
}

public void nodeDisconnectedLocal(ReasonCode reason) {
lastDisconnectedTime = System.currentTimeMillis();
tronLastLocalDisconnectReason = reason;
}

public void notifyDisconnect() {
lastDisconnectedTime = System.currentTimeMillis();
disconnectTimes++;
persistedReputation = persistedReputation / 2;
}

public boolean wasDisconnected() {
Expand Down
28 changes: 15 additions & 13 deletions src/main/java/org/tron/common/overlay/message/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
import org.tron.common.utils.Sha256Hash;
import org.tron.core.net.message.MessageTypes;


public abstract class Message {

protected static final Logger logger = LoggerFactory.getLogger("Net");
protected static final Logger logger = LoggerFactory.getLogger("Message");

protected boolean unpacked;
protected byte[] data;
Expand All @@ -32,7 +31,6 @@ public Message(byte type, byte[] packed) {
unpacked = false;
}


public ByteBuf getSendData(){
return Unpooled.wrappedBuffer(ArrayUtils.add(this.getData(), 0 ,type));
}
Expand All @@ -41,13 +39,25 @@ public Sha256Hash getMessageId() {
return Sha256Hash.of(getData());
}

public abstract byte[] getData();
public byte[] getData(){
return this.data;
}

public MessageTypes getType(){
return MessageTypes.fromByte(this.type);
}

public abstract Class<?> getAnswerMessage();

@Override
public String toString() {
return "[Message Type: " + getType() + ", Message Hash: " + getMessageId() + "]";
}

public abstract Class<?> getAnswerMessage();
@Override
public int hashCode() {
return Arrays.hashCode(data);
}

@Override
public boolean equals(Object o) {
Expand All @@ -60,12 +70,4 @@ public boolean equals(Object o) {
Message message = (Message) o;
return Arrays.equals(data, message.data);
}

@Override
public int hashCode() {
return Arrays.hashCode(data);
}

public abstract MessageTypes getType();

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

public abstract class MessageFactory {

public static String ERR_NO_SUCH_MSG = "No such message";
public static String ERR_PARSE_FAILED = "parse message failed";

protected abstract Message create(byte[] data) throws Exception;

}
2 changes: 2 additions & 0 deletions src/main/java/org/tron/common/overlay/message/ReasonCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public enum ReasonCode {

TIME_OUT(0x20),

CONNECT_FAIL(0x21),

/**
* [0xFF] Reason not specified
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/tron/common/overlay/server/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void processException(Throwable throwable){
if (throwable instanceof ReadTimeoutException){
logger.error("Read timeout, {}", address);
}else if(baseThrowable instanceof P2pException){
logger.error("type: {}, info: {}, {}", ((P2pException) throwable).getType(), errMsg, address);
logger.error("type: {}, info: {}, {}", ((P2pException) baseThrowable).getType(), errMsg, address);
}else if (errMsg != null && errMsg.contains("Connection reset by peer")){
logger.error("{}, {}", errMsg, address);
}else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public void notifyDisconnect(Channel channel) {
|| channel.getChannelHandlerContext().channel() == null) {
return;
}
channel.getNodeStatistics().notifyDisconnect();
InetSocketAddress socketAddress = (InetSocketAddress) channel.getChannelHandlerContext()
.channel().remoteAddress();
recentlyDisconnected.put(socketAddress.getAddress(), new Date());
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/org/tron/common/overlay/server/MessageQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class MessageQueue {

private static final Logger logger = LoggerFactory.getLogger("MessageQueue");

private boolean sendMsgFlag = false;
private volatile boolean sendMsgFlag = false;

private Thread sendMsgThread;

Expand Down Expand Up @@ -70,7 +70,7 @@ public void activate(ChannelHandlerContext ctx) {
Message msg = msgQueue.take();
ctx.writeAndFlush(msg.getSendData()).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
}catch (Exception e) {
logger.error("send message failed, {}, error info: {}", ctx.channel().remoteAddress(), e.getMessage());
logger.error("Send message failed, {}, error info: {}", ctx.channel().remoteAddress(), e.getMessage());
}
}
});
Expand Down Expand Up @@ -102,6 +102,15 @@ public void close() {
sendMsgFlag = false;
if(sendTask != null && !sendTask.isCancelled()){
sendTask.cancel(false);
sendTask = null;
}
if (sendMsgThread != null){
try{
sendMsgThread.join(20);
sendMsgThread = null;
}catch (Exception e){
logger.warn("Join send thread failed, peer {}", ctx.channel().remoteAddress());
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/tron/common/overlay/server/SyncPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ private void fillUp() {
nodesInUse.add(nodeManager.getPublicHomeNode().getHexId());

List<NodeHandler> newNodes = nodeManager.getNodes(new NodeSelector(nodesInUse), lackSize);
newNodes.forEach(n -> peerClient.connectAsync(n.getNode().getHost(), n.getNode().getPort(),
n.getNode().getHexId(), false));
newNodes.forEach(n -> peerClient.connectAsync(n, false));
}

// for test only
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/org/tron/common/utils/ByteUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,6 @@ public static int byteArrayToInt(byte[] b) {
return new BigInteger(1, b).intValue();
}

public static boolean isNullOrZeroArray(byte[] array) {
return (array == null) || (array.length == 0);
}

public static boolean isSingleZero(byte[] array) {
return (array.length == 1 && array[0] == 0);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/tron/core/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Objects;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand All @@ -38,7 +39,6 @@
import org.tron.common.overlay.message.Message;
import org.tron.common.utils.Base58;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.ByteUtil;
import org.tron.common.utils.Utils;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.AssetIssueCapsule;
Expand Down Expand Up @@ -112,7 +112,7 @@ public static void setAddressPreFixByte(byte addressPreFixByte) {
}

public static boolean addressValid(byte[] address) {
if (ByteUtil.isNullOrZeroArray(address)) {
if (ArrayUtils.isEmpty(address)) {
logger.warn("Warning: Address is empty !!");
return false;
}
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/org/tron/core/actuator/TransferActuator.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public boolean execute(TransactionResultCapsule ret) throws ContractExeException
long fee = calcFee();
try {

// if account with to_address is not existed, create it.
AccountCapsule toAccount = dbManager.getAccountStore()
.get(transferContract.getToAddress().toByteArray());
if (toAccount == null) {
toAccount = new AccountCapsule(ByteString.copyFrom(toAddress), AccountType.Normal,
dbManager.getHeadBlockTimeStamp());
dbManager.getAccountStore().put(toAddress, toAccount);
}
dbManager.adjustBalance(transferContract.getOwnerAddress().toByteArray(), -calcFee());
ret.setStatus(fee, code.SUCESS);
dbManager.adjustBalance(transferContract.getOwnerAddress().toByteArray(),
Expand Down Expand Up @@ -113,9 +121,6 @@ public boolean validate() throws ContractValidateException {
throw new ContractValidateException(
"For a non-existent account transfer, the minimum amount is 1 TRX");
}
toAccount = new AccountCapsule(ByteString.copyFrom(toAddress), AccountType.Normal,
dbManager.getHeadBlockTimeStamp());
dbManager.getAccountStore().put(toAddress, toAccount);
} else {
//check to account balance if overflow
balance = Math.addExact(toAccount.getBalance(), amount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ public boolean execute(TransactionResultCapsule ret) throws ContractExeException
AccountStore accountStore = this.dbManager.getAccountStore();
byte[] ownerKey = transferAssetContract.getOwnerAddress().toByteArray();
byte[] toKey = transferAssetContract.getToAddress().toByteArray();
ByteString assertName = transferAssetContract.getAssetName();
ByteString assetName = transferAssetContract.getAssetName();
long amount = transferAssetContract.getAmount();

AccountCapsule ownerAccountCapsule = accountStore.get(ownerKey);
if (!ownerAccountCapsule.reduceAssetAmount(assertName, amount)) {
if (!ownerAccountCapsule.reduceAssetAmount(assetName, amount)) {
throw new ContractExeException("reduceAssetAmount failed !");
}
accountStore.put(ownerKey, ownerAccountCapsule);

AccountCapsule toAccountCapsule = accountStore.get(toKey);
toAccountCapsule.addAssetAmount(assertName, amount);
toAccountCapsule.addAssetAmount(assetName, amount);
accountStore.put(toKey, toAccountCapsule);

ret.setStatus(fee, code.SUCESS);
Expand Down
Loading