From e7cc24af1fc6f21d8ac4fd9385e694190075a4b0 Mon Sep 17 00:00:00 2001 From: Christoph Jentzsch Date: Tue, 11 Nov 2014 08:42:17 +0100 Subject: [PATCH 1/3] fix wrong unnecessary downcast --- libevm/VM.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libevm/VM.h b/libevm/VM.h index d8f71dfc9..a35b3f005 100644 --- a/libevm/VM.h +++ b/libevm/VM.h @@ -701,7 +701,7 @@ template dev::bytesConstRef dev::eth::VM::go(Ext& _ext, OnOpFunc con break; case Instruction::JUMP: nextPC = m_stack.back(); - if (!m_jumpDests.count((unsigned)nextPC)) + if (!m_jumpDests.count(nextPC)) BOOST_THROW_EXCEPTION(BadJumpDestination()); m_stack.pop_back(); break; @@ -709,7 +709,7 @@ template dev::bytesConstRef dev::eth::VM::go(Ext& _ext, OnOpFunc con if (m_stack[m_stack.size() - 2]) { nextPC = m_stack.back(); - if (!m_jumpDests.count((unsigned)nextPC)) + if (!m_jumpDests.count(nextPC)) BOOST_THROW_EXCEPTION(BadJumpDestination()); } m_stack.pop_back(); From 07663d141bc9c85f827cd2e8ad10b6a5fbc6bf04 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Tue, 11 Nov 2014 10:54:48 +0000 Subject: [PATCH 2/3] Make OOG on precompileds consistent. Warning fix. Use high-res clock for randoms. Whisper test. --- exp/main.cpp | 96 +++++++++++++++++++++++--------------- libdevcrypto/Common.cpp | 3 +- libethereum/State.cpp | 10 ++-- libevmcore/Instruction.cpp | 2 +- test/whisperTopic.cpp | 84 +++++++++++++++++++++++++++++++++ 5 files changed, 151 insertions(+), 44 deletions(-) create mode 100644 test/whisperTopic.cpp diff --git a/exp/main.cpp b/exp/main.cpp index 3af192380..8be79cad5 100644 --- a/exp/main.cpp +++ b/exp/main.cpp @@ -36,7 +36,7 @@ using namespace dev::eth; using namespace dev::p2p; using namespace dev::shh; -#if 0 +#if 1 int main() { DownloadMan man; @@ -44,18 +44,19 @@ int main() DownloadSub s1(man); DownloadSub s2(man); man.resetToChain(h256s({u256(0), u256(1), u256(2), u256(3), u256(4), u256(5), u256(6), u256(7), u256(8)})); - cnote << s0.nextFetch(2); - cnote << s1.nextFetch(2); - cnote << s2.nextFetch(2); - s0.noteBlock(u256(0)); + assert((s0.nextFetch(2) == h256Set{(u256)7, (u256)8})); + assert((s1.nextFetch(2) == h256Set{(u256)5, (u256)6})); + assert((s2.nextFetch(2) == h256Set{(u256)3, (u256)4})); + s0.noteBlock(u256(8)); s0.doneFetch(); - cnote << s0.nextFetch(2); - s1.noteBlock(u256(2)); - s1.noteBlock(u256(3)); + assert((s0.nextFetch(2) == h256Set{(u256)2, (u256)7})); + s1.noteBlock(u256(6)); + s1.noteBlock(u256(5)); s1.doneFetch(); - cnote << s1.nextFetch(2); - s0.doneFetch(); - cnote << s0.nextFetch(2); + assert((s1.nextFetch(2) == h256Set{(u256)0, (u256)1})); + s0.doneFetch(); // TODO: check exact semantics of doneFetch & nextFetch. Not sure if they're right -> doneFetch calls resetFetch which kills all the info of past fetches. + cdebug << s0.nextFetch(2); + assert((s0.nextFetch(2) == h256Set{(u256)3, (u256)4})); /* RangeMask m(0, 100); cnote << m; @@ -73,49 +74,70 @@ int main() } #endif -int main(int argc, char** argv) +/*int other(bool& o_started) { - g_logVerbosity = 20; - - short listenPort = 30303; - string remoteHost; - short remotePort = 30303; + setThreadName("other"); - for (int i = 1; i < argc; ++i) - { - string arg = argv[i]; - if (arg == "-l" && i + 1 < argc) - listenPort = (short)atoi(argv[++i]); - else if (arg == "-r" && i + 1 < argc) - remoteHost = argv[++i]; - else if (arg == "-p" && i + 1 < argc) - remotePort = (short)atoi(argv[++i]); - else - remoteHost = argv[i]; - } + short listenPort = 30300; Host ph("Test", NetworkPreferences(listenPort, "", false, true)); auto wh = ph.registerCapability(new WhisperHost()); ph.start(); - if (!remoteHost.empty()) - ph.connect(remoteHost, remotePort); + o_started = true; /// Only interested in odd packets auto w = wh->installWatch(BuildTopicMask()("odd")); - KeyPair us = KeyPair::create(); - for (int i = 0; ; ++i) + unsigned last = 0; + unsigned total = 0; + + for (int i = 0; i < 100 && last < 81; ++i) { - wh->post(us.sec(), RLPStream().append(i * i).out(), BuildTopic(i)(i % 2 ? "odd" : "even")); for (auto i: wh->checkWatch(w)) { Message msg = wh->envelope(i).open(); - + last = RLP(msg.payload()).toInt(); cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt(); + total += last; } - this_thread::sleep_for(chrono::seconds(1)); + this_thread::sleep_for(chrono::milliseconds(50)); } - return 0; + return total; } + +int main(int, char**) +{ + g_logVerbosity = 0; + + bool started = false; + unsigned result; + std::thread listener([&](){ return (result = other(started)); }); + while (!started) + this_thread::sleep_for(chrono::milliseconds(50)); + + short listenPort = 30303; + string remoteHost = "127.0.0.1"; + short remotePort = 30300; + + Host ph("Test", NetworkPreferences(listenPort, "", false, true)); + auto wh = ph.registerCapability(new WhisperHost()); + + ph.start(); + + if (!remoteHost.empty()) + ph.connect(remoteHost, remotePort); + + KeyPair us = KeyPair::create(); + for (int i = 0; i < 10; ++i) + { + wh->post(us.sec(), RLPStream().append(i * i).out(), BuildTopic(i)(i % 2 ? "odd" : "even")); + this_thread::sleep_for(chrono::milliseconds(250)); + } + + listener.join(); + assert(result == 1 + 9 + 25 + 49 + 81); + + return 0; +}*/ diff --git a/libdevcrypto/Common.cpp b/libdevcrypto/Common.cpp index d82098655..2db65aae3 100644 --- a/libdevcrypto/Common.cpp +++ b/libdevcrypto/Common.cpp @@ -21,6 +21,7 @@ */ #include +#include #include #include "EC.h" #include "SHA3.h" @@ -39,7 +40,7 @@ Address dev::toAddress(Secret _secret) KeyPair KeyPair::create() { - static mt19937_64 s_eng(time(0)); + static mt19937_64 s_eng(time(0) + chrono::high_resolution_clock::now().time_since_epoch().count()); uniform_int_distribution d(0, 255); for (int i = 0; i < 100; ++i) diff --git a/libethereum/State.cpp b/libethereum/State.cpp index 35e00f588..d468d33ec 100644 --- a/libethereum/State.cpp +++ b/libethereum/State.cpp @@ -1194,11 +1194,11 @@ bool State::call(Address _receiveAddress, Address _codeAddress, Address _senderA auto it = !(_codeAddress & ~h160(0xffffffff)) ? c_precompiled.find((unsigned)(u160)_codeAddress) : c_precompiled.end(); if (it != c_precompiled.end()) { - if (*_gas >= it->second.gas) - { - *_gas -= it->second.gas; - it->second.exec(_data, _out); - } + if (*_gas < it->second.gas) + return false; + + *_gas -= it->second.gas; + it->second.exec(_data, _out); } else if (addressHasCode(_codeAddress)) { diff --git a/libevmcore/Instruction.cpp b/libevmcore/Instruction.cpp index 2df77a3c5..5d94a22ca 100644 --- a/libevmcore/Instruction.cpp +++ b/libevmcore/Instruction.cpp @@ -327,7 +327,7 @@ InstructionInfo dev::eth::instructionInfo(Instruction _inst) catch (...) { cwarn << "\n" << boost::current_exception_diagnostic_information(); - return InstructionInfo({"", 0, 0, 0}); + return InstructionInfo({"", 0, 0, 0, false}); } } diff --git a/test/whisperTopic.cpp b/test/whisperTopic.cpp new file mode 100644 index 000000000..493b37bc2 --- /dev/null +++ b/test/whisperTopic.cpp @@ -0,0 +1,84 @@ +/* + 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 . +*/ +/** @file whisperTopic.cpp + * @author Gav Wood + * @date 2014 + */ +#include +#include +#include +#include +#include +using namespace std; +using namespace dev; +using namespace dev::p2p; +using namespace dev::shh; + +BOOST_AUTO_TEST_SUITE(whisper) + +BOOST_AUTO_TEST_CASE(topic) +{ + g_logVerbosity = 0; + + bool started = false; + unsigned result = 0; + std::thread listener([&]() + { + setThreadName("other"); + + Host ph("Test", NetworkPreferences(30303, "", false, true)); + auto wh = ph.registerCapability(new WhisperHost()); + ph.start(); + + started = true; + + /// Only interested in odd packets + auto w = wh->installWatch(BuildTopicMask()("odd")); + + for (int i = 0, last = 0; i < 100 && last < 81; ++i) + { + for (auto i: wh->checkWatch(w)) + { + Message msg = wh->envelope(i).open(); + last = RLP(msg.payload()).toInt(); + cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt(); + result += last; + } + this_thread::sleep_for(chrono::milliseconds(50)); + } + }); + + while (!started) + this_thread::sleep_for(chrono::milliseconds(50)); + + Host ph("Test", NetworkPreferences(30300, "", false, true)); + auto wh = ph.registerCapability(new WhisperHost()); + ph.start(); + ph.connect("127.0.0.1", 30303); + + KeyPair us = KeyPair::create(); + for (int i = 0; i < 10; ++i) + { + wh->post(us.sec(), RLPStream().append(i * i).out(), BuildTopic(i)(i % 2 ? "odd" : "even")); + this_thread::sleep_for(chrono::milliseconds(250)); + } + + listener.join(); + BOOST_REQUIRE_EQUAL(result, 1 + 9 + 25 + 49 + 81); +} + +BOOST_AUTO_TEST_SUITE_END() From 4fa7cbb6ac4e6d6b926f8d0cada16afb6c436002 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Tue, 11 Nov 2014 10:56:41 +0000 Subject: [PATCH 3/3] Merge #505 --- libethereum/State.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libethereum/State.cpp b/libethereum/State.cpp index d468d33ec..5eb315439 100644 --- a/libethereum/State.cpp +++ b/libethereum/State.cpp @@ -1195,7 +1195,10 @@ bool State::call(Address _receiveAddress, Address _codeAddress, Address _senderA if (it != c_precompiled.end()) { if (*_gas < it->second.gas) + { + *_gas = 0; return false; + } *_gas -= it->second.gas; it->second.exec(_data, _out);