Browse Source

Merge branch 'develop' into build_enhancement

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
79e13701d7
  1. 4
      alethzero/MainWin.cpp
  2. 14
      libethcore/BlockInfo.cpp
  3. 12
      libethcore/BlockInfo.h
  4. 10
      libethereum/State.cpp
  5. 14
      libwhisper/Common.cpp
  6. 2
      test/genesis.cpp
  7. 16
      test/whisperTopic.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/>Coinbase: <b>" << pretty(info.coinbaseAddress).toHtmlEscaped().toStdString() << "</b> " << info.coinbaseAddress;
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>";
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
s << "<br/>Proof-of-Work: <i>Phil has nothing to prove</i>";
s << "<br/>Parent: <b>" << info.parentHash << "</b>";

14
libethcore/BlockInfo.cpp

@ -49,19 +49,19 @@ BlockInfo BlockInfo::fromHeader(bytesConstRef _block)
return ret;
}
h256 BlockInfo::headerHashWithoutNonce() const
h256 BlockInfo::headerHash(IncludeNonce _n) const
{
RLPStream s;
streamRLP(s, false);
streamRLP(s, _n);
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
<< difficulty << number << gasLimit << gasUsed << timestamp << extraData;
if (_nonce)
if (_n == WithNonce)
_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.
if (_checkNonce && parentHash && !ProofOfWork::verify(headerHashWithoutNonce(), nonce, difficulty))
BOOST_THROW_EXCEPTION(InvalidBlockNonce(headerHashWithoutNonce(), nonce, difficulty));
if (_checkNonce && parentHash && !ProofOfWork::verify(headerHash(WithoutNonce), nonce, difficulty))
BOOST_THROW_EXCEPTION(InvalidBlockNonce(headerHash(WithoutNonce), nonce, difficulty));
if (gasUsed > gasLimit)
BOOST_THROW_EXCEPTION(TooMuchGasUsed());

12
libethcore/BlockInfo.h

@ -32,6 +32,12 @@ namespace eth
extern u256 c_genesisDifficulty;
enum IncludeNonce
{
WithoutNonce = 0,
WithNonce = 1
};
/** @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
* 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()
* 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
* conversion operator.
@ -113,8 +119,8 @@ public:
u256 calculateGasLimit(BlockInfo const& _parent) const;
/// No-nonce sha3 of the header only.
h256 headerHashWithoutNonce() const;
void streamRLP(RLPStream& _s, bool _nonce) const;
h256 headerHash(IncludeNonce _n) const;
void streamRLP(RLPStream& _s, IncludeNonce _n) const;
};
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:
RLPStream block;
block.appendList(3);
m_currentBlock.streamRLP(block, true);
m_currentBlock.streamRLP(block, WithNonce);
block.appendRaw(m_currentTxs);
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.
{
BlockInfo ubi(_bc.block(u));
ubi.streamRLP(unclesData, true);
ubi.streamRLP(unclesData, WithNonce);
++unclesCount;
uncleAddresses.push_back(ubi.coinbaseAddress);
}
@ -895,13 +895,13 @@ MineInfo State::mine(unsigned _msTimeout, bool _turbo)
m_currentBlock.difficulty = m_currentBlock.calculateDifficulty(m_previousBlock);
// 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)
m_currentBytes.clear();
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;
@ -915,7 +915,7 @@ void State::completeMine()
// Compile block:
RLPStream ret;
ret.appendList(3);
m_currentBlock.streamRLP(ret, true);
m_currentBlock.streamRLP(ret, WithNonce);
ret.appendRaw(m_currentTxs);
ret.appendRaw(m_currentUncles);
ret.swapOut(m_currentBytes);

14
libwhisper/Common.cpp

@ -54,10 +54,16 @@ bool TopicFilter::matches(Envelope const& _e) const
{
for (TopicMask const& t: m_topicMasks)
{
if (_e.topics().size() == t.size())
for (unsigned i = 0; i < t.size(); ++i)
if (((t[i].first ^ _e.topics()[i]) & t[i].second) != 0)
goto NEXT_TOPICMASK;
for (unsigned i = 0; i < t.size(); ++i)
{
for (auto et: _e.topics())
if (((t[i].first ^ et) & t[i].second) == 0)
goto NEXT_TOPICPART;
// failed to match topicmask against any topics: move on to next mask
goto NEXT_TOPICMASK;
NEXT_TOPICPART:;
}
// all topicmasks matched.
return true;
NEXT_TOPICMASK:;
}

2
test/genesis.cpp

@ -60,7 +60,7 @@ BOOST_AUTO_TEST_CASE(genesis_tests)
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(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()

16
test/whisperTopic.cpp

@ -32,7 +32,7 @@ BOOST_AUTO_TEST_SUITE(whisper)
BOOST_AUTO_TEST_CASE(topic)
{
g_logVerbosity = 0;
g_logVerbosity = 20;
bool started = false;
unsigned result = 0;
@ -40,16 +40,16 @@ BOOST_AUTO_TEST_CASE(topic)
{
setThreadName("other");
Host ph("Test", NetworkPreferences(30303, "", false, true));
Host ph("Test", NetworkPreferences(50303, "", false, true));
auto wh = ph.registerCapability(new WhisperHost());
ph.start();
started = true;
/// Only interested in odd packets
auto w = wh->installWatch(BuildTopicMask()("odd"));
auto w = wh->installWatch(BuildTopicMask("odd"));
for (int i = 0, last = 0; i < 100 && last < 81; ++i)
for (int i = 0, last = 0; i < 200 && last < 81; ++i)
{
for (auto i: wh->checkWatch(w))
{
@ -65,10 +65,12 @@ BOOST_AUTO_TEST_CASE(topic)
while (!started)
this_thread::sleep_for(chrono::milliseconds(50));
Host ph("Test", NetworkPreferences(30300, "", false, true));
Host ph("Test", NetworkPreferences(50300, "", false, true));
auto wh = ph.registerCapability(new WhisperHost());
this_thread::sleep_for(chrono::milliseconds(500));
ph.start();
ph.connect("127.0.0.1", 30303);
this_thread::sleep_for(chrono::milliseconds(500));
ph.connect("127.0.0.1", 50303);
KeyPair us = KeyPair::create();
for (int i = 0; i < 10; ++i)
@ -78,6 +80,8 @@ BOOST_AUTO_TEST_CASE(topic)
}
listener.join();
g_logVerbosity = 0;
BOOST_REQUIRE_EQUAL(result, 1 + 9 + 25 + 49 + 81);
}

Loading…
Cancel
Save