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

Propagate pooled arrays to RlpStream #6536

Merged
merged 1 commit into from
Jan 14, 2024
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 @@ -18,7 +18,7 @@ public Rlp Encode(UserOperationWithEntryPoint? item, RlpBehaviors rlpBehaviors =

RlpStream rlpStream = new(GetLength(item, rlpBehaviors));
Encode(rlpStream, item, rlpBehaviors);
return new Rlp(rlpStream.Data!);
return new Rlp(rlpStream.Data.ToArray()!);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private Hash256[] EncodeDecode(Hash256[] input)
}

Span<byte> buffer = stackalloc byte[32];
KeccaksIterator iterator = new(rlpStream.Data, buffer);
KeccaksIterator iterator = new(rlpStream.Data.AsSpan(), buffer);

List<Hash256> decoded = new();
while (iterator.TryGetNext(out Hash256StructRef kec))
Expand Down Expand Up @@ -92,7 +92,7 @@ private Hash256[] EncodeDecodeReDecoded(Hash256[] input)
}

Span<byte> buffer = stackalloc byte[32];
KeccaksIterator iterator = new(rlpStream.Data, buffer);
KeccaksIterator iterator = new(rlpStream.Data.AsSpan(), buffer);

List<Hash256> decoded = new();
while (iterator.TryGetNext(out Hash256StructRef kec))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Rlp Encode(PendingValidators item, RlpBehaviors rlpBehaviors = RlpBehavio

RlpStream rlpStream = new RlpStream(GetLength(item, rlpBehaviors));
Encode(rlpStream, item, rlpBehaviors);
return new Rlp(rlpStream.Data);
return new Rlp(rlpStream.Data.ToArray());
}

public void Encode(RlpStream rlpStream, PendingValidators item, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Rlp Encode(ValidatorInfo? item, RlpBehaviors rlpBehaviors = RlpBehaviors.

RlpStream rlpStream = new(GetLength(item, rlpBehaviors));
Encode(rlpStream, item, rlpBehaviors);
return new Rlp(rlpStream.Data);
return new Rlp(rlpStream.Data.ToArray());
}

public void Encode(RlpStream stream, ValidatorInfo? item, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ bool IBlockProducer.IsProducingBlocks(ulong? maxProducingInterval)
Array.Empty<BlockHeader>(),
spec.WithdrawalsEnabled ? Enumerable.Empty<Withdrawal>() : null
);
header.TxRoot = new TxTrie(block.Transactions).RootHash;
header.TxRoot = TxTrie.CalculateRoot(block.Transactions);
block.Header.Author = _sealer.Address;
return block;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ block is BlockToProduce blockToProduce

public static bool TrySetTransactions(this Block block, Transaction[] transactions)
{
block.Header.TxRoot = new TxTrie(transactions).RootHash;
block.Header.TxRoot = TxTrie.CalculateRoot(transactions);

if (block is BlockToProduce blockToProduce)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public static bool ValidateTxRootMatchesTxs(Block block, out Hash256 txRoot)
}
public static bool ValidateTxRootMatchesTxs(BlockHeader header, BlockBody body, out Hash256 txRoot)
{
txRoot = new TxTrie(body.Transactions).RootHash;
txRoot = TxTrie.CalculateRoot(body.Transactions);
return txRoot == header.TxRoot;
}

Expand Down
9 changes: 3 additions & 6 deletions src/Nethermind/Nethermind.Core.Test/Builders/BlockBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,17 @@ public BlockBuilder WithTransactions(int txCount, ISpecProvider specProvider)
}

BlockBuilder result = WithTransactions(txs);
ReceiptTrie receiptTrie = new(specProvider.GetSpec(TestObjectInternal.Header), receipts);
receiptTrie.UpdateRootHash();
TestObjectInternal.Header.ReceiptsRoot = receiptTrie.RootHash;
Hash256 receiptHash = ReceiptTrie.CalculateRoot(specProvider.GetSpec(TestObjectInternal.Header), receipts);
TestObjectInternal.Header.ReceiptsRoot = receiptHash;
return result;
}

