diff --git a/eth/main.cpp b/eth/main.cpp index 04e4871b3..e34862fde 100644 --- a/eth/main.cpp +++ b/eth/main.cpp @@ -20,6 +20,8 @@ * Ethereum client. */ +#include +#include #include #include "Defaults.h" #include "Client.h" @@ -236,7 +238,7 @@ int main(int argc, char** argv) c.stopMining(); else c.startMining(); - usleep(100000); + this_thread::sleep_for(chrono::milliseconds(100000)); } } diff --git a/libethereum/BlockChain.h b/libethereum/BlockChain.h index 61339dbb7..22863d0ae 100644 --- a/libethereum/BlockChain.h +++ b/libethereum/BlockChain.h @@ -22,6 +22,7 @@ #pragma once #include "Common.h" +#include "AddressState.h" namespace ldb = leveldb; namespace eth @@ -91,6 +92,10 @@ public: h256 genesisHash() const { return m_genesisHash; } + std::vector> interestQueue() { std::vector> ret; swap(ret, m_interestQueue); return ret; } + void pushInterest(Address _a) { m_interest[_a]++; } + void popInterest(Address _a) { if (m_interest[_a] > 1) m_interest[_a]--; else if (m_interest[_a]) m_interest.erase(_a); } + private: void checkConsistency(); @@ -98,6 +103,10 @@ private: mutable std::map m_details; mutable std::map m_cache; + /// The queue of transactions that have happened that we're interested in. + std::map m_interest; + std::vector> m_interestQueue; + ldb::DB* m_db; ldb::DB* m_detailsDB; diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index ea9c6b3bc..9cbab7033 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -21,6 +21,8 @@ #include "Client.h" +#include +#include #include "Common.h" #include "Defaults.h" using namespace std; @@ -41,18 +43,18 @@ Client::Client(std::string const& _clientVersion, Address _us, std::string const m_s.sync(m_tq); m_changed = true; - static std::string thread_name = "eth"; + static const char* c_threadName = "eth"; #if defined(__APPLE__) static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ - m_work = dispatch_queue_create(thread_name.c_str(), DISPATCH_QUEUE_SERIAL); + m_work = dispatch_queue_create(c_threadName, DISPATCH_QUEUE_SERIAL); }); dispatch_async(m_work, ^{ #else m_work = new thread([&](){ - setThreadName(thread_name); + setThreadName(c_threadName); #endif while (m_workState != Deleting) work(); m_workState = Deleted; @@ -64,7 +66,7 @@ Client::~Client() if (m_workState == Active) m_workState = Deleting; while (m_workState != Deleted) - usleep(10000); + this_thread::sleep_for(chrono::milliseconds(10000)); } void Client::startNetwork(short _listenPort, std::string const& _seedHost, short _port, NodeMode _mode, unsigned _peers, string const& _publicIP, bool _upnp) @@ -158,7 +160,7 @@ void Client::work() } } else - usleep(100000); + this_thread::sleep_for(chrono::milliseconds(100000)); } void Client::lock() diff --git a/libethereum/Client.h b/libethereum/Client.h index e75369be9..be485e768 100644 --- a/libethereum/Client.h +++ b/libethereum/Client.h @@ -40,6 +40,18 @@ struct MineProgress uint current; }; +class Client; + +class ClientGuard +{ +public: + inline ClientGuard(Client* _c); + inline ~ClientGuard(); + +private: + Client* m_client; +}; + class Client { public: @@ -56,10 +68,10 @@ public: void setInterest(Address _dest); /// @returns incoming minable transactions that we wanted to be notified of. Clears the queue. - Transactions pendingQueue(); + Transactions pendingQueue() { ClientGuard g(this); return m_tq.interestQueue(); } /// @returns alterations in state of a mined block that we wanted to be notified of. Clears the queue. - std::vector> minedQueue(); + std::vector> minedQueue() { ClientGuard g(this); return m_bc.interestQueue(); } // Not yet - probably best as using some sort of signals implementation. /// Calls @a _f when a valid transaction is received that involves @a _dest and once per such transaction. @@ -135,4 +147,14 @@ private: mutable bool m_changed; }; +inline ClientGuard::ClientGuard(Client* _c): m_client(_c) +{ + m_client->lock(); +} + +inline ClientGuard::~ClientGuard() +{ + m_client->unlock(); +} + } diff --git a/libethereum/Common.cpp b/libethereum/Common.cpp index bede9f1fb..f5896db95 100644 --- a/libethereum/Common.cpp +++ b/libethereum/Common.cpp @@ -43,8 +43,8 @@ int eth::g_logVerbosity = 8; map eth::g_logOverride; #if !defined(__APPLE__) -thread_local std::string eth::t_logThreadName = "???"; -static std::string g_mainThreadName = (eth::t_logThreadName = "main"); +thread_local char const* eth::t_logThreadName = "???"; +static char const* g_mainThreadName = (eth::t_logThreadName = "main"); #endif void eth::simpleDebugOut(std::string const& _s, char const*) diff --git a/libethereum/Common.h b/libethereum/Common.h index a184a0906..cd4f81af6 100644 --- a/libethereum/Common.h +++ b/libethereum/Common.h @@ -158,8 +158,8 @@ public: extern std::map g_logOverride; #if !defined(__APPLE__) -extern thread_local std::string t_logThreadName; -inline void setThreadName(std::string const& _n) { t_logThreadName = _n; } +extern thread_local char const* t_logThreadName; +inline void setThreadName(char const* _n) { t_logThreadName = _n; } #endif struct LogChannel { static const char constexpr* name = " "; static const int verbosity = 1; }; diff --git a/libethereum/PeerNetwork.cpp b/libethereum/PeerNetwork.cpp index ecc2e6f72..bb89fcdee 100644 --- a/libethereum/PeerNetwork.cpp +++ b/libethereum/PeerNetwork.cpp @@ -30,6 +30,7 @@ #endif #include +#include #include "Exceptions.h" #include "Common.h" #include "BlockChain.h" @@ -919,7 +920,7 @@ bool PeerServer::process(BlockChain& _bc, TransactionQueue& _tq, Overlay& _o) std::vector PeerServer::peers() const { const_cast(this)->pingAll(); - usleep(200000); + this_thread::sleep_for(chrono::milliseconds(200000)); std::vector ret; for (auto& i: m_peers) if (auto j = i.lock()) diff --git a/test/peer.cpp b/test/peer.cpp index 5a440a88f..ab7ea9dfd 100644 --- a/test/peer.cpp +++ b/test/peer.cpp @@ -20,6 +20,8 @@ * Peer Network test functions. */ +#include +#include #include #include using namespace std; @@ -53,7 +55,7 @@ int peerTest(int argc, char** argv) for (int i = 0; ; ++i) { - usleep(100000); + this_thread::sleep_for(chrono::milliseconds(100000)); pn.process(ch); if (!(i % 10)) pn.pingAll();