public void SyncChain(NodeChainHeight longestChainNode) { ApplicationLog.Info("Downloading chain."); ApplicationState.ConsensusState = ConsensusStates.SyncingChain; ViewerServices viewerServices = new ViewerServices(); int localHeight = viewerServices.GetChainHeight(); if (localHeight < 0) { localHeight = 0; } //TODO: Future enhancement to download from different nodes in parellel int currentHeight = localHeight; int lastHeight = longestChainNode.ChainHeight; while (currentHeight <= lastHeight) { var blocks = GetBlocks(longestChainNode.Address, currentHeight, currentHeight + 10); foreach (var block in blocks) { AddBlockToChain(block); currentHeight++; } } }
public bool IsChainUpToDate(out NodeChainHeight longestChainNode) { longestChainNode = null; bool isUpToDate = false; int margin = 1; Node longestNode = null; int longestChain = GetLongestChain(out longestNode); if (longestChain == -1) { throw new ValidationException(ResponseCodes.NoChainExistOnNetwork, "Failed to sync chain: " + ResponseMessages.NoChainExistOnNetwork); } //Compare with self height ViewerServices viewerServices = new ViewerServices(); int localHeight = viewerServices.GetChainHeight(); if (longestChain > (localHeight + margin)) { isUpToDate = false; } else { isUpToDate = true; } longestChainNode = new NodeChainHeight(); longestChainNode.Address = longestNode.ServerAddress; longestChainNode.ChainHeight = longestChain; return(isUpToDate); }