use of io.nuls.network.model.Node in project nuls by nuls-io.
the class DownloadThreadManager method checkRollback.
private boolean checkRollback(Block localBestBlock, int rollbackCount) throws NulsException {
if (rollbackCount >= 20) {
// resetNetwork("number of rollbackTx blocks greater than 10 during download");
return false;
}
List<Node> nodes = newestInfos.getNodes();
long localHeight = localBestBlock.getHeader().getHeight();
NulsDigestData localBestHash = localBestBlock.getHeader().getHash();
for (Node node : nodes) {
Block block = DownloadUtils.getBlockByHash(localBestHash, node);
if (block != null && localHeight == block.getHeader().getHeight()) {
return true;
}
}
if (newestInfos.getNodes().size() > 0) {
consensusService.rollbackBlock(localBestBlock);
} else {
// resetNetwork("the number of available nodes is insufficient for rollbackTx blocks");
return false;
}
localBestBlock = blockService.getBestBlock().getData();
return checkRollback(localBestBlock, rollbackCount + 1);
}
use of io.nuls.network.model.Node in project nuls by nuls-io.
the class DownloadProcessor method getNetworkNewestBlock.
public NetworkNewestBlockInfos getNetworkNewestBlock() {
Collection<Node> nodeList = networkService.getAvailableNodes();
Map<NulsDigestData, Integer> statisticsMaps = new HashMap<>();
Map<NulsDigestData, List<Node>> nodeMaps = new HashMap<>();
// System.out.println("--------------start download-------------------");
for (Node node : nodeList) {
// System.out.println(node.getId() + " : " + node.getBestBlockHeight() + " : " + node.getBestBlockHash());
NulsDigestData hash = node.getBestBlockHash();
Integer statistics = statisticsMaps.get(hash);
if (statistics == null) {
statisticsMaps.put(hash, 0);
}
statisticsMaps.put(hash, statisticsMaps.get(hash) + 1);
List<Node> nodes = nodeMaps.get(hash);
if (nodes == null) {
nodes = new ArrayList<>();
nodeMaps.put(hash, nodes);
}
nodes.add(node);
}
// max number
int max = 0;
long bestHeight = 0;
NulsDigestData bestHash = null;
List<Node> nodes = null;
for (Map.Entry<NulsDigestData, Integer> entry : statisticsMaps.entrySet()) {
int count = entry.getValue();
NulsDigestData hash = entry.getKey();
List<Node> tempNodes = nodeMaps.get(hash);
long height = tempNodes.get(0).getBestBlockHeight();
if (count > max || (count == max && bestHeight < height)) {
max = count;
bestHash = hash;
bestHeight = height;
nodes = tempNodes;
}
}
if (nodes == null || nodes.size() == 0) {
throw new NulsRuntimeException(NetworkErrorCode.NET_NODE_NOT_FOUND);
}
return new NetworkNewestBlockInfos(bestHeight, bestHash, nodes);
}
use of io.nuls.network.model.Node in project nuls by nuls-io.
the class RequestThread method downloadRound.
private void downloadRound() {
for (int i = nodeList.size() - 1; i >= 0; i--) {
Node node = nodeList.get(i);
if (!node.isHandShake()) {
nodeList.remove(i);
continue;
}
int size = count;
if ((startHeight + size) > endHeight) {
size = (int) (endHeight - startHeight + 1);
}
if (size <= 0) {
break;
}
// Log.info("request:{}-{} ,from {}.", startHeight, size, node.getId());
boolean result = request(node, startHeight, size);
if (result) {
startHeight += size;
}
}
}
use of io.nuls.network.model.Node in project nuls by nuls-io.
the class ClientChannelHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
try {
Attribute<Node> nodeAttribute = ctx.channel().attr(NodeAttributeKey.NODE_KEY);
Node node = nodeAttribute.get();
ByteBuf buf = (ByteBuf) msg;
messageProcessor.processor(buf, node);
} catch (Exception e) {
Log.error("----------------exceptionCaught 111 ---------");
throw e;
}
}
use of io.nuls.network.model.Node in project nuls by nuls-io.
the class ClientChannelHandler method channelActive.
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
Attribute<Node> nodeAttribute = ctx.channel().attr(NodeAttributeKey.NODE_KEY);
Node node = nodeAttribute.get();
if (node != null) {
node.setChannel(ctx.channel());
}
if (node != null && node.getConnectedListener() != null) {
node.getConnectedListener().action();
}
}
Aggregations