Browse Source

More info in AZ.

cl-refactor
Gav Wood 10 years ago
parent
commit
95587acdc0
  1. 22
      alethzero/MainWin.cpp
  2. 18
      libethcore/BlockInfo.cpp
  3. 7
      libethcore/BlockInfo.h
  4. 1
      libethcore/Exceptions.h
  5. 8
      libethcore/ProofOfWork.cpp
  6. 11
      libethcore/ProofOfWork.h

22
alethzero/MainWin.cpp

@ -1362,11 +1362,16 @@ void Main::on_blocks_currentItemChanged()
s << "&nbsp;&emsp;&nbsp;Children: <b>" << details.children.size() << "</b></h5>"; s << "&nbsp;&emsp;&nbsp;Children: <b>" << details.children.size() << "</b></h5>";
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/>Seed hash: <b>" << info.seedHash << "</b>";
s << "<br/>Mix hash: <b>" << info.mixHash << "</b>";
s << "<br/>Nonce: <b>" << info.nonce << "</b>"; s << "<br/>Nonce: <b>" << info.nonce << "</b>";
s << "<br/>Hash w/o nonce: <b>" << info.headerHash(WithoutNonce) << "</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) << " &lt;= " << (h256)u256((bigint(1) << 256) / info.difficulty) << "</b>"; {
auto e = ProofOfWork::eval(info);
s << "<br/>Proof-of-Work: <b>" << e.value << " &lt;= " << (h256)u256((bigint(1) << 256) / info.difficulty) << "</b> (mixhash: " << e.mixHash.abridged() << ")";
}
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>";
@ -1378,9 +1383,18 @@ void Main::on_blocks_currentItemChanged()
for (auto u: block[2]) for (auto u: block[2])
{ {
BlockInfo uncle = BlockInfo::fromHeader(u.data()); BlockInfo uncle = BlockInfo::fromHeader(u.data());
s << "<br/><span style=\"margin-left: 2em\">&nbsp;</span>Hash: <b>" << uncle.hash << "</b>"; char const* line = "<br/><span style=\"margin-left: 2em\">&nbsp;</span>";
s << "<br/><span style=\"margin-left: 2em\">&nbsp;</span>Parent: <b>" << uncle.parentHash << "</b>"; s << line << "Hash: <b>" << uncle.hash << "</b>";
s << "<br/><span style=\"margin-left: 2em\">&nbsp;</span>Number: <b>" << uncle.number << "</b>"; s << line << "Parent: <b>" << uncle.parentHash << "</b>";
s << line << "Number: <b>" << uncle.number << "</b>";
s << line << "Coinbase: <b>" << pretty(uncle.coinbaseAddress).toHtmlEscaped().toStdString() << "</b> " << uncle.coinbaseAddress;
s << line << "Seed hash: <b>" << uncle.seedHash << "</b>";
s << line << "Mix hash: <b>" << uncle.mixHash << "</b>";
s << line << "Nonce: <b>" << uncle.nonce << "</b>";
s << line << "Hash w/o nonce: <b>" << uncle.headerHash(WithoutNonce) << "</b>";
s << line << "Difficulty: <b>" << uncle.difficulty << "</b>";
auto e = ProofOfWork::eval(uncle);
s << line << "Proof-of-Work: <b>" << e.value << " &lt;= " << (h256)u256((bigint(1) << 256) / uncle.difficulty) << "</b> (mixhash: " << e.mixHash.abridged() << ")";
} }
if (info.parentHash) if (info.parentHash)
s << "<br/>Pre: <b>" << BlockInfo(ethereum()->blockChain().block(info.parentHash)).stateRoot << "</b>"; s << "<br/>Pre: <b>" << BlockInfo(ethereum()->blockChain().block(info.parentHash)).stateRoot << "</b>";

18
libethcore/BlockInfo.cpp

