public static ChainedHeader SetTip(this ChainIndexer chainIndexer, ChainedHeader block)
        {
            ChainedHeader fork = chainIndexer.Tip.FindFork(block);

            chainIndexer.Initialize(block);

            return(fork);
        }
        public void Query_Range_To_Header_Null()
        {
            var chainIndexer = new ChainIndexer(this.network);

            var chain = this.CreateChain(chainIndexer.Genesis, 5);

            chainIndexer.Initialize(chain.Last());

            var query = new ChainIndexerRangeQuery(chainIndexer);

            // Chain length is only 5, so this should return all elements.
            var result = query.EnumerateRange(0, 10);

            Assert.Equal(5, result.Count());
        }
        public void Query_Range_From_Header_Null_Empty()
        {
            var chainIndexer = new ChainIndexer(this.network);

            var chain = this.CreateChain(chainIndexer.Genesis, 1);

            chainIndexer.Initialize(chain.Last());

            var query = new ChainIndexerRangeQuery(chainIndexer);

            // Chain length is only the genesis block, so this should fail.
            var result = query.EnumerateRange(1, 10);

            Assert.Empty(result);
        }
        public void Query_Range_During_Reorg_Success(int chainLength, int start, int?end)
        {
            // Simulate a reorg occuring mid-enumeration and check that the query still returns the full old chain.

            var chainIndexer = new ChainIndexer(this.network);

            ChainedHeader[] chainBeforeReorg = this.CreateChain(chainIndexer.Genesis, chainLength);

            // Create a new reorg that removes 3 blocks and adds another 5.
            ChainedHeader[] chainAfterReorg = this.CreateChain(chainBeforeReorg[chainLength - 3], 5);

            chainIndexer.Initialize(chainBeforeReorg.Last());

            var query = new ChainIndexerRangeQuery(chainIndexer);

            IEnumerator <ChainedHeader> enumerator = query.EnumerateRange(start, end).GetEnumerator();

            int position = start;

            while (position < (end ?? chainBeforeReorg.Length))
            {
                enumerator.MoveNext();

                ChainedHeader item = enumerator.Current;

                // Trigger a reorg at position chainLength - 3
                if (position == chainLength - 3)
                {
                    // Remove two headers.
                    chainIndexer.Remove(chainIndexer.Tip);
                    chainIndexer.Remove(chainIndexer.Tip);

                    // Add the reorged chain's headers.
                    // Note: Most likely is that headers are removed only before the enumeration is completed.
                    chainIndexer.Add(chainAfterReorg[1]);
                    chainIndexer.Add(chainAfterReorg[2]);
                    chainIndexer.Add(chainAfterReorg[3]);
                    chainIndexer.Add(chainAfterReorg[4]);
                }

                Assert.Equal(chainBeforeReorg[position], item);

                position++;
            }

            enumerator.Dispose();
        }
        public void Query_Range_Success(int chainLength, int start, int?end)
        {
            var chainIndexer = new ChainIndexer(this.network);

            var chain = this.CreateChain(chainIndexer.Genesis, chainLength);

            chainIndexer.Initialize(chain.Last());

            var query = new ChainIndexerRangeQuery(chainIndexer);

            var result = query.EnumerateRange(start, end).ToList();

            for (var i = 0; i < result.Count; i++)
            {
                Assert.Equal(chain[start + i], result[i]);
            }

            Assert.Equal((end ?? (chainLength - 1)) - start + 1, result.Count);
        }
Esempio n. 6
0
        public void CanEnumerateAfterChainedBlock()
        {
            var chain = new ChainIndexer(this.network);

            this.AppendBlock(chain);
            ChainedHeader a = this.AppendBlock(chain);
            ChainedHeader b = this.AppendBlock(chain);
            ChainedHeader c = this.AppendBlock(chain);

            Assert.True(chain.EnumerateAfter(a).SequenceEqual(new[] { b, c }));

            ChainedHeader d = this.AppendBlock(chain);

            IEnumerator <ChainedHeader> enumerator = chain.EnumerateAfter(b).GetEnumerator();

            enumerator.MoveNext();
            Assert.True(enumerator.Current == c);

            chain.Initialize(b);
            ChainedHeader cc = this.AppendBlock(chain);
            ChainedHeader dd = this.AppendBlock(chain);

            Assert.False(enumerator.MoveNext());
        }