Skip to content

Commit

Permalink
Revert "Perf/eth getlogs with compact encoding (#5569)"
Browse files Browse the repository at this point in the history
This reverts commit e848081.

# Conflicts:
#	src/Nethermind/Nethermind.Facade/Filters/LogFinder.cs
#	src/Nethermind/Nethermind.Serialization.Rlp/Rlp.cs
  • Loading branch information
MarekM25 committed Apr 26, 2023
1 parent 24e6f06 commit 85fffa3
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 310 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,16 @@ public void Adds_and_retrieves_receipts_for_block_with_iterator()
_storage.ClearCache();
_storage.TryGetReceiptsIterator(block.Number, block.Hash!, out ReceiptsIterator iterator).Should().BeTrue();
iterator.TryGetNext(out TxReceiptStructRef receiptStructRef).Should().BeTrue();
receiptStructRef.LogsRlp.ToArray().Should().NotBeEmpty();
receiptStructRef.Logs.Should().BeNullOrEmpty();
if (_useCompactReceipts)
{
receiptStructRef.LogsRlp.IsNullOrEmpty().Should().BeTrue();
receiptStructRef.Logs.Should().NotBeNullOrEmpty();
}
else
{
receiptStructRef.LogsRlp.ToArray().Should().NotBeEmpty();
receiptStructRef.Logs.Should().BeNullOrEmpty();
}

