Exemple #1
0
        public void MempoolConcurrencyTest()
        {
            NodeSettings settings  = NodeSettings.Default(KnownNetworks.TestNet);
            var          pool      = new TxMempool(DateTimeProvider.Default, new BlockPolicyEstimator(new MempoolSettings(settings), settings.LoggerFactory, settings), settings.LoggerFactory, settings);
            var          scheduler = new SchedulerLock();
            var          rand      = new Random();

            int value = 10000;
            var txs   = new List <Transaction>();

            for (int i = 0; i < 20; i++)
            {
                var tx = new Transaction();
                tx.AddInput(new TxIn(new Script(OpcodeType.OP_11)));
                tx.AddOutput(new TxOut(new Money(value++), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));
                txs.Add(tx);
            }

            var tasks   = new List <Task>();
            var options = new ParallelOptions {
                MaxDegreeOfParallelism = 10
            };

            Parallel.ForEach(txs, options, transaction =>
            {
                var entry = new TxMempoolEntry(transaction, new Money(rand.Next(100)), 0, 0.0, 1, transaction.TotalOut, false, 4, new LockPoints(), new ConsensusOptions(), new ConsensusFactory());
                tasks.Add(scheduler.WriteAsync(() => pool.AddUnchecked(transaction.GetHash(), entry)));
            });

            Task.WaitAll(tasks.ToArray());
            Assert.Equal(20, scheduler.ReadAsync(() => pool.Size).Result);
        }
Exemple #2
0
 /// <summary>
 /// Constructs a memory pool coin view.
 /// </summary>
 /// <param name="inner">The backing coin view.</param>
 /// <param name="memPool">Transaction memory pool for managing transactions in the memory pool.</param>
 /// <param name="mempoolLock">A lock for managing asynchronous access to memory pool.</param>
 /// <param name="mempoolValidator">Memory pool validator for validating transactions.</param>
 public MempoolCoinView(CoinView inner, ITxMempool memPool, SchedulerLock mempoolLock, IMempoolValidator mempoolValidator)
 {
     this.Inner            = inner;
     this.memPool          = memPool;
     this.mempoolLock      = mempoolLock;
     this.mempoolValidator = mempoolValidator;
     this.Set = new UnspentOutputSet();
 }
        public void SchedulerPairSessionTest()
        {
            var session   = new SchedulerLock();
            var collector = new List <int>();

            var task = Task.Run(async() =>
            {
                await await session.WriteAsync(async() =>
                {
                    collector.Add(1);
                    // push another exclusive task to the scheduler
                    Task exclusiveTask = session.WriteAsync(() => collector.Add(2));
                    // await a concurrent task, this will split the current method in two tasks
                    // the pushed exclusive task will processes before the await yields back control
                    await session.ReadAsync(() => collector.Add(3));
                    collector.Add(4);
                    await exclusiveTask;
                });
            });

            task.Wait();

            Assert.True(IsSequential(collector.ToArray()));
        }