@ -57,7 +57,7 @@ void BlockInfo::setEmpty()
timestamp = 0; timestamp = 0;
extraData.clear(); extraData.clear();
seedHash = h256(); seedHash = h256();
mixBytes = h256(); mixHash = h256();
nonce = Nonce(); nonce = Nonce();
hash = headerHash(WithNonce); hash = headerHash(WithNonce);
} }
@ -82,7 +82,7 @@ void BlockInfo::streamRLP(RLPStream& _s, IncludeNonce _n) const
<< parentHash << sha3Uncles << coinbaseAddress << stateRoot << transactionsRoot << receiptsRoot << logBloom << parentHash << sha3Uncles << coinbaseAddress << stateRoot << transactionsRoot << receiptsRoot << logBloom
<< difficulty << number << gasLimit << gasUsed << timestamp << extraData << seedHash; << difficulty << number << gasLimit << gasUsed << timestamp << extraData << seedHash;
if (_n == WithNonce) if (_n == WithNonce)
_s << mixBytes << nonce; _s << mixHash << nonce;
} }
h256 BlockInfo::headerHash(bytesConstRef _block) h256 BlockInfo::headerHash(bytesConstRef _block)
@ -111,7 +111,7 @@ void BlockInfo::populateFromHeader(RLP const& _header, bool _checkNonce)
timestamp = _header[field = 11].toInt<u256>(); timestamp = _header[field = 11].toInt<u256>();
extraData = _header[field = 12].toBytes(); extraData = _header[field = 12].toBytes();
seedHash = _header[field = 13].toHash<h256>(); seedHash = _header[field = 13].toHash<h256>();
mixBytes = _header[field = 14].toHash<h256>(); mixHash = _header[field = 14].toHash<h256>();
nonce = _header[field = 15].toHash<Nonce>(); nonce = _header[field = 15].toHash<Nonce>();
} }
@ -180,7 +180,12 @@ void BlockInfo::populateFromParent(BlockInfo const& _parent)
gasLimit = calculateGasLimit(_parent); gasLimit = calculateGasLimit(_parent);
gasUsed = 0; gasUsed = 0;
difficulty = calculateDifficulty(_parent); difficulty = calculateDifficulty(_parent);
seedHash = number % 30 == 0 ? sha3(_parent.seedHash.asBytes() /*+ _parent.hash.asBytes()*/) : _parent.seedHash; seedHash = calculateSeedHash(_parent);
}
h256 BlockInfo::calculateSeedHash(BlockInfo const& _parent) const
{
return number % 30 == 0 ? sha3(_parent.seedHash.asBytes()) : _parent.seedHash;
} }
u256 BlockInfo::calculateGasLimit(BlockInfo const& _parent) const u256 BlockInfo::calculateGasLimit(BlockInfo const& _parent) const
@ -196,7 +201,7 @@ u256 BlockInfo::calculateDifficulty(BlockInfo const& _parent) const
if (!parentHash) if (!parentHash)
return c_genesisDifficulty; return c_genesisDifficulty;
else else
return max<u256>(2048, timestamp >= _parent.timestamp + (c_protocolVersion == 49 ? 5 : 8) ? _parent.difficulty - (_parent.difficulty / 2048) : (_parent.difficulty + (_parent.difficulty / 2048))); return max<u256>(2048, timestamp >= _parent.timestamp + 8 ? _parent.difficulty - (_parent.difficulty / 2048) : (_parent.difficulty + (_parent.difficulty / 2048)));
} }
template <class N> inline N diff(N const& _a, N const& _b) { return max(_a, _b) - min(_a, _b); } template <class N> inline N diff(N const& _a, N const& _b) { return max(_a, _b) - min(_a, _b); }
@ -209,6 +214,9 @@ void BlockInfo::verifyParent(BlockInfo const& _parent) const
if (diff(gasLimit, _parent.gasLimit) <= _parent.gasLimit / 2048) if (diff(gasLimit, _parent.gasLimit) <= _parent.gasLimit / 2048)
BOOST_THROW_EXCEPTION(InvalidGasLimit(gasLimit, calculateGasLimit(_parent), diff(gasLimit, _parent.gasLimit), _parent.gasLimit / 2048)); BOOST_THROW_EXCEPTION(InvalidGasLimit(gasLimit, calculateGasLimit(_parent), diff(gasLimit, _parent.gasLimit), _parent.gasLimit / 2048));
if (seedHash != calculateSeedHash(_parent))
BOOST_THROW_EXCEPTION(InvalidSeedHash());
// Check timestamp is after previous timestamp. // Check timestamp is after previous timestamp.
if (parentHash) if (parentHash)
{ {

7
libethcore/BlockInfo.h

@ -76,7 +76,7 @@ public:
u256 gasUsed; u256 gasUsed;
u256 timestamp; u256 timestamp;
bytes extraData; bytes extraData;
h256 mixBytes; h256 mixHash;
h256 seedHash; h256 seedHash;
Nonce nonce; Nonce nonce;
@ -106,7 +106,7 @@ public:
gasUsed == _cmp.gasUsed && gasUsed == _cmp.gasUsed &&
timestamp == _cmp.timestamp && timestamp == _cmp.timestamp &&
extraData == _cmp.extraData && extraData == _cmp.extraData &&
mixBytes == _cmp.mixBytes && mixHash == _cmp.mixHash &&
seedHash == _cmp.seedHash && seedHash == _cmp.seedHash &&
nonce == _cmp.nonce; nonce == _cmp.nonce;
} }
@ -123,6 +123,7 @@ public:
u256 calculateDifficulty(BlockInfo const& _parent) const; u256 calculateDifficulty(BlockInfo const& _parent) const;
u256 calculateGasLimit(BlockInfo const& _parent) const; u256 calculateGasLimit(BlockInfo const& _parent) const;
h256 calculateSeedHash(BlockInfo const& _parent) const;
/// sha3 of the header only. /// sha3 of the header only.
h256 headerHash(IncludeNonce _n) const; h256 headerHash(IncludeNonce _n) const;
@ -133,7 +134,7 @@ inline std::ostream& operator<<(std::ostream& _out, BlockInfo const& _bi)
{ {
_out << _bi.hash << " " << _bi.parentHash << " " << _bi.sha3Uncles << " " << _bi.coinbaseAddress << " " << _bi.stateRoot << " " << _bi.transactionsRoot << " " << _out << _bi.hash << " " << _bi.parentHash << " " << _bi.sha3Uncles << " " << _bi.coinbaseAddress << " " << _bi.stateRoot << " " << _bi.transactionsRoot << " " <<
_bi.receiptsRoot << " " << _bi.logBloom << " " << _bi.difficulty << " " << _bi.number << " " << _bi.gasLimit << " " << _bi.receiptsRoot << " " << _bi.logBloom << " " << _bi.difficulty << " " << _bi.number << " " << _bi.gasLimit << " " <<
_bi.gasUsed << " " << _bi.timestamp << " " << _bi.mixBytes << " " << _bi.seedHash << " " << _bi.nonce; _bi.gasUsed << " " << _bi.timestamp << " " << _bi.mixHash << " " << _bi.seedHash << " " << _bi.nonce;
return _out; return _out;
} }

