Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asdacap committed Oct 7, 2023
1 parent 314b8eb commit 0b5292f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
19 changes: 10 additions & 9 deletions src/Nethermind/Nethermind.Blockchain.Test/BlockTreeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
7 changes: 7 additions & 0 deletions src/Nethermind/Nethermind.Blockchain/Blocks/BlockStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions src/Nethermind/Nethermind.Merge.Plugin.Test/BlockTreeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<InvalidOperationException>(() => scenario.NotSyncedTree.FindBlock(beaconBlock.Header.Hash, BlockTreeLookupOptions.None));
}

Expand Down

0 comments on commit 0b5292f

Please sign in to comment.