Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Receipt key with block number #5651

Merged
merged 7 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Nethermind.Blockchain.Receipts;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Core.Test;
using Nethermind.Core.Test.Builders;
using Nethermind.Crypto;
using Nethermind.Db;
Expand All @@ -24,12 +26,13 @@ namespace Nethermind.Blockchain.Test.Receipts
[TestFixture(false)]
public class PersistentReceiptStorageTests
{
private MemColumnsDb<ReceiptsColumns> _receiptsDb = null!;
private TestMemColumnsDb<ReceiptsColumns> _receiptsDb = null!;
private ReceiptsRecovery _receiptsRecovery;
private IBlockTree _blockTree;
private readonly bool _useCompactReceipts;
private ReceiptConfig _receiptConfig;
private PersistentReceiptStorage _storage;
private ReceiptArrayStorageDecoder _decoder;

public PersistentReceiptStorageTests(bool useCompactReceipts)
{
Expand All @@ -43,21 +46,22 @@ public void SetUp()
EthereumEcdsa ethereumEcdsa = new(specProvider.ChainId, LimboLogs.Instance);
_receiptConfig = new ReceiptConfig();
_receiptsRecovery = new(ethereumEcdsa, specProvider);
_receiptsDb = new MemColumnsDb<ReceiptsColumns>();
_receiptsDb = new TestMemColumnsDb<ReceiptsColumns>();
_receiptsDb.GetColumnDb(ReceiptsColumns.Blocks).Set(Keccak.Zero, Array.Empty<byte>());
_blockTree = Substitute.For<IBlockTree>();
CreateStorage();
}

private void CreateStorage()
{
_decoder = new ReceiptArrayStorageDecoder(_useCompactReceipts);
_storage = new PersistentReceiptStorage(
_receiptsDb,
MainnetSpecProvider.Instance,
_receiptsRecovery,
_blockTree,
_receiptConfig,
new ReceiptArrayStorageDecoder(_useCompactReceipts)
_decoder
)
{ MigratedBlockNumber = 0 };
}
Expand Down Expand Up @@ -100,6 +104,53 @@ public void Adds_and_retrieves_receipts_for_block()
_storage.Get(block).Should().BeEquivalentTo(receipts);
}

[Test]
public void Adds_should_prefix_key_with_blockNumber()
{
var (block, receipts) = InsertBlock();

Span<byte> blockNumPrefixed = stackalloc byte[40];
block.Number.ToBigEndianByteArray().CopyTo(blockNumPrefixed); // TODO: We don't need to create an array here...
block.Hash!.Bytes.CopyTo(blockNumPrefixed[8..]);

_receiptsDb.GetColumnDb(ReceiptsColumns.Blocks)[blockNumPrefixed].Should().NotBeNull();
}

[Test]
public void Adds_should_attempt_hash_key_first_if_inserted_with_hashkey()
{
var (block, receipts) = PrepareBlock();

using NettyRlpStream rlpStream = _decoder.EncodeToNewNettyStream(receipts, RlpBehaviors.Storage);
_receiptsDb.GetColumnDb(ReceiptsColumns.Blocks)[block.Hash.Bytes] = rlpStream.AsSpan().ToArray();

CreateStorage();
_storage.Get(block);

Span<byte> blockNumPrefixed = stackalloc byte[40];
block.Number.ToBigEndianByteArray().CopyTo(blockNumPrefixed); // TODO: We don't need to create an array here...
block.Hash!.Bytes.CopyTo(blockNumPrefixed[8..]);

TestMemDb blocksDb = (TestMemDb)_receiptsDb.GetColumnDb(ReceiptsColumns.Blocks);
blocksDb.KeyWasRead(blockNumPrefixed.ToArray(), 0);
blocksDb.KeyWasRead(block.Hash.Bytes, 1);
}

[Test]
public void Should_be_able_to_get_block_with_hash_address()
{
var (block, receipts) = PrepareBlock();

Span<byte> blockNumPrefixed = stackalloc byte[40];
block.Number.ToBigEndianByteArray().CopyTo(blockNumPrefixed); // TODO: We don't need to create an array here...
block.Hash!.Bytes.CopyTo(blockNumPrefixed[8..]);

using NettyRlpStream rlpStream = _decoder.EncodeToNewNettyStream(receipts, RlpBehaviors.Storage);
_receiptsDb.GetColumnDb(ReceiptsColumns.Blocks)[block.Hash.Bytes] = rlpStream.AsSpan().ToArray();

_storage.Get(block).Length.Should().Be(receipts.Length);
}

