2016-11-28 00:21:01 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2016-11-28 00:21:01 +00:00
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2016
|
|
|
|
* Framework for executing contracts and testing them using RPC.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <test/ExecutionFramework.h>
|
|
|
|
|
2019-06-26 16:42:24 +00:00
|
|
|
#include <test/EVMHost.h>
|
2017-09-20 12:23:00 +00:00
|
|
|
|
2019-06-26 21:22:42 +00:00
|
|
|
#include <test/evmc/evmc.hpp>
|
|
|
|
|
2021-04-29 23:09:36 +00:00
|
|
|
#include <test/libsolidity/util/SoltestTypes.h>
|
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonIO.h>
|
2019-06-26 16:42:24 +00:00
|
|
|
|
2020-06-05 23:10:48 +00:00
|
|
|
#include <liblangutil/Exceptions.h>
|
|
|
|
|
2019-06-26 16:42:24 +00:00
|
|
|
#include <boost/test/framework.hpp>
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2021-04-29 23:09:36 +00:00
|
|
|
#include <range/v3/range.hpp>
|
|
|
|
#include <range/v3/view/transform.hpp>
|
2018-02-22 16:21:26 +00:00
|
|
|
|
2019-06-26 16:42:24 +00:00
|
|
|
#include <cstdlib>
|
2021-09-16 14:33:28 +00:00
|
|
|
#include <limits>
|
2019-04-04 10:04:53 +00:00
|
|
|
|
2016-11-28 00:21:01 +00:00
|
|
|
using namespace std;
|
2019-12-23 15:50:30 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::test;
|
2021-04-29 23:09:36 +00:00
|
|
|
using namespace solidity::frontend::test;
|
2016-11-28 00:21:01 +00:00
|
|
|
|
2019-02-06 11:10:05 +00:00
|
|
|
ExecutionFramework::ExecutionFramework():
|
2020-06-05 23:10:48 +00:00
|
|
|
ExecutionFramework(solidity::test::CommonOptions::get().evmVersion(), solidity::test::CommonOptions::get().vmPaths)
|
2019-02-06 11:10:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-05 23:10:48 +00:00
|
|
|
ExecutionFramework::ExecutionFramework(langutil::EVMVersion _evmVersion, vector<boost::filesystem::path> const& _vmPaths):
|
2019-03-15 16:22:04 +00:00
|
|
|
m_evmVersion(_evmVersion),
|
2019-12-23 15:50:30 +00:00
|
|
|
m_optimiserSettings(solidity::frontend::OptimiserSettings::minimal()),
|
2020-01-14 16:48:17 +00:00
|
|
|
m_showMessages(solidity::test::CommonOptions::get().showMessages),
|
2020-06-05 23:10:48 +00:00
|
|
|
m_vmPaths(_vmPaths)
|
2016-11-28 00:21:01 +00:00
|
|
|
{
|
2020-05-06 09:49:05 +00:00
|
|
|
if (solidity::test::CommonOptions::get().optimize)
|
2019-12-23 15:50:30 +00:00
|
|
|
m_optimiserSettings = solidity::frontend::OptimiserSettings::standard();
|
2019-06-26 16:42:24 +00:00
|
|
|
|
2020-06-05 23:10:48 +00:00
|
|
|
for (auto const& path: m_vmPaths)
|
|
|
|
if (EVMHost::getVM(path.string()).has_capability(EVMC_CAPABILITY_EWASM))
|
|
|
|
m_supportsEwasm = true;
|
|
|
|
|
|
|
|
selectVM(evmc_capabilities::EVMC_CAPABILITY_EVM1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExecutionFramework::selectVM(evmc_capabilities _cap)
|
|
|
|
{
|
|
|
|
m_evmcHost.reset();
|
|
|
|
for (auto const& path: m_vmPaths)
|
|
|
|
{
|
|
|
|
evmc::VM& vm = EVMHost::getVM(path.string());
|
|
|
|
if (vm.has_capability(_cap))
|
|
|
|
{
|
|
|
|
m_evmcHost = make_unique<EVMHost>(m_evmVersion, vm);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
solAssert(m_evmcHost != nullptr, "");
|
2020-03-03 11:21:16 +00:00
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExecutionFramework::reset()
|
|
|
|
{
|
2020-06-05 23:10:48 +00:00
|
|
|
m_evmcHost->reset();
|
2019-06-26 16:42:24 +00:00
|
|
|
for (size_t i = 0; i < 10; i++)
|
2020-06-05 23:10:48 +00:00
|
|
|
m_evmcHost->accounts[EVMHost::convertToEVMC(account(i))].balance =
|
2019-06-26 16:42:24 +00:00
|
|
|
EVMHost::convertToEVMC(u256(1) << 100);
|
2016-11-28 00:21:01 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 12:23:00 +00:00
|
|
|
std::pair<bool, string> ExecutionFramework::compareAndCreateMessage(
|
|
|
|
bytes const& _result,
|
|
|
|
bytes const& _expectation
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (_result == _expectation)
|
|
|
|
return std::make_pair(true, std::string{});
|
|
|
|
std::string message =
|
|
|
|
"Invalid encoded data\n"
|
|
|
|
" Result Expectation\n";
|
2021-09-16 14:33:28 +00:00
|
|
|
auto resultHex = boost::replace_all_copy(util::toHex(_result), "0", ".");
|
|
|
|
auto expectedHex = boost::replace_all_copy(util::toHex(_expectation), "0", ".");
|
2017-09-20 12:23:00 +00:00
|
|
|
for (size_t i = 0; i < std::max(resultHex.size(), expectedHex.size()); i += 0x40)
|
|
|
|
{
|
|
|
|
std::string result{i >= resultHex.size() ? string{} : resultHex.substr(i, 0x40)};
|
|
|
|
std::string expected{i > expectedHex.size() ? string{} : expectedHex.substr(i, 0x40)};
|
|
|
|
message +=
|
|
|
|
(result == expected ? " " : " X ") +
|
|
|
|
result +
|
|
|
|
std::string(0x41 - result.size(), ' ') +
|
|
|
|
expected +
|
|
|
|
"\n";
|
|
|
|
}
|
|
|
|
return make_pair(false, message);
|
|
|
|
}
|
|
|
|
|
2020-10-13 11:28:39 +00:00
|
|
|
bytes ExecutionFramework::panicData(util::PanicCode _code)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
m_evmVersion.supportsReturndata() ?
|
2022-09-27 12:13:44 +00:00
|
|
|
toCompactBigEndian(selectorFromSignatureU32("Panic(uint256)"), 4) + encode(u256(static_cast<unsigned>(_code))) :
|
2020-10-13 11:28:39 +00:00
|
|
|
bytes();
|
|
|
|
}
|
|
|
|
|
2018-08-10 17:19:21 +00:00
|
|
|
u256 ExecutionFramework::gasLimit() const
|
|
|
|
{
|
2020-06-05 23:10:48 +00:00
|
|
|
return {m_evmcHost->tx_context.block_gas_limit};
|
2018-08-10 17:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u256 ExecutionFramework::gasPrice() const
|
|
|
|
{
|
2020-08-21 22:01:29 +00:00
|
|
|
// here and below we use "return u256{....}" instead of just "return {....}"
|
|
|
|
// to please MSVC and avoid unexpected
|
|
|
|
// warning C4927 : illegal conversion; more than one user - defined conversion has been implicitly applied
|
2020-06-05 23:10:48 +00:00
|
|
|
return u256{EVMHost::convertFromEVMC(m_evmcHost->tx_context.tx_gas_price)};
|
2018-08-10 17:19:21 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 16:42:24 +00:00
|
|
|
u256 ExecutionFramework::blockHash(u256 const& _number) const
|
2018-08-10 17:19:21 +00:00
|
|
|
{
|
2020-08-21 22:01:29 +00:00
|
|
|
return u256{EVMHost::convertFromEVMC(
|
2020-06-05 23:10:48 +00:00
|
|
|
m_evmcHost->get_block_hash(static_cast<int64_t>(_number & numeric_limits<uint64_t>::max()))
|
2020-06-03 08:27:23 +00:00
|
|
|
)};
|
2019-06-26 16:42:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u256 ExecutionFramework::blockNumber() const
|
|
|
|
{
|
2020-06-05 23:10:48 +00:00
|
|
|
return m_evmcHost->tx_context.block_number;
|
2018-08-10 17:19:21 +00:00
|
|
|
}
|
|
|
|
|
2016-11-28 00:21:01 +00:00
|
|
|
void ExecutionFramework::sendMessage(bytes const& _data, bool _isCreation, u256 const& _value)
|
|
|
|
{
|
2020-06-05 23:10:48 +00:00
|
|
|
m_evmcHost->newBlock();
|
2019-06-26 16:42:24 +00:00
|
|
|
|
2016-12-06 17:38:59 +00:00
|
|
|
if (m_showMessages)
|
|
|
|
{
|
|
|
|
if (_isCreation)
|
2016-12-06 22:52:39 +00:00
|
|
|
cout << "CREATE " << m_sender.hex() << ":" << endl;
|
2016-12-06 17:38:59 +00:00
|
|
|
else
|
2016-12-06 22:52:39 +00:00
|
|
|
cout << "CALL " << m_sender.hex() << " -> " << m_contractAddress.hex() << ":" << endl;
|
|
|
|
if (_value > 0)
|
|
|
|
cout << " value: " << _value << endl;
|
2021-09-16 14:33:28 +00:00
|
|
|
cout << " in: " << util::toHex(_data) << endl;
|
2016-12-06 17:38:59 +00:00
|
|
|
}
|
2022-09-16 10:56:23 +00:00
|
|
|
evmc_message message{};
|
2019-06-26 16:42:24 +00:00
|
|
|
message.input_data = _data.data();
|
|
|
|
message.input_size = _data.size();
|
|
|
|
message.sender = EVMHost::convertToEVMC(m_sender);
|
|
|
|
message.value = EVMHost::convertToEVMC(_value);
|
|
|
|
|
|
|
|
if (_isCreation)
|
2016-11-28 00:21:01 +00:00
|
|
|
{
|
2019-06-26 16:42:24 +00:00
|
|
|
message.kind = EVMC_CREATE;
|
2022-09-16 10:56:23 +00:00
|
|
|
message.recipient = {};
|
|
|
|
message.code_address = {};
|
2016-11-28 00:21:01 +00:00
|
|
|
}
|
2019-06-26 16:42:24 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
message.kind = EVMC_CALL;
|
2022-09-16 10:56:23 +00:00
|
|
|
message.recipient = EVMHost::convertToEVMC(m_contractAddress);
|
|
|
|
message.code_address = message.recipient;
|
2019-06-26 16:42:24 +00:00
|
|
|
}
|
2022-09-16 10:56:23 +00:00
|
|
|
|
2021-11-15 13:42:10 +00:00
|
|
|
message.gas = InitialGas.convert_to<int64_t>();
|
2016-11-28 00:21:01 +00:00
|
|
|
|
2022-09-16 10:56:23 +00:00
|
|
|
evmc::Result result = m_evmcHost->call(message);
|
2017-02-07 16:20:05 +00:00
|
|
|
|
2019-06-26 16:42:24 +00:00
|
|
|
m_output = bytes(result.output_data, result.output_data + result.output_size);
|
2016-11-28 00:21:01 +00:00
|
|
|
if (_isCreation)
|
2019-06-26 16:42:24 +00:00
|
|
|
m_contractAddress = EVMHost::convertFromEVMC(result.create_address);
|
|
|
|
|
2021-11-15 13:42:10 +00:00
|
|
|
m_gasUsed = InitialGas - result.gas_left;
|
2019-06-26 16:42:24 +00:00
|
|
|
m_transactionSuccessful = (result.status_code == EVMC_SUCCESS);
|
2016-11-28 00:21:01 +00:00
|
|
|
|
2016-12-06 17:38:59 +00:00
|
|
|
if (m_showMessages)
|
2016-12-09 13:18:10 +00:00
|
|
|
{
|
2021-09-16 14:33:28 +00:00
|
|
|
cout << " out: " << util::toHex(m_output) << endl;
|
2020-06-03 08:27:23 +00:00
|
|
|
cout << " result: " << static_cast<size_t>(result.status_code) << endl;
|
2019-06-26 16:42:24 +00:00
|
|
|
cout << " gas used: " << m_gasUsed.str() << endl;
|
2016-12-09 13:18:10 +00:00
|
|
|
}
|
2019-06-26 16:42:24 +00:00
|
|
|
}
|
2016-12-06 17:38:59 +00:00
|
|
|
|
2020-11-13 16:03:49 +00:00
|
|
|
void ExecutionFramework::sendEther(h160 const& _addr, u256 const& _amount)
|
2019-06-26 16:42:24 +00:00
|
|
|
{
|
2020-06-05 23:10:48 +00:00
|
|
|
m_evmcHost->newBlock();
|
2019-06-26 16:42:24 +00:00
|
|
|
|
|
|
|
if (m_showMessages)
|
2016-11-28 00:21:01 +00:00
|
|
|
{
|
2019-06-26 16:42:24 +00:00
|
|
|
cout << "SEND_ETHER " << m_sender.hex() << " -> " << _addr.hex() << ":" << endl;
|
|
|
|
if (_amount > 0)
|
|
|
|
cout << " value: " << _amount << endl;
|
2016-11-28 00:21:01 +00:00
|
|
|
}
|
2022-09-16 10:56:23 +00:00
|
|
|
evmc_message message{};
|
2019-06-26 16:42:24 +00:00
|
|
|
message.sender = EVMHost::convertToEVMC(m_sender);
|
|
|
|
message.value = EVMHost::convertToEVMC(_amount);
|
|
|
|
message.kind = EVMC_CALL;
|
2022-09-16 10:56:23 +00:00
|
|
|
message.recipient = EVMHost::convertToEVMC(_addr);
|
|
|
|
message.code_address = message.recipient;
|
2021-11-15 13:42:10 +00:00
|
|
|
message.gas = InitialGas.convert_to<int64_t>();
|
2018-06-15 10:18:00 +00:00
|
|
|
|
2020-06-05 23:10:48 +00:00
|
|
|
m_evmcHost->call(message);
|
2019-06-26 16:42:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t ExecutionFramework::currentTimestamp()
|
|
|
|
{
|
2020-06-05 23:10:48 +00:00
|
|
|
return static_cast<size_t>(m_evmcHost->tx_context.block_timestamp);
|
2019-06-26 16:42:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t ExecutionFramework::blockTimestamp(u256 _block)
|
|
|
|
{
|
|
|
|
if (_block > blockNumber())
|
|
|
|
return 0;
|
2018-06-15 10:18:00 +00:00
|
|
|
else
|
2020-06-03 08:27:23 +00:00
|
|
|
return static_cast<size_t>((currentTimestamp() / blockNumber()) * _block);
|
2016-11-28 00:21:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 16:03:49 +00:00
|
|
|
h160 ExecutionFramework::account(size_t _idx)
|
2016-11-28 00:21:01 +00:00
|
|
|
{
|
2020-11-13 16:03:49 +00:00
|
|
|
return h160(h256(u256{"0x1212121212121212121212121212120000000012"} + _idx * 0x1000), h160::AlignRight);
|
2019-06-26 16:42:24 +00:00
|
|
|
}
|
2016-11-28 00:21:01 +00:00
|
|
|
|
2021-05-01 03:13:08 +00:00
|
|
|
bool ExecutionFramework::addressHasCode(h160 const& _addr) const
|
2019-06-26 16:42:24 +00:00
|
|
|
{
|
2020-06-05 23:10:48 +00:00
|
|
|
return m_evmcHost->get_code_size(EVMHost::convertToEVMC(_addr)) != 0;
|
2016-11-28 00:21:01 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 16:42:24 +00:00
|
|
|
size_t ExecutionFramework::numLogs() const
|
2016-11-28 00:21:01 +00:00
|
|
|
{
|
2020-06-05 23:10:48 +00:00
|
|
|
return m_evmcHost->recorded_logs.size();
|
2016-11-28 00:21:01 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 16:42:24 +00:00
|
|
|
size_t ExecutionFramework::numLogTopics(size_t _logIdx) const
|
2017-02-07 16:20:05 +00:00
|
|
|
{
|
2020-06-05 23:10:48 +00:00
|
|
|
return m_evmcHost->recorded_logs.at(_logIdx).topics.size();
|
2017-02-07 16:20:05 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 16:42:24 +00:00
|
|
|
h256 ExecutionFramework::logTopic(size_t _logIdx, size_t _topicIdx) const
|
2016-11-28 00:21:01 +00:00
|
|
|
{
|
2020-06-05 23:10:48 +00:00
|
|
|
return EVMHost::convertFromEVMC(m_evmcHost->recorded_logs.at(_logIdx).topics.at(_topicIdx));
|
2016-11-28 00:21:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 16:03:49 +00:00
|
|
|
h160 ExecutionFramework::logAddress(size_t _logIdx) const
|
2019-06-26 16:42:24 +00:00
|
|
|
{
|
2020-06-05 23:10:48 +00:00
|
|
|
return EVMHost::convertFromEVMC(m_evmcHost->recorded_logs.at(_logIdx).creator);
|
2019-06-26 16:42:24 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 10:38:47 +00:00
|
|
|
bytes ExecutionFramework::logData(size_t _logIdx) const
|
2016-11-28 00:21:01 +00:00
|
|
|
{
|
2022-03-04 11:20:57 +00:00
|
|
|
auto const& data = m_evmcHost->recorded_logs.at(_logIdx).data;
|
2019-11-28 10:38:47 +00:00
|
|
|
// TODO: Return a copy of log data, because this is expected from REQUIRE_LOG_DATA(),
|
|
|
|
// but reference type like string_view would be preferable.
|
|
|
|
return {data.begin(), data.end()};
|
2016-11-28 00:21:01 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 03:13:08 +00:00
|
|
|
u256 ExecutionFramework::balanceAt(h160 const& _addr) const
|
2016-11-28 00:21:01 +00:00
|
|
|
{
|
2020-06-05 23:10:48 +00:00
|
|
|
return u256(EVMHost::convertFromEVMC(m_evmcHost->get_balance(EVMHost::convertToEVMC(_addr))));
|
2016-11-28 00:21:01 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 03:13:08 +00:00
|
|
|
bool ExecutionFramework::storageEmpty(h160 const& _addr) const
|
2016-11-28 00:21:01 +00:00
|
|
|
{
|
2020-06-05 23:10:48 +00:00
|
|
|
const auto it = m_evmcHost->accounts.find(EVMHost::convertToEVMC(_addr));
|
|
|
|
if (it != m_evmcHost->accounts.end())
|
2019-06-26 16:42:24 +00:00
|
|
|
{
|
2019-11-29 23:10:41 +00:00
|
|
|
for (auto const& entry: it->second.storage)
|
2022-11-08 20:48:56 +00:00
|
|
|
if (entry.second.current != evmc::bytes32{})
|
2019-06-26 16:42:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2016-11-28 00:21:01 +00:00
|
|
|
}
|
2021-04-29 23:09:36 +00:00
|
|
|
|
|
|
|
vector<solidity::frontend::test::LogRecord> ExecutionFramework::recordedLogs() const
|
|
|
|
{
|
|
|
|
vector<LogRecord> logs;
|
|
|
|
for (evmc::MockedHost::log_record const& logRecord: m_evmcHost->recorded_logs)
|
|
|
|
logs.emplace_back(
|
|
|
|
EVMHost::convertFromEVMC(logRecord.creator),
|
|
|
|
bytes{logRecord.data.begin(), logRecord.data.end()},
|
|
|
|
logRecord.topics | ranges::views::transform([](evmc::bytes32 _bytes) { return EVMHost::convertFromEVMC(_bytes); }) | ranges::to<vector>
|
|
|
|
);
|
|
|
|
return logs;
|
|
|
|
}
|