Exemple #1
0
        // HACK: [vermorel] Constructor used as the setup of the unit tests, should be refactored.
        public LineageTests()
        {
            var store = new VolatileChainStore();

            //var chain = new SimpleBlockchain();
            store.TryOpenBlock(CommittedBlockId.GenesisParent, out var b0);
            var id = b0.Alias;

            // U(0)
            store.TryCommitBlock(id, CommittedBlockId.Genesis, out _);
            // C(0)
            store.TryOpenBlock(CommittedBlockId.Genesis, out var b1);
            Assert.Equal(_1, b1.Alias);
            // C(0) -> U(1)
            store.TryCommitBlock(_1, _id_1, out _);
            // C(0) -> C(1)
            store.TryOpenBlock(_id_1, out var b2);
            Assert.Equal(_2_0, b2.Alias);
            // C(0) -> C(1) -> U(2)
            store.TryCommitBlock(_2_0, _id_2_0, out _);
            // C(0) -> C(1) -> C(2)
            // Second child for second block
            store.TryOpenBlock(_id_1, out var b21);
            Assert.Equal(_2_1, b21.Alias);
            // C(0) -> C(1) -> C(2)
            //             \-> U(2-1)
            store.TryCommitBlock(_2_1, _id_2_1, out _);
            // C(0) -> C(1) -> C(2)
            //             \-> C(2-1)

            var lineage = (Lineage)store.GetLineage();

            lineage.CoinPruneHeight = _1.BlockHeight;

            _lineage = lineage;
        }
Exemple #2
0
        public (IChainStore, BoundedInbox, BoundedInbox, ChainController, ClientId) Setup(int pruneLimit = 100)
        {
            var store = new VolatileChainStore();

            store.BlockPruneLimitDistance = pruneLimit;

            Assert.Equal(OpenBlockStatus.Success,
                         store.TryOpenBlock(CommittedBlockId.GenesisParent, out var openedBlock));
            var id = openedBlock.Alias;

            Assert.NotEqual(BlockAlias.GenesisParent, id);

            Assert.Equal(CommitBlockStatus.Success, store.TryCommitBlock(id, CommittedBlockId.Genesis, out _));
            // C(0)

            Assert.Equal(OpenBlockStatus.Success, store.TryOpenBlock(CommittedBlockId.Genesis, out var cb1));
            Assert.Equal(_1, cb1.Alias);
            Assert.Equal(CommitBlockStatus.Success, store.TryCommitBlock(cb1.Alias, _id_1, out _));
            // C(0) -> C(1)

            Assert.Equal(OpenBlockStatus.Success, store.TryOpenBlock(_id_1, out var cb2));
            Assert.Equal(_2, cb2.Alias);
            Assert.Equal(CommitBlockStatus.Success, store.TryCommitBlock(cb2.Alias, _id_2, out _));
            // C(0) -> C(1) -> C(2)

            // Second child for block 2
            Assert.Equal(OpenBlockStatus.Success, store.TryOpenBlock(_id_1, out var cb3));
            Assert.Equal(_2_1, cb3.Alias);
            Assert.Equal(CommitBlockStatus.Success, store.TryCommitBlock(cb3.Alias, _id_2_1, out _));
            // C(0) -> C(1) -> C(2)
            //             \-> C(2-1)

            // main chain prolonged
            Assert.Equal(OpenBlockStatus.Success, store.TryOpenBlock(_id_2, out var cb4));
            Assert.Equal(_3, cb4.Alias);
            Assert.Equal(CommitBlockStatus.Success, store.TryCommitBlock(cb4.Alias, _id_3, out _));
            // C(0) -> C(1) -> C(2)  -> C(3)
            //             \-> C(2-1)

            // side chain prolonged
            Assert.Equal(OpenBlockStatus.Success, store.TryOpenBlock(_id_2_1, out var cb5));
            // the new assert
            Assert.Equal(_3_1, cb5.Alias);
            // the new assert
            Assert.Equal(CommitBlockStatus.Success, store.TryCommitBlock(cb5.Alias, _id_3_1, out _));
            // C(0) -> C(1) -> C(2)   -> C(3)
            //             \-> C(2-1) -> C(3-1)

            // fork on end of main chain
            Assert.Equal(OpenBlockStatus.Success, store.TryOpenBlock(_id_3, out var cb6));
            Assert.Equal(_4_0, cb6.Alias);
            Assert.Equal(CommitBlockStatus.Success, store.TryCommitBlock(cb6.Alias, _id_4_0, out _));

            Assert.Equal(OpenBlockStatus.Success, store.TryOpenBlock(_id_3, out var cb7));
            Assert.Equal(_4_1, cb7.Alias);
            Assert.Equal(CommitBlockStatus.Success, store.TryCommitBlock(cb7.Alias, _id_4_1, out _));

            // C(0) -> C(1) -> C(2)   -> C(3) -> C(4)
            //             |                 \-> C(4-1)
            //             \-> C(2-1) -> C(3-1)

            var inbox  = new BoundedInbox();
            var outbox = new BoundedInbox();

            var chainController = new ChainController(
                store,
                inbox,
                outbox,
                lineage => { });

            ClientId.Next();
            ClientId.Next();
            ClientId.Next();
            ClientId.Next();
            var c5 = ClientId.Next();

            return(store, inbox, outbox, chainController, c5);
        }
