public void TryTake_WithoutCache()
        {
            var chainId        = 123;
            var initTarget     = 1;
            var blockInfoCache = new ChainCacheEntity(chainId, initTarget);
            var res            = blockInfoCache.TryTake(initTarget, out _, false);

            Assert.False(res);
        }
Esempio n. 2
0
        public void TargetHeight_WithEmptyQueue()
        {
            var chainId        = 123;
            var initTarget     = 1;
            var blockInfoCache = new ChainCacheEntity(chainId, initTarget);

            blockInfoCache.TryAdd(new SideChainBlockData
            {
                Height = 1
            });
            blockInfoCache.TryAdd(new SideChainBlockData
            {
                Height = 2
            });
            blockInfoCache.TryTake(1, out _, false);
            blockInfoCache.TryTake(2, out _, false);

            Assert.Equal(3, blockInfoCache.TargetChainHeight());
        }
Esempio n. 3
0
        public void TryTake_OutDatedData()
        {
            var chainId        = 123;
            var initTarget     = 1;
            var blockInfoCache = new ChainCacheEntity(chainId, initTarget);
            int i = 0;

            while (i++ < initTarget + CrossChainConstants.MinimalBlockCacheEntityCount)
            {
                var t = blockInfoCache.TryAdd(new SideChainBlockData
                {
                    Height = i
                });
            }

            blockInfoCache.TryTake(2, out var b1, true);
            var res = blockInfoCache.TryTake(1, out var b2, true);

            Assert.True(res);
            Assert.True(b2.Height == 1);
        }
        public void ClearByHeight()
        {
            var chainId        = 123;
            var initTarget     = 2;
            var blockInfoCache = new ChainCacheEntity(chainId, initTarget);
            int i = 0;

            while (i++ < initTarget + CrossChainConstants.DefaultBlockCacheEntityCount)
            {
                blockInfoCache.TryAdd(new SideChainBlockData
                {
                    Height  = i,
                    ChainId = chainId,
                    TransactionStatusMerkleTreeRoot = Hash.FromString(i.ToString())
                });
            }

            {
                blockInfoCache.ClearOutOfDateCacheByHeight(initTarget - 1);
                var res = blockInfoCache.TryTake(initTarget, out _, false);
                Assert.True(res);
            }

            {
                blockInfoCache.ClearOutOfDateCacheByHeight(initTarget);
                var res = blockInfoCache.TryTake(initTarget, out _, false);
                Assert.False(res);
            }

            {
                var targetHeight = blockInfoCache.TargetChainHeight();
                blockInfoCache.ClearOutOfDateCacheByHeight(targetHeight);
                Assert.True(targetHeight == blockInfoCache.TargetChainHeight());
                for (int j = 0; j < targetHeight; j++)
                {
                    var res = blockInfoCache.TryTake(j, out _, false);
                    Assert.False(res);
                }
            }
        }
Esempio n. 5
0
        public void TryTake_Twice()
        {
            var chainId        = 123;
            var initTarget     = 2;
            var blockInfoCache = new ChainCacheEntity(chainId, initTarget);
            int i = 0;

            while (i++ < initTarget + CrossChainConstants.MinimalBlockCacheEntityCount)
            {
                var t = blockInfoCache.TryAdd(new SideChainBlockData
                {
                    Height = i
                });
            }

            var res = blockInfoCache.TryTake(initTarget, out var b1, true);

            Assert.True(res);
            res = blockInfoCache.TryTake(initTarget, out var b2, true);
            Assert.True(res);
            Assert.Equal(b1, b2);
        }
        public void TargetHeight_WithEmptyQueue()
        {
            var sideChainId    = 123;
            var initTarget     = 1;
            var blockInfoCache = new ChainCacheEntity(sideChainId, initTarget);

            blockInfoCache.TryAdd(new SideChainBlockData
            {
                Height  = 1,
                ChainId = sideChainId,
                TransactionStatusMerkleTreeRoot = Hash.FromString("1")
            });
            blockInfoCache.TryAdd(new SideChainBlockData
            {
                Height  = 2,
                ChainId = sideChainId,
                TransactionStatusMerkleTreeRoot = Hash.FromString("2")
            });
            blockInfoCache.TryTake(1, out _, false);
            blockInfoCache.TryTake(2, out _, false);

            Assert.Equal(3, blockInfoCache.TargetChainHeight());
        }
        public void TryTake_OutDatedData()
        {
            var chainId        = 123;
            var initTarget     = 1;
            var blockInfoCache = new ChainCacheEntity(chainId, initTarget);
            int i = 0;

            while (i++ < initTarget + CrossChainConstants.DefaultBlockCacheEntityCount)
            {
                blockInfoCache.TryAdd(new SideChainBlockData
                {
                    Height  = i,
                    ChainId = chainId,
                    TransactionStatusMerkleTreeRoot = Hash.FromString(i.ToString())
                });
            }

            blockInfoCache.TryTake(2, out _, true);
            var res = blockInfoCache.TryTake(1, out var b2, true);

            Assert.True(res);
            Assert.True(b2.Height == 1);
        }
        public void TryTake_Twice()
        {
            var chainId        = 123;
            var initTarget     = 2;
            var blockInfoCache = new ChainCacheEntity(chainId, initTarget);
            int i = 0;

            while (i++ < initTarget + CrossChainConstants.DefaultBlockCacheEntityCount)
            {
                blockInfoCache.TryAdd(new SideChainBlockData
                {
                    Height  = i,
                    ChainId = chainId,
                    TransactionStatusMerkleTreeRoot = Hash.FromString(i.ToString())
                });
            }

            var res = blockInfoCache.TryTake(initTarget, out var b1, true);

            Assert.True(res);
            res = blockInfoCache.TryTake(initTarget, out var b2, true);
            Assert.True(res);
            Assert.Equal(b1, b2);
        }
Esempio n. 9
0
        public void TryTake_WithoutSizeLimit()
        {
            var chainId        = 123;
            var initTarget     = 1;
            var blockInfoCache = new ChainCacheEntity(chainId, initTarget);

            blockInfoCache.TryAdd(new SideChainBlockData
            {
                Height = 1
            });
            var res = blockInfoCache.TryTake(initTarget, out var blockInfo, false);

            Assert.True(res);
            Assert.True(blockInfo.Height == initTarget);
        }
        public void TryTake_WithoutSizeLimit()
        {
            var chainId        = 123;
            var initTarget     = 1;
            var blockInfoCache = new ChainCacheEntity(chainId, initTarget);

            blockInfoCache.TryAdd(new SideChainBlockData
            {
                Height  = 1,
                ChainId = chainId,
                TransactionStatusMerkleTreeRoot = Hash.FromString("1")
            });
            var res = blockInfoCache.TryTake(initTarget, out var blockInfo, false);

            Assert.True(res);
            Assert.True(blockInfo.Height == initTarget);
        }
        public void TryTake_WithoutEnoughCache()
        {
            var chainId        = 123;
            var initTarget     = 1;
            var blockInfoCache = new ChainCacheEntity(chainId, initTarget);
            int i = 0;

            while (i++ < CrossChainConstants.DefaultBlockCacheEntityCount)
            {
                blockInfoCache.TryAdd(new SideChainBlockData
                {
                    Height = i
                });
            }

            var res = blockInfoCache.TryTake(initTarget, out _, true);

            Assert.False(res);
        }
Esempio n. 12
0
        public void TryTake_WithClearCacheNeeded()
        {
            var chainId        = 123;
            var initTarget     = 2;
            var blockInfoCache = new ChainCacheEntity(chainId, initTarget);
            int i = 0;

            while (i++ < (int)initTarget + CrossChainConstants.MinimalBlockCacheEntityCount)
            {
                var t = blockInfoCache.TryAdd(new SideChainBlockData
                {
                    Height = i
                });
            }

            var res = blockInfoCache.TryTake(initTarget, out var blockInfo, true);

            Assert.True(res);
            Assert.True(blockInfo.Height == initTarget);
        }
        public void TryTake_WithSizeLimit()
        {
            var chainId        = 123;
            var initTarget     = 1;
            var blockInfoCache = new ChainCacheEntity(chainId, initTarget);
            int i = 0;

            while (i++ <= CrossChainConstants.DefaultBlockCacheEntityCount)
            {
                blockInfoCache.TryAdd(new SideChainBlockData
                {
                    Height  = i,
                    ChainId = chainId,
                    TransactionStatusMerkleTreeRoot = Hash.FromString(i.ToString())
                });
            }

            var res = blockInfoCache.TryTake(initTarget, out var blockInfo, true);

            Assert.True(res);
            Assert.True(blockInfo.Height == initTarget);
        }