public void AddBlockInfoCache(int remoteChainId, BlockInfoCache blockInfoCache)
 {
     if (blockInfoCache == null)
     {
         return;
     }
     _blockInfoCaches.TryAdd(remoteChainId, blockInfoCache);
 }
Exemple #2
0
        public void TryTake_WithoutCache()
        {
            var initTarget     = 1;
            var blockInfoCache = new BlockInfoCache(initTarget);
            var res            = blockInfoCache.TryTake(initTarget, out var blockInfo, false);

            Assert.False(res);
        }
        public void TryGet()
        {
            int chainId = 123;

            var blockInfoCache = new BlockInfoCache(1);

            _multiChainBlockInfoCacheProvider.AddBlockInfoCache(chainId, blockInfoCache);

            var actualBlockInfoCache = _multiChainBlockInfoCacheProvider.GetBlockInfoCache(chainId);

            Assert.Equal(blockInfoCache, actualBlockInfoCache);
        }
Exemple #4
0
        public void TryAdd_SingleThread_Success()
        {
            var height         = 1;
            var blockInfoCache = new BlockInfoCache(1);
            var res            = blockInfoCache.TryAdd(new SideChainBlockData
            {
                SideChainHeight = height
            });

            Assert.True(res);
            Assert.True(blockInfoCache.TargetChainHeight() == height + 1);
        }
        public void TryAdd_Twice_With_SameChainId_SameValue()
        {
            int chainId = 123;

            var blockInfoCache = new BlockInfoCache(1);

            _multiChainBlockInfoCacheProvider.AddBlockInfoCache(chainId, blockInfoCache);
            _multiChainBlockInfoCacheProvider.AddBlockInfoCache(chainId, blockInfoCache);
            Assert.True(_multiChainBlockInfoCacheProvider.Size == 1);
            var actualBlockInfoCache = _multiChainBlockInfoCacheProvider.GetBlockInfoCache(chainId);

            Assert.Equal(blockInfoCache, actualBlockInfoCache);
        }
Exemple #6
0
        public void TryAdd_SingleThread_Fail()
        {
            var height         = 2;
            var initTarget     = 1;
            var blockInfoCache = new BlockInfoCache(initTarget);
            var res            = blockInfoCache.TryAdd(new SideChainBlockData
            {
                SideChainHeight = height
            });

            Assert.False(res);
            Assert.True(blockInfoCache.TargetChainHeight() == initTarget);
        }
Exemple #7
0
        public void TryTake_WithoutSizeLimit()
        {
            var initTarget     = 1;
            var blockInfoCache = new BlockInfoCache(initTarget);

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

            Assert.True(res);
            Assert.True(blockInfo.Height == initTarget);
        }
Exemple #8
0
        public void TryAdd_DataContinuous()
        {
            var initTarget     = 1;
            var blockInfoCache = new BlockInfoCache(initTarget);
            var i = 0;

            while (i < 5)
            {
                blockInfoCache.TryAdd(new SideChainBlockData
                {
                    SideChainHeight = i++
                });
            }
            Assert.True(blockInfoCache.TargetChainHeight() == 5);
        }
Exemple #9
0
        public void TryTake_WithoutEnoughCache()
        {
            var initTarget     = 1;
            var blockInfoCache = new BlockInfoCache(initTarget);
            int i = 0;

            while (i++ < CrossChainConstants.MinimalBlockInfoCacheThreshold)
            {
                var t = blockInfoCache.TryAdd(new SideChainBlockData
                {
                    SideChainHeight = i
                });
            }
            var res = blockInfoCache.TryTake(initTarget, out var blockInfo, true);

            Assert.False(res);
        }
Exemple #10
0
        public void TargetHeight_WithEmptyQueue()
        {
            var initTarget     = 1;
            var blockInfoCache = new BlockInfoCache(initTarget);

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

            Assert.True(blockInfoCache.TargetChainHeight() == 3);
        }
Exemple #11
0
        public void TryTake_WithClearCacheNeeded()
        {
            var initTarget     = 2;
            var blockInfoCache = new BlockInfoCache(initTarget);
            int i = 0;

            while (i++ < (int)initTarget + CrossChainConstants.MinimalBlockInfoCacheThreshold)
            {
                var t = blockInfoCache.TryAdd(new SideChainBlockData
                {
                    SideChainHeight = i
                });
            }
            var res = blockInfoCache.TryTake(initTarget, out var blockInfo, true);

            Assert.True(res);
            Assert.True(blockInfo.Height == initTarget);
        }
Exemple #12
0
        public void TryTake_OutDatedData()
        {
            var initTarget     = 1;
            var blockInfoCache = new BlockInfoCache(initTarget);
            int i = 0;

            while (i++ < (int)initTarget + CrossChainConstants.MinimalBlockInfoCacheThreshold)
            {
                var t = blockInfoCache.TryAdd(new SideChainBlockData
                {
                    SideChainHeight = 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);
        }
Exemple #13
0
        public void TryTake_Twice()
        {
            var initTarget     = 2;
            var blockInfoCache = new BlockInfoCache(initTarget);
            int i = 0;

            while (i++ < (int)initTarget + CrossChainConstants.MinimalBlockInfoCacheThreshold)
            {
                var t = blockInfoCache.TryAdd(new SideChainBlockData
                {
                    SideChainHeight = i
                });
            }
            blockInfoCache.TryTake(initTarget, out var b1, true);
            var res = blockInfoCache.TryTake(initTarget, out var b2, true);

            Assert.True(res);
            Assert.Equal(b1, b2);
        }
Exemple #14
0
        public void TryAdd_MultiThreads_WithSameData()
        {
            var initTarget     = 1;
            var blockInfoCache = new BlockInfoCache(initTarget);
            var i        = 0;
            var taskList = new List <Task>();

            while (i++ < 5)
            {
                var t = Task.Run(() => blockInfoCache.TryAdd(new SideChainBlockData
                {
                    SideChainHeight = initTarget
                }));
                taskList.Add(t);
            }

            Task.WaitAll(taskList.ToArray());
            Assert.True(blockInfoCache.TargetChainHeight() == initTarget + 1);
        }
        public void TryTake_WrongIndex()
        {
            int chainId        = 123;
            var blockInfoCache = new BlockInfoCache(1);

            blockInfoCache.TryAdd(new SideChainBlockData
            {
                SideChainId     = chainId,
                SideChainHeight = 1
            });
            var dict = new Dictionary <int, BlockInfoCache>
            {
                { chainId, blockInfoCache }
            };

            CreateFakeCache(dict);
            var blockInfo = _crossChainDataConsumer.TryTake <SideChainBlockData>(chainId, 2, false);

            Assert.Null(blockInfo);
        }
Exemple #16
0
        public void TryAdd_DataNotContinuous()
        {
            var initTarget     = 1;
            var blockInfoCache = new BlockInfoCache(initTarget);

            blockInfoCache.TryAdd(new SideChainBlockData
            {
                SideChainHeight = 1
            });
            blockInfoCache.TryAdd(new SideChainBlockData
            {
                SideChainHeight = 2
            });

            // 3 is absent.
            blockInfoCache.TryAdd(new SideChainBlockData
            {
                SideChainHeight = 4
            });
            Assert.True(blockInfoCache.TargetChainHeight() == 3);
        }