Beispiel #1
0
 public void Can_get_values()
 {
     MemDb memDb = new MemDb();
     memDb.Set(TestItem.KeccakA, _sampleValue);
     memDb.Set(TestItem.KeccakB, _sampleValue);
     memDb.Values.Should().HaveCount(2);
 }
Beispiel #2
0
        public async Task Can_load_blocks_from_db_odd()
        {
            for (int chainLength = 2; chainLength <= 32; chainLength++)
            {
                Block genesisBlock = Build.A.Block.Genesis.TestObject;

                MemDb blocksDb     = new MemDb();
                MemDb blockInfosDb = new MemDb();

                BlockTree testTree = Build.A.BlockTree(genesisBlock).OfChainLength(chainLength).TestObject;
                for (int i = 0; i < testTree.Head.Number + 1; i++)
                {
                    Block ithBlock = testTree.FindBlock((ulong)i);
                    blocksDb.Set(ithBlock.Hash, Rlp.Encode(ithBlock).Bytes);

                    ChainLevelInfo ithLevel = new ChainLevelInfo(true, new BlockInfo[1] {
                        new BlockInfo(ithBlock.Hash, ithBlock.TotalDifficulty.Value, (ulong)ithBlock.Transactions.Length)
                    });
                    blockInfosDb.Set(i, Rlp.Encode(ithLevel).Bytes);
                }

                blocksDb.Set(Keccak.Zero, Rlp.Encode(testTree.FindBlock(1)).Bytes);

                BlockTree blockTree = new BlockTree(blocksDb, blockInfosDb, OlympicSpecProvider.Instance, Substitute.For <ITransactionPool>(), LimboLogs.Instance);
                await blockTree.LoadBlocksFromDb(CancellationToken.None);

                Assert.AreEqual(blockTree.BestSuggested.Hash, testTree.Head.Hash, $"head {chainLength}");
            }
        }
Beispiel #3
0
        public async Task Can_load_blocks_from_db_odd()
        {
            for (int chainLength = 2; chainLength <= 32; chainLength++)
            {
                Block genesisBlock = Build.A.Block.Genesis.TestObject;

                MemDb blocksDb     = new MemDb();
                MemDb blockInfosDb = new MemDb();
                MemDb headersDb    = new MemDb();

                BlockTree testTree = Build.A.BlockTree(genesisBlock).OfChainLength(chainLength).TestObject;
                for (int i = 0; i < testTree.Head.Number + 1; i++)
                {
                    Block ithBlock = testTree.FindBlock(i, BlockTreeLookupOptions.None);
                    blocksDb.Set(ithBlock.Hash, Rlp.Encode(ithBlock).Bytes);

                    headersDb.Set(ithBlock.Hash, Rlp.Encode(ithBlock.Header).Bytes);

                    ChainLevelInfo ithLevel = new ChainLevelInfo(true, new BlockInfo[1] {
                        new BlockInfo(ithBlock.Hash, ithBlock.TotalDifficulty.Value)
                    });
                    blockInfosDb.Set(i, Rlp.Encode(ithLevel).Bytes);
                }

                blockInfosDb.Set(Keccak.Zero, genesisBlock.Header.Hash.Bytes);
                headersDb.Set(genesisBlock.Header.Hash, Rlp.Encode(genesisBlock.Header).Bytes);

                BlockTree      blockTree = new BlockTree(blocksDb, headersDb, blockInfosDb, new ChainLevelInfoRepository(blockInfosDb), OlympicSpecProvider.Instance, Substitute.For <ITxPool>(), NullBloomStorage.Instance, LimboLogs.Instance);
                DbBlocksLoader loader    = new DbBlocksLoader(blockTree, LimboNoErrorLogger.Instance);
                await blockTree.Accept(loader, CancellationToken.None);

                Assert.AreEqual(testTree.Head.Hash, blockTree.BestSuggestedHeader.Hash, $"head {chainLength}");
            }
        }
Beispiel #4
0
        public void Can_init_head_block_from_db_by_hash()
        {
            Block genesisBlock = Build.A.Block.Genesis.TestObject;
            Block headBlock    = genesisBlock;

            MemDb blocksDb = new MemDb();

            blocksDb.Set(genesisBlock.Hash, Rlp.Encode(genesisBlock).Bytes);

            MemDb blockInfosDb = new MemDb();

            blockInfosDb.Set(Keccak.Zero, genesisBlock.Hash.Bytes);
            ChainLevelInfo level = new ChainLevelInfo(true, new BlockInfo[1] {
                new BlockInfo(headBlock.Hash, headBlock.Difficulty, (ulong)headBlock.Transactions.Length)
            });

            level.BlockInfos[0].WasProcessed = true;

            blockInfosDb.Set(0, Rlp.Encode(level).Bytes);

            BlockTree blockTree = new BlockTree(blocksDb, blockInfosDb, OlympicSpecProvider.Instance, Substitute.For <ITransactionPool>(), LimboLogs.Instance);

            Assert.AreEqual(headBlock.Hash, blockTree.Head?.Hash, "head");
            Assert.AreEqual(headBlock.Hash, blockTree.Genesis?.Hash, "genesis");
        }
