Example #1
0
        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++;
                }
            }
        }
Example #2
0
        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);
        }
Example #3
0
        public void DeleteIndexForAllBlocks()
        {
            ViewerServices viewerService = new ViewerServices();
            var            allBlocks     = viewerService.GetAllBlocks();

            foreach (var block in allBlocks)
            {
                DeleteIndex(block);
            }
        }
Example #4
0
        private void UpdateProofOfStake()
        {
            //Get genesis block
            ViewerServices viewerServices = new ViewerServices();
            var            genesisBlock   = viewerServices.GetBlock(0);

            if (genesisBlock != null)
            {
                List <NodeStake> stakes = new List <NodeStake>();

                foreach (var node in ApplicationState.ConnectedNodes)
                {
                    stakes.Add(new NodeStake()
                    {
                        WalletAddress = node.WalletAddress,
                        Balance       = node.Balance
                    });
                }

                ApplicationState.PosPool.UpdateNodes(stakes, genesisBlock.Header.Timestamp, 15);
            }
        }