Esempio n. 1
0
        public Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("MainLoop Started");
            var bootUrl = _configuration["boot"];

            if (!String.IsNullOrWhiteSpace(bootUrl))
            {
                var myNode = _chainService.GetMyChainCredentials();
                //Fill my node from configuration
                myNode.Endpoint = _configuration["NodeIdentification:IPEndpoint"];
                IEnumerable <NetworkNode> remoteNodes = _network.CallWelcomeNode(bootUrl, myNode).Result;
                //Add remote credentials to my list
                foreach (var node in remoteNodes)
                {
                    _network.AddNode(node);
                }
                NetworkNode bootNode = new NetworkNode()
                {
                    AccessKey   = "",
                    Description = "",
                    Endpoint    = bootUrl
                };

                //Get Chain
                IEnumerable <DataBlock> chainBlocks = _network.CallGetNodesList(bootNode).Result;
                //Replace my Chain Nodes
                _chainService.BootstrapNodes(chainBlocks);
                _logger.LogInformation($"Rebuilding local blockchain");
                //Download Chain to local folder
                foreach (var block in chainBlocks)
                {
                    _logger.LogInformation($"Downloading DocsChain Desc {block.Description}");
                    _logger.LogInformation($"Downloading DocsChain Node {block.Index}");
                    //Download Data from the remote server
                    var bytesToStore = _network.CallGetDataBlockBytes(block.Index, bootNode).Result;
                    _logger.LogInformation($"{bytesToStore.Length / 1024} KBytes downloaded");
                    _chainService.StoreBlockBytesToDisk(block.Guid, bytesToStore);
                }
                //Validate The chain
                _logger.LogInformation("Validating downloaded DocsChain...");
                _chainService.CheckFullChainIntegrity();
            }

            var createtestchain = _configuration["create-test-chain"];

            if (!String.IsNullOrWhiteSpace(createtestchain))
            {
                _chainService.AddTestDocuments(3);
            }


            return(Task.CompletedTask);
        }
Esempio n. 2
0
        public async Task <bool> AddChainBlock([FromBody] DataBlock block)
        {
            _logger.LogInformation("Received new Data Block");
            //pick another node for download
            if (block.Index == 0)
            {
                throw new Exception("Block Index 0 is invalid");
            }

            var bytes = await _networkManager.GetDataBlockFromRandomNode(block.Index);

            //Add Block to Chain
            _chainService.StoreReceivedBlock(block, bytes);
            //Verify Chain Integrity
            _chainService.CheckFullChainIntegrity();
            return(true);
        }
Esempio n. 3
0
        public IActionResult BootFromUrl(string bootUrl)
        {
            bootUrl = $"http://{bootUrl}/";
            if (!String.IsNullOrWhiteSpace(bootUrl))
            {
                var myNode = _chainService.GetMyChainCredentials();
                //Fill my node from configuration
                myNode.Endpoint = _configuration["NodeIdentification:IPEndpoint"];
                IEnumerable <NetworkNode> remoteNodes = _networkManager.CallWelcomeNode(bootUrl, myNode).Result;
                //Add remote credentials to my list
                foreach (var node in remoteNodes)
                {
                    _networkManager.AddNode(node);
                }
                NetworkNode bootNode = new NetworkNode()
                {
                    AccessKey   = "",
                    Description = "",
                    Endpoint    = bootUrl
                };

                //Get Chain
                IEnumerable <DataBlock> chainBlocks = _networkManager.CallGetNodesList(bootNode).Result;
                //Replace my Chain Nodes
                _chainService.BootstrapNodes(chainBlocks);
                _logger.LogInformation($"Rebuilding local blockchain");
                //Download Chain to local folder
                foreach (var block in chainBlocks)
                {
                    _logger.LogInformation($"Downloading DocsChain Desc {block.Description}");
                    _logger.LogInformation($"Downloading DocsChain Node {block.Index}");
                    //Download Data from the remote server
                    var bytesToStore = _networkManager.CallGetDataBlockBytes(block.Index, bootNode).Result;
                    _logger.LogInformation($"{bytesToStore.Length / 1024} KBytes downloaded");
                    _chainService.StoreBlockBytesToDisk(block.Guid, bytesToStore);
                }
                //Validate The chain
                _logger.LogInformation("Validating downloaded DocsChain...");
                _chainService.CheckFullChainIntegrity();
            }
            return(RedirectToAction("Index"));
        }