artur-zawlocki
10 years ago
152 changed files with 2984 additions and 1279 deletions
@ -0,0 +1,59 @@ |
|||
# Set necessary compile and link flags |
|||
|
|||
|
|||
# C++11 check and activation |
|||
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") |
|||
execute_process( |
|||
COMMAND ${CMAKE_CXX_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION) |
|||
if (NOT (GCC_VERSION VERSION_GREATER 4.7 OR GCC_VERSION VERSION_EQUAL 4.7)) |
|||
message(FATAL_ERROR "${PROJECT_NAME} requires g++ 4.7 or greater.") |
|||
endif () |
|||
elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") |
|||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") |
|||
else () |
|||
message(FATAL_ERROR "Your C++ compiler does not support C++11.") |
|||
endif () |
|||
|
|||
|
|||
|
|||
# Initialize CXXFLAGS |
|||
set(CMAKE_CXX_FLAGS "-std=c++11 -Wall -Wno-unknown-pragmas -Wextra -DSHAREDLIB") |
|||
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -DETH_DEBUG") |
|||
set(CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG -DETH_RELEASE") |
|||
set(CMAKE_CXX_FLAGS_RELEASE "-O4 -DNDEBUG -DETH_RELEASE") |
|||
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g -DETH_DEBUG") |
|||
|
|||
# Windows |
|||
if ("${TARGET_PLATFORM}" STREQUAL "w64") |
|||
set(CMAKE_SYSTEM_NAME Windows) |
|||
|
|||
set(CMAKE_CXX_LIBRARY_ARCHITECTURE x86_64-w64-mingw32) |
|||
set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc) |
|||
set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++) |
|||
set(CMAKE_RC_COMPILER x86_64-w64-mingw32-windres) |
|||
set(CMAKE_AR x86_64-w64-mingw32-ar) |
|||
set(CMAKE_RANLIB x86_64-w64-mingw32-ranlib) |
|||
|
|||
set(CMAKE_EXECUTABLE_SUFFIX .exe) |
|||
|
|||
set(CMAKE_FIND_ROOT_PATH |
|||
/usr/x86_64-w64-mingw32 |
|||
) |
|||
|
|||
include_directories(/usr/x86_64-w64-mingw32/include/cryptopp) |
|||
|
|||
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) |
|||
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) |
|||
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) |
|||
|
|||
set(CMAKE_INSTALL_PREFIX /usr/x86_64-w64-mingw32) |
|||
set(ETH_BUILD_PLATFORM "windows") |
|||
set(ETH_STATIC 1) |
|||
else () |
|||
set(ETH_BUILD_PLATFORM ${CMAKE_SYSTEM_NAME}) |
|||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") |
|||
set(ETH_SHARED 1) |
|||
endif() |
|||
|
|||
|
|||
|
@ -0,0 +1,208 @@ |
|||
# search for and configure dependencies |
|||
|
|||
# deprecated. TODO will rewrite to proper CMake packages |
|||
|
|||
|
|||
|
|||
if("${TARGET_PLATFORM}" STREQUAL "w64") |
|||
# set(MINIUPNPC_LS /usr/x86_64-w64-mingw32/lib/libminiupnpc.a) |
|||
set(LEVELDB_LS leveldb) |
|||
set(CRYPTOPP_LS cryptopp) |
|||
set(CRYPTOPP_ID /usr/x86_64-w64-mingw32/include/cryptopp) |
|||
else() |
|||
# Look for available Crypto++ version and if it is >= 5.6.2 |
|||
find_path(ID cryptlib.h |
|||
../cryptopp/src |
|||
../../cryptopp/src |
|||
/usr/include/cryptopp |
|||
/usr/include/crypto++ |
|||
/usr/local/include/cryptopp |
|||
/usr/local/include/crypto++ |
|||
/opt/local/include/cryptopp |
|||
/opt/local/include/crypto++ |
|||
) |
|||
find_library(LS NAMES cryptoppeth cryptopp |
|||
../cryptopp/src/../target/build/release |
|||
../../cryptopp/src/../target/build/release |
|||
PATHS |
|||
/usr/lib |
|||
/usr/local/lib |
|||
/opt/local/lib |
|||
) |
|||
|
|||
if (ID AND LS) |
|||
message(STATUS "Found Crypto++: ${ID}, ${LS}") |
|||
set(_CRYPTOPP_VERSION_HEADER ${ID}/config.h) |
|||
if(EXISTS ${_CRYPTOPP_VERSION_HEADER}) |
|||
file(STRINGS ${_CRYPTOPP_VERSION_HEADER} _CRYPTOPP_VERSION REGEX "^#define CRYPTOPP_VERSION[ \t]+[0-9]+$") |
|||
string(REGEX REPLACE "^#define CRYPTOPP_VERSION[ \t]+([0-9]+)" "\\1" _CRYPTOPP_VERSION ${_CRYPTOPP_VERSION}) |
|||
if(${_CRYPTOPP_VERSION} LESS 562) |
|||
message(FATAL_ERROR "Crypto++ version found is smaller than 5.6.2.") |
|||
else() |
|||
set(CRYPTOPP_ID ${ID} CACHE FILEPATH "") |
|||
set(CRYPTOPP_LS ${LS} CACHE FILEPATH "") |
|||
message(STATUS "Crypto++ found and version greater or equal to 5.6.2") |
|||
endif() |
|||
endif() |
|||
else() |
|||
message(STATUS "Crypto++ Not Found: ${CRYPTOPP_ID}, ${CRYPTOPP_LS}") |
|||
endif() |
|||
|
|||
find_path( LEVELDB_ID leveldb/db.h |
|||
/usr/include |
|||
/usr/local/include |
|||
) |
|||
if ( LEVELDB_ID STREQUAL "LEVELDB_ID-NOTFOUND" ) |
|||
message(FATAL_ERROR "Failed to find the LevelDB headers") |
|||
else () |
|||
message(STATUS "Found LevelDB Headers") |
|||
|
|||
# Check for accessory dev libraries leveldb and miniupnpc |
|||
find_library( LEVELDB_LS NAMES leveldb |
|||
PATHS |
|||
/usr/lib |
|||
/usr/local/lib |
|||
/opt/local/lib |
|||
/usr/lib/*/ |
|||
) |
|||
if ( LEVELDB_LS STREQUAL "LEVELDB_LS-NOTFOUND" ) |
|||
message(FATAL_ERROR "Failed to find the LevelDB Library!") |
|||
else () |
|||
message(STATUS "Found LevelDB Library: ${LEVELDB_LS}") |
|||
add_definitions(-DETH_LEVELDB) |
|||
endif () |
|||
endif () |
|||
|
|||
find_path( PYTHON_ID pyconfig.h |
|||
${PYTHON_INCLUDE_DIR} |
|||
/usr/include/python2.7 |
|||
/usr/local/include/python2.7 |
|||
) |
|||
if ( PYTHON_ID STREQUAL "PYTHON_ID-NOTFOUND" ) |
|||
message(STATUS "Failed to find the Python-2.7 headers") |
|||
else () |
|||
message(STATUS "Found Python-2.7 Headers: ${PYTHON_ID}") |
|||
|
|||
# Check for accessory dev libraries leveldb and miniupnpc |
|||
find_library( PYTHON_LS NAMES python2.7 |
|||
PATHS |
|||
/usr/lib |
|||
/usr/local/lib |
|||
/opt/local/lib |
|||
/usr/lib/*/ |
|||
) |
|||
if ( PYTHON_LS STREQUAL "PYTHON_LS-NOTFOUND" ) |
|||
message(STATUS "Failed to find the Python-2.7 Library!") |
|||
set(PYTHON_ID) |
|||
set(PYTHON_LS) |
|||
else () |
|||
message(STATUS "Found Python-2.7 Library: ${PYTHON_LS}") |
|||
add_definitions(-DETH_PYTHON) |
|||
endif () |
|||
endif () |
|||
|
|||
find_path( MINIUPNPC_ID miniupnpc/miniwget.h |
|||
/usr/include |
|||
/usr/local/include |
|||
) |
|||
if ( MINIUPNPC_ID ) |
|||
message(STATUS "Found miniupnpc headers") |
|||
|
|||
find_library( MINIUPNPC_LS NAMES miniupnpc |
|||
PATHS |
|||
/usr/lib |
|||
/usr/local/lib |
|||
/opt/local/lib |
|||
/usr/lib/*/ |
|||
) |
|||
if ( MINIUPNPC_LS ) |
|||
message(STATUS "Found miniupnpc library: ${MINIUPNPC_LS}") |
|||
add_definitions(-DETH_MINIUPNPC) |
|||
else () |
|||
message(STATUS "Failed to find the miniupnpc library!") |
|||
endif () |
|||
else () |
|||
message(STATUS "Failed to find the miniupnpc headers!") |
|||
endif () |
|||
|
|||
find_path( JSONRPC_ID jsonrpc/rpc.h |
|||
/usr/include |
|||
/usr/local/include |
|||
) |
|||
if ( JSONRPC_ID ) |
|||
message(STATUS "Found jsonrpc headers") |
|||
find_library( JSONRPC_LS NAMES jsonrpc |
|||
PATHS |
|||
/usr/lib |
|||
/usr/local/lib |
|||
/opt/local/lib |
|||
/usr/lib/*/ |
|||
) |
|||
if ( JSONRPC_LS ) |
|||
message(STATUS "Found jsonrpc library: ${JSONRPC_LS}") |
|||
add_definitions(-DETH_JSONRPC) |
|||
else () |
|||
message(STATUS "Failed to find the jsonrpc library!") |
|||
endif () |
|||
else () |
|||
message(STATUS "Failed to find the jsonrpc headers!") |
|||
endif () |
|||
|
|||
find_path( READLINE_ID readline/readline.h |
|||
/usr/include |
|||
/usr/local/include |
|||
) |
|||
if ( READLINE_ID ) |
|||
message(STATUS "Found readline headers") |
|||
find_library( READLINE_LS NAMES readline |
|||
PATHS |
|||
/usr/lib |
|||
/usr/local/lib |
|||
/opt/local/lib |
|||
/usr/lib/*/ |
|||
) |
|||
if ( READLINE_LS ) |
|||
message(STATUS "Found readline library: ${READLINE_LS}") |
|||
add_definitions(-DETH_READLINE) |
|||
else () |
|||
message(STATUS "Failed to find the readline library!") |
|||
endif () |
|||
else () |
|||
message(STATUS "Failed to find the readline headers!") |
|||
endif () |
|||
|
|||
if (LANGUAGES) |
|||
find_package(Boost 1.53 REQUIRED COMPONENTS thread date_time) |
|||
else() |
|||
find_package(Boost 1.53 REQUIRED COMPONENTS thread date_time system regex) |
|||
endif() |
|||
|
|||
set(QTQML 1) |
|||
endif() |
|||
|
|||
if(CRYPTOPP_ID) |
|||
include_directories(${CRYPTOPP_ID}) |
|||
endif() |
|||
if(PYTHON_ID) |
|||
include_directories(${PYTHON_ID}) |
|||
endif() |
|||
if(MINIUPNPC_ID) |
|||
include_directories(${MINIUPNPC_ID}) |
|||
endif() |
|||
if(LEVELDB_ID) |
|||
include_directories(${LEVELDB_ID}) |
|||
endif() |
|||
if(READLINE_ID) |
|||
include_directories(${READLINE_ID}) |
|||
endif() |
|||
if(JSONRPC_ID) |
|||
include_directories(${JSONRPC_ID}) |
|||
endif() |
|||
|
|||
|
|||
|
|||
|
|||
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") |
|||
link_directories(/usr/local/lib) |
|||
include_directories(/usr/local/include) |
|||
endif() |
@ -1 +0,0 @@ |
|||
600160805460006080530b6016596003608054601b586002608054 |
@ -1,21 +0,0 @@ |
|||
.code: |
|||
PUSH 1 |
|||
PUSH 128 |
|||
MSTORE |
|||
PUSH 0 |
|||
PUSH 128 |
|||
MLOAD |
|||
GT |
|||
PUSH [tag0] |
|||
JUMPI |
|||
PUSH 3 |
|||
PUSH 128 |
|||
MSTORE |
|||
PUSH [tag1] |
|||
JUMP |
|||
tag0: |
|||
PUSH 2 |
|||
PUSH 128 |
|||
MSTORE |
|||
tag1: |
|||
|
@ -1,5 +0,0 @@ |
|||
{ |
|||
[i] 1 |
|||
|
|||
( if (> @i 0) [i] 2 [i] 3 ) |
|||
} |
@ -0,0 +1,121 @@ |
|||
/* Copyright (c) 2013, Scott Tsai
|
|||
* |
|||
* All rights reserved. |
|||
* |
|||
* Redistribution and use in source and binary forms, with or without |
|||
* modification, are permitted provided that the following conditions are met: |
|||
* |
|||
* 1. Redistributions of source code must retain the above copyright notice, |
|||
* this list of conditions and the following disclaimer. |
|||
* 2. Redistributions in binary form must reproduce the above copyright notice, |
|||
* this list of conditions and the following disclaimer in the documentation |
|||
* and/or other materials provided with the distribution. |
|||
* |
|||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|||
* POSSIBILITY OF SUCH DAMAGE. |
|||
*/ |
|||
|
|||
#ifndef DEBUG_BREAK_H |
|||
#define DEBUG_BREAK_H |
|||
|
|||
#ifdef _MSC_VER |
|||
|
|||
#define debug_break __debugbreak |
|||
|
|||
#else |
|||
|
|||
#include <signal.h> |
|||
#include <unistd.h> |
|||
#include <sys/syscall.h> |
|||
|
|||
#ifdef __cplusplus |
|||
extern "C" { |
|||
#endif |
|||
|
|||
enum { |
|||
/* gcc optimizers consider code after __builtin_trap() dead.
|
|||
* Making __builtin_trap() unsuitable for breaking into the debugger */ |
|||
DEBUG_BREAK_PREFER_BUILTIN_TRAP_TO_SIGTRAP = 0, |
|||
}; |
|||
|
|||
#if defined(__i386__) || defined(__x86_64__) |
|||
enum { HAVE_TRAP_INSTRUCTION = 1, }; |
|||
__attribute__((gnu_inline, always_inline)) |
|||
static void __inline__ trap_instruction(void) |
|||
{ |
|||
__asm__ volatile("int $0x03"); |
|||
} |
|||
#elif defined(__thumb__) |
|||
enum { HAVE_TRAP_INSTRUCTION = 1, }; |
|||
/* FIXME: handle __THUMB_INTERWORK__ */ |
|||
__attribute__((gnu_inline, always_inline)) |
|||
static void __inline__ trap_instruction(void) |
|||
{ |
|||
/* See 'arm-linux-tdep.c' in GDB source.
|
|||
* Both instruction sequences below works. */ |
|||
#if 1 |
|||
/* 'eabi_linux_thumb_le_breakpoint' */ |
|||
__asm__ volatile(".inst 0xde01"); |
|||
#else |
|||
/* 'eabi_linux_thumb2_le_breakpoint' */ |
|||
__asm__ volatile(".inst.w 0xf7f0a000"); |
|||
#endif |
|||
|
|||
/* Known problem:
|
|||
* After a breakpoint hit, can't stepi, step, or continue in GDB. |
|||
* 'step' stuck on the same instruction. |
|||
* |
|||
* Workaround: a new GDB command, |
|||
* 'debugbreak-step' is defined in debugbreak-gdb.py |
|||
* that does: |
|||
* (gdb) set $instruction_len = 2 |
|||
* (gdb) tbreak *($pc + $instruction_len) |
|||
* (gdb) jump *($pc + $instruction_len) |
|||
*/ |
|||
} |
|||
#elif defined(__arm__) && !defined(__thumb__) |
|||
enum { HAVE_TRAP_INSTRUCTION = 1, }; |
|||
__attribute__((gnu_inline, always_inline)) |
|||
static void __inline__ trap_instruction(void) |
|||
{ |
|||
/* See 'arm-linux-tdep.c' in GDB source,
|
|||
* 'eabi_linux_arm_le_breakpoint' */ |
|||
__asm__ volatile(".inst 0xe7f001f0"); |
|||
/* Has same known problem and workaround
|
|||
* as Thumb mode */ |
|||
} |
|||
#else |
|||
enum { HAVE_TRAP_INSTRUCTION = 0, }; |
|||
#endif |
|||
|
|||
__attribute__((gnu_inline, always_inline)) |
|||
static void __inline__ debug_break(void) |
|||
{ |
|||
if (HAVE_TRAP_INSTRUCTION) { |
|||
trap_instruction(); |
|||
} else if (DEBUG_BREAK_PREFER_BUILTIN_TRAP_TO_SIGTRAP) { |
|||
/* raises SIGILL on Linux x86{,-64}, to continue in gdb:
|
|||
* (gdb) handle SIGILL stop nopass |
|||
* */ |
|||
__builtin_trap(); |
|||
} else { |
|||
raise(SIGTRAP); |
|||
} |
|||
} |
|||
|
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
|||
|
|||
#endif |
|||
|
|||
#endif |
@ -1,84 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file Dagger.h
|
|||
* @author Gav Wood <i@gavwood.com> |
|||
* @date 2014 |
|||
* |
|||
* Dagger algorithm. Or not. |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <libdevcrypto/SHA3.h> |
|||
#include "CommonEth.h" |
|||
|
|||
#define FAKE_DAGGER 1 |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
struct MineInfo |
|||
{ |
|||
void combine(MineInfo const& _m) { requirement = std::max(requirement, _m.requirement); best = std::min(best, _m.best); hashes += _m.hashes; completed = completed || _m.completed; } |
|||
double requirement = 0; |
|||
double best = 1e99; |
|||
unsigned hashes = 0; |
|||
bool completed = false; |
|||
}; |
|||
|
|||
#if FAKE_DAGGER |
|||
|
|||
class Dagger |
|||
{ |
|||
public: |
|||
static h256 eval(h256 const& _root, h256 const& _nonce) { h256 b[2] = { _root, _nonce }; return sha3(bytesConstRef((byte const*)&b[0], 64)); } |
|||
static bool verify(h256 const& _root, h256 const& _nonce, u256 const& _difficulty) { return (bigint)(u256)eval(_root, _nonce) <= (bigint(1) << 256) / _difficulty; } |
|||
|
|||
MineInfo mine(h256& o_solution, h256 const& _root, u256 const& _difficulty, unsigned _msTimeout = 100, bool _continue = true, bool _turbo = false); |
|||
|
|||
h256 m_last; |
|||
}; |
|||
|
|||
#else |
|||
|
|||
/// Functions are not re-entrant. If you want to multi-thread, then use different classes for each thread.
|
|||
class Dagger |
|||
{ |
|||
public: |
|||
Dagger(); |
|||
~Dagger(); |
|||
|
|||
static u256 bound(u256 const& _difficulty); |
|||
static h256 eval(h256 const& _root, u256 const& _nonce); |
|||
static bool verify(h256 const& _root, u256 const& _nonce, u256 const& _difficulty); |
|||
|
|||
bool mine(u256& o_solution, h256 const& _root, u256 const& _difficulty, unsigned _msTimeout = 100, bool const& _continue = bool(true)); |
|||
|
|||
private: |
|||
|
|||
static h256 node(h256 const& _root, h256 const& _xn, uint_fast32_t _L, uint_fast32_t _i); |
|||
|
|||
h256 m_root; |
|||
u256 m_nonce; |
|||
}; |
|||
|
|||
#endif |
|||
|
|||
} |
|||
} |
@ -0,0 +1,117 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file ProofOfWork.h
|
|||
* @author Gav Wood <i@gavwood.com> |
|||
* @date 2014 |
|||
* |
|||
* ProofOfWork algorithm. Or not. |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <chrono> |
|||
#include <thread> |
|||
#include <cstdint> |
|||
#include <libdevcrypto/SHA3.h> |
|||
#include "CommonEth.h" |
|||
|
|||
#define FAKE_DAGGER 1 |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
struct MineInfo |
|||
{ |
|||
void combine(MineInfo const& _m) { requirement = std::max(requirement, _m.requirement); best = std::min(best, _m.best); hashes += _m.hashes; completed = completed || _m.completed; } |
|||
double requirement = 0; |
|||
double best = 1e99; |
|||
unsigned hashes = 0; |
|||
bool completed = false; |
|||
}; |
|||
|
|||
template <class Evaluator> |
|||
class ProofOfWorkEngine: public Evaluator |
|||
{ |
|||
public: |
|||
static bool verify(h256 const& _root, h256 const& _nonce, u256 const& _difficulty) { return (bigint)(u256)Evaluator::eval(_root, _nonce) <= (bigint(1) << 256) / _difficulty; } |
|||
|
|||
inline MineInfo mine(h256& o_solution, h256 const& _root, u256 const& _difficulty, unsigned _msTimeout = 100, bool _continue = true, bool _turbo = false); |
|||
|
|||
protected: |
|||
h256 m_last; |
|||
}; |
|||
|
|||
class SHA3Evaluator |
|||
{ |
|||
public: |
|||
static h256 eval(h256 const& _root, h256 const& _nonce) { h256 b[2] = { _root, _nonce }; return sha3(bytesConstRef((byte const*)&b[0], 64)); } |
|||
}; |
|||
|
|||
// TODO: class ARPoWEvaluator
|
|||
|
|||
class DaggerEvaluator |
|||
{ |
|||
public: |
|||
static h256 eval(h256 const& _root, h256 const& _nonce); |
|||
|
|||
private: |
|||
static h256 node(h256 const& _root, h256 const& _xn, uint_fast32_t _L, uint_fast32_t _i); |
|||
}; |
|||
|
|||
using SHA3ProofOfWork = ProofOfWorkEngine<SHA3Evaluator>; |
|||
|
|||
using ProofOfWork = SHA3ProofOfWork; |
|||
|
|||
template <class Evaluator> |
|||
MineInfo ProofOfWorkEngine<Evaluator>::mine(h256& o_solution, h256 const& _root, u256 const& _difficulty, unsigned _msTimeout, bool _continue, bool _turbo) |
|||
{ |
|||
MineInfo ret; |
|||
static std::mt19937_64 s_eng((time(0) + (unsigned)m_last)); |
|||
u256 s = (m_last = h256::random(s_eng)); |
|||
|
|||
bigint d = (bigint(1) << 256) / _difficulty; |
|||
ret.requirement = log2((double)d); |
|||
|
|||
// 2^ 0 32 64 128 256
|
|||
// [--------*-------------------------]
|
|||
//
|
|||
// evaluate until we run out of time
|
|||
auto startTime = std::chrono::steady_clock::now(); |
|||
if (!_turbo) |
|||
std::this_thread::sleep_for(std::chrono::milliseconds(_msTimeout * 90 / 100)); |
|||
for (; (std::chrono::steady_clock::now() - startTime) < std::chrono::milliseconds(_msTimeout) && _continue; s++, ret.hashes++) |
|||
{ |
|||
o_solution = (h256)s; |
|||
auto e = (bigint)(u256)Evaluator::eval(_root, o_solution); |
|||
ret.best = std::min<double>(ret.best, log2((double)e)); |
|||
if (e <= d) |
|||
{ |
|||
ret.completed = true; |
|||
break; |
|||
} |
|||
} |
|||
|
|||
if (ret.completed) |
|||
assert(verify(_root, o_solution, _difficulty)); |
|||
|
|||
return ret; |
|||
} |
|||
|
|||
} |
|||
} |
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue