-
Notifications
You must be signed in to change notification settings - Fork 514
/
Copy pathNativePrestateTracer.cs
289 lines (257 loc) · 10.4 KB
/
NativePrestateTracer.cs
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Collections.Generic;
using System.Text.Json;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Int256;
using Nethermind.Serialization.Json;
using Nethermind.State;
namespace Nethermind.Evm.Tracing.GethStyle.Custom.Native.Prestate;
public sealed class NativePrestateTracer : GethLikeNativeTxTracer
{
public const string PrestateTracer = "prestateTracer";
private readonly IWorldState? _worldState;
private readonly Hash256? _txHash;
private TraceMemory _memoryTrace;
private Instruction _op;
private Address? _executingAccount;
private EvmExceptionType? _error;
private readonly Dictionary<AddressAsKey, NativePrestateTracerAccount> _prestate = new();
private readonly Dictionary<AddressAsKey, NativePrestateTracerAccount> _poststate;
private readonly HashSet<AddressAsKey> _createdAccounts;
private readonly HashSet<AddressAsKey> _deletedAccounts;
private readonly bool _diffMode;
public NativePrestateTracer(
IWorldState worldState,
GethTraceOptions options,
Hash256? txHash,
Address? from,
Address? to = null,
Address? beneficiary = null)
: base(options)
{
IsTracingRefunds = true;
IsTracingActions = true;
IsTracingMemory = true;
IsTracingStack = true;
_worldState = worldState;
_txHash = txHash;
NativePrestateTracerConfig config = options.TracerConfig?.Deserialize<NativePrestateTracerConfig>(EthereumJsonSerializer.JsonOptions) ?? new NativePrestateTracerConfig();
_diffMode = config.DiffMode;
if (_diffMode)
{
_poststate = new Dictionary<AddressAsKey, NativePrestateTracerAccount>();
_deletedAccounts = new HashSet<AddressAsKey>();
_createdAccounts = new HashSet<AddressAsKey>();
}
LookupAccount(from!);
LookupAccount(to ?? ContractAddress.From(from, _prestate[from].Nonce ?? 0));
LookupAccount(beneficiary ?? Address.Zero);
}
protected override GethLikeTxTrace CreateTrace() => new();
public override GethLikeTxTrace BuildResult()
{
GethLikeTxTrace result = base.BuildResult();
result.TxHash = _txHash;
result.CustomTracerResult = new GethLikeCustomTrace
{
Value = _diffMode
? new NativePrestateTracerDiffMode { pre = _prestate, post = _poststate }
: _prestate
};
return result;
}
public override void MarkAsSuccess(Address recipient, GasConsumed gasSpent, byte[] output, LogEntry[] logs, Hash256? stateRoot = null)
{
base.MarkAsSuccess(recipient, gasSpent, output, logs, stateRoot);
if (_diffMode)
ProcessDiffState();
}
public override void MarkAsFailed(Address recipient, GasConsumed gasSpent, byte[] output, string? error, Hash256? stateRoot = null)
{
base.MarkAsFailed(recipient, gasSpent, output, error, stateRoot);
if (_diffMode)
ProcessDiffState();
}
public override void StartOperation(int pc, Instruction opcode, long gas, in ExecutionEnvironment env)
{
base.StartOperation(pc, opcode, gas, env);
if (_error is not null) return;
_op = opcode;
_executingAccount = env.ExecutingAccount;
}
public override void SetOperationMemory(TraceMemory memoryTrace)
{
base.SetOperationMemory(memoryTrace);
_memoryTrace = memoryTrace;
}
public override void SetOperationStack(TraceStack stack)
{
base.SetOperationStack(stack);
int stackLen = stack.Count;
Address address;
switch (_op)
{
case Instruction.SLOAD:
case Instruction.SSTORE:
if (stackLen >= 1)
{
UInt256 index = stack.PeekUInt256(0);
LookupStorage(_executingAccount!, index);
}
break;
case Instruction.EXTCODECOPY:
case Instruction.EXTCODEHASH:
case Instruction.EXTCODESIZE:
case Instruction.BALANCE:
case Instruction.SELFDESTRUCT:
if (stackLen >= 1)
{
address = stack.PeekAddress(0);
LookupAccount(address);
if (_diffMode && _op == Instruction.SELFDESTRUCT)
_deletedAccounts.Add(address);
}
break;
case Instruction.DELEGATECALL:
case Instruction.CALL:
case Instruction.STATICCALL:
case Instruction.CALLCODE:
if (stackLen >= 5)
{
address = stack.PeekAddress(1);
LookupAccount(address);
}
break;
case Instruction.CREATE2:
if (stackLen >= 4)
{
try
{
int offset = stack.Peek(1).ReadEthInt32();
int length = stack.Peek(2).ReadEthInt32();
ReadOnlySpan<byte> initCode = _memoryTrace.Slice(offset, length);
ReadOnlySpan<byte> salt = stack.Peek(3);
address = ContractAddress.From(_executingAccount!, salt, initCode);
LookupAccount(address);
if (_diffMode)
_createdAccounts.Add(address);
}
catch
{
/*
* This operation error will be recorded in ReportOperationError and all
* subsequent operations will be ignored from the prestate trace
*/
}
}
break;
case Instruction.CREATE:
UInt256 nonce = _worldState!.GetNonce(_executingAccount!);
address = ContractAddress.From(_executingAccount, nonce);
LookupAccount(address!);
if (_diffMode)
_createdAccounts.Add(address);
break;
}
}
public override void ReportOperationError(EvmExceptionType error)
{
base.ReportOperationError(error);
_error = error;
}
private void LookupAccount(Address addr)
{
if (!_prestate.ContainsKey(addr))
{
if (_worldState!.TryGetAccount(addr, out AccountStruct account))
{
UInt256 nonce = account.Nonce;
byte[]? code = _worldState.GetCode(addr);
_prestate.Add(addr, new NativePrestateTracerAccount(account.Balance, nonce, code));
}
else
{
_prestate.Add(addr, new NativePrestateTracerAccount
{
Balance = UInt256.Zero
});
}
}
}
private void LookupStorage(Address addr, UInt256 index)
{
NativePrestateTracerAccount account = _prestate[addr];
account.Storage ??= new Dictionary<UInt256, UInt256>();
if (!account.Storage.ContainsKey(index))
{
UInt256 storage = new(_worldState!.Get(new StorageCell(addr, index)).BytesWithNoLeadingZeroes, true);
account.Storage.Add(index, storage);
}
}
private void ProcessDiffState()
{
foreach ((AddressAsKey addr, NativePrestateTracerAccount prestateAccount) in _prestate)
{
// If an account was deleted then don't show it in the postState trace
if (_deletedAccounts.Contains(addr))
continue;
_worldState!.TryGetAccount(addr, out AccountStruct poststateAccountStruct);
NativePrestateTracerAccount poststateAccount = new NativePrestateTracerAccount(
poststateAccountStruct.Balance,
poststateAccountStruct.Nonce,
_worldState.GetCode(addr));
NativePrestateTracerAccount? diffAccount = new NativePrestateTracerAccount();
bool modified = false;
if (!poststateAccount.Balance.Equals(prestateAccount.Balance))
{
modified = true;
diffAccount.Balance = poststateAccount.Balance;
}
if (!poststateAccount.Nonce.Equals(prestateAccount.Nonce))
{
modified = true;
diffAccount.Nonce = poststateAccount.Nonce;
}
if (!Bytes.NullableEqualityComparer.Equals(poststateAccount.Code, prestateAccount.Code))
{
modified = true;
diffAccount.Code = poststateAccount.Code;
}
if (prestateAccount.Storage is not null)
{
foreach ((UInt256 index, UInt256 prestateStorage) in prestateAccount.Storage)
{
// Remove any empty slots from the state diff
if (prestateStorage.IsZero)
prestateAccount.Storage.Remove(index);
UInt256 poststateStorage = new(_worldState!.Get(new StorageCell(addr, index)).BytesWithNoLeadingZeroes, true);
if (!prestateStorage.Equals(poststateStorage))
{
modified = true;
if (!poststateStorage.IsZero)
{
diffAccount.Storage ??= new Dictionary<UInt256, UInt256>();
diffAccount.Storage.Add(index, poststateStorage);
}
}
else
{
// Remove the storage slot from the prestate trace if it wasn't modified
prestateAccount.Storage.Remove(index);
}
}
}
// If any account fields were modified then add the account to the poststate trace
if (modified)
_poststate.Add(addr, diffAccount);
// If no account fields were modified or the account was created then remove it from the prestate trace
if (!modified || _createdAccounts.Contains(addr))
_prestate.Remove(addr);
}
}
}