public async Task GetBlock_Header_And_Body_Test()
        {
            var block     = _kernelTestHelper.GenerateBlock(0, Hash.Empty);
            var blockHash = block.GetHash();
            await _blockManager.AddBlockHeaderAsync(block.Header);

            var blockHeader = await _blockManager.GetBlockHeaderAsync(blockHash);

            Assert.Equal(blockHeader, block.Header);

            var storedBlock = await _blockManager.GetBlockAsync(blockHash);

            storedBlock.ShouldBeNull();

            await _blockManager.AddBlockBodyAsync(blockHash, block.Body);

            storedBlock = await _blockManager.GetBlockAsync(blockHash);

            Assert.Equal(storedBlock.Header, block.Header);
            Assert.Equal(storedBlock.Body, block.Body);

            (await _blockManager.HasBlockAsync(blockHash)).ShouldBeTrue();

            await _blockManager.RemoveBlockAsync(blockHash);

            await _blockManager.AddBlockBodyAsync(blockHash, block.Body);

            storedBlock = await _blockManager.GetBlockAsync(blockHash);

            storedBlock.ShouldBeNull();

            (await _blockManager.HasBlockAsync(blockHash)).ShouldBeFalse();
        }
Exemplo n.º 2
0
        public async Task GetBlock_Header_And_Body_Test()
        {
            var header = new BlockHeader()
            {
                ChainId = _chainId,
                Height  = 1
            };
            var hash = header.GetHash();
            await _blockManager.AddBlockHeaderAsync(header);

            var h = await _blockManager.GetBlockHeaderAsync(hash);

            Assert.Equal(header, h);

            var body = new BlockBody()
            {
                BlockHeader = hash
            };
            await _blockManager.AddBlockBodyAsync(hash, body);

            var block = await _blockManager.GetBlockAsync(hash);

            Assert.Equal(block.Header, header);
            Assert.Equal(block.Body, body);
        }
Exemplo n.º 3
0
        public async Task <List <Hash> > GetBlockHashesAsync(Chain chain, Hash firstHash, int count,
                                                             Hash chainBranchBlockHash)
        {
            var first = await _blockManager.GetBlockHeaderAsync(firstHash);

            if (first == null)
            {
                return(new List <Hash>());
            }

            var height = first.Height + count;

            var chainBranchBlockHashKey = chainBranchBlockHash.ToStorageKey();

            ChainBlockLink chainBlockLink = null;

            if (chain.Branches.ContainsKey(chainBranchBlockHashKey))
            {
                if (height > chain.Branches[chainBranchBlockHashKey])
                {
                    height = chain.Branches[chainBranchBlockHashKey];
                    count  = (int)(height - first.Height);
                }
            }
            else
            {
                var branchChainBlockLink = await _chainManager.GetChainBlockLinkAsync(chainBranchBlockHash);

                if (height > branchChainBlockLink.Height)
                {
                    height         = branchChainBlockLink.Height;
                    chainBlockLink = branchChainBlockLink;
                    count          = (int)(height - first.Height);
                }
            }

            var hashes = new List <Hash>();

            if (count <= 0)
            {
                return(hashes);
            }


            if (chainBlockLink == null)
            {
                var last = await GetBlockHashByHeightAsync(chain, height, chainBranchBlockHash);

                if (last == null)
                {
                    throw new InvalidOperationException("not support");
                }

                chainBlockLink = await _chainManager.GetChainBlockLinkAsync(last);
            }


            hashes.Add(chainBlockLink.BlockHash);
            for (var i = 0; i < count - 1; i++)
            {
                chainBlockLink = await _chainManager.GetChainBlockLinkAsync(chainBlockLink.PreviousBlockHash);

                hashes.Add(chainBlockLink.BlockHash);
            }

            if (chainBlockLink.PreviousBlockHash != firstHash)
            {
                throw new Exception("wrong branch");
            }

            hashes.Reverse();

            return(hashes);
        }
Exemplo n.º 4
0
 public async Task <bool> HasBlockAsync(Hash blockId)
 {
     return((await _blockManager.GetBlockHeaderAsync(blockId)) != null);
 }