Browse Source

Fix genesis test.

Refactor/cleaning.
cl-refactor
Gav Wood 10 years ago
parent
commit
0528037690
  1. 4
      alethzero/MainWin.cpp
  2. 14
      libethcore/BlockInfo.cpp
  3. 12
      libethcore/BlockInfo.h
  4. 10
      libethereum/State.cpp
  5. 2
      test/genesis.cpp

4
alethzero/MainWin.cpp

@ -1261,10 +1261,10 @@ void Main::on_blocks_currentItemChanged()
s << "<br/>Gas used/limit: <b>" << info.gasUsed << "</b>/<b>" << info.gasLimit << "</b>"; s << "<br/>Gas used/limit: <b>" << info.gasUsed << "</b>/<b>" << info.gasLimit << "</b>";
s << "<br/>Coinbase: <b>" << pretty(info.coinbaseAddress).toHtmlEscaped().toStdString() << "</b> " << info.coinbaseAddress; s << "<br/>Coinbase: <b>" << pretty(info.coinbaseAddress).toHtmlEscaped().toStdString() << "</b> " << info.coinbaseAddress;
s << "<br/>Nonce: <b>" << info.nonce << "</b>"; s << "<br/>Nonce: <b>" << info.nonce << "</b>";
s << "<br/>Hash w/o nonce: <b>" << info.headerHashWithoutNonce() << "</b>"; s << "<br/>Hash w/o nonce: <b>" << info.headerHash(WithoutNonce) << "</b>";
s << "<br/>Difficulty: <b>" << info.difficulty << "</b>"; s << "<br/>Difficulty: <b>" << info.difficulty << "</b>";
if (info.number) if (info.number)
s << "<br/>Proof-of-Work: <b>" << ProofOfWork::eval(info.headerHashWithoutNonce(), info.nonce) << " &lt;= " << (h256)u256((bigint(1) << 256) / info.difficulty) << "</b>"; s << "<br/>Proof-of-Work: <b>" << ProofOfWork::eval(info.headerHash(WithoutNonce), info.nonce) << " &lt;= " << (h256)u256((bigint(1) << 256) / info.difficulty) << "</b>";
else else
s << "<br/>Proof-of-Work: <i>Phil has nothing to prove</i>"; s << "<br/>Proof-of-Work: <i>Phil has nothing to prove</i>";
s << "<br/>Parent: <b>" << info.parentHash << "</b>"; s << "<br/>Parent: <b>" << info.parentHash << "</b>";

14
libethcore/BlockInfo.cpp

@ -49,19 +49,19 @@ BlockInfo BlockInfo::fromHeader(bytesConstRef _block)
return ret; return ret;
} }
h256 BlockInfo::headerHashWithoutNonce() const h256 BlockInfo::headerHash(IncludeNonce _n) const
{ {
RLPStream s; RLPStream s;
streamRLP(s, false); streamRLP(s, _n);
return sha3(s.out()); return sha3(s.out());
} }
void BlockInfo::streamRLP(RLPStream& _s, bool _nonce) const void BlockInfo::streamRLP(RLPStream& _s, IncludeNonce _n) const
{ {
_s.appendList(_nonce ? 14 : 13) _s.appendList(_n == WithNonce ? 14 : 13)
<< parentHash << sha3Uncles << coinbaseAddress << stateRoot << transactionsRoot << receiptsRoot << logBloom << parentHash << sha3Uncles << coinbaseAddress << stateRoot << transactionsRoot << receiptsRoot << logBloom
<< difficulty << number << gasLimit << gasUsed << timestamp << extraData; << difficulty << number << gasLimit << gasUsed << timestamp << extraData;
if (_nonce) if (_n == WithNonce)
_s << nonce; _s << nonce;
} }
@ -100,8 +100,8 @@ void BlockInfo::populateFromHeader(RLP const& _header, bool _checkNonce)
} }
// check it hashes according to proof of work or that it's the genesis block. // check it hashes according to proof of work or that it's the genesis block.
if (_checkNonce && parentHash && !ProofOfWork::verify(headerHashWithoutNonce(), nonce, difficulty)) if (_checkNonce && parentHash && !ProofOfWork::verify(headerHash(WithoutNonce), nonce, difficulty))
BOOST_THROW_EXCEPTION(InvalidBlockNonce(headerHashWithoutNonce(), nonce, difficulty)); BOOST_THROW_EXCEPTION(InvalidBlockNonce(headerHash(WithoutNonce), nonce, difficulty));
if (gasUsed > gasLimit) if (gasUsed > gasLimit)
BOOST_THROW_EXCEPTION(TooMuchGasUsed()); BOOST_THROW_EXCEPTION(TooMuchGasUsed());

12
libethcore/BlockInfo.h