[Test, Timeout(Timeout.MaxTestTime)]
public void Should_not_cache_empty_non_processed_blocks()
{
Expand Down Expand Up @@ -167,14 +218,14 @@ public void Should_handle_inserting_null_receipts()
[Test, Timeout(Timeout.MaxTestTime)]
public void HasBlock_should_returnFalseForMissingHash()
{
_storage.HasBlock(Keccak.Compute("missing-value")).Should().BeFalse();
_storage.HasBlock(0, Keccak.Compute("missing-value")).Should().BeFalse();
}

[Test, Timeout(Timeout.MaxTestTime)]
public void HasBlock_should_returnTrueForKnownHash()
{
var (block, _) = InsertBlock();
_storage.HasBlock(block.Hash!).Should().BeTrue();
_storage.HasBlock(block.Number, block.Hash!).Should().BeTrue();
}

[Test, Timeout(Timeout.MaxTestTime)]
Expand Down Expand Up @@ -307,7 +358,7 @@ public void When_NewHeadBlock_ClearOldTxIndex()
);
}

private (Block block, TxReceipt[] receipts) InsertBlock(Block? block = null, bool isFinalized = false, long? headNumber = null)
private (Block block, TxReceipt[] receipts) PrepareBlock(Block? block = null, bool isFinalized = false, long? headNumber = null)
{
block ??= Build.A.Block
.WithNumber(1)
Expand Down Expand Up @@ -335,6 +386,12 @@ public void When_NewHeadBlock_ClearOldTxIndex()
_blockTree.FindBestSuggestedHeader().Returns(farHead);
}
var receipts = new[] { Build.A.Receipt.WithCalculatedBloom().TestObject };
return (block, receipts);
}

private (Block block, TxReceipt[] receipts) InsertBlock(Block? block = null, bool isFinalized = false, long? headNumber = null)
{
(block, TxReceipt[] receipts) = PrepareBlock(block, isFinalized, headNumber);
_storage.Insert(block, receipts);
_receiptsRecovery.TryRecover(block, receipts);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<LangVersion>latest</LangVersion>
<Nullable>annotations</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IReceiptStorage : IReceiptFinder
void Insert(Block block, TxReceipt[]? txReceipts, bool ensureCanonical);
long? LowestInsertedReceiptBlockNumber { get; set; }
long MigratedBlockNumber { get; set; }
bool HasBlock(Keccak hash);
bool HasBlock(long blockNumber, Keccak hash);
void EnsureCanonical(Block block);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void Insert(Block block, TxReceipt[] txReceipts, bool ensureCanonical = t
}
}

public bool HasBlock(Keccak hash)
public bool HasBlock(long blockNumber, Keccak hash)
{
return _receipts.ContainsKey(hash);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public event EventHandler<ReceiptsEventArgs> ReceiptsInserted
remove { }
}

public bool HasBlock(Keccak hash)
public bool HasBlock(long blockNumber, Keccak hash)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Nethermind.Core;
using Nethermind.Core.Caching;
Expand All @@ -28,6 +30,7 @@ public class PersistentReceiptStorage : IReceiptStorage
private readonly ReceiptArrayStorageDecoder _storageDecoder = ReceiptArrayStorageDecoder.Instance;
private readonly IBlockTree _blockTree;
private readonly IReceiptConfig _receiptConfig;
private readonly bool _legacyHashKey;

private const int CacheSize = 64;
private readonly LruCache<KeccakKey, TxReceipt[]> _receiptsCache = new(CacheSize, CacheSize, "receipts");
Expand Down Expand Up @@ -56,6 +59,9 @@ public PersistentReceiptStorage(
_lowestInsertedReceiptBlock = lowestBytes is null ? (long?)null : new RlpStream(lowestBytes).DecodeLong();
_migratedBlockNumber = Get(MigrationBlockNumberKey, long.MaxValue);

KeyValuePair<byte[], byte[]>? firstValue = _blocksDb.GetAll().FirstOrDefault();
_legacyHashKey = firstValue.HasValue && firstValue.Value.Key != null && firstValue.Value.Key.Length == Keccak.Size;

_blockTree.BlockAddedToMain += BlockTreeOnBlockAddedToMain;
}

Expand Down Expand Up @@ -139,7 +145,8 @@ public TxReceipt[] Get(Block block)
return receipts ?? Array.Empty<TxReceipt>();
}

Span<byte> receiptsData = _blocksDb.GetSpan(blockHash);
Span<byte> receiptsData = GetReceiptData(block.Number, blockHash);

try
{
if (receiptsData.IsNullOrEmpty())
Expand All @@ -162,6 +169,48 @@ public TxReceipt[] Get(Block block)
}
}

private unsafe Span<byte> GetReceiptData(long blockNumber, Keccak blockHash)
{
if (_legacyHashKey)
{
Span<byte> receiptsData = _blocksDb.GetSpan(blockHash);
if (receiptsData != null)
{
return receiptsData;
}

Span<byte> blockNumPrefixed = stackalloc byte[40];
GetBlockNumPrefixedKey(blockNumber, blockHash, blockNumPrefixed);

#pragma warning disable CS9080
receiptsData = _blocksDb.GetSpan(blockNumPrefixed);
#pragma warning restore CS9080

return receiptsData;
}
else
{
Span<byte> blockNumPrefixed = stackalloc byte[40];
GetBlockNumPrefixedKey(blockNumber, blockHash, blockNumPrefixed);

Span<byte> receiptsData = _blocksDb.GetSpan(blockNumPrefixed);
if (receiptsData.IsNull())
{
receiptsData = _blocksDb.GetSpan(blockHash);
}

#pragma warning disable CS9080
return receiptsData;
#pragma warning restore CS9080
}
}

