-
Notifications
You must be signed in to change notification settings - Fork 694
Expand file tree
/
Copy pathEthSimulateTestsBlocksAndTransactions.cs
More file actions
842 lines (745 loc) · 36.1 KB
/
EthSimulateTestsBlocksAndTransactions.cs
File metadata and controls
842 lines (745 loc) · 36.1 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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Nethermind.Blockchain.Find;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Core.Test.Builders;
using Nethermind.Crypto;
using Nethermind.Evm;
using Nethermind.Facade.Eth.RpcTransaction;
using Nethermind.Facade.Proxy.Models.Simulate;
using Nethermind.Facade.Simulate;
using Nethermind.Int256;
using Nethermind.JsonRpc.Modules.Eth;
using Nethermind.Serialization.Json;
using Nethermind.Specs;
using Nethermind.Specs.Forks;
using Nethermind.Specs.Test;
using NUnit.Framework;
using ResultType = Nethermind.Facade.Proxy.Models.Simulate.ResultType;
namespace Nethermind.JsonRpc.Test.Modules.Eth;
public class EthSimulateTestsBlocksAndTransactions
{
public static SimulatePayload<TransactionForRpc> CreateSerializationPayload(TestRpcBlockchain chain)
{
UInt256 nonceA = chain.ReadOnlyState.GetNonce(TestItem.AddressA);
Transaction txToFail = GetTransferTxData(nonceA, chain.EthereumEcdsa, TestItem.PrivateKeyA, TestItem.AddressB, 10_000_000);
UInt256 nextNonceA = ++nonceA;
Transaction tx = GetTransferTxData(nextNonceA, chain.EthereumEcdsa, TestItem.PrivateKeyA, TestItem.AddressB, 4_000_000);
return new()
{
BlockStateCalls =
[
new()
{
BlockOverrides = new BlockOverride { Number = 10, BaseFeePerGas = 0 },
Calls = [ToRpcForInput(txToFail), ToRpcForInput(tx)],
StateOverrides = new Dictionary<Address, AccountOverride>
{
{ TestItem.AddressA, new AccountOverride { Balance = 2100.Ether } }
}
}
],
TraceTransfers = true,
Validation = true
};
}
public static SimulatePayload<TransactionForRpc> CreateEthMovedPayload(TestRpcBlockchain chain, UInt256 nonceA)
{
Transaction txAtoB1 = GetTransferTxData(nonceA + 1, chain.EthereumEcdsa, TestItem.PrivateKeyA, TestItem.AddressB, 1);
Transaction txAtoB2 = GetTransferTxData(nonceA + 2, chain.EthereumEcdsa, TestItem.PrivateKeyA, TestItem.AddressB, 1);
Transaction txAtoB3 = GetTransferTxData(nonceA + 3, chain.EthereumEcdsa, TestItem.PrivateKeyA, TestItem.AddressB, 1);
Transaction txAtoB4 = GetTransferTxData(nonceA + 4, chain.EthereumEcdsa, TestItem.PrivateKeyA, TestItem.AddressB, 1);
return new()
{
BlockStateCalls = new List<BlockStateCall<TransactionForRpc>>
{
new()
{
BlockOverrides =
new BlockOverride
{
Number = (ulong)chain.BlockFinder.Head!.Number+2,
GasLimit = 5_000_000,
FeeRecipient = TestItem.AddressC,
BaseFeePerGas = 0
},
Calls = [ToRpcForInput(txAtoB1), ToRpcForInput(txAtoB2)]
},
new()
{
BlockOverrides =
new BlockOverride
{
Number = (ulong)checked(chain.Bridge.HeadBlock.Number + 10),
GasLimit = 5_000_000,
FeeRecipient = TestItem.AddressC,
BaseFeePerGas = 0
},
Calls = [ToRpcForInput(txAtoB3), ToRpcForInput(txAtoB4)]
}
},
TraceTransfers = true
};
}
// Helper to convert Transaction to RPC format suitable for input (clears GasPrice for EIP-1559+ to avoid ambiguity)
private static TransactionForRpc ToRpcForInput(Transaction tx)
{
TransactionForRpc rpc = TransactionForRpc.FromTransaction(tx);
if (rpc is EIP1559TransactionForRpc eip1559Rpc)
eip1559Rpc.GasPrice = null;
return rpc;
}
public static SimulatePayload<TransactionForRpc> CreateTransactionsForcedFail(TestRpcBlockchain chain, UInt256 nonceA)
{
//shall be Ok
Transaction txAtoB1 =
GetTransferTxData(nonceA + 1, chain.EthereumEcdsa, TestItem.PrivateKeyA, TestItem.AddressB, 1);
//shall fail
Transaction txAtoB2 =
GetTransferTxData(nonceA + 2, chain.EthereumEcdsa, TestItem.PrivateKeyA, TestItem.AddressB, UInt256.MaxValue);
LegacyTransactionForRpc transactionForRpc = (LegacyTransactionForRpc)ToRpcForInput(txAtoB2);
transactionForRpc.Nonce = null;
LegacyTransactionForRpc transactionForRpc2 = (LegacyTransactionForRpc)ToRpcForInput(txAtoB1);
transactionForRpc2.Nonce = null;
return new()
{
BlockStateCalls = new List<BlockStateCall<TransactionForRpc>>
{
new()
{
BlockOverrides =
new BlockOverride
{
Number = (ulong)checked(chain.Bridge.HeadBlock.Number + 10),
GasLimit = 5_000_000,
FeeRecipient = TestItem.AddressC,
BaseFeePerGas = 0
},
Calls = [transactionForRpc2]
},
new()
{
BlockOverrides =
new BlockOverride
{
Number = 123,
GasLimit = 5_000_000,
FeeRecipient = TestItem.AddressC,
BaseFeePerGas = 0
},
Calls = new[] { transactionForRpc }
}
},
TraceTransfers = true,
Validation = true
};
}
public static Transaction GetTransferTxData(UInt256 nonce, IEthereumEcdsa ethereumEcdsa, PrivateKey from, Address to, UInt256 amount, TxType type = TxType.EIP1559)
{
Transaction tx = new()
{
Type = type,
Value = amount,
Nonce = nonce,
GasLimit = 50_000,
SenderAddress = from.Address,
To = to,
GasPrice = 20.GWei,
DecodedMaxFeePerGas = type >= TxType.EIP1559 ? 20.GWei : 0
};
ethereumEcdsa.Sign(from, tx);
tx.Hash = tx.CalculateHash();
return tx;
}
[Test]
public async Task Test_eth_simulate_serialization()
{
TestRpcBlockchain chain = await EthRpcSimulateTestsBase.CreateChain();
SimulatePayload<TransactionForRpc> payload = CreateSerializationPayload(chain);
//Force persistence of head block in main chain
chain.BlockTree.UpdateMainChain(new List<Block> { chain.BlockFinder.Head! }, true, true);
chain.BlockTree.UpdateHeadBlock(chain.BlockFinder.Head!.Hash!);
//will mock our GetCachedCodeInfo function - it shall be called 3 times if redirect is working, 2 times if not
SimulateTxExecutor<SimulateCallResult> executor = new(chain.Bridge, chain.BlockFinder, new JsonRpcConfig(), chain.SpecProvider, new SimulateBlockMutatorTracerFactory());
ResultWrapper<IReadOnlyList<SimulateBlockResult<SimulateCallResult>>> result = executor.Execute(payload, BlockParameter.Latest);
IReadOnlyList<SimulateBlockResult<SimulateCallResult>> data = result.Data;
Assert.That((bool)result.Result, Is.EqualTo(true), result.Result.ToString());
Assert.That(data, Has.Count.EqualTo(7));
SimulateBlockResult<SimulateCallResult> blockResult = data.Last();
blockResult.Calls.Select(static c => c.Status).Should().BeEquivalentTo(new[] { (ulong)ResultType.Success, (ulong)ResultType.Success });
blockResult.Calls.Should().OnlyContain(static c => c.MaxUsedGas.HasValue && c.GasUsed.HasValue && c.MaxUsedGas.Value >= c.GasUsed.Value);
}
/// <summary>
/// This test verifies that a temporary forked blockchain can make transactions, blocks and report on them
/// We test on blocks before current head and after it,
/// Note that if we get blocks before head we set simulation start state to one of that first block
/// </summary>
[Test]
public async Task Test_eth_simulate_eth_moved()
{
TestRpcBlockchain chain = await EthRpcSimulateTestsBase.CreateChain();
UInt256 nonceA = chain.ReadOnlyState.GetNonce(TestItem.AddressA);
Transaction txMainnetAtoB = GetTransferTxData(nonceA, chain.EthereumEcdsa, TestItem.PrivateKeyA, TestItem.AddressB, 1, type: TxType.Legacy);
SimulatePayload<TransactionForRpc> payload = CreateEthMovedPayload(chain, nonceA);
//Test that transfer tx works on mainchain
UInt256 before = chain.ReadOnlyState.GetBalance(TestItem.AddressA);
await chain.AddBlock(txMainnetAtoB);
UInt256 after = chain.ReadOnlyState.GetBalance(TestItem.AddressA);
Assert.That(after, Is.LessThan(before));
chain.Bridge.GetReceipt(txMainnetAtoB.Hash!);
//Force persistence of head block in main chain
chain.BlockTree.UpdateMainChain(new List<Block> { chain.BlockFinder.Head! }, true, true);
chain.BlockTree.UpdateHeadBlock(chain.BlockFinder.Head!.Hash!);
//will mock our GetCachedCodeInfo function - it shall be called 3 times if redirect is working, 2 times if not
SimulateTxExecutor<SimulateCallResult> executor = new(chain.Bridge, chain.BlockFinder, new JsonRpcConfig(), chain.SpecProvider, new SimulateBlockMutatorTracerFactory());
ResultWrapper<IReadOnlyList<SimulateBlockResult<SimulateCallResult>>> result =
executor.Execute(payload, BlockParameter.Latest);
IReadOnlyList<SimulateBlockResult<SimulateCallResult>> data = result.Data;
Assert.That(data.Count, Is.EqualTo(9));
SimulateBlockResult<SimulateCallResult> blockResult = data[0];
Assert.That(blockResult.Calls.Count, Is.EqualTo(2));
blockResult = data.Last();
Assert.That(blockResult.Calls.Count, Is.EqualTo(2));
}
/// <summary>
/// This test verifies that a temporary forked blockchain can make transactions, blocks and report on them
/// </summary>
[Test]
public async Task Test_eth_simulate_transactions_forced_fail()
{
TestRpcBlockchain chain = await EthRpcSimulateTestsBase.CreateChain();
UInt256 nonceA = chain.ReadOnlyState.GetNonce(TestItem.AddressA);
Transaction txMainnetAtoB =
GetTransferTxData(nonceA, chain.EthereumEcdsa, TestItem.PrivateKeyA, TestItem.AddressB, 1, type: TxType.Legacy);
SimulatePayload<TransactionForRpc> payload = CreateTransactionsForcedFail(chain, nonceA);
//Test that transfer tx works on mainchain
UInt256 before = chain.ReadOnlyState.GetBalance(TestItem.AddressA);
await chain.AddBlock(txMainnetAtoB);
UInt256 after = chain.ReadOnlyState.GetBalance(TestItem.AddressA);
Assert.That(after, Is.LessThan(before));
chain.Bridge.GetReceipt(txMainnetAtoB.Hash!);
//Force persistence of head block in main chain
chain.BlockTree.UpdateMainChain(new List<Block> { chain.BlockFinder.Head! }, true, true);
chain.BlockTree.UpdateHeadBlock(chain.BlockFinder.Head!.Hash!);
//will mock our GetCachedCodeInfo function - it shall be called 3 times if redirect is working, 2 times if not
SimulateTxExecutor<SimulateCallResult> executor = new(chain.Bridge, chain.BlockFinder, new JsonRpcConfig(), chain.SpecProvider, new SimulateBlockMutatorTracerFactory());
ResultWrapper<IReadOnlyList<SimulateBlockResult<SimulateCallResult>>> result =
executor.Execute(payload, BlockParameter.Latest);
Assert.That(result.Result!.Error, Is.EqualTo(SimulateErrorMessages.InsufficientFunds));
Assert.That(result.ErrorCode, Is.EqualTo(ErrorCodes.InsufficientFunds));
}
public static SimulatePayload<TransactionForRpc> CreateTransferLogsAddressPayload()
{
EthereumJsonSerializer serializer = new();
string input = """
{
"traceTransfers": true,
"blockStateCalls": [
{
"blockOverrides": {
"baseFeePerGas": "0xa"
},
"stateOverrides": {
"0xc000000000000000000000000000000000000000": {
"balance": "0x35a4ece8"
}
},
"calls": [
{
"type": "0x2",
"from": "0xc000000000000000000000000000000000000000",
"to": "0xc100000000000000000000000000000000000000",
"gas": "0x5208",
"maxFeePerGas": "0x14",
"maxPriorityFeePerGas": "0x1",
"maxFeePerBlobGas": "0x0",
"value": "0x65",
"nonce": "0x0",
"input": "0x"
}
]
}
]
}
""";
return serializer.Deserialize<SimulatePayload<TransactionForRpc>>(input);
}
[Test]
public async Task Test_eth_simulate_caps_gas_to_gas_cap()
{
TestRpcBlockchain chain = await EthRpcSimulateTestsBase.CreateChain();
long gasCap = 50_000;
chain.RpcConfig.GasCap = gasCap;
// Contract: GAS PUSH1 0 MSTORE PUSH1 32 PUSH1 0 RETURN — returns remaining gas
Address contractAddress = new("0xc200000000000000000000000000000000000000");
SimulatePayload<TransactionForRpc> payload = new()
{
BlockStateCalls =
[
new()
{
StateOverrides = new Dictionary<Address, AccountOverride>
{
{ contractAddress, new AccountOverride { Code = Bytes.FromHexString("0x5a60005260206000f3") } }
},
Calls =
[
new LegacyTransactionForRpc
{
From = TestItem.AddressA,
To = contractAddress,
Gas = 100_000,
GasPrice = 0
}
]
}
]
};
ResultWrapper<IReadOnlyList<SimulateBlockResult<SimulateCallResult>>> result = chain.EthRpcModule.eth_simulateV1(payload, BlockParameter.Latest);
Assert.That((bool)result.Result, Is.True, result.Result.ToString());
SimulateCallResult callResult = result.Data.First().Calls.First();
Assert.That(callResult.Status, Is.EqualTo((ulong)ResultType.Success));
Assert.That(callResult.MaxUsedGas, Is.Not.Null);
Assert.That(callResult.GasUsed, Is.Not.Null);
ulong maxUsedGas = callResult.MaxUsedGas ?? 0;
ulong gasUsed = callResult.GasUsed ?? 0;
Assert.That(maxUsedGas, Is.GreaterThanOrEqualTo(gasUsed));
UInt256 gasAvailable = new(callResult.ReturnData!, isBigEndian: true);
Assert.That(gasAvailable, Is.LessThan((UInt256)gasCap));
Assert.That(gasAvailable, Is.GreaterThan(UInt256.Zero));
}
[Test]
public async Task TestTransferLogsAddress([Values] bool eip7708)
{
SimulatePayload<TransactionForRpc> payload = CreateTransferLogsAddressPayload();
OverridableReleaseSpec spec = new(London.Instance);
TestRpcBlockchain chain = await EthRpcSimulateTestsBase.CreateChain(spec);
spec.IsEip7708Enabled = eip7708;
Console.WriteLine("current test: simulateTransferOverBlockStateCalls");
ResultWrapper<IReadOnlyList<SimulateBlockResult<SimulateCallResult>>> result = chain.EthRpcModule.eth_simulateV1(payload!, BlockParameter.Latest);
Log[] logs = result.Data.First().Calls.First().Logs.ToArray();
Assert.That(logs.Length, Is.EqualTo(1));
Assert.That(logs.First().Address == (eip7708 ? TransferLog.Sender : TransferLog.Erc20Sender));
}
[Test]
public async Task TestSerializationEthSimulate()
{
SimulatePayload<TransactionForRpc> payload = CreateTransferLogsAddressPayload();
TestRpcBlockchain chain = await EthRpcSimulateTestsBase.CreateChain();
JsonRpcResponse response = await RpcTest.TestRequest(chain.EthRpcModule, "eth_simulateV1", payload!, "latest");
response.Should().BeOfType<JsonRpcSuccessResponse>();
JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse)response;
IReadOnlyList<SimulateBlockResult<SimulateCallResult>> data = (IReadOnlyList<SimulateBlockResult<SimulateCallResult>>)successResponse.Result!;
Log[] logs = data[0].Calls.First().Logs.ToArray();
Assert.That(logs.First().Address == new Address("0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"));
}
[Test]
public async Task Test_eth_simulate_log_index_increments_across_transactions()
{
TestRpcBlockchain chain = await EthRpcSimulateTestsBase.CreateChain();
Address contractWith2Logs = new("0xc200000000000000000000000000000000000000");
Address contractWith1Log = new("0xc300000000000000000000000000000000000000");
SimulatePayload<TransactionForRpc> payload = new()
{
BlockStateCalls =
[
new()
{
StateOverrides = new Dictionary<Address, AccountOverride>
{
{ TestItem.AddressA, new AccountOverride { Balance = 100.Ether } },
{ contractWith2Logs, new AccountOverride { Code = Bytes.FromHexString("0x60006000a060006000a0") } },
{ contractWith1Log, new AccountOverride { Code = Bytes.FromHexString("0x60006000a0") } }
},
Calls =
[
new LegacyTransactionForRpc
{
From = TestItem.AddressA,
To = contractWith2Logs,
Gas = 100_000,
GasPrice = 0
},
new LegacyTransactionForRpc
{
From = TestItem.AddressA,
To = contractWith1Log,
Gas = 100_000,
GasPrice = 0
}
]
}
]
};
SimulateTxExecutor<SimulateCallResult> executor = new(chain.Bridge, chain.BlockFinder, new JsonRpcConfig(), chain.SpecProvider, new SimulateBlockMutatorTracerFactory());
ResultWrapper<IReadOnlyList<SimulateBlockResult<SimulateCallResult>>> result = executor.Execute(payload, BlockParameter.Latest);
Assert.That((bool)result.Result, Is.True, result.Result.ToString());
SimulateBlockResult<SimulateCallResult> block = result.Data.First();
Assert.That(block.Calls, Has.Count.EqualTo(2));
SimulateCallResult[] calls = block.Calls.ToArray();
Log[] tx0Logs = calls[0].Logs.ToArray();
Assert.That(tx0Logs, Has.Length.EqualTo(2));
Assert.That(tx0Logs[0].LogIndex, Is.EqualTo(0ul));
Assert.That(tx0Logs[1].LogIndex, Is.EqualTo(1ul));
Log[] tx1Logs = calls[1].Logs.ToArray();
Assert.That(tx1Logs, Has.Length.EqualTo(1));
Assert.That(tx1Logs[0].LogIndex, Is.EqualTo(2ul));
}
[TestCase(
"""{"blockStateCalls":[{"stateOverrides":{"0x0000000000000000000000000000000000000001":{"MovePrecompileToAddress":"0x0000000000000000000000000000000000000001"}}}]}""",
ErrorCodes.MovePrecompileSelfReference,
"MovePrecompileToAddress referenced itself in replacement",
TestName = "SelfReference_38022")]
public async Task eth_simulateV1_MovePrecompileToAddress_invalid_override_returns_error(string payloadJson, int expectedErrorCode, string expectedMessage)
{
EthereumJsonSerializer serializer = new();
SimulatePayload<TransactionForRpc> payload = serializer.Deserialize<SimulatePayload<TransactionForRpc>>(payloadJson);
TestRpcBlockchain chain = await EthRpcSimulateTestsBase.CreateChain();
ResultWrapper<IReadOnlyList<SimulateBlockResult<SimulateCallResult>>> result =
chain.EthRpcModule.eth_simulateV1(payload, BlockParameter.Latest);
result.ErrorCode.Should().Be(expectedErrorCode);
result.Result.Error.Should().Be(expectedMessage);
}
// Minimal bytecode: PREVRANDAO PUSH1 0x00 MSTORE PUSH1 0x20 PUSH1 0x00 RETURN
private static readonly byte[] PrevRandaoBytecode = [0x44, 0x60, 0x00, 0x52, 0x60, 0x20, 0x60, 0x00, 0xF3];
private static Task<TestRpcBlockchain> CreatePostMergeChain()
{
TestRpcBlockchain chain = new();
// MergeBlockNumber = 0 ensures simulated blocks have IsPostMerge = true,
// so PREVRANDAO reads header.MixHash rather than header.Difficulty.
TestSpecProvider specProvider = new(Cancun.Instance);
specProvider.UpdateMergeTransitionInfo(0);
return TestRpcBlockchain.ForTest(chain).Build(specProvider);
}
[TestCase("0xc300000000000000000000000000000000000000000000000000000000000001",
TestName = "prevrandao_with_nonzero_override_returns_overridden_value")]
[TestCase("0x0000000000000000000000000000000000000000000000000000000000000000",
TestName = "prevrandao_with_zero_override_returns_zero")]
[TestCase(null,
TestName = "prevrandao_without_override_returns_zero")]
public async Task eth_simulateV1_prevrandao_opcode_returns_expected_value(string? overrideHex)
{
TestRpcBlockchain chain = await CreatePostMergeChain();
Hash256? overrideHash = overrideHex is not null ? new Hash256(overrideHex) : null;
Hash256 expected = overrideHash ?? Hash256.Zero;
Address contractAddress = TestItem.AddressC;
SimulatePayload<TransactionForRpc> payload = new()
{
BlockStateCalls =
[
new()
{
BlockOverrides = overrideHash is not null ? new BlockOverride { PrevRandao = overrideHash } : null,
StateOverrides = new Dictionary<Address, AccountOverride>
{
{ contractAddress, new AccountOverride { Code = PrevRandaoBytecode } },
{ TestItem.AddressA, new AccountOverride { Balance = 1.Ether } }
},
Calls =
[
new LegacyTransactionForRpc
{
From = TestItem.AddressA,
To = contractAddress,
Gas = 100_000
}
]
}
]
};
ResultWrapper<IReadOnlyList<SimulateBlockResult<SimulateCallResult>>> result =
chain.EthRpcModule.eth_simulateV1(payload, BlockParameter.Latest);
result.Result.ResultType.Should().Be(Core.ResultType.Success);
SimulateCallResult callResult = result.Data.First().Calls.First();
callResult.Status.Should().Be((ulong)ResultType.Success);
callResult.ReturnData.Should().NotBeNull().And.HaveCount(32);
new Hash256(callResult.ReturnData!).Should().Be(expected);
}
// Regression test for https://github.com/NethermindEth/nethermind/issues/8480
// Verifies that blockOverrides.time is respected by the EVM TIMESTAMP opcode in eth_simulateV1
[TestCase(false)]
[TestCase(true)]
public async Task Test_eth_simulateV1_block_override_time_is_seen_by_timestamp_opcode(bool validation)
{
TestRpcBlockchain chain = await EthRpcSimulateTestsBase.CreateChain();
Address contractAddress = new("0xc200000000000000000000000000000000000000");
ulong headTimestamp = chain.BlockFinder.Head!.Header.Timestamp;
ulong futureTimestamp = headTimestamp + 24000;
// Contract: TIMESTAMP PUSH1 0 MSTORE PUSH1 0x20 PUSH1 0 RETURN (reads block.timestamp and returns it)
SimulatePayload<TransactionForRpc> payload = new()
{
Validation = validation,
BlockStateCalls =
[
new()
{
BlockOverrides = new BlockOverride { Time = futureTimestamp, BaseFeePerGas = 0 },
StateOverrides = new Dictionary<Address, AccountOverride>
{
{ contractAddress, new AccountOverride { Code = Bytes.FromHexString("0x4260005260206000f3") } }
},
Calls =
[
new LegacyTransactionForRpc
{
From = TestItem.AddressA,
To = contractAddress,
Gas = 100_000,
GasPrice = 0
}
]
}
]
};
ResultWrapper<IReadOnlyList<SimulateBlockResult<SimulateCallResult>>> result =
chain.EthRpcModule.eth_simulateV1(payload, BlockParameter.Latest);
Assert.That((bool)result.Result, Is.True, result.Result.ToString());
SimulateCallResult call = result.Data.First().Calls.First();
Assert.That(call.Error, Is.Null, call.Error?.Message);
// returnData should be the 32-byte ABI encoding of futureTimestamp
byte[] returnData = call.ReturnData ?? [];
UInt256 returnedTimestamp = new(returnData, isBigEndian: true);
Assert.That((ulong)returnedTimestamp, Is.EqualTo(futureTimestamp),
$"Expected block.timestamp = {futureTimestamp} (overridden), got {returnedTimestamp}");
}
/// <summary>
/// Regression test for https://github.com/NethermindEth/nethermind/issues/11217.
/// eth_simulateV1 must return -38014 with the spec-mandated message when the sender has
/// insufficient funds, regardless of the <c>validation</c> flag.
/// </summary>
[TestCase(true)]
[TestCase(false)]
public async Task eth_simulateV1_insufficient_funds_returns_spec_error_code_and_message(bool validation)
{
TestRpcBlockchain chain = await EthRpcSimulateTestsBase.CreateChain();
SimulatePayload<TransactionForRpc> payload = new()
{
BlockStateCalls =
[
new() { Calls = [new LegacyTransactionForRpc { From = TestItem.AddressA, To = TestItem.AddressB, Value = 1_000_000.Ether }] }
],
Validation = validation
};
ResultWrapper<IReadOnlyList<SimulateBlockResult<SimulateCallResult>>> result =
chain.EthRpcModule.eth_simulateV1(payload, BlockParameter.Latest);
Assert.That(result.Result!.Error, Is.EqualTo(SimulateErrorMessages.InsufficientFunds));
Assert.That(result.ErrorCode, Is.EqualTo(ErrorCodes.InsufficientFunds));
}
/// <summary>
/// Regression test for https://github.com/NethermindEth/nethermind/issues/11215
/// eth_simulateV1 with validation:true and maxFeePerGas below block baseFee must return
/// code -38012 with message "max fee per gas less than block base fee".
/// </summary>
[Test]
public async Task eth_simulateV1_fee_cap_below_base_fee_returns_spec_error_code_and_message()
{
TestRpcBlockchain chain = await EthRpcSimulateTestsBase.CreateChain();
// baseFeePerGas = 100 gwei, maxFeePerGas = 1 gwei → fee cap is below base fee
UInt256 baseFee = 100.GWei;
UInt256 feeCap = 1.GWei;
SimulatePayload<TransactionForRpc> payload = new()
{
BlockStateCalls =
[
new()
{
BlockOverrides = new BlockOverride { BaseFeePerGas = baseFee },
StateOverrides = new Dictionary<Address, AccountOverride>
{
{ TestItem.AddressA, new AccountOverride { Balance = 100.Ether } }
},
Calls =
[
new EIP1559TransactionForRpc
{
From = TestItem.AddressA,
To = TestItem.AddressB,
Value = UInt256.Zero,
Gas = 21_000,
MaxFeePerGas = feeCap,
MaxPriorityFeePerGas = UInt256.Zero
}
]
}
],
Validation = true
};
ResultWrapper<IReadOnlyList<SimulateBlockResult<SimulateCallResult>>> result =
chain.EthRpcModule.eth_simulateV1(payload, BlockParameter.Latest);
Assert.That(result.ErrorCode, Is.EqualTo(ErrorCodes.FeeCapBelowBaseFee));
Assert.That(result.Result!.Error, Is.EqualTo(SimulateErrorMessages.FeeCapBelowBaseFee));
}
/// <summary>
/// Regression test for https://github.com/NethermindEth/nethermind/issues/11218.
/// eth_simulateV1 must return -38013 with the spec-mandated message when the transaction
/// gas limit is below the intrinsic gas cost.
/// </summary>
[Test]
public async Task eth_simulateV1_intrinsic_gas_returns_spec_error_code_and_message()
{
TestRpcBlockchain chain = await EthRpcSimulateTestsBase.CreateChain();
// Gas = 1 is below the intrinsic gas cost of 21_000 for a basic transfer.
SimulatePayload<TransactionForRpc> payload = new()
{
BlockStateCalls =
[
new()
{
BlockOverrides = new BlockOverride { BaseFeePerGas = UInt256.Zero },
StateOverrides = new Dictionary<Address, AccountOverride>
{
{ TestItem.AddressA, new AccountOverride { Balance = 1.Ether } }
},
Calls =
[
new LegacyTransactionForRpc
{
From = TestItem.AddressA,
To = TestItem.AddressB,
Value = UInt256.Zero,
Gas = 1,
GasPrice = UInt256.Zero
}
]
}
],
Validation = true
};
ResultWrapper<IReadOnlyList<SimulateBlockResult<SimulateCallResult>>> result =
chain.EthRpcModule.eth_simulateV1(payload, BlockParameter.Latest);
Assert.That(result.ErrorCode, Is.EqualTo(ErrorCodes.IntrinsicGas));
Assert.That(result.Result!.Error, Is.EqualTo(SimulateErrorMessages.IntrinsicGas));
}
/// <summary>
/// Regression test: eth_simulateV1 with validation:true and a nonce below the account's current
/// nonce must return -38010 (NonceTooLow).
/// </summary>
[Test]
public async Task eth_simulateV1_nonce_too_low_returns_spec_error_code()
{
TestRpcBlockchain chain = await EthRpcSimulateTestsBase.CreateChain();
// Set the account's nonce to 10, then send a tx with nonce 0 (below current).
SimulatePayload<TransactionForRpc> payload = new()
{
BlockStateCalls =
[
new()
{
StateOverrides = new Dictionary<Address, AccountOverride>
{
{ TestItem.AddressA, new AccountOverride { Balance = 1.Ether, Nonce = 10 } }
},
Calls =
[
new LegacyTransactionForRpc
{
From = TestItem.AddressA,
To = TestItem.AddressB,
Value = UInt256.Zero,
Nonce = 0,
GasPrice = UInt256.Zero,
Gas = 21_000
}
]
}
],
Validation = true
};
ResultWrapper<IReadOnlyList<SimulateBlockResult<SimulateCallResult>>> result =
chain.EthRpcModule.eth_simulateV1(payload, BlockParameter.Latest);
Assert.That(result.ErrorCode, Is.EqualTo(ErrorCodes.NonceTooLow));
}
/// <summary>
/// Regression test: eth_simulateV1 with validation:true and a nonce above the account's current
/// nonce must return -38011 (NonceTooHigh).
/// </summary>
[Test]
public async Task eth_simulateV1_nonce_too_high_returns_spec_error_code()
{
TestRpcBlockchain chain = await EthRpcSimulateTestsBase.CreateChain();
// Account nonce is 0; send a tx with nonce 100 (way above current).
SimulatePayload<TransactionForRpc> payload = new()
{
BlockStateCalls =
[
new()
{
StateOverrides = new Dictionary<Address, AccountOverride>
{
{ TestItem.AddressA, new AccountOverride { Balance = 1.Ether } }
},
Calls =
[
new LegacyTransactionForRpc
{
From = TestItem.AddressA,
To = TestItem.AddressB,
Value = UInt256.Zero,
Nonce = 100,
GasPrice = UInt256.Zero,
Gas = 21_000
}
]
}
],
Validation = true
};
ResultWrapper<IReadOnlyList<SimulateBlockResult<SimulateCallResult>>> result =
chain.EthRpcModule.eth_simulateV1(payload, BlockParameter.Latest);
Assert.That(result.ErrorCode, Is.EqualTo(ErrorCodes.NonceTooHigh));
}
/// <summary>
/// Regression test: eth_simulateV1 with validation:true and a sender address that has deployed
/// code (EIP-3607) must return -38024 (SenderIsNotEoa).
/// </summary>
[Test]
public async Task eth_simulateV1_sender_is_not_eoa_returns_spec_error_code()
{
OverridableReleaseSpec spec = new(London.Instance) { IsEip3607Enabled = true };
TestSpecProvider specProvider = new(spec) { AllowTestChainOverride = false };
TestRpcBlockchain chain = await TestRpcBlockchain.ForTest(new TestRpcBlockchain()).Build(specProvider);
// Override TestItem.AddressC with contract code — makes it a non-EOA sender.
SimulatePayload<TransactionForRpc> payload = new()
{
BlockStateCalls =
[
new()
{
StateOverrides = new Dictionary<Address, AccountOverride>
{
{
TestItem.AddressC,
new AccountOverride
{
Balance = 1.Ether,
Code = Bytes.FromHexString("0x60006000")
}
}
},
Calls =
[
new LegacyTransactionForRpc
{
From = TestItem.AddressC,
To = TestItem.AddressB,
Value = UInt256.Zero,
GasPrice = UInt256.Zero,
Gas = 21_000
}
]
}
],
Validation = true
};
ResultWrapper<IReadOnlyList<SimulateBlockResult<SimulateCallResult>>> result =
chain.EthRpcModule.eth_simulateV1(payload, BlockParameter.Latest);
Assert.That(result.ErrorCode, Is.EqualTo(ErrorCodes.SenderIsNotEoa));
}
}