From e000d36b3c9187539bdb36ba18d0d8c0c218a399 Mon Sep 17 00:00:00 2001 From: Vlad Gluhovsky <gluk256@gmail.com> Date: Fri, 7 Aug 2015 19:50:35 +0200 Subject: [PATCH 01/16] debug --- libp2p/NodeTable.cpp | 15 ++++++++++++--- libp2p/NodeTable.h | 3 +++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/libp2p/NodeTable.cpp b/libp2p/NodeTable.cpp index 44ab0aa99..19c86cc0f 100644 --- a/libp2p/NodeTable.cpp +++ b/libp2p/NodeTable.cpp @@ -46,6 +46,7 @@ NodeTable::NodeTable(ba::io_service& _io, KeyPair const& _alias, NodeIPEndpoint m_socket(new NodeSocket(_io, *this, (bi::udp::endpoint)m_node.endpoint)), m_socketPointer(m_socket.get()), m_timers(_io) + ,m_debug_destroyed(false) { for (unsigned i = 0; i < s_bins; i++) m_state[i].distance = i; @@ -67,8 +68,12 @@ NodeTable::NodeTable(ba::io_service& _io, KeyPair const& _alias, NodeIPEndpoint NodeTable::~NodeTable() { - m_timers.stop(); - m_socketPointer->disconnect(); + DEV_GUARDED(x_debug) + { + m_debug_destroyed = true; + m_timers.stop(); + m_socketPointer->disconnect(); + } } void NodeTable::processEvents() @@ -200,7 +205,11 @@ void NodeTable::doDiscover(NodeId _node, unsigned _round, shared_ptr<set<shared_ { if (_ec) clog(NodeTableWarn) << "Discovery timer canceled!"; - doDiscover(_node, _round + 1, _tried); + + if (!m_debug_destroyed) + DEV_GUARDED(x_debug) + if (!m_debug_destroyed) + doDiscover(_node, _round + 1, _tried); }); } diff --git a/libp2p/NodeTable.h b/libp2p/NodeTable.h index 4d6b02d99..9fbe6d4a1 100644 --- a/libp2p/NodeTable.h +++ b/libp2p/NodeTable.h @@ -265,6 +265,9 @@ private: NodeSocket* m_socketPointer; ///< Set to m_socket.get(). Socket is created in constructor and disconnected in destructor to ensure access to pointer is safe. DeadlineOps m_timers; + + Mutex x_debug; + bool m_debug_destroyed; }; inline std::ostream& operator<<(std::ostream& _out, NodeTable const& _nodeTable) From 2996033df0cf370c4987b346a9c455db04a38cb1 Mon Sep 17 00:00:00 2001 From: Vlad Gluhovsky <gluk256@gmail.com> Date: Mon, 10 Aug 2015 20:37:56 +0200 Subject: [PATCH 02/16] bugfix: error code 995 --- libp2p/Common.h | 4 +++- libp2p/NodeTable.cpp | 24 ++++++++++++------------ libp2p/NodeTable.h | 3 --- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/libp2p/Common.h b/libp2p/Common.h index 1b65d4759..5d4b136ff 100644 --- a/libp2p/Common.h +++ b/libp2p/Common.h @@ -221,7 +221,7 @@ class DeadlineOps { public: DeadlineOp(ba::io_service& _io, unsigned _msInFuture, std::function<void(boost::system::error_code const&)> const& _f): m_timer(new ba::deadline_timer(_io)) { m_timer->expires_from_now(boost::posix_time::milliseconds(_msInFuture)); m_timer->async_wait(_f); } - ~DeadlineOp() {} + ~DeadlineOp() { if (m_timer) m_timer->cancel(); } DeadlineOp(DeadlineOp&& _s): m_timer(_s.m_timer.release()) {} DeadlineOp& operator=(DeadlineOp&& _s) { m_timer.reset(_s.m_timer.release()); return *this; } @@ -241,6 +241,8 @@ public: void schedule(unsigned _msInFuture, std::function<void(boost::system::error_code const&)> const& _f) { if (m_stopped) return; DEV_GUARDED(x_timers) m_timers.emplace_back(m_io, _msInFuture, _f); } void stop() { m_stopped = true; DEV_GUARDED(x_timers) m_timers.clear(); } + + bool isStopped() const { return m_stopped; } protected: void reap(); diff --git a/libp2p/NodeTable.cpp b/libp2p/NodeTable.cpp index 19c86cc0f..94803f3f0 100644 --- a/libp2p/NodeTable.cpp +++ b/libp2p/NodeTable.cpp @@ -46,7 +46,6 @@ NodeTable::NodeTable(ba::io_service& _io, KeyPair const& _alias, NodeIPEndpoint m_socket(new NodeSocket(_io, *this, (bi::udp::endpoint)m_node.endpoint)), m_socketPointer(m_socket.get()), m_timers(_io) - ,m_debug_destroyed(false) { for (unsigned i = 0; i < s_bins; i++) m_state[i].distance = i; @@ -68,12 +67,8 @@ NodeTable::NodeTable(ba::io_service& _io, KeyPair const& _alias, NodeIPEndpoint NodeTable::~NodeTable() { - DEV_GUARDED(x_debug) - { - m_debug_destroyed = true; - m_timers.stop(); - m_socketPointer->disconnect(); - } + m_socketPointer->disconnect(); + m_timers.stop(); } void NodeTable::processEvents() @@ -204,12 +199,17 @@ void NodeTable::doDiscover(NodeId _node, unsigned _round, shared_ptr<set<shared_ m_timers.schedule(c_reqTimeout.count() * 2, [this, _node, _round, _tried](boost::system::error_code const& _ec) { if (_ec) - clog(NodeTableWarn) << "Discovery timer canceled!"; + clog(NodeTableWarn) << "Discovery timer canceled: " << _ec.value() << _ec.message(); + + if (995 == _ec.value() || m_timers.isStopped()) + return; + + // Error code 995 means that the timer was probably aborted. It usually happens when "this" object + // is deallocated, in which case subsequent call to doDiscover() would cause a crash. + // We can not rely on m_timers.isStopped(), because "this" pointer was captured by the lambda, + // and therefore, in case of deallocation m_timers object no longer exists. - if (!m_debug_destroyed) - DEV_GUARDED(x_debug) - if (!m_debug_destroyed) - doDiscover(_node, _round + 1, _tried); + doDiscover(_node, _round + 1, _tried); }); } diff --git a/libp2p/NodeTable.h b/libp2p/NodeTable.h index 9fbe6d4a1..4d6b02d99 100644 --- a/libp2p/NodeTable.h +++ b/libp2p/NodeTable.h @@ -265,9 +265,6 @@ private: NodeSocket* m_socketPointer; ///< Set to m_socket.get(). Socket is created in constructor and disconnected in destructor to ensure access to pointer is safe. DeadlineOps m_timers; - - Mutex x_debug; - bool m_debug_destroyed; }; inline std::ostream& operator<<(std::ostream& _out, NodeTable const& _nodeTable) From 6beb7a69a1ec3630de41b9010b7282c7a2a75693 Mon Sep 17 00:00:00 2001 From: chriseth <c@ethdev.com> Date: Tue, 11 Aug 2015 00:04:01 +0200 Subject: [PATCH 03/16] Provide global function loadScript to load external file. --- libjsengine/JSEngine.h | 2 +- libjsengine/JSV8Engine.cpp | 46 +++++++++++++++++++++++++++++++++----- libjsengine/JSV8Engine.h | 2 +- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/libjsengine/JSEngine.h b/libjsengine/JSEngine.h index adebfdd13..961026b60 100644 --- a/libjsengine/JSEngine.h +++ b/libjsengine/JSEngine.h @@ -59,7 +59,7 @@ class JSEngine { public: // should be used to evalute javascript expression - virtual T eval(char const* _cstr) const = 0; + virtual T eval(char const* _cstr, char const* _origin = "(shell)") const = 0; }; } diff --git a/libjsengine/JSV8Engine.cpp b/libjsengine/JSV8Engine.cpp index 1a75a4620..471ea71e0 100644 --- a/libjsengine/JSV8Engine.cpp +++ b/libjsengine/JSV8Engine.cpp @@ -21,6 +21,8 @@ */ #include <memory> +#include <iostream> +#include <fstream> #include "JSV8Engine.h" #include "JSV8Printer.h" #include "libjsengine/JSEngineResources.hpp" @@ -103,6 +105,35 @@ Handle<Value> ConsoleLog(Arguments const& _args) return Undefined(); } +Handle<Value> LoadScript(Arguments const& _args) +{ + Local<External> wrap = Local<External>::Cast(_args.Data()); + auto engine = reinterpret_cast<JSV8Engine const*>(wrap->Value()); + + if (_args.Length() < 1) + return v8::ThrowException(v8::String::New("Missing file name.")); + if (_args[0].IsEmpty() || _args[0]->IsUndefined()) + return v8::ThrowException(v8::String::New("Invalid file name.")); + v8::String::Utf8Value fileName(_args[0]); + if (fileName.length() == 0) + return v8::ThrowException(v8::String::New("Invalid file name.")); + + std::ifstream is(*fileName, std::ifstream::binary); + if (!is) + return v8::ThrowException(v8::String::New("Error opening file.")); + + string contents; + is.seekg(0, is.end); + streamoff length = is.tellg(); + if (length > 0) + { + is.seekg(0, is.beg); + contents.resize(length); + is.read(const_cast<char*>(contents.data()), length); + } + + return engine->eval(contents.data(), *fileName).value(); +} class JSV8Scope { @@ -158,11 +189,14 @@ JSV8Engine::JSV8Engine(): m_scope(new JSV8Scope()) auto consoleTemplate = ObjectTemplate::New(); - Local<FunctionTemplate> function = FunctionTemplate::New(ConsoleLog, External::New(this)); - consoleTemplate->Set(String::New("debug"), function); - consoleTemplate->Set(String::New("log"), function); - consoleTemplate->Set(String::New("error"), function); + Local<FunctionTemplate> consoleLog = FunctionTemplate::New(ConsoleLog, External::New(this)); + consoleTemplate->Set(String::New("debug"), consoleLog); + consoleTemplate->Set(String::New("log"), consoleLog); + consoleTemplate->Set(String::New("error"), consoleLog); context()->Global()->Set(String::New("console"), consoleTemplate->NewInstance()); + + Local<FunctionTemplate> loadScript = FunctionTemplate::New(LoadScript, External::New(this)); + context()->Global()->Set(String::New("loadScript"), loadScript->GetFunction()); } JSV8Engine::~JSV8Engine() @@ -170,11 +204,11 @@ JSV8Engine::~JSV8Engine() delete m_scope; } -JSV8Value JSV8Engine::eval(char const* _cstr) const +JSV8Value JSV8Engine::eval(char const* _cstr, char const* _origin) const { TryCatch tryCatch; Local<String> source = String::New(_cstr); - Local<String> name(String::New("(shell)")); + Local<String> name(String::New(_origin)); ScriptOrigin origin(name); Handle<Script> script = Script::Compile(source, &origin); diff --git a/libjsengine/JSV8Engine.h b/libjsengine/JSV8Engine.h index 9d08d9b43..eb204c31e 100644 --- a/libjsengine/JSV8Engine.h +++ b/libjsengine/JSV8Engine.h @@ -54,7 +54,7 @@ class JSV8Engine: public JSEngine<JSV8Value> public: JSV8Engine(); virtual ~JSV8Engine(); - JSV8Value eval(char const* _cstr) const; + JSV8Value eval(char const* _cstr, char const* _origin = "(shell)") const; v8::Handle<v8::Context> const& context() const; private: From 1df63eda3295044fd6c92c79ef44ba1f0c96e1fd Mon Sep 17 00:00:00 2001 From: Dimitry Khokhlov <winsvega@mail.ru> Date: Thu, 25 Jun 2015 23:34:35 +0400 Subject: [PATCH 04/16] Coverage: script fix --- getcoverage.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/getcoverage.sh b/getcoverage.sh index 196629170..4249eecfe 100755 --- a/getcoverage.sh +++ b/getcoverage.sh @@ -39,9 +39,9 @@ if which lcov >/dev/null; then lcov --capture --initial --directory $BUILD_DIR --output-file $OUTPUT_DIR/coverage_base.info echo Running testeth... - $CPP_ETHEREUM_PATH/build/test/testeth $TEST_MODE - $CPP_ETHEREUM_PATH/build/test/testeth -t StateTests --jit $TEST_MODE - $CPP_ETHEREUM_PATH/build/test/testeth -t VMTests --jit $TEST_MODE + $BUILD_DIR/test/testeth $TEST_MODE + $BUILD_DIR/test/testeth -t StateTests --jit $TEST_MODE + $BUILD_DIR/test/testeth -t VMTests --jit $TEST_MODE echo Prepearing coverage info... lcov --capture --directory $BUILD_DIR --output-file $OUTPUT_DIR/coverage_test.info From 4494712371660f03ca84c327facd82ab5c06da25 Mon Sep 17 00:00:00 2001 From: Dimitry Khokhlov <winsvega@mail.ru> Date: Fri, 26 Jun 2015 13:58:19 +0400 Subject: [PATCH 05/16] Coverage: Filltests option --- getcoverage.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/getcoverage.sh b/getcoverage.sh index 4249eecfe..b11f8dced 100755 --- a/getcoverage.sh +++ b/getcoverage.sh @@ -17,6 +17,10 @@ case $i in TEST_MODE="--all" shift ;; + --filltests) + TEST_FILL="--filltests" + shift + ;; esac done @@ -39,7 +43,7 @@ if which lcov >/dev/null; then lcov --capture --initial --directory $BUILD_DIR --output-file $OUTPUT_DIR/coverage_base.info echo Running testeth... - $BUILD_DIR/test/testeth $TEST_MODE + $BUILD_DIR/test/testeth $TEST_MODE $TEST_FILL $BUILD_DIR/test/testeth -t StateTests --jit $TEST_MODE $BUILD_DIR/test/testeth -t VMTests --jit $TEST_MODE From 085311d8517e217ec9cba97d4a6e9b588c9071cd Mon Sep 17 00:00:00 2001 From: Dimitry Khokhlov <winsvega@mail.ru> Date: Fri, 26 Jun 2015 14:48:59 +0400 Subject: [PATCH 06/16] Coverage: libethereum::AccountDiff --- test/libethereum/unit.cpp | 70 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 test/libethereum/unit.cpp diff --git a/test/libethereum/unit.cpp b/test/libethereum/unit.cpp new file mode 100644 index 000000000..28a89df81 --- /dev/null +++ b/test/libethereum/unit.cpp @@ -0,0 +1,70 @@ +/* + 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 unit.cpp + * @author Dimitry Khokhlov <Dimitry@ethdev.com> + * @date 2015 + * libethereum unit test functions. + */ + +#include <boost/filesystem/operations.hpp> +#include <boost/test/unit_test.hpp> +#include <libethereum/Defaults.h> +#include <libethereum/AccountDiff.h> + +BOOST_AUTO_TEST_SUITE(libethereum) + +BOOST_AUTO_TEST_CASE(AccountDiff) +{ + std::cout << "AccountDiff" << std::endl; + dev::eth::AccountDiff accDiff; + + // exist = true exist_from = true AccountChange::Deletion + accDiff.exist = dev::Diff<bool>(true, false); + BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::Deletion, "Account change type expected to be Deletion!"); + + // exist = true exist_from = false AccountChange::Creation + accDiff.exist = dev::Diff<bool>(false, true); + BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::Creation, "Account change type expected to be Creation!"); + + // exist = false bn = true sc = true AccountChange::All + accDiff.exist = dev::Diff<bool>(false, false); + accDiff.nonce = dev::Diff<dev::u256>(1, 2); + accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("01")); + BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::All, "Account change type expected to be All!"); + + // exist = false bn = true sc = false AccountChange::Intrinsic + accDiff.exist = dev::Diff<bool>(false, false); + accDiff.nonce = dev::Diff<dev::u256>(1, 2); + accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("00")); + BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::Intrinsic, "Account change type expected to be Intrinsic!"); + + // exist = false bn = false sc = true AccountChange::CodeStorage + accDiff.exist = dev::Diff<bool>(false, false); + accDiff.nonce = dev::Diff<dev::u256>(1, 1); + accDiff.balance = dev::Diff<dev::u256>(1, 1); + accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("01")); + BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::CodeStorage, "Account change type expected to be CodeStorage!"); + + // exist = false bn = false sc = false AccountChange::None + accDiff.exist = dev::Diff<bool>(false, false); + accDiff.nonce = dev::Diff<dev::u256>(1, 1); + accDiff.balance = dev::Diff<dev::u256>(1, 1); + accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("00")); + BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::None, "Account change type expected to be None!"); +} + +BOOST_AUTO_TEST_SUITE_END() From c6074f051a8b3f4ee16ed1c56984ac28e3e71d59 Mon Sep 17 00:00:00 2001 From: Dimitry Khokhlov <winsvega@mail.ru> Date: Mon, 29 Jun 2015 17:15:13 +0400 Subject: [PATCH 07/16] CodeCoverage: libethereum --- test/libethereum/unit.cpp | 133 +++++++++++++++++++++++++++++++++++++- 1 file changed, 131 insertions(+), 2 deletions(-) diff --git a/test/libethereum/unit.cpp b/test/libethereum/unit.cpp index 28a89df81..2b78d764e 100644 --- a/test/libethereum/unit.cpp +++ b/test/libethereum/unit.cpp @@ -17,40 +17,54 @@ /** @file unit.cpp * @author Dimitry Khokhlov <Dimitry@ethdev.com> * @date 2015 - * libethereum unit test functions. + * libethereum unit test functions coverage. */ #include <boost/filesystem/operations.hpp> #include <boost/test/unit_test.hpp> +#include <test/TestHelper.h> +#include "../JsonSpiritHeaders.h" + +#include <libdevcore/TransientDirectory.h> + #include <libethereum/Defaults.h> #include <libethereum/AccountDiff.h> +#include <libethereum/BlockChain.h> +#include <libethereum/BlockQueue.h> + +using namespace dev; +using namespace eth; BOOST_AUTO_TEST_SUITE(libethereum) BOOST_AUTO_TEST_CASE(AccountDiff) { - std::cout << "AccountDiff" << std::endl; dev::eth::AccountDiff accDiff; + // Testing changeType // exist = true exist_from = true AccountChange::Deletion accDiff.exist = dev::Diff<bool>(true, false); BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::Deletion, "Account change type expected to be Deletion!"); + BOOST_CHECK_MESSAGE(strcmp(dev::eth::lead(accDiff.changeType()), "XXX") == 0, "Deletion lead expected to be 'XXX'!"); // exist = true exist_from = false AccountChange::Creation accDiff.exist = dev::Diff<bool>(false, true); BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::Creation, "Account change type expected to be Creation!"); + BOOST_CHECK_MESSAGE(strcmp(dev::eth::lead(accDiff.changeType()), "+++") == 0, "Creation lead expected to be '+++'!"); // exist = false bn = true sc = true AccountChange::All accDiff.exist = dev::Diff<bool>(false, false); accDiff.nonce = dev::Diff<dev::u256>(1, 2); accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("01")); BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::All, "Account change type expected to be All!"); + BOOST_CHECK_MESSAGE(strcmp(dev::eth::lead(accDiff.changeType()), "***") == 0, "All lead expected to be '***'!"); // exist = false bn = true sc = false AccountChange::Intrinsic accDiff.exist = dev::Diff<bool>(false, false); accDiff.nonce = dev::Diff<dev::u256>(1, 2); accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("00")); BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::Intrinsic, "Account change type expected to be Intrinsic!"); + BOOST_CHECK_MESSAGE(strcmp(dev::eth::lead(accDiff.changeType()), " * ") == 0, "Intrinsic lead expected to be ' * '!"); // exist = false bn = false sc = true AccountChange::CodeStorage accDiff.exist = dev::Diff<bool>(false, false); @@ -58,6 +72,7 @@ BOOST_AUTO_TEST_CASE(AccountDiff) accDiff.balance = dev::Diff<dev::u256>(1, 1); accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("01")); BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::CodeStorage, "Account change type expected to be CodeStorage!"); + BOOST_CHECK_MESSAGE(strcmp(dev::eth::lead(accDiff.changeType()), "* *") == 0, "CodeStorage lead expected to be '* *'!"); // exist = false bn = false sc = false AccountChange::None accDiff.exist = dev::Diff<bool>(false, false); @@ -65,6 +80,120 @@ BOOST_AUTO_TEST_CASE(AccountDiff) accDiff.balance = dev::Diff<dev::u256>(1, 1); accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("00")); BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::None, "Account change type expected to be None!"); + BOOST_CHECK_MESSAGE(strcmp(dev::eth::lead(accDiff.changeType()), " ") == 0, "None lead expected to be ' '!"); + + //ofstream + accDiff.exist = dev::Diff<bool>(false, false); + accDiff.nonce = dev::Diff<dev::u256>(1, 2); + accDiff.balance = dev::Diff<dev::u256>(1, 2); + accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("01")); + std::map<dev::u256, dev::Diff<dev::u256>> storage; + storage[1] = accDiff.nonce; + accDiff.storage = storage; + std::stringstream buffer; + + //if (!_s.exist.to()) + buffer << accDiff; + BOOST_CHECK_MESSAGE(strcmp(buffer.str().c_str(), "") == 0, "Not expected output: '" + buffer.str() + "'"); + buffer.str(std::string()); + + accDiff.exist = dev::Diff<bool>(false, true); + buffer << accDiff; + BOOST_CHECK_MESSAGE(strcmp(buffer.str().c_str(), "#2 (+1) 2 (+1) $[1] ([0]) \n * 0000000000000000000000000000000000000000000000000000000000000001: 2 (1)") == 0, "Not expected output: '" + buffer.str() + "'"); + buffer.str(std::string()); + + storage[1] = dev::Diff<dev::u256>(0, 0); + accDiff.storage = storage; + buffer << accDiff; + BOOST_CHECK_MESSAGE(strcmp(buffer.str().c_str(), "#2 (+1) 2 (+1) $[1] ([0]) \n + 0000000000000000000000000000000000000000000000000000000000000001: 0") == 0, "Not expected output: '" + buffer.str() + "'"); + buffer.str(std::string()); + + storage[1] = dev::Diff<dev::u256>(1, 0); + accDiff.storage = storage; + buffer << accDiff; + BOOST_CHECK_MESSAGE(strcmp(buffer.str().c_str(), "#2 (+1) 2 (+1) $[1] ([0]) \nXXX 0000000000000000000000000000000000000000000000000000000000000001 (1)") == 0, "Not expected output: '" + buffer.str() + "'"); + buffer.str(std::string()); + + BOOST_CHECK_MESSAGE(accDiff.changed() == true, "dev::eth::AccountDiff::changed(): incorrect return value"); + + //unexpected value + BOOST_CHECK_MESSAGE(strcmp(dev::eth::lead((dev::eth::AccountChange)123), "") != 0, "Not expected output when dev::eth::lead on unexpected value"); +} + +BOOST_AUTO_TEST_CASE(StateDiff) +{ + dev::eth::StateDiff stateDiff; + dev::eth::AccountDiff accDiff; + + accDiff.exist = dev::Diff<bool>(false, false); + accDiff.nonce = dev::Diff<dev::u256>(1, 2); + accDiff.balance = dev::Diff<dev::u256>(1, 2); + accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("01")); + std::map<dev::u256, dev::Diff<dev::u256>> storage; + storage[1] = accDiff.nonce; + accDiff.storage = storage; + std::stringstream buffer; + + dev::Address address("001122334455667788991011121314151617181920"); + stateDiff.accounts[address] = accDiff; + buffer << stateDiff; + + BOOST_CHECK_MESSAGE(strcmp(buffer.str().c_str(), "1 accounts changed:\n*** 0000000000000000000000000000000000000000: \n") == 0, "Not expected output: '" + buffer.str() + "'"); +} + +BOOST_AUTO_TEST_CASE(BlockChain) +{ + std::string genesisRLP = "0xf901fcf901f7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0cafd881ab193703b83816c49ff6c2bf6ba6f464a1be560c42106128c8dbc35e7a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8808454c98c8142a09eb47d85ccdf855f34cce66288b11d43a90d288dfc12eb7a900dcb7953a69a5a88448f2f62ce8e0392c0c0"; + dev::bytes genesisBlockRLP = dev::test::importByteArray(genesisRLP); + BlockInfo biGenesisBlock(genesisBlockRLP); + + std::string blockRLP = "0xf90260f901f9a01f08d9b73350445d921aa74a44861b5cc12d1258a4da8ac3e0a41d1757aa8112a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017a0dff021d89bbd62a468fc16a27430f8fbf0cc9e41473f26601fa9e4bebd0660d5a0bc37d79753ad738a6dac4921e57392f145d8887476de3f783dfa7edae9283e52b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd882520884558c98ed80a0f8e40e5173c7d73b2214e311c109fec63eb116b8ff90f78989e1a65082d6e74f8871f5f6541955ba68f861f85f800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d870a801ca0a1da1d695f9ff1595dcd950cd8f001bfb69e0ed6e731ad51353f52ed9c7117e3a0fe5f0ef2425069a91b2619d3147ab3a8386542be9b4c7ef738a0553d22361646c0"; + dev::bytes blockRLPbytes = dev::test::importByteArray(blockRLP); + BlockInfo biBlock(blockRLPbytes); + + TransientDirectory td1, td2; + State trueState(OverlayDB(State::openDB(td1.path())), BaseState::Empty, biGenesisBlock.coinbaseAddress); + dev::eth::BlockChain trueBc(genesisBlockRLP, td2.path(), WithExisting::Verify); + + json_spirit::mObject o; + json_spirit::mObject accountaddress; + json_spirit::mObject account; + account["balance"] = "0x02540be400"; + account["code"] = "0x"; + account["nonce"] = "0x00"; + account["storage"] = json_spirit::mObject(); + accountaddress["a94f5374fce5edbc8e2a8697c15331677e6ebf0b"] = account; + o["pre"] = accountaddress; + + dev::test::ImportTest importer(o["pre"].get_obj()); + importer.importState(o["pre"].get_obj(), trueState); + trueState.commit(); + + trueBc.import(blockRLPbytes, trueState.db()); + + std::stringstream buffer; + buffer << trueBc; + BOOST_CHECK_MESSAGE(strcmp(buffer.str().c_str(), "96d3fba448912a3f714221956ad5b6b7984236d4a1748c7f8ff5d637ca88e19f00: 1 @ 1f08d9b73350445d921aa74a44861b5cc12d1258a4da8ac3e0a41d1757aa8112\n") == 0, "Not expected output: '" + buffer.str() + "'"); + + trueBc.garbageCollect(true); + + //Block Queue Test + BlockQueue bcQueue; + bcQueue.tick(trueBc); + QueueStatus bStatus = bcQueue.blockStatus(trueBc.info().hash()); + BOOST_CHECK(bStatus == QueueStatus::Unknown); + + dev::bytesConstRef bytesConst(&blockRLPbytes.at(0), blockRLPbytes.size()); + bcQueue.import(bytesConst, trueBc); + bStatus = bcQueue.blockStatus(biBlock.hash()); + BOOST_CHECK(bStatus == QueueStatus::Unknown); + + bcQueue.clear(); +} + +BOOST_AUTO_TEST_CASE(BlockQueue) +{ + } BOOST_AUTO_TEST_SUITE_END() From 101b8cda92782e32dd0d25578cccd4fee4f83439 Mon Sep 17 00:00:00 2001 From: Dimitry <dimitry@ethdev.com> Date: Wed, 5 Aug 2015 12:43:42 +0300 Subject: [PATCH 08/16] Coverage: tests --- test/TestHelper.cpp | 59 ++++++----- test/TestHelper.h | 8 +- test/libethereum/AccountDiff.cpp | 144 ++++++++++++++++++++++++++ test/libethereum/transactionTests.cpp | 7 +- 4 files changed, 186 insertions(+), 32 deletions(-) create mode 100644 test/libethereum/AccountDiff.cpp diff --git a/test/TestHelper.cpp b/test/TestHelper.cpp index 399779756..fa889be16 100644 --- a/test/TestHelper.cpp +++ b/test/TestHelper.cpp @@ -191,7 +191,7 @@ void ImportTest::importState(json_spirit::mObject& _o, State& _state) BOOST_THROW_EXCEPTION(MissingFields() << errinfo_comment("Import State: Missing state fields!")); } -void ImportTest::importTransaction(json_spirit::mObject& _o) +void ImportTest::importTransaction (json_spirit::mObject const& _o, eth::Transaction& o_tr) { if (_o.count("secretKey") > 0) { @@ -202,18 +202,18 @@ void ImportTest::importTransaction(json_spirit::mObject& _o) assert(_o.count("value") > 0); assert(_o.count("data") > 0); - if (bigint(_o["nonce"].get_str()) >= c_max256plus1) + 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["gasPrice"].get_str()) >= c_max256plus1) + 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["gasLimit"].get_str()) >= c_max256plus1) + 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["value"].get_str()) >= c_max256plus1) + if (bigint(_o.at("value").get_str()) >= c_max256plus1) BOOST_THROW_EXCEPTION(ValueTooLarge() << errinfo_comment("Transaction 'value' is equal or greater than 2**256") ); - m_transaction = _o["to"].get_str().empty() ? - Transaction(toInt(_o["value"]), toInt(_o["gasPrice"]), toInt(_o["gasLimit"]), importData(_o), toInt(_o["nonce"]), Secret(_o["secretKey"].get_str())) : - Transaction(toInt(_o["value"]), toInt(_o["gasPrice"]), toInt(_o["gasLimit"]), Address(_o["to"].get_str()), importData(_o), toInt(_o["nonce"]), Secret(_o["secretKey"].get_str())); + 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 { @@ -221,14 +221,14 @@ void ImportTest::importTransaction(json_spirit::mObject& _o) RLP transactionRLP(transactionRLPStream.out()); try { - m_transaction = Transaction(transactionRLP.data(), CheckTransaction::Everything); + o_tr = Transaction(transactionRLP.data(), CheckTransaction::Everything); } catch (InvalidSignature) { // create unsigned transaction - m_transaction = _o["to"].get_str().empty() ? - Transaction(toInt(_o["value"]), toInt(_o["gasPrice"]), toInt(_o["gasLimit"]), importData(_o), toInt(_o["nonce"])) : - Transaction(toInt(_o["value"]), toInt(_o["gasPrice"]), toInt(_o["gasLimit"]), Address(_o["to"].get_str()), importData(_o), toInt(_o["nonce"])); + 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) { @@ -237,6 +237,11 @@ void ImportTest::importTransaction(json_spirit::mObject& _o) } } +void ImportTest::importTransaction(json_spirit::mObject const& o_tr) +{ + importTransaction(o_tr, m_transaction); +} + void ImportTest::compareStates(State const& _stateExpect, State const& _statePost, AccountMaskMap const _expectedStateOptions, WhenError _throw) { #define CHECK(a,b) \ @@ -421,13 +426,13 @@ bytes importByteArray(std::string const& _str) return fromHex(_str.substr(0, 2) == "0x" ? _str.substr(2) : _str, WhenError::Throw); } -bytes importData(json_spirit::mObject& _o) +bytes importData(json_spirit::mObject const& _o) { bytes data; - if (_o["data"].type() == json_spirit::str_type) - data = importByteArray(_o["data"].get_str()); + if (_o.at("data").type() == json_spirit::str_type) + data = importByteArray(_o.at("data").get_str()); else - for (auto const& j: _o["data"].get_array()) + for (auto const& j: _o.at("data").get_array()) data.push_back(toByte(j)); return data; } @@ -633,46 +638,46 @@ void executeTests(const string& _name, const string& _testPathAppendix, const bo } } -RLPStream createRLPStreamFromTransactionFields(json_spirit::mObject& _tObj) +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["nonce"].get_str()); + rlpStream << bigint(_tObj.at("nonce").get_str()); if (_tObj.count("gasPrice")) - rlpStream << bigint(_tObj["gasPrice"].get_str()); + rlpStream << bigint(_tObj.at("gasPrice").get_str()); if (_tObj.count("gasLimit")) - rlpStream << bigint(_tObj["gasLimit"].get_str()); + rlpStream << bigint(_tObj.at("gasLimit").get_str()); if (_tObj.count("to")) { - if (_tObj["to"].get_str().empty()) + if (_tObj.at("to").get_str().empty()) rlpStream << ""; else - rlpStream << importByteArray(_tObj["to"].get_str()); + rlpStream << importByteArray(_tObj.at("to").get_str()); } if (_tObj.count("value")) - rlpStream << bigint(_tObj["value"].get_str()); + rlpStream << bigint(_tObj.at("value").get_str()); if (_tObj.count("data")) rlpStream << importData(_tObj); if (_tObj.count("v")) - rlpStream << bigint(_tObj["v"].get_str()); + rlpStream << bigint(_tObj.at("v").get_str()); if (_tObj.count("r")) - rlpStream << bigint(_tObj["r"].get_str()); + rlpStream << bigint(_tObj.at("r").get_str()); if (_tObj.count("s")) - rlpStream << bigint(_tObj["s"].get_str()); + rlpStream << bigint(_tObj.at("s").get_str()); if (_tObj.count("extrafield")) - rlpStream << bigint(_tObj["extrafield"].get_str()); + rlpStream << bigint(_tObj.at("extrafield").get_str()); return rlpStream; } diff --git a/test/TestHelper.h b/test/TestHelper.h index 9d2625e19..5f2f8d6d5 100644 --- a/test/TestHelper.h +++ b/test/TestHelper.h @@ -32,7 +32,6 @@ #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(); @@ -139,7 +138,8 @@ public: 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); - void importTransaction(json_spirit::mObject& _o); + 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(); @@ -168,7 +168,7 @@ protected: 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& _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); @@ -193,7 +193,7 @@ dev::eth::Ethash::BlockHeader constructHeader( 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& _tObj); +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); diff --git a/test/libethereum/AccountDiff.cpp b/test/libethereum/AccountDiff.cpp new file mode 100644 index 000000000..3b009df6c --- /dev/null +++ b/test/libethereum/AccountDiff.cpp @@ -0,0 +1,144 @@ +/* + 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 AccountDiff.cpp + * @author Dimitry Khokhlov <Dimitry@ethdev.com> + * @date 2015 + * libethereum unit test functions coverage. + */ + +#include <boost/filesystem/operations.hpp> +#include <boost/test/unit_test.hpp> +#include <test/TestHelper.h> +#include "../JsonSpiritHeaders.h" + +#include <libdevcore/TransientDirectory.h> + +#include <libethereum/Defaults.h> +#include <libethereum/AccountDiff.h> +#include <libethereum/BlockChain.h> +#include <libethereum/BlockQueue.h> + +using namespace dev; +using namespace eth; + +BOOST_AUTO_TEST_SUITE(libethereum) + +BOOST_AUTO_TEST_CASE(AccountDiff) +{ + dev::eth::AccountDiff accDiff; + + // Testing changeType + // exist = true exist_from = true AccountChange::Deletion + accDiff.exist = dev::Diff<bool>(true, false); + BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::Deletion, "Account change type expected to be Deletion!"); + BOOST_CHECK_MESSAGE(strcmp(dev::eth::lead(accDiff.changeType()), "XXX") == 0, "Deletion lead expected to be 'XXX'!"); + + // exist = true exist_from = false AccountChange::Creation + accDiff.exist = dev::Diff<bool>(false, true); + BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::Creation, "Account change type expected to be Creation!"); + BOOST_CHECK_MESSAGE(strcmp(dev::eth::lead(accDiff.changeType()), "+++") == 0, "Creation lead expected to be '+++'!"); + + // exist = false bn = true sc = true AccountChange::All + accDiff.exist = dev::Diff<bool>(false, false); + accDiff.nonce = dev::Diff<dev::u256>(1, 2); + accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("01")); + BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::All, "Account change type expected to be All!"); + BOOST_CHECK_MESSAGE(strcmp(dev::eth::lead(accDiff.changeType()), "***") == 0, "All lead expected to be '***'!"); + + // exist = false bn = true sc = false AccountChange::Intrinsic + accDiff.exist = dev::Diff<bool>(false, false); + accDiff.nonce = dev::Diff<dev::u256>(1, 2); + accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("00")); + BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::Intrinsic, "Account change type expected to be Intrinsic!"); + BOOST_CHECK_MESSAGE(strcmp(dev::eth::lead(accDiff.changeType()), " * ") == 0, "Intrinsic lead expected to be ' * '!"); + + // exist = false bn = false sc = true AccountChange::CodeStorage + accDiff.exist = dev::Diff<bool>(false, false); + accDiff.nonce = dev::Diff<dev::u256>(1, 1); + accDiff.balance = dev::Diff<dev::u256>(1, 1); + accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("01")); + BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::CodeStorage, "Account change type expected to be CodeStorage!"); + BOOST_CHECK_MESSAGE(strcmp(dev::eth::lead(accDiff.changeType()), "* *") == 0, "CodeStorage lead expected to be '* *'!"); + + // exist = false bn = false sc = false AccountChange::None + accDiff.exist = dev::Diff<bool>(false, false); + accDiff.nonce = dev::Diff<dev::u256>(1, 1); + accDiff.balance = dev::Diff<dev::u256>(1, 1); + accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("00")); + BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::None, "Account change type expected to be None!"); + BOOST_CHECK_MESSAGE(strcmp(dev::eth::lead(accDiff.changeType()), " ") == 0, "None lead expected to be ' '!"); + + //ofstream + accDiff.exist = dev::Diff<bool>(false, false); + accDiff.nonce = dev::Diff<dev::u256>(1, 2); + accDiff.balance = dev::Diff<dev::u256>(1, 2); + accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("01")); + std::map<dev::u256, dev::Diff<dev::u256>> storage; + storage[1] = accDiff.nonce; + accDiff.storage = storage; + std::stringstream buffer; + + //if (!_s.exist.to()) + buffer << accDiff; + BOOST_CHECK_MESSAGE(buffer.str() == "", "Not expected output: '" + buffer.str() + "'"); + buffer.str(std::string()); + + accDiff.exist = dev::Diff<bool>(false, true); + buffer << accDiff; + BOOST_CHECK_MESSAGE(buffer.str() == "#2 (+1) 2 (+1) $[1] ([0]) \n * 0000000000000000000000000000000000000000000000000000000000000001: 2 (1)", "Not expected output: '" + buffer.str() + "'"); + buffer.str(std::string()); + + storage[1] = dev::Diff<dev::u256>(0, 0); + accDiff.storage = storage; + buffer << accDiff; + BOOST_CHECK_MESSAGE(buffer.str() == "#2 (+1) 2 (+1) $[1] ([0]) \n + 0000000000000000000000000000000000000000000000000000000000000001: 0", "Not expected output: '" + buffer.str() + "'"); + buffer.str(std::string()); + + storage[1] = dev::Diff<dev::u256>(1, 0); + accDiff.storage = storage; + buffer << accDiff; + BOOST_CHECK_MESSAGE(buffer.str() == "#2 (+1) 2 (+1) $[1] ([0]) \nXXX 0000000000000000000000000000000000000000000000000000000000000001 (1)", "Not expected output: '" + buffer.str() + "'"); + buffer.str(std::string()); + + BOOST_CHECK_MESSAGE(accDiff.changed() == true, "dev::eth::AccountDiff::changed(): incorrect return value"); + + //unexpected value + //BOOST_CHECK_MESSAGE(strcmp(dev::eth::lead((dev::eth::AccountChange)123), "") != 0, "Not expected output when dev::eth::lead on unexpected value"); +} + +BOOST_AUTO_TEST_CASE(StateDiff) +{ + dev::eth::StateDiff stateDiff; + dev::eth::AccountDiff accDiff; + + accDiff.exist = dev::Diff<bool>(false, false); + accDiff.nonce = dev::Diff<dev::u256>(1, 2); + accDiff.balance = dev::Diff<dev::u256>(1, 2); + accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("01")); + std::map<dev::u256, dev::Diff<dev::u256>> storage; + storage[1] = accDiff.nonce; + accDiff.storage = storage; + std::stringstream buffer; + + dev::Address address("001122334455667788991011121314151617181920"); + stateDiff.accounts[address] = accDiff; + buffer << stateDiff; + + BOOST_CHECK_MESSAGE(buffer.str() == "1 accounts changed:\n*** 0000000000000000000000000000000000000000: \n", "Not expected output: '" + buffer.str() + "'"); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/test/libethereum/transactionTests.cpp b/test/libethereum/transactionTests.cpp index fe620eac6..f6bde061a 100644 --- a/test/libethereum/transactionTests.cpp +++ b/test/libethereum/transactionTests.cpp @@ -34,9 +34,14 @@ void doTransactionTests(json_spirit::mValue& _v, bool _fillin) { for (auto& i: _v.get_obj()) { - cerr << i.first << endl; mObject& o = i.second.get_obj(); + if (test::Options::get().singleTest && test::Options::get().singleTestName != i.first) + { + o.clear(); + continue; + } + cerr << i.first << endl; if (_fillin) { TBOOST_REQUIRE((o.count("transaction") > 0)); From 383b50c7c077913db1b3a65d076c105198d45866 Mon Sep 17 00:00:00 2001 From: Dimitry <dimitry@ethdev.com> Date: Wed, 5 Aug 2015 13:15:44 +0300 Subject: [PATCH 09/16] Cover: remove unit.cpp --- test/libethereum/unit.cpp | 199 -------------------------------------- 1 file changed, 199 deletions(-) delete mode 100644 test/libethereum/unit.cpp diff --git a/test/libethereum/unit.cpp b/test/libethereum/unit.cpp deleted file mode 100644 index 2b78d764e..000000000 --- a/test/libethereum/unit.cpp +++ /dev/null @@ -1,199 +0,0 @@ -/* - 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 unit.cpp - * @author Dimitry Khokhlov <Dimitry@ethdev.com> - * @date 2015 - * libethereum unit test functions coverage. - */ - -#include <boost/filesystem/operations.hpp> -#include <boost/test/unit_test.hpp> -#include <test/TestHelper.h> -#include "../JsonSpiritHeaders.h" - -#include <libdevcore/TransientDirectory.h> - -#include <libethereum/Defaults.h> -#include <libethereum/AccountDiff.h> -#include <libethereum/BlockChain.h> -#include <libethereum/BlockQueue.h> - -using namespace dev; -using namespace eth; - -BOOST_AUTO_TEST_SUITE(libethereum) - -BOOST_AUTO_TEST_CASE(AccountDiff) -{ - dev::eth::AccountDiff accDiff; - - // Testing changeType - // exist = true exist_from = true AccountChange::Deletion - accDiff.exist = dev::Diff<bool>(true, false); - BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::Deletion, "Account change type expected to be Deletion!"); - BOOST_CHECK_MESSAGE(strcmp(dev::eth::lead(accDiff.changeType()), "XXX") == 0, "Deletion lead expected to be 'XXX'!"); - - // exist = true exist_from = false AccountChange::Creation - accDiff.exist = dev::Diff<bool>(false, true); - BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::Creation, "Account change type expected to be Creation!"); - BOOST_CHECK_MESSAGE(strcmp(dev::eth::lead(accDiff.changeType()), "+++") == 0, "Creation lead expected to be '+++'!"); - - // exist = false bn = true sc = true AccountChange::All - accDiff.exist = dev::Diff<bool>(false, false); - accDiff.nonce = dev::Diff<dev::u256>(1, 2); - accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("01")); - BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::All, "Account change type expected to be All!"); - BOOST_CHECK_MESSAGE(strcmp(dev::eth::lead(accDiff.changeType()), "***") == 0, "All lead expected to be '***'!"); - - // exist = false bn = true sc = false AccountChange::Intrinsic - accDiff.exist = dev::Diff<bool>(false, false); - accDiff.nonce = dev::Diff<dev::u256>(1, 2); - accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("00")); - BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::Intrinsic, "Account change type expected to be Intrinsic!"); - BOOST_CHECK_MESSAGE(strcmp(dev::eth::lead(accDiff.changeType()), " * ") == 0, "Intrinsic lead expected to be ' * '!"); - - // exist = false bn = false sc = true AccountChange::CodeStorage - accDiff.exist = dev::Diff<bool>(false, false); - accDiff.nonce = dev::Diff<dev::u256>(1, 1); - accDiff.balance = dev::Diff<dev::u256>(1, 1); - accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("01")); - BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::CodeStorage, "Account change type expected to be CodeStorage!"); - BOOST_CHECK_MESSAGE(strcmp(dev::eth::lead(accDiff.changeType()), "* *") == 0, "CodeStorage lead expected to be '* *'!"); - - // exist = false bn = false sc = false AccountChange::None - accDiff.exist = dev::Diff<bool>(false, false); - accDiff.nonce = dev::Diff<dev::u256>(1, 1); - accDiff.balance = dev::Diff<dev::u256>(1, 1); - accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("00")); - BOOST_CHECK_MESSAGE(accDiff.changeType() == dev::eth::AccountChange::None, "Account change type expected to be None!"); - BOOST_CHECK_MESSAGE(strcmp(dev::eth::lead(accDiff.changeType()), " ") == 0, "None lead expected to be ' '!"); - - //ofstream - accDiff.exist = dev::Diff<bool>(false, false); - accDiff.nonce = dev::Diff<dev::u256>(1, 2); - accDiff.balance = dev::Diff<dev::u256>(1, 2); - accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("01")); - std::map<dev::u256, dev::Diff<dev::u256>> storage; - storage[1] = accDiff.nonce; - accDiff.storage = storage; - std::stringstream buffer; - - //if (!_s.exist.to()) - buffer << accDiff; - BOOST_CHECK_MESSAGE(strcmp(buffer.str().c_str(), "") == 0, "Not expected output: '" + buffer.str() + "'"); - buffer.str(std::string()); - - accDiff.exist = dev::Diff<bool>(false, true); - buffer << accDiff; - BOOST_CHECK_MESSAGE(strcmp(buffer.str().c_str(), "#2 (+1) 2 (+1) $[1] ([0]) \n * 0000000000000000000000000000000000000000000000000000000000000001: 2 (1)") == 0, "Not expected output: '" + buffer.str() + "'"); - buffer.str(std::string()); - - storage[1] = dev::Diff<dev::u256>(0, 0); - accDiff.storage = storage; - buffer << accDiff; - BOOST_CHECK_MESSAGE(strcmp(buffer.str().c_str(), "#2 (+1) 2 (+1) $[1] ([0]) \n + 0000000000000000000000000000000000000000000000000000000000000001: 0") == 0, "Not expected output: '" + buffer.str() + "'"); - buffer.str(std::string()); - - storage[1] = dev::Diff<dev::u256>(1, 0); - accDiff.storage = storage; - buffer << accDiff; - BOOST_CHECK_MESSAGE(strcmp(buffer.str().c_str(), "#2 (+1) 2 (+1) $[1] ([0]) \nXXX 0000000000000000000000000000000000000000000000000000000000000001 (1)") == 0, "Not expected output: '" + buffer.str() + "'"); - buffer.str(std::string()); - - BOOST_CHECK_MESSAGE(accDiff.changed() == true, "dev::eth::AccountDiff::changed(): incorrect return value"); - - //unexpected value - BOOST_CHECK_MESSAGE(strcmp(dev::eth::lead((dev::eth::AccountChange)123), "") != 0, "Not expected output when dev::eth::lead on unexpected value"); -} - -BOOST_AUTO_TEST_CASE(StateDiff) -{ - dev::eth::StateDiff stateDiff; - dev::eth::AccountDiff accDiff; - - accDiff.exist = dev::Diff<bool>(false, false); - accDiff.nonce = dev::Diff<dev::u256>(1, 2); - accDiff.balance = dev::Diff<dev::u256>(1, 2); - accDiff.code = dev::Diff<dev::bytes>(dev::fromHex("00"), dev::fromHex("01")); - std::map<dev::u256, dev::Diff<dev::u256>> storage; - storage[1] = accDiff.nonce; - accDiff.storage = storage; - std::stringstream buffer; - - dev::Address address("001122334455667788991011121314151617181920"); - stateDiff.accounts[address] = accDiff; - buffer << stateDiff; - - BOOST_CHECK_MESSAGE(strcmp(buffer.str().c_str(), "1 accounts changed:\n*** 0000000000000000000000000000000000000000: \n") == 0, "Not expected output: '" + buffer.str() + "'"); -} - -BOOST_AUTO_TEST_CASE(BlockChain) -{ - std::string genesisRLP = "0xf901fcf901f7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0cafd881ab193703b83816c49ff6c2bf6ba6f464a1be560c42106128c8dbc35e7a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8808454c98c8142a09eb47d85ccdf855f34cce66288b11d43a90d288dfc12eb7a900dcb7953a69a5a88448f2f62ce8e0392c0c0"; - dev::bytes genesisBlockRLP = dev::test::importByteArray(genesisRLP); - BlockInfo biGenesisBlock(genesisBlockRLP); - - std::string blockRLP = "0xf90260f901f9a01f08d9b73350445d921aa74a44861b5cc12d1258a4da8ac3e0a41d1757aa8112a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017a0dff021d89bbd62a468fc16a27430f8fbf0cc9e41473f26601fa9e4bebd0660d5a0bc37d79753ad738a6dac4921e57392f145d8887476de3f783dfa7edae9283e52b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd882520884558c98ed80a0f8e40e5173c7d73b2214e311c109fec63eb116b8ff90f78989e1a65082d6e74f8871f5f6541955ba68f861f85f800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d870a801ca0a1da1d695f9ff1595dcd950cd8f001bfb69e0ed6e731ad51353f52ed9c7117e3a0fe5f0ef2425069a91b2619d3147ab3a8386542be9b4c7ef738a0553d22361646c0"; - dev::bytes blockRLPbytes = dev::test::importByteArray(blockRLP); - BlockInfo biBlock(blockRLPbytes); - - TransientDirectory td1, td2; - State trueState(OverlayDB(State::openDB(td1.path())), BaseState::Empty, biGenesisBlock.coinbaseAddress); - dev::eth::BlockChain trueBc(genesisBlockRLP, td2.path(), WithExisting::Verify); - - json_spirit::mObject o; - json_spirit::mObject accountaddress; - json_spirit::mObject account; - account["balance"] = "0x02540be400"; - account["code"] = "0x"; - account["nonce"] = "0x00"; - account["storage"] = json_spirit::mObject(); - accountaddress["a94f5374fce5edbc8e2a8697c15331677e6ebf0b"] = account; - o["pre"] = accountaddress; - - dev::test::ImportTest importer(o["pre"].get_obj()); - importer.importState(o["pre"].get_obj(), trueState); - trueState.commit(); - - trueBc.import(blockRLPbytes, trueState.db()); - - std::stringstream buffer; - buffer << trueBc; - BOOST_CHECK_MESSAGE(strcmp(buffer.str().c_str(), "96d3fba448912a3f714221956ad5b6b7984236d4a1748c7f8ff5d637ca88e19f00: 1 @ 1f08d9b73350445d921aa74a44861b5cc12d1258a4da8ac3e0a41d1757aa8112\n") == 0, "Not expected output: '" + buffer.str() + "'"); - - trueBc.garbageCollect(true); - - //Block Queue Test - BlockQueue bcQueue; - bcQueue.tick(trueBc); - QueueStatus bStatus = bcQueue.blockStatus(trueBc.info().hash()); - BOOST_CHECK(bStatus == QueueStatus::Unknown); - - dev::bytesConstRef bytesConst(&blockRLPbytes.at(0), blockRLPbytes.size()); - bcQueue.import(bytesConst, trueBc); - bStatus = bcQueue.blockStatus(biBlock.hash()); - BOOST_CHECK(bStatus == QueueStatus::Unknown); - - bcQueue.clear(); -} - -BOOST_AUTO_TEST_CASE(BlockQueue) -{ - -} - -BOOST_AUTO_TEST_SUITE_END() From 231466858148ee710e433d26f94610842708af94 Mon Sep 17 00:00:00 2001 From: Vlad Gluhovsky <gluk256@gmail.com> Date: Tue, 11 Aug 2015 13:08:46 +0200 Subject: [PATCH 10/16] style update --- libp2p/NodeTable.cpp | 9 +++++---- libp2p/NodeTable.h | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/libp2p/NodeTable.cpp b/libp2p/NodeTable.cpp index 94803f3f0..c550a2343 100644 --- a/libp2p/NodeTable.cpp +++ b/libp2p/NodeTable.cpp @@ -201,12 +201,13 @@ void NodeTable::doDiscover(NodeId _node, unsigned _round, shared_ptr<set<shared_ if (_ec) clog(NodeTableWarn) << "Discovery timer canceled: " << _ec.value() << _ec.message(); - if (995 == _ec.value() || m_timers.isStopped()) + if (_ec.value() == boost::asio::error::operation_aborted || m_timers.isStopped()) return; - // Error code 995 means that the timer was probably aborted. It usually happens when "this" object - // is deallocated, in which case subsequent call to doDiscover() would cause a crash. - // We can not rely on m_timers.isStopped(), because "this" pointer was captured by the lambda, + // error::operation_aborted means that the timer was probably aborted. + // It usually happens when "this" object is deallocated, in which case + // subsequent call to doDiscover() would cause a crash. We can not rely on + // m_timers.isStopped(), because "this" pointer was captured by the lambda, // and therefore, in case of deallocation m_timers object no longer exists. doDiscover(_node, _round + 1, _tried); diff --git a/libp2p/NodeTable.h b/libp2p/NodeTable.h index 4d6b02d99..66e27084b 100644 --- a/libp2p/NodeTable.h +++ b/libp2p/NodeTable.h @@ -264,7 +264,7 @@ private: std::shared_ptr<NodeSocket> m_socket; ///< Shared pointer for our UDPSocket; ASIO requires shared_ptr. NodeSocket* m_socketPointer; ///< Set to m_socket.get(). Socket is created in constructor and disconnected in destructor to ensure access to pointer is safe. - DeadlineOps m_timers; + DeadlineOps m_timers; ///< this should be the last member - it must be destroyed first }; inline std::ostream& operator<<(std::ostream& _out, NodeTable const& _nodeTable) From 98a270a899f7f9093013e850dd8b86f9ca79d57c Mon Sep 17 00:00:00 2001 From: chriseth <c@ethdev.com> Date: Tue, 11 Aug 2015 13:37:54 +0200 Subject: [PATCH 11/16] Lowercased functions. --- libjsengine/JSV8Engine.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/libjsengine/JSV8Engine.cpp b/libjsengine/JSV8Engine.cpp index 471ea71e0..6d57cad48 100644 --- a/libjsengine/JSV8Engine.cpp +++ b/libjsengine/JSV8Engine.cpp @@ -95,7 +95,7 @@ void reportException(TryCatch* _tryCatch) } } -Handle<Value> ConsoleLog(Arguments const& _args) +Handle<Value> consoleLog(Arguments const& _args) { Local<External> wrap = Local<External>::Cast(_args.Data()); auto engine = reinterpret_cast<JSV8Engine const*>(wrap->Value()); @@ -105,7 +105,7 @@ Handle<Value> ConsoleLog(Arguments const& _args) return Undefined(); } -Handle<Value> LoadScript(Arguments const& _args) +Handle<Value> loadScript(Arguments const& _args) { Local<External> wrap = Local<External>::Cast(_args.Data()); auto engine = reinterpret_cast<JSV8Engine const*>(wrap->Value()); @@ -189,14 +189,14 @@ JSV8Engine::JSV8Engine(): m_scope(new JSV8Scope()) auto consoleTemplate = ObjectTemplate::New(); - Local<FunctionTemplate> consoleLog = FunctionTemplate::New(ConsoleLog, External::New(this)); - consoleTemplate->Set(String::New("debug"), consoleLog); - consoleTemplate->Set(String::New("log"), consoleLog); - consoleTemplate->Set(String::New("error"), consoleLog); + Local<FunctionTemplate> consoleLogFunction = FunctionTemplate::New(consoleLog, External::New(this)); + consoleTemplate->Set(String::New("debug"), consoleLogFunction); + consoleTemplate->Set(String::New("log"), consoleLogFunction); + consoleTemplate->Set(String::New("error"), consoleLogFunction); context()->Global()->Set(String::New("console"), consoleTemplate->NewInstance()); - Local<FunctionTemplate> loadScript = FunctionTemplate::New(LoadScript, External::New(this)); - context()->Global()->Set(String::New("loadScript"), loadScript->GetFunction()); + Local<FunctionTemplate> loadScriptFunction = FunctionTemplate::New(loadScript, External::New(this)); + context()->Global()->Set(String::New("loadScript"), loadScriptFunction->GetFunction()); } JSV8Engine::~JSV8Engine() From b78ee4c18cd0b785ab6f22a2f36d0e36229f3bcd Mon Sep 17 00:00:00 2001 From: chriseth <c@ethdev.com> Date: Mon, 10 Aug 2015 16:56:14 +0200 Subject: [PATCH 12/16] Reduce usage of "new". --- libethereum/BlockChainSync.h | 5 +++-- libethereum/Client.cpp | 4 ++-- libethereum/Executive.cpp | 2 +- libp2p/Host.cpp | 19 ++++++++++++------- libp2p/Host.h | 2 +- libp2p/HostCapability.h | 5 +++-- libp2p/Network.cpp | 7 ++----- libp2p/NodeTable.cpp | 8 ++++---- libp2p/RLPxHandshake.h | 2 +- libp2p/UPnP.cpp | 4 ++-- libp2p/UPnP.h | 4 ++-- libwebthree/WebThree.cpp | 2 +- test/libp2p/capability.cpp | 4 ++-- test/libwhisper/shhrpc.cpp | 8 ++++---- test/libwhisper/whisperDB.cpp | 12 ++++++------ test/libwhisper/whisperTopic.cpp | 22 +++++++++++----------- 16 files changed, 57 insertions(+), 53 deletions(-) diff --git a/libethereum/BlockChainSync.h b/libethereum/BlockChainSync.h index 4e328d5ba..dc8971a1b 100644 --- a/libethereum/BlockChainSync.h +++ b/libethereum/BlockChainSync.h @@ -113,6 +113,9 @@ protected: /// Request blocks from peer if needed void requestBlocks(std::shared_ptr<EthereumPeer> _peer); +private: + EthereumHost& m_host; + protected: Handler<> m_bqRoomAvailable; ///< Triggered once block queue mutable RecursiveMutex x_sync; @@ -124,8 +127,6 @@ private: static char const* const s_stateNames[static_cast<int>(SyncState::Size)]; bool invariants() const override = 0; void logNewBlock(h256 const& _h); - - EthereumHost& m_host; }; diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index aed6f60d6..2631a28cb 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -116,9 +116,9 @@ void Client::init(p2p::Host* _extNet, std::string const& _dbPath, WithExisting _ m_gp->update(bc()); - auto host = _extNet->registerCapability(new EthereumHost(bc(), m_tq, m_bq, _networkId)); + auto host = _extNet->registerCapability(make_shared<EthereumHost>(bc(), m_tq, m_bq, _networkId)); m_host = host; - _extNet->addCapability(host, EthereumHost::staticName(), EthereumHost::c_oldProtocolVersion); //TODO: remove this one v61+ protocol is common + _extNet->addCapability(host, EthereumHost::staticName(), EthereumHost::c_oldProtocolVersion); //TODO: remove this once v61+ protocol is common if (_dbPath.size()) Defaults::setDBPath(_dbPath); diff --git a/libethereum/Executive.cpp b/libethereum/Executive.cpp index 7eb37ff99..64b29a8ba 100644 --- a/libethereum/Executive.cpp +++ b/libethereum/Executive.cpp @@ -39,7 +39,7 @@ const char* VMTraceChannel::name() { return "EVM"; } const char* ExecutiveWarnChannel::name() { return WarnChannel::name(); } StandardTrace::StandardTrace(): - m_trace(new Json::Value(Json::arrayValue)) + m_trace(make_shared<Json::Value>(Json::arrayValue)) {} bool changesMemory(Instruction _inst) diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 64d10b172..69437c392 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -237,9 +237,9 @@ void Host::startPeerSession(Public const& _id, RLP const& _rlp, RLPXFrameCoder* { // peer doesn't exist, try to get port info from node table if (Node n = m_nodeTable->node(_id)) - p.reset(new Peer(n)); + p = make_shared<Peer>(n); else - p.reset(new Peer(Node(_id, UnspecifiedNodeIPEndpoint))); + p = make_shared<Peer>(Node(_id, UnspecifiedNodeIPEndpoint)); m_peers[_id] = p; } } @@ -294,7 +294,7 @@ void Host::startPeerSession(Public const& _id, RLP const& _rlp, RLPXFrameCoder* for (auto const& i: caps) if (haveCapability(i)) { - ps->m_capabilities[i] = shared_ptr<Capability>(m_capabilities[i]->newPeerCapability(ps, o, i)); + ps->m_capabilities[i] = m_capabilities[i]->newPeerCapability(ps, o, i); o += m_capabilities[i]->messageCount(); } ps->start(); @@ -323,7 +323,7 @@ void Host::onNodeTableEvent(NodeId const& _n, NodeTableEventType const& _e) } else { - p.reset(new Peer(n)); + p = make_shared<Peer>(n); m_peers[_n] = p; clog(NetP2PNote) << "p2p.host.peers.events.peerAdded " << _n << p->endpoint; } @@ -491,14 +491,14 @@ void Host::requirePeer(NodeId const& _n, NodeIPEndpoint const& _endpoint) } else { - p.reset(new Peer(node)); + p = make_shared<Peer>(node); m_peers[_n] = p; } } else if (m_nodeTable) { m_nodeTable->addNode(node); - shared_ptr<boost::asio::deadline_timer> t(new boost::asio::deadline_timer(m_ioService)); + auto t = make_shared<boost::asio::deadline_timer>(m_ioService); t->expires_from_now(boost::posix_time::milliseconds(600)); t->async_wait([this, _n](boost::system::error_code const& _ec) { @@ -712,7 +712,12 @@ void Host::startedWorking() else clog(NetP2PNote) << "p2p.start.notice id:" << id() << "TCP Listen port is invalid or unavailable."; - shared_ptr<NodeTable> nodeTable(new NodeTable(m_ioService, m_alias, NodeIPEndpoint(bi::address::from_string(listenAddress()), listenPort(), listenPort()), m_netPrefs.discovery)); + auto nodeTable = make_shared<NodeTable>( + m_ioService, + m_alias, + NodeIPEndpoint(bi::address::from_string(listenAddress()), listenPort(), listenPort()), + m_netPrefs.discovery + ); nodeTable->setEventHandler(new HostNodeTableHandler(*this)); m_nodeTable = nodeTable; restoreNetwork(&m_restoreNetwork); diff --git a/libp2p/Host.h b/libp2p/Host.h index 863149899..2ba39e095 100644 --- a/libp2p/Host.h +++ b/libp2p/Host.h @@ -161,7 +161,7 @@ public: static std::unordered_map<Public, std::string> const& pocHosts(); /// Register a peer-capability; all new peer connections will have this capability. - template <class T> std::shared_ptr<T> registerCapability(T* _t) { _t->m_host = this; std::shared_ptr<T> ret(_t); m_capabilities[std::make_pair(T::staticName(), T::staticVersion())] = ret; return ret; } + template <class T> std::shared_ptr<T> registerCapability(std::shared_ptr<T> const& _t) { _t->m_host = this; m_capabilities[std::make_pair(T::staticName(), T::staticVersion())] = _t; return _t; } template <class T> void addCapability(std::shared_ptr<T> const & _p, std::string const& _name, u256 const& _version) { m_capabilities[std::make_pair(_name, _version)] = _p; } bool haveCapability(CapDesc const& _name) const { return m_capabilities.count(_name) != 0; } diff --git a/libp2p/HostCapability.h b/libp2p/HostCapability.h index 7032e31cb..61f046ed3 100644 --- a/libp2p/HostCapability.h +++ b/libp2p/HostCapability.h @@ -23,6 +23,7 @@ #pragma once +#include <memory> #include "Peer.h" #include "Common.h" @@ -53,7 +54,7 @@ protected: virtual u256 version() const = 0; CapDesc capDesc() const { return std::make_pair(name(), version()); } virtual unsigned messageCount() const = 0; - virtual Capability* newPeerCapability(std::shared_ptr<Session> _s, unsigned _idOffset, CapDesc const& _cap) = 0; + virtual std::shared_ptr<Capability> newPeerCapability(std::shared_ptr<Session> const& _s, unsigned _idOffset, CapDesc const& _cap) = 0; virtual void onStarting() {} virtual void onStopping() {} @@ -77,7 +78,7 @@ protected: virtual std::string name() const { return PeerCap::name(); } virtual u256 version() const { return PeerCap::version(); } virtual unsigned messageCount() const { return PeerCap::messageCount(); } - virtual Capability* newPeerCapability(std::shared_ptr<Session> _s, unsigned _idOffset, CapDesc const& _cap) { return new PeerCap(_s, this, _idOffset, _cap); } + virtual std::shared_ptr<Capability> newPeerCapability(std::shared_ptr<Session> const& _s, unsigned _idOffset, CapDesc const& _cap) { return std::make_shared<PeerCap>(_s, this, _idOffset, _cap); } }; } diff --git a/libp2p/Network.cpp b/libp2p/Network.cpp index b44ac1e0a..ef44c912d 100644 --- a/libp2p/Network.cpp +++ b/libp2p/Network.cpp @@ -169,10 +169,10 @@ bi::tcp::endpoint Network::traverseNAT(std::set<bi::address> const& _ifAddresses { asserts(_listenPort != 0); - UPnP* upnp = nullptr; + unique_ptr<UPnP> upnp; try { - upnp = new UPnP; + upnp.reset(new UPnP); } // let m_upnp continue as null - we handle it properly. catch (...) {} @@ -200,9 +200,6 @@ bi::tcp::endpoint Network::traverseNAT(std::set<bi::address> const& _ifAddresses } else clog(NetWarn) << "Couldn't punch through NAT (or no NAT in place)."; - - if (upnp) - delete upnp; } return upnpEP; diff --git a/libp2p/NodeTable.cpp b/libp2p/NodeTable.cpp index 44ab0aa99..9395466f4 100644 --- a/libp2p/NodeTable.cpp +++ b/libp2p/NodeTable.cpp @@ -43,7 +43,7 @@ NodeEntry::NodeEntry(NodeId const& _src, Public const& _pubk, NodeIPEndpoint con NodeTable::NodeTable(ba::io_service& _io, KeyPair const& _alias, NodeIPEndpoint const& _endpoint, bool _enabled): m_node(Node(_alias.pub(), _endpoint)), m_secret(_alias.sec()), - m_socket(new NodeSocket(_io, *this, (bi::udp::endpoint)m_node.endpoint)), + m_socket(make_shared<NodeSocket>(_io, *reinterpret_cast<UDPSocketEvents*>(this), (bi::udp::endpoint)m_node.endpoint)), m_socketPointer(m_socket.get()), m_timers(_io) { @@ -81,7 +81,7 @@ shared_ptr<NodeEntry> NodeTable::addNode(Node const& _node, NodeRelation _relati { if (_relation == Known) { - shared_ptr<NodeEntry> ret(new NodeEntry(m_node.id, _node.id, _node.endpoint)); + auto ret = make_shared<NodeEntry>(m_node.id, _node.id, _node.endpoint); ret->pending = false; DEV_GUARDED(x_nodes) m_nodes[_node.id] = ret; @@ -107,7 +107,7 @@ shared_ptr<NodeEntry> NodeTable::addNode(Node const& _node, NodeRelation _relati if (m_nodes.count(_node.id)) return m_nodes[_node.id]; - shared_ptr<NodeEntry> ret(new NodeEntry(m_node.id, _node.id, _node.endpoint)); + auto ret = make_shared<NodeEntry>(m_node.id, _node.id, _node.endpoint); DEV_GUARDED(x_nodes) m_nodes[_node.id] = ret; clog(NodeTableConnect) << "addNode pending for" << _node.endpoint; @@ -167,7 +167,7 @@ void NodeTable::doDiscover(NodeId _node, unsigned _round, shared_ptr<set<shared_ } else if (!_round && !_tried) // initialized _tried on first round - _tried.reset(new set<shared_ptr<NodeEntry>>()); + _tried = make_shared<set<shared_ptr<NodeEntry>>>(); auto nearest = nearestNodeEntries(_node); list<shared_ptr<NodeEntry>> tried; diff --git a/libp2p/RLPxHandshake.h b/libp2p/RLPxHandshake.h index 82d2a6f44..cbdc1da41 100644 --- a/libp2p/RLPxHandshake.h +++ b/libp2p/RLPxHandshake.h @@ -130,4 +130,4 @@ protected: }; } -} \ No newline at end of file +} diff --git a/libp2p/UPnP.cpp b/libp2p/UPnP.cpp index 36795c540..ebb1a8b6a 100644 --- a/libp2p/UPnP.cpp +++ b/libp2p/UPnP.cpp @@ -40,8 +40,8 @@ using namespace dev::p2p; UPnP::UPnP() { #if ETH_MINIUPNPC - m_urls.reset(new UPNPUrls); - m_data.reset(new IGDdatas); + m_urls = make_shared<UPNPUrls>(); + m_data = make_shared<IGDdatas>(); m_ok = false; diff --git a/libp2p/UPnP.h b/libp2p/UPnP.h index 4d53a998b..9ee01eacc 100644 --- a/libp2p/UPnP.h +++ b/libp2p/UPnP.h @@ -50,8 +50,8 @@ public: private: std::set<int> m_reg; bool m_ok; - std::shared_ptr<struct UPNPUrls> m_urls; - std::shared_ptr<struct IGDdatas> m_data; + std::shared_ptr<UPNPUrls> m_urls; + std::shared_ptr<IGDdatas> m_data; }; } diff --git a/libwebthree/WebThree.cpp b/libwebthree/WebThree.cpp index ba5990bf3..eef2c199e 100644 --- a/libwebthree/WebThree.cpp +++ b/libwebthree/WebThree.cpp @@ -64,7 +64,7 @@ WebThreeDirect::WebThreeDirect( } if (_interfaces.count("shh")) - m_whisper = m_net.registerCapability<WhisperHost>(new WhisperHost); + m_whisper = m_net.registerCapability(make_shared<WhisperHost>()); } WebThreeDirect::~WebThreeDirect() diff --git a/test/libp2p/capability.cpp b/test/libp2p/capability.cpp index e8c69655f..e60f7f131 100644 --- a/test/libp2p/capability.cpp +++ b/test/libp2p/capability.cpp @@ -110,11 +110,11 @@ BOOST_AUTO_TEST_CASE(capability) NetworkPreferences prefs2(localhost, 30302, false); Host host1("Test", prefs1); - auto thc1 = host1.registerCapability(new TestHostCapability()); + auto thc1 = host1.registerCapability(make_shared<TestHostCapability>()); host1.start(); Host host2("Test", prefs2); - auto thc2 = host2.registerCapability(new TestHostCapability()); + auto thc2 = host2.registerCapability(make_shared<TestHostCapability>()); host2.start(); int const step = 10; diff --git a/test/libwhisper/shhrpc.cpp b/test/libwhisper/shhrpc.cpp index f14aea6b2..e7937f5c9 100644 --- a/test/libwhisper/shhrpc.cpp +++ b/test/libwhisper/shhrpc.cpp @@ -121,7 +121,7 @@ BOOST_AUTO_TEST_CASE(basic) NetworkPreferences prefs2("127.0.0.1", port2, false); string const version2 = "shhrpc-host2"; Host host2(version2, prefs2); - auto whost2 = host2.registerCapability(new WhisperHost()); + auto whost2 = host2.registerCapability(make_shared<WhisperHost>()); host2.start(); for (unsigned i = 0; i < 3000 && !host2.haveNetwork(); i += step) @@ -169,7 +169,7 @@ BOOST_AUTO_TEST_CASE(send) Host host2("shhrpc-host2", NetworkPreferences("127.0.0.1", port2, false)); host2.setIdealPeerCount(1); - auto whost2 = host2.registerCapability(new WhisperHost()); + auto whost2 = host2.registerCapability(make_shared<WhisperHost>()); host2.start(); web3->startNetwork(); @@ -239,7 +239,7 @@ BOOST_AUTO_TEST_CASE(receive) uint16_t port2 = 30338; Host host2("shhrpc-host2", NetworkPreferences("127.0.0.1", port2, false)); host2.setIdealPeerCount(1); - auto whost2 = host2.registerCapability(new WhisperHost()); + auto whost2 = host2.registerCapability(make_shared<WhisperHost>()); host2.start(); web3->startNetwork(); @@ -379,7 +379,7 @@ BOOST_AUTO_TEST_CASE(server) uint16_t port2 = 30339; Host host2("shhrpc-host2", NetworkPreferences("127.0.0.1", port2, false)); host2.setIdealPeerCount(1); - auto whost2 = host2.registerCapability(new WhisperHost()); + auto whost2 = host2.registerCapability(make_shared<WhisperHost>()); host2.start(); b = jsonrpcServer->admin_net_start(sess2); diff --git a/test/libwhisper/whisperDB.cpp b/test/libwhisper/whisperDB.cpp index 892714393..3d7b09915 100644 --- a/test/libwhisper/whisperDB.cpp +++ b/test/libwhisper/whisperDB.cpp @@ -143,7 +143,7 @@ BOOST_AUTO_TEST_CASE(messages) { p2p::Host h("Test"); - auto wh = h.registerCapability(new WhisperHost(true)); + auto wh = h.registerCapability(make_shared<WhisperHost>(true)); preexisting = wh->all(); cnote << preexisting.size() << "preexisting messages in DB"; wh->installWatch(BuildTopic("test")); @@ -156,7 +156,7 @@ BOOST_AUTO_TEST_CASE(messages) { p2p::Host h("Test"); - auto wh = h.registerCapability(new WhisperHost(true)); + auto wh = h.registerCapability(make_shared<WhisperHost>(true)); map<h256, Envelope> m2 = wh->all(); wh->installWatch(BuildTopic("test")); BOOST_REQUIRE_EQUAL(m1.size(), m2.size()); @@ -204,7 +204,7 @@ BOOST_AUTO_TEST_CASE(corruptedData) { p2p::Host h("Test"); - auto wh = h.registerCapability(new WhisperHost(true)); + auto wh = h.registerCapability(make_shared<WhisperHost>(true)); m = wh->all(); BOOST_REQUIRE(m.end() == m.find(x)); } @@ -228,7 +228,7 @@ BOOST_AUTO_TEST_CASE(filters) { WhisperFiltersDB db; p2p::Host h("Test"); - auto wh = h.registerCapability(new WhisperHost()); + auto wh = h.registerCapability(make_shared<WhisperHost>()); wh->installWatch(BuildTopic("t1")); wh->installWatch(BuildTopic("t2")); db.saveTopicsToDB(*wh, persistID); @@ -243,7 +243,7 @@ BOOST_AUTO_TEST_CASE(filters) Host host1("Test", NetworkPreferences("127.0.0.1", port1, false)); host1.setIdealPeerCount(1); - auto whost1 = host1.registerCapability(new WhisperHost()); + auto whost1 = host1.registerCapability(make_shared<WhisperHost>()); host1.start(); WhisperFiltersDB db; auto watches = db.restoreTopicsFromDB(whost1.get(), persistID); @@ -277,7 +277,7 @@ BOOST_AUTO_TEST_CASE(filters) Host host2("Test", NetworkPreferences("127.0.0.1", 30309, false)); host2.setIdealPeerCount(1); - auto whost2 = host2.registerCapability(new WhisperHost()); + auto whost2 = host2.registerCapability(make_shared<WhisperHost>()); host2.start(); for (unsigned i = 0; i < 3000 && !host1.haveNetwork(); i += step) diff --git a/test/libwhisper/whisperTopic.cpp b/test/libwhisper/whisperTopic.cpp index 39728fb17..39d953e31 100644 --- a/test/libwhisper/whisperTopic.cpp +++ b/test/libwhisper/whisperTopic.cpp @@ -52,7 +52,7 @@ BOOST_AUTO_TEST_CASE(topic) uint16_t port1 = 30311; Host host1("Test", NetworkPreferences("127.0.0.1", port1, false)); host1.setIdealPeerCount(1); - auto whost1 = host1.registerCapability(new WhisperHost()); + auto whost1 = host1.registerCapability(make_shared<WhisperHost>()); host1.start(); bool host1Ready = false; @@ -85,7 +85,7 @@ BOOST_AUTO_TEST_CASE(topic) Host host2("Test", NetworkPreferences("127.0.0.1", 30310, false)); host2.setIdealPeerCount(1); - auto whost2 = host2.registerCapability(new WhisperHost()); + auto whost2 = host2.registerCapability(make_shared<WhisperHost>()); host2.start(); for (unsigned i = 0; i < 3000 && (!host1.haveNetwork() || !host2.haveNetwork()); i += step) @@ -130,7 +130,7 @@ BOOST_AUTO_TEST_CASE(forwarding) uint16_t port1 = 30312; Host host1("Listner", NetworkPreferences("127.0.0.1", port1, false)); host1.setIdealPeerCount(1); - auto whost1 = host1.registerCapability(new WhisperHost()); + auto whost1 = host1.registerCapability(make_shared<WhisperHost>()); host1.start(); while (!host1.haveNetwork()) this_thread::sleep_for(chrono::milliseconds(2)); @@ -165,7 +165,7 @@ BOOST_AUTO_TEST_CASE(forwarding) uint16_t port2 = 30313; Host host2("Forwarder", NetworkPreferences("127.0.0.1", port2, false)); host2.setIdealPeerCount(1); - auto whost2 = host2.registerCapability(new WhisperHost()); + auto whost2 = host2.registerCapability(make_shared<WhisperHost>()); host2.start(); while (!host2.haveNetwork()) this_thread::sleep_for(chrono::milliseconds(2)); @@ -203,7 +203,7 @@ BOOST_AUTO_TEST_CASE(forwarding) Host ph("Sender", NetworkPreferences("127.0.0.1", 30314, false)); ph.setIdealPeerCount(1); - shared_ptr<WhisperHost> wh = ph.registerCapability(new WhisperHost()); + shared_ptr<WhisperHost> wh = ph.registerCapability(make_shared<WhisperHost>()); ph.start(); ph.addNode(host2.id(), NodeIPEndpoint(bi::address::from_string("127.0.0.1"), port2, port2)); while (!ph.haveNetwork()) @@ -237,7 +237,7 @@ BOOST_AUTO_TEST_CASE(asyncforwarding) uint16_t port1 = 30315; Host host1("Forwarder", NetworkPreferences("127.0.0.1", port1, false)); host1.setIdealPeerCount(1); - auto whost1 = host1.registerCapability(new WhisperHost()); + auto whost1 = host1.registerCapability(make_shared<WhisperHost>()); host1.start(); while (!host1.haveNetwork()) this_thread::sleep_for(chrono::milliseconds(2)); @@ -266,7 +266,7 @@ BOOST_AUTO_TEST_CASE(asyncforwarding) { Host host2("Sender", NetworkPreferences("127.0.0.1", 30316, false)); host2.setIdealPeerCount(1); - shared_ptr<WhisperHost> whost2 = host2.registerCapability(new WhisperHost()); + shared_ptr<WhisperHost> whost2 = host2.registerCapability(make_shared<WhisperHost>()); host2.start(); while (!host2.haveNetwork()) this_thread::sleep_for(chrono::milliseconds(2)); @@ -283,7 +283,7 @@ BOOST_AUTO_TEST_CASE(asyncforwarding) { Host ph("Listener", NetworkPreferences("127.0.0.1", 30317, false)); ph.setIdealPeerCount(1); - shared_ptr<WhisperHost> wh = ph.registerCapability(new WhisperHost()); + shared_ptr<WhisperHost> wh = ph.registerCapability(make_shared<WhisperHost>()); ph.start(); while (!ph.haveNetwork()) this_thread::sleep_for(chrono::milliseconds(2)); @@ -319,7 +319,7 @@ BOOST_AUTO_TEST_CASE(topicAdvertising) Host host1("first", NetworkPreferences("127.0.0.1", 30319, false)); host1.setIdealPeerCount(1); - auto whost1 = host1.registerCapability(new WhisperHost()); + auto whost1 = host1.registerCapability(make_shared<WhisperHost>()); host1.start(); while (!host1.haveNetwork()) this_thread::sleep_for(chrono::milliseconds(10)); @@ -328,7 +328,7 @@ BOOST_AUTO_TEST_CASE(topicAdvertising) uint16_t port2 = 30318; Host host2("second", NetworkPreferences("127.0.0.1", port2, false)); host2.setIdealPeerCount(1); - auto whost2 = host2.registerCapability(new WhisperHost()); + auto whost2 = host2.registerCapability(make_shared<WhisperHost>()); unsigned w2 = whost2->installWatch(BuildTopicMask("test2")); host2.start(); @@ -399,7 +399,7 @@ BOOST_AUTO_TEST_CASE(selfAddressed) BuildTopicMask mask(text); Host host("first", NetworkPreferences("127.0.0.1", 30320, false)); - auto wh = host.registerCapability(new WhisperHost()); + auto wh = host.registerCapability(make_shared<WhisperHost>()); auto watch = wh->installWatch(BuildTopicMask(text)); unsigned const sample = 0xFEED; From 2bcaf4781113fa00b7119b823f9558f9c959ec6c Mon Sep 17 00:00:00 2001 From: chriseth <c@ethdev.com> Date: Mon, 10 Aug 2015 18:37:25 +0200 Subject: [PATCH 13/16] Use unique_ptr for RLPXFrameCoder. --- libp2p/Host.cpp | 4 ++-- libp2p/Host.h | 2 +- libp2p/RLPxHandshake.cpp | 7 +++---- libp2p/RLPxHandshake.h | 4 ++-- libp2p/Session.cpp | 5 ++--- libp2p/Session.h | 4 ++-- 6 files changed, 12 insertions(+), 14 deletions(-) diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 64d10b172..786ed77bf 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -225,7 +225,7 @@ void Host::doneWorking() m_sessions.clear(); } -void Host::startPeerSession(Public const& _id, RLP const& _rlp, RLPXFrameCoder* _io, std::shared_ptr<RLPXSocket> const& _s) +void Host::startPeerSession(Public const& _id, RLP const& _rlp, unique_ptr<RLPXFrameCoder>&& _io, std::shared_ptr<RLPXSocket> const& _s) { // session maybe ingress or egress so m_peers and node table entries may not exist shared_ptr<Peer> p; @@ -264,7 +264,7 @@ void Host::startPeerSession(Public const& _id, RLP const& _rlp, RLPXFrameCoder* clog(NetMessageSummary) << "Hello: " << clientVersion << "V[" << protocolVersion << "]" << _id << showbase << capslog.str() << dec << listenPort; // create session so disconnects are managed - auto ps = make_shared<Session>(this, _io, _s, p, PeerSessionInfo({_id, clientVersion, p->endpoint.address.to_string(), listenPort, chrono::steady_clock::duration(), _rlp[2].toSet<CapDesc>(), 0, map<string, string>(), protocolVersion})); + auto ps = make_shared<Session>(this, move(_io), _s, p, PeerSessionInfo({_id, clientVersion, p->endpoint.address.to_string(), listenPort, chrono::steady_clock::duration(), _rlp[2].toSet<CapDesc>(), 0, map<string, string>(), protocolVersion})); if (protocolVersion < dev::p2p::c_protocolVersion - 1) { ps->disconnect(IncompatibleProtocol); diff --git a/libp2p/Host.h b/libp2p/Host.h index 863149899..18fde758f 100644 --- a/libp2p/Host.h +++ b/libp2p/Host.h @@ -225,7 +225,7 @@ public: bool haveNetwork() const { return m_run && !!m_nodeTable; } /// Validates and starts peer session, taking ownership of _io. Disconnects and returns false upon error. - void startPeerSession(Public const& _id, RLP const& _hello, RLPXFrameCoder* _io, std::shared_ptr<RLPXSocket> const& _s); + void startPeerSession(Public const& _id, RLP const& _hello, std::unique_ptr<RLPXFrameCoder>&& _io, std::shared_ptr<RLPXSocket> const& _s); /// Get session by id std::shared_ptr<Session> peerSession(NodeId const& _id) { RecursiveGuard l(x_sessions); return m_sessions.count(_id) ? m_sessions[_id].lock() : std::shared_ptr<Session>(); } diff --git a/libp2p/RLPxHandshake.cpp b/libp2p/RLPxHandshake.cpp index 4fee9b42b..5f7e527c4 100644 --- a/libp2p/RLPxHandshake.cpp +++ b/libp2p/RLPxHandshake.cpp @@ -143,8 +143,7 @@ void RLPXHandshake::error() clog(NetP2PConnect) << "Handshake Failed (Connection reset by peer)"; m_socket->close(); - if (m_io != nullptr) - delete m_io; + m_io.reset(); } void RLPXHandshake::transition(boost::system::error_code _ech) @@ -194,7 +193,7 @@ void RLPXHandshake::transition(boost::system::error_code _ech) /// This pointer will be freed if there is an error otherwise /// it will be passed to Host which will take ownership. - m_io = new RLPXFrameCoder(*this); + m_io.reset(new RLPXFrameCoder(*this)); // old packet format // 5 arguments, HelloPacket @@ -286,7 +285,7 @@ void RLPXHandshake::transition(boost::system::error_code _ech) try { RLP rlp(frame.cropped(1), RLP::ThrowOnFail | RLP::FailIfTooSmall); - m_host->startPeerSession(m_remote, rlp, m_io, m_socket); + m_host->startPeerSession(m_remote, rlp, move(m_io), m_socket); } catch (std::exception const& _e) { diff --git a/libp2p/RLPxHandshake.h b/libp2p/RLPxHandshake.h index 82d2a6f44..a11f26f0a 100644 --- a/libp2p/RLPxHandshake.h +++ b/libp2p/RLPxHandshake.h @@ -123,11 +123,11 @@ protected: /// Used to read and write RLPx encrypted frames for last step of handshake authentication. /// Passed onto Host which will take ownership. - RLPXFrameCoder* m_io = nullptr; + std::unique_ptr<RLPXFrameCoder> m_io; std::shared_ptr<RLPXSocket> m_socket; ///< Socket. boost::asio::deadline_timer m_idleTimer; ///< Timer which enforces c_timeout. }; } -} \ No newline at end of file +} diff --git a/libp2p/Session.cpp b/libp2p/Session.cpp index 0d90ea6ea..ac38e813d 100644 --- a/libp2p/Session.cpp +++ b/libp2p/Session.cpp @@ -33,9 +33,9 @@ using namespace std; using namespace dev; using namespace dev::p2p; -Session::Session(Host* _h, RLPXFrameCoder* _io, std::shared_ptr<RLPXSocket> const& _s, std::shared_ptr<Peer> const& _n, PeerSessionInfo _info): +Session::Session(Host* _h, unique_ptr<RLPXFrameCoder>&& _io, std::shared_ptr<RLPXSocket> const& _s, std::shared_ptr<Peer> const& _n, PeerSessionInfo _info): m_server(_h), - m_io(_io), + m_io(move(_io)), m_socket(_s), m_peer(_n), m_info(_info), @@ -69,7 +69,6 @@ Session::~Session() } } catch (...){} - delete m_io; } ReputationManager& Session::repMan() const diff --git a/libp2p/Session.h b/libp2p/Session.h index 8d8c3ea1e..4d9a12a6c 100644 --- a/libp2p/Session.h +++ b/libp2p/Session.h @@ -55,7 +55,7 @@ class Session: public std::enable_shared_from_this<Session> friend class HostCapabilityFace; public: - Session(Host* _server, RLPXFrameCoder* _io, std::shared_ptr<RLPXSocket> const& _s, std::shared_ptr<Peer> const& _n, PeerSessionInfo _info); + Session(Host* _server, std::unique_ptr<RLPXFrameCoder>&& _io, std::shared_ptr<RLPXSocket> const& _s, std::shared_ptr<Peer> const& _n, PeerSessionInfo _info); virtual ~Session(); void start(); @@ -113,7 +113,7 @@ private: Host* m_server; ///< The host that owns us. Never null. - RLPXFrameCoder* m_io; ///< Transport over which packets are sent. + std::unique_ptr<RLPXFrameCoder> m_io; ///< Transport over which packets are sent. std::shared_ptr<RLPXSocket> m_socket; ///< Socket of peer's connection. Mutex x_writeQueue; ///< Mutex for the write queue. std::deque<bytes> m_writeQueue; ///< The write queue. From 4c256e1c4adfae6e74b31c7125b1ab7ab5979037 Mon Sep 17 00:00:00 2001 From: subtly <subtly> Date: Tue, 11 Aug 2015 16:25:11 +0200 Subject: [PATCH 14/16] field setting for ARM --- secp256k1/libsecp256k1-config.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/secp256k1/libsecp256k1-config.h b/secp256k1/libsecp256k1-config.h index 0cbbc2434..d0f8f18bf 100644 --- a/secp256k1/libsecp256k1-config.h +++ b/secp256k1/libsecp256k1-config.h @@ -90,7 +90,7 @@ #if defined(__x86_64__) || defined(_M_X64) #define USE_FIELD_5X52 1 -#elif defined(__i386) || defined(_M_IX86) +#elif defined(__i386) || defined(_M_IX86) || defined(__arm__) || defined(__M_ARM) #define USE_FIELD_10X26 1 #endif @@ -108,7 +108,7 @@ #if defined(__x86_64__) || defined(_M_X64) #define USE_SCALAR_4X64 1 -#elif defined(__i386) || defined(_M_IX86) +#elif defined(__i386) || defined(_M_IX86) || defined(__arm__) || defined(__M_ARM) #define USE_SCALAR_8X32 1 #endif From 59146478fb6bec1d0c6630203621bd302d9c1edb Mon Sep 17 00:00:00 2001 From: arkpar <arkady.paronyan@gmail.com> Date: Tue, 11 Aug 2015 16:38:44 +0200 Subject: [PATCH 15/16] fixed build for older qt versions --- alethzero/CMakeLists.txt | 4 ++-- mix/CMakeLists.txt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/alethzero/CMakeLists.txt b/alethzero/CMakeLists.txt index 9bbb8f92d..1576a016f 100644 --- a/alethzero/CMakeLists.txt +++ b/alethzero/CMakeLists.txt @@ -63,8 +63,8 @@ target_link_libraries(${EXECUTABLE} Qt5::Core) target_link_libraries(${EXECUTABLE} Qt5::Widgets) target_link_libraries(${EXECUTABLE} Qt5::WebEngine) target_link_libraries(${EXECUTABLE} Qt5::WebEngineWidgets) -if (APPLE) - target_link_libraries(${EXECUTABLE} Qt5::WebEngineCore) +if (APPLE AND (NOT "${Qt5Core_VERSION_STRING}" VERSION_LESS "5.5")) + target_link_libraries(${EXECUTABLE} Qt5::WebEngineCore) target_link_libraries(${EXECUTABLE} Qt5::DBus) target_link_libraries(${EXECUTABLE} Qt5::PrintSupport) endif() diff --git a/mix/CMakeLists.txt b/mix/CMakeLists.txt index 44cd9c12a..91e3d8508 100644 --- a/mix/CMakeLists.txt +++ b/mix/CMakeLists.txt @@ -21,7 +21,7 @@ if (("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") AND NOT (CMAKE_CXX_COMPILER_VER endif () find_package (Qt5WebEngine) -if (APPLE) +if (APPLE AND (NOT "${Qt5Core_VERSION_STRING}" VERSION_LESS "5.5")) # TODO: remove indirect dependencies once macdeployqt is fixed find_package (Qt5WebEngineCore) find_package (Qt5DBus) From dd14158a089ce06f912f2fbce217f600b8ba283a Mon Sep 17 00:00:00 2001 From: Vlad Gluhovsky <gluk256@gmail.com> Date: Tue, 11 Aug 2015 17:28:42 +0200 Subject: [PATCH 16/16] verbosity changed --- libp2p/NodeTable.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libp2p/NodeTable.cpp b/libp2p/NodeTable.cpp index c550a2343..f20d7436f 100644 --- a/libp2p/NodeTable.cpp +++ b/libp2p/NodeTable.cpp @@ -199,7 +199,7 @@ void NodeTable::doDiscover(NodeId _node, unsigned _round, shared_ptr<set<shared_ m_timers.schedule(c_reqTimeout.count() * 2, [this, _node, _round, _tried](boost::system::error_code const& _ec) { if (_ec) - clog(NodeTableWarn) << "Discovery timer canceled: " << _ec.value() << _ec.message(); + clog(NodeTableMessageDetail) << "Discovery timer canceled: " << _ec.value() << _ec.message(); if (_ec.value() == boost::asio::error::operation_aborted || m_timers.isStopped()) return;