Beispiel #5
0
 public void Can_delete()
 {
     MemDb memDb = new MemDb();
     memDb.Set(TestItem.KeccakA, _sampleValue);
     memDb.Set(TestItem.KeccakB, _sampleValue);
     memDb.Clear();
     memDb.Keys.Should().HaveCount(0);
 }
Beispiel #6
0
 public void Can_get_some_keys()
 {
     MemDb memDb = new MemDb();
     memDb.Set(TestItem.KeccakA, _sampleValue);
     memDb.Set(TestItem.KeccakB, _sampleValue);
     var result = memDb[new[] {TestItem.KeccakB.Bytes, TestItem.KeccakB.Bytes, TestItem.KeccakC.Bytes}];
     result.Should().HaveCount(3);
     result[0].Value.Should().NotBeNull();
     result[1].Value.Should().NotBeNull();
     result[2].Value.Should().BeNull();
 }
Beispiel #7
0
 public void Can_remove_key()
 {
     MemDb memDb = new MemDb();
     memDb.Set(TestItem.KeccakA, _sampleValue);
     memDb.Remove(TestItem.KeccakA.Bytes);
     memDb.KeyExists(TestItem.KeccakA).Should().BeFalse();
 }
Beispiel #8
0
 public void Can_check_if_key_exists()
 {
     MemDb memDb = new MemDb();
     memDb.Set(TestItem.KeccakA, _sampleValue);
     memDb.KeyExists(TestItem.KeccakA).Should().BeTrue();
     memDb.KeyExists(TestItem.KeccakB).Should().BeFalse();
 }
Beispiel #9
0
 public void Can_create_with_name()
 {
     MemDb memDb = new MemDb("desc");
     memDb.Set(TestItem.KeccakA, new byte[] {1, 2, 3});
     memDb.Get(TestItem.KeccakA);
     memDb.Name.Should().Be("desc");
 }
Beispiel #10
0
 public void Can_create_with_delays()
 {
     MemDb memDb = new MemDb(10, 10);
     memDb.Set(TestItem.KeccakA, new byte[] {1, 2, 3});
     memDb.Get(TestItem.KeccakA);
     var some = memDb[new[] {TestItem.KeccakA.Bytes}];
 }
Beispiel #11
0
        public void Can_create_without_arguments()
        {
            MemDb memDb = new MemDb();

            memDb.Set(TestItem.KeccakA, new byte[] { 1, 2, 3 });
            memDb.Get(TestItem.KeccakA);
        }
Beispiel #12
0
 public void Simple_set_get_is_fine()
 {
     IDb memDb = new MemDb();
     byte[] bytes = new byte[] {1, 2, 3};
     memDb.Set(TestItem.KeccakA, bytes);
     byte[] retrievedBytes = memDb.Get(TestItem.KeccakA);
     retrievedBytes.Should().BeEquivalentTo(bytes);
 }
        public BlockTreeBuilder(Block genesisBlock)
        {
            MemDb blocksDb = new MemDb(); // so we automatically include in all tests my questionable decision of storing Head block header at 00...

            blocksDb.Set(Keccak.Zero, Rlp.Encode(Build.A.BlockHeader.TestObject).Bytes);

            _genesisBlock      = genesisBlock;
            TestObjectInternal = new BlockTree(blocksDb, new MemDb(), new MemDb(), RopstenSpecProvider.Instance, NullLogManager.Instance);
        }
Beispiel #14
0
 public void Can_use_batches_without_issues()
 {
     MemDb memDb = new MemDb();
     memDb.StartBatch();
     memDb.Set(TestItem.KeccakA, _sampleValue);
     memDb.CommitBatch();
     var retrieved = memDb.Get(TestItem.KeccakA);
     retrieved.Should().BeEquivalentTo(_sampleValue);
 }
        public BlockTreeBuilder(Block genesisBlock)
        {
            BlocksDb    = new MemDb();
            HeadersDb   = new MemDb();
            BlockInfoDb = new MemDb();

            // so we automatically include in all tests my questionable decision of storing Head block header at 00...
            BlocksDb.Set(Keccak.Zero, Rlp.Encode(Build.A.BlockHeader.TestObject).Bytes);
            _genesisBlock            = genesisBlock;
            ChainLevelInfoRepository = new ChainLevelInfoRepository(BlockInfoDb);
            TestObjectInternal       = new BlockTree(BlocksDb, HeadersDb, BlockInfoDb, ChainLevelInfoRepository, RopstenSpecProvider.Instance, Substitute.For <ITxPool>(), Substitute.For <IBloomStorage>(), LimboLogs.Instance);
        }