Exemple #3
0
        // HACK: [vermorel] Constructor used as the setup of the unit tests, should be refactored.
        public LineagePruneTests()
        {
            var store = new VolatileChainStore();

            UncommittedBlock freshBlock;

            store.TryOpenBlock(CommittedBlockId.GenesisParent, out var b0);
            var id = b0.Alias;

            // U(0)
            store.TryCommitBlock(id, CommittedBlockId.Genesis, out _);
            // C(0)

            store.TryOpenBlock(CommittedBlockId.Genesis, out freshBlock);
            Assert.Equal(_1, freshBlock.Alias);
            // C(0) -> U(1)
            store.TryCommitBlock(_1, _id_1, out _);
            // C(0) -> C(1)
            store.TryOpenBlock(_id_1, out freshBlock);
            Assert.Equal(_2, freshBlock.Alias);
            // C(0) -> C(1) -> U(2)
            store.TryCommitBlock(_2, _id_2, out _);
            // C(0) -> C(1) -> C(2)
            // Second child for second block
            store.TryOpenBlock(_id_1, out freshBlock);
            Assert.Equal(_2_1, freshBlock.Alias);
            // C(0) -> C(1) -> C(2)
            //             \-> U(2-1)
            store.TryCommitBlock(_2_1, _id_2_1, out _);
            // C(0) -> C(1) -> C(2)
            //             \-> C(2-1)
            store.TryOpenBlock(_id_2, out freshBlock);
            Assert.Equal(_3, freshBlock.Alias);
            store.TryCommitBlock(_3, _id_3, out _);

            store.TryOpenBlock(_id_2_1, out freshBlock);
            Assert.Equal(_3_1, freshBlock.Alias);
            store.TryCommitBlock(_3_1, _id_3_1, out _);
            // C(0) -> C(1) -> C(2)   -> C(3)
            //             \-> C(2-1) -> C(3-1)

            store.TryOpenBlock(_id_3_1, out freshBlock);
            Assert.Equal(_4, freshBlock.Alias);
            store.TryCommitBlock(_4, _id_4, out _);

            store.TryOpenBlock(_id_4, out freshBlock);
            Assert.Equal(_5, freshBlock.Alias);
            store.TryCommitBlock(_5, _id_5, out _);

            store.TryOpenBlock(_id_4, out freshBlock);
            Assert.Equal(_5_1, freshBlock.Alias);
            store.TryCommitBlock(_5_1, _id_5_1, out _);

            // C(0) -> C(1) -> C(2)   -> C(3)
            //             \-> C(2-1) -> C(3-1) -> C(4) -> C(5)
            //                                        \ -> C(5-1)

            // pruning limit is _3_1
            var lineage = (Lineage)store.GetLineage();

            lineage.CoinPruneHeight = _3_1.BlockHeight;

            _lineage = lineage;
        }