Skip to content

Commit

Permalink
Add catch on async void to avoid process crash + refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaszRozmej committed Jan 2, 2025
1 parent 85670e9 commit 68e0aaa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 50 deletions.
13 changes: 9 additions & 4 deletions src/Nethermind/Nethermind.Optimism/CL/P2P/OptimismCLP2P.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using Nethermind.Optimism.CL;
using Nethermind.Optimism.Rpc;
using Nethermind.Core.Crypto;
using Nethermind.JsonRpc;
using Snappier;

namespace Nethermind.Optimism;
Expand Down Expand Up @@ -91,6 +92,10 @@ private async void OnMessage(byte[] msg)
}
}
}
catch (Exception e)
{
if (_logger.IsError) _logger.Error("Unhandled exception in Optimism CL P2P:", e);
}
finally
{
_semaphore.Release();
Expand All @@ -100,7 +105,7 @@ private async void OnMessage(byte[] msg)
private bool TryValidateAndDecodePayload(byte[] msg, [MaybeNullWhen(false)] out ExecutionPayloadV3 payload)
{
int length = Snappy.GetUncompressedLength(msg);
if (length < 65 || length > MaxGossipSize)
if (length is < 65 or > MaxGossipSize)
{
payload = null;
return false;
Expand Down Expand Up @@ -130,7 +135,7 @@ private bool TryValidateAndDecodePayload(byte[] msg, [MaybeNullWhen(false)] out
return false;
}

var validationResult = _blockValidator.Validate(payload, P2PTopic.BlocksV3);
ValidityStatus validationResult = _blockValidator.Validate(payload, P2PTopic.BlocksV3);

if (validationResult == ValidityStatus.Reject)
{
Expand All @@ -142,7 +147,7 @@ private bool TryValidateAndDecodePayload(byte[] msg, [MaybeNullWhen(false)] out

private async Task<bool> SendNewPayloadToEL(ExecutionPayloadV3 executionPayload)
{
var npResult = await _engineRpcModule.engine_newPayloadV3(executionPayload, Array.Empty<byte[]>(),
ResultWrapper<PayloadStatusV1> npResult = await _engineRpcModule.engine_newPayloadV3(executionPayload, Array.Empty<byte[]>(),
executionPayload.ParentBeaconBlockRoot);

if (npResult.Result.ResultType == ResultType.Failure)
Expand All @@ -165,7 +170,7 @@ private async Task<bool> SendNewPayloadToEL(ExecutionPayloadV3 executionPayload)

private async Task<bool> SendForkChoiceUpdatedToEL(Hash256 headBlockHash)
{
var fcuResult = await _engineRpcModule.engine_forkchoiceUpdatedV3(
ResultWrapper<ForkchoiceUpdatedV1Result> fcuResult = await _engineRpcModule.engine_forkchoiceUpdatedV3(
new ForkchoiceStateV1(headBlockHash, headBlockHash, headBlockHash),
null);

Expand Down
68 changes: 22 additions & 46 deletions src/Nethermind/Nethermind.Optimism/CL/P2P/PayloadDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Buffers.Binary;
using Nethermind.Core;
using Nethermind.Core.Extensions;
using Nethermind.Merge.Plugin.Data;

namespace Nethermind.Optimism.CL;
Expand All @@ -27,56 +28,34 @@ public ExecutionPayloadV3 DecodePayload(ReadOnlySpan<byte> data)
throw new ArgumentException("Invalid payload data size");
}

int offset = 0;
payload.ParentBeaconBlockRoot = new(data[offset..(offset + 32)]);
offset += 32;
payload.ParentHash = new(data[offset..(offset + 32)]);
offset += 32;
payload.FeeRecipient = new(data[offset..(offset + 20)]);
offset += 20;
payload.StateRoot = new(data[offset..(offset + 32)]);
offset += 32;
payload.ReceiptsRoot = new(data[offset..(offset + 32)]);
offset += 32;
payload.LogsBloom = new(data[offset..(offset + 256)]);
offset += 256;
payload.PrevRandao = new(data[offset..(offset + 32)]);
offset += 32;
payload.BlockNumber = (long)BinaryPrimitives.ReadUInt64LittleEndian(data[offset..(offset + 8)]);
offset += 8;
payload.GasLimit = (long)BinaryPrimitives.ReadUInt64LittleEndian(data[offset..(offset + 8)]);
offset += 8;
payload.GasUsed = (long)BinaryPrimitives.ReadUInt64LittleEndian(data[offset..(offset + 8)]);
offset += 8;
payload.Timestamp = BinaryPrimitives.ReadUInt64LittleEndian(data[offset..(offset + 8)]);
offset += 8;

UInt32 extraDataOffset = 32 + BinaryPrimitives.ReadUInt32LittleEndian(data[offset..(offset + 4)]);
offset += 4;
payload.BaseFeePerGas = new(data[offset..(offset + 32)]);
offset += 32;
payload.BlockHash = new(data[offset..(offset + 32)]);
offset += 32;

UInt32 transactionsOffset = 32 + BinaryPrimitives.ReadUInt32LittleEndian(data[offset..(offset + 4)]);
offset += 4;
UInt32 withdrawalsOffset = 32 + BinaryPrimitives.ReadUInt32LittleEndian(data[offset..(offset + 4)]);
offset += 4;

payload.BlobGasUsed = BinaryPrimitives.ReadUInt64LittleEndian(data[offset..(offset + 8)]);
offset += 8;
payload.ExcessBlobGas = BinaryPrimitives.ReadUInt64LittleEndian(data[offset..(offset + 8)]);
offset += 8;
ReadOnlySpan<byte> movingData = data;
payload.ParentBeaconBlockRoot = new(movingData.TakeAndMove(32));
payload.ParentHash = new(movingData.TakeAndMove(32));
payload.FeeRecipient = new(movingData.TakeAndMove(20));
payload.StateRoot = new(movingData.TakeAndMove(32));
payload.ReceiptsRoot = new(movingData.TakeAndMove(32));
payload.LogsBloom = new(movingData.TakeAndMove(256));
payload.PrevRandao = new(movingData.TakeAndMove(32));
payload.BlockNumber = (long)BinaryPrimitives.ReadUInt64LittleEndian(movingData.TakeAndMove(8));
payload.GasLimit = (long)BinaryPrimitives.ReadUInt64LittleEndian(movingData.TakeAndMove(8));
payload.GasUsed = (long)BinaryPrimitives.ReadUInt64LittleEndian(movingData.TakeAndMove(8));
payload.Timestamp = BinaryPrimitives.ReadUInt64LittleEndian(movingData.TakeAndMove(8));
UInt32 extraDataOffset = 32 + BinaryPrimitives.ReadUInt32LittleEndian(movingData.TakeAndMove(4));
payload.BaseFeePerGas = new(movingData.TakeAndMove(32));
payload.BlockHash = new(movingData.TakeAndMove(32));
UInt32 transactionsOffset = 32 + BinaryPrimitives.ReadUInt32LittleEndian(movingData.TakeAndMove(4));
UInt32 withdrawalsOffset = 32 + BinaryPrimitives.ReadUInt32LittleEndian(movingData.TakeAndMove(4));
payload.BlobGasUsed = BinaryPrimitives.ReadUInt64LittleEndian(movingData.TakeAndMove(8));
payload.ExcessBlobGas = BinaryPrimitives.ReadUInt64LittleEndian(movingData.TakeAndMove(8));

if (withdrawalsOffset > data.Length || transactionsOffset >= withdrawalsOffset || extraDataOffset > transactionsOffset || withdrawalsOffset != data.Length)
{
throw new ArgumentException($"Invalid offsets. Data length: {data.Length}, extraData: {extraDataOffset}, transactions: {transactionsOffset}, withdrawals: {withdrawalsOffset}");
}

payload.ExtraData = data[(int)extraDataOffset..(int)transactionsOffset].ToArray();

payload.Transactions = DecodeTransactions(data[(int)transactionsOffset..(int)withdrawalsOffset]);
payload.Withdrawals = Array.Empty<Withdrawal>();
payload.Withdrawals = [];

return payload;
}
Expand Down Expand Up @@ -108,8 +87,5 @@ byte[][] DecodeTransactions(ReadOnlySpan<byte> data)
return txs;
}

public byte[] EncodePayload(ExecutionPayloadV3 payload)
{
throw new System.NotImplementedException();
}
public byte[] EncodePayload(ExecutionPayloadV3 payload) => throw new NotImplementedException();
}

0 comments on commit 68e0aaa

Please sign in to comment.