Beispiel #1
0
 private BlockVm GenerateBlock(object @object)
 {
     try
     {
         BlockVm       block = (BlockVm)@object;
         Task <byte[]> hashGenerationProcess = new Task <byte[]>((obj) => BlockHashing.ComputeBlockHashPoW((BlockVm)obj), block);
         hashGenerationProcess.Start();
         while (!hashGenerationProcess.IsCompleted)
         {
             blockGenerationCancellToken.ThrowIfCancellationRequested();
             Task.Delay(250).Wait();
         }
         block.Header.Hash      = hashGenerationProcess.Result;
         block.Header.Sign      = Encryptor.GetSign(BlockHashing.GetBlockBytes(block), NodeData.Instance.NodeKeys.SignPrivateKey, NodeData.Instance.NodeKeys.Password);
         block.Header.SignKeyId = NodeData.Instance.NodeKeys.KeyId;
         using (BlockchainDbContext context = new BlockchainDbContext())
         {
             var resultBlock = context.Add(BlockBuilder.GetBlock(block));
             context.SaveChanges();
             return(BlockBuilder.GetBlockVm(resultBlock.Entity));
         }
     }
     catch (Exception ex)
     {
         throw new BlockGenerationException("An error occurred while generating the block.", ex);
     }
 }
        private async Task DownloadBlockchainAsync(long startId)
        {
            var webInfo = await LicensorClient.Instance.GetBlockchainInfoAsync().ConfigureAwait(false);

            BlockchainDataRestorer dataRestorer = new BlockchainDataRestorer();

            try
            {
                BlockchainInfo ownInfo = await BlockchainReadService.GetBlockchainInformationAsync().ConfigureAwait(false);

                List <BlockVm> newBlocks = default;
                for (long i = startId; i < webInfo.Count; i += 100)
                {
                    if (webInfo.Count - ownInfo.Count > 100)
                    {
                        newBlocks = await LicensorClient.Instance.GetBlockchainBlocksAsync(i, i + 100).ConfigureAwait(false);
                    }
                    else
                    {
                        newBlocks = await LicensorClient.Instance.GetBlockchainBlocksAsync(ownInfo.Count, webInfo.Count).ConfigureAwait(false);
                    }
                    if (newBlocks != null)
                    {
                        foreach (BlockVm block in newBlocks)
                        {
                            using (BlockchainDbContext context = new BlockchainDbContext())
                            {
                                try
                                {
                                    await context.Blocks.AddAsync(BlockBuilder.GetBlock(block)).ConfigureAwait(false);

                                    await context.SaveChangesAsync().ConfigureAwait(false);

                                    await dataRestorer.SaveBlockDataAsync(block).ConfigureAwait(false);
                                }
                                catch (Exception ex)
                                {
                                    Logger.WriteLog(ex);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
            }
        }