-
Notifications
You must be signed in to change notification settings - Fork 713
Expand file tree
/
Copy pathStructLogEnvelopeWriter.cs
More file actions
156 lines (135 loc) · 5.83 KB
/
Copy pathStructLogEnvelopeWriter.cs
File metadata and controls
156 lines (135 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.IO.Pipelines;
using System.Text.Json;
using System.Threading;
using Nethermind.Blockchain;
using Nethermind.Blockchain.Tracing.GethStyle;
using Nethermind.Core.Exceptions;
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Logging;
using Nethermind.Serialization.Json;
using Nethermind.State;
using ErrorType = Nethermind.Evm.TransactionProcessing.TransactionResult.ErrorType;
namespace Nethermind.JsonRpc.Modules.DebugModule;
/// <summary>
/// Shared envelope writer for streaming Geth-style struct-log traces. Single-tx and
/// bundle (debug_traceCallMany) results both use this to keep the per-tx output format
/// byte-identical and the failure-handling path DRY.
/// </summary>
internal static class StructLogEnvelopeWriter
{
public static void EmitTraceObject(
Utf8JsonWriter writer,
PipeWriter? pipeWriter,
CancellationToken cancellationToken,
Func<Utf8JsonWriter, PipeWriter?, CancellationToken, GethLikeTxTrace?> runTrace,
ILogger logger,
ulong fallbackGas = 0UL)
{
GethLikeTxTrace? trace = null;
Exception? failure = null;
writer.WriteStartObject();
writer.WritePropertyName("structLogs"u8);
writer.WriteStartArray();
try
{
trace = runTrace(writer, pipeWriter, cancellationToken);
}
catch (Exception ex) when (!cancellationToken.IsCancellationRequested)
{
failure = ex;
}
finally
{
writer.WriteEndArray();
string? errorMessage;
int errorCode;
if (failure is not null)
{
errorMessage = FormatErrorMessage(failure);
errorCode = ResolveErrorCode(failure);
}
else if (trace is null)
{
errorMessage = "tracing failed: trace not found";
errorCode = ErrorCodes.ResourceNotFound;
}
else
{
errorMessage = null;
errorCode = default;
}
WriteFooter(writer, trace, errorMessage, errorCode, fallbackGas);
}
LogFailure(logger, failure);
}
/// <summary>
/// Writes a failure trace envelope for the case where the trace was never invoked
/// (e.g. tx couldn't even be constructed from RPC input). No structLogs are emitted.
/// </summary>
public static void EmitFailedTrace(Utf8JsonWriter writer, ulong gasLimit, string? errorMessage = null)
{
writer.WriteStartObject();
writer.WritePropertyName("structLogs"u8);
writer.WriteStartArray();
writer.WriteEndArray();
string? formattedMessage = errorMessage is null ? null : $"tracing failed: {errorMessage}";
WriteFooter(writer, trace: null, formattedMessage, ErrorCodes.InvalidInput, fallbackGas: gasLimit);
}
internal static int ResolveErrorCode(Exception failure)
{
ArgumentNullException.ThrowIfNull(failure);
for (Exception? current = failure; current is not null; current = current.InnerException)
{
if (current is InsufficientBalanceException or InvalidBlockException or InvalidTransactionException) return ErrorCodes.InvalidInput;
}
return ErrorCodes.InternalError;
}
internal static string FormatErrorMessage(Exception failure) => failure switch
{
InvalidTransactionException tx => $"tracing failed: {FormatErrorDescription(tx.Reason)}",
_ => $"tracing failed: {failure.Message}",
};
private static string FormatErrorDescription(TransactionResult result)
{
string detail = result.ErrorDescription;
return result.Error switch
{
ErrorType.InsufficientMaxFeePerGasForSenderBalance => detail,
ErrorType.SenderHasDeployedCode => ReplacePrefix(detail, "sender has deployed code", "sender not an eoa"),
ErrorType.NonceOverflow => ReplacePrefix(detail, "nonce overflow", "nonce has max value"),
ErrorType.MinerPremiumNegative => ReplacePrefix(detail, "miner premium is negative", "max priority fee per gas higher than max fee per gas"),
ErrorType.TransactionSizeOverMaxInitCodeSize => ReplacePrefix(detail, "EIP-3860 - transaction size over max init code size", "max initcode size exceeded"),
ErrorType.BlockGasLimitExceeded => ReplacePrefix(detail, "Block gas limit exceeded", "exceeds block gas limit"),
_ => detail,
};
}
private static string ReplacePrefix(string s, string oldPrefix, string newPrefix)
=> s.StartsWith(oldPrefix, StringComparison.Ordinal) ? newPrefix + s[oldPrefix.Length..] : s;
private static void WriteFooter(Utf8JsonWriter writer, GethLikeTxTrace? trace, string? errorMessage, int errorCode, ulong fallbackGas)
{
ulong gas = trace?.Gas ?? fallbackGas;
bool failed = errorMessage is not null || trace is null || trace.Failed;
byte[] returnValue = trace?.ReturnValue ?? [];
writer.WriteNumber("gas"u8, gas);
writer.WritePropertyName("failed"u8);
writer.WriteBooleanValue(failed);
writer.WritePropertyName("returnValue"u8);
ByteArrayConverter.Convert(writer, returnValue, skipLeadingZeros: false);
if (errorMessage is not null)
{
writer.WritePropertyName("error"u8);
writer.WriteStringValue(errorMessage);
writer.WritePropertyName("errorCode"u8);
writer.WriteNumberValue(errorCode);
}
writer.WriteEndObject();
}
private static void LogFailure(ILogger logger, Exception? failure)
{
if (failure is null) return;
if (logger.IsWarn) logger.Warn($"debug_trace streaming failed mid-response: {failure}");
}
}