diff --git a/libethereum/BlockChain.cpp b/libethereum/BlockChain.cpp index eac0e6d4a..c49f32ab3 100644 --- a/libethereum/BlockChain.cpp +++ b/libethereum/BlockChain.cpp @@ -127,17 +127,17 @@ void BlockChain::import(bytes const& _block, Overlay const& _db) // Check block doesn't already exist first! if (details(newHash)) { -// cout << " Not new." << endl; + clog(BlockChainNote) << " Not new."; throw AlreadyHaveBlock(); } - cdebug << "Attempting import of " << newHash << "..."; + clog(BlockChainNote) << "Attempting import of " << newHash << "..."; // Work out its number as the parent's number + 1 auto pd = details(bi.parentHash); if (!pd) { - cdebug << " Unknown parent " << bi.parentHash; + clog(BlockChainNote) << " Unknown parent " << bi.parentHash; // We don't know the parent (yet) - discard for now. It'll get resent to us if we find out about its ancestry later on. throw UnknownParent(); } @@ -177,12 +177,11 @@ void BlockChain::import(bytes const& _block, Overlay const& _db) { m_lastBlockHash = newHash; m_detailsDB->Put(m_writeOptions, ldb::Slice("best"), ldb::Slice((char const*)&newHash, 32)); - cnote << " Imported and best."; + clog(BlockChainNote) << " Imported and best."; } else { - cnote << " Imported."; -// cwarn << "Imported block not newest (otd=" << m_details[m_lastBlockHash].totalDifficulty << ", td=" << td << ")"; + clog(BlockChainNote) << " Imported but not best (oTD:" << m_details[m_lastBlockHash].totalDifficulty << ", TD:" << td << ")"; } } diff --git a/libethereum/BlockChain.h b/libethereum/BlockChain.h index 22863d0ae..810dad4ef 100644 --- a/libethereum/BlockChain.h +++ b/libethereum/BlockChain.h @@ -55,6 +55,9 @@ class Overlay; class AlreadyHaveBlock: public std::exception {}; class UnknownParent: public std::exception {}; +struct BlockChainNote: public LogChannel { static const char constexpr* name = "-B-"; }; +struct BlockChainWarn: public LogChannel { static const char constexpr* name = "!B!"; }; + /** * @brief Implements the blockchain database. All data this gives is disk-backed. */ diff --git a/libethereum/BlockInfo.cpp b/libethereum/BlockInfo.cpp index 3146ffe44..520b2c202 100644 --- a/libethereum/BlockInfo.cpp +++ b/libethereum/BlockInfo.cpp @@ -39,6 +39,13 @@ BlockInfo::BlockInfo(bytesConstRef _block) populate(_block); } +BlockInfo BlockInfo::fromHeader(bytesConstRef _block) +{ + BlockInfo ret; + ret.populateFromHeader(RLP(_block)); + return ret; +} + bytes BlockInfo::createGenesisBlock() { RLPStream block(3); @@ -79,30 +86,36 @@ void BlockInfo::populateGenesis() populate(&genesisBlock); } -void BlockInfo::populate(bytesConstRef _block) +void BlockInfo::populateFromHeader(RLP const& _header) { - RLP root(_block); int field = 0; - RLP header = root[0]; - if (!header.isList()) - throw InvalidBlockFormat(0, header.data()); try { - hash = eth::sha3(_block); - parentHash = header[field = 0].toHash(); - sha3Uncles = header[field = 1].toHash(); - coinbaseAddress = header[field = 2].toHash
(); - stateRoot = header[field = 3].toHash(); - sha3Transactions = header[field = 4].toHash(); - difficulty = header[field = 5].toInt(); - timestamp = header[field = 6].toInt(); - extraData = header[field = 7].toBytes(); - nonce = header[field = 8].toHash(); + parentHash = _header[field = 0].toHash(); + sha3Uncles = _header[field = 1].toHash(); + coinbaseAddress = _header[field = 2].toHash
(); + stateRoot = _header[field = 3].toHash(); + sha3Transactions = _header[field = 4].toHash(); + difficulty = _header[field = 5].toInt(); + timestamp = _header[field = 6].toInt(); + extraData = _header[field = 7].toBytes(); + nonce = _header[field = 8].toHash(); } catch (RLP::BadCast) { - throw InvalidBlockHeaderFormat(field, header[field].data()); + throw InvalidBlockHeaderFormat(field, _header[field].data()); } +} + +void BlockInfo::populate(bytesConstRef _block) +{ + hash = eth::sha3(_block); + + RLP root(_block); + RLP header = root[0]; + if (!header.isList()) + throw InvalidBlockFormat(0, header.data()); + populateFromHeader(header); if (!root[1].isList()) throw InvalidBlockFormat(1, root[1].data()); diff --git a/libethereum/BlockInfo.h b/libethereum/BlockInfo.h index d932f63de..26d5497a9 100644 --- a/libethereum/BlockInfo.h +++ b/libethereum/BlockInfo.h @@ -44,6 +44,8 @@ public: BlockInfo(); explicit BlockInfo(bytesConstRef _block); + static BlockInfo fromHeader(bytesConstRef _block); + explicit operator bool() const { return timestamp != Invalid256; } bool operator==(BlockInfo const& _cmp) const @@ -61,6 +63,7 @@ public: bool operator!=(BlockInfo const& _cmp) const { return !operator==(_cmp); } static BlockInfo const& genesis() { if (!s_genesis) (s_genesis = new BlockInfo)->populateGenesis(); return *s_genesis; } + void populateFromHeader(RLP const& _header); void populate(bytesConstRef _block); void verifyInternals(bytesConstRef _block) const; void verifyParent(BlockInfo const& _parent) const; diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index b053fa9c1..b098f1f41 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -138,16 +138,18 @@ void Client::work() // all blocks. // Resynchronise state with block chain & trans if (m_s.sync(m_bc)) + { changed = true; - if (m_s.sync(m_tq)) - changed = true; + m_mined = m_s; + } m_lock.unlock(); if (m_doMine) { - if (changed || m_miningStarted) + if (m_miningStarted) { m_mined = m_s; + m_mined.sync(m_tq); m_mined.commitToMine(m_bc); } diff --git a/libethereum/State.cpp b/libethereum/State.cpp index f2424bec8..c4ad37bf4 100644 --- a/libethereum/State.cpp +++ b/libethereum/State.cpp @@ -344,7 +344,7 @@ u256 State::playback(bytesConstRef _block, BlockInfo const& _grandParent, bool _ Addresses rewarded; for (auto const& i: RLP(_block)[2]) { - BlockInfo uncle(i.data()); + BlockInfo uncle = BlockInfo::fromHeader(i.data()); if (m_previousBlock.parentHash != uncle.parentHash) throw InvalidUncle(); if (_grandParent) @@ -391,6 +391,8 @@ u256 State::playback(bytesConstRef _block, BlockInfo const& _grandParent, bool _ // (i.e. all the transactions we executed). void State::commitToMine(BlockChain const& _bc) { + cnote << "Commiting to mine on" << m_previousBlock.hash; + if (m_currentBlock.sha3Transactions != h256() || m_currentBlock.sha3Uncles != h256()) return; @@ -458,7 +460,7 @@ MineInfo State::mine(uint _msTimeout) ret.appendRaw(m_currentUncles); ret.swapOut(m_currentBytes); m_currentBlock.hash = sha3(m_currentBytes); - cout << "*** SUCCESS: Mined " << m_currentBlock.hash << " (parent: " << m_currentBlock.parentHash << ")" << endl; + cnote << "Mined " << m_currentBlock.hash << "(parent: " << m_currentBlock.parentHash << ")"; } else m_currentBytes.clear();