@ -32,6 +32,12 @@ namespace eth
extern u256 c_genesisDifficulty; extern u256 c_genesisDifficulty;
enum IncludeNonce
{
WithoutNonce = 0,
WithNonce = 1
};
/** @brief Encapsulation of a block header. /** @brief Encapsulation of a block header.
* Class to contain all of a block header's data. It is able to parse a block header and populate * Class to contain all of a block header's data. It is able to parse a block header and populate
* from some given RLP block serialisation with the static fromHeader(), through the method * from some given RLP block serialisation with the static fromHeader(), through the method
@ -48,7 +54,7 @@ extern u256 c_genesisDifficulty;
* *
* The difficulty and gas-limit derivations may be calculated with the calculateDifficulty() * The difficulty and gas-limit derivations may be calculated with the calculateDifficulty()
* and calculateGasLimit() and the object serialised to RLP with streamRLP. To determine the * and calculateGasLimit() and the object serialised to RLP with streamRLP. To determine the
* header hash without the nonce (for mining), the method headerHashWithoutNonce() is provided. * header hash without the nonce (for mining), the method headerHash(WithoutNonce) is provided.
* *
* The default constructor creates an empty object, which can be tested against with the boolean * The default constructor creates an empty object, which can be tested against with the boolean
* conversion operator. * conversion operator.
@ -113,8 +119,8 @@ public:
u256 calculateGasLimit(BlockInfo const& _parent) const; u256 calculateGasLimit(BlockInfo const& _parent) const;
/// No-nonce sha3 of the header only. /// No-nonce sha3 of the header only.
h256 headerHashWithoutNonce() const; h256 headerHash(IncludeNonce _n) const;
void streamRLP(RLPStream& _s, bool _nonce) const; void streamRLP(RLPStream& _s, IncludeNonce _n) const;
}; };
inline std::ostream& operator<<(std::ostream& _out, BlockInfo const& _bi) inline std::ostream& operator<<(std::ostream& _out, BlockInfo const& _bi)

10
libethereum/State.cpp

@ -764,7 +764,7 @@ bool State::amIJustParanoid(BlockChain const& _bc)
// Compile block: // Compile block:
RLPStream block; RLPStream block;
block.appendList(3); block.appendList(3);
m_currentBlock.streamRLP(block, true); m_currentBlock.streamRLP(block, WithNonce);
block.appendRaw(m_currentTxs); block.appendRaw(m_currentTxs);
block.appendRaw(m_currentUncles); block.appendRaw(m_currentUncles);
@ -831,7 +831,7 @@ void State::commitToMine(BlockChain const& _bc)
if (!knownUncles.count(u)) // ignore any uncles/mainline blocks that we know about. if (!knownUncles.count(u)) // ignore any uncles/mainline blocks that we know about.
{ {
BlockInfo ubi(_bc.block(u)); BlockInfo ubi(_bc.block(u));
ubi.streamRLP(unclesData, true); ubi.streamRLP(unclesData, WithNonce);
++unclesCount; ++unclesCount;
uncleAddresses.push_back(ubi.coinbaseAddress); uncleAddresses.push_back(ubi.coinbaseAddress);
} }
@ -895,13 +895,13 @@ MineInfo State::mine(unsigned _msTimeout, bool _turbo)
m_currentBlock.difficulty = m_currentBlock.calculateDifficulty(m_previousBlock); m_currentBlock.difficulty = m_currentBlock.calculateDifficulty(m_previousBlock);
// TODO: Miner class that keeps dagger between mine calls (or just non-polling mining). // TODO: Miner class that keeps dagger between mine calls (or just non-polling mining).
auto ret = m_pow.mine(/*out*/m_currentBlock.nonce, m_currentBlock.headerHashWithoutNonce(), m_currentBlock.difficulty, _msTimeout, true, _turbo); auto ret = m_pow.mine(/*out*/m_currentBlock.nonce, m_currentBlock.headerHash(WithoutNonce), m_currentBlock.difficulty, _msTimeout, true, _turbo);
if (!ret.completed) if (!ret.completed)
m_currentBytes.clear(); m_currentBytes.clear();
else else
{ {
cnote << "Completed" << m_currentBlock.headerHashWithoutNonce().abridged() << m_currentBlock.nonce.abridged() << m_currentBlock.difficulty << ProofOfWork::verify(m_currentBlock.headerHashWithoutNonce(), m_currentBlock.nonce, m_currentBlock.difficulty); cnote << "Completed" << m_currentBlock.headerHash(WithoutNonce).abridged() << m_currentBlock.nonce.abridged() << m_currentBlock.difficulty << ProofOfWork::verify(m_currentBlock.headerHash(WithoutNonce), m_currentBlock.nonce, m_currentBlock.difficulty);
} }
return ret; return ret;
@ -915,7 +915,7 @@ void State::completeMine()
// Compile block: // Compile block:
RLPStream ret; RLPStream ret;
ret.appendList(3); ret.appendList(3);
m_currentBlock.streamRLP(ret, true); m_currentBlock.streamRLP(ret, WithNonce);
ret.appendRaw(m_currentTxs); ret.appendRaw(m_currentTxs);
ret.appendRaw(m_currentUncles); ret.appendRaw(m_currentUncles);
ret.swapOut(m_currentBytes); ret.swapOut(m_currentBytes);

2
test/genesis.cpp

@ -51,7 +51,7 @@ BOOST_AUTO_TEST_CASE(genesis_tests)
BOOST_CHECK_EQUAL(BlockChain::genesis().stateRoot, h256(o["genesis_state_root"].get_str())); BOOST_CHECK_EQUAL(BlockChain::genesis().stateRoot, h256(o["genesis_state_root"].get_str()));
BOOST_CHECK_EQUAL(toHex(BlockChain::createGenesisBlock()), toHex(fromHex(o["genesis_rlp_hex"].get_str()))); BOOST_CHECK_EQUAL(toHex(BlockChain::createGenesisBlock()), toHex(fromHex(o["genesis_rlp_hex"].get_str())));
BOOST_CHECK_EQUAL(sha3(BlockChain::createGenesisBlock()), h256(o["genesis_hash"].get_str())); BOOST_CHECK_EQUAL(BlockInfo::headerHash(BlockChain::createGenesisBlock()), h256(o["genesis_hash"].get_str()));
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

Loading…
Cancel
Save