Beispiel #16
0
        public async Task When_cleaning_descendants_of_invalid_does_not_touch_other_branches()
        {
            MemDb     blocksDb     = new MemDb();
            MemDb     blockInfosDb = new MemDb();
            MemDb     headersDb    = new MemDb();
            BlockTree tree         = new BlockTree(blocksDb, headersDb, blockInfosDb, MainNetSpecProvider.Instance, NullTxPool.Instance, LimboLogs.Instance);
            Block     block0       = Build.A.Block.WithNumber(0).WithDifficulty(1).TestObject;
            Block     block1       = Build.A.Block.WithNumber(1).WithDifficulty(2).WithParent(block0).TestObject;
            Block     block2       = Build.A.Block.WithNumber(2).WithDifficulty(3).WithParent(block1).TestObject;
            Block     block3       = Build.A.Block.WithNumber(3).WithDifficulty(4).WithParent(block2).TestObject;

            Block block1B = Build.A.Block.WithNumber(1).WithDifficulty(1).WithParent(block0).TestObject;
            Block block2B = Build.A.Block.WithNumber(2).WithDifficulty(1).WithParent(block1B).TestObject;
            Block block3B = Build.A.Block.WithNumber(3).WithDifficulty(1).WithParent(block2B).TestObject;

            tree.SuggestBlock(block0);
            tree.SuggestBlock(block1);
            tree.SuggestBlock(block2);
            tree.SuggestBlock(block3);

            tree.SuggestBlock(block1B);
            tree.SuggestBlock(block2B);
            tree.SuggestBlock(block3B);

            blockInfosDb.Set(BlockTree.DeletePointerAddressInDb, block1.Hash.Bytes);

            CancellationTokenSource tokenSource = new CancellationTokenSource();

#pragma warning disable 4014
            Task.Delay(_dbLoadTimeout).ContinueWith(t => tokenSource.Cancel());
#pragma warning restore 4014
            await tree.LoadBlocksFromDb(tokenSource.Token);

            Assert.AreEqual(3L, tree.BestKnownNumber, "best known");
            Assert.AreEqual(null, tree.Head, "head");
            Assert.AreEqual(block3B.Hash, tree.BestSuggested.Hash, "suggested");

            Assert.IsNull(blocksDb.Get(block1.Hash), "block 1");
            Assert.IsNull(blocksDb.Get(block2.Hash), "block 2");
            Assert.IsNull(blocksDb.Get(block3.Hash), "block 3");

            Assert.NotNull(blockInfosDb.Get(1), "level 1");
            Assert.NotNull(blockInfosDb.Get(2), "level 2");
            Assert.NotNull(blockInfosDb.Get(3), "level 3");
        }
Beispiel #17
0
        public async Task Cleans_invalid_blocks_before_starting_DB_load()
        {
            MemDb     blocksDb     = new MemDb();
            MemDb     blockInfosDb = new MemDb();
            BlockTree tree         = new BlockTree(blocksDb, blockInfosDb, MainNetSpecProvider.Instance, NullTransactionPool.Instance, LimboLogs.Instance);
            Block     block0       = Build.A.Block.WithNumber(0).WithDifficulty(1).TestObject;
            Block     block1       = Build.A.Block.WithNumber(1).WithDifficulty(2).WithParent(block0).TestObject;
            Block     block2       = Build.A.Block.WithNumber(2).WithDifficulty(3).WithParent(block1).TestObject;
            Block     block3       = Build.A.Block.WithNumber(3).WithDifficulty(4).WithParent(block2).TestObject;

            tree.SuggestBlock(block0);
            tree.SuggestBlock(block1);
            tree.SuggestBlock(block2);
            tree.SuggestBlock(block3);

            blockInfosDb.Set(BlockTree.DeletePointerAddressInDb, block1.Hash.Bytes);

            CancellationTokenSource tokenSource = new CancellationTokenSource();

#pragma warning disable 4014
            Task.Delay(_dbLoadTimeout).ContinueWith(t => tokenSource.Cancel());
#pragma warning restore 4014
            await tree.LoadBlocksFromDb(tokenSource.Token);

            Assert.AreEqual(UInt256.Zero, tree.BestKnownNumber, "best known");
            Assert.AreEqual(null, tree.Head, "head");
            Assert.AreEqual(UInt256.Zero, tree.BestSuggested.Number, "suggested");

            Assert.IsNull(blocksDb.Get(block2.Hash), "block 1");
            Assert.IsNull(blocksDb.Get(block2.Hash), "block 2");
            Assert.IsNull(blocksDb.Get(block3.Hash), "block 3");

            Assert.IsNull(blockInfosDb.Get(2), "level 1");
            Assert.IsNull(blockInfosDb.Get(2), "level 2");
            Assert.IsNull(blockInfosDb.Get(3), "level 3");
        }