コード例 #1
0
ファイル: MemoryPool.cs プロジェクト: stephankempkes/cypher
        /// <summary>
        ///
        /// </summary>
        /// <param name="transactionModel"></param>
        /// <returns></returns>
        public VerifyResult Add(byte[] transactionModel)
        {
            Guard.Argument(transactionModel, nameof(transactionModel)).NotNull();
            try
            {
                var transaction = Helper.Util.DeserializeFlatBuffer <TransactionModel>(transactionModel);
                if (transaction.Validate().Any())
                {
                    return(VerifyResult.Invalid);
                }
                if (!_pooledSeenTransactions.Contains(transaction.TxnId.ByteToHex()))
                {
                    _pooledSeenTransactions.Add(transaction.TxnId.ByteToHex());
                    _pooledTransactions.Add(transaction);
                    _localNode.Broadcast(TopicType.AddTransaction, transactionModel);
                }
            }
            catch (Exception ex)
            {
                _logger.Here().Error(ex, ex.Message);
                return(VerifyResult.Invalid);
            }

            return(VerifyResult.Succeed);
        }
コード例 #2
0
            public void BasicInsert(T[] items, T item, int index, int repeat)
            {
                using (var list = new PooledList <T>(items))
                {
                    for (int i = 0; i < repeat; i++)
                    {
                        list.Insert(index, item);
                    }

                    Assert.True(list.Contains(item));                //"Expect it to contain the item."
                    Assert.Equal(list.Count, items.Length + repeat); //"Expect to be the same."

                    for (int i = 0; i < index; i++)
                    {
                        Assert.Equal(list[i], items[i]); //"Expect to be the same."
                    }

                    for (int i = index; i < index + repeat; i++)
                    {
                        Assert.Equal(list[i], item); //"Expect to be the same."
                    }


                    for (int i = index + repeat; i < list.Count; i++)
                    {
                        Assert.Equal(list[i], items[i - repeat]); //"Expect to be the same."
                    }
                }
            }
コード例 #3
0
            public void InsertRangeIEnumerable(T[] itemsX, T[] itemsY, int index, int repeat, Func <T[], IEnumerable <T> > constructIEnumerable)
            {
                PooledList <T> list = new PooledList <T>(constructIEnumerable(itemsX));

                for (int i = 0; i < repeat; i++)
                {
                    list.InsertRange(index, constructIEnumerable(itemsY));
                }

                foreach (T item in itemsY)
                {
                    Assert.True(list.Contains(item));                               //"Should contain the item."
                }
                Assert.Equal(list.Count, itemsX.Length + (itemsY.Length * repeat)); //"Should have the same result."

                for (int i = 0; i < index; i++)
                {
                    Assert.Equal(list[i], itemsX[i]); //"Should have the same result."
                }

                for (int i = index; i < index + (itemsY.Length * repeat); i++)
                {
                    Assert.Equal(list[i], itemsY[(i - index) % itemsY.Length]); //"Should have the same result."
                }

                for (int i = index + (itemsY.Length * repeat); i < list.Count; i++)
                {
                    Assert.Equal(list[i], itemsX[i - (itemsY.Length * repeat)]); //"Should have the same result."
                }

                //InsertRange into itself
                list.Dispose();
                list = new PooledList <T>(constructIEnumerable(itemsX));
                list.InsertRange(index, list);

                foreach (T item in itemsX)
                {
                    Assert.True(list.Contains(item));                    //"Should contain the item."
                }
                Assert.Equal(list.Count, itemsX.Length + itemsX.Length); //"Should have the same result."

                for (int i = 0; i < index; i++)
                {
                    Assert.Equal(list[i], itemsX[i]); //"Should have the same result."
                }

                for (int i = index; i < index + itemsX.Length; i++)
                {
                    Assert.Equal(list[i], itemsX[(i - index) % itemsX.Length]); //"Should have the same result."
                }

                for (int i = index + (itemsX.Length); i < list.Count; i++)
                {
                    Assert.Equal(list[i], itemsX[i - (itemsX.Length)]); //"Should have the same result."
                }

                list.Dispose();
            }
コード例 #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="blockGraph"></param>
        /// <returns></returns>
        public Task <VerifyResult> TryAddBlockGraph(BlockGraph blockGraph)
        {
            Guard.Argument(blockGraph, nameof(blockGraph)).NotNull();
            try
            {
                if (_pooledBlockGraphs.Contains(blockGraph))
                {
                    return(Task.FromResult(VerifyResult.AlreadyExists));
                }

                _pooledBlockGraphs.Add(blockGraph);
                OnBlockGraphAdd(new BlockGraphEventArgs(blockGraph));
            }
            catch (Exception ex)
            {
                _logger.Here().Error(ex, ex.Message);
                return(Task.FromResult(VerifyResult.Invalid));
            }

            return(Task.FromResult(VerifyResult.Succeed));
        }