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 2014
|
|
|
|
* Framework for executing contracts and testing them using RPC.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-01-14 16:48:17 +00:00
|
|
|
#include <test/Common.h>
|
2020-06-05 23:10:48 +00:00
|
|
|
#include <test/EVMHost.h>
|
2016-11-28 00:21:01 +00:00
|
|
|
|
2019-03-12 14:14:12 +00:00
|
|
|
#include <libsolidity/interface/OptimiserSettings.h>
|
2019-09-18 14:44:36 +00:00
|
|
|
#include <libsolidity/interface/DebugSettings.h>
|
2019-03-12 14:14:12 +00:00
|
|
|
|
2018-11-14 13:59:30 +00:00
|
|
|
#include <liblangutil/EVMVersion.h>
|
2018-02-22 16:21:26 +00:00
|
|
|
|
2022-09-27 12:13:44 +00:00
|
|
|
#include <libsolutil/FunctionSelector.h>
|
2020-10-13 11:28:39 +00:00
|
|
|
#include <libsolutil/ErrorCodes.h>
|
2017-08-25 09:57:33 +00:00
|
|
|
|
|
|
|
#include <functional>
|
2016-11-28 00:21:01 +00:00
|
|
|
|
2021-12-20 12:11:00 +00:00
|
|
|
#include <boost/rational.hpp>
|
2020-01-14 14:55:31 +00:00
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2021-04-29 23:09:36 +00:00
|
|
|
namespace solidity::frontend::test
|
|
|
|
{
|
|
|
|
struct LogRecord;
|
|
|
|
} // namespace solidity::frontend::test
|
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
namespace solidity::test
|
2016-11-28 00:21:01 +00:00
|
|
|
{
|
2019-12-23 15:50:30 +00:00
|
|
|
using rational = boost::rational<bigint>;
|
2019-06-26 16:42:24 +00:00
|
|
|
|
2020-07-08 17:45:30 +00:00
|
|
|
// The ether and gwei denominations; here for ease of use where needed within code.
|
2021-08-14 18:29:59 +00:00
|
|
|
static u256 const gwei = u256(1) << 9;
|
|
|
|
static u256 const ether = u256(1) << 18;
|
2016-11-28 00:21:01 +00:00
|
|
|
class ExecutionFramework
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
ExecutionFramework();
|
2020-06-05 23:10:48 +00:00
|
|
|
ExecutionFramework(langutil::EVMVersion _evmVersion, std::vector<boost::filesystem::path> const& _vmPaths);
|
2018-05-02 11:29:16 +00:00
|
|
|
virtual ~ExecutionFramework() = default;
|
2016-11-28 00:21:01 +00:00
|
|
|
|
|
|
|
virtual bytes const& compileAndRunWithoutCheck(
|
2020-06-18 10:31:55 +00:00
|
|
|
std::map<std::string, std::string> const& _sourceCode,
|
2016-11-28 00:21:01 +00:00
|
|
|
u256 const& _value = 0,
|
|
|
|
std::string const& _contractName = "",
|
2020-06-18 10:31:55 +00:00
|
|
|
bytes const& _arguments = {},
|
2021-03-15 22:52:24 +00:00
|
|
|
std::map<std::string, util::h160> const& _libraryAddresses = {},
|
|
|
|
std::optional<std::string> const& _sourceName = std::nullopt
|
2016-11-28 00:21:01 +00:00
|
|
|
) = 0;
|
|
|
|
|
|
|
|
bytes const& compileAndRun(
|
|
|
|
std::string const& _sourceCode,
|
|
|
|
u256 const& _value = 0,
|
|
|
|
std::string const& _contractName = "",
|
2020-06-18 10:31:55 +00:00
|
|
|
bytes const& _arguments = {},
|
2020-11-13 16:03:49 +00:00
|
|
|
std::map<std::string, util::h160> const& _libraryAddresses = {}
|
2016-11-28 00:21:01 +00:00
|
|
|
)
|
|
|
|
{
|
2020-06-18 10:31:55 +00:00
|
|
|
compileAndRunWithoutCheck(
|
|
|
|
{{"", _sourceCode}},
|
|
|
|
_value,
|
|
|
|
_contractName,
|
|
|
|
_arguments,
|
|
|
|
_libraryAddresses
|
|
|
|
);
|
2018-06-15 10:18:00 +00:00
|
|
|
BOOST_REQUIRE(m_transactionSuccessful);
|
2016-11-28 00:21:01 +00:00
|
|
|
BOOST_REQUIRE(!m_output.empty());
|
|
|
|
return m_output;
|
|
|
|
}
|
|
|
|
|
2016-11-28 11:19:31 +00:00
|
|
|
bytes const& callFallbackWithValue(u256 const& _value)
|
|
|
|
{
|
|
|
|
sendMessage(bytes(), false, _value);
|
|
|
|
return m_output;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes const & callFallback()
|
|
|
|
{
|
|
|
|
return callFallbackWithValue(0);
|
|
|
|
}
|
|
|
|
|
2019-09-09 18:09:15 +00:00
|
|
|
bytes const& callLowLevel(bytes const& _data, u256 const& _value)
|
|
|
|
{
|
|
|
|
sendMessage(_data, false, _value);
|
|
|
|
return m_output;
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:16:45 +00:00
|
|
|
bytes const& callContractFunctionWithValueNoEncoding(std::string _sig, u256 const& _value, bytes const& _arguments)
|
2016-11-28 00:21:01 +00:00
|
|
|
{
|
2022-09-27 12:13:44 +00:00
|
|
|
sendMessage(util::selectorFromSignatureH32(_sig).asBytes() + _arguments, false, _value);
|
2016-11-28 00:21:01 +00:00
|
|
|
return m_output;
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:16:45 +00:00
|
|
|
bytes const& callContractFunctionNoEncoding(std::string _sig, bytes const& _arguments)
|
|
|
|
{
|
|
|
|
return callContractFunctionWithValueNoEncoding(_sig, 0, _arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... Args>
|
|
|
|
bytes const& callContractFunctionWithValue(std::string _sig, u256 const& _value, Args const&... _arguments)
|
|
|
|
{
|
|
|
|
return callContractFunctionWithValueNoEncoding(_sig, _value, encodeArgs(_arguments...));
|
|
|
|
}
|
|
|
|
|
2016-11-28 00:21:01 +00:00
|
|
|
template <class... Args>
|
|
|
|
bytes const& callContractFunction(std::string _sig, Args const&... _arguments)
|
|
|
|
{
|
|
|
|
return callContractFunctionWithValue(_sig, 0, _arguments...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class CppFunction, class... Args>
|
2016-11-29 21:46:52 +00:00
|
|
|
void testContractAgainstCpp(std::string _sig, CppFunction const& _cppFunction, Args const&... _arguments)
|
2016-11-28 00:21:01 +00:00
|
|
|
{
|
2016-11-29 21:46:52 +00:00
|
|
|
bytes contractResult = callContractFunction(_sig, _arguments...);
|
2016-11-28 00:21:01 +00:00
|
|
|
bytes cppResult = callCppAndEncodeResult(_cppFunction, _arguments...);
|
|
|
|
BOOST_CHECK_MESSAGE(
|
2016-11-29 21:46:52 +00:00
|
|
|
contractResult == cppResult,
|
|
|
|
"Computed values do not match.\nContract: " +
|
2019-12-23 15:50:30 +00:00
|
|
|
util::toHex(contractResult) +
|
2016-11-28 00:21:01 +00:00
|
|
|
"\nC++: " +
|
2019-12-23 15:50:30 +00:00
|
|
|
util::toHex(cppResult)
|
2016-11-28 00:21:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class CppFunction, class... Args>
|
2016-11-29 21:46:52 +00:00
|
|
|
void testContractAgainstCppOnRange(std::string _sig, CppFunction const& _cppFunction, u256 const& _rangeStart, u256 const& _rangeEnd)
|
2016-11-28 00:21:01 +00:00
|
|
|
{
|
|
|
|
for (u256 argument = _rangeStart; argument < _rangeEnd; ++argument)
|
|
|
|
{
|
2016-11-29 21:46:52 +00:00
|
|
|
bytes contractResult = callContractFunction(_sig, argument);
|
2016-11-28 00:21:01 +00:00
|
|
|
bytes cppResult = callCppAndEncodeResult(_cppFunction, argument);
|
|
|
|
BOOST_CHECK_MESSAGE(
|
2016-11-29 21:46:52 +00:00
|
|
|
contractResult == cppResult,
|
|
|
|
"Computed values do not match.\nContract: " +
|
2019-12-23 15:50:30 +00:00
|
|
|
util::toHex(contractResult) +
|
2016-11-28 00:21:01 +00:00
|
|
|
"\nC++: " +
|
2019-12-23 15:50:30 +00:00
|
|
|
util::toHex(cppResult) +
|
2016-11-28 00:21:01 +00:00
|
|
|
"\nArgument: " +
|
2019-12-23 15:50:30 +00:00
|
|
|
util::toHex(encode(argument))
|
2016-11-28 00:21:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-20 12:23:00 +00:00
|
|
|
static std::pair<bool, std::string> compareAndCreateMessage(bytes const& _result, bytes const& _expectation);
|
|
|
|
|
2018-11-07 11:04:46 +00:00
|
|
|
static bytes encode(bool _value) { return encode(uint8_t(_value)); }
|
2016-11-28 00:21:01 +00:00
|
|
|
static bytes encode(int _value) { return encode(u256(_value)); }
|
|
|
|
static bytes encode(size_t _value) { return encode(u256(_value)); }
|
|
|
|
static bytes encode(char const* _value) { return encode(std::string(_value)); }
|
2018-11-07 11:04:46 +00:00
|
|
|
static bytes encode(uint8_t _value) { return bytes(31, 0) + bytes{_value}; }
|
2021-09-16 14:33:28 +00:00
|
|
|
static bytes encode(u256 const& _value) { return toBigEndian(_value); }
|
2016-11-28 00:21:01 +00:00
|
|
|
/// @returns the fixed-point encoding of a rational number with a given
|
|
|
|
/// number of fractional bits.
|
|
|
|
static bytes encode(std::pair<rational, int> const& _valueAndPrecision)
|
|
|
|
{
|
|
|
|
rational const& value = _valueAndPrecision.first;
|
|
|
|
int fractionalBits = _valueAndPrecision.second;
|
|
|
|
return encode(u256((value.numerator() << fractionalBits) / value.denominator()));
|
|
|
|
}
|
2019-12-23 15:50:30 +00:00
|
|
|
static bytes encode(util::h256 const& _value) { return _value.asBytes(); }
|
2020-11-13 16:03:49 +00:00
|
|
|
static bytes encode(util::h160 const& _value) { return encode(util::h256(_value, util::h256::AlignRight)); }
|
2016-11-28 00:21:01 +00:00
|
|
|
static bytes encode(bytes const& _value, bool _padLeft = true)
|
|
|
|
{
|
|
|
|
bytes padding = bytes((32 - _value.size() % 32) % 32, 0);
|
|
|
|
return _padLeft ? padding + _value : _value + padding;
|
|
|
|
}
|
2019-12-23 15:50:30 +00:00
|
|
|
static bytes encode(std::string const& _value) { return encode(util::asBytes(_value), false); }
|
2020-02-17 17:44:39 +00:00
|
|
|
template <class T>
|
|
|
|
static bytes encode(std::vector<T> const& _value)
|
2016-11-28 00:21:01 +00:00
|
|
|
{
|
|
|
|
bytes ret;
|
|
|
|
for (auto const& v: _value)
|
|
|
|
ret += encode(v);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class FirstArg, class... Args>
|
|
|
|
static bytes encodeArgs(FirstArg const& _firstArg, Args const&... _followingArgs)
|
|
|
|
{
|
|
|
|
return encode(_firstArg) + encodeArgs(_followingArgs...);
|
|
|
|
}
|
|
|
|
static bytes encodeArgs()
|
|
|
|
{
|
|
|
|
return bytes();
|
|
|
|
}
|
2020-10-13 11:28:39 +00:00
|
|
|
/// @returns error returndata corresponding to the Panic(uint256) error code,
|
|
|
|
/// if REVERT is supported by the current EVM version and the empty string otherwise.
|
|
|
|
bytes panicData(util::PanicCode _code);
|
2017-10-18 23:12:32 +00:00
|
|
|
|
2016-11-28 00:21:01 +00:00
|
|
|
//@todo might be extended in the future
|
|
|
|
template <class Arg>
|
|
|
|
static bytes encodeDyn(Arg const& _arg)
|
|
|
|
{
|
|
|
|
return encodeArgs(u256(0x20), u256(_arg.size()), _arg);
|
|
|
|
}
|
|
|
|
|
2018-08-10 17:19:21 +00:00
|
|
|
u256 gasLimit() const;
|
|
|
|
u256 gasPrice() const;
|
|
|
|
u256 blockHash(u256 const& _blockNumber) const;
|
2019-06-26 16:42:24 +00:00
|
|
|
u256 blockNumber() const;
|
2018-08-10 17:19:21 +00:00
|
|
|
|
2019-03-05 11:01:44 +00:00
|
|
|
template<typename Range>
|
2019-03-06 21:01:19 +00:00
|
|
|
static bytes encodeArray(bool _dynamicallySized, bool _dynamicallyEncoded, Range const& _elements)
|
2019-03-05 11:01:44 +00:00
|
|
|
{
|
|
|
|
bytes result;
|
|
|
|
if (_dynamicallySized)
|
|
|
|
result += encode(u256(_elements.size()));
|
|
|
|
if (_dynamicallyEncoded)
|
|
|
|
{
|
|
|
|
u256 offset = u256(_elements.size()) * 32;
|
|
|
|
std::vector<bytes> subEncodings;
|
|
|
|
for (auto const& element: _elements)
|
|
|
|
{
|
|
|
|
result += encode(offset);
|
|
|
|
subEncodings.emplace_back(encode(element));
|
|
|
|
offset += subEncodings.back().size();
|
|
|
|
}
|
|
|
|
for (auto const& subEncoding: subEncodings)
|
|
|
|
result += subEncoding;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
for (auto const& element: _elements)
|
|
|
|
result += encode(element);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-20 23:11:29 +00:00
|
|
|
util::h160 setAccount(size_t _accountNumber)
|
|
|
|
{
|
|
|
|
m_sender = account(_accountNumber);
|
|
|
|
return m_sender;
|
|
|
|
}
|
|
|
|
|
2021-04-29 23:09:36 +00:00
|
|
|
size_t numLogs() const;
|
|
|
|
size_t numLogTopics(size_t _logIdx) const;
|
|
|
|
util::h256 logTopic(size_t _logIdx, size_t _topicIdx) const;
|
|
|
|
util::h160 logAddress(size_t _logIdx) const;
|
|
|
|
bytes logData(size_t _logIdx) const;
|
|
|
|
|
2016-11-28 00:21:01 +00:00
|
|
|
private:
|
|
|
|
template <class CppFunction, class... Args>
|
|
|
|
auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments)
|
|
|
|
-> typename std::enable_if<std::is_void<decltype(_cppFunction(_arguments...))>::value, bytes>::type
|
|
|
|
{
|
|
|
|
_cppFunction(_arguments...);
|
|
|
|
return bytes();
|
|
|
|
}
|
|
|
|
template <class CppFunction, class... Args>
|
|
|
|
auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments)
|
|
|
|
-> typename std::enable_if<!std::is_void<decltype(_cppFunction(_arguments...))>::value, bytes>::type
|
|
|
|
{
|
|
|
|
return encode(_cppFunction(_arguments...));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2021-11-15 13:42:10 +00:00
|
|
|
u256 const InitialGas = 100000000;
|
|
|
|
|
2020-06-05 23:10:48 +00:00
|
|
|
void selectVM(evmc_capabilities _cap = evmc_capabilities::EVMC_CAPABILITY_EVM1);
|
2020-03-03 11:21:16 +00:00
|
|
|
void reset();
|
|
|
|
|
2016-11-28 00:21:01 +00:00
|
|
|
void sendMessage(bytes const& _data, bool _isCreation, u256 const& _value = 0);
|
2020-11-13 16:03:49 +00:00
|
|
|
void sendEther(util::h160 const& _to, u256 const& _value);
|
2016-11-28 00:21:01 +00:00
|
|
|
size_t currentTimestamp();
|
2017-08-22 17:24:02 +00:00
|
|
|
size_t blockTimestamp(u256 _number);
|
2016-11-28 00:21:01 +00:00
|
|
|
|
|
|
|
/// @returns the (potentially newly created) _ith address.
|
2020-11-13 16:03:49 +00:00
|
|
|
util::h160 account(size_t _i);
|
2016-11-28 00:21:01 +00:00
|
|
|
|
2021-05-01 03:13:08 +00:00
|
|
|
u256 balanceAt(util::h160 const& _addr) const;
|
|
|
|
bool storageEmpty(util::h160 const& _addr) const;
|
|
|
|
bool addressHasCode(util::h160 const& _addr) const;
|
2016-11-28 00:21:01 +00:00
|
|
|
|
2021-04-29 23:09:36 +00:00
|
|
|
std::vector<frontend::test::LogRecord> recordedLogs() const;
|
2016-11-28 00:21:01 +00:00
|
|
|
|
2019-02-25 14:29:57 +00:00
|
|
|
langutil::EVMVersion m_evmVersion;
|
2019-12-23 15:50:30 +00:00
|
|
|
solidity::frontend::RevertStrings m_revertStrings = solidity::frontend::RevertStrings::Default;
|
|
|
|
solidity::frontend::OptimiserSettings m_optimiserSettings = solidity::frontend::OptimiserSettings::minimal();
|
2016-12-06 17:38:59 +00:00
|
|
|
bool m_showMessages = false;
|
2020-06-05 23:10:48 +00:00
|
|
|
std::unique_ptr<EVMHost> m_evmcHost;
|
|
|
|
|
|
|
|
std::vector<boost::filesystem::path> m_vmPaths;
|
2019-06-26 16:42:24 +00:00
|
|
|
|
2018-06-15 10:18:00 +00:00
|
|
|
bool m_transactionSuccessful = true;
|
2020-11-13 16:03:49 +00:00
|
|
|
util::h160 m_sender = account(0);
|
|
|
|
util::h160 m_contractAddress;
|
2016-11-28 00:21:01 +00:00
|
|
|
bytes m_output;
|
|
|
|
u256 m_gasUsed;
|
|
|
|
};
|
|
|
|
|
2017-09-20 12:23:00 +00:00
|
|
|
#define ABI_CHECK(result, expectation) do { \
|
|
|
|
auto abiCheckResult = ExecutionFramework::compareAndCreateMessage((result), (expectation)); \
|
|
|
|
BOOST_CHECK_MESSAGE(abiCheckResult.first, abiCheckResult.second); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2016-11-28 00:21:01 +00:00
|
|
|
} // end namespaces
|