Beispiel #1
0
        public void MempoolRemoveTest()
        {
            var entry = new TestMemPoolEntryHelper();

            // Parent transaction with three children,
            // and three grand-children:
            var txParent = new Transaction();

            txParent.AddInput(new TxIn());
            txParent.Inputs[0].ScriptSig = new Script(OpcodeType.OP_11);

            for (int i = 0; i < 3; i++)
            {
                txParent.AddOutput(new TxOut(new Money(33000L), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));
            }

            var txChild = new Transaction[3];

            for (int i = 0; i < 3; i++)
            {
                txChild[i] = new Transaction();
                txChild[i].AddInput(new TxIn(new OutPoint(txParent, i), new Script(OpcodeType.OP_11)));
                txChild[i].AddOutput(new TxOut(new Money(11000L), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));
            }

            var txGrandChild = new Transaction[3];

            for (int i = 0; i < 3; i++)
            {
                txGrandChild[i] = new Transaction();
                txGrandChild[i].AddInput(new TxIn(new OutPoint(txChild[i], 0), new Script(OpcodeType.OP_11)));
                txGrandChild[i].AddOutput(new TxOut(new Money(11000L), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));
            }

            NodeSettings settings = NodeSettings.Default(KnownNetworks.TestNet);
            var          testPool = new TxMempool(DateTimeProvider.Default, new BlockPolicyEstimator(new MempoolSettings(settings), settings.LoggerFactory, settings), settings.LoggerFactory, settings);

            // Nothing in pool, remove should do nothing:
            long poolSize = testPool.Size;

            testPool.RemoveRecursive(txParent);
            Assert.Equal(testPool.Size, poolSize);

            // Just the parent:
            testPool.AddUnchecked(txParent.GetHash(), entry.FromTx(txParent));
            poolSize = testPool.Size;
            testPool.RemoveRecursive(txParent);
            Assert.Equal(testPool.Size, poolSize - 1);

            // Parent, children, grandchildren:
            testPool.AddUnchecked(txParent.GetHash(), entry.FromTx(txParent));
            for (int i = 0; i < 3; i++)
            {
                testPool.AddUnchecked(txChild[i].GetHash(), entry.FromTx(txChild[i]));
                testPool.AddUnchecked(txGrandChild[i].GetHash(), entry.FromTx(txGrandChild[i]));
            }
            // Remove Child[0], GrandChild[0] should be removed:
            poolSize = testPool.Size;
            testPool.RemoveRecursive(txChild[0]);
            Assert.Equal(testPool.Size, poolSize - 2);
            // ... make sure grandchild and child are gone:
            poolSize = testPool.Size;
            testPool.RemoveRecursive(txGrandChild[0]);
            Assert.Equal(testPool.Size, poolSize);
            poolSize = testPool.Size;
            testPool.RemoveRecursive(txChild[0]);
            Assert.Equal(testPool.Size, poolSize);
            // Remove parent, all children/grandchildren should go:
            poolSize = testPool.Size;
            testPool.RemoveRecursive(txParent);
            Assert.Equal(testPool.Size, poolSize - 5);
            Assert.Equal(0, testPool.Size);

            // Add children and grandchildren, but NOT the parent (simulate the parent being in a block)
            for (int i = 0; i < 3; i++)
            {
                testPool.AddUnchecked(txChild[i].GetHash(), entry.FromTx(txChild[i]));
                testPool.AddUnchecked(txGrandChild[i].GetHash(), entry.FromTx(txGrandChild[i]));
            }
            // Now remove the parent, as might happen if a block-re-org occurs but the parent cannot be
            // put into the mempool (maybe because it is non-standard):
            poolSize = testPool.Size;
            testPool.RemoveRecursive(txParent);
            Assert.Equal(poolSize - 6, testPool.Size);
            Assert.Equal(0, testPool.Size);
        }
Beispiel #2
0
        public void MempoolIndexingTest()
        {
            NodeSettings settings = NodeSettings.Default(KnownNetworks.TestNet);
            var          pool     = new TxMempool(DateTimeProvider.Default, new BlockPolicyEstimator(new MempoolSettings(settings), settings.LoggerFactory, settings), settings.LoggerFactory, settings);
            var          entry    = new TestMemPoolEntryHelper();

            /* 3rd highest fee */
            var tx1 = new Transaction();

            tx1.AddOutput(new TxOut(new Money(10 * Money.COIN), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));
            pool.AddUnchecked(tx1.GetHash(), entry.Fee(new Money(10000L)).Priority(10.0).FromTx(tx1));

            /* highest fee */
            var tx2 = new Transaction();

            tx2.AddOutput(new TxOut(new Money(2 * Money.COIN), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));
            pool.AddUnchecked(tx2.GetHash(), entry.Fee(new Money(20000L)).Priority(9.0).FromTx(tx2));

            /* lowest fee */
            var tx3 = new Transaction();

            tx3.AddOutput(new TxOut(new Money(5 * Money.COIN), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));
            pool.AddUnchecked(tx3.GetHash(), entry.Fee(new Money(0L)).Priority(100.0).FromTx(tx3));

            /* 2nd highest fee */
            var tx4 = new Transaction();

            tx4.AddOutput(new TxOut(new Money(6 * Money.COIN), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));
            pool.AddUnchecked(tx4.GetHash(), entry.Fee(new Money(15000L)).Priority(1.0).FromTx(tx4));

            /* equal fee rate to tx1, but newer */
            var tx5 = new Transaction();

            tx5.AddOutput(new TxOut(new Money(11 * Money.COIN), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));
            pool.AddUnchecked(tx5.GetHash(), entry.Fee(new Money(10000L)).Priority(10.0).Time(1).FromTx(tx5));

            // assert size
            Assert.Equal(5, pool.Size);

            var sortedOrder = new List <string>(5);

            sortedOrder.Insert(0, tx3.GetHash().ToString()); // 0
            sortedOrder.Insert(1, tx5.GetHash().ToString()); // 10000
            sortedOrder.Insert(2, tx1.GetHash().ToString()); // 10000
            sortedOrder.Insert(3, tx4.GetHash().ToString()); // 15000
            sortedOrder.Insert(4, tx2.GetHash().ToString()); // 20000
            this.CheckSort(pool, pool.MapTx.DescendantScore.ToList(), sortedOrder);

            /* low fee but with high fee child */
            /* tx6 -> tx7 -> tx8, tx9 -> tx10 */
            var tx6 = new Transaction();

            tx6.AddOutput(new TxOut(new Money(20 * Money.COIN), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));
            pool.AddUnchecked(tx6.GetHash(), entry.Fee(new Money(0L)).FromTx(tx6));

            // assert size
            Assert.Equal(6, pool.Size);

            // Check that at this point, tx6 is sorted low
            sortedOrder.Insert(0, tx6.GetHash().ToString());
            this.CheckSort(pool, pool.MapTx.DescendantScore.ToList(), sortedOrder);

            var setAncestors = new TxMempool.SetEntries();

            setAncestors.Add(pool.MapTx.TryGet(tx6.GetHash()));
            var tx7 = new Transaction();

            tx7.AddInput(new TxIn(new OutPoint(tx6.GetHash(), 0), new Script(OpcodeType.OP_11)));
            tx7.AddOutput(new TxOut(new Money(10 * Money.COIN), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));
            tx7.AddOutput(new TxOut(new Money(1 * Money.COIN), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));

            var    setAncestorsCalculated = new TxMempool.SetEntries();
            string dummy;

            Assert.True(pool.CalculateMemPoolAncestors(entry.Fee(2000000L).FromTx(tx7), setAncestorsCalculated, 100, 1000000, 1000, 1000000, out dummy));
            Assert.True(setAncestorsCalculated.Equals(setAncestors));

            pool.AddUnchecked(tx7.GetHash(), entry.FromTx(tx7), setAncestors);
            Assert.Equal(7, pool.Size);

            // Now tx6 should be sorted higher (high fee child): tx7, tx6, tx2, ...
            sortedOrder.RemoveAt(0);
            sortedOrder.Add(tx6.GetHash().ToString());
            sortedOrder.Add(tx7.GetHash().ToString());
            this.CheckSort(pool, pool.MapTx.DescendantScore.ToList(), sortedOrder);

            /* low fee child of tx7 */
            var tx8 = new Transaction();

            tx8.AddInput(new TxIn(new OutPoint(tx7.GetHash(), 0), new Script(OpcodeType.OP_11)));
            tx8.AddOutput(new TxOut(new Money(10 * Money.COIN), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));
            setAncestors.Add(pool.MapTx.TryGet(tx7.GetHash()));
            pool.AddUnchecked(tx8.GetHash(), entry.Fee(0L).Time(2).FromTx(tx8), setAncestors);

            // Now tx8 should be sorted low, but tx6/tx both high
            sortedOrder.Insert(0, tx8.GetHash().ToString());
            this.CheckSort(pool, pool.MapTx.DescendantScore.ToList(), sortedOrder);

            /* low fee child of tx7 */
            var tx9 = new Transaction();

            tx9.AddInput(new TxIn(new OutPoint(tx7.GetHash(), 1), new Script(OpcodeType.OP_11)));
            tx9.AddOutput(new TxOut(new Money(1 * Money.COIN), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));
            pool.AddUnchecked(tx9.GetHash(), entry.Fee(0L).Time(3).FromTx(tx9), setAncestors);

            // tx9 should be sorted low
            Assert.Equal(9, pool.Size);

            sortedOrder.Insert(0, tx9.GetHash().ToString());
            this.CheckSort(pool, pool.MapTx.DescendantScore.ToList(), sortedOrder);

            List <string> snapshotOrder = sortedOrder.ToList();

            setAncestors.Add(pool.MapTx.TryGet(tx8.GetHash()));
            setAncestors.Add(pool.MapTx.TryGet(tx9.GetHash()));
            /* tx10 depends on tx8 and tx9 and has a high fee*/
            var tx10 = new Transaction();

            tx10.AddInput(new TxIn(new OutPoint(tx8.GetHash(), 0), new Script(OpcodeType.OP_11)));
            tx10.AddInput(new TxIn(new OutPoint(tx9.GetHash(), 0), new Script(OpcodeType.OP_11)));
            tx10.AddOutput(new TxOut(new Money(10 * Money.COIN), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));

            setAncestorsCalculated.Clear();
            Assert.True(pool.CalculateMemPoolAncestors(entry.Fee(200000L).Time(4).FromTx(tx10), setAncestorsCalculated, 100, 1000000, 1000, 1000000, out dummy));
            Assert.True(setAncestorsCalculated.Equals(setAncestors));

            pool.AddUnchecked(tx10.GetHash(), entry.FromTx(tx10), setAncestors);

            /**
             *  tx8 and tx9 should both now be sorted higher
             *  Final order after tx10 is added:
             *
             *  tx3 = 0 (1)
             *  tx5 = 10000 (1)
             *  tx1 = 10000 (1)
             *  tx4 = 15000 (1)
             *  tx2 = 20000 (1)
             *  tx9 = 200k (2 txs)
             *  tx8 = 200k (2 txs)
             *  tx10 = 200k (1 tx)
             *  tx6 = 2.2M (5 txs)
             *  tx7 = 2.2M (4 txs)
             */
            sortedOrder.RemoveRange(0, 2); // take out tx9, tx8 from the beginning
            sortedOrder.Insert(5, tx9.GetHash().ToString());
            sortedOrder.Insert(6, tx8.GetHash().ToString());
            sortedOrder.Insert(7, tx10.GetHash().ToString()); // tx10 is just before tx6
            this.CheckSort(pool, pool.MapTx.DescendantScore.ToList(), sortedOrder);

            // there should be 10 transactions in the mempool
            Assert.Equal(10, pool.Size);

            // Now try removing tx10 and verify the sort order returns to normal
            pool.RemoveRecursive(pool.MapTx.TryGet(tx10.GetHash()).Transaction);
            this.CheckSort(pool, pool.MapTx.DescendantScore.ToList(), snapshotOrder);

            pool.RemoveRecursive(pool.MapTx.TryGet(tx9.GetHash()).Transaction);
            pool.RemoveRecursive(pool.MapTx.TryGet(tx8.GetHash()).Transaction);

            /* Now check the sort on the mining score index.
             * Final order should be:
             *
             * tx7 (2M)
             * tx2 (20k)
             * tx4 (15000)
             * tx1/tx5 (10000)
             * tx3/6 (0)
             * (Ties resolved by hash)
             */
            sortedOrder.Clear();
            sortedOrder.Add(tx7.GetHash().ToString());
            sortedOrder.Add(tx2.GetHash().ToString());
            sortedOrder.Add(tx4.GetHash().ToString());
            if (tx1.GetHash() < tx5.GetHash())
            {
                sortedOrder.Add(tx5.GetHash().ToString());
                sortedOrder.Add(tx1.GetHash().ToString());
            }
            else
            {
                sortedOrder.Add(tx1.GetHash().ToString());
                sortedOrder.Add(tx5.GetHash().ToString());
            }
            if (tx3.GetHash() < tx6.GetHash())
            {
                sortedOrder.Add(tx6.GetHash().ToString());
                sortedOrder.Add(tx3.GetHash().ToString());
            }
            else
            {
                sortedOrder.Add(tx3.GetHash().ToString());
                sortedOrder.Add(tx6.GetHash().ToString());
            }
            this.CheckSort(pool, pool.MapTx.MiningScore.ToList(), sortedOrder);
        }