public BlockBuilder WithTransactions(params Transaction[] transactions)
{
TestObjectInternal = TestObjectInternal.WithReplacedBody(
TestObjectInternal.Body.WithChangedTransactions(transactions));
TxTrie trie = new(transactions);
trie.UpdateRootHash();

TestObjectInternal.Header.TxRoot = trie.RootHash;
TestObjectInternal.Header.TxRoot = TxTrie.CalculateRoot(transactions);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ private Block CreateBlock(int splitVariant, int splitFrom, int blockIndex, Block
currentBlock.Bloom!.Add(receipt.Logs);
}

currentBlock.Header.TxRoot = new TxTrie(currentBlock.Transactions).RootHash;
currentBlock.Header.TxRoot = TxTrie.CalculateRoot(currentBlock.Transactions);
TxReceipt[] txReceipts = receipts.ToArray();
currentBlock.Header.ReceiptsRoot = new ReceiptTrie(_specProvider.GetSpec(currentBlock.Header), txReceipts).RootHash;
currentBlock.Header.ReceiptsRoot = ReceiptTrie.CalculateRoot(_specProvider.GetSpec(currentBlock.Header), txReceipts);
currentBlock.Header.Hash = currentBlock.CalculateHash();
foreach (TxReceipt txReceipt in txReceipts)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void Roundtrip_value((string TestName, AccessList? AccessList) testCase)
RlpStream rlpStream = new(10000);
_decoder.Encode(rlpStream, testCase.AccessList);
rlpStream.Position = 0;
Rlp.ValueDecoderContext ctx = rlpStream.Data.AsRlpValueContext();
Rlp.ValueDecoderContext ctx = rlpStream.Data.AsSpan().AsRlpValueContext();
AccessList decoded = _decoder.Decode(ref ctx)!;
if (testCase.AccessList is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static Rlp BlockInfoEncodeDeprecated(BlockInfo? item, bool chainWithFinal
stream.Encode(item.IsFinalized);
}

return new Rlp(stream.Data!);
return new Rlp(stream.Data.ToArray()!);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void Roundtrip_ValueDecoderContext_WithMemorySlice((Transaction Tx, strin
RlpStream rlpStream = new(10000);
_txDecoder.Encode(rlpStream, testCase.Tx);

Rlp.ValueDecoderContext decoderContext = new(rlpStream.Data, true);
Rlp.ValueDecoderContext decoderContext = new(rlpStream.Data.ToArray(), true);
rlpStream.Position = 0;
Transaction? decoded = _txDecoder.Decode(ref decoderContext);
decoded!.SenderAddress =
Expand All @@ -143,7 +143,7 @@ public void ValueDecoderContext_DecodeWithMemorySlice_ShouldUseSameBuffer((Trans
RlpStream rlpStream = new(10000);
_txDecoder.Encode(rlpStream, testCase.Tx);

Rlp.ValueDecoderContext decoderContext = new(rlpStream.Data, true);
Rlp.ValueDecoderContext decoderContext = new(rlpStream.Data.ToArray(), true);
rlpStream.Position = 0;
Transaction? decoded = _txDecoder.Decode(ref decoderContext);

Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Core.Test/RlpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public void RlpContextWithSliceMemory_shouldNotCopyUnderlyingData(bool sliceValu
stream.Encode(randomBytes);
stream.Encode(randomBytes);

Memory<byte> memory = stream.Data;
Memory<byte> memory = stream.Data.ToArray();
Rlp.ValueDecoderContext context = new Rlp.ValueDecoderContext(memory, sliceValue);

for (int i = 0; i < 3; i++)
Expand Down
38 changes: 32 additions & 6 deletions src/Nethermind/Nethermind.Core/Buffers/CappedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ namespace Nethermind.Core.Buffers;
/// </summary>
public readonly struct CappedArray<T>
{
private readonly T[]? _array = null;
private readonly int _length = 0;
private readonly static CappedArray<T> _null = default;
private readonly static CappedArray<T> _empty = new CappedArray<T>(Array.Empty<T>());
public static ref readonly CappedArray<T> Null => ref _null;
public static ref readonly CappedArray<T> Empty => ref _empty;

private readonly T[]? _array;
private readonly int _length;

public CappedArray(T[]? array, int length)
{
Expand All @@ -31,27 +36,48 @@ public CappedArray(T[]? array)
}
}

public static implicit operator ReadOnlySpan<T>(CappedArray<T> array)
public static implicit operator ReadOnlySpan<T>(in CappedArray<T> array)
{
return array.AsSpan();
}

public static implicit operator CappedArray<T>(T[]? array)
{
if (array == null) return new CappedArray<T>(null);
if (array == null) return default;
return new CappedArray<T>(array);
}

public T this[int index]
{
get
{
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, _length);
return _array![index];
}
set
{
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, _length);
_array![index] = value;
}
}

public readonly int Length => _length;
public readonly int UnderlyingLength => _array?.Length ?? 0;

public readonly T[]? Array => _array;
public readonly T[]? UnderlyingArray => _array;
public readonly bool IsUncapped => _length == _array?.Length;
public readonly bool IsNull => _array is null;
public readonly bool IsNotNull => _array is not null;
public readonly bool IsNotNullOrEmpty => _length > 0;

public readonly Span<T> AsSpan()
{
return _array.AsSpan()[..Length];
return _array.AsSpan(0, _length);
}

public readonly Span<T> AsSpan(int start, int length)
{
return _array.AsSpan(start, length);
}

public readonly T[]? ToArray()
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Core/Buffers/ICappedArrayPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface ICappedArrayPool
{
CappedArray<byte> Rent(int size);

void Return(CappedArray<byte> buffer);
void Return(in CappedArray<byte> buffer);
}

public static class BufferPoolExtensions
Expand All @@ -18,7 +18,7 @@ public static CappedArray<byte> SafeRentBuffer(this ICappedArrayPool? pool, int
return pool.Rent(size);
}

public static void SafeReturnBuffer(this ICappedArrayPool? pool, CappedArray<byte> buffer)
public static void SafeReturnBuffer(this ICappedArrayPool? pool, in CappedArray<byte> buffer)
{
pool?.Return(buffer);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Core/KeyValueStoreExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ public static void Set(this IWriteOnlyKeyValueStore db, Hash256 key, byte[] valu
db.PutSpan(key.Bytes, value, writeFlags);
}

public static void Set(this IWriteOnlyKeyValueStore db, Hash256 key, CappedArray<byte> value, WriteFlags writeFlags = WriteFlags.None)
public static void Set(this IWriteOnlyKeyValueStore db, Hash256 key, in CappedArray<byte> value, WriteFlags writeFlags = WriteFlags.None)
{
if (value.IsUncapped && db.PreferWriteByArray)
{
db.Set(key.Bytes, value.ToArray(), writeFlags);
return;
}

db.PutSpan(key.Bytes, value, writeFlags);
db.PutSpan(key.Bytes, value.AsSpan(), writeFlags);
}

public static void Set(this IWriteOnlyKeyValueStore db, long blockNumber, Hash256 key, ReadOnlySpan<byte> value, WriteFlags writeFlags = WriteFlags.None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ public void InsertReceipts(BlockParameter blockParameter, TxReceipt[] txReceipts
}

Block block = searchResult.Object;
ReceiptTrie receiptTrie = new(_specProvider.GetSpec(block.Header), txReceipts);
receiptTrie.UpdateRootHash();
if (block.ReceiptsRoot != receiptTrie.RootHash)
Hash256 receiptHash = ReceiptTrie.CalculateRoot(_specProvider.GetSpec(block.Header), txReceipts);
if (block.ReceiptsRoot != receiptHash)
{
throw new InvalidDataException("Receipts root mismatch");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ private byte[][] CollectHeaderBytes(ProofTxTracer proofTxTracer, BlockHeader tra

private static byte[][] BuildTxProofs(Transaction[] txs, IReleaseSpec releaseSpec, int index)
{
return new TxTrie(txs, true).BuildProof(index);
return TxTrie.CalculateProof(txs, index);
}

private byte[][] BuildReceiptProofs(BlockHeader blockHeader, TxReceipt[] receipts, int index)
{
return new ReceiptTrie(_specProvider.GetSpec(blockHeader), receipts, true).BuildProof(index);
return ReceiptTrie.CalculateReceiptProofs(_specProvider.GetSpec(blockHeader), receipts, index);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public virtual bool TryGetBlock(out Block? block, UInt256? totalDifficulty = nul
Author = FeeRecipient,
IsPostMerge = true,
TotalDifficulty = totalDifficulty,
TxRoot = new TxTrie(transactions).RootHash,
TxRoot = TxTrie.CalculateRoot(transactions),
WithdrawalsRoot = Withdrawals is null ? null : new WithdrawalTrie(Withdrawals).RootHash,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ private bool TryOverwritePivot(Hash256 finalizedBlockHash, long finalizedBlockNu
RlpStream pivotData = new(38); //1 byte (prefix) + 4 bytes (long) + 1 byte (prefix) + 32 bytes (Keccak)
pivotData.Encode(finalizedBlockNumber);
pivotData.Encode(finalizedBlockHash);
_metadataDb.Set(MetadataDbKeys.UpdatedPivotData, pivotData.Data!);
_metadataDb.Set(MetadataDbKeys.UpdatedPivotData, pivotData.Data.ToArray()!);

if (_logger.IsInfo) _logger.Info($"New pivot block has been set based on ForkChoiceUpdate from CL. Pivot block number: {finalizedBlockNumber}, hash: {finalizedBlockHash}");
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Network.Enr/NodeRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public string GetHex()
int totalLength = Rlp.LengthOfSequence(contentLength);
RlpStream rlpStream = new(totalLength);
Encode(rlpStream);
return rlpStream.Data!.ToHexString();
return rlpStream.Data.AsSpan().ToHexString();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Network.Enr/NodeRecordSigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public NodeRecord Deserialize(RlpStream rlpStream)
originalContentStream.StartSequence(noSigContentLength);
originalContentStream.Write(rlpStream.Read(noSigContentLength));
rlpStream.Position = startPosition;
nodeRecord.OriginalContentRlp = originalContentStream.Data!;
nodeRecord.OriginalContentRlp = originalContentStream.Data.ToArray()!;
}

nodeRecord.EnrSequence = enrSequence;
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Network/NetworkNodeDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public Rlp Encode(NetworkNode item, RlpBehaviors rlpBehaviors = RlpBehaviors.Non
stream.Encode(item.Port);
stream.Encode(string.Empty);
stream.Encode(item.Reputation);
return new Rlp(stream.Data);
return new Rlp(stream.Data.ToArray());
}

public void Encode(MemoryStream stream, NetworkNode item, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public Rlp Encode(Account? item, RlpBehaviors rlpBehaviors = RlpBehaviors.None)

Encode(item, rlpStream, contentLength);

return new Rlp(rlpStream.Data);
return new Rlp(rlpStream.Data.ToArray());
}

public void Encode(Account account, RlpStream rlpStream, int? contentLength = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public Rlp Encode(Block? item, RlpBehaviors rlpBehaviors = RlpBehaviors.None)

RlpStream rlpStream = new(GetLength(item, rlpBehaviors));
Encode(rlpStream, item, rlpBehaviors);
return new(rlpStream.Data);
return new(rlpStream.Data.ToArray());
}

public void Encode(RlpStream stream, Block? item, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public static RlpStream AsRlpStream(this byte[]? bytes)
return new(bytes ?? Array.Empty<byte>());
}

public static RlpStream AsRlpStream(this CappedArray<byte> bytes)
public static RlpStream AsRlpStream(in this CappedArray<byte> bytes)
{
return new(bytes.Array ?? Array.Empty<byte>());
return new(in bytes.IsNotNull ? ref bytes : ref CappedArray<byte>.Empty);
}

public static Rlp.ValueDecoderContext AsRlpValueContext(this byte[]? bytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public Rlp Encode(TxReceipt item, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
{
RlpStream rlpStream = new(GetLength(item, rlpBehaviors));
Encode(rlpStream, item, rlpBehaviors);
return new Rlp(rlpStream.Data);
return new Rlp(rlpStream.Data.ToArray());
}

public void Encode(RlpStream rlpStream, TxReceipt? item, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public Rlp Encode(BlockHeader? item, RlpBehaviors rlpBehaviors = RlpBehaviors.No
RlpStream rlpStream = new(GetLength(item, rlpBehaviors));
Encode(rlpStream, item, rlpBehaviors);

return new Rlp(rlpStream.Data);
return new Rlp(rlpStream.Data.ToArray());
}

private static int GetContentLength(BlockHeader? item, RlpBehaviors rlpBehaviors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Rlp Encode(LogEntry? item, RlpBehaviors rlpBehaviors = RlpBehaviors.None)

RlpStream rlpStream = new(GetLength(item, rlpBehaviors));
Encode(rlpStream, item, rlpBehaviors);
return new Rlp(rlpStream.Data);
return new Rlp(rlpStream.Data.ToArray());
}

public void Encode(RlpStream rlpStream, LogEntry? item, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public byte[] EncodeNew(TxReceipt? item, RlpBehaviors rlpBehaviors = RlpBehavior
int length = GetLength(item, rlpBehaviors);
RlpStream stream = new(length);
Encode(stream, item, rlpBehaviors);
return stream.Data;
return stream.Data.ToArray();
}

public void Encode(RlpStream rlpStream, TxReceipt item, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
Expand Down
Loading