1
libethcore/Exceptions.h

@ -57,6 +57,7 @@ struct InvalidGasUsed: virtual dev::Exception {};
class InvalidTransactionsHash: virtual public dev::Exception { public: InvalidTransactionsHash(h256 _head, h256 _real); h256 head; h256 real; }; class InvalidTransactionsHash: virtual public dev::Exception { public: InvalidTransactionsHash(h256 _head, h256 _real); h256 head; h256 real; };
struct InvalidTransaction: virtual dev::Exception {}; struct InvalidTransaction: virtual dev::Exception {};
struct InvalidDifficulty: virtual dev::Exception {}; struct InvalidDifficulty: virtual dev::Exception {};
struct InvalidSeedHash: virtual dev::Exception {};
class InvalidGasLimit: virtual public dev::Exception { public: InvalidGasLimit(u256 _provided = 0, u256 _valid = 0, u256 _gD = 0, u256 _mD = 0): provided(_provided), valid(_valid), givenDiff(_gD), maxDiff(_mD) {} u256 provided; u256 valid; u256 givenDiff; u256 maxDiff; virtual const char* what() const noexcept; }; class InvalidGasLimit: virtual public dev::Exception { public: InvalidGasLimit(u256 _provided = 0, u256 _valid = 0, u256 _gD = 0, u256 _mD = 0): provided(_provided), valid(_valid), givenDiff(_gD), maxDiff(_mD) {} u256 provided; u256 valid; u256 givenDiff; u256 maxDiff; virtual const char* what() const noexcept; };
class InvalidMinGasPrice: virtual public dev::Exception { public: InvalidMinGasPrice(u256 _provided = 0, u256 _limit = 0): provided(_provided), limit(_limit) {} u256 provided; u256 limit; virtual const char* what() const noexcept; }; class InvalidMinGasPrice: virtual public dev::Exception { public: InvalidMinGasPrice(u256 _provided = 0, u256 _limit = 0): provided(_provided), limit(_limit) {} u256 provided; u256 limit; virtual const char* what() const noexcept; };
struct InvalidTransactionGasUsed: virtual dev::Exception {}; struct InvalidTransactionGasUsed: virtual dev::Exception {};