private static void GetBlockNumPrefixedKey(long blockNumber, Keccak blockHash, Span<byte> output)
{
blockNumber.WriteBigEndian(output);
blockHash!.Bytes.CopyTo(output[8..]);
}

public TxReceipt[] Get(Keccak blockHash)
{
Block? block = _blockTree.FindBlock(blockHash);
Expand All @@ -180,8 +229,7 @@ public bool TryGetReceiptsIterator(long blockNumber, Keccak blockHash, out Recei
}

var result = CanGetReceiptsByHash(blockNumber);
var receiptsData = _blocksDb.GetSpan(blockHash);

Span<byte> receiptsData = GetReceiptData(blockNumber, blockHash);

Func<IReceiptsRecovery.IRecoveryContext?> recoveryContextFactory = () => null;

Expand Down Expand Up @@ -220,7 +268,10 @@ public void Insert(Block block, TxReceipt[]? txReceipts, bool ensureCanonical =

using (NettyRlpStream stream = _storageDecoder.EncodeToNewNettyStream(txReceipts, behaviors))
{
_blocksDb.Set(block.Hash!, stream.AsSpan());
Span<byte> blockNumPrefixed = stackalloc byte[40];
GetBlockNumPrefixedKey(blockNumber, block.Hash!, blockNumPrefixed);

_blocksDb.Set(blockNumPrefixed, stream.AsSpan());
}

if (blockNumber < MigratedBlockNumber)
Expand Down Expand Up @@ -264,9 +315,26 @@ internal void ClearCache()
_receiptsCache.Clear();
}

public bool HasBlock(Keccak hash)
public bool HasBlock(long blockNumber, Keccak blockHash)
{
return _receiptsCache.Contains(hash) || _blocksDb.KeyExists(hash);
if (_receiptsCache.Contains(blockHash)) return true;

if (_legacyHashKey)
{
if (_blocksDb.KeyExists(blockHash)) return true;

Span<byte> blockNumPrefixed = stackalloc byte[40];
GetBlockNumPrefixedKey(blockNumber, blockHash, blockNumPrefixed);

return _blocksDb.KeyExists(blockNumPrefixed);
}
else
{
Span<byte> blockNumPrefixed = stackalloc byte[40];
GetBlockNumPrefixedKey(blockNumber, blockHash, blockNumPrefixed);

return _blocksDb.KeyExists(blockNumPrefixed) || _blocksDb.KeyExists(blockHash);
}
}

public void EnsureCanonical(Block block)
Expand Down
32 changes: 32 additions & 0 deletions src/Nethermind/Nethermind.Core.Test/TestMemColumnDb.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System.Collections.Generic;
using Nethermind.Db;

namespace Nethermind.Core.Test;

public class TestMemColumnsDb<TKey> : TestMemDb, IColumnsDb<TKey>
{
private readonly IDictionary<TKey, IDbWithSpan> _columnDbs = new Dictionary<TKey, IDbWithSpan>();

public TestMemColumnsDb()
{
}

public TestMemColumnsDb(params TKey[] keys)
{
foreach (var key in keys)
{
GetColumnDb(key);
}
}

public IDbWithSpan GetColumnDb(TKey key) => !_columnDbs.TryGetValue(key, out var db) ? _columnDbs[key] = new TestMemDb() : db;
public IEnumerable<TKey> ColumnKeys => _columnDbs.Keys;

public IReadOnlyDb CreateReadOnly(bool createInMemWriteStore)
{
return new ReadOnlyColumnsDb<TKey>(this, createInMemWriteStore);
}
}
5 changes: 5 additions & 0 deletions src/Nethermind/Nethermind.Core/Extensions/Int64Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ public static byte[] ToBigEndianByteArray(this long value)
return bytes;
}

public static void WriteBigEndian(this long value, Span<byte> output)
{
BinaryPrimitives.WriteInt64BigEndian(output, value);
}

[ThreadStatic]
private static byte[]? t_byteBuffer64;
private static byte[] GetByteBuffer64() => t_byteBuffer64 ??= new byte[8];
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Db.Rocks/ColumnDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void Set(ReadOnlySpan<byte> key, byte[]? value, WriteFlags flags = WriteF

public IEnumerable<KeyValuePair<byte[], byte[]>> GetAll(bool ordered = false)
{
using Iterator iterator = _mainDb.CreateIterator(ordered, _columnFamily);
Iterator iterator = _mainDb.CreateIterator(ordered, _columnFamily);
return _mainDb.GetAllCore(iterator);
}

Expand Down
Loading