From 0b5292f9e4a37e1c0e3fddddce4d26ebaa3b8c43 Mon Sep 17 00:00:00 2001 From: Amirul Ashraf Date: Sat, 7 Oct 2023 10:30:23 +0800 Subject: [PATCH] Fix tests --- .../BlockTreeTests.cs | 19 ++++++++++--------- .../Blocks/BlockStore.cs | 7 +++++++ .../BlockTreeTests.cs | 9 +++++++-- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/Nethermind/Nethermind.Blockchain.Test/BlockTreeTests.cs b/src/Nethermind/Nethermind.Blockchain.Test/BlockTreeTests.cs index 07119be768f2..716d76386c11 100644 --- a/src/Nethermind/Nethermind.Blockchain.Test/BlockTreeTests.cs +++ b/src/Nethermind/Nethermind.Blockchain.Test/BlockTreeTests.cs @@ -6,6 +6,7 @@ using System.Threading; using System.Threading.Tasks; using FluentAssertions; +using Nethermind.Blockchain.Blocks; using Nethermind.Blockchain.Find; using Nethermind.Blockchain.Synchronization; using Nethermind.Blockchain.Visitors; @@ -959,9 +960,9 @@ public void When_deleting_invalid_block_deletes_its_descendants() Assert.That(tree.Head!.Number, Is.EqualTo(1L), "head"); Assert.That(tree.BestSuggestedHeader!.Number, Is.EqualTo(1L), "suggested"); - Assert.NotNull(blocksDb.Get(block1.Hash!), "block 1"); - Assert.IsNull(blocksDb.Get(block2.Hash!), "block 2"); - Assert.IsNull(blocksDb.Get(block3.Hash!), "block 3"); + Assert.NotNull(blocksDb.Get(BlockStore.GetBlockNumPrefixedKey(block1.Number, block1.Hash!)), "block 1"); + Assert.IsNull(blocksDb.Get(BlockStore.GetBlockNumPrefixedKey(block2.Number, block2.Hash!)), "block 2"); + Assert.IsNull(blocksDb.Get(BlockStore.GetBlockNumPrefixedKey(block3.Number, block3.Hash!)), "block 3"); Assert.NotNull(blockInfosDb.Get(1), "level 1"); Assert.IsNull(blockInfosDb.Get(2), "level 2"); @@ -1002,12 +1003,12 @@ public void When_deleting_invalid_block_deletes_its_descendants_even_if_not_firs Assert.That(tree.Head!.Number, Is.EqualTo(1L), "head"); Assert.That(tree.BestSuggestedHeader!.Number, Is.EqualTo(1L), "suggested"); - Assert.NotNull(blocksDb.Get(block1.Hash!), "block 1"); - Assert.NotNull(blocksDb.Get(block2.Hash!), "block 2"); - Assert.NotNull(blocksDb.Get(block3.Hash!), "block 3"); - Assert.Null(blocksDb.Get(block1b.Hash!), "block 1b"); - Assert.Null(blocksDb.Get(block2b.Hash!), "block 2b"); - Assert.Null(blocksDb.Get(block3b.Hash!), "block 3b"); + Assert.NotNull(blocksDb.Get(BlockStore.GetBlockNumPrefixedKey(block1.Number, block1.Hash!)), "block 1"); + Assert.NotNull(blocksDb.Get(BlockStore.GetBlockNumPrefixedKey(block2.Number, block2.Hash!)), "block 2"); + Assert.NotNull(blocksDb.Get(BlockStore.GetBlockNumPrefixedKey(block3.Number, block3.Hash!)), "block 3"); + Assert.Null(blocksDb.Get(BlockStore.GetBlockNumPrefixedKey(block1b.Number, block1b.Hash!)), "block 1b"); + Assert.Null(blocksDb.Get(BlockStore.GetBlockNumPrefixedKey(block2b.Number, block2b.Hash!)), "block 2b"); + Assert.Null(blocksDb.Get(BlockStore.GetBlockNumPrefixedKey(block3b.Number, block3b.Hash!)), "block 3b"); Assert.NotNull(blockInfosDb.Get(1), "level 1"); Assert.NotNull(blockInfosDb.Get(2), "level 2"); diff --git a/src/Nethermind/Nethermind.Blockchain/Blocks/BlockStore.cs b/src/Nethermind/Nethermind.Blockchain/Blocks/BlockStore.cs index f14b3b5dd4f0..8a58bac9b318 100644 --- a/src/Nethermind/Nethermind.Blockchain/Blocks/BlockStore.cs +++ b/src/Nethermind/Nethermind.Blockchain/Blocks/BlockStore.cs @@ -65,6 +65,13 @@ private static void GetBlockNumPrefixedKey(long blockNumber, Keccak blockHash, S blockHash!.Bytes.CopyTo(output[8..]); } + public static byte[] GetBlockNumPrefixedKey(long blockNumber, Keccak blockHash) + { + byte[] output = new byte[40]; + GetBlockNumPrefixedKey(blockNumber, blockHash,output); + return output; + } + public void Delete(long blockNumber, Keccak blockHash) { _blockDb.Remove(blockHash.Bytes); diff --git a/src/Nethermind/Nethermind.Merge.Plugin.Test/BlockTreeTests.cs b/src/Nethermind/Nethermind.Merge.Plugin.Test/BlockTreeTests.cs index 8d4f289ed710..bd6034c466f2 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin.Test/BlockTreeTests.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin.Test/BlockTreeTests.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using FluentAssertions; using Nethermind.Blockchain; +using Nethermind.Blockchain.Blocks; using Nethermind.Blockchain.Receipts; using Nethermind.Blockchain.Synchronization; using Nethermind.Core; @@ -278,9 +279,10 @@ public void FindBlock_will_not_change_total_difficulty_when_it_is_zero() Block? beaconBlock = syncedTree.FindBlock(14, BlockTreeLookupOptions.None)!; BlockTreeInsertHeaderOptions headerOptions = BlockTreeInsertHeaderOptions.BeaconBlockInsert; AddBlockResult insertResult = notSyncedTree.Insert(beaconBlock, BlockTreeInsertBlockOptions.SaveHeader, headerOptions); + Block? beaconBlock2 = syncedTree.FindBlock(13, BlockTreeLookupOptions.None); beaconBlock2!.Header.TotalDifficulty = null; - AddBlockResult insertOutcome = notSyncedTree.Insert(beaconBlock2, BlockTreeInsertBlockOptions.None); + AddBlockResult insertOutcome = notSyncedTree.Insert(beaconBlock2, BlockTreeInsertBlockOptions.SaveHeader, headerOptions); Assert.That(insertResult, Is.EqualTo(insertOutcome)); Block? blockToCheck = notSyncedTree.FindBlock(beaconBlock2.Hash, BlockTreeLookupOptions.None); @@ -589,7 +591,9 @@ public ScenarioBuilder InsertToBlockDb(Block block) { BlockDecoder blockDecoder = new(); Rlp newRlp = blockDecoder.Encode(block); - NotSyncedTreeBuilder.BlocksDb.Set(block.GetOrCalculateHash(), newRlp.Bytes); + NotSyncedTreeBuilder.BlocksDb.Set( + BlockStore.GetBlockNumPrefixedKey(block.Number, block.GetOrCalculateHash()), + newRlp.Bytes); return this; } @@ -684,6 +688,7 @@ public void FindBlock_should_throw_exception_when_trying_to_find_dangling_block( Block? beaconBlock = scenario.SyncedTree.FindBlock(14, BlockTreeLookupOptions.None)!; scenario.InsertToBlockDb(beaconBlock); + scenario.InsertToHeaderDb(beaconBlock.Header); Assert.Throws(() => scenario.NotSyncedTree.FindBlock(beaconBlock.Header.Hash, BlockTreeLookupOptions.None)); }