aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/CMakeLists.txt121
-rw-r--r--test/TestHelper.cpp892
-rw-r--r--test/TestHelper.h290
-rw-r--r--test/TestUtils.cpp122
-rw-r--r--test/TestUtils.h83
-rw-r--r--test/boostTest.cpp92
-rw-r--r--test/contracts/AuctionRegistrar.cpp497
-rw-r--r--test/contracts/CMakeLists.txt5
-rw-r--r--test/contracts/FixedFeeRegistrar.cpp242
-rw-r--r--test/contracts/Wallet.cpp668
-rw-r--r--test/libsolidity/Assembly.cpp120
-rw-r--r--test/libsolidity/CMakeLists.txt5
-rw-r--r--test/libsolidity/GasMeter.cpp235
-rw-r--r--test/libsolidity/SolidityABIJSON.cpp602
-rw-r--r--test/libsolidity/SolidityEndToEndTest.cpp5168
-rw-r--r--test/libsolidity/SolidityExpressionCompiler.cpp491
-rw-r--r--test/libsolidity/SolidityInterface.cpp149
-rw-r--r--test/libsolidity/SolidityNameAndTypeResolution.cpp2201
-rw-r--r--test/libsolidity/SolidityNatspecJSON.cpp534
-rw-r--r--test/libsolidity/SolidityOptimizer.cpp1132
-rw-r--r--test/libsolidity/SolidityParser.cpp921
-rw-r--r--test/libsolidity/SolidityScanner.cpp288
-rw-r--r--test/libsolidity/SolidityTypes.cpp93
-rw-r--r--test/libsolidity/solidityExecutionFramework.h310
24 files changed, 15261 insertions, 0 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
new file mode 100644
index 00000000..f7475112
--- /dev/null
+++ b/test/CMakeLists.txt
@@ -0,0 +1,121 @@
+cmake_policy(SET CMP0015 NEW)
+
+aux_source_directory(. SRC_LIST)
+
+macro (add_sources)
+ file (RELATIVE_PATH _relPath "${CMAKE_SOURCE_DIR}/test" "${CMAKE_CURRENT_SOURCE_DIR}")
+ foreach (_src ${ARGN})
+ if (_relPath)
+ list (APPEND SRC "${_relPath}/${_src}")
+ else()
+ list (APPEND SRC "${_src}")
+ endif()
+ endforeach()
+ if (_relPath)
+ # propagate SRCS to parent directory
+ set (SRC ${SRC} PARENT_SCOPE)
+ endif()
+endmacro()
+
+add_subdirectory(fuzzTesting)
+add_subdirectory(libdevcore)
+add_subdirectory(libdevcrypto)
+add_subdirectory(libethcore)
+add_subdirectory(libethereum)
+add_subdirectory(libevm)
+add_subdirectory(libnatspec)
+add_subdirectory(libp2p)
+add_subdirectory(external-dependencies)
+
+if (JSCONSOLE)
+ add_subdirectory(libjsengine)
+endif()
+
+if (SOLIDITY)
+ add_subdirectory(libsolidity)
+ add_subdirectory(contracts)
+endif ()
+if (JSONRPC)
+add_subdirectory(libweb3jsonrpc)
+endif ()
+add_subdirectory(libwhisper)
+
+set(SRC_LIST ${SRC_LIST} ${SRC})
+
+include_directories(BEFORE ${JSONCPP_INCLUDE_DIRS})
+include_directories(BEFORE ..)
+include_directories(${Boost_INCLUDE_DIRS})
+include_directories(${CRYPTOPP_INCLUDE_DIRS})
+include_directories(${JSON_RPC_CPP_INCLUDE_DIRS})
+
+if (JSCONSOLE)
+ include_directories(${V8_INCLUDE_DIRS})
+endif()
+
+# search for test names and create ctest tests
+enable_testing()
+foreach(file ${SRC_LIST})
+ file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/${file} test_list_raw REGEX "BOOST_.*TEST_(SUITE|CASE)")
+ set(TestSuite "DEFAULT")
+ foreach(test_raw ${test_list_raw})
+ string(REGEX REPLACE ".*TEST_(SUITE|CASE)\\(([^ ,\\)]*).*" "\\1 \\2" test ${test_raw})
+ if(test MATCHES "^SUITE .*")
+ string(SUBSTRING ${test} 6 -1 TestSuite)
+ elseif(test MATCHES "^CASE .*")
+ string(SUBSTRING ${test} 5 -1 TestCase)
+ add_test(NAME ${TestSuite}/${TestCase} WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/test COMMAND testeth -t ${TestSuite}/${TestCase})
+ endif(test MATCHES "^SUITE .*")
+ endforeach(test_raw)
+endforeach(file)
+
+file(GLOB HEADERS "*.h")
+add_executable(testeth ${SRC_LIST} ${HEADERS})
+
+target_link_libraries(testeth ${Boost_UNIT_TEST_FRAMEWORK_LIBRARIES})
+target_link_libraries(testeth ${CURL_LIBRARIES})
+target_link_libraries(testeth ${CRYPTOPP_LIBRARIES})
+target_link_libraries(testeth ethereum)
+target_link_libraries(testeth ethcore)
+if (NOT WIN32)
+ target_link_libraries(testeth secp256k1)
+endif ()
+
+if (JSCONSOLE)
+ target_link_libraries(testeth jsengine)
+endif()
+
+if (SOLIDITY)
+ target_link_libraries(testeth solidity)
+endif ()
+
+target_link_libraries(testeth testutils)
+
+if (GUI AND NOT JUSTTESTS)
+ target_link_libraries(testeth webthree)
+ target_link_libraries(testeth natspec)
+endif()
+
+if (JSONRPC)
+ target_link_libraries(testeth web3jsonrpc)
+ target_link_libraries(testeth ${JSON_RPC_CPP_CLIENT_LIBRARIES})
+endif()
+
+enable_testing()
+set(CTEST_OUTPUT_ON_FAILURE TRUE)
+
+include(EthUtils)
+
+eth_add_test(ClientBase
+ ARGS --eth_testfile=BlockTests/bcJS_API_Test --eth_threads=1
+ ARGS --eth_testfile=BlockTests/bcJS_API_Test --eth_threads=3
+ ARGS --eth_testfile=BlockTests/bcJS_API_Test --eth_threads=10
+ ARGS --eth_testfile=BlockTests/bcValidBlockTest --eth_threads=1
+ ARGS --eth_testfile=BlockTests/bcValidBlockTest --eth_threads=3
+ ARGS --eth_testfile=BlockTests/bcValidBlockTest --eth_threads=10
+)
+
+eth_add_test(JsonRpc
+ ARGS --eth_testfile=BlockTests/bcJS_API_Test
+ ARGS --eth_testfile=BlockTests/bcValidBlockTest
+)
+
diff --git a/test/TestHelper.cpp b/test/TestHelper.cpp
new file mode 100644
index 00000000..e7633c5e
--- /dev/null
+++ b/test/TestHelper.cpp
@@ -0,0 +1,892 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum 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.
+
+ cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+*/
+/** @file TestHelper.cpp
+ * @author Marko Simovic <markobarko@gmail.com>
+ * @date 2014
+ */
+
+#include "TestHelper.h"
+
+#include <thread>
+#include <chrono>
+#include <libethcore/EthashAux.h>
+#include <libethereum/Client.h>
+#include <libevm/ExtVMFace.h>
+#include <liblll/Compiler.h>
+#include <libevm/VMFactory.h>
+#include "Stats.h"
+
+using namespace std;
+using namespace dev::eth;
+
+namespace dev
+{
+namespace eth
+{
+
+void mine(Client& c, int numBlocks)
+{
+ auto startBlock = c.blockChain().details().number;
+
+ c.startMining();
+ while(c.blockChain().details().number < startBlock + numBlocks)
+ std::this_thread::sleep_for(std::chrono::milliseconds(100));
+ c.stopMining();
+}
+
+void connectClients(Client& c1, Client& c2)
+{
+ (void)c1;
+ (void)c2;
+ // TODO: Move to WebThree. eth::Client no longer handles networking.
+#if 0
+ short c1Port = 20000;
+ short c2Port = 21000;
+ c1.startNetwork(c1Port);
+ c2.startNetwork(c2Port);
+ c2.connect("127.0.0.1", c1Port);
+#endif
+}
+
+void mine(Block& s, BlockChain const& _bc)
+{
+ std::unique_ptr<SealEngineFace> sealer(Ethash::createSealEngine());
+ s.commitToSeal(_bc);
+ Notified<bytes> sealed;
+ sealer->onSealGenerated([&](bytes const& sealedHeader){ sealed = sealedHeader; });
+ sealer->generateSeal(s.info());
+ sealed.waitNot({});
+ sealer.reset();
+ s.sealBlock(sealed);
+}
+
+void mine(Ethash::BlockHeader& _bi)
+{
+ std::unique_ptr<SealEngineFace> sealer(Ethash::createSealEngine());
+ Notified<bytes> sealed;
+ sealer->onSealGenerated([&](bytes const& sealedHeader){ sealed = sealedHeader; });
+ sealer->generateSeal(_bi);
+ sealed.waitNot({});
+ sealer.reset();
+ _bi = Ethash::BlockHeader(sealed, CheckNothing, h256{}, HeaderData);
+}
+
+}
+
+namespace test
+{
+
+struct ValueTooLarge: virtual Exception {};
+struct MissingFields : virtual Exception {};
+
+bigint const c_max256plus1 = bigint(1) << 256;
+
+ImportTest::ImportTest(json_spirit::mObject& _o, bool isFiller, testType testTemplate):
+ m_statePre(OverlayDB(), eth::BaseState::Empty),
+ m_statePost(OverlayDB(), eth::BaseState::Empty),
+ m_testObject(_o)
+{
+ if (testTemplate == testType::StateTests)
+ {
+ importEnv(_o["env"].get_obj());
+ importTransaction(_o["transaction"].get_obj());
+ importState(_o["pre"].get_obj(), m_statePre);
+ if (!isFiller)
+ {
+ if (_o.count("post"))
+ importState(_o["post"].get_obj(), m_statePost);
+ else
+ importState(_o["postState"].get_obj(), m_statePost);
+ m_logsExpected = importLog(_o["logs"].get_array());
+ }
+ }
+}
+
+//executes an imported transacton on preState
+bytes ImportTest::executeTest()
+{
+ ExecutionResult res;
+ eth::State tmpState = m_statePre;
+ try
+ {
+ std::pair<ExecutionResult, TransactionReceipt> execOut = m_statePre.execute(m_envInfo, m_transaction);
+ res = execOut.first;
+ m_logs = execOut.second.log();
+ }
+ catch (Exception const& _e)
+ {
+ cnote << "Exception: " << diagnostic_information(_e);
+ }
+ catch (std::exception const& _e)
+ {
+ cnote << "state execution exception: " << _e.what();
+ }
+
+ m_statePre.commit();
+ m_statePost = m_statePre;
+ m_statePre = tmpState;
+
+ return res.output;
+}
+
+json_spirit::mObject& ImportTest::makeAllFieldsHex(json_spirit::mObject& _o)
+{
+ static const set<string> hashes {"bloom" , "coinbase", "hash", "mixHash", "parentHash", "receiptTrie",
+ "stateRoot", "transactionsTrie", "uncleHash", "currentCoinbase",
+ "previousHash", "to", "address", "caller", "origin", "secretKey", "data"};
+
+ for (auto& i: _o)
+ {
+ std::string key = i.first;
+ if (hashes.count(key))
+ continue;
+
+ std::string str;
+ json_spirit::mValue value = i.second;
+
+ if (value.type() == json_spirit::int_type)
+ str = toString(value.get_int());
+ else if (value.type() == json_spirit::str_type)
+ str = value.get_str();
+ else continue;
+
+ _o[key] = (str.substr(0, 2) == "0x") ? str : toCompactHex(toInt(str), HexPrefix::Add, 1);
+ }
+ return _o;
+}
+
+void ImportTest::importEnv(json_spirit::mObject& _o)
+{
+ assert(_o.count("currentGasLimit") > 0);
+ assert(_o.count("currentDifficulty") > 0);
+ assert(_o.count("currentNumber") > 0);
+ assert(_o.count("currentTimestamp") > 0);
+ assert(_o.count("currentCoinbase") > 0);
+ m_envInfo.setGasLimit(toInt(_o["currentGasLimit"]));
+ m_envInfo.setDifficulty(toInt(_o["currentDifficulty"]));
+ m_envInfo.setNumber(toInt(_o["currentNumber"]));
+ m_envInfo.setTimestamp(toInt(_o["currentTimestamp"]));
+ m_envInfo.setBeneficiary(Address(_o["currentCoinbase"].get_str()));
+ m_envInfo.setLastHashes( lastHashes( m_envInfo.number() ) );
+}
+
+// import state from not fully declared json_spirit::mObject, writing to _stateOptionsMap which fields were defined in json
+
+void ImportTest::importState(json_spirit::mObject& _o, State& _state, AccountMaskMap& o_mask)
+{
+ std::string jsondata = json_spirit::write_string((json_spirit::mValue)_o, false);
+ _state.populateFrom(jsonToAccountMap(jsondata, &o_mask));
+}
+
+void ImportTest::importState(json_spirit::mObject& _o, State& _state)
+{
+ AccountMaskMap mask;
+ importState(_o, _state, mask);
+ for (auto const& i: mask)
+ //check that every parameter was declared in state object
+ if (!i.second.allSet())
+ BOOST_THROW_EXCEPTION(MissingFields() << errinfo_comment("Import State: Missing state fields!"));
+}
+
+void ImportTest::importTransaction (json_spirit::mObject const& _o, eth::Transaction& o_tr)
+{
+ if (_o.count("secretKey") > 0)
+ {
+ assert(_o.count("nonce") > 0);
+ assert(_o.count("gasPrice") > 0);
+ assert(_o.count("gasLimit") > 0);
+ assert(_o.count("to") > 0);
+ assert(_o.count("value") > 0);
+ assert(_o.count("data") > 0);
+
+ if (bigint(_o.at("nonce").get_str()) >= c_max256plus1)
+ BOOST_THROW_EXCEPTION(ValueTooLarge() << errinfo_comment("Transaction 'nonce' is equal or greater than 2**256") );
+ if (bigint(_o.at("gasPrice").get_str()) >= c_max256plus1)
+ BOOST_THROW_EXCEPTION(ValueTooLarge() << errinfo_comment("Transaction 'gasPrice' is equal or greater than 2**256") );
+ if (bigint(_o.at("gasLimit").get_str()) >= c_max256plus1)
+ BOOST_THROW_EXCEPTION(ValueTooLarge() << errinfo_comment("Transaction 'gasLimit' is equal or greater than 2**256") );
+ if (bigint(_o.at("value").get_str()) >= c_max256plus1)
+ BOOST_THROW_EXCEPTION(ValueTooLarge() << errinfo_comment("Transaction 'value' is equal or greater than 2**256") );
+
+ o_tr = _o.at("to").get_str().empty() ?
+ Transaction(toInt(_o.at("value")), toInt(_o.at("gasPrice")), toInt(_o.at("gasLimit")), importData(_o), toInt(_o.at("nonce")), Secret(_o.at("secretKey").get_str())) :
+ Transaction(toInt(_o.at("value")), toInt(_o.at("gasPrice")), toInt(_o.at("gasLimit")), Address(_o.at("to").get_str()), importData(_o), toInt(_o.at("nonce")), Secret(_o.at("secretKey").get_str()));
+ }
+ else
+ {
+ RLPStream transactionRLPStream = createRLPStreamFromTransactionFields(_o);
+ RLP transactionRLP(transactionRLPStream.out());
+ try
+ {
+ o_tr = Transaction(transactionRLP.data(), CheckTransaction::Everything);
+ }
+ catch (InvalidSignature)
+ {
+ // create unsigned transaction
+ o_tr = _o.at("to").get_str().empty() ?
+ Transaction(toInt(_o.at("value")), toInt(_o.at("gasPrice")), toInt(_o.at("gasLimit")), importData(_o), toInt(_o.at("nonce"))) :
+ Transaction(toInt(_o.at("value")), toInt(_o.at("gasPrice")), toInt(_o.at("gasLimit")), Address(_o.at("to").get_str()), importData(_o), toInt(_o.at("nonce")));
+ }
+ catch (Exception& _e)
+ {
+ cnote << "invalid transaction" << boost::diagnostic_information(_e);
+ }
+ }
+}
+
+void ImportTest::importTransaction(json_spirit::mObject const& o_tr)
+{
+ importTransaction(o_tr, m_transaction);
+}
+
+int ImportTest::compareStates(State const& _stateExpect, State const& _statePost, AccountMaskMap const _expectedStateOptions, WhenError _throw)
+{
+ #define CHECK(a,b) \
+ { \
+ if (_throw == WhenError::Throw) \
+ { \
+ TBOOST_CHECK_MESSAGE(a, b); \
+ if (!a) \
+ return 1; \
+ } \
+ else \
+ {TBOOST_WARN_MESSAGE(a,b);} \
+ }
+
+ for (auto const& a: _stateExpect.addresses())
+ {
+ CHECK(_statePost.addressInUse(a.first), "Check State: " << a.first << " missing expected address!");
+ if (_statePost.addressInUse(a.first))
+ {
+ AccountMask addressOptions(true);
+ if(_expectedStateOptions.size())
+ {
+ try
+ {
+ addressOptions = _expectedStateOptions.at(a.first);
+ }
+ catch(std::out_of_range const&)
+ {
+ TBOOST_ERROR("expectedStateOptions map does not match expectedState in checkExpectedState!");
+ break;
+ }
+ }
+
+ if (addressOptions.hasBalance())
+ CHECK((_stateExpect.balance(a.first) == _statePost.balance(a.first)),
+ "Check State: " << a.first << ": incorrect balance " << _statePost.balance(a.first) << ", expected " << _stateExpect.balance(a.first));
+
+ if (addressOptions.hasNonce())
+ CHECK((_stateExpect.transactionsFrom(a.first) == _statePost.transactionsFrom(a.first)),
+ "Check State: " << a.first << ": incorrect nonce " << _statePost.transactionsFrom(a.first) << ", expected " << _stateExpect.transactionsFrom(a.first));
+
+ if (addressOptions.hasStorage())
+ {
+ unordered_map<u256, u256> stateStorage = _statePost.storage(a.first);
+ for (auto const& s: _stateExpect.storage(a.first))
+ CHECK((stateStorage[s.first] == s.second),
+ "Check State: " << a.first << ": incorrect storage [" << s.first << "] = " << toHex(stateStorage[s.first]) << ", expected [" << s.first << "] = " << toHex(s.second));
+
+ //Check for unexpected storage values
+ stateStorage = _stateExpect.storage(a.first);
+ for (auto const& s: _statePost.storage(a.first))
+ CHECK((stateStorage[s.first] == s.second),
+ "Check State: " << a.first << ": incorrect storage [" << s.first << "] = " << toHex(s.second) << ", expected [" << s.first << "] = " << toHex(stateStorage[s.first]));
+ }
+
+ if (addressOptions.hasCode())
+ CHECK((_stateExpect.code(a.first) == _statePost.code(a.first)),
+ "Check State: " << a.first << ": incorrect code '" << toHex(_statePost.code(a.first)) << "', expected '" << toHex(_stateExpect.code(a.first)) << "'");
+ }
+ }
+ return 0;
+}
+
+int ImportTest::exportTest(bytes const& _output)
+{
+ int err = 0;
+ // export output
+ m_testObject["out"] = (_output.size() > 4096 && !Options::get().fulloutput) ? "#" + toString(_output.size()) : toHex(_output, 2, HexPrefix::Add);
+
+ // compare expected output with post output
+ if (m_testObject.count("expectOut") > 0)
+ {
+ std::string warning = "Check State: Error! Unexpected output: " + m_testObject["out"].get_str() + " Expected: " + m_testObject["expectOut"].get_str();
+ if (Options::get().checkState)
+ {
+ bool statement = (m_testObject["out"].get_str() == m_testObject["expectOut"].get_str());
+ TBOOST_CHECK_MESSAGE(statement, warning);
+ if (!statement)
+ err = 1;
+ }
+ else
+ TBOOST_WARN_MESSAGE((m_testObject["out"].get_str() == m_testObject["expectOut"].get_str()), warning);
+
+ m_testObject.erase(m_testObject.find("expectOut"));
+ }
+
+ // export logs
+ m_testObject["logs"] = exportLog(m_logs);
+
+ // compare expected state with post state
+ if (m_testObject.count("expect") > 0)
+ {
+ eth::AccountMaskMap stateMap;
+ State expectState(OverlayDB(), eth::BaseState::Empty);
+ importState(m_testObject["expect"].get_obj(), expectState, stateMap);
+ compareStates(expectState, m_statePost, stateMap, Options::get().checkState ? WhenError::Throw : WhenError::DontThrow);
+ m_testObject.erase(m_testObject.find("expect"));
+ }
+
+ // export post state
+ m_testObject["post"] = fillJsonWithState(m_statePost);
+ m_testObject["postStateRoot"] = toHex(m_statePost.rootHash().asBytes());
+
+ // export pre state
+ m_testObject["pre"] = fillJsonWithState(m_statePre);
+ m_testObject["env"] = makeAllFieldsHex(m_testObject["env"].get_obj());
+ m_testObject["transaction"] = makeAllFieldsHex(m_testObject["transaction"].get_obj());
+ return err;
+}
+
+json_spirit::mObject fillJsonWithTransaction(Transaction _txn)
+{
+ json_spirit::mObject txObject;
+ txObject["nonce"] = toCompactHex(_txn.nonce(), HexPrefix::Add, 1);
+ txObject["data"] = toHex(_txn.data(), 2, HexPrefix::Add);
+ txObject["gasLimit"] = toCompactHex(_txn.gas(), HexPrefix::Add, 1);
+ txObject["gasPrice"] = toCompactHex(_txn.gasPrice(), HexPrefix::Add, 1);
+ txObject["r"] = toCompactHex(_txn.signature().r, HexPrefix::Add, 1);
+ txObject["s"] = toCompactHex(_txn.signature().s, HexPrefix::Add, 1);
+ txObject["v"] = toCompactHex(_txn.signature().v + 27, HexPrefix::Add, 1);
+ txObject["to"] = _txn.isCreation() ? "" : toString(_txn.receiveAddress());
+ txObject["value"] = toCompactHex(_txn.value(), HexPrefix::Add, 1);
+ return txObject;
+}
+
+json_spirit::mObject fillJsonWithState(State _state)
+{
+ json_spirit::mObject oState;
+ for (auto const& a: _state.addresses())
+ {
+ json_spirit::mObject o;
+ o["balance"] = toCompactHex(_state.balance(a.first), HexPrefix::Add, 1);
+ o["nonce"] = toCompactHex(_state.transactionsFrom(a.first), HexPrefix::Add, 1);
+ {
+ json_spirit::mObject store;
+ for (auto const& s: _state.storage(a.first))
+ store[toCompactHex(s.first, HexPrefix::Add, 1)] = toCompactHex(s.second, HexPrefix::Add, 1);
+ o["storage"] = store;
+ }
+ o["code"] = toHex(_state.code(a.first), 2, HexPrefix::Add);
+ oState[toString(a.first)] = o;
+ }
+ return oState;
+}
+
+json_spirit::mArray exportLog(eth::LogEntries _logs)
+{
+ json_spirit::mArray ret;
+ if (_logs.size() == 0) return ret;
+ for (LogEntry const& l: _logs)
+ {
+ json_spirit::mObject o;
+ o["address"] = toString(l.address);
+ json_spirit::mArray topics;
+ for (auto const& t: l.topics)
+ topics.push_back(toString(t));
+ o["topics"] = topics;
+ o["data"] = toHex(l.data, 2, HexPrefix::Add);
+ o["bloom"] = toString(l.bloom());
+ ret.push_back(o);
+ }
+ return ret;
+}
+
+u256 toInt(json_spirit::mValue const& _v)
+{
+ switch (_v.type())
+ {
+ case json_spirit::str_type: return u256(_v.get_str());
+ case json_spirit::int_type: return (u256)_v.get_uint64();
+ case json_spirit::bool_type: return (u256)(uint64_t)_v.get_bool();
+ case json_spirit::real_type: return (u256)(uint64_t)_v.get_real();
+ default: cwarn << "Bad type for scalar: " << _v.type();
+ }
+ return 0;
+}
+
+byte toByte(json_spirit::mValue const& _v)
+{
+ switch (_v.type())
+ {
+ case json_spirit::str_type: return (byte)stoi(_v.get_str());
+ case json_spirit::int_type: return (byte)_v.get_uint64();
+ case json_spirit::bool_type: return (byte)_v.get_bool();
+ case json_spirit::real_type: return (byte)_v.get_real();
+ default: cwarn << "Bad type for scalar: " << _v.type();
+ }
+ return 0;
+}
+
+bytes importByteArray(std::string const& _str)
+{
+ return fromHex(_str.substr(0, 2) == "0x" ? _str.substr(2) : _str, WhenError::Throw);
+}
+
+bytes importData(json_spirit::mObject const& _o)
+{
+ bytes data;
+ if (_o.at("data").type() == json_spirit::str_type)
+ data = importByteArray(_o.at("data").get_str());
+ else
+ for (auto const& j: _o.at("data").get_array())
+ data.push_back(toByte(j));
+ return data;
+}
+
+bytes importCode(json_spirit::mObject& _o)
+{
+ bytes code;
+ if (_o["code"].type() == json_spirit::str_type)
+ if (_o["code"].get_str().find("0x") != 0)
+ code = compileLLL(_o["code"].get_str(), false);
+ else
+ code = fromHex(_o["code"].get_str().substr(2));
+ else if (_o["code"].type() == json_spirit::array_type)
+ {
+ code.clear();
+ for (auto const& j: _o["code"].get_array())
+ code.push_back(toByte(j));
+ }
+ return code;
+}
+
+LogEntries importLog(json_spirit::mArray& _a)
+{
+ LogEntries logEntries;
+ for (auto const& l: _a)
+ {
+ json_spirit::mObject o = l.get_obj();
+ // cant use BOOST_REQUIRE, because this function is used outside boost test (createRandomTest)
+ assert(o.count("address") > 0);
+ assert(o.count("topics") > 0);
+ assert(o.count("data") > 0);
+ assert(o.count("bloom") > 0);
+ LogEntry log;
+ log.address = Address(o["address"].get_str());
+ for (auto const& t: o["topics"].get_array())
+ log.topics.push_back(h256(t.get_str()));
+ log.data = importData(o);
+ logEntries.push_back(log);
+ }
+ return logEntries;
+}
+
+void checkOutput(bytes const& _output, json_spirit::mObject& _o)
+{
+ int j = 0;
+
+ if (_o["out"].get_str().find("#") == 0)
+ {TBOOST_CHECK(((u256)_output.size() == toInt(_o["out"].get_str().substr(1))));}
+ else if (_o["out"].type() == json_spirit::array_type)
+ for (auto const& d: _o["out"].get_array())
+ {
+ TBOOST_CHECK_MESSAGE((_output[j] == toInt(d)), "Output byte [" << j << "] different!");
+ ++j;
+ }
+ else if (_o["out"].get_str().find("0x") == 0)
+ {TBOOST_CHECK((_output == fromHex(_o["out"].get_str().substr(2))));}
+ else
+ TBOOST_CHECK((_output == fromHex(_o["out"].get_str())));
+}
+
+void checkStorage(map<u256, u256> _expectedStore, map<u256, u256> _resultStore, Address _expectedAddr)
+{
+ _expectedAddr = _expectedAddr; //unsed parametr when macro
+ for (auto&& expectedStorePair : _expectedStore)
+ {
+ auto& expectedStoreKey = expectedStorePair.first;
+ auto resultStoreIt = _resultStore.find(expectedStoreKey);
+ if (resultStoreIt == _resultStore.end())
+ {TBOOST_ERROR(_expectedAddr << ": missing store key " << expectedStoreKey);}
+ else
+ {
+ auto& expectedStoreValue = expectedStorePair.second;
+ auto& resultStoreValue = resultStoreIt->second;
+ TBOOST_CHECK_MESSAGE((expectedStoreValue == resultStoreValue), _expectedAddr << ": store[" << expectedStoreKey << "] = " << resultStoreValue << ", expected " << expectedStoreValue);
+ }
+ }
+ TBOOST_CHECK_EQUAL(_resultStore.size(), _expectedStore.size());
+ for (auto&& resultStorePair: _resultStore)
+ {
+ if (!_expectedStore.count(resultStorePair.first))
+ TBOOST_ERROR(_expectedAddr << ": unexpected store key " << resultStorePair.first);
+ }
+}
+
+void checkLog(LogEntries _resultLogs, LogEntries _expectedLogs)
+{
+ TBOOST_REQUIRE_EQUAL(_resultLogs.size(), _expectedLogs.size());
+
+ for (size_t i = 0; i < _resultLogs.size(); ++i)
+ {
+ TBOOST_CHECK_EQUAL(_resultLogs[i].address, _expectedLogs[i].address);
+ TBOOST_CHECK_EQUAL(_resultLogs[i].topics, _expectedLogs[i].topics);
+ TBOOST_CHECK((_resultLogs[i].data == _expectedLogs[i].data));
+ }
+}
+
+void checkCallCreates(eth::Transactions _resultCallCreates, eth::Transactions _expectedCallCreates)
+{
+ TBOOST_REQUIRE_EQUAL(_resultCallCreates.size(), _expectedCallCreates.size());
+
+ for (size_t i = 0; i < _resultCallCreates.size(); ++i)
+ {
+ TBOOST_CHECK((_resultCallCreates[i].data() == _expectedCallCreates[i].data()));
+ TBOOST_CHECK((_resultCallCreates[i].receiveAddress() == _expectedCallCreates[i].receiveAddress()));
+ TBOOST_CHECK((_resultCallCreates[i].gas() == _expectedCallCreates[i].gas()));
+ TBOOST_CHECK((_resultCallCreates[i].value() == _expectedCallCreates[i].value()));
+ }
+}
+
+void userDefinedTest(std::function<void(json_spirit::mValue&, bool)> doTests)
+{
+ if (!Options::get().singleTest)
+ return;
+
+ if (Options::get().singleTestFile.empty() || Options::get().singleTestName.empty())
+ {
+ cnote << "Missing user test specification\nUsage: testeth --singletest <filename> <testname>\n";
+ return;
+ }
+
+ auto& filename = Options::get().singleTestFile;
+ auto& testname = Options::get().singleTestName;
+
+ if (g_logVerbosity != -1)
+ VerbosityHolder sentinel(12);
+
+ try
+ {
+ cnote << "Testing user defined test: " << filename;
+ json_spirit::mValue v;
+ string s = contentsString(filename);
+ TBOOST_REQUIRE_MESSAGE((s.length() > 0), "Contents of " + filename + " is empty. ");
+ json_spirit::read_string(s, v);
+ json_spirit::mObject oSingleTest;
+
+ json_spirit::mObject::const_iterator pos = v.get_obj().find(testname);
+ if (pos == v.get_obj().end())
+ {
+ cnote << "Could not find test: " << testname << " in " << filename << "\n";
+ return;
+ }
+ else
+ oSingleTest[pos->first] = pos->second;
+
+ json_spirit::mValue v_singleTest(oSingleTest);
+ doTests(v_singleTest, test::Options::get().fillTests);
+ }
+ catch (Exception const& _e)
+ {
+ TBOOST_ERROR("Failed Test with Exception: " << diagnostic_information(_e));
+ }
+ catch (std::exception const& _e)
+ {
+ TBOOST_ERROR("Failed Test with Exception: " << _e.what());
+ }
+}
+
+void executeTests(const string& _name, const string& _testPathAppendix, const boost::filesystem::path _pathToFiller, std::function<void(json_spirit::mValue&, bool)> doTests)
+{
+ string testPath = getTestPath();
+ testPath += _testPathAppendix;
+
+ if (Options::get().stats)
+ Listener::registerListener(Stats::get());
+
+ if (Options::get().fillTests)
+ {
+ try
+ {
+ cnote << "Populating tests...";
+ json_spirit::mValue v;
+ boost::filesystem::path p(__FILE__);
+ string s = asString(dev::contents(_pathToFiller.string() + "/" + _name + "Filler.json"));
+ TBOOST_REQUIRE_MESSAGE((s.length() > 0), "Contents of " + _pathToFiller.string() + "/" + _name + "Filler.json is empty.");
+ json_spirit::read_string(s, v);
+ doTests(v, true);
+ writeFile(testPath + "/" + _name + ".json", asBytes(json_spirit::write_string(v, true)));
+ }
+ catch (Exception const& _e)
+ {
+ TBOOST_ERROR("Failed filling test with Exception: " << diagnostic_information(_e));
+ }
+ catch (std::exception const& _e)
+ {
+ TBOOST_ERROR("Failed filling test with Exception: " << _e.what());
+ }
+ }
+
+ try
+ {
+ cnote << "TEST " << _name << ":";
+ json_spirit::mValue v;
+ string s = asString(dev::contents(testPath + "/" + _name + ".json"));
+ TBOOST_REQUIRE_MESSAGE((s.length() > 0), "Contents of " + testPath + "/" + _name + ".json is empty. Have you cloned the 'tests' repo branch develop and set ETHEREUM_TEST_PATH to its path?");
+ json_spirit::read_string(s, v);
+ Listener::notifySuiteStarted(_name);
+ doTests(v, false);
+ }
+ catch (Exception const& _e)
+ {
+ TBOOST_ERROR("Failed test with Exception: " << diagnostic_information(_e));
+ }
+ catch (std::exception const& _e)
+ {
+ TBOOST_ERROR("Failed test with Exception: " << _e.what());
+ }
+}
+
+RLPStream createRLPStreamFromTransactionFields(json_spirit::mObject const& _tObj)
+{
+ //Construct Rlp of the given transaction
+ RLPStream rlpStream;
+ rlpStream.appendList(_tObj.size());
+
+ if (_tObj.count("nonce"))
+ rlpStream << bigint(_tObj.at("nonce").get_str());
+
+ if (_tObj.count("gasPrice"))
+ rlpStream << bigint(_tObj.at("gasPrice").get_str());
+
+ if (_tObj.count("gasLimit"))
+ rlpStream << bigint(_tObj.at("gasLimit").get_str());
+
+ if (_tObj.count("to"))
+ {
+ if (_tObj.at("to").get_str().empty())
+ rlpStream << "";
+ else
+ rlpStream << importByteArray(_tObj.at("to").get_str());
+ }
+
+ if (_tObj.count("value"))
+ rlpStream << bigint(_tObj.at("value").get_str());
+
+ if (_tObj.count("data"))
+ rlpStream << importData(_tObj);
+
+ if (_tObj.count("v"))
+ rlpStream << bigint(_tObj.at("v").get_str());
+
+ if (_tObj.count("r"))
+ rlpStream << bigint(_tObj.at("r").get_str());
+
+ if (_tObj.count("s"))
+ rlpStream << bigint(_tObj.at("s").get_str());
+
+ if (_tObj.count("extrafield"))
+ rlpStream << bigint(_tObj.at("extrafield").get_str());
+
+ return rlpStream;
+}
+
+Options::Options()
+{
+ auto argc = boost::unit_test::framework::master_test_suite().argc;
+ auto argv = boost::unit_test::framework::master_test_suite().argv;
+
+ for (auto i = 0; i < argc; ++i)
+ {
+ auto arg = std::string{argv[i]};
+ if (arg == "--vm" && i + 1 < argc)
+ {
+ string vmKind = argv[++i];
+ if (vmKind == "interpreter")
+ VMFactory::setKind(VMKind::Interpreter);
+ else if (vmKind == "jit")
+ VMFactory::setKind(VMKind::JIT);
+ else if (vmKind == "smart")
+ VMFactory::setKind(VMKind::Smart);
+ else
+ cerr << "Unknown VM kind: " << vmKind << endl;
+ }
+ else if (arg == "--jit") // TODO: Remove deprecated option "--jit"
+ VMFactory::setKind(VMKind::JIT);
+ else if (arg == "--vmtrace")
+ vmtrace = true;
+ else if (arg == "--filltests")
+ fillTests = true;
+ else if (arg == "--stats" && i + 1 < argc)
+ {
+ stats = true;
+ statsOutFile = argv[i + 1];
+ }
+ else if (arg == "--performance")
+ performance = true;
+ else if (arg == "--quadratic")
+ quadratic = true;
+ else if (arg == "--memory")
+ memory = true;
+ else if (arg == "--inputlimits")
+ inputLimits = true;
+ else if (arg == "--bigdata")
+ bigData = true;
+ else if (arg == "--checkstate")
+ checkState = true;
+ else if (arg == "--wallet")
+ wallet = true;
+ else if (arg == "--nonetwork")
+ nonetwork = true;
+ else if (arg == "--network")
+ nonetwork = false;
+ else if (arg == "--nodag")
+ nodag = true;
+ else if (arg == "--all")
+ {
+ performance = true;
+ quadratic = true;
+ memory = true;
+ inputLimits = true;
+ bigData = true;
+ wallet = true;
+ }
+ else if (arg == "--singletest" && i + 1 < argc)
+ {
+ singleTest = true;
+ auto name1 = std::string{argv[i + 1]};
+ if (i + 1 < argc) // two params
+ {
+ auto name2 = std::string{argv[i + 2]};
+ if (name2[0] == '-') // not param, another option
+ singleTestName = std::move(name1);
+ else
+ {
+ singleTestFile = std::move(name1);
+ singleTestName = std::move(name2);
+ }
+ }
+ else
+ singleTestName = std::move(name1);
+ }
+ else if (arg == "--fulloutput")
+ fulloutput = true;
+ else if (arg == "--verbosity" && i + 1 < argc)
+ {
+ static std::ostringstream strCout; //static string to redirect logs to
+ std::string indentLevel = std::string{argv[i + 1]};
+ if (indentLevel == "0")
+ {
+ logVerbosity = Verbosity::None;
+ std::cout.rdbuf(strCout.rdbuf());
+ std::cerr.rdbuf(strCout.rdbuf());
+ }
+ else if (indentLevel == "1")
+ logVerbosity = Verbosity::NiceReport;
+ else
+ logVerbosity = Verbosity::Full;
+ }
+ }
+
+ //Default option
+ if (logVerbosity == Verbosity::NiceReport)
+ g_logVerbosity = -1; //disable cnote but leave cerr and cout
+}
+
+Options const& Options::get()
+{
+ static Options instance;
+ return instance;
+}
+
+LastHashes lastHashes(u256 _currentBlockNumber)
+{
+ LastHashes ret;
+ for (u256 i = 1; i <= 256 && i <= _currentBlockNumber; ++i)
+ ret.push_back(sha3(toString(_currentBlockNumber - i)));
+ return ret;
+}
+
+dev::eth::Ethash::BlockHeader constructHeader(
+ h256 const& _parentHash,
+ h256 const& _sha3Uncles,
+ Address const& _coinbaseAddress,
+ h256 const& _stateRoot,
+ h256 const& _transactionsRoot,
+ h256 const& _receiptsRoot,
+ dev::eth::LogBloom const& _logBloom,
+ u256 const& _difficulty,
+ u256 const& _number,
+ u256 const& _gasLimit,
+ u256 const& _gasUsed,
+ u256 const& _timestamp,
+ bytes const& _extraData)
+{
+ RLPStream rlpStream;
+ rlpStream.appendList(Ethash::BlockHeader::Fields);
+
+ rlpStream << _parentHash << _sha3Uncles << _coinbaseAddress << _stateRoot << _transactionsRoot << _receiptsRoot << _logBloom
+ << _difficulty << _number << _gasLimit << _gasUsed << _timestamp << _extraData << h256{} << Nonce{};
+
+ return Ethash::BlockHeader(rlpStream.out(), CheckNothing, h256{}, HeaderData);
+}
+
+void updateEthashSeal(dev::eth::Ethash::BlockHeader& _header, h256 const& _mixHash, dev::eth::Nonce const& _nonce)
+{
+ RLPStream source;
+ _header.streamRLP(source);
+ RLP sourceRlp(source.out());
+ RLPStream header;
+ header.appendList(Ethash::BlockHeader::Fields);
+ for (size_t i = 0; i < BlockInfo::BasicFields; i++)
+ header << sourceRlp[i];
+
+ header << _mixHash << _nonce;
+ _header = Ethash::BlockHeader(header.out(), CheckNothing, h256{}, HeaderData);
+}
+
+namespace
+{
+ Listener* g_listener;
+}
+
+void Listener::registerListener(Listener& _listener)
+{
+ g_listener = &_listener;
+}
+
+void Listener::notifySuiteStarted(std::string const& _name)
+{
+ if (g_listener)
+ g_listener->suiteStarted(_name);
+}
+
+void Listener::notifyTestStarted(std::string const& _name)
+{
+ if (g_listener)
+ g_listener->testStarted(_name);
+}
+
+void Listener::notifyTestFinished()
+{
+ if (g_listener)
+ g_listener->testFinished();
+}
+
+} } // namespaces
diff --git a/test/TestHelper.h b/test/TestHelper.h
new file mode 100644
index 00000000..e5cf1323
--- /dev/null
+++ b/test/TestHelper.h
@@ -0,0 +1,290 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum 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.
+
+ cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+*/
+/** @file TestHelper.h
+ * @author Marko Simovic <markobarko@gmail.com>
+ * @date 2014
+ */
+
+#pragma once
+
+#include <functional>
+#include <boost/test/unit_test.hpp>
+#include <boost/filesystem.hpp>
+
+#include "JsonSpiritHeaders.h"
+#include <libethcore/Ethash.h>
+#include <libethereum/State.h>
+#include <libevm/ExtVMFace.h>
+#include <libtestutils/Common.h>
+
+#ifdef NOBOOST
+ #define TBOOST_REQUIRE(arg) if(arg == false) throw dev::Exception();
+ #define TBOOST_REQUIRE_EQUAL(arg1, arg2) if(arg1 != arg2) throw dev::Exception();
+ #define TBOOST_CHECK_EQUAL(arg1, arg2) if(arg1 != arg2) throw dev::Exception();
+ #define TBOOST_CHECK(arg) if(arg == false) throw dev::Exception();
+ #define TBOOST_REQUIRE_MESSAGE(arg1, arg2) if(arg1 == false) throw dev::Exception();
+ #define TBOOST_CHECK_MESSAGE(arg1, arg2) if(arg1 == false) throw dev::Exception();
+ #define TBOOST_WARN_MESSAGE(arg1, arg2) throw dev::Exception();
+ #define TBOOST_ERROR(arg) throw dev::Exception();
+#else
+ #define TBOOST_REQUIRE(arg) BOOST_REQUIRE(arg)
+ #define TBOOST_REQUIRE_EQUAL(arg1, arg2) BOOST_REQUIRE_EQUAL(arg1, arg2)
+ #define TBOOST_CHECK(arg) BOOST_CHECK(arg)
+ #define TBOOST_CHECK_EQUAL(arg1, arg2) BOOST_CHECK_EQUAL(arg1, arg2)
+ #define TBOOST_CHECK_MESSAGE(arg1, arg2) BOOST_CHECK_MESSAGE(arg1, arg2)
+ #define TBOOST_REQUIRE_MESSAGE(arg1, arg2) BOOST_REQUIRE_MESSAGE(arg1, arg2)
+ #define TBOOST_WARN_MESSAGE(arg1, arg2) BOOST_WARN_MESSAGE(arg1, arg2)
+ #define TBOOST_ERROR(arg) BOOST_ERROR(arg)
+#endif
+
+namespace dev
+{
+namespace eth
+{
+
+class Client;
+class State;
+
+void mine(Client& c, int numBlocks);
+void connectClients(Client& c1, Client& c2);
+void mine(Block& _s, BlockChain const& _bc);
+void mine(Ethash::BlockHeader& _bi);
+
+}
+
+namespace test
+{
+
+/// Make sure that no Exception is thrown during testing. If one is thrown show its info and fail the test.
+/// Our version of BOOST_REQUIRE_NO_THROW()
+/// @param _statenent The statement for which to make sure no exceptions are thrown
+/// @param _message A message to act as a prefix to the expression's error information
+#define ETH_TEST_REQUIRE_NO_THROW(_statement, _message) \
+ do \
+ { \
+ try \
+ { \
+ BOOST_TEST_PASSPOINT(); \
+ _statement; \
+ } \
+ catch (boost::exception const& _e) \
+ { \
+ auto msg = std::string(_message " due to an exception thrown by " \
+ BOOST_STRINGIZE(_statement) "\n") + boost::diagnostic_information(_e); \
+ BOOST_CHECK_IMPL(false, msg, REQUIRE, CHECK_MSG); \
+ } \
+ catch (...) \
+ { \
+ BOOST_CHECK_IMPL(false, "Unknown exception thrown by " \
+ BOOST_STRINGIZE(_statement), REQUIRE, CHECK_MSG); \
+ } \
+ } \
+ while (0)
+
+/// Check if an Exception is thrown during testing. If one is thrown show its info and continue the test
+/// Our version of BOOST_CHECK_NO_THROW()
+/// @param _statement The statement for which to make sure no exceptions are thrown
+/// @param _message A message to act as a prefix to the expression's error information
+#define ETH_TEST_CHECK_NO_THROW(_statement, _message) \
+ do \
+ { \
+ try \
+ { \
+ BOOST_TEST_PASSPOINT(); \
+ _statement; \
+ } \
+ catch (boost::exception const& _e) \
+ { \
+ auto msg = std::string(_message " due to an exception thrown by " \
+ BOOST_STRINGIZE(_statement) "\n") + boost::diagnostic_information(_e); \
+ BOOST_CHECK_IMPL(false, msg, CHECK, CHECK_MSG); \
+ } \
+ catch (...) \
+ { \
+ BOOST_CHECK_IMPL(false, "Unknown exception thrown by " \
+ BOOST_STRINGIZE(_statement), CHECK, CHECK_MSG ); \
+ } \
+ } \
+ while (0)
+
+enum class testType
+{
+ StateTests,
+ BlockChainTests,
+ Other
+};
+
+class ImportTest
+{
+public:
+ ImportTest(json_spirit::mObject& _o, bool isFiller, testType testTemplate = testType::StateTests);
+
+ // imports
+ void importEnv(json_spirit::mObject& _o);
+ static void importState(json_spirit::mObject& _o, eth::State& _state);
+ static void importState(json_spirit::mObject& _o, eth::State& _state, eth::AccountMaskMap& o_mask);
+ static void importTransaction (json_spirit::mObject const& _o, eth::Transaction& o_tr);
+ void importTransaction(json_spirit::mObject const& _o);
+ static json_spirit::mObject& makeAllFieldsHex(json_spirit::mObject& _o);
+
+ bytes executeTest();
+ int exportTest(bytes const& _output);
+ static int compareStates(eth::State const& _stateExpect, eth::State const& _statePost, eth::AccountMaskMap const _expectedStateOptions = eth::AccountMaskMap(), WhenError _throw = WhenError::Throw);
+
+ eth::State m_statePre;
+ eth::State m_statePost;
+ eth::EnvInfo m_envInfo;
+ eth::Transaction m_transaction;
+ eth::LogEntries m_logs;
+ eth::LogEntries m_logsExpected;
+
+private:
+ json_spirit::mObject& m_testObject;
+};
+
+class ZeroGasPricer: public eth::GasPricer
+{
+protected:
+ u256 ask(eth::Block const&) const override { return 0; }
+ u256 bid(eth::TransactionPriority = eth::TransactionPriority::Medium) const override { return 0; }
+};
+
+// helping functions
+u256 toInt(json_spirit::mValue const& _v);
+byte toByte(json_spirit::mValue const& _v);
+bytes importCode(json_spirit::mObject& _o);
+bytes importData(json_spirit::mObject const& _o);
+bytes importByteArray(std::string const& _str);
+eth::LogEntries importLog(json_spirit::mArray& _o);
+json_spirit::mArray exportLog(eth::LogEntries _logs);
+void checkOutput(bytes const& _output, json_spirit::mObject& _o);
+void checkStorage(std::map<u256, u256> _expectedStore, std::map<u256, u256> _resultStore, Address _expectedAddr);
+void checkLog(eth::LogEntries _resultLogs, eth::LogEntries _expectedLogs);
+void checkCallCreates(eth::Transactions _resultCallCreates, eth::Transactions _expectedCallCreates);
+dev::eth::Ethash::BlockHeader constructHeader(
+ h256 const& _parentHash,
+ h256 const& _sha3Uncles,
+ Address const& _coinbaseAddress,
+ h256 const& _stateRoot,
+ h256 const& _transactionsRoot,
+ h256 const& _receiptsRoot,
+ dev::eth::LogBloom const& _logBloom,
+ u256 const& _difficulty,
+ u256 const& _number,
+ u256 const& _gasLimit,
+ u256 const& _gasUsed,
+ u256 const& _timestamp,
+ bytes const& _extraData);
+void updateEthashSeal(dev::eth::Ethash::BlockHeader& _header, h256 const& _mixHash, dev::eth::Nonce const& _nonce);
+void executeTests(const std::string& _name, const std::string& _testPathAppendix, const boost::filesystem::path _pathToFiller, std::function<void(json_spirit::mValue&, bool)> doTests);
+void userDefinedTest(std::function<void(json_spirit::mValue&, bool)> doTests);
+RLPStream createRLPStreamFromTransactionFields(json_spirit::mObject const& _tObj);
+eth::LastHashes lastHashes(u256 _currentBlockNumber);
+json_spirit::mObject fillJsonWithState(eth::State _state);
+json_spirit::mObject fillJsonWithTransaction(eth::Transaction _txn);
+
+//Fill Test Functions
+void doTransactionTests(json_spirit::mValue& _v, bool _fillin);
+void doStateTests(json_spirit::mValue& v, bool _fillin);
+void doVMTests(json_spirit::mValue& v, bool _fillin);
+void doBlockchainTests(json_spirit::mValue& _v, bool _fillin);
+void doRlpTests(json_spirit::mValue& v, bool _fillin);
+
+/*template<typename mapType>
+void checkAddresses(mapType& _expectedAddrs, mapType& _resultAddrs)
+{
+ for (auto& resultPair : _resultAddrs)
+ {
+ auto& resultAddr = resultPair.first;
+ auto expectedAddrIt = _expectedAddrs.find(resultAddr);
+ if (expectedAddrIt == _expectedAddrs.end())
+ TBOOST_ERROR("Missing result address " << resultAddr);
+ }
+ TBOOST_CHECK((_expectedAddrs == _resultAddrs));
+}*/
+
+enum class Verbosity
+{
+ Full,
+ NiceReport,
+ None
+};
+
+class Options
+{
+public:
+ bool vmtrace = false; ///< Create EVM execution tracer // TODO: Link with log verbosity?
+ bool fillTests = false; ///< Create JSON test files from execution results
+ bool stats = false; ///< Execution time stats
+ std::string statsOutFile; ///< Stats output file. "out" for standard output
+ bool checkState = false;///< Throw error when checking test states
+ bool fulloutput = false;///< Replace large output to just it's length
+ Verbosity logVerbosity = Verbosity::NiceReport;
+
+ /// Test selection
+ /// @{
+ bool singleTest = false;
+ std::string singleTestFile;
+ std::string singleTestName;
+ bool performance = false;
+ bool quadratic = false;
+ bool memory = false;
+ bool inputLimits = false;
+ bool bigData = false;
+ bool wallet = false;
+ bool nonetwork = true;
+ bool nodag = true;
+ /// @}
+
+ /// Get reference to options
+ /// The first time used, options are parsed
+ static Options const& get();
+
+private:
+ Options();
+ Options(Options const&) = delete;
+};
+
+/// Allows observing test execution process.
+/// This class also provides methods for registering and notifying the listener
+class Listener
+{
+public:
+ virtual ~Listener() = default;
+
+ virtual void suiteStarted(std::string const&) {}
+ virtual void testStarted(std::string const& _name) = 0;
+ virtual void testFinished() = 0;
+
+ static void registerListener(Listener& _listener);
+ static void notifySuiteStarted(std::string const& _name);
+ static void notifyTestStarted(std::string const& _name);
+ static void notifyTestFinished();
+
+ /// Test started/finished notification RAII helper
+ class ExecTimeGuard
+ {
+ public:
+ ExecTimeGuard(std::string const& _testName) { notifyTestStarted(_testName); }
+ ~ExecTimeGuard() { notifyTestFinished(); }
+ ExecTimeGuard(ExecTimeGuard const&) = delete;
+ ExecTimeGuard& operator=(ExecTimeGuard) = delete;
+ };
+};
+
+}
+}
diff --git a/test/TestUtils.cpp b/test/TestUtils.cpp
new file mode 100644
index 00000000..5e0619c0
--- /dev/null
+++ b/test/TestUtils.cpp
@@ -0,0 +1,122 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum 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.
+
+ cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+ */
+/** @file TestUtils.cpp
+ * @author Marek Kotewicz <marek@ethdev.com>
+ * @date 2015
+ */
+
+#include <thread>
+#include <boost/test/unit_test.hpp>
+#include <boost/filesystem.hpp>
+#include <libdevcrypto/Common.h>
+#include <libtestutils/Common.h>
+#include <libtestutils/BlockChainLoader.h>
+#include <libtestutils/FixedClient.h>
+#include "TestUtils.h"
+
+using namespace std;
+using namespace dev;
+using namespace dev::eth;
+using namespace dev::test;
+
+namespace dev
+{
+namespace test
+{
+
+bool getCommandLineOption(std::string const& _name);
+std::string getCommandLineArgument(std::string const& _name, bool _require = false);
+
+}
+}
+
+bool dev::test::getCommandLineOption(string const& _name)
+{
+ auto argc = boost::unit_test::framework::master_test_suite().argc;
+ auto argv = boost::unit_test::framework::master_test_suite().argv;
+ bool result = false;
+ for (auto i = 0; !result && i < argc; ++i)
+ result = _name == argv[i];
+ return result;
+}
+
+std::string dev::test::getCommandLineArgument(string const& _name, bool _require)
+{
+ auto argc = boost::unit_test::framework::master_test_suite().argc;
+ auto argv = boost::unit_test::framework::master_test_suite().argv;
+ for (auto i = 1; i < argc; ++i)
+ {
+ string str = argv[i];
+ if (_name == str.substr(0, _name.size()))
+ return str.substr(str.find("=") + 1);
+ }
+ if (_require)
+ BOOST_ERROR("Failed getting command line argument: " << _name << " from: " << argv);
+ return "";
+}
+
+LoadTestFileFixture::LoadTestFileFixture()
+{
+ m_json = loadJsonFromFile(toTestFilePath(getCommandLineArgument("--eth_testfile")));
+}
+
+void ParallelFixture::enumerateThreads(std::function<void()> callback) const
+{
+ size_t threadsCount = std::stoul(getCommandLineArgument("--eth_threads"), nullptr, 10);
+
+ vector<thread> workers;
+ for (size_t i = 0; i < threadsCount; i++)
+ workers.emplace_back(callback);
+
+ for_each(workers.begin(), workers.end(), [](thread &t)
+ {
+ t.join();
+ });
+}
+
+void BlockChainFixture::enumerateBlockchains(std::function<void(Json::Value const&, dev::eth::BlockChain const&, State state)> callback) const
+{
+ for (string const& name: m_json.getMemberNames())
+ {
+ BlockChainLoader bcl(m_json[name]);
+ callback(m_json[name], bcl.bc(), bcl.state());
+ }
+}
+
+void ClientBaseFixture::enumerateClients(std::function<void(Json::Value const&, dev::eth::ClientBase&)> callback) const
+{
+ enumerateBlockchains([&callback](Json::Value const& _json, BlockChain const& _bc, State _state) -> void
+ {
+ cerr << "void ClientBaseFixture::enumerateClients. FixedClient now accepts block not sate!" << endl;
+ _state.commit(); //unused variable. remove this line
+ FixedClient client(_bc, eth::Block {});
+ callback(_json, client);
+ });
+}
+
+void ParallelClientBaseFixture::enumerateClients(std::function<void(Json::Value const&, dev::eth::ClientBase&)> callback) const
+{
+ ClientBaseFixture::enumerateClients([this, &callback](Json::Value const& _json, dev::eth::ClientBase& _client) -> void
+ {
+ // json is being copied here
+ enumerateThreads([callback, _json, &_client]() -> void
+ {
+ callback(_json, _client);
+ });
+ });
+}
+
diff --git a/test/TestUtils.h b/test/TestUtils.h
new file mode 100644
index 00000000..a7a0eacf
--- /dev/null
+++ b/test/TestUtils.h
@@ -0,0 +1,83 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum 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.
+
+ cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+ */
+/** @file TestUtils.h
+ * @author Marek Kotewicz <marek@ethdev.com>
+ * @date 2015
+ */
+
+#pragma once
+
+#include <functional>
+#include <string>
+#include <json/json.h>
+#include <libdevcore/TransientDirectory.h>
+#include <libethereum/BlockChain.h>
+#include <libethereum/ClientBase.h>
+
+namespace dev
+{
+namespace test
+{
+
+// should be used for multithread tests
+static SharedMutex x_boostTest;
+#define ETH_CHECK_EQUAL(x, y) { dev::WriteGuard(x_boostTest); BOOST_CHECK_EQUAL(x, y); }
+#define ETH_CHECK_EQUAL_COLLECTIONS(xb, xe, yb, ye) { dev::WriteGuard(x_boostTest); BOOST_CHECK_EQUAL_COLLECTIONS(xb, xe, yb, ye); }
+#define ETH_REQUIRE(x) { dev::WriteGuard(x_boostTest); BOOST_REQUIRE(x); }
+
+struct LoadTestFileFixture
+{
+ LoadTestFileFixture();
+
+protected:
+ Json::Value m_json;
+};
+
+struct ParallelFixture
+{
+ void enumerateThreads(std::function<void()> callback) const;
+};
+
+struct BlockChainFixture: public LoadTestFileFixture
+{
+ void enumerateBlockchains(std::function<void(Json::Value const&, dev::eth::BlockChain const&, dev::eth::State state)> callback) const;
+};
+
+struct ClientBaseFixture: public BlockChainFixture
+{
+ void enumerateClients(std::function<void(Json::Value const&, dev::eth::ClientBase&)> callback) const;
+};
+
+// important BOOST TEST do have problems with thread safety!!!
+// BOOST_CHECK is not thread safe
+// BOOST_MESSAGE is not thread safe
+// http://boost.2283326.n4.nabble.com/Is-boost-test-thread-safe-td3471644.html
+// http://lists.boost.org/boost-users/2010/03/57691.php
+// worth reading
+// https://codecrafter.wordpress.com/2012/11/01/c-unit-test-framework-adapter-part-3/
+struct ParallelClientBaseFixture: public ClientBaseFixture, public ParallelFixture
+{
+ void enumerateClients(std::function<void(Json::Value const&, dev::eth::ClientBase&)> callback) const;
+};
+
+struct JsonRpcFixture: public ClientBaseFixture
+{
+
+};
+
+}
+}
diff --git a/test/boostTest.cpp b/test/boostTest.cpp
new file mode 100644
index 00000000..f448c48e
--- /dev/null
+++ b/test/boostTest.cpp
@@ -0,0 +1,92 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum 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.
+
+ cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+*/
+/** @file boostTest.cpp
+ * @author Marko Simovic <markobarko@gmail.com>
+ * @date 2014
+ * Stub for generating main boost.test module.
+ * Original code taken from boost sources.
+ */
+
+#define BOOST_TEST_MODULE EthereumTests
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wunused-parameter"
+//#define BOOST_DISABLE_WIN32 //disables SEH warning
+#define BOOST_TEST_NO_MAIN
+#include <boost/test/included/unit_test.hpp>
+#pragma GCC diagnostic pop
+
+#include <test/TestHelper.h>
+using namespace boost::unit_test;
+
+//Custom Boost Initialization
+test_suite* init_func( int argc, char* argv[] )
+{
+ if (argc == 0)
+ argv[1]=(char*)"a";
+
+ dev::test::Options::get();
+
+ return 0;
+}
+
+//Custom Boost Unit Test Main
+int main( int argc, char* argv[] )
+{
+ try
+ {
+ framework::init( init_func, argc, argv );
+
+ if( !runtime_config::test_to_run().is_empty() )
+ {
+ test_case_filter filter( runtime_config::test_to_run() );
+
+ traverse_test_tree( framework::master_test_suite().p_id, filter );
+ }
+
+ framework::run();
+
+ results_reporter::make_report();
+
+ return runtime_config::no_result_code()
+ ? boost::exit_success
+ : results_collector.results( framework::master_test_suite().p_id ).result_code();
+ }
+ catch (framework::nothing_to_test const&)
+ {
+ return boost::exit_success;
+ }
+ catch (framework::internal_error const& ex)
+ {
+ results_reporter::get_stream() << "Boost.Test framework internal error: " << ex.what() << std::endl;
+
+ return boost::exit_exception_failure;
+ }
+ catch (framework::setup_error const& ex)
+ {
+ results_reporter::get_stream() << "Test setup error: " << ex.what() << std::endl;
+
+ return boost::exit_exception_failure;
+ }
+ catch (...)
+ {
+ results_reporter::get_stream() << "Boost.Test framework internal error: unknown reason" << std::endl;
+
+ return boost::exit_exception_failure;
+ }
+
+ return 0;
+}
diff --git a/test/contracts/AuctionRegistrar.cpp b/test/contracts/AuctionRegistrar.cpp
new file mode 100644
index 00000000..7c5d9fa3
--- /dev/null
+++ b/test/contracts/AuctionRegistrar.cpp
@@ -0,0 +1,497 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum 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.
+
+ cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * @author Christian <c@ethdev.com>
+ * @date 2015
+ * Tests for a fixed fee registrar contract.
+ */
+
+#include <string>
+#include <tuple>
+#include <boost/test/unit_test.hpp>
+#include <libdevcore/Hash.h>
+#include <libethcore/ABI.h>
+#include <test/libsolidity/solidityExecutionFramework.h>
+
+using namespace std;
+
+namespace dev
+{
+namespace solidity
+{
+namespace test
+{
+
+namespace
+{
+
+static char const* registrarCode = R"DELIMITER(
+//sol
+
+contract NameRegister {
+ function addr(string _name) constant returns (address o_owner);
+ function name(address _owner) constant returns (string o_name);
+}
+
+contract Registrar is NameRegister {
+ event Changed(string indexed name);
+ event PrimaryChanged(string indexed name, address indexed addr);
+
+ function owner(string _name) constant returns (address o_owner);
+ function addr(string _name) constant returns (address o_address);
+ function subRegistrar(string _name) constant returns (address o_subRegistrar);
+ function content(string _name) constant returns (bytes32 o_content);
+
+ function name(address _owner) constant returns (string o_name);
+}
+
+contract AuctionSystem {
+ event AuctionEnded(string indexed _name, address _winner);
+ event NewBid(string indexed _name, address _bidder, uint _value);
+
+ /// Function that is called once an auction ends.
+ function onAuctionEnd(string _name) internal;
+
+ function bid(string _name, address _bidder, uint _value) internal {
+ var auction = m_auctions[_name];
+ if (auction.endDate > 0 && now > auction.endDate)
+ {
+ AuctionEnded(_name, auction.highestBidder);
+ onAuctionEnd(_name);
+ delete m_auctions[_name];
+ return;
+ }
+ if (msg.value > auction.highestBid)
+ {
+ // new bid on auction
+ auction.secondHighestBid = auction.highestBid;
+ auction.sumOfBids += _value;
+ auction.highestBid = _value;
+ auction.highestBidder = _bidder;
+ auction.endDate = now + c_biddingTime;
+
+ NewBid(_name, _bidder, _value);
+ }
+ }
+
+ uint constant c_biddingTime = 7 days;
+
+ struct Auction {
+ address highestBidder;
+ uint highestBid;
+ uint secondHighestBid;
+ uint sumOfBids;
+ uint endDate;
+ }
+ mapping(string => Auction) m_auctions;
+}
+
+contract GlobalRegistrar is Registrar, AuctionSystem {
+ struct Record {
+ address owner;
+ address primary;
+ address subRegistrar;
+ bytes32 content;
+ uint renewalDate;
+ }
+
+ uint constant c_renewalInterval = 1 years;
+ uint constant c_freeBytes = 12;
+
+ function Registrar() {
+ // TODO: Populate with hall-of-fame.
+ }
+
+ function() {
+ // prevent people from just sending funds to the registrar
+ __throw();
+ }
+
+ function onAuctionEnd(string _name) internal {
+ var auction = m_auctions[_name];
+ var record = m_toRecord[_name];
+ if (record.owner != 0)
+ record.owner.send(auction.sumOfBids - auction.highestBid / 100);
+ else
+ auction.highestBidder.send(auction.highestBid - auction.secondHighestBid);
+ record.renewalDate = now + c_renewalInterval;
+ record.owner = auction.highestBidder;
+ Changed(_name);
+ }
+
+ function reserve(string _name) external {
+ if (bytes(_name).length == 0)
+ __throw();
+ bool needAuction = requiresAuction(_name);
+ if (needAuction)
+ {
+ if (now < m_toRecord[_name].renewalDate)
+ __throw();
+ bid(_name, msg.sender, msg.value);
+ }
+ else
+ {
+ Record record = m_toRecord[_name];
+ if (record.owner != 0)
+ __throw();
+ m_toRecord[_name].owner = msg.sender;
+ Changed(_name);
+ }
+ }
+
+ function requiresAuction(string _name) internal returns (bool) {
+ return bytes(_name).length < c_freeBytes;
+ }
+
+ modifier onlyrecordowner(string _name) { if (m_toRecord[_name].owner == msg.sender) _ }
+
+ function transfer(string _name, address _newOwner) onlyrecordowner(_name) {
+ m_toRecord[_name].owner = _newOwner;
+ Changed(_name);
+ }
+
+ function disown(string _name) onlyrecordowner(_name) {
+ if (stringsEqual(m_toName[m_toRecord[_name].primary], _name))
+ {
+ PrimaryChanged(_name, m_toRecord[_name].primary);
+ m_toName[m_toRecord[_name].primary] = "";
+ }
+ delete m_toRecord[_name];
+ Changed(_name);
+ }
+
+ function setAddress(string _name, address _a, bool _primary) onlyrecordowner(_name) {
+ m_toRecord[_name].primary = _a;
+ if (_primary)
+ {
+ PrimaryChanged(_name, _a);
+ m_toName[_a] = _name;
+ }
+ Changed(_name);
+ }
+ function setSubRegistrar(string _name, address _registrar) onlyrecordowner(_name) {
+ m_toRecord[_name].subRegistrar = _registrar;
+ Changed(_name);
+ }
+ function setContent(string _name, bytes32 _content) onlyrecordowner(_name) {
+ m_toRecord[_name].content = _content;
+ Changed(_name);
+ }
+
+ function stringsEqual(string storage _a, string memory _b) internal returns (bool) {
+ bytes storage a = bytes(_a);
+ bytes memory b = bytes(_b);
+ if (a.length != b.length)
+ return false;
+ // @todo unroll this loop
+ for (uint i = 0; i < a.length; i ++)
+ if (a[i] != b[i])
+ return false;
+ return true;
+ }
+
+ function owner(string _name) constant returns (address) { return m_toRecord[_name].owner; }
+ function addr(string _name) constant returns (address) { return m_toRecord[_name].primary; }
+ function subRegistrar(string _name) constant returns (address) { return m_toRecord[_name].subRegistrar; }
+ function content(string _name) constant returns (bytes32) { return m_toRecord[_name].content; }
+ function name(address _addr) constant returns (string o_name) { return m_toName[_addr]; }
+
+ function __throw() internal {
+ // workaround until we have "throw"
+ uint[] x; x[1];
+ }
+
+ mapping (address => string) m_toName;
+ mapping (string => Record) m_toRecord;
+}
+)DELIMITER";
+
+static unique_ptr<bytes> s_compiledRegistrar;
+
+class AuctionRegistrarTestFramework: public ExecutionFramework
+{
+protected:
+ void deployRegistrar()
+ {
+ if (!s_compiledRegistrar)
+ {
+ m_optimize = true;
+ m_compiler.reset(false, m_addStandardSources);
+ m_compiler.addSource("", registrarCode);
+ ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize, m_optimizeRuns), "Compiling contract failed");
+ s_compiledRegistrar.reset(new bytes(m_compiler.getBytecode("GlobalRegistrar")));
+ }
+ sendMessage(*s_compiledRegistrar, true);
+ BOOST_REQUIRE(!m_output.empty());
+ }
+
+ using ContractInterface = ExecutionFramework::ContractInterface;
+ class RegistrarInterface: public ContractInterface
+ {
+ public:
+ RegistrarInterface(ExecutionFramework& _framework): ContractInterface(_framework) {}
+ void reserve(string const& _name)
+ {
+ callString("reserve", _name);
+ }
+ u160 owner(string const& _name)
+ {
+ return callStringReturnsAddress("owner", _name);
+ }
+ void setAddress(string const& _name, u160 const& _address, bool _primary)
+ {
+ callStringAddressBool("setAddress", _name, _address, _primary);
+ }
+ u160 addr(string const& _name)
+ {
+ return callStringReturnsAddress("addr", _name);
+ }
+ string name(u160 const& _addr)
+ {
+ return callAddressReturnsString("name", _addr);
+ }
+ void setSubRegistrar(string const& _name, u160 const& _address)
+ {
+ callStringAddress("setSubRegistrar", _name, _address);
+ }
+ u160 subRegistrar(string const& _name)
+ {
+ return callStringReturnsAddress("subRegistrar", _name);
+ }
+ void setContent(string const& _name, h256 const& _content)
+ {
+ callStringBytes32("setContent", _name, _content);
+ }
+ h256 content(string const& _name)
+ {
+ return callStringReturnsBytes32("content", _name);
+ }
+ void transfer(string const& _name, u160 const& _target)
+ {
+ return callStringAddress("transfer", _name, _target);
+ }
+ void disown(string const& _name)
+ {
+ return callString("disown", _name);
+ }
+ };
+
+ u256 const m_biddingTime = u256(7 * 24 * 3600);
+ u256 const m_renewalInterval = u256(365 * 24 * 3600);
+};
+
+}
+
+/// This is a test suite that tests optimised code!
+BOOST_FIXTURE_TEST_SUITE(SolidityAuctionRegistrar, AuctionRegistrarTestFramework)
+
+BOOST_AUTO_TEST_CASE(creation)
+{
+ deployRegistrar();
+}
+
+BOOST_AUTO_TEST_CASE(reserve)
+{
+ // Test that reserving works for long strings
+ deployRegistrar();
+ vector<string> names{"abcabcabcabcabc", "defdefdefdefdef", "ghighighighighighighighighighighighighighighi"};
+ m_sender = Address(0x123);
+
+ RegistrarInterface registrar(*this);
+
+ // should not work
+ registrar.reserve("");
+ BOOST_CHECK_EQUAL(registrar.owner(""), u160(0));
+
+ for (auto const& name: names)
+ {
+ registrar.reserve(name);
+ BOOST_CHECK_EQUAL(registrar.owner(name), u160(0x123));
+ }
+}
+
+BOOST_AUTO_TEST_CASE(double_reserve_long)
+{
+ // Test that it is not possible to re-reserve from a different address.
+ deployRegistrar();
+ string name = "abcabcabcabcabcabcabcabcabcabca";
+ m_sender = Address(0x123);
+ RegistrarInterface registrar(*this);
+ registrar.reserve(name);
+ BOOST_CHECK_EQUAL(registrar.owner(name), u160(0x123));
+
+ m_sender = Address(0x124);
+ registrar.reserve(name);
+ BOOST_CHECK_EQUAL(registrar.owner(name), u160(0x123));
+}
+
+BOOST_AUTO_TEST_CASE(properties)
+{
+ // Test setting and retrieving the various properties works.
+ deployRegistrar();
+ RegistrarInterface registrar(*this);
+ string names[] = {"abcaeouoeuaoeuaoeu", "defncboagufra,fui", "ghagpyajfbcuajouhaeoi"};
+ size_t addr = 0x9872543;
+ for (string const& name: names)
+ {
+ addr++;
+ size_t sender = addr + 10007;
+ m_sender = Address(sender);
+ // setting by sender works
+ registrar.reserve(name);
+ BOOST_CHECK_EQUAL(registrar.owner(name), u160(sender));
+ registrar.setAddress(name, addr, true);
+ BOOST_CHECK_EQUAL(registrar.addr(name), u160(addr));
+ registrar.setSubRegistrar(name, addr + 20);
+ BOOST_CHECK_EQUAL(registrar.subRegistrar(name), u160(addr + 20));
+ registrar.setContent(name, h256(u256(addr + 90)));
+ BOOST_CHECK_EQUAL(registrar.content(name), h256(u256(addr + 90)));
+
+ // but not by someone else
+ m_sender = Address(h256(addr + 10007 - 1));
+ BOOST_CHECK_EQUAL(registrar.owner(name), u160(sender));
+ registrar.setAddress(name, addr + 1, true);
+ BOOST_CHECK_EQUAL(registrar.addr(name), u160(addr));
+ registrar.setSubRegistrar(name, addr + 20 + 1);
+ BOOST_CHECK_EQUAL(registrar.subRegistrar(name), u160(addr + 20));
+ registrar.setContent(name, h256(u256(addr + 90 + 1)));
+ BOOST_CHECK_EQUAL(registrar.content(name), h256(u256(addr + 90)));
+ }
+}
+
+BOOST_AUTO_TEST_CASE(transfer)
+{
+ deployRegistrar();
+ string name = "abcaoeguaoucaeoduceo";
+ m_sender = Address(0x123);
+ RegistrarInterface registrar(*this);
+ registrar.reserve(name);
+ registrar.setContent(name, h256(u256(123)));
+ registrar.transfer(name, u160(555));
+ BOOST_CHECK_EQUAL(registrar.owner(name), u160(555));
+ BOOST_CHECK_EQUAL(registrar.content(name), h256(u256(123)));
+}
+
+BOOST_AUTO_TEST_CASE(disown)
+{
+ deployRegistrar();
+ string name = "abcaoeguaoucaeoduceo";
+ m_sender = Address(0x123);
+ RegistrarInterface registrar(*this);
+ registrar.reserve(name);
+ registrar.setContent(name, h256(u256(123)));
+ registrar.setAddress(name, u160(124), true);
+ registrar.setSubRegistrar(name, u160(125));
+ BOOST_CHECK_EQUAL(registrar.name(u160(124)), name);
+
+ // someone else tries disowning
+ m_sender = Address(0x128);
+ registrar.disown(name);
+ BOOST_CHECK_EQUAL(registrar.owner(name), 0x123);
+
+ m_sender = Address(0x123);
+ registrar.disown(name);
+ BOOST_CHECK_EQUAL(registrar.owner(name), 0);
+ BOOST_CHECK_EQUAL(registrar.addr(name), 0);
+ BOOST_CHECK_EQUAL(registrar.subRegistrar(name), 0);
+ BOOST_CHECK_EQUAL(registrar.content(name), h256());
+ BOOST_CHECK_EQUAL(registrar.name(u160(124)), "");
+}
+
+BOOST_AUTO_TEST_CASE(auction_simple)
+{
+ deployRegistrar();
+ string name = "x";
+ m_sender = Address(0x123);
+ RegistrarInterface registrar(*this);
+ // initiate auction
+ registrar.setNextValue(8);
+ registrar.reserve(name);
+ BOOST_CHECK_EQUAL(registrar.owner(name), 0);
+ // "wait" until auction end
+ m_envInfo.setTimestamp(m_envInfo.timestamp() + m_biddingTime + 10);
+ // trigger auction again
+ registrar.reserve(name);
+ BOOST_CHECK_EQUAL(registrar.owner(name), 0x123);
+}
+
+BOOST_AUTO_TEST_CASE(auction_bidding)
+{
+ deployRegistrar();
+ string name = "x";
+ m_sender = Address(0x123);
+ RegistrarInterface registrar(*this);
+ // initiate auction
+ registrar.setNextValue(8);
+ registrar.reserve(name);
+ BOOST_CHECK_EQUAL(registrar.owner(name), 0);
+ // overbid self
+ m_envInfo.setTimestamp(m_biddingTime - 10);
+ registrar.setNextValue(12);
+ registrar.reserve(name);
+ // another bid by someone else
+ m_sender = Address(0x124);
+ m_envInfo.setTimestamp(2 * m_biddingTime - 50);
+ registrar.setNextValue(13);
+ registrar.reserve(name);
+ BOOST_CHECK_EQUAL(registrar.owner(name), 0);
+ // end auction by first bidder (which is not highest) trying to overbid again (too late)
+ m_sender = Address(0x123);
+ m_envInfo.setTimestamp(4 * m_biddingTime);
+ registrar.setNextValue(20);
+ registrar.reserve(name);
+ BOOST_CHECK_EQUAL(registrar.owner(name), 0x124);
+}
+
+BOOST_AUTO_TEST_CASE(auction_renewal)
+{
+ deployRegistrar();
+ string name = "x";
+ RegistrarInterface registrar(*this);
+ // register name by auction
+ m_sender = Address(0x123);
+ registrar.setNextValue(8);
+ registrar.reserve(name);
+ m_envInfo.setTimestamp(4 * m_biddingTime);
+ registrar.reserve(name);
+ BOOST_CHECK_EQUAL(registrar.owner(name), 0x123);
+
+ // try to re-register before interval end
+ m_sender = Address(0x222);
+ registrar.setNextValue(80);
+ m_envInfo.setTimestamp(m_envInfo.timestamp() + m_renewalInterval - 1);
+ registrar.reserve(name);
+ m_envInfo.setTimestamp(m_envInfo.timestamp() + m_biddingTime);
+ // if there is a bug in the renewal logic, this would transfer the ownership to 0x222,
+ // but if there is no bug, this will initiate the auction, albeit with a zero bid
+ registrar.reserve(name);
+ BOOST_CHECK_EQUAL(registrar.owner(name), 0x123);
+
+ m_envInfo.setTimestamp(m_envInfo.timestamp() + 2);
+ registrar.setNextValue(80);
+ registrar.reserve(name);
+ BOOST_CHECK_EQUAL(registrar.owner(name), 0x123);
+ m_envInfo.setTimestamp(m_envInfo.timestamp() + m_biddingTime + 2);
+ registrar.reserve(name);
+ BOOST_CHECK_EQUAL(registrar.owner(name), 0x222);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+}
+}
+} // end namespaces
diff --git a/test/contracts/CMakeLists.txt b/test/contracts/CMakeLists.txt
new file mode 100644
index 00000000..3ceda13b
--- /dev/null
+++ b/test/contracts/CMakeLists.txt
@@ -0,0 +1,5 @@
+cmake_policy(SET CMP0015 NEW)
+
+aux_source_directory(. SRCS)
+
+add_sources(${SRCS})
diff --git a/test/contracts/FixedFeeRegistrar.cpp b/test/contracts/FixedFeeRegistrar.cpp
new file mode 100644
index 00000000..ed2ecf0a
--- /dev/null
+++ b/test/contracts/FixedFeeRegistrar.cpp
@@ -0,0 +1,242 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum 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.
+
+ cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * @author Christian <c@ethdev.com>
+ * @date 2015
+ * Tests for a fixed fee registrar contract.
+ */
+
+#include <string>
+#include <tuple>
+#include <boost/test/unit_test.hpp>
+#include <libdevcore/Hash.h>
+#include <test/libsolidity/solidityExecutionFramework.h>
+
+using namespace std;
+
+namespace dev
+{
+namespace solidity
+{
+namespace test
+{
+
+namespace
+{
+
+static char const* registrarCode = R"DELIMITER(
+//sol FixedFeeRegistrar
+// Simple global registrar with fixed-fee reservations.
+// @authors:
+// Gav Wood <g@ethdev.com>
+
+contract Registrar {
+ event Changed(string indexed name);
+
+ function owner(string _name) constant returns (address o_owner);
+ function addr(string _name) constant returns (address o_address);
+ function subRegistrar(string _name) constant returns (address o_subRegistrar);
+ function content(string _name) constant returns (bytes32 o_content);
+}
+
+contract FixedFeeRegistrar is Registrar {
+ struct Record {
+ address addr;
+ address subRegistrar;
+ bytes32 content;
+ address owner;
+ }
+
+ modifier onlyrecordowner(string _name) { if (m_record(_name).owner == msg.sender) _ }
+
+ function reserve(string _name) {
+ Record rec = m_record(_name);
+ if (rec.owner == 0 && msg.value >= c_fee) {
+ rec.owner = msg.sender;
+ Changed(_name);
+ }
+ }
+ function disown(string _name, address _refund) onlyrecordowner(_name) {
+ delete m_recordData[uint(sha3(_name)) / 8];
+ _refund.send(c_fee);
+ Changed(_name);
+ }
+ function transfer(string _name, address _newOwner) onlyrecordowner(_name) {
+ m_record(_name).owner = _newOwner;
+ Changed(_name);
+ }
+ function setAddr(string _name, address _a) onlyrecordowner(_name) {
+ m_record(_name).addr = _a;
+ Changed(_name);
+ }
+ function setSubRegistrar(string _name, address _registrar) onlyrecordowner(_name) {
+ m_record(_name).subRegistrar = _registrar;
+ Changed(_name);
+ }
+ function setContent(string _name, bytes32 _content) onlyrecordowner(_name) {
+ m_record(_name).content = _content;
+ Changed(_name);
+ }
+
+ function record(string _name) constant returns (address o_addr, address o_subRegistrar, bytes32 o_content, address o_owner) {
+ Record rec = m_record(_name);
+ o_addr = rec.addr;
+ o_subRegistrar = rec.subRegistrar;
+ o_content = rec.content;
+ o_owner = rec.owner;
+ }
+ function addr(string _name) constant returns (address) { return m_record(_name).addr; }
+ function subRegistrar(string _name) constant returns (address) { return m_record(_name).subRegistrar; }
+ function content(string _name) constant returns (bytes32) { return m_record(_name).content; }
+ function owner(string _name) constant returns (address) { return m_record(_name).owner; }
+
+ Record[2**253] m_recordData;
+ function m_record(string _name) constant internal returns (Record storage o_record) {
+ return m_recordData[uint(sha3(_name)) / 8];
+ }
+ uint constant c_fee = 69 ether;
+}
+)DELIMITER";
+
+static unique_ptr<bytes> s_compiledRegistrar;
+
+class RegistrarTestFramework: public ExecutionFramework
+{
+protected:
+ void deployRegistrar()
+ {
+ if (!s_compiledRegistrar)
+ {
+ m_optimize = true;
+ m_compiler.reset(false, m_addStandardSources);
+ m_compiler.addSource("", registrarCode);
+ ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize, m_optimizeRuns), "Compiling contract failed");
+ s_compiledRegistrar.reset(new bytes(m_compiler.getBytecode("FixedFeeRegistrar")));
+ }
+ sendMessage(*s_compiledRegistrar, true);
+ BOOST_REQUIRE(!m_output.empty());
+ }
+ u256 const m_fee = u256("69000000000000000000");
+};
+
+}
+
+/// This is a test suite that tests optimised code!
+BOOST_FIXTURE_TEST_SUITE(SolidityFixedFeeRegistrar, RegistrarTestFramework)
+
+BOOST_AUTO_TEST_CASE(creation)
+{
+ deployRegistrar();
+}
+
+BOOST_AUTO_TEST_CASE(reserve)
+{
+ // Test that reserving works and fee is taken into account.
+ deployRegistrar();
+ string name[] = {"abc", "def", "ghi"};
+ m_sender = Address(0x123);
+ BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee, encodeDyn(name[0])) == encodeArgs());
+ BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name[0])) == encodeArgs(h256(0x123)));
+ BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee + 1, encodeDyn(name[1])) == encodeArgs());
+ BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name[1])) == encodeArgs(h256(0x123)));
+ BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee - 1, encodeDyn(name[2])) == encodeArgs());
+ BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name[2])) == encodeArgs(h256(0)));
+}
+
+BOOST_AUTO_TEST_CASE(double_reserve)
+{
+ // Test that it is not possible to re-reserve from a different address.
+ deployRegistrar();
+ string name = "abc";
+ m_sender = Address(0x123);
+ BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee, encodeDyn(name)) == encodeArgs());
+ BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(h256(0x123)));
+
+ m_sender = Address(0x124);
+ BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee, encodeDyn(name)) == encodeArgs());
+ BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(h256(0x123)));
+}
+
+BOOST_AUTO_TEST_CASE(properties)
+{
+ // Test setting and retrieving the various properties works.
+ deployRegistrar();
+ string names[] = {"abc", "def", "ghi"};
+ size_t addr = 0x9872543;
+ for (string const& name: names)
+ {
+ addr++;
+ size_t sender = addr + 10007;
+ m_sender = Address(sender);
+ // setting by sender works
+ BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee, encodeDyn(name)) == encodeArgs());
+ BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(u256(sender)));
+ BOOST_CHECK(callContractFunction("setAddr(string,address)", u256(0x40), u256(addr), u256(name.length()), name) == encodeArgs());
+ BOOST_CHECK(callContractFunction("addr(string)", encodeDyn(name)) == encodeArgs(addr));
+ BOOST_CHECK(callContractFunction("setSubRegistrar(string,address)", u256(0x40), addr + 20, u256(name.length()), name) == encodeArgs());
+ BOOST_CHECK(callContractFunction("subRegistrar(string)", encodeDyn(name)) == encodeArgs(addr + 20));
+ BOOST_CHECK(callContractFunction("setContent(string,bytes32)", u256(0x40), addr + 90, u256(name.length()), name) == encodeArgs());
+ BOOST_CHECK(callContractFunction("content(string)", encodeDyn(name)) == encodeArgs(addr + 90));
+ // but not by someone else
+ m_sender = Address(h256(addr + 10007 - 1));
+ BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(sender));
+ BOOST_CHECK(callContractFunction("setAddr(string,address)", u256(0x40), addr + 1, u256(name.length()), name) == encodeArgs());
+ BOOST_CHECK(callContractFunction("addr(string)", encodeDyn(name)) == encodeArgs(addr));
+ BOOST_CHECK(callContractFunction("setSubRegistrar(string,address)", u256(0x40), addr + 20 + 1, u256(name.length()), name) == encodeArgs());
+ BOOST_CHECK(callContractFunction("subRegistrar(string)", encodeDyn(name)) == encodeArgs(addr + 20));
+ BOOST_CHECK(callContractFunction("setContent(string,bytes32)", u256(0x40), addr + 90 + 1, u256(name.length()), name) == encodeArgs());
+ BOOST_CHECK(callContractFunction("content(string)", encodeDyn(name)) == encodeArgs(addr + 90));
+ }
+}
+
+BOOST_AUTO_TEST_CASE(transfer)
+{
+ deployRegistrar();
+ string name = "abc";
+ m_sender = Address(0x123);
+ BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee, encodeDyn(name)) == encodeArgs());
+ BOOST_CHECK(callContractFunction("setContent(string,bytes32)", u256(0x40), u256(123), u256(name.length()), name) == encodeArgs());
+ BOOST_CHECK(callContractFunction("transfer(string,address)", u256(0x40), u256(555), u256(name.length()), name) == encodeArgs());
+ BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(u256(555)));
+ BOOST_CHECK(callContractFunction("content(string)", encodeDyn(name)) == encodeArgs(u256(123)));
+}
+
+BOOST_AUTO_TEST_CASE(disown)
+{
+ deployRegistrar();
+ string name = "abc";
+ m_sender = Address(0x123);
+ BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee, encodeDyn(name)) == encodeArgs());
+ BOOST_CHECK(callContractFunction("setContent(string,bytes32)", u256(0x40), u256(123), u256(name.length()), name) == encodeArgs());
+ BOOST_CHECK(callContractFunction("setAddr(string,address)", u256(0x40), u256(124), u256(name.length()), name) == encodeArgs());
+ BOOST_CHECK(callContractFunction("setSubRegistrar(string,address)", u256(0x40), u256(125), u256(name.length()), name) == encodeArgs());
+
+ BOOST_CHECK_EQUAL(m_state.balance(Address(0x124)), 0);
+ BOOST_CHECK(callContractFunction("disown(string,address)", u256(0x40), u256(0x124), name.size(), name) == encodeArgs());
+ BOOST_CHECK_EQUAL(m_state.balance(Address(0x124)), m_fee);
+
+ BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(u256(0)));
+ BOOST_CHECK(callContractFunction("content(string)", encodeDyn(name)) == encodeArgs(u256(0)));
+ BOOST_CHECK(callContractFunction("addr(string)", encodeDyn(name)) == encodeArgs(u256(0)));
+ BOOST_CHECK(callContractFunction("subRegistrar(string)", encodeDyn(name)) == encodeArgs(u256(0)));
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+}
+}
+} // end namespaces
diff --git a/test/contracts/Wallet.cpp b/test/contracts/Wallet.cpp
new file mode 100644
index 00000000..5f9febd4
--- /dev/null
+++ b/test/contracts/Wallet.cpp
@@ -0,0 +1,668 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum 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.
+
+ cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * @author Christian <c@ethdev.com>
+ * @date 2015
+ * Tests for a (comparatively) complex multisig wallet contract.
+ */
+
+#include <string>
+#include <tuple>
+#include <boost/test/unit_test.hpp>
+#include <libdevcore/Hash.h>
+#include <test/libsolidity/solidityExecutionFramework.h>
+
+using namespace std;
+
+namespace dev
+{
+namespace solidity
+{
+namespace test
+{
+
+static char const* walletCode = R"DELIMITER(
+//sol Wallet
+// Multi-sig, daily-limited account proxy/wallet.
+// @authors:
+// Gav Wood <g@ethdev.com>
+// inheritable "property" contract that enables methods to be protected by requiring the acquiescence of either a
+// single, or, crucially, each of a number of, designated owners.
+// usage:
+// use modifiers onlyowner (just own owned) or onlymanyowners(hash), whereby the same hash must be provided by
+// some number (specified in constructor) of the set of owners (specified in the constructor, modifiable) before the
+// interior is executed.
+contract multiowned {
+
+ // TYPES
+
+ // struct for the status of a pending operation.
+ struct PendingState {
+ uint yetNeeded;
+ uint ownersDone;
+ uint index;
+ }
+
+ // EVENTS
+
+ // this contract only has five types of events: it can accept a confirmation, in which case
+ // we record owner and operation (hash) alongside it.
+ event Confirmation(address owner, bytes32 operation);
+ event Revoke(address owner, bytes32 operation);
+ // some others are in the case of an owner changing.
+ event OwnerChanged(address oldOwner, address newOwner);
+ event OwnerAdded(address newOwner);
+ event OwnerRemoved(address oldOwner);
+ // the last one is emitted if the required signatures change
+ event RequirementChanged(uint newRequirement);
+
+ // MODIFIERS
+
+ // simple single-sig function modifier.
+ modifier onlyowner {
+ if (isOwner(msg.sender))
+ _
+ }
+ // multi-sig function modifier: the operation must have an intrinsic hash in order
+ // that later attempts can be realised as the same underlying operation and
+ // thus count as confirmations.
+ modifier onlymanyowners(bytes32 _operation) {
+ if (confirmAndCheck(_operation))
+ _
+ }
+
+ // METHODS
+
+ // constructor is given number of sigs required to do protected "onlymanyowners" transactions
+ // as well as the selection of addresses capable of confirming them.
+ function multiowned(address[] _owners, uint _required) {
+ m_numOwners = _owners.length + 1;
+ m_owners[1] = uint(msg.sender);
+ m_ownerIndex[uint(msg.sender)] = 1;
+ for (uint i = 0; i < _owners.length; ++i)
+ {
+ m_owners[2 + i] = uint(_owners[i]);
+ m_ownerIndex[uint(_owners[i])] = 2 + i;
+ }
+ m_required = _required;
+ }
+
+ // Revokes a prior confirmation of the given operation
+ function revoke(bytes32 _operation) external {
+ uint ownerIndex = m_ownerIndex[uint(msg.sender)];
+ // make sure they're an owner
+ if (ownerIndex == 0) return;
+ uint ownerIndexBit = 2**ownerIndex;
+ var pending = m_pending[_operation];
+ if (pending.ownersDone & ownerIndexBit > 0) {
+ pending.yetNeeded++;
+ pending.ownersDone -= ownerIndexBit;
+ Revoke(msg.sender, _operation);
+ }
+ }
+
+ // Replaces an owner `_from` with another `_to`.
+ function changeOwner(address _from, address _to) onlymanyowners(sha3(msg.data)) external {
+ if (isOwner(_to)) return;
+ uint ownerIndex = m_ownerIndex[uint(_from)];
+ if (ownerIndex == 0) return;
+
+ clearPending();
+ m_owners[ownerIndex] = uint(_to);
+ m_ownerIndex[uint(_from)] = 0;
+ m_ownerIndex[uint(_to)] = ownerIndex;
+ OwnerChanged(_from, _to);
+ }
+
+ function addOwner(address _owner) onlymanyowners(sha3(msg.data)) external {
+ if (isOwner(_owner)) return;
+
+ clearPending();
+ if (m_numOwners >= c_maxOwners)
+ reorganizeOwners();
+ if (m_numOwners >= c_maxOwners)
+ return;
+ m_numOwners++;
+ m_owners[m_numOwners] = uint(_owner);
+ m_ownerIndex[uint(_owner)] = m_numOwners;
+ OwnerAdded(_owner);
+ }
+
+ function removeOwner(address _owner) onlymanyowners(sha3(msg.data)) external {
+ uint ownerIndex = m_ownerIndex[uint(_owner)];
+ if (ownerIndex == 0) return;
+ if (m_required > m_numOwners - 1) return;
+
+ m_owners[ownerIndex] = 0;
+ m_ownerIndex[uint(_owner)] = 0;
+ clearPending();
+ reorganizeOwners(); //make sure m_numOwner is equal to the number of owners and always points to the optimal free slot
+ OwnerRemoved(_owner);
+ }
+
+ function changeRequirement(uint _newRequired) onlymanyowners(sha3(msg.data)) external {
+ if (_newRequired > m_numOwners) return;
+ m_required = _newRequired;
+ clearPending();
+ RequirementChanged(_newRequired);
+ }
+
+ function isOwner(address _addr) returns (bool) {
+ return m_ownerIndex[uint(_addr)] > 0;
+ }
+
+ function hasConfirmed(bytes32 _operation, address _owner) constant returns (bool) {
+ var pending = m_pending[_operation];
+ uint ownerIndex = m_ownerIndex[uint(_owner)];
+
+ // make sure they're an owner
+ if (ownerIndex == 0) return false;
+
+ // determine the bit to set for this owner.
+ uint ownerIndexBit = 2**ownerIndex;
+ if (pending.ownersDone & ownerIndexBit == 0) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+ // INTERNAL METHODS
+
+ function confirmAndCheck(bytes32 _operation) internal returns (bool) {
+ // determine what index the present sender is:
+ uint ownerIndex = m_ownerIndex[uint(msg.sender)];
+ // make sure they're an owner
+ if (ownerIndex == 0) return;
+
+ var pending = m_pending[_operation];
+ // if we're not yet working on this operation, switch over and reset the confirmation status.
+ if (pending.yetNeeded == 0) {
+ // reset count of confirmations needed.
+ pending.yetNeeded = m_required;
+ // reset which owners have confirmed (none) - set our bitmap to 0.
+ pending.ownersDone = 0;
+ pending.index = m_pendingIndex.length++;
+ m_pendingIndex[pending.index] = _operation;
+ }
+ // determine the bit to set for this owner.
+ uint ownerIndexBit = 2**ownerIndex;
+ // make sure we (the message sender) haven't confirmed this operation previously.
+ if (pending.ownersDone & ownerIndexBit == 0) {
+ Confirmation(msg.sender, _operation);
+ // ok - check if count is enough to go ahead.
+ if (pending.yetNeeded <= 1) {
+ // enough confirmations: reset and run interior.
+ delete m_pendingIndex[m_pending[_operation].index];
+ delete m_pending[_operation];
+ return true;
+ }
+ else
+ {
+ // not enough: record that this owner in particular confirmed.
+ pending.yetNeeded--;
+ pending.ownersDone |= ownerIndexBit;
+ }
+ }
+ }
+
+ function reorganizeOwners() private returns (bool) {
+ uint free = 1;
+ while (free < m_numOwners)
+ {
+ while (free < m_numOwners && m_owners[free] != 0) free++;
+ while (m_numOwners > 1 && m_owners[m_numOwners] == 0) m_numOwners--;
+ if (free < m_numOwners && m_owners[m_numOwners] != 0 && m_owners[free] == 0)
+ {
+ m_owners[free] = m_owners[m_numOwners];
+ m_ownerIndex[m_owners[free]] = free;
+ m_owners[m_numOwners] = 0;
+ }
+ }
+ }
+
+ function clearPending() internal {
+ uint length = m_pendingIndex.length;
+ for (uint i = 0; i < length; ++i)
+ if (m_pendingIndex[i] != 0)
+ delete m_pending[m_pendingIndex[i]];
+ delete m_pendingIndex;
+ }
+
+ // FIELDS
+
+ // the number of owners that must confirm the same operation before it is run.
+ uint public m_required;
+ // pointer used to find a free slot in m_owners
+ uint public m_numOwners;
+
+ // list of owners
+ uint[256] m_owners;
+ uint constant c_maxOwners = 250;
+ // index on the list of owners to allow reverse lookup
+ mapping(uint => uint) m_ownerIndex;
+ // the ongoing operations.
+ mapping(bytes32 => PendingState) m_pending;
+ bytes32[] m_pendingIndex;
+}
+
+// inheritable "property" contract that enables methods to be protected by placing a linear limit (specifiable)
+// on a particular resource per calendar day. is multiowned to allow the limit to be altered. resource that method
+// uses is specified in the modifier.
+contract daylimit is multiowned {
+
+ // MODIFIERS
+
+ // simple modifier for daily limit.
+ modifier limitedDaily(uint _value) {
+ if (underLimit(_value))
+ _
+ }
+
+ // METHODS
+
+ // constructor - stores initial daily limit and records the present day's index.
+ function daylimit(uint _limit) {
+ m_dailyLimit = _limit;
+ m_lastDay = today();
+ }
+ // (re)sets the daily limit. needs many of the owners to confirm. doesn't alter the amount already spent today.
+ function setDailyLimit(uint _newLimit) onlymanyowners(sha3(msg.data)) external {
+ m_dailyLimit = _newLimit;
+ }
+ // (re)sets the daily limit. needs many of the owners to confirm. doesn't alter the amount already spent today.
+ function resetSpentToday() onlymanyowners(sha3(msg.data)) external {
+ m_spentToday = 0;
+ }
+
+ // INTERNAL METHODS
+
+ // checks to see if there is at least `_value` left from the daily limit today. if there is, subtracts it and
+ // returns true. otherwise just returns false.
+ function underLimit(uint _value) internal onlyowner returns (bool) {
+ // reset the spend limit if we're on a different day to last time.
+ if (today() > m_lastDay) {
+ m_spentToday = 0;
+ m_lastDay = today();
+ }
+ // check to see if there's enough left - if so, subtract and return true.
+ if (m_spentToday + _value >= m_spentToday && m_spentToday + _value <= m_dailyLimit) {
+ m_spentToday += _value;
+ return true;
+ }
+ return false;
+ }
+ // determines today's index.
+ function today() private constant returns (uint) { return now / 1 days; }
+
+ // FIELDS
+
+ uint public m_dailyLimit;
+ uint m_spentToday;
+ uint m_lastDay;
+}
+
+// interface contract for multisig proxy contracts; see below for docs.
+contract multisig {
+
+ // EVENTS
+
+ // logged events:
+ // Funds has arrived into the wallet (record how much).
+ event Deposit(address from, uint value);
+ // Single transaction going out of the wallet (record who signed for it, how much, and to whom it's going).
+ event SingleTransact(address owner, uint value, address to, bytes data);
+ // Multi-sig transaction going out of the wallet (record who signed for it last, the operation hash, how much, and to whom it's going).
+ event MultiTransact(address owner, bytes32 operation, uint value, address to, bytes data);
+ // Confirmation still needed for a transaction.
+ event ConfirmationNeeded(bytes32 operation, address initiator, uint value, address to, bytes data);
+
+ // FUNCTIONS
+
+ // TODO: document
+ function changeOwner(address _from, address _to) external;
+ function execute(address _to, uint _value, bytes _data) external returns (bytes32);
+ function confirm(bytes32 _h) returns (bool);
+}
+
+// usage:
+// bytes32 h = Wallet(w).from(oneOwner).transact(to, value, data);
+// Wallet(w).from(anotherOwner).confirm(h);
+contract Wallet is multisig, multiowned, daylimit {
+
+ // TYPES
+
+ // Transaction structure to remember details of transaction lest it need be saved for a later call.
+ struct Transaction {
+ address to;
+ uint value;
+ bytes data;
+ }
+
+ // METHODS
+
+ // constructor - just pass on the owner array to the multiowned and
+ // the limit to daylimit
+ function Wallet(address[] _owners, uint _required, uint _daylimit)
+ multiowned(_owners, _required) daylimit(_daylimit) {
+ }
+
+ // kills the contract sending everything to `_to`.
+ function kill(address _to) onlymanyowners(sha3(msg.data)) external {
+ suicide(_to);
+ }
+
+ // gets called when no other function matches
+ function() {
+ // just being sent some cash?
+ if (msg.value > 0)
+ Deposit(msg.sender, msg.value);
+ }
+
+ // Outside-visible transact entry point. Executes transacion immediately if below daily spend limit.
+ // If not, goes into multisig process. We provide a hash on return to allow the sender to provide
+ // shortcuts for the other confirmations (allowing them to avoid replicating the _to, _value
+ // and _data arguments). They still get the option of using them if they want, anyways.
+ function execute(address _to, uint _value, bytes _data) external onlyowner returns (bytes32 _r) {
+ // first, take the opportunity to check that we're under the daily limit.
+ if (underLimit(_value)) {
+ SingleTransact(msg.sender, _value, _to, _data);
+ // yes - just execute the call.
+ _to.call.value(_value)(_data);
+ return 0;
+ }
+ // determine our operation hash.
+ _r = sha3(msg.data, block.number);
+ if (!confirm(_r) && m_txs[_r].to == 0) {
+ m_txs[_r].to = _to;
+ m_txs[_r].value = _value;
+ m_txs[_r].data = _data;
+ ConfirmationNeeded(_r, msg.sender, _value, _to, _data);
+ }
+ }
+
+ // confirm a transaction through just the hash. we use the previous transactions map, m_txs, in order
+ // to determine the body of the transaction from the hash provided.
+ function confirm(bytes32 _h) onlymanyowners(_h) returns (bool) {
+ if (m_txs[_h].to != 0) {
+ m_txs[_h].to.call.value(m_txs[_h].value)(m_txs[_h].data);
+ MultiTransact(msg.sender, _h, m_txs[_h].value, m_txs[_h].to, m_txs[_h].data);
+ delete m_txs[_h];
+ return true;
+ }
+ }
+
+ // INTERNAL METHODS
+
+ function clearPending() internal {
+ uint length = m_pendingIndex.length;
+ for (uint i = 0; i < length; ++i)
+ delete m_txs[m_pendingIndex[i]];
+ super.clearPending();
+ }
+
+ // FIELDS
+
+ // pending transactions we have at present.
+ mapping (bytes32 => Transaction) m_txs;
+}
+)DELIMITER";
+
+static unique_ptr<bytes> s_compiledWallet;
+
+class WalletTestFramework: public ExecutionFramework
+{
+protected:
+ void deployWallet(
+ u256 const& _value = 0,
+ vector<u256> const& _owners = vector<u256>{},
+ u256 _required = 1,
+ u256 _dailyLimit = 0
+ )
+ {
+ if (!s_compiledWallet)
+ {
+ m_optimize = true;
+ m_compiler.reset(false, m_addStandardSources);
+ m_compiler.addSource("", walletCode);
+ ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize, m_optimizeRuns), "Compiling contract failed");
+ s_compiledWallet.reset(new bytes(m_compiler.getBytecode("Wallet")));
+ }
+ bytes args = encodeArgs(u256(0x60), _required, _dailyLimit, u256(_owners.size()), _owners);
+ sendMessage(*s_compiledWallet + args, true, _value);
+ BOOST_REQUIRE(!m_output.empty());
+ }
+};
+
+/// This is a test suite that tests optimised code!
+BOOST_FIXTURE_TEST_SUITE(SolidityWallet, WalletTestFramework)
+
+BOOST_AUTO_TEST_CASE(creation)
+{
+ deployWallet(200);
+ BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(m_sender, h256::AlignRight)) == encodeArgs(true));
+ BOOST_REQUIRE(callContractFunction("isOwner(address)", ~h256(m_sender, h256::AlignRight)) == encodeArgs(false));
+}
+
+BOOST_AUTO_TEST_CASE(add_owners)
+{
+ deployWallet(200);
+ Address originalOwner = m_sender;
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12)) == encodeArgs());
+ BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12)) == encodeArgs(true));
+ // now let the new owner add someone
+ m_sender = Address(0x12);
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x13)) == encodeArgs());
+ BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x13)) == encodeArgs(true));
+ // and check that a non-owner cannot add a new owner
+ m_sender = Address(0x50);
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x20)) == encodeArgs());
+ BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x20)) == encodeArgs(false));
+ // finally check that all the owners are there
+ BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(originalOwner, h256::AlignRight)) == encodeArgs(true));
+ BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12)) == encodeArgs(true));
+ BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x13)) == encodeArgs(true));
+}
+
+BOOST_AUTO_TEST_CASE(change_owners)
+{
+ deployWallet(200);
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12)) == encodeArgs());
+ BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12)) == encodeArgs(true));
+ BOOST_REQUIRE(callContractFunction("changeOwner(address,address)", h256(0x12), h256(0x13)) == encodeArgs());
+ BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12)) == encodeArgs(false));
+ BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x13)) == encodeArgs(true));
+}
+
+BOOST_AUTO_TEST_CASE(remove_owner)
+{
+ deployWallet(200);
+ // add 10 owners
+ for (unsigned i = 0; i < 10; ++i)
+ {
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12 + i)) == encodeArgs());
+ BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12 + i)) == encodeArgs(true));
+ }
+ // check they are there again
+ for (unsigned i = 0; i < 10; ++i)
+ BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12 + i)) == encodeArgs(true));
+ // remove the odd owners
+ for (unsigned i = 0; i < 10; ++i)
+ if (i % 2 == 1)
+ BOOST_REQUIRE(callContractFunction("removeOwner(address)", h256(0x12 + i)) == encodeArgs());
+ // check the result
+ for (unsigned i = 0; i < 10; ++i)
+ BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12 + i)) == encodeArgs(i % 2 == 0));
+ // add them again
+ for (unsigned i = 0; i < 10; ++i)
+ if (i % 2 == 1)
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12 + i)) == encodeArgs());
+ // check everyone is there
+ for (unsigned i = 0; i < 10; ++i)
+ BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12 + i)) == encodeArgs(true));
+}
+
+BOOST_AUTO_TEST_CASE(initial_owners)
+{
+ vector<u256> owners{
+ u256("0x00000000000000000000000042c56279432962a17176998a4747d1b4d6ed4367"),
+ u256("0x000000000000000000000000d4d4669f5ba9f4c27d38ef02a358c339b5560c47"),
+ u256("0x000000000000000000000000e6716f9544a56c530d868e4bfbacb172315bdead"),
+ u256("0x000000000000000000000000775e18be7a50a0abb8a4e82b1bd697d79f31fe04"),
+ u256("0x000000000000000000000000f4dd5c3794f1fd0cdc0327a83aa472609c806e99"),
+ u256("0x0000000000000000000000004c9113886af165b2de069d6e99430647e94a9fff"),
+ u256("0x0000000000000000000000003fb1cd2cd96c6d5c0b5eb3322d807b34482481d4")
+ };
+ deployWallet(0, owners, 4, 2);
+ BOOST_CHECK(callContractFunction("m_numOwners()") == encodeArgs(u256(8)));
+ BOOST_CHECK(callContractFunction("isOwner(address)", h256(m_sender, h256::AlignRight)) == encodeArgs(true));
+ for (u256 const& owner: owners)
+ {
+ BOOST_CHECK(callContractFunction("isOwner(address)", owner) == encodeArgs(true));
+ }
+}
+
+BOOST_AUTO_TEST_CASE(multisig_value_transfer)
+{
+ deployWallet(200);
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12)) == encodeArgs());
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x13)) == encodeArgs());
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x14)) == encodeArgs());
+ // 4 owners, set required to 3
+ BOOST_REQUIRE(callContractFunction("changeRequirement(uint256)", u256(3)) == encodeArgs());
+ // check that balance is and stays zero at destination address
+ h256 opHash("6244b4fa93f73e09db0ae52750095ca0364a76b72bc01723c97011fcb876cc9e");
+ BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0);
+ m_sender = Address(0x12);
+ BOOST_REQUIRE(callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00) == encodeArgs(opHash));
+ BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0);
+ m_sender = Address(0x13);
+ BOOST_REQUIRE(callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00) == encodeArgs(opHash));
+ BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0);
+ m_sender = Address(0x14);
+ BOOST_REQUIRE(callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00) == encodeArgs(opHash));
+ // now it should go through
+ BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 100);
+}
+
+BOOST_AUTO_TEST_CASE(revoke_addOwner)
+{
+ deployWallet();
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12)) == encodeArgs());
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x13)) == encodeArgs());
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x14)) == encodeArgs());
+ // 4 owners, set required to 3
+ BOOST_REQUIRE(callContractFunction("changeRequirement(uint256)", u256(3)) == encodeArgs());
+ // add a new owner
+ Address deployer = m_sender;
+ h256 opHash = sha3(FixedHash<4>(dev::sha3("addOwner(address)")).asBytes() + h256(0x33).asBytes());
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x33)) == encodeArgs());
+ BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x33)) == encodeArgs(false));
+ m_sender = Address(0x12);
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x33)) == encodeArgs());
+ BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x33)) == encodeArgs(false));
+ // revoke one confirmation
+ m_sender = deployer;
+ BOOST_REQUIRE(callContractFunction("revoke(bytes32)", opHash) == encodeArgs());
+ m_sender = Address(0x13);
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x33)) == encodeArgs());
+ BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x33)) == encodeArgs(false));
+ m_sender = Address(0x14);
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x33)) == encodeArgs());
+ BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x33)) == encodeArgs(true));
+}
+
+BOOST_AUTO_TEST_CASE(revoke_transaction)
+{
+ deployWallet(200);
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12)) == encodeArgs());
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x13)) == encodeArgs());
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x14)) == encodeArgs());
+ // 4 owners, set required to 3
+ BOOST_REQUIRE(callContractFunction("changeRequirement(uint256)", u256(3)) == encodeArgs());
+ // create a transaction
+ Address deployer = m_sender;
+ h256 opHash("6244b4fa93f73e09db0ae52750095ca0364a76b72bc01723c97011fcb876cc9e");
+ BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0);
+ m_sender = Address(0x12);
+ BOOST_REQUIRE(callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00) == encodeArgs(opHash));
+ BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0);
+ m_sender = Address(0x13);
+ BOOST_REQUIRE(callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00) == encodeArgs(opHash));
+ BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0);
+ m_sender = Address(0x12);
+ BOOST_REQUIRE(callContractFunction("revoke(bytes32)", opHash) == encodeArgs());
+ m_sender = deployer;
+ BOOST_REQUIRE(callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00) == encodeArgs(opHash));
+ BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0);
+ m_sender = Address(0x14);
+ BOOST_REQUIRE(callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00) == encodeArgs(opHash));
+ // now it should go through
+ BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 100);
+}
+
+BOOST_AUTO_TEST_CASE(daylimit)
+{
+ deployWallet(200);
+ BOOST_REQUIRE(callContractFunction("m_dailyLimit()") == encodeArgs(u256(0)));
+ BOOST_REQUIRE(callContractFunction("setDailyLimit(uint256)", h256(100)) == encodeArgs());
+ BOOST_REQUIRE(callContractFunction("m_dailyLimit()") == encodeArgs(u256(100)));
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12)) == encodeArgs());
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x13)) == encodeArgs());
+ BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x14)) == encodeArgs());
+ // 4 owners, set required to 3
+ BOOST_REQUIRE(callContractFunction("changeRequirement(uint256)", u256(3)) == encodeArgs());
+
+ // try to send tx over daylimit
+ BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0);
+ m_sender = Address(0x12);
+ BOOST_REQUIRE(
+ callContractFunction("execute(address,uint256,bytes)", h256(0x05), 150, 0x60, 0x00) !=
+ encodeArgs(u256(0))
+ );
+ BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0);
+ // try to send tx under daylimit by stranger
+ m_sender = Address(0x77);
+ BOOST_REQUIRE(
+ callContractFunction("execute(address,uint256,bytes)", h256(0x05), 90, 0x60, 0x00) ==
+ encodeArgs(u256(0))
+ );
+ BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0);
+ // now send below limit by owner
+ m_sender = Address(0x12);
+ BOOST_REQUIRE(
+ callContractFunction("execute(address,uint256,bytes)", h256(0x05), 90, 0x60, 0x00) ==
+ encodeArgs(u256(0))
+ );
+ BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 90);
+}
+
+BOOST_AUTO_TEST_CASE(daylimit_constructor)
+{
+ deployWallet(200, {}, 1, 20);
+ BOOST_REQUIRE(callContractFunction("m_dailyLimit()") == encodeArgs(u256(20)));
+ BOOST_REQUIRE(callContractFunction("setDailyLimit(uint256)", h256(30)) == encodeArgs());
+ BOOST_REQUIRE(callContractFunction("m_dailyLimit()") == encodeArgs(u256(30)));
+}
+
+//@todo test data calls
+
+BOOST_AUTO_TEST_SUITE_END()
+
+}
+}
+} // end namespaces
diff --git a/test/libsolidity/Assembly.cpp b/test/libsolidity/Assembly.cpp
new file mode 100644
index 00000000..8d316a97
--- /dev/null
+++ b/test/libsolidity/Assembly.cpp
@@ -0,0 +1,120 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum 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.
+
+ cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * @author Lefteris Karapetsas <lefteris@ethdev.com>
+ * @date 2015
+ * Unit tests for Assembly Items from evmasm/Assembly.h
+ */
+
+#include <string>
+#include <iostream>
+#include <boost/test/unit_test.hpp>
+#include <libdevcore/Log.h>
+#include <libevmasm/SourceLocation.h>
+#include <libevmasm/Assembly.h>
+#include <libsolidity/Scanner.h>
+#include <libsolidity/Parser.h>
+#include <libsolidity/NameAndTypeResolver.h>
+#include <libsolidity/Compiler.h>
+#include <libsolidity/AST.h>
+
+using namespace std;
+using namespace dev::eth;
+
+namespace dev
+{
+namespace solidity
+{
+namespace test
+{
+
+namespace
+{
+
+eth::AssemblyItems compileContract(const string& _sourceCode)
+{
+ Parser parser;
+ ASTPointer<SourceUnit> sourceUnit;
+ BOOST_REQUIRE_NO_THROW(sourceUnit = parser.parse(make_shared<Scanner>(CharStream(_sourceCode))));
+ NameAndTypeResolver resolver({});
+ resolver.registerDeclarations(*sourceUnit);
+ for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
+ if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
+ {
+ BOOST_REQUIRE_NO_THROW(resolver.resolveNamesAndTypes(*contract));
+ }
+ for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
+ if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
+ {
+ BOOST_REQUIRE_NO_THROW(resolver.checkTypeRequirements(*contract));
+ }
+ for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
+ if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
+ {
+ Compiler compiler;
+ compiler.compileContract(*contract, map<ContractDefinition const*, bytes const*>{});
+
+ return compiler.getRuntimeAssemblyItems();
+ }
+ BOOST_FAIL("No contract found in source.");
+ return AssemblyItems();
+}
+
+void checkAssemblyLocations(AssemblyItems const& _items, vector<SourceLocation> const& _locations)
+{
+ BOOST_CHECK_EQUAL(_items.size(), _locations.size());
+ for (size_t i = 0; i < min(_items.size(), _locations.size()); ++i)
+ {
+ BOOST_CHECK_MESSAGE(
+ _items[i].getLocation() == _locations[i],
+ "Location mismatch for assembly item " + to_string(i) + ". Found: " +
+ to_string(_items[i].getLocation().start) + "-" +
+ to_string(_items[i].getLocation().end) + ", expected: " +
+ to_string(_locations[i].start) + "-" +
+ to_string(_locations[i].end));
+ }
+}
+
+} // end anonymous namespace
+
+BOOST_AUTO_TEST_SUITE(Assembly)
+
+BOOST_AUTO_TEST_CASE(location_test)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f() returns (uint256 a) {
+ return 16;
+ }
+ }
+ )";
+ shared_ptr<string const> n = make_shared<string>("source");
+ AssemblyItems items = compileContract(sourceCode);
+ vector<SourceLocation> locations =
+ vector<SourceLocation>(17, SourceLocation(2, 75, n)) +
+ vector<SourceLocation>(26, SourceLocation(20, 72, n)) +
+ vector<SourceLocation>{SourceLocation(42, 51, n), SourceLocation(65, 67, n)} +
+ vector<SourceLocation>(4, SourceLocation(58, 67, n)) +
+ vector<SourceLocation>(3, SourceLocation(20, 72, n));
+ checkAssemblyLocations(items, locations);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+}
+}
+} // end namespaces
diff --git a/test/libsolidity/CMakeLists.txt b/test/libsolidity/CMakeLists.txt
new file mode 100644
index 00000000..3ceda13b
--- /dev/null
+++ b/test/libsolidity/CMakeLists.txt
@@ -0,0 +1,5 @@
+cmake_policy(SET CMP0015 NEW)
+
+aux_source_directory(. SRCS)
+
+add_sources(${SRCS})
diff --git a/test/libsolidity/GasMeter.cpp b/test/libsolidity/GasMeter.cpp
new file mode 100644
index 00000000..5f442654
--- /dev/null
+++ b/test/libsolidity/GasMeter.cpp
@@ -0,0 +1,235 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum 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.
+
+ cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * @author Christian <c@ethdev.com>
+ * @date 2015
+ * Unit tests for the gas estimator.
+ */
+
+#include <test/libsolidity/solidityExecutionFramework.h>
+#include <libevmasm/GasMeter.h>
+#include <libevmasm/KnownState.h>
+#include <libevmasm/PathGasMeter.h>
+#include <libsolidity/AST.h>
+#include <libsolidity/GasEstimator.h>
+#include <libsolidity/SourceReferenceFormatter.h>
+
+using namespace std;
+using namespace dev::eth;
+using namespace dev::solidity;
+
+namespace dev
+{
+namespace solidity
+{
+namespace test
+{
+
+class GasMeterTestFramework: public ExecutionFramework
+{
+public:
+ GasMeterTestFramework() { }
+ void compile(string const& _sourceCode)
+ {
+ m_compiler.setSource(_sourceCode);
+ ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(), "Compiling contract failed");
+
+ AssemblyItems const* items = m_compiler.getRuntimeAssemblyItems("");
+ ASTNode const& sourceUnit = m_compiler.getAST();
+ BOOST_REQUIRE(items != nullptr);
+ m_gasCosts = GasEstimator::breakToStatementLevel(
+ GasEstimator::structuralEstimation(*items, vector<ASTNode const*>({&sourceUnit})),
+ {&sourceUnit}
+ );
+ }
+
+ void testCreationTimeGas(string const& _sourceCode)
+ {
+ compileAndRun(_sourceCode);
+ auto state = make_shared<KnownState>();
+ PathGasMeter meter(*m_compiler.getAssemblyItems());
+ GasMeter::GasConsumption gas = meter.estimateMax(0, state);
+ u256 bytecodeSize(m_compiler.getRuntimeBytecode().size());
+ gas += bytecodeSize * c_createDataGas;
+ BOOST_REQUIRE(!gas.isInfinite);
+ BOOST_CHECK(gas.value == m_gasUsed);
+ }
+
+ /// Compares the gas computed by PathGasMeter for the given signature (but unknown arguments)
+ /// against the actual gas usage computed by the VM on the given set of argument variants.
+ void testRunTimeGas(string const& _sig, vector<bytes> _argumentVariants)
+ {
+ u256 gasUsed = 0;
+ FixedHash<4> hash(dev::sha3(_sig));
+ for (bytes const& arguments: _argumentVariants)
+ {
+ sendMessage(hash.asBytes() + arguments, false, 0);
+ gasUsed = max(gasUsed, m_gasUsed);
+ }
+
+ GasMeter::GasConsumption gas = GasEstimator::functionalEstimation(
+ *m_compiler.getRuntimeAssemblyItems(),
+ _sig
+ );
+ BOOST_REQUIRE(!gas.isInfinite);
+ BOOST_CHECK(gas.value == m_gasUsed);
+ }
+
+protected:
+ map<ASTNode const*, eth::GasMeter::GasConsumption> m_gasCosts;
+};
+
+BOOST_FIXTURE_TEST_SUITE(GasMeterTests, GasMeterTestFramework)
+
+BOOST_AUTO_TEST_CASE(non_overlapping_filtered_costs)
+{
+ char const* sourceCode = R"(
+ contract test {
+ bytes x;
+ function f(uint a) returns (uint b) {
+ x.length = a;
+ for (; a < 200; ++a) {
+ x[a] = 9;
+ b = a * a;
+ }
+ return f(a - 1);
+ }
+ }
+ )";
+ compile(sourceCode);
+ for (auto first = m_gasCosts.cbegin(); first != m_gasCosts.cend(); ++first)
+ {
+ auto second = first;
+ for (++second; second != m_gasCosts.cend(); ++second)
+ if (first->first->getLocation().intersects(second->first->getLocation()))
+ {
+ BOOST_CHECK_MESSAGE(false, "Source locations should not overlap!");
+ SourceReferenceFormatter::printSourceLocation(cout, first->first->getLocation(), m_compiler.getScanner());
+ SourceReferenceFormatter::printSourceLocation(cout, second->first->getLocation(), m_compiler.getScanner());
+ }
+ }
+}
+
+BOOST_AUTO_TEST_CASE(simple_contract)
+{
+ // Tests a simple "deploy contract" code without constructor. The actual contract is not relevant.
+ char const* sourceCode = R"(
+ contract test {
+ bytes32 public shaValue;
+ function f(uint a) {
+ shaValue = sha3(a);
+ }
+ }
+ )";
+ testCreationTimeGas(sourceCode);
+}
+
+BOOST_AUTO_TEST_CASE(store_sha3)
+{
+ char const* sourceCode = R"(
+ contract test {
+ bytes32 public shaValue;
+ function test(uint a) {
+ shaValue = sha3(a);
+ }
+ }
+ )";
+ testCreationTimeGas(sourceCode);
+}
+
+BOOST_AUTO_TEST_CASE(updating_store)
+{
+ char const* sourceCode = R"(
+ contract test {
+ uint data;
+ uint data2;
+ function test() {
+ data = 1;
+ data = 2;
+ data2 = 0;
+ }
+ }
+ )";
+ testCreationTimeGas(sourceCode);
+}
+
+BOOST_AUTO_TEST_CASE(branches)
+{
+ char const* sourceCode = R"(
+ contract test {
+ uint data;
+ uint data2;
+ function f(uint x) {
+ if (x > 7)
+ data2 = 1;
+ else
+ data = 1;
+ }
+ }
+ )";
+ testCreationTimeGas(sourceCode);
+ testRunTimeGas("f(uint256)", vector<bytes>{encodeArgs(2), encodeArgs(8)});
+}
+
+BOOST_AUTO_TEST_CASE(function_calls)
+{
+ char const* sourceCode = R"(
+ contract test {
+ uint data;
+ uint data2;
+ function f(uint x) {
+ if (x > 7)
+ data2 = g(x**8) + 1;
+ else
+ data = 1;
+ }
+ function g(uint x) internal returns (uint) {
+ return data2;
+ }
+ }
+ )";
+ testCreationTimeGas(sourceCode);
+ testRunTimeGas("f(uint256)", vector<bytes>{encodeArgs(2), encodeArgs(8)});
+}
+
+BOOST_AUTO_TEST_CASE(multiple_external_functions)
+{
+ char const* sourceCode = R"(
+ contract test {
+ uint data;
+ uint data2;
+ function f(uint x) {
+ if (x > 7)
+ data2 = g(x**8) + 1;
+ else
+ data = 1;
+ }
+ function g(uint x) returns (uint) {
+ return data2;
+ }
+ }
+ )";
+ testCreationTimeGas(sourceCode);
+ testRunTimeGas("f(uint256)", vector<bytes>{encodeArgs(2), encodeArgs(8)});
+ testRunTimeGas("g(uint256)", vector<bytes>{encodeArgs(2)});
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+}
+}
+}
diff --git a/test/libsolidity/SolidityABIJSON.cpp b/test/libsolidity/SolidityABIJSON.cpp
new file mode 100644
index 00000000..f7390dc9
--- /dev/null
+++ b/test/libsolidity/SolidityABIJSON.cpp
@@ -0,0 +1,602 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum 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.
+
+ cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+ */
+/**
+ * @author Marek Kotewicz <marek@ethdev.com>
+ * @date 2014
+ * Unit tests for the solidity compiler JSON Interface output.
+ */
+
+#include "../TestHelper.h"
+#include <libsolidity/CompilerStack.h>
+#include <json/json.h>
+#include <libdevcore/Exceptions.h>
+
+namespace dev
+{
+namespace solidity
+{
+namespace test
+{
+
+class JSONInterfaceChecker
+{
+public:
+ JSONInterfaceChecker(): m_compilerStack(false) {}
+
+ void checkInterface(std::string const& _code, std::string const& _expectedInterfaceString)
+ {
+ ETH_TEST_REQUIRE_NO_THROW(m_compilerStack.parse(_code), "Parsing contract failed");
+ std::string generatedInterfaceString = m_compilerStack.getMetadata("", DocumentationType::ABIInterface);
+ Json::Value generatedInterface;
+ m_reader.parse(generatedInterfaceString, generatedInterface);
+ Json::Value expectedInterface;
+ m_reader.parse(_expectedInterfaceString, expectedInterface);
+ BOOST_CHECK_MESSAGE(expectedInterface == generatedInterface,
+ "Expected:\n" << expectedInterface.toStyledString() <<
+ "\n but got:\n" << generatedInterface.toStyledString());
+ }
+
+private:
+ CompilerStack m_compilerStack;
+ Json::Reader m_reader;
+};
+
+BOOST_FIXTURE_TEST_SUITE(SolidityABIJSON, JSONInterfaceChecker)
+
+BOOST_AUTO_TEST_CASE(basic_test)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f(uint a) returns(uint d) { return a * 7; }\n"
+ "}\n";
+
+ char const* interface = R"([
+ {
+ "name": "f",
+ "constant": false,
+ "type": "function",
+ "inputs": [
+ {
+ "name": "a",
+ "type": "uint256"
+ }
+ ],
+ "outputs": [
+ {
+ "name": "d",
+ "type": "uint256"
+ }
+ ]
+ }
+ ])";
+
+ checkInterface(sourceCode, interface);
+}
+
+BOOST_AUTO_TEST_CASE(empty_contract)
+{
+ char const* sourceCode = "contract test {\n"
+ "}\n";
+ char const* interface = "[]";
+
+ checkInterface(sourceCode, interface);
+}
+
+BOOST_AUTO_TEST_CASE(multiple_methods)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f(uint a) returns(uint d) { return a * 7; }\n"
+ " function g(uint b) returns(uint e) { return b * 8; }\n"
+ "}\n";
+
+ char const* interface = R"([
+ {
+ "name": "f",
+ "constant": false,
+ "type": "function",
+ "inputs": [
+ {
+ "name": "a",
+ "type": "uint256"
+ }
+ ],
+ "outputs": [
+ {
+ "name": "d",
+ "type": "uint256"
+ }
+ ]
+ },
+ {
+ "name": "g",
+ "constant": false,
+ "type": "function",
+ "inputs": [
+ {
+ "name": "b",
+ "type": "uint256"
+ }
+ ],
+ "outputs": [
+ {
+ "name": "e",
+ "type": "uint256"
+ }
+ ]
+ }
+ ])";
+
+ checkInterface(sourceCode, interface);
+}
+
+BOOST_AUTO_TEST_CASE(multiple_params)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f(uint a, uint b) returns(uint d) { return a + b; }\n"
+ "}\n";
+
+ char const* interface = R"([
+ {
+ "name": "f",
+ "constant": false,
+ "type": "function",
+ "inputs": [
+ {
+ "name": "a",
+ "type": "uint256"
+ },
+ {
+ "name": "b",
+ "type": "uint256"
+ }
+ ],
+ "outputs": [
+ {
+ "name": "d",
+ "type": "uint256"
+ }
+ ]
+ }
+ ])";
+
+ checkInterface(sourceCode, interface);
+}
+
+BOOST_AUTO_TEST_CASE(multiple_methods_order)
+{
+ // methods are expected to be in alpabetical order
+ char const* sourceCode = "contract test {\n"
+ " function f(uint a) returns(uint d) { return a * 7; }\n"
+ " function c(uint b) returns(uint e) { return b * 8; }\n"
+ "}\n";
+
+ char const* interface = R"([
+ {
+ "name": "c",
+ "constant": false,
+ "type": "function",
+ "inputs": [
+ {
+ "name": "b",
+ "type": "uint256"
+ }
+ ],
+ "outputs": [
+ {
+ "name": "e",
+ "type": "uint256"
+ }
+ ]
+ },
+ {
+ "name": "f",
+ "constant": false,
+ "type": "function",
+ "inputs": [
+ {
+ "name": "a",
+ "type": "uint256"
+ }
+ ],
+ "outputs": [
+ {
+ "name": "d",
+ "type": "uint256"
+ }
+ ]
+ }
+ ])";
+
+ checkInterface(sourceCode, interface);
+}
+
+BOOST_AUTO_TEST_CASE(const_function)
+{
+ char const* sourceCode = "contract test {\n"
+ " function foo(uint a, uint b) returns(uint d) { return a + b; }\n"
+ " function boo(uint32 a) constant returns(uint b) { return a * 4; }\n"
+ "}\n";
+
+ char const* interface = R"([
+ {
+ "name": "foo",
+ "constant": false,
+ "type": "function",
+ "inputs": [
+ {
+ "name": "a",
+ "type": "uint256"
+ },
+ {
+ "name": "b",
+ "type": "uint256"
+ }
+ ],
+ "outputs": [
+ {
+ "name": "d",
+ "type": "uint256"
+ }
+ ]
+ },
+ {
+ "name": "boo",
+ "constant": true,
+ "type": "function",
+ "inputs": [{
+ "name": "a",
+ "type": "uint32"
+ }],
+ "outputs": [
+ {
+ "name": "b",
+ "type": "uint256"
+ }
+ ]
+ }
+ ])";
+
+ checkInterface(sourceCode, interface);
+}
+
+BOOST_AUTO_TEST_CASE(exclude_fallback_function)
+{
+ char const* sourceCode = "contract test { function() {} }";
+
+ char const* interface = "[]";
+
+ checkInterface(sourceCode, interface);
+}
+
+BOOST_AUTO_TEST_CASE(events)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f(uint a) returns(uint d) { return a * 7; }\n"
+ " event e1(uint b, address indexed c); \n"
+ " event e2(); \n"
+ "}\n";
+ char const* interface = R"([
+ {
+ "name": "f",
+ "constant": false,
+ "type": "function",
+ "inputs": [
+ {
+ "name": "a",
+ "type": "uint256"
+ }
+ ],
+ "outputs": [
+ {
+ "name": "d",
+ "type": "uint256"
+ }
+ ]
+ },
+ {
+ "name": "e1",
+ "type": "event",
+ "anonymous": false,
+ "inputs": [
+ {
+ "indexed": false,
+ "name": "b",
+ "type": "uint256"
+ },
+ {
+ "indexed": true,
+ "name": "c",
+ "type": "address"
+ }
+ ]
+ },
+ {
+ "name": "e2",
+ "type": "event",
+ "anonymous": false,
+ "inputs": []
+ }
+
+ ])";
+
+ checkInterface(sourceCode, interface);
+}
+
+BOOST_AUTO_TEST_CASE(events_anonymous)
+{
+ char const* sourceCode = "contract test {\n"
+ " event e() anonymous; \n"
+ "}\n";
+ char const* interface = R"([
+ {
+ "name": "e",
+ "type": "event",
+ "anonymous": true,
+ "inputs": []
+ }
+
+ ])";
+
+ checkInterface(sourceCode, interface);
+}
+
+BOOST_AUTO_TEST_CASE(inherited)
+{
+ char const* sourceCode =
+ " contract Base { \n"
+ " function baseFunction(uint p) returns (uint i) { return p; } \n"
+ " event baseEvent(bytes32 indexed evtArgBase); \n"
+ " } \n"
+ " contract Derived is Base { \n"
+ " function derivedFunction(bytes32 p) returns (bytes32 i) { return p; } \n"
+ " event derivedEvent(uint indexed evtArgDerived); \n"
+ " }";
+
+ char const* interface = R"([
+ {
+ "name": "baseFunction",
+ "constant": false,
+ "type": "function",
+ "inputs":
+ [{
+ "name": "p",
+ "type": "uint256"
+ }],
+ "outputs":
+ [{
+ "name": "i",
+ "type": "uint256"
+ }]
+ },
+ {
+ "name": "derivedFunction",
+ "constant": false,
+ "type": "function",
+ "inputs":
+ [{
+ "name": "p",
+ "type": "bytes32"
+ }],
+ "outputs":
+ [{
+ "name": "i",
+ "type": "bytes32"
+ }]
+ },
+ {
+ "name": "derivedEvent",
+ "type": "event",
+ "anonymous": false,
+ "inputs":
+ [{
+ "indexed": true,
+ "name": "evtArgDerived",
+ "type": "uint256"
+ }]
+ },
+ {
+ "name": "baseEvent",
+ "type": "event",
+ "anonymous": false,
+ "inputs":
+ [{
+ "indexed": true,
+ "name": "evtArgBase",
+ "type": "bytes32"
+ }]
+ }])";
+
+
+ checkInterface(sourceCode, interface);
+}
+BOOST_AUTO_TEST_CASE(empty_name_input_parameter_with_named_one)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f(uint, uint k) returns(uint ret_k, uint ret_g){
+ uint g = 8;
+ ret_k = k;
+ ret_g = g;
+ }
+ })";
+
+ char const* interface = R"([
+ {
+ "name": "f",
+ "constant": false,
+ "type": "function",
+ "inputs": [
+ {
+ "name": "",
+ "type": "uint256"
+ },
+ {
+ "name": "k",
+ "type": "uint256"
+ }
+ ],
+ "outputs": [
+ {
+ "name": "ret_k",
+ "type": "uint256"
+ },
+ {
+ "name": "ret_g",
+ "type": "uint256"
+ }
+ ]
+ }
+ ])";
+
+ checkInterface(sourceCode, interface);
+}
+
+BOOST_AUTO_TEST_CASE(empty_name_return_parameter)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f(uint k) returns(uint){
+ return k;
+ }
+ })";
+
+ char const* interface = R"([
+ {
+ "name": "f",
+ "constant": false,
+ "type": "function",
+ "inputs": [
+ {
+ "name": "k",
+ "type": "uint256"
+ }
+ ],
+ "outputs": [
+ {
+ "name": "",
+ "type": "uint256"
+ }
+ ]
+ }
+ ])";
+ checkInterface(sourceCode, interface);
+}
+
+BOOST_AUTO_TEST_CASE(constructor_abi)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function test(uint param1, test param2, bool param3) {}
+ }
+ )";
+
+ char const* interface = R"([
+ {
+ "inputs": [
+ {
+ "name": "param1",
+ "type": "uint256"
+ },
+ {
+ "name": "param2",
+ "type": "address"
+ },
+ {
+ "name": "param3",
+ "type": "bool"
+ }
+ ],
+ "type": "constructor"
+ }
+ ])";
+ checkInterface(sourceCode, interface);
+}
+
+
+BOOST_AUTO_TEST_CASE(return_param_in_abi)
+{
+ // bug #1801
+ char const* sourceCode = R"(
+ contract test {
+ enum ActionChoices { GoLeft, GoRight, GoStraight, Sit }
+ function test(ActionChoices param) {}
+ function ret() returns(ActionChoices){
+ ActionChoices action = ActionChoices.GoLeft;
+ return action;
+ }
+ }
+ )";
+
+ char const* interface = R"(
+ [
+ {
+ "constant" : false,
+ "inputs" : [],
+ "name" : "ret",
+ "outputs" : [
+ {
+ "name" : "",
+ "type" : "uint8"
+ }
+ ],
+ "type" : "function"
+ },
+ {
+ "inputs": [
+ {
+ "name": "param",
+ "type": "uint8"
+ }
+ ],
+ "type": "constructor"
+ }
+ ]
+ )";
+ checkInterface(sourceCode, interface);
+}
+
+BOOST_AUTO_TEST_CASE(strings_and_arrays)
+{
+ // bug #1801
+ char const* sourceCode = R"(
+ contract test {
+ function f(string a, bytes b, uint[] c) external {}
+ }
+ )";
+
+ char const* interface = R"(
+ [
+ {
+ "constant" : false,
+ "name": "f",
+ "inputs": [
+ { "name": "a", "type": "string" },
+ { "name": "b", "type": "bytes" },
+ { "name": "c", "type": "uint256[]" }
+ ],
+ "outputs": [],
+ "type" : "function"
+ }
+ ]
+ )";
+ checkInterface(sourceCode, interface);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+}
+}
+}
diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp
new file mode 100644
index 00000000..ae2fc6dc
--- /dev/null
+++ b/test/libsolidity/SolidityEndToEndTest.cpp
@@ -0,0 +1,5168 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum 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.
+
+ cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * @author Christian <c@ethdev.com>
+ * @author Gav Wood <g@ethdev.com>
+ * @date 2014
+ * Unit tests for the solidity expression compiler, testing the behaviour of the code.
+ */
+
+#include <string>
+#include <tuple>
+#include <boost/test/unit_test.hpp>
+#include <libdevcore/Hash.h>
+#include <libsolidity/Exceptions.h>
+#include <test/libsolidity/solidityExecutionFramework.h>
+
+using namespace std;
+
+namespace dev
+{
+namespace solidity
+{
+namespace test
+{
+
+BOOST_FIXTURE_TEST_SUITE(SolidityEndToEndTest, ExecutionFramework)
+
+BOOST_AUTO_TEST_CASE(smoke_test)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f(uint a) returns(uint d) { return a * 7; }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ testSolidityAgainstCppOnRange("f(uint256)", [](u256 const& a) -> u256 { return a * 7; }, 0, 100);
+}
+
+BOOST_AUTO_TEST_CASE(empty_contract)
+{
+ char const* sourceCode = "contract test {\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("i_am_not_there()", bytes()).empty());
+}
+
+BOOST_AUTO_TEST_CASE(exp_operator)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f(uint a) returns(uint d) { return 2 ** a; }
+ })";
+ compileAndRun(sourceCode);
+ testSolidityAgainstCppOnRange("f(uint256)", [](u256 const& a) -> u256 { return u256(1 << a.convert_to<int>()); }, 0, 16);
+}
+
+BOOST_AUTO_TEST_CASE(exp_operator_const)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f() returns(uint d) { return 2 ** 3; }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("f()", bytes()) == toBigEndian(u256(8)));
+}
+
+BOOST_AUTO_TEST_CASE(exp_operator_const_signed)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f() returns(int d) { return (-2) ** 3; }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("f()", bytes()) == toBigEndian(u256(-8)));
+}
+
+BOOST_AUTO_TEST_CASE(recursive_calls)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f(uint n) returns(uint nfac) {\n"
+ " if (n <= 1) return 1;\n"
+ " else return n * f(n - 1);\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ function<u256(u256)> recursive_calls_cpp = [&recursive_calls_cpp](u256 const& n) -> u256
+ {
+ if (n <= 1)
+ return 1;
+ else
+ return n * recursive_calls_cpp(n - 1);
+ };
+
+ testSolidityAgainstCppOnRange("f(uint256)", recursive_calls_cpp, 0, 5);
+}
+
+BOOST_AUTO_TEST_CASE(multiple_functions)
+{
+ char const* sourceCode = "contract test {\n"
+ " function a() returns(uint n) { return 0; }\n"
+ " function b() returns(uint n) { return 1; }\n"
+ " function c() returns(uint n) { return 2; }\n"
+ " function f() returns(uint n) { return 3; }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("a()", bytes()) == toBigEndian(u256(0)));
+ BOOST_CHECK(callContractFunction("b()", bytes()) == toBigEndian(u256(1)));
+ BOOST_CHECK(callContractFunction("c()", bytes()) == toBigEndian(u256(2)));
+ BOOST_CHECK(callContractFunction("f()", bytes()) == toBigEndian(u256(3)));
+ BOOST_CHECK(callContractFunction("i_am_not_there()", bytes()) == bytes());
+}
+
+BOOST_AUTO_TEST_CASE(named_args)
+{
+ char const* sourceCode = "contract test {\n"
+ " function a(uint a, uint b, uint c) returns (uint r) { r = a * 100 + b * 10 + c * 1; }\n"
+ " function b() returns (uint r) { r = a({a: 1, b: 2, c: 3}); }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("b()", bytes()) == toBigEndian(u256(123)));
+}
+
+BOOST_AUTO_TEST_CASE(disorder_named_args)
+{
+ char const* sourceCode = "contract test {\n"
+ " function a(uint a, uint b, uint c) returns (uint r) { r = a * 100 + b * 10 + c * 1; }\n"
+ " function b() returns (uint r) { r = a({c: 3, a: 1, b: 2}); }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("b()", bytes()) == toBigEndian(u256(123)));
+}
+
+BOOST_AUTO_TEST_CASE(while_loop)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f(uint n) returns(uint nfac) {\n"
+ " nfac = 1;\n"
+ " var i = 2;\n"
+ " while (i <= n) nfac *= i++;\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+
+ auto while_loop_cpp = [](u256 const& n) -> u256
+ {
+ u256 nfac = 1;
+ u256 i = 2;
+ while (i <= n)
+ nfac *= i++;
+
+ return nfac;
+ };
+
+ testSolidityAgainstCppOnRange("f(uint256)", while_loop_cpp, 0, 5);
+}
+
+BOOST_AUTO_TEST_CASE(break_outside_loop)
+{
+ // break and continue outside loops should be simply ignored
+ char const* sourceCode = "contract test {\n"
+ " function f(uint x) returns(uint y) {\n"
+ " break; continue; return 2;\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ testSolidityAgainstCpp("f(uint256)", [](u256 const&) -> u256 { return 2; }, u256(0));
+}
+
+BOOST_AUTO_TEST_CASE(nested_loops)
+{
+ // tests that break and continue statements in nested loops jump to the correct place
+ char const* sourceCode = "contract test {\n"
+ " function f(uint x) returns(uint y) {\n"
+ " while (x > 1) {\n"
+ " if (x == 10) break;\n"
+ " while (x > 5) {\n"
+ " if (x == 8) break;\n"
+ " x--;\n"
+ " if (x == 6) continue;\n"
+ " return x;\n"
+ " }\n"
+ " x--;\n"
+ " if (x == 3) continue;\n"
+ " break;\n"
+ " }\n"
+ " return x;\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+
+ auto nested_loops_cpp = [](u256 n) -> u256
+ {
+ while (n > 1)
+ {
+ if (n == 10)
+ break;
+ while (n > 5)
+ {
+ if (n == 8)
+ break;
+ n--;
+ if (n == 6)
+ continue;
+ return n;
+ }
+ n--;
+ if (n == 3)
+ continue;
+ break;
+ }
+
+ return n;
+ };
+
+ testSolidityAgainstCppOnRange("f(uint256)", nested_loops_cpp, 0, 12);
+}
+
+BOOST_AUTO_TEST_CASE(for_loop)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f(uint n) returns(uint nfac) {\n"
+ " nfac = 1;\n"
+ " for (var i = 2; i <= n; i++)\n"
+ " nfac *= i;\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+
+ auto for_loop_cpp = [](u256 const& n) -> u256
+ {
+ u256 nfac = 1;
+ for (auto i = 2; i <= n; i++)
+ nfac *= i;
+ return nfac;
+ };
+
+ testSolidityAgainstCppOnRange("f(uint256)", for_loop_cpp, 0, 5);
+}
+
+BOOST_AUTO_TEST_CASE(for_loop_empty)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f() returns(uint ret) {\n"
+ " ret = 1;\n"
+ " for (;;)\n"
+ " {\n"
+ " ret += 1;\n"
+ " if (ret >= 10) break;\n"
+ " }\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+
+ auto for_loop_empty_cpp = []() -> u256
+ {
+ u256 ret = 1;
+ for (;;)
+ {
+ ret += 1;
+ if (ret >= 10) break;
+ }
+ return ret;
+ };
+
+ testSolidityAgainstCpp("f()", for_loop_empty_cpp);
+}
+
+BOOST_AUTO_TEST_CASE(for_loop_simple_init_expr)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f(uint n) returns(uint nfac) {\n"
+ " nfac = 1;\n"
+ " uint256 i;\n"
+ " for (i = 2; i <= n; i++)\n"
+ " nfac *= i;\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+
+ auto for_loop_simple_init_expr_cpp = [](u256 const& n) -> u256
+ {
+ u256 nfac = 1;
+ u256 i;
+ for (i = 2; i <= n; i++)
+ nfac *= i;
+ return nfac;
+ };
+
+ testSolidityAgainstCppOnRange("f(uint256)", for_loop_simple_init_expr_cpp, 0, 5);
+}
+
+BOOST_AUTO_TEST_CASE(for_loop_break_continue)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f(uint n) returns (uint r)
+ {
+ uint i = 1;
+ uint k = 0;
+ for (i *= 5; k < n; i *= 7)
+ {
+ k++;
+ i += 4;
+ if (n % 3 == 0)
+ break;
+ i += 9;
+ if (n % 2 == 0)
+ continue;
+ i += 19;
+ }
+ return i;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+
+ auto breakContinue = [](u256 const& n) -> u256
+ {
+ u256 i = 1;
+ u256 k = 0;
+ for (i *= 5; k < n; i *= 7)
+ {
+ k++;
+ i += 4;
+ if (n % 3 == 0)
+ break;
+ i += 9;
+ if (n % 2 == 0)
+ continue;
+ i += 19;
+ }
+ return i;
+ };
+
+ testSolidityAgainstCppOnRange("f(uint256)", breakContinue, 0, 10);
+}
+
+BOOST_AUTO_TEST_CASE(calling_other_functions)
+{
+ char const* sourceCode = "contract collatz {\n"
+ " function run(uint x) returns(uint y) {\n"
+ " while ((y = x) > 1) {\n"
+ " if (x % 2 == 0) x = evenStep(x);\n"
+ " else x = oddStep(x);\n"
+ " }\n"
+ " }\n"
+ " function evenStep(uint x) returns(uint y) {\n"
+ " return x / 2;\n"
+ " }\n"
+ " function oddStep(uint x) returns(uint y) {\n"
+ " return 3 * x + 1;\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+
+ auto evenStep_cpp = [](u256 const& n) -> u256
+ {
+ return n / 2;
+ };
+
+ auto oddStep_cpp = [](u256 const& n) -> u256
+ {
+ return 3 * n + 1;
+ };
+
+ auto collatz_cpp = [&evenStep_cpp, &oddStep_cpp](u256 n) -> u256
+ {
+ u256 y;
+ while ((y = n) > 1)
+ {
+ if (n % 2 == 0)
+ n = evenStep_cpp(n);
+ else
+ n = oddStep_cpp(n);
+ }
+ return y;
+ };
+
+ testSolidityAgainstCpp("run(uint256)", collatz_cpp, u256(0));
+ testSolidityAgainstCpp("run(uint256)", collatz_cpp, u256(1));
+ testSolidityAgainstCpp("run(uint256)", collatz_cpp, u256(2));
+ testSolidityAgainstCpp("run(uint256)", collatz_cpp, u256(8));
+ testSolidityAgainstCpp("run(uint256)", collatz_cpp, u256(127));
+}
+
+BOOST_AUTO_TEST_CASE(many_local_variables)
+{
+ char const* sourceCode = "contract test {\n"
+ " function run(uint x1, uint x2, uint x3) returns(uint y) {\n"
+ " var a = 0x1; var b = 0x10; var c = 0x100;\n"
+ " y = a + b + c + x1 + x2 + x3;\n"
+ " y += b + x2;\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ auto f = [](u256 const& x1, u256 const& x2, u256 const& x3) -> u256
+ {
+ u256 a = 0x1;
+ u256 b = 0x10;
+ u256 c = 0x100;
+ u256 y = a + b + c + x1 + x2 + x3;
+ return y + b + x2;
+ };
+ testSolidityAgainstCpp("run(uint256,uint256,uint256)", f, u256(0x1000), u256(0x10000), u256(0x100000));
+}
+
+BOOST_AUTO_TEST_CASE(packing_unpacking_types)
+{
+ char const* sourceCode = "contract test {\n"
+ " function run(bool a, uint32 b, uint64 c) returns(uint256 y) {\n"
+ " if (a) y = 1;\n"
+ " y = y * 0x100000000 | ~b;\n"
+ " y = y * 0x10000000000000000 | ~c;\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("run(bool,uint32,uint64)", true, fromHex("0f0f0f0f"), fromHex("f0f0f0f0f0f0f0f0"))
+ == fromHex("00000000000000000000000000000000000000""01""f0f0f0f0""0f0f0f0f0f0f0f0f"));
+}
+
+BOOST_AUTO_TEST_CASE(packing_signed_types)
+{
+ char const* sourceCode = "contract test {\n"
+ " function run() returns(int8 y) {\n"
+ " uint8 x = 0xfa;\n"
+ " return int8(x);\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("run()")
+ == fromHex("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa"));
+}
+
+BOOST_AUTO_TEST_CASE(multiple_return_values)
+{
+ char const* sourceCode = "contract test {\n"
+ " function run(bool x1, uint x2) returns(uint y1, bool y2, uint y3) {\n"
+ " y1 = x2; y2 = x1;\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("run(bool,uint256)", true, 0xcd) == encodeArgs(0xcd, true, 0));
+}
+
+BOOST_AUTO_TEST_CASE(short_circuiting)
+{
+ char const* sourceCode = "contract test {\n"
+ " function run(uint x) returns(uint y) {\n"
+ " x == 0 || ((x = 8) > 0);\n"
+ " return x;"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+
+ auto short_circuiting_cpp = [](u256 n) -> u256
+ {
+ (void)(n == 0 || (n = 8) > 0);
+ return n;
+ };
+
+ testSolidityAgainstCppOnRange("run(uint256)", short_circuiting_cpp, 0, 2);
+}
+
+BOOST_AUTO_TEST_CASE(high_bits_cleaning)
+{
+ char const* sourceCode = "contract test {\n"
+ " function run() returns(uint256 y) {\n"
+ " uint32 t = uint32(0xffffffff);\n"
+ " uint32 x = t + 10;\n"
+ " if (x >= 0xffffffff) return 0;\n"
+ " return x;"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ auto high_bits_cleaning_cpp = []() -> u256
+ {
+ uint32_t t = uint32_t(0xffffffff);
+ uint32_t x = t + 10;
+ if (x >= 0xffffffff)
+ return 0;
+ return x;
+ };
+ testSolidityAgainstCpp("run()", high_bits_cleaning_cpp);
+}
+
+BOOST_AUTO_TEST_CASE(sign_extension)
+{
+ char const* sourceCode = "contract test {\n"
+ " function run() returns(uint256 y) {\n"
+ " int64 x = -int32(0xff);\n"
+ " if (x >= 0xff) return 0;\n"
+ " return -uint256(x);"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ auto sign_extension_cpp = []() -> u256
+ {
+ int64_t x = -int32_t(0xff);
+ if (x >= 0xff)
+ return 0;
+ return u256(x) * -1;
+ };
+ testSolidityAgainstCpp("run()", sign_extension_cpp);
+}
+
+BOOST_AUTO_TEST_CASE(small_unsigned_types)
+{
+ char const* sourceCode = "contract test {\n"
+ " function run() returns(uint256 y) {\n"
+ " uint32 t = uint32(0xffffff);\n"
+ " uint32 x = t * 0xffffff;\n"
+ " return x / 0x100;"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ auto small_unsigned_types_cpp = []() -> u256
+ {
+ uint32_t t = uint32_t(0xffffff);
+ uint32_t x = t * 0xffffff;
+ return x / 0x100;
+ };
+ testSolidityAgainstCpp("run()", small_unsigned_types_cpp);
+}
+
+BOOST_AUTO_TEST_CASE(small_signed_types)
+{
+ char const* sourceCode = "contract test {\n"
+ " function run() returns(int256 y) {\n"
+ " return -int32(10) * -int64(20);\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ auto small_signed_types_cpp = []() -> u256
+ {
+ return -int32_t(10) * -int64_t(20);
+ };
+ testSolidityAgainstCpp("run()", small_signed_types_cpp);
+}
+
+BOOST_AUTO_TEST_CASE(strings)
+{
+ char const* sourceCode = "contract test {\n"
+ " function fixed() returns(bytes32 ret) {\n"
+ " return \"abc\\x00\\xff__\";\n"
+ " }\n"
+ " function pipeThrough(bytes2 small, bool one) returns(bytes16 large, bool oneRet) {\n"
+ " oneRet = one;\n"
+ " large = small;\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("fixed()") == encodeArgs(string("abc\0\xff__", 7)));
+ BOOST_CHECK(callContractFunction("pipeThrough(bytes2,bool)", string("\0\x02", 2), true) == encodeArgs(string("\0\x2", 2), true));
+}
+
+BOOST_AUTO_TEST_CASE(inc_dec_operators)
+{
+ char const* sourceCode = R"(
+ contract test {
+ uint8 x;
+ uint v;
+ function f() returns (uint r) {
+ uint a = 6;
+ r = a;
+ r += (a++) * 0x10;
+ r += (++a) * 0x100;
+ v = 3;
+ r += (v++) * 0x1000;
+ r += (++v) * 0x10000;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("f()") == encodeArgs(0x53866));
+}
+
+BOOST_AUTO_TEST_CASE(bytes_comparison)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f() returns (bool) {
+ bytes2 a = "a";
+ bytes2 x = "aa";
+ bytes2 b = "b";
+ return a < x && x < b;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("f()") == encodeArgs(true));
+}
+
+BOOST_AUTO_TEST_CASE(state_smoke_test)
+{
+ char const* sourceCode = "contract test {\n"
+ " uint256 value1;\n"
+ " uint256 value2;\n"
+ " function get(uint8 which) returns (uint256 value) {\n"
+ " if (which == 0) return value1;\n"
+ " else return value2;\n"
+ " }\n"
+ " function set(uint8 which, uint256 value) {\n"
+ " if (which == 0) value1 = value;\n"
+ " else value2 = value;\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("get(uint8)", byte(0x00)) == encodeArgs(0));
+ BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(0));
+ BOOST_CHECK(callContractFunction("set(uint8,uint256)", byte(0x00), 0x1234) == encodeArgs());
+ BOOST_CHECK(callContractFunction("set(uint8,uint256)", byte(0x01), 0x8765) == encodeArgs());
+ BOOST_CHECK(callContractFunction("get(uint8)", byte( 0x00)) == encodeArgs(0x1234));
+ BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(0x8765));
+ BOOST_CHECK(callContractFunction("set(uint8,uint256)", byte(0x00), 0x3) == encodeArgs());
+ BOOST_CHECK(callContractFunction("get(uint8)", byte(0x00)) == encodeArgs(0x3));
+}
+
+BOOST_AUTO_TEST_CASE(compound_assign)
+{
+ char const* sourceCode = "contract test {\n"
+ " uint value1;\n"
+ " uint value2;\n"
+ " function f(uint x, uint y) returns (uint w) {\n"
+ " uint value3 = y;"
+ " value1 += x;\n"
+ " value3 *= x;"
+ " value2 *= value3 + value1;\n"
+ " return value2 += 7;"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+
+ u256 value1;
+ u256 value2;
+ auto f = [&](u256 const& _x, u256 const& _y) -> u256
+ {
+ u256 value3 = _y;
+ value1 += _x;
+ value3 *= _x;
+ value2 *= value3 + value1;
+ return value2 += 7;
+ };
+ testSolidityAgainstCpp("f(uint256,uint256)", f, u256(0), u256(6));
+ testSolidityAgainstCpp("f(uint256,uint256)", f, u256(1), u256(3));
+ testSolidityAgainstCpp("f(uint256,uint256)", f, u256(2), u256(25));
+ testSolidityAgainstCpp("f(uint256,uint256)", f, u256(3), u256(69));
+ testSolidityAgainstCpp("f(uint256,uint256)", f, u256(4), u256(84));
+ testSolidityAgainstCpp("f(uint256,uint256)", f, u256(5), u256(2));
+ testSolidityAgainstCpp("f(uint256,uint256)", f, u256(6), u256(51));
+ testSolidityAgainstCpp("f(uint256,uint256)", f, u256(7), u256(48));
+}
+
+BOOST_AUTO_TEST_CASE(simple_mapping)
+{
+ char const* sourceCode = "contract test {\n"
+ " mapping(uint8 => uint8) table;\n"
+ " function get(uint8 k) returns (uint8 v) {\n"
+ " return table[k];\n"
+ " }\n"
+ " function set(uint8 k, uint8 v) {\n"
+ " table[k] = v;\n"
+ " }\n"
+ "}";
+ compileAndRun(sourceCode);
+
+ BOOST_CHECK(callContractFunction("get(uint8)", byte(0)) == encodeArgs(byte(0x00)));
+ BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(byte(0x00)));
+ BOOST_CHECK(callContractFunction("get(uint8)", byte(0xa7)) == encodeArgs(byte(0x00)));
+ callContractFunction("set(uint8,uint8)", byte(0x01), byte(0xa1));
+ BOOST_CHECK(callContractFunction("get(uint8)", byte(0x00)) == encodeArgs(byte(0x00)));
+ BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(byte(0xa1)));
+ BOOST_CHECK(callContractFunction("get(uint8)", byte(0xa7)) == encodeArgs(byte(0x00)));
+ callContractFunction("set(uint8,uint8)", byte(0x00), byte(0xef));
+ BOOST_CHECK(callContractFunction("get(uint8)", byte(0x00)) == encodeArgs(byte(0xef)));
+ BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(byte(0xa1)));
+ BOOST_CHECK(callContractFunction("get(uint8)", byte(0xa7)) == encodeArgs(byte(0x00)));
+ callContractFunction("set(uint8,uint8)", byte(0x01), byte(0x05));
+ BOOST_CHECK(callContractFunction("get(uint8)", byte(0x00)) == encodeArgs(byte(0xef)));
+ BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(byte(0x05)));
+ BOOST_CHECK(callContractFunction("get(uint8)", byte(0xa7)) == encodeArgs(byte(0x00)));
+}
+
+BOOST_AUTO_TEST_CASE(mapping_state)
+{
+ char const* sourceCode = "contract Ballot {\n"
+ " mapping(address => bool) canVote;\n"
+ " mapping(address => uint) voteCount;\n"
+ " mapping(address => bool) voted;\n"
+ " function getVoteCount(address addr) returns (uint retVoteCount) {\n"
+ " return voteCount[addr];\n"
+ " }\n"
+ " function grantVoteRight(address addr) {\n"
+ " canVote[addr] = true;\n"
+ " }\n"
+ " function vote(address voter, address vote) returns (bool success) {\n"
+ " if (!canVote[voter] || voted[voter]) return false;\n"
+ " voted[voter] = true;\n"
+ " voteCount[vote] = voteCount[vote] + 1;\n"
+ " return true;\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ class Ballot
+ {
+ public:
+ u256 getVoteCount(u160 _address) { return m_voteCount[_address]; }
+ void grantVoteRight(u160 _address) { m_canVote[_address] = true; }
+ bool vote(u160 _voter, u160 _vote)
+ {
+ if (!m_canVote[_voter] || m_voted[_voter]) return false;
+ m_voted[_voter] = true;
+ m_voteCount[_vote]++;
+ return true;
+ }
+ private:
+ map<u160, bool> m_canVote;
+ map<u160, u256> m_voteCount;
+ map<u160, bool> m_voted;
+ } ballot;
+
+ auto getVoteCount = bind(&Ballot::getVoteCount, &ballot, _1);
+ auto grantVoteRight = bind(&Ballot::grantVoteRight, &ballot, _1);
+ auto vote = bind(&Ballot::vote, &ballot, _1, _2);
+ testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
+ testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
+ testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
+ // voting without vote right should be rejected
+ testSolidityAgainstCpp("vote(address,address)", vote, u160(0), u160(2));
+ testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
+ testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
+ testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
+ // grant vote rights
+ testSolidityAgainstCpp("grantVoteRight(address)", grantVoteRight, u160(0));
+ testSolidityAgainstCpp("grantVoteRight(address)", grantVoteRight, u160(1));
+ // vote, should increase 2's vote count
+ testSolidityAgainstCpp("vote(address,address)", vote, u160(0), u160(2));
+ testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
+ testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
+ testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
+ // vote again, should be rejected
+ testSolidityAgainstCpp("vote(address,address)", vote, u160(0), u160(1));
+ testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
+ testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
+ testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
+ // vote without right to vote
+ testSolidityAgainstCpp("vote(address,address)", vote, u160(2), u160(1));
+ testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
+ testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
+ testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
+ // grant vote right and now vote again
+ testSolidityAgainstCpp("grantVoteRight(address)", grantVoteRight, u160(2));
+ testSolidityAgainstCpp("vote(address,address)", vote, u160(2), u160(1));
+ testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
+ testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
+ testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
+}
+
+BOOST_AUTO_TEST_CASE(mapping_state_inc_dec)
+{
+ char const* sourceCode = "contract test {\n"
+ " uint value;\n"
+ " mapping(uint => uint) table;\n"
+ " function f(uint x) returns (uint y) {\n"
+ " value = x;\n"
+ " if (x > 0) table[++value] = 8;\n"
+ " if (x > 1) value--;\n"
+ " if (x > 2) table[value]++;\n"
+ " return --table[value++];\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+
+ u256 value = 0;
+ map<u256, u256> table;
+ auto f = [&](u256 const& _x) -> u256
+ {
+ value = _x;
+ if (_x > 0)
+ table[++value] = 8;
+ if (_x > 1)
+ value --;
+ if (_x > 2)
+ table[value]++;
+ return --table[value++];
+ };
+ testSolidityAgainstCppOnRange("f(uint256)", f, 0, 5);
+}
+
+BOOST_AUTO_TEST_CASE(multi_level_mapping)
+{
+ char const* sourceCode = "contract test {\n"
+ " mapping(uint => mapping(uint => uint)) table;\n"
+ " function f(uint x, uint y, uint z) returns (uint w) {\n"
+ " if (z == 0) return table[x][y];\n"
+ " else return table[x][y] = z;\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+
+ map<u256, map<u256, u256>> table;
+ auto f = [&](u256 const& _x, u256 const& _y, u256 const& _z) -> u256
+ {
+ if (_z == 0) return table[_x][_y];
+ else return table[_x][_y] = _z;
+ };
+ testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(4), u256(5), u256(0));
+ testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(5), u256(4), u256(0));
+ testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(4), u256(5), u256(9));
+ testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(4), u256(5), u256(0));
+ testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(5), u256(4), u256(0));
+ testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(5), u256(4), u256(7));
+ testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(4), u256(5), u256(0));
+ testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(5), u256(4), u256(0));
+}
+
+BOOST_AUTO_TEST_CASE(structs)
+{
+ char const* sourceCode = "contract test {\n"
+ " struct s1 {\n"
+ " uint8 x;\n"
+ " bool y;\n"
+ " }\n"
+ " struct s2 {\n"
+ " uint32 z;\n"
+ " s1 s1data;\n"
+ " mapping(uint8 => s2) recursive;\n"
+ " }\n"
+ " s2 data;\n"
+ " function check() returns (bool ok) {\n"
+ " return data.z == 1 && data.s1data.x == 2 && \n"
+ " data.s1data.y == true && \n"
+ " data.recursive[3].recursive[4].z == 5 && \n"
+ " data.recursive[4].recursive[3].z == 6 && \n"
+ " data.recursive[0].s1data.y == false && \n"
+ " data.recursive[4].z == 9;\n"
+ " }\n"
+ " function set() {\n"
+ " data.z = 1;\n"
+ " data.s1data.x = 2;\n"
+ " data.s1data.y = true;\n"
+ " data.recursive[3].recursive[4].z = 5;\n"
+ " data.recursive[4].recursive[3].z = 6;\n"
+ " data.recursive[0].s1data.y = false;\n"
+ " data.recursive[4].z = 9;\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("check()") == encodeArgs(false));
+ BOOST_CHECK(callContractFunction("set()") == bytes());
+ BOOST_CHECK(callContractFunction("check()") == encodeArgs(true));
+}
+
+BOOST_AUTO_TEST_CASE(struct_reference)
+{
+ char const* sourceCode = "contract test {\n"
+ " struct s2 {\n"
+ " uint32 z;\n"
+ " mapping(uint8 => s2) recursive;\n"
+ " }\n"
+ " s2 data;\n"
+ " function check() returns (bool ok) {\n"
+ " return data.z == 2 && \n"
+ " data.recursive[0].z == 3 && \n"
+ " data.recursive[0].recursive[1].z == 0 && \n"
+ " data.recursive[0].recursive[0].z == 1;\n"
+ " }\n"
+ " function set() {\n"
+ " data.z = 2;\n"
+ " var map = data.recursive;\n"
+ " s2 inner = map[0];\n"
+ " inner.z = 3;\n"
+ " inner.recursive[0].z = inner.recursive[1].z + 1;\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("check()") == encodeArgs(false));
+ BOOST_CHECK(callContractFunction("set()") == bytes());
+ BOOST_CHECK(callContractFunction("check()") == encodeArgs(true));
+}
+
+BOOST_AUTO_TEST_CASE(deleteStruct)
+{
+ char const* sourceCode = R"(
+ contract test {
+ struct topStruct {
+ nestedStruct nstr;
+ emptyStruct empty;
+ uint topValue;
+ mapping (uint => uint) topMapping;
+ }
+ uint toDelete;
+ topStruct str;
+ struct nestedStruct {
+ uint nestedValue;
+ mapping (uint => bool) nestedMapping;
+ }
+ struct emptyStruct{
+ }
+ function test(){
+ toDelete = 5;
+ str.topValue = 1;
+ str.topMapping[0] = 1;
+ str.topMapping[1] = 2;
+
+ str.nstr.nestedValue = 2;
+ str.nstr.nestedMapping[0] = true;
+ str.nstr.nestedMapping[1] = false;
+ delete str;
+ delete toDelete;
+ }
+ function getToDelete() returns (uint res){
+ res = toDelete;
+ }
+ function getTopValue() returns(uint topValue){
+ topValue = str.topValue;
+ }
+ function getNestedValue() returns(uint nestedValue){
+ nestedValue = str.nstr.nestedValue;
+ }
+ function getTopMapping(uint index) returns(uint ret) {
+ ret = str.topMapping[index];
+ }
+ function getNestedMapping(uint index) returns(bool ret) {
+ return str.nstr.nestedMapping[index];
+ }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("getToDelete()") == encodeArgs(0));
+ BOOST_CHECK(callContractFunction("getTopValue()") == encodeArgs(0));
+ BOOST_CHECK(callContractFunction("getNestedValue()") == encodeArgs(0));
+ // mapping values should be the same
+ BOOST_CHECK(callContractFunction("getTopMapping(uint256)", 0) == encodeArgs(1));
+ BOOST_CHECK(callContractFunction("getTopMapping(uint256)", 1) == encodeArgs(2));
+ BOOST_CHECK(callContractFunction("getNestedMapping(uint256)", 0) == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("getNestedMapping(uint256)", 1) == encodeArgs(false));
+}
+
+BOOST_AUTO_TEST_CASE(deleteLocal)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function delLocal() returns (uint res){
+ uint v = 5;
+ delete v;
+ res = v;
+ }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("delLocal()") == encodeArgs(0));
+}
+
+BOOST_AUTO_TEST_CASE(deleteLocals)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function delLocal() returns (uint res1, uint res2){
+ uint v = 5;
+ uint w = 6;
+ uint x = 7;
+ delete v;
+ res1 = w;
+ res2 = x;
+ }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("delLocal()") == encodeArgs(6, 7));
+}
+
+BOOST_AUTO_TEST_CASE(constructor)
+{
+ char const* sourceCode = "contract test {\n"
+ " mapping(uint => uint) data;\n"
+ " function test() {\n"
+ " data[7] = 8;\n"
+ " }\n"
+ " function get(uint key) returns (uint value) {\n"
+ " return data[key];"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ map<u256, byte> data;
+ data[7] = 8;
+ auto get = [&](u256 const& _x) -> u256
+ {
+ return data[_x];
+ };
+ testSolidityAgainstCpp("get(uint256)", get, u256(6));
+ testSolidityAgainstCpp("get(uint256)", get, u256(7));
+}
+
+BOOST_AUTO_TEST_CASE(simple_accessor)
+{
+ char const* sourceCode = "contract test {\n"
+ " uint256 public data;\n"
+ " function test() {\n"
+ " data = 8;\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("data()") == encodeArgs(8));
+}
+
+BOOST_AUTO_TEST_CASE(array_accessor)
+{
+ char const* sourceCode = R"(
+ contract test {
+ uint[8] public data;
+ uint[] public dynamicData;
+ uint24[] public smallTypeData;
+ struct st { uint a; uint[] finalArray; }
+ mapping(uint256 => mapping(uint256 => st[5])) public multiple_map;
+
+ function test() {
+ data[0] = 8;
+ dynamicData.length = 3;
+ dynamicData[2] = 8;
+ smallTypeData.length = 128;
+ smallTypeData[1] = 22;
+ smallTypeData[127] = 2;
+ multiple_map[2][1][2].a = 3;
+ multiple_map[2][1][2].finalArray.length = 4;
+ multiple_map[2][1][2].finalArray[3] = 5;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("data(uint256)", 0) == encodeArgs(8));
+ BOOST_CHECK(callContractFunction("data(uint256)", 8) == encodeArgs());
+ BOOST_CHECK(callContractFunction("dynamicData(uint256)", 2) == encodeArgs(8));
+ BOOST_CHECK(callContractFunction("dynamicData(uint256)", 8) == encodeArgs());
+ BOOST_CHECK(callContractFunction("smallTypeData(uint256)", 1) == encodeArgs(22));
+ BOOST_CHECK(callContractFunction("smallTypeData(uint256)", 127) == encodeArgs(2));
+ BOOST_CHECK(callContractFunction("smallTypeData(uint256)", 128) == encodeArgs());
+ BOOST_CHECK(callContractFunction("multiple_map(uint256,uint256,uint256)", 2, 1, 2) == encodeArgs(3));
+}
+
+BOOST_AUTO_TEST_CASE(accessors_mapping_for_array)
+{
+ char const* sourceCode = R"(
+ contract test {
+ mapping(uint => uint[8]) public data;
+ mapping(uint => uint[]) public dynamicData;
+ function test() {
+ data[2][2] = 8;
+ dynamicData[2].length = 3;
+ dynamicData[2][2] = 8;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("data(uint256,uint256)", 2, 2) == encodeArgs(8));
+ BOOST_CHECK(callContractFunction("data(uint256, 256)", 2, 8) == encodeArgs());
+ BOOST_CHECK(callContractFunction("dynamicData(uint256,uint256)", 2, 2) == encodeArgs(8));
+ BOOST_CHECK(callContractFunction("dynamicData(uint256,uint256)", 2, 8) == encodeArgs());
+}
+
+BOOST_AUTO_TEST_CASE(multiple_elementary_accessors)
+{
+ char const* sourceCode = "contract test {\n"
+ " uint256 public data;\n"
+ " bytes6 public name;\n"
+ " bytes32 public a_hash;\n"
+ " address public an_address;\n"
+ " function test() {\n"
+ " data = 8;\n"
+ " name = \"Celina\";\n"
+ " a_hash = sha3(123);\n"
+ " an_address = address(0x1337);\n"
+ " super_secret_data = 42;\n"
+ " }\n"
+ " uint256 super_secret_data;"
+ "}\n";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("data()") == encodeArgs(8));
+ BOOST_CHECK(callContractFunction("name()") == encodeArgs("Celina"));
+ BOOST_CHECK(callContractFunction("a_hash()") == encodeArgs(dev::sha3(bytes(1, 0x7b))));
+ BOOST_CHECK(callContractFunction("an_address()") == encodeArgs(toBigEndian(u160(0x1337))));
+ BOOST_CHECK(callContractFunction("super_secret_data()") == bytes());
+}
+
+BOOST_AUTO_TEST_CASE(complex_accessors)
+{
+ char const* sourceCode = R"(
+ contract test {
+ mapping(uint256 => bytes4) public to_string_map;
+ mapping(uint256 => bool) public to_bool_map;
+ mapping(uint256 => uint256) public to_uint_map;
+ mapping(uint256 => mapping(uint256 => uint256)) public to_multiple_map;
+ function test() {
+ to_string_map[42] = "24";
+ to_bool_map[42] = false;
+ to_uint_map[42] = 12;
+ to_multiple_map[42][23] = 31;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("to_string_map(uint256)", 42) == encodeArgs("24"));
+ BOOST_CHECK(callContractFunction("to_bool_map(uint256)", 42) == encodeArgs(false));
+ BOOST_CHECK(callContractFunction("to_uint_map(uint256)", 42) == encodeArgs(12));
+ BOOST_CHECK(callContractFunction("to_multiple_map(uint256,uint256)", 42, 23) == encodeArgs(31));
+}
+
+BOOST_AUTO_TEST_CASE(struct_accessor)
+{
+ char const* sourceCode = R"(
+ contract test {
+ struct Data { uint a; uint8 b; mapping(uint => uint) c; bool d; }
+ mapping(uint => Data) public data;
+ function test() {
+ data[7].a = 1;
+ data[7].b = 2;
+ data[7].c[0] = 3;
+ data[7].d = true;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("data(uint256)", 7) == encodeArgs(1, 2, true));
+}
+
+BOOST_AUTO_TEST_CASE(balance)
+{
+ char const* sourceCode = "contract test {\n"
+ " function getBalance() returns (uint256 balance) {\n"
+ " return address(this).balance;\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode, 23);
+ BOOST_CHECK(callContractFunction("getBalance()") == encodeArgs(23));
+}
+
+BOOST_AUTO_TEST_CASE(blockchain)
+{
+ char const* sourceCode = "contract test {\n"
+ " function someInfo() returns (uint256 value, address coinbase, uint256 blockNumber) {\n"
+ " value = msg.value;\n"
+ " coinbase = block.coinbase;\n"
+ " blockNumber = block.number;\n"
+ " }\n"
+ "}\n";
+ m_envInfo.setBeneficiary(Address(0x123));
+ m_envInfo.setNumber(7);
+ compileAndRun(sourceCode, 27);
+ BOOST_CHECK(callContractFunctionWithValue("someInfo()", 28) == encodeArgs(28, 0x123, 7));
+}
+
+BOOST_AUTO_TEST_CASE(msg_sig)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function foo(uint256 a) returns (bytes4 value) {
+ return msg.sig;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunctionWithValue("foo(uint256)", 13) == encodeArgs(asString(FixedHash<4>(dev::sha3("foo(uint256)")).asBytes())));
+}
+
+BOOST_AUTO_TEST_CASE(msg_sig_after_internal_call_is_same)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function boo() returns (bytes4 value) {
+ return msg.sig;
+ }
+ function foo(uint256 a) returns (bytes4 value) {
+ return boo();
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunctionWithValue("foo(uint256)", 13) == encodeArgs(asString(FixedHash<4>(dev::sha3("foo(uint256)")).asBytes())));
+}
+
+BOOST_AUTO_TEST_CASE(now)
+{
+ char const* sourceCode = "contract test {\n"
+ " function someInfo() returns (bool equal, uint val) {\n"
+ " equal = block.timestamp == now;\n"
+ " val = now;\n"
+ " }\n"
+ "}\n";
+ m_envInfo.setTimestamp(9);
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("someInfo()") == encodeArgs(true, 9));
+}
+
+BOOST_AUTO_TEST_CASE(type_conversions_cleanup)
+{
+ // 22-byte integer converted to a contract (i.e. address, 20 bytes), converted to a 32 byte
+ // integer should drop the first two bytes
+ char const* sourceCode = R"(
+ contract Test {
+ function test() returns (uint ret) { return uint(address(Test(address(0x11223344556677889900112233445566778899001122)))); }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_REQUIRE(callContractFunction("test()") == bytes({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22,
+ 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22}));
+}
+// fixed bytes to fixed bytes conversion tests
+BOOST_AUTO_TEST_CASE(convert_fixed_bytes_to_fixed_bytes_smaller_size)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ function bytesToBytes(bytes4 input) returns (bytes2 ret) {
+ return bytes2(input);
+ }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("bytesToBytes(bytes4)", "abcd") == encodeArgs("ab"));
+}
+
+BOOST_AUTO_TEST_CASE(convert_fixed_bytes_to_fixed_bytes_greater_size)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ function bytesToBytes(bytes2 input) returns (bytes4 ret) {
+ return bytes4(input);
+ }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("bytesToBytes(bytes2)", "ab") == encodeArgs("ab"));
+}
+
+BOOST_AUTO_TEST_CASE(convert_fixed_bytes_to_fixed_bytes_same_size)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ function bytesToBytes(bytes4 input) returns (bytes4 ret) {
+ return bytes4(input);
+ }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("bytesToBytes(bytes4)", "abcd") == encodeArgs("abcd"));
+}
+// fixed bytes to uint conversion tests
+BOOST_AUTO_TEST_CASE(convert_fixed_bytes_to_uint_same_size)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ function bytesToUint(bytes32 s) returns (uint256 h) {
+ return uint(s);
+ }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("bytesToUint(bytes32)", string("abc2")) ==
+ encodeArgs(u256("0x6162633200000000000000000000000000000000000000000000000000000000")));
+}
+
+BOOST_AUTO_TEST_CASE(convert_fixed_bytes_to_uint_same_min_size)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ function bytesToUint(bytes1 s) returns (uint8 h) {
+ return uint8(s);
+ }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("bytesToUint(bytes1)", string("a")) ==
+ encodeArgs(u256("0x61")));
+}
+
+BOOST_AUTO_TEST_CASE(convert_fixed_bytes_to_uint_smaller_size)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ function bytesToUint(bytes4 s) returns (uint16 h) {
+ return uint16(s);
+ }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("bytesToUint(bytes4)", string("abcd")) ==
+ encodeArgs(u256("0x6364")));
+}
+
+BOOST_AUTO_TEST_CASE(convert_fixed_bytes_to_uint_greater_size)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ function bytesToUint(bytes4 s) returns (uint64 h) {
+ return uint64(s);
+ }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("bytesToUint(bytes4)", string("abcd")) ==
+ encodeArgs(u256("0x61626364")));
+}
+// uint fixed bytes conversion tests
+BOOST_AUTO_TEST_CASE(convert_uint_to_fixed_bytes_same_size)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ function uintToBytes(uint256 h) returns (bytes32 s) {
+ return bytes32(h);
+ }
+ })";
+ compileAndRun(sourceCode);
+ u256 a("0x6162630000000000000000000000000000000000000000000000000000000000");
+ BOOST_CHECK(callContractFunction("uintToBytes(uint256)", a) == encodeArgs(a));
+}
+
+BOOST_AUTO_TEST_CASE(convert_uint_to_fixed_bytes_same_min_size)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ function UintToBytes(uint8 h) returns (bytes1 s) {
+ return bytes1(h);
+ }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("UintToBytes(uint8)", u256("0x61")) ==
+ encodeArgs(string("a")));
+}
+
+BOOST_AUTO_TEST_CASE(convert_uint_to_fixed_bytes_smaller_size)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ function uintToBytes(uint32 h) returns (bytes2 s) {
+ return bytes2(h);
+ }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("uintToBytes(uint32)",
+ u160("0x61626364")) == encodeArgs(string("cd")));
+}
+
+BOOST_AUTO_TEST_CASE(convert_uint_to_fixed_bytes_greater_size)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ function UintToBytes(uint16 h) returns (bytes8 s) {
+ return bytes8(h);
+ }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("UintToBytes(uint16)", u256("0x6162")) ==
+ encodeArgs(string("\0\0\0\0\0\0ab", 8)));
+}
+
+BOOST_AUTO_TEST_CASE(send_ether)
+{
+ char const* sourceCode = "contract test {\n"
+ " function a(address addr, uint amount) returns (uint ret) {\n"
+ " addr.send(amount);\n"
+ " return address(this).balance;\n"
+ " }\n"
+ "}\n";
+ u256 amount(130);
+ compileAndRun(sourceCode, amount + 1);
+ u160 address(23);
+ BOOST_CHECK(callContractFunction("a(address,uint256)", address, amount) == encodeArgs(1));
+ BOOST_CHECK_EQUAL(m_state.balance(address), amount);
+}
+
+BOOST_AUTO_TEST_CASE(log0)
+{
+ char const* sourceCode = "contract test {\n"
+ " function a() {\n"
+ " log0(1);\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ callContractFunction("a()");
+ BOOST_CHECK_EQUAL(m_logs.size(), 1);
+ BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
+ BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1)));
+ BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(log1)
+{
+ char const* sourceCode = "contract test {\n"
+ " function a() {\n"
+ " log1(1, 2);\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ callContractFunction("a()");
+ BOOST_CHECK_EQUAL(m_logs.size(), 1);
+ BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
+ BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1)));
+ BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 1);
+ BOOST_CHECK_EQUAL(m_logs[0].topics[0], h256(u256(2)));
+}
+
+BOOST_AUTO_TEST_CASE(log2)
+{
+ char const* sourceCode = "contract test {\n"
+ " function a() {\n"
+ " log2(1, 2, 3);\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ callContractFunction("a()");
+ BOOST_CHECK_EQUAL(m_logs.size(), 1);
+ BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
+ BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1)));
+ BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 2);
+ for (unsigned i = 0; i < 2; ++i)
+ BOOST_CHECK_EQUAL(m_logs[0].topics[i], h256(u256(i + 2)));
+}
+
+BOOST_AUTO_TEST_CASE(log3)
+{
+ char const* sourceCode = "contract test {\n"
+ " function a() {\n"
+ " log3(1, 2, 3, 4);\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ callContractFunction("a()");
+ BOOST_CHECK_EQUAL(m_logs.size(), 1);
+ BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
+ BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1)));
+ BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 3);
+ for (unsigned i = 0; i < 3; ++i)
+ BOOST_CHECK_EQUAL(m_logs[0].topics[i], h256(u256(i + 2)));
+}
+
+BOOST_AUTO_TEST_CASE(log4)
+{
+ char const* sourceCode = "contract test {\n"
+ " function a() {\n"
+ " log4(1, 2, 3, 4, 5);\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ callContractFunction("a()");
+ BOOST_CHECK_EQUAL(m_logs.size(), 1);
+ BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
+ BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1)));
+ BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 4);
+ for (unsigned i = 0; i < 4; ++i)
+ BOOST_CHECK_EQUAL(m_logs[0].topics[i], h256(u256(i + 2)));
+}
+
+BOOST_AUTO_TEST_CASE(log_in_constructor)
+{
+ char const* sourceCode = "contract test {\n"
+ " function test() {\n"
+ " log1(1, 2);\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ BOOST_CHECK_EQUAL(m_logs.size(), 1);
+ BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
+ BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1)));
+ BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 1);
+ BOOST_CHECK_EQUAL(m_logs[0].topics[0], h256(u256(2)));
+}
+
+BOOST_AUTO_TEST_CASE(suicide)
+{
+ char const* sourceCode = "contract test {\n"
+ " function a(address receiver) returns (uint ret) {\n"
+ " suicide(receiver);\n"
+ " return 10;\n"
+ " }\n"
+ "}\n";
+ u256 amount(130);
+ compileAndRun(sourceCode, amount);
+ u160 address(23);
+ BOOST_CHECK(callContractFunction("a(address)", address) == bytes());
+ BOOST_CHECK(!m_state.addressHasCode(m_contractAddress));
+ BOOST_CHECK_EQUAL(m_state.balance(address), amount);
+}
+
+BOOST_AUTO_TEST_CASE(sha3)
+{
+ char const* sourceCode = "contract test {\n"
+ " function a(bytes32 input) returns (bytes32 sha3hash) {\n"
+ " return sha3(input);\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ auto f = [&](u256 const& _x) -> u256
+ {
+ return dev::sha3(toBigEndian(_x));
+ };
+ testSolidityAgainstCpp("a(bytes32)", f, u256(4));
+ testSolidityAgainstCpp("a(bytes32)", f, u256(5));
+ testSolidityAgainstCpp("a(bytes32)", f, u256(-1));
+}
+
+BOOST_AUTO_TEST_CASE(sha256)
+{
+ char const* sourceCode = "contract test {\n"
+ " function a(bytes32 input) returns (bytes32 sha256hash) {\n"
+ " return sha256(input);\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ auto f = [&](u256 const& _input) -> u256
+ {
+ return dev::sha256(dev::ref(toBigEndian(_input)));
+ };
+ testSolidityAgainstCpp("a(bytes32)", f, u256(4));
+ testSolidityAgainstCpp("a(bytes32)", f, u256(5));
+ testSolidityAgainstCpp("a(bytes32)", f, u256(-1));
+}
+
+BOOST_AUTO_TEST_CASE(ripemd)
+{
+ char const* sourceCode = "contract test {\n"
+ " function a(bytes32 input) returns (bytes32 sha256hash) {\n"
+ " return ripemd160(input);\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ auto f = [&](u256 const& _input) -> u256
+ {
+ return h256(dev::ripemd160(h256(_input).ref()), h256::AlignLeft); // This should be aligned right. i guess it's fixed elsewhere?
+ };
+ testSolidityAgainstCpp("a(bytes32)", f, u256(4));
+ testSolidityAgainstCpp("a(bytes32)", f, u256(5));
+ testSolidityAgainstCpp("a(bytes32)", f, u256(-1));
+}
+
+BOOST_AUTO_TEST_CASE(ecrecover)
+{
+ char const* sourceCode = "contract test {\n"
+ " function a(bytes32 h, uint8 v, bytes32 r, bytes32 s) returns (address addr) {\n"
+ " return ecrecover(h, v, r, s);\n"
+ " }\n"
+ "}\n";
+ compileAndRun(sourceCode);
+ u256 h("0x18c547e4f7b0f325ad1e56f57e26c745b09a3e503d86e00e5255ff7f715d3d1c");
+ byte v = 28;
+ u256 r("0x73b1693892219d736caba55bdb67216e485557ea6b6af75f37096c9aa6a5a75f");
+ u256 s("0xeeb940b1d03b21e36b0e47e79769f095fe2ab855bd91e3a38756b7d75a9c4549");
+ u160 addr("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b");
+ BOOST_CHECK(callContractFunction("a(bytes32,uint8,bytes32,bytes32)", h, v, r, s) == encodeArgs(addr));
+}
+
+BOOST_AUTO_TEST_CASE(inter_contract_calls)
+{
+ char const* sourceCode = R"(
+ contract Helper {
+ function multiply(uint a, uint b) returns (uint c) {
+ return a * b;
+ }
+ }
+ contract Main {
+ Helper h;
+ function callHelper(uint a, uint b) returns (uint c) {
+ return h.multiply(a, b);
+ }
+ function getHelper() returns (address haddress) {
+ return address(h);
+ }
+ function setHelper(address haddress) {
+ h = Helper(haddress);
+ }
+ })";
+ compileAndRun(sourceCode, 0, "Helper");
+ u160 const c_helperAddress = m_contractAddress;
+ compileAndRun(sourceCode, 0, "Main");
+ BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes());
+ BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress));
+ u256 a(3456789);
+ u256 b("0x282837623374623234aa74");
+ BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == encodeArgs(a * b));
+}
+
+BOOST_AUTO_TEST_CASE(inter_contract_calls_with_complex_parameters)
+{
+ char const* sourceCode = R"(
+ contract Helper {
+ function sel(uint a, bool select, uint b) returns (uint c) {
+ if (select) return a; else return b;
+ }
+ }
+ contract Main {
+ Helper h;
+ function callHelper(uint a, bool select, uint b) returns (uint c) {
+ return h.sel(a, select, b) * 3;
+ }
+ function getHelper() returns (address haddress) {
+ return address(h);
+ }
+ function setHelper(address haddress) {
+ h = Helper(haddress);
+ }
+ })";
+ compileAndRun(sourceCode, 0, "Helper");
+ u160 const c_helperAddress = m_contractAddress;
+ compileAndRun(sourceCode, 0, "Main");
+ BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes());
+ BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress));
+ u256 a(3456789);
+ u256 b("0x282837623374623234aa74");
+ BOOST_REQUIRE(callContractFunction("callHelper(uint256,bool,uint256)", a, true, b) == encodeArgs(a * 3));
+ BOOST_REQUIRE(callContractFunction("callHelper(uint256,bool,uint256)", a, false, b) == encodeArgs(b * 3));
+}
+
+BOOST_AUTO_TEST_CASE(inter_contract_calls_accessing_this)
+{
+ char const* sourceCode = R"(
+ contract Helper {
+ function getAddress() returns (address addr) {
+ return address(this);
+ }
+ }
+ contract Main {
+ Helper h;
+ function callHelper() returns (address addr) {
+ return h.getAddress();
+ }
+ function getHelper() returns (address addr) {
+ return address(h);
+ }
+ function setHelper(address addr) {
+ h = Helper(addr);
+ }
+ })";
+ compileAndRun(sourceCode, 0, "Helper");
+ u160 const c_helperAddress = m_contractAddress;
+ compileAndRun(sourceCode, 0, "Main");
+ BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes());
+ BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress));
+ BOOST_REQUIRE(callContractFunction("callHelper()") == encodeArgs(c_helperAddress));
+}
+
+BOOST_AUTO_TEST_CASE(calls_to_this)
+{
+ char const* sourceCode = R"(
+ contract Helper {
+ function invoke(uint a, uint b) returns (uint c) {
+ return this.multiply(a, b, 10);
+ }
+ function multiply(uint a, uint b, uint8 c) returns (uint ret) {
+ return a * b + c;
+ }
+ }
+ contract Main {
+ Helper h;
+ function callHelper(uint a, uint b) returns (uint ret) {
+ return h.invoke(a, b);
+ }
+ function getHelper() returns (address addr) {
+ return address(h);
+ }
+ function setHelper(address addr) {
+ h = Helper(addr);
+ }
+ })";
+ compileAndRun(sourceCode, 0, "Helper");
+ u160 const c_helperAddress = m_contractAddress;
+ compileAndRun(sourceCode, 0, "Main");
+ BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes());
+ BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress));
+ u256 a(3456789);
+ u256 b("0x282837623374623234aa74");
+ BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == encodeArgs(a * b + 10));
+}
+
+BOOST_AUTO_TEST_CASE(inter_contract_calls_with_local_vars)
+{
+ // note that a reference to another contract's function occupies two stack slots,
+ // so this tests correct stack slot allocation
+ char const* sourceCode = R"(
+ contract Helper {
+ function multiply(uint a, uint b) returns (uint c) {
+ return a * b;
+ }
+ }
+ contract Main {
+ Helper h;
+ function callHelper(uint a, uint b) returns (uint c) {
+ var fu = h.multiply;
+ var y = 9;
+ var ret = fu(a, b);
+ return ret + y;
+ }
+ function getHelper() returns (address haddress) {
+ return address(h);
+ }
+ function setHelper(address haddress) {
+ h = Helper(haddress);
+ }
+ })";
+ compileAndRun(sourceCode, 0, "Helper");
+ u160 const c_helperAddress = m_contractAddress;
+ compileAndRun(sourceCode, 0, "Main");
+ BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes());
+ BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress));
+ u256 a(3456789);
+ u256 b("0x282837623374623234aa74");
+ BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == encodeArgs(a * b + 9));
+}
+
+BOOST_AUTO_TEST_CASE(fixed_bytes_in_calls)
+{
+ char const* sourceCode = R"(
+ contract Helper {
+ function invoke(bytes3 x, bool stop) returns (bytes4 ret) {
+ return x;
+ }
+ }
+ contract Main {
+ Helper h;
+ function callHelper(bytes2 x, bool stop) returns (bytes5 ret) {
+ return h.invoke(x, stop);
+ }
+ function getHelper() returns (address addr) {
+ return address(h);
+ }
+ function setHelper(address addr) {
+ h = Helper(addr);
+ }
+ })";
+ compileAndRun(sourceCode, 0, "Helper");
+ u160 const c_helperAddress = m_contractAddress;
+ compileAndRun(sourceCode, 0, "Main");
+ BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes());
+ BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress));
+ BOOST_CHECK(callContractFunction("callHelper(bytes2,bool)", string("\0a", 2), true) == encodeArgs(string("\0a\0\0\0", 5)));
+}
+
+BOOST_AUTO_TEST_CASE(constructor_arguments_internal)
+{
+ char const* sourceCode = R"(
+ contract Helper {
+ bytes3 name;
+ bool flag;
+
+ function Helper(bytes3 x, bool f) {
+ name = x;
+ flag = f;
+ }
+ function getName() returns (bytes3 ret) { return name; }
+ function getFlag() returns (bool ret) { return flag; }
+ }
+ contract Main {
+ Helper h;
+ function Main() {
+ h = new Helper("abc", true);
+ }
+ function getFlag() returns (bool ret) { return h.getFlag(); }
+ function getName() returns (bytes3 ret) { return h.getName(); }
+ })";
+ compileAndRun(sourceCode, 0, "Main");
+ BOOST_CHECK(callContractFunction("getFlag()") == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("getName()") == encodeArgs("abc"));
+}
+
+BOOST_AUTO_TEST_CASE(constructor_arguments_external)
+{
+ char const* sourceCode = R"(
+ contract Main {
+ bytes3 name;
+ bool flag;
+
+ function Main(bytes3 x, bool f) {
+ name = x;
+ flag = f;
+ }
+ function getName() returns (bytes3 ret) { return name; }
+ function getFlag() returns (bool ret) { return flag; }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Main", encodeArgs("abc", true));
+ BOOST_CHECK(callContractFunction("getFlag()") == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("getName()") == encodeArgs("abc"));
+}
+
+BOOST_AUTO_TEST_CASE(functions_called_by_constructor)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ bytes3 name;
+ bool flag;
+ function Test() {
+ setName("abc");
+ }
+ function getName() returns (bytes3 ret) { return name; }
+ function setName(bytes3 _name) private { name = _name; }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_REQUIRE(callContractFunction("getName()") == encodeArgs("abc"));
+}
+
+BOOST_AUTO_TEST_CASE(contracts_as_addresses)
+{
+ char const* sourceCode = R"(
+ contract helper {
+ }
+ contract test {
+ helper h;
+ function test() { h = new helper(); h.send(5); }
+ function getBalance() returns (uint256 myBalance, uint256 helperBalance) {
+ myBalance = this.balance;
+ helperBalance = h.balance;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 20);
+ BOOST_REQUIRE(callContractFunction("getBalance()") == encodeArgs(u256(20 - 5), u256(5)));
+}
+
+BOOST_AUTO_TEST_CASE(gas_and_value_basic)
+{
+ char const* sourceCode = R"(
+ contract helper {
+ bool flag;
+ function getBalance() returns (uint256 myBalance) {
+ return this.balance;
+ }
+ function setFlag() { flag = true; }
+ function getFlag() returns (bool fl) { return flag; }
+ }
+ contract test {
+ helper h;
+ function test() { h = new helper(); }
+ function sendAmount(uint amount) returns (uint256 bal) {
+ return h.getBalance.value(amount)();
+ }
+ function outOfGas() returns (bool ret) {
+ h.setFlag.gas(2)(); // should fail due to OOG
+ return true;
+ }
+ function checkState() returns (bool flagAfter, uint myBal) {
+ flagAfter = h.getFlag();
+ myBal = this.balance;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 20);
+ BOOST_REQUIRE(callContractFunction("sendAmount(uint256)", 5) == encodeArgs(5));
+ // call to helper should not succeed but amount should be transferred anyway
+ BOOST_REQUIRE(callContractFunction("outOfGas()", 5) == bytes());
+ BOOST_REQUIRE(callContractFunction("checkState()", 5) == encodeArgs(false, 20 - 5));
+}
+
+BOOST_AUTO_TEST_CASE(gas_for_builtin)
+{
+ char const* sourceCode = R"(
+ contract Contract {
+ function test(uint g) returns (bytes32 data, bool flag) {
+ data = ripemd160.gas(g)("abc");
+ flag = true;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("test(uint256)", 500) == bytes());
+ BOOST_CHECK(callContractFunction("test(uint256)", 800) == encodeArgs(u256("0x8eb208f7e05d987a9b044a8e98c6b087f15a0bfc000000000000000000000000"), true));
+}
+
+BOOST_AUTO_TEST_CASE(value_complex)
+{
+ char const* sourceCode = R"(
+ contract helper {
+ function getBalance() returns (uint256 myBalance) {
+ return this.balance;
+ }
+ }
+ contract test {
+ helper h;
+ function test() { h = new helper(); }
+ function sendAmount(uint amount) returns (uint256 bal) {
+ var x1 = h.getBalance.value(amount);
+ uint someStackElement = 20;
+ var x2 = x1.gas(1000);
+ return x2.value(amount + 3)();// overwrite value
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 20);
+ BOOST_REQUIRE(callContractFunction("sendAmount(uint256)", 5) == encodeArgs(8));
+}
+
+BOOST_AUTO_TEST_CASE(value_insane)
+{
+ char const* sourceCode = R"(
+ contract helper {
+ function getBalance() returns (uint256 myBalance) {
+ return this.balance;
+ }
+ }
+ contract test {
+ helper h;
+ function test() { h = new helper(); }
+ function sendAmount(uint amount) returns (uint256 bal) {
+ var x1 = h.getBalance.value;
+ var x2 = x1(amount).gas;
+ var x3 = x2(1000).value;
+ return x3(amount + 3)();// overwrite value
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 20);
+ BOOST_REQUIRE(callContractFunction("sendAmount(uint256)", 5) == encodeArgs(8));
+}
+
+BOOST_AUTO_TEST_CASE(value_for_constructor)
+{
+ char const* sourceCode = R"(
+ contract Helper {
+ bytes3 name;
+ bool flag;
+ function Helper(bytes3 x, bool f) {
+ name = x;
+ flag = f;
+ }
+ function getName() returns (bytes3 ret) { return name; }
+ function getFlag() returns (bool ret) { return flag; }
+ }
+ contract Main {
+ Helper h;
+ function Main() {
+ h = new Helper.value(10)("abc", true);
+ }
+ function getFlag() returns (bool ret) { return h.getFlag(); }
+ function getName() returns (bytes3 ret) { return h.getName(); }
+ function getBalances() returns (uint me, uint them) { me = this.balance; them = h.balance;}
+ })";
+ compileAndRun(sourceCode, 22, "Main");
+ BOOST_REQUIRE(callContractFunction("getFlag()") == encodeArgs(true));
+ BOOST_REQUIRE(callContractFunction("getName()") == encodeArgs("abc"));
+ BOOST_REQUIRE(callContractFunction("getBalances()") == encodeArgs(12, 10));
+}
+
+BOOST_AUTO_TEST_CASE(virtual_function_calls)
+{
+ char const* sourceCode = R"(
+ contract Base {
+ function f() returns (uint i) { return g(); }
+ function g() returns (uint i) { return 1; }
+ }
+ contract Derived is Base {
+ function g() returns (uint i) { return 2; }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Derived");
+ BOOST_CHECK(callContractFunction("g()") == encodeArgs(2));
+ BOOST_CHECK(callContractFunction("f()") == encodeArgs(2));
+}
+
+BOOST_AUTO_TEST_CASE(access_base_storage)
+{
+ char const* sourceCode = R"(
+ contract Base {
+ uint dataBase;
+ function getViaBase() returns (uint i) { return dataBase; }
+ }
+ contract Derived is Base {
+ uint dataDerived;
+ function setData(uint base, uint derived) returns (bool r) {
+ dataBase = base;
+ dataDerived = derived;
+ return true;
+ }
+ function getViaDerived() returns (uint base, uint derived) {
+ base = dataBase;
+ derived = dataDerived;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Derived");
+ BOOST_CHECK(callContractFunction("setData(uint256,uint256)", 1, 2) == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("getViaBase()") == encodeArgs(1));
+ BOOST_CHECK(callContractFunction("getViaDerived()") == encodeArgs(1, 2));
+}
+
+BOOST_AUTO_TEST_CASE(single_copy_with_multiple_inheritance)
+{
+ char const* sourceCode = R"(
+ contract Base {
+ uint data;
+ function setData(uint i) { data = i; }
+ function getViaBase() returns (uint i) { return data; }
+ }
+ contract A is Base { function setViaA(uint i) { setData(i); } }
+ contract B is Base { function getViaB() returns (uint i) { return getViaBase(); } }
+ contract Derived is Base, B, A { }
+ )";
+ compileAndRun(sourceCode, 0, "Derived");
+ BOOST_CHECK(callContractFunction("getViaB()") == encodeArgs(0));
+ BOOST_CHECK(callContractFunction("setViaA(uint256)", 23) == encodeArgs());
+ BOOST_CHECK(callContractFunction("getViaB()") == encodeArgs(23));
+}
+
+BOOST_AUTO_TEST_CASE(explicit_base_cass)
+{
+ char const* sourceCode = R"(
+ contract BaseBase { function g() returns (uint r) { return 1; } }
+ contract Base is BaseBase { function g() returns (uint r) { return 2; } }
+ contract Derived is Base {
+ function f() returns (uint r) { return BaseBase.g(); }
+ function g() returns (uint r) { return 3; }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Derived");
+ BOOST_CHECK(callContractFunction("g()") == encodeArgs(3));
+ BOOST_CHECK(callContractFunction("f()") == encodeArgs(1));
+}
+
+BOOST_AUTO_TEST_CASE(base_constructor_arguments)
+{
+ char const* sourceCode = R"(
+ contract BaseBase {
+ uint m_a;
+ function BaseBase(uint a) {
+ m_a = a;
+ }
+ }
+ contract Base is BaseBase(7) {
+ function Base() {
+ m_a *= m_a;
+ }
+ }
+ contract Derived is Base() {
+ function getA() returns (uint r) { return m_a; }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Derived");
+ BOOST_CHECK(callContractFunction("getA()") == encodeArgs(7 * 7));
+}
+
+BOOST_AUTO_TEST_CASE(function_usage_in_constructor_arguments)
+{
+ char const* sourceCode = R"(
+ contract BaseBase {
+ uint m_a;
+ function BaseBase(uint a) {
+ m_a = a;
+ }
+ function g() returns (uint r) { return 2; }
+ }
+ contract Base is BaseBase(BaseBase.g()) {
+ }
+ contract Derived is Base() {
+ function getA() returns (uint r) { return m_a; }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Derived");
+ BOOST_CHECK(callContractFunction("getA()") == encodeArgs(2));
+}
+
+BOOST_AUTO_TEST_CASE(virtual_function_usage_in_constructor_arguments)
+{
+ char const* sourceCode = R"(
+ contract BaseBase {
+ uint m_a;
+ function BaseBase(uint a) {
+ m_a = a;
+ }
+ function overridden() returns (uint r) { return 1; }
+ function g() returns (uint r) { return overridden(); }
+ }
+ contract Base is BaseBase(BaseBase.g()) {
+ }
+ contract Derived is Base() {
+ function getA() returns (uint r) { return m_a; }
+ function overridden() returns (uint r) { return 2; }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Derived");
+ BOOST_CHECK(callContractFunction("getA()") == encodeArgs(2));
+}
+
+BOOST_AUTO_TEST_CASE(constructor_argument_overriding)
+{
+ char const* sourceCode = R"(
+ contract BaseBase {
+ uint m_a;
+ function BaseBase(uint a) {
+ m_a = a;
+ }
+ }
+ contract Base is BaseBase(2) { }
+ contract Derived is BaseBase(3), Base {
+ function getA() returns (uint r) { return m_a; }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Derived");
+ BOOST_CHECK(callContractFunction("getA()") == encodeArgs(3));
+}
+
+BOOST_AUTO_TEST_CASE(function_modifier)
+{
+ char const* sourceCode = R"(
+ contract C {
+ function getOne() nonFree returns (uint r) { return 1; }
+ modifier nonFree { if (msg.value > 0) _ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("getOne()") == encodeArgs(0));
+ BOOST_CHECK(callContractFunctionWithValue("getOne()", 1) == encodeArgs(1));
+}
+
+BOOST_AUTO_TEST_CASE(function_modifier_local_variables)
+{
+ char const* sourceCode = R"(
+ contract C {
+ modifier mod1 { var a = 1; var b = 2; _ }
+ modifier mod2(bool a) { if (a) return; else _ }
+ function f(bool a) mod1 mod2(a) returns (uint r) { return 3; }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("f(bool)", true) == encodeArgs(0));
+ BOOST_CHECK(callContractFunction("f(bool)", false) == encodeArgs(3));
+}
+
+BOOST_AUTO_TEST_CASE(function_modifier_loop)
+{
+ char const* sourceCode = R"(
+ contract C {
+ modifier repeat(uint count) { for (var i = 0; i < count; ++i) _ }
+ function f() repeat(10) returns (uint r) { r += 1; }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("f()") == encodeArgs(10));
+}
+
+BOOST_AUTO_TEST_CASE(function_modifier_multi_invocation)
+{
+ char const* sourceCode = R"(
+ contract C {
+ modifier repeat(bool twice) { if (twice) _ _ }
+ function f(bool twice) repeat(twice) returns (uint r) { r += 1; }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("f(bool)", false) == encodeArgs(1));
+ BOOST_CHECK(callContractFunction("f(bool)", true) == encodeArgs(2));
+}
+
+BOOST_AUTO_TEST_CASE(function_modifier_multi_with_return)
+{
+ // Here, the explicit return prevents the second execution
+ char const* sourceCode = R"(
+ contract C {
+ modifier repeat(bool twice) { if (twice) _ _ }
+ function f(bool twice) repeat(twice) returns (uint r) { r += 1; return r; }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("f(bool)", false) == encodeArgs(1));
+ BOOST_CHECK(callContractFunction("f(bool)", true) == encodeArgs(1));
+}
+
+BOOST_AUTO_TEST_CASE(function_modifier_overriding)
+{
+ char const* sourceCode = R"(
+ contract A {
+ function f() mod returns (bool r) { return true; }
+ modifier mod { _ }
+ }
+ contract C is A {
+ modifier mod { }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("f()") == encodeArgs(false));
+}
+
+BOOST_AUTO_TEST_CASE(function_modifier_calling_functions_in_creation_context)
+{
+ char const* sourceCode = R"(
+ contract A {
+ uint data;
+ function A() mod1 { f1(); }
+ function f1() mod2 { data |= 0x1; }
+ function f2() { data |= 0x20; }
+ function f3() { }
+ modifier mod1 { f2(); _ }
+ modifier mod2 { f3(); }
+ function getData() returns (uint r) { return data; }
+ }
+ contract C is A {
+ modifier mod1 { f4(); _ }
+ function f3() { data |= 0x300; }
+ function f4() { data |= 0x4000; }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("getData()") == encodeArgs(0x4300));
+}
+
+BOOST_AUTO_TEST_CASE(function_modifier_for_constructor)
+{
+ char const* sourceCode = R"(
+ contract A {
+ uint data;
+ function A() mod1 { data |= 2; }
+ modifier mod1 { data |= 1; _ }
+ function getData() returns (uint r) { return data; }
+ }
+ contract C is A {
+ modifier mod1 { data |= 4; _ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("getData()") == encodeArgs(4 | 2));
+}
+
+BOOST_AUTO_TEST_CASE(use_std_lib)
+{
+ char const* sourceCode = R"(
+ import "mortal";
+ contract Icarus is mortal { }
+ )";
+ m_addStandardSources = true;
+ u256 amount(130);
+ u160 address(23);
+ compileAndRun(sourceCode, amount, "Icarus");
+ u256 balanceBefore = m_state.balance(m_sender);
+ BOOST_CHECK(callContractFunction("kill()") == bytes());
+ BOOST_CHECK(!m_state.addressHasCode(m_contractAddress));
+ BOOST_CHECK(m_state.balance(m_sender) > balanceBefore);
+}
+
+BOOST_AUTO_TEST_CASE(crazy_elementary_typenames_on_stack)
+{
+ char const* sourceCode = R"(
+ contract C {
+ function f() returns (uint r) {
+ uint; uint; uint; uint;
+ int x = -7;
+ var a = uint;
+ return a(x);
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(-7)));
+}
+
+BOOST_AUTO_TEST_CASE(super)
+{
+ char const* sourceCode = R"(
+ contract A { function f() returns (uint r) { return 1; } }
+ contract B is A { function f() returns (uint r) { return super.f() | 2; } }
+ contract C is A { function f() returns (uint r) { return super.f() | 4; } }
+ contract D is B, C { function f() returns (uint r) { return super.f() | 8; } }
+ )";
+ compileAndRun(sourceCode, 0, "D");
+ BOOST_CHECK(callContractFunction("f()") == encodeArgs(1 | 2 | 4 | 8));
+}
+
+BOOST_AUTO_TEST_CASE(super_in_constructor)
+{
+ char const* sourceCode = R"(
+ contract A { function f() returns (uint r) { return 1; } }
+ contract B is A { function f() returns (uint r) { return super.f() | 2; } }
+ contract C is A { function f() returns (uint r) { return super.f() | 4; } }
+ contract D is B, C { uint data; function D() { data = super.f() | 8; } function f() returns (uint r) { return data; } }
+ )";
+ compileAndRun(sourceCode, 0, "D");
+ BOOST_CHECK(callContractFunction("f()") == encodeArgs(1 | 2 | 4 | 8));
+}
+
+BOOST_AUTO_TEST_CASE(fallback_function)
+{
+ char const* sourceCode = R"(
+ contract A {
+ uint data;
+ function() returns (uint r) { data = 1; return 2; }
+ function getData() returns (uint r) { return data; }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("getData()") == encodeArgs(0));
+ BOOST_CHECK(callContractFunction("") == encodeArgs(2));
+ BOOST_CHECK(callContractFunction("getData()") == encodeArgs(1));
+}
+
+BOOST_AUTO_TEST_CASE(inherited_fallback_function)
+{
+ char const* sourceCode = R"(
+ contract A {
+ uint data;
+ function() returns (uint r) { data = 1; return 2; }
+ function getData() returns (uint r) { return data; }
+ }
+ contract B is A {}
+ )";
+ compileAndRun(sourceCode, 0, "B");
+ BOOST_CHECK(callContractFunction("getData()") == encodeArgs(0));
+ BOOST_CHECK(callContractFunction("") == encodeArgs(2));
+ BOOST_CHECK(callContractFunction("getData()") == encodeArgs(1));
+}
+
+BOOST_AUTO_TEST_CASE(event)
+{
+ char const* sourceCode = R"(
+ contract ClientReceipt {
+ event Deposit(address indexed _from, bytes32 indexed _id, uint _value);
+ function deposit(bytes32 _id, bool _manually) {
+ if (_manually) {
+ bytes32 s = 0x19dacbf83c5de6658e14cbf7bcae5c15eca2eedecf1c66fbca928e4d351bea0f;
+ log3(bytes32(msg.value), s, bytes32(msg.sender), _id);
+ } else
+ Deposit(msg.sender, _id, msg.value);
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ u256 value(18);
+ u256 id(0x1234);
+ for (bool manually: {true, false})
+ {
+ callContractFunctionWithValue("deposit(bytes32,bool)", value, id, manually);
+ BOOST_REQUIRE_EQUAL(m_logs.size(), 1);
+ BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
+ BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(value)));
+ BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 3);
+ BOOST_CHECK_EQUAL(m_logs[0].topics[0], dev::sha3(string("Deposit(address,bytes32,uint256)")));
+ BOOST_CHECK_EQUAL(m_logs[0].topics[1], h256(m_sender));
+ BOOST_CHECK_EQUAL(m_logs[0].topics[2], h256(id));
+ }
+}
+
+BOOST_AUTO_TEST_CASE(event_no_arguments)
+{
+ char const* sourceCode = R"(
+ contract ClientReceipt {
+ event Deposit;
+ function deposit() {
+ Deposit();
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ callContractFunction("deposit()");
+ BOOST_REQUIRE_EQUAL(m_logs.size(), 1);
+ BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
+ BOOST_CHECK(m_logs[0].data.empty());
+ BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 1);
+ BOOST_CHECK_EQUAL(m_logs[0].topics[0], dev::sha3(string("Deposit()")));
+}
+
+BOOST_AUTO_TEST_CASE(event_anonymous)
+{
+ char const* sourceCode = R"(
+ contract ClientReceipt {
+ event Deposit() anonymous;
+ function deposit() {
+ Deposit();
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ callContractFunction("deposit()");
+ BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(event_anonymous_with_topics)
+{
+ char const* sourceCode = R"(
+ contract ClientReceipt {
+ event Deposit(address indexed _from, bytes32 indexed _id, uint _value) anonymous;
+ function deposit(bytes32 _id, bool _manually) {
+ Deposit(msg.sender, _id, msg.value);
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ u256 value(18);
+ u256 id(0x1234);
+ callContractFunctionWithValue("deposit(bytes32,bool)", value, id);
+ BOOST_REQUIRE_EQUAL(m_logs.size(), 1);
+ BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
+ BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(value)));
+ BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 2);
+ BOOST_CHECK_EQUAL(m_logs[0].topics[0], h256(m_sender));
+ BOOST_CHECK_EQUAL(m_logs[0].topics[1], h256(id));
+}
+
+BOOST_AUTO_TEST_CASE(event_lots_of_data)
+{
+ char const* sourceCode = R"(
+ contract ClientReceipt {
+ event Deposit(address _from, bytes32 _id, uint _value, bool _flag);
+ function deposit(bytes32 _id) {
+ Deposit(msg.sender, _id, msg.value, true);
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ u256 value(18);
+ u256 id(0x1234);
+ callContractFunctionWithValue("deposit(bytes32)", value, id);
+ BOOST_REQUIRE_EQUAL(m_logs.size(), 1);
+ BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
+ BOOST_CHECK(m_logs[0].data == encodeArgs((u160)m_sender, id, value, true));
+ BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 1);
+ BOOST_CHECK_EQUAL(m_logs[0].topics[0], dev::sha3(string("Deposit(address,bytes32,uint256,bool)")));
+}
+
+BOOST_AUTO_TEST_CASE(event_really_lots_of_data)
+{
+ char const* sourceCode = R"(
+ contract ClientReceipt {
+ event Deposit(uint fixeda, bytes dynx, uint fixedb);
+ function deposit() {
+ Deposit(10, msg.data, 15);
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ callContractFunction("deposit()");
+ BOOST_REQUIRE_EQUAL(m_logs.size(), 1);
+ BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
+ BOOST_CHECK(m_logs[0].data == encodeArgs(10, 0x60, 15, 4) + FixedHash<4>(dev::sha3("deposit()")).asBytes());
+ BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 1);
+ BOOST_CHECK_EQUAL(m_logs[0].topics[0], dev::sha3(string("Deposit(uint256,bytes,uint256)")));
+}
+
+BOOST_AUTO_TEST_CASE(event_really_lots_of_data_from_storage)
+{
+ char const* sourceCode = R"(
+ contract ClientReceipt {
+ bytes x;
+ event Deposit(uint fixeda, bytes dynx, uint fixedb);
+ function deposit() {
+ x.length = 3;
+ x[0] = "A";
+ x[1] = "B";
+ x[2] = "C";
+ Deposit(10, x, 15);
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ callContractFunction("deposit()");
+ BOOST_REQUIRE_EQUAL(m_logs.size(), 1);
+ BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
+ BOOST_CHECK(m_logs[0].data == encodeArgs(10, 0x60, 15, 3, string("ABC")));
+ BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 1);
+ BOOST_CHECK_EQUAL(m_logs[0].topics[0], dev::sha3(string("Deposit(uint256,bytes,uint256)")));
+}
+
+BOOST_AUTO_TEST_CASE(empty_name_input_parameter_with_named_one)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f(uint, uint k) returns(uint ret_k, uint ret_g){
+ uint g = 8;
+ ret_k = k;
+ ret_g = g;
+ }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("f(uint256,uint256)", 5, 9) != encodeArgs(5, 8));
+ BOOST_CHECK(callContractFunction("f(uint256,uint256)", 5, 9) == encodeArgs(9, 8));
+}
+
+BOOST_AUTO_TEST_CASE(empty_name_return_parameter)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f(uint k) returns(uint){
+ return k;
+ }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("f(uint256)", 9) == encodeArgs(9));
+}
+
+BOOST_AUTO_TEST_CASE(sha3_multiple_arguments)
+{
+ char const* sourceCode = R"(
+ contract c {
+ function foo(uint a, uint b, uint c) returns (bytes32 d)
+ {
+ d = sha3(a, b, c);
+ }
+ })";
+ compileAndRun(sourceCode);
+
+ BOOST_CHECK(callContractFunction("foo(uint256,uint256,uint256)", 10, 12, 13) == encodeArgs(
+ dev::sha3(
+ toBigEndian(u256(10)) +
+ toBigEndian(u256(12)) +
+ toBigEndian(u256(13)))));
+}
+
+BOOST_AUTO_TEST_CASE(sha3_multiple_arguments_with_numeric_literals)
+{
+ char const* sourceCode = R"(
+ contract c {
+ function foo(uint a, uint16 b) returns (bytes32 d)
+ {
+ d = sha3(a, b, 145);
+ }
+ })";
+ compileAndRun(sourceCode);
+
+ BOOST_CHECK(callContractFunction("foo(uint256,uint16)", 10, 12) == encodeArgs(
+ dev::sha3(
+ toBigEndian(u256(10)) +
+ bytes{0x0, 0xc} +
+ bytes(1, 0x91))));
+}
+
+BOOST_AUTO_TEST_CASE(sha3_multiple_arguments_with_string_literals)
+{
+ char const* sourceCode = R"(
+ contract c {
+ function foo() returns (bytes32 d)
+ {
+ d = sha3("foo");
+ }
+ function bar(uint a, uint16 b) returns (bytes32 d)
+ {
+ d = sha3(a, b, 145, "foo");
+ }
+ })";
+ compileAndRun(sourceCode);
+
+ BOOST_CHECK(callContractFunction("foo()") == encodeArgs(dev::sha3("foo")));
+
+ BOOST_CHECK(callContractFunction("bar(uint256,uint16)", 10, 12) == encodeArgs(
+ dev::sha3(
+ toBigEndian(u256(10)) +
+ bytes{0x0, 0xc} +
+ bytes(1, 0x91) +
+ bytes{0x66, 0x6f, 0x6f})));
+}
+
+BOOST_AUTO_TEST_CASE(sha3_with_bytes)
+{
+ char const* sourceCode = R"(
+ contract c {
+ bytes data;
+ function foo() returns (bool)
+ {
+ data.length = 3;
+ data[0] = "f";
+ data[1] = "o";
+ data[2] = "o";
+ return sha3(data) == sha3("foo");
+ }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("foo()") == encodeArgs(true));
+}
+
+BOOST_AUTO_TEST_CASE(iterated_sha3_with_bytes)
+{
+ char const* sourceCode = R"(
+ contract c {
+ bytes data;
+ function foo() returns (bytes32)
+ {
+ data.length = 3;
+ data[0] = "x";
+ data[1] = "y";
+ data[2] = "z";
+ return sha3("b", sha3(data), "a");
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("foo()") == encodeArgs(
+ u256(dev::sha3(bytes{'b'} + dev::sha3("xyz").asBytes() + bytes{'a'}))
+ ));
+}
+
+BOOST_AUTO_TEST_CASE(generic_call)
+{
+ char const* sourceCode = R"**(
+ contract receiver {
+ uint public received;
+ function receive(uint256 x) { received = x; }
+ }
+ contract sender {
+ function doSend(address rec) returns (uint d)
+ {
+ bytes4 signature = bytes4(bytes32(sha3("receive(uint256)")));
+ rec.call.value(2)(signature, 23);
+ return receiver(rec).received();
+ }
+ }
+ )**";
+ compileAndRun(sourceCode, 0, "receiver");
+ u160 const c_receiverAddress = m_contractAddress;
+ compileAndRun(sourceCode, 50, "sender");
+ BOOST_REQUIRE(callContractFunction("doSend(address)", c_receiverAddress) == encodeArgs(23));
+ BOOST_CHECK_EQUAL(m_state.balance(m_contractAddress), 50 - 2);
+}
+
+BOOST_AUTO_TEST_CASE(generic_callcode)
+{
+ char const* sourceCode = R"**(
+ contract receiver {
+ uint public received;
+ function receive(uint256 x) { received = x; }
+ }
+ contract sender {
+ uint public received;
+ function doSend(address rec) returns (uint d)
+ {
+ bytes4 signature = bytes4(bytes32(sha3("receive(uint256)")));
+ rec.callcode.value(2)(signature, 23);
+ return receiver(rec).received();
+ }
+ }
+ )**";
+ compileAndRun(sourceCode, 0, "receiver");
+ u160 const c_receiverAddress = m_contractAddress;
+ compileAndRun(sourceCode, 50, "sender");
+ u160 const c_senderAddress = m_contractAddress;
+ BOOST_CHECK(callContractFunction("doSend(address)", c_receiverAddress) == encodeArgs(0));
+ BOOST_CHECK(callContractFunction("received()") == encodeArgs(23));
+ m_contractAddress = c_receiverAddress;
+ BOOST_CHECK(callContractFunction("received()") == encodeArgs(0));
+ BOOST_CHECK(m_state.storage(c_receiverAddress).empty());
+ BOOST_CHECK(!m_state.storage(c_senderAddress).empty());
+ BOOST_CHECK_EQUAL(m_state.balance(c_receiverAddress), 0);
+ BOOST_CHECK_EQUAL(m_state.balance(c_senderAddress), 50);
+}
+
+BOOST_AUTO_TEST_CASE(store_bytes)
+{
+ // this test just checks that the copy loop does not mess up the stack
+ char const* sourceCode = R"(
+ contract C {
+ function save() returns (uint r) {
+ r = 23;
+ savedData = msg.data;
+ r = 24;
+ }
+ bytes savedData;
+ }
+ )";
+ compileAndRun(sourceCode);
+ // empty copy loop
+ BOOST_CHECK(callContractFunction("save()") == encodeArgs(24));
+ BOOST_CHECK(callContractFunction("save()", "abcdefg") == encodeArgs(24));
+}
+
+BOOST_AUTO_TEST_CASE(bytes_from_calldata_to_memory)
+{
+ char const* sourceCode = R"(
+ contract C {
+ function() returns (bytes32) {
+ return sha3("abc", msg.data);
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ bytes calldata1 = bytes(61, 0x22) + bytes(12, 0x12);
+ sendMessage(calldata1, false);
+ BOOST_CHECK(m_output == encodeArgs(dev::sha3(bytes{'a', 'b', 'c'} + calldata1)));
+}
+
+BOOST_AUTO_TEST_CASE(call_forward_bytes)
+{
+ char const* sourceCode = R"(
+ contract receiver {
+ uint public received;
+ function receive(uint x) { received += x + 1; }
+ function() { received = 0x80; }
+ }
+ contract sender {
+ function sender() { rec = new receiver(); }
+ function() { savedData = msg.data; }
+ function forward() returns (bool) { rec.call(savedData); return true; }
+ function clear() returns (bool) { delete savedData; return true; }
+ function val() returns (uint) { return rec.received(); }
+ receiver rec;
+ bytes savedData;
+ }
+ )";
+ compileAndRun(sourceCode, 0, "sender");
+ BOOST_CHECK(callContractFunction("receive(uint256)", 7) == bytes());
+ BOOST_CHECK(callContractFunction("val()") == encodeArgs(0));
+ BOOST_CHECK(callContractFunction("forward()") == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("val()") == encodeArgs(8));
+ BOOST_CHECK(callContractFunction("clear()") == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("val()") == encodeArgs(8));
+ BOOST_CHECK(callContractFunction("forward()") == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("val()") == encodeArgs(0x80));
+}
+
+BOOST_AUTO_TEST_CASE(copying_bytes_multiassign)
+{
+ char const* sourceCode = R"(
+ contract receiver {
+ uint public received;
+ function receive(uint x) { received += x + 1; }
+ function() { received = 0x80; }
+ }
+ contract sender {
+ function sender() { rec = new receiver(); }
+ function() { savedData1 = savedData2 = msg.data; }
+ function forward(bool selector) returns (bool) {
+ if (selector) { rec.call(savedData1); delete savedData1; }
+ else { rec.call(savedData2); delete savedData2; }
+ return true;
+ }
+ function val() returns (uint) { return rec.received(); }
+ receiver rec;
+ bytes savedData1;
+ bytes savedData2;
+ }
+ )";
+ compileAndRun(sourceCode, 0, "sender");
+ BOOST_CHECK(callContractFunction("receive(uint256)", 7) == bytes());
+ BOOST_CHECK(callContractFunction("val()") == encodeArgs(0));
+ BOOST_CHECK(callContractFunction("forward(bool)", true) == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("val()") == encodeArgs(8));
+ BOOST_CHECK(callContractFunction("forward(bool)", false) == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("val()") == encodeArgs(16));
+ BOOST_CHECK(callContractFunction("forward(bool)", true) == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("val()") == encodeArgs(0x80));
+}
+
+BOOST_AUTO_TEST_CASE(delete_removes_bytes_data)
+{
+ char const* sourceCode = R"(
+ contract c {
+ function() { data = msg.data; }
+ function del() returns (bool) { delete data; return true; }
+ bytes data;
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("---", 7) == bytes());
+ BOOST_CHECK(!m_state.storage(m_contractAddress).empty());
+ BOOST_CHECK(callContractFunction("del()", 7) == encodeArgs(true));
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+}
+
+BOOST_AUTO_TEST_CASE(copy_from_calldata_removes_bytes_data)
+{
+ char const* sourceCode = R"(
+ contract c {
+ function set() returns (bool) { data = msg.data; return true; }
+ function() { data = msg.data; }
+ bytes data;
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("set()", 1, 2, 3, 4, 5) == encodeArgs(true));
+ BOOST_CHECK(!m_state.storage(m_contractAddress).empty());
+ sendMessage(bytes(), false);
+ BOOST_CHECK(m_output == bytes());
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+}
+
+BOOST_AUTO_TEST_CASE(copy_removes_bytes_data)
+{
+ char const* sourceCode = R"(
+ contract c {
+ function set() returns (bool) { data1 = msg.data; return true; }
+ function reset() returns (bool) { data1 = data2; return true; }
+ bytes data1;
+ bytes data2;
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("set()", 1, 2, 3, 4, 5) == encodeArgs(true));
+ BOOST_CHECK(!m_state.storage(m_contractAddress).empty());
+ BOOST_CHECK(callContractFunction("reset()") == encodeArgs(true));
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+}
+
+BOOST_AUTO_TEST_CASE(bytes_inside_mappings)
+{
+ char const* sourceCode = R"(
+ contract c {
+ function set(uint key) returns (bool) { data[key] = msg.data; return true; }
+ function copy(uint from, uint to) returns (bool) { data[to] = data[from]; return true; }
+ mapping(uint => bytes) data;
+ }
+ )";
+ compileAndRun(sourceCode);
+ // store a short byte array at 1 and a longer one at 2
+ BOOST_CHECK(callContractFunction("set(uint256)", 1, 2) == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("set(uint256)", 2, 2, 3, 4, 5) == encodeArgs(true));
+ BOOST_CHECK(!m_state.storage(m_contractAddress).empty());
+ // copy shorter to longer
+ BOOST_CHECK(callContractFunction("copy(uint256,uint256)", 1, 2) == encodeArgs(true));
+ BOOST_CHECK(!m_state.storage(m_contractAddress).empty());
+ // copy empty to both
+ BOOST_CHECK(callContractFunction("copy(uint256,uint256)", 99, 1) == encodeArgs(true));
+ BOOST_CHECK(!m_state.storage(m_contractAddress).empty());
+ BOOST_CHECK(callContractFunction("copy(uint256,uint256)", 99, 2) == encodeArgs(true));
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+}
+
+BOOST_AUTO_TEST_CASE(bytes_length_member)
+{
+ char const* sourceCode = R"(
+ contract c {
+ function set() returns (bool) { data = msg.data; return true; }
+ function getLength() returns (uint) { return data.length; }
+ bytes data;
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("getLength()") == encodeArgs(0));
+ BOOST_CHECK(callContractFunction("set()", 1, 2) == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("getLength()") == encodeArgs(4+32+32));
+}
+
+BOOST_AUTO_TEST_CASE(struct_copy)
+{
+ char const* sourceCode = R"(
+ contract c {
+ struct Nested { uint x; uint y; }
+ struct Struct { uint a; mapping(uint => Struct) b; Nested nested; uint c; }
+ mapping(uint => Struct) data;
+ function set(uint k) returns (bool) {
+ data[k].a = 1;
+ data[k].nested.x = 3;
+ data[k].nested.y = 4;
+ data[k].c = 2;
+ return true;
+ }
+ function copy(uint from, uint to) returns (bool) {
+ data[to] = data[from];
+ return true;
+ }
+ function retrieve(uint k) returns (uint a, uint x, uint y, uint c)
+ {
+ a = data[k].a;
+ x = data[k].nested.x;
+ y = data[k].nested.y;
+ c = data[k].c;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("set(uint256)", 7) == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("retrieve(uint256)", 7) == encodeArgs(1, 3, 4, 2));
+ BOOST_CHECK(callContractFunction("copy(uint256,uint256)", 7, 8) == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("retrieve(uint256)", 7) == encodeArgs(1, 3, 4, 2));
+ BOOST_CHECK(callContractFunction("retrieve(uint256)", 8) == encodeArgs(1, 3, 4, 2));
+ BOOST_CHECK(callContractFunction("copy(uint256,uint256)", 0, 7) == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("retrieve(uint256)", 7) == encodeArgs(0, 0, 0, 0));
+ BOOST_CHECK(callContractFunction("retrieve(uint256)", 8) == encodeArgs(1, 3, 4, 2));
+ BOOST_CHECK(callContractFunction("copy(uint256,uint256)", 7, 8) == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("retrieve(uint256)", 8) == encodeArgs(0, 0, 0, 0));
+}
+
+BOOST_AUTO_TEST_CASE(struct_containing_bytes_copy_and_delete)
+{
+ char const* sourceCode = R"(
+ contract c {
+ struct Struct { uint a; bytes data; uint b; }
+ Struct data1;
+ Struct data2;
+ function set(uint _a, bytes _data, uint _b) external returns (bool) {
+ data1.a = _a;
+ data1.b = _b;
+ data1.data = _data;
+ return true;
+ }
+ function copy() returns (bool) {
+ data1 = data2;
+ return true;
+ }
+ function del() returns (bool) {
+ delete data1;
+ return true;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ string data = "123456789012345678901234567890123";
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+ BOOST_CHECK(callContractFunction("set(uint256,bytes,uint256)", 12, u256(data.length()), 13, data) == encodeArgs(true));
+ BOOST_CHECK(!m_state.storage(m_contractAddress).empty());
+ BOOST_CHECK(callContractFunction("copy()") == encodeArgs(true));
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+ BOOST_CHECK(callContractFunction("set(uint256,bytes,uint256)", 12, u256(data.length()), 13, data) == encodeArgs(true));
+ BOOST_CHECK(!m_state.storage(m_contractAddress).empty());
+ BOOST_CHECK(callContractFunction("del()") == encodeArgs(true));
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+}
+
+BOOST_AUTO_TEST_CASE(struct_copy_via_local)
+{
+ char const* sourceCode = R"(
+ contract c {
+ struct Struct { uint a; uint b; }
+ Struct data1;
+ Struct data2;
+ function test() returns (bool) {
+ data1.a = 1;
+ data1.b = 2;
+ var x = data1;
+ data2 = x;
+ return data2.a == data1.a && data2.b == data1.b;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("test()") == encodeArgs(true));
+}
+
+BOOST_AUTO_TEST_CASE(using_enums)
+{
+ char const* sourceCode = R"(
+ contract test {
+ enum ActionChoices { GoLeft, GoRight, GoStraight, Sit }
+ function test()
+ {
+ choices = ActionChoices.GoStraight;
+ }
+ function getChoice() returns (uint d)
+ {
+ d = uint256(choices);
+ }
+ ActionChoices choices;
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("getChoice()") == encodeArgs(2));
+}
+
+BOOST_AUTO_TEST_CASE(constructing_enums_from_ints)
+{
+ char const* sourceCode = R"(
+ contract c {
+ enum Truth { False, True }
+ function test() returns (uint)
+ {
+ return uint(Truth(uint8(0x701)));
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("test()") == encodeArgs(1));
+}
+
+BOOST_AUTO_TEST_CASE(inline_member_init)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function test(){
+ m_b = 6;
+ m_c = 8;
+ }
+ uint m_a = 5;
+ uint m_b;
+ uint m_c = 7;
+ function get() returns (uint a, uint b, uint c){
+ a = m_a;
+ b = m_b;
+ c = m_c;
+ }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("get()") == encodeArgs(5, 6, 8));
+}
+
+BOOST_AUTO_TEST_CASE(inline_member_init_inheritence)
+{
+ char const* sourceCode = R"(
+ contract Base {
+ function Base(){}
+ uint m_base = 5;
+ function getBMember() returns (uint i) { return m_base; }
+ }
+ contract Derived is Base {
+ function Derived(){}
+ uint m_derived = 6;
+ function getDMember() returns (uint i) { return m_derived; }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("getBMember()") == encodeArgs(5));
+ BOOST_CHECK(callContractFunction("getDMember()") == encodeArgs(6));
+}
+
+BOOST_AUTO_TEST_CASE(inline_member_init_inheritence_without_constructor)
+{
+ char const* sourceCode = R"(
+ contract Base {
+ uint m_base = 5;
+ function getBMember() returns (uint i) { return m_base; }
+ }
+ contract Derived is Base {
+ uint m_derived = 6;
+ function getDMember() returns (uint i) { return m_derived; }
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("getBMember()") == encodeArgs(5));
+ BOOST_CHECK(callContractFunction("getDMember()") == encodeArgs(6));
+}
+
+BOOST_AUTO_TEST_CASE(external_function)
+{
+ char const* sourceCode = R"(
+ contract c {
+ function f(uint a) returns (uint) { return a; }
+ function test(uint a, uint b) external returns (uint r_a, uint r_b) {
+ r_a = f(a + 7);
+ r_b = b;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("test(uint256,uint256)", 2, 3) == encodeArgs(2+7, 3));
+}
+
+BOOST_AUTO_TEST_CASE(bytes_in_arguments)
+{
+ char const* sourceCode = R"(
+ contract c {
+ uint result;
+ function f(uint a, uint b) { result += a + b; }
+ function g(uint a) { result *= a; }
+ function test(uint a, bytes data1, bytes data2, uint b) external returns (uint r_a, uint r, uint r_b, uint l) {
+ r_a = a;
+ this.call(data1);
+ this.call(data2);
+ r = result;
+ r_b = b;
+ l = data1.length;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+
+ string innercalldata1 = asString(FixedHash<4>(dev::sha3("f(uint256,uint256)")).asBytes() + encodeArgs(8, 9));
+ string innercalldata2 = asString(FixedHash<4>(dev::sha3("g(uint256)")).asBytes() + encodeArgs(3));
+ bytes calldata = encodeArgs(
+ 12, 32 * 4, u256(32 * 4 + 32 + (innercalldata1.length() + 31) / 32 * 32), 13,
+ u256(innercalldata1.length()), innercalldata1,
+ u256(innercalldata2.length()), innercalldata2);
+ BOOST_CHECK(callContractFunction("test(uint256,bytes,bytes,uint256)", calldata)
+ == encodeArgs(12, (8 + 9) * 3, 13, u256(innercalldata1.length())));
+}
+
+BOOST_AUTO_TEST_CASE(fixed_arrays_in_storage)
+{
+ char const* sourceCode = R"(
+ contract c {
+ struct Data { uint x; uint y; }
+ Data[2**10] data;
+ uint[2**10 + 3] ids;
+ function setIDStatic(uint id) { ids[2] = id; }
+ function setID(uint index, uint id) { ids[index] = id; }
+ function setData(uint index, uint x, uint y) { data[index].x = x; data[index].y = y; }
+ function getID(uint index) returns (uint) { return ids[index]; }
+ function getData(uint index) returns (uint x, uint y) { x = data[index].x; y = data[index].y; }
+ function getLengths() returns (uint l1, uint l2) { l1 = data.length; l2 = ids.length; }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("setIDStatic(uint256)", 11) == bytes());
+ BOOST_CHECK(callContractFunction("getID(uint256)", 2) == encodeArgs(11));
+ BOOST_CHECK(callContractFunction("setID(uint256,uint256)", 7, 8) == bytes());
+ BOOST_CHECK(callContractFunction("getID(uint256)", 7) == encodeArgs(8));
+ BOOST_CHECK(callContractFunction("setData(uint256,uint256,uint256)", 7, 8, 9) == bytes());
+ BOOST_CHECK(callContractFunction("setData(uint256,uint256,uint256)", 8, 10, 11) == bytes());
+ BOOST_CHECK(callContractFunction("getData(uint256)", 7) == encodeArgs(8, 9));
+ BOOST_CHECK(callContractFunction("getData(uint256)", 8) == encodeArgs(10, 11));
+ BOOST_CHECK(callContractFunction("getLengths()") == encodeArgs(u256(1) << 10, (u256(1) << 10) + 3));
+}
+
+BOOST_AUTO_TEST_CASE(dynamic_arrays_in_storage)
+{
+ char const* sourceCode = R"(
+ contract c {
+ struct Data { uint x; uint y; }
+ Data[] data;
+ uint[] ids;
+ function setIDStatic(uint id) { ids[2] = id; }
+ function setID(uint index, uint id) { ids[index] = id; }
+ function setData(uint index, uint x, uint y) { data[index].x = x; data[index].y = y; }
+ function getID(uint index) returns (uint) { return ids[index]; }
+ function getData(uint index) returns (uint x, uint y) { x = data[index].x; y = data[index].y; }
+ function getLengths() returns (uint l1, uint l2) { l1 = data.length; l2 = ids.length; }
+ function setLengths(uint l1, uint l2) { data.length = l1; ids.length = l2; }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("getLengths()") == encodeArgs(0, 0));
+ BOOST_CHECK(callContractFunction("setLengths(uint256,uint256)", 48, 49) == bytes());
+ BOOST_CHECK(callContractFunction("getLengths()") == encodeArgs(48, 49));
+ BOOST_CHECK(callContractFunction("setIDStatic(uint256)", 11) == bytes());
+ BOOST_CHECK(callContractFunction("getID(uint256)", 2) == encodeArgs(11));
+ BOOST_CHECK(callContractFunction("setID(uint256,uint256)", 7, 8) == bytes());
+ BOOST_CHECK(callContractFunction("getID(uint256)", 7) == encodeArgs(8));
+ BOOST_CHECK(callContractFunction("setData(uint256,uint256,uint256)", 7, 8, 9) == bytes());
+ BOOST_CHECK(callContractFunction("setData(uint256,uint256,uint256)", 8, 10, 11) == bytes());
+ BOOST_CHECK(callContractFunction("getData(uint256)", 7) == encodeArgs(8, 9));
+ BOOST_CHECK(callContractFunction("getData(uint256)", 8) == encodeArgs(10, 11));
+}
+
+BOOST_AUTO_TEST_CASE(fixed_out_of_bounds_array_access)
+{
+ char const* sourceCode = R"(
+ contract c {
+ uint[4] data;
+ function set(uint index, uint value) returns (bool) { data[index] = value; return true; }
+ function get(uint index) returns (uint) { return data[index]; }
+ function length() returns (uint) { return data.length; }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("length()") == encodeArgs(4));
+ BOOST_CHECK(callContractFunction("set(uint256,uint256)", 3, 4) == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("set(uint256,uint256)", 4, 5) == bytes());
+ BOOST_CHECK(callContractFunction("set(uint256,uint256)", 400, 5) == bytes());
+ BOOST_CHECK(callContractFunction("get(uint256)", 3) == encodeArgs(4));
+ BOOST_CHECK(callContractFunction("get(uint256)", 4) == bytes());
+ BOOST_CHECK(callContractFunction("get(uint256)", 400) == bytes());
+ BOOST_CHECK(callContractFunction("length()") == encodeArgs(4));
+}
+
+BOOST_AUTO_TEST_CASE(dynamic_out_of_bounds_array_access)
+{
+ char const* sourceCode = R"(
+ contract c {
+ uint[] data;
+ function enlarge(uint amount) returns (uint) { return data.length += amount; }
+ function set(uint index, uint value) returns (bool) { data[index] = value; return true; }
+ function get(uint index) returns (uint) { return data[index]; }
+ function length() returns (uint) { return data.length; }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("length()") == encodeArgs(0));
+ BOOST_CHECK(callContractFunction("get(uint256)", 3) == bytes());
+ BOOST_CHECK(callContractFunction("enlarge(uint256)", 4) == encodeArgs(4));
+ BOOST_CHECK(callContractFunction("length()") == encodeArgs(4));
+ BOOST_CHECK(callContractFunction("set(uint256,uint256)", 3, 4) == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("get(uint256)", 3) == encodeArgs(4));
+ BOOST_CHECK(callContractFunction("length()") == encodeArgs(4));
+ BOOST_CHECK(callContractFunction("set(uint256,uint256)", 4, 8) == bytes());
+ BOOST_CHECK(callContractFunction("length()") == encodeArgs(4));
+}
+
+BOOST_AUTO_TEST_CASE(fixed_array_cleanup)
+{
+ char const* sourceCode = R"(
+ contract c {
+ uint spacer1;
+ uint spacer2;
+ uint[20] data;
+ function fill() {
+ for (uint i = 0; i < data.length; ++i) data[i] = i+1;
+ }
+ function clear() { delete data; }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+ BOOST_CHECK(callContractFunction("fill()") == bytes());
+ BOOST_CHECK(!m_state.storage(m_contractAddress).empty());
+ BOOST_CHECK(callContractFunction("clear()") == bytes());
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+}
+
+BOOST_AUTO_TEST_CASE(short_fixed_array_cleanup)
+{
+ char const* sourceCode = R"(
+ contract c {
+ uint spacer1;
+ uint spacer2;
+ uint[3] data;
+ function fill() {
+ for (uint i = 0; i < data.length; ++i) data[i] = i+1;
+ }
+ function clear() { delete data; }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+ BOOST_CHECK(callContractFunction("fill()") == bytes());
+ BOOST_CHECK(!m_state.storage(m_contractAddress).empty());
+ BOOST_CHECK(callContractFunction("clear()") == bytes());
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+}
+
+BOOST_AUTO_TEST_CASE(dynamic_array_cleanup)
+{
+ char const* sourceCode = R"(
+ contract c {
+ uint[20] spacer;
+ uint[] dynamic;
+ function fill() {
+ dynamic.length = 21;
+ for (uint i = 0; i < dynamic.length; ++i) dynamic[i] = i+1;
+ }
+ function halfClear() { dynamic.length = 5; }
+ function fullClear() { delete dynamic; }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+ BOOST_CHECK(callContractFunction("fill()") == bytes());
+ BOOST_CHECK(!m_state.storage(m_contractAddress).empty());
+ BOOST_CHECK(callContractFunction("halfClear()") == bytes());
+ BOOST_CHECK(!m_state.storage(m_contractAddress).empty());
+ BOOST_CHECK(callContractFunction("fullClear()") == bytes());
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+}
+
+BOOST_AUTO_TEST_CASE(dynamic_multi_array_cleanup)
+{
+ char const* sourceCode = R"(
+ contract c {
+ struct s { uint[][] d; }
+ s[] data;
+ function fill() returns (uint) {
+ data.length = 3;
+ data[2].d.length = 4;
+ data[2].d[3].length = 5;
+ data[2].d[3][4] = 8;
+ return data[2].d[3][4];
+ }
+ function clear() { delete data; }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+ BOOST_CHECK(callContractFunction("fill()") == encodeArgs(8));
+ BOOST_CHECK(!m_state.storage(m_contractAddress).empty());
+ BOOST_CHECK(callContractFunction("clear()") == bytes());
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+}
+
+BOOST_AUTO_TEST_CASE(array_copy_storage_storage_dyn_dyn)
+{
+ char const* sourceCode = R"(
+ contract c {
+ uint[] data1;
+ uint[] data2;
+ function setData1(uint length, uint index, uint value) {
+ data1.length = length; if (index < length) data1[index] = value;
+ }
+ function copyStorageStorage() { data2 = data1; }
+ function getData2(uint index) returns (uint len, uint val) {
+ len = data2.length; if (index < len) val = data2[index];
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("setData1(uint256,uint256,uint256)", 10, 5, 4) == bytes());
+ BOOST_CHECK(callContractFunction("copyStorageStorage()") == bytes());
+ BOOST_CHECK(callContractFunction("getData2(uint256)", 5) == encodeArgs(10, 4));
+ BOOST_CHECK(callContractFunction("setData1(uint256,uint256,uint256)", 0, 0, 0) == bytes());
+ BOOST_CHECK(callContractFunction("copyStorageStorage()") == bytes());
+ BOOST_CHECK(callContractFunction("getData2(uint256)", 0) == encodeArgs(0, 0));
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+}
+
+BOOST_AUTO_TEST_CASE(array_copy_storage_storage_static_static)
+{
+ char const* sourceCode = R"(
+ contract c {
+ uint[40] data1;
+ uint[20] data2;
+ function test() returns (uint x, uint y){
+ data1[30] = 4;
+ data1[2] = 7;
+ data1[3] = 9;
+ data2[3] = 8;
+ data1 = data2;
+ x = data1[3];
+ y = data1[30]; // should be cleared
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("test()") == encodeArgs(8, 0));
+}
+
+BOOST_AUTO_TEST_CASE(array_copy_storage_storage_static_dynamic)
+{
+ char const* sourceCode = R"(
+ contract c {
+ uint[9] data1;
+ uint[] data2;
+ function test() returns (uint x, uint y){
+ data1[8] = 4;
+ data2 = data1;
+ x = data2.length;
+ y = data2[8];
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("test()") == encodeArgs(9, 4));
+}
+
+BOOST_AUTO_TEST_CASE(array_copy_different_packing)
+{
+ char const* sourceCode = R"(
+ contract c {
+ bytes8[] data1; // 4 per slot
+ bytes10[] data2; // 3 per slot
+ function test() returns (bytes10 a, bytes10 b, bytes10 c, bytes10 d, bytes10 e) {
+ data1.length = 9;
+ for (uint i = 0; i < data1.length; ++i)
+ data1[i] = bytes8(i);
+ data2 = data1;
+ a = data2[1];
+ b = data2[2];
+ c = data2[3];
+ d = data2[4];
+ e = data2[5];
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("test()") == encodeArgs(
+ asString(fromHex("0000000000000001")),
+ asString(fromHex("0000000000000002")),
+ asString(fromHex("0000000000000003")),
+ asString(fromHex("0000000000000004")),
+ asString(fromHex("0000000000000005"))
+ ));
+}
+
+BOOST_AUTO_TEST_CASE(array_copy_target_simple)
+{
+ char const* sourceCode = R"(
+ contract c {
+ bytes8[9] data1; // 4 per slot
+ bytes17[10] data2; // 1 per slot, no offset counter
+ function test() returns (bytes17 a, bytes17 b, bytes17 c, bytes17 d, bytes17 e) {
+ for (uint i = 0; i < data1.length; ++i)
+ data1[i] = bytes8(i);
+ data2[8] = data2[9] = 2;
+ data2 = data1;
+ a = data2[1];
+ b = data2[2];
+ c = data2[3];
+ d = data2[4];
+ e = data2[9];
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("test()") == encodeArgs(
+ asString(fromHex("0000000000000001")),
+ asString(fromHex("0000000000000002")),
+ asString(fromHex("0000000000000003")),
+ asString(fromHex("0000000000000004")),
+ asString(fromHex("0000000000000000"))
+ ));
+}
+
+BOOST_AUTO_TEST_CASE(array_copy_target_leftover)
+{
+ // test that leftover elements in the last slot of target are correctly cleared during assignment
+ char const* sourceCode = R"(
+ contract c {
+ byte[10] data1;
+ bytes2[32] data2;
+ function test() returns (uint check, uint res1, uint res2) {
+ uint i;
+ for (i = 0; i < data2.length; ++i)
+ data2[i] = 0xffff;
+ check = uint(data2[31]) * 0x10000 | uint(data2[14]);
+ for (i = 0; i < data1.length; ++i)
+ data1[i] = byte(uint8(1 + i));
+ data2 = data1;
+ for (i = 0; i < 16; ++i)
+ res1 |= uint(data2[i]) * 0x10000**i;
+ for (i = 0; i < 16; ++i)
+ res2 |= uint(data2[16 + i]) * 0x10000**i;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("test()") == encodeArgs(
+ u256("0xffffffff"),
+ asString(fromHex("0000000000000000""000000000a000900""0800070006000500""0400030002000100")),
+ asString(fromHex("0000000000000000""0000000000000000""0000000000000000""0000000000000000"))
+ ));
+}
+
+BOOST_AUTO_TEST_CASE(array_copy_target_leftover2)
+{
+ // since the copy always copies whole slots, we have to make sure that the source size maxes
+ // out a whole slot and at the same time there are still elements left in the target at that point
+ char const* sourceCode = R"(
+ contract c {
+ bytes8[4] data1; // fits into one slot
+ bytes10[6] data2; // 4 elements need two slots
+ function test() returns (bytes10 r1, bytes10 r2, bytes10 r3) {
+ data1[0] = 1;
+ data1[1] = 2;
+ data1[2] = 3;
+ data1[3] = 4;
+ for (uint i = 0; i < data2.length; ++i)
+ data2[i] = bytes10(0xffff00 | (1 + i));
+ data2 = data1;
+ r1 = data2[3];
+ r2 = data2[4];
+ r3 = data2[5];
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("test()") == encodeArgs(
+ asString(fromHex("0000000000000004")),
+ asString(fromHex("0000000000000000")),
+ asString(fromHex("0000000000000000"))
+ ));
+}
+BOOST_AUTO_TEST_CASE(array_copy_storage_storage_struct)
+{
+ char const* sourceCode = R"(
+ contract c {
+ struct Data { uint x; uint y; }
+ Data[] data1;
+ Data[] data2;
+ function test() returns (uint x, uint y) {
+ data1.length = 9;
+ data1[8].x = 4;
+ data1[8].y = 5;
+ data2 = data1;
+ x = data2[8].x;
+ y = data2[8].y;
+ data1.length = 0;
+ data2 = data1;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("test()") == encodeArgs(4, 5));
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+}
+
+BOOST_AUTO_TEST_CASE(external_array_args)
+{
+ char const* sourceCode = R"(
+ contract c {
+ function test(uint[8] a, uint[] b, uint[5] c, uint a_index, uint b_index, uint c_index)
+ external returns (uint av, uint bv, uint cv) {
+ av = a[a_index];
+ bv = b[b_index];
+ cv = c[c_index];
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ bytes params = encodeArgs(
+ 1, 2, 3, 4, 5, 6, 7, 8, // a
+ 32 * (8 + 1 + 5 + 1 + 1 + 1), // offset to b
+ 21, 22, 23, 24, 25, // c
+ 0, 1, 2, // (a,b,c)_index
+ 3, // b.length
+ 11, 12, 13 // b
+ );
+ BOOST_CHECK(callContractFunction("test(uint256[8],uint256[],uint256[5],uint256,uint256,uint256)", params) == encodeArgs(1, 12, 23));
+}
+
+BOOST_AUTO_TEST_CASE(bytes_index_access)
+{
+ char const* sourceCode = R"(
+ contract c {
+ bytes data;
+ function direct(bytes arg, uint index) external returns (uint) {
+ return uint(arg[index]);
+ }
+ function storageCopyRead(bytes arg, uint index) external returns (uint) {
+ data = arg;
+ return uint(data[index]);
+ }
+ function storageWrite() external returns (uint) {
+ data.length = 35;
+ data[31] = 0x77;
+ data[32] = 0x14;
+
+ data[31] = 1;
+ data[31] |= 8;
+ data[30] = 1;
+ data[32] = 3;
+ return uint(data[30]) * 0x100 | uint(data[31]) * 0x10 | uint(data[32]);
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ string array{
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
+ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
+ 30, 31, 32, 33};
+ BOOST_CHECK(callContractFunction("direct(bytes,uint256)", 64, 33, u256(array.length()), array) == encodeArgs(33));
+ BOOST_CHECK(callContractFunction("storageCopyRead(bytes,uint256)", 64, 33, u256(array.length()), array) == encodeArgs(33));
+ BOOST_CHECK(callContractFunction("storageWrite()") == encodeArgs(0x193));
+}
+
+BOOST_AUTO_TEST_CASE(bytes_delete_element)
+{
+ char const* sourceCode = R"(
+ contract c {
+ bytes data;
+ function test1() external returns (bool) {
+ data.length = 100;
+ for (uint i = 0; i < data.length; i++)
+ data[i] = byte(i);
+ delete data[94];
+ delete data[96];
+ delete data[98];
+ return data[94] == 0 && data[95] == 95 && data[96] == 0 && data[97] == 97;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("test1()") == encodeArgs(true));
+}
+
+BOOST_AUTO_TEST_CASE(array_copy_calldata_storage)
+{
+ char const* sourceCode = R"(
+ contract c {
+ uint[9] m_data;
+ uint[] m_data_dyn;
+ uint8[][] m_byte_data;
+ function store(uint[9] a, uint8[3][] b) external returns (uint8) {
+ m_data = a;
+ m_data_dyn = a;
+ m_byte_data = b;
+ return b[3][1]; // note that access and declaration are reversed to each other
+ }
+ function retrieve() returns (uint a, uint b, uint c, uint d, uint e, uint f, uint g) {
+ a = m_data.length;
+ b = m_data[7];
+ c = m_data_dyn.length;
+ d = m_data_dyn[7];
+ e = m_byte_data.length;
+ f = m_byte_data[3].length;
+ g = m_byte_data[3][1];
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("store(uint256[9],uint8[3][])", encodeArgs(
+ 21, 22, 23, 24, 25, 26, 27, 28, 29, // a
+ u256(32 * (9 + 1)),
+ 4, // size of b
+ 1, 2, 3, // b[0]
+ 11, 12, 13, // b[1]
+ 21, 22, 23, // b[2]
+ 31, 32, 33 // b[3]
+ )) == encodeArgs(32));
+ BOOST_CHECK(callContractFunction("retrieve()") == encodeArgs(
+ 9, 28, 9, 28,
+ 4, 3, 32));
+}
+
+BOOST_AUTO_TEST_CASE(array_copy_nested_array)
+{
+ char const* sourceCode = R"(
+ contract c {
+ uint[4][] a;
+ uint[10][] b;
+ uint[][] c;
+ function test(uint[2][] d) external returns (uint) {
+ a = d;
+ b = a;
+ c = b;
+ return c[1][1] | c[1][2] | c[1][3] | c[1][4];
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("test(uint256[2][])", encodeArgs(
+ 32, 3,
+ 7, 8,
+ 9, 10,
+ 11, 12
+ )) == encodeArgs(10));
+}
+
+BOOST_AUTO_TEST_CASE(array_copy_including_mapping)
+{
+ char const* sourceCode = R"(
+ contract c {
+ mapping(uint=>uint)[90][] large;
+ mapping(uint=>uint)[3][] small;
+ function test() returns (uint r) {
+ large.length = small.length = 7;
+ large[3][2][0] = 2;
+ large[1] = large[3];
+ small[3][2][0] = 2;
+ small[1] = small[2];
+ r = ((
+ small[3][2][0] * 0x100 |
+ small[1][2][0]) * 0x100 |
+ large[3][2][0]) * 0x100 |
+ large[1][2][0];
+ delete small;
+ delete large;
+ }
+ function clear() returns (uint r) {
+ large.length = small.length = 7;
+ small[3][2][0] = 0;
+ large[3][2][0] = 0;
+ small.length = large.length = 0;
+ return 7;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("test()") == encodeArgs(0x02000200));
+ // storage is not empty because we cannot delete the mappings
+ BOOST_CHECK(!m_state.storage(m_contractAddress).empty());
+ BOOST_CHECK(callContractFunction("clear()") == encodeArgs(7));
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+}
+
+BOOST_AUTO_TEST_CASE(pass_dynamic_arguments_to_the_base)
+{
+ char const* sourceCode = R"(
+ contract Base {
+ function Base(uint i)
+ {
+ m_i = i;
+ }
+ uint public m_i;
+ }
+ contract Derived is Base(2) {
+ function Derived(uint i) Base(i)
+ {}
+ }
+ contract Final is Derived(4) {
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("m_i()") == encodeArgs(4));
+}
+
+BOOST_AUTO_TEST_CASE(pass_dynamic_arguments_to_the_base_base)
+{
+ char const* sourceCode = R"(
+ contract Base {
+ function Base(uint j)
+ {
+ m_i = j;
+ }
+ uint public m_i;
+ }
+ contract Base1 is Base(3) {
+ function Base1(uint k) Base(k*k) {}
+ }
+ contract Derived is Base(3), Base1(2) {
+ function Derived(uint i) Base(i) Base1(i)
+ {}
+ }
+ contract Final is Derived(4) {
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("m_i()") == encodeArgs(4));
+}
+
+BOOST_AUTO_TEST_CASE(pass_dynamic_arguments_to_the_base_base_with_gap)
+{
+ char const* sourceCode = R"(
+ contract Base {
+ function Base(uint i)
+ {
+ m_i = i;
+ }
+ uint public m_i;
+ }
+ contract Base1 is Base(3) {}
+ contract Derived is Base(2), Base1 {
+ function Derived(uint i) Base(i) {}
+ }
+ contract Final is Derived(4) {
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("m_i()") == encodeArgs(4));
+}
+
+BOOST_AUTO_TEST_CASE(simple_constant_variables_test)
+{
+ char const* sourceCode = R"(
+ contract Foo {
+ function getX() returns (uint r) { return x; }
+ uint constant x = 56;
+ })";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("getX()") == encodeArgs(56));
+}
+
+BOOST_AUTO_TEST_CASE(constant_variables)
+{
+ //for now constant specifier is valid only for uint bytesXX and enums
+ char const* sourceCode = R"(
+ contract Foo {
+ uint constant x = 56;
+ enum ActionChoices { GoLeft, GoRight, GoStraight, Sit }
+ ActionChoices constant choices = ActionChoices.GoLeft;
+ bytes32 constant st = "abc\x00\xff__";
+ })";
+ compileAndRun(sourceCode);
+}
+
+BOOST_AUTO_TEST_CASE(packed_storage_structs_uint)
+{
+ char const* sourceCode = R"(
+ contract C {
+ struct str { uint8 a; uint16 b; uint248 c; }
+ str data;
+ function test() returns (uint) {
+ data.a = 2;
+ if (data.a != 2) return 2;
+ data.b = 0xabcd;
+ if (data.b != 0xabcd) return 3;
+ data.c = 0x1234567890;
+ if (data.c != 0x1234567890) return 4;
+ if (data.a != 2) return 5;
+ data.a = 8;
+ if (data.a != 8) return 6;
+ if (data.b != 0xabcd) return 7;
+ data.b = 0xdcab;
+ if (data.b != 0xdcab) return 8;
+ if (data.c != 0x1234567890) return 9;
+ data.c = 0x9876543210;
+ if (data.c != 0x9876543210) return 10;
+ return 1;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("test()") == encodeArgs(1));
+}
+
+BOOST_AUTO_TEST_CASE(packed_storage_structs_enum)
+{
+ char const* sourceCode = R"(
+ contract C {
+ enum small { A, B, C, D }
+ enum larger { A, B, C, D, E}
+ struct str { small a; small b; larger c; larger d; }
+ str data;
+ function test() returns (uint) {
+ data.a = small.B;
+ if (data.a != small.B) return 2;
+ data.b = small.C;
+ if (data.b != small.C) return 3;
+ data.c = larger.D;
+ if (data.c != larger.D) return 4;
+ if (data.a != small.B) return 5;
+ data.a = small.C;
+ if (data.a != small.C) return 6;
+ if (data.b != small.C) return 7;
+ data.b = small.D;
+ if (data.b != small.D) return 8;
+ if (data.c != larger.D) return 9;
+ data.c = larger.B;
+ if (data.c != larger.B) return 10;
+ return 1;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("test()") == encodeArgs(1));
+}
+
+BOOST_AUTO_TEST_CASE(packed_storage_structs_bytes)
+{
+ char const* sourceCode = R"(
+ contract C {
+ struct s1 { byte a; byte b; bytes10 c; bytes9 d; bytes10 e; }
+ struct s2 { byte a; s1 inner; byte b; byte c; }
+ byte x;
+ s2 data;
+ byte y;
+ function test() returns (bool) {
+ x = 1;
+ data.a = 2;
+ data.inner.a = 3;
+ data.inner.b = 4;
+ data.inner.c = "1234567890";
+ data.inner.d = "123456789";
+ data.inner.e = "abcdefghij";
+ data.b = 5;
+ data.c = 6;
+ y = 7;
+ return x == 1 && data.a == 2 && data.inner.a == 3 && data.inner.b == 4 &&
+ data.inner.c == "1234567890" && data.inner.d == "123456789" &&
+ data.inner.e == "abcdefghij" && data.b == 5 && data.c == 6 && y == 7;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("test()") == encodeArgs(true));
+}
+
+BOOST_AUTO_TEST_CASE(packed_storage_structs_delete)
+{
+ char const* sourceCode = R"(
+ contract C {
+ struct str { uint8 a; uint16 b; uint8 c; }
+ uint8 x;
+ uint16 y;
+ str data;
+ function test() returns (uint) {
+ x = 1;
+ y = 2;
+ data.a = 2;
+ data.b = 0xabcd;
+ data.c = 0xfa;
+ if (x != 1 || y != 2 || data.a != 2 || data.b != 0xabcd || data.c != 0xfa)
+ return 2;
+ delete y;
+ delete data.b;
+ if (x != 1 || y != 0 || data.a != 2 || data.b != 0 || data.c != 0xfa)
+ return 3;
+ delete x;
+ delete data;
+ return 1;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("test()") == encodeArgs(1));
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+}
+
+BOOST_AUTO_TEST_CASE(overloaded_function_call_resolve_to_first)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f(uint k) returns(uint d) { return k; }
+ function f(uint a, uint b) returns(uint d) { return a + b; }
+ function g() returns(uint d) { return f(3); }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("g()") == encodeArgs(3));
+}
+
+BOOST_AUTO_TEST_CASE(overloaded_function_call_resolve_to_second)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f(uint a, uint b) returns(uint d) { return a + b; }
+ function f(uint k) returns(uint d) { return k; }
+ function g() returns(uint d) { return f(3, 7); }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("g()") == encodeArgs(10));
+}
+
+BOOST_AUTO_TEST_CASE(overloaded_function_call_with_if_else)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f(uint a, uint b) returns(uint d) { return a + b; }
+ function f(uint k) returns(uint d) { return k; }
+ function g(bool flag) returns(uint d) {
+ if (flag)
+ return f(3);
+ else
+ return f(3, 7);
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("g(bool)", true) == encodeArgs(3));
+ BOOST_CHECK(callContractFunction("g(bool)", false) == encodeArgs(10));
+}
+
+BOOST_AUTO_TEST_CASE(derived_overload_base_function_direct)
+{
+ char const* sourceCode = R"(
+ contract B { function f() returns(uint) { return 10; } }
+ contract C is B {
+ function f(uint i) returns(uint) { return 2 * i; }
+ function g() returns(uint) { return f(1); }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "C");
+ BOOST_CHECK(callContractFunction("g()") == encodeArgs(2));
+}
+
+BOOST_AUTO_TEST_CASE(derived_overload_base_function_indirect)
+{
+ char const* sourceCode = R"(
+ contract A { function f(uint a) returns(uint) { return 2 * a; } }
+ contract B { function f() returns(uint) { return 10; } }
+ contract C is A, B {
+ function g() returns(uint) { return f(); }
+ function h() returns(uint) { return f(1); }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "C");
+ BOOST_CHECK(callContractFunction("g()") == encodeArgs(10));
+ BOOST_CHECK(callContractFunction("h()") == encodeArgs(2));
+}
+
+BOOST_AUTO_TEST_CASE(super_overload)
+{
+ char const* sourceCode = R"(
+ contract A { function f(uint a) returns(uint) { return 2 * a; } }
+ contract B { function f(bool b) returns(uint) { return 10; } }
+ contract C is A, B {
+ function g() returns(uint) { return super.f(true); }
+ function h() returns(uint) { return super.f(1); }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "C");
+ BOOST_CHECK(callContractFunction("g()") == encodeArgs(10));
+ BOOST_CHECK(callContractFunction("h()") == encodeArgs(2));
+}
+
+BOOST_AUTO_TEST_CASE(packed_storage_signed)
+{
+ char const* sourceCode = R"(
+ contract C {
+ int8 a;
+ uint8 b;
+ int8 c;
+ uint8 d;
+ function test() returns (uint x1, uint x2, uint x3, uint x4) {
+ a = -2;
+ b = -uint8(a) * 2;
+ c = a * int8(120) * int8(121);
+ x1 = uint(a);
+ x2 = b;
+ x3 = uint(c);
+ x4 = d;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK( callContractFunction("test()") == encodeArgs(u256(-2), u256(4), u256(-112), u256(0)));
+}
+
+BOOST_AUTO_TEST_CASE(external_types_in_calls)
+{
+ char const* sourceCode = R"(
+ contract C1 { C1 public bla; function C1(C1 x) { bla = x; } }
+ contract C {
+ function test() returns (C1 x, C1 y) {
+ C1 c = new C1(C1(9));
+ x = c.bla();
+ y = this.t1(C1(7));
+ }
+ function t1(C1 a) returns (C1) { return a; }
+ function() returns (C1) { return C1(9); }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "C");
+ BOOST_CHECK(callContractFunction("test()") == encodeArgs(u256(9), u256(7)));
+ BOOST_CHECK(callContractFunction("nonexisting") == encodeArgs(u256(9)));
+}
+
+BOOST_AUTO_TEST_CASE(proper_order_of_overwriting_of_attributes)
+{
+ // bug #1798
+ char const* sourceCode = R"(
+ contract init {
+ function isOk() returns (bool) { return false; }
+ bool public ok = false;
+ }
+ contract fix {
+ function isOk() returns (bool) { return true; }
+ bool public ok = true;
+ }
+
+ contract init_fix is init, fix {
+ function checkOk() returns (bool) { return ok; }
+ }
+ contract fix_init is fix, init {
+ function checkOk() returns (bool) { return ok; }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "init_fix");
+ BOOST_CHECK(callContractFunction("isOk()") == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("ok()") == encodeArgs(true));
+
+ compileAndRun(sourceCode, 0, "fix_init");
+ BOOST_CHECK(callContractFunction("isOk()") == encodeArgs(false));
+ BOOST_CHECK(callContractFunction("ok()") == encodeArgs(false));
+}
+
+BOOST_AUTO_TEST_CASE(proper_overwriting_accessor_by_function)
+{
+ // bug #1798
+ char const* sourceCode = R"(
+ contract attribute {
+ bool ok = false;
+ }
+ contract func {
+ function ok() returns (bool) { return true; }
+ }
+
+ contract attr_func is attribute, func {
+ function checkOk() returns (bool) { return ok(); }
+ }
+ contract func_attr is func, attribute {
+ function checkOk() returns (bool) { return ok; }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "attr_func");
+ BOOST_CHECK(callContractFunction("ok()") == encodeArgs(true));
+ compileAndRun(sourceCode, 0, "func_attr");
+ BOOST_CHECK(callContractFunction("checkOk()") == encodeArgs(false));
+}
+
+
+BOOST_AUTO_TEST_CASE(overwriting_inheritance)
+{
+ // bug #1798
+ char const* sourceCode = R"(
+ contract A {
+ function ok() returns (uint) { return 1; }
+ }
+ contract B {
+ function ok() returns (uint) { return 2; }
+ }
+ contract C {
+ uint ok = 6;
+ }
+ contract AB is A, B {
+ function ok() returns (uint) { return 4; }
+ }
+ contract reversedE is C, AB {
+ function checkOk() returns (uint) { return ok(); }
+ }
+ contract E is AB, C {
+ function checkOk() returns (uint) { return ok; }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "reversedE");
+ BOOST_CHECK(callContractFunction("checkOk()") == encodeArgs(4));
+ compileAndRun(sourceCode, 0, "E");
+ BOOST_CHECK(callContractFunction("checkOk()") == encodeArgs(6));
+}
+
+BOOST_AUTO_TEST_CASE(struct_assign_reference_to_struct)
+{
+ char const* sourceCode = R"(
+ contract test {
+ struct testStruct
+ {
+ uint m_value;
+ }
+ testStruct data1;
+ testStruct data2;
+ testStruct data3;
+ function test()
+ {
+ data1.m_value = 2;
+ }
+ function assign() returns (uint ret_local, uint ret_global, uint ret_global3, uint ret_global1)
+ {
+ testStruct x = data1; //x is a reference data1.m_value == 2 as well as x.m_value = 2
+ data2 = data1; // should copy data. data2.m_value == 2
+
+ ret_local = x.m_value; // = 2
+ ret_global = data2.m_value; // = 2
+
+ x.m_value = 3;
+ data3 = x; //should copy the data. data3.m_value == 3
+ ret_global3 = data3.m_value; // = 3
+ ret_global1 = data1.m_value; // = 3. Changed due to the assignment to x.m_value
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "test");
+ BOOST_CHECK(callContractFunction("assign()") == encodeArgs(2, 2, 3, 3));
+}
+
+BOOST_AUTO_TEST_CASE(struct_delete_member)
+{
+ char const* sourceCode = R"(
+ contract test {
+ struct testStruct
+ {
+ uint m_value;
+ }
+ testStruct data1;
+ function test()
+ {
+ data1.m_value = 2;
+ }
+ function deleteMember() returns (uint ret_value)
+ {
+ testStruct x = data1; //should not copy the data. data1.m_value == 2 but x.m_value = 0
+ x.m_value = 4;
+ delete x.m_value;
+ ret_value = data1.m_value;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "test");
+ BOOST_CHECK(callContractFunction("deleteMember()") == encodeArgs(0));
+}
+
+BOOST_AUTO_TEST_CASE(struct_delete_struct_in_mapping)
+{
+ char const* sourceCode = R"(
+ contract test {
+ struct testStruct
+ {
+ uint m_value;
+ }
+ mapping (uint => testStruct) campaigns;
+
+ function test()
+ {
+ campaigns[0].m_value = 2;
+ }
+ function deleteIt() returns (uint)
+ {
+ delete campaigns[0];
+ return campaigns[0].m_value;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "test");
+ BOOST_CHECK(callContractFunction("deleteIt()") == encodeArgs(0));
+}
+
+BOOST_AUTO_TEST_CASE(evm_exceptions_out_of_band_access)
+{
+ char const* sourceCode = R"(
+ contract A {
+ uint[3] arr;
+ bool public test = false;
+ function getElement(uint i) returns (uint)
+ {
+ return arr[i];
+ }
+ function testIt() returns (bool)
+ {
+ uint i = this.getElement(5);
+ test = true;
+ return true;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "A");
+ BOOST_CHECK(callContractFunction("test()") == encodeArgs(false));
+ BOOST_CHECK(callContractFunction("testIt()") == encodeArgs());
+ BOOST_CHECK(callContractFunction("test()") == encodeArgs(false));
+}
+
+BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor_call_fail)
+{
+ char const* sourceCode = R"(
+ contract A {
+ function A()
+ {
+ this.call("123");
+ }
+ }
+ contract B {
+ uint public test = 1;
+ function testIt()
+ {
+ A a = new A();
+ ++test;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "B");
+
+ BOOST_CHECK(callContractFunction("testIt()") == encodeArgs());
+ BOOST_CHECK(callContractFunction("test()") == encodeArgs(2));
+}
+
+BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor_out_of_baund)
+{
+ char const* sourceCode = R"(
+ contract A {
+ uint public test = 1;
+ uint[3] arr;
+ function A()
+ {
+ test = arr[5];
+ ++test;
+ }
+ }
+ )";
+ BOOST_CHECK(compileAndRunWithoutCheck(sourceCode, 0, "A").empty());
+}
+
+BOOST_AUTO_TEST_CASE(positive_integers_to_signed)
+{
+ char const* sourceCode = R"(
+ contract test {
+ int8 public x = 2;
+ int8 public y = 127;
+ int16 public q = 250;
+ }
+ )";
+ compileAndRun(sourceCode, 0, "test");
+ BOOST_CHECK(callContractFunction("x()") == encodeArgs(2));
+ BOOST_CHECK(callContractFunction("y()") == encodeArgs(127));
+ BOOST_CHECK(callContractFunction("q()") == encodeArgs(250));
+}
+
+BOOST_AUTO_TEST_CASE(failing_send)
+{
+ char const* sourceCode = R"(
+ contract Helper {
+ uint[] data;
+ function () {
+ data[9]; // trigger exception
+ }
+ }
+ contract Main {
+ function callHelper(address _a) returns (bool r, uint bal) {
+ r = !_a.send(5);
+ bal = this.balance;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Helper");
+ u160 const c_helperAddress = m_contractAddress;
+ compileAndRun(sourceCode, 20, "Main");
+ BOOST_REQUIRE(callContractFunction("callHelper(address)", c_helperAddress) == encodeArgs(true, 20));
+}
+
+BOOST_AUTO_TEST_CASE(reusing_memory)
+{
+ // Invoke some features that use memory and test that they do not interfere with each other.
+ char const* sourceCode = R"(
+ contract Helper {
+ uint public flag;
+ function Helper(uint x) {
+ flag = x;
+ }
+ }
+ contract Main {
+ mapping(uint => uint) map;
+ function f(uint x) returns (uint) {
+ map[x] = x;
+ return (new Helper(uint(sha3(this.g(map[x]))))).flag();
+ }
+ function g(uint a) returns (uint)
+ {
+ return map[a];
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Main");
+ BOOST_REQUIRE(callContractFunction("f(uint256)", 0x34) == encodeArgs(dev::sha3(dev::toBigEndian(u256(0x34)))));
+}
+
+BOOST_AUTO_TEST_CASE(return_string)
+{
+ char const* sourceCode = R"(
+ contract Main {
+ string public s;
+ function set(string _s) external {
+ s = _s;
+ }
+ function get1() returns (string r) {
+ return s;
+ }
+ function get2() returns (string r) {
+ r = s;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Main");
+ string s("Julia");
+ bytes args = encodeArgs(u256(0x20), u256(s.length()), s);
+ BOOST_REQUIRE(callContractFunction("set(string)", asString(args)) == encodeArgs());
+ BOOST_CHECK(callContractFunction("get1()") == args);
+ BOOST_CHECK(callContractFunction("get2()") == args);
+ BOOST_CHECK(callContractFunction("s()") == args);
+}
+
+BOOST_AUTO_TEST_CASE(return_multiple_strings_of_various_sizes)
+{
+ char const* sourceCode = R"(
+ contract Main {
+ string public s1;
+ string public s2;
+ function set(string _s1, uint x, string _s2) external returns (uint) {
+ s1 = _s1;
+ s2 = _s2;
+ return x;
+ }
+ function get() returns (string r1, string r2) {
+ r1 = s1;
+ r2 = s2;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Main");
+ string s1(
+ "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
+ "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
+ "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
+ "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
+ );
+ string s2(
+ "ABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZ"
+ "ABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZ"
+ "ABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZ"
+ "ABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZ"
+ "ABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZ"
+ );
+ vector<size_t> lengthes{0, 30, 32, 63, 64, 65, 210, 300};
+ for (auto l1: lengthes)
+ for (auto l2: lengthes)
+ {
+ bytes dyn1 = encodeArgs(u256(l1), s1.substr(0, l1));
+ bytes dyn2 = encodeArgs(u256(l2), s2.substr(0, l2));
+ bytes args = encodeArgs(u256(0x60), u256(l1), u256(0x60 + dyn1.size())) + dyn1 + dyn2;
+ BOOST_REQUIRE(
+ callContractFunction("set(string,uint256,string)", asString(args)) ==
+ encodeArgs(u256(l1))
+ );
+ bytes result = encodeArgs(u256(0x40), u256(0x40 + dyn1.size())) + dyn1 + dyn2;
+ BOOST_CHECK(callContractFunction("get()") == result);
+ BOOST_CHECK(callContractFunction("s1()") == encodeArgs(0x20) + dyn1);
+ BOOST_CHECK(callContractFunction("s2()") == encodeArgs(0x20) + dyn2);
+ }
+}
+
+BOOST_AUTO_TEST_CASE(accessor_involving_strings)
+{
+ char const* sourceCode = R"(
+ contract Main {
+ struct stringData { string a; uint b; string c; }
+ mapping(uint => stringData[]) public data;
+ function set(uint x, uint y, string a, uint b, string c) external returns (bool) {
+ data[x].length = y + 1;
+ data[x][y].a = a;
+ data[x][y].b = b;
+ data[x][y].c = c;
+ return true;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Main");
+ string s1("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
+ string s2("ABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZ");
+ bytes s1Data = encodeArgs(u256(s1.length()), s1);
+ bytes s2Data = encodeArgs(u256(s2.length()), s2);
+ u256 b = 765;
+ u256 x = 7;
+ u256 y = 123;
+ bytes args = encodeArgs(x, y, u256(0xa0), b, u256(0xa0 + s1Data.size()), s1Data, s2Data);
+ bytes result = encodeArgs(u256(0x60), b, u256(0x60 + s1Data.size()), s1Data, s2Data);
+ BOOST_REQUIRE(callContractFunction("set(uint256,uint256,string,uint256,string)", asString(args)) == encodeArgs(true));
+ BOOST_REQUIRE(callContractFunction("data(uint256,uint256)", x, y) == result);
+}
+
+BOOST_AUTO_TEST_CASE(bytes_in_function_calls)
+{
+ char const* sourceCode = R"(
+ contract Main {
+ string public s1;
+ string public s2;
+ function set(string _s1, uint x, string _s2) returns (uint) {
+ s1 = _s1;
+ s2 = _s2;
+ return x;
+ }
+ function setIndirectFromMemory(string _s1, uint x, string _s2) returns (uint) {
+ return this.set(_s1, x, _s2);
+ }
+ function setIndirectFromCalldata(string _s1, uint x, string _s2) external returns (uint) {
+ return this.set(_s1, x, _s2);
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Main");
+ string s1("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
+ string s2("ABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZ");
+ vector<size_t> lengthes{0, 31, 64, 65};
+ for (auto l1: lengthes)
+ for (auto l2: lengthes)
+ {
+ bytes dyn1 = encodeArgs(u256(l1), s1.substr(0, l1));
+ bytes dyn2 = encodeArgs(u256(l2), s2.substr(0, l2));
+ bytes args1 = encodeArgs(u256(0x60), u256(l1), u256(0x60 + dyn1.size())) + dyn1 + dyn2;
+ BOOST_REQUIRE(
+ callContractFunction("setIndirectFromMemory(string,uint256,string)", asString(args1)) ==
+ encodeArgs(u256(l1))
+ );
+ BOOST_CHECK(callContractFunction("s1()") == encodeArgs(0x20) + dyn1);
+ BOOST_CHECK(callContractFunction("s2()") == encodeArgs(0x20) + dyn2);
+ // swapped
+ bytes args2 = encodeArgs(u256(0x60), u256(l1), u256(0x60 + dyn2.size())) + dyn2 + dyn1;
+ BOOST_REQUIRE(
+ callContractFunction("setIndirectFromCalldata(string,uint256,string)", asString(args2)) ==
+ encodeArgs(u256(l1))
+ );
+ BOOST_CHECK(callContractFunction("s1()") == encodeArgs(0x20) + dyn2);
+ BOOST_CHECK(callContractFunction("s2()") == encodeArgs(0x20) + dyn1);
+ }
+}
+
+BOOST_AUTO_TEST_CASE(return_bytes_internal)
+{
+ char const* sourceCode = R"(
+ contract Main {
+ bytes s1;
+ function doSet(bytes _s1) returns (bytes _r1) {
+ s1 = _s1;
+ _r1 = s1;
+ }
+ function set(bytes _s1) external returns (uint _r, bytes _r1) {
+ _r1 = doSet(_s1);
+ _r = _r1.length;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Main");
+ string s1("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
+ vector<size_t> lengthes{0, 31, 64, 65};
+ for (auto l1: lengthes)
+ {
+ bytes dyn1 = encodeArgs(u256(l1), s1.substr(0, l1));
+ bytes args1 = encodeArgs(u256(0x20)) + dyn1;
+ BOOST_REQUIRE(
+ callContractFunction("set(bytes)", asString(args1)) ==
+ encodeArgs(u256(l1), u256(0x40)) + dyn1
+ );
+ }
+}
+
+BOOST_AUTO_TEST_CASE(bytes_index_access_memory)
+{
+ char const* sourceCode = R"(
+ contract Main {
+ function f(bytes _s1, uint i1, uint i2, uint i3) returns (byte c1, byte c2, byte c3) {
+ c1 = _s1[i1];
+ c2 = intern(_s1, i2);
+ c3 = internIndirect(_s1)[i3];
+ }
+ function intern(bytes _s1, uint i) returns (byte c) {
+ return _s1[i];
+ }
+ function internIndirect(bytes _s1) returns (bytes) {
+ return _s1;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Main");
+ string s1("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
+ bytes dyn1 = encodeArgs(u256(s1.length()), s1);
+ bytes args1 = encodeArgs(u256(0x80), u256(3), u256(4), u256(5)) + dyn1;
+ BOOST_REQUIRE(
+ callContractFunction("f(bytes,uint256,uint256,uint256)", asString(args1)) ==
+ encodeArgs(string{s1[3]}, string{s1[4]}, string{s1[5]})
+ );
+}
+
+BOOST_AUTO_TEST_CASE(bytes_in_constructors_unpacker)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ uint public m_x;
+ bytes public m_s;
+ function Test(uint x, bytes s) {
+ m_x = x;
+ m_s = s;
+ }
+ }
+ )";
+ string s1("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
+ bytes dyn1 = encodeArgs(u256(s1.length()), s1);
+ u256 x = 7;
+ bytes args1 = encodeArgs(x, u256(0x40)) + dyn1;
+ compileAndRun(sourceCode, 0, "Test", args1);
+ BOOST_REQUIRE(callContractFunction("m_x()") == encodeArgs(x));
+ BOOST_REQUIRE(callContractFunction("m_s()") == encodeArgs(u256(0x20)) + dyn1);
+}
+
+BOOST_AUTO_TEST_CASE(bytes_in_constructors_packer)
+{
+ char const* sourceCode = R"(
+ contract Base {
+ uint public m_x;
+ bytes m_s;
+ function Base(uint x, bytes s) {
+ m_x = x;
+ m_s = s;
+ }
+ function part(uint i) returns (byte) {
+ return m_s[i];
+ }
+ }
+ contract Main is Base {
+ function Main(bytes s, uint x) Base(x, f(s)) {}
+ function f(bytes s) returns (bytes) {
+ return s;
+ }
+ }
+ contract Creator {
+ function f(uint x, bytes s) returns (uint r, byte ch) {
+ var c = new Main(s, x);
+ r = c.m_x();
+ ch = c.part(x);
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Creator");
+ string s1("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
+ bytes dyn1 = encodeArgs(u256(s1.length()), s1);
+ u256 x = 7;
+ bytes args1 = encodeArgs(x, u256(0x40)) + dyn1;
+ BOOST_REQUIRE(
+ callContractFunction("f(uint256,bytes)", asString(args1)) ==
+ encodeArgs(x, string{s1[unsigned(x)]})
+ );
+}
+
+BOOST_AUTO_TEST_CASE(arrays_in_constructors)
+{
+ char const* sourceCode = R"(
+ contract Base {
+ uint public m_x;
+ address[] m_s;
+ function Base(uint x, address[] s) {
+ m_x = x;
+ m_s = s;
+ }
+ function part(uint i) returns (address) {
+ return m_s[i];
+ }
+ }
+ contract Main is Base {
+ function Main(address[] s, uint x) Base(x, f(s)) {}
+ function f(address[] s) returns (address[]) {
+ return s;
+ }
+ }
+ contract Creator {
+ function f(uint x, address[] s) returns (uint r, address ch) {
+ var c = new Main(s, x);
+ r = c.m_x();
+ ch = c.part(x);
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Creator");
+ vector<u256> s1{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+ bytes dyn1 = encodeArgs(u256(s1.size()), s1);
+ u256 x = 7;
+ bytes args1 = encodeArgs(x, u256(0x40)) + dyn1;
+ BOOST_REQUIRE(
+ callContractFunction("f(uint256,address[])", asString(args1)) ==
+ encodeArgs(x, s1[unsigned(x)])
+ );
+}
+
+BOOST_AUTO_TEST_CASE(arrays_from_and_to_storage)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ uint24[] public data;
+ function set(uint24[] _data) returns (uint) {
+ data = _data;
+ return data.length;
+ }
+ function get() returns (uint24[]) {
+ return data;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Test");
+
+ vector<u256> data{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
+ BOOST_REQUIRE(
+ callContractFunction("set(uint24[])", u256(0x20), u256(data.size()), data) ==
+ encodeArgs(u256(data.size()))
+ );
+ BOOST_CHECK(callContractFunction("data(uint256)", u256(7)) == encodeArgs(u256(8)));
+ BOOST_CHECK(callContractFunction("data(uint256)", u256(15)) == encodeArgs(u256(16)));
+ BOOST_CHECK(callContractFunction("data(uint256)", u256(18)) == encodeArgs());
+ BOOST_CHECK(callContractFunction("get()") == encodeArgs(u256(0x20), u256(data.size()), data));
+}
+
+BOOST_AUTO_TEST_CASE(arrays_complex_from_and_to_storage)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ uint24[3][] public data;
+ function set(uint24[3][] _data) returns (uint) {
+ data = _data;
+ return data.length;
+ }
+ function get() returns (uint24[3][]) {
+ return data;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Test");
+
+ vector<u256> data{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
+ BOOST_REQUIRE(
+ callContractFunction("set(uint24[3][])", u256(0x20), u256(data.size() / 3), data) ==
+ encodeArgs(u256(data.size() / 3))
+ );
+ BOOST_CHECK(callContractFunction("data(uint256,uint256)", u256(2), u256(2)) == encodeArgs(u256(9)));
+ BOOST_CHECK(callContractFunction("data(uint256,uint256)", u256(5), u256(1)) == encodeArgs(u256(17)));
+ BOOST_CHECK(callContractFunction("data(uint256,uint256)", u256(6), u256(0)) == encodeArgs());
+ BOOST_CHECK(callContractFunction("get()") == encodeArgs(u256(0x20), u256(data.size() / 3), data));
+}
+
+BOOST_AUTO_TEST_CASE(arrays_complex_memory_index_access)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ function set(uint24[3][] _data, uint a, uint b) returns (uint l, uint e) {
+ l = _data.length;
+ e = _data[a][b];
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Test");
+
+ vector<u256> data{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
+ BOOST_REQUIRE(callContractFunction(
+ "set(uint24[3][],uint256,uint256)",
+ u256(0x60),
+ u256(3),
+ u256(2),
+ u256(data.size() / 3),
+ data
+ ) == encodeArgs(u256(data.size() / 3), u256(data[3 * 3 + 2])));
+}
+
+BOOST_AUTO_TEST_CASE(bytes_memory_index_access)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ function set(bytes _data, uint i) returns (uint l, byte c) {
+ l = _data.length;
+ c = _data[i];
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Test");
+
+ string data("abcdefgh");
+ BOOST_REQUIRE(callContractFunction(
+ "set(bytes,uint256)",
+ u256(0x40),
+ u256(3),
+ u256(data.size()),
+ data
+ ) == encodeArgs(u256(data.size()), string("d")));
+}
+
+BOOST_AUTO_TEST_CASE(dev_title_at_function_error)
+{
+ char const* sourceCode = " /// @author Lefteris\n"
+ " /// @title Just a test contract\n"
+ "contract test {\n"
+ " /// @dev Mul function\n"
+ " /// @title I really should not be here\n"
+ " function mul(uint a, uint second) returns(uint d) { return a * 7 + second; }\n"
+ "}\n";
+
+ compileRequireThrow<DocstringParsingError>(sourceCode);
+}
+
+BOOST_AUTO_TEST_CASE(dev_documenting_nonexistant_param)
+{
+ char const* sourceCode = "contract test {\n"
+ " /// @dev Multiplies a number by 7 and adds second parameter\n"
+ " /// @param a Documentation for the first parameter\n"
+ " /// @param not_existing Documentation for the second parameter\n"
+ " function mul(uint a, uint second) returns(uint d) { return a * 7 + second; }\n"
+ "}\n";
+
+ compileRequireThrow<DocstringParsingError>(sourceCode);
+}
+
+
+BOOST_AUTO_TEST_CASE(storage_array_ref)
+{
+ char const* sourceCode = R"(
+ contract BinarySearch {
+ /// Finds the position of _value in the sorted list _data.
+ /// Note that "internal" is important here, because storage references only work for internal or private functions
+ function find(uint[] storage _data, uint _value) internal returns (uint o_position) {
+ return find(_data, 0, _data.length, _value);
+ }
+ function find(uint[] storage _data, uint _begin, uint _len, uint _value) private returns (uint o_position) {
+ if (_len == 0 || (_len == 1 && _data[_begin] != _value))
+ return uint(-1); // failure
+ uint halfLen = _len / 2;
+ uint v = _data[_begin + halfLen];
+ if (_value < v)
+ return find(_data, _begin, halfLen, _value);
+ else if (_value > v)
+ return find(_data, _begin + halfLen + 1, halfLen - 1, _value);
+ else
+ return _begin + halfLen;
+ }
+ }
+
+ contract Store is BinarySearch {
+ uint[] data;
+ function add(uint v) {
+ data.length++;
+ data[data.length - 1] = v;
+ }
+ function find(uint v) returns (uint) {
+ return find(data, v);
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Store");
+ BOOST_REQUIRE(callContractFunction("find(uint256)", u256(7)) == encodeArgs(u256(-1)));
+ BOOST_REQUIRE(callContractFunction("add(uint256)", u256(7)) == encodeArgs());
+ BOOST_REQUIRE(callContractFunction("find(uint256)", u256(7)) == encodeArgs(u256(0)));
+ BOOST_CHECK(callContractFunction("add(uint256)", u256(11)) == encodeArgs());
+ BOOST_CHECK(callContractFunction("add(uint256)", u256(17)) == encodeArgs());
+ BOOST_CHECK(callContractFunction("add(uint256)", u256(27)) == encodeArgs());
+ BOOST_CHECK(callContractFunction("add(uint256)", u256(31)) == encodeArgs());
+ BOOST_CHECK(callContractFunction("add(uint256)", u256(32)) == encodeArgs());
+ BOOST_CHECK(callContractFunction("add(uint256)", u256(66)) == encodeArgs());
+ BOOST_CHECK(callContractFunction("add(uint256)", u256(177)) == encodeArgs());
+ BOOST_CHECK(callContractFunction("find(uint256)", u256(7)) == encodeArgs(u256(0)));
+ BOOST_CHECK(callContractFunction("find(uint256)", u256(27)) == encodeArgs(u256(3)));
+ BOOST_CHECK(callContractFunction("find(uint256)", u256(32)) == encodeArgs(u256(5)));
+ BOOST_CHECK(callContractFunction("find(uint256)", u256(176)) == encodeArgs(u256(-1)));
+ BOOST_CHECK(callContractFunction("find(uint256)", u256(0)) == encodeArgs(u256(-1)));
+ BOOST_CHECK(callContractFunction("find(uint256)", u256(400)) == encodeArgs(u256(-1)));
+}
+
+BOOST_AUTO_TEST_CASE(memory_types_initialisation)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ mapping(uint=>uint) data;
+ function stat() returns (uint[5])
+ {
+ data[2] = 3; // make sure to use some memory
+ }
+ function dyn() returns (uint[]) { stat(); }
+ function nested() returns (uint[3][]) { stat(); }
+ function nestedStat() returns (uint[3][7]) { stat(); }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Test");
+
+ BOOST_CHECK(callContractFunction("stat()") == encodeArgs(vector<u256>(5)));
+ BOOST_CHECK(callContractFunction("dyn()") == encodeArgs(u256(0x20), u256(0)));
+ BOOST_CHECK(callContractFunction("nested()") == encodeArgs(u256(0x20), u256(0)));
+ BOOST_CHECK(callContractFunction("nestedStat()") == encodeArgs(vector<u256>(3 * 7)));
+}
+
+BOOST_AUTO_TEST_CASE(memory_arrays_delete)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ function del() returns (uint24[3][4]) {
+ uint24[3][4] memory x;
+ for (uint24 i = 0; i < x.length; i ++)
+ for (uint24 j = 0; j < x[i].length; j ++)
+ x[i][j] = i * 0x10 + j;
+ delete x[1];
+ delete x[3][2];
+ return x;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Test");
+
+ vector<u256> data(3 * 4);
+ for (unsigned i = 0; i < 4; i++)
+ for (unsigned j = 0; j < 3; j++)
+ {
+ u256 v = 0;
+ if (!(i == 1 || (i == 3 && j == 2)))
+ v = i * 0x10 + j;
+ data[i * 3 + j] = v;
+ }
+ BOOST_CHECK(callContractFunction("del()") == encodeArgs(data));
+}
+
+BOOST_AUTO_TEST_CASE(memory_arrays_index_access_write)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ function set(uint24[3][4] x) {
+ x[2][2] = 1;
+ x[3][2] = 7;
+ }
+ function f() returns (uint24[3][4]){
+ uint24[3][4] memory data;
+ set(data);
+ return data;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Test");
+
+ vector<u256> data(3 * 4);
+ data[3 * 2 + 2] = 1;
+ data[3 * 3 + 2] = 7;
+ BOOST_CHECK(callContractFunction("f()") == encodeArgs(data));
+}
+
+BOOST_AUTO_TEST_CASE(memory_arrays_dynamic_index_access_write)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ uint24[3][][4] data;
+ function set(uint24[3][][4] x) internal returns (uint24[3][][4]) {
+ x[1][2][2] = 1;
+ x[1][3][2] = 7;
+ return x;
+ }
+ function f() returns (uint24[3][]) {
+ data[1].length = 4;
+ return set(data)[1];
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Test");
+
+ vector<u256> data(3 * 4);
+ data[3 * 2 + 2] = 1;
+ data[3 * 3 + 2] = 7;
+ BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(0x20), u256(4), data));
+}
+
+BOOST_AUTO_TEST_CASE(memory_structs_read_write)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ struct S { uint8 x; uint16 y; uint z; uint8[2] a; }
+ S[5] data;
+ function testInit() returns (uint8 x, uint16 y, uint z, uint8 a, bool flag) {
+ S[2] memory d;
+ x = d[0].x;
+ y = d[0].y;
+ z = d[0].z;
+ a = d[0].a[1];
+ flag = true;
+ }
+ function testCopyRead() returns (uint8 x, uint16 y, uint z, uint8 a) {
+ data[2].x = 1;
+ data[2].y = 2;
+ data[2].z = 3;
+ data[2].a[1] = 4;
+ S memory s = data[2];
+ x = s.x;
+ y = s.y;
+ z = s.z;
+ a = s.a[1];
+ }
+ function testAssign() returns (uint8 x, uint16 y, uint z, uint8 a) {
+ S memory s;
+ s.x = 1;
+ s.y = 2;
+ s.z = 3;
+ s.a[1] = 4;
+ x = s.x;
+ y = s.y;
+ z = s.z;
+ a = s.a[1];
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Test");
+
+ BOOST_CHECK(callContractFunction("testInit()") == encodeArgs(u256(0), u256(0), u256(0), u256(0), true));
+ BOOST_CHECK(callContractFunction("testCopyRead()") == encodeArgs(u256(1), u256(2), u256(3), u256(4)));
+ BOOST_CHECK(callContractFunction("testAssign()") == encodeArgs(u256(1), u256(2), u256(3), u256(4)));
+}
+
+BOOST_AUTO_TEST_CASE(memory_structs_as_function_args)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ struct S { uint8 x; uint16 y; uint z; }
+ function test() returns (uint x, uint y, uint z) {
+ S memory data = combine(1, 2, 3);
+ x = extract(data, 0);
+ y = extract(data, 1);
+ z = extract(data, 2);
+ }
+ function extract(S s, uint which) internal returns (uint x) {
+ if (which == 0) return s.x;
+ else if (which == 1) return s.y;
+ else return s.z;
+ }
+ function combine(uint8 x, uint16 y, uint z) internal returns (S s) {
+ s.x = x;
+ s.y = y;
+ s.z = z;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Test");
+
+ BOOST_CHECK(callContractFunction("test()") == encodeArgs(u256(1), u256(2), u256(3)));
+}
+
+BOOST_AUTO_TEST_CASE(memory_structs_nested)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ struct S { uint8 x; uint16 y; uint z; }
+ struct X { uint8 x; S s; }
+ function test() returns (uint a, uint x, uint y, uint z) {
+ X memory d = combine(1, 2, 3, 4);
+ a = extract(d, 0);
+ x = extract(d, 1);
+ y = extract(d, 2);
+ z = extract(d, 3);
+ }
+ function extract(X s, uint which) internal returns (uint x) {
+ if (which == 0) return s.x;
+ else if (which == 1) return s.s.x;
+ else if (which == 2) return s.s.y;
+ else return s.s.z;
+ }
+ function combine(uint8 a, uint8 x, uint16 y, uint z) internal returns (X s) {
+ s.x = a;
+ s.s.x = x;
+ s.s.y = y;
+ s.s.z = z;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Test");
+
+ BOOST_CHECK(callContractFunction("test()") == encodeArgs(u256(1), u256(2), u256(3), u256(4)));
+}
+
+BOOST_AUTO_TEST_CASE(memory_structs_nested_load)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ struct S { uint8 x; uint16 y; uint z; }
+ struct X { uint8 x; S s; uint8[2] a; }
+ X m_x;
+ function load() returns (uint a, uint x, uint y, uint z, uint a1, uint a2) {
+ m_x.x = 1;
+ m_x.s.x = 2;
+ m_x.s.y = 3;
+ m_x.s.z = 4;
+ m_x.a[0] = 5;
+ m_x.a[1] = 6;
+ X memory d = m_x;
+ a = d.x;
+ x = d.s.x;
+ y = d.s.y;
+ z = d.s.z;
+ a1 = d.a[0];
+ a2 = d.a[1];
+ }
+ function store() returns (uint a, uint x, uint y, uint z, uint a1, uint a2) {
+ X memory d;
+ d.x = 1;
+ d.s.x = 2;
+ d.s.y = 3;
+ d.s.z = 4;
+ d.a[0] = 5;
+ d.a[1] = 6;
+ m_x = d;
+ a = m_x.x;
+ x = m_x.s.x;
+ y = m_x.s.y;
+ z = m_x.s.z;
+ a1 = m_x.a[0];
+ a2 = m_x.a[1];
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Test");
+
+ auto out = encodeArgs(u256(1), u256(2), u256(3), u256(4), u256(5), u256(6));
+ BOOST_CHECK(callContractFunction("load()") == out);
+ BOOST_CHECK(callContractFunction("store()") == out);
+}
+
+BOOST_AUTO_TEST_CASE(struct_constructor_nested)
+{
+ char const* sourceCode = R"(
+ contract C {
+ struct X { uint x1; uint x2; }
+ struct S { uint s1; uint[3] s2; X s3; }
+ S s;
+ function C() {
+ uint[3] memory s2;
+ s2[1] = 9;
+ s = S(1, s2, X(4, 5));
+ }
+ function get() returns (uint s1, uint[3] s2, uint x1, uint x2)
+ {
+ s1 = s.s1;
+ s2 = s.s2;
+ x1 = s.s3.x1;
+ x2 = s.s3.x2;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "C");
+
+ auto out = encodeArgs(u256(1), u256(0), u256(9), u256(0), u256(4), u256(5));
+ BOOST_CHECK(callContractFunction("get()") == out);
+}
+
+BOOST_AUTO_TEST_CASE(struct_named_constructor)
+{
+ char const* sourceCode = R"(
+ contract C {
+ struct S { uint a; bool x; }
+ S public s;
+ function C() {
+ s = S({a: 1, x: true});
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "C");
+
+ BOOST_CHECK(callContractFunction("s()") == encodeArgs(u256(1), true));
+}
+
+BOOST_AUTO_TEST_CASE(literal_strings)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ string public long;
+ string public medium;
+ string public short;
+ string public empty;
+ function f() returns (string) {
+ long = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789001234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678900123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789001234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
+ medium = "01234567890123456789012345678901234567890123456789012345678901234567890123456789";
+ short = "123";
+ empty = "";
+ return "Hello, World!";
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Test");
+ string longStr = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789001234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678900123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789001234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
+ string medium = "01234567890123456789012345678901234567890123456789012345678901234567890123456789";
+ string shortStr = "123";
+ string hello = "Hello, World!";
+
+ BOOST_CHECK(callContractFunction("f()") == encodeDyn(hello));
+ BOOST_CHECK(callContractFunction("long()") == encodeDyn(longStr));
+ BOOST_CHECK(callContractFunction("medium()") == encodeDyn(medium));
+ BOOST_CHECK(callContractFunction("short()") == encodeDyn(shortStr));
+ BOOST_CHECK(callContractFunction("empty()") == encodeDyn(string()));
+}
+
+BOOST_AUTO_TEST_CASE(initialise_string_constant)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ string public short = "abcdef";
+ string public long = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789001234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678900123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789001234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Test");
+ string longStr = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789001234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678900123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789001234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
+ string shortStr = "abcdef";
+
+ BOOST_CHECK(callContractFunction("long()") == encodeDyn(longStr));
+ BOOST_CHECK(callContractFunction("short()") == encodeDyn(shortStr));
+}
+
+BOOST_AUTO_TEST_CASE(memory_structs_with_mappings)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ struct S { uint8 a; mapping(uint => uint) b; uint8 c; }
+ S s;
+ function f() returns (uint) {
+ S memory x;
+ if (x.a != 0 || x.c != 0) return 1;
+ x.a = 4; x.c = 5;
+ s = x;
+ if (s.a != 4 || s.c != 5) return 2;
+ x = S(2, 3);
+ if (x.a != 2 || x.c != 3) return 3;
+ x = s;
+ if (s.a != 4 || s.c != 5) return 4;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Test");
+ BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(0)));
+}
+
+BOOST_AUTO_TEST_CASE(string_bytes_conversion)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ string s;
+ bytes b;
+ function f(string _s, uint n) returns (byte) {
+ b = bytes(_s);
+ s = string(b);
+ return bytes(s)[n];
+ }
+ function l() returns (uint) { return bytes(s).length; }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Test");
+ BOOST_CHECK(callContractFunction(
+ "f(string,uint256)",
+ u256(0x40),
+ u256(2),
+ u256(6),
+ string("abcdef")
+ ) == encodeArgs("c"));
+ BOOST_CHECK(callContractFunction("l()") == encodeArgs(u256(6)));
+}
+
+BOOST_AUTO_TEST_CASE(string_as_mapping_key)
+{
+ char const* sourceCode = R"(
+ contract Test {
+ mapping(string => uint) data;
+ function set(string _s, uint _v) { data[_s] = _v; }
+ function get(string _s) returns (uint) { return data[_s]; }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Test");
+ vector<string> strings{
+ "Hello, World!",
+ "Hello, World!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1111",
+ "",
+ "1"
+ };
+ for (unsigned i = 0; i < strings.size(); i++)
+ BOOST_CHECK(callContractFunction(
+ "set(string,uint256)",
+ u256(0x40),
+ u256(7 + i),
+ u256(strings[i].size()),
+ strings[i]
+ ) == encodeArgs());
+ for (unsigned i = 0; i < strings.size(); i++)
+ BOOST_CHECK(callContractFunction(
+ "get(string)",
+ u256(0x20),
+ u256(strings[i].size()),
+ strings[i]
+ ) == encodeArgs(u256(7 + i)));
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+}
+}
+} // end namespaces
diff --git a/test/libsolidity/SolidityExpressionCompiler.cpp b/test/libsolidity/SolidityExpressionCompiler.cpp
new file mode 100644
index 00000000..ee631197
--- /dev/null
+++ b/test/libsolidity/SolidityExpressionCompiler.cpp
@@ -0,0 +1,491 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum 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.
+
+ cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * @author Christian <c@ethdev.com>
+ * @date 2014
+ * Unit tests for the solidity expression compiler.
+ */
+
+#include <string>
+
+#include <libdevcore/Log.h>
+#include <libsolidity/Scanner.h>
+#include <libsolidity/Parser.h>
+#include <libsolidity/NameAndTypeResolver.h>
+#include <libsolidity/CompilerContext.h>
+#include <libsolidity/ExpressionCompiler.h>
+#include <libsolidity/AST.h>
+#include "../TestHelper.h"
+
+using namespace std;
+
+namespace dev
+{
+namespace solidity
+{
+namespace test
+{
+
+namespace
+{
+
+/// Helper class that extracts the first expression in an AST.
+class FirstExpressionExtractor: private ASTVisitor
+{
+public:
+ FirstExpressionExtractor(ASTNode& _node): m_expression(nullptr) { _node.accept(*this); }
+ Expression* getExpression() const { return m_expression; }
+private:
+ virtual bool visit(Assignment& _expression) override { return checkExpression(_expression); }
+ virtual bool visit(UnaryOperation& _expression) override { return checkExpression(_expression); }
+ virtual bool visit(BinaryOperation& _expression) override { return checkExpression(_expression); }
+ virtual bool visit(FunctionCall& _expression) override { return checkExpression(_expression); }
+ virtual bool visit(MemberAccess& _expression) override { return checkExpression(_expression); }
+ virtual bool visit(IndexAccess& _expression) override { return checkExpression(_expression); }
+ virtual bool visit(Identifier& _expression) override { return checkExpression(_expression); }
+ virtual bool visit(ElementaryTypeNameExpression& _expression) override { return checkExpression(_expression); }
+ virtual bool visit(Literal& _expression) override { return checkExpression(_expression); }
+ bool checkExpression(Expression& _expression)
+ {
+ if (m_expression == nullptr)
+ m_expression = &_expression;
+ return false;
+ }
+private:
+ Expression* m_expression;
+};
+
+Declaration const& resolveDeclaration(
+ vector<string> const& _namespacedName, NameAndTypeResolver const& _resolver)
+{
+ Declaration const* declaration = nullptr;
+ // bracers are required, cause msvc couldnt handle this macro in for statement
+ for (string const& namePart: _namespacedName)
+ {
+ auto declarations = _resolver.resolveName(namePart, declaration);
+ BOOST_REQUIRE(!declarations.empty());
+ BOOST_REQUIRE(declaration = *declarations.begin());
+ }
+ BOOST_REQUIRE(declaration);
+ return *declaration;
+}
+
+bytes compileFirstExpression(const string& _sourceCode, vector<vector<string>> _functions = {},
+ vector<vector<string>> _localVariables = {},
+ vector<shared_ptr<MagicVariableDeclaration const>> _globalDeclarations = {})
+{
+ Parser parser;
+ ASTPointer<SourceUnit> sourceUnit;
+ try
+ {
+ sourceUnit = parser.parse(make_shared<Scanner>(CharStream(_sourceCode)));
+ }
+ catch(boost::exception const& _e)
+ {
+ auto msg = std::string("Parsing source code failed with: \n") + boost::diagnostic_information(_e);
+ BOOST_FAIL(msg);
+ }
+
+ vector<Declaration const*> declarations;
+ declarations.reserve(_globalDeclarations.size() + 1);
+ for (ASTPointer<Declaration const> const& variable: _globalDeclarations)
+ declarations.push_back(variable.get());
+ NameAndTypeResolver resolver(declarations);
+ resolver.registerDeclarations(*sourceUnit);
+
+ vector<ContractDefinition const*> inheritanceHierarchy;
+ for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
+ if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
+ {
+ ETH_TEST_REQUIRE_NO_THROW(resolver.resolveNamesAndTypes(*contract), "Resolving names failed");
+ inheritanceHierarchy = vector<ContractDefinition const*>(1, contract);
+ }
+ for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
+ if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
+ {
+ ETH_TEST_REQUIRE_NO_THROW(resolver.checkTypeRequirements(*contract), "Checking type Requirements failed");
+ }
+ for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
+ if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
+ {
+ FirstExpressionExtractor extractor(*contract);
+ BOOST_REQUIRE(extractor.getExpression() != nullptr);
+
+ CompilerContext context;
+ context.resetVisitedNodes(contract);
+ context.setInheritanceHierarchy(inheritanceHierarchy);
+ unsigned parametersSize = _localVariables.size(); // assume they are all one slot on the stack
+ context.adjustStackOffset(parametersSize);
+ for (vector<string> const& variable: _localVariables)
+ context.addVariable(dynamic_cast<VariableDeclaration const&>(resolveDeclaration(variable, resolver)),
+ parametersSize--);
+
+ ExpressionCompiler(context).compile(*extractor.getExpression());
+
+ for (vector<string> const& function: _functions)
+ context << context.getFunctionEntryLabel(dynamic_cast<FunctionDefinition const&>(resolveDeclaration(function, resolver)));
+ bytes instructions = context.getAssembledBytecode();
+ // debug
+ // cout << eth::disassemble(instructions) << endl;
+ return instructions;
+ }
+ BOOST_FAIL("No contract found in source.");
+ return bytes();
+}
+
+} // end anonymous namespace
+
+BOOST_AUTO_TEST_SUITE(SolidityExpressionCompiler)
+
+BOOST_AUTO_TEST_CASE(literal_true)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f() { var x = true; }"
+ "}\n";
+ bytes code = compileFirstExpression(sourceCode);
+
+ bytes expectation({byte(eth::Instruction::PUSH1), 0x1});
+ BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
+}
+
+BOOST_AUTO_TEST_CASE(literal_false)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f() { var x = false; }"
+ "}\n";
+ bytes code = compileFirstExpression(sourceCode);
+
+ bytes expectation({byte(eth::Instruction::PUSH1), 0x0});
+ BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
+}
+
+BOOST_AUTO_TEST_CASE(int_literal)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f() { var x = 0x12345678901234567890; }"
+ "}\n";
+ bytes code = compileFirstExpression(sourceCode);
+
+ bytes expectation({byte(eth::Instruction::PUSH10), 0x12, 0x34, 0x56, 0x78, 0x90,
+ 0x12, 0x34, 0x56, 0x78, 0x90});
+ BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
+}
+
+BOOST_AUTO_TEST_CASE(int_with_wei_ether_subdenomination)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function test ()
+ {
+ var x = 1 wei;
+ }
+ })";
+ bytes code = compileFirstExpression(sourceCode);
+
+ bytes expectation({byte(eth::Instruction::PUSH1), 0x1});
+ BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
+}
+
+BOOST_AUTO_TEST_CASE(int_with_szabo_ether_subdenomination)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function test ()
+ {
+ var x = 1 szabo;
+ }
+ })";
+ bytes code = compileFirstExpression(sourceCode);
+
+ bytes expectation({byte(eth::Instruction::PUSH5), 0xe8, 0xd4, 0xa5, 0x10, 0x00});
+ BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
+}
+
+BOOST_AUTO_TEST_CASE(int_with_finney_ether_subdenomination)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function test ()
+ {
+ var x = 1 finney;
+ }
+ })";
+ bytes code = compileFirstExpression(sourceCode);
+
+ bytes expectation({byte(eth::Instruction::PUSH7), 0x3, 0x8d, 0x7e, 0xa4, 0xc6, 0x80, 0x00});
+ BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
+}
+
+BOOST_AUTO_TEST_CASE(int_with_ether_ether_subdenomination)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function test ()
+ {
+ var x = 1 ether;
+ }
+ })";
+ bytes code = compileFirstExpression(sourceCode);
+
+ bytes expectation({byte(eth::Instruction::PUSH8), 0xd, 0xe0, 0xb6, 0xb3, 0xa7, 0x64, 0x00, 0x00});
+ BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
+}
+
+BOOST_AUTO_TEST_CASE(comparison)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f() { var x = (0x10aa < 0x11aa) != true; }"
+ "}\n";
+ bytes code = compileFirstExpression(sourceCode);
+
+ bytes expectation({byte(eth::Instruction::PUSH1), 0x1,
+ byte(eth::Instruction::PUSH2), 0x11, 0xaa,
+ byte(eth::Instruction::PUSH2), 0x10, 0xaa,
+ byte(eth::Instruction::LT),
+ byte(eth::Instruction::EQ),
+ byte(eth::Instruction::ISZERO)});
+ BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
+}
+
+BOOST_AUTO_TEST_CASE(short_circuiting)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f() { var x = true != (4 <= 8 + 10 || 9 != 2); }"
+ "}\n";
+ bytes code = compileFirstExpression(sourceCode);
+
+ bytes expectation({byte(eth::Instruction::PUSH1), 0x12, // 8 + 10
+ byte(eth::Instruction::PUSH1), 0x4,
+ byte(eth::Instruction::GT),
+ byte(eth::Instruction::ISZERO), // after this we have 4 <= 8 + 10
+ byte(eth::Instruction::DUP1),
+ byte(eth::Instruction::PUSH1), 0x11,
+ byte(eth::Instruction::JUMPI), // short-circuit if it is true
+ byte(eth::Instruction::POP),
+ byte(eth::Instruction::PUSH1), 0x2,
+ byte(eth::Instruction::PUSH1), 0x9,
+ byte(eth::Instruction::EQ),
+ byte(eth::Instruction::ISZERO), // after this we have 9 != 2
+ byte(eth::Instruction::JUMPDEST),
+ byte(eth::Instruction::PUSH1), 0x1,
+ byte(eth::Instruction::EQ),
+ byte(eth::Instruction::ISZERO)});
+ BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
+}
+
+BOOST_AUTO_TEST_CASE(arithmetics)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f(uint y) { var x = ((((((((y ^ 8) & 7) | 6) - 5) + 4) % 3) / 2) * 1); }"
+ "}\n";
+ bytes code = compileFirstExpression(sourceCode, {}, {{"test", "f", "y"}, {"test", "f", "x"}});
+ bytes expectation({byte(eth::Instruction::PUSH1), 0x1,
+ byte(eth::Instruction::PUSH1), 0x2,
+ byte(eth::Instruction::PUSH1), 0x3,
+ byte(eth::Instruction::PUSH1), 0x4,
+ byte(eth::Instruction::PUSH1), 0x5,
+ byte(eth::Instruction::PUSH1), 0x6,
+ byte(eth::Instruction::PUSH1), 0x7,
+ byte(eth::Instruction::PUSH1), 0x8,
+ byte(eth::Instruction::DUP10),
+ byte(eth::Instruction::XOR),
+ byte(eth::Instruction::AND),
+ byte(eth::Instruction::OR),
+ byte(eth::Instruction::SUB),
+ byte(eth::Instruction::ADD),
+ byte(eth::Instruction::MOD),
+ byte(eth::Instruction::DIV),
+ byte(eth::Instruction::MUL)});
+ BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
+}
+
+BOOST_AUTO_TEST_CASE(unary_operators)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f(int y) { var x = !(~+- y == 2); }"
+ "}\n";
+ bytes code = compileFirstExpression(sourceCode, {}, {{"test", "f", "y"}, {"test", "f", "x"}});
+
+ bytes expectation({byte(eth::Instruction::PUSH1), 0x2,
+ byte(eth::Instruction::DUP3),
+ byte(eth::Instruction::PUSH1), 0x0,
+ byte(eth::Instruction::SUB),
+ byte(eth::Instruction::NOT),
+ byte(eth::Instruction::EQ),
+ byte(eth::Instruction::ISZERO)});
+ BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
+}
+
+BOOST_AUTO_TEST_CASE(unary_inc_dec)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f(uint a) { var x = --a ^ (a-- ^ (++a ^ a++)); }"
+ "}\n";
+ bytes code = compileFirstExpression(sourceCode, {}, {{"test", "f", "a"}, {"test", "f", "x"}});
+
+ // Stack: a, x
+ bytes expectation({byte(eth::Instruction::DUP2),
+ byte(eth::Instruction::DUP1),
+ byte(eth::Instruction::PUSH1), 0x1,
+ byte(eth::Instruction::ADD),
+ // Stack here: a x a (a+1)
+ byte(eth::Instruction::SWAP3),
+ byte(eth::Instruction::POP), // first ++
+ // Stack here: (a+1) x a
+ byte(eth::Instruction::DUP3),
+ byte(eth::Instruction::PUSH1), 0x1,
+ byte(eth::Instruction::ADD),
+ // Stack here: (a+1) x a (a+2)
+ byte(eth::Instruction::SWAP3),
+ byte(eth::Instruction::POP),
+ // Stack here: (a+2) x a
+ byte(eth::Instruction::DUP3), // second ++
+ byte(eth::Instruction::XOR),
+ // Stack here: (a+2) x a^(a+2)
+ byte(eth::Instruction::DUP3),
+ byte(eth::Instruction::DUP1),
+ byte(eth::Instruction::PUSH1), 0x1,
+ byte(eth::Instruction::SWAP1),
+ byte(eth::Instruction::SUB),
+ // Stack here: (a+2) x a^(a+2) (a+2) (a+1)
+ byte(eth::Instruction::SWAP4),
+ byte(eth::Instruction::POP), // first --
+ byte(eth::Instruction::XOR),
+ // Stack here: (a+1) x a^(a+2)^(a+2)
+ byte(eth::Instruction::DUP3),
+ byte(eth::Instruction::PUSH1), 0x1,
+ byte(eth::Instruction::SWAP1),
+ byte(eth::Instruction::SUB),
+ // Stack here: (a+1) x a^(a+2)^(a+2) a
+ byte(eth::Instruction::SWAP3),
+ byte(eth::Instruction::POP), // second ++
+ // Stack here: a x a^(a+2)^(a+2)
+ byte(eth::Instruction::DUP3), // will change
+ byte(eth::Instruction::XOR)});
+ // Stack here: a x a^(a+2)^(a+2)^a
+ BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
+}
+
+BOOST_AUTO_TEST_CASE(assignment)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f(uint a, uint b) { (a += b) * 2; }"
+ "}\n";
+ bytes code = compileFirstExpression(sourceCode, {}, {{"test", "f", "a"}, {"test", "f", "b"}});
+
+ // Stack: a, b
+ bytes expectation({byte(eth::Instruction::PUSH1), 0x2,
+ byte(eth::Instruction::DUP2),
+ byte(eth::Instruction::DUP4),
+ byte(eth::Instruction::ADD),
+ // Stack here: a b 2 a+b
+ byte(eth::Instruction::SWAP3),
+ byte(eth::Instruction::POP),
+ byte(eth::Instruction::DUP3),
+ // Stack here: a+b b 2 a+b
+ byte(eth::Instruction::MUL)});
+ BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
+}
+
+BOOST_AUTO_TEST_CASE(function_call)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f(uint a, uint b) { a += g(a + 1, b) * 2; }\n"
+ " function g(uint a, uint b) returns (uint c) {}\n"
+ "}\n";
+ bytes code = compileFirstExpression(sourceCode, {{"test", "g"}},
+ {{"test", "f", "a"}, {"test", "f", "b"}});
+
+ // Stack: a, b
+ bytes expectation({byte(eth::Instruction::PUSH1), 0x02,
+ byte(eth::Instruction::PUSH1), 0x0c,
+ byte(eth::Instruction::PUSH1), 0x01,
+ byte(eth::Instruction::DUP5),
+ byte(eth::Instruction::ADD),
+ // Stack here: a b 2 <ret label> (a+1)
+ byte(eth::Instruction::DUP4),
+ byte(eth::Instruction::PUSH1), 0x13,
+ byte(eth::Instruction::JUMP),
+ byte(eth::Instruction::JUMPDEST),
+ // Stack here: a b 2 g(a+1, b)
+ byte(eth::Instruction::MUL),
+ // Stack here: a b g(a+1, b)*2
+ byte(eth::Instruction::DUP3),
+ byte(eth::Instruction::ADD),
+ // Stack here: a b a+g(a+1, b)*2
+ byte(eth::Instruction::SWAP2),
+ byte(eth::Instruction::POP),
+ byte(eth::Instruction::DUP2),
+ byte(eth::Instruction::JUMPDEST)});
+ BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
+}
+
+BOOST_AUTO_TEST_CASE(negative_literals_8bits)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f() { int8 x = -0x80; }\n"
+ "}\n";
+ bytes code = compileFirstExpression(sourceCode);
+
+ bytes expectation(bytes({byte(eth::Instruction::PUSH32)}) + bytes(31, 0xff) + bytes(1, 0x80));
+ BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
+}
+
+BOOST_AUTO_TEST_CASE(negative_literals_16bits)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f() { int64 x = ~0xabc; }\n"
+ "}\n";
+ bytes code = compileFirstExpression(sourceCode);
+
+ bytes expectation(bytes({byte(eth::Instruction::PUSH32)}) + bytes(30, 0xff) + bytes{0xf5, 0x43});
+ BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
+}
+
+BOOST_AUTO_TEST_CASE(intermediately_overflowing_literals)
+{
+ // first literal itself is too large for 256 bits but it fits after all constant operations
+ // have been applied
+ char const* sourceCode = "contract test {\n"
+ " function f() { var x = (0xffffffffffffffffffffffffffffffffffffffff * 0xffffffffffffffffffffffffff01) & 0xbf; }\n"
+ "}\n";
+ bytes code = compileFirstExpression(sourceCode);
+
+ bytes expectation(bytes({byte(eth::Instruction::PUSH1), 0xbf}));
+ BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
+}
+
+BOOST_AUTO_TEST_CASE(blockhash)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f() {\n"
+ " block.blockhash(3);\n"
+ " }\n"
+ "}\n";
+ bytes code = compileFirstExpression(sourceCode, {}, {},
+ {make_shared<MagicVariableDeclaration>("block", make_shared<MagicType>(MagicType::Kind::Block))});
+
+ bytes expectation({byte(eth::Instruction::PUSH1), 0x03,
+ byte(eth::Instruction::BLOCKHASH)});
+ BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+}
+}
+} // end namespaces
diff --git a/test/libsolidity/SolidityInterface.cpp b/test/libsolidity/SolidityInterface.cpp
new file mode 100644
index 00000000..9c9373f0
--- /dev/null
+++ b/test/libsolidity/SolidityInterface.cpp
@@ -0,0 +1,149 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum 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.
+
+ cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+ */
+/**
+ * @author Christian <c@ethdev.com>
+ * @date 2015
+ * Unit tests for generating source interfaces for Solidity contracts.
+ */
+
+#include "../TestHelper.h"
+#include <libsolidity/CompilerStack.h>
+#include <libsolidity/AST.h>
+
+using namespace std;
+
+namespace dev
+{
+namespace solidity
+{
+namespace test
+{
+
+class SolidityInterfaceChecker
+{
+public:
+ SolidityInterfaceChecker(): m_compilerStack(false) {}
+
+ /// Compiles the given code, generates the interface and parses that again.
+ ContractDefinition const& checkInterface(string const& _code, string const& _contractName = "")
+ {
+ m_code = _code;
+ ETH_TEST_REQUIRE_NO_THROW(m_compilerStack.parse(_code), "Parsing failed");
+ m_interface = m_compilerStack.getMetadata("", DocumentationType::ABISolidityInterface);
+ ETH_TEST_REQUIRE_NO_THROW(m_reCompiler.parse(m_interface), "Interface parsing failed");
+ return m_reCompiler.getContractDefinition(_contractName);
+ }
+
+ string getSourcePart(ASTNode const& _node) const
+ {
+ SourceLocation location = _node.getLocation();
+ BOOST_REQUIRE(!location.isEmpty());
+ return m_interface.substr(location.start, location.end - location.start);
+ }
+
+protected:
+ string m_code;
+ string m_interface;
+ CompilerStack m_compilerStack;
+ CompilerStack m_reCompiler;
+};
+
+BOOST_FIXTURE_TEST_SUITE(SolidityInterface, SolidityInterfaceChecker)
+
+BOOST_AUTO_TEST_CASE(empty_contract)
+{
+ ContractDefinition const& contract = checkInterface("contract test {}");
+ BOOST_CHECK_EQUAL(getSourcePart(contract), "contract test{}");
+}
+
+BOOST_AUTO_TEST_CASE(single_function)
+{
+ ContractDefinition const& contract = checkInterface(
+ "contract test {\n"
+ " function f(uint a) returns(uint d) { return a * 7; }\n"
+ "}\n");
+ BOOST_REQUIRE_EQUAL(1, contract.getDefinedFunctions().size());
+ BOOST_CHECK_EQUAL(getSourcePart(*contract.getDefinedFunctions().front()),
+ "function f(uint256 a)returns(uint256 d);");
+}
+
+BOOST_AUTO_TEST_CASE(single_constant_function)
+{
+ ContractDefinition const& contract = checkInterface(
+ "contract test { function f(uint a) constant returns(bytes1 x) { 1==2; } }");
+ BOOST_REQUIRE_EQUAL(1, contract.getDefinedFunctions().size());
+ BOOST_CHECK_EQUAL(getSourcePart(*contract.getDefinedFunctions().front()),
+ "function f(uint256 a)constant returns(bytes1 x);");
+}
+
+BOOST_AUTO_TEST_CASE(multiple_functions)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f(uint a) returns(uint d) { return a * 7; }\n"
+ " function g(uint b) returns(uint e) { return b * 8; }\n"
+ "}\n";
+ ContractDefinition const& contract = checkInterface(sourceCode);
+ set<string> expectation({"function f(uint256 a)returns(uint256 d);",
+ "function g(uint256 b)returns(uint256 e);"});
+ BOOST_REQUIRE_EQUAL(2, contract.getDefinedFunctions().size());
+ BOOST_CHECK(expectation == set<string>({getSourcePart(*contract.getDefinedFunctions().at(0)),
+ getSourcePart(*contract.getDefinedFunctions().at(1))}));
+}
+
+BOOST_AUTO_TEST_CASE(exclude_fallback_function)
+{
+ char const* sourceCode = "contract test { function() {} }";
+ ContractDefinition const& contract = checkInterface(sourceCode);
+ BOOST_CHECK_EQUAL(getSourcePart(contract), "contract test{}");
+}
+
+BOOST_AUTO_TEST_CASE(events)
+{
+ char const* sourceCode = "contract test {\n"
+ " function f(uint a) returns(uint d) { return a * 7; }\n"
+ " event e1(uint b, address indexed c); \n"
+ " event e2(); \n"
+ "}\n";
+ ContractDefinition const& contract = checkInterface(sourceCode);
+ // events should not appear in the Solidity Interface
+ BOOST_REQUIRE_EQUAL(0, contract.getEvents().size());
+}
+
+BOOST_AUTO_TEST_CASE(inheritance)
+{
+ char const* sourceCode =
+ " contract Base { \n"
+ " function baseFunction(uint p) returns (uint i) { return p; } \n"
+ " event baseEvent(bytes32 indexed evtArgBase); \n"
+ " } \n"
+ " contract Derived is Base { \n"
+ " function derivedFunction(bytes32 p) returns (bytes32 i) { return p; } \n"
+ " event derivedEvent(uint indexed evtArgDerived); \n"
+ " }";
+ ContractDefinition const& contract = checkInterface(sourceCode);
+ set<string> expectedFunctions({"function baseFunction(uint256 p)returns(uint256 i);",
+ "function derivedFunction(bytes32 p)returns(bytes32 i);"});
+ BOOST_REQUIRE_EQUAL(2, contract.getDefinedFunctions().size());
+ BOOST_CHECK(expectedFunctions == set<string>({getSourcePart(*contract.getDefinedFunctions().at(0)),
+ getSourcePart(*contract.getDefinedFunctions().at(1))}));
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+}
+}
+}
diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp
new file mode 100644
index 00000000..3daabc85
--- /dev/null
+++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp
@@ -0,0 +1,2201 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum 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.
+
+ cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * @author Christian <c@ethdev.com>
+ * @date 2014
+ * Unit tests for the name and type resolution of the solidity parser.
+ */
+
+#include <string>
+
+#include <libdevcore/Log.h>
+#include <libdevcore/SHA3.h>
+#include <libsolidity/Scanner.h>
+#include <libsolidity/Parser.h>
+#include <libsolidity/NameAndTypeResolver.h>
+#include <libsolidity/Exceptions.h>
+#include <libsolidity/GlobalContext.h>
+#include "../TestHelper.h"
+
+using namespace std;
+
+namespace dev
+{
+namespace solidity
+{
+namespace test
+{
+
+namespace
+{
+
+ASTPointer<SourceUnit> parseTextAndResolveNames(std::string const& _source)
+{
+ Parser parser;
+ ASTPointer<SourceUnit> sourceUnit = parser.parse(std::make_shared<Scanner>(CharStream(_source)));
+ NameAndTypeResolver resolver({});
+ resolver.registerDeclarations(*sourceUnit);
+ std::shared_ptr<GlobalContext> globalContext = make_shared<GlobalContext>();
+
+ for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
+ if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
+ {
+ globalContext->setCurrentContract(*contract);
+ resolver.updateDeclaration(*globalContext->getCurrentThis());
+ resolver.updateDeclaration(*globalContext->getCurrentSuper());
+ resolver.resolveNamesAndTypes(*contract);
+ }
+ for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
+ if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
+ {
+ globalContext->setCurrentContract(*contract);
+ resolver.updateDeclaration(*globalContext->getCurrentThis());
+ resolver.checkTypeRequirements(*contract);
+ }
+
+ return sourceUnit;
+}
+
+
+static ContractDefinition const* retrieveContract(ASTPointer<SourceUnit> _source, unsigned index)
+{
+ ContractDefinition* contract;
+ unsigned counter = 0;
+ for (ASTPointer<ASTNode> const& node: _source->getNodes())
+ if ((contract = dynamic_cast<ContractDefinition*>(node.get())) && counter == index)
+ return contract;
+
+ return NULL;
+}
+
+static FunctionTypePointer const& retrieveFunctionBySignature(ContractDefinition const* _contract,
+ std::string const& _signature)
+{
+ FixedHash<4> hash(dev::sha3(_signature));
+ return _contract->getInterfaceFunctions()[hash];
+}
+
+}
+
+BOOST_AUTO_TEST_SUITE(SolidityNameAndTypeResolution)
+
+BOOST_AUTO_TEST_CASE(smoke_test)
+{
+ char const* text = "contract test {\n"
+ " uint256 stateVariable1;\n"
+ " function fun(uint256 arg1) { uint256 y; }"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(double_stateVariable_declaration)
+{
+ char const* text = "contract test {\n"
+ " uint256 variable;\n"
+ " uint128 variable;\n"
+ "}\n";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), DeclarationError);
+}
+
+BOOST_AUTO_TEST_CASE(double_function_declaration)
+{
+ char const* text = "contract test {\n"
+ " function fun() { var x; }\n"
+ " function fun() { var x; }\n"
+ "}\n";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), DeclarationError);
+}
+
+BOOST_AUTO_TEST_CASE(double_variable_declaration)
+{
+ char const* text = "contract test {\n"
+ " function f() { uint256 x; if (true) { uint256 x; } }\n"
+ "}\n";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), DeclarationError);
+}
+
+BOOST_AUTO_TEST_CASE(name_shadowing)
+{
+ char const* text = "contract test {\n"
+ " uint256 variable;\n"
+ " function f() { uint32 variable ; }"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(name_references)
+{
+ char const* text = "contract test {\n"
+ " uint256 variable;\n"
+ " function f(uint256 arg) returns (uint out) { f(variable); test; out; }"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(undeclared_name)
+{
+ char const* text = "contract test {\n"
+ " uint256 variable;\n"
+ " function f(uint256 arg) { f(notfound); }"
+ "}\n";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), DeclarationError);
+}
+
+BOOST_AUTO_TEST_CASE(reference_to_later_declaration)
+{
+ char const* text = "contract test {\n"
+ " function g() { f(); }"
+ " function f() { }"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(struct_definition_directly_recursive)
+{
+ char const* text = "contract test {\n"
+ " struct MyStructName {\n"
+ " address addr;\n"
+ " MyStructName x;\n"
+ " }\n"
+ "}\n";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), ParserError);
+}
+
+BOOST_AUTO_TEST_CASE(struct_definition_indirectly_recursive)
+{
+ char const* text = "contract test {\n"
+ " struct MyStructName1 {\n"
+ " address addr;\n"
+ " uint256 count;\n"
+ " MyStructName2 x;\n"
+ " }\n"
+ " struct MyStructName2 {\n"
+ " MyStructName1 x;\n"
+ " }\n"
+ "}\n";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), ParserError);
+}
+
+BOOST_AUTO_TEST_CASE(struct_definition_not_really_recursive)
+{
+ char const* text = R"(
+ contract test {
+ struct s1 { uint a; }
+ struct s2 { s1 x; s1 y; }
+ }
+ )";
+ BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text));
+}
+
+BOOST_AUTO_TEST_CASE(struct_definition_recursion_via_mapping)
+{
+ char const* text = "contract test {\n"
+ " struct MyStructName1 {\n"
+ " address addr;\n"
+ " uint256 count;\n"
+ " mapping(uint => MyStructName1) x;\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(type_inference_smoke_test)
+{
+ char const* text = "contract test {\n"
+ " function f(uint256 arg1, uint32 arg2) returns (bool ret) { var x = arg1 + arg2 == 8; ret = x; }"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(type_checking_return)
+{
+ char const* text = "contract test {\n"
+ " function f() returns (bool r) { return 1 >= 2; }"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(type_checking_return_wrong_number)
+{
+ char const* text = "contract test {\n"
+ " function f() returns (bool r1, bool r2) { return 1 >= 2; }"
+ "}\n";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(type_checking_return_wrong_type)
+{
+ char const* text = "contract test {\n"
+ " function f() returns (uint256 r) { return 1 >= 2; }"
+ "}\n";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(type_checking_function_call)
+{
+ char const* text = "contract test {\n"
+ " function f() returns (bool r) { return g(12, true) == 3; }\n"
+ " function g(uint256 a, bool b) returns (uint256 r) { }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(type_conversion_for_comparison)
+{
+ char const* text = "contract test {\n"
+ " function f() { uint32(2) == int64(2); }"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(type_conversion_for_comparison_invalid)
+{
+ char const* text = "contract test {\n"
+ " function f() { int32(2) == uint64(2); }"
+ "}\n";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(type_inference_explicit_conversion)
+{
+ char const* text = "contract test {\n"
+ " function f() returns (int256 r) { var x = int256(uint32(2)); return x; }"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(large_string_literal)
+{
+ char const* text = "contract test {\n"
+ " function f() { var x = \"123456789012345678901234567890123\"; }"
+ "}\n";
+ BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text));
+}
+
+BOOST_AUTO_TEST_CASE(balance)
+{
+ char const* text = "contract test {\n"
+ " function fun() {\n"
+ " uint256 x = address(0).balance;\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(balance_invalid)
+{
+ char const* text = "contract test {\n"
+ " function fun() {\n"
+ " address(0).balance = 7;\n"
+ " }\n"
+ "}\n";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(assignment_to_mapping)
+{
+ char const* text = "contract test {\n"
+ " struct str {\n"
+ " mapping(uint=>uint) map;\n"
+ " }\n"
+ " str data;"
+ " function fun() {\n"
+ " var a = data.map;\n"
+ " data.map = a;\n"
+ " }\n"
+ "}\n";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(assignment_to_struct)
+{
+ char const* text = "contract test {\n"
+ " struct str {\n"
+ " mapping(uint=>uint) map;\n"
+ " }\n"
+ " str data;"
+ " function fun() {\n"
+ " var a = data;\n"
+ " data = a;\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(returns_in_constructor)
+{
+ char const* text = "contract test {\n"
+ " function test() returns (uint a) {\n"
+ " }\n"
+ "}\n";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(forward_function_reference)
+{
+ char const* text = "contract First {\n"
+ " function fun() returns (bool ret) {\n"
+ " return Second(1).fun(1, true, 3) > 0;\n"
+ " }\n"
+ "}\n"
+ "contract Second {\n"
+ " function fun(uint a, bool b, uint c) returns (uint ret) {\n"
+ " if (First(2).fun() == true) return 1;\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(comparison_bitop_precedence)
+{
+ char const* text = "contract First {\n"
+ " function fun() returns (bool ret) {\n"
+ " return 1 & 2 == 8 & 9 && 1 ^ 2 < 4 | 6;\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(function_no_implementation)
+{
+ ASTPointer<SourceUnit> sourceUnit;
+ char const* text = "contract test {\n"
+ " function functionName(bytes32 input) returns (bytes32 out);\n"
+ "}\n";
+ ETH_TEST_REQUIRE_NO_THROW(sourceUnit = parseTextAndResolveNames(text), "Parsing and name Resolving failed");
+ std::vector<ASTPointer<ASTNode>> nodes = sourceUnit->getNodes();
+ ContractDefinition* contract = dynamic_cast<ContractDefinition*>(nodes[0].get());
+ BOOST_CHECK(contract);
+ BOOST_CHECK(!contract->isFullyImplemented());
+ BOOST_CHECK(!contract->getDefinedFunctions()[0]->isFullyImplemented());
+}
+
+BOOST_AUTO_TEST_CASE(abstract_contract)
+{
+ ASTPointer<SourceUnit> sourceUnit;
+ char const* text = R"(
+ contract base { function foo(); }
+ contract derived is base { function foo() {} }
+ )";
+ ETH_TEST_REQUIRE_NO_THROW(sourceUnit = parseTextAndResolveNames(text), "Parsing and name Resolving failed");
+ std::vector<ASTPointer<ASTNode>> nodes = sourceUnit->getNodes();
+ ContractDefinition* base = dynamic_cast<ContractDefinition*>(nodes[0].get());
+ ContractDefinition* derived = dynamic_cast<ContractDefinition*>(nodes[1].get());
+ BOOST_CHECK(base);
+ BOOST_CHECK(!base->isFullyImplemented());
+ BOOST_CHECK(!base->getDefinedFunctions()[0]->isFullyImplemented());
+ BOOST_CHECK(derived);
+ BOOST_CHECK(derived->isFullyImplemented());
+ BOOST_CHECK(derived->getDefinedFunctions()[0]->isFullyImplemented());
+}
+
+BOOST_AUTO_TEST_CASE(abstract_contract_with_overload)
+{
+ ASTPointer<SourceUnit> sourceUnit;
+ char const* text = R"(
+ contract base { function foo(bool); }
+ contract derived is base { function foo(uint) {} }
+ )";
+ ETH_TEST_REQUIRE_NO_THROW(sourceUnit = parseTextAndResolveNames(text), "Parsing and name Resolving failed");
+ std::vector<ASTPointer<ASTNode>> nodes = sourceUnit->getNodes();
+ ContractDefinition* base = dynamic_cast<ContractDefinition*>(nodes[0].get());
+ ContractDefinition* derived = dynamic_cast<ContractDefinition*>(nodes[1].get());
+ BOOST_REQUIRE(base);
+ BOOST_CHECK(!base->isFullyImplemented());
+ BOOST_REQUIRE(derived);
+ BOOST_CHECK(!derived->isFullyImplemented());
+}
+
+BOOST_AUTO_TEST_CASE(create_abstract_contract)
+{
+ ASTPointer<SourceUnit> sourceUnit;
+ char const* text = R"(
+ contract base { function foo(); }
+ contract derived {
+ base b;
+ function foo() { b = new base();}
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(abstract_contract_constructor_args_optional)
+{
+ ASTPointer<SourceUnit> sourceUnit;
+ char const* text = R"(
+ contract BaseBase { function BaseBase(uint j); }
+ contract base is BaseBase { function foo(); }
+ contract derived is base {
+ function derived(uint i) BaseBase(i){}
+ function foo() {}
+ }
+ )";
+ ETH_TEST_REQUIRE_NO_THROW(parseTextAndResolveNames(text), "Parsing and name resolving failed");
+}
+
+BOOST_AUTO_TEST_CASE(abstract_contract_constructor_args_not_provided)
+{
+ ASTPointer<SourceUnit> sourceUnit;
+ char const* text = R"(
+ contract BaseBase { function BaseBase(uint j); }
+ contract base is BaseBase { function foo(); }
+ contract derived is base {
+ function derived(uint i) {}
+ function foo() {}
+ }
+ )";
+ ETH_TEST_REQUIRE_NO_THROW(sourceUnit = parseTextAndResolveNames(text), "Parsing and name resolving failed");
+ std::vector<ASTPointer<ASTNode>> nodes = sourceUnit->getNodes();
+ BOOST_CHECK_EQUAL(nodes.size(), 3);
+ ContractDefinition* derived = dynamic_cast<ContractDefinition*>(nodes[2].get());
+ BOOST_CHECK(derived);
+ BOOST_CHECK(!derived->isFullyImplemented());
+}
+
+BOOST_AUTO_TEST_CASE(redeclare_implemented_abstract_function_as_abstract)
+{
+ ASTPointer<SourceUnit> sourceUnit;
+ char const* text = R"(
+ contract base { function foo(); }
+ contract derived is base { function foo() {} }
+ contract wrong is derived { function foo(); }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(function_canonical_signature)
+{
+ ASTPointer<SourceUnit> sourceUnit;
+ char const* text = "contract Test {\n"
+ " function foo(uint256 arg1, uint64 arg2, bool arg3) returns (uint256 ret) {\n"
+ " ret = arg1 + arg2;\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_REQUIRE_NO_THROW(sourceUnit = parseTextAndResolveNames(text), "Parsing and name Resolving failed");
+ for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
+ if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
+ {
+ auto functions = contract->getDefinedFunctions();
+ BOOST_CHECK_EQUAL("foo(uint256,uint64,bool)", functions[0]->externalSignature());
+ }
+}
+
+BOOST_AUTO_TEST_CASE(function_canonical_signature_type_aliases)
+{
+ ASTPointer<SourceUnit> sourceUnit;
+ char const* text = "contract Test {\n"
+ " function boo(uint arg1, bytes32 arg2, address arg3) returns (uint ret) {\n"
+ " ret = 5;\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_REQUIRE_NO_THROW(sourceUnit = parseTextAndResolveNames(text), "Parsing and name Resolving failed");
+ for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
+ if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
+ {
+ auto functions = contract->getDefinedFunctions();
+ if (functions.empty())
+ continue;
+ BOOST_CHECK_EQUAL("boo(uint256,bytes32,address)", functions[0]->externalSignature());
+ }
+}
+
+BOOST_AUTO_TEST_CASE(function_external_types)
+{
+ ASTPointer<SourceUnit> sourceUnit;
+ char const* text = R"(
+ contract C {
+ uint a;
+ }
+ contract Test {
+ function boo(uint arg2, bool arg3, bytes8 arg4, bool[2] pairs, uint[] dynamic, C carg, address[] addresses) external returns (uint ret) {
+ ret = 5;
+ }
+ })";
+ ETH_TEST_REQUIRE_NO_THROW(sourceUnit = parseTextAndResolveNames(text), "Parsing and name Resolving failed");
+ for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
+ if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
+ {
+ auto functions = contract->getDefinedFunctions();
+ if (functions.empty())
+ continue;
+ BOOST_CHECK_EQUAL("boo(uint256,bool,bytes8,bool[2],uint256[],address,address[])", functions[0]->externalSignature());
+ }
+}
+
+BOOST_AUTO_TEST_CASE(enum_external_type)
+{
+ // bug #1801
+ ASTPointer<SourceUnit> sourceUnit;
+ char const* text = R"(
+ contract Test {
+ enum ActionChoices { GoLeft, GoRight, GoStraight, Sit }
+ function boo(ActionChoices enumArg) external returns (uint ret) {
+ ret = 5;
+ }
+ })";
+ ETH_TEST_REQUIRE_NO_THROW(sourceUnit = parseTextAndResolveNames(text), "Parsing and name Resolving failed");
+ for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
+ if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
+ {
+ auto functions = contract->getDefinedFunctions();
+ if (functions.empty())
+ continue;
+ BOOST_CHECK_EQUAL("boo(uint8)", functions[0]->externalSignature());
+ }
+}
+
+BOOST_AUTO_TEST_CASE(function_external_call_allowed_conversion)
+{
+ char const* text = R"(
+ contract C {}
+ contract Test {
+ function externalCall() {
+ C arg;
+ this.g(arg);
+ }
+ function g (C c) external {}
+ })";
+ BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text));
+}
+
+BOOST_AUTO_TEST_CASE(function_external_call_not_allowed_conversion)
+{
+ char const* text = R"(
+ contract C {}
+ contract Test {
+ function externalCall() {
+ address arg;
+ this.g(arg);
+ }
+ function g (C c) external {}
+ })";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(function_internal_allowed_conversion)
+{
+ char const* text = R"(
+ contract C {
+ uint a;
+ }
+ contract Test {
+ C a;
+ function g (C c) {}
+ function internalCall() {
+ g(a);
+ }
+ })";
+ BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text));
+}
+
+BOOST_AUTO_TEST_CASE(function_internal_not_allowed_conversion)
+{
+ char const* text = R"(
+ contract C {
+ uint a;
+ }
+ contract Test {
+ address a;
+ function g (C c) {}
+ function internalCall() {
+ g(a);
+ }
+ })";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(hash_collision_in_interface)
+{
+ char const* text = "contract test {\n"
+ " function gsf() {\n"
+ " }\n"
+ " function tgeo() {\n"
+ " }\n"
+ "}\n";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(inheritance_basic)
+{
+ char const* text = R"(
+ contract base { uint baseMember; struct BaseType { uint element; } }
+ contract derived is base {
+ BaseType data;
+ function f() { baseMember = 7; }
+ }
+ )";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(inheritance_diamond_basic)
+{
+ char const* text = R"(
+ contract root { function rootFunction() {} }
+ contract inter1 is root { function f() {} }
+ contract inter2 is root { function f() {} }
+ contract derived is root, inter2, inter1 {
+ function g() { f(); rootFunction(); }
+ }
+ )";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(cyclic_inheritance)
+{
+ char const* text = R"(
+ contract A is B { }
+ contract B is A { }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(legal_override_direct)
+{
+ char const* text = R"(
+ contract B { function f() {} }
+ contract C is B { function f(uint i) {} }
+ )";
+ BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text));
+}
+
+BOOST_AUTO_TEST_CASE(legal_override_indirect)
+{
+ char const* text = R"(
+ contract A { function f(uint a) {} }
+ contract B { function f() {} }
+ contract C is A, B { }
+ )";
+ BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text));
+}
+
+BOOST_AUTO_TEST_CASE(illegal_override_visibility)
+{
+ char const* text = R"(
+ contract B { function f() internal {} }
+ contract C is B { function f() public {} }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(illegal_override_constness)
+{
+ char const* text = R"(
+ contract B { function f() constant {} }
+ contract C is B { function f() {} }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(complex_inheritance)
+{
+ char const* text = R"(
+ contract A { function f() { uint8 x = C(0).g(); } }
+ contract B { function f() {} function g() returns (uint8 r) {} }
+ contract C is A, B { }
+ )";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(constructor_visibility)
+{
+ // The constructor of a base class should not be visible in the derived class
+ char const* text = R"(
+ contract A { function A() { } }
+ contract B is A { function f() { A x = A(0); } }
+ )";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(overriding_constructor)
+{
+ // It is fine to "override" constructor of a base class since it is invisible
+ char const* text = R"(
+ contract A { function A() { } }
+ contract B is A { function A() returns (uint8 r) {} }
+ )";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(missing_base_constructor_arguments)
+{
+ char const* text = R"(
+ contract A { function A(uint a) { } }
+ contract B is A { }
+ )";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(base_constructor_arguments_override)
+{
+ char const* text = R"(
+ contract A { function A(uint a) { } }
+ contract B is A { }
+ )";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(implicit_derived_to_base_conversion)
+{
+ char const* text = R"(
+ contract A { }
+ contract B is A {
+ function f() { A a = B(1); }
+ }
+ )";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(implicit_base_to_derived_conversion)
+{
+ char const* text = R"(
+ contract A { }
+ contract B is A {
+ function f() { B b = A(1); }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(function_modifier_invocation)
+{
+ char const* text = R"(
+ contract B {
+ function f() mod1(2, true) mod2("0123456") { }
+ modifier mod1(uint a, bool b) { if (b) _ }
+ modifier mod2(bytes7 a) { while (a == "1234567") _ }
+ }
+ )";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(invalid_function_modifier_type)
+{
+ char const* text = R"(
+ contract B {
+ function f() mod1(true) { }
+ modifier mod1(uint a) { if (a > 0) _ }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(function_modifier_invocation_parameters)
+{
+ char const* text = R"(
+ contract B {
+ function f(uint8 a) mod1(a, true) mod2(r) returns (bytes7 r) { }
+ modifier mod1(uint a, bool b) { if (b) _ }
+ modifier mod2(bytes7 a) { while (a == "1234567") _ }
+ }
+ )";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(function_modifier_invocation_local_variables)
+{
+ char const* text = R"(
+ contract B {
+ function f() mod(x) { uint x = 7; }
+ modifier mod(uint a) { if (a > 0) _ }
+ }
+ )";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(legal_modifier_override)
+{
+ char const* text = R"(
+ contract A { modifier mod(uint a) {} }
+ contract B is A { modifier mod(uint a) {} }
+ )";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(illegal_modifier_override)
+{
+ char const* text = R"(
+ contract A { modifier mod(uint a) {} }
+ contract B is A { modifier mod(uint8 a) {} }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(modifier_overrides_function)
+{
+ char const* text = R"(
+ contract A { modifier mod(uint a) {} }
+ contract B is A { function mod(uint a) {} }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(function_overrides_modifier)
+{
+ char const* text = R"(
+ contract A { function mod(uint a) {} }
+ contract B is A { modifier mod(uint a) {} }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(modifier_returns_value)
+{
+ char const* text = R"(
+ contract A {
+ function f(uint a) mod(2) returns (uint r) {}
+ modifier mod(uint a) { return 7; }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(state_variable_accessors)
+{
+ char const* text = "contract test {\n"
+ " function fun() {\n"
+ " uint64(2);\n"
+ " }\n"
+ "uint256 public foo;\n"
+ "mapping(uint=>bytes4) public map;\n"
+ "mapping(uint=>mapping(uint=>bytes4)) public multiple_map;\n"
+ "}\n";
+
+ ASTPointer<SourceUnit> source;
+ ContractDefinition const* contract;
+ ETH_TEST_CHECK_NO_THROW(source = parseTextAndResolveNames(text), "Parsing and Resolving names failed");
+ BOOST_REQUIRE((contract = retrieveContract(source, 0)) != nullptr);
+ FunctionTypePointer function = retrieveFunctionBySignature(contract, "foo()");
+ BOOST_REQUIRE(function && function->hasDeclaration());
+ auto returnParams = function->getReturnParameterTypeNames();
+ BOOST_CHECK_EQUAL(returnParams.at(0), "uint256");
+ BOOST_CHECK(function->isConstant());
+
+ function = retrieveFunctionBySignature(contract, "map(uint256)");
+ BOOST_REQUIRE(function && function->hasDeclaration());
+ auto params = function->getParameterTypeNames();
+ BOOST_CHECK_EQUAL(params.at(0), "uint256");
+ returnParams = function->getReturnParameterTypeNames();
+ BOOST_CHECK_EQUAL(returnParams.at(0), "bytes4");
+ BOOST_CHECK(function->isConstant());
+
+ function = retrieveFunctionBySignature(contract, "multiple_map(uint256,uint256)");
+ BOOST_REQUIRE(function && function->hasDeclaration());
+ params = function->getParameterTypeNames();
+ BOOST_CHECK_EQUAL(params.at(0), "uint256");
+ BOOST_CHECK_EQUAL(params.at(1), "uint256");
+ returnParams = function->getReturnParameterTypeNames();
+ BOOST_CHECK_EQUAL(returnParams.at(0), "bytes4");
+ BOOST_CHECK(function->isConstant());
+}
+
+BOOST_AUTO_TEST_CASE(function_clash_with_state_variable_accessor)
+{
+ char const* text = "contract test {\n"
+ " function fun() {\n"
+ " uint64(2);\n"
+ " }\n"
+ "uint256 foo;\n"
+ " function foo() {}\n"
+ "}\n";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), DeclarationError);
+}
+
+BOOST_AUTO_TEST_CASE(private_state_variable)
+{
+ char const* text = "contract test {\n"
+ " function fun() {\n"
+ " uint64(2);\n"
+ " }\n"
+ "uint256 private foo;\n"
+ "uint256 internal bar;\n"
+ "}\n";
+
+ ASTPointer<SourceUnit> source;
+ ContractDefinition const* contract;
+ ETH_TEST_CHECK_NO_THROW(source = parseTextAndResolveNames(text), "Parsing and Resolving names failed");
+ BOOST_CHECK((contract = retrieveContract(source, 0)) != nullptr);
+ FunctionTypePointer function;
+ function = retrieveFunctionBySignature(contract, "foo()");
+ BOOST_CHECK_MESSAGE(function == nullptr, "Accessor function of a private variable should not exist");
+ function = retrieveFunctionBySignature(contract, "bar()");
+ BOOST_CHECK_MESSAGE(function == nullptr, "Accessor function of an internal variable should not exist");
+}
+
+BOOST_AUTO_TEST_CASE(base_class_state_variable_accessor)
+{
+ // test for issue #1126 https://github.com/ethereum/cpp-ethereum/issues/1126
+ char const* text = "contract Parent {\n"
+ " uint256 public m_aMember;\n"
+ "}\n"
+ "contract Child is Parent{\n"
+ " function foo() returns (uint256) { return Parent.m_aMember; }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(base_class_state_variable_internal_member)
+{
+ char const* text = "contract Parent {\n"
+ " uint256 internal m_aMember;\n"
+ "}\n"
+ "contract Child is Parent{\n"
+ " function foo() returns (uint256) { return Parent.m_aMember; }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(state_variable_member_of_wrong_class1)
+{
+ char const* text = "contract Parent1 {\n"
+ " uint256 internal m_aMember1;\n"
+ "}\n"
+ "contract Parent2 is Parent1{\n"
+ " uint256 internal m_aMember2;\n"
+ "}\n"
+ "contract Child is Parent2{\n"
+ " function foo() returns (uint256) { return Parent2.m_aMember1; }\n"
+ "}\n";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(state_variable_member_of_wrong_class2)
+{
+ char const* text = "contract Parent1 {\n"
+ " uint256 internal m_aMember1;\n"
+ "}\n"
+ "contract Parent2 is Parent1{\n"
+ " uint256 internal m_aMember2;\n"
+ "}\n"
+ "contract Child is Parent2{\n"
+ " function foo() returns (uint256) { return Child.m_aMember2; }\n"
+ " uint256 public m_aMember3;\n"
+ "}\n";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(fallback_function)
+{
+ char const* text = R"(
+ contract C {
+ uint x;
+ function() { x = 2; }
+ }
+ )";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(fallback_function_with_arguments)
+{
+ char const* text = R"(
+ contract C {
+ uint x;
+ function(uint a) { x = 2; }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(fallback_function_twice)
+{
+ char const* text = R"(
+ contract C {
+ uint x;
+ function() { x = 2; }
+ function() { x = 3; }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), DeclarationError);
+}
+
+BOOST_AUTO_TEST_CASE(fallback_function_inheritance)
+{
+ char const* text = R"(
+ contract A {
+ uint x;
+ function() { x = 1; }
+ }
+ contract C is A {
+ function() { x = 2; }
+ }
+ )";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(event)
+{
+ char const* text = R"(
+ contract c {
+ event e(uint indexed a, bytes3 indexed s, bool indexed b);
+ function f() { e(2, "abc", true); }
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(event_too_many_indexed)
+{
+ char const* text = R"(
+ contract c {
+ event e(uint indexed a, bytes3 indexed b, bool indexed c, uint indexed d);
+ function f() { e(2, "abc", true); }
+ })";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(event_call)
+{
+ char const* text = R"(
+ contract c {
+ event e(uint a, bytes3 indexed s, bool indexed b);
+ function f() { e(2, "abc", true); }
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(event_inheritance)
+{
+ char const* text = R"(
+ contract base {
+ event e(uint a, bytes3 indexed s, bool indexed b);
+ }
+ contract c is base {
+ function f() { e(2, "abc", true); }
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(multiple_events_argument_clash)
+{
+ char const* text = R"(
+ contract c {
+ event e1(uint a, uint e1, uint e2);
+ event e2(uint a, uint e1, uint e2);
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(access_to_default_function_visibility)
+{
+ char const* text = R"(
+ contract c {
+ function f() {}
+ }
+ contract d {
+ function g() { c(0).f(); }
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(access_to_internal_function)
+{
+ char const* text = R"(
+ contract c {
+ function f() internal {}
+ }
+ contract d {
+ function g() { c(0).f(); }
+ })";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(access_to_default_state_variable_visibility)
+{
+ char const* text = R"(
+ contract c {
+ uint a;
+ }
+ contract d {
+ function g() { c(0).a(); }
+ })";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(access_to_internal_state_variable)
+{
+ char const* text = R"(
+ contract c {
+ uint public a;
+ }
+ contract d {
+ function g() { c(0).a(); }
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(error_count_in_named_args)
+{
+ char const* sourceCode = "contract test {\n"
+ " function a(uint a, uint b) returns (uint r) { r = a + b; }\n"
+ " function b() returns (uint r) { r = a({a: 1}); }\n"
+ "}\n";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(empty_in_named_args)
+{
+ char const* sourceCode = "contract test {\n"
+ " function a(uint a, uint b) returns (uint r) { r = a + b; }\n"
+ " function b() returns (uint r) { r = a({}); }\n"
+ "}\n";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(duplicate_parameter_names_in_named_args)
+{
+ char const* sourceCode = "contract test {\n"
+ " function a(uint a, uint b) returns (uint r) { r = a + b; }\n"
+ " function b() returns (uint r) { r = a({a: 1, a: 2}); }\n"
+ "}\n";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(invalid_parameter_names_in_named_args)
+{
+ char const* sourceCode = "contract test {\n"
+ " function a(uint a, uint b) returns (uint r) { r = a + b; }\n"
+ " function b() returns (uint r) { r = a({a: 1, c: 2}); }\n"
+ "}\n";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(empty_name_input_parameter)
+{
+ char const* text = R"(
+ contract test {
+ function f(uint){
+ }
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(empty_name_return_parameter)
+{
+ char const* text = R"(
+ contract test {
+ function f() returns(bool){
+ }
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(empty_name_input_parameter_with_named_one)
+{
+ char const* text = R"(
+ contract test {
+ function f(uint, uint k) returns(uint ret_k){
+ return k;
+ }
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(empty_name_return_parameter_with_named_one)
+{
+ char const* text = R"(
+ contract test {
+ function f() returns(uint ret_k, uint){
+ return 5;
+ }
+ })";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(disallow_declaration_of_void_type)
+{
+ char const* sourceCode = "contract c { function f() { var x = f(); } }";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(overflow_caused_by_ether_units)
+{
+ char const* sourceCodeFine = R"(
+ contract c {
+ function c ()
+ {
+ a = 115792089237316195423570985008687907853269984665640564039458;
+ }
+ uint256 a;
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(sourceCodeFine),
+ "Parsing and Resolving names failed");
+ char const* sourceCode = R"(
+ contract c {
+ function c ()
+ {
+ a = 115792089237316195423570985008687907853269984665640564039458 ether;
+ }
+ uint256 a;
+ })";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(exp_operator_negative_exponent)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f() returns(uint d) { return 2 ** -3; }
+ })";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(exp_operator_exponent_too_big)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f() returns(uint d) { return 2 ** 10000000000; }
+ })";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(enum_member_access)
+{
+ char const* text = R"(
+ contract test {
+ enum ActionChoices { GoLeft, GoRight, GoStraight, Sit }
+ function test()
+ {
+ choices = ActionChoices.GoStraight;
+ }
+ ActionChoices choices;
+ }
+ )";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(enum_invalid_member_access)
+{
+ char const* text = R"(
+ contract test {
+ enum ActionChoices { GoLeft, GoRight, GoStraight, Sit }
+ function test()
+ {
+ choices = ActionChoices.RunAroundWavingYourHands;
+ }
+ ActionChoices choices;
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(enum_explicit_conversion_is_okay)
+{
+ char const* text = R"(
+ contract test {
+ enum ActionChoices { GoLeft, GoRight, GoStraight, Sit }
+ function test()
+ {
+ a = uint256(ActionChoices.GoStraight);
+ b = uint64(ActionChoices.Sit);
+ }
+ uint256 a;
+ uint64 b;
+ }
+ )";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(int_to_enum_explicit_conversion_is_okay)
+{
+ char const* text = R"(
+ contract test {
+ enum ActionChoices { GoLeft, GoRight, GoStraight, Sit }
+ function test()
+ {
+ a = 2;
+ b = ActionChoices(a);
+ }
+ uint256 a;
+ ActionChoices b;
+ }
+ )";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(enum_implicit_conversion_is_not_okay)
+{
+ char const* text = R"(
+ contract test {
+ enum ActionChoices { GoLeft, GoRight, GoStraight, Sit }
+ function test()
+ {
+ a = ActionChoices.GoStraight;
+ b = ActionChoices.Sit;
+ }
+ uint256 a;
+ uint64 b;
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(enum_duplicate_values)
+{
+ char const* text = R"(
+ contract test {
+ enum ActionChoices { GoLeft, GoRight, GoLeft, Sit }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), DeclarationError);
+}
+
+BOOST_AUTO_TEST_CASE(private_visibility)
+{
+ char const* sourceCode = R"(
+ contract base {
+ function f() private {}
+ }
+ contract derived is base {
+ function g() { f(); }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), DeclarationError);
+}
+
+BOOST_AUTO_TEST_CASE(private_visibility_via_explicit_base_access)
+{
+ char const* sourceCode = R"(
+ contract base {
+ function f() private {}
+ }
+ contract derived is base {
+ function g() { base.f(); }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(external_visibility)
+{
+ char const* sourceCode = R"(
+ contract c {
+ function f() external {}
+ function g() { f(); }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), DeclarationError);
+}
+
+BOOST_AUTO_TEST_CASE(external_base_visibility)
+{
+ char const* sourceCode = R"(
+ contract base {
+ function f() external {}
+ }
+ contract derived is base {
+ function g() { base.f(); }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(external_argument_assign)
+{
+ char const* sourceCode = R"(
+ contract c {
+ function f(uint a) external { a = 1; }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(external_argument_increment)
+{
+ char const* sourceCode = R"(
+ contract c {
+ function f(uint a) external { a++; }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(external_argument_delete)
+{
+ char const* sourceCode = R"(
+ contract c {
+ function f(uint a) external { delete a; }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(test_for_bug_override_function_with_bytearray_type)
+{
+ char const* sourceCode = R"(
+ contract Vehicle {
+ function f(bytes _a) external returns (uint256 r) {r = 1;}
+ }
+ contract Bike is Vehicle {
+ function f(bytes _a) external returns (uint256 r) {r = 42;}
+ }
+ )";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(sourceCode), "Parsing and Name Resolving failed");
+}
+
+BOOST_AUTO_TEST_CASE(array_with_nonconstant_length)
+{
+ char const* text = R"(
+ contract c {
+ function f(uint a) { uint8[a] x; }
+ })";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(array_copy_with_different_types1)
+{
+ char const* text = R"(
+ contract c {
+ bytes a;
+ uint[] b;
+ function f() { b = a; }
+ })";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(array_copy_with_different_types2)
+{
+ char const* text = R"(
+ contract c {
+ uint32[] a;
+ uint8[] b;
+ function f() { b = a; }
+ })";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(array_copy_with_different_types_conversion_possible)
+{
+ char const* text = R"(
+ contract c {
+ uint32[] a;
+ uint8[] b;
+ function f() { a = b; }
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(array_copy_with_different_types_static_dynamic)
+{
+ char const* text = R"(
+ contract c {
+ uint32[] a;
+ uint8[80] b;
+ function f() { a = b; }
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
+}
+
+BOOST_AUTO_TEST_CASE(array_copy_with_different_types_dynamic_static)
+{
+ char const* text = R"(
+ contract c {
+ uint[] a;
+ uint[80] b;
+ function f() { b = a; }
+ })";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(storage_variable_initialization_with_incorrect_type_int)
+{
+ char const* text = R"(
+ contract c {
+ uint8 a = 1000;
+ })";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(storage_variable_initialization_with_incorrect_type_string)
+{
+ char const* text = R"(
+ contract c {
+ uint a = "abc";
+ })";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(test_fromElementaryTypeName)
+{
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int) == *make_shared<IntegerType>(256, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int8) == *make_shared<IntegerType>(8, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int16) == *make_shared<IntegerType>(16, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int24) == *make_shared<IntegerType>(24, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int32) == *make_shared<IntegerType>(32, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int40) == *make_shared<IntegerType>(40, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int48) == *make_shared<IntegerType>(48, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int56) == *make_shared<IntegerType>(56, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int64) == *make_shared<IntegerType>(64, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int72) == *make_shared<IntegerType>(72, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int80) == *make_shared<IntegerType>(80, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int88) == *make_shared<IntegerType>(88, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int96) == *make_shared<IntegerType>(96, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int104) == *make_shared<IntegerType>(104, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int112) == *make_shared<IntegerType>(112, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int120) == *make_shared<IntegerType>(120, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int128) == *make_shared<IntegerType>(128, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int136) == *make_shared<IntegerType>(136, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int144) == *make_shared<IntegerType>(144, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int152) == *make_shared<IntegerType>(152, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int160) == *make_shared<IntegerType>(160, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int168) == *make_shared<IntegerType>(168, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int176) == *make_shared<IntegerType>(176, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int184) == *make_shared<IntegerType>(184, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int192) == *make_shared<IntegerType>(192, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int200) == *make_shared<IntegerType>(200, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int208) == *make_shared<IntegerType>(208, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int216) == *make_shared<IntegerType>(216, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int224) == *make_shared<IntegerType>(224, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int232) == *make_shared<IntegerType>(232, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int240) == *make_shared<IntegerType>(240, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int248) == *make_shared<IntegerType>(248, IntegerType::Modifier::Signed));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Int256) == *make_shared<IntegerType>(256, IntegerType::Modifier::Signed));
+
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt) == *make_shared<IntegerType>(256, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt8) == *make_shared<IntegerType>(8, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt16) == *make_shared<IntegerType>(16, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt24) == *make_shared<IntegerType>(24, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt32) == *make_shared<IntegerType>(32, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt40) == *make_shared<IntegerType>(40, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt48) == *make_shared<IntegerType>(48, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt56) == *make_shared<IntegerType>(56, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt64) == *make_shared<IntegerType>(64, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt72) == *make_shared<IntegerType>(72, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt80) == *make_shared<IntegerType>(80, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt88) == *make_shared<IntegerType>(88, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt96) == *make_shared<IntegerType>(96, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt104) == *make_shared<IntegerType>(104, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt112) == *make_shared<IntegerType>(112, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt120) == *make_shared<IntegerType>(120, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt128) == *make_shared<IntegerType>(128, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt136) == *make_shared<IntegerType>(136, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt144) == *make_shared<IntegerType>(144, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt152) == *make_shared<IntegerType>(152, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt160) == *make_shared<IntegerType>(160, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt168) == *make_shared<IntegerType>(168, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt176) == *make_shared<IntegerType>(176, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt184) == *make_shared<IntegerType>(184, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt192) == *make_shared<IntegerType>(192, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt200) == *make_shared<IntegerType>(200, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt208) == *make_shared<IntegerType>(208, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt216) == *make_shared<IntegerType>(216, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt224) == *make_shared<IntegerType>(224, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt232) == *make_shared<IntegerType>(232, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt240) == *make_shared<IntegerType>(240, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt248) == *make_shared<IntegerType>(248, IntegerType::Modifier::Unsigned));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt256) == *make_shared<IntegerType>(256, IntegerType::Modifier::Unsigned));
+
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Byte) == *make_shared<FixedBytesType>(1));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes1) == *make_shared<FixedBytesType>(1));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes2) == *make_shared<FixedBytesType>(2));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes3) == *make_shared<FixedBytesType>(3));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes4) == *make_shared<FixedBytesType>(4));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes5) == *make_shared<FixedBytesType>(5));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes6) == *make_shared<FixedBytesType>(6));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes7) == *make_shared<FixedBytesType>(7));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes8) == *make_shared<FixedBytesType>(8));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes9) == *make_shared<FixedBytesType>(9));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes10) == *make_shared<FixedBytesType>(10));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes11) == *make_shared<FixedBytesType>(11));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes12) == *make_shared<FixedBytesType>(12));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes13) == *make_shared<FixedBytesType>(13));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes14) == *make_shared<FixedBytesType>(14));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes15) == *make_shared<FixedBytesType>(15));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes16) == *make_shared<FixedBytesType>(16));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes17) == *make_shared<FixedBytesType>(17));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes18) == *make_shared<FixedBytesType>(18));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes19) == *make_shared<FixedBytesType>(19));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes20) == *make_shared<FixedBytesType>(20));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes21) == *make_shared<FixedBytesType>(21));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes22) == *make_shared<FixedBytesType>(22));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes23) == *make_shared<FixedBytesType>(23));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes24) == *make_shared<FixedBytesType>(24));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes25) == *make_shared<FixedBytesType>(25));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes26) == *make_shared<FixedBytesType>(26));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes27) == *make_shared<FixedBytesType>(27));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes28) == *make_shared<FixedBytesType>(28));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes29) == *make_shared<FixedBytesType>(29));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes30) == *make_shared<FixedBytesType>(30));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes31) == *make_shared<FixedBytesType>(31));
+ BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes32) == *make_shared<FixedBytesType>(32));
+}
+
+BOOST_AUTO_TEST_CASE(test_byte_is_alias_of_byte1)
+{
+ char const* text = R"(
+ contract c {
+ bytes arr;
+ function f() { byte a = arr[0];}
+ })";
+ ETH_TEST_REQUIRE_NO_THROW(parseTextAndResolveNames(text), "Type resolving failed");
+}
+
+BOOST_AUTO_TEST_CASE(assigning_value_to_const_variable)
+{
+ char const* text = R"(
+ contract Foo {
+ function changeIt() { x = 9; }
+ uint constant x = 56;
+ })";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(complex_const_variable)
+{
+ //for now constant specifier is valid only for uint bytesXX and enums
+ char const* text = R"(
+ contract Foo {
+ mapping(uint => bool) constant mapVar;
+ })";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(uninitialized_const_variable)
+{
+ char const* text = R"(
+ contract Foo {
+ uint constant y;
+ })";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(local_const_variable)
+{
+ char const* text = R"(
+ contract Foo {
+ function localConst() returns (uint ret)
+ {
+ uint constant local = 4;
+ return local;
+ }
+ })";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), ParserError);
+}
+
+BOOST_AUTO_TEST_CASE(overloaded_function_cannot_resolve)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f() returns(uint) { return 1; }
+ function f(uint a) returns(uint) { return a; }
+ function g() returns(uint) { return f(3, 5); }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(ambiguous_overloaded_function)
+{
+ // literal 1 can be both converted to uint and uint8, so the call is ambiguous.
+ char const* sourceCode = R"(
+ contract test {
+ function f(uint8 a) returns(uint) { return a; }
+ function f(uint a) returns(uint) { return 2*a; }
+ function g() returns(uint) { return f(1); }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(assignment_of_nonoverloaded_function)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f(uint a) returns(uint) { return 2 * a; }
+ function g() returns(uint) { var x = f; return x(7); }
+ }
+ )";
+ ETH_TEST_REQUIRE_NO_THROW(parseTextAndResolveNames(sourceCode), "Type resolving failed");
+}
+
+BOOST_AUTO_TEST_CASE(assignment_of_overloaded_function)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f() returns(uint) { return 1; }
+ function f(uint a) returns(uint) { return 2 * a; }
+ function g() returns(uint) { var x = f; return x(7); }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(external_types_clash)
+{
+ char const* sourceCode = R"(
+ contract base {
+ enum a { X }
+ function f(a) { }
+ }
+ contract test is base {
+ function f(uint8 a) { }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(override_changes_return_types)
+{
+ char const* sourceCode = R"(
+ contract base {
+ function f(uint a) returns (uint) { }
+ }
+ contract test is base {
+ function f(uint a) returns (uint8) { }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(multiple_constructors)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function test(uint a) { }
+ function test() {}
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), DeclarationError);
+}
+
+BOOST_AUTO_TEST_CASE(equal_overload)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function test(uint a) returns (uint b) { }
+ function test(uint a) external {}
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), DeclarationError);
+}
+
+BOOST_AUTO_TEST_CASE(uninitialized_var)
+{
+ char const* sourceCode = R"(
+ contract C {
+ function f() returns (uint) { var x; return 2; }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(string)
+{
+ char const* sourceCode = R"(
+ contract C {
+ string s;
+ function f(string x) external { s = x; }
+ }
+ )";
+ BOOST_CHECK_NO_THROW(parseTextAndResolveNames(sourceCode));
+}
+
+BOOST_AUTO_TEST_CASE(string_index)
+{
+ char const* sourceCode = R"(
+ contract C {
+ string s;
+ function f() { var a = s[2]; }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(string_length)
+{
+ char const* sourceCode = R"(
+ contract C {
+ string s;
+ function f() { var a = s.length; }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(negative_integers_to_signed_out_of_bound)
+{
+ char const* sourceCode = R"(
+ contract test {
+ int8 public i = -129;
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(negative_integers_to_signed_min)
+{
+ char const* sourceCode = R"(
+ contract test {
+ int8 public i = -128;
+ }
+ )";
+ BOOST_CHECK_NO_THROW(parseTextAndResolveNames(sourceCode));
+}
+
+BOOST_AUTO_TEST_CASE(positive_integers_to_signed_out_of_bound)
+{
+ char const* sourceCode = R"(
+ contract test {
+ int8 public j = 128;
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(positive_integers_to_signed_out_of_bound_max)
+{
+ char const* sourceCode = R"(
+ contract test {
+ int8 public j = 127;
+ }
+ )";
+ BOOST_CHECK_NO_THROW(parseTextAndResolveNames(sourceCode));
+}
+
+BOOST_AUTO_TEST_CASE(negative_integers_to_unsigned)
+{
+ char const* sourceCode = R"(
+ contract test {
+ uint8 public x = -1;
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(positive_integers_to_unsigned_out_of_bound)
+{
+ char const* sourceCode = R"(
+ contract test {
+ uint8 public x = 700;
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(integer_boolean_operators)
+{
+ char const* sourceCode1 = R"(
+ contract test { function() { uint x = 1; uint y = 2; x || y; } }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode1), TypeError);
+ char const* sourceCode2 = R"(
+ contract test { function() { uint x = 1; uint y = 2; x && y; } }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode2), TypeError);
+ char const* sourceCode3 = R"(
+ contract test { function() { uint x = 1; !x; } }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode3), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(reference_compare_operators)
+{
+ char const* sourceCode1 = R"(
+ contract test { bytes a; bytes b; function() { a == b; } }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode1), TypeError);
+ char const* sourceCode2 = R"(
+ contract test { struct s {uint a;} s x; s y; function() { x == y; } }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode2), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(overwrite_memory_location_external)
+{
+ char const* sourceCode = R"(
+ contract C {
+ function f(uint[] memory a) external {}
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(overwrite_storage_location_external)
+{
+ char const* sourceCode = R"(
+ contract C {
+ function f(uint[] storage a) external {}
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(storage_location_local_variables)
+{
+ char const* sourceCode = R"(
+ contract C {
+ function f() {
+ uint[] storage x;
+ uint[] memory y;
+ uint[] memory z;
+ }
+ }
+ )";
+ BOOST_CHECK_NO_THROW(parseTextAndResolveNames(sourceCode));
+}
+
+BOOST_AUTO_TEST_CASE(no_mappings_in_memory_array)
+{
+ char const* sourceCode = R"(
+ contract C {
+ function f() {
+ mapping(uint=>uint)[] memory x;
+ }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(assignment_mem_to_local_storage_variable)
+{
+ char const* sourceCode = R"(
+ contract C {
+ uint[] data;
+ function f(uint[] x) {
+ var dataRef = data;
+ dataRef = x;
+ }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(storage_assign_to_different_local_variable)
+{
+ char const* sourceCode = R"(
+ contract C {
+ uint[] data;
+ uint8[] otherData;
+ function f() {
+ uint8[] storage x = otherData;
+ uint[] storage y = data;
+ y = x;
+ // note that data = otherData works
+ }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(no_delete_on_storage_pointers)
+{
+ char const* sourceCode = R"(
+ contract C {
+ uint[] data;
+ function f() {
+ var x = data;
+ delete x;
+ }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(assignment_mem_storage_variable_directly)
+{
+ char const* sourceCode = R"(
+ contract C {
+ uint[] data;
+ function f(uint[] x) {
+ data = x;
+ }
+ }
+ )";
+ BOOST_CHECK_NO_THROW(parseTextAndResolveNames(sourceCode));
+}
+
+BOOST_AUTO_TEST_CASE(function_argument_mem_to_storage)
+{
+ char const* sourceCode = R"(
+ contract C {
+ function f(uint[] storage x) private {
+ }
+ function g(uint[] x) {
+ f(x);
+ }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(function_argument_storage_to_mem)
+{
+ char const* sourceCode = R"(
+ contract C {
+ function f(uint[] storage x) private {
+ g(x);
+ }
+ function g(uint[] x) {
+ }
+ }
+ )";
+ BOOST_CHECK_NO_THROW(parseTextAndResolveNames(sourceCode));
+}
+
+BOOST_AUTO_TEST_CASE(mem_array_assignment_changes_base_type)
+{
+ // Such an assignment is possible in storage, but not in memory
+ // (because it would incur an otherwise unnecessary copy).
+ // This requirement might be lifted, though.
+ char const* sourceCode = R"(
+ contract C {
+ function f(uint8[] memory x) private {
+ uint[] memory y = x;
+ }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(dynamic_return_types_not_possible)
+{
+ char const* sourceCode = R"(
+ contract C {
+ function f(uint) returns (string);
+ function g() {
+ var x = this.f(2);
+ }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(memory_arrays_not_resizeable)
+{
+ char const* sourceCode = R"(
+ contract C {
+ function f() {
+ uint[] memory x;
+ x.length = 2;
+ }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(struct_constructor)
+{
+ char const* sourceCode = R"(
+ contract C {
+ struct S { uint a; bool x; }
+ function f() {
+ S memory s = S(1, true);
+ }
+ }
+ )";
+ BOOST_CHECK_NO_THROW(parseTextAndResolveNames(sourceCode));
+}
+
+BOOST_AUTO_TEST_CASE(struct_constructor_nested)
+{
+ char const* sourceCode = R"(
+ contract C {
+ struct X { uint x1; uint x2; }
+ struct S { uint s1; uint[3] s2; X s3; }
+ function f() {
+ uint[3] memory s2;
+ S memory s = S(1, s2, X(4, 5));
+ }
+ }
+ )";
+ BOOST_CHECK_NO_THROW(parseTextAndResolveNames(sourceCode));
+}
+
+BOOST_AUTO_TEST_CASE(struct_named_constructor)
+{
+ char const* sourceCode = R"(
+ contract C {
+ struct S { uint a; bool x; }
+ function f() {
+ S memory s = S({a: 1, x: true});
+ }
+ }
+ )";
+ BOOST_CHECK_NO_THROW(parseTextAndResolveNames(sourceCode));
+}
+
+BOOST_AUTO_TEST_CASE(literal_strings)
+{
+ char const* text = R"(
+ contract Foo {
+ function f() {
+ string memory long = "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
+ string memory short = "123";
+ }
+ }
+ )";
+ BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text));
+}
+
+BOOST_AUTO_TEST_CASE(invalid_integer_literal_fraction)
+{
+ char const* text = R"(
+ contract Foo {
+ function f() {
+ var x = 1.20;
+ }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(invalid_integer_literal_exp)
+{
+ char const* text = R"(
+ contract Foo {
+ function f() {
+ var x = 1e2;
+ }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(memory_structs_with_mappings)
+{
+ char const* text = R"(
+ contract Test {
+ struct S { uint8 a; mapping(uint => uint) b; uint8 c; }
+ S s;
+ function f() {
+ S memory x;
+ x.b[1];
+ }
+ }
+ )";
+ BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
+}
+
+BOOST_AUTO_TEST_CASE(string_bytes_conversion)
+{
+ char const* text = R"(
+ contract Test {
+ string s;
+ bytes b;
+ function h(string _s) external { bytes(_s).length; }
+ function i(string _s) internal { bytes(_s).length; }
+ function j() internal { bytes(s).length; }
+ function k(bytes _b) external { string(_b); }
+ function l(bytes _b) internal { string(_b); }
+ function m() internal { string(b); }
+ }
+ )";
+ BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text));
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+}
+}
+} // end namespaces
diff --git a/test/libsolidity/SolidityNatspecJSON.cpp b/test/libsolidity/SolidityNatspecJSON.cpp
new file mode 100644
index 00000000..73c080f7
--- /dev/null
+++ b/test/libsolidity/SolidityNatspecJSON.cpp
@@ -0,0 +1,534 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum 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.
+
+ cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+ */
+/**
+ * @author Lefteris Karapetsas <lefteris@ethdev.com>
+ * @date 2014
+ * Unit tests for the solidity compiler JSON Interface output.
+ */
+
+#include "../TestHelper.h"
+#include <string>
+#include <json/json.h>
+#include <libsolidity/CompilerStack.h>
+#include <libsolidity/Exceptions.h>
+#include <libdevcore/Exceptions.h>
+
+namespace dev
+{
+namespace solidity
+{
+namespace test
+{
+
+class DocumentationChecker
+{
+public:
+ DocumentationChecker(): m_compilerStack(false) {}
+
+ void checkNatspec(
+ std::string const& _code,
+ std::string const& _expectedDocumentationString,
+ bool _userDocumentation
+ )
+ {
+ std::string generatedDocumentationString;
+ ETH_TEST_REQUIRE_NO_THROW(m_compilerStack.parse(_code), "Parsing failed");
+
+ if (_userDocumentation)
+ generatedDocumentationString = m_compilerStack.getMetadata("", DocumentationType::NatspecUser);
+ else
+ generatedDocumentationString = m_compilerStack.getMetadata("", DocumentationType::NatspecDev);
+ Json::Value generatedDocumentation;
+ m_reader.parse(generatedDocumentationString, generatedDocumentation);
+ Json::Value expectedDocumentation;
+ m_reader.parse(_expectedDocumentationString, expectedDocumentation);
+ BOOST_CHECK_MESSAGE(
+ expectedDocumentation == generatedDocumentation,
+ "Expected " << _expectedDocumentationString <<
+ "\n but got:\n" << generatedDocumentationString
+ );
+ }
+
+private:
+ CompilerStack m_compilerStack;
+ Json::Reader m_reader;
+};
+
+BOOST_FIXTURE_TEST_SUITE(SolidityNatspecJSON, DocumentationChecker)
+
+BOOST_AUTO_TEST_CASE(user_basic_test)
+{
+ char const* sourceCode = "contract test {\n"
+ " /// @notice Multiplies `a` by 7\n"
+ " function mul(uint a) returns(uint d) { return a * 7; }\n"
+ "}\n";
+
+ char const* natspec = "{"
+ "\"methods\":{"
+ " \"mul(uint256)\":{ \"notice\": \"Multiplies `a` by 7\"}"
+ "}}";
+
+ checkNatspec(sourceCode, natspec, true);
+}
+
+BOOST_AUTO_TEST_CASE(dev_and_user_basic_test)
+{
+ char const* sourceCode = "contract test {\n"
+ " /// @notice Multiplies `a` by 7\n"
+ " /// @dev Multiplies a number by 7\n"
+ " function mul(uint a) returns(uint d) { return a * 7; }\n"
+ "}\n";
+
+ char const* devNatspec = "{"
+ "\"methods\":{"
+ " \"mul(uint256)\":{ \n"
+ " \"details\": \"Multiplies a number by 7\"\n"
+ " }\n"
+ " }\n"
+ "}}";
+
+ char const* userNatspec = "{"
+ "\"methods\":{"
+ " \"mul(uint256)\":{ \"notice\": \"Multiplies `a` by 7\"}"
+ "}}";
+
+ checkNatspec(sourceCode, devNatspec, false);
+ checkNatspec(sourceCode, userNatspec, true);
+}
+
+BOOST_AUTO_TEST_CASE(user_multiline_comment)
+{
+ char const* sourceCode = "contract test {\n"
+ " /// @notice Multiplies `a` by 7\n"
+ " /// and then adds `b`\n"
+ " function mul_and_add(uint a, uint256 b) returns(uint256 d)\n"
+ " {\n"
+ " return (a * 7) + b;\n"
+ " }\n"
+ "}\n";
+
+ char const* natspec = "{"
+ "\"methods\":{"
+ " \"mul_and_add(uint256,uint256)\":{ \"notice\": \"Multiplies `a` by 7 and then adds `b`\"}"
+ "}}";
+
+ checkNatspec(sourceCode, natspec, true);
+}
+
+BOOST_AUTO_TEST_CASE(user_multiple_functions)
+{
+ char const* sourceCode = "contract test {\n"
+ " /// @notice Multiplies `a` by 7 and then adds `b`\n"
+ " function mul_and_add(uint a, uint256 b) returns(uint256 d)\n"
+ " {\n"
+ " return (a * 7) + b;\n"
+ " }\n"
+ "\n"
+ " /// @notice Divides `input` by `div`\n"
+ " function divide(uint input, uint div) returns(uint d)\n"
+ " {\n"
+ " return input / div;\n"
+ " }\n"
+ " /// @notice Subtracts 3 from `input`\n"
+ " function sub(int input) returns(int d)\n"
+ " {\n"
+ " return input - 3;\n"
+ " }\n"
+ "}\n";
+
+ char const* natspec = "{"
+ "\"methods\":{"
+ " \"mul_and_add(uint256,uint256)\":{ \"notice\": \"Multiplies `a` by 7 and then adds `b`\"},"
+ " \"divide(uint256,uint256)\":{ \"notice\": \"Divides `input` by `div`\"},"
+ " \"sub(int256)\":{ \"notice\": \"Subtracts 3 from `input`\"}"
+ "}}";
+
+ checkNatspec(sourceCode, natspec, true);
+}
+
+BOOST_AUTO_TEST_CASE(user_empty_contract)
+{
+ char const* sourceCode = "contract test {\n"
+ "}\n";
+
+ char const* natspec = "{\"methods\":{} }";
+
+ checkNatspec(sourceCode, natspec, true);
+}
+
+BOOST_AUTO_TEST_CASE(dev_and_user_no_doc)
+{
+ char const* sourceCode = "contract test {\n"
+ " function mul(uint a) returns(uint d) { return a * 7; }\n"
+ " function sub(int input) returns(int d)\n"
+ " {\n"
+ " return input - 3;\n"
+ " }\n"
+ "}\n";
+
+ char const* devNatspec = "{\"methods\":{}}";
+ char const* userNatspec = "{\"methods\":{}}";
+
+ checkNatspec(sourceCode, devNatspec, false);
+ checkNatspec(sourceCode, userNatspec, true);
+}
+
+BOOST_AUTO_TEST_CASE(dev_desc_after_nl)
+{
+ char const* sourceCode = "contract test {\n"
+ " /// @dev\n"
+ " /// Multiplies a number by 7 and adds second parameter\n"
+ " /// @param a Documentation for the first parameter\n"
+ " /// @param second Documentation for the second parameter\n"
+ " function mul(uint a, uint second) returns(uint d) { return a * 7 + second; }\n"
+ "}\n";
+
+ char const* natspec = "{"
+ "\"methods\":{"
+ " \"mul(uint256,uint256)\":{ \n"
+ " \"details\": \" Multiplies a number by 7 and adds second parameter\",\n"
+ " \"params\": {\n"
+ " \"a\": \"Documentation for the first parameter\",\n"
+ " \"second\": \"Documentation for the second parameter\"\n"
+ " }\n"
+ " }\n"
+ "}}";
+
+ checkNatspec(sourceCode, natspec, false);
+}
+
+BOOST_AUTO_TEST_CASE(dev_multiple_params)
+{
+ char const* sourceCode = "contract test {\n"
+ " /// @dev Multiplies a number by 7 and adds second parameter\n"
+ " /// @param a Documentation for the first parameter\n"
+ " /// @param second Documentation for the second parameter\n"
+ " function mul(uint a, uint second) returns(uint d) { return a * 7 + second; }\n"
+ "}\n";
+
+ char const* natspec = "{"
+ "\"methods\":{"
+ " \"mul(uint256,uint256)\":{ \n"
+ " \"details\": \"Multiplies a number by 7 and adds second parameter\",\n"
+ " \"params\": {\n"
+ " \"a\": \"Documentation for the first parameter\",\n"
+ " \"second\": \"Documentation for the second parameter\"\n"
+ " }\n"
+ " }\n"
+ "}}";
+
+ checkNatspec(sourceCode, natspec, false);
+}
+
+BOOST_AUTO_TEST_CASE(dev_mutiline_param_description)
+{
+ char const* sourceCode = "contract test {\n"
+ " /// @dev Multiplies a number by 7 and adds second parameter\n"
+ " /// @param a Documentation for the first parameter starts here.\n"
+ " /// Since it's a really complicated parameter we need 2 lines\n"
+ " /// @param second Documentation for the second parameter\n"
+ " function mul(uint a, uint second) returns(uint d) { return a * 7 + second; }\n"
+ "}\n";
+
+ char const* natspec = "{"
+ "\"methods\":{"
+ " \"mul(uint256,uint256)\":{ \n"
+ " \"details\": \"Multiplies a number by 7 and adds second parameter\",\n"
+ " \"params\": {\n"
+ " \"a\": \"Documentation for the first parameter starts here. Since it's a really complicated parameter we need 2 lines\",\n"
+ " \"second\": \"Documentation for the second parameter\"\n"
+ " }\n"
+ " }\n"
+ "}}";
+
+ checkNatspec(sourceCode, natspec, false);
+}
+
+BOOST_AUTO_TEST_CASE(dev_multiple_functions)
+{
+ char const* sourceCode = "contract test {\n"
+ " /// @dev Multiplies a number by 7 and adds second parameter\n"
+ " /// @param a Documentation for the first parameter\n"
+ " /// @param second Documentation for the second parameter\n"
+ " function mul(uint a, uint second) returns(uint d) { return a * 7 + second; }\n"
+ " \n"
+ " /// @dev Divides 2 numbers\n"
+ " /// @param input Documentation for the input parameter\n"
+ " /// @param div Documentation for the div parameter\n"
+ " function divide(uint input, uint div) returns(uint d)\n"
+ " {\n"
+ " return input / div;\n"
+ " }\n"
+ " /// @dev Subtracts 3 from `input`\n"
+ " /// @param input Documentation for the input parameter\n"
+ " function sub(int input) returns(int d)\n"
+ " {\n"
+ " return input - 3;\n"
+ " }\n"
+ "}\n";
+
+ char const* natspec = "{"
+ "\"methods\":{"
+ " \"mul(uint256,uint256)\":{ \n"
+ " \"details\": \"Multiplies a number by 7 and adds second parameter\",\n"
+ " \"params\": {\n"
+ " \"a\": \"Documentation for the first parameter\",\n"
+ " \"second\": \"Documentation for the second parameter\"\n"
+ " }\n"
+ " },\n"
+ " \"divide(uint256,uint256)\":{ \n"
+ " \"details\": \"Divides 2 numbers\",\n"
+ " \"params\": {\n"
+ " \"input\": \"Documentation for the input parameter\",\n"
+ " \"div\": \"Documentation for the div parameter\"\n"
+ " }\n"
+ " },\n"
+ " \"sub(int256)\":{ \n"
+ " \"details\": \"Subtracts 3 from `input`\",\n"
+ " \"params\": {\n"
+ " \"input\": \"Documentation for the input parameter\"\n"
+ " }\n"
+ " }\n"
+ "}}";
+
+ checkNatspec(sourceCode, natspec, false);
+}
+
+BOOST_AUTO_TEST_CASE(dev_return)
+{
+ char const* sourceCode = "contract test {\n"
+ " /// @dev Multiplies a number by 7 and adds second parameter\n"
+ " /// @param a Documentation for the first parameter starts here.\n"
+ " /// Since it's a really complicated parameter we need 2 lines\n"
+ " /// @param second Documentation for the second parameter\n"
+ " /// @return The result of the multiplication\n"
+ " function mul(uint a, uint second) returns(uint d) { return a * 7 + second; }\n"
+ "}\n";
+
+ char const* natspec = "{"
+ "\"methods\":{"
+ " \"mul(uint256,uint256)\":{ \n"
+ " \"details\": \"Multiplies a number by 7 and adds second parameter\",\n"
+ " \"params\": {\n"
+ " \"a\": \"Documentation for the first parameter starts here. Since it's a really complicated parameter we need 2 lines\",\n"
+ " \"second\": \"Documentation for the second parameter\"\n"
+ " },\n"
+ " \"return\": \"The result of the multiplication\"\n"
+ " }\n"
+ "}}";
+
+ checkNatspec(sourceCode, natspec, false);
+}
+BOOST_AUTO_TEST_CASE(dev_return_desc_after_nl)
+{
+ char const* sourceCode = "contract test {\n"
+ " /// @dev Multiplies a number by 7 and adds second parameter\n"
+ " /// @param a Documentation for the first parameter starts here.\n"
+ " /// Since it's a really complicated parameter we need 2 lines\n"
+ " /// @param second Documentation for the second parameter\n"
+ " /// @return\n"
+ " /// The result of the multiplication\n"
+ " function mul(uint a, uint second) returns(uint d) { return a * 7 + second; }\n"
+ "}\n";
+
+ char const* natspec = "{"
+ "\"methods\":{"
+ " \"mul(uint256,uint256)\":{ \n"
+ " \"details\": \"Multiplies a number by 7 and adds second parameter\",\n"
+ " \"params\": {\n"
+ " \"a\": \"Documentation for the first parameter starts here. Since it's a really complicated parameter we need 2 lines\",\n"
+ " \"second\": \"Documentation for the second parameter\"\n"
+ " },\n"
+ " \"return\": \" The result of the multiplication\"\n"
+ " }\n"
+ "}}";
+
+ checkNatspec(sourceCode, natspec, false);
+}
+
+
+BOOST_AUTO_TEST_CASE(dev_multiline_return)
+{
+ char const* sourceCode = "contract test {\n"
+ " /// @dev Multiplies a number by 7 and adds second parameter\n"
+ " /// @param a Documentation for the first parameter starts here.\n"
+ " /// Since it's a really complicated parameter we need 2 lines\n"
+ " /// @param second Documentation for the second parameter\n"
+ " /// @return The result of the multiplication\n"
+ " /// and cookies with nutella\n"
+ " function mul(uint a, uint second) returns(uint d) { return a * 7 + second; }\n"
+ "}\n";
+
+ char const* natspec = "{"
+ "\"methods\":{"
+ " \"mul(uint256,uint256)\":{ \n"
+ " \"details\": \"Multiplies a number by 7 and adds second parameter\",\n"
+ " \"params\": {\n"
+ " \"a\": \"Documentation for the first parameter starts here. Since it's a really complicated parameter we need 2 lines\",\n"
+ " \"second\": \"Documentation for the second parameter\"\n"
+ " },\n"
+ " \"return\": \"The result of the multiplication and cookies with nutella\"\n"
+ " }\n"
+ "}}";
+
+ checkNatspec(sourceCode, natspec, false);
+}
+
+BOOST_AUTO_TEST_CASE(dev_multiline_comment)
+{
+ char const* sourceCode = "contract test {\n"
+ " /**\n"
+ " * @dev Multiplies a number by 7 and adds second parameter\n"
+ " * @param a Documentation for the first parameter starts here.\n"
+ " * Since it's a really complicated parameter we need 2 lines\n"
+ " * @param second Documentation for the second parameter\n"
+ " * @return The result of the multiplication\n"
+ " * and cookies with nutella\n"
+ " */"
+ " function mul(uint a, uint second) returns(uint d) { return a * 7 + second; }\n"
+ "}\n";
+
+ char const* natspec = "{"
+ "\"methods\":{"
+ " \"mul(uint256,uint256)\":{ \n"
+ " \"details\": \"Multiplies a number by 7 and adds second parameter\",\n"
+ " \"params\": {\n"
+ " \"a\": \"Documentation for the first parameter starts here. Since it's a really complicated parameter we need 2 lines\",\n"
+ " \"second\": \"Documentation for the second parameter\"\n"
+ " },\n"
+ " \"return\": \"The result of the multiplication and cookies with nutella\"\n"
+ " }\n"
+ "}}";
+
+ checkNatspec(sourceCode, natspec, false);
+}
+
+BOOST_AUTO_TEST_CASE(dev_contract_no_doc)
+{
+ char const* sourceCode = "contract test {\n"
+ " /// @dev Mul function\n"
+ " function mul(uint a, uint second) returns(uint d) { return a * 7 + second; }\n"
+ "}\n";
+
+ char const* natspec = "{"
+ " \"methods\":{"
+ " \"mul(uint256,uint256)\":{ \n"
+ " \"details\": \"Mul function\"\n"
+ " }\n"
+ " }\n"
+ "}";
+
+ checkNatspec(sourceCode, natspec, false);
+}
+
+BOOST_AUTO_TEST_CASE(dev_contract_doc)
+{
+ char const* sourceCode = " /// @author Lefteris\n"
+ " /// @title Just a test contract\n"
+ "contract test {\n"
+ " /// @dev Mul function\n"
+ " function mul(uint a, uint second) returns(uint d) { return a * 7 + second; }\n"
+ "}\n";
+
+ char const* natspec = "{"
+ " \"author\": \"Lefteris\","
+ " \"title\": \"Just a test contract\","
+ " \"methods\":{"
+ " \"mul(uint256,uint256)\":{ \n"
+ " \"details\": \"Mul function\"\n"
+ " }\n"
+ " }\n"
+ "}";
+
+ checkNatspec(sourceCode, natspec, false);
+}
+
+BOOST_AUTO_TEST_CASE(dev_author_at_function)
+{
+ char const* sourceCode = " /// @author Lefteris\n"
+ " /// @title Just a test contract\n"
+ "contract test {\n"
+ " /// @dev Mul function\n"
+ " /// @author John Doe\n"
+ " function mul(uint a, uint second) returns(uint d) { return a * 7 + second; }\n"
+ "}\n";
+
+ char const* natspec = "{"
+ " \"author\": \"Lefteris\","
+ " \"title\": \"Just a test contract\","
+ " \"methods\":{"
+ " \"mul(uint256,uint256)\":{ \n"
+ " \"details\": \"Mul function\",\n"
+ " \"author\": \"John Doe\",\n"
+ " }\n"
+ " }\n"
+ "}";
+
+ checkNatspec(sourceCode, natspec, false);
+}
+
+BOOST_AUTO_TEST_CASE(natspec_notice_without_tag)
+{
+ char const* sourceCode = R"(
+ contract test {
+ /// I do something awesome
+ function mul(uint a) returns(uint d) { return a * 7; }
+ }
+ )";
+
+
+ char const* natspec = R"ABCDEF(
+ {
+ "methods" : {
+ "mul(uint256)" : {
+ "notice" : "I do something awesome"
+ }
+ }
+ }
+ )ABCDEF";
+
+ checkNatspec(sourceCode, natspec, true);
+}
+
+BOOST_AUTO_TEST_CASE(natspec_multiline_notice_without_tag)
+{
+ char const* sourceCode = R"(
+ contract test {
+ /// I do something awesome
+ /// which requires two lines to explain
+ function mul(uint a) returns(uint d) { return a * 7; }
+ }
+ )";
+
+ char const* natspec = R"ABCDEF(
+ {
+ "methods" : {
+ "mul(uint256)" : {
+ "notice" : "I do something awesome which requires two lines to explain"
+ }
+ }
+ }
+ )ABCDEF";
+
+ checkNatspec(sourceCode, natspec, true);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+}
+}
+}
diff --git a/test/libsolidity/SolidityOptimizer.cpp b/test/libsolidity/SolidityOptimizer.cpp
new file mode 100644
index 00000000..85a88c03
--- /dev/null
+++ b/test/libsolidity/SolidityOptimizer.cpp
@@ -0,0 +1,1132 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum 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.
+
+ cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * @author Christian <c@ethdev.com>
+ * @date 2014
+ * Tests for the Solidity optimizer.
+ */
+
+#include <string>
+#include <tuple>
+#include <memory>
+#include <boost/test/unit_test.hpp>
+#include <boost/lexical_cast.hpp>
+#include <test/libsolidity/solidityExecutionFramework.h>
+#include <libevmasm/CommonSubexpressionEliminator.h>
+#include <libevmasm/ControlFlowGraph.h>
+#include <libevmasm/Assembly.h>
+#include <libevmasm/BlockDeduplicator.h>
+
+using namespace std;
+using namespace dev::eth;
+
+namespace dev
+{
+namespace solidity
+{
+namespace test
+{
+
+class OptimizerTestFramework: public ExecutionFramework
+{
+public:
+ OptimizerTestFramework() { }
+ /// Compiles the source code with and without optimizing.
+ void compileBothVersions(
+ std::string const& _sourceCode,
+ u256 const& _value = 0,
+ std::string const& _contractName = ""
+ )
+ {
+ m_optimize = false;
+ bytes nonOptimizedBytecode = compileAndRun(_sourceCode, _value, _contractName);
+ m_nonOptimizedContract = m_contractAddress;
+ m_optimize = true;
+ bytes optimizedBytecode = compileAndRun(_sourceCode, _value, _contractName);
+ size_t nonOptimizedSize = 0;
+ eth::eachInstruction(nonOptimizedBytecode, [&](Instruction, u256 const&) {
+ nonOptimizedSize++;
+ });
+ size_t optimizedSize = 0;
+ eth::eachInstruction(optimizedBytecode, [&](Instruction, u256 const&) {
+ optimizedSize++;
+ });
+ BOOST_CHECK_MESSAGE(
+ nonOptimizedSize > optimizedSize,
+ "Optimizer did not reduce bytecode size."
+ );
+ m_optimizedContract = m_contractAddress;
+ }
+
+ template <class... Args>
+ void compareVersions(std::string _sig, Args const&... _arguments)
+ {
+ m_contractAddress = m_nonOptimizedContract;
+ bytes nonOptimizedOutput = callContractFunction(_sig, _arguments...);
+ m_contractAddress = m_optimizedContract;
+ bytes optimizedOutput = callContractFunction(_sig, _arguments...);
+ BOOST_CHECK_MESSAGE(nonOptimizedOutput == optimizedOutput, "Computed values do not match."
+ "\nNon-Optimized: " + toHex(nonOptimizedOutput) +
+ "\nOptimized: " + toHex(optimizedOutput));
+ }
+
+ AssemblyItems addDummyLocations(AssemblyItems const& _input)
+ {
+ // add dummy locations to each item so that we can check that they are not deleted
+ AssemblyItems input = _input;
+ for (AssemblyItem& item: input)
+ item.setLocation(SourceLocation(1, 3, make_shared<string>("")));
+ return input;
+ }
+
+ eth::KnownState createInitialState(AssemblyItems const& _input)
+ {
+ eth::KnownState state;
+ for (auto const& item: addDummyLocations(_input))
+ state.feedItem(item, true);
+ return state;
+ }
+
+ AssemblyItems getCSE(AssemblyItems const& _input, eth::KnownState const& _state = eth::KnownState())
+ {
+ AssemblyItems input = addDummyLocations(_input);
+
+ eth::CommonSubexpressionEliminator cse(_state);
+ BOOST_REQUIRE(cse.feedItems(input.begin(), input.end()) == input.end());
+ AssemblyItems output = cse.getOptimizedItems();
+
+ for (AssemblyItem const& item: output)
+ {
+ BOOST_CHECK(item == Instruction::POP || !item.getLocation().isEmpty());
+ }
+ return output;
+ }
+
+ void checkCSE(
+ AssemblyItems const& _input,
+ AssemblyItems const& _expectation,
+ KnownState const& _state = eth::KnownState()
+ )
+ {
+ AssemblyItems output = getCSE(_input, _state);
+ BOOST_CHECK_EQUAL_COLLECTIONS(_expectation.begin(), _expectation.end(), output.begin(), output.end());
+ }
+
+ AssemblyItems getCFG(AssemblyItems const& _input)
+ {
+ AssemblyItems output = _input;
+ // Running it four times should be enough for these tests.
+ for (unsigned i = 0; i < 4; ++i)
+ {
+ ControlFlowGraph cfg(output);
+ AssemblyItems optItems;
+ for (BasicBlock const& block: cfg.optimisedBlocks())
+ copy(output.begin() + block.begin, output.begin() + block.end,
+ back_inserter(optItems));
+ output = move(optItems);
+ }
+ return output;
+ }
+
+ void checkCFG(AssemblyItems const& _input, AssemblyItems const& _expectation)
+ {
+ AssemblyItems output = getCFG(_input);
+ BOOST_CHECK_EQUAL_COLLECTIONS(_expectation.begin(), _expectation.end(), output.begin(), output.end());
+ }
+
+protected:
+ Address m_optimizedContract;
+ Address m_nonOptimizedContract;
+};
+
+BOOST_FIXTURE_TEST_SUITE(SolidityOptimizer, OptimizerTestFramework)
+
+BOOST_AUTO_TEST_CASE(smoke_test)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f(uint a) returns (uint b) {
+ return a;
+ }
+ })";
+ compileBothVersions(sourceCode);
+ compareVersions("f(uint256)", u256(7));
+}
+
+BOOST_AUTO_TEST_CASE(identities)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f(int a) returns (int b) {
+ return int(0) | (int(1) * (int(0) ^ (0 + a)));
+ }
+ })";
+ compileBothVersions(sourceCode);
+ compareVersions("f(uint256)", u256(0x12334664));
+}
+
+BOOST_AUTO_TEST_CASE(unused_expressions)
+{
+ char const* sourceCode = R"(
+ contract test {
+ uint data;
+ function f() returns (uint a, uint b) {
+ 10 + 20;
+ data;
+ }
+ })";
+ compileBothVersions(sourceCode);
+ compareVersions("f()");
+}
+
+BOOST_AUTO_TEST_CASE(constant_folding_both_sides)
+{
+ // if constants involving the same associative and commutative operator are applied from both
+ // sides, the operator should be applied only once, because the expression compiler pushes
+ // literals as late as possible
+ char const* sourceCode = R"(
+ contract test {
+ function f(uint x) returns (uint y) {
+ return 98 ^ (7 * ((1 | (x | 1000)) * 40) ^ 102);
+ }
+ })";
+ compileBothVersions(sourceCode);
+ compareVersions("f(uint256)");
+}
+
+BOOST_AUTO_TEST_CASE(storage_access)
+{
+ char const* sourceCode = R"(
+ contract test {
+ uint8[40] data;
+ function f(uint x) returns (uint y) {
+ data[2] = data[7] = uint8(x);
+ data[4] = data[2] * 10 + data[3];
+ }
+ }
+ )";
+ compileBothVersions(sourceCode);
+ compareVersions("f(uint256)");
+}
+
+BOOST_AUTO_TEST_CASE(array_copy)
+{
+ char const* sourceCode = R"(
+ contract test {
+ bytes2[] data1;
+ bytes5[] data2;
+ function f(uint x) returns (uint l, uint y) {
+ for (uint i = 0; i < msg.data.length; ++i)
+ data1[i] = msg.data[i];
+ data2 = data1;
+ l = data2.length;
+ y = uint(data2[x]);
+ }
+ }
+ )";
+ compileBothVersions(sourceCode);
+ compareVersions("f(uint256)", 0);
+ compareVersions("f(uint256)", 10);
+ compareVersions("f(uint256)", 36);
+}
+
+BOOST_AUTO_TEST_CASE(function_calls)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f1(uint x) returns (uint) { return x*x; }
+ function f(uint x) returns (uint) { return f1(7+x) - this.f1(x**9); }
+ }
+ )";
+ compileBothVersions(sourceCode);
+ compareVersions("f(uint256)", 0);
+ compareVersions("f(uint256)", 10);
+ compareVersions("f(uint256)", 36);
+}
+
+BOOST_AUTO_TEST_CASE(storage_write_in_loops)
+{
+ char const* sourceCode = R"(
+ contract test {
+ uint d;
+ function f(uint a) returns (uint r) {
+ var x = d;
+ for (uint i = 1; i < a * a; i++) {
+ r = d;
+ d = i;
+ }
+
+ }
+ }
+ )";
+ compileBothVersions(sourceCode);
+ compareVersions("f(uint256)", 0);
+ compareVersions("f(uint256)", 10);
+ compareVersions("f(uint256)", 36);
+}
+
+BOOST_AUTO_TEST_CASE(retain_information_in_branches)
+{
+ // This tests that the optimizer knows that we already have "z == sha3(y)" inside both branches.
+ char const* sourceCode = R"(
+ contract c {
+ bytes32 d;
+ uint a;
+ function f(uint x, bytes32 y) returns (uint r_a, bytes32 r_d) {
+ bytes32 z = sha3(y);
+ if (x > 8) {
+ z = sha3(y);
+ a = x;
+ } else {
+ z = sha3(y);
+ a = x;
+ }
+ r_a = a;
+ r_d = d;
+ }
+ }
+ )";
+ compileBothVersions(sourceCode);
+ compareVersions("f(uint256,bytes32)", 0, "abc");
+ compareVersions("f(uint256,bytes32)", 8, "def");
+ compareVersions("f(uint256,bytes32)", 10, "ghi");
+
+ m_optimize = true;
+ bytes optimizedBytecode = compileAndRun(sourceCode, 0, "c");
+ size_t numSHA3s = 0;
+ eth::eachInstruction(optimizedBytecode, [&](Instruction _instr, u256 const&) {
+ if (_instr == eth::Instruction::SHA3)
+ numSHA3s++;
+ });
+ BOOST_CHECK_EQUAL(1, numSHA3s);
+}
+
+BOOST_AUTO_TEST_CASE(store_tags_as_unions)
+{
+ // This calls the same function from two sources and both calls have a certain sha3 on
+ // the stack at the same position.
+ // Without storing tags as unions, the return from the shared function would not know where to
+ // jump and thus all jumpdests are forced to clear their state and we do not know about the
+ // sha3 anymore.
+ // Note that, for now, this only works if the functions have the same number of return
+ // parameters since otherwise, the return jump addresses are at different stack positions
+ // which triggers the "unknown jump target" situation.
+ char const* sourceCode = R"(
+ contract test {
+ bytes32 data;
+ function f(uint x, bytes32 y) external returns (uint r_a, bytes32 r_d) {
+ r_d = sha3(y);
+ shared(y);
+ r_d = sha3(y);
+ r_a = 5;
+ }
+ function g(uint x, bytes32 y) external returns (uint r_a, bytes32 r_d) {
+ r_d = sha3(y);
+ shared(y);
+ r_d = bytes32(uint(sha3(y)) + 2);
+ r_a = 7;
+ }
+ function shared(bytes32 y) internal {
+ data = sha3(y);
+ }
+ }
+ )";
+ compileBothVersions(sourceCode);
+ compareVersions("f()", 7, "abc");
+
+ m_optimize = true;
+ bytes optimizedBytecode = compileAndRun(sourceCode, 0, "test");
+ size_t numSHA3s = 0;
+ eth::eachInstruction(optimizedBytecode, [&](Instruction _instr, u256 const&) {
+ if (_instr == eth::Instruction::SHA3)
+ numSHA3s++;
+ });
+// TEST DISABLED UNTIL 93693404 IS IMPLEMENTED
+// BOOST_CHECK_EQUAL(2, numSHA3s);
+}
+
+BOOST_AUTO_TEST_CASE(cse_intermediate_swap)
+{
+ eth::KnownState state;
+ eth::CommonSubexpressionEliminator cse(state);
+ AssemblyItems input{
+ Instruction::SWAP1, Instruction::POP, Instruction::ADD, u256(0), Instruction::SWAP1,
+ Instruction::SLOAD, Instruction::SWAP1, u256(100), Instruction::EXP, Instruction::SWAP1,
+ Instruction::DIV, u256(0xff), Instruction::AND
+ };
+ BOOST_REQUIRE(cse.feedItems(input.begin(), input.end()) == input.end());
+ AssemblyItems output = cse.getOptimizedItems();
+ BOOST_CHECK(!output.empty());
+}
+
+BOOST_AUTO_TEST_CASE(cse_negative_stack_access)
+{
+ AssemblyItems input{Instruction::DUP2, u256(0)};
+ checkCSE(input, input);
+}
+
+BOOST_AUTO_TEST_CASE(cse_negative_stack_end)
+{
+ AssemblyItems input{Instruction::ADD};
+ checkCSE(input, input);
+}
+
+BOOST_AUTO_TEST_CASE(cse_intermediate_negative_stack)
+{
+ AssemblyItems input{Instruction::ADD, u256(1), Instruction::DUP1};
+ checkCSE(input, input);
+}
+
+BOOST_AUTO_TEST_CASE(cse_pop)
+{
+ checkCSE({Instruction::POP}, {Instruction::POP});
+}
+
+BOOST_AUTO_TEST_CASE(cse_unneeded_items)
+{
+ AssemblyItems input{
+ Instruction::ADD,
+ Instruction::SWAP1,
+ Instruction::POP,
+ u256(7),
+ u256(8),
+ };
+ checkCSE(input, input);
+}
+
+BOOST_AUTO_TEST_CASE(cse_constant_addition)
+{
+ AssemblyItems input{u256(7), u256(8), Instruction::ADD};
+ checkCSE(input, {u256(7 + 8)});
+}
+
+BOOST_AUTO_TEST_CASE(cse_invariants)
+{
+ AssemblyItems input{
+ Instruction::DUP1,
+ Instruction::DUP1,
+ u256(0),
+ Instruction::OR,
+ Instruction::OR
+ };
+ checkCSE(input, {Instruction::DUP1});
+}
+
+BOOST_AUTO_TEST_CASE(cse_subself)
+{
+ checkCSE({Instruction::DUP1, Instruction::SUB}, {Instruction::POP, u256(0)});
+}
+
+BOOST_AUTO_TEST_CASE(cse_subother)
+{
+ checkCSE({Instruction::SUB}, {Instruction::SUB});
+}
+
+BOOST_AUTO_TEST_CASE(cse_double_negation)
+{
+ checkCSE({Instruction::DUP5, Instruction::NOT, Instruction::NOT}, {Instruction::DUP5});
+}
+
+BOOST_AUTO_TEST_CASE(cse_double_iszero)
+{
+ checkCSE({Instruction::GT, Instruction::ISZERO, Instruction::ISZERO}, {Instruction::GT});
+ checkCSE({Instruction::GT, Instruction::ISZERO}, {Instruction::GT, Instruction::ISZERO});
+ checkCSE(
+ {Instruction::ISZERO, Instruction::ISZERO, Instruction::ISZERO},
+ {Instruction::ISZERO}
+ );
+}
+
+BOOST_AUTO_TEST_CASE(cse_associativity)
+{
+ AssemblyItems input{
+ Instruction::DUP1,
+ Instruction::DUP1,
+ u256(0),
+ Instruction::OR,
+ Instruction::OR
+ };
+ checkCSE(input, {Instruction::DUP1});
+}
+
+BOOST_AUTO_TEST_CASE(cse_associativity2)
+{
+ AssemblyItems input{
+ u256(0),
+ Instruction::DUP2,
+ u256(2),
+ u256(1),
+ Instruction::DUP6,
+ Instruction::ADD,
+ u256(2),
+ Instruction::ADD,
+ Instruction::ADD,
+ Instruction::ADD,
+ Instruction::ADD
+ };
+ checkCSE(input, {Instruction::DUP2, Instruction::DUP2, Instruction::ADD, u256(5), Instruction::ADD});
+}
+
+BOOST_AUTO_TEST_CASE(cse_storage)
+{
+ AssemblyItems input{
+ u256(0),
+ Instruction::SLOAD,
+ u256(0),
+ Instruction::SLOAD,
+ Instruction::ADD,
+ u256(0),
+ Instruction::SSTORE
+ };
+ checkCSE(input, {
+ u256(0),
+ Instruction::DUP1,
+ Instruction::SLOAD,
+ Instruction::DUP1,
+ Instruction::ADD,
+ Instruction::SWAP1,
+ Instruction::SSTORE
+ });
+}
+
+BOOST_AUTO_TEST_CASE(cse_noninterleaved_storage)
+{
+ // two stores to the same location should be replaced by only one store, even if we
+ // read in the meantime
+ AssemblyItems input{
+ u256(7),
+ Instruction::DUP2,
+ Instruction::SSTORE,
+ Instruction::DUP1,
+ Instruction::SLOAD,
+ u256(8),
+ Instruction::DUP3,
+ Instruction::SSTORE
+ };
+ checkCSE(input, {
+ u256(8),
+ Instruction::DUP2,
+ Instruction::SSTORE,
+ u256(7)
+ });
+}
+
+BOOST_AUTO_TEST_CASE(cse_interleaved_storage)
+{
+ // stores and reads to/from two unknown locations, should not optimize away the first store
+ AssemblyItems input{
+ u256(7),
+ Instruction::DUP2,
+ Instruction::SSTORE, // store to "DUP1"
+ Instruction::DUP2,
+ Instruction::SLOAD, // read from "DUP2", might be equal to "DUP1"
+ u256(0),
+ Instruction::DUP3,
+ Instruction::SSTORE // store different value to "DUP1"
+ };
+ checkCSE(input, input);
+}
+
+BOOST_AUTO_TEST_CASE(cse_interleaved_storage_same_value)
+{
+ // stores and reads to/from two unknown locations, should not optimize away the first store
+ // but it should optimize away the second, since we already know the value will be the same
+ AssemblyItems input{
+ u256(7),
+ Instruction::DUP2,
+ Instruction::SSTORE, // store to "DUP1"
+ Instruction::DUP2,
+ Instruction::SLOAD, // read from "DUP2", might be equal to "DUP1"
+ u256(6),
+ u256(1),
+ Instruction::ADD,
+ Instruction::DUP3,
+ Instruction::SSTORE // store same value to "DUP1"
+ };
+ checkCSE(input, {
+ u256(7),
+ Instruction::DUP2,
+ Instruction::SSTORE,
+ Instruction::DUP2,
+ Instruction::SLOAD
+ });
+}
+
+BOOST_AUTO_TEST_CASE(cse_interleaved_storage_at_known_location)
+{
+ // stores and reads to/from two known locations, should optimize away the first store,
+ // because we know that the location is different
+ AssemblyItems input{
+ u256(0x70),
+ u256(1),
+ Instruction::SSTORE, // store to 1
+ u256(2),
+ Instruction::SLOAD, // read from 2, is different from 1
+ u256(0x90),
+ u256(1),
+ Instruction::SSTORE // store different value at 1
+ };
+ checkCSE(input, {
+ u256(2),
+ Instruction::SLOAD,
+ u256(0x90),
+ u256(1),
+ Instruction::SSTORE
+ });
+}
+
+BOOST_AUTO_TEST_CASE(cse_interleaved_storage_at_known_location_offset)
+{
+ // stores and reads to/from two locations which are known to be different,
+ // should optimize away the first store, because we know that the location is different
+ AssemblyItems input{
+ u256(0x70),
+ Instruction::DUP2,
+ u256(1),
+ Instruction::ADD,
+ Instruction::SSTORE, // store to "DUP1"+1
+ Instruction::DUP1,
+ u256(2),
+ Instruction::ADD,
+ Instruction::SLOAD, // read from "DUP1"+2, is different from "DUP1"+1
+ u256(0x90),
+ Instruction::DUP3,
+ u256(1),
+ Instruction::ADD,
+ Instruction::SSTORE // store different value at "DUP1"+1
+ };
+ checkCSE(input, {
+ u256(2),
+ Instruction::DUP2,
+ Instruction::ADD,
+ Instruction::SLOAD,
+ u256(0x90),
+ u256(1),
+ Instruction::DUP4,
+ Instruction::ADD,
+ Instruction::SSTORE
+ });
+}
+
+BOOST_AUTO_TEST_CASE(cse_interleaved_memory_at_known_location_offset)
+{
+ // stores and reads to/from two locations which are known to be different,
+ // should not optimize away the first store, because the location overlaps with the load,
+ // but it should optimize away the second, because we know that the location is different by 32
+ AssemblyItems input{
+ u256(0x50),
+ Instruction::DUP2,
+ u256(2),
+ Instruction::ADD,
+ Instruction::MSTORE, // ["DUP1"+2] = 0x50
+ u256(0x60),
+ Instruction::DUP2,
+ u256(32),
+ Instruction::ADD,
+ Instruction::MSTORE, // ["DUP1"+32] = 0x60
+ Instruction::DUP1,
+ Instruction::MLOAD, // read from "DUP1"
+ u256(0x70),
+ Instruction::DUP3,
+ u256(32),
+ Instruction::ADD,
+ Instruction::MSTORE, // ["DUP1"+32] = 0x70
+ u256(0x80),
+ Instruction::DUP3,
+ u256(2),
+ Instruction::ADD,
+ Instruction::MSTORE, // ["DUP1"+2] = 0x80
+ };
+ // If the actual code changes too much, we could also simply check that the output contains
+ // exactly 3 MSTORE and exactly 1 MLOAD instruction.
+ checkCSE(input, {
+ u256(0x50),
+ u256(2),
+ Instruction::DUP3,
+ Instruction::ADD,
+ Instruction::SWAP1,
+ Instruction::DUP2,
+ Instruction::MSTORE, // ["DUP1"+2] = 0x50
+ Instruction::DUP2,
+ Instruction::MLOAD, // read from "DUP1"
+ u256(0x70),
+ u256(32),
+ Instruction::DUP5,
+ Instruction::ADD,
+ Instruction::MSTORE, // ["DUP1"+32] = 0x70
+ u256(0x80),
+ Instruction::SWAP1,
+ Instruction::SWAP2,
+ Instruction::MSTORE // ["DUP1"+2] = 0x80
+ });
+}
+
+BOOST_AUTO_TEST_CASE(cse_deep_stack)
+{
+ AssemblyItems input{
+ Instruction::ADD,
+ Instruction::SWAP1,
+ Instruction::POP,
+ Instruction::SWAP8,
+ Instruction::POP,
+ Instruction::SWAP8,
+ Instruction::POP,
+ Instruction::SWAP8,
+ Instruction::SWAP5,
+ Instruction::POP,
+ Instruction::POP,
+ Instruction::POP,
+ Instruction::POP,
+ Instruction::POP,
+ };
+ checkCSE(input, {
+ Instruction::SWAP4,
+ Instruction::SWAP12,
+ Instruction::SWAP3,
+ Instruction::SWAP11,
+ Instruction::POP,
+ Instruction::SWAP1,
+ Instruction::SWAP3,
+ Instruction::ADD,
+ Instruction::SWAP8,
+ Instruction::POP,
+ Instruction::SWAP6,
+ Instruction::POP,
+ Instruction::POP,
+ Instruction::POP,
+ Instruction::POP,
+ Instruction::POP,
+ Instruction::POP,
+ });
+}
+
+BOOST_AUTO_TEST_CASE(cse_jumpi_no_jump)
+{
+ AssemblyItems input{
+ u256(0),
+ u256(1),
+ Instruction::DUP2,
+ AssemblyItem(PushTag, 1),
+ Instruction::JUMPI
+ };
+ checkCSE(input, {
+ u256(0),
+ u256(1)
+ });
+}
+
+BOOST_AUTO_TEST_CASE(cse_jumpi_jump)
+{
+ AssemblyItems input{
+ u256(1),
+ u256(1),
+ Instruction::DUP2,
+ AssemblyItem(PushTag, 1),
+ Instruction::JUMPI
+ };
+ checkCSE(input, {
+ u256(1),
+ Instruction::DUP1,
+ AssemblyItem(PushTag, 1),
+ Instruction::JUMP
+ });
+}
+
+BOOST_AUTO_TEST_CASE(cse_empty_sha3)
+{
+ AssemblyItems input{
+ u256(0),
+ Instruction::DUP2,
+ Instruction::SHA3
+ };
+ checkCSE(input, {
+ u256(sha3(bytesConstRef()))
+ });
+}
+
+BOOST_AUTO_TEST_CASE(cse_partial_sha3)
+{
+ AssemblyItems input{
+ u256(0xabcd) << (256 - 16),
+ u256(0),
+ Instruction::MSTORE,
+ u256(2),
+ u256(0),
+ Instruction::SHA3
+ };
+ checkCSE(input, {
+ u256(0xabcd) << (256 - 16),
+ u256(0),
+ Instruction::MSTORE,
+ u256(sha3(bytes{0xab, 0xcd}))
+ });
+}
+
+BOOST_AUTO_TEST_CASE(cse_sha3_twice_same_location)
+{
+ // sha3 twice from same dynamic location
+ AssemblyItems input{
+ Instruction::DUP2,
+ Instruction::DUP1,
+ Instruction::MSTORE,
+ u256(64),
+ Instruction::DUP2,
+ Instruction::SHA3,
+ u256(64),
+ Instruction::DUP3,
+ Instruction::SHA3
+ };
+ checkCSE(input, {
+ Instruction::DUP2,
+ Instruction::DUP1,
+ Instruction::MSTORE,
+ u256(64),
+ Instruction::DUP2,
+ Instruction::SHA3,
+ Instruction::DUP1
+ });
+}
+
+BOOST_AUTO_TEST_CASE(cse_sha3_twice_same_content)
+{
+ // sha3 twice from different dynamic location but with same content
+ AssemblyItems input{
+ Instruction::DUP1,
+ u256(0x80),
+ Instruction::MSTORE, // m[128] = DUP1
+ u256(0x20),
+ u256(0x80),
+ Instruction::SHA3, // sha3(m[128..(128+32)])
+ Instruction::DUP2,
+ u256(12),
+ Instruction::MSTORE, // m[12] = DUP1
+ u256(0x20),
+ u256(12),
+ Instruction::SHA3 // sha3(m[12..(12+32)])
+ };
+ checkCSE(input, {
+ u256(0x80),
+ Instruction::DUP2,
+ Instruction::DUP2,
+ Instruction::MSTORE,
+ u256(0x20),
+ Instruction::SWAP1,
+ Instruction::SHA3,
+ u256(12),
+ Instruction::DUP3,
+ Instruction::SWAP1,
+ Instruction::MSTORE,
+ Instruction::DUP1
+ });
+}
+
+BOOST_AUTO_TEST_CASE(cse_sha3_twice_same_content_dynamic_store_in_between)
+{
+ // sha3 twice from different dynamic location but with same content,
+ // dynamic mstore in between, which forces us to re-calculate the sha3
+ AssemblyItems input{
+ u256(0x80),
+ Instruction::DUP2,
+ Instruction::DUP2,
+ Instruction::MSTORE, // m[128] = DUP1
+ u256(0x20),
+ Instruction::DUP1,
+ Instruction::DUP3,
+ Instruction::SHA3, // sha3(m[128..(128+32)])
+ u256(12),
+ Instruction::DUP5,
+ Instruction::DUP2,
+ Instruction::MSTORE, // m[12] = DUP1
+ Instruction::DUP12,
+ Instruction::DUP14,
+ Instruction::MSTORE, // destroys memory knowledge
+ Instruction::SWAP2,
+ Instruction::SWAP1,
+ Instruction::SWAP2,
+ Instruction::SHA3 // sha3(m[12..(12+32)])
+ };
+ checkCSE(input, input);
+}
+
+BOOST_AUTO_TEST_CASE(cse_sha3_twice_same_content_noninterfering_store_in_between)
+{
+ // sha3 twice from different dynamic location but with same content,
+ // dynamic mstore in between, but does not force us to re-calculate the sha3
+ AssemblyItems input{
+ u256(0x80),
+ Instruction::DUP2,
+ Instruction::DUP2,
+ Instruction::MSTORE, // m[128] = DUP1
+ u256(0x20),
+ Instruction::DUP1,
+ Instruction::DUP3,
+ Instruction::SHA3, // sha3(m[128..(128+32)])
+ u256(12),
+ Instruction::DUP5,
+ Instruction::DUP2,
+ Instruction::MSTORE, // m[12] = DUP1
+ Instruction::DUP12,
+ u256(12 + 32),
+ Instruction::MSTORE, // does not destoy memory knowledge
+ Instruction::DUP13,
+ u256(128 - 32),
+ Instruction::MSTORE, // does not destoy memory knowledge
+ u256(0x20),
+ u256(12),
+ Instruction::SHA3 // sha3(m[12..(12+32)])
+ };
+ // if this changes too often, only count the number of SHA3 and MSTORE instructions
+ AssemblyItems output = getCSE(input);
+ BOOST_CHECK_EQUAL(4, count(output.begin(), output.end(), AssemblyItem(Instruction::MSTORE)));
+ BOOST_CHECK_EQUAL(1, count(output.begin(), output.end(), AssemblyItem(Instruction::SHA3)));
+}
+
+BOOST_AUTO_TEST_CASE(cse_with_initially_known_stack)
+{
+ eth::KnownState state = createInitialState(AssemblyItems{
+ u256(0x12),
+ u256(0x20),
+ Instruction::ADD
+ });
+ AssemblyItems input{
+ u256(0x12 + 0x20)
+ };
+ checkCSE(input, AssemblyItems{Instruction::DUP1}, state);
+}
+
+BOOST_AUTO_TEST_CASE(cse_equality_on_initially_known_stack)
+{
+ eth::KnownState state = createInitialState(AssemblyItems{Instruction::DUP1});
+ AssemblyItems input{
+ Instruction::EQ
+ };
+ AssemblyItems output = getCSE(input, state);
+ // check that it directly pushes 1 (true)
+ BOOST_CHECK(find(output.begin(), output.end(), AssemblyItem(u256(1))) != output.end());
+}
+
+BOOST_AUTO_TEST_CASE(cse_access_previous_sequence)
+{
+ // Tests that the code generator detects whether it tries to access SLOAD instructions
+ // from a sequenced expression which is not in its scope.
+ eth::KnownState state = createInitialState(AssemblyItems{
+ u256(0),
+ Instruction::SLOAD,
+ u256(1),
+ Instruction::ADD,
+ u256(0),
+ Instruction::SSTORE
+ });
+ // now stored: val_1 + 1 (value at sequence 1)
+ // if in the following instructions, the SLOAD cresolves to "val_1 + 1",
+ // this cannot be generated because we cannot load from sequence 1 anymore.
+ AssemblyItems input{
+ u256(0),
+ Instruction::SLOAD,
+ };
+ BOOST_CHECK_THROW(getCSE(input, state), StackTooDeepException);
+ // @todo for now, this throws an exception, but it should recover to the following
+ // (or an even better version) at some point:
+ // 0, SLOAD, 1, ADD, SSTORE, 0 SLOAD
+}
+
+BOOST_AUTO_TEST_CASE(cse_optimise_return)
+{
+ checkCSE(
+ AssemblyItems{u256(0), u256(7), Instruction::RETURN},
+ AssemblyItems{Instruction::STOP}
+ );
+}
+
+BOOST_AUTO_TEST_CASE(control_flow_graph_remove_unused)
+{
+ // remove parts of the code that are unused
+ AssemblyItems input{
+ AssemblyItem(PushTag, 1),
+ Instruction::JUMP,
+ u256(7),
+ AssemblyItem(Tag, 1),
+ };
+ checkCFG(input, {});
+}
+
+BOOST_AUTO_TEST_CASE(control_flow_graph_remove_unused_loop)
+{
+ AssemblyItems input{
+ AssemblyItem(PushTag, 3),
+ Instruction::JUMP,
+ AssemblyItem(Tag, 1),
+ u256(7),
+ AssemblyItem(PushTag, 2),
+ Instruction::JUMP,
+ AssemblyItem(Tag, 2),
+ u256(8),
+ AssemblyItem(PushTag, 1),
+ Instruction::JUMP,
+ AssemblyItem(Tag, 3),
+ u256(11)
+ };
+ checkCFG(input, {u256(11)});
+}
+
+BOOST_AUTO_TEST_CASE(control_flow_graph_reconnect_single_jump_source)
+{
+ // move code that has only one unconditional jump source
+ AssemblyItems input{
+ u256(1),
+ AssemblyItem(PushTag, 1),
+ Instruction::JUMP,
+ AssemblyItem(Tag, 2),
+ u256(2),
+ AssemblyItem(PushTag, 3),
+ Instruction::JUMP,
+ AssemblyItem(Tag, 1),
+ u256(3),
+ AssemblyItem(PushTag, 2),
+ Instruction::JUMP,
+ AssemblyItem(Tag, 3),
+ u256(4),
+ };
+ checkCFG(input, {u256(1), u256(3), u256(2), u256(4)});
+}
+
+BOOST_AUTO_TEST_CASE(control_flow_graph_do_not_remove_returned_to)
+{
+ // do not remove parts that are "returned to"
+ AssemblyItems input{
+ AssemblyItem(PushTag, 1),
+ AssemblyItem(PushTag, 2),
+ Instruction::JUMP,
+ AssemblyItem(Tag, 2),
+ Instruction::JUMP,
+ AssemblyItem(Tag, 1),
+ u256(2)
+ };
+ checkCFG(input, {u256(2)});
+}
+
+BOOST_AUTO_TEST_CASE(block_deduplicator)
+{
+ AssemblyItems input{
+ AssemblyItem(PushTag, 2),
+ AssemblyItem(PushTag, 1),
+ AssemblyItem(PushTag, 3),
+ u256(6),
+ eth::Instruction::SWAP3,
+ eth::Instruction::JUMP,
+ AssemblyItem(Tag, 1),
+ u256(6),
+ eth::Instruction::SWAP3,
+ eth::Instruction::JUMP,
+ AssemblyItem(Tag, 2),
+ u256(6),
+ eth::Instruction::SWAP3,
+ eth::Instruction::JUMP,
+ AssemblyItem(Tag, 3)
+ };
+ BlockDeduplicator dedup(input);
+ dedup.deduplicate();
+
+ set<u256> pushTags;
+ for (AssemblyItem const& item: input)
+ if (item.type() == PushTag)
+ pushTags.insert(item.data());
+ BOOST_CHECK_EQUAL(pushTags.size(), 2);
+}
+
+BOOST_AUTO_TEST_CASE(block_deduplicator_loops)
+{
+ AssemblyItems input{
+ u256(0),
+ eth::Instruction::SLOAD,
+ AssemblyItem(PushTag, 1),
+ AssemblyItem(PushTag, 2),
+ eth::Instruction::JUMPI,
+ eth::Instruction::JUMP,
+ AssemblyItem(Tag, 1),
+ u256(5),
+ u256(6),
+ eth::Instruction::SSTORE,
+ AssemblyItem(PushTag, 1),
+ eth::Instruction::JUMP,
+ AssemblyItem(Tag, 2),
+ u256(5),
+ u256(6),
+ eth::Instruction::SSTORE,
+ AssemblyItem(PushTag, 2),
+ eth::Instruction::JUMP,
+ };
+ BlockDeduplicator dedup(input);
+ dedup.deduplicate();
+
+ set<u256> pushTags;
+ for (AssemblyItem const& item: input)
+ if (item.type() == PushTag)
+ pushTags.insert(item.data());
+ BOOST_CHECK_EQUAL(pushTags.size(), 1);
+}
+
+BOOST_AUTO_TEST_CASE(computing_constants)
+{
+ char const* sourceCode = R"(
+ contract c {
+ uint a;
+ uint b;
+ uint c;
+ function set() returns (uint a, uint b, uint c) {
+ a = 0x77abc0000000000000000000000000000000000000000000000000000000001;
+ b = 0x817416927846239487123469187231298734162934871263941234127518276;
+ g();
+ }
+ function g() {
+ b = 0x817416927846239487123469187231298734162934871263941234127518276;
+ c = 0x817416927846239487123469187231298734162934871263941234127518276;
+ }
+ function get() returns (uint ra, uint rb, uint rc) {
+ ra = a;
+ rb = b;
+ rc = c ;
+ }
+ }
+ )";
+ compileBothVersions(sourceCode);
+ compareVersions("set()");
+ compareVersions("get()");
+
+ m_optimize = true;
+ m_optimizeRuns = 1;
+ bytes optimizedBytecode = compileAndRun(sourceCode, 0, "c");
+ bytes complicatedConstant = toBigEndian(u256("0x817416927846239487123469187231298734162934871263941234127518276"));
+ unsigned occurrences = 0;
+ for (auto iter = optimizedBytecode.cbegin(); iter < optimizedBytecode.cend(); ++occurrences)
+ iter = search(iter, optimizedBytecode.cend(), complicatedConstant.cbegin(), complicatedConstant.cend()) + 1;
+ BOOST_CHECK_EQUAL(2, occurrences);
+
+ bytes constantWithZeros = toBigEndian(u256("0x77abc0000000000000000000000000000000000000000000000000000000001"));
+ BOOST_CHECK(search(
+ optimizedBytecode.cbegin(),
+ optimizedBytecode.cend(),
+ constantWithZeros.cbegin(),
+ constantWithZeros.cend()
+ ) == optimizedBytecode.cend());
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+}
+}
+} // end namespaces
diff --git a/test/libsolidity/SolidityParser.cpp b/test/libsolidity/SolidityParser.cpp
new file mode 100644
index 00000000..438e650b
--- /dev/null
+++ b/test/libsolidity/SolidityParser.cpp
@@ -0,0 +1,921 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum 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.
+
+ cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * @author Christian <c@ethdev.com>
+ * @date 2014
+ * Unit tests for the solidity parser.
+ */
+
+#include <string>
+#include <memory>
+#include <libdevcore/Log.h>
+#include <libsolidity/Scanner.h>
+#include <libsolidity/Parser.h>
+#include <libsolidity/Exceptions.h>
+#include "../TestHelper.h"
+
+using namespace std;
+
+namespace dev
+{
+namespace solidity
+{
+namespace test
+{
+
+namespace
+{
+ASTPointer<ContractDefinition> parseText(std::string const& _source)
+{
+ Parser parser;
+ ASTPointer<SourceUnit> sourceUnit = parser.parse(std::make_shared<Scanner>(CharStream(_source)));
+ for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
+ if (ASTPointer<ContractDefinition> contract = dynamic_pointer_cast<ContractDefinition>(node))
+ return contract;
+ BOOST_FAIL("No contract found in source.");
+ return ASTPointer<ContractDefinition>();
+}
+
+static void checkFunctionNatspec(ASTPointer<FunctionDefinition> _function,
+ std::string const& _expectedDoc)
+{
+ auto doc = _function->getDocumentation();
+ BOOST_CHECK_MESSAGE(doc != nullptr, "Function does not have Natspec Doc as expected");
+ BOOST_CHECK_EQUAL(*doc, _expectedDoc);
+}
+
+}
+
+
+BOOST_AUTO_TEST_SUITE(SolidityParser)
+
+BOOST_AUTO_TEST_CASE(smoke_test)
+{
+ char const* text = "contract test {\n"
+ " uint256 stateVariable1;\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed.");
+}
+
+BOOST_AUTO_TEST_CASE(missing_variable_name_in_declaration)
+{
+ char const* text = "contract test {\n"
+ " uint256 ;\n"
+ "}\n";
+ BOOST_CHECK_THROW(parseText(text), ParserError);
+}
+
+BOOST_AUTO_TEST_CASE(empty_function)
+{
+ char const* text = "contract test {\n"
+ " uint256 stateVar;\n"
+ " function functionName(bytes20 arg1, address addr) constant\n"
+ " returns (int id)\n"
+ " { }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed.");
+}
+
+BOOST_AUTO_TEST_CASE(no_function_params)
+{
+ char const* text = "contract test {\n"
+ " uint256 stateVar;\n"
+ " function functionName() {}\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed.");
+}
+
+BOOST_AUTO_TEST_CASE(single_function_param)
+{
+ char const* text = "contract test {\n"
+ " uint256 stateVar;\n"
+ " function functionName(bytes32 input) returns (bytes32 out) {}\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed.");
+}
+
+BOOST_AUTO_TEST_CASE(function_no_body)
+{
+ char const* text = "contract test {\n"
+ " function functionName(bytes32 input) returns (bytes32 out);\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed.");
+}
+
+BOOST_AUTO_TEST_CASE(missing_parameter_name_in_named_args)
+{
+ char const* text = "contract test {\n"
+ " function a(uint a, uint b, uint c) returns (uint r) { r = a * 100 + b * 10 + c * 1; }\n"
+ " function b() returns (uint r) { r = a({: 1, : 2, : 3}); }\n"
+ "}\n";
+ BOOST_CHECK_THROW(parseText(text), ParserError);
+}
+
+BOOST_AUTO_TEST_CASE(missing_argument_in_named_args)
+{
+ char const* text = "contract test {\n"
+ " function a(uint a, uint b, uint c) returns (uint r) { r = a * 100 + b * 10 + c * 1; }\n"
+ " function b() returns (uint r) { r = a({a: , b: , c: }); }\n"
+ "}\n";
+ BOOST_CHECK_THROW(parseText(text), ParserError);
+}
+
+BOOST_AUTO_TEST_CASE(two_exact_functions)
+{
+ char const* text = R"(
+ contract test {
+ function fun(uint a) returns(uint r) { return a; }
+ function fun(uint a) returns(uint r) { return a; }
+ }
+ )";
+ // with support of overloaded functions, during parsing,
+ // we can't determine whether they match exactly, however
+ // it will throw DeclarationError in following stage.
+ BOOST_CHECK_NO_THROW(parseText(text));
+}
+
+BOOST_AUTO_TEST_CASE(overloaded_functions)
+{
+ char const* text = R"(
+ contract test {
+ function fun(uint a) returns(uint r) { return a; }
+ function fun(uint a, uint b) returns(uint r) { return a + b; }
+ }
+ )";
+ BOOST_CHECK_NO_THROW(parseText(text));
+}
+
+BOOST_AUTO_TEST_CASE(function_natspec_documentation)
+{
+ ASTPointer<ContractDefinition> contract;
+ ASTPointer<FunctionDefinition> function;
+ char const* text = "contract test {\n"
+ " uint256 stateVar;\n"
+ " /// This is a test function\n"
+ " function functionName(bytes32 input) returns (bytes32 out) {}\n"
+ "}\n";
+ ETH_TEST_REQUIRE_NO_THROW(contract = parseText(text), "Parsing failed");
+ auto functions = contract->getDefinedFunctions();
+ ETH_TEST_REQUIRE_NO_THROW(function = functions.at(0), "Failed to retrieve function");
+ checkFunctionNatspec(function, "This is a test function");
+}
+
+BOOST_AUTO_TEST_CASE(function_normal_comments)
+{
+ ASTPointer<ContractDefinition> contract;
+ ASTPointer<FunctionDefinition> function;
+ char const* text = "contract test {\n"
+ " uint256 stateVar;\n"
+ " // We won't see this comment\n"
+ " function functionName(bytes32 input) returns (bytes32 out) {}\n"
+ "}\n";
+ ETH_TEST_REQUIRE_NO_THROW(contract = parseText(text), "Parsing failed");
+ auto functions = contract->getDefinedFunctions();
+ ETH_TEST_REQUIRE_NO_THROW(function = functions.at(0), "Failed to retrieve function");
+ BOOST_CHECK_MESSAGE(function->getDocumentation() == nullptr,
+ "Should not have gotten a Natspecc comment for this function");
+}
+
+BOOST_AUTO_TEST_CASE(multiple_functions_natspec_documentation)
+{
+ ASTPointer<ContractDefinition> contract;
+ ASTPointer<FunctionDefinition> function;
+ char const* text = "contract test {\n"
+ " uint256 stateVar;\n"
+ " /// This is test function 1\n"
+ " function functionName1(bytes32 input) returns (bytes32 out) {}\n"
+ " /// This is test function 2\n"
+ " function functionName2(bytes32 input) returns (bytes32 out) {}\n"
+ " // nothing to see here\n"
+ " function functionName3(bytes32 input) returns (bytes32 out) {}\n"
+ " /// This is test function 4\n"
+ " function functionName4(bytes32 input) returns (bytes32 out) {}\n"
+ "}\n";
+ ETH_TEST_REQUIRE_NO_THROW(contract = parseText(text), "Parsing failed");
+ auto functions = contract->getDefinedFunctions();
+
+ ETH_TEST_REQUIRE_NO_THROW(function = functions.at(0), "Failed to retrieve function");
+ checkFunctionNatspec(function, "This is test function 1");
+
+ ETH_TEST_REQUIRE_NO_THROW(function = functions.at(1), "Failed to retrieve function");
+ checkFunctionNatspec(function, "This is test function 2");
+
+ ETH_TEST_REQUIRE_NO_THROW(function = functions.at(2), "Failed to retrieve function");
+ BOOST_CHECK_MESSAGE(function->getDocumentation() == nullptr,
+ "Should not have gotten natspec comment for functionName3()");
+
+ ETH_TEST_REQUIRE_NO_THROW(function = functions.at(3), "Failed to retrieve function");
+ checkFunctionNatspec(function, "This is test function 4");
+}
+
+BOOST_AUTO_TEST_CASE(multiline_function_documentation)
+{
+ ASTPointer<ContractDefinition> contract;
+ ASTPointer<FunctionDefinition> function;
+ char const* text = "contract test {\n"
+ " uint256 stateVar;\n"
+ " /// This is a test function\n"
+ " /// and it has 2 lines\n"
+ " function functionName1(bytes32 input) returns (bytes32 out) {}\n"
+ "}\n";
+ ETH_TEST_REQUIRE_NO_THROW(contract = parseText(text), "Parsing failed");
+ auto functions = contract->getDefinedFunctions();
+
+ ETH_TEST_REQUIRE_NO_THROW(function = functions.at(0), "Failed to retrieve function");
+ checkFunctionNatspec(function, "This is a test function\n"
+ " and it has 2 lines");
+}
+
+BOOST_AUTO_TEST_CASE(natspec_comment_in_function_body)
+{
+ ASTPointer<ContractDefinition> contract;
+ ASTPointer<FunctionDefinition> function;
+ char const* text = "contract test {\n"
+ " /// fun1 description\n"
+ " function fun1(uint256 a) {\n"
+ " var b;\n"
+ " /// I should not interfere with actual natspec comments\n"
+ " uint256 c;\n"
+ " mapping(address=>bytes32) d;\n"
+ " bytes7 name = \"Solidity\";"
+ " }\n"
+ " /// This is a test function\n"
+ " /// and it has 2 lines\n"
+ " function fun(bytes32 input) returns (bytes32 out) {}\n"
+ "}\n";
+ ETH_TEST_REQUIRE_NO_THROW(contract = parseText(text), "Parsing failed");
+ auto functions = contract->getDefinedFunctions();
+
+ ETH_TEST_REQUIRE_NO_THROW(function = functions.at(0), "Failed to retrieve function");
+ checkFunctionNatspec(function, "fun1 description");
+
+ ETH_TEST_REQUIRE_NO_THROW(function = functions.at(1), "Failed to retrieve function");
+ checkFunctionNatspec(function, "This is a test function\n"
+ " and it has 2 lines");
+}
+
+BOOST_AUTO_TEST_CASE(natspec_docstring_between_keyword_and_signature)
+{
+ ASTPointer<ContractDefinition> contract;
+ ASTPointer<FunctionDefinition> function;
+ char const* text = "contract test {\n"
+ " uint256 stateVar;\n"
+ " function ///I am in the wrong place \n"
+ " fun1(uint256 a) {\n"
+ " var b;\n"
+ " /// I should not interfere with actual natspec comments\n"
+ " uint256 c;\n"
+ " mapping(address=>bytes32) d;\n"
+ " bytes7 name = \"Solidity\";"
+ " }\n"
+ "}\n";
+ ETH_TEST_REQUIRE_NO_THROW(contract = parseText(text), "Parsing failed");
+ auto functions = contract->getDefinedFunctions();
+
+ ETH_TEST_REQUIRE_NO_THROW(function = functions.at(0), "Failed to retrieve function");
+ BOOST_CHECK_MESSAGE(!function->getDocumentation(),
+ "Shouldn't get natspec docstring for this function");
+}
+
+BOOST_AUTO_TEST_CASE(natspec_docstring_after_signature)
+{
+ ASTPointer<ContractDefinition> contract;
+ ASTPointer<FunctionDefinition> function;
+ char const* text = "contract test {\n"
+ " uint256 stateVar;\n"
+ " function fun1(uint256 a) {\n"
+ " /// I should have been above the function signature\n"
+ " var b;\n"
+ " /// I should not interfere with actual natspec comments\n"
+ " uint256 c;\n"
+ " mapping(address=>bytes32) d;\n"
+ " bytes7 name = \"Solidity\";"
+ " }\n"
+ "}\n";
+ ETH_TEST_REQUIRE_NO_THROW(contract = parseText(text), "Parsing failed");
+ auto functions = contract->getDefinedFunctions();
+
+ ETH_TEST_REQUIRE_NO_THROW(function = functions.at(0), "Failed to retrieve function");
+ BOOST_CHECK_MESSAGE(!function->getDocumentation(),
+ "Shouldn't get natspec docstring for this function");
+}
+
+BOOST_AUTO_TEST_CASE(struct_definition)
+{
+ char const* text = "contract test {\n"
+ " uint256 stateVar;\n"
+ " struct MyStructName {\n"
+ " address addr;\n"
+ " uint256 count;\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(mapping)
+{
+ char const* text = "contract test {\n"
+ " mapping(address => bytes32) names;\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(mapping_in_struct)
+{
+ char const* text = "contract test {\n"
+ " struct test_struct {\n"
+ " address addr;\n"
+ " uint256 count;\n"
+ " mapping(bytes32 => test_struct) self_reference;\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(mapping_to_mapping_in_struct)
+{
+ char const* text = "contract test {\n"
+ " struct test_struct {\n"
+ " address addr;\n"
+ " mapping (uint64 => mapping (bytes32 => uint)) complex_mapping;\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(variable_definition)
+{
+ char const* text = "contract test {\n"
+ " function fun(uint256 a) {\n"
+ " var b;\n"
+ " uint256 c;\n"
+ " mapping(address=>bytes32) d;\n"
+ " customtype varname;\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(variable_definition_with_initialization)
+{
+ char const* text = "contract test {\n"
+ " function fun(uint256 a) {\n"
+ " var b = 2;\n"
+ " uint256 c = 0x87;\n"
+ " mapping(address=>bytes32) d;\n"
+ " bytes7 name = \"Solidity\";"
+ " customtype varname;\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(variable_definition_in_function_parameter)
+{
+ char const* text = R"(
+ contract test {
+ function fun(var a) {}
+ }
+ )";
+ BOOST_CHECK_THROW(parseText(text), ParserError);
+}
+
+BOOST_AUTO_TEST_CASE(variable_definition_in_mapping)
+{
+ char const* text = R"(
+ contract test {
+ function fun() {
+ mapping(var=>bytes32) d;
+ }
+ }
+ )";
+ BOOST_CHECK_THROW(parseText(text), ParserError);
+}
+
+BOOST_AUTO_TEST_CASE(variable_definition_in_function_return)
+{
+ char const* text = R"(
+ contract test {
+ function fun() returns(var d) {
+ return 1;
+ }
+ }
+ )";
+ BOOST_CHECK_THROW(parseText(text), ParserError);
+}
+
+BOOST_AUTO_TEST_CASE(operator_expression)
+{
+ char const* text = "contract test {\n"
+ " function fun(uint256 a) {\n"
+ " uint256 x = (1 + 4) || false && (1 - 12) + -9;\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(complex_expression)
+{
+ char const* text = "contract test {\n"
+ " function fun(uint256 a) {\n"
+ " uint256 x = (1 + 4).member(++67)[a/=9] || true;\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(exp_expression)
+{
+ char const* text = R"(
+ contract test {
+ function fun(uint256 a) {
+ uint256 x = 3 ** a;
+ }
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(while_loop)
+{
+ char const* text = "contract test {\n"
+ " function fun(uint256 a) {\n"
+ " while (true) { uint256 x = 1; break; continue; } x = 9;\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(for_loop_vardef_initexpr)
+{
+ char const* text = "contract test {\n"
+ " function fun(uint256 a) {\n"
+ " for (uint256 i = 0; i < 10; i++)\n"
+ " { uint256 x = i; break; continue; }\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(for_loop_simple_initexpr)
+{
+ char const* text = "contract test {\n"
+ " function fun(uint256 a) {\n"
+ " uint256 i =0;\n"
+ " for (i = 0; i < 10; i++)\n"
+ " { uint256 x = i; break; continue; }\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(for_loop_simple_noexpr)
+{
+ char const* text = "contract test {\n"
+ " function fun(uint256 a) {\n"
+ " uint256 i =0;\n"
+ " for (;;)\n"
+ " { uint256 x = i; break; continue; }\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(for_loop_single_stmt_body)
+{
+ char const* text = "contract test {\n"
+ " function fun(uint256 a) {\n"
+ " uint256 i =0;\n"
+ " for (i = 0; i < 10; i++)\n"
+ " continue;\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(if_statement)
+{
+ char const* text = "contract test {\n"
+ " function fun(uint256 a) {\n"
+ " if (a >= 8) return 2; else { var b = 7; }\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(else_if_statement)
+{
+ char const* text = "contract test {\n"
+ " function fun(uint256 a) returns (address b) {\n"
+ " if (a < 0) b = 0x67; else if (a == 0) b = 0x12; else b = 0x78;\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(statement_starting_with_type_conversion)
+{
+ char const* text = "contract test {\n"
+ " function fun() {\n"
+ " uint64(2);\n"
+ " uint64[7](3);\n"
+ " uint64[](3);\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(type_conversion_to_dynamic_array)
+{
+ char const* text = "contract test {\n"
+ " function fun() {\n"
+ " var x = uint64[](3);\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(import_directive)
+{
+ char const* text = "import \"abc\";\n"
+ "contract test {\n"
+ " function fun() {\n"
+ " uint64(2);\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(multiple_contracts)
+{
+ char const* text = "contract test {\n"
+ " function fun() {\n"
+ " uint64(2);\n"
+ " }\n"
+ "}\n"
+ "contract test2 {\n"
+ " function fun() {\n"
+ " uint64(2);\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(multiple_contracts_and_imports)
+{
+ char const* text = "import \"abc\";\n"
+ "contract test {\n"
+ " function fun() {\n"
+ " uint64(2);\n"
+ " }\n"
+ "}\n"
+ "import \"def\";\n"
+ "contract test2 {\n"
+ " function fun() {\n"
+ " uint64(2);\n"
+ " }\n"
+ "}\n"
+ "import \"ghi\";\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(contract_inheritance)
+{
+ char const* text = "contract base {\n"
+ " function fun() {\n"
+ " uint64(2);\n"
+ " }\n"
+ "}\n"
+ "contract derived is base {\n"
+ " function fun() {\n"
+ " uint64(2);\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(contract_multiple_inheritance)
+{
+ char const* text = "contract base {\n"
+ " function fun() {\n"
+ " uint64(2);\n"
+ " }\n"
+ "}\n"
+ "contract derived is base, nonExisting {\n"
+ " function fun() {\n"
+ " uint64(2);\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(contract_multiple_inheritance_with_arguments)
+{
+ char const* text = "contract base {\n"
+ " function fun() {\n"
+ " uint64(2);\n"
+ " }\n"
+ "}\n"
+ "contract derived is base(2), nonExisting(\"abc\", \"def\", base.fun()) {\n"
+ " function fun() {\n"
+ " uint64(2);\n"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(placeholder_in_function_context)
+{
+ char const* text = "contract c {\n"
+ " function fun() returns (uint r) {\n"
+ " var _ = 8;\n"
+ " return _ + 1;"
+ " }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(modifier)
+{
+ char const* text = "contract c {\n"
+ " modifier mod { if (msg.sender == 0) _ }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(modifier_arguments)
+{
+ char const* text = "contract c {\n"
+ " modifier mod(uint a) { if (msg.sender == a) _ }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(modifier_invocation)
+{
+ char const* text = "contract c {\n"
+ " modifier mod1(uint a) { if (msg.sender == a) _ }\n"
+ " modifier mod2 { if (msg.sender == 2) _ }\n"
+ " function f() mod1(7) mod2 { }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(fallback_function)
+{
+ char const* text = "contract c {\n"
+ " function() { }\n"
+ "}\n";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(event)
+{
+ char const* text = R"(
+ contract c {
+ event e();
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(event_arguments)
+{
+ char const* text = R"(
+ contract c {
+ event e(uint a, bytes32 s);
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(event_arguments_indexed)
+{
+ char const* text = R"(
+ contract c {
+ event e(uint a, bytes32 indexed s, bool indexed b);
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(visibility_specifiers)
+{
+ char const* text = R"(
+ contract c {
+ uint private a;
+ uint internal b;
+ uint public c;
+ uint d;
+ function f() {}
+ function f_priv() private {}
+ function f_public() public {}
+ function f_internal() internal {}
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(multiple_visibility_specifiers)
+{
+ char const* text = R"(
+ contract c {
+ uint private internal a;
+ })";
+ BOOST_CHECK_THROW(parseText(text), ParserError);
+}
+
+BOOST_AUTO_TEST_CASE(literal_constants_with_ether_subdenominations)
+{
+ char const* text = R"(
+ contract c {
+ function c ()
+ {
+ a = 1 wei;
+ b = 2 szabo;
+ c = 3 finney;
+ b = 4 ether;
+ }
+ uint256 a;
+ uint256 b;
+ uint256 c;
+ uint256 d;
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(literal_constants_with_ether_subdenominations_in_expressions)
+{
+ char const* text = R"(
+ contract c {
+ function c ()
+ {
+ a = 1 wei * 100 wei + 7 szabo - 3;
+ }
+ uint256 a;
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(enum_valid_declaration)
+{
+ char const* text = R"(
+ contract c {
+ enum validEnum { Value1, Value2, Value3, Value4 }
+ function c ()
+ {
+ a = foo.Value3;
+ }
+ uint256 a;
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(empty_enum_declaration)
+{
+ char const* text = R"(
+ contract c {
+ enum foo { }
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(malformed_enum_declaration)
+{
+ char const* text = R"(
+ contract c {
+ enum foo { WARNING,}
+ })";
+ BOOST_CHECK_THROW(parseText(text), ParserError);
+}
+
+BOOST_AUTO_TEST_CASE(external_function)
+{
+ char const* text = R"(
+ contract c {
+ function x() external {}
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(external_variable)
+{
+ char const* text = R"(
+ contract c {
+ uint external x;
+ })";
+ BOOST_CHECK_THROW(parseText(text), ParserError);
+}
+
+BOOST_AUTO_TEST_CASE(arrays_in_storage)
+{
+ char const* text = R"(
+ contract c {
+ uint[10] a;
+ uint[] a2;
+ struct x { uint[2**20] b; y[0] c; }
+ struct y { uint d; mapping(uint=>x)[] e; }
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(arrays_in_events)
+{
+ char const* text = R"(
+ contract c {
+ event e(uint[10] a, bytes7[8] indexed b, c[3] x);
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(arrays_in_expressions)
+{
+ char const* text = R"(
+ contract c {
+ function f() { c[10] a = 7; uint8[10 * 2] x; }
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(multi_arrays)
+{
+ char const* text = R"(
+ contract c {
+ mapping(uint => mapping(uint => int8)[8][][9])[] x;
+ })";
+ ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
+}
+
+BOOST_AUTO_TEST_CASE(constant_is_keyword)
+{
+ char const* text = R"(
+ contract Foo {
+ uint constant = 4;
+ })";
+ BOOST_CHECK_THROW(parseText(text), ParserError);
+}
+
+BOOST_AUTO_TEST_CASE(var_array)
+{
+ char const* text = R"(
+ contract Foo {
+ function f() { var[] a; }
+ })";
+ BOOST_CHECK_THROW(parseText(text), ParserError);
+}
+
+BOOST_AUTO_TEST_CASE(location_specifiers_for_params)
+{
+ char const* text = R"(
+ contract Foo {
+ function f(uint[] storage constant x, uint[] memory y) { }
+ }
+ )";
+ BOOST_CHECK_NO_THROW(parseText(text));
+}
+
+BOOST_AUTO_TEST_CASE(location_specifiers_for_locals)
+{
+ char const* text = R"(
+ contract Foo {
+ function f() {
+ uint[] storage x;
+ uint[] memory y;
+ }
+ }
+ )";
+ BOOST_CHECK_NO_THROW(parseText(text));
+}
+
+BOOST_AUTO_TEST_CASE(location_specifiers_for_state)
+{
+ char const* text = R"(
+ contract Foo {
+ uint[] memory x;
+ })";
+ BOOST_CHECK_THROW(parseText(text), ParserError);
+}
+
+BOOST_AUTO_TEST_CASE(location_specifiers_with_var)
+{
+ char const* text = R"(
+ contract Foo {
+ function f() { var memory x; }
+ })";
+ BOOST_CHECK_THROW(parseText(text), ParserError);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+}
+}
+} // end namespaces
diff --git a/test/libsolidity/SolidityScanner.cpp b/test/libsolidity/SolidityScanner.cpp
new file mode 100644
index 00000000..8d3e5392
--- /dev/null
+++ b/test/libsolidity/SolidityScanner.cpp
@@ -0,0 +1,288 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum 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.
+
+ cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * @author Christian <c@ethdev.com>
+ * @date 2014
+ * Unit tests for the solidity scanner.
+ */
+
+#include <libsolidity/Scanner.h>
+#include <boost/test/unit_test.hpp>
+
+namespace dev
+{
+namespace solidity
+{
+namespace test
+{
+
+BOOST_AUTO_TEST_SUITE(SolidityScanner)
+
+BOOST_AUTO_TEST_CASE(test_empty)
+{
+ Scanner scanner(CharStream(""));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::EOS);
+}
+
+BOOST_AUTO_TEST_CASE(smoke_test)
+{
+ Scanner scanner(CharStream("function break;765 \t \"string1\",'string2'\nidentifier1"));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::Function);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Break);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Semicolon);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Number);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLiteral(), "765");
+ BOOST_CHECK_EQUAL(scanner.next(), Token::StringLiteral);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLiteral(), "string1");
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Comma);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::StringLiteral);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLiteral(), "string2");
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLiteral(), "identifier1");
+ BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
+}
+
+BOOST_AUTO_TEST_CASE(string_escapes)
+{
+ Scanner scanner(CharStream(" { \"a\\x61\""));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::LBrace);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::StringLiteral);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLiteral(), "aa");
+}
+
+BOOST_AUTO_TEST_CASE(string_escapes_with_zero)
+{
+ Scanner scanner(CharStream(" { \"a\\x61\\x00abc\""));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::LBrace);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::StringLiteral);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLiteral(), std::string("aa\0abc", 6));
+}
+
+BOOST_AUTO_TEST_CASE(string_escape_illegal)
+{
+ Scanner scanner(CharStream(" bla \"\\x6rf\" (illegalescape)"));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Illegal);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLiteral(), "");
+ // TODO recovery from illegal tokens should be improved
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Illegal);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Illegal);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
+}
+
+BOOST_AUTO_TEST_CASE(hex_numbers)
+{
+ Scanner scanner(CharStream("var x = 0x765432536763762734623472346;"));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::Var);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Assign);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Number);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLiteral(), "0x765432536763762734623472346");
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Semicolon);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
+}
+
+BOOST_AUTO_TEST_CASE(negative_numbers)
+{
+ Scanner scanner(CharStream("var x = -.2 + -0x78 + -7.3 + 8.9;"));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::Var);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Assign);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Sub);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Number);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLiteral(), ".2");
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Add);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Sub);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Number);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLiteral(), "0x78");
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Add);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Sub);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Number);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLiteral(), "7.3");
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Add);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Number);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLiteral(), "8.9");
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Semicolon);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
+}
+
+BOOST_AUTO_TEST_CASE(locations)
+{
+ Scanner scanner(CharStream("function_identifier has ; -0x743/*comment*/\n ident //comment"));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLocation().start, 0);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLocation().end, 19);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLocation().start, 20);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLocation().end, 23);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Semicolon);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLocation().start, 24);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLocation().end, 25);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Sub);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Number);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLocation().start, 27);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLocation().end, 32);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLocation().start, 45);
+ BOOST_CHECK_EQUAL(scanner.getCurrentLocation().end, 50);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
+}
+
+BOOST_AUTO_TEST_CASE(ambiguities)
+{
+ // test scanning of some operators which need look-ahead
+ Scanner scanner(CharStream("<=""<""+ +=a++ =>""<<"));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::LessThanOrEqual);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::LessThan);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Add);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::AssignAdd);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Inc);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Arrow);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::SHL);
+}
+
+BOOST_AUTO_TEST_CASE(documentation_comments_parsed_begin)
+{
+ Scanner scanner(CharStream("/// Send $(value / 1000) chocolates to the user"));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::EOS);
+ BOOST_CHECK_EQUAL(scanner.getCurrentCommentLiteral(), "Send $(value / 1000) chocolates to the user");
+}
+
+BOOST_AUTO_TEST_CASE(multiline_documentation_comments_parsed_begin)
+{
+ Scanner scanner(CharStream("/** Send $(value / 1000) chocolates to the user*/"));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::EOS);
+ BOOST_CHECK_EQUAL(scanner.getCurrentCommentLiteral(), "Send $(value / 1000) chocolates to the user");
+}
+
+BOOST_AUTO_TEST_CASE(documentation_comments_parsed)
+{
+ Scanner scanner(CharStream("some other tokens /// Send $(value / 1000) chocolates to the user"));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
+ BOOST_CHECK_EQUAL(scanner.getCurrentCommentLiteral(), "Send $(value / 1000) chocolates to the user");
+}
+
+BOOST_AUTO_TEST_CASE(multiline_documentation_comments_parsed)
+{
+ Scanner scanner(CharStream("some other tokens /**\n"
+ "* Send $(value / 1000) chocolates to the user\n"
+ "*/"));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
+ BOOST_CHECK_EQUAL(scanner.getCurrentCommentLiteral(), "Send $(value / 1000) chocolates to the user");
+}
+
+BOOST_AUTO_TEST_CASE(multiline_documentation_no_stars)
+{
+ Scanner scanner(CharStream("some other tokens /**\n"
+ " Send $(value / 1000) chocolates to the user\n"
+ "*/"));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
+ BOOST_CHECK_EQUAL(scanner.getCurrentCommentLiteral(), "Send $(value / 1000) chocolates to the user");
+}
+
+BOOST_AUTO_TEST_CASE(multiline_documentation_whitespace_hell)
+{
+ Scanner scanner(CharStream("some other tokens /** \t \r \n"
+ "\t \r * Send $(value / 1000) chocolates to the user\n"
+ "*/"));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
+ BOOST_CHECK_EQUAL(scanner.getCurrentCommentLiteral(), "Send $(value / 1000) chocolates to the user");
+}
+
+BOOST_AUTO_TEST_CASE(comment_before_eos)
+{
+ Scanner scanner(CharStream("//"));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::EOS);
+ BOOST_CHECK_EQUAL(scanner.getCurrentCommentLiteral(), "");
+}
+
+BOOST_AUTO_TEST_CASE(documentation_comment_before_eos)
+{
+ Scanner scanner(CharStream("///"));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::EOS);
+ BOOST_CHECK_EQUAL(scanner.getCurrentCommentLiteral(), "");
+}
+
+BOOST_AUTO_TEST_CASE(empty_multiline_comment)
+{
+ Scanner scanner(CharStream("/**/"));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::EOS);
+ BOOST_CHECK_EQUAL(scanner.getCurrentCommentLiteral(), "");
+}
+
+BOOST_AUTO_TEST_CASE(empty_multiline_documentation_comment_before_eos)
+{
+ Scanner scanner(CharStream("/***/"));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::EOS);
+ BOOST_CHECK_EQUAL(scanner.getCurrentCommentLiteral(), "");
+}
+
+BOOST_AUTO_TEST_CASE(comments_mixed_in_sequence)
+{
+ Scanner scanner(CharStream("hello_world ///documentation comment \n"
+ "//simple comment \n"
+ "<<"));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::SHL);
+ BOOST_CHECK_EQUAL(scanner.getCurrentCommentLiteral(), "documentation comment ");
+}
+
+BOOST_AUTO_TEST_CASE(ether_subdenominations)
+{
+ Scanner scanner(CharStream("wei szabo finney ether"));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::SubWei);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::SubSzabo);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::SubFinney);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::SubEther);
+}
+
+BOOST_AUTO_TEST_CASE(time_subdenominations)
+{
+ Scanner scanner(CharStream("seconds minutes hours days weeks years"));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::SubSecond);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::SubMinute);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::SubHour);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::SubDay);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::SubWeek);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::SubYear);
+}
+
+BOOST_AUTO_TEST_CASE(time_after)
+{
+ Scanner scanner(CharStream("after 1"));
+ BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::After);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+}
+}
+} // end namespaces
diff --git a/test/libsolidity/SolidityTypes.cpp b/test/libsolidity/SolidityTypes.cpp
new file mode 100644
index 00000000..7892de67
--- /dev/null
+++ b/test/libsolidity/SolidityTypes.cpp
@@ -0,0 +1,93 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum 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.
+
+ cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * @author Christian <c@ethdev.com>
+ * @date 2015
+ * Unit tests for the type system of Solidity.
+ */
+
+#include <libsolidity/Types.h>
+#include <boost/test/unit_test.hpp>
+
+using namespace std;
+
+namespace dev
+{
+namespace solidity
+{
+namespace test
+{
+
+BOOST_AUTO_TEST_SUITE(SolidityTypes)
+
+BOOST_AUTO_TEST_CASE(storage_layout_simple)
+{
+ MemberList members(MemberList::MemberMap({
+ {string("first"), Type::fromElementaryTypeName("uint128")},
+ {string("second"), Type::fromElementaryTypeName("uint120")},
+ {string("wraps"), Type::fromElementaryTypeName("uint16")}
+ }));
+ BOOST_REQUIRE_EQUAL(u256(2), members.getStorageSize());
+ BOOST_REQUIRE(members.getMemberStorageOffset("first") != nullptr);
+ BOOST_REQUIRE(members.getMemberStorageOffset("second") != nullptr);
+ BOOST_REQUIRE(members.getMemberStorageOffset("wraps") != nullptr);
+ BOOST_CHECK(*members.getMemberStorageOffset("first") == make_pair(u256(0), unsigned(0)));
+ BOOST_CHECK(*members.getMemberStorageOffset("second") == make_pair(u256(0), unsigned(16)));
+ BOOST_CHECK(*members.getMemberStorageOffset("wraps") == make_pair(u256(1), unsigned(0)));
+}
+
+BOOST_AUTO_TEST_CASE(storage_layout_mapping)
+{
+ MemberList members(MemberList::MemberMap({
+ {string("first"), Type::fromElementaryTypeName("uint128")},
+ {string("second"), make_shared<MappingType>(
+ Type::fromElementaryTypeName("uint8"),
+ Type::fromElementaryTypeName("uint8")
+ )},
+ {string("third"), Type::fromElementaryTypeName("uint16")},
+ {string("final"), make_shared<MappingType>(
+ Type::fromElementaryTypeName("uint8"),
+ Type::fromElementaryTypeName("uint8")
+ )},
+ }));
+ BOOST_REQUIRE_EQUAL(u256(4), members.getStorageSize());
+ BOOST_REQUIRE(members.getMemberStorageOffset("first") != nullptr);
+ BOOST_REQUIRE(members.getMemberStorageOffset("second") != nullptr);
+ BOOST_REQUIRE(members.getMemberStorageOffset("third") != nullptr);
+ BOOST_REQUIRE(members.getMemberStorageOffset("final") != nullptr);
+ BOOST_CHECK(*members.getMemberStorageOffset("first") == make_pair(u256(0), unsigned(0)));
+ BOOST_CHECK(*members.getMemberStorageOffset("second") == make_pair(u256(1), unsigned(0)));
+ BOOST_CHECK(*members.getMemberStorageOffset("third") == make_pair(u256(2), unsigned(0)));
+ BOOST_CHECK(*members.getMemberStorageOffset("final") == make_pair(u256(3), unsigned(0)));
+}
+
+BOOST_AUTO_TEST_CASE(storage_layout_arrays)
+{
+ BOOST_CHECK(ArrayType(DataLocation::Storage, make_shared<FixedBytesType>(1), 32).getStorageSize() == 1);
+ BOOST_CHECK(ArrayType(DataLocation::Storage, make_shared<FixedBytesType>(1), 33).getStorageSize() == 2);
+ BOOST_CHECK(ArrayType(DataLocation::Storage, make_shared<FixedBytesType>(2), 31).getStorageSize() == 2);
+ BOOST_CHECK(ArrayType(DataLocation::Storage, make_shared<FixedBytesType>(7), 8).getStorageSize() == 2);
+ BOOST_CHECK(ArrayType(DataLocation::Storage, make_shared<FixedBytesType>(7), 9).getStorageSize() == 3);
+ BOOST_CHECK(ArrayType(DataLocation::Storage, make_shared<FixedBytesType>(31), 9).getStorageSize() == 9);
+ BOOST_CHECK(ArrayType(DataLocation::Storage, make_shared<FixedBytesType>(32), 9).getStorageSize() == 9);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+}
+}
+}
diff --git a/test/libsolidity/solidityExecutionFramework.h b/test/libsolidity/solidityExecutionFramework.h
new file mode 100644
index 00000000..05f93bb6
--- /dev/null
+++ b/test/libsolidity/solidityExecutionFramework.h
@@ -0,0 +1,310 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum 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.
+
+ cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * @author Christian <c@ethdev.com>
+ * @date 2014
+ * Framework for executing Solidity contracts and testing them against C++ implementation.
+ */
+
+#pragma once
+
+#include <string>
+#include <tuple>
+#include "../TestHelper.h"
+#include <libethcore/ABI.h>
+#include <libethereum/State.h>
+#include <libethereum/Executive.h>
+#include <libsolidity/CompilerStack.h>
+#include <libsolidity/Exceptions.h>
+
+namespace dev
+{
+
+namespace solidity
+{
+namespace test
+{
+
+class ExecutionFramework
+{
+public:
+ ExecutionFramework()
+ {
+ if (g_logVerbosity != -1)
+ g_logVerbosity = 0;
+ //m_state.resetCurrent();
+ }
+
+ bytes const& compileAndRunWithoutCheck(
+ std::string const& _sourceCode,
+ u256 const& _value = 0,
+ std::string const& _contractName = "",
+ bytes const& _arguments = bytes()
+ )
+ {
+ m_compiler.reset(false, m_addStandardSources);
+ m_compiler.addSource("", _sourceCode);
+ ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize, m_optimizeRuns), "Compiling contract failed");
+ bytes code = m_compiler.getBytecode(_contractName);
+ sendMessage(code + _arguments, true, _value);
+ return m_output;
+ }
+
+ template <class Exceptiontype>
+ void compileRequireThrow(std::string const& _sourceCode)
+ {
+ m_compiler.reset(false, m_addStandardSources);
+ m_compiler.addSource("", _sourceCode);
+ BOOST_REQUIRE_THROW(m_compiler.compile(m_optimize, m_optimizeRuns), Exceptiontype);
+ }
+
+ bytes const& compileAndRun(
+ std::string const& _sourceCode,
+ u256 const& _value = 0,
+ std::string const& _contractName = "",
+ bytes const& _arguments = bytes()
+ )
+ {
+ compileAndRunWithoutCheck(_sourceCode, _value, _contractName, _arguments);
+ BOOST_REQUIRE(!m_output.empty());
+ return m_output;
+ }
+
+ template <class... Args>
+ bytes const& callContractFunctionWithValue(std::string _sig, u256 const& _value, Args const&... _arguments)
+ {
+ FixedHash<4> hash(dev::sha3(_sig));
+ sendMessage(hash.asBytes() + encodeArgs(_arguments...), false, _value);
+ return m_output;
+ }
+
+ template <class... Args>
+ bytes const& callContractFunction(std::string _sig, Args const&... _arguments)
+ {
+ return callContractFunctionWithValue(_sig, 0, _arguments...);
+ }
+
+ template <class CppFunction, class... Args>
+ void testSolidityAgainstCpp(std::string _sig, CppFunction const& _cppFunction, Args const&... _arguments)
+ {
+ bytes solidityResult = callContractFunction(_sig, _arguments...);
+ bytes cppResult = callCppAndEncodeResult(_cppFunction, _arguments...);
+ BOOST_CHECK_MESSAGE(
+ solidityResult == cppResult,
+ "Computed values do not match.\nSolidity: " +
+ toHex(solidityResult) +
+ "\nC++: " +
+ toHex(cppResult));
+ }
+
+ template <class CppFunction, class... Args>
+ void testSolidityAgainstCppOnRange(std::string _sig, CppFunction const& _cppFunction, u256 const& _rangeStart, u256 const& _rangeEnd)
+ {
+ for (u256 argument = _rangeStart; argument < _rangeEnd; ++argument)
+ {
+ bytes solidityResult = callContractFunction(_sig, argument);
+ bytes cppResult = callCppAndEncodeResult(_cppFunction, argument);
+ BOOST_CHECK_MESSAGE(
+ solidityResult == cppResult,
+ "Computed values do not match.\nSolidity: " +
+ toHex(solidityResult) +
+ "\nC++: " +
+ toHex(cppResult) +
+ "\nArgument: " +
+ toHex(encode(argument))
+ );
+ }
+ }
+
+ static bytes encode(bool _value) { return encode(byte(_value)); }
+ 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)); }
+ static bytes encode(byte _value) { return bytes(31, 0) + bytes{_value}; }
+ static bytes encode(u256 const& _value) { return toBigEndian(_value); }
+ static bytes encode(h256 const& _value) { return _value.asBytes(); }
+ static bytes encode(bytes const& _value, bool _padLeft = true)
+ {
+ bytes padding = bytes((32 - _value.size() % 32) % 32, 0);
+ return _padLeft ? padding + _value : _value + padding;
+ }
+ static bytes encode(std::string const& _value) { return encode(asBytes(_value), false); }
+ template <class _T>
+ static bytes encode(std::vector<_T> const& _value)
+ {
+ 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();
+ }
+ //@todo might be extended in the future
+ template <class Arg>
+ static bytes encodeDyn(Arg const& _arg)
+ {
+ return encodeArgs(u256(0x20), u256(_arg.size()), _arg);
+ }
+
+ class ContractInterface
+ {
+ public:
+ ContractInterface(ExecutionFramework& _framework): m_framework(_framework) {}
+
+ void setNextValue(u256 const& _value) { m_nextValue = _value; }
+
+ protected:
+ template <class... Args>
+ bytes const& call(std::string const& _sig, Args const&... _arguments)
+ {
+ auto const& ret = m_framework.callContractFunctionWithValue(_sig, m_nextValue, _arguments...);
+ m_nextValue = 0;
+ return ret;
+ }
+
+ void callString(std::string const& _name, std::string const& _arg)
+ {
+ BOOST_CHECK(call(_name + "(string)", u256(0x20), _arg.length(), _arg).empty());
+ }
+
+ void callStringAddress(std::string const& _name, std::string const& _arg1, u160 const& _arg2)
+ {
+ BOOST_CHECK(call(_name + "(string,address)", u256(0x40), _arg2, _arg1.length(), _arg1).empty());
+ }
+
+ void callStringAddressBool(std::string const& _name, std::string const& _arg1, u160 const& _arg2, bool _arg3)
+ {
+ BOOST_CHECK(call(_name + "(string,address,bool)", u256(0x60), _arg2, _arg3, _arg1.length(), _arg1).empty());
+ }
+
+ void callStringBytes32(std::string const& _name, std::string const& _arg1, h256 const& _arg2)
+ {
+ BOOST_CHECK(call(_name + "(string,bytes32)", u256(0x40), _arg2, _arg1.length(), _arg1).empty());
+ }
+
+ u160 callStringReturnsAddress(std::string const& _name, std::string const& _arg)
+ {
+ bytes const& ret = call(_name + "(string)", u256(0x20), _arg.length(), _arg);
+ BOOST_REQUIRE(ret.size() == 0x20);
+ BOOST_CHECK(std::count(ret.begin(), ret.begin() + 12, 0) == 12);
+ return eth::abiOut<u160>(ret);
+ }
+
+ std::string callAddressReturnsString(std::string const& _name, u160 const& _arg)
+ {
+ bytesConstRef ret = ref(call(_name + "(address)", _arg));
+ BOOST_REQUIRE(ret.size() >= 0x20);
+ u256 offset = eth::abiOut<u256>(ret);
+ BOOST_REQUIRE_EQUAL(offset, 0x20);
+ u256 len = eth::abiOut<u256>(ret);
+ BOOST_REQUIRE_EQUAL(ret.size(), ((len + 0x1f) / 0x20) * 0x20);
+ return ret.cropped(0, size_t(len)).toString();
+ }
+
+ h256 callStringReturnsBytes32(std::string const& _name, std::string const& _arg)
+ {
+ bytes const& ret = call(_name + "(string)", u256(0x20), _arg.length(), _arg);
+ BOOST_REQUIRE(ret.size() == 0x20);
+ return eth::abiOut<h256>(ret);
+ }
+
+ private:
+ u256 m_nextValue;
+ ExecutionFramework& m_framework;
+ };
+
+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:
+ void sendMessage(bytes const& _data, bool _isCreation, u256 const& _value = 0)
+ {
+ m_state.addBalance(m_sender, _value); // just in case
+ eth::Executive executive(m_state, m_envInfo, 0);
+ eth::ExecutionResult res;
+ executive.setResultRecipient(res);
+ eth::Transaction t =
+ _isCreation ?
+ eth::Transaction(_value, m_gasPrice, m_gas, _data, 0, KeyPair::create().sec()) :
+ eth::Transaction(_value, m_gasPrice, m_gas, m_contractAddress, _data, 0, KeyPair::create().sec());
+ bytes transactionRLP = t.rlp();
+ try
+ {
+ // this will throw since the transaction is invalid, but it should nevertheless store the transaction
+ executive.initialize(&transactionRLP);
+ executive.execute();
+ }
+ catch (...) {}
+ if (_isCreation)
+ {
+ BOOST_REQUIRE(!executive.create(m_sender, _value, m_gasPrice, m_gas, &_data, m_sender));
+ m_contractAddress = executive.newAddress();
+ BOOST_REQUIRE(m_contractAddress);
+ BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress));
+ }
+ else
+ {
+ BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress));
+ BOOST_REQUIRE(!executive.call(m_contractAddress, m_sender, _value, m_gasPrice, &_data, m_gas));
+ }
+ BOOST_REQUIRE(executive.go(/* DEBUG eth::Executive::simpleTrace() */));
+ m_state.noteSending(m_sender);
+ executive.finalize();
+ m_gasUsed = res.gasUsed;
+ m_output = std::move(res.output);
+ m_logs = executive.logs();
+ }
+
+ size_t m_optimizeRuns = 200;
+ bool m_optimize = false;
+ bool m_addStandardSources = false;
+ dev::solidity::CompilerStack m_compiler;
+ Address m_sender;
+ Address m_contractAddress;
+ eth::EnvInfo m_envInfo;
+ eth::State m_state;
+ u256 const m_gasPrice = 100 * eth::szabo;
+ u256 const m_gas = 100000000;
+ bytes m_output;
+ eth::LogEntries m_logs;
+ u256 m_gasUsed;
+};
+
+}
+}
+} // end namespaces
+