8
libethcore/ProofOfWork.cpp

@ -118,16 +118,16 @@ Ethasher* Ethasher::s_this = nullptr;
bool Ethash::verify(BlockInfo const& _header) bool Ethash::verify(BlockInfo const& _header)
{ {
bigint boundary = (bigint(1) << 256) / _header.difficulty; bigint boundary = (bigint(1) << 256) / _header.difficulty;
u256 e(eval(_header, _header.nonce)); auto e = eval(_header, _header.nonce);
return e <= boundary; return (u256)e.value <= boundary && e.mixHash == _header.mixHash;
} }
h256 Ethash::eval(BlockInfo const& _header, Nonce const& _nonce) Ethash::Result Ethash::eval(BlockInfo const& _header, Nonce const& _nonce)
{ {
auto p = Ethasher::params(_header); auto p = Ethasher::params(_header);
ethash_return_value r; ethash_return_value r;
ethash_compute_light(&r, Ethasher::get()->cache(_header).data(), &p, _header.headerHash(WithoutNonce).data(), (uint64_t)(u64)_nonce); ethash_compute_light(&r, Ethasher::get()->cache(_header).data(), &p, _header.headerHash(WithoutNonce).data(), (uint64_t)(u64)_nonce);
return h256(r.result, h256::ConstructFromPointer); return Result{h256(r.result, h256::ConstructFromPointer), h256(r.mix_hash, h256::ConstructFromPointer)};
} }
std::pair<MineInfo, Ethash::Proof> Ethash::mine(BlockInfo const& _header, unsigned _msTimeout, bool _continue, bool _turbo) std::pair<MineInfo, Ethash::Proof> Ethash::mine(BlockInfo const& _header, unsigned _msTimeout, bool _continue, bool _turbo)

11
libethcore/ProofOfWork.h

@ -55,12 +55,17 @@ public:
Nonce nonce; Nonce nonce;
h256 mixHash; h256 mixHash;
}; };
struct Result
{
h256 value;
h256 mixHash;
};
static h256 eval(BlockInfo const& _header) { return eval(_header, _header.nonce); } static Result eval(BlockInfo const& _header) { return eval(_header, _header.nonce); }
static h256 eval(BlockInfo const& _header, Nonce const& _nonce); static Result eval(BlockInfo const& _header, Nonce const& _nonce);
static bool verify(BlockInfo const& _header); static bool verify(BlockInfo const& _header);
std::pair<MineInfo, Proof> mine(BlockInfo const& _header, unsigned _msTimeout = 100, bool _continue = true, bool _turbo = false); std::pair<MineInfo, Proof> mine(BlockInfo const& _header, unsigned _msTimeout = 100, bool _continue = true, bool _turbo = false);
static void assignResult(Proof const& _r, BlockInfo& _header) { _header.nonce = _r.nonce; _header.mixBytes = _r.mixHash; } static void assignResult(Proof const& _r, BlockInfo& _header) { _header.nonce = _r.nonce; _header.mixHash = _r.mixHash; }
protected: protected:
Nonce m_last; Nonce m_last;

Loading…
Cancel
Save