-
Notifications
You must be signed in to change notification settings - Fork 514
/
Copy pathVirtualMachineTestsBase.cs
396 lines (341 loc) · 17.4 KB
/
VirtualMachineTestsBase.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Numerics;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Core.Specs;
using Nethermind.Specs;
using Nethermind.Core.Test.Builders;
using Nethermind.Crypto;
using Nethermind.Db;
using Nethermind.Int256;
using Nethermind.Evm.Tracing;
using Nethermind.Evm.Tracing.GethStyle;
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Logging;
using Nethermind.State;
using Nethermind.Trie.Pruning;
using NUnit.Framework;
namespace Nethermind.Evm.Test;
public class VirtualMachineTestsBase
{
protected const string SampleHexData1 = "a01234";
protected const string SampleHexData2 = "b15678";
protected const string HexZero = "00";
protected const long DefaultBlockGasLimit = 8000000;
private IEthereumEcdsa _ethereumEcdsa;
protected ITransactionProcessor _processor;
private IDb _stateDb;
protected VirtualMachine Machine { get; private set; }
protected CodeInfoRepository CodeInfoRepository { get; private set; }
protected IWorldState TestState { get; private set; }
protected static Address Contract { get; } = new("0xd75a3a95360e44a3874e691fb48d77855f127069");
protected static Address Sender { get; } = TestItem.AddressA;
protected static Address Recipient { get; } = TestItem.AddressB;
protected static Address Miner { get; } = TestItem.AddressD;
protected static PrivateKey SenderKey { get; } = TestItem.PrivateKeyA;
protected static PrivateKey RecipientKey { get; } = TestItem.PrivateKeyB;
protected static PrivateKey MinerKey { get; } = TestItem.PrivateKeyD;
protected virtual ForkActivation Activation => (BlockNumber, Timestamp);
protected virtual long BlockNumber { get; } = MainnetSpecProvider.ByzantiumBlockNumber;
protected virtual ulong Timestamp => 0UL;
protected virtual ISpecProvider SpecProvider => MainnetSpecProvider.Instance;
protected IReleaseSpec Spec => SpecProvider.GetSpec(Activation);
protected virtual ILogManager GetLogManager()
{
return LimboLogs.Instance;
}
[SetUp]
public virtual void Setup()
{
ILogManager logManager = GetLogManager();
IDb codeDb = new MemDb();
_stateDb = new MemDb();
ITrieStore trieStore = new TrieStore(_stateDb, logManager);
TestState = new WorldState(trieStore, codeDb, logManager);
_ethereumEcdsa = new EthereumEcdsa(SpecProvider.ChainId);
IBlockhashProvider blockhashProvider = new TestBlockhashProvider(SpecProvider);
CodeInfoRepository = new CodeInfoRepository();
Machine = new VirtualMachine(blockhashProvider, SpecProvider, CodeInfoRepository, logManager);
_processor = new TransactionProcessor(SpecProvider, TestState, Machine, CodeInfoRepository, logManager);
}
[TearDown]
public virtual void TearDown() => _stateDb?.Dispose();
protected GethLikeTxTrace ExecuteAndTrace(params byte[] code)
{
(Block block, Transaction transaction) = PrepareTx(Activation, 100000, code);
GethLikeTxMemoryTracer tracer = new(transaction, GethTraceOptions.Default with { EnableMemory = true });
_processor.Execute(transaction, new BlockExecutionContext(block.Header, Spec), tracer);
return tracer.BuildResult();
}
protected GethLikeTxTrace ExecuteAndTrace(long blockNumber, long gasLimit, params byte[] code)
{
(Block block, Transaction transaction) = PrepareTx((blockNumber, Timestamp), gasLimit, code);
GethLikeTxMemoryTracer tracer = new(transaction, GethTraceOptions.Default);
_processor.Execute(transaction, new BlockExecutionContext(block.Header, Spec), tracer);
return tracer.BuildResult();
}
protected GethLikeTxTrace ExecuteAndTrace(long gasLimit, params byte[] code)
{
(Block block, Transaction transaction) = PrepareTx(Activation, gasLimit, code);
GethLikeTxMemoryTracer tracer = new(transaction, GethTraceOptions.Default);
_processor.Execute(transaction, new BlockExecutionContext(block.Header, Spec), tracer);
return tracer.BuildResult();
}
protected GethLikeTxTrace ExecuteAndTraceToFile(Action<GethTxFileTraceEntry> dumpCallback, byte[] code, GethTraceOptions options)
{
(Block block, Transaction transaction) = PrepareTx(Activation, 100000, code);
GethLikeTxFileTracer tracer = new(dumpCallback, options);
_processor.Execute(transaction, new BlockExecutionContext(block.Header, Spec), tracer);
return tracer.BuildResult();
}
/// <summary>
/// deprecated. Please use activation instead of blockNumber.
/// </summary>
protected TestAllTracerWithOutput Execute(long blockNumber, params byte[] code)
{
return Execute((blockNumber, Timestamp), code);
}
protected TestAllTracerWithOutput Execute(ForkActivation activation, params byte[] code)
{
(Block block, Transaction transaction) = PrepareTx(activation, 100000, code);
TestAllTracerWithOutput tracer = CreateTracer();
_processor.Execute(transaction, new BlockExecutionContext(block.Header, Spec), tracer);
return tracer;
}
protected TestAllTracerWithOutput Execute(ForkActivation activation, Transaction tx)
{
(Block block, _) = PrepareTx(activation, 100000, null);
TestAllTracerWithOutput tracer = CreateTracer();
_processor.Execute(tx, new BlockExecutionContext(block.Header, Spec), tracer);
return tracer;
}
protected TestAllTracerWithOutput Execute(params byte[] code)
{
return Execute(Activation, code);
}
protected TestAllTracerWithOutput Execute(Transaction tx)
{
return Execute(Activation, tx);
}
protected virtual TestAllTracerWithOutput CreateTracer() => new();
protected T ExecuteBlock<T>(T tracer, byte[] code, ForkActivation? forkActivation = null) where T : IBlockTracer
{
(Block block, Transaction transaction) = PrepareTx(forkActivation ?? Activation, 100000, code);
tracer.StartNewBlockTrace(block);
ITxTracer txTracer = tracer.StartNewTxTrace(transaction);
_processor.Execute(transaction, new BlockExecutionContext(block.Header, Spec), txTracer);
tracer.EndTxTrace();
tracer.EndBlockTrace();
return tracer;
}
protected T Execute<T>(T tracer, byte[] code, ForkActivation? forkActivation = null) where T : ITxTracer
{
(Block block, Transaction transaction) = PrepareTx(forkActivation ?? Activation, 100000, code);
_processor.Execute(transaction, new BlockExecutionContext(block.Header, Spec), tracer);
return tracer;
}
/// <summary>
/// deprecated. Please use activation instead of blockNumber.
/// </summary>
protected TestAllTracerWithOutput Execute(long blockNumber, long gasLimit, byte[] code,
long blockGasLimit = DefaultBlockGasLimit, byte[][] blobVersionedHashes = null)
{
(Block block, Transaction transaction) = PrepareTx((blockNumber, Timestamp), gasLimit, code,
blockGasLimit: blockGasLimit, blobVersionedHashes: blobVersionedHashes);
TestAllTracerWithOutput tracer = CreateTracer();
_processor.Execute(transaction, new BlockExecutionContext(block.Header, Spec), tracer);
return tracer;
}
protected TestAllTracerWithOutput Execute(ForkActivation activation, long gasLimit, byte[] code,
long blockGasLimit = DefaultBlockGasLimit, byte[][] blobVersionedHashes = null)
{
(Block block, Transaction transaction) = PrepareTx(activation, gasLimit, code,
blockGasLimit: blockGasLimit, blobVersionedHashes: blobVersionedHashes);
TestAllTracerWithOutput tracer = CreateTracer();
_processor.Execute(transaction, new BlockExecutionContext(block.Header, Spec), tracer);
return tracer;
}
/// <summary>
/// deprecated. Please use activation instead of blockNumber.
/// </summary>
protected (Block block, Transaction transaction) PrepareTx(
long blockNumber,
long gasLimit,
byte[]? code = null,
SenderRecipientAndMiner? senderRecipientAndMiner = null,
int value = 1,
long blockGasLimit = DefaultBlockGasLimit,
byte[][]? blobVersionedHashes = null,
ulong excessBlobGas = 0)
{
return PrepareTx((blockNumber, Timestamp), gasLimit, code, senderRecipientAndMiner, value, blockGasLimit, blobVersionedHashes, excessBlobGas);
}
protected (Block block, Transaction transaction) PrepareTx(
ForkActivation activation,
long gasLimit,
byte[]? code = null,
SenderRecipientAndMiner? senderRecipientAndMiner = null,
int value = 1,
long blockGasLimit = DefaultBlockGasLimit,
byte[][]? blobVersionedHashes = null,
ulong excessBlobGas = 0,
Transaction transaction = null)
{
senderRecipientAndMiner ??= SenderRecipientAndMiner.Default;
// checking if account exists - because creating new accounts overwrites already existing accounts,
// thus overwriting storage roots - essentially clearing the storage slots
// earlier it used to work - because the cache mapping address:storageTree was never cleared on account of
// TestState.CommitTrees() not being called. But now the WorldState.CommitTrees which also calls TestState.CommitTrees, clearing the cache.
if (!TestState.AccountExists(senderRecipientAndMiner.Sender))
TestState.CreateAccount(senderRecipientAndMiner.Sender, 100.Ether());
else
TestState.AddToBalance(senderRecipientAndMiner.Sender, 100.Ether(), SpecProvider.GenesisSpec);
if (!TestState.AccountExists(senderRecipientAndMiner.Recipient))
TestState.CreateAccount(senderRecipientAndMiner.Recipient, 100.Ether());
else
TestState.AddToBalance(senderRecipientAndMiner.Recipient, 100.Ether(), SpecProvider.GenesisSpec);
if (code is not null)
{
TestState.InsertCode(senderRecipientAndMiner.Recipient, code, SpecProvider.GenesisSpec);
}
GetLogManager().GetClassLogger().Debug("Committing initial state");
TestState.Commit(SpecProvider.GenesisSpec);
GetLogManager().GetClassLogger().Debug("Committed initial state");
GetLogManager().GetClassLogger().Debug("Committing initial tree");
TestState.CommitTree(0);
GetLogManager().GetClassLogger().Debug("Committed initial tree");
transaction ??= Build.A.Transaction
.WithGasLimit(gasLimit)
.WithGasPrice(1)
.WithValue(value)
.WithBlobVersionedHashes(blobVersionedHashes)
.WithNonce(TestState.GetNonce(senderRecipientAndMiner.Sender))
.To(senderRecipientAndMiner.Recipient)
.SignedAndResolved(_ethereumEcdsa, senderRecipientAndMiner.SenderKey)
.TestObject;
Block block = BuildBlock(activation, senderRecipientAndMiner, transaction, blockGasLimit, excessBlobGas);
return (block, transaction);
}
/// <summary>
/// deprecated. Please use activation instead of blockNumber.
/// </summary>
protected (Block block, Transaction transaction) PrepareTx(long blockNumber, long gasLimit, byte[] code,
byte[] input, UInt256 value, SenderRecipientAndMiner senderRecipientAndMiner = null)
{
return PrepareTx((blockNumber, Timestamp), gasLimit, code, input, value, senderRecipientAndMiner);
}
protected (Block block, Transaction transaction) PrepareTx(ForkActivation activation, long gasLimit, byte[] code,
byte[] input, UInt256 value, SenderRecipientAndMiner senderRecipientAndMiner = null)
{
senderRecipientAndMiner ??= SenderRecipientAndMiner.Default;
// checking if account exists - because creating new accounts overwrites already existing accounts,
// thus overwriting storage roots - essentially clearing the storage slots
// earlier it used to work - because the cache mapping address:storageTree was never cleared on account of
// TestState.CommitTrees() not being called. But now the WorldState.CommitTrees which also calls TestState.CommitTrees, clearing the cache.
if (!TestState.AccountExists(senderRecipientAndMiner.Sender))
TestState.CreateAccount(senderRecipientAndMiner.Sender, 100.Ether());
else
TestState.AddToBalance(senderRecipientAndMiner.Sender, 100.Ether(), SpecProvider.GenesisSpec);
if (!TestState.AccountExists(senderRecipientAndMiner.Recipient))
TestState.CreateAccount(senderRecipientAndMiner.Recipient, 100.Ether());
else
TestState.AddToBalance(senderRecipientAndMiner.Recipient, 100.Ether(), SpecProvider.GenesisSpec);
TestState.InsertCode(senderRecipientAndMiner.Recipient, code, SpecProvider.GenesisSpec);
TestState.Commit(SpecProvider.GenesisSpec);
Transaction transaction = Build.A.Transaction
.WithGasLimit(gasLimit)
.WithGasPrice(1)
.WithNonce(TestState.GetNonce(senderRecipientAndMiner.Sender))
.WithData(input)
.WithValue(value)
.To(senderRecipientAndMiner.Recipient)
.SignedAndResolved(_ethereumEcdsa, senderRecipientAndMiner.SenderKey)
.TestObject;
Block block = BuildBlock(activation, senderRecipientAndMiner);
return (block, transaction);
}
protected (Block block, Transaction transaction) PrepareInitTx(ForkActivation activation, long gasLimit, byte[] code,
SenderRecipientAndMiner senderRecipientAndMiner = null)
{
senderRecipientAndMiner ??= SenderRecipientAndMiner.Default;
TestState.CreateAccount(senderRecipientAndMiner.Sender, 100.Ether());
TestState.Commit(SpecProvider.GenesisSpec);
Transaction transaction = Build.A.Transaction
.WithTo(null)
.WithGasLimit(gasLimit)
.WithGasPrice(1)
.WithCode(code)
.SignedAndResolved(_ethereumEcdsa, senderRecipientAndMiner.SenderKey)
.TestObject;
Block block = BuildBlock(activation, senderRecipientAndMiner);
return (block, transaction);
}
protected Block BuildBlock(ForkActivation activation, SenderRecipientAndMiner senderRecipientAndMiner)
{
return BuildBlock(activation, senderRecipientAndMiner, null);
}
protected virtual Block BuildBlock(ForkActivation activation, SenderRecipientAndMiner senderRecipientAndMiner,
Transaction tx, long blockGasLimit = DefaultBlockGasLimit, ulong excessBlobGas = 0)
{
senderRecipientAndMiner ??= SenderRecipientAndMiner.Default;
return Build.A.Block.WithNumber(activation.BlockNumber)
.WithTimestamp(activation.Timestamp ?? 0)
.WithTransactions(tx is null ? [] : new[] { tx })
.WithGasLimit(blockGasLimit)
.WithBeneficiary(senderRecipientAndMiner.Miner)
.WithBlobGasUsed(0)
.WithExcessBlobGas(0)
.WithParentBeaconBlockRoot(TestItem.KeccakG)
.WithExcessBlobGas(excessBlobGas)
.TestObject;
}
protected void AssertGas(TestAllTracerWithOutput receipt, long gas)
{
Assert.That(receipt.GasSpent, Is.EqualTo(gas), "gas");
}
protected void AssertStorage(UInt256 address, Address value)
{
Assert.That(TestState.Get(new StorageCell(Recipient, address)).Bytes.ToArray(), Is.EqualTo(value.Bytes.PadLeft(32)), "storage");
}
protected void AssertStorage(UInt256 address, Hash256 value)
{
Assert.That(TestState.Get(new StorageCell(Recipient, address)).Bytes.ToArray(), Is.EqualTo(value.BytesToArray()), "storage");
}
protected void AssertStorage(UInt256 address, ReadOnlySpan<byte> value)
{
Assert.That(TestState.Get(new StorageCell(Recipient, address)).Bytes.ToArray(), Is.EqualTo(new ZeroPaddedSpan(value, 32 - value.Length, PadDirection.Left).ToArray()), "storage");
}
protected void AssertStorage(UInt256 address, BigInteger expectedValue)
{
byte[] actualValue = TestState.Get(new StorageCell(Recipient, address)).ToArray();
byte[] expected = expectedValue < 0 ? expectedValue.ToBigEndianByteArray(32) : expectedValue.ToBigEndianByteArray();
Assert.That(actualValue, Is.EqualTo(expected), "storage");
}
protected void AssertStorage(UInt256 address, UInt256 expectedValue)
{
byte[] bytes = ((BigInteger)expectedValue).ToBigEndianByteArray();
byte[] actualValue = TestState.Get(new StorageCell(Recipient, address)).ToArray();
Assert.That(actualValue, Is.EqualTo(bytes), "storage");
}
private static int _callIndex = -1;
protected void AssertStorage(StorageCell storageCell, UInt256 expectedValue)
{
_callIndex++;
if (!TestState.AccountExists(storageCell.Address))
{
Assert.That(new byte[] { 0 }, Is.EqualTo(expectedValue.ToBigEndian().WithoutLeadingZeros().ToArray()), $"storage {storageCell}, call {_callIndex}");
}
else
{
byte[] actualValue = TestState.Get(storageCell).ToArray();
Assert.That(actualValue, Is.EqualTo(expectedValue.ToBigEndian().WithoutLeadingZeros().ToArray()), $"storage {storageCell}, call {_callIndex}");
}
}
protected void AssertCodeHash(Address address, Hash256 codeHash)
{
Assert.That(TestState.GetCodeHash(address), Is.EqualTo(codeHash), "code hash");
}
}