Beispiel #1
0
        public void TestNavigateTowardsInvalidChains()
        {
            // create distinct chains
            var fakeHeadersA = new FakeHeaders();
            var header0A = fakeHeadersA.GenesisChained();
            var header1A = fakeHeadersA.NextChained();

            var fakeHeadersB = new FakeHeaders();
            var header0B = fakeHeadersB.GenesisChained();
            var header1B = fakeHeadersB.NextChained();

            var chainEmpty = new ChainBuilder().ToImmutable();
            var chainA = new ChainBuilder(new[] { header0A, header1A }).ToImmutable();
            var chainB = new ChainBuilder(new[] { header0B, header1B, }).ToImmutable();

            // unrelated chains should error
            AssertMethods.AssertThrows<InvalidOperationException>(() => chainA.NavigateTowards(chainB).ToList());
            AssertMethods.AssertThrows<InvalidOperationException>(() => chainB.NavigateTowards(chainA).ToList());
        }
Beispiel #2
0
        public void TestNavigateTowardsFunc()
        {
            // create chains
            var fakeHeaders = new FakeHeaders();
            var header0 = fakeHeaders.GenesisChained();
            var header1 = fakeHeaders.NextChained();
            var header2 = fakeHeaders.NextChained();
            var header3 = fakeHeaders.NextChained();

            var chain0 = new ChainBuilder(new[] { header0, }).ToImmutable();
            var chain1 = new ChainBuilder(new[] { header0, header1, }).ToImmutable();
            var chain2 = new ChainBuilder(new[] { header0, header1, header2 }).ToImmutable();
            var chain3 = new ChainBuilder(new[] { header0, header1, header2, header3 }).ToImmutable();

            // the list of target chains to use, stays 1 ahead and then catches up with chain 3
            var targetStack = new Stack<Chain>(new[] { chain1, chain2, chain3, chain3 }.Reverse());

            // verify navigating towards an updating chain
            CollectionAssert.AreEqual(
                new[]
                {
                    Tuple.Create(+1, header1),
                    Tuple.Create(+1, header2),
                    Tuple.Create(+1, header3)
                }
                , chain0.NavigateTowards(() => targetStack.Pop()).ToList());

            // verify all targets used
            Assert.AreEqual(0, targetStack.Count);
        }
Beispiel #3
0
        public void TestNavigateTowardsEmpty()
        {
            // create chain
            var fakeHeaders = new FakeHeaders();
            var header0 = fakeHeaders.GenesisChained();
            var header1 = fakeHeaders.NextChained();
            var header2 = fakeHeaders.NextChained();

            var chainEmpty = new ChainBuilder().ToImmutable();
            var chain = new ChainBuilder(new[] { header0, header1, header2 }).ToImmutable();

            // verify chaining to empty does nothing
            Assert.AreEqual(0, chainEmpty.NavigateTowards(chainEmpty).Count());
            Assert.AreEqual(0, chain.NavigateTowards(chainEmpty).Count());

            // verify path from empty chain to chain
            CollectionAssert.AreEqual(
                new[]
                {
                    Tuple.Create(+1, header0),
                    Tuple.Create(+1, header1),
                    Tuple.Create(+1, header2)
                }
                , chainEmpty.NavigateTowards(chain).ToList());
        }
Beispiel #4
0
        public void TestNavigateTowards()
        {
            // create forked chains
            var fakeHeadersA = new FakeHeaders();
            var header0 = fakeHeadersA.GenesisChained();
            var header1 = fakeHeadersA.NextChained();

            var fakeHeadersB = new FakeHeaders(fakeHeadersA);
            var header2A = fakeHeadersA.NextChained();
            var header3A = fakeHeadersA.NextChained();
            var header4A = fakeHeadersA.NextChained();
            var header2B = fakeHeadersB.NextChained();
            var header3B = fakeHeadersB.NextChained();
            var header4B = fakeHeadersB.NextChained();

            var chain0 = new ChainBuilder(new[] { header0, header1, header2A, header3A }).ToImmutable();
            var chain1 = new ChainBuilder(new[] { header0, header1, header2B, header3B, header4B }).ToImmutable();

            // verify path from chain 0 to chain 1
            CollectionAssert.AreEqual(
                new[]
                {
                    Tuple.Create(-1, header3A),
                    Tuple.Create(-1, header2A),
                    Tuple.Create(+1, header2B),
                    Tuple.Create(+1, header3B),
                    Tuple.Create(+1, header4B)
                }
                , chain0.NavigateTowards(chain1).ToList());

            // verify path from chain 1 to chain 0
            CollectionAssert.AreEqual(
                new[]
                {
                    Tuple.Create(-1, header4B),
                    Tuple.Create(-1, header3B),
                    Tuple.Create(-1, header2B),
                    Tuple.Create(+1, header2A),
                    Tuple.Create(+1, header3A)
                }
                , chain1.NavigateTowards(chain0).ToList());
        }