Exemple #1
0
 //4.(修改后重用)恢复区块池(blockReadBlocksFromFile)
 public void restore_blockReadBlocksFromFile(SliceFileItem_Class sliceFileItemObject, Block_Pooling_Manager_Class blockPoolingManagerObject)
 {
     foreach (FileStatusItem_Class fileStatusItemObject in sliceFileItemObject.fileStatusList)
     {
         blockPoolingManagerObject.blockCountOfFile.Add(fileStatusItemObject.totalBlocks);
         if (fileStatusItemObject.unusedBlocks == 0)
         {
             for (int i = 0; i < fileStatusItemObject.totalBlocks; i++)
             {
                 blockPoolingManagerObject.blockReadBlocksFromFile.Add(null);
             }
         }
         else
         {
             List <Block> blocksOfFile = blockfileManagerObject.load_one_blockfile(Configuration_Class.preprocessedBlockFilePath + "\\" + fileStatusItemObject.fileName);
             foreach (Block block in blocksOfFile)
             {
                 if (fileStatusItemObject.unusedBlockHash.Contains(block.BlockHeader.BlockHash.ToString()))
                 {
                     blockPoolingManagerObject.blockReadBlocksFromFile.Add(block);
                 }
                 else
                 {
                     blockPoolingManagerObject.blockReadBlocksFromFile.Add(null);
                 }
             }
         }
     }
 }
Exemple #2
0
 //2.(修改后重用)保存切片状态
 public void save_SliceFile(string sliceFilePath, Block_Pooling_Manager_Class blockPoolingManagerObject, int processedBlockAmount, int sliceFileAmount)
 {
     if (countOfFileBlock(blockPoolingManagerObject.blockCountOfFile) != blockPoolingManagerObject.blockReadBlocksFromFile.Count)
     {
         Console.WriteLine("读取文件总数和区块池文件总数不一致");
     }
     else
     {
         record_OrphanBlock(blockPoolingManagerObject);
         fileStatusList.Clear();
         int readBlockPointer = 0;
         for (int i = 0; i < blockPoolingManagerObject.blockCountOfFile.Count; i++)
         {
             string        fileName        = "blk" + i;
             int           totalBlocks     = blockPoolingManagerObject.blockCountOfFile[i];
             int           unusedBlocks    = 0;
             List <string> unusedBlockHash = new List <string>();
             for (int j = 0; j < blockPoolingManagerObject.blockCountOfFile[i]; j++)
             {
                 if (blockPoolingManagerObject.blockReadBlocksFromFile[readBlockPointer] != null)
                 {
                     unusedBlockHash.Add(blockPoolingManagerObject.blockReadBlocksFromFile[readBlockPointer].BlockHeader.BlockHash.ToString());
                     unusedBlocks++;
                 }
                 readBlockPointer++;
             }
             fileStatusList.Add(new FileStatusItem_Class(fileName, totalBlocks, unusedBlocks, unusedBlockHash));
         }
         SliceFileItem_Class sliceFileItem = new SliceFileItem_Class(fileStatusList, utxoDictionary, blockPoolingManagerObject.forkedBlockList,
                                                                     blockPoolingManagerObject.orphanBlockList, blockPoolingManagerObject.lastProcessedBlockElement.BlockHeader.BlockHash.ToString(),
                                                                     blockPoolingManagerObject.blockReadPointer, blockPoolingManagerObject.currentLoadFileNumber, processedBlockAmount, sliceFileAmount, sameTransactionCount);
         using (StreamWriter sw = File.CreateText(sliceFilePath))
         {
             JsonSerializer serializer = new JsonSerializer();
             serializer.Serialize(sw, sliceFileItem);
         }
     }
 }
Exemple #3
0
        //3.(修改后重用)从最近的时间切片恢复程序状态(正在修改.....)
        public bool restore_StatusForProgram(Block_Pooling_Manager_Class blockPoolingManagerObject)
        {
            string recentlySliceFilePath = "";

            if (exist_TimeingSlice(Configuration_Class.sliceStateFilePath, out recentlySliceFilePath))
            {
                SliceFileItem_Class sliceFileItemObject = null;
                //反序列化切片文件
                Console.WriteLine("开始提取切片中的数据.........");
                Stopwatch timer = new Stopwatch();
                timer.Start();
                try
                {
                    using (StreamReader sr = File.OpenText(recentlySliceFilePath))
                    {
                        JsonSerializer jsonSerializer = new JsonSerializer();
                        sliceFileItemObject = jsonSerializer.Deserialize(sr, typeof(SliceFileItem_Class)) as SliceFileItem_Class;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine("最近的切片状态可能保存不完整或已经损坏。该错误可能是由于在保存切片状态时提前终止程序造成的,或是人为修改了最近的切片状态。");
                }
                timer.Stop();
                Console.WriteLine("提取结束.........");
                Console.WriteLine("反序列化切片用时:" + timer.Elapsed);
                //恢复区块池
                Console.WriteLine("开始恢复区块池.........");
                Stopwatch timer1 = new Stopwatch();
                timer1.Start();
                restore_blockReadBlocksFromFile(sliceFileItemObject, blockPoolingManagerObject);
                timer1.Stop();
                Console.WriteLine("恢复区块池用时:" + timer1.Elapsed);
                //恢复UTXO字典
                utxoDictionary = sliceFileItemObject.utxoDictionary;
                //恢复队列池
                Console.WriteLine("开始恢复区块队列.........");
                Stopwatch timer2 = new Stopwatch();
                timer2.Start();
                Block queueBlock;
                if ((queueBlock = restore_QueueHeaderBlock(sliceFileItemObject.lastProcessedBlockHash, blockPoolingManagerObject)) != null)
                {
                    blockPoolingManagerObject.initialize_BlockQueuePooling(queueBlock);
                }
                else
                {
                    Console.WriteLine("队首区块未找到");
                    return(false);
                }
                timer2.Stop();
                Console.WriteLine("恢复区块队列用时:" + timer2.Elapsed);
                //恢复其它
                blockPoolingManagerObject.forkedBlockList       = sliceFileItemObject.forkedBlockList;
                blockPoolingManagerObject.orphanBlockList       = sliceFileItemObject.orphanBlockList;
                blockPoolingManagerObject.blockReadPointer      = sliceFileItemObject.blockReadPointer;
                blockPoolingManagerObject.currentLoadFileNumber = sliceFileItemObject.currentLoadFileNumber;
                processedBlockAmount = sliceFileItemObject.processedBlockAmount;
                sliceFileAmount      = sliceFileItemObject.sliceFileAmount;
                sameTransactionCount = sliceFileItemObject.sameTransactionCount;
                sliceFileItemObject.Dispose();
            }
            else
            {
                Console.WriteLine("时间切片不存在");
                return(false);
            }
            return(true);
        }