iterator.TryGetNext(out receiptStructRef).Should().BeFalse();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,20 @@ namespace Nethermind.Blockchain.Receipts
{
private readonly int _length;
private Rlp.ValueDecoderContext _decoderContext;
private readonly Span<byte> _buffer;
public long Index { get; private set; }

public KeccaksIterator(Span<byte> data, Span<byte> buffer)
public KeccaksIterator(Span<byte> data)
{
if (buffer.Length != 32) throw new ArgumentException("Buffer must be 32 bytes long");
_decoderContext = new Rlp.ValueDecoderContext(data);
_length = _decoderContext.ReadSequenceLength();
_buffer = buffer;
Index = -1;
}

public bool TryGetNext(out KeccakStructRef current)
{
if (_decoderContext.Position < _length)
{
_decoderContext.DecodeZeroPrefixedKeccakStructRef(out current, _buffer);
_decoderContext.DecodeKeccakStructRef(out current);
Index++;
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ namespace Nethermind.Blockchain.Receipts
private readonly LogEntry[]? _logs;
private readonly int _length;
private Rlp.ValueDecoderContext _decoderContext;
private IReceiptRefDecoder _receiptRefDecoder;
public long Index { get; private set; }

public LogEntriesIterator(Span<byte> data, IReceiptRefDecoder receiptRefDecoder)
public LogEntriesIterator(Span<byte> data)
{
_decoderContext = new Rlp.ValueDecoderContext(data);
_length = _decoderContext.ReadSequenceLength();
Index = -1;
_logs = null;
_receiptRefDecoder = receiptRefDecoder;
}

public LogEntriesIterator(LogEntry[] logs)
Expand All @@ -38,7 +36,7 @@ public bool TryGetNext(out LogEntryStructRef current)
{
if (_decoderContext.Position < _length)
{
_receiptRefDecoder.DecodeLogEntryStructRef(ref _decoderContext, RlpBehaviors.None, out current);
LogEntryDecoder.DecodeStructRef(ref _decoderContext, RlpBehaviors.None, out current);
Index++;
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,15 @@ public bool TryGetReceiptsIterator(long blockNumber, Keccak blockHash, out Recei

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


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

if (_storageDecoder.IsCompactEncoding(receiptsData))
{
recoveryContextFactory = () =>
{
Block block = _blockTree.FindBlock(blockHash);
return _receiptsRecovery.CreateRecoveryContext(block!);
};
Block block = _blockTree.FindBlock(blockHash);
recoveryContext = _receiptsRecovery.CreateRecoveryContext(block!);
}

IReceiptRefDecoder refDecoder = _storageDecoder.GetRefDecoder(receiptsData);

iterator = result ? new ReceiptsIterator(receiptsData, _blocksDb, recoveryContextFactory, refDecoder) : new ReceiptsIterator();
iterator = result ? new ReceiptsIterator(receiptsData, _blocksDb, recoveryContext) : new ReceiptsIterator();
return result;
}

Expand Down
78 changes: 30 additions & 48 deletions src/Nethermind/Nethermind.Blockchain/Receipts/ReceiptsIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using Nethermind.Blockchain.Find;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Db;
using Nethermind.Serialization.Rlp;
#pragma warning disable 618
Expand All @@ -16,33 +15,30 @@ namespace Nethermind.Blockchain.Receipts
private readonly IDbWithSpan _blocksDb;
private readonly int _length;
private Rlp.ValueDecoderContext _decoderContext;
private int _startingPosition;

private readonly TxReceipt[]? _receipts;
private int _receiptIndex;
private int _position;
private readonly IReceiptsRecovery.IRecoveryContext? _recoveryContext;
private readonly bool _compactEncoding;

private readonly Func<IReceiptsRecovery.IRecoveryContext>? _recoveryContextFactory;
private IReceiptsRecovery.IRecoveryContext? _recoveryContext;
private IReceiptRefDecoder _receiptRefDecoder;
private bool _recoveryContextConfigured;

public ReceiptsIterator(scoped in Span<byte> receiptsData, IDbWithSpan blocksDb, Func<IReceiptsRecovery.IRecoveryContext?>? recoveryContextFactory, IReceiptRefDecoder receiptRefDecoder)
public ReceiptsIterator(scoped in Span<byte> receiptsData, IDbWithSpan blocksDb, IReceiptsRecovery.IRecoveryContext? receiptsRecoveryContext)
{
_decoderContext = receiptsData.AsRlpValueContext();
_blocksDb = blocksDb;
_receipts = null;
_receiptIndex = 0;
_recoveryContextFactory = recoveryContextFactory;
_recoveryContextConfigured = false;
_recoveryContext = null;
_receiptRefDecoder = receiptRefDecoder;
_position = 0;
_recoveryContext = receiptsRecoveryContext;

if (_decoderContext.Length > 0 && _decoderContext.PeekByte() == ReceiptArrayStorageDecoder.CompactEncoding)
{
_compactEncoding = true;
_decoderContext.ReadByte();
}
else
{
_compactEncoding = false;
}

_startingPosition = _decoderContext.Position;
_length = receiptsData.Length == 0 ? 0 : _decoderContext.ReadSequenceLength();
}

Expand All @@ -56,8 +52,7 @@ public ReceiptsIterator(TxReceipt[] receipts)
_length = receipts.Length;
_blocksDb = null;
_receipts = receipts;
_receiptIndex = 0;
_recoveryContextConfigured = true;
_position = 0;
}

public bool TryGetNext(out TxReceiptStructRef current)
Expand All @@ -66,17 +61,23 @@ public bool TryGetNext(out TxReceiptStructRef current)
{
if (_decoderContext.Position < _length)
{
_receiptRefDecoder.DecodeStructRef(ref _decoderContext, RlpBehaviors.Storage, out current);
if (_compactEncoding)
{
CompactReceiptStorageDecoder.Instance.DecodeStructRef(ref _decoderContext, RlpBehaviors.Storage, out current);
}
else
{
ReceiptStorageDecoder.Instance.DecodeStructRef(ref _decoderContext, RlpBehaviors.Storage, out current);
}
_recoveryContext?.RecoverReceiptData(ref current);
_receiptIndex++;
return true;
}
}
else
{
if (_receiptIndex < _length)
if (_position < _length)
{
current = new TxReceiptStructRef(_receipts[_receiptIndex++]);
current = new TxReceiptStructRef(_receipts[_position++]);
return true;
}
}
Expand All @@ -85,24 +86,17 @@ public bool TryGetNext(out TxReceiptStructRef current)
return false;
}

public void RecoverIfNeeded(ref TxReceiptStructRef current)
public void Reset()
{
if (_recoveryContextConfigured) return;

_recoveryContext = _recoveryContextFactory?.Invoke();
if (_recoveryContext != null)
if (_receipts is not null)
{
// Need to replay the context.
_decoderContext.Position = _startingPosition;
if (_length != 0) _decoderContext.ReadSequenceLength();
for (int i = 0; i < _receiptIndex; i++)
{
_receiptRefDecoder.DecodeStructRef(ref _decoderContext, RlpBehaviors.Storage, out current);
_recoveryContext?.RecoverReceiptData(ref current);
}
_position = 0;
}
else
{
_decoderContext.Position = 0;
_decoderContext.ReadSequenceLength();
}

_recoveryContextConfigured = true;
}

public void Dispose()
Expand All @@ -112,17 +106,5 @@ public void Dispose()
_blocksDb?.DangerousReleaseMemory(_decoderContext.Data);
}
}

public LogEntriesIterator IterateLogs(TxReceiptStructRef receipt)
{
return receipt.Logs is null ? new LogEntriesIterator(receipt.LogsRlp, _receiptRefDecoder) : new LogEntriesIterator(receipt.Logs);
}

public Keccak[] DecodeTopics(Rlp.ValueDecoderContext valueDecoderContext)
{
return _receiptRefDecoder.DecodeTopics(valueDecoderContext);
}

public bool CanDecodeBloom => _receiptRefDecoder == null || _receiptRefDecoder.CanDecodeBloom;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public ReceiptsRecoveryResult TryRecover(Block block, TxReceipt[] receipts, bool
return ReceiptsRecoveryResult.Fail;
}

public IReceiptsRecovery.IRecoveryContext CreateRecoveryContext(Block block, bool forceRecoverSender = false)
public IReceiptsRecovery.IRecoveryContext CreateRecoveryContext(Block block, bool forceRecoverSender = true)
{
var releaseSpec = _specProvider.GetSpec(block.Header);
return new RecoveryContext(releaseSpec, block, forceRecoverSender, _ecdsa);
Expand Down Expand Up @@ -101,7 +101,7 @@ public void RecoverReceiptData(TxReceipt receipt)
receipt.BlockNumber = _block.Number;
receipt.TxHash = transaction.Hash;
receipt.Index = _transactionIndex;
receipt.Sender ??= transaction.SenderAddress ?? (_forceRecoverSender ? _ecdsa.RecoverAddress(transaction, !_releaseSpec.ValidateChainId) : null);
receipt.Sender = transaction.SenderAddress ?? (_forceRecoverSender ? _ecdsa.RecoverAddress(transaction, !_releaseSpec.ValidateChainId) : null);
receipt.Recipient = transaction.IsContractCreation ? null : transaction.To;

// how would it be in CREATE2?
Expand Down Expand Up @@ -129,10 +129,7 @@ public void RecoverReceiptData(ref TxReceiptStructRef receipt)
receipt.BlockNumber = _block.Number;
receipt.TxHash = transaction.Hash!.ToStructRef();
receipt.Index = _transactionIndex;
if (receipt.Sender.Bytes == Address.Zero.Bytes)
{
receipt.Sender = (transaction.SenderAddress ?? (_forceRecoverSender ? _ecdsa.RecoverAddress(transaction, !_releaseSpec.ValidateChainId) : Address.Zero))!.ToStructRef();
}
receipt.Sender = (transaction.SenderAddress ?? (_forceRecoverSender ? _ecdsa.RecoverAddress(transaction, !_releaseSpec.ValidateChainId) : Address.Zero))!.ToStructRef();
receipt.Recipient = (transaction.IsContractCreation ? Address.Zero : transaction.To)!.ToStructRef();

// how would it be in CREATE2?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public BlockBuilder WithTransactions(int txCount, ISpecProvider specProvider)
for (int i = 0; i < txCount; i++)
{
txs[i] = new Transaction();
txs[i].Hash = txs[i].CalculateHash();
}

TxReceipt[] receipts = new TxReceipt[txCount];
Expand Down
Loading

0 comments on commit 85fffa3

Please sign in to comment.