From c187452a1d2926924b2e10ff6a9f9f1dd881a07f Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 5 Mar 2015 11:09:34 +0100 Subject: [PATCH] Bunch of repotting/curating. [10:59:28] Vitalik Buterin: block.parent.gas_limit * 1023 / 1024 <= block.gas_limit <= block.parent.gas_limit * 1025/1024 --- libethcore/BlockInfo.cpp | 7 +- libethcore/Exceptions.cpp | 2 +- libethcore/Exceptions.h | 4 +- libethcore/ProofOfWork.h | 2 +- libethereum/Precompiled.cpp | 2 +- rlp/base64.cpp | 128 ------------------------------------ rlp/base64.h | 40 ----------- rlp/main.cpp | 94 +++++++++++++------------- 8 files changed, 54 insertions(+), 225 deletions(-) delete mode 100644 rlp/base64.cpp delete mode 100644 rlp/base64.h diff --git a/libethcore/BlockInfo.cpp b/libethcore/BlockInfo.cpp index c8d5625a6..7c3360e4a 100644 --- a/libethcore/BlockInfo.cpp +++ b/libethcore/BlockInfo.cpp @@ -203,8 +203,6 @@ u256 BlockInfo::calculateDifficulty(BlockInfo const& _parent) const return max(c_minimumDifficulty, timestamp >= _parent.timestamp + c_durationLimit ? _parent.difficulty - (_parent.difficulty / c_difficultyBoundDivisor) : (_parent.difficulty + (_parent.difficulty / c_difficultyBoundDivisor))); } -template inline N diff(N const& _a, N const& _b) { return max(_a, _b) - min(_a, _b); } - void BlockInfo::verifyParent(BlockInfo const& _parent) const { // Check difficulty is correct given the two timestamps. @@ -215,8 +213,9 @@ void BlockInfo::verifyParent(BlockInfo const& _parent) const gasLimit > _parent.gasLimit * (c_gasLimitBoundDivisor + 1) / c_gasLimitBoundDivisor) BOOST_THROW_EXCEPTION(InvalidGasLimit(gasLimit, _parent.gasLimit * (c_gasLimitBoundDivisor - 1) / c_gasLimitBoundDivisor, _parent.gasLimit * (c_gasLimitBoundDivisor + 1) / c_gasLimitBoundDivisor)); - if (diff(gasLimit, _parent.gasLimit) <= _parent.gasLimit / c_gasLimitBoundDivisor) - BOOST_THROW_EXCEPTION(InvalidGasLimit(gasLimit, calculateGasLimit(_parent), diff(gasLimit, _parent.gasLimit), _parent.gasLimit / c_gasLimitBoundDivisor)); + if (gasLimit < _parent.gasLimit * (c_gasLimitBoundDivisor - 1) / c_gasLimitBoundDivisor || + gasLimit > _parent.gasLimit * (c_gasLimitBoundDivisor + 1) / c_gasLimitBoundDivisor) + BOOST_THROW_EXCEPTION(InvalidGasLimit(gasLimit, _parent.gasLimit * (c_gasLimitBoundDivisor - 1) / c_gasLimitBoundDivisor, _parent.gasLimit * (c_gasLimitBoundDivisor + 1) / c_gasLimitBoundDivisor)); if (seedHash != calculateSeedHash(_parent)) BOOST_THROW_EXCEPTION(InvalidSeedHash()); diff --git a/libethcore/Exceptions.cpp b/libethcore/Exceptions.cpp index c4628f4aa..c489c8f4a 100644 --- a/libethcore/Exceptions.cpp +++ b/libethcore/Exceptions.cpp @@ -39,7 +39,7 @@ static boost::thread_specific_ptr g_exceptionMessage; const char* InvalidBlockFormat::what() const noexcept { ETH_RETURN_STRING("Invalid block format: Bad field " + toString(m_f) + " (" + toHex(m_d) + ")"); } 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())); } -const char* InvalidGasLimit::what() const noexcept { ETH_RETURN_STRING("Invalid gas limit (provided: " + toString(provided) + " default:" + toString(valid) + " givenDiff:" + toString(givenDiff) + " maxDiff:" + toString(maxDiff) + ")"); } +const char* InvalidGasLimit::what() const noexcept { ETH_RETURN_STRING("Invalid gas limit (provided: " + toString(provided) + " minimum:" + toString(minimum) + " max:" + toString(maximum) + ")"); } 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) + ")"); } const char* InvalidBlockNonce::what() const noexcept { ETH_RETURN_STRING("Invalid nonce (h: " + toString(h) + " n:" + toString(n) + " d:" + toString(d) + ")"); } diff --git a/libethcore/Exceptions.h b/libethcore/Exceptions.h index 45667f44a..edf3d6b03 100644 --- a/libethcore/Exceptions.h +++ b/libethcore/Exceptions.h @@ -22,7 +22,7 @@ #pragma once #include -#include "CommonEth.h" +#include "Common.h" namespace dev { @@ -58,7 +58,7 @@ class InvalidTransactionsHash: virtual public dev::Exception { public: InvalidTr struct InvalidTransaction: 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 _n = 0, u256 _x = 0): provided(_provided), minimum(_n), maximum(_x) {} u256 provided; u256 minimum; u256 maximum; 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 InvalidTransactionsStateRoot: virtual dev::Exception {}; diff --git a/libethcore/ProofOfWork.h b/libethcore/ProofOfWork.h index fdf51f0d4..7006f6f61 100644 --- a/libethcore/ProofOfWork.h +++ b/libethcore/ProofOfWork.h @@ -27,7 +27,7 @@ #include #include #include -#include "CommonEth.h" +#include "Common.h" #include "BlockInfo.h" #define FAKE_DAGGER 1 diff --git a/libethereum/Precompiled.cpp b/libethereum/Precompiled.cpp index 6e0d4756c..62d159418 100644 --- a/libethereum/Precompiled.cpp +++ b/libethereum/Precompiled.cpp @@ -23,7 +23,7 @@ #include #include -#include +#include #include using namespace std; using namespace dev; diff --git a/rlp/base64.cpp b/rlp/base64.cpp deleted file mode 100644 index 5e2b32000..000000000 --- a/rlp/base64.cpp +++ /dev/null @@ -1,128 +0,0 @@ -/* - base64.cpp and base64.h - - Copyright (C) 2004-2008 René Nyffenegger - - This source code is provided 'as-is', without any express or implied - warranty. In no event will the author be held liable for any damages - arising from the use of this software. - - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: - - 1. The origin of this source code must not be misrepresented; you must not - claim that you wrote the original source code. If you use this source code - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original source code. - - 3. This notice may not be removed or altered from any source distribution. - - René Nyffenegger rene.nyffenegger@adp-gmbh.ch -*/ -/// code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c -/// originally by René Nyffenegger, modified by some other guy and then again by Gav Wood. - -#include "base64.h" - -#include - -using namespace std; -using namespace dev; - -static const std::string base64_chars = - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789+/"; - -static inline bool is_base64(byte c) { - return (isalnum(c) || (c == '+') || (c == '/')); -} - -std::string dev::base64_encode(bytesConstRef _in) { - std::string ret; - int i = 0; - int j = 0; - byte char_array_3[3]; - byte char_array_4[4]; - - auto buf = _in.data(); - auto bufLen = _in.size(); - - while (bufLen--) { - char_array_3[i++] = *(buf++); - if (i == 3) { - char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; - char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); - char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); - char_array_4[3] = char_array_3[2] & 0x3f; - - for(i = 0; (i <4) ; i++) - ret += base64_chars[char_array_4[i]]; - i = 0; - } - } - - if (i) - { - for(j = i; j < 3; j++) - char_array_3[j] = '\0'; - - char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; - char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); - char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); - char_array_4[3] = char_array_3[2] & 0x3f; - - for (j = 0; (j < i + 1); j++) - ret += base64_chars[char_array_4[j]]; - - while((i++ < 3)) - ret += '='; - } - - return ret; -} - -bytes dev::base64_decode(std::string const& encoded_string) { - int in_len = encoded_string.size(); - int i = 0; - int j = 0; - int in_ = 0; - byte char_array_4[4], char_array_3[3]; - bytes ret; - - while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) { - char_array_4[i++] = encoded_string[in_]; in_++; - if (i ==4) { - for (i = 0; i <4; i++) - char_array_4[i] = base64_chars.find(char_array_4[i]); - - char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); - char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); - char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; - - for (i = 0; (i < 3); i++) - ret.push_back(char_array_3[i]); - i = 0; - } - } - - if (i) { - for (j = i; j <4; j++) - char_array_4[j] = 0; - - for (j = 0; j <4; j++) - char_array_4[j] = base64_chars.find(char_array_4[j]); - - char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); - char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); - char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; - - for (j = 0; (j < i - 1); j++) ret.push_back(char_array_3[j]); - } - - return ret; -} diff --git a/rlp/base64.h b/rlp/base64.h deleted file mode 100644 index 53ba282c8..000000000 --- a/rlp/base64.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - base64.cpp and base64.h - - Copyright (C) 2004-2008 René Nyffenegger - - This source code is provided 'as-is', without any express or implied - warranty. In no event will the author be held liable for any damages - arising from the use of this software. - - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: - - 1. The origin of this source code must not be misrepresented; you must not - claim that you wrote the original source code. If you use this source code - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original source code. - - 3. This notice may not be removed or altered from any source distribution. - - René Nyffenegger rene.nyffenegger@adp-gmbh.ch -*/ -/// code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c -/// originally by René Nyffenegger, modified by some other guy and then again by Gav Wood. -#pragma once - -#include -#include -#include - -namespace dev -{ - -std::string base64_encode(bytesConstRef _in); -bytes base64_decode(std::string const& _in); - -} diff --git a/rlp/main.cpp b/rlp/main.cpp index c3c04535b..85c08e0e6 100644 --- a/rlp/main.cpp +++ b/rlp/main.cpp @@ -22,11 +22,9 @@ #include #include #include -#include #include #include #include -#include "base64.h" using namespace std; using namespace dev; @@ -218,7 +216,7 @@ int main(int argc, char** argv) boost::algorithm::replace_all(s, " ", ""); boost::algorithm::replace_all(s, "\n", ""); boost::algorithm::replace_all(s, "\t", ""); - b = base64_decode(s); + b = fromBase64(s); break; } default: @@ -228,60 +226,60 @@ int main(int argc, char** argv) try { - RLP rlp(b); - switch (mode) - { - case Mode::ListArchive: - { - if (!rlp.isList()) + RLP rlp(b); + switch (mode) { - cout << "Error: Invalid format; RLP data is not a list." << endl; - exit(1); - } - cout << rlp.itemCount() << " items:" << endl; - for (auto i: rlp) + case Mode::ListArchive: { - if (!i.isData()) + if (!rlp.isList()) { - cout << "Error: Invalid format; RLP list item is not data." << endl; - if (!lenience) - exit(1); + cout << "Error: Invalid format; RLP data is not a list." << endl; + exit(1); } - cout << " " << i.size() << " bytes: " << sha3(i.data()) << endl; - } - break; - } - case Mode::ExtractArchive: - { - if (!rlp.isList()) - { - cout << "Error: Invalid format; RLP data is not a list." << endl; - exit(1); + cout << rlp.itemCount() << " items:" << endl; + for (auto i: rlp) + { + if (!i.isData()) + { + cout << "Error: Invalid format; RLP list item is not data." << endl; + if (!lenience) + exit(1); + } + cout << " " << i.size() << " bytes: " << sha3(i.data()) << endl; + } + break; } - cout << rlp.itemCount() << " items:" << endl; - for (auto i: rlp) + case Mode::ExtractArchive: { - if (!i.isData()) + if (!rlp.isList()) { - cout << "Error: Invalid format; RLP list item is not data." << endl; - if (!lenience) - exit(1); + cout << "Error: Invalid format; RLP data is not a list." << endl; + exit(1); } - ofstream fout; - fout.open(toString(sha3(i.data()))); - fout.write(reinterpret_cast(i.data().data()), i.data().size()); + cout << rlp.itemCount() << " items:" << endl; + for (auto i: rlp) + { + if (!i.isData()) + { + cout << "Error: Invalid format; RLP list item is not data." << endl; + if (!lenience) + exit(1); + } + ofstream fout; + fout.open(toString(sha3(i.data()))); + fout.write(reinterpret_cast(i.data().data()), i.data().size()); + } + break; + } + case Mode::Render: + { + RLPStreamer s(cout, prefs); + s.output(rlp); + cout << endl; + break; + } + default:; } - break; - } - case Mode::Render: - { - RLPStreamer s(cout, prefs); - s.output(rlp); - cout << endl; - break; - } - default:; - } } catch (...) {