From 278995e19a4faf4c6bcef44d4a4d21951827d6e7 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sat, 30 Aug 2014 11:20:48 +0200 Subject: [PATCH] Use sha3(header) rather than sha3(block) for hash. --- exp/main.cpp | 12 ++++++++++-- libethcore/BlockInfo.cpp | 5 +++-- libethereum/BlockChain.cpp | 6 +++--- libethereum/BlockQueue.cpp | 5 ++--- libethereum/EthereumSession.cpp | 4 ++-- 5 files changed, 20 insertions(+), 12 deletions(-) diff --git a/exp/main.cpp b/exp/main.cpp index 7406a883d..d79fbb973 100644 --- a/exp/main.cpp +++ b/exp/main.cpp @@ -25,19 +25,24 @@ #include #include #include - +#endif #include #include #include +#include +#if 0 #include #include "BuildInfo.h" +#endif using namespace std; using namespace eth; +#if 0 +#if 0 namespace qi = boost::spirit::qi; namespace px = boost::phoenix; namespace sp = boost::spirit; -#if 0 + class ASTSymbol: public string { public: @@ -291,5 +296,8 @@ int main(int, char**) killBigints(out); cout << endl; #endif + + cnote << RLP(fromHex("f837c0c0b4600160003556601359506301000000600035040f6018590060005660805460016080530160005760003560805760203560003557")); + cnote << toHex(RLPStream(1).append(bytes(54, 0)).out()); return 0; } diff --git a/libethcore/BlockInfo.cpp b/libethcore/BlockInfo.cpp index 0b56c6c85..7f9b94860 100644 --- a/libethcore/BlockInfo.cpp +++ b/libethcore/BlockInfo.cpp @@ -71,6 +71,8 @@ h256 BlockInfo::headerHash(bytesConstRef _block) void BlockInfo::populateFromHeader(RLP const& _header, bool _checkNonce) { + hash = eth::sha3(_header.data()); + int field = 0; try { @@ -106,10 +108,9 @@ void BlockInfo::populateFromHeader(RLP const& _header, bool _checkNonce) void BlockInfo::populate(bytesConstRef _block, bool _checkNonce) { - hash = eth::sha3(_block); - RLP root(_block); RLP header = root[0]; + if (!header.isList()) throw InvalidBlockFormat(0, header.data()); populateFromHeader(header, _checkNonce); diff --git a/libethereum/BlockChain.cpp b/libethereum/BlockChain.cpp index 6a44c8d27..97d69eb41 100644 --- a/libethereum/BlockChain.cpp +++ b/libethereum/BlockChain.cpp @@ -193,7 +193,7 @@ h256s BlockChain::sync(BlockQueue& _bq, OverlayDB const& _stateDB, unsigned _max } catch (UnknownParent) { - cwarn << "Unknown parent of block!!!" << eth::sha3(block).abridged(); + cwarn << "Unknown parent of block!!!" << BlockInfo::headerHash(block).abridged(); _bq.import(&block, *this); } catch (...){} @@ -233,7 +233,7 @@ h256s BlockChain::import(bytes const& _block, OverlayDB const& _db) throw; } #endif - auto newHash = eth::sha3(_block); + auto newHash = BlockInfo::headerHash(_block); // Check block doesn't already exist first! if (details(newHash)) @@ -416,7 +416,7 @@ h256Set BlockChain::allUnclesFrom(h256 _parent) const h256 p = _parent; for (unsigned i = 0; i < 6 && p != m_genesisHash; ++i, p = details(p).parent) { - ret.insert(sha3(RLP(block(p))[0].data())); + ret.insert(p); // TODO: check: should this be details(p).parent? for (auto i: RLP(block(p))[2]) ret.insert(sha3(i.data())); } diff --git a/libethereum/BlockQueue.cpp b/libethereum/BlockQueue.cpp index 2d8b6a6b6..1aca07847 100644 --- a/libethereum/BlockQueue.cpp +++ b/libethereum/BlockQueue.cpp @@ -31,7 +31,7 @@ using namespace eth; bool BlockQueue::import(bytesConstRef _block, BlockChain const& _bc) { // Check if we already know this block. - h256 h = sha3(_block); + h256 h = BlockInfo::headerHash(_block); cnote << "Queuing block" << h.abridged() << "for import..."; @@ -61,10 +61,9 @@ bool BlockQueue::import(bytesConstRef _block, BlockChain const& _bc) return false; } #endif - auto newHash = eth::sha3(_block); // Check block doesn't already exist first! - if (_bc.details(newHash)) + if (_bc.details(h)) { cnote << "Already known in chain."; return false; diff --git a/libethereum/EthereumSession.cpp b/libethereum/EthereumSession.cpp index 5f9ee76fc..a04ccf0a3 100644 --- a/libethereum/EthereumSession.cpp +++ b/libethereum/EthereumSession.cpp @@ -319,7 +319,7 @@ bool EthereumSession::interpret(RLP const& _r) unsigned used = 0; for (unsigned i = 1; i < _r.itemCount(); ++i) { - auto h = sha3(_r[i].data()); + auto h = BlockInfo::headerHash(_r[i].data()); if (m_server->noteBlock(h, _r[i].data())) used++; m_askedBlocks.erase(h); @@ -332,7 +332,7 @@ bool EthereumSession::interpret(RLP const& _r) { for (unsigned i = 1; i < _r.itemCount(); ++i) { - auto h = sha3(_r[i].data()); + auto h = BlockInfo::headerHash(_r[i].data()); BlockInfo bi(_r[i].data()); if (!m_server->m_chain->details(bi.parentHash) && !m_knownBlocks.count(bi.parentHash)) {