Browse Source

Lenient gas limit.

Two uncles only.
Min 2048 difficulty & assoc adjustment.
cl-refactor
Gav Wood 10 years ago
parent
commit
10dccac64b
  1. 9
      libethcore/BlockInfo.cpp
  2. 36
      libethcore/Exceptions.cpp
  3. 5
      libethcore/Exceptions.h
  4. 21
      libethereum/State.cpp

9
libethcore/BlockInfo.cpp

@ -191,16 +191,19 @@ u256 BlockInfo::calculateDifficulty(BlockInfo const& _parent) const
if (!parentHash) if (!parentHash)
return c_genesisDifficulty; return c_genesisDifficulty;
else else
return max<u256>(1024, timestamp >= _parent.timestamp + (c_protocolVersion == 49 ? 5 : 8) ? _parent.difficulty - (_parent.difficulty >> 10) : (_parent.difficulty + (_parent.difficulty >> 10))); return max<u256>(2048, timestamp >= _parent.timestamp + (c_protocolVersion == 49 ? 5 : 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); }
void BlockInfo::verifyParent(BlockInfo const& _parent) const void BlockInfo::verifyParent(BlockInfo const& _parent) const
{ // Check difficulty is correct given the two timestamps. { // Check difficulty is correct given the two timestamps.
if (difficulty != calculateDifficulty(_parent)) if (difficulty != calculateDifficulty(_parent))
BOOST_THROW_EXCEPTION(InvalidDifficulty()); BOOST_THROW_EXCEPTION(InvalidDifficulty());
if (gasLimit != calculateGasLimit(_parent)) if (diff(gasLimit, _parent.gasLimit) <= _parent.gasLimit / 1024)
BOOST_THROW_EXCEPTION(InvalidGasLimit(gasLimit, calculateGasLimit(_parent))); BOOST_THROW_EXCEPTION(InvalidGasLimit(gasLimit, calculateGasLimit(_parent), diff(gasLimit, _parent.gasLimit), _parent.gasLimit / 1024));
// Check timestamp is after previous timestamp. // Check timestamp is after previous timestamp.
if (parentHash) if (parentHash)

36
libethcore/Exceptions.cpp

@ -27,24 +27,20 @@ using namespace std;
using namespace dev; using namespace dev;
using namespace dev::eth; using namespace dev::eth;
InvalidBlockFormat::InvalidBlockFormat(int _f, bytesConstRef _d): #if ALL_COMPILERS_ARE_CPP11
Exception("Invalid block format: Bad field " + toString(_f) + " (" + toHex(_d) + ")"), f(_f), d(_d.toBytes()) {} #define ETH_RETURN_STRING(S) thread_local static string s_what; s_what = S; return s_what.c_str();
#elsif USE_BOOST_TLS
UncleInChain::UncleInChain(h256Set _uncles, h256 _block): static boost::thread_specific_ptr<string> g_exceptionMessage;
Exception("Uncle in block already mentioned: Uncles " + toString(_uncles) + " (" + _block.abridged() + ")"), uncles(_uncles), block(_block) {} #define ETH_RETURN_STRING(S) if (!g_exceptionMessage.get()); g_exceptionMessage.reset(new string); *g_exceptionMessage.get() = S; return g_exceptionMessage.get()->c_str();
#else
InvalidTransactionsHash::InvalidTransactionsHash(h256 _head, h256 _real): #define ETH_RETURN_STRING(S) m_message = S; return m_message.c_str();
Exception("Invalid transactions hash: header says: " + toHex(_head.ref()) + " block is:" + toHex(_real.ref())), head(_head), real(_real) {} #endif
InvalidGasLimit::InvalidGasLimit(u256 _provided, u256 _valid): const char* InvalidBlockFormat::what() const noexcept { ETH_RETURN_STRING("Invalid block format: Bad field " + toString(m_f) + " (" + toHex(m_d) + ")"); }
Exception("Invalid gas limit (provided: " + toString(_provided) + " valid:" + toString(_valid) + ")"), provided(_provided), valid(_valid) {} const char* UncleInChain::what() const noexcept { ETH_RETURN_STRING("Uncle in block already mentioned: Uncles " + toString(m_uncles) + " (" + m_block.abridged() + ")"); }
const char* InvalidTransactionsHash::what() const noexcept { ETH_RETURN_STRING("Invalid transactions hash: header says: " + toHex(m_head.ref()) + " block is:" + toHex(m_real.ref())); }
InvalidMinGasPrice::InvalidMinGasPrice(u256 _provided, u256 _limit): const char* InvalidGasLimit::what() const noexcept { ETH_RETURN_STRING("Invalid gas limit (provided: " + toString(provided) + " default:" + toString(valid) + " givenDiff:" + toString(givenDiff) + " maxDiff:" + toString(maxDiff) + ")"); }
Exception("Invalid minimum gas price (provided: " + toString(_provided) + " limit:" + toString(_limit) + ")"), provided(_provided), limit(_limit) {} const char* InvalidMinGasPrice::what() const noexcept { ETH_RETURN_STRING("Invalid minimum gas price (provided: " + toString(provided) + " limit:" + toString(limit) + ")"); }
const char* InvalidNonce::what() const noexcept { ETH_RETURN_STRING("Invalid nonce (r: " + toString(required) + " c:" + toString(candidate) + ")"); }
InvalidNonce::InvalidNonce(u256 _required, u256 _candidate): const char* InvalidBlockNonce::what() const noexcept { ETH_RETURN_STRING("Invalid nonce (h: " + toString(h) + " n:" + toString(n) + " d:" + toString(d) + ")"); }
Exception("Invalid nonce (r: " + toString(_required) + " c:" + toString(_candidate) + ")"), required(_required), candidate(_candidate) {}
InvalidBlockNonce::InvalidBlockNonce(h256 _h, h256 _n, u256 _d):
Exception("Invalid nonce (h: " + toString(h) + " n:" + toString(n) + " d:" + toString(d) + ")"), h(_h), n(_n), d(_d) {}

5
libethcore/Exceptions.h

@ -47,6 +47,7 @@ struct InvalidSignature: virtual dev::Exception {};
class InvalidBlockFormat: virtual public dev::Exception { public: InvalidBlockFormat(int _f, bytesConstRef _d); int f; bytes d; }; class InvalidBlockFormat: virtual public dev::Exception { public: InvalidBlockFormat(int _f, bytesConstRef _d); int f; bytes d; };
struct InvalidUnclesHash: virtual dev::Exception {}; struct InvalidUnclesHash: virtual dev::Exception {};
struct InvalidUncle: virtual dev::Exception {}; struct InvalidUncle: virtual dev::Exception {};
struct TooManyUncles: virtual dev::Exception {};
struct UncleTooOld: virtual dev::Exception {}; struct UncleTooOld: virtual dev::Exception {};
class UncleInChain: virtual public dev::Exception { public: UncleInChain(h256Set _uncles, h256 _block); h256Set uncles; h256 block; }; class UncleInChain: virtual public dev::Exception { public: UncleInChain(h256Set _uncles, h256 _block); h256Set uncles; h256 block; };
struct DuplicateUncleNonce: virtual dev::Exception {}; struct DuplicateUncleNonce: virtual dev::Exception {};
@ -55,8 +56,8 @@ 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 {};
class InvalidGasLimit: virtual public dev::Exception { public: InvalidGasLimit(u256 _provided = 0, u256 _valid = 0); u256 provided; u256 valid; }; 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); u256 provided; u256 limit; }; 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 {};
struct InvalidTransactionsStateRoot: virtual dev::Exception {}; struct InvalidTransactionsStateRoot: virtual dev::Exception {};
struct InvalidReceiptsStateRoot: virtual dev::Exception {}; struct InvalidReceiptsStateRoot: virtual dev::Exception {};

21
libethereum/State.cpp

@ -487,10 +487,11 @@ u256 State::enact(bytesConstRef _block, BlockChain const& _bc, bool _checkNonce)
receiptsTrie.init(); receiptsTrie.init();
LastHashes lh = getLastHashes(_bc, (unsigned)m_previousBlock.number); LastHashes lh = getLastHashes(_bc, (unsigned)m_previousBlock.number);
RLP rlp(_block);
// All ok with the block generally. Play back the transactions now... // All ok with the block generally. Play back the transactions now...
unsigned i = 0; unsigned i = 0;
for (auto const& tr: RLP(_block)[1]) for (auto const& tr: rlp[1])
{ {
RLPStream k; RLPStream k;
k << i; k << i;
@ -504,17 +505,11 @@ u256 State::enact(bytesConstRef _block, BlockChain const& _bc, bool _checkNonce)
++i; ++i;
} }
if (transactionsTrie.root() != m_currentBlock.transactionsRoot)
{
cwarn << "Bad transactions state root!";
BOOST_THROW_EXCEPTION(InvalidTransactionsStateRoot());
}
if (receiptsTrie.root() != m_currentBlock.receiptsRoot) if (receiptsTrie.root() != m_currentBlock.receiptsRoot)
{ {
cwarn << "Bad receipts state root."; cwarn << "Bad receipts state root.";
cwarn << "Block:" << toHex(_block); cwarn << "Block:" << toHex(_block);
cwarn << "Block RLP:" << RLP(_block); cwarn << "Block RLP:" << rlp;
cwarn << "Calculated: " << receiptsTrie.root(); cwarn << "Calculated: " << receiptsTrie.root();
for (unsigned j = 0; j < i; ++j) for (unsigned j = 0; j < i; ++j)
{ {
@ -549,10 +544,14 @@ u256 State::enact(bytesConstRef _block, BlockChain const& _bc, bool _checkNonce)
u256 tdIncrease = m_currentBlock.difficulty; u256 tdIncrease = m_currentBlock.difficulty;
// Check uncles & apply their rewards to state. // Check uncles & apply their rewards to state.
if (rlp[2].itemCount() > 2)
BOOST_THROW_EXCEPTION(TooManyUncles());
set<h256> nonces = { m_currentBlock.nonce }; set<h256> nonces = { m_currentBlock.nonce };
Addresses rewarded; Addresses rewarded;
set<h256> knownUncles = _bc.allUnclesFrom(m_currentBlock.parentHash); set<h256> knownUncles = _bc.allUnclesFrom(m_currentBlock.parentHash);
for (auto const& i: RLP(_block)[2])
for (auto const& i: rlp[2])
{ {
if (knownUncles.count(sha3(i.data()))) if (knownUncles.count(sha3(i.data())))
BOOST_THROW_EXCEPTION(UncleInChain(knownUncles, sha3(i.data()) )); BOOST_THROW_EXCEPTION(UncleInChain(knownUncles, sha3(i.data()) ));
@ -700,7 +699,7 @@ void State::commitToMine(BlockChain const& _bc)
// cout << "Checking " << m_previousBlock.hash << ", parent=" << m_previousBlock.parentHash << endl; // cout << "Checking " << m_previousBlock.hash << ", parent=" << m_previousBlock.parentHash << endl;
set<h256> knownUncles = _bc.allUnclesFrom(m_currentBlock.parentHash); set<h256> knownUncles = _bc.allUnclesFrom(m_currentBlock.parentHash);
auto p = m_previousBlock.parentHash; auto p = m_previousBlock.parentHash;
for (unsigned gen = 0; gen < 6 && p != _bc.genesisHash(); ++gen, p = _bc.details(p).parent) for (unsigned gen = 0; gen < 6 && p != _bc.genesisHash() && unclesCount < 2; ++gen, p = _bc.details(p).parent)
{ {
auto us = _bc.details(p).children; auto us = _bc.details(p).children;
assert(us.size() >= 1); // must be at least 1 child of our grandparent - it's our own parent! assert(us.size() >= 1); // must be at least 1 child of our grandparent - it's our own parent!
@ -711,6 +710,8 @@ void State::commitToMine(BlockChain const& _bc)
ubi.streamRLP(unclesData, WithNonce); ubi.streamRLP(unclesData, WithNonce);
++unclesCount; ++unclesCount;
uncleAddresses.push_back(ubi.coinbaseAddress); uncleAddresses.push_back(ubi.coinbaseAddress);
if (unclesCount == 2)
break;
} }
} }
} }

Loading…
Cancel
Save