Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Commit 23b85b5

Browse files
committed
More unit tests
1 parent 75f488a commit 23b85b5

File tree

4 files changed

+174
-2
lines changed

4 files changed

+174
-2
lines changed

libethereum/Executive.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ class Executive
186186
/// Revert all changes made to the state by this execution.
187187
void revert();
188188

189+
/// Used only in tests
190+
ExtVM const& extVM() const { return *m_ext; }
191+
189192
private:
190193
/// @returns false iff go() must be called (and thus a VM execution in required).
191194
bool createWithAddressFromNonceAndSender(Address const& _sender, u256 const& _endowment,

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ set(unittest_sources
2121
unittests/libethcore/CommonJS.cpp
2222
unittests/libethcore/KeyManager.cpp
2323

24+
unittests/libethereum/ExecutiveTest.cpp
2425
unittests/libethereum/ValidationSchemes.cpp
2526

2627
unittests/libp2p/capability.cpp
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// Aleth: Ethereum C++ client, tools and libraries.
2+
// Copyright 2019 Aleth Authors.
3+
// Licensed under the GNU General Public License, Version 3.
4+
5+
#include <libethashseal/Ethash.h>
6+
#include <libethashseal/GenesisInfo.h>
7+
#include <libethereum/ChainParams.h>
8+
#include <libethereum/Executive.h>
9+
#include <libethereum/ExtVM.h>
10+
#include <libethereum/State.h>
11+
#include <test/tools/libtestutils/TestLastBlockHashes.h>
12+
#include <gtest/gtest.h>
13+
14+
using namespace dev;
15+
using namespace dev::eth;
16+
using namespace dev::test;
17+
18+
class ExecutiveTest : public testing::Test
19+
{
20+
public:
21+
ExecutiveTest()
22+
{
23+
ethash.setChainParams(ChainParams{genesisInfo(eth::Network::IstanbulTransitionTest)});
24+
}
25+
26+
Ethash ethash;
27+
BlockHeader blockHeader;
28+
TestLastBlockHashes lastBlockHashes{{}};
29+
State state{0};
30+
31+
Address receiveAddress{"0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b"};
32+
Address txSender{"0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"};
33+
u256 txValue;
34+
u256 gasPrice;
35+
bytesConstRef txData{};
36+
u256 gas = 1000000;
37+
bytes code = {1, 2, 3, 4};
38+
};
39+
40+
TEST_F(ExecutiveTest, callUsesAccountVersion)
41+
{
42+
EnvInfo envInfo(blockHeader, lastBlockHashes, 0);
43+
44+
state.createContract(receiveAddress);
45+
u256 version = 1;
46+
state.setCode(receiveAddress, bytes{code}, version);
47+
state.commit(State::CommitBehaviour::RemoveEmptyAccounts);
48+
49+
Executive executive(state, envInfo, ethash);
50+
51+
bool done = executive.call(receiveAddress, txSender, txValue, gasPrice, txData, gas);
52+
53+
EXPECT_FALSE(done);
54+
EXPECT_EQ(executive.extVM().version, version);
55+
}
56+
57+
TEST_F(ExecutiveTest, createUsesLatestForkVersion)
58+
{
59+
// block in Istanbul fork
60+
blockHeader.setNumber(10);
61+
62+
EnvInfo envInfo(blockHeader, lastBlockHashes, 0);
63+
64+
Executive executive(state, envInfo, ethash);
65+
66+
bool done = executive.create(txSender, txValue, gasPrice, gas, ref(code), txSender);
67+
68+
EXPECT_FALSE(done);
69+
EXPECT_EQ(executive.extVM().version, IstanbulSchedule.version);
70+
}
71+
72+
TEST_F(ExecutiveTest, createOpcodeUsesParentVersion)
73+
{
74+
EnvInfo envInfo(blockHeader, lastBlockHashes, 0);
75+
76+
state.createContract(txSender);
77+
u256 version = 1;
78+
state.setCode(txSender, bytes{code}, version);
79+
state.commit(State::CommitBehaviour::RemoveEmptyAccounts);
80+
81+
Executive executive(state, envInfo, ethash);
82+
83+
bool done = executive.createOpcode(txSender, txValue, gasPrice, gas, ref(code), txSender);
84+
85+
EXPECT_FALSE(done);
86+
EXPECT_EQ(executive.extVM().version, version);
87+
}
88+
89+
TEST_F(ExecutiveTest, create2OpcodeUsesParentVersion)
90+
{
91+
EnvInfo envInfo(blockHeader, lastBlockHashes, 0);
92+
93+
state.createContract(txSender);
94+
u256 version = 1;
95+
state.setCode(txSender, bytes{code}, version);
96+
state.commit(State::CommitBehaviour::RemoveEmptyAccounts);
97+
98+
Executive executive(state, envInfo, ethash);
99+
100+
bool done = executive.create2Opcode(txSender, txValue, gasPrice, gas, ref(code), txSender, 0);
101+
102+
EXPECT_FALSE(done);
103+
EXPECT_EQ(executive.extVM().version, version);
104+
}
105+
106+
TEST_F(ExecutiveTest, emptyInitCodeSetsParentVersion)
107+
{
108+
EnvInfo envInfo(blockHeader, lastBlockHashes, 0);
109+
110+
state.createContract(txSender);
111+
u256 version = 1;
112+
state.setCode(txSender, bytes{code}, version);
113+
state.commit(State::CommitBehaviour::RemoveEmptyAccounts);
114+
115+
Executive executive(state, envInfo, ethash);
116+
117+
bytes initCode;
118+
bool done = executive.createOpcode(txSender, txValue, gasPrice, gas, ref(initCode), txSender);
119+
120+
EXPECT_TRUE(done);
121+
EXPECT_FALSE(state.addressHasCode(executive.newAddress()));
122+
EXPECT_EQ(state.version(executive.newAddress()), version);
123+
}
124+
125+
TEST_F(ExecutiveTest, createdContractHasParentVersion)
126+
{
127+
EnvInfo envInfo(blockHeader, lastBlockHashes, 0);
128+
129+
state.createContract(txSender);
130+
u256 version = 1;
131+
state.setCode(txSender, bytes{code}, version);
132+
state.commit(State::CommitBehaviour::RemoveEmptyAccounts);
133+
134+
Executive executive(state, envInfo, ethash);
135+
136+
// mstore(0, 0x60)
137+
// return(0, 0x20)
138+
bytes initCode = fromHex("606060005260206000f3");
139+
140+
bool done = executive.createOpcode(txSender, txValue, gasPrice, gas, ref(initCode), txSender);
141+
EXPECT_FALSE(done);
142+
143+
done = executive.go();
144+
EXPECT_TRUE(done);
145+
146+
EXPECT_TRUE(state.addressHasCode(executive.newAddress()));
147+
EXPECT_EQ(state.version(executive.newAddress()), version);
148+
}

test/unittests/libethereum/StateUnitTests.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,11 @@ BOOST_AUTO_TEST_CASE(RollbackSetCode)
6666
s.setCode(addr, {std::begin(codeData), std::end(codeData)}, version);
6767
s.rollback(savepoint);
6868

69-
BOOST_CHECK(!s.addressHasCode(addr));
70-
BOOST_CHECK(!s.accountNonemptyAndExisting(addr));
69+
BOOST_CHECK(!s.addressInUse(addr));
70+
BOOST_CHECK(s.version(addr) == 0);
71+
72+
// only state root exists in DB
73+
BOOST_CHECK_EQUAL(s.db().keys().size(), 1);
7174
}
7275

7376
BOOST_AUTO_TEST_CASE(CodeVersionZero)
@@ -85,6 +88,23 @@ BOOST_AUTO_TEST_CASE(CodeVersionZero)
8588
BOOST_CHECK_EQUAL(s.version(addr), version);
8689
}
8790

91+
BOOST_AUTO_TEST_CASE(SetEmptyCodeNonZeroVersion)
92+
{
93+
Address addr{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"};
94+
State s{0};
95+
s.createContract(addr);
96+
s.setNonce(addr, 1);
97+
u256 version = 123;
98+
s.setCode(addr, {}, version);
99+
s.commit(State::CommitBehaviour::RemoveEmptyAccounts);
100+
101+
BOOST_CHECK(!s.addressHasCode(addr));
102+
BOOST_CHECK_EQUAL(s.version(addr), version);
103+
104+
// empty code is not saved to DB
105+
BOOST_CHECK(!s.db().exists(EmptySHA3));
106+
}
107+
88108
class AddressRangeTestFixture : public TestOutputHelperFixture
89109
{
90110
public:

0 commit comments

Comments
 (0)