From 717b4ff779c1c8cb0e82a05832887106fc7abb1d Mon Sep 17 00:00:00 2001 From: Arkady Paronyan Date: Fri, 3 Oct 2014 17:51:37 +0400 Subject: [PATCH 1/7] fixed msvc buld --- windows/Alethzero.vcxproj | 3 +++ 1 file changed, 3 insertions(+) diff --git a/windows/Alethzero.vcxproj b/windows/Alethzero.vcxproj index 38dbd6265..e1082110a 100644 --- a/windows/Alethzero.vcxproj +++ b/windows/Alethzero.vcxproj @@ -168,6 +168,9 @@ + + {1cc213a4-3482-4211-b47b-172e90dac7de} + {df3c5b07-a1a2-4f16-b37f-a27333622cdd} From c161ddb66a52ca3db53620e9c44e90deb7980ed9 Mon Sep 17 00:00:00 2001 From: Arkady Paronyan Date: Fri, 3 Oct 2014 17:53:04 +0400 Subject: [PATCH 2/7] fixed segfaults on accessing temp objects vector buffers --- alethzero/MainWin.cpp | 5 +++-- libdevcore/RLP.h | 3 +++ libdevcrypto/TrieDB.h | 26 +++++++++++++++++++------- libethereum/BlockChain.cpp | 9 ++++++--- libethereum/Client.cpp | 4 +++- test/crypto.cpp | 20 ++++++++++++-------- 6 files changed, 46 insertions(+), 21 deletions(-) diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index 2181098a9..c7f4f7e10 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -109,8 +109,9 @@ Main::Main(QWidget *parent) : cerr << "State root: " << BlockChain::genesis().stateRoot << endl; cerr << "Block Hash: " << sha3(BlockChain::createGenesisBlock()) << endl; - cerr << "Block RLP: " << RLP(BlockChain::createGenesisBlock()) << endl; - cerr << "Block Hex: " << toHex(BlockChain::createGenesisBlock()) << endl; + auto block = BlockChain::createGenesisBlock(); + cerr << "Block RLP: " << RLP(block) << endl; + cerr << "Block Hex: " << toHex(BlockChain::createGenesisBlock()) << endl; cerr << "Network protocol version: " << dev::eth::c_protocolVersion << endl; cerr << "Client database version: " << dev::eth::c_databaseVersion << endl; diff --git a/libdevcore/RLP.h b/libdevcore/RLP.h index 35e1d1dcf..4e45eda25 100644 --- a/libdevcore/RLP.h +++ b/libdevcore/RLP.h @@ -307,6 +307,9 @@ private: mutable unsigned m_lastIndex = (unsigned)-1; mutable unsigned m_lastEnd = 0; mutable bytesConstRef m_lastItem; + + /// Disable construction from rvalue + explicit RLP(bytes const&& _d) {} }; /** diff --git a/libdevcrypto/TrieDB.h b/libdevcrypto/TrieDB.h index 8ee6f3bca..68b819404 100644 --- a/libdevcrypto/TrieDB.h +++ b/libdevcrypto/TrieDB.h @@ -552,12 +552,18 @@ template bytes GenericTrieDB::mergeAt(RLP const& _orig, NibbleSli auto sh = _k.shared(k); // std::cout << _k << " sh " << k << " = " << sh << std::endl; - if (sh) + if (sh) + { // shared stuff - cleve at disagreement. - return mergeAt(RLP(cleve(_orig, sh)), _k, _v, true); + auto cleved = cleve(_orig, sh); + return mergeAt(RLP(cleved), _k, _v, true); + } else + { // nothing shared - branch - return mergeAt(RLP(branch(_orig)), _k, _v, true); + auto branched = branch(_orig); + return mergeAt(RLP(branched), _k, _v, true); + } } else { @@ -685,8 +691,11 @@ template bytes GenericTrieDB::deleteAt(RLP const& _orig, NibbleSl byte used = uniqueInUse(_orig, 16); if (used != 255) - if (isTwoItemNode(_orig[used])) - return graft(RLP(merge(_orig, used))); + if (isTwoItemNode(_orig[used])) + { + auto merged = merge(_orig, used); + return graft(RLP(merged)); + } else return merge(_orig, used); else @@ -721,8 +730,11 @@ template bytes GenericTrieDB::deleteAt(RLP const& _orig, NibbleSl return r.out(); // yes; merge - if (isTwoItemNode(rlp[used])) - return graft(RLP(merge(rlp, used))); + if (isTwoItemNode(rlp[used])) + { + auto merged = merge(rlp, used); + return graft(RLP(merged)); + } else return merge(rlp, used); } diff --git a/libethereum/BlockChain.cpp b/libethereum/BlockChain.cpp index f375adfcb..c8646c2db 100644 --- a/libethereum/BlockChain.cpp +++ b/libethereum/BlockChain.cpp @@ -277,8 +277,10 @@ h256s BlockChain::import(bytes const& _block, OverlayDB const& _db) auto pd = details(bi.parentHash); if (!pd) { - cwarn << "Odd: details is returning false despite block known:" << RLP(pd.rlp()); - cwarn << "Block:" << RLP(block(bi.parentHash)); + auto pdata = pd.rlp(); + cwarn << "Odd: details is returning false despite block known:" << RLP(pdata); + auto parentBlock = block(bi.parentHash); + cwarn << "Block:" << RLP(parentBlock); } // Check it's not crazy @@ -447,7 +449,8 @@ h256Set BlockChain::allUnclesFrom(h256 _parent) const for (unsigned i = 0; i < 6 && p != m_genesisHash; ++i, p = details(p).parent) { ret.insert(p); // TODO: check: should this be details(p).parent? - for (auto i: RLP(block(p))[2]) + auto bl = block(p); + for (auto i: RLP(bl)[2]) ret.insert(sha3(i.data())); } return ret; diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index de0077f89..b38b1db8e 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -36,7 +36,9 @@ using namespace p2p; VersionChecker::VersionChecker(string const& _dbPath): m_path(_dbPath.size() ? _dbPath : Defaults::dbPath()) { - m_ok = RLP(contents(m_path + "/protocol")).toInt(RLP::LaisezFaire) == c_protocolVersion && RLP(contents(m_path + "/database")).toInt(RLP::LaisezFaire) == c_databaseVersion; + auto protocolContents = contents(m_path + "/protocol"); + auto databaseContents = contents(m_path + "/database"); + m_ok = RLP(protocolContents).toInt(RLP::LaisezFaire) == c_protocolVersion && RLP(databaseContents).toInt(RLP::LaisezFaire) == c_databaseVersion; } void VersionChecker::setOk() diff --git a/test/crypto.cpp b/test/crypto.cpp index d2943984e..55feb1a54 100644 --- a/test/crypto.cpp +++ b/test/crypto.cpp @@ -46,12 +46,14 @@ BOOST_AUTO_TEST_CASE(crypto_tests) t.nonce = 0; t.receiveAddress = h160(fromHex("944400f4b88ac9589a0f17ed4671da26bddb668b")); t.value = 1000; - cnote << RLP(t.rlp(false)); - cnote << toHex(t.rlp(false)); + auto rlp = t.rlp(false); + cnote << RLP(rlp); + cnote << toHex(rlp); cnote << t.sha3(false); t.sign(p.secret()); - cnote << RLP(t.rlp(true)); - cnote << toHex(t.rlp(true)); + rlp = t.rlp(true); + cnote << RLP(rlp); + cnote << toHex(rlp); cnote << t.sha3(true); BOOST_REQUIRE(t.sender() == p.address()); } @@ -72,12 +74,14 @@ int cryptoTest() t.nonce = 0; t.receiveAddress = h160(fromHex("944400f4b88ac9589a0f17ed4671da26bddb668b")); t.value = 1000; - cnote << RLP(t.rlp(false)); - cnote << toHex(t.rlp(false)); + auto rlp = t.rlp(false); + cnote << RLP(rlp); + cnote << toHex(rlp); cnote << t.sha3(false); t.sign(p.secret()); - cnote << RLP(t.rlp(true)); - cnote << toHex(t.rlp(true)); + rlp = t.rlp(true); + cnote << RLP(rlp); + cnote << toHex(rlp); cnote << t.sha3(true); assert(t.sender() == p.address()); } From 582ae31f9f75927e32f254f157f0910f6eb37193 Mon Sep 17 00:00:00 2001 From: Arkady Paronyan Date: Fri, 3 Oct 2014 17:53:39 +0400 Subject: [PATCH 3/7] fixed races --- libethereum/DownloadMan.cpp | 3 ++- libethereum/DownloadMan.h | 29 ++++++++++++++++++----------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/libethereum/DownloadMan.cpp b/libethereum/DownloadMan.cpp index 3c8dae58e..303b7b000 100644 --- a/libethereum/DownloadMan.cpp +++ b/libethereum/DownloadMan.cpp @@ -42,6 +42,7 @@ DownloadSub::~DownloadSub() h256Set DownloadSub::nextFetch(unsigned _n) { Guard l(m_fetch); + ReadGuard cl(m_man->x_chain); if (m_remaining.size()) return m_remaining; @@ -50,7 +51,7 @@ h256Set DownloadSub::nextFetch(unsigned _n) m_indices.clear(); m_remaining.clear(); - if (!m_man || m_man->chain().empty()) + if (!m_man || m_man->m_chain.empty()) return h256Set(); m_asked = (~(m_man->taken() + m_attempted)).lowest(_n); diff --git a/libethereum/DownloadMan.h b/libethereum/DownloadMan.h index 3b88040fd..382c6aeae 100644 --- a/libethereum/DownloadMan.h +++ b/libethereum/DownloadMan.h @@ -54,8 +54,8 @@ public: /// Nothing doing here. void doneFetch() { resetFetch(); } - RangeMask const& asked() const { return m_asked; } - RangeMask const& attemped() const { return m_attempted; } + RangeMask const& asked() const { Guard l(m_fetch); return m_asked; } + RangeMask const& attemped() const { Guard l(m_fetch); return m_attempted; } private: void resetFetch() // Called by DownloadMan when we need to reset the download. @@ -69,7 +69,7 @@ private: DownloadMan* m_man = nullptr; - Mutex m_fetch; + mutable Mutex m_fetch; h256Set m_remaining; std::map m_indices; RangeMask m_asked; @@ -94,11 +94,14 @@ public: for (auto i: m_subs) i->resetFetch(); } - m_chain.clear(); - m_chain.reserve(_chain.size()); - for (auto i = _chain.rbegin(); i != _chain.rend(); ++i) - m_chain.push_back(*i); - m_blocksGot = RangeMask(0, m_chain.size()); + { + WriteGuard l(x_chain); + m_chain.clear(); + m_chain.reserve(_chain.size()); + for (auto i = _chain.rbegin(); i != _chain.rend(); ++i) + m_chain.push_back(*i); + m_blocksGot = RangeMask(0, m_chain.size()); + } } void reset() @@ -108,8 +111,11 @@ public: for (auto i: m_subs) i->resetFetch(); } - m_chain.clear(); - m_blocksGot.clear(); + { + WriteGuard l(x_chain); + m_chain.clear(); + m_blocksGot.clear(); + } } RangeMask taken(bool _desperate = false) const @@ -129,12 +135,13 @@ public: return m_blocksGot.full(); } - h256s chain() const { return m_chain; } + h256s chain() const { ReadGuard l(x_chain); return m_chain; } void foreachSub(std::function const& _f) const { ReadGuard l(x_subs); for(auto i: m_subs) _f(*i); } unsigned subCount() const { ReadGuard l(x_subs); return m_subs.size(); } RangeMask blocksGot() const { return m_blocksGot; } private: + mutable SharedMutex x_chain; h256s m_chain; RangeMask m_blocksGot; From 5aca7ce654398ab7b8c4bf199cfb88e0919e4e94 Mon Sep 17 00:00:00 2001 From: Arkady Paronyan Date: Fri, 3 Oct 2014 18:12:04 +0400 Subject: [PATCH 4/7] fixed formatting --- alethzero/MainWin.cpp | 4 ++-- libdevcrypto/TrieDB.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index c7f4f7e10..ee17a2be3 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -107,8 +107,8 @@ Main::Main(QWidget *parent) : #endif m_servers.append(QString::fromStdString(Host::pocHost() + ":30303")); - cerr << "State root: " << BlockChain::genesis().stateRoot << endl; - cerr << "Block Hash: " << sha3(BlockChain::createGenesisBlock()) << endl; + cerr << "State root: " << BlockChain::genesis().stateRoot << endl; + cerr << "Block Hash: " << sha3(BlockChain::createGenesisBlock()) << endl; auto block = BlockChain::createGenesisBlock(); cerr << "Block RLP: " << RLP(block) << endl; cerr << "Block Hex: " << toHex(BlockChain::createGenesisBlock()) << endl; diff --git a/libdevcrypto/TrieDB.h b/libdevcrypto/TrieDB.h index 68b819404..a8de703eb 100644 --- a/libdevcrypto/TrieDB.h +++ b/libdevcrypto/TrieDB.h @@ -552,7 +552,7 @@ template bytes GenericTrieDB::mergeAt(RLP const& _orig, NibbleSli auto sh = _k.shared(k); // std::cout << _k << " sh " << k << " = " << sh << std::endl; - if (sh) + if (sh) { // shared stuff - cleve at disagreement. auto cleved = cleve(_orig, sh); @@ -691,7 +691,7 @@ template bytes GenericTrieDB::deleteAt(RLP const& _orig, NibbleSl byte used = uniqueInUse(_orig, 16); if (used != 255) - if (isTwoItemNode(_orig[used])) + if (isTwoItemNode(_orig[used])) { auto merged = merge(_orig, used); return graft(RLP(merged)); @@ -730,7 +730,7 @@ template bytes GenericTrieDB::deleteAt(RLP const& _orig, NibbleSl return r.out(); // yes; merge - if (isTwoItemNode(rlp[used])) + if (isTwoItemNode(rlp[used])) { auto merged = merge(rlp, used); return graft(RLP(merged)); From c0aafadb5f6e82aee179d408b6f25e08037de986 Mon Sep 17 00:00:00 2001 From: Arkady Paronyan Date: Mon, 6 Oct 2014 13:25:41 +0400 Subject: [PATCH 5/7] reverted obsolete changes --- libethereum/BlockChain.cpp | 4 ++-- libethereum/DownloadMan.cpp | 3 +-- windows/Alethzero.vcxproj | 5 +---- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/libethereum/BlockChain.cpp b/libethereum/BlockChain.cpp index c8646c2db..00d1068e2 100644 --- a/libethereum/BlockChain.cpp +++ b/libethereum/BlockChain.cpp @@ -449,8 +449,8 @@ h256Set BlockChain::allUnclesFrom(h256 _parent) const for (unsigned i = 0; i < 6 && p != m_genesisHash; ++i, p = details(p).parent) { ret.insert(p); // TODO: check: should this be details(p).parent? - auto bl = block(p); - for (auto i: RLP(bl)[2]) + auto b = block(p); + for (auto i: RLP(b)[2]) ret.insert(sha3(i.data())); } return ret; diff --git a/libethereum/DownloadMan.cpp b/libethereum/DownloadMan.cpp index 303b7b000..3c8dae58e 100644 --- a/libethereum/DownloadMan.cpp +++ b/libethereum/DownloadMan.cpp @@ -42,7 +42,6 @@ DownloadSub::~DownloadSub() h256Set DownloadSub::nextFetch(unsigned _n) { Guard l(m_fetch); - ReadGuard cl(m_man->x_chain); if (m_remaining.size()) return m_remaining; @@ -51,7 +50,7 @@ h256Set DownloadSub::nextFetch(unsigned _n) m_indices.clear(); m_remaining.clear(); - if (!m_man || m_man->m_chain.empty()) + if (!m_man || m_man->chain().empty()) return h256Set(); m_asked = (~(m_man->taken() + m_attempted)).lowest(_n); diff --git a/windows/Alethzero.vcxproj b/windows/Alethzero.vcxproj index e1082110a..8db991c2c 100644 --- a/windows/Alethzero.vcxproj +++ b/windows/Alethzero.vcxproj @@ -168,9 +168,6 @@ - - {1cc213a4-3482-4211-b47b-172e90dac7de} - {df3c5b07-a1a2-4f16-b37f-a27333622cdd} @@ -311,4 +308,4 @@ - \ No newline at end of file + From 90f2066eaa75eec550d68ef8c81d6d76fd981da7 Mon Sep 17 00:00:00 2001 From: Arkady Paronyan Date: Mon, 6 Oct 2014 13:27:38 +0400 Subject: [PATCH 6/7] reordered members in rlp.h --- libdevcore/RLP.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libdevcore/RLP.h b/libdevcore/RLP.h index 4e45eda25..ec4bee44a 100644 --- a/libdevcore/RLP.h +++ b/libdevcore/RLP.h @@ -288,6 +288,9 @@ public: unsigned actualSize() const; private: + /// Disable construction from rvalue + explicit RLP(bytes const&& _d) {} + /// Single-byte data payload. bool isSingleByte() const { return !isNull() && m_data[0] < c_rlpDataImmLenStart; } @@ -307,9 +310,6 @@ private: mutable unsigned m_lastIndex = (unsigned)-1; mutable unsigned m_lastEnd = 0; mutable bytesConstRef m_lastItem; - - /// Disable construction from rvalue - explicit RLP(bytes const&& _d) {} }; /** From beaca33c6243518b306bee0d493b9071ee3b166e Mon Sep 17 00:00:00 2001 From: Arkady Paronyan Date: Mon, 6 Oct 2014 13:30:08 +0400 Subject: [PATCH 7/7] reverted Alethzero.vcxproj --- windows/Alethzero.vcxproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/windows/Alethzero.vcxproj b/windows/Alethzero.vcxproj index 8db991c2c..38dbd6265 100644 --- a/windows/Alethzero.vcxproj +++ b/windows/Alethzero.vcxproj @@ -308,4 +308,4 @@ - + \ No newline at end of file