|
| 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 | +} |
0 commit comments