Beispiel #1
0
        public ChainedBlock AppendBlock(ChainedBlock previous, params ConcurrentChain[] chains)
        {
            ChainedBlock last  = null;
            var          nonce = RandomUtils.GetUInt32();

            foreach (var chain in chains)
            {
                var block = TestUtils.CreateFakeBlock(new Transaction());
                block.Header.HashPrevBlock = previous == null ? chain.Tip.HashBlock : previous.HashBlock;
                block.Header.Nonce         = nonce;
                if (!chain.TrySetTip(block.Header, out last))
                {
                    throw new InvalidOperationException("Previous not existing");
                }
            }
            return(last);
        }
Beispiel #2
0
        public ChainedHeader AppendBlock(ChainedHeader previous, params ChainIndexer[] chainsIndexer)
        {
            ChainedHeader last  = null;
            uint          nonce = RandomUtils.GetUInt32();

            foreach (ChainIndexer chain in chainsIndexer)
            {
                Block block = TestUtils.CreateFakeBlock(this.network);
                block.Header.HashPrevBlock = previous == null ? chain.Tip.HashBlock : previous.HashBlock;
                block.Header.Nonce         = nonce;
                if (!chain.TrySetTip(block.Header, out last))
                {
                    throw new InvalidOperationException("Previous not existing");
                }
            }
            return(last);
        }
Beispiel #3
0
        private ChainIndexer CreateChain(BlockHeader genesis, int height)
        {
            var chain = new ChainIndexer(this.network);

            var chainedHeaderPrev = chain.Tip;

            for (int i = 0; i < height; i++)
            {
                Block b = TestUtils.CreateFakeBlock(this.network);
                b.Header.HashPrevBlock = chainedHeaderPrev.HashBlock;

                chainedHeaderPrev = new ChainedHeader(b.Header, b.Header.GetHash(), chainedHeaderPrev);
            }

            chain.SetTip(chainedHeaderPrev);

            return(chain);
        }
Beispiel #4
0
        public void CanForkBackwardPartialChain()
        {
            PersistantChain chain = new PersistantChain(TestUtils.CreateFakeBlock().Header, 10, new StreamObjectStream <ChainChange>());

            AppendBlock(chain);
            AppendBlock(chain);
            var fork = AppendBlock(chain);

            //Test single block back fork
            var last = AppendBlock(chain);

            Assert.Equal(14, chain.Height);
            Assert.Equal(14, last.Height);
            Assert.Equal(last.HashBlock, chain.Tip.HashBlock);
            Assert.Equal(fork.HashBlock, chain.SetTip(fork).HashBlock);
            Assert.Equal(13, chain.Height);
            Assert.Equal(13, fork.Height);
            Assert.Equal(fork.HashBlock, chain.Tip.HashBlock);
            Assert.Null(chain.GetBlock(last.HashBlock));
            Assert.NotNull(chain.GetBlock(fork.HashBlock));

            //Test 3 blocks back fork
            var b1 = AppendBlock(chain);
            var b2 = AppendBlock(chain);

            last = AppendBlock(chain);
            Assert.Equal(16, chain.Height);
            Assert.Equal(16, last.Height);
            Assert.Equal(last.HashBlock, chain.Tip.HashBlock);

            Assert.Equal(fork.HashBlock, chain.SetTip(fork).HashBlock);
            Assert.Equal(13, chain.Height);
            Assert.Equal(13, fork.Height);
            Assert.Equal(fork.HashBlock, chain.Tip.HashBlock);
            Assert.Null(chain.GetBlock(last.HashBlock));
            Assert.Null(chain.GetBlock(b1.HashBlock));
            Assert.Null(chain.GetBlock(b2.HashBlock));

            chain.SetTip(last);
            Assert.Equal(16, chain.Height);
            Assert.Equal(16, last.Height);
            Assert.Equal(last.HashBlock, chain.Tip.HashBlock);
        }
Beispiel #5
0
        private Transaction RecieveTransaction(Transaction tx, Chain[] chains)
        {
            var block = TestUtils.CreateFakeBlock(tx);

            if (chains != null)
            {
                foreach (var c in chains)
                {
                    var localBlock = block.Clone();
                    localBlock.Header.HashPrevBlock = c.Tip.Header.GetHash();
                    c.GetOrAdd(localBlock.Header);
                    _Index.Put(localBlock);
                }
            }
            else
            {
                Wallet.UnconfirmedTransaction(tx);
            }
            return(tx);
        }
Beispiel #6
0
 private ChainIndexer CreateChain(int height)
 {
     return(this.CreateChain(TestUtils.CreateFakeBlock(this.network).Header, height));
 }
Beispiel #7
0
 private ConcurrentChain CreateChain(int height)
 {
     return(CreateChain(TestUtils.CreateFakeBlock().Header, height));
 }