use of io.nuls.protocol.base.download.entity.NetworkNewestBlockInfos 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.protocol.base.download.entity.NetworkNewestBlockInfos in project nuls by nuls-io.
the class DownloadProcessor method checkIsNewest.
private boolean checkIsNewest(NetworkNewestBlockInfos downloadInfos) {
long downloadBestHeight = downloadInfos.getNetBestHeight();
long time = TimeService.currentTimeMillis();
long timeout = 60 * 1000L;
long localBestHeight = 0L;
while (true) {
if (TimeService.currentTimeMillis() - time > timeout) {
break;
}
long bestHeight = blockService.getBestBlock().getData().getHeader().getHeight();
if (bestHeight >= downloadBestHeight) {
break;
} else if (bestHeight != localBestHeight) {
localBestHeight = bestHeight;
time = TimeService.currentTimeMillis();
}
try {
Thread.sleep(100L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
NetworkNewestBlockInfos newestInfos = getNetworkNewestBlockInfos();
if (newestInfos.getNetBestHeight() > blockService.getBestBlock().getData().getHeader().getHeight()) {
downloadStatus = DownloadStatus.WAIT;
return false;
}
return true;
}
use of io.nuls.protocol.base.download.entity.NetworkNewestBlockInfos in project nuls by nuls-io.
the class DownloadProcessor method doSynchronize.
/**
* 区块同步流程
* block synchronization process
*/
private void doSynchronize() {
if (downloadStatus != DownloadStatus.READY) {
return;
}
downloadStatus = DownloadStatus.DOWNLOADING;
// 查找网络大多数节点一致的最高区块hash
// Finding the highest block hash consistent with most nodes in the network
NetworkNewestBlockInfos newestInfos = getNetworkNewestBlockInfos();
if (newestInfos.getNodes().size() < ProtocolConstant.ALIVE_MIN_NODE_COUNT) {
downloadStatus = DownloadStatus.WAIT;
Log.info("too few nodes");
return;
}
NulsContext.getInstance().setNetBestBlockHeight(newestInfos.getNetBestHeight());
DownloadThreadManager downloadThreadManager = new DownloadThreadManager(newestInfos);
FutureTask<Boolean> threadManagerFuture = new FutureTask<>(downloadThreadManager);
TaskManager.createAndRunThread(ProtocolConstant.MODULE_ID_PROTOCOL, "download-thread-manager", threadManagerFuture);
try {
Boolean downResult = threadManagerFuture.get();
boolean success = downResult != null && downResult.booleanValue();
if (success && checkIsNewest(newestInfos)) {
downloadStatus = DownloadStatus.SUCCESS;
} else if (downloadStatus != DownloadStatus.WAIT) {
downloadStatus = DownloadStatus.FAILED;
}
} catch (Exception e) {
Log.error(e);
downloadStatus = DownloadStatus.FAILED;
}
}
Aggregations