-
Notifications
You must be signed in to change notification settings - Fork 514
/
Copy pathEip1014Tests.cs
193 lines (144 loc) · 7.62 KB
/
Eip1014Tests.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
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using FluentAssertions;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Specs;
using Nethermind.State;
using Nethermind.Core.Test.Builders;
using Nethermind.Trie;
using NUnit.Framework;
namespace Nethermind.Evm.Test
{
[TestFixture]
public class Eip1014Tests : VirtualMachineTestsBase
{
protected override long BlockNumber => MainnetSpecProvider.ConstantinopleFixBlockNumber;
private void AssertEip1014(Address address, byte[] code)
{
AssertCodeHash(address, Keccak.Compute(code));
}
[Test]
public void TestHive()
{
byte[] code = Prepare.EvmCode
.FromCode("0x73095e7baea6a6c7c4c2dfeb977efac326af552d873173095e7baea6a6c7c4c2dfeb977efac326af552d873173095e7baea6a6c7c4c2dfeb977efac326af552d87313700")
.Done;
Execute(code);
}
[Test]
public void Test()
{
byte[] salt = { 4, 5, 6 };
byte[] deployedCode = { 1, 2, 3 };
byte[] initCode = Prepare.EvmCode
.ForInitOf(deployedCode).Done;
byte[] createCode = Prepare.EvmCode
.Create2(initCode, salt, 0).Done;
TestState.CreateAccount(TestItem.AddressC, 1.Ether());
TestState.InsertCode(TestItem.AddressC, createCode, Spec);
byte[] code = Prepare.EvmCode
.Call(TestItem.AddressC, 50000)
.Done;
Execute(code);
Address expectedAddress = ContractAddress.From(TestItem.AddressC, salt.PadLeft(32).AsSpan(), initCode.AsSpan());
AssertEip1014(expectedAddress, deployedCode);
}
[Test]
public void Test_out_of_gas_existing_account()
{
byte[] salt = { 4, 5, 6 };
byte[] deployedCode = { 1, 2, 3 };
byte[] initCode = Prepare.EvmCode
.ForInitOf(deployedCode).Done;
byte[] createCode = Prepare.EvmCode
.Create2(initCode, salt, 0).Done;
Address expectedAddress = ContractAddress.From(TestItem.AddressC, salt.PadLeft(32).AsSpan(), initCode.AsSpan());
TestState.CreateAccount(expectedAddress, 1.Ether());
TestState.CreateAccount(TestItem.AddressC, 1.Ether());
TestState.InsertCode(TestItem.AddressC, createCode, Spec);
byte[] code = Prepare.EvmCode
.Call(TestItem.AddressC, 32100)
.Done;
Execute(code);
TestState.TryGetAccount(expectedAddress, out AccountStruct account).Should().BeTrue();
account.Balance.Should().Be(1.Ether());
AssertEip1014(expectedAddress, []);
}
[Test]
public void Test_out_of_gas_existing_account_with_storage()
{
byte[] salt = { 4, 5, 6 };
byte[] deployedCode = { 1, 2, 3 };
byte[] initCode = Prepare.EvmCode
.ForInitOf(deployedCode).Done;
byte[] createCode = Prepare.EvmCode
.Create2(initCode, salt, 0).Done;
Address expectedAddress = ContractAddress.From(TestItem.AddressC, salt.PadLeft(32).AsSpan(), initCode.AsSpan());
TestState.CreateAccount(expectedAddress, 1.Ether());
TestState.Set(new StorageCell(expectedAddress, 1), new StorageValue([1, 2, 3, 4, 5]));
TestState.Commit(Spec);
TestState.CommitTree(0);
ValueHash256 storageRoot = TestState.GetStorageRoot(expectedAddress);
storageRoot.Should().NotBe(PatriciaTree.EmptyTreeHash);
TestState.CreateAccount(TestItem.AddressC, 1.Ether());
TestState.InsertCode(TestItem.AddressC, createCode, Spec);
byte[] code = Prepare.EvmCode
.Call(TestItem.AddressC, 32100)
.Done;
Execute(code);
TestState.TryGetAccount(expectedAddress, out AccountStruct account).Should().BeTrue();
account.Balance.Should().Be(1.Ether());
account.StorageRoot.Should().Be(storageRoot);
AssertEip1014(expectedAddress, []);
}
[Test]
public void Test_out_of_gas_non_existing_account()
{
byte[] salt = [4, 5, 6];
byte[] deployedCode = [1, 2, 3];
byte[] initCode = Prepare.EvmCode
.ForInitOf(deployedCode).Done;
byte[] createCode = Prepare.EvmCode
.Create2(initCode, salt, 0).Done;
Address expectedAddress = ContractAddress.From(TestItem.AddressC, salt.PadLeft(32).AsSpan(), initCode.AsSpan());
// TestState.CreateAccount(expectedAddress, 1.Ether()); <-- non-existing
TestState.CreateAccount(TestItem.AddressC, 1.Ether());
TestState.InsertCode(TestItem.AddressC, createCode, Spec);
byte[] code = Prepare.EvmCode
.Call(TestItem.AddressC, 32100)
.Done;
Execute(code);
TestState.AccountExists(expectedAddress).Should().BeFalse();
}
/// <summary>
/// https://eips.ethereum.org/EIPS/eip-1014
/// </summary>
[TestCase("0x0000000000000000000000000000000000000000", "0x0000000000000000000000000000000000000000000000000000000000000000", "0x00", 32006, "0x4D1A2e2bB4F88F0250f26Ffff098B0b30B26BF38")]
[TestCase("0xdeadbeef00000000000000000000000000000000", "0x0000000000000000000000000000000000000000000000000000000000000000", "0x00", 32006, "0xB928f69Bb1D91Cd65274e3c79d8986362984fDA3")]
[TestCase("0xdeadbeef00000000000000000000000000000000", "0x000000000000000000000000feed000000000000000000000000000000000000", "0x00", 32006, "0xD04116cDd17beBE565EB2422F2497E06cC1C9833")]
[TestCase("0x0000000000000000000000000000000000000000", "0x0000000000000000000000000000000000000000000000000000000000000000", "0xdeadbeef", 32006, "0x70f2b2914A2a4b783FaEFb75f459A580616Fcb5e")]
[TestCase("0x00000000000000000000000000000000deadbeef", "0x00000000000000000000000000000000000000000000000000000000cafebabe", "0xdeadbeef", 32006, "0x60f3f640a8508fC6a86d45DF051962668E1e8AC7")]
[TestCase("0x00000000000000000000000000000000deadbeef", "0x00000000000000000000000000000000000000000000000000000000cafebabe", "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef", 32012, "0x1d8bfDC5D46DC4f61D6b6115972536eBE6A8854C")]
[TestCase("0x0000000000000000000000000000000000000000", "0x0000000000000000000000000000000000000000000000000000000000000000", "0x", 32000, "0xE33C0C7F7df4809055C3ebA6c09CFe4BaF1BD9e0")]
public void Examples_from_eip_spec_are_executed_correctly(string addressHex, string saltHex, string initCodeHex, long gas, string resultHex)
{
byte[] salt = Bytes.FromHexString(saltHex);
byte[] deployedCode = [];
byte[] initCode = Bytes.FromHexString(initCodeHex);
byte[] createCode = Prepare.EvmCode
.Create2(initCode, salt, 0).Done;
TestState.CreateAccount(TestItem.AddressC, 1.Ether());
TestState.InsertCode(TestItem.AddressC, createCode, Spec);
byte[] code = Prepare.EvmCode
.Call(TestItem.AddressC, 50000)
.Done;
_ = ExecuteAndTrace(code);
Address expectedAddress = new(resultHex);
AssertEip1014(expectedAddress, deployedCode);
// Assert.AreEqual(gas, trace.Entries.Single(e => e.Operation == Instruction.CREATE2.ToString()).GasCost);
}
}
}