Exemplo n.º 1
0
        public async Task CleanChainBranchAsync(Chain chain, DiscardedBranch discardedBranch)
        {
            var longestChainKey = chain.LongestChainHash.ToStorageKey();
            var bestChainKey    = chain.BestChainHash.ToStorageKey();

            foreach (var key in discardedBranch.BranchKeys)
            {
                if (key == bestChainKey)
                {
                    continue;
                }

                if (key == longestChainKey)
                {
                    chain.LongestChainHash   = chain.BestChainHash;
                    chain.LongestChainHeight = chain.BestChainHeight;
                }

                chain.Branches.Remove(key);
            }

            foreach (var key in discardedBranch.NotLinkedKeys)
            {
                chain.NotLinkedBlocks.Remove(key);
            }

            Logger.LogDebug(
                $"Clean chain branch, Branches: [{discardedBranch.BranchKeys.JoinAsString(",")}], NotLinkedBlocks: [{discardedBranch.NotLinkedKeys.JoinAsString(",")}]");

            await _chains.SetAsync(chain.Id.ToStorageKey(), chain);
        }
        public async Task Clean_ChainBranch_Test()
        {
            var chain = await _fullBlockchainService.GetChainAsync();

            var bestChainKey    = chain.BestChainHash.ToStorageKey();
            var longestChainKey = chain.LongestChainHash.ToStorageKey();

            var discardedBranch = new DiscardedBranch
            {
                BranchKeys = new List <string>
                {
                    bestChainKey,
                    longestChainKey,
                    _kernelTestHelper.ForkBranchBlockList.Last().GetHash().ToStorageKey(),
                    "Not Exist Branch"
                },
                NotLinkedKeys = new List <string>
                {
                    _kernelTestHelper.NotLinkedBlockList[0].Header.PreviousBlockHash.ToStorageKey(),
                    _kernelTestHelper.NotLinkedBlockList[1].Header.PreviousBlockHash.ToStorageKey(),
                    "Not Exist Block"
                }
            };
            await _fullBlockchainService.CleanChainBranchAsync(discardedBranch);

            var currentChain = await _fullBlockchainService.GetChainAsync();

            currentChain.LongestChainHash.ShouldBe(currentChain.BestChainHash);

            currentChain.Branches.Count.ShouldBe(chain.Branches.Count - 2);
            currentChain.Branches.ShouldNotContainKey(longestChainKey);
            currentChain.Branches.ShouldNotContainKey(_kernelTestHelper.ForkBranchBlockList.Last().GetHash()
                                                      .ToStorageKey());

            currentChain.NotLinkedBlocks.Count.ShouldBe(3);
            currentChain.NotLinkedBlocks.ShouldNotContainKey(discardedBranch.NotLinkedKeys[0]);
            currentChain.NotLinkedBlocks.ShouldNotContainKey(discardedBranch.NotLinkedKeys[1]);
        }
 public Task CleanChainBranchAsync(DiscardedBranch discardedBranch)
 {
     throw new System.NotImplementedException();
 }
Exemplo n.º 4
0
        public async Task CleanChainBranchAsync(DiscardedBranch discardedBranch)
        {
            var chain = await GetChainAsync();

            await _chainManager.CleanChainBranchAsync(chain, discardedBranch);
        }