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

Commit aeae419

Browse files
committed
More unit tests
1 parent 75f488a commit aeae419

File tree

4 files changed

+152
-2
lines changed

4 files changed

+152
-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: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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 <libethcore/SealEngine.h>
8+
#include <libethereum/ChainParams.h>
9+
#include <libethereum/Executive.h>
10+
#include <libethereum/ExtVM.h>
11+
#include <libethereum/State.h>
12+
#include <test/tools/libtestutils/TestLastBlockHashes.h>
13+
#include <gtest/gtest.h>
14+
15+
using namespace dev;
16+
using namespace dev::eth;
17+
using namespace dev::test;
18+
19+
class ExecutiveTest : public testing::Test
20+
{
21+
public:
22+
ExecutiveTest()
23+
{
24+
ethash.setChainParams(ChainParams{genesisInfo(eth::Network::IstanbulTransitionTest)});
25+
26+
blockHeader.setGasLimit(5000);
27+
blockHeader.setTimestamp(0);
28+
}
29+
30+
Ethash ethash;
31+
BlockHeader blockHeader;
32+
TestLastBlockHashes lastBlockHashes{{}};
33+
State state{0};
34+
35+
Address receiveAddress{"0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b"};
36+
Address txSender{"0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"};
37+
u256 txValue;
38+
u256 gasPrice;
39+
bytesConstRef txData{};
40+
u256 gas = 1000000;
41+
bytes code = {1, 2, 3, 4};
42+
};
43+
44+
TEST_F(ExecutiveTest, callUsesAccountVersion)
45+
{
46+
EnvInfo envInfo(blockHeader, lastBlockHashes, 0);
47+
48+
state.createContract(receiveAddress);
49+
u256 version = 1;
50+
state.setCode(receiveAddress, bytes{code}, version);
51+
state.commit(State::CommitBehaviour::RemoveEmptyAccounts);
52+
53+
Executive executive(state, envInfo, ethash);
54+
55+
bool done = executive.call(receiveAddress, txSender, txValue, gasPrice, txData, gas);
56+
57+
EXPECT_FALSE(done);
58+
EXPECT_EQ(executive.extVM().version, version);
59+
}
60+
61+
TEST_F(ExecutiveTest, createUsesLatestForkVersion)
62+
{
63+
// block in Istanbul fork
64+
blockHeader.setNumber(10);
65+
66+
EnvInfo envInfo(blockHeader, lastBlockHashes, 0);
67+
68+
Executive executive(state, envInfo, ethash);
69+
70+
bool done = executive.create(txSender, txValue, gasPrice, gas, ref(code), txSender);
71+
72+
EXPECT_FALSE(done);
73+
EXPECT_EQ(executive.extVM().version, IstanbulSchedule.version);
74+
}
75+
76+
TEST_F(ExecutiveTest, createOpcodeUsesParentVersion)
77+
{
78+
EnvInfo envInfo(blockHeader, lastBlockHashes, 0);
79+
80+
state.createContract(txSender);
81+
u256 version = 1;
82+
state.setCode(txSender, bytes{code}, version);
83+
state.commit(State::CommitBehaviour::RemoveEmptyAccounts);
84+
85+
Executive executive(state, envInfo, ethash);
86+
87+
bool done = executive.createOpcode(txSender, txValue, gasPrice, gas, ref(code), txSender);
88+
89+
EXPECT_FALSE(done);
90+
EXPECT_EQ(executive.extVM().version, version);
91+
}
92+
93+
TEST_F(ExecutiveTest, create2OpcodeUsesParentVersion)
94+
{
95+
EnvInfo envInfo(blockHeader, lastBlockHashes, 0);
96+
97+
state.createContract(txSender);
98+
u256 version = 1;
99+
state.setCode(txSender, bytes{code}, version);
100+
state.commit(State::CommitBehaviour::RemoveEmptyAccounts);
101+
102+
Executive executive(state, envInfo, ethash);
103+
104+
bool done = executive.create2Opcode(txSender, txValue, gasPrice, gas, ref(code), txSender, 0);
105+
106+
EXPECT_FALSE(done);
107+
EXPECT_EQ(executive.extVM().version, version);
108+
}
109+
110+
TEST_F(ExecutiveTest, emptyInitCodeSetsParentVersion)
111+
{
112+
EnvInfo envInfo(blockHeader, lastBlockHashes, 0);
113+
114+
state.createContract(txSender);
115+
u256 version = 1;
116+
state.setCode(txSender, bytes{code}, version);
117+
state.commit(State::CommitBehaviour::RemoveEmptyAccounts);
118+
119+
Executive executive(state, envInfo, ethash);
120+
121+
bool done = executive.createOpcode(txSender, txValue, gasPrice, gas, {}, txSender);
122+
123+
EXPECT_TRUE(done);
124+
EXPECT_FALSE(state.addressHasCode(executive.newAddress()));
125+
EXPECT_EQ(state.version(executive.newAddress()), version);
126+
}

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)