Browse Source

Merge branch 'xcthulhu-quick_check_against_dos' into develop

cl-refactor
Gav Wood 10 years ago
parent
commit
78656047e5
  1. 2
      libdevcore/FixedHash.h
  2. 18
      libethash/ethash.h
  3. 6
      libethash/internal.h
  4. 21
      libethcore/Ethasher.cpp

2
libdevcore/FixedHash.h

@ -87,6 +87,8 @@ public:
bool operator!=(FixedHash const& _c) const { return m_data != _c.m_data; } bool operator!=(FixedHash const& _c) const { return m_data != _c.m_data; }
bool operator<(FixedHash const& _c) const { for (unsigned i = 0; i < N; ++i) if (m_data[i] < _c.m_data[i]) return true; else if (m_data[i] > _c.m_data[i]) return false; return false; } bool operator<(FixedHash const& _c) const { for (unsigned i = 0; i < N; ++i) if (m_data[i] < _c.m_data[i]) return true; else if (m_data[i] > _c.m_data[i]) return false; return false; }
bool operator>=(FixedHash const& _c) const { return !operator<(_c); } bool operator>=(FixedHash const& _c) const { return !operator<(_c); }
bool operator<=(FixedHash const& _c) const { return operator==(_c) || operator<(_c); }
bool operator>(FixedHash const& _c) const { return !operator<=(_c); }
// The obvious binary operators. // The obvious binary operators.
FixedHash& operator^=(FixedHash const& _c) { for (unsigned i = 0; i < N; ++i) m_data[i] ^= _c.m_data[i]; return *this; } FixedHash& operator^=(FixedHash const& _c) { for (unsigned i = 0; i < N; ++i) m_data[i] ^= _c.m_data[i]; return *this; }

18
libethash/ethash.h

@ -25,6 +25,17 @@
#include <stddef.h> #include <stddef.h>
#include "compiler.h" #include "compiler.h"
#define ETHASH_REVISION 19
#define ETHASH_DAGSIZE_BYTES_INIT 1073741824U // 2**30
#define ETHASH_DAG_GROWTH 113000000U
#define ETHASH_EPOCH_LENGTH 30000U
#define ETHASH_MIX_BYTES 128
#define ETHASH_DAG_PARENTS 256
#define ETHASH_CACHE_ROUNDS 3
#define ETHASH_ACCESSES 64
// *********************
// TODO: Remove once other code moved over to compliant naming scheme.
#define REVISION 19 #define REVISION 19
#define DAGSIZE_BYTES_INIT 1073741824U // 2**30 #define DAGSIZE_BYTES_INIT 1073741824U // 2**30
#define DAG_GROWTH 113000000U #define DAG_GROWTH 113000000U
@ -33,6 +44,7 @@
#define DAG_PARENTS 256 #define DAG_PARENTS 256
#define CACHE_ROUNDS 3 #define CACHE_ROUNDS 3
#define ACCESSES 64 #define ACCESSES 64
// *********************
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -82,11 +94,11 @@ static inline int ethash_check_difficulty(
return 1; return 1;
} }
int ethash_quick_check_difficulty( void ethash_quick_hash(
uint8_t return_hash[32],
const uint8_t header_hash[32], const uint8_t header_hash[32],
const uint64_t nonce, const uint64_t nonce,
const uint8_t mix_hash[32], const uint8_t mix_hash[32]);
const uint8_t difficulty[32]);
#ifdef __cplusplus #ifdef __cplusplus
} }

6
libethash/internal.h

@ -37,12 +37,6 @@ void ethash_calculate_dag_item(
ethash_cache const *cache ethash_cache const *cache
); );
void ethash_quick_hash(
uint8_t return_hash[32],
const uint8_t header_hash[32],
const uint64_t nonce,
const uint8_t mix_hash[32]);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

21
libethcore/Ethasher.cpp

@ -43,6 +43,12 @@ Ethasher* dev::eth::Ethasher::s_this = nullptr;
bytes const& Ethasher::cache(BlockInfo const& _header) bytes const& Ethasher::cache(BlockInfo const& _header)
{ {
RecursiveGuard l(x_this); RecursiveGuard l(x_this);
if (_header.number > EPOCH_LENGTH*2048) {
std::ostringstream error;
error << "block number is too high; max is " << EPOCH_LENGTH*2048 << "(was " << _header.number << ")";
throw std::invalid_argument( error.str() );
}
if (!m_caches.count(_header.seedHash)) if (!m_caches.count(_header.seedHash))
{ {
ethash_params p = params((unsigned)_header.number); ethash_params p = params((unsigned)_header.number);
@ -94,7 +100,19 @@ ethash_params Ethasher::params(unsigned _n)
bool Ethasher::verify(BlockInfo const& _header) bool Ethasher::verify(BlockInfo const& _header)
{ {
bigint boundary = (bigint(1) << 256) / _header.difficulty; if (_header.number >= ETHASH_EPOCH_LENGTH * 2048)
return false;
h256 boundary = u256((bigint(1) << 256) / _header.difficulty);
uint8_t quickHashOut[32];
ethash_quick_hash(
quickHashOut,
_header.headerHash(WithoutNonce).data(),
(uint64_t)(u64)_header.nonce,
_header.mixHash.data()
);
h256 quickHashOut256 = h256(quickHashOut, h256::ConstructFromPointer);
if (quickHashOut256 > boundary)
return false;
auto e = eval(_header, _header.nonce); auto e = eval(_header, _header.nonce);
return (u256)e.value <= boundary && e.mixHash == _header.mixHash; return (u256)e.value <= boundary && e.mixHash == _header.mixHash;
} }
@ -106,4 +124,3 @@ Ethasher::Result Ethasher::eval(BlockInfo const& _header, Nonce const& _nonce)
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 Result{h256(r.result, h256::ConstructFromPointer), h256(r.mix_hash, h256::ConstructFromPointer)}; return Result{h256(r.result, h256::ConstructFromPointer), h256(r.mix_hash, h256::ConstructFromPointer)};
} }

Loading…
Cancel
Save