From 68e0aaa0ba2fbe41c85d12a4241e43f76cad0168 Mon Sep 17 00:00:00 2001 From: "lukasz.rozmej" Date: Thu, 2 Jan 2025 11:19:01 +0100 Subject: [PATCH] Add catch on async void to avoid process crash + refactors --- .../CL/P2P/OptimismCLP2P.cs | 13 ++-- .../CL/P2P/PayloadDecoder.cs | 68 ++++++------------- 2 files changed, 31 insertions(+), 50 deletions(-) diff --git a/src/Nethermind/Nethermind.Optimism/CL/P2P/OptimismCLP2P.cs b/src/Nethermind/Nethermind.Optimism/CL/P2P/OptimismCLP2P.cs index ffb234c6a8b..b333580fab4 100644 --- a/src/Nethermind/Nethermind.Optimism/CL/P2P/OptimismCLP2P.cs +++ b/src/Nethermind/Nethermind.Optimism/CL/P2P/OptimismCLP2P.cs @@ -24,6 +24,7 @@ using Nethermind.Optimism.CL; using Nethermind.Optimism.Rpc; using Nethermind.Core.Crypto; +using Nethermind.JsonRpc; using Snappier; namespace Nethermind.Optimism; @@ -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(); @@ -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; @@ -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) { @@ -142,7 +147,7 @@ private bool TryValidateAndDecodePayload(byte[] msg, [MaybeNullWhen(false)] out private async Task SendNewPayloadToEL(ExecutionPayloadV3 executionPayload) { - var npResult = await _engineRpcModule.engine_newPayloadV3(executionPayload, Array.Empty(), + ResultWrapper npResult = await _engineRpcModule.engine_newPayloadV3(executionPayload, Array.Empty(), executionPayload.ParentBeaconBlockRoot); if (npResult.Result.ResultType == ResultType.Failure) @@ -165,7 +170,7 @@ private async Task SendNewPayloadToEL(ExecutionPayloadV3 executionPayload) private async Task SendForkChoiceUpdatedToEL(Hash256 headBlockHash) { - var fcuResult = await _engineRpcModule.engine_forkchoiceUpdatedV3( + ResultWrapper fcuResult = await _engineRpcModule.engine_forkchoiceUpdatedV3( new ForkchoiceStateV1(headBlockHash, headBlockHash, headBlockHash), null); diff --git a/src/Nethermind/Nethermind.Optimism/CL/P2P/PayloadDecoder.cs b/src/Nethermind/Nethermind.Optimism/CL/P2P/PayloadDecoder.cs index 35b3295258f..2ac55d2463b 100644 --- a/src/Nethermind/Nethermind.Optimism/CL/P2P/PayloadDecoder.cs +++ b/src/Nethermind/Nethermind.Optimism/CL/P2P/PayloadDecoder.cs @@ -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; @@ -27,46 +28,25 @@ public ExecutionPayloadV3 DecodePayload(ReadOnlySpan 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 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) { @@ -74,9 +54,8 @@ public ExecutionPayloadV3 DecodePayload(ReadOnlySpan data) } payload.ExtraData = data[(int)extraDataOffset..(int)transactionsOffset].ToArray(); - payload.Transactions = DecodeTransactions(data[(int)transactionsOffset..(int)withdrawalsOffset]); - payload.Withdrawals = Array.Empty(); + payload.Withdrawals = []; return payload; } @@ -108,8 +87,5 @@ byte[][] DecodeTransactions(ReadOnlySpan data) return txs; } - public byte[] EncodePayload(ExecutionPayloadV3 payload) - { - throw new System.NotImplementedException(); - } + public byte[] EncodePayload(ExecutionPayloadV3 payload) => throw new NotImplementedException(); }