Compare commits
189 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9a5be0a321 | |||
| c203f6eb0a | |||
| b713f4e702 | |||
| 111087d96a | |||
| 9e7666d12f | |||
| cb147773b5 | |||
| d84bef8f14 | |||
| 32bb318af4 | |||
| 055b5e2256 | |||
| c1754d122f | |||
| 6d121122f8 | |||
| bfa8334aeb | |||
| 0e222afd6a | |||
| b191962d32 | |||
| 1cf13e142a | |||
| 8f1f81749d | |||
| 72d70eba16 | |||
| ae3612f595 | |||
| 37b6457054 | |||
| 9579be5c7d | |||
| d05d47fa75 | |||
| bd88c04e9e | |||
| eb3a3872eb | |||
| 5cea7be405 | |||
| 516c382fe4 | |||
| 710e55422a | |||
| 3406cf78b4 | |||
| 8fa354796c | |||
| 6c6ef1c1b8 | |||
| c3c326c680 | |||
| 92e20485b2 | |||
| edd782ac23 | |||
| a4d84e318d | |||
| 08ee599f79 | |||
| 134f2d68a3 | |||
| d23c46ff84 | |||
| 820c5db5e8 | |||
| 990916ab62 | |||
| d4e362cd76 | |||
| 1dd06cc509 | |||
| 2e2bd1d137 | |||
| 0818d91f4f | |||
| 6e1f34b168 | |||
| 1a35177633 | |||
| c45689b02b | |||
| 8f27d940e3 | |||
| 6b4640c413 | |||
| 8aebcdc580 | |||
| e23a2e77a8 | |||
| 0f9362e14e | |||
| ab974b9684 | |||
| 54acfee036 | |||
| a78ddb50d5 | |||
| b2f86a5f6b | |||
| 2a77695832 | |||
| 21d8cb917b | |||
| 2c70bf8616 | |||
| 7522111bb7 | |||
| 7b5b910f3f | |||
| afb52e4235 | |||
| 93cd9a659a | |||
| 9e287ab4f0 | |||
| 094209f311 | |||
| dee66c3f32 | |||
| 32533e3c96 | |||
| a073923a82 | |||
| 3484b24caa | |||
| 1b3c1eb850 | |||
| 7fe032915f | |||
| 5b9b73abcc | |||
| a2afa29052 | |||
| a2d3dbad5e | |||
| e50cf060cb | |||
| 915988d694 | |||
| 345c231a9a | |||
| a7aed2f221 | |||
| 292e2580e5 | |||
| 028a5d0c88 | |||
| 8b27faad6d | |||
| cdc3206ee8 | |||
| 0c2b7d6010 | |||
| ef15c3b54f | |||
| f4bcdc7fe9 | |||
| aada1bb5cc | |||
| 796850d8c5 | |||
| ab0bc0488e | |||
| f3af02111d | |||
| c21d052d7a | |||
| 62b1690780 | |||
| 52050bbcfb | |||
| 0db57e5047 | |||
| e7919514d4 | |||
| 7ab21c6afd | |||
| 2209e0cc70 | |||
| a73b4616a4 | |||
| 9593311332 | |||
| 9a09d15bea | |||
| f082488e4b | |||
| c56773d4d6 | |||
| 752e2381d6 | |||
| 03f829e18c | |||
| 06b66c99db | |||
| 0b0ad9ff82 | |||
| de9f13d393 | |||
| 272d206741 | |||
| c9493ec7fa | |||
| 5ef4134955 | |||
| 8045c40026 | |||
| d4a1b14820 | |||
| cc6ce4a366 | |||
| 25e5f80a81 | |||
| d4b339b1a3 | |||
| 4bdd524d84 | |||
| b78fe02405 | |||
| dfa0c7ffb1 | |||
| ec97a14da7 | |||
| 3a2e9e8b94 | |||
| 069c96aa1d | |||
| 42370bf11e | |||
| dc506bb1b9 | |||
| ec2f4467fb | |||
| 9f449320b5 | |||
| 8767ef9e19 | |||
| cf184295b4 | |||
| 0cd9876a46 | |||
| 1e4e4bd08e | |||
| a640e41d74 | |||
| f90e00edea | |||
| 38ffd73b54 | |||
| c10a5ce323 | |||
| e2931f94b5 | |||
| 95c268be4c | |||
| 4ac4f8f860 | |||
| 248fcee174 | |||
| 89bcceb986 | |||
| 2b01350add | |||
| f1b6212c82 | |||
| 7d961ab3b9 | |||
| 862e7d965b | |||
| da2d9f7e2e | |||
| d8bba4dc82 | |||
| 3a8a24a5ee | |||
| eaf29872cb | |||
| 74f553274f | |||
| d0f1e6eb2e | |||
| f9def6cb73 | |||
| 2f659c8ecf | |||
| 2d559ca764 | |||
| 1328ea6aef | |||
| 07cfb54bdc | |||
| 5dd8d11b72 | |||
| dbab191bfc | |||
| e44baad73c | |||
| bb7a5b4d6d | |||
| 64116643c2 | |||
| 1e4c6ef5f4 | |||
| 61edc6ed70 | |||
| d283f91eb0 | |||
| dff2413cae | |||
| 998c2ba72f | |||
| b56d027dd5 | |||
| eaed654012 | |||
| 981b161cc9 | |||
| f59e9171e8 | |||
| b45540ca08 | |||
| 806e1ca0a3 | |||
| 113e39817d | |||
| 0869326f39 | |||
| 75d62c0303 | |||
| da4686b88f | |||
| a008eac8c6 | |||
| 3d60ae8c32 | |||
| 88f346414a | |||
| 2071a24a9f | |||
| 1c0da4513f | |||
| 6a002cda46 | |||
| c21c9615a3 | |||
| c0c210664a | |||
| 6859d11445 | |||
| 111324b6e0 | |||
| 148b9dd294 | |||
| 04d18cdf1d | |||
| 352ad81a0a | |||
| d26902468d | |||
| 0382027d70 | |||
| 966b499e5c | |||
| 9885f82e9e | |||
| 32a58d1534 | |||
| bc341918f6 |
+80
-14
@@ -9,15 +9,20 @@ on:
|
||||
jobs:
|
||||
build-ubuntu:
|
||||
|
||||
runs-on: ${{ matrix.os }}
|
||||
runs-on: ${{ matrix.config.os }}
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, ubuntu-18.04]
|
||||
config:
|
||||
- {os: ubuntu-latest, c: gcc-11, cpp: g++-11}
|
||||
- {os: ubuntu-18.04, c: gcc, cpp: g++}
|
||||
|
||||
steps:
|
||||
- name: Install dependencies
|
||||
run: sudo apt update && sudo apt install git build-essential cmake libuv1-dev libzmq3-dev libsodium-dev libpgm-dev libnorm-dev libgss-dev
|
||||
run: |
|
||||
sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
|
||||
sudo apt update
|
||||
sudo apt install -y git build-essential cmake libuv1-dev libzmq3-dev libsodium-dev libpgm-dev libnorm-dev libgss-dev ${{ matrix.config.c }} ${{ matrix.config.cpp }}
|
||||
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v2
|
||||
@@ -28,7 +33,7 @@ jobs:
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ..
|
||||
cmake .. -DCMAKE_C_COMPILER=${{ matrix.config.c }} -DCMAKE_CXX_COMPILER=${{ matrix.config.cpp }}
|
||||
make -j2
|
||||
|
||||
- name: Build tests
|
||||
@@ -36,7 +41,7 @@ jobs:
|
||||
cd tests
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ..
|
||||
cmake .. -DCMAKE_C_COMPILER=${{ matrix.config.c }} -DCMAKE_CXX_COMPILER=${{ matrix.config.cpp }}
|
||||
make -j2
|
||||
|
||||
- name: Run tests
|
||||
@@ -45,7 +50,51 @@ jobs:
|
||||
- name: Archive binary
|
||||
uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: p2pool-${{ matrix.os }}
|
||||
name: p2pool-${{ matrix.config.os }}
|
||||
path: build/p2pool
|
||||
|
||||
build-ubuntu-aarch64:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
sudo apt update
|
||||
sudo apt install -y git build-essential cmake gcc-aarch64-linux-gnu g++-aarch64-linux-gnu binutils-aarch64-linux-gnu
|
||||
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
submodules: true
|
||||
|
||||
- name: Build libuv
|
||||
run: |
|
||||
cd external/src/libuv
|
||||
mkdir build
|
||||
cd build
|
||||
cmake .. -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc -DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++
|
||||
make -j$(nproc)
|
||||
|
||||
- name: Build libzmq
|
||||
run: |
|
||||
cd external/src/libzmq
|
||||
mkdir build
|
||||
cd build
|
||||
cmake .. -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc -DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++ -DWITH_TLS=OFF -DWITH_LIBSODIUM=OFF -DWITH_LIBBSD=OFF -DBUILD_TESTS=OFF
|
||||
make -j$(nproc)
|
||||
|
||||
- name: Build p2pool
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake .. -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc -DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++ -DCMAKE_STRIP=/usr/bin/aarch64-linux-gnu-strip -DSTATIC_LIBS=ON -DARCH_ID=aarch64
|
||||
make -j$(nproc)
|
||||
|
||||
- name: Archive binary
|
||||
uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: p2pool-ubuntu-latest-aarch64
|
||||
path: build/p2pool
|
||||
|
||||
build-windows-msys2:
|
||||
@@ -101,8 +150,9 @@ jobs:
|
||||
strategy:
|
||||
matrix:
|
||||
config:
|
||||
- {vs: Visual Studio 16 2019, os: 2019, msbuild: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\MSBuild\\Current\\Bin\\amd64\\"}
|
||||
- {vs: Visual Studio 17 2022, os: 2022, msbuild: "C:\\Program Files\\Microsoft Visual Studio\\2022\\Preview\\Msbuild\\Current\\Bin\\amd64\\"}
|
||||
- {vs: Visual Studio 16 2019, os: 2019, msbuild: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\MSBuild\\Current\\Bin\\amd64\\", rx: "ON"}
|
||||
- {vs: Visual Studio 17 2022, os: 2022, msbuild: "C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise\\Msbuild\\Current\\Bin\\amd64\\", rx: "ON"}
|
||||
- {vs: Visual Studio 17 2022, os: 2022, msbuild: "C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise\\Msbuild\\Current\\Bin\\amd64\\", rx: "OFF"}
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
@@ -117,7 +167,7 @@ jobs:
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake .. -G "${{ matrix.config.vs }}"
|
||||
cmake .. -G "${{ matrix.config.vs }}" -DWITH_RANDOMX=${{ matrix.config.rx }}
|
||||
& "${{ matrix.config.msbuild }}msbuild" /m /p:Configuration=Release p2pool.vcxproj
|
||||
|
||||
- name: Build tests
|
||||
@@ -136,7 +186,7 @@ jobs:
|
||||
- name: Archive binary
|
||||
uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: p2pool-msbuild-${{ matrix.config.os }}.exe
|
||||
name: p2pool-msbuild-${{ matrix.config.os }}-randomx-${{ matrix.config.rx }}.exe
|
||||
path: build/Release/p2pool.exe
|
||||
|
||||
build-macos:
|
||||
@@ -145,7 +195,7 @@ jobs:
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
os: [macos-latest, macos-11]
|
||||
os: [macos-10.15, macos-11]
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
@@ -154,13 +204,29 @@ jobs:
|
||||
submodules: recursive
|
||||
|
||||
- name: Install dependencies
|
||||
run: HOMEBREW_NO_AUTO_UPDATE=1 brew install cmake libuv zmq libpgm
|
||||
run: HOMEBREW_NO_AUTO_UPDATE=1 brew install cmake
|
||||
|
||||
- name: Build libuv
|
||||
run: |
|
||||
cd external/src/libuv
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ..
|
||||
make -j3
|
||||
|
||||
- name: Build libzmq
|
||||
run: |
|
||||
cd external/src/libzmq
|
||||
mkdir build
|
||||
cd build
|
||||
cmake .. -DWITH_TLS=OFF -DWITH_LIBSODIUM=OFF
|
||||
make -j3
|
||||
|
||||
- name: Build p2pool
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ..
|
||||
cmake .. -DSTATIC_BINARY=ON
|
||||
make -j3
|
||||
|
||||
- name: Build tests
|
||||
@@ -168,7 +234,7 @@ jobs:
|
||||
cd tests
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ..
|
||||
cmake .. -DSTATIC_LIBS=ON
|
||||
make -j3
|
||||
|
||||
- name: Run tests
|
||||
|
||||
@@ -12,14 +12,22 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Install cppcheck
|
||||
run: sudo apt update && sudo apt install cppcheck
|
||||
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
submodules: true
|
||||
|
||||
- name: Checkout cppcheck
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
repository: danmar/cppcheck
|
||||
path: cppcheck-main
|
||||
|
||||
- name: Build cppcheck
|
||||
run: |
|
||||
cd cppcheck-main
|
||||
make -j$(nproc) cppcheck
|
||||
|
||||
- name: Run cppcheck
|
||||
run: |
|
||||
cd cppcheck
|
||||
@@ -30,14 +38,22 @@ jobs:
|
||||
runs-on: windows-latest
|
||||
|
||||
steps:
|
||||
- name: Install cppcheck
|
||||
run: choco install cppcheck
|
||||
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
submodules: true
|
||||
|
||||
- name: Checkout cppcheck
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
repository: danmar/cppcheck
|
||||
path: cppcheck-main
|
||||
|
||||
- name: Build cppcheck
|
||||
run: |
|
||||
cd cppcheck-main
|
||||
& "C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise\\Msbuild\\Current\\Bin\\amd64\\msbuild.exe" /m /p:Configuration=Release /p:Platform=x64
|
||||
|
||||
- name: Setup cmake
|
||||
uses: lukka/get-cmake@latest
|
||||
|
||||
@@ -45,7 +61,7 @@ jobs:
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake .. -G "Visual Studio 16 2019"
|
||||
cmake .. -G "Visual Studio 17 2022"
|
||||
|
||||
- name: Run cppcheck
|
||||
run: |
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
# This workflow uses actions that are not certified by GitHub.
|
||||
# They are provided by a third-party and are governed by
|
||||
# separate terms of service, privacy policy, and support
|
||||
# documentation.
|
||||
#
|
||||
# Find more information at:
|
||||
# https://github.com/microsoft/msvc-code-analysis-action
|
||||
|
||||
name: Microsoft C++ Code Analysis
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
schedule:
|
||||
- cron: '40 10 * * 0'
|
||||
|
||||
env:
|
||||
# Path to the CMake build directory.
|
||||
build: '${{ github.workspace }}/build'
|
||||
config: 'Release'
|
||||
|
||||
jobs:
|
||||
analyze:
|
||||
name: Analyze
|
||||
runs-on: windows-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
submodules: true
|
||||
|
||||
- name: Configure CMake
|
||||
run: cmake -B ${{ env.build }} -DCMAKE_BUILD_TYPE=${{ env.config }}
|
||||
|
||||
# Build is not required unless generated source files are used
|
||||
# - name: Build CMake
|
||||
# run: cmake --build ${{ env.build }}
|
||||
|
||||
- name: Initialize MSVC Code Analysis
|
||||
uses: microsoft/msvc-code-analysis-action@47ecec99bf7ce0399ed85cbb5012b6f24eab5959
|
||||
# Provide a unique ID to access the sarif output path
|
||||
id: run-analysis
|
||||
with:
|
||||
cmakeBuildDirectory: ${{ env.build }}
|
||||
buildConfiguration: ${{ env.config }}
|
||||
# Ruleset file that will determine what checks will be run
|
||||
ruleset: NativeRecommendedRules.ruleset
|
||||
|
||||
# Upload SARIF file to GitHub Code Scanning Alerts
|
||||
- name: Upload SARIF to GitHub
|
||||
uses: github/codeql-action/upload-sarif@v1
|
||||
with:
|
||||
sarif_file: ${{ steps.run-analysis.outputs.sarif }}
|
||||
|
||||
# Upload SARIF file as an Artifact to download and view
|
||||
- name: Upload SARIF as an Artifact
|
||||
uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: sarif-file
|
||||
path: ${{ steps.run-analysis.outputs.sarif }}
|
||||
+3
-3
@@ -10,12 +10,12 @@
|
||||
[submodule "external/src/cppzmq"]
|
||||
path = external/src/cppzmq
|
||||
url = https://github.com/SChernykh/cppzmq
|
||||
[submodule "external/src/libsodium"]
|
||||
path = external/src/libsodium
|
||||
url = https://github.com/SChernykh/libsodium
|
||||
[submodule "external/src/libuv"]
|
||||
path = external/src/libuv
|
||||
url = https://github.com/SChernykh/libuv
|
||||
[submodule "external/src/libzmq"]
|
||||
path = external/src/libzmq
|
||||
url = https://github.com/SChernykh/libzmq
|
||||
[submodule "external/src/robin-hood-hashing"]
|
||||
path = external/src/robin-hood-hashing
|
||||
url = https://github.com/SChernykh/robin-hood-hashing
|
||||
|
||||
+55
-10
@@ -1,16 +1,21 @@
|
||||
cmake_minimum_required(VERSION 2.8.12)
|
||||
project(p2pool)
|
||||
|
||||
option(STATIC_LINUX_BINARY "Build static Linux binary" OFF)
|
||||
option(STATIC_BINARY "Build static binary" OFF)
|
||||
option(STATIC_LIBS "Link libuv and libzmq statically" OFF)
|
||||
option(WITH_RANDOMX "Include the RandomX library in the build. If this is turned off, p2pool will rely on monerod for verifying RandomX hashes" ON)
|
||||
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
|
||||
|
||||
if (${CMAKE_VERSION} VERSION_GREATER "3.5.2")
|
||||
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.6.0")
|
||||
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT p2pool)
|
||||
endif()
|
||||
|
||||
add_subdirectory(external/src/RandomX)
|
||||
set(LIBS randomx)
|
||||
if (WITH_RANDOMX)
|
||||
add_definitions(-DWITH_RANDOMX)
|
||||
add_subdirectory(external/src/RandomX)
|
||||
set(LIBS randomx)
|
||||
endif()
|
||||
|
||||
include(cmake/flags.cmake)
|
||||
|
||||
@@ -73,6 +78,11 @@ set(SOURCES
|
||||
src/zmq_reader.cpp
|
||||
)
|
||||
|
||||
if (WITH_RANDOMX)
|
||||
set(HEADERS ${HEADERS} src/miner.h)
|
||||
set(SOURCES ${SOURCES} src/miner.cpp)
|
||||
endif()
|
||||
|
||||
include_directories(src)
|
||||
include_directories(external/src)
|
||||
include_directories(external/src/cryptonote)
|
||||
@@ -80,13 +90,18 @@ include_directories(external/src/libuv/include)
|
||||
include_directories(external/src/cppzmq)
|
||||
include_directories(external/src/libzmq/include)
|
||||
include_directories(external/src/llhttp)
|
||||
include_directories(external/src/RandomX/src)
|
||||
if (WITH_RANDOMX)
|
||||
include_directories(external/src/RandomX/src)
|
||||
endif()
|
||||
include_directories(external/src/rapidjson/include)
|
||||
include_directories(external/src/robin-hood-hashing/src/include)
|
||||
|
||||
if (WIN32)
|
||||
set(LIBS ${LIBS} ws2_32 iphlpapi userenv psapi)
|
||||
elseif (CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
|
||||
set(LIBS ${LIBS} pthread)
|
||||
elseif (NOT APPLE)
|
||||
set(LIBS ${LIBS} pthread gss dl)
|
||||
set(LIBS ${LIBS} pthread dl)
|
||||
endif()
|
||||
|
||||
if (CMAKE_CXX_COMPILER_ID MATCHES MSVC)
|
||||
@@ -119,17 +134,47 @@ endif()
|
||||
|
||||
add_definitions(/DZMQ_STATIC)
|
||||
|
||||
include(CheckSymbolExists)
|
||||
|
||||
set(CMAKE_REQUIRED_FLAGS "${GENERAL_FLAGS}")
|
||||
check_symbol_exists(pthread_cancel pthread.h HAVE_PTHREAD_CANCEL)
|
||||
|
||||
if (HAVE_PTHREAD_CANCEL)
|
||||
add_definitions(/DHAVE_PTHREAD_CANCEL)
|
||||
endif()
|
||||
|
||||
include(CheckCXXSourceCompiles)
|
||||
|
||||
check_cxx_source_compiles("int main(){ return __builtin_clzll(1);}" HAVE_BUILTIN_CLZLL)
|
||||
check_cxx_source_compiles("#include <intrin.h>\n#pragma intrinsic(_BitScanReverse64)\nint main(){unsigned long r;_BitScanReverse64(&r,1);return r;}" HAVE_BITSCANREVERSE64)
|
||||
|
||||
if (HAVE_BUILTIN_CLZLL)
|
||||
add_definitions(/DHAVE_BUILTIN_CLZLL)
|
||||
endif()
|
||||
|
||||
if (HAVE_BITSCANREVERSE64)
|
||||
add_definitions(/DHAVE_BITSCANREVERSE64)
|
||||
endif()
|
||||
|
||||
add_executable(${CMAKE_PROJECT_NAME} ${HEADERS} ${SOURCES})
|
||||
|
||||
if (STATIC_LINUX_BINARY)
|
||||
if (STATIC_BINARY OR STATIC_LIBS)
|
||||
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_STRIP} ${CMAKE_PROJECT_NAME})
|
||||
|
||||
if (WITH_RANDOMX)
|
||||
set(STATIC_LIBS randomx)
|
||||
endif()
|
||||
|
||||
if (CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
|
||||
set(STATIC_LIBS ${STATIC_LIBS} pthread)
|
||||
elseif (NOT APPLE)
|
||||
set(STATIC_LIBS ${STATIC_LIBS} pthread dl)
|
||||
endif()
|
||||
|
||||
target_link_libraries(${CMAKE_PROJECT_NAME}
|
||||
"${CMAKE_SOURCE_DIR}/external/src/libsodium/src/libsodium/.libs/libsodium.a"
|
||||
"${CMAKE_SOURCE_DIR}/external/src/libzmq/build/lib/libzmq.a"
|
||||
"${CMAKE_SOURCE_DIR}/external/src/libuv/build/libuv_a.a"
|
||||
randomx
|
||||
pthread
|
||||
${STATIC_LIBS}
|
||||
)
|
||||
else()
|
||||
target_link_libraries(${CMAKE_PROJECT_NAME} debug ${ZMQ_LIBRARY_DEBUG} debug ${UV_LIBRARY_DEBUG} optimized ${ZMQ_LIBRARY} optimized ${UV_LIBRARY} ${LIBS})
|
||||
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
FROM ubuntu:latest
|
||||
|
||||
RUN set -e && \
|
||||
apt-get update -q -y --no-install-recommends && \
|
||||
DEBIAN_FRONTEND="noninteractive" apt-get install -q -y --no-install-recommends \
|
||||
git \
|
||||
build-essential \
|
||||
ca-certificates \
|
||||
cmake \
|
||||
libuv1-dev \
|
||||
libzmq3-dev \
|
||||
libsodium-dev \
|
||||
libpgm-dev \
|
||||
libnorm-dev \
|
||||
libgss-dev
|
||||
|
||||
ADD . /usr/src/p2pool
|
||||
WORKDIR /usr/src/p2pool
|
||||
RUN git submodule update --init --recursive && \
|
||||
mkdir build && \
|
||||
cd build && \
|
||||
cmake .. && \
|
||||
make -j$(nproc)
|
||||
|
||||
# ---
|
||||
|
||||
FROM ubuntu:latest
|
||||
COPY --from=0 /usr/src/p2pool/build/p2pool /
|
||||
|
||||
RUN set -e && \
|
||||
apt-get update -q -y --no-install-recommends && \
|
||||
DEBIAN_FRONTEND="noninteractive" apt-get install -q -y --no-install-recommends \
|
||||
libzmq5 \
|
||||
libuv1 \
|
||||
&& \
|
||||
apt-get clean
|
||||
|
||||
RUN groupadd -r p2pool -g 1000 && \
|
||||
useradd -u 1000 -r -g p2pool -s /sbin/nologin -c "p2pool user" p2pool
|
||||
RUN mkdir -p /home/p2pool/.p2pool && \
|
||||
chown p2pool.p2pool /home/p2pool /home/p2pool/.p2pool
|
||||
USER p2pool
|
||||
|
||||
EXPOSE 3333
|
||||
EXPOSE 37889
|
||||
|
||||
VOLUME /home/p2pool/.p2pool
|
||||
|
||||
WORKDIR /home/p2pool/.p2pool
|
||||
ENTRYPOINT ["/p2pool"]
|
||||
@@ -2,17 +2,30 @@
|
||||
|
||||
Decentralized pool for Monero mining.
|
||||
|
||||
Mining on the mainnet has started! You can check the pool status at https://p2pool.io/
|
||||
Pool status and monitoring pages can be found at https://p2pool.io/, https://p2pool.io/mini/ and https://p2pool.observer/
|
||||
|
||||
### Build status
|
||||
### Build Status
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||
<a href="https://scan.coverity.com/projects/schernykh-p2pool">
|
||||
<img alt="Coverity Scan Build Status"
|
||||
src="https://scan.coverity.com/projects/23659/badge.svg"/>
|
||||
</a>
|
||||
# Contents
|
||||
- [Pool mining vs Solo mining vs P2Pool mining](#pool-mining-vs-solo-mining-vs-p2pool-mining)
|
||||
- [Features](#features)
|
||||
- [How PPLNS works in P2Pool](#how-pplns-works-in-p2pool)
|
||||
- [Default P2Pool parameters](#default-p2pool-parameters)
|
||||
- [Monero version support](#monero-version-support)
|
||||
- [How to mine on P2Pool](#how-to-mine-on-p2pool)
|
||||
- [General Considerations](#general-considerations)
|
||||
- [GNU/Linux](#gnulinux)
|
||||
- [Windows](#windows)
|
||||
- [Build instructions](#build-instructions)
|
||||
- [Donations](#donations)
|
||||
|
||||
## Pool mining vs Solo mining vs P2Pool mining
|
||||
|
||||
@@ -22,7 +35,7 @@ Here's the comparison table of the different ways of mining. While pool mining i
|
||||
|-|-|-|-|-|-|-|-|
|
||||
|Centralized pool|Regular|0-3%|0.001-0.01 XMR|Yes|Less stable due to pool server outages|Pool admin controls your mined funds, what you mine and can execute network attacks|Only miner software is required
|
||||
|Solo|Rare|0%|0.6 XMR or more|No|As stable as your Monero node|100% under your control|Monero node + optional miner
|
||||
|**P2Pool**|Regular|0%|less than 0.0004 XMR|No|As stable as your Monero node|100% under your control|Monero node + P2Pool node + miner
|
||||
|**P2Pool**|Regular|0%|~0.0003 XMR|No|As stable as your Monero node|100% under your control|Monero node + P2Pool node + miner
|
||||
|
||||
## Features
|
||||
|
||||
@@ -32,7 +45,7 @@ Here's the comparison table of the different ways of mining. While pool mining i
|
||||
* PPLNS payout scheme
|
||||
* **0% fee**
|
||||
* **0 XMR payout fee**
|
||||
* **Less than 0.0004 XMR minimal payout**
|
||||
* **~0.0003 XMR minimal payout**
|
||||
* Fast block times, down to 1 second
|
||||
* Uncle blocks are supported to avoid orphans - all your shares will be accounted for!
|
||||
* Configurable PPLNS window size and block time
|
||||
@@ -49,13 +62,143 @@ First you need to find a pool share. This share will stay in PPLNS window for 21
|
||||
|
||||
* Block time: 10 seconds
|
||||
* PPLNS window: 2160 blocks (6 hours)
|
||||
* Minimum payout = Monero block reward/2160, currently ~0.0004 XMR
|
||||
* Minimum payout = Monero block reward/2160, ~0.0003 XMR
|
||||
|
||||
## Monero version support
|
||||
|
||||
Monero will undergo a network upgrade on July 16th, 2022 (block 2,668,888). In order to continue mining after that date, you must update both Monero and P2Pool software to the latest available versions as soon as they are released.
|
||||
|
||||
|Monero protocol version|Required Monero software version|Required P2Pool version
|
||||
|-|-|-|
|
||||
|v14 (active until July 16th, 2022)|v0.17.3.0 or newer|v1.0 or newer
|
||||
|v15, v16 (active after July 16th, 2022)|v0.18.0.0 or newer|v2.0 or newer
|
||||
|
||||
## How to mine on P2Pool
|
||||
|
||||
### General Considerations
|
||||
|
||||
- In order to mine on P2Pool, a synced Monero node using monerod v0.17.3.0 or newer is required. If you do not currently have one configured, you can find instructions to do so [here](https://sethforprivacy.com/guides/run-a-monero-node-advanced/).
|
||||
- It is highly recommended that you create a separate restricted user account for mining. While P2Pool has been battle-tested for a long time now, any software may have unknown bugs/vulnerabilities.
|
||||
- You have to use a primary wallet address for mining. Subaddresses and integrated addresses are not supported, just like with monerod solo mining.
|
||||
- Starting from P2Pool v1.7, you can add the `--mini` parameter to your P2Pool command to connect to the **p2pool-mini** sidechain. Note that it will also change the default p2p port from 37889 to 37888.
|
||||
- Check that ports 18080 (Monero p2p port) and 37889/37888 (P2Pool/P2Pool mini p2p port) are open in your firewall to ensure better connectivity. If you're mining from a computer behind NAT (like a router) you could consider forwarding the ports to your local machine.
|
||||
- You can connect multiple miners to the same P2Pool node. The more the better!
|
||||
- The below steps assume that you run everything on the same machine. If it's not the case, change `127.0.0.1` to appropriate IP addresses for your setup.
|
||||
- It is highly recommended to create a new mainnet wallet for P2Pool mining because **wallet addresses are public on P2Pool**.
|
||||
|
||||
**Wallet software compatible with P2Pool payouts**
|
||||
- [Official Monero CLI and GUI v0.17.2.3 and newer](https://www.getmonero.org/downloads/)
|
||||
- [Monerujo v2.1.0 "Vertant" and newer](https://www.monerujo.io/)
|
||||
- [Cake Wallet v4.2.7 and newer](https://cakewallet.com/)
|
||||
- [Monero.com by Cake Wallet](https://monero.com/)
|
||||
- [Feather Wallet v1.0.0 and newer](https://featherwallet.org/)
|
||||
- [MyMonero](https://mymonero.com/)
|
||||
|
||||
### GNU/Linux
|
||||
|
||||
1. Download the latest P2Pool binaries [here](https://github.com/SChernykh/p2pool/releases/latest).
|
||||
- Alternatively, grab the latest source code for P2Pool and [build it](#build-instructions).
|
||||
2. Download the latest XMRig (linux-static-x64) binary [here](https://github.com/xmrig/xmrig/releases/latest).
|
||||
3. Prepare enough huge pages (required for each instance of monerod/P2Pool/XMRig):
|
||||
```
|
||||
sudo sysctl vm.nr_hugepages=3072
|
||||
```
|
||||
4. Check that ports 18080 (Monero p2p port) and 37889/37888 (P2Pool/P2Pool mini p2p port) are open in your local firewall to ensure better connectivity.
|
||||
5. Start `monerod` with the following command/options:
|
||||
```
|
||||
./monerod --zmq-pub tcp://127.0.0.1:18083 --disable-dns-checkpoints --enable-dns-blocklist
|
||||
```
|
||||
**Note:** The `--zmq-pub` option is required for P2Pool to work properly.
|
||||
|
||||
6. Start P2Pool with the following command/options:
|
||||
```
|
||||
./p2pool --host 127.0.0.1 --wallet YOUR_WALLET_ADDRESS
|
||||
```
|
||||
7. Wait until the initial P2Pool sync is finished (shouldn't take more than 5-10 minutes).
|
||||
8. Start XMRig with the following command/options:
|
||||
```
|
||||
./xmrig -o 127.0.0.1:3333
|
||||
```
|
||||
- Note that you don't need to specify your wallet address for XMRig. **Wallet addresses set in XMRig config will be ignored!**
|
||||
- To set a custom fixed difficulty for your miner (for example, 10000), instead start XMRig with the following options:
|
||||
```
|
||||
./xmrig -u x+10000 -o 127.0.0.1:3333
|
||||
```
|
||||
9. XMRig should connect and start mining!
|
||||
|
||||
**Additional Information:**
|
||||
- For a more in-depth beginner friendly walk-through with the option of using Docker, please see SethForPrivacy's guide at: https://sethforprivacy.com/guides/run-a-p2pool-node/
|
||||
- You can check the p2pool.log for any warnings or errors using the following command:
|
||||
```
|
||||
grep -E 'WARNING|ERROR' p2pool.log
|
||||
```
|
||||
- P2Pool has verbose logging by default, you can reduce it by using "loglevel N" command where N is between 0 and 6. Default loglevel is 3.
|
||||
- You can use `logrotate` with a config like this to control logfile growth:
|
||||
```
|
||||
<path-to-logfile>
|
||||
{
|
||||
rotate 7
|
||||
daily
|
||||
missingok
|
||||
delaycompress
|
||||
nocreate
|
||||
}
|
||||
```
|
||||
|
||||
### Windows
|
||||
|
||||
**Note:** *Windows SmartScreen may block incoming connections by files that are "Downloaded from the Internet". You can allow 'p2pool.exe' and 'monerod.exe' by double-clicking them, clicking "More Info", then click "Run Anyway" and then closing them immediately so you can run them from the command line. Advanced users can use the PowerShell cmdlet `Unblock-File` to remove this flag.*
|
||||
|
||||
1. Download the latest P2Pool binaries [here](https://github.com/SChernykh/p2pool/releases/latest).
|
||||
- Alternatively, grab the latest source code for P2Pool and [build it](#build-instructions).
|
||||
2. Download the latest XMRig binary [here](https://github.com/xmrig/xmrig/releases/latest).
|
||||
3. Expand the P2Pool binaries into an appropriate location (`%USERPROFILE%/bin` or `C:/bin/` are good options)
|
||||
4. Expand XMRig binary into an appropriate location (the same folder as P2Pool is fine).
|
||||
5. Prepare huge pages to work properly (each instance of monerod/P2Pool/XMRig needs them):
|
||||
- On Windows 10 or above, run XMRig at least once as Administrator (right-click Run As Administrator)
|
||||
- On earlier versions of Windows, you'll need to run XMRig as Administrator at least once per login.
|
||||
6. Open a command prompt and navigate to the folder where you extracted P2Pool.
|
||||
|
||||
**Note:** *When running the below commands, Windows Firewall may prompt to allow connections, click "Allow" if prompted.*
|
||||
|
||||
7. Start `monerod` with the following command/options:
|
||||
```
|
||||
.\Monero\monerod.exe --zmq-pub tcp://127.0.0.1:18083 --disable-dns-checkpoints --enable-dns-blocklist
|
||||
```
|
||||
**Note:** The `--zmq-pub` option is required for P2Pool to work properly.
|
||||
8. Start P2Pool with the following command/options:
|
||||
```
|
||||
.\p2pool.exe --host 127.0.0.1 --wallet YOUR_WALLET_ADDRESS
|
||||
```
|
||||
9. Wait until the initial P2Pool sync is finished (shouldn't take more than 5-10 minutes).
|
||||
10. Start XMRig with the following command/options:
|
||||
```
|
||||
.\xmrig.exe -o 127.0.0.1:3333
|
||||
```
|
||||
- Note that you don't need to specify your wallet address for XMRig. **Wallet addresses set in XMRig config will be ignored!**
|
||||
- To set a custom fixed difficulty for your miner (for example, 10000), instead start XMRig with the following options:
|
||||
```
|
||||
xmrig.exe -u x+10000 -o 127.0.0.1:3333
|
||||
```
|
||||
11. XMRig should connect and start mining!
|
||||
12. *(Optional but highly recommended)* You can create a Quickstart by creating a batch (.bat) file with the following contents and placing it in your P2Pool directory along with `xmrig.exe`.
|
||||
```
|
||||
@ECHO OFF
|
||||
start cmd /k %~dp0\Monero\monerod.exe --zmq-pub tcp://127.0.0.1:18083 --disable-dns-checkpoints --enable-dns-blocklist
|
||||
ECHO Wait until the Monero daemon shows fully synced before continuing. This can take some time. Type 'status' in other window to check progress.
|
||||
PAUSE
|
||||
start cmd /k %~dp0\p2pool.exe --wallet YOUR_WALLET_ADDRESS
|
||||
ECHO Wait until the daemon shows fully synced before continuing. This can take some time.
|
||||
PAUSE
|
||||
%~dp0\xmrig.exe -u x+30000 -o 127.0.0.1
|
||||
```
|
||||
|
||||
## Build instructions
|
||||
Please see the relevant instructions for your platform:
|
||||
|
||||
### Ubuntu 20.04
|
||||
|
||||
p2pool binary:
|
||||
Run the following commands to install the necessary prerequisites, clone this repo, and build P2Pool locally on Ubuntu 20.04:
|
||||
```
|
||||
sudo apt update && sudo apt install git build-essential cmake libuv1-dev libzmq3-dev libsodium-dev libpgm-dev libnorm-dev libgss-dev
|
||||
git clone --recursive https://github.com/SChernykh/p2pool
|
||||
@@ -65,23 +208,13 @@ cmake ..
|
||||
make -j$(nproc)
|
||||
```
|
||||
|
||||
monerod binary compatible with p2pool:
|
||||
```
|
||||
sudo apt update && sudo apt install git build-essential cmake pkg-config libssl-dev libzmq3-dev libunbound-dev libsodium-dev libunwind8-dev liblzma-dev libreadline6-dev libldns-dev libexpat1-dev libpgm-dev qttools5-dev-tools libhidapi-dev libusb-1.0-0-dev libprotobuf-dev protobuf-compiler libudev-dev libboost-chrono-dev libboost-date-time-dev libboost-filesystem-dev libboost-locale-dev libboost-program-options-dev libboost-regex-dev libboost-serialization-dev libboost-system-dev libboost-thread-dev ccache doxygen graphviz
|
||||
git clone --recursive https://github.com/SChernykh/monero
|
||||
cd monero
|
||||
git checkout p2pool-api-v0.17
|
||||
git submodule sync && git submodule update --init --force --recursive
|
||||
make release-static -j$(nproc)
|
||||
```
|
||||
|
||||
### Arch Linux [AUR](https://wiki.archlinux.org/title/Arch_User_Repository)
|
||||
|
||||
Make the package: [p2pool-git](https://aur.archlinux.org/packages/p2pool-git/)
|
||||
|
||||
### [Nix/NixOS](https://nixos.org)
|
||||
|
||||
This is a flake only project. So you have to use [nixUnstable with nix flakes](https://nixos.wiki/wiki/Flakes) to build or install p2pool.
|
||||
This is a flake only project. So you have to use [nixUnstable with nix flakes](https://nixos.wiki/wiki/Flakes) to build or install P2Pool.
|
||||
The commands below use the new flake specific reference-format, so be sure to also set `ca-references` in `--experimental-features`.
|
||||
|
||||
Because this project has submodules which are not fixed in _nixUnstable_ yet you have to use the `nix/master` branch:
|
||||
@@ -99,31 +232,9 @@ Run the binary with arguments:
|
||||
nix run git+https://github.com/SChernykh/p2pool?ref=master -- --help
|
||||
```
|
||||
|
||||
### macOS
|
||||
|
||||
p2pool binary:
|
||||
```
|
||||
brew update && brew install git cmake libuv zmq libpgm
|
||||
git clone --recursive https://github.com/SChernykh/p2pool
|
||||
cd p2pool
|
||||
mkdir build && cd build
|
||||
cmake ..
|
||||
make -j$(sysctl -n hw.logicalcpu)
|
||||
```
|
||||
|
||||
monerod binary compatible with p2pool:
|
||||
```
|
||||
git clone --recursive https://github.com/SChernykh/monero
|
||||
cd monero
|
||||
git checkout p2pool-api-v0.17
|
||||
git submodule sync && git submodule update --init --force --recursive
|
||||
brew update && brew bundle --file=contrib/brew/Brewfile
|
||||
make -j$(sysctl -n hw.logicalcpu)
|
||||
```
|
||||
|
||||
### Windows
|
||||
|
||||
p2pool binary (Visual Studio Community 2019 build):
|
||||
P2Pool binary (Visual Studio Community 2019 build):
|
||||
*NOTE: You need to have the "Desktop Development with C++" module installed.*
|
||||
```
|
||||
git clone --recursive https://github.com/SChernykh/p2pool
|
||||
@@ -136,91 +247,40 @@ then open generated build\p2pool.sln in Visual Studio and build it there
|
||||
|
||||
Alternatively, you can select "Clone a repository" within the GUI, then select "Build" from the menu.
|
||||
|
||||
monerod binary compatible with p2pool:
|
||||
### macOS
|
||||
|
||||
Run the following commands to install the necessary prerequisites, clone this repo, and build P2Pool locally on your Mac:
|
||||
```
|
||||
git clone --recursive https://github.com/SChernykh/monero
|
||||
cd monero
|
||||
git checkout p2pool-api-v0.17
|
||||
git submodule sync && git submodule update --init --force --recursive
|
||||
brew update && brew install git cmake libuv zmq libpgm
|
||||
git clone --recursive https://github.com/SChernykh/p2pool
|
||||
cd p2pool
|
||||
mkdir build && cd build
|
||||
cmake ..
|
||||
make -j$(sysctl -n hw.logicalcpu)
|
||||
```
|
||||
then follow the instructions from https://github.com/monero-project/monero/#on-windows
|
||||
|
||||
## How to mine on P2Pool
|
||||
### FreeBSD
|
||||
|
||||
This guide assumes that you run everything on the same machine. If it's not the case, change `127.0.0.1` to appropriate IP addresses for your setup. It's highly recommended to create a new mainnet wallet for mining because **wallet addresses are public on p2pool**.
|
||||
|
||||
**Wallet software compatible with p2pool payouts**
|
||||
- Official Monero CLI and GUI v0.17.2.3 and newer
|
||||
- Monerujo v2.1.0 "Vertant" and newer
|
||||
- Cake Wallet v4.2.7 and newer
|
||||
- MyMonero
|
||||
|
||||
**General Considerations**
|
||||
|
||||
- Create a separate restricted user account for mining. p2pool is relatively new and may still have serious bugs/vulnerabilities!
|
||||
- You have to use the primary wallet address for mining. Subaddresses and integrated addresses are not supported, just like with monerod solo mining.
|
||||
- Check that ports 18080 (Monero p2p port) and 37889 (p2pool p2p port) are open in your firewall to ensure better connectivity. If you're mining from a computer behind NAT (like a router) you could consider forwarding the ports to your local machine.
|
||||
- You can connect multiple miners to the same p2pool node. The more the better!
|
||||
|
||||
Step-by-step guide:
|
||||
|
||||
### GNU/Linux
|
||||
|
||||
- Download binaries from https://github.com/SChernykh/p2pool/releases/latest
|
||||
- Alternatively, grab the latest source code for both p2pool and monerod and build them (see above, also notice that the branch name for monerod changed, you'll need to checkout p2pool-api-v0.17)
|
||||
- Prepare enough huge pages (each of monerod/p2pool/xmrig needs them): `sudo sysctl vm.nr_hugepages=3072`
|
||||
- Get xmrig (linux-static-x64) binary from https://github.com/xmrig/xmrig/releases/latest
|
||||
- Check that ports 18080 (Monero p2p port) and 37889 (p2pool p2p port) are open in your firewall to ensure better connectivity
|
||||
- Run `./monerod --zmq-pub tcp://127.0.0.1:18083 --disable-dns-checkpoints --enable-dns-blocklist` **don't forget --zmq-pub parameter in the command line**
|
||||
- Double check that it shows **Monero 'Oxygen Orion' (v0.17.2.3-7dbb0d1fc)** on startup. Wait until it's synchronized.
|
||||
- Run `./p2pool --host 127.0.0.1 --wallet YOUR_WALLET_ADDRESS`
|
||||
- p2pool has _very_ verbose logging by default, it will spam a lot, no I mean A LOT in both console and in p2pool.log. Logs help testing immensely!
|
||||
- You can use `logrotate` with a config like this to control logfile growth:
|
||||
```
|
||||
<path-to-logfile>
|
||||
{
|
||||
rotate 7
|
||||
daily
|
||||
missingok
|
||||
delaycompress
|
||||
nocreate
|
||||
}
|
||||
```
|
||||
- Wait until initial p2pool sync is finished, it shouldn't take more than 5-10 minutes, once completed xmrig should be able to connect to the stratum server on port 3333.
|
||||
- Run `./xmrig -o 127.0.0.1:3333`. Note that you don't need to specify wallet address for xmrig. **Wallet address set in xmrig config will be ignored!**
|
||||
- To set custom fixed difficulty for your miner (for example, 10000), run `./xmrig -u x+10000 -o 127.0.0.1:3333`
|
||||
- xmrig should connect and start mining
|
||||
- Also check p2pool.log for any warnings and errors: `grep -E 'WARNING|ERROR' p2pool.log`
|
||||
|
||||
### Windows
|
||||
|
||||
*NOTE: Windows SmartScreen may block incoming connections by files that are "Downloaded from the Internet". You can allow 'p2pool.exe' and 'monerod.exe' by double-clicking them, clicking "More Info", then click "Run Anyway" and then closing them immediately so you can run them from the command line. Advanced users can use the PowerShell cmdlet `Unblock-File` to remove this flag.*
|
||||
|
||||
- Download p2pool binaries from https://github.com/SChernykh/p2pool/releases/latest
|
||||
- Download xmrig binary from https://github.com/xmrig/xmrig/releases/latest *(xmrig-6.15.0-gcc-win64.zip is the current version)*
|
||||
- Expand the p2pool binaries into an appropriate location (`%USERPROFILE%/bin` or `C:/bin/` are good options)
|
||||
- Expand xmrig binary into appropriate location (same folder as p2pool is fine)
|
||||
- Prepare huge pages (each of monerod/p2pool/xmrig needs them):
|
||||
- On Windows 10 or above, run xmrig at least once as Administrator (right-click Run As Administrator)
|
||||
- On earlier versions of Windows, you'll need to run it as admin at least once per login.
|
||||
- Open a command prompt and navigate to the folder where you extracted p2pool.
|
||||
- *When running these commands, Windows Firewall may prompt to allow connections, click "Allow"*
|
||||
- Run `.\Monero\monerod.exe --zmq-pub tcp://127.0.0.1:18083 --disable-dns-checkpoints --enable-dns-blocklist` *NOTE: don't forget --zmq-pub parameter in the command line*
|
||||
- Double check that it shows **Monero 'Oxygen Orion' (v0.17.2.3-7dbb0d1fc)** on startup. Wait until it's synchronized.
|
||||
- Run `.\p2pool.exe --host 127.0.0.1 --wallet YOUR_WALLET_ADDRESS`
|
||||
- Wait until initial p2pool sync is finished, it shouldn't take more than 5-10 minutes, once completed xmrig should be able to connect to the stratum server on port 3333.
|
||||
- Run `.\xmrig.exe -o 127.0.0.1:3333`. Note that you don't need to specify wallet address for xmrig. **Wallet address set in xmrig config will be ignored!**
|
||||
- To set custom fixed difficulty for your miner (for example, 10000), run `xmrig.exe -u x+10000 -o 127.0.0.1:3333`
|
||||
- Windows Quickstart: Create a batch (.bat) file with the following contents and place it in your p2pool directory along with xmrig.exe.
|
||||
Run the following commands to install the necessary prerequisites, clone this repo, and build P2Pool locally on FreeBSD:
|
||||
```
|
||||
@ECHO OFF
|
||||
start cmd /k %~dp0\Monero\monerod.exe --zmq-pub tcp://127.0.0.1:18083 --disable-dns-checkpoints --enable-dns-blocklist
|
||||
ECHO Wait until the Monero daemon shows fully synced before continuing. This can take some time. Type 'status' in other window to check progress.
|
||||
PAUSE
|
||||
start cmd /k %~dp0\p2pool.exe --wallet YOUR_WALLET_ADDRESS
|
||||
ECHO Wait until the daemon shows fully synced before continuing. This can take some time.
|
||||
PAUSE
|
||||
%~dp0\xmrig.exe -u x+30000 -o 127.0.0.1
|
||||
pkg install git cmake libuv libzmq4
|
||||
git clone --recursive https://github.com/SChernykh/p2pool
|
||||
cd p2pool
|
||||
mkdir build && cd build
|
||||
cmake ..
|
||||
make
|
||||
```
|
||||
|
||||
### Android (Termux)
|
||||
|
||||
Run the following commands to install the necessary prerequisites, clone this repo, and build P2Pool locally in Termux:
|
||||
```
|
||||
pkg install git build-essential cmake libuv libzmq
|
||||
git clone --recursive https://github.com/SChernykh/p2pool
|
||||
cd p2pool
|
||||
mkdir build && cd build
|
||||
cmake ..
|
||||
make -j$(nproc)
|
||||
```
|
||||
|
||||
## Donations
|
||||
|
||||
+18
-15
@@ -6,45 +6,48 @@ set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||
|
||||
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
|
||||
set(GENERAL_FLAGS "-pthread")
|
||||
set(WARNING_FLAGS "-Wall -Wextra -Werror")
|
||||
set(OPTIMIZATION_FLAGS "-Ofast -s")
|
||||
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WARNING_FLAGS}")
|
||||
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GENERAL_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${GENERAL_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WARNING_FLAGS}")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GENERAL_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${GENERAL_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
|
||||
if (WIN32)
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
|
||||
else()
|
||||
if (STATIC_LINUX_BINARY)
|
||||
if (STATIC_BINARY)
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
|
||||
else()
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++")
|
||||
endif()
|
||||
endif()
|
||||
elseif (CMAKE_CXX_COMPILER_ID MATCHES MSVC)
|
||||
set(GENERAL_FLAGS "")
|
||||
set(WARNING_FLAGS "/Wall /WX /sdl")
|
||||
set(SECURITY_FLAGS "/GS /guard:cf")
|
||||
set(OPTIMIZATION_FLAGS "/O2 /Oi /Ob2 /Ot /DNDEBUG /GL")
|
||||
|
||||
set(CMAKE_C_FLAGS_DEBUG "${WARNING_FLAGS} ${SECURITY_FLAGS} /Od /Ob0 /Zi /MTd /fsanitize=address")
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${WARNING_FLAGS} ${SECURITY_FLAGS} /Od /Ob0 /Zi /MTd /fsanitize=address")
|
||||
set(CMAKE_C_FLAGS_DEBUG "${GENERAL_FLAGS} ${WARNING_FLAGS} ${SECURITY_FLAGS} /Od /Ob0 /Zi /MTd /fsanitize=address")
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${GENERAL_FLAGS} ${WARNING_FLAGS} ${SECURITY_FLAGS} /Od /Ob0 /Zi /MTd /fsanitize=address")
|
||||
|
||||
set(CMAKE_C_FLAGS_RELEASE "${WARNING_FLAGS} ${SECURITY_FLAGS} ${OPTIMIZATION_FLAGS} /MT")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${WARNING_FLAGS} ${SECURITY_FLAGS} ${OPTIMIZATION_FLAGS} /MT")
|
||||
set(CMAKE_C_FLAGS_RELEASE "${GENERAL_FLAGS} ${WARNING_FLAGS} ${SECURITY_FLAGS} ${OPTIMIZATION_FLAGS} /MT")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${GENERAL_FLAGS} ${WARNING_FLAGS} ${SECURITY_FLAGS} ${OPTIMIZATION_FLAGS} /MT")
|
||||
|
||||
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${WARNING_FLAGS} ${SECURITY_FLAGS} /Ob1 /Ot /Zi /MT")
|
||||
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${WARNING_FLAGS} ${SECURITY_FLAGS} /Ob1 /Ot /Zi /MT")
|
||||
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${GENERAL_FLAGS} ${WARNING_FLAGS} ${SECURITY_FLAGS} /Ob1 /Ot /Zi /MT")
|
||||
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${GENERAL_FLAGS} ${WARNING_FLAGS} ${SECURITY_FLAGS} /Ob1 /Ot /Zi /MT")
|
||||
|
||||
elseif (CMAKE_CXX_COMPILER_ID MATCHES Clang)
|
||||
set(GENERAL_FLAGS "-pthread")
|
||||
set(WARNING_FLAGS "-Wall -Wextra -Wno-undefined-internal -Werror")
|
||||
set(OPTIMIZATION_FLAGS "-Ofast -funroll-loops -fmerge-all-constants")
|
||||
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WARNING_FLAGS}")
|
||||
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GENERAL_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${GENERAL_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WARNING_FLAGS}")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GENERAL_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${GENERAL_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
endif()
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
// Fixed difficulty for miners that connect to your node must be set in their config.json
|
||||
//
|
||||
{
|
||||
"name": "mainnet test 2",
|
||||
"name": "default",
|
||||
"password": "",
|
||||
"block_time": 10,
|
||||
"min_diff": 100000,
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
../src/
|
||||
../external/src/
|
||||
../external/src/cryptonote/
|
||||
../external/src/libuv/
|
||||
../external/src/libuv/include/
|
||||
../external/src/cppzmq/
|
||||
../external/src/libzmq/
|
||||
../external/src/libzmq/include/
|
||||
../external/src/llhttp/
|
||||
../external/src/RandomX/src/
|
||||
../external/src/rapidjson/include
|
||||
../external/src/robin-hood-hashing/src/include
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
@echo off
|
||||
"C:\Program Files\cppcheck\cppcheck.exe" --project=..\build\p2pool.vcxproj --project-configuration="Release|x64" -DZMQ_STATIC --platform=win64 --std=c++14 --enable=all --inconclusive --inline-suppr --template="{file}:{line}:{id}{inconclusive: INCONCLUSIVE} {message}" --includes-file=includes.txt --suppressions-list=suppressions.txt --output-file=errors_full.txt
|
||||
"..\cppcheck-main\bin\cppcheck.exe" --project=..\build\p2pool.vcxproj --project-configuration="Release|x64" -DSIZE_MAX=UINT64_MAX -DRAPIDJSON_ENDIAN=RAPIDJSON_LITTLEENDIAN --platform=win64 --std=c++14 --enable=all --inconclusive --inline-suppr --template="{file}:{line}:{id}{inconclusive: INCONCLUSIVE} {message}" --includes-file=includes.txt --suppressions-list=suppressions.txt --output-file=errors_full.txt
|
||||
findstr /V /C:"external\src" errors_full.txt > errors_filtered.txt
|
||||
for /f %%i in ("errors_filtered.txt") do set size=%%~zi
|
||||
if %size% gtr 0 (
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
#!/bin/bash
|
||||
cppcheck ../src -DZMQ_STATIC --platform=unix64 --std=c++14 --enable=all --inconclusive --inline-suppr --template="{file}:{line}:{id}{inconclusive: INCONCLUSIVE} {message}" -I ../src/ -I ../external/src/ -I ../external/src/cryptonote/ -I ../external/src/libuv/ -I ../external/src/cppzmq/ -I ../external/src/libzmq/ -I ../external/src/llhttp/ -I ../external/src/RandomX/src/ -I ../external/src/rapidjson/include --suppressions-list=suppressions.txt --output-file=errors_full.txt
|
||||
grep -v 'external' errors_full.txt | grep -v 'unmatchedSuppression' > errors_filtered.txt
|
||||
../cppcheck-main/cppcheck ../src -DSIZE_MAX=UINT64_MAX -DRAPIDJSON_ENDIAN=RAPIDJSON_LITTLEENDIAN --platform=unix64 --std=c++14 --enable=all --inconclusive --inline-suppr --template="{file}:{line}:{id}{inconclusive: INCONCLUSIVE} {message}" --includes-file=includes.txt --suppressions-list=suppressions.txt --output-file=errors_full.txt
|
||||
grep -v 'external' errors_full.txt > errors_filtered.txt
|
||||
if [ -s errors_filtered.txt ]; then
|
||||
cat errors_filtered.txt
|
||||
exit 1
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
missingIncludeSystem
|
||||
unusedFunction
|
||||
useStlAlgorithm
|
||||
functionStatic
|
||||
functionConst
|
||||
unmatchedSuppression
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
##
|
||||
# Monero Wallet Address for mining rewards (replace this with your own address)
|
||||
WALLET_ADDRESS="44MnN1f3Eto8DZYUWuE5XZNUtE3vcRzt2j6PzqWpPau34e6Cf4fAxt6X2MBmrm6F9YMEiMNjN6W4Shn4pLcfNAja621jwyg"
|
||||
|
||||
##
|
||||
# p2pool settings
|
||||
#
|
||||
# Use p2pool-mini (uncomment to enable)
|
||||
#P2POOL_MINI="--mini"
|
||||
#
|
||||
# Which port to listen for miner connections
|
||||
P2POOL_STRATUM_PORT=3333
|
||||
#
|
||||
# How much logging - (Less) 0 - 6 (More)
|
||||
P2POOL_LOGLEVEL=2
|
||||
|
||||
|
||||
##
|
||||
# Monero Node Settings
|
||||
#
|
||||
# Version of Monero to build. Must be v0.17.3.0 or later for p2pool support. "latest" pulls the most recent release tag
|
||||
MONERO_GIT_TAG=latest
|
||||
#
|
||||
# Limit the size of the blockchain on disk (comment to disable)
|
||||
PRUNE_NODE="--prune-blockchain"
|
||||
#
|
||||
# Other monerod commandline options
|
||||
MONERO_EXTRA_OPTIONS=""
|
||||
|
||||
|
||||
##
|
||||
# Xmrig Miner Settings
|
||||
#
|
||||
# Submit shares at a lower fixed difficulty to show mining progress (comment to disable, edit to customize)
|
||||
FIXED_MINING_DIFFICULTY="-u x+500000"
|
||||
#
|
||||
# set process priority (0 idle, 2 normal to 5 highest)
|
||||
MINING_CPU_PRIORITY="--cpu-priority=2"
|
||||
#
|
||||
# Other XMRIG commandline options
|
||||
# See: https://xmrig.com/docs/miner/command-line-options
|
||||
XMRIG_EXTRA_OPTIONS=""
|
||||
@@ -0,0 +1,52 @@
|
||||
# Monero P2Pool Docker Compose
|
||||
|
||||
Run your own <b>Monero Node + P2Pool + XMRig</b> in Docker
|
||||
|
||||
## Instructions
|
||||
|
||||
#### Install docker and docker-compose
|
||||
[Install Docker](https://docs.docker.com/engine/install/)
|
||||
[Install Docker Compose](https://docs.docker.com/compose/install/)
|
||||
|
||||
#### Clone the P2Pool project
|
||||
```
|
||||
git clone --recursive https://github.com/SChernykh/p2pool
|
||||
```
|
||||
|
||||
#### Configure your Monero address for mining rewards
|
||||
```
|
||||
cd p2pool/docker-compose
|
||||
vi .env
|
||||
```
|
||||
**WALLET_ADDRESS** is the only setting that needs to be updated in that file
|
||||
|
||||
#### Build the docker containers
|
||||
```
|
||||
docker-compose build
|
||||
```
|
||||
|
||||
#### Run the node, pool, and CPU miner
|
||||
```
|
||||
docker-compose up
|
||||
```
|
||||
|
||||
#### Optional
|
||||
* Open ports 18080 (Monero p2p port) and 37889 (P2Pool p2p port) or 37888 (P2Pool-mini p2p port) in your firewall to ensure better connectivity. If you're mining from a computer behind NAT (like a router) you could consider forwarding the ports to your local machine
|
||||
* An XMRig CPU miner is included by default, but you can connect additional miners to this same p2pool node using port 3333
|
||||
* Configure your kernel for maximum mining performance: [XMRig RandomX Optimization Guide](https://xmrig.com/docs/miner/randomx-optimization-guide)
|
||||
* Small miners can mine on p2pool-mini by editing the .env file
|
||||
|
||||
|
||||
#### Other usefull commands
|
||||
* You can **run everythng in the background** by adding the "-d" argument to the "docker-compose up" command: ```docker-compose up -d```
|
||||
* You can **stop everything** with CTRL-C or ```docker-compose down```
|
||||
* You can see logs when running in the background for with the "docker logs" command: ```docker logs -f p2pool-xmrig``` or ```docker logs -f p2pool-p2pool``` or ```docker logs -f p2pool-monero```
|
||||
* You can pause mining with: ```docker-compose pause xmrig``` and resume mining with: ```docker-compose unpause xmrig```
|
||||
* You can disable mining with: ```docker-compose stop xmrig``` and re-enable mining with: ```docker-compose start xmrig```
|
||||
|
||||
|
||||
#### Uninstall
|
||||
Change to p2pool/docker-compose directory
|
||||
Stop and remove all containers: ```docker-compose down```
|
||||
Remove the p2pool data: ```docker volume rm p2pool```
|
||||
Remove the monero data: ```docker volume rm monero```
|
||||
@@ -0,0 +1,86 @@
|
||||
---
|
||||
version: '3.4'
|
||||
|
||||
networks:
|
||||
p2pool:
|
||||
driver: bridge
|
||||
|
||||
volumes:
|
||||
p2pool:
|
||||
name: p2pool
|
||||
monero:
|
||||
name: monero
|
||||
|
||||
services:
|
||||
monero:
|
||||
image: monero:latest
|
||||
build:
|
||||
context: monero
|
||||
args:
|
||||
- MONERO_GIT_TAG=${MONERO_GIT_TAG}
|
||||
container_name: p2pool-monero
|
||||
networks:
|
||||
- p2pool
|
||||
ports:
|
||||
- 18080:18080/tcp
|
||||
volumes:
|
||||
- monero:/home/monero/.bitmonero:rw
|
||||
- /dev/null:/home/monero/.bitmonero/bitmonero.log:rw
|
||||
- /dev/hugepages:/dev/hugepages:rw
|
||||
restart: unless-stopped
|
||||
command: >-
|
||||
--zmq-pub tcp://0.0.0.0:18083
|
||||
--disable-dns-checkpoints
|
||||
--enable-dns-blocklist
|
||||
--non-interactive
|
||||
--p2p-bind-ip=0.0.0.0
|
||||
--p2p-bind-port=18080
|
||||
--rpc-bind-ip=0.0.0.0
|
||||
--rpc-bind-port=18081
|
||||
--confirm-external-bind
|
||||
${PRUNE_NODE}
|
||||
${MONERO_EXTRA_OPTIONS}
|
||||
|
||||
p2pool:
|
||||
image: p2pool:latest
|
||||
build: ../
|
||||
container_name: p2pool-p2pool
|
||||
networks:
|
||||
- p2pool
|
||||
ports:
|
||||
- ${P2POOL_STRATUM_PORT}:3333/tcp
|
||||
- 37888:37888/tcp
|
||||
- 37889:37889/tcp
|
||||
volumes:
|
||||
- p2pool:/home/p2pool/.p2pool:rw
|
||||
- /dev/null:/home/p2pool/.p2pool/p2pool.log:rw
|
||||
- /dev/hugepages:/dev/hugepages:rw
|
||||
depends_on:
|
||||
- monero
|
||||
restart: unless-stopped
|
||||
command: >-
|
||||
--host monero
|
||||
--wallet ${WALLET_ADDRESS}
|
||||
--loglevel ${P2POOL_LOGLEVEL}
|
||||
${P2POOL_MINI}
|
||||
|
||||
xmrig:
|
||||
image: xmrig:latest
|
||||
build: xmrig
|
||||
container_name: p2pool-xmrig
|
||||
networks:
|
||||
- p2pool
|
||||
privileged: true
|
||||
volumes:
|
||||
- /dev:/dev:ro
|
||||
- /lib/modules:/lib/modules:ro
|
||||
- /dev/hugepages:/dev/hugepages:rw
|
||||
depends_on:
|
||||
- p2pool
|
||||
restart: unless-stopped
|
||||
command: >-
|
||||
${FIXED_MINING_DIFFICULTY}
|
||||
${MINING_CPU_PRIORITY}
|
||||
${XMRIG_EXTRA_OPTIONS}
|
||||
--randomx-1gb-pages
|
||||
-o p2pool:3333
|
||||
@@ -0,0 +1,79 @@
|
||||
FROM ubuntu:latest
|
||||
ARG MONERO_GIT_TAG="latest"
|
||||
|
||||
RUN set -e && \
|
||||
apt-get update -q -y --no-install-recommends && \
|
||||
DEBIAN_FRONTEND="noninteractive" apt-get install -q -y --no-install-recommends \
|
||||
git \
|
||||
ca-certificates \
|
||||
build-essential \
|
||||
pkg-config \
|
||||
cmake \
|
||||
libuv1-dev \
|
||||
libzmq3-dev \
|
||||
libsodium-dev \
|
||||
libpgm-dev \
|
||||
libnorm-dev \
|
||||
libgss-dev \
|
||||
libssl-dev \
|
||||
libzmq3-dev \
|
||||
libunbound-dev \
|
||||
libsodium-dev \
|
||||
libunwind8-dev \
|
||||
liblzma-dev \
|
||||
libreadline6-dev \
|
||||
libldns-dev \
|
||||
libexpat1-dev \
|
||||
libpgm-dev \
|
||||
qttools5-dev-tools \
|
||||
libhidapi-dev \
|
||||
libusb-1.0-0-dev \
|
||||
libprotobuf-dev \
|
||||
protobuf-compiler \
|
||||
libudev-dev \
|
||||
libboost-chrono-dev \
|
||||
libboost-date-time-dev \
|
||||
libboost-filesystem-dev \
|
||||
libboost-locale-dev \
|
||||
libboost-program-options-dev \
|
||||
libboost-regex-dev \
|
||||
libboost-serialization-dev \
|
||||
libboost-system-dev \
|
||||
libboost-thread-dev \
|
||||
ccache \
|
||||
doxygen \
|
||||
graphviz
|
||||
|
||||
WORKDIR /usr/src
|
||||
RUN git clone --recursive https://github.com/monero-project/monero && \
|
||||
cd monero && \
|
||||
if [ "x$MONERO_GIT_TAG" = "xlatest" ]; then MONERO_GIT_TAG=$(git describe --tags $(git rev-list --tags --max-count=1)); fi && \
|
||||
git checkout $MONERO_GIT_TAG && \
|
||||
git submodule sync && git submodule update --init --force --recursive && \
|
||||
make release-static -j$(nproc)
|
||||
|
||||
# ---
|
||||
|
||||
FROM ubuntu:latest
|
||||
|
||||
COPY --from=0 /usr/src/monero/build/Linux/*/release/bin/* /
|
||||
|
||||
RUN set -e && \
|
||||
apt-get update -q -y --no-install-recommends && \
|
||||
DEBIAN_FRONTEND="noninteractive" apt-get install -q -y --no-install-recommends \
|
||||
libgssapi-krb5-2 \
|
||||
&& \
|
||||
apt-get clean
|
||||
|
||||
RUN groupadd -r monero -g 1000 && \
|
||||
useradd -u 1000 -r -g monero -s /sbin/nologin -c "monero node user" monero
|
||||
RUN mkdir -p /home/monero/.bitmonero && \
|
||||
chown monero.monero /home/monero /home/monero/.bitmonero
|
||||
USER monero
|
||||
|
||||
EXPOSE 18080 18081 18083
|
||||
|
||||
VOLUME /home/monero/.bitmonero
|
||||
|
||||
WORKDIR /home/monero
|
||||
ENTRYPOINT ["/monerod"]
|
||||
@@ -0,0 +1,37 @@
|
||||
FROM ubuntu:latest
|
||||
|
||||
RUN set -e && \
|
||||
apt-get update -q -y --no-install-recommends && \
|
||||
DEBIAN_FRONTEND="noninteractive" apt-get install -q -y --no-install-recommends \
|
||||
git \
|
||||
build-essential \
|
||||
ca-certificates \
|
||||
cmake \
|
||||
libuv1-dev \
|
||||
libssl-dev \
|
||||
libhwloc-dev
|
||||
|
||||
WORKDIR /usr/src
|
||||
RUN git clone https://github.com/xmrig/xmrig.git && \
|
||||
mkdir xmrig/build && \
|
||||
cd xmrig/build && \
|
||||
cmake .. && \
|
||||
make -j$(nproc)
|
||||
|
||||
# ---
|
||||
|
||||
FROM ubuntu:latest
|
||||
|
||||
COPY --from=0 /usr/src/xmrig/build/xmrig /
|
||||
|
||||
RUN set -e && \
|
||||
apt-get update -q -y --no-install-recommends && \
|
||||
DEBIAN_FRONTEND="noninteractive" apt-get install -q -y --no-install-recommends \
|
||||
libssl1.1 \
|
||||
libcrypto++6 \
|
||||
libhwloc15 \
|
||||
&& \
|
||||
apt-get clean
|
||||
|
||||
WORKDIR /
|
||||
ENTRYPOINT ["/xmrig"]
|
||||
Vendored
BIN
Binary file not shown.
Vendored
BIN
Binary file not shown.
Vendored
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Vendored
+1
-1
Submodule external/src/RandomX updated: 1adc7d8efd...6e059124bd
Vendored
+1
-1
Submodule external/src/cppzmq updated: 286d8be432...26362337a9
Vendored
+44
-8
@@ -3831,15 +3831,51 @@ int sc_isnonzero(const unsigned char *s) {
|
||||
s[27] | s[28] | s[29] | s[30] | s[31]) - 1) >> 8) + 1;
|
||||
}
|
||||
|
||||
int ge_p3_is_point_at_infinity(const ge_p3 *p) {
|
||||
// X = 0 and Y == Z
|
||||
int n;
|
||||
for (n = 0; n < 10; ++n)
|
||||
int ge_p3_is_point_at_infinity_vartime(const ge_p3 *p) {
|
||||
// https://eprint.iacr.org/2008/522
|
||||
// X == T == 0 and Y/Z == 1
|
||||
// note: convert all pieces to canonical bytes in case rounding is required (i.e. an element is > q)
|
||||
// note2: even though T = XY/Z is true for valid point representations (implying it isn't necessary to
|
||||
// test T == 0), the input to this function might NOT be valid, so we must test T == 0
|
||||
char result_X_bytes[32];
|
||||
fe_tobytes((unsigned char*)&result_X_bytes, p->X);
|
||||
|
||||
// X != 0
|
||||
for (int i = 0; i < 32; ++i)
|
||||
{
|
||||
if (p->X[n] | p->T[n])
|
||||
return 0;
|
||||
if (p->Y[n] != p->Z[n])
|
||||
if (result_X_bytes[i])
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
|
||||
char result_T_bytes[32];
|
||||
fe_tobytes((unsigned char*)&result_T_bytes, p->T);
|
||||
|
||||
// T != 0
|
||||
for (int i = 0; i < 32; ++i)
|
||||
{
|
||||
if (result_T_bytes[i])
|
||||
return 0;
|
||||
}
|
||||
|
||||
char result_Y_bytes[32];
|
||||
char result_Z_bytes[32];
|
||||
fe_tobytes((unsigned char*)&result_Y_bytes, p->Y);
|
||||
fe_tobytes((unsigned char*)&result_Z_bytes, p->Z);
|
||||
|
||||
// Y != Z
|
||||
for (int i = 0; i < 32; ++i)
|
||||
{
|
||||
if (result_Y_bytes[i] != result_Z_bytes[i])
|
||||
return 0;
|
||||
}
|
||||
|
||||
// is Y nonzero? then Y/Z == 1
|
||||
for (int i = 0; i < 32; ++i)
|
||||
{
|
||||
if (result_Y_bytes[i] != 0)
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Y/Z = 0/0
|
||||
return 0;
|
||||
}
|
||||
|
||||
Vendored
+1
-1
@@ -162,4 +162,4 @@ void fe_add(fe h, const fe f, const fe g);
|
||||
void fe_tobytes(unsigned char *, const fe);
|
||||
void fe_invert(fe out, const fe z);
|
||||
|
||||
int ge_p3_is_point_at_infinity(const ge_p3 *p);
|
||||
int ge_p3_is_point_at_infinity_vartime(const ge_p3 *p);
|
||||
|
||||
Vendored
-1
Submodule external/src/libsodium deleted from 64129657a5
Vendored
+1
-1
Submodule external/src/libuv updated: a39009a5a9...99ab53e998
Vendored
+1
-1
Submodule external/src/libzmq updated: 056f37f352...37224c93de
Vendored
+1
-1
Submodule external/src/rapidjson updated: 5bbae3e50b...6e3ab6dd56
+1
Submodule external/src/robin-hood-hashing added at 7f87d77122
+3
-2
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -154,6 +154,7 @@ struct BlockCache::Impl : public nocopy_nomove
|
||||
BlockCache::BlockCache()
|
||||
: m_impl(new Impl())
|
||||
, m_flushRunning(0)
|
||||
, m_storeIndex(0)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -171,7 +172,7 @@ void BlockCache::store(const PoolBlock& block)
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t* data = m_impl->m_data + (static_cast<size_t>(block.m_sidechainHeight % NUM_BLOCKS) * BLOCK_SIZE);
|
||||
uint8_t* data = m_impl->m_data + (static_cast<size_t>((m_storeIndex++) % NUM_BLOCKS) * BLOCK_SIZE);
|
||||
|
||||
*reinterpret_cast<uint32_t*>(data) = static_cast<uint32_t>(n1 + n2);
|
||||
memcpy(data + sizeof(uint32_t), block.m_mainChainData.data(), n1);
|
||||
|
||||
+2
-1
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -37,6 +37,7 @@ private:
|
||||
struct Impl;
|
||||
Impl* m_impl;
|
||||
std::atomic<uint32_t> m_flushRunning;
|
||||
std::atomic<uint32_t> m_storeIndex;
|
||||
};
|
||||
|
||||
} // namespace p2pool
|
||||
|
||||
+95
-25
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
* Portions Copyright (c) 2012-2013 The Cryptonote developers
|
||||
* Portions Copyright (c) 2014-2021 The Monero Project
|
||||
* Portions Copyright (c) 2021 XMRig <https://github.com/xmrig>
|
||||
@@ -115,7 +115,7 @@ BlockTemplate& BlockTemplate::operator=(const BlockTemplate& b)
|
||||
m_extraNonceOffsetInTemplate = b.m_extraNonceOffsetInTemplate;
|
||||
m_numTransactionHashes = b.m_numTransactionHashes;
|
||||
m_prevId = b.m_prevId;
|
||||
m_height = b.m_height;
|
||||
m_height = b.m_height.load();
|
||||
m_difficulty = b.m_difficulty;
|
||||
m_seedHash = b.m_seedHash;
|
||||
m_timestamp = b.m_timestamp;
|
||||
@@ -190,12 +190,17 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet
|
||||
|
||||
++m_templateId;
|
||||
|
||||
// When block template generation fails for any reason
|
||||
auto use_old_template = [this]() {
|
||||
const uint32_t id = m_templateId - 1;
|
||||
LOGWARN(4, "using old block template with ID = " << id);
|
||||
*this = *m_oldTemplates[id % array_size(&BlockTemplate::m_oldTemplates)];
|
||||
};
|
||||
|
||||
m_height = data.height;
|
||||
m_difficulty = data.difficulty;
|
||||
m_seedHash = data.seed_hash;
|
||||
|
||||
const time_t cur_time = time(nullptr);
|
||||
|
||||
// Only choose transactions that were received 10 or more seconds ago
|
||||
size_t total_mempool_transactions;
|
||||
{
|
||||
@@ -205,6 +210,8 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet
|
||||
|
||||
total_mempool_transactions = mempool.m_transactions.size();
|
||||
|
||||
const uint64_t cur_time = seconds_since_epoch();
|
||||
|
||||
for (auto& it : mempool.m_transactions) {
|
||||
if (cur_time >= it.second.time_received + 10) {
|
||||
m_mempoolTxs.emplace_back(it.second);
|
||||
@@ -252,7 +259,7 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet
|
||||
m_poolBlockTemplate->m_minorVersion = HARDFORK_SUPPORTED_VERSION;
|
||||
|
||||
// Timestamp
|
||||
m_timestamp = cur_time;
|
||||
m_timestamp = time(nullptr);
|
||||
if (m_timestamp <= data.median_timestamp) {
|
||||
LOGWARN(2, "timestamp adjusted from " << m_timestamp << " to " << data.median_timestamp + 1 << ". Fix your system time!");
|
||||
m_timestamp = data.median_timestamp + 1;
|
||||
@@ -275,21 +282,26 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet
|
||||
|
||||
m_pool->side_chain().fill_sidechain_data(*m_poolBlockTemplate, miner_wallet, m_txkeySec, m_shares);
|
||||
if (!SideChain::split_reward(max_reward, m_shares, m_rewards)) {
|
||||
use_old_template();
|
||||
return;
|
||||
}
|
||||
|
||||
const uint64_t max_reward_amounts_weight = std::accumulate(m_rewards.begin(), m_rewards.end(), 0ULL,
|
||||
[](uint64_t a, uint64_t b)
|
||||
{
|
||||
writeVarint(b, [&a](uint8_t) { ++a; });
|
||||
return a;
|
||||
});
|
||||
auto get_reward_amounts_weight = [this]() {
|
||||
return std::accumulate(m_rewards.begin(), m_rewards.end(), 0ULL,
|
||||
[](uint64_t a, uint64_t b)
|
||||
{
|
||||
writeVarint(b, [&a](uint8_t) { ++a; });
|
||||
return a;
|
||||
});
|
||||
};
|
||||
uint64_t max_reward_amounts_weight = get_reward_amounts_weight();
|
||||
|
||||
if (!create_miner_tx(data, m_shares, max_reward_amounts_weight, true)) {
|
||||
if (create_miner_tx(data, m_shares, max_reward_amounts_weight, true) < 0) {
|
||||
use_old_template();
|
||||
return;
|
||||
}
|
||||
|
||||
const uint64_t miner_tx_weight = m_minerTx.size();
|
||||
uint64_t miner_tx_weight = m_minerTx.size();
|
||||
|
||||
// Select transactions from the mempool
|
||||
uint64_t final_reward, final_fees, final_weight;
|
||||
@@ -427,17 +439,64 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet
|
||||
}
|
||||
|
||||
if (!SideChain::split_reward(final_reward, m_shares, m_rewards)) {
|
||||
use_old_template();
|
||||
return;
|
||||
}
|
||||
|
||||
m_finalReward = final_reward;
|
||||
|
||||
if (!create_miner_tx(data, m_shares, max_reward_amounts_weight, false)) {
|
||||
return;
|
||||
const int create_miner_tx_result = create_miner_tx(data, m_shares, max_reward_amounts_weight, false);
|
||||
if (create_miner_tx_result < 0) {
|
||||
if (create_miner_tx_result == -3) {
|
||||
// Too many extra bytes were added, refine max_reward_amounts_weight and miner_tx_weight
|
||||
LOGINFO(4, "Readjusting miner_tx to reduce extra nonce size");
|
||||
|
||||
// The difference between max possible reward and the actual reward can't reduce the size of output amount varints by more than 1 byte each
|
||||
// So block weight will be >= current weight - number of outputs
|
||||
const uint64_t w = (final_weight > m_rewards.size()) ? (final_weight - m_rewards.size()) : 0;
|
||||
|
||||
// Block reward will be <= r due to how block size penalty works
|
||||
const uint64_t r = get_block_reward(base_reward, data.median_weight, final_fees, w);
|
||||
|
||||
if (!SideChain::split_reward(r, m_shares, m_rewards)) {
|
||||
use_old_template();
|
||||
return;
|
||||
}
|
||||
|
||||
max_reward_amounts_weight = get_reward_amounts_weight();
|
||||
|
||||
if (create_miner_tx(data, m_shares, max_reward_amounts_weight, true) < 0) {
|
||||
use_old_template();
|
||||
return;
|
||||
}
|
||||
|
||||
final_weight -= miner_tx_weight;
|
||||
final_weight += m_minerTx.size();
|
||||
miner_tx_weight = m_minerTx.size();
|
||||
|
||||
final_reward = get_block_reward(base_reward, data.median_weight, final_fees, final_weight);
|
||||
|
||||
if (!SideChain::split_reward(final_reward, m_shares, m_rewards)) {
|
||||
use_old_template();
|
||||
return;
|
||||
}
|
||||
|
||||
if (create_miner_tx(data, m_shares, max_reward_amounts_weight, false) < 0) {
|
||||
use_old_template();
|
||||
return;
|
||||
}
|
||||
|
||||
LOGINFO(4, "New extra nonce size = " << m_poolBlockTemplate->m_extraNonceSize);
|
||||
}
|
||||
else {
|
||||
use_old_template();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_minerTx.size() != miner_tx_weight) {
|
||||
LOGERR(1, "miner tx size changed after adjusting reward");
|
||||
use_old_template();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -516,7 +575,6 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet
|
||||
m_rewards.clear();
|
||||
m_mempoolTxs.clear();
|
||||
m_mempoolTxsOrder.clear();
|
||||
m_shares.clear();
|
||||
}
|
||||
|
||||
#if TEST_MEMPOOL_PICKING_ALGORITHM
|
||||
@@ -600,7 +658,7 @@ void BlockTemplate::fill_optimal_knapsack(const MinerData& data, uint64_t base_r
|
||||
}
|
||||
#endif
|
||||
|
||||
bool BlockTemplate::create_miner_tx(const MinerData& data, const std::vector<MinerShare>& shares, uint64_t max_reward_amounts_weight, bool dry_run)
|
||||
int BlockTemplate::create_miner_tx(const MinerData& data, const std::vector<MinerShare>& shares, uint64_t max_reward_amounts_weight, bool dry_run)
|
||||
{
|
||||
// Miner transaction (coinbase)
|
||||
m_minerTx.clear();
|
||||
@@ -630,6 +688,8 @@ bool BlockTemplate::create_miner_tx(const MinerData& data, const std::vector<Min
|
||||
m_poolBlockTemplate->m_outputs.clear();
|
||||
m_poolBlockTemplate->m_outputs.reserve(num_outputs);
|
||||
|
||||
const uint8_t tx_type = m_poolBlockTemplate->get_tx_type();
|
||||
|
||||
uint64_t reward_amounts_weight = 0;
|
||||
for (size_t i = 0; i < num_outputs; ++i) {
|
||||
writeVarint(m_rewards[i], [this, &reward_amounts_weight](uint8_t b)
|
||||
@@ -637,30 +697,36 @@ bool BlockTemplate::create_miner_tx(const MinerData& data, const std::vector<Min
|
||||
m_minerTx.push_back(b);
|
||||
++reward_amounts_weight;
|
||||
});
|
||||
m_minerTx.push_back(TXOUT_TO_KEY);
|
||||
m_minerTx.push_back(tx_type);
|
||||
|
||||
uint8_t view_tag = 0;
|
||||
|
||||
if (dry_run) {
|
||||
m_minerTx.insert(m_minerTx.end(), HASH_SIZE, 0);
|
||||
}
|
||||
else {
|
||||
hash eph_public_key;
|
||||
if (!shares[i].m_wallet->get_eph_public_key(m_txkeySec, i, eph_public_key)) {
|
||||
if (!shares[i].m_wallet->get_eph_public_key(m_txkeySec, i, eph_public_key, view_tag)) {
|
||||
LOGERR(1, "get_eph_public_key failed at index " << i);
|
||||
}
|
||||
m_minerTx.insert(m_minerTx.end(), eph_public_key.h, eph_public_key.h + HASH_SIZE);
|
||||
m_poolBlockTemplate->m_outputs.emplace_back(m_rewards[i], eph_public_key);
|
||||
m_poolBlockTemplate->m_outputs.emplace_back(m_rewards[i], eph_public_key, tx_type, view_tag);
|
||||
}
|
||||
|
||||
if (tx_type == TXOUT_TO_TAGGED_KEY) {
|
||||
m_minerTx.emplace_back(view_tag);
|
||||
}
|
||||
}
|
||||
|
||||
if (dry_run) {
|
||||
if (reward_amounts_weight != max_reward_amounts_weight) {
|
||||
LOGERR(1, "create_miner_tx: incorrect miner rewards during the dry run (" << reward_amounts_weight << " != " << max_reward_amounts_weight << ")");
|
||||
return false;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else if (reward_amounts_weight > max_reward_amounts_weight) {
|
||||
LOGERR(1, "create_miner_tx: incorrect miner rewards during the real run (" << reward_amounts_weight << " > " << max_reward_amounts_weight << ")");
|
||||
return false;
|
||||
return -2;
|
||||
}
|
||||
|
||||
m_poolBlockTemplate->m_txkeyPub = m_txkeyPub;
|
||||
@@ -676,6 +742,10 @@ bool BlockTemplate::create_miner_tx(const MinerData& data, const std::vector<Min
|
||||
|
||||
const uint64_t corrected_extra_nonce_size = EXTRA_NONCE_SIZE + max_reward_amounts_weight - reward_amounts_weight;
|
||||
if (corrected_extra_nonce_size > EXTRA_NONCE_SIZE) {
|
||||
if (corrected_extra_nonce_size > EXTRA_NONCE_MAX_SIZE) {
|
||||
LOGWARN(4, "create_miner_tx: corrected_extra_nonce_size (" << corrected_extra_nonce_size << ") is too large");
|
||||
return -3;
|
||||
}
|
||||
LOGINFO(4, "increased EXTRA_NONCE from " << EXTRA_NONCE_SIZE << " to " << corrected_extra_nonce_size << " bytes to maintain miner tx weight");
|
||||
}
|
||||
writeVarint(corrected_extra_nonce_size, m_minerTxExtra);
|
||||
@@ -701,7 +771,7 @@ bool BlockTemplate::create_miner_tx(const MinerData& data, const std::vector<Min
|
||||
// Not a part of transaction hash data
|
||||
m_minerTx.push_back(0);
|
||||
|
||||
return true;
|
||||
return 1;
|
||||
}
|
||||
|
||||
hash BlockTemplate::calc_sidechain_hash() const
|
||||
@@ -1002,7 +1072,7 @@ void BlockTemplate::submit_sidechain_block(uint32_t template_id, uint32_t nonce,
|
||||
m_poolBlockTemplate->m_nonce = nonce;
|
||||
m_poolBlockTemplate->m_extraNonce = extra_nonce;
|
||||
memcpy(m_poolBlockTemplate->m_mainChainData.data() + m_nonceOffset, &nonce, NONCE_SIZE);
|
||||
memcpy(m_poolBlockTemplate->m_mainChainData.data() + m_extraNonceOffsetInTemplate, &extra_nonce, NONCE_SIZE);
|
||||
memcpy(m_poolBlockTemplate->m_mainChainData.data() + m_extraNonceOffsetInTemplate, &extra_nonce, EXTRA_NONCE_SIZE);
|
||||
|
||||
SideChain& side_chain = m_pool->side_chain();
|
||||
|
||||
@@ -1018,7 +1088,7 @@ void BlockTemplate::submit_sidechain_block(uint32_t template_id, uint32_t nonce,
|
||||
}
|
||||
|
||||
hash pow_hash;
|
||||
if (!check.get_pow_hash(m_pool->hasher(), m_seedHash, pow_hash)) {
|
||||
if (!check.get_pow_hash(m_pool->hasher(), check.m_txinGenHeight, m_seedHash, pow_hash)) {
|
||||
LOGERR(1, "PoW check failed for the sidechain block. Fix it! ");
|
||||
}
|
||||
else if (!check.m_difficulty.check_pow(pow_hash)) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -50,18 +50,17 @@ public:
|
||||
void update_tx_keys();
|
||||
|
||||
FORCEINLINE uint64_t height() const { return m_height; }
|
||||
FORCEINLINE time_t timestamp() const { return m_timestamp; }
|
||||
FORCEINLINE difficulty_type difficulty() const { return m_difficulty; }
|
||||
|
||||
void submit_sidechain_block(uint32_t template_id, uint32_t nonce, uint32_t extra_nonce);
|
||||
|
||||
FORCEINLINE uint64_t final_reward() const { return m_finalReward; }
|
||||
FORCEINLINE const std::vector<MinerShare>& shares() const { return m_shares; }
|
||||
|
||||
private:
|
||||
p2pool* m_pool;
|
||||
|
||||
private:
|
||||
bool create_miner_tx(const MinerData& data, const std::vector<MinerShare>& shares, uint64_t max_reward_amounts_weight, bool dry_run);
|
||||
int create_miner_tx(const MinerData& data, const std::vector<MinerShare>& shares, uint64_t max_reward_amounts_weight, bool dry_run);
|
||||
hash calc_sidechain_hash() const;
|
||||
hash calc_miner_tx_hash(uint32_t extra_nonce) const;
|
||||
void calc_merkle_tree_main_branch();
|
||||
@@ -83,7 +82,7 @@ private:
|
||||
|
||||
size_t m_numTransactionHashes;
|
||||
hash m_prevId;
|
||||
uint64_t m_height;
|
||||
std::atomic<uint64_t> m_height;
|
||||
difficulty_type m_difficulty;
|
||||
hash m_seedHash;
|
||||
|
||||
|
||||
+60
-5
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -19,22 +19,25 @@
|
||||
|
||||
#ifdef _MSC_VER
|
||||
|
||||
#pragma warning(disable : 4005 4061 4365 4464 4625 4626 4668 4710 4711 4804 4820 5039 5045 5220)
|
||||
#pragma warning(disable : 4005 4061 4365 4464 4625 4626 4668 4710 4711 4804 4820 5039 5045 5220 5246)
|
||||
#define FORCEINLINE __forceinline
|
||||
#define NOINLINE __declspec(noinline)
|
||||
#define LIKELY(expression) expression
|
||||
#define MSVC_PRAGMA(...) __pragma(__VA_ARGS__)
|
||||
|
||||
#elif __GNUC__
|
||||
|
||||
#define FORCEINLINE __attribute__((always_inline)) inline
|
||||
#define NOINLINE __attribute__((noinline))
|
||||
#define LIKELY(expression) __builtin_expect(expression, 1)
|
||||
#define MSVC_PRAGMA(...)
|
||||
|
||||
#else
|
||||
|
||||
#define FORCEINLINE inline
|
||||
#define NOINLINE
|
||||
#define LIKELY(expression) expression
|
||||
#define MSVC_PRAGMA(...)
|
||||
|
||||
#endif
|
||||
|
||||
@@ -54,6 +57,7 @@
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
@@ -80,13 +84,16 @@
|
||||
namespace p2pool {
|
||||
|
||||
constexpr size_t HASH_SIZE = 32;
|
||||
constexpr uint8_t HARDFORK_SUPPORTED_VERSION = 14;
|
||||
constexpr uint8_t HARDFORK_VIEW_TAGS_VERSION = 15;
|
||||
constexpr uint8_t HARDFORK_SUPPORTED_VERSION = 16;
|
||||
constexpr uint8_t MINER_REWARD_UNLOCK_TIME = 60;
|
||||
constexpr uint8_t NONCE_SIZE = 4;
|
||||
constexpr uint8_t EXTRA_NONCE_SIZE = 4;
|
||||
constexpr uint8_t EXTRA_NONCE_MAX_SIZE = EXTRA_NONCE_SIZE + 10;
|
||||
constexpr uint8_t TX_VERSION = 2;
|
||||
constexpr uint8_t TXIN_GEN = 0xFF;
|
||||
constexpr uint8_t TXOUT_TO_KEY = 2;
|
||||
constexpr uint8_t TXOUT_TO_TAGGED_KEY = 3;
|
||||
constexpr uint8_t TX_EXTRA_TAG_PUBKEY = 1;
|
||||
constexpr uint8_t TX_EXTRA_NONCE = 2;
|
||||
constexpr uint8_t TX_EXTRA_MERGE_MINING_TAG = 3;
|
||||
@@ -113,6 +120,8 @@ FORCEINLINE uint64_t udiv128(uint64_t hi, uint64_t lo, uint64_t divisor, uint64_
|
||||
}
|
||||
#endif
|
||||
|
||||
template<typename T> FORCEINLINE T round_up(T a, size_t granularity) { return static_cast<T>(((a + (granularity - static_cast<size_t>(1))) / granularity) * granularity); }
|
||||
|
||||
struct hash
|
||||
{
|
||||
alignas(8) uint8_t h[HASH_SIZE];
|
||||
@@ -192,11 +201,16 @@ struct difficulty_type
|
||||
FORCEINLINE bool operator==(const difficulty_type& other) const { return (lo == other.lo) && (hi == other.hi); }
|
||||
FORCEINLINE bool operator!=(const difficulty_type& other) const { return (lo != other.lo) || (hi != other.hi); }
|
||||
|
||||
FORCEINLINE bool operator==(uint64_t other) const { return (lo == other) && (hi == 0); }
|
||||
FORCEINLINE bool operator!=(uint64_t other) const { return (lo != other) || (hi != 0); }
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& s, const difficulty_type& d);
|
||||
friend std::istream& operator>>(std::istream& s, difficulty_type& d);
|
||||
|
||||
FORCEINLINE double to_double() const { return static_cast<double>(hi) * 18446744073709551616.0 + static_cast<double>(lo); }
|
||||
|
||||
FORCEINLINE bool empty() const { return (lo == 0) && (hi == 0); }
|
||||
|
||||
// Finds a 64-bit target for mining (target = 2^64 / difficulty) and rounds up the result of division
|
||||
// Because of that, there's a very small chance that miners will find a hash that meets the target but is still wrong (hash * difficulty >= 2^256)
|
||||
// A proper difficulty check is in check_pow()
|
||||
@@ -232,7 +246,7 @@ struct TxMempoolData
|
||||
uint64_t blob_size;
|
||||
uint64_t weight;
|
||||
uint64_t fee;
|
||||
time_t time_received;
|
||||
uint64_t time_received;
|
||||
};
|
||||
|
||||
struct MinerData
|
||||
@@ -258,7 +272,7 @@ struct MinerData
|
||||
uint64_t median_timestamp;
|
||||
std::vector<TxMempoolData> tx_backlog;
|
||||
|
||||
std::chrono::system_clock::time_point time_received;
|
||||
std::chrono::high_resolution_clock::time_point time_received;
|
||||
};
|
||||
|
||||
struct ChainMain
|
||||
@@ -279,6 +293,47 @@ enum class NetworkType {
|
||||
Stagenet,
|
||||
};
|
||||
|
||||
struct raw_ip
|
||||
{
|
||||
alignas(8) uint8_t data[16];
|
||||
|
||||
FORCEINLINE bool operator<(const raw_ip& other) const
|
||||
{
|
||||
const uint64_t* a = reinterpret_cast<const uint64_t*>(data);
|
||||
const uint64_t* b = reinterpret_cast<const uint64_t*>(other.data);
|
||||
|
||||
if (a[1] < b[1]) return true;
|
||||
if (a[1] > b[1]) return false;
|
||||
|
||||
return a[0] < b[0];
|
||||
}
|
||||
|
||||
FORCEINLINE bool operator==(const raw_ip& other) const
|
||||
{
|
||||
const uint64_t* a = reinterpret_cast<const uint64_t*>(data);
|
||||
const uint64_t* b = reinterpret_cast<const uint64_t*>(other.data);
|
||||
|
||||
return (a[0] == b[0]) && (a[1] == b[1]);
|
||||
}
|
||||
|
||||
FORCEINLINE bool operator!=(const raw_ip& other) const { return !operator==(other); }
|
||||
|
||||
FORCEINLINE bool is_localhost() const
|
||||
{
|
||||
static constexpr raw_ip localhost_ipv4 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01 };
|
||||
static constexpr raw_ip localhost_ipv6 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
|
||||
|
||||
return (*this == localhost_ipv4) || (*this == localhost_ipv6);
|
||||
}
|
||||
};
|
||||
|
||||
static_assert(sizeof(raw_ip) == 16, "struct raw_ip has invalid size");
|
||||
|
||||
void* malloc_hook(size_t n) noexcept;
|
||||
void* realloc_hook(void* ptr, size_t size) noexcept;
|
||||
void* calloc_hook(size_t count, size_t size) noexcept;
|
||||
void free_hook(void* p) noexcept;
|
||||
|
||||
} // namespace p2pool
|
||||
|
||||
#include "util.h"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021 hyc <https://github.com/hyc>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
@@ -21,9 +21,16 @@
|
||||
#include "p2pool.h"
|
||||
#include "stratum_server.h"
|
||||
#include "p2p_server.h"
|
||||
#ifdef WITH_RANDOMX
|
||||
#include "miner.h"
|
||||
#endif
|
||||
#include "side_chain.h"
|
||||
#include <iostream>
|
||||
|
||||
#ifdef HAVE_PTHREAD_CANCEL
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
static constexpr char log_category_prefix[] = "ConsoleCommands ";
|
||||
|
||||
namespace p2pool {
|
||||
@@ -42,7 +49,7 @@ ConsoleCommands::~ConsoleCommands()
|
||||
|
||||
#ifdef _WIN32
|
||||
TerminateThread(reinterpret_cast<HANDLE>(m_worker->native_handle()), 0);
|
||||
#else
|
||||
#elif defined HAVE_PTHREAD_CANCEL
|
||||
pthread_cancel(m_worker->native_handle());
|
||||
#endif
|
||||
|
||||
@@ -69,7 +76,11 @@ typedef struct cmd {
|
||||
cmdfunc *func;
|
||||
} cmd;
|
||||
|
||||
static cmdfunc do_help, do_status, do_loglevel, do_addpeers, do_droppeers, do_exit;
|
||||
static cmdfunc do_help, do_status, do_loglevel, do_addpeers, do_droppeers, do_showpeers, do_showworkers, do_showbans, do_outpeers, do_inpeers, do_exit;
|
||||
|
||||
#ifdef WITH_RANDOMX
|
||||
static cmdfunc do_start_mining, do_stop_mining;
|
||||
#endif
|
||||
|
||||
static cmd cmds[] = {
|
||||
{ STRCONST("help"), "", "display list of commands", do_help },
|
||||
@@ -77,17 +88,25 @@ static cmd cmds[] = {
|
||||
{ STRCONST("loglevel"), "<level>", "set log level", do_loglevel },
|
||||
{ STRCONST("addpeers"), "<peeraddr>", "add peer", do_addpeers },
|
||||
{ STRCONST("droppeers"), "", "disconnect all peers", do_droppeers },
|
||||
{ STRCONST("peers"), "", "show all peers", do_showpeers },
|
||||
{ STRCONST("workers"), "", "show all connected workers", do_showworkers },
|
||||
{ STRCONST("bans"), "", "show all banned IPs", do_showbans },
|
||||
{ STRCONST("outpeers"), "", "set maximum number of outgoing connections", do_outpeers },
|
||||
{ STRCONST("inpeers"), "", "set maximum number of incoming connections", do_inpeers },
|
||||
#ifdef WITH_RANDOMX
|
||||
{ STRCONST("start_mining"), "<threads>", "start mining", do_start_mining },
|
||||
{ STRCONST("stop_mining"), "", "stop mining", do_stop_mining },
|
||||
#endif
|
||||
{ STRCONST("exit"), "", "terminate p2pool", do_exit },
|
||||
{ STRCNULL, NULL, NULL, NULL }
|
||||
};
|
||||
|
||||
static int do_help(p2pool * /* m_pool */, const char * /* args */)
|
||||
{
|
||||
int i;
|
||||
|
||||
LOGINFO(0, "List of commands");
|
||||
for (i=0; cmds[i].name.len; i++)
|
||||
for (int i = 0; cmds[i].name.len; ++i) {
|
||||
LOGINFO(0, cmds[i].name.str << " " << cmds[i].arg << "\t" << cmds[i].descr);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -100,13 +119,16 @@ static int do_status(p2pool *m_pool, const char * /* args */)
|
||||
if (m_pool->p2p_server()) {
|
||||
m_pool->p2p_server()->print_status();
|
||||
}
|
||||
#ifdef WITH_RANDOMX
|
||||
m_pool->print_miner_status();
|
||||
#endif
|
||||
bkg_jobs_tracker.print_status();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_loglevel(p2pool * /* m_pool */, const char *args)
|
||||
{
|
||||
int level = atoi(args);
|
||||
int level = strtol(args, nullptr, 10);
|
||||
level = std::min(std::max(level, 0), log::MAX_GLOBAL_LOG_LEVEL);
|
||||
log::GLOBAL_LOG_LEVEL = level;
|
||||
LOGINFO(0, "log level set to " << level);
|
||||
@@ -129,6 +151,67 @@ static int do_droppeers(p2pool *m_pool, const char * /* args */)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_showpeers(p2pool* m_pool, const char* /* args */)
|
||||
{
|
||||
if (m_pool->p2p_server()) {
|
||||
m_pool->p2p_server()->show_peers();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_showworkers(p2pool* m_pool, const char* /* args */)
|
||||
{
|
||||
if (m_pool->stratum_server()) {
|
||||
m_pool->stratum_server()->show_workers();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_showbans(p2pool* m_pool, const char* /* args */)
|
||||
{
|
||||
if (m_pool->stratum_server()) {
|
||||
m_pool->stratum_server()->print_bans();
|
||||
}
|
||||
if (m_pool->p2p_server()) {
|
||||
m_pool->p2p_server()->print_bans();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_outpeers(p2pool* m_pool, const char* args)
|
||||
{
|
||||
if (m_pool->p2p_server()) {
|
||||
m_pool->p2p_server()->set_max_outgoing_peers(strtoul(args, nullptr, 10));
|
||||
LOGINFO(0, "max outgoing peers set to " << m_pool->p2p_server()->max_outgoing_peers());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_inpeers(p2pool* m_pool, const char* args)
|
||||
{
|
||||
if (m_pool->p2p_server()) {
|
||||
m_pool->p2p_server()->set_max_incoming_peers(strtoul(args, nullptr, 10));
|
||||
LOGINFO(0, "max incoming peers set to " << m_pool->p2p_server()->max_incoming_peers());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef WITH_RANDOMX
|
||||
static int do_start_mining(p2pool* m_pool, const char* args)
|
||||
{
|
||||
uint32_t threads = strtoul(args, nullptr, 10);
|
||||
threads = std::min(std::max(threads, 1u), 64u);
|
||||
m_pool->start_mining(threads);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_stop_mining(p2pool* m_pool, const char* /*args*/)
|
||||
{
|
||||
m_pool->stop_mining();
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int do_exit(p2pool *m_pool, const char * /* args */)
|
||||
{
|
||||
bkg_jobs_tracker.wait();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
|
||||
+46
-24
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -19,8 +19,6 @@
|
||||
#include "crypto.h"
|
||||
#include "keccak.h"
|
||||
#include "uv_util.h"
|
||||
#include <random>
|
||||
#include <unordered_map>
|
||||
|
||||
extern "C" {
|
||||
#include "crypto-ops.h"
|
||||
@@ -33,9 +31,12 @@ namespace {
|
||||
class RandomBytes
|
||||
{
|
||||
public:
|
||||
RandomBytes() : rd(), rng(rd()), dist(0, 255)
|
||||
RandomBytes() : rng(RandomDeviceSeed::instance), dist(0, 255)
|
||||
{
|
||||
uv_mutex_init_checked(&m);
|
||||
|
||||
// Diffuse the initial state in case it has low quality
|
||||
rng.discard(10000);
|
||||
}
|
||||
|
||||
~RandomBytes()
|
||||
@@ -55,7 +56,6 @@ public:
|
||||
private:
|
||||
uv_mutex_t m;
|
||||
|
||||
std::random_device rd;
|
||||
std::mt19937_64 rng;
|
||||
std::uniform_int_distribution<> dist;
|
||||
};
|
||||
@@ -109,9 +109,9 @@ bool check_keys(const hash& pub, const hash& sec)
|
||||
return pub == pub_check;
|
||||
}
|
||||
|
||||
static FORCEINLINE void hash_to_scalar(const uint8_t* data, size_t length, uint8_t(&res)[HASH_SIZE])
|
||||
static FORCEINLINE void hash_to_scalar(const uint8_t* data, int length, uint8_t (&res)[HASH_SIZE])
|
||||
{
|
||||
keccak(data, static_cast<int>(length), res, HASH_SIZE);
|
||||
keccak(data, length, res, HASH_SIZE);
|
||||
sc_reduce32(res);
|
||||
}
|
||||
|
||||
@@ -122,18 +122,13 @@ static FORCEINLINE void derivation_to_scalar(const hash& derivation, size_t outp
|
||||
uint8_t output_index[(sizeof(size_t) * 8 + 6) / 7];
|
||||
} buf;
|
||||
|
||||
uint8_t* begin = buf.derivation;
|
||||
uint8_t* end = buf.output_index;
|
||||
memcpy(buf.derivation, derivation.h, sizeof(buf.derivation));
|
||||
|
||||
size_t k = output_index;
|
||||
while (k >= 0x80) {
|
||||
*(end++) = (static_cast<uint8_t>(k) & 0x7F) | 0x80;
|
||||
k >>= 7;
|
||||
}
|
||||
*(end++) = static_cast<uint8_t>(k);
|
||||
uint8_t* p = buf.output_index;
|
||||
writeVarint(output_index, [&p](uint8_t b) { *(p++) = b; });
|
||||
|
||||
hash_to_scalar(begin, end - begin, res);
|
||||
const uint8_t* data = buf.derivation;
|
||||
hash_to_scalar(data, static_cast<int>(p - data), res);
|
||||
}
|
||||
|
||||
class Cache
|
||||
@@ -149,17 +144,19 @@ public:
|
||||
uv_mutex_destroy(&m);
|
||||
}
|
||||
|
||||
bool get_derivation(const hash& key1, const hash& key2, hash& derivation)
|
||||
bool get_derivation(const hash& key1, const hash& key2, size_t output_index, hash& derivation, uint8_t& view_tag)
|
||||
{
|
||||
std::array<uint8_t, HASH_SIZE * 2> index;
|
||||
std::array<uint8_t, HASH_SIZE * 2 + sizeof(size_t)> index;
|
||||
memcpy(index.data(), key1.h, HASH_SIZE);
|
||||
memcpy(index.data() + HASH_SIZE, key2.h, HASH_SIZE);
|
||||
memcpy(index.data() + HASH_SIZE * 2, &output_index, sizeof(size_t));
|
||||
|
||||
{
|
||||
MutexLock lock(m);
|
||||
auto it = derivations.find(index);
|
||||
if (it != derivations.end()) {
|
||||
derivation = it->second;
|
||||
derivation = it->second.derivation;
|
||||
view_tag = it->second.view_tag;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -177,9 +174,11 @@ public:
|
||||
ge_p1p1_to_p2(&point2, &point3);
|
||||
ge_tobytes(reinterpret_cast<uint8_t*>(&derivation), &point2);
|
||||
|
||||
derive_view_tag(derivation, output_index, view_tag);
|
||||
|
||||
{
|
||||
MutexLock lock(m);
|
||||
derivations.emplace(index, derivation);
|
||||
derivations.emplace(index, DerivationEntry{ derivation, view_tag } );
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -236,16 +235,23 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
struct DerivationEntry
|
||||
{
|
||||
hash derivation;
|
||||
// cppcheck-suppress unusedStructMember
|
||||
uint8_t view_tag;
|
||||
};
|
||||
|
||||
uv_mutex_t m;
|
||||
std::unordered_map<std::array<uint8_t, HASH_SIZE * 2>, hash> derivations;
|
||||
std::unordered_map<std::array<uint8_t, HASH_SIZE * 2 + sizeof(size_t)>, hash> public_keys;
|
||||
unordered_map<std::array<uint8_t, HASH_SIZE * 2 + sizeof(size_t)>, DerivationEntry> derivations;
|
||||
unordered_map<std::array<uint8_t, HASH_SIZE * 2 + sizeof(size_t)>, hash> public_keys;
|
||||
};
|
||||
|
||||
static Cache* cache = nullptr;
|
||||
|
||||
bool generate_key_derivation(const hash& key1, const hash& key2, hash& derivation)
|
||||
bool generate_key_derivation(const hash& key1, const hash& key2, size_t output_index, hash& derivation, uint8_t& view_tag)
|
||||
{
|
||||
return cache->get_derivation(key1, key2, derivation);
|
||||
return cache->get_derivation(key1, key2, output_index, derivation, view_tag);
|
||||
}
|
||||
|
||||
bool derive_public_key(const hash& derivation, size_t output_index, const hash& base, hash& derived_key)
|
||||
@@ -253,6 +259,22 @@ bool derive_public_key(const hash& derivation, size_t output_index, const hash&
|
||||
return cache->get_public_key(derivation, output_index, base, derived_key);
|
||||
}
|
||||
|
||||
void derive_view_tag(const hash& derivation, size_t output_index, uint8_t& view_tag)
|
||||
{
|
||||
constexpr uint8_t salt[] = "view_tag";
|
||||
constexpr size_t SALT_SIZE = sizeof(salt) - 1;
|
||||
|
||||
uint8_t buf[64];
|
||||
memcpy(buf, salt, SALT_SIZE);
|
||||
memcpy(buf + SALT_SIZE, derivation.h, HASH_SIZE);
|
||||
uint8_t* p = buf + SALT_SIZE + HASH_SIZE;
|
||||
writeVarint(output_index, [&p](uint8_t b) { *(p++) = b; });
|
||||
|
||||
hash view_tag_full;
|
||||
keccak(buf, static_cast<int>(p - buf), view_tag_full.h, HASH_SIZE);
|
||||
view_tag = view_tag_full.h[0];
|
||||
}
|
||||
|
||||
void init_crypto_cache()
|
||||
{
|
||||
if (!cache) {
|
||||
|
||||
+3
-2
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -21,8 +21,9 @@ namespace p2pool {
|
||||
|
||||
void generate_keys(hash& pub, hash& sec);
|
||||
bool check_keys(const hash& pub, const hash& sec);
|
||||
bool generate_key_derivation(const hash& key1, const hash& key2, hash& derivation);
|
||||
bool generate_key_derivation(const hash& key1, const hash& key2, size_t output_index, hash& derivation, uint8_t& view_tag);
|
||||
bool derive_public_key(const hash& derivation, size_t output_index, const hash& base, hash& derived_key);
|
||||
void derive_view_tag(const hash& derivation, size_t output_index, uint8_t& view_tag);
|
||||
|
||||
void init_crypto_cache();
|
||||
void destroy_crypto_cache();
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -25,7 +25,7 @@ static constexpr char log_category_prefix[] = "JSONRPCRequest ";
|
||||
|
||||
namespace p2pool {
|
||||
|
||||
JSONRPCRequest::JSONRPCRequest(const char* address, int port, const char* req, CallbackBase* cb, CallbackBase* close_cb)
|
||||
JSONRPCRequest::JSONRPCRequest(const char* address, int port, const char* req, CallbackBase* cb, CallbackBase* close_cb, uv_loop_t* loop)
|
||||
: m_socket{}
|
||||
, m_connect{}
|
||||
, m_write{}
|
||||
@@ -38,7 +38,7 @@ JSONRPCRequest::JSONRPCRequest(const char* address, int port, const char* req, C
|
||||
{
|
||||
m_readBuf[0] = '\0';
|
||||
|
||||
uv_tcp_init(uv_default_loop_checked(), &m_socket);
|
||||
uv_tcp_init(loop ? loop : uv_default_loop_checked(), &m_socket);
|
||||
uv_tcp_nodelay(&m_socket, 1);
|
||||
|
||||
sockaddr_storage addr;
|
||||
@@ -55,13 +55,25 @@ JSONRPCRequest::JSONRPCRequest(const char* address, int port, const char* req, C
|
||||
m_connect.data = this;
|
||||
m_write.data = this;
|
||||
|
||||
const size_t len = strlen(req);
|
||||
const char* uri = "/json_rpc";
|
||||
|
||||
size_t len = req ? strlen(req) : 0;
|
||||
if (!len) {
|
||||
LOGERR(1, "Empty JSONRPCRequest, fix the code!");
|
||||
m_valid = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (req[0] == '/') {
|
||||
uri = req;
|
||||
len = 0;
|
||||
}
|
||||
|
||||
m_request.reserve(std::max<size_t>(len + 128, log::Stream::BUF_SIZE + 1));
|
||||
m_request.resize(log::Stream::BUF_SIZE + 1);
|
||||
|
||||
log::Stream s(m_request.data());
|
||||
s << "POST /json_rpc HTTP/1.1\nContent-Type: application/json\nContent-Length: " << len << "\n\n";
|
||||
log::Stream s(m_request.data(), m_request.size());
|
||||
s << "POST " << uri << " HTTP/1.1\nContent-Type: application/json\nContent-Length: " << len << "\n\n";
|
||||
|
||||
m_request.resize(s.m_pos);
|
||||
m_request.insert(m_request.end(), req, req + len);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -26,20 +26,21 @@ public:
|
||||
static FORCEINLINE void call(const char* address, int port, const char* req, T&& cb)
|
||||
{
|
||||
// It will be deleted in one of the tcp callbacks eventually
|
||||
JSONRPCRequest* r = new JSONRPCRequest(address, port, req, new Callback<T>(std::move(cb)), nullptr);
|
||||
JSONRPCRequest* r = new JSONRPCRequest(address, port, req, new Callback<T>(std::move(cb)), nullptr, nullptr);
|
||||
if (!r->m_valid) {
|
||||
delete r;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
static FORCEINLINE void call(const char* address, int port, const char* req, T&& cb, U&& close_cb)
|
||||
static FORCEINLINE void call(const char* address, int port, const char* req, T&& cb, U&& close_cb, uv_loop_t* loop = nullptr)
|
||||
{
|
||||
// It will be deleted in one of the tcp callbacks eventually
|
||||
JSONRPCRequest* r = new JSONRPCRequest(address, port, req, new Callback<T>(std::move(cb)), new Callback<U>(std::move(close_cb)));
|
||||
CallbackBase* close_callback = new Callback<U>(std::move(close_cb));
|
||||
JSONRPCRequest* r = new JSONRPCRequest(address, port, req, new Callback<T>(std::move(cb)), close_callback, loop);
|
||||
if (!r->m_valid) {
|
||||
constexpr char err[] = "internal error";
|
||||
close_cb(err, sizeof(err) - 1);
|
||||
(*close_callback)(err, sizeof(err) - 1);
|
||||
delete r;
|
||||
}
|
||||
}
|
||||
@@ -62,7 +63,7 @@ private:
|
||||
T m_cb;
|
||||
};
|
||||
|
||||
JSONRPCRequest(const char* address, int port, const char* req, CallbackBase* cb, CallbackBase* close_cb);
|
||||
JSONRPCRequest(const char* address, int port, const char* req, CallbackBase* cb, CallbackBase* close_cb, uv_loop_t* loop);
|
||||
~JSONRPCRequest();
|
||||
|
||||
static void on_connect(uv_connect_t* req, int status);
|
||||
@@ -70,6 +71,7 @@ private:
|
||||
static void on_alloc(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
|
||||
static void on_read(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf);
|
||||
void on_read(const char* data, size_t size);
|
||||
// cppcheck-suppress functionConst
|
||||
void close();
|
||||
static void on_close(uv_handle_t* handle);
|
||||
|
||||
|
||||
+34
-63
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -48,17 +48,23 @@ NOINLINE void keccakf(uint64_t* st)
|
||||
bc[3] = st[3] ^ st[8] ^ st[13] ^ st[18] ^ st[23];
|
||||
bc[4] = st[4] ^ st[9] ^ st[14] ^ st[19] ^ st[24];
|
||||
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
uint64_t t = bc[(i + 4) % 5] ^ ROTL64(bc[(i + 1) % 5], 1);
|
||||
st[i + 0 ] ^= t;
|
||||
st[i + 5] ^= t;
|
||||
st[i + 10] ^= t;
|
||||
st[i + 15] ^= t;
|
||||
st[i + 20] ^= t;
|
||||
#define THETA(i) { \
|
||||
const uint64_t t = bc[(i + 4) % 5] ^ ROTL64(bc[(i + 1) % 5], 1); \
|
||||
st[i + 0 ] ^= t; \
|
||||
st[i + 5] ^= t; \
|
||||
st[i + 10] ^= t; \
|
||||
st[i + 15] ^= t; \
|
||||
st[i + 20] ^= t; \
|
||||
}
|
||||
|
||||
THETA(0);
|
||||
THETA(1);
|
||||
THETA(2);
|
||||
THETA(3);
|
||||
THETA(4);
|
||||
|
||||
// Rho Pi
|
||||
uint64_t t = st[1];
|
||||
const uint64_t t = st[1];
|
||||
st[ 1] = ROTL64(st[ 6], 44);
|
||||
st[ 6] = ROTL64(st[ 9], 20);
|
||||
st[ 9] = ROTL64(st[22], 61);
|
||||
@@ -85,60 +91,25 @@ NOINLINE void keccakf(uint64_t* st)
|
||||
st[10] = ROTL64(t, 1);
|
||||
|
||||
// Chi
|
||||
// unrolled loop, where only last iteration is different
|
||||
int j = 0;
|
||||
bc[0] = st[j + 0];
|
||||
bc[1] = st[j + 1];
|
||||
#define CHI(j) { \
|
||||
const uint64_t st0 = st[j ]; \
|
||||
const uint64_t st1 = st[j + 1]; \
|
||||
const uint64_t st2 = st[j + 2]; \
|
||||
const uint64_t st3 = st[j + 3]; \
|
||||
const uint64_t st4 = st[j + 4]; \
|
||||
st[j ] ^= ~st1 & st2; \
|
||||
st[j + 1] ^= ~st2 & st3; \
|
||||
st[j + 2] ^= ~st3 & st4; \
|
||||
st[j + 3] ^= ~st4 & st0; \
|
||||
st[j + 4] ^= ~st0 & st1; \
|
||||
}
|
||||
|
||||
st[j + 0] ^= (~st[j + 1]) & st[j + 2];
|
||||
st[j + 1] ^= (~st[j + 2]) & st[j + 3];
|
||||
st[j + 2] ^= (~st[j + 3]) & st[j + 4];
|
||||
st[j + 3] ^= (~st[j + 4]) & bc[0];
|
||||
st[j + 4] ^= (~bc[0]) & bc[1];
|
||||
CHI( 0);
|
||||
CHI( 5);
|
||||
CHI(10);
|
||||
CHI(15);
|
||||
CHI(20);
|
||||
|
||||
j = 5;
|
||||
bc[0] = st[j + 0];
|
||||
bc[1] = st[j + 1];
|
||||
|
||||
st[j + 0] ^= (~st[j + 1]) & st[j + 2];
|
||||
st[j + 1] ^= (~st[j + 2]) & st[j + 3];
|
||||
st[j + 2] ^= (~st[j + 3]) & st[j + 4];
|
||||
st[j + 3] ^= (~st[j + 4]) & bc[0];
|
||||
st[j + 4] ^= (~bc[0]) & bc[1];
|
||||
|
||||
j = 10;
|
||||
bc[0] = st[j + 0];
|
||||
bc[1] = st[j + 1];
|
||||
|
||||
st[j + 0] ^= (~st[j + 1]) & st[j + 2];
|
||||
st[j + 1] ^= (~st[j + 2]) & st[j + 3];
|
||||
st[j + 2] ^= (~st[j + 3]) & st[j + 4];
|
||||
st[j + 3] ^= (~st[j + 4]) & bc[0];
|
||||
st[j + 4] ^= (~bc[0]) & bc[1];
|
||||
|
||||
j = 15;
|
||||
bc[0] = st[j + 0];
|
||||
bc[1] = st[j + 1];
|
||||
|
||||
st[j + 0] ^= (~st[j + 1]) & st[j + 2];
|
||||
st[j + 1] ^= (~st[j + 2]) & st[j + 3];
|
||||
st[j + 2] ^= (~st[j + 3]) & st[j + 4];
|
||||
st[j + 3] ^= (~st[j + 4]) & bc[0];
|
||||
st[j + 4] ^= (~bc[0]) & bc[1];
|
||||
|
||||
j = 20;
|
||||
bc[0] = st[j + 0];
|
||||
bc[1] = st[j + 1];
|
||||
bc[2] = st[j + 2];
|
||||
bc[3] = st[j + 3];
|
||||
bc[4] = st[j + 4];
|
||||
|
||||
st[j + 0] ^= (~bc[1]) & bc[2];
|
||||
st[j + 1] ^= (~bc[2]) & bc[3];
|
||||
st[j + 2] ^= (~bc[3]) & bc[4];
|
||||
st[j + 3] ^= (~bc[4]) & bc[0];
|
||||
st[j + 4] ^= (~bc[0]) & bc[1];
|
||||
|
||||
// Iota
|
||||
st[0] ^= keccakf_rndc[round];
|
||||
}
|
||||
@@ -155,7 +126,7 @@ NOINLINE void keccak(const uint8_t* in, int inlen, uint8_t* md, int mdlen)
|
||||
|
||||
for (; inlen >= rsiz; inlen -= rsiz, in += rsiz) {
|
||||
for (int i = 0; i < rsizw; i++) {
|
||||
st[i] ^= ((uint64_t*)in)[i];
|
||||
st[i] ^= read_unaligned(reinterpret_cast<const uint64_t*>(in) + i);
|
||||
}
|
||||
keccakf(st);
|
||||
}
|
||||
@@ -169,7 +140,7 @@ NOINLINE void keccak(const uint8_t* in, int inlen, uint8_t* md, int mdlen)
|
||||
temp[rsiz - 1] |= 0x80;
|
||||
|
||||
for (int i = 0; i < rsizw; i++) {
|
||||
st[i] ^= ((uint64_t*)temp)[i];
|
||||
st[i] ^= reinterpret_cast<uint64_t*>(temp)[i];
|
||||
}
|
||||
|
||||
keccakf(st);
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
|
||||
+83
-25
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "uv_util.h"
|
||||
#include <ctime>
|
||||
#include <fstream>
|
||||
#include <thread>
|
||||
|
||||
static constexpr char log_category_prefix[] = "Log ";
|
||||
static constexpr char log_file_name[] = "p2pool.log";
|
||||
@@ -28,13 +29,15 @@ namespace p2pool {
|
||||
namespace log {
|
||||
|
||||
int GLOBAL_LOG_LEVEL = 3;
|
||||
bool CONSOLE_COLORS = true;
|
||||
|
||||
#ifndef P2POOL_LOG_DISABLE
|
||||
|
||||
static volatile bool stopped = false;
|
||||
static std::atomic<bool> stopped{ false };
|
||||
static volatile bool worker_started = false;
|
||||
|
||||
#ifdef _WIN32
|
||||
static const HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
|
||||
static const HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
static const HANDLE hStdErr = GetStdHandle(STD_ERROR_HANDLE);
|
||||
#endif
|
||||
@@ -45,14 +48,14 @@ public:
|
||||
enum params : int
|
||||
{
|
||||
SLOT_SIZE = 1024,
|
||||
BUF_SIZE = SLOT_SIZE * 16384,
|
||||
BUF_SIZE = SLOT_SIZE * 8192,
|
||||
};
|
||||
|
||||
FORCEINLINE Worker()
|
||||
: m_writePos(0)
|
||||
, m_readPos(0)
|
||||
{
|
||||
is_main_thread = true;
|
||||
set_main_thread();
|
||||
|
||||
m_logFile.open(log_file_name, std::ios::app | std::ios::binary);
|
||||
|
||||
@@ -63,12 +66,22 @@ public:
|
||||
|
||||
uv_cond_init(&m_cond);
|
||||
uv_mutex_init(&m_mutex);
|
||||
uv_thread_create(&m_worker, run_wrapper, this);
|
||||
|
||||
do {} while (!worker_started);
|
||||
const int err = uv_thread_create(&m_worker, run_wrapper, this);
|
||||
if (err) {
|
||||
fprintf(stderr, "failed to start logger thread (%s), aborting\n", uv_err_name(err));
|
||||
abort();
|
||||
}
|
||||
|
||||
while (!worker_started) {
|
||||
std::this_thread::yield();
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
DWORD dwConsoleMode;
|
||||
if (GetConsoleMode(hStdIn, &dwConsoleMode)) {
|
||||
SetConsoleMode(hStdIn, dwConsoleMode & ~ENABLE_QUICK_EDIT_MODE);
|
||||
}
|
||||
if (GetConsoleMode(hStdOut, &dwConsoleMode)) {
|
||||
SetConsoleMode(hStdOut, dwConsoleMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
|
||||
}
|
||||
@@ -81,18 +94,21 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
FORCEINLINE ~Worker()
|
||||
~Worker()
|
||||
{
|
||||
stop();
|
||||
try {
|
||||
stop();
|
||||
}
|
||||
catch (...) {
|
||||
}
|
||||
}
|
||||
|
||||
FORCEINLINE void stop()
|
||||
{
|
||||
if (stopped) {
|
||||
if (stopped.exchange(true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
stopped = true;
|
||||
LOGINFO(0, "stopped");
|
||||
uv_thread_join(&m_worker);
|
||||
uv_cond_destroy(&m_cond);
|
||||
@@ -117,6 +133,7 @@ public:
|
||||
|
||||
memcpy(p + 1, buf + 1, size - 1);
|
||||
|
||||
// Ensure memory order in the writer thread
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
|
||||
// Mark that everything is written into this log slot
|
||||
@@ -135,18 +152,26 @@ private:
|
||||
|
||||
do {
|
||||
uv_mutex_lock(&m_mutex);
|
||||
uv_cond_wait(&m_cond, &m_mutex);
|
||||
if (m_readPos == m_writePos.load()) {
|
||||
// Nothing to do, wait for the signal
|
||||
uv_cond_wait(&m_cond, &m_mutex);
|
||||
}
|
||||
uv_mutex_unlock(&m_mutex);
|
||||
|
||||
for (uint32_t writePos = m_writePos.load(); m_readPos < writePos; writePos = m_writePos.load()) {
|
||||
for (uint32_t writePos = m_writePos.load(); m_readPos != writePos; writePos = m_writePos.load()) {
|
||||
// We have at least one log slot pending, possibly more than one
|
||||
// Process everything in a loop before reading m_writePos again
|
||||
do {
|
||||
char* p = m_buf.data() + (m_readPos % BUF_SIZE);
|
||||
|
||||
// Wait until everything is written into this log slot
|
||||
volatile char& severity = *p;
|
||||
while (!severity) {}
|
||||
volatile char& severity = p[0];
|
||||
while (!severity) {
|
||||
std::this_thread::yield();
|
||||
}
|
||||
|
||||
// Ensure memory order in the reader thread
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
|
||||
uint32_t size = static_cast<uint8_t>(p[2]);
|
||||
size = (size << 8) + static_cast<uint8_t>(p[1]);
|
||||
@@ -155,6 +180,13 @@ private:
|
||||
p += 3;
|
||||
size -= 3;
|
||||
|
||||
// Read CONSOLE_COLORS only once because its value can be changed in another thread
|
||||
const bool c = CONSOLE_COLORS;
|
||||
|
||||
if (!c) {
|
||||
strip_colors(p, size);
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
DWORD k;
|
||||
WriteConsole((severity == 1) ? hStdOut : hStdErr, p, size, &k, nullptr);
|
||||
@@ -162,17 +194,10 @@ private:
|
||||
fwrite(p, 1, size, (severity == 1) ? stdout : stderr);
|
||||
#endif
|
||||
|
||||
// Reopen the log file if it's been moved (logrotate support)
|
||||
if (m_logFile.is_open()) {
|
||||
struct stat buf;
|
||||
if (stat(log_file_name, &buf) != 0) {
|
||||
m_logFile.close();
|
||||
m_logFile.open(log_file_name, std::ios::app | std::ios::binary);
|
||||
if (c) {
|
||||
strip_colors(p, size);
|
||||
}
|
||||
}
|
||||
|
||||
if (m_logFile.is_open()) {
|
||||
strip_colors(p, size);
|
||||
|
||||
if (severity == 1) {
|
||||
m_logFile.write("NOTICE ", 8);
|
||||
@@ -185,7 +210,6 @@ private:
|
||||
}
|
||||
|
||||
m_logFile.write(p, size);
|
||||
m_logFile.flush();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -195,6 +219,18 @@ private:
|
||||
m_readPos += SLOT_SIZE;
|
||||
} while (m_readPos < writePos);
|
||||
}
|
||||
|
||||
// Flush the log file only after all pending log lines have been written
|
||||
if (m_logFile.is_open()) {
|
||||
m_logFile.flush();
|
||||
|
||||
// Reopen the log file if it's been moved (logrotate support)
|
||||
struct stat buf;
|
||||
if (stat(log_file_name, &buf) != 0) {
|
||||
m_logFile.close();
|
||||
m_logFile.open(log_file_name, std::ios::app | std::ios::binary);
|
||||
}
|
||||
}
|
||||
} while (!stopped);
|
||||
}
|
||||
|
||||
@@ -240,7 +276,6 @@ static Worker worker;
|
||||
|
||||
#endif // P2POOL_LOG_DISABLE
|
||||
|
||||
// cppcheck-suppress uninitMemberVar
|
||||
NOINLINE Writer::Writer(Severity severity) : Stream(m_stackBuf)
|
||||
{
|
||||
m_buf[0] = static_cast<char>(severity);
|
||||
@@ -301,6 +336,29 @@ NOINLINE void Stream::writeCurrentTime()
|
||||
m_numberWidth = 1;
|
||||
}
|
||||
|
||||
NOINLINE void put_rawip(const raw_ip& value, Stream* wrapper)
|
||||
{
|
||||
const char* addr_str;
|
||||
char addr_str_buf[64];
|
||||
|
||||
static constexpr uint8_t ipv4_prefix[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255 };
|
||||
const bool is_v6 = (memcmp(value.data, ipv4_prefix, 12) != 0);
|
||||
|
||||
if (is_v6) {
|
||||
addr_str = inet_ntop(AF_INET6, value.data, addr_str_buf, sizeof(addr_str_buf));
|
||||
}
|
||||
else {
|
||||
addr_str = inet_ntop(AF_INET, value.data + 12, addr_str_buf, sizeof(addr_str_buf));
|
||||
}
|
||||
|
||||
if (addr_str) {
|
||||
*wrapper << addr_str;
|
||||
}
|
||||
else {
|
||||
*wrapper << "N/A";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace log
|
||||
|
||||
} // namespace p2pool
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -22,6 +22,7 @@ namespace p2pool {
|
||||
namespace log {
|
||||
|
||||
extern int GLOBAL_LOG_LEVEL;
|
||||
extern bool CONSOLE_COLORS;
|
||||
constexpr int MAX_GLOBAL_LOG_LEVEL = 6;
|
||||
|
||||
enum class Severity {
|
||||
@@ -34,8 +35,10 @@ struct Stream
|
||||
{
|
||||
enum params : int { BUF_SIZE = 1024 - 1 };
|
||||
|
||||
explicit FORCEINLINE Stream(char* buf) : m_pos(0), m_numberWidth(1), m_buf(buf), m_bufSize(BUF_SIZE) {}
|
||||
FORCEINLINE Stream(char* buf, size_t size) : m_pos(0), m_numberWidth(1), m_buf(buf), m_bufSize(static_cast<int>(size) - 1) {}
|
||||
template<size_t N>
|
||||
explicit FORCEINLINE Stream(char (&buf)[N]) : m_pos(0), m_numberWidth(1), m_buf(buf), m_bufSize(N - 1) {}
|
||||
|
||||
FORCEINLINE Stream(void* buf, size_t size) : m_pos(0), m_numberWidth(1), m_buf(reinterpret_cast<char*>(buf)), m_bufSize(static_cast<int>(size) - 1) {}
|
||||
|
||||
template<typename T>
|
||||
struct Entry
|
||||
@@ -181,7 +184,10 @@ INT_ENTRY(unsigned long)
|
||||
template<typename T, int base>
|
||||
struct BasedValue
|
||||
{
|
||||
explicit FORCEINLINE BasedValue(T value) : m_value(value) {}
|
||||
explicit FORCEINLINE BasedValue(T value) : m_value(value)
|
||||
{
|
||||
static_assert(std::is_integral<T>::value, "Must be an integer type here");
|
||||
}
|
||||
|
||||
T m_value;
|
||||
};
|
||||
@@ -189,7 +195,7 @@ struct BasedValue
|
||||
template<typename T, int base>
|
||||
struct Stream::Entry<BasedValue<T, base>>
|
||||
{
|
||||
static FORCEINLINE void put(BasedValue<T, base>&& data, Stream* wrapper)
|
||||
static FORCEINLINE void put(BasedValue<T, base> data, Stream* wrapper)
|
||||
{
|
||||
wrapper->writeInt<T, base>(data.m_value);
|
||||
}
|
||||
@@ -301,6 +307,7 @@ struct Hashrate
|
||||
{
|
||||
FORCEINLINE Hashrate() : m_data(0), m_valid(false) {}
|
||||
explicit FORCEINLINE Hashrate(uint64_t data) : m_data(data), m_valid(true) {}
|
||||
FORCEINLINE Hashrate(uint64_t data, bool valid) : m_data(data), m_valid(valid) {}
|
||||
|
||||
uint64_t m_data;
|
||||
bool m_valid;
|
||||
@@ -308,7 +315,7 @@ struct Hashrate
|
||||
|
||||
template<> struct log::Stream::Entry<Hashrate>
|
||||
{
|
||||
static NOINLINE void put(Hashrate&& value, Stream* wrapper)
|
||||
static NOINLINE void put(const Hashrate& value, Stream* wrapper)
|
||||
{
|
||||
if (!value.m_valid) {
|
||||
return;
|
||||
@@ -353,7 +360,7 @@ struct XMRAmount
|
||||
|
||||
template<> struct log::Stream::Entry<XMRAmount>
|
||||
{
|
||||
static NOINLINE void put(XMRAmount&& value, Stream* wrapper)
|
||||
static NOINLINE void put(XMRAmount value, Stream* wrapper)
|
||||
{
|
||||
constexpr uint64_t denomination = 1000000000000ULL;
|
||||
|
||||
@@ -371,8 +378,7 @@ template<> struct log::Stream::Entry<XMRAmount>
|
||||
|
||||
template<> struct log::Stream::Entry<NetworkType>
|
||||
{
|
||||
// cppcheck-suppress constParameter
|
||||
static NOINLINE void put(const NetworkType& value, Stream* wrapper)
|
||||
static NOINLINE void put(NetworkType value, Stream* wrapper)
|
||||
{
|
||||
switch (value) {
|
||||
case NetworkType::Invalid: *wrapper << "invalid"; break;
|
||||
@@ -383,6 +389,75 @@ template<> struct log::Stream::Entry<NetworkType>
|
||||
}
|
||||
};
|
||||
|
||||
struct Duration
|
||||
{
|
||||
explicit FORCEINLINE Duration(uint64_t data) : m_data(data) {}
|
||||
|
||||
uint64_t m_data;
|
||||
};
|
||||
|
||||
template<> struct log::Stream::Entry<Duration>
|
||||
{
|
||||
static NOINLINE void put(Duration value, Stream* wrapper)
|
||||
{
|
||||
const uint64_t uptime = value.m_data;
|
||||
|
||||
const int64_t s = uptime % 60;
|
||||
const int64_t m = (uptime / 60) % 60;
|
||||
const int64_t h = (uptime / 3600) % 24;
|
||||
const int64_t d = uptime / 86400;
|
||||
|
||||
if (d > 0) {
|
||||
*wrapper << d << "d ";
|
||||
}
|
||||
*wrapper << h << "h " << m << "m " << s << 's';
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct PadRight
|
||||
{
|
||||
FORCEINLINE PadRight(const T& value, int len) : m_value(value), m_len(len) {}
|
||||
|
||||
const T& m_value;
|
||||
int m_len;
|
||||
|
||||
// Declare it to make compiler happy
|
||||
PadRight(const PadRight&);
|
||||
|
||||
private:
|
||||
PadRight& operator=(const PadRight&) = delete;
|
||||
PadRight& operator=(PadRight&&) = delete;
|
||||
};
|
||||
|
||||
template<typename T> FORCEINLINE PadRight<T> pad_right(const T& value, int len) { return PadRight<T>(value, len); }
|
||||
|
||||
template<typename T>
|
||||
struct log::Stream::Entry<PadRight<T>>
|
||||
{
|
||||
static NOINLINE void put(PadRight<T>&& data, Stream* wrapper)
|
||||
{
|
||||
char buf[log::Stream::BUF_SIZE + 1];
|
||||
log::Stream s(buf);
|
||||
s << data.m_value;
|
||||
|
||||
const int len = std::min<int>(data.m_len, log::Stream::BUF_SIZE);
|
||||
if (s.m_pos < len) {
|
||||
memset(buf + s.m_pos, ' ', static_cast<size_t>(len) - s.m_pos);
|
||||
s.m_pos = len;
|
||||
}
|
||||
|
||||
wrapper->writeBuf(buf, s.m_pos);
|
||||
}
|
||||
};
|
||||
|
||||
void put_rawip(const raw_ip& value, Stream* wrapper);
|
||||
|
||||
template<> struct log::Stream::Entry<raw_ip>
|
||||
{
|
||||
static FORCEINLINE void put(const raw_ip& value, Stream* wrapper) { put_rawip(value, wrapper); }
|
||||
};
|
||||
|
||||
namespace {
|
||||
template<log::Severity severity> void apply_severity(log::Stream&);
|
||||
|
||||
@@ -394,16 +469,47 @@ namespace {
|
||||
#define CONCAT(a, b) CONCAT2(a, b)
|
||||
#define CONCAT2(a, b) a##b
|
||||
|
||||
// This is to check that LOG() call doesn't modify variables in scope, making program behavior dependent on the log level:
|
||||
//
|
||||
// int some_func(int& n) { return ++n; }
|
||||
// ...
|
||||
// LOGINFO(1, "Some important value: " << some_func(n));
|
||||
//
|
||||
// will not compile because the dummy lambda capture uses const-qualified copies of all variables.
|
||||
//
|
||||
// The check is "free": compiler will remove it entirely in release builds.
|
||||
|
||||
struct DummyStream
|
||||
{
|
||||
template<typename T>
|
||||
FORCEINLINE DummyStream& operator<<(const T&)
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
#define SIDE_EFFECT_CHECK(level, ...) \
|
||||
do { \
|
||||
if (0) { \
|
||||
MSVC_PRAGMA(warning(suppress:26444)) \
|
||||
[=]() { \
|
||||
log::DummyStream x; \
|
||||
x << level << __VA_ARGS__; \
|
||||
}; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#ifdef P2POOL_LOG_DISABLE
|
||||
|
||||
#define LOGINFO(level, ...)
|
||||
#define LOGWARN(level, ...)
|
||||
#define LOGERR(level, ...)
|
||||
#define LOGINFO(level, ...) SIDE_EFFECT_CHECK(level, __VA_ARGS__)
|
||||
#define LOGWARN(level, ...) SIDE_EFFECT_CHECK(level, __VA_ARGS__)
|
||||
#define LOGERR(level, ...) SIDE_EFFECT_CHECK(level, __VA_ARGS__)
|
||||
|
||||
#else
|
||||
|
||||
#define LOG(level, severity, ...) \
|
||||
do { \
|
||||
SIDE_EFFECT_CHECK(level, __VA_ARGS__); \
|
||||
if (level <= log::GLOBAL_LOG_LEVEL) { \
|
||||
log::Writer CONCAT(log_wrapper_, __LINE__)(severity); \
|
||||
CONCAT(log_wrapper_, __LINE__) << log::Gray() << log_category_prefix; \
|
||||
|
||||
+21
-6
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -21,7 +21,7 @@
|
||||
#include "stratum_server.h"
|
||||
#include "p2p_server.h"
|
||||
|
||||
static void usage()
|
||||
void p2pool_usage()
|
||||
{
|
||||
printf("P2Pool %s\n"
|
||||
"\nUsage:\n\n" \
|
||||
@@ -36,13 +36,23 @@ static void usage()
|
||||
"--loglevel Verbosity of the log, integer number between 0 and %d\n"
|
||||
"--config Name of the p2pool config file\n"
|
||||
"--data-api Path to the p2pool JSON data (use it in tandem with an external web-server)\n"
|
||||
"--stratum-api Enable /local/ path in api path for Stratum Server statistics\n"
|
||||
"--local-api Enable /local/ path in api path for Stratum Server and built-in miner statistics\n"
|
||||
"--stratum-api An alias for --local-api\n"
|
||||
"--no-cache Disable p2pool.cache\n"
|
||||
"--no-color Disable colors in console output\n"
|
||||
"--no-randomx Disable internal RandomX hasher: p2pool will use RPC calls to monerod to check PoW hashes\n"
|
||||
"--out-peers N Maximum number of outgoing connections for p2p server (any value between 10 and 1000)\n"
|
||||
"--in-peers N Maximum number of incoming connections for p2p server (any value between 10 and 1000)\n"
|
||||
"--start-mining N Start built-in miner using N threads (any value between 1 and 64)\n"
|
||||
"--mini Connect to p2pool-mini sidechain. Note that it will also change default p2p port from %d to %d\n"
|
||||
"--no-autodiff Disable automatic difficulty adjustment for miners connected to stratum\n"
|
||||
"--help Show this help message\n\n"
|
||||
"Example command line:\n\n"
|
||||
"%s --host 127.0.0.1 --rpc-port 18081 --zmq-port 18083 --wallet YOUR_WALLET_ADDRESS --stratum 0.0.0.0:%d --p2p 0.0.0.0:%d\n\n",
|
||||
p2pool::VERSION,
|
||||
p2pool::log::MAX_GLOBAL_LOG_LEVEL,
|
||||
p2pool::DEFAULT_P2P_PORT,
|
||||
p2pool::DEFAULT_P2P_PORT_MINI,
|
||||
#ifdef _WIN32
|
||||
"p2pool.exe"
|
||||
#else
|
||||
@@ -59,13 +69,13 @@ void memory_tracking_stop();
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
if (argc == 1) {
|
||||
usage();
|
||||
p2pool_usage();
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "/help") || !strcmp(argv[i], "-h") || !strcmp(argv[i], "/h")) {
|
||||
usage();
|
||||
p2pool_usage();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -75,10 +85,15 @@ int main(int argc, char* argv[])
|
||||
memory_tracking_start();
|
||||
|
||||
p2pool::init_crypto_cache();
|
||||
{
|
||||
|
||||
try {
|
||||
p2pool::p2pool pool(argc, argv);
|
||||
result = pool.run();
|
||||
}
|
||||
catch (...) {
|
||||
result = 1;
|
||||
}
|
||||
|
||||
p2pool::destroy_crypto_cache();
|
||||
|
||||
memory_tracking_stop();
|
||||
|
||||
+43
-11
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -120,9 +120,9 @@ FORCEINLINE static void remove_allocation(void* p)
|
||||
__debugbreak();
|
||||
}
|
||||
|
||||
FORCEINLINE static void* allocate_noexcept(size_t n) noexcept
|
||||
void* malloc_hook(size_t n) noexcept
|
||||
{
|
||||
void* p = malloc(n + sizeof(TrackedAllocation));
|
||||
void* p = malloc(n);
|
||||
if (p) {
|
||||
add_alocation(p, n);
|
||||
}
|
||||
@@ -131,25 +131,46 @@ FORCEINLINE static void* allocate_noexcept(size_t n) noexcept
|
||||
|
||||
FORCEINLINE static void* allocate(size_t n)
|
||||
{
|
||||
void* p = allocate_noexcept(n);
|
||||
void* p = malloc_hook(n);
|
||||
if (!p) {
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
FORCEINLINE static void deallocate(void* p)
|
||||
void free_hook(void* p) noexcept
|
||||
{
|
||||
remove_allocation(p);
|
||||
free(p);
|
||||
}
|
||||
|
||||
void* realloc_hook(void* ptr, size_t size) noexcept
|
||||
{
|
||||
remove_allocation(ptr);
|
||||
|
||||
void* p = realloc(ptr, size);
|
||||
if (p) {
|
||||
add_alocation(p, size);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
void* calloc_hook(size_t count, size_t size) noexcept
|
||||
{
|
||||
void* p = calloc(count, size);
|
||||
if (p) {
|
||||
add_alocation(p, size);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
} // p2pool
|
||||
|
||||
void memory_tracking_start()
|
||||
{
|
||||
using namespace p2pool;
|
||||
|
||||
uv_replace_allocator(malloc_hook, realloc_hook, calloc_hook, free_hook);
|
||||
uv_mutex_init_checked(&allocation_lock);
|
||||
track_memory = true;
|
||||
}
|
||||
@@ -207,14 +228,25 @@ void memory_tracking_stop()
|
||||
|
||||
NOINLINE void* operator new(size_t n) { return p2pool::allocate(n); }
|
||||
NOINLINE void* operator new[](size_t n) { return p2pool::allocate(n); }
|
||||
NOINLINE void* operator new(size_t n, const std::nothrow_t&) noexcept { return p2pool::allocate_noexcept(n); }
|
||||
NOINLINE void* operator new[](size_t n, const std::nothrow_t&) noexcept { return p2pool::allocate_noexcept(n); }
|
||||
NOINLINE void operator delete(void* p) noexcept { p2pool::deallocate(p); }
|
||||
NOINLINE void operator delete[](void* p) noexcept { p2pool::deallocate(p); }
|
||||
NOINLINE void operator delete(void* p, size_t) noexcept { p2pool::deallocate(p); }
|
||||
NOINLINE void operator delete[](void* p, size_t) noexcept { p2pool::deallocate(p); }
|
||||
NOINLINE void* operator new(size_t n, const std::nothrow_t&) noexcept { return p2pool::malloc_hook(n); }
|
||||
NOINLINE void* operator new[](size_t n, const std::nothrow_t&) noexcept { return p2pool::malloc_hook(n); }
|
||||
NOINLINE void operator delete(void* p) noexcept { p2pool::free_hook(p); }
|
||||
NOINLINE void operator delete[](void* p) noexcept { p2pool::free_hook(p); }
|
||||
NOINLINE void operator delete(void* p, size_t) noexcept { p2pool::free_hook(p); }
|
||||
NOINLINE void operator delete[](void* p, size_t) noexcept { p2pool::free_hook(p); }
|
||||
|
||||
#else
|
||||
// cppcheck-suppress functionStatic
|
||||
void memory_tracking_start() {}
|
||||
// cppcheck-suppress functionStatic
|
||||
void memory_tracking_stop() {}
|
||||
|
||||
namespace p2pool {
|
||||
|
||||
void* malloc_hook(size_t n) noexcept { return malloc(n); }
|
||||
void* realloc_hook(void* ptr, size_t size) noexcept { return realloc(ptr, size); }
|
||||
void* calloc_hook(size_t count, size_t size) noexcept { return calloc(count, size); }
|
||||
void free_hook(void* p) noexcept { free(p); }
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -44,7 +44,7 @@ void Mempool::add(const TxMempoolData& tx)
|
||||
|
||||
void Mempool::swap(std::vector<TxMempoolData>& transactions)
|
||||
{
|
||||
const time_t cur_time = time(nullptr);
|
||||
const uint64_t cur_time = seconds_since_epoch();
|
||||
|
||||
WriteLock lock(m_lock);
|
||||
|
||||
|
||||
+2
-3
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -18,7 +18,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "uv_util.h"
|
||||
#include <unordered_map>
|
||||
|
||||
namespace p2pool {
|
||||
|
||||
@@ -35,7 +34,7 @@ public:
|
||||
|
||||
public:
|
||||
mutable uv_rwlock_t m_lock;
|
||||
std::unordered_map<hash, TxMempoolData> m_transactions;
|
||||
unordered_map<hash, TxMempoolData> m_transactions;
|
||||
};
|
||||
|
||||
} // namespace p2pool
|
||||
|
||||
+228
@@ -0,0 +1,228 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program 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, version 3.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
#include "miner.h"
|
||||
#include "p2pool.h"
|
||||
#include "stratum_server.h"
|
||||
#include "block_template.h"
|
||||
#include "pow_hash.h"
|
||||
#include "randomx.h"
|
||||
#include "params.h"
|
||||
#include "p2pool_api.h"
|
||||
#include "side_chain.h"
|
||||
#include <thread>
|
||||
|
||||
static constexpr char log_category_prefix[] = "Miner ";
|
||||
|
||||
using namespace std::chrono;
|
||||
|
||||
namespace p2pool {
|
||||
|
||||
Miner::Miner(p2pool* pool, uint32_t threads)
|
||||
: m_pool(pool)
|
||||
, m_threads(threads)
|
||||
, m_stopped{ false }
|
||||
, m_startTimestamp(high_resolution_clock::now())
|
||||
, m_nonce(0)
|
||||
, m_nonceTimestamp(m_startTimestamp)
|
||||
, m_extraNonce(0xF19E3779U)
|
||||
, m_totalHashes(0)
|
||||
, m_sharesFound(0)
|
||||
, m_job{}
|
||||
, m_jobIndex{ 0 }
|
||||
{
|
||||
on_block(m_pool->block_template());
|
||||
|
||||
m_minerThreads.reserve(threads);
|
||||
|
||||
for (uint32_t i = 0; i < threads; ++i) {
|
||||
WorkerData* data = new WorkerData{ this, i + 1, threads, {} };
|
||||
const int err = uv_thread_create(&data->m_worker, run, data);
|
||||
if (err) {
|
||||
LOGERR(1, "failed to start worker thread " << data->m_index << '/' << threads << ", error " << uv_err_name(err));
|
||||
delete data;
|
||||
continue;
|
||||
}
|
||||
m_minerThreads.push_back(data);
|
||||
}
|
||||
}
|
||||
|
||||
Miner::~Miner()
|
||||
{
|
||||
m_stopped = true;
|
||||
|
||||
for (WorkerData* data : m_minerThreads) {
|
||||
uv_thread_join(&data->m_worker);
|
||||
delete data;
|
||||
}
|
||||
}
|
||||
|
||||
void Miner::print_status()
|
||||
{
|
||||
const uint32_t hash_count = 0 - m_nonce.load();
|
||||
|
||||
const double dt = static_cast<double>(duration_cast<nanoseconds>(high_resolution_clock::now() - m_nonceTimestamp).count()) / 1e9;
|
||||
const uint64_t hr = (dt > 0.0) ? static_cast<uint64_t>(hash_count / dt) : 0;
|
||||
|
||||
LOGINFO(0, "status" <<
|
||||
"\nThreads = " << m_threads <<
|
||||
"\nHashrate = " << log::Hashrate(hr)
|
||||
);
|
||||
}
|
||||
|
||||
void Miner::on_block(const BlockTemplate& block)
|
||||
{
|
||||
const uint32_t next_index = m_jobIndex ^ 1;
|
||||
Job& j = m_job[next_index];
|
||||
hash seed;
|
||||
j.m_blobSize = block.get_hashing_blob(m_extraNonce, j.m_blob, j.m_height, j.m_diff, j.m_sidechainDiff, seed, j.m_nonceOffset, j.m_templateId);
|
||||
|
||||
const uint32_t hash_count = 0 - m_nonce.exchange(0);
|
||||
m_jobIndex = next_index;
|
||||
|
||||
const auto cur_ts = high_resolution_clock::now();
|
||||
const double dt = static_cast<double>(duration_cast<nanoseconds>(cur_ts - m_nonceTimestamp).count()) / 1e9;
|
||||
|
||||
m_nonceTimestamp = cur_ts;
|
||||
m_totalHashes += hash_count;
|
||||
|
||||
if (m_pool->api() && m_pool->params().m_localStats) {
|
||||
const double block_reward_share_percent = m_pool->side_chain().get_reward_share(m_pool->params().m_wallet) * 100.0;
|
||||
|
||||
m_pool->api()->set(p2pool_api::Category::LOCAL, "miner",
|
||||
[cur_ts, hash_count, dt, block_reward_share_percent, this](log::Stream& s)
|
||||
{
|
||||
const uint64_t hr = (dt > 0.0) ? static_cast<uint64_t>(hash_count / dt) : 0;
|
||||
const double time_running = static_cast<double>(duration_cast<milliseconds>(cur_ts - m_startTimestamp).count()) / 1e3;
|
||||
|
||||
s << "{\"current_hashrate\":" << hr
|
||||
<< ",\"total_hashes\":" << m_totalHashes
|
||||
<< ",\"time_running\":" << time_running
|
||||
<< ",\"shares_found\":" << m_sharesFound.load()
|
||||
<< ",\"block_reward_share_percent\":" << block_reward_share_percent
|
||||
<< ",\"threads\":" << m_threads
|
||||
<< "}";
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void Miner::run(void* data)
|
||||
{
|
||||
WorkerData* d = static_cast<WorkerData*>(data);
|
||||
LOGINFO(1, "worker thread " << d->m_index << '/' << d->m_count << " started");
|
||||
make_thread_background();
|
||||
d->m_miner->run(d);
|
||||
LOGINFO(1, "worker thread " << d->m_index << '/' << d->m_count << " stopped");
|
||||
}
|
||||
|
||||
void Miner::run(WorkerData* data)
|
||||
{
|
||||
RandomX_Hasher_Base* hasher = m_pool->hasher();
|
||||
randomx_cache* cache = hasher->cache();
|
||||
randomx_dataset* dataset = hasher->dataset();
|
||||
|
||||
if (!cache && !dataset) {
|
||||
LOGERR(1, "worker thread " << data->m_index << '/' << data->m_count << ": RandomX cache and dataset are not ready");
|
||||
return;
|
||||
}
|
||||
|
||||
randomx_flags flags = randomx_get_flags();
|
||||
if (dataset) {
|
||||
flags |= RANDOMX_FLAG_FULL_MEM;
|
||||
}
|
||||
|
||||
randomx_vm* vm = randomx_create_vm(flags | RANDOMX_FLAG_LARGE_PAGES, dataset ? nullptr : cache, dataset);
|
||||
if (!vm) {
|
||||
LOGWARN(1, "couldn't allocate RandomX VM using large pages");
|
||||
vm = randomx_create_vm(flags, dataset ? nullptr : cache, dataset);
|
||||
if (!vm) {
|
||||
LOGERR(1, "couldn't allocate RandomX VM");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t index = 0;
|
||||
Job job[2];
|
||||
|
||||
uint32_t seed_counter = 0;
|
||||
bool first = true;
|
||||
|
||||
Miner* miner = data->m_miner;
|
||||
|
||||
while (!m_stopped) {
|
||||
if (hasher->seed_counter() != seed_counter) {
|
||||
LOGINFO(5, "worker thread " << data->m_index << '/' << data->m_count << " paused (waiting for RandomX cache/dataset update)");
|
||||
hasher->sync_wait();
|
||||
seed_counter = hasher->seed_counter();
|
||||
if (flags & RANDOMX_FLAG_FULL_MEM) {
|
||||
dataset = hasher->dataset();
|
||||
randomx_vm_set_dataset(vm, dataset);
|
||||
}
|
||||
else {
|
||||
cache = hasher->cache();
|
||||
randomx_vm_set_cache(vm, cache);
|
||||
}
|
||||
LOGINFO(5, "worker thread " << data->m_index << '/' << data->m_count << " resumed");
|
||||
}
|
||||
|
||||
if (first) {
|
||||
first = false;
|
||||
memcpy(&job[index], &miner->m_job[miner->m_jobIndex], sizeof(Job));
|
||||
job[index].set_nonce(miner->m_nonce.fetch_sub(1), miner->m_extraNonce);
|
||||
randomx_calculate_hash_first(vm, job[index].m_blob, job[index].m_blobSize);
|
||||
}
|
||||
|
||||
const Job& j = job[index];
|
||||
index ^= 1;
|
||||
memcpy(&job[index], &miner->m_job[miner->m_jobIndex], sizeof(Job));
|
||||
job[index].set_nonce(miner->m_nonce.fetch_sub(1), miner->m_extraNonce);
|
||||
|
||||
hash h;
|
||||
randomx_calculate_hash_next(vm, job[index].m_blob, job[index].m_blobSize, &h);
|
||||
|
||||
if (j.m_diff.check_pow(h)) {
|
||||
LOGINFO(0, log::Green() << "worker thread " << data->m_index << '/' << data->m_count << " found a mainchain block, submitting it");
|
||||
m_pool->submit_block_async(j.m_templateId, j.m_nonce, j.m_extraNonce);
|
||||
m_pool->block_template().update_tx_keys();
|
||||
}
|
||||
|
||||
if (j.m_sidechainDiff.check_pow(h)) {
|
||||
LOGINFO(0, log::Green() << "SHARE FOUND: mainchain height " << j.m_height << ", diff " << j.m_sidechainDiff << ", worker thread " << data->m_index << '/' << data->m_count);
|
||||
m_pool->submit_sidechain_block(j.m_templateId, j.m_nonce, j.m_extraNonce);
|
||||
++m_sharesFound;
|
||||
}
|
||||
|
||||
std::this_thread::yield();
|
||||
}
|
||||
|
||||
randomx_destroy_vm(vm);
|
||||
}
|
||||
|
||||
void Miner::Job::set_nonce(uint32_t nonce, uint32_t extra_nonce)
|
||||
{
|
||||
m_nonce = nonce;
|
||||
m_extraNonce = extra_nonce;
|
||||
|
||||
uint8_t* p = m_blob + m_nonceOffset;
|
||||
p[0] = static_cast<uint8_t>(nonce);
|
||||
p[1] = static_cast<uint8_t>(nonce >> 8);
|
||||
p[2] = static_cast<uint8_t>(nonce >> 16);
|
||||
p[3] = static_cast<uint8_t>(nonce >> 24);
|
||||
}
|
||||
|
||||
} // namespace p2pool
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program 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, version 3.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "uv_util.h"
|
||||
#include <chrono>
|
||||
|
||||
namespace p2pool {
|
||||
|
||||
class p2pool;
|
||||
class BlockTemplate;
|
||||
|
||||
class Miner
|
||||
{
|
||||
public:
|
||||
Miner(p2pool* pool, uint32_t threads);
|
||||
~Miner();
|
||||
|
||||
void print_status();
|
||||
void on_block(const BlockTemplate& block);
|
||||
|
||||
private:
|
||||
static void run(void* data);
|
||||
|
||||
p2pool* m_pool;
|
||||
uint32_t m_threads;
|
||||
|
||||
struct WorkerData
|
||||
{
|
||||
Miner* m_miner;
|
||||
uint32_t m_index;
|
||||
uint32_t m_count;
|
||||
uv_thread_t m_worker;
|
||||
};
|
||||
|
||||
std::vector<WorkerData*> m_minerThreads;
|
||||
std::atomic<bool> m_stopped;
|
||||
|
||||
std::chrono::high_resolution_clock::time_point m_startTimestamp;
|
||||
|
||||
std::atomic<uint32_t> m_nonce;
|
||||
std::chrono::high_resolution_clock::time_point m_nonceTimestamp;
|
||||
const uint32_t m_extraNonce;
|
||||
|
||||
uint64_t m_totalHashes;
|
||||
std::atomic<uint32_t> m_sharesFound;
|
||||
|
||||
struct Job
|
||||
{
|
||||
uint8_t m_blob[128] = {};
|
||||
uint32_t m_blobSize = 0;
|
||||
uint32_t m_templateId = 0;
|
||||
difficulty_type m_diff = {};
|
||||
difficulty_type m_sidechainDiff = {};
|
||||
uint64_t m_height = 0;
|
||||
size_t m_nonceOffset = 0;
|
||||
uint32_t m_nonce = 0;
|
||||
uint32_t m_extraNonce = 0;
|
||||
|
||||
void set_nonce(uint32_t nonce, uint32_t extra_nonce);
|
||||
};
|
||||
Job m_job[2];
|
||||
std::atomic<uint32_t> m_jobIndex;
|
||||
|
||||
void run(WorkerData* data);
|
||||
};
|
||||
|
||||
} // namespace p2pool
|
||||
+453
-111
File diff suppressed because it is too large
Load Diff
+34
-14
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -18,8 +18,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "tcp_server.h"
|
||||
#include <random>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace p2pool {
|
||||
|
||||
@@ -30,6 +28,7 @@ class BlockCache;
|
||||
static constexpr size_t P2P_BUF_SIZE = 128 * 1024;
|
||||
static constexpr size_t PEER_LIST_RESPONSE_MAX_PEERS = 16;
|
||||
static constexpr int DEFAULT_P2P_PORT = 37889;
|
||||
static constexpr int DEFAULT_P2P_PORT_MINI = 37888;
|
||||
|
||||
class P2PServer : public TCPServer<P2P_BUF_SIZE, P2P_BUF_SIZE>
|
||||
{
|
||||
@@ -109,11 +108,20 @@ public:
|
||||
bool m_handshakeComplete;
|
||||
bool m_handshakeInvalid;
|
||||
int m_listenPort;
|
||||
time_t m_nextPeerListRequest;
|
||||
|
||||
uint32_t m_fastPeerListRequestCount;
|
||||
uint64_t m_prevIncomingPeerListRequest;
|
||||
uint64_t m_nextOutgoingPeerListRequest;
|
||||
std::chrono::high_resolution_clock::time_point m_lastPeerListRequestTime;
|
||||
int m_peerListPendingRequests;
|
||||
time_t m_lastAlive;
|
||||
time_t m_lastBroadcastTimestamp;
|
||||
time_t m_lastBlockrequestTimestamp;
|
||||
int64_t m_pingTime;
|
||||
|
||||
int m_blockPendingRequests;
|
||||
bool m_chainTipBlockRequest;
|
||||
|
||||
uint64_t m_lastAlive;
|
||||
uint64_t m_lastBroadcastTimestamp;
|
||||
uint64_t m_lastBlockrequestTimestamp;
|
||||
|
||||
hash m_broadcastedHashes[8];
|
||||
std::atomic<uint32_t> m_broadcastedHashesIndex{ 0 };
|
||||
@@ -124,15 +132,25 @@ public:
|
||||
uint64_t get_peerId() const { return m_peerId; }
|
||||
|
||||
void print_status() override;
|
||||
void show_peers();
|
||||
size_t peer_list_size() const { MutexLock lock(m_peerListLock); return m_peerList.size(); }
|
||||
|
||||
uint32_t max_outgoing_peers() const { return m_maxOutgoingPeers; }
|
||||
uint32_t max_incoming_peers() const { return m_maxIncomingPeers; }
|
||||
|
||||
void set_max_outgoing_peers(uint32_t n) { m_maxOutgoingPeers = std::min(std::max(n, 10U), 1000U); }
|
||||
void set_max_incoming_peers(uint32_t n) { m_maxIncomingPeers = std::min(std::max(n, 10U), 1000U); }
|
||||
|
||||
private:
|
||||
p2pool* m_pool;
|
||||
BlockCache* m_cache;
|
||||
bool m_cacheLoaded;
|
||||
std::string m_initialPeerList;
|
||||
uint32_t m_maxOutgoingPeers;
|
||||
uint32_t m_maxIncomingPeers;
|
||||
|
||||
uv_rwlock_t m_cachedBlocksLock;
|
||||
std::unordered_map<hash, PoolBlock*> m_cachedBlocks;
|
||||
unordered_map<hash, PoolBlock*>* m_cachedBlocks;
|
||||
|
||||
private:
|
||||
static void on_timer(uv_timer_t* timer) { reinterpret_cast<P2PServer*>(timer->data)->on_timer(); }
|
||||
@@ -146,23 +164,24 @@ private:
|
||||
void save_peer_list_async();
|
||||
void save_peer_list();
|
||||
void load_peer_list();
|
||||
void load_monerod_peer_list();
|
||||
void update_peer_in_list(bool is_v6, const raw_ip& ip, int port);
|
||||
void remove_peer_from_list(P2PClient* client);
|
||||
void remove_peer_from_list(const raw_ip& ip);
|
||||
|
||||
uv_mutex_t m_rngLock;
|
||||
std::random_device m_rd;
|
||||
std::mt19937_64 m_rng;
|
||||
|
||||
uv_mutex_t m_blockLock;
|
||||
PoolBlock* m_block;
|
||||
|
||||
uv_timer_t m_timer;
|
||||
uint32_t m_timerCounter;
|
||||
uint64_t m_timerCounter;
|
||||
uint64_t m_timerInterval;
|
||||
|
||||
uint64_t m_peerId;
|
||||
|
||||
uv_mutex_t m_peerListLock;
|
||||
mutable uv_mutex_t m_peerListLock;
|
||||
|
||||
struct Peer
|
||||
{
|
||||
@@ -170,11 +189,12 @@ private:
|
||||
raw_ip m_addr;
|
||||
int m_port;
|
||||
uint32_t m_numFailedConnections;
|
||||
time_t m_lastSeen;
|
||||
uint64_t m_lastSeen;
|
||||
};
|
||||
|
||||
std::vector<Peer> m_peerList;
|
||||
time_t m_peerListLastSaved;
|
||||
std::vector<Peer> m_peerListMonero;
|
||||
std::atomic<uint64_t> m_peerListLastSaved;
|
||||
|
||||
struct Broadcast
|
||||
{
|
||||
@@ -188,7 +208,7 @@ private:
|
||||
std::vector<Broadcast*> m_broadcastQueue;
|
||||
|
||||
uv_mutex_t m_missingBlockRequestsLock;
|
||||
std::set<std::pair<uint64_t, uint64_t>> m_missingBlockRequests;
|
||||
unordered_set<std::pair<uint64_t, uint64_t>> m_missingBlockRequests;
|
||||
|
||||
static void on_broadcast(uv_async_t* handle) { reinterpret_cast<P2PServer*>(handle->data)->on_broadcast(); }
|
||||
void on_broadcast();
|
||||
|
||||
+292
-58
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -27,10 +27,15 @@
|
||||
#include "side_chain.h"
|
||||
#include "stratum_server.h"
|
||||
#include "p2p_server.h"
|
||||
#ifdef WITH_RANDOMX
|
||||
#include "miner.h"
|
||||
#endif
|
||||
#include "params.h"
|
||||
#include "console_commands.h"
|
||||
#include "crypto.h"
|
||||
#include "p2pool_api.h"
|
||||
#include "pool_block.h"
|
||||
#include "keccak.h"
|
||||
#include <thread>
|
||||
#include <fstream>
|
||||
|
||||
@@ -49,26 +54,29 @@ p2pool::p2pool(int argc, char* argv[])
|
||||
, m_params(new Params(argc, argv))
|
||||
, m_updateSeed(true)
|
||||
, m_submitBlockData{}
|
||||
, m_zmqLastActive(0)
|
||||
, m_startTime(seconds_since_epoch())
|
||||
{
|
||||
LOGINFO(1, log::LightCyan() << VERSION);
|
||||
|
||||
if (!m_params->m_wallet.valid()) {
|
||||
LOGERR(1, "Invalid wallet address. Try \"p2pool --help\".");
|
||||
panic();
|
||||
throw std::exception();
|
||||
}
|
||||
|
||||
bool is_v6;
|
||||
if (!resolve_host(m_params->m_host, is_v6)) {
|
||||
LOGERR(1, "resolve_host failed for " << m_params->m_host);
|
||||
panic();
|
||||
throw std::exception();
|
||||
}
|
||||
|
||||
hash pub, sec, eph_public_key;
|
||||
generate_keys(pub, sec);
|
||||
|
||||
if (!m_params->m_wallet.get_eph_public_key(sec, 0, eph_public_key)) {
|
||||
uint8_t view_tag;
|
||||
if (!m_params->m_wallet.get_eph_public_key(sec, 0, eph_public_key, view_tag)) {
|
||||
LOGERR(1, "Invalid wallet address: get_eph_public_key failed");
|
||||
panic();
|
||||
throw std::exception();
|
||||
}
|
||||
|
||||
const NetworkType type = m_params->m_wallet.type();
|
||||
@@ -83,32 +91,69 @@ p2pool::p2pool(int argc, char* argv[])
|
||||
int err = uv_async_init(uv_default_loop_checked(), &m_submitBlockAsync, on_submit_block);
|
||||
if (err) {
|
||||
LOGERR(1, "uv_async_init failed, error " << uv_err_name(err));
|
||||
panic();
|
||||
throw std::exception();
|
||||
}
|
||||
m_submitBlockAsync.data = this;
|
||||
|
||||
err = uv_async_init(uv_default_loop_checked(), &m_blockTemplateAsync, on_update_block_template);
|
||||
if (err) {
|
||||
LOGERR(1, "uv_async_init failed, error " << uv_err_name(err));
|
||||
panic();
|
||||
throw std::exception();
|
||||
}
|
||||
m_blockTemplateAsync.data = this;
|
||||
|
||||
err = uv_async_init(uv_default_loop_checked(), &m_stopAsync, on_stop);
|
||||
if (err) {
|
||||
LOGERR(1, "uv_async_init failed, error " << uv_err_name(err));
|
||||
panic();
|
||||
throw std::exception();
|
||||
}
|
||||
m_stopAsync.data = this;
|
||||
|
||||
err = uv_async_init(uv_default_loop_checked(), &m_restartZMQAsync, on_restart_zmq);
|
||||
if (err) {
|
||||
LOGERR(1, "uv_async_init failed, error " << uv_err_name(err));
|
||||
throw std::exception();
|
||||
}
|
||||
m_restartZMQAsync.data = this;
|
||||
|
||||
uv_rwlock_init_checked(&m_mainchainLock);
|
||||
uv_rwlock_init_checked(&m_minerDataLock);
|
||||
uv_mutex_init_checked(&m_foundBlocksLock);
|
||||
#ifdef WITH_RANDOMX
|
||||
uv_mutex_init_checked(&m_minerLock);
|
||||
#endif
|
||||
uv_mutex_init_checked(&m_submitBlockDataLock);
|
||||
|
||||
m_api = m_params->m_apiPath.empty() ? nullptr : new p2pool_api(m_params->m_apiPath, m_params->m_localStats);
|
||||
|
||||
m_sideChain = new SideChain(this, type);
|
||||
m_hasher = new RandomX_Hasher(this);
|
||||
if (m_params->m_localStats && !m_api) {
|
||||
LOGERR(1, "--local-api and --stratum-api command line parameters can't be used without --data-api");
|
||||
throw std::exception();
|
||||
}
|
||||
|
||||
m_sideChain = new SideChain(this, type, m_params->m_mini ? "mini" : nullptr);
|
||||
|
||||
if (m_params->m_p2pAddresses.empty()) {
|
||||
const int p2p_port = m_sideChain->is_mini() ? DEFAULT_P2P_PORT_MINI : DEFAULT_P2P_PORT;
|
||||
|
||||
char buf[log::Stream::BUF_SIZE + 1];
|
||||
log::Stream s(buf);
|
||||
s << "[::]:" << p2p_port << ",0.0.0.0:" << p2p_port << '\0';
|
||||
|
||||
m_params->m_p2pAddresses = buf;
|
||||
}
|
||||
|
||||
#ifdef WITH_RANDOMX
|
||||
if (m_params->m_disableRandomX) {
|
||||
m_hasher = new RandomX_Hasher_RPC(this);
|
||||
}
|
||||
else {
|
||||
m_hasher = new RandomX_Hasher(this);
|
||||
}
|
||||
#else
|
||||
m_hasher = new RandomX_Hasher_RPC(this);
|
||||
#endif
|
||||
|
||||
m_blockTemplate = new BlockTemplate(this);
|
||||
m_mempool = new Mempool();
|
||||
m_consoleCommands = new ConsoleCommands(this);
|
||||
@@ -117,7 +162,11 @@ p2pool::p2pool(int argc, char* argv[])
|
||||
p2pool::~p2pool()
|
||||
{
|
||||
uv_rwlock_destroy(&m_mainchainLock);
|
||||
uv_rwlock_destroy(&m_minerDataLock);
|
||||
uv_mutex_destroy(&m_foundBlocksLock);
|
||||
#ifdef WITH_RANDOMX
|
||||
uv_mutex_destroy(&m_minerLock);
|
||||
#endif
|
||||
uv_mutex_destroy(&m_submitBlockDataLock);
|
||||
|
||||
delete m_api;
|
||||
@@ -129,9 +178,9 @@ p2pool::~p2pool()
|
||||
delete m_consoleCommands;
|
||||
}
|
||||
|
||||
bool p2pool::calculate_hash(const void* data, size_t size, const hash& seed, hash& result)
|
||||
bool p2pool::calculate_hash(const void* data, size_t size, uint64_t height, const hash& seed, hash& result)
|
||||
{
|
||||
return m_hasher->calculate(data, size, seed, result);
|
||||
return m_hasher->calculate(data, size, height, seed, result);
|
||||
}
|
||||
|
||||
uint64_t p2pool::get_seed_height(uint64_t height)
|
||||
@@ -155,6 +204,17 @@ bool p2pool::get_seed(uint64_t height, hash& seed) const
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef WITH_RANDOMX
|
||||
void p2pool::print_miner_status()
|
||||
{
|
||||
MutexLock lock(m_minerLock);
|
||||
|
||||
if (m_miner) {
|
||||
m_miner->print_status();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void p2pool::handle_tx(TxMempoolData& tx)
|
||||
{
|
||||
if (!tx.weight || !tx.fee) {
|
||||
@@ -171,10 +231,10 @@ void p2pool::handle_tx(TxMempoolData& tx)
|
||||
", fee = " << log::Gray() << static_cast<double>(tx.fee) / 1e6 << " um");
|
||||
|
||||
#if TEST_MEMPOOL_PICKING_ALGORITHM
|
||||
m_blockTemplate->update(m_minerData, *m_mempool, &m_params->m_wallet);
|
||||
m_blockTemplate->update(miner_data(), *m_mempool, &m_params->m_wallet);
|
||||
#endif
|
||||
|
||||
m_zmqLastActive = time(nullptr);
|
||||
m_zmqLastActive = seconds_since_epoch();
|
||||
}
|
||||
|
||||
void p2pool::handle_miner_data(MinerData& data)
|
||||
@@ -201,11 +261,16 @@ void p2pool::handle_miner_data(MinerData& data)
|
||||
c.reward = 0;
|
||||
|
||||
m_mainchainByHash[c.id] = c;
|
||||
|
||||
cleanup_mainchain_data(data.height);
|
||||
}
|
||||
|
||||
data.tx_backlog.clear();
|
||||
data.time_received = std::chrono::system_clock::now();
|
||||
m_minerData = data;
|
||||
data.time_received = std::chrono::high_resolution_clock::now();
|
||||
{
|
||||
WriteLock lock(m_minerDataLock);
|
||||
m_minerData = data;
|
||||
}
|
||||
m_updateSeed = true;
|
||||
update_median_timestamp();
|
||||
|
||||
@@ -222,14 +287,50 @@ void p2pool::handle_miner_data(MinerData& data)
|
||||
"\n---------------------------------------------------------------------------------------------------------------"
|
||||
);
|
||||
|
||||
if (!is_main_thread) {
|
||||
if (!is_main_thread()) {
|
||||
update_block_template_async();
|
||||
}
|
||||
else {
|
||||
update_block_template();
|
||||
}
|
||||
|
||||
m_zmqLastActive = time(nullptr);
|
||||
m_zmqLastActive = seconds_since_epoch();
|
||||
|
||||
if (m_serversStarted.load()) {
|
||||
std::vector<uint64_t> missing_heights;
|
||||
{
|
||||
WriteLock lock(m_mainchainLock);
|
||||
|
||||
for (uint64_t h = data.height; h && (h + BLOCK_HEADERS_REQUIRED > data.height); --h) {
|
||||
auto it = m_mainchainByHeight.find(h);
|
||||
if ((it == m_mainchainByHeight.end()) || it->second.difficulty.empty()) {
|
||||
LOGWARN(3, "Mainchain data for height " << h << " is missing, requesting it from monerod again");
|
||||
missing_heights.push_back(h);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (uint64_t h : missing_heights) {
|
||||
char buf[log::Stream::BUF_SIZE + 1];
|
||||
log::Stream s(buf);
|
||||
s << "{\"jsonrpc\":\"2.0\",\"id\":\"0\",\"method\":\"get_block_header_by_height\",\"params\":{\"height\":" << h << "}}\0";
|
||||
|
||||
JSONRPCRequest::call(m_params->m_host.c_str(), m_params->m_rpcPort, buf,
|
||||
[this, h](const char* data, size_t size)
|
||||
{
|
||||
ChainMain block;
|
||||
if (!parse_block_header(data, size, block)) {
|
||||
LOGERR(1, "couldn't download block header for height " << h);
|
||||
}
|
||||
},
|
||||
[h](const char* data, size_t size)
|
||||
{
|
||||
if (size > 0) {
|
||||
LOGERR(1, "couldn't download block header for height " << h << ", error " << log::const_buf(data, size));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const char* BLOCK_FOUND = "\n\
|
||||
@@ -282,8 +383,13 @@ void p2pool::handle_chain_main(ChainMain& data, const char* extra)
|
||||
", reward = " << log::Gray() << log::XMRAmount(data.reward));
|
||||
|
||||
if (!sidechain_id.empty()) {
|
||||
if (side_chain().has_block(sidechain_id)) {
|
||||
PoolBlock* block = side_chain().find_block(sidechain_id);
|
||||
if (block) {
|
||||
LOGINFO(0, log::LightGreen() << "BLOCK FOUND: main chain block at height " << data.height << " was mined by this p2pool" << BLOCK_FOUND);
|
||||
const uint64_t payout = block->get_payout(params().m_wallet);
|
||||
if (payout) {
|
||||
LOGINFO(0, log::LightCyan() << "You received a payout of " << log::LightGreen() << log::XMRAmount(payout) << log::LightCyan() << " in block " << log::LightGreen() << data.height);
|
||||
}
|
||||
api_update_block_found(&data);
|
||||
}
|
||||
else {
|
||||
@@ -293,7 +399,7 @@ void p2pool::handle_chain_main(ChainMain& data, const char* extra)
|
||||
|
||||
api_update_network_stats();
|
||||
|
||||
m_zmqLastActive = time(nullptr);
|
||||
m_zmqLastActive = seconds_since_epoch();
|
||||
}
|
||||
|
||||
void p2pool::submit_block_async(uint32_t template_id, uint32_t nonce, uint32_t extra_nonce)
|
||||
@@ -341,6 +447,7 @@ void p2pool::on_stop(uv_async_t* async)
|
||||
uv_close(reinterpret_cast<uv_handle_t*>(&pool->m_submitBlockAsync), nullptr);
|
||||
uv_close(reinterpret_cast<uv_handle_t*>(&pool->m_blockTemplateAsync), nullptr);
|
||||
uv_close(reinterpret_cast<uv_handle_t*>(&pool->m_stopAsync), nullptr);
|
||||
uv_close(reinterpret_cast<uv_handle_t*>(&pool->m_restartZMQAsync), nullptr);
|
||||
uv_stop(uv_default_loop());
|
||||
}
|
||||
|
||||
@@ -475,11 +582,13 @@ void p2pool::update_block_template_async()
|
||||
|
||||
void p2pool::update_block_template()
|
||||
{
|
||||
MinerData data = miner_data();
|
||||
|
||||
if (m_updateSeed) {
|
||||
m_hasher->set_seed_async(m_minerData.seed_hash);
|
||||
m_hasher->set_seed_async(data.seed_hash);
|
||||
m_updateSeed = false;
|
||||
}
|
||||
m_blockTemplate->update(m_minerData, *m_mempool, &m_params->m_wallet);
|
||||
m_blockTemplate->update(data, *m_mempool, &m_params->m_wallet);
|
||||
stratum_on_block();
|
||||
api_update_pool_stats();
|
||||
}
|
||||
@@ -522,29 +631,44 @@ void p2pool::download_block_headers(uint64_t current_height)
|
||||
});
|
||||
}
|
||||
|
||||
const uint64_t start_height = (current_height > BLOCK_HEADERS_REQUIRED) ? (current_height - BLOCK_HEADERS_REQUIRED) : 0;
|
||||
|
||||
s.m_pos = 0;
|
||||
s << "{\"jsonrpc\":\"2.0\",\"id\":\"0\",\"method\":\"get_block_headers_range\",\"params\":{\"start_height\":" << current_height - BLOCK_HEADERS_REQUIRED << ",\"end_height\":" << current_height - 1 << "}}\0";
|
||||
s << "{\"jsonrpc\":\"2.0\",\"id\":\"0\",\"method\":\"get_block_headers_range\",\"params\":{\"start_height\":" << start_height << ",\"end_height\":" << current_height - 1 << "}}\0";
|
||||
|
||||
JSONRPCRequest::call(m_params->m_host.c_str(), m_params->m_rpcPort, buf,
|
||||
[this, current_height](const char* data, size_t size)
|
||||
[this, start_height, current_height](const char* data, size_t size)
|
||||
{
|
||||
if (parse_block_headers_range(data, size) == BLOCK_HEADERS_REQUIRED) {
|
||||
if (parse_block_headers_range(data, size) == current_height - start_height) {
|
||||
update_median_timestamp();
|
||||
if (m_serversStarted.exchange(1) == 0) {
|
||||
try {
|
||||
m_ZMQReader = new ZMQReader(m_params->m_host.c_str(), m_params->m_zmqPort, this);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
LOGERR(1, "Couldn't start ZMQ reader: exception " << e.what());
|
||||
panic();
|
||||
}
|
||||
|
||||
m_stratumServer = new StratumServer(this);
|
||||
m_p2pServer = new P2PServer(this);
|
||||
#ifdef WITH_RANDOMX
|
||||
if (m_params->m_minerThreads) {
|
||||
start_mining(m_params->m_minerThreads);
|
||||
}
|
||||
#endif
|
||||
api_update_network_stats();
|
||||
}
|
||||
}
|
||||
else {
|
||||
LOGERR(1, "fatal error: couldn't download block headers for heights " << current_height - BLOCK_HEADERS_REQUIRED << " - " << current_height - 1);
|
||||
LOGERR(1, "fatal error: couldn't download block headers for heights " << start_height << " - " << current_height - 1);
|
||||
panic();
|
||||
}
|
||||
},
|
||||
[current_height](const char* data, size_t size)
|
||||
[start_height, current_height](const char* data, size_t size)
|
||||
{
|
||||
if (size > 0) {
|
||||
LOGERR(1, "fatal error: couldn't download block headers for heights " << current_height - BLOCK_HEADERS_REQUIRED << " - " << current_height - 1 << ", error " << log::const_buf(data, size));
|
||||
LOGERR(1, "fatal error: couldn't download block headers for heights " << start_height << " - " << current_height - 1 << ", error " << log::const_buf(data, size));
|
||||
panic();
|
||||
}
|
||||
});
|
||||
@@ -586,6 +710,7 @@ void p2pool::update_median_timestamp()
|
||||
uint64_t timestamps[TIMESTAMP_WINDOW];
|
||||
if (!get_timestamps(timestamps))
|
||||
{
|
||||
WriteLock lock(m_minerDataLock);
|
||||
m_minerData.median_timestamp = 0;
|
||||
return;
|
||||
}
|
||||
@@ -593,28 +718,28 @@ void p2pool::update_median_timestamp()
|
||||
std::sort(timestamps, timestamps + TIMESTAMP_WINDOW);
|
||||
|
||||
// Shift it +1 block compared to Monero's code because we don't have the latest block yet when we receive new miner data
|
||||
m_minerData.median_timestamp = (timestamps[TIMESTAMP_WINDOW / 2] + timestamps[TIMESTAMP_WINDOW / 2 + 1]) / 2;
|
||||
LOGINFO(4, "median timestamp updated to " << log::Gray() << m_minerData.median_timestamp);
|
||||
const uint64_t ts = (timestamps[TIMESTAMP_WINDOW / 2] + timestamps[TIMESTAMP_WINDOW / 2 + 1]) / 2;
|
||||
LOGINFO(4, "median timestamp updated to " << log::Gray() << ts);
|
||||
|
||||
WriteLock lock(m_minerDataLock);
|
||||
m_minerData.median_timestamp = ts;
|
||||
}
|
||||
|
||||
void p2pool::stratum_on_block()
|
||||
{
|
||||
#if 0
|
||||
uint8_t hashing_blob[128];
|
||||
uint64_t height;
|
||||
difficulty_type difficulty;
|
||||
difficulty_type sidechain_difficulty;
|
||||
hash seed_hash;
|
||||
size_t nonce_offset;
|
||||
uint32_t template_id;
|
||||
#ifdef WITH_RANDOMX
|
||||
{
|
||||
MutexLock lock(m_minerLock);
|
||||
|
||||
if (m_miner) {
|
||||
m_miner->on_block(*m_blockTemplate);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
m_blockTemplate->get_hashing_blob(0, hashing_blob, height, difficulty, sidechain_difficulty, seed_hash, nonce_offset, template_id);
|
||||
submit_block(template_id, 0, 0);
|
||||
#else
|
||||
if (m_stratumServer) {
|
||||
m_stratumServer->on_block(*m_blockTemplate);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void p2pool::get_info()
|
||||
@@ -686,18 +811,22 @@ void p2pool::parse_get_info_rpc(const char* data, size_t size)
|
||||
const auto& result = doc["result"];
|
||||
|
||||
struct {
|
||||
bool busy_syncing, mainnet, testnet, stagenet;
|
||||
bool busy_syncing, synchronized, mainnet, testnet, stagenet;
|
||||
} info;
|
||||
|
||||
if (!PARSE(result, info, busy_syncing) || !PARSE(result, info, mainnet) || !PARSE(result, info, testnet) || !PARSE(result, info, stagenet)) {
|
||||
if (!PARSE(result, info, busy_syncing) ||
|
||||
!PARSE(result, info, synchronized) ||
|
||||
!PARSE(result, info, mainnet) ||
|
||||
!PARSE(result, info, testnet) ||
|
||||
!PARSE(result, info, stagenet)) {
|
||||
LOGWARN(1, "get_info RPC response is invalid, trying again in 1 second");
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||
get_info();
|
||||
return;
|
||||
}
|
||||
|
||||
if (info.busy_syncing) {
|
||||
LOGINFO(1, "monerod is busy syncing, trying again in 1 second");
|
||||
if (info.busy_syncing || !info.synchronized) {
|
||||
LOGINFO(1, "monerod is " << (info.busy_syncing ? "busy syncing" : "not synchronized") << ", trying again in 1 second");
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||
get_info();
|
||||
return;
|
||||
@@ -767,10 +896,14 @@ void p2pool::parse_get_version_rpc(const char* data, size_t size)
|
||||
return;
|
||||
}
|
||||
|
||||
if (version < 0x30008) {
|
||||
const uint64_t required = m_params->m_disableRandomX ? 0x30009 : 0x30008;
|
||||
|
||||
if (version < required) {
|
||||
const uint64_t version_hi = version >> 16;
|
||||
const uint64_t version_lo = version & 65535;
|
||||
LOGERR(1, "monerod RPC v" << version_hi << '.' << version_lo << " is incompatible, update to RPC >= v3.8");
|
||||
const uint64_t required_version_hi = required >> 16;
|
||||
const uint64_t required_version_lo = required & 65535;
|
||||
LOGERR(1, "monerod RPC v" << version_hi << '.' << version_lo << " is incompatible, update to RPC >= v" << required_version_hi << '.' << required_version_lo << " (Monero v0.17.3.0 or newer)");
|
||||
panic();
|
||||
}
|
||||
|
||||
@@ -779,6 +912,8 @@ void p2pool::parse_get_version_rpc(const char* data, size_t size)
|
||||
|
||||
void p2pool::get_miner_data()
|
||||
{
|
||||
m_getMinerDataPending = true;
|
||||
|
||||
JSONRPCRequest::call(m_params->m_host.c_str(), m_params->m_rpcPort, "{\"jsonrpc\":\"2.0\",\"id\":\"0\",\"method\":\"get_miner_data\"}",
|
||||
[this](const char* data, size_t size)
|
||||
{
|
||||
@@ -791,11 +926,22 @@ void p2pool::get_miner_data()
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||
get_miner_data();
|
||||
}
|
||||
else {
|
||||
m_getMinerDataPending = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void p2pool::parse_get_miner_data_rpc(const char* data, size_t size)
|
||||
{
|
||||
hash h;
|
||||
keccak(reinterpret_cast<const uint8_t*>(data), static_cast<int>(size), h.h, HASH_SIZE);
|
||||
if (h == m_getMinerDataHash) {
|
||||
LOGWARN(4, "Received a duplicate get_miner_data RPC response, ignoring it");
|
||||
return;
|
||||
}
|
||||
m_getMinerDataHash = h;
|
||||
|
||||
rapidjson::Document doc;
|
||||
doc.Parse<rapidjson::kParseCommentsFlag | rapidjson::kParseTrailingCommasFlag>(data, size);
|
||||
|
||||
@@ -941,10 +1087,16 @@ void p2pool::api_update_network_stats()
|
||||
return;
|
||||
}
|
||||
|
||||
hash prev_id;
|
||||
{
|
||||
ReadLock lock(m_minerDataLock);
|
||||
prev_id = m_minerData.prev_id;
|
||||
}
|
||||
|
||||
ChainMain mainnet_tip;
|
||||
{
|
||||
ReadLock lock(m_mainchainLock);
|
||||
mainnet_tip = m_mainchainByHash[m_minerData.prev_id];
|
||||
mainnet_tip = m_mainchainByHash[prev_id];
|
||||
}
|
||||
|
||||
m_api->set(p2pool_api::Category::NETWORK, "stats",
|
||||
@@ -969,7 +1121,7 @@ void p2pool::api_update_pool_stats()
|
||||
uint64_t t;
|
||||
const difficulty_type& diff = m_sideChain->difficulty();
|
||||
const uint64_t hashrate = udiv128(diff.hi, diff.lo, m_sideChain->block_time(), &t);
|
||||
const uint64_t miners = m_sideChain->miner_count();
|
||||
const uint64_t miners = std::max<uint64_t>(m_sideChain->miner_count(), m_p2pServer ? m_p2pServer->peer_list_size() : 0U);
|
||||
const difficulty_type total_hashes = m_sideChain->total_hashes();
|
||||
|
||||
time_t last_block_found_time = 0;
|
||||
@@ -1006,10 +1158,16 @@ void p2pool::api_update_stats_mod()
|
||||
return;
|
||||
}
|
||||
|
||||
hash prev_id;
|
||||
{
|
||||
ReadLock lock(m_minerDataLock);
|
||||
prev_id = m_minerData.prev_id;
|
||||
}
|
||||
|
||||
ChainMain mainnet_tip;
|
||||
{
|
||||
ReadLock lock(m_mainchainLock);
|
||||
mainnet_tip = m_mainchainByHash[m_minerData.prev_id];
|
||||
mainnet_tip = m_mainchainByHash[prev_id];
|
||||
}
|
||||
|
||||
time_t last_block_found_time = 0;
|
||||
@@ -1020,10 +1178,11 @@ void p2pool::api_update_stats_mod()
|
||||
{
|
||||
MutexLock lock(m_foundBlocksLock);
|
||||
if (!m_foundBlocks.empty()) {
|
||||
last_block_found_time = m_foundBlocks.back().timestamp;
|
||||
last_block_found_height = m_foundBlocks.back().height;
|
||||
last_block_found_hash = m_foundBlocks.back().id;
|
||||
last_block_total_hashes = m_foundBlocks.back().total_hashes;
|
||||
const FoundBlock& b = m_foundBlocks.back();
|
||||
last_block_found_time = b.timestamp;
|
||||
last_block_found_height = b.height;
|
||||
last_block_found_hash = b.id;
|
||||
last_block_total_hashes = b.total_hashes;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1032,7 +1191,7 @@ void p2pool::api_update_stats_mod()
|
||||
s << last_block_found_hash << '\0';
|
||||
memcpy(last_block_found_buf + 4, "...", 4);
|
||||
|
||||
const uint64_t miners = m_sideChain->miner_count();
|
||||
const uint64_t miners = std::max<uint64_t>(m_sideChain->miner_count(), m_p2pServer ? m_p2pServer->peer_list_size() : 0U);
|
||||
|
||||
uint64_t t;
|
||||
const difficulty_type& diff = m_sideChain->difficulty();
|
||||
@@ -1049,7 +1208,7 @@ void p2pool::api_update_stats_mod()
|
||||
m_api->set(p2pool_api::Category::GLOBAL, "stats_mod",
|
||||
[&mainnet_tip, last_block_found_time, &last_block_found_buf, last_block_found_height, miners, hashrate, round_hashes, stratum_port](log::Stream& s)
|
||||
{
|
||||
s << "{\"config\":{\"ports\":[{\"port\":" << stratum_port << ",\"tls\":false}],\"fee\":0,\"minPaymentThreshold\":400000000},\"network\":{\"height\":"
|
||||
s << "{\"config\":{\"ports\":[{\"port\":" << stratum_port << ",\"tls\":false}],\"fee\":0,\"minPaymentThreshold\":300000000},\"network\":{\"height\":"
|
||||
<< mainnet_tip.height << "},\"pool\":{\"stats\":{\"lastBlockFound\":\""
|
||||
<< last_block_found_time << "000\"},\"blocks\":[\""
|
||||
<< static_cast<char*>(last_block_found_buf) << static_cast<char*>(last_block_found_buf) + HASH_SIZE * 2 - 4 << ':'
|
||||
@@ -1061,6 +1220,31 @@ void p2pool::api_update_stats_mod()
|
||||
});
|
||||
}
|
||||
|
||||
void p2pool::cleanup_mainchain_data(uint64_t height)
|
||||
{
|
||||
// Expects m_mainchainLock to be already locked here
|
||||
// Deletes everything older than 720 blocks, except for the 3 latest RandomX seed heights
|
||||
|
||||
constexpr uint64_t PRUNE_DISTANCE = BLOCK_HEADERS_REQUIRED;
|
||||
const uint64_t seed_height = get_seed_height(height);
|
||||
const std::array<uint64_t, 3> seed_heights{ seed_height, seed_height - SEEDHASH_EPOCH_BLOCKS, seed_height - SEEDHASH_EPOCH_BLOCKS * 2 };
|
||||
|
||||
for (auto it = m_mainchainByHeight.begin(); it != m_mainchainByHeight.end();) {
|
||||
const uint64_t h = it->first;
|
||||
if (h + PRUNE_DISTANCE >= height) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (std::find(seed_heights.begin(), seed_heights.end(), h) == seed_heights.end()) {
|
||||
m_mainchainByHash.erase(it->second.id);
|
||||
it = m_mainchainByHeight.erase(it);
|
||||
}
|
||||
else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void p2pool::api_update_block_found(const ChainMain* data)
|
||||
{
|
||||
clear_crypto_cache();
|
||||
@@ -1124,6 +1308,26 @@ bool p2pool::get_difficulty_at_height(uint64_t height, difficulty_type& diff)
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef WITH_RANDOMX
|
||||
void p2pool::start_mining(uint32_t threads)
|
||||
{
|
||||
stop_mining();
|
||||
|
||||
MutexLock lock(m_minerLock);
|
||||
m_miner = new Miner(this, threads);
|
||||
}
|
||||
|
||||
void p2pool::stop_mining()
|
||||
{
|
||||
MutexLock lock(m_minerLock);
|
||||
|
||||
if (m_miner) {
|
||||
delete m_miner;
|
||||
m_miner = nullptr;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void on_signal(uv_signal_t* handle, int signum)
|
||||
{
|
||||
p2pool* pool = reinterpret_cast<p2pool*>(handle->data);
|
||||
@@ -1166,6 +1370,10 @@ static bool init_uv_threadpool()
|
||||
|
||||
static bool init_signals(p2pool* pool)
|
||||
{
|
||||
#ifdef SIGPIPE
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
#endif
|
||||
|
||||
constexpr int signal_names[] = {
|
||||
SIGHUP,
|
||||
SIGINT,
|
||||
@@ -1198,6 +1406,29 @@ void p2pool::stop()
|
||||
uv_async_send(&m_stopAsync);
|
||||
}
|
||||
|
||||
void p2pool::restart_zmq()
|
||||
{
|
||||
if (!is_main_thread()) {
|
||||
uv_async_send(&m_restartZMQAsync);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!m_getMinerDataPending) {
|
||||
get_miner_data();
|
||||
}
|
||||
|
||||
delete m_ZMQReader;
|
||||
m_ZMQReader = nullptr;
|
||||
|
||||
try {
|
||||
m_ZMQReader = new ZMQReader(m_params->m_host.c_str(), m_params->m_zmqPort, this);
|
||||
m_zmqLastActive = seconds_since_epoch();
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
LOGERR(1, "Couldn't restart ZMQ reader: exception " << e.what());
|
||||
}
|
||||
}
|
||||
|
||||
int p2pool::run()
|
||||
{
|
||||
if (!m_params->ok()) {
|
||||
@@ -1216,11 +1447,11 @@ int p2pool::run()
|
||||
}
|
||||
|
||||
try {
|
||||
ZMQReader z(m_params->m_host.c_str(), m_params->m_zmqPort, this);
|
||||
get_info();
|
||||
load_found_blocks();
|
||||
const int rc = uv_run(uv_default_loop_checked(), UV_RUN_DEFAULT);
|
||||
LOGINFO(1, "uv_run exited, result = " << rc);
|
||||
delete m_ZMQReader;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
const char* s = e.what();
|
||||
@@ -1232,6 +1463,9 @@ int p2pool::run()
|
||||
|
||||
bkg_jobs_tracker.wait();
|
||||
|
||||
#ifdef WITH_RANDOMX
|
||||
delete m_miner;
|
||||
#endif
|
||||
delete m_stratumServer;
|
||||
delete m_p2pServer;
|
||||
|
||||
|
||||
+48
-14
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -19,19 +19,20 @@
|
||||
|
||||
#include "uv_util.h"
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace p2pool {
|
||||
|
||||
struct Params;
|
||||
class RandomX_Hasher;
|
||||
class RandomX_Hasher_Base;
|
||||
class BlockTemplate;
|
||||
class Mempool;
|
||||
class SideChain;
|
||||
class StratumServer;
|
||||
class P2PServer;
|
||||
class Miner;
|
||||
class ConsoleCommands;
|
||||
class p2pool_api;
|
||||
class ZMQReader;
|
||||
|
||||
class p2pool : public MinerCallbackHandler
|
||||
{
|
||||
@@ -47,18 +48,27 @@ public:
|
||||
const Params& params() const { return *m_params; }
|
||||
BlockTemplate& block_template() { return *m_blockTemplate; }
|
||||
SideChain& side_chain() { return *m_sideChain; }
|
||||
const MinerData& miner_data() const { return m_minerData; }
|
||||
|
||||
FORCEINLINE MinerData miner_data() const
|
||||
{
|
||||
ReadLock lock(m_minerDataLock);
|
||||
return m_minerData;
|
||||
}
|
||||
|
||||
p2pool_api* api() const { return m_api; }
|
||||
|
||||
RandomX_Hasher* hasher() const { return m_hasher; }
|
||||
bool calculate_hash(const void* data, size_t size, const hash& seed, hash& result);
|
||||
RandomX_Hasher_Base* hasher() const { return m_hasher; }
|
||||
bool calculate_hash(const void* data, size_t size, uint64_t height, const hash& seed, hash& result);
|
||||
static uint64_t get_seed_height(uint64_t height);
|
||||
bool get_seed(uint64_t height, hash& seed) const;
|
||||
|
||||
StratumServer* stratum_server() const { return m_stratumServer; }
|
||||
P2PServer* p2p_server() const { return m_p2pServer; }
|
||||
|
||||
#ifdef WITH_RANDOMX
|
||||
void print_miner_status();
|
||||
#endif
|
||||
|
||||
virtual void handle_tx(TxMempoolData& tx) override;
|
||||
virtual void handle_miner_data(MinerData& data) override;
|
||||
virtual void handle_chain_main(ChainMain& data, const char* extra) override;
|
||||
@@ -78,7 +88,14 @@ public:
|
||||
|
||||
bool get_difficulty_at_height(uint64_t height, difficulty_type& diff);
|
||||
|
||||
time_t zmq_last_active() const { return m_zmqLastActive; }
|
||||
#ifdef WITH_RANDOMX
|
||||
void start_mining(uint32_t threads);
|
||||
void stop_mining();
|
||||
#endif
|
||||
|
||||
uint64_t zmq_last_active() const { return m_zmqLastActive; }
|
||||
uint64_t start_time() const { return m_startTime; }
|
||||
void restart_zmq();
|
||||
|
||||
private:
|
||||
p2pool(const p2pool&) = delete;
|
||||
@@ -87,6 +104,7 @@ private:
|
||||
static void on_submit_block(uv_async_t* async) { reinterpret_cast<p2pool*>(async->data)->submit_block(); }
|
||||
static void on_update_block_template(uv_async_t* async) { reinterpret_cast<p2pool*>(async->data)->update_block_template(); }
|
||||
static void on_stop(uv_async_t*);
|
||||
static void on_restart_zmq(uv_async_t* async) { reinterpret_cast<p2pool*>(async->data)->restart_zmq(); }
|
||||
|
||||
void submit_block() const;
|
||||
|
||||
@@ -96,15 +114,17 @@ private:
|
||||
|
||||
p2pool_api* m_api;
|
||||
SideChain* m_sideChain;
|
||||
RandomX_Hasher* m_hasher;
|
||||
RandomX_Hasher_Base* m_hasher;
|
||||
BlockTemplate* m_blockTemplate;
|
||||
MinerData m_minerData;
|
||||
bool m_updateSeed;
|
||||
Mempool* m_mempool;
|
||||
|
||||
mutable uv_rwlock_t m_mainchainLock;
|
||||
std::map<uint64_t, ChainMain> m_mainchainByHeight;
|
||||
std::unordered_map<hash, ChainMain> m_mainchainByHash;
|
||||
unordered_map<hash, ChainMain> m_mainchainByHash;
|
||||
|
||||
mutable uv_rwlock_t m_minerDataLock;
|
||||
MinerData m_minerData;
|
||||
|
||||
enum { TIMESTAMP_WINDOW = 60 };
|
||||
bool get_timestamps(uint64_t (×tamps)[TIMESTAMP_WINDOW]) const;
|
||||
@@ -129,6 +149,8 @@ private:
|
||||
void api_update_pool_stats();
|
||||
void api_update_stats_mod();
|
||||
|
||||
void cleanup_mainchain_data(uint64_t height);
|
||||
|
||||
struct FoundBlock
|
||||
{
|
||||
FORCEINLINE FoundBlock(time_t _t, uint64_t _h, const hash& _id, const difficulty_type& _block_diff, const difficulty_type& _total_hashes)
|
||||
@@ -153,13 +175,18 @@ private:
|
||||
StratumServer* m_stratumServer = nullptr;
|
||||
P2PServer* m_p2pServer = nullptr;
|
||||
|
||||
#ifdef WITH_RANDOMX
|
||||
uv_mutex_t m_minerLock;
|
||||
Miner* m_miner = nullptr;
|
||||
#endif
|
||||
|
||||
ConsoleCommands* m_consoleCommands;
|
||||
|
||||
struct SubmitBlockData
|
||||
{
|
||||
uint32_t template_id;
|
||||
uint32_t nonce;
|
||||
uint32_t extra_nonce;
|
||||
uint32_t template_id = 0;
|
||||
uint32_t nonce = 0;
|
||||
uint32_t extra_nonce = 0;
|
||||
std::vector<uint8_t> blob;
|
||||
};
|
||||
|
||||
@@ -170,7 +197,14 @@ private:
|
||||
uv_async_t m_blockTemplateAsync;
|
||||
uv_async_t m_stopAsync;
|
||||
|
||||
time_t m_zmqLastActive = 0;
|
||||
std::atomic<uint64_t> m_zmqLastActive;
|
||||
uint64_t m_startTime;
|
||||
uv_async_t m_restartZMQAsync;
|
||||
|
||||
ZMQReader* m_ZMQReader = nullptr;
|
||||
|
||||
hash m_getMinerDataHash;
|
||||
bool m_getMinerDataPending = false;
|
||||
};
|
||||
|
||||
} // namespace p2pool
|
||||
|
||||
+2
-4
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -127,16 +127,14 @@ void p2pool_api::dump_to_file_async_internal(const Category& category, const cha
|
||||
|
||||
void p2pool_api::dump_to_file()
|
||||
{
|
||||
std::unordered_map<std::string, std::vector<char>> data;
|
||||
unordered_map<std::string, std::vector<char>> data;
|
||||
{
|
||||
MutexLock lock(m_dumpDataLock);
|
||||
data = std::move(m_dumpData);
|
||||
// cppcheck-suppress accessMoved
|
||||
m_dumpData.clear();
|
||||
}
|
||||
|
||||
for (auto& it : data) {
|
||||
// cppcheck-suppress uninitvar
|
||||
DumpFileWork* work = new DumpFileWork{ {}, {}, {}, it.first, std::move(it.second) };
|
||||
work->open_req.data = work;
|
||||
work->write_req.data = work;
|
||||
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -18,7 +18,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "uv_util.h"
|
||||
#include <unordered_map>
|
||||
|
||||
namespace p2pool {
|
||||
|
||||
@@ -35,6 +34,7 @@ public:
|
||||
LOCAL,
|
||||
};
|
||||
|
||||
// cppcheck-suppress functionConst
|
||||
void on_stop();
|
||||
|
||||
template<typename T>
|
||||
@@ -84,7 +84,7 @@ private:
|
||||
std::string m_localPath;
|
||||
|
||||
uv_mutex_t m_dumpDataLock;
|
||||
std::unordered_map<std::string, std::vector<char>> m_dumpData;
|
||||
unordered_map<std::string, std::vector<char>> m_dumpData;
|
||||
|
||||
uv_async_t m_dumpToFileAsync;
|
||||
};
|
||||
|
||||
+63
-15
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -20,62 +20,120 @@
|
||||
#include "stratum_server.h"
|
||||
#include "p2p_server.h"
|
||||
|
||||
void p2pool_usage();
|
||||
|
||||
namespace p2pool {
|
||||
|
||||
Params::Params(int argc, char* argv[])
|
||||
{
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
bool ok = false;
|
||||
|
||||
if ((strcmp(argv[i], "--host") == 0) && (i + 1 < argc)) {
|
||||
m_host = argv[++i];
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if ((strcmp(argv[i], "--rpc-port") == 0) && (i + 1 < argc)) {
|
||||
m_rpcPort = static_cast<uint32_t>(atoi(argv[++i]));
|
||||
m_rpcPort = strtoul(argv[++i], nullptr, 10);
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if ((strcmp(argv[i], "--zmq-port") == 0) && (i + 1 < argc)) {
|
||||
m_zmqPort = static_cast<uint32_t>(atoi(argv[++i]));
|
||||
m_zmqPort = strtoul(argv[++i], nullptr, 10);
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if (strcmp(argv[i], "--light-mode") == 0) {
|
||||
m_lightMode = true;
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if ((strcmp(argv[i], "--wallet") == 0) && (i + 1 < argc)) {
|
||||
m_wallet.decode(argv[++i]);
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if ((strcmp(argv[i], "--stratum") == 0) && (i + 1 < argc)) {
|
||||
m_stratumAddresses = argv[++i];
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if ((strcmp(argv[i], "--p2p") == 0) && (i + 1 < argc)) {
|
||||
m_p2pAddresses = argv[++i];
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if ((strcmp(argv[i], "--addpeers") == 0) && (i + 1 < argc)) {
|
||||
m_p2pPeerList = argv[++i];
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if ((strcmp(argv[i], "--loglevel") == 0) && (i + 1 < argc)) {
|
||||
const int level = std::min(std::max(atoi(argv[++i]), 0), log::MAX_GLOBAL_LOG_LEVEL);
|
||||
const int level = std::min(std::max<int>(strtol(argv[++i], nullptr, 10), 0), log::MAX_GLOBAL_LOG_LEVEL);
|
||||
log::GLOBAL_LOG_LEVEL = level;
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if ((strcmp(argv[i], "--config") == 0) && (i + 1 < argc)) {
|
||||
m_config = argv[++i];
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if ((strcmp(argv[i], "--data-api") == 0) && (i + 1 < argc)) {
|
||||
m_apiPath = argv[++i];
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if (strcmp(argv[i], "--stratum-api") == 0) {
|
||||
if ((strcmp(argv[i], "--local-api") == 0) || (strcmp(argv[i], "--stratum-api") == 0)) {
|
||||
m_localStats = true;
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if (strcmp(argv[i], "--no-cache") == 0) {
|
||||
m_blockCache = false;
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if (strcmp(argv[i], "--no-color") == 0) {
|
||||
log::CONSOLE_COLORS = false;
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if (strcmp(argv[i], "--no-randomx") == 0) {
|
||||
m_disableRandomX = true;
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if ((strcmp(argv[i], "--out-peers") == 0) && (i + 1 < argc)) {
|
||||
m_maxOutgoingPeers = std::min(std::max(strtoul(argv[++i], nullptr, 10), 10UL), 1000UL);
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if ((strcmp(argv[i], "--in-peers") == 0) && (i + 1 < argc)) {
|
||||
m_maxIncomingPeers = std::min(std::max(strtoul(argv[++i], nullptr, 10), 10UL), 1000UL);
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if ((strcmp(argv[i], "--start-mining") == 0) && (i + 1 < argc)) {
|
||||
m_minerThreads = std::min(std::max(strtoul(argv[++i], nullptr, 10), 1UL), 64UL);
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if (strcmp(argv[i], "--mini") == 0) {
|
||||
m_mini = true;
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if (strcmp(argv[i], "--no-autodiff") == 0) {
|
||||
m_autoDiff = false;
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if (!ok) {
|
||||
fprintf(stderr, "Unknown command line parameter %s\n\n", argv[i]);
|
||||
p2pool_usage();
|
||||
throw std::exception();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,16 +146,6 @@ Params::Params(int argc, char* argv[])
|
||||
|
||||
m_stratumAddresses = buf;
|
||||
}
|
||||
|
||||
if (m_p2pAddresses.empty()) {
|
||||
const int p2p_port = DEFAULT_P2P_PORT;
|
||||
|
||||
char buf[log::Stream::BUF_SIZE + 1];
|
||||
log::Stream s(buf);
|
||||
s << "[::]:" << p2p_port << ",0.0.0.0:" << p2p_port << '\0';
|
||||
|
||||
m_p2pAddresses = buf;
|
||||
}
|
||||
}
|
||||
|
||||
bool Params::ok() const
|
||||
|
||||
+11
-1
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -39,6 +39,16 @@ struct Params
|
||||
std::string m_apiPath;
|
||||
bool m_localStats = false;
|
||||
bool m_blockCache = true;
|
||||
#ifdef WITH_RANDOMX
|
||||
bool m_disableRandomX = false;
|
||||
#else
|
||||
bool m_disableRandomX = true;
|
||||
#endif
|
||||
uint32_t m_maxOutgoingPeers = 10;
|
||||
uint32_t m_maxIncomingPeers = 1000;
|
||||
uint32_t m_minerThreads = 0;
|
||||
bool m_mini = false;
|
||||
bool m_autoDiff = true;
|
||||
};
|
||||
|
||||
} // namespace p2pool
|
||||
|
||||
+37
-13
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -53,14 +53,13 @@ PoolBlock::PoolBlock()
|
||||
, m_invalid(false)
|
||||
, m_broadcasted(false)
|
||||
, m_wantBroadcast(false)
|
||||
, m_localTimestamp(time(nullptr))
|
||||
, m_localTimestamp(seconds_since_epoch())
|
||||
{
|
||||
uv_mutex_init_checked(&m_lock);
|
||||
|
||||
m_mainChainData.reserve(48 * 1024);
|
||||
m_outputs.reserve(2048);
|
||||
m_transactions.reserve(256);
|
||||
m_tmpInts.reserve(m_transactions.capacity() * HASH_SIZE);
|
||||
m_sideChainData.reserve(512);
|
||||
m_uncles.reserve(8);
|
||||
m_tmpTxExtra.reserve(80);
|
||||
@@ -110,14 +109,13 @@ PoolBlock& PoolBlock::operator=(const PoolBlock& b)
|
||||
m_cumulativeDifficulty = b.m_cumulativeDifficulty;
|
||||
m_sidechainId = b.m_sidechainId;
|
||||
m_tmpTxExtra = b.m_tmpTxExtra;
|
||||
m_tmpInts = b.m_tmpInts;
|
||||
m_depth = b.m_depth;
|
||||
m_verified = b.m_verified;
|
||||
m_invalid = b.m_invalid;
|
||||
m_broadcasted = b.m_broadcasted;
|
||||
m_wantBroadcast = b.m_wantBroadcast;
|
||||
|
||||
m_localTimestamp = time(nullptr);
|
||||
m_localTimestamp = seconds_since_epoch();
|
||||
|
||||
if (lock_result == 0) {
|
||||
uv_mutex_unlock(&b.m_lock);
|
||||
@@ -159,8 +157,12 @@ void PoolBlock::serialize_mainchain_data(uint32_t nonce, uint32_t extra_nonce, c
|
||||
|
||||
for (TxOutput& output : m_outputs) {
|
||||
writeVarint(output.m_reward, m_mainChainData);
|
||||
m_mainChainData.push_back(TXOUT_TO_KEY);
|
||||
m_mainChainData.push_back(output.m_txType);
|
||||
m_mainChainData.insert(m_mainChainData.end(), output.m_ephPublicKey.h, output.m_ephPublicKey.h + HASH_SIZE);
|
||||
|
||||
if (output.m_txType == TXOUT_TO_TAGGED_KEY) {
|
||||
m_mainChainData.push_back(output.m_viewTag);
|
||||
}
|
||||
}
|
||||
|
||||
m_mainChainOutputsBlobSize = static_cast<int>(m_mainChainData.size()) - m_mainChainOutputsOffset;
|
||||
@@ -227,7 +229,7 @@ void PoolBlock::serialize_sidechain_data()
|
||||
writeVarint(m_cumulativeDifficulty.hi, m_sideChainData);
|
||||
}
|
||||
|
||||
bool PoolBlock::get_pow_hash(RandomX_Hasher* hasher, const hash& seed_hash, hash& pow_hash)
|
||||
bool PoolBlock::get_pow_hash(RandomX_Hasher_Base* hasher, uint64_t height, const hash& seed_hash, hash& pow_hash)
|
||||
{
|
||||
alignas(8) uint8_t hashes[HASH_SIZE * 3];
|
||||
|
||||
@@ -276,28 +278,50 @@ bool PoolBlock::get_pow_hash(RandomX_Hasher* hasher, const hash& seed_hash, hash
|
||||
|
||||
cnt >>= 1;
|
||||
|
||||
m_tmpInts.resize(cnt * HASH_SIZE);
|
||||
memcpy(m_tmpInts.data(), h, (cnt * 2 - count) * HASH_SIZE);
|
||||
std::vector<uint8_t> tmp_ints(cnt * HASH_SIZE);
|
||||
memcpy(tmp_ints.data(), h, (cnt * 2 - count) * HASH_SIZE);
|
||||
|
||||
for (i = cnt * 2 - count, j = cnt * 2 - count; j < cnt; i += 2, ++j) {
|
||||
keccak(h + i * HASH_SIZE, HASH_SIZE * 2, m_tmpInts.data() + j * HASH_SIZE, HASH_SIZE);
|
||||
keccak(h + i * HASH_SIZE, HASH_SIZE * 2, tmp_ints.data() + j * HASH_SIZE, HASH_SIZE);
|
||||
}
|
||||
|
||||
while (cnt > 2) {
|
||||
cnt >>= 1;
|
||||
for (i = 0, j = 0; j < cnt; i += 2, ++j) {
|
||||
keccak(m_tmpInts.data() + i * HASH_SIZE, HASH_SIZE * 2, m_tmpInts.data() + j * HASH_SIZE, HASH_SIZE);
|
||||
keccak(tmp_ints.data() + i * HASH_SIZE, HASH_SIZE * 2, tmp_ints.data() + j * HASH_SIZE, HASH_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
keccak(m_tmpInts.data(), HASH_SIZE * 2, blob + blob_size, HASH_SIZE);
|
||||
keccak(tmp_ints.data(), HASH_SIZE * 2, blob + blob_size, HASH_SIZE);
|
||||
}
|
||||
}
|
||||
blob_size += HASH_SIZE;
|
||||
|
||||
writeVarint(count, [&blob, &blob_size](uint8_t b) { blob[blob_size++] = b; });
|
||||
|
||||
return hasher->calculate(blob, blob_size, seed_hash, pow_hash);
|
||||
return hasher->calculate(blob, blob_size, height, seed_hash, pow_hash);
|
||||
}
|
||||
|
||||
uint64_t PoolBlock::get_payout(const Wallet& w) const
|
||||
{
|
||||
for (size_t i = 0, n = m_outputs.size(); i < n; ++i) {
|
||||
const TxOutput& out = m_outputs[i];
|
||||
hash eph_public_key;
|
||||
|
||||
if (out.m_txType == TXOUT_TO_TAGGED_KEY) {
|
||||
if (w.get_eph_public_key_with_view_tag(m_txkeySec, i, eph_public_key, out.m_viewTag) && (eph_public_key == out.m_ephPublicKey)) {
|
||||
return out.m_reward;
|
||||
}
|
||||
}
|
||||
else {
|
||||
uint8_t view_tag;
|
||||
if (w.get_eph_public_key(m_txkeySec, i, eph_public_key, view_tag) && (eph_public_key == out.m_ephPublicKey)) {
|
||||
return out.m_reward;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace p2pool
|
||||
|
||||
+14
-7
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -28,7 +28,7 @@
|
||||
|
||||
namespace p2pool {
|
||||
|
||||
class RandomX_Hasher;
|
||||
class RandomX_Hasher_Base;
|
||||
class SideChain;
|
||||
|
||||
/*
|
||||
@@ -81,11 +81,13 @@ struct PoolBlock
|
||||
|
||||
struct TxOutput
|
||||
{
|
||||
FORCEINLINE TxOutput() : m_reward(0), m_ephPublicKey() {}
|
||||
FORCEINLINE TxOutput(uint64_t r, const hash& k) : m_reward(r), m_ephPublicKey(k) {}
|
||||
FORCEINLINE TxOutput() : m_reward(0), m_ephPublicKey(), m_txType(0), m_viewTag(0) {}
|
||||
FORCEINLINE TxOutput(uint64_t r, const hash& k, uint8_t tx_type, uint8_t view_tag) : m_reward(r), m_ephPublicKey(k), m_txType(tx_type), m_viewTag(view_tag) {}
|
||||
|
||||
uint64_t m_reward;
|
||||
hash m_ephPublicKey;
|
||||
uint8_t m_txType;
|
||||
uint8_t m_viewTag;
|
||||
};
|
||||
|
||||
std::vector<TxOutput> m_outputs;
|
||||
@@ -121,7 +123,6 @@ struct PoolBlock
|
||||
|
||||
// Just temporary stuff, not a part of the block
|
||||
std::vector<uint8_t> m_tmpTxExtra;
|
||||
std::vector<uint8_t> m_tmpInts;
|
||||
|
||||
uint64_t m_depth;
|
||||
|
||||
@@ -131,13 +132,19 @@ struct PoolBlock
|
||||
bool m_broadcasted;
|
||||
bool m_wantBroadcast;
|
||||
|
||||
time_t m_localTimestamp;
|
||||
uint64_t m_localTimestamp;
|
||||
|
||||
void serialize_mainchain_data(uint32_t nonce, uint32_t extra_nonce, const hash& sidechain_hash);
|
||||
void serialize_sidechain_data();
|
||||
|
||||
int deserialize(const uint8_t* data, size_t size, SideChain& sidechain);
|
||||
bool get_pow_hash(RandomX_Hasher* hasher, const hash& seed_hash, hash& pow_hash);
|
||||
bool get_pow_hash(RandomX_Hasher_Base* hasher, uint64_t height, const hash& seed_hash, hash& pow_hash);
|
||||
|
||||
uint64_t get_payout(const Wallet& w) const;
|
||||
|
||||
// Both tx types are allowed by Monero consensus during v15 because it needs to process pre-fork mempool transactions,
|
||||
// but P2Pool can switch to using only TXOUT_TO_TAGGED_KEY for miner payouts starting from v15
|
||||
FORCEINLINE uint8_t get_tx_type() const { return (m_majorVersion < HARDFORK_VIEW_TAGS_VERSION) ? TXOUT_TO_KEY : TXOUT_TO_TAGGED_KEY; }
|
||||
};
|
||||
|
||||
} // namespace p2pool
|
||||
|
||||
+14
-30
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -46,29 +46,7 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, SideChain& sidechai
|
||||
#define READ_BYTE(x) do { if (!read_byte(x)) return __LINE__; } while (0)
|
||||
#define EXPECT_BYTE(value) do { uint8_t tmp; READ_BYTE(tmp); if (tmp != (value)) return __LINE__; } while (0)
|
||||
|
||||
auto read_varint = [&data, data_end](auto& b) -> bool
|
||||
{
|
||||
uint64_t result = 0;
|
||||
int k = 0;
|
||||
|
||||
while (data < data_end) {
|
||||
if (k >= static_cast<int>(sizeof(b)) * 8) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint64_t cur_byte = *(data++);
|
||||
result |= (cur_byte & 0x7F) << k;
|
||||
k += 7;
|
||||
|
||||
if ((cur_byte & 0x80) == 0) {
|
||||
b = result;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
#define READ_VARINT(x) do { if (!read_varint(x)) return __LINE__; } while(0)
|
||||
#define READ_VARINT(x) do { data = readVarint(data, data_end, x); if (!data) return __LINE__; } while(0)
|
||||
|
||||
auto read_buf = [&data, data_end](void* buf, size_t size) -> bool
|
||||
{
|
||||
@@ -121,7 +99,7 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, SideChain& sidechai
|
||||
if (num_outputs > 0) {
|
||||
// Outputs are in the buffer, just read them
|
||||
// Each output is at least 34 bytes, exit early if there's not enough data left
|
||||
// 1 byte for reward, 1 byte for TXOUT_TO_KEY, 32 bytes for eph_pub_key
|
||||
// 1 byte for reward, 1 byte for tx_type, 32 bytes for eph_pub_key
|
||||
constexpr uint64_t MIN_OUTPUT_SIZE = 34;
|
||||
|
||||
if (num_outputs > std::numeric_limits<uint64_t>::max() / MIN_OUTPUT_SIZE) return __LINE__;
|
||||
@@ -130,15 +108,23 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, SideChain& sidechai
|
||||
m_outputs.clear();
|
||||
m_outputs.reserve(num_outputs);
|
||||
|
||||
const uint8_t expected_tx_type = get_tx_type();
|
||||
|
||||
for (uint64_t i = 0; i < num_outputs; ++i) {
|
||||
TxOutput t;
|
||||
|
||||
READ_VARINT(t.m_reward);
|
||||
total_reward += t.m_reward;
|
||||
|
||||
EXPECT_BYTE(TXOUT_TO_KEY);
|
||||
EXPECT_BYTE(expected_tx_type);
|
||||
t.m_txType = expected_tx_type;
|
||||
|
||||
READ_BUF(t.m_ephPublicKey.h, HASH_SIZE);
|
||||
|
||||
if (expected_tx_type == TXOUT_TO_TAGGED_KEY) {
|
||||
READ_BYTE(t.m_viewTag);
|
||||
}
|
||||
|
||||
m_outputs.emplace_back(std::move(t));
|
||||
}
|
||||
|
||||
@@ -187,7 +173,7 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, SideChain& sidechai
|
||||
READ_VARINT(m_extraNonceSize);
|
||||
|
||||
// Sanity check
|
||||
if ((m_extraNonceSize < EXTRA_NONCE_SIZE) || (m_extraNonceSize > EXTRA_NONCE_SIZE + 10)) return __LINE__;
|
||||
if ((m_extraNonceSize < EXTRA_NONCE_SIZE) || (m_extraNonceSize > EXTRA_NONCE_MAX_SIZE)) return __LINE__;
|
||||
|
||||
const int extra_nonce_offset = static_cast<int>((data - data_begin) + outputs_blob_size_diff);
|
||||
READ_BUF(&m_extraNonce, EXTRA_NONCE_SIZE);
|
||||
@@ -201,7 +187,6 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, SideChain& sidechai
|
||||
const int sidechain_hash_offset = static_cast<int>((data - data_begin) + outputs_blob_size_diff);
|
||||
READ_BUF(m_sidechainId.h, HASH_SIZE);
|
||||
|
||||
// cppcheck-suppress duplicateExpression
|
||||
if (static_cast<uint64_t>(data - tx_extra_begin) != tx_extra_size) return __LINE__;
|
||||
|
||||
EXPECT_BYTE(0);
|
||||
@@ -341,7 +326,6 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, SideChain& sidechai
|
||||
|
||||
// Defaults for off-chain variables
|
||||
m_tmpTxExtra.clear();
|
||||
m_tmpInts.clear();
|
||||
|
||||
m_depth = 0;
|
||||
|
||||
@@ -351,7 +335,7 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, SideChain& sidechai
|
||||
m_broadcasted = false;
|
||||
m_wantBroadcast = false;
|
||||
|
||||
m_localTimestamp = time(nullptr);
|
||||
m_localTimestamp = seconds_since_epoch();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
+135
-12
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -19,22 +19,29 @@
|
||||
#include "pow_hash.h"
|
||||
#include "p2pool.h"
|
||||
#include "params.h"
|
||||
#ifdef WITH_RANDOMX
|
||||
#include "randomx.h"
|
||||
#include "configuration.h"
|
||||
#include "virtual_machine.hpp"
|
||||
#endif
|
||||
#include "json_rpc_request.h"
|
||||
#include "json_parsers.h"
|
||||
#include <rapidjson/document.h>
|
||||
#include <thread>
|
||||
|
||||
static constexpr char log_category_prefix[] = "RandomX_Hasher ";
|
||||
|
||||
namespace p2pool {
|
||||
|
||||
#ifdef WITH_RANDOMX
|
||||
RandomX_Hasher::RandomX_Hasher(p2pool* pool)
|
||||
: m_pool(pool)
|
||||
, m_cache{}
|
||||
, m_dataset(nullptr)
|
||||
, m_seed{}
|
||||
, m_index(0)
|
||||
, m_setSeedCounter(0)
|
||||
, m_seedCounter(0)
|
||||
, m_oldSeedCounter(0)
|
||||
{
|
||||
uint64_t memory_allocated = 0;
|
||||
|
||||
@@ -116,6 +123,10 @@ RandomX_Hasher::~RandomX_Hasher()
|
||||
|
||||
void RandomX_Hasher::set_seed_async(const hash& seed)
|
||||
{
|
||||
if (m_seed[m_index] == seed) {
|
||||
return;
|
||||
}
|
||||
|
||||
struct Work
|
||||
{
|
||||
p2pool* pool;
|
||||
@@ -124,10 +135,7 @@ void RandomX_Hasher::set_seed_async(const hash& seed)
|
||||
uv_work_t req;
|
||||
};
|
||||
|
||||
Work* work = new Work{};
|
||||
work->pool = m_pool;
|
||||
work->hasher = this;
|
||||
work->seed = seed;
|
||||
Work* work = new Work{ m_pool, this, seed, {} };
|
||||
work->req.data = work;
|
||||
|
||||
const int err = uv_queue_work(uv_default_loop_checked(), &work->req,
|
||||
@@ -164,7 +172,7 @@ void RandomX_Hasher::set_seed(const hash& seed)
|
||||
WriteLock lock(m_datasetLock);
|
||||
uv_rwlock_wrlock(&m_cacheLock);
|
||||
|
||||
m_setSeedCounter.fetch_add(1);
|
||||
m_seedCounter.fetch_add(1);
|
||||
|
||||
if (m_seed[m_index] == seed) {
|
||||
uv_rwlock_wrunlock(&m_cacheLock);
|
||||
@@ -172,7 +180,6 @@ void RandomX_Hasher::set_seed(const hash& seed)
|
||||
}
|
||||
|
||||
{
|
||||
// cppcheck-suppress unreadVariable
|
||||
ON_SCOPE_LEAVE([this]() { uv_rwlock_wrunlock(&m_cacheLock); });
|
||||
|
||||
if (m_stopped.load()) {
|
||||
@@ -216,6 +223,11 @@ void RandomX_Hasher::set_seed(const hash& seed)
|
||||
numThreads /= 2;
|
||||
}
|
||||
|
||||
// wait for set_old_seed() before initializing dataset
|
||||
while (m_oldSeedCounter.load() == 0) {
|
||||
std::this_thread::yield();
|
||||
}
|
||||
|
||||
LOGINFO(1, log::LightCyan() << "running " << numThreads << " threads to update dataset");
|
||||
|
||||
ReadLock lock2(m_cacheLock);
|
||||
@@ -266,8 +278,8 @@ void RandomX_Hasher::set_seed(const hash& seed)
|
||||
void RandomX_Hasher::set_old_seed(const hash& seed)
|
||||
{
|
||||
// set_seed() must go first, wait for it
|
||||
while (m_setSeedCounter.load() == 0) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
while (m_seedCounter.load() == 0) {
|
||||
std::this_thread::yield();
|
||||
}
|
||||
|
||||
LOGINFO(1, "old seed " << log::LightBlue() << seed);
|
||||
@@ -275,6 +287,8 @@ void RandomX_Hasher::set_old_seed(const hash& seed)
|
||||
{
|
||||
WriteLock lock(m_cacheLock);
|
||||
|
||||
m_oldSeedCounter.fetch_add(1);
|
||||
|
||||
const uint32_t old_index = m_index ^ 1;
|
||||
m_seed[old_index] = seed;
|
||||
|
||||
@@ -302,11 +316,16 @@ void RandomX_Hasher::set_old_seed(const hash& seed)
|
||||
LOGINFO(1, log::LightCyan() << "old cache updated");
|
||||
}
|
||||
|
||||
bool RandomX_Hasher::calculate(const void* data, size_t size, const hash& seed, hash& result)
|
||||
void RandomX_Hasher::sync_wait()
|
||||
{
|
||||
ReadLock lock(m_datasetLock);
|
||||
ReadLock lock2(m_cacheLock);
|
||||
}
|
||||
|
||||
bool RandomX_Hasher::calculate(const void* data, size_t size, uint64_t /*height*/, const hash& seed, hash& result)
|
||||
{
|
||||
// First try to use the dataset if it's ready
|
||||
if (uv_rwlock_tryrdlock(&m_datasetLock) == 0) {
|
||||
// cppcheck-suppress unreadVariable
|
||||
ON_SCOPE_LEAVE([this]() { uv_rwlock_rdunlock(&m_datasetLock); });
|
||||
|
||||
if (m_stopped.load()) {
|
||||
@@ -347,5 +366,109 @@ bool RandomX_Hasher::calculate(const void* data, size_t size, const hash& seed,
|
||||
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
RandomX_Hasher_RPC::RandomX_Hasher_RPC(p2pool* pool)
|
||||
: m_pool(pool)
|
||||
, m_loopThread{}
|
||||
{
|
||||
int err = uv_loop_init(&m_loop);
|
||||
if (err) {
|
||||
LOGERR(1, "failed to create event loop, error " << uv_err_name(err));
|
||||
panic();
|
||||
}
|
||||
|
||||
uv_async_init(&m_loop, &m_shutdownAsync, on_shutdown);
|
||||
uv_async_init(&m_loop, &m_kickTheLoopAsync, nullptr);
|
||||
m_shutdownAsync.data = this;
|
||||
|
||||
uv_mutex_init_checked(&m_requestMutex);
|
||||
uv_mutex_init_checked(&m_condMutex);
|
||||
|
||||
err = uv_cond_init(&m_cond);
|
||||
if (err) {
|
||||
LOGERR(1, "failed to create cond, error " << uv_err_name(err));
|
||||
panic();
|
||||
}
|
||||
|
||||
err = uv_thread_create(&m_loopThread, loop, this);
|
||||
if (err) {
|
||||
LOGERR(1, "failed to start event loop thread, error " << uv_err_name(err));
|
||||
panic();
|
||||
}
|
||||
}
|
||||
|
||||
RandomX_Hasher_RPC::~RandomX_Hasher_RPC()
|
||||
{
|
||||
uv_async_send(&m_shutdownAsync);
|
||||
uv_thread_join(&m_loopThread);
|
||||
|
||||
uv_mutex_destroy(&m_requestMutex);
|
||||
uv_mutex_destroy(&m_condMutex);
|
||||
uv_cond_destroy(&m_cond);
|
||||
|
||||
LOGINFO(1, "stopped");
|
||||
}
|
||||
|
||||
void RandomX_Hasher_RPC::loop(void* data)
|
||||
{
|
||||
LOGINFO(1, "event loop started");
|
||||
RandomX_Hasher_RPC* hasher = static_cast<RandomX_Hasher_RPC*>(data);
|
||||
uv_run(&hasher->m_loop, UV_RUN_DEFAULT);
|
||||
uv_loop_close(&hasher->m_loop);
|
||||
LOGINFO(1, "event loop stopped");
|
||||
}
|
||||
|
||||
bool RandomX_Hasher_RPC::calculate(const void* data_ptr, size_t size, uint64_t height, const hash& /*seed*/, hash& h)
|
||||
{
|
||||
MutexLock lock(m_requestMutex);
|
||||
|
||||
const uint8_t* data = reinterpret_cast<const uint8_t*>(data_ptr);
|
||||
const uint8_t major_version = data[0];
|
||||
|
||||
char buf[log::Stream::BUF_SIZE + 1];
|
||||
log::Stream s(buf);
|
||||
s << "{\"jsonrpc\":\"2.0\",\"id\":\"0\",\"method\":\"calc_pow\",\"params\":{\"major_version\":" << major_version <<
|
||||
",\"height\":" << height <<
|
||||
",\"block_blob\":\"" << log::hex_buf(data, size) << '"' <<
|
||||
",\"seed_hash\":\"\"}}";
|
||||
|
||||
volatile int result = 0;
|
||||
volatile bool done = false;
|
||||
|
||||
JSONRPCRequest::call(m_pool->params().m_host.c_str(), m_pool->params().m_rpcPort, buf,
|
||||
[&result, &h](const char* data, size_t size)
|
||||
{
|
||||
rapidjson::Document doc;
|
||||
if (doc.Parse(data, size).HasParseError() || !parseValue(doc, "result", h)) {
|
||||
LOGWARN(3, "RPC calc_pow: invalid JSON response (parse error)");
|
||||
result = -1;
|
||||
return;
|
||||
}
|
||||
result = 1;
|
||||
},
|
||||
[this, &result, &done](const char* data, size_t size)
|
||||
{
|
||||
if (size > 0) {
|
||||
LOGWARN(3, "RPC calc_pow: server returned error " << log::const_buf(data, size));
|
||||
result = -1;
|
||||
}
|
||||
|
||||
MutexLock lock2(m_condMutex);
|
||||
done = true;
|
||||
uv_cond_signal(&m_cond);
|
||||
}, &m_loop);
|
||||
|
||||
uv_async_send(&m_kickTheLoopAsync);
|
||||
|
||||
{
|
||||
MutexLock lock2(m_condMutex);
|
||||
while (!done) {
|
||||
uv_cond_wait(&m_cond, &m_condMutex);
|
||||
}
|
||||
}
|
||||
|
||||
return result > 0;
|
||||
}
|
||||
|
||||
} // namespace p2pool
|
||||
|
||||
+64
-9
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -27,25 +27,47 @@ namespace p2pool {
|
||||
|
||||
class p2pool;
|
||||
|
||||
class RandomX_Hasher
|
||||
class RandomX_Hasher_Base
|
||||
{
|
||||
public:
|
||||
virtual ~RandomX_Hasher_Base() {}
|
||||
|
||||
virtual void set_seed_async(const hash&) {}
|
||||
virtual void set_old_seed(const hash&) {}
|
||||
|
||||
virtual randomx_cache* cache() const { return nullptr; }
|
||||
virtual randomx_dataset* dataset() const { return nullptr; }
|
||||
virtual uint32_t seed_counter() const { return 0; }
|
||||
virtual void sync_wait() {}
|
||||
|
||||
virtual bool calculate(const void* data, size_t size, uint64_t height, const hash& seed, hash& result) = 0;
|
||||
};
|
||||
|
||||
#ifdef WITH_RANDOMX
|
||||
class RandomX_Hasher : public RandomX_Hasher_Base
|
||||
{
|
||||
public:
|
||||
explicit RandomX_Hasher(p2pool* pool);
|
||||
~RandomX_Hasher();
|
||||
|
||||
void set_seed_async(const hash& seed);
|
||||
void set_seed_async(const hash& seed) override;
|
||||
void set_seed(const hash& seed);
|
||||
|
||||
void set_old_seed(const hash& seed);
|
||||
void set_old_seed(const hash& seed) override;
|
||||
|
||||
bool calculate(const void* data, size_t size, const hash& seed, hash& result);
|
||||
randomx_cache* cache() const override { return m_cache[m_index]; }
|
||||
randomx_dataset* dataset() const override { return m_dataset; }
|
||||
uint32_t seed_counter() const override { return m_seedCounter.load(); }
|
||||
void sync_wait() override;
|
||||
|
||||
bool calculate(const void* data, size_t size, uint64_t height, const hash& seed, hash& result) override;
|
||||
|
||||
private:
|
||||
|
||||
struct ThreadSafeVM
|
||||
{
|
||||
uv_mutex_t mutex;
|
||||
randomx_vm* vm;
|
||||
uv_mutex_t mutex{};
|
||||
randomx_vm* vm = nullptr;
|
||||
};
|
||||
|
||||
p2pool* m_pool;
|
||||
@@ -62,12 +84,45 @@ private:
|
||||
// 1: light VM for the previous seed
|
||||
// 2: full dataset VM for the current seed
|
||||
enum { FULL_DATASET_VM = 2 };
|
||||
ThreadSafeVM m_vm[3];
|
||||
ThreadSafeVM m_vm[3]{};
|
||||
|
||||
hash m_seed[2];
|
||||
uint32_t m_index;
|
||||
|
||||
std::atomic<uint32_t> m_setSeedCounter;
|
||||
std::atomic<uint32_t> m_seedCounter;
|
||||
std::atomic<uint32_t> m_oldSeedCounter;
|
||||
};
|
||||
#endif
|
||||
|
||||
class RandomX_Hasher_RPC : public RandomX_Hasher_Base
|
||||
{
|
||||
public:
|
||||
explicit RandomX_Hasher_RPC(p2pool* pool);
|
||||
~RandomX_Hasher_RPC();
|
||||
|
||||
bool calculate(const void* data, size_t size, uint64_t height, const hash& seed, hash& result) override;
|
||||
|
||||
private:
|
||||
static void loop(void* data);
|
||||
|
||||
p2pool* m_pool;
|
||||
|
||||
uv_mutex_t m_requestMutex;
|
||||
uv_loop_t m_loop;
|
||||
|
||||
uv_thread_t m_loopThread;
|
||||
uv_mutex_t m_condMutex;
|
||||
uv_cond_t m_cond;
|
||||
|
||||
uv_async_t m_shutdownAsync;
|
||||
uv_async_t m_kickTheLoopAsync;
|
||||
|
||||
static void on_shutdown(uv_async_t* async)
|
||||
{
|
||||
RandomX_Hasher_RPC* server = reinterpret_cast<RandomX_Hasher_RPC*>(async->data);
|
||||
uv_close(reinterpret_cast<uv_handle_t*>(&server->m_shutdownAsync), nullptr);
|
||||
uv_close(reinterpret_cast<uv_handle_t*>(&server->m_kickTheLoopAsync), nullptr);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace p2pool
|
||||
|
||||
+345
-149
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -21,12 +21,15 @@
|
||||
#include "pool_block.h"
|
||||
#include "wallet.h"
|
||||
#include "block_template.h"
|
||||
#ifdef WITH_RANDOMX
|
||||
#include "randomx.h"
|
||||
#include "dataset.hpp"
|
||||
#include "configuration.h"
|
||||
#include "intrin_portable.h"
|
||||
#endif
|
||||
#include "keccak.h"
|
||||
#include "p2p_server.h"
|
||||
#include "stratum_server.h"
|
||||
#include "params.h"
|
||||
#include "json_parsers.h"
|
||||
#include <rapidjson/document.h>
|
||||
@@ -51,14 +54,14 @@ static_assert(1 <= UNCLE_BLOCK_DEPTH && UNCLE_BLOCK_DEPTH <= 10, "Invalid UNCLE_
|
||||
|
||||
namespace p2pool {
|
||||
|
||||
static constexpr uint8_t default_consensus_id[HASH_SIZE] = {
|
||||
34,175,126,231,181,11,104,146,227,153,218,107,44,108,68,39,178,81,4,212,169,4,142,0,177,110,157,240,68,7,249,24
|
||||
};
|
||||
static constexpr uint8_t default_consensus_id[HASH_SIZE] = { 34,175,126,231,181,11,104,146,227,153,218,107,44,108,68,39,178,81,4,212,169,4,142,0,177,110,157,240,68,7,249,24 };
|
||||
static constexpr uint8_t mini_consensus_id[HASH_SIZE] = { 57,130,201,26,149,174,199,250,66,80,189,18,108,216,194,220,136,23,63,24,64,113,221,44,219,86,39,163,53,24,126,196 };
|
||||
|
||||
SideChain::SideChain(p2pool* pool, NetworkType type, const char* pool_name)
|
||||
: m_pool(pool)
|
||||
, m_networkType(type)
|
||||
, m_chainTip(nullptr)
|
||||
, m_chainTip{ nullptr }
|
||||
, m_seenWalletsLastPruneTime(0)
|
||||
, m_poolName(pool_name ? pool_name : "default")
|
||||
, m_targetBlockTime(10)
|
||||
, m_minDifficulty(MIN_DIFFICULTY, 0)
|
||||
@@ -76,7 +79,10 @@ SideChain::SideChain(p2pool* pool, NetworkType type, const char* pool_name)
|
||||
panic();
|
||||
}
|
||||
|
||||
uv_mutex_init_checked(&m_sidechainLock);
|
||||
uv_rwlock_init_checked(&m_sidechainLock);
|
||||
uv_mutex_init_checked(&m_seenWalletsLock);
|
||||
uv_mutex_init_checked(&m_seenBlocksLock);
|
||||
uv_rwlock_init_checked(&m_curDifficultyLock);
|
||||
|
||||
m_difficultyData.reserve(m_chainWindowSize);
|
||||
|
||||
@@ -94,12 +100,18 @@ SideChain::SideChain(p2pool* pool, NetworkType type, const char* pool_name)
|
||||
<< m_unclePenalty << '\0';
|
||||
|
||||
constexpr char default_config[] = "mainnet\0" "default\0" "\0" "10\0" "100000\0" "2160\0" "20\0";
|
||||
constexpr char mini_config[] = "mainnet\0" "mini\0" "\0" "10\0" "100000\0" "2160\0" "20\0";
|
||||
|
||||
// Hardcoded default consensus ID
|
||||
if (memcmp(buf, default_config, sizeof(default_config) - 1) == 0) {
|
||||
m_consensusId.assign(default_consensus_id, default_consensus_id + HASH_SIZE);
|
||||
}
|
||||
// Hardcoded mini consensus ID
|
||||
else if (memcmp(buf, mini_config, sizeof(mini_config) - 1) == 0) {
|
||||
m_consensusId.assign(mini_consensus_id, mini_consensus_id + HASH_SIZE);
|
||||
}
|
||||
else {
|
||||
#ifdef WITH_RANDOMX
|
||||
const randomx_flags flags = randomx_get_flags();
|
||||
randomx_cache* cache = randomx_alloc_cache(flags | RANDOMX_FLAG_LARGE_PAGES);
|
||||
if (!cache) {
|
||||
@@ -134,6 +146,10 @@ SideChain::SideChain(p2pool* pool, NetworkType type, const char* pool_name)
|
||||
keccak(reinterpret_cast<uint8_t*>(scratchpad), static_cast<int>(scratchpad_size * sizeof(rx_vec_i128)), id.h, HASH_SIZE);
|
||||
randomx_release_cache(cache);
|
||||
m_consensusId.assign(id.h, id.h + HASH_SIZE);
|
||||
#else
|
||||
LOGERR(1, "Can't calculate consensus ID without RandomX library");
|
||||
panic();
|
||||
#endif
|
||||
}
|
||||
|
||||
s.m_pos = 0;
|
||||
@@ -141,26 +157,32 @@ SideChain::SideChain(p2pool* pool, NetworkType type, const char* pool_name)
|
||||
|
||||
// Hide most consensus ID bytes, we only want it on screen to show that we're on the right sidechain
|
||||
memset(buf + 8, '*', HASH_SIZE * 2 - 16);
|
||||
LOGINFO(1, "consensus ID = " << log::LightCyan() << static_cast<char*>(buf));
|
||||
m_consensusIdDisplayStr.assign(buf);
|
||||
LOGINFO(1, "consensus ID = " << log::LightCyan() << m_consensusIdDisplayStr.c_str());
|
||||
}
|
||||
|
||||
SideChain::~SideChain()
|
||||
{
|
||||
uv_mutex_destroy(&m_sidechainLock);
|
||||
for (auto& it : m_blocksById) {
|
||||
uv_rwlock_destroy(&m_sidechainLock);
|
||||
uv_mutex_destroy(&m_seenWalletsLock);
|
||||
uv_mutex_destroy(&m_seenBlocksLock);
|
||||
uv_rwlock_destroy(&m_curDifficultyLock);
|
||||
for (const auto& it : m_blocksById) {
|
||||
delete it.second;
|
||||
}
|
||||
}
|
||||
|
||||
void SideChain::fill_sidechain_data(PoolBlock& block, Wallet* w, const hash& txkeySec, std::vector<MinerShare>& shares)
|
||||
void SideChain::fill_sidechain_data(PoolBlock& block, const Wallet* w, const hash& txkeySec, std::vector<MinerShare>& shares) const
|
||||
{
|
||||
MutexLock lock(m_sidechainLock);
|
||||
ReadLock lock(m_sidechainLock);
|
||||
|
||||
block.m_minerWallet = *w;
|
||||
block.m_txkeySec = txkeySec;
|
||||
block.m_uncles.clear();
|
||||
|
||||
if (!m_chainTip) {
|
||||
const PoolBlock* tip = m_chainTip;
|
||||
|
||||
if (!tip) {
|
||||
block.m_parent = {};
|
||||
block.m_sidechainHeight = 0;
|
||||
block.m_difficulty = m_minDifficulty;
|
||||
@@ -170,8 +192,8 @@ void SideChain::fill_sidechain_data(PoolBlock& block, Wallet* w, const hash& txk
|
||||
return;
|
||||
}
|
||||
|
||||
block.m_parent = m_chainTip->m_sidechainId;
|
||||
block.m_sidechainHeight = m_chainTip->m_sidechainHeight + 1;
|
||||
block.m_parent = tip->m_sidechainId;
|
||||
block.m_sidechainHeight = tip->m_sidechainHeight + 1;
|
||||
|
||||
// Collect uncles from 3 previous block heights
|
||||
|
||||
@@ -179,15 +201,19 @@ void SideChain::fill_sidechain_data(PoolBlock& block, Wallet* w, const hash& txk
|
||||
std::vector<hash> mined_blocks;
|
||||
mined_blocks.reserve(UNCLE_BLOCK_DEPTH * 2 + 1);
|
||||
|
||||
PoolBlock* tmp = m_chainTip;
|
||||
for (uint64_t i = 0, n = std::min<uint64_t>(UNCLE_BLOCK_DEPTH, m_chainTip->m_sidechainHeight + 1); tmp && (i < n); ++i) {
|
||||
const PoolBlock* tmp = tip;
|
||||
for (uint64_t i = 0, n = std::min<uint64_t>(UNCLE_BLOCK_DEPTH, tip->m_sidechainHeight + 1); tmp && (i < n); ++i) {
|
||||
mined_blocks.push_back(tmp->m_sidechainId);
|
||||
mined_blocks.insert(mined_blocks.end(), tmp->m_uncles.begin(), tmp->m_uncles.end());
|
||||
tmp = get_parent(tmp);
|
||||
}
|
||||
|
||||
for (uint64_t i = 0, n = std::min<uint64_t>(UNCLE_BLOCK_DEPTH, m_chainTip->m_sidechainHeight + 1); i < n; ++i) {
|
||||
for (PoolBlock* uncle : m_blocksByHeight[m_chainTip->m_sidechainHeight - i]) {
|
||||
for (uint64_t i = 0, n = std::min<uint64_t>(UNCLE_BLOCK_DEPTH, tip->m_sidechainHeight + 1); i < n; ++i) {
|
||||
auto it = m_blocksByHeight.find(tip->m_sidechainHeight - i);
|
||||
if (it == m_blocksByHeight.end()) {
|
||||
continue;
|
||||
}
|
||||
for (const PoolBlock* uncle : it->second) {
|
||||
// Only add verified and valid blocks
|
||||
if (!uncle || !uncle->m_verified || uncle->m_invalid) {
|
||||
continue;
|
||||
@@ -201,17 +227,14 @@ void SideChain::fill_sidechain_data(PoolBlock& block, Wallet* w, const hash& txk
|
||||
// Only add it if it's on the same chain
|
||||
bool same_chain = false;
|
||||
do {
|
||||
tmp = m_chainTip;
|
||||
while (tmp->m_sidechainHeight > uncle->m_sidechainHeight) {
|
||||
tmp = tip;
|
||||
while (tmp && (tmp->m_sidechainHeight > uncle->m_sidechainHeight)) {
|
||||
tmp = get_parent(tmp);
|
||||
if (!tmp) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!tmp || (tmp->m_sidechainHeight < uncle->m_sidechainHeight)) {
|
||||
break;
|
||||
}
|
||||
PoolBlock* tmp2 = uncle;
|
||||
const PoolBlock* tmp2 = uncle;
|
||||
for (size_t j = 0; (j < UNCLE_BLOCK_DEPTH) && tmp && tmp2 && (tmp->m_sidechainHeight + UNCLE_BLOCK_DEPTH >= block.m_sidechainHeight); ++j) {
|
||||
if (tmp->m_parent == tmp2->m_parent) {
|
||||
same_chain = true;
|
||||
@@ -244,8 +267,8 @@ void SideChain::fill_sidechain_data(PoolBlock& block, Wallet* w, const hash& txk
|
||||
block.m_uncles.erase(std::unique(block.m_uncles.begin(), block.m_uncles.end()), block.m_uncles.end());
|
||||
}
|
||||
|
||||
block.m_difficulty = m_curDifficulty;
|
||||
block.m_cumulativeDifficulty = m_chainTip->m_cumulativeDifficulty + block.m_difficulty;
|
||||
block.m_difficulty = difficulty();
|
||||
block.m_cumulativeDifficulty = tip->m_cumulativeDifficulty + block.m_difficulty;
|
||||
|
||||
for (const hash& uncle_id : block.m_uncles) {
|
||||
auto it = m_blocksById.find(uncle_id);
|
||||
@@ -259,6 +282,11 @@ void SideChain::fill_sidechain_data(PoolBlock& block, Wallet* w, const hash& txk
|
||||
get_shares(&block, shares);
|
||||
}
|
||||
|
||||
P2PServer* SideChain::p2pServer() const
|
||||
{
|
||||
return m_pool ? m_pool->p2p_server() : nullptr;
|
||||
}
|
||||
|
||||
bool SideChain::get_shares(PoolBlock* tip, std::vector<MinerShare>& shares) const
|
||||
{
|
||||
shares.clear();
|
||||
@@ -343,21 +371,22 @@ bool SideChain::get_shares(PoolBlock* tip, std::vector<MinerShare>& shares) cons
|
||||
|
||||
bool SideChain::block_seen(const PoolBlock& block)
|
||||
{
|
||||
MutexLock lock(m_sidechainLock);
|
||||
|
||||
// Check if it's some old block
|
||||
if (m_chainTip && m_chainTip->m_sidechainHeight > block.m_sidechainHeight + m_chainWindowSize * 2 &&
|
||||
block.m_cumulativeDifficulty < m_chainTip->m_cumulativeDifficulty) {
|
||||
const PoolBlock* tip = m_chainTip;
|
||||
|
||||
if (tip && tip->m_sidechainHeight > block.m_sidechainHeight + m_chainWindowSize * 2 &&
|
||||
block.m_cumulativeDifficulty < tip->m_cumulativeDifficulty) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if it was received before
|
||||
MutexLock lock(m_seenBlocksLock);
|
||||
return !m_seenBlocks.insert(block.m_sidechainId).second;
|
||||
}
|
||||
|
||||
void SideChain::unsee_block(const PoolBlock& block)
|
||||
{
|
||||
MutexLock lock(m_sidechainLock);
|
||||
MutexLock lock(m_seenBlocksLock);
|
||||
m_seenBlocks.erase(block.m_sidechainId);
|
||||
}
|
||||
|
||||
@@ -370,31 +399,36 @@ bool SideChain::add_external_block(PoolBlock& block, std::vector<hash>& missing_
|
||||
return false;
|
||||
}
|
||||
|
||||
difficulty_type min_accepted_diff;
|
||||
const difficulty_type expected_diff = difficulty();
|
||||
bool too_low_diff = (block.m_difficulty < expected_diff);
|
||||
{
|
||||
MutexLock lock(m_sidechainLock);
|
||||
ReadLock lock(m_sidechainLock);
|
||||
if (m_blocksById.find(block.m_sidechainId) != m_blocksById.end()) {
|
||||
LOGINFO(4, "add_external_block: block " << block.m_sidechainId << " is already added");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Find the minimum difficulty in the current PPLNS window
|
||||
min_accepted_diff = m_curDifficulty;
|
||||
for (PoolBlock* tmp = m_chainTip; tmp && (tmp->m_sidechainHeight + m_chainWindowSize > m_chainTip->m_sidechainHeight); tmp = get_parent(tmp)) {
|
||||
if (tmp->m_difficulty < min_accepted_diff) {
|
||||
min_accepted_diff = tmp->m_difficulty;
|
||||
// This is mainly an anti-spam measure, not an actual verification step
|
||||
if (too_low_diff) {
|
||||
// Reduce required diff by 50% (by doubling this block's diff) to account for alternative chains
|
||||
difficulty_type diff2 = block.m_difficulty;
|
||||
diff2 += block.m_difficulty;
|
||||
|
||||
const PoolBlock* tip = m_chainTip;
|
||||
|
||||
for (const PoolBlock* tmp = tip; tmp && (tmp->m_sidechainHeight + m_chainWindowSize > tip->m_sidechainHeight); tmp = get_parent(tmp)) {
|
||||
if (diff2 >= tmp->m_difficulty) {
|
||||
too_low_diff = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LOGINFO(4, "add_external_block: height = " << block.m_sidechainHeight << ", id = " << block.m_sidechainId << ", mainchain height = " << block.m_txinGenHeight);
|
||||
|
||||
// Reduce it by 50% to account for alternative chains. This is mainly an anti-spam measure, not an actual verification step
|
||||
min_accepted_diff.lo = (min_accepted_diff.lo >> 1) | (min_accepted_diff.hi << 63);
|
||||
min_accepted_diff.hi >>= 1;
|
||||
|
||||
if (block.m_difficulty < min_accepted_diff) {
|
||||
LOGWARN(4, "add_external_block: block has too low difficulty " << block.m_difficulty << ", expected >= " << min_accepted_diff << ". Ignoring it.");
|
||||
if (too_low_diff) {
|
||||
LOGWARN(4, "add_external_block: block has too low difficulty " << block.m_difficulty << ", expected >= ~" << expected_diff << ". Ignoring it.");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -418,14 +452,14 @@ bool SideChain::add_external_block(PoolBlock& block, std::vector<hash>& missing_
|
||||
}
|
||||
|
||||
hash pow_hash;
|
||||
if (!block.get_pow_hash(m_pool->hasher(), seed, pow_hash)) {
|
||||
if (!block.get_pow_hash(m_pool->hasher(), block.m_txinGenHeight, seed, pow_hash)) {
|
||||
LOGWARN(3, "add_external_block: couldn't get PoW hash for height = " << block.m_sidechainHeight << ", mainchain height " << block.m_txinGenHeight << ". Ignoring it.");
|
||||
unsee_block(block);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if it has the correct parent and difficulty to go right to monerod for checking
|
||||
const MinerData& miner_data = m_pool->miner_data();
|
||||
MinerData miner_data = m_pool->miner_data();
|
||||
if ((block.m_prevId == miner_data.prev_id) && miner_data.difficulty.check_pow(pow_hash)) {
|
||||
LOGINFO(0, log::LightGreen() << "add_external_block: block " << block.m_sidechainId << " has enough PoW for Monero network, submitting it");
|
||||
m_pool->submit_block_async(block.m_mainChainData);
|
||||
@@ -450,7 +484,7 @@ bool SideChain::add_external_block(PoolBlock& block, std::vector<hash>& missing_
|
||||
|
||||
missing_blocks.clear();
|
||||
{
|
||||
MutexLock lock(m_sidechainLock);
|
||||
WriteLock lock(m_sidechainLock);
|
||||
if (!block.m_parent.empty() && (m_blocksById.find(block.m_parent) == m_blocksById.end())) {
|
||||
missing_blocks.push_back(block.m_parent);
|
||||
}
|
||||
@@ -466,6 +500,11 @@ bool SideChain::add_external_block(PoolBlock& block, std::vector<hash>& missing_
|
||||
m_watchBlockSidechainId = {};
|
||||
data = m_watchBlock;
|
||||
block_found = true;
|
||||
|
||||
const uint64_t payout = block.get_payout(m_pool->params().m_wallet);
|
||||
if (payout) {
|
||||
LOGINFO(0, log::LightCyan() << "You received a payout of " << log::LightGreen() << log::XMRAmount(payout) << log::LightCyan() << " in block " << log::LightGreen() << data.height);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -485,14 +524,13 @@ void SideChain::add_block(const PoolBlock& block)
|
||||
", verified = " << (block.m_verified ? 1 : 0)
|
||||
);
|
||||
|
||||
// Save it for faster syncing on the next p2pool start
|
||||
if (m_pool->p2p_server()) {
|
||||
m_pool->p2p_server()->store_in_cache(block);
|
||||
PoolBlock* new_block = new PoolBlock(block);
|
||||
{
|
||||
MutexLock lock(m_seenWalletsLock);
|
||||
m_seenWallets[new_block->m_minerWallet.spend_public_key()] = new_block->m_localTimestamp;
|
||||
}
|
||||
|
||||
PoolBlock* new_block = new PoolBlock(block);
|
||||
|
||||
MutexLock lock(m_sidechainLock);
|
||||
WriteLock lock(m_sidechainLock);
|
||||
|
||||
auto result = m_blocksById.insert({ new_block->m_sidechainId, new_block });
|
||||
if (!result.second) {
|
||||
@@ -512,37 +550,51 @@ void SideChain::add_block(const PoolBlock& block)
|
||||
if (new_block->m_verified) {
|
||||
if (!new_block->m_invalid) {
|
||||
update_chain_tip(new_block);
|
||||
|
||||
// Save it for faster syncing on the next p2pool start
|
||||
if (P2PServer* server = p2pServer()) {
|
||||
server->store_in_cache(*new_block);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
verify_loop(new_block);
|
||||
}
|
||||
|
||||
m_seenWallets[new_block->m_minerWallet.spend_public_key()] = new_block->m_localTimestamp;
|
||||
}
|
||||
|
||||
bool SideChain::has_block(const hash& id)
|
||||
PoolBlock* SideChain::find_block(const hash& id) const
|
||||
{
|
||||
MutexLock lock(m_sidechainLock);
|
||||
return m_blocksById.find(id) != m_blocksById.end();
|
||||
ReadLock lock(m_sidechainLock);
|
||||
|
||||
auto it = m_blocksById.find(id);
|
||||
if (it != m_blocksById.end()) {
|
||||
return it->second;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void SideChain::watch_mainchain_block(const ChainMain& data, const hash& possible_id)
|
||||
{
|
||||
MutexLock lock(m_sidechainLock);
|
||||
WriteLock lock(m_sidechainLock);
|
||||
m_watchBlock = data;
|
||||
m_watchBlockSidechainId = possible_id;
|
||||
}
|
||||
|
||||
bool SideChain::get_block_blob(const hash& id, std::vector<uint8_t>& blob)
|
||||
bool SideChain::get_block_blob(const hash& id, std::vector<uint8_t>& blob) const
|
||||
{
|
||||
MutexLock lock(m_sidechainLock);
|
||||
ReadLock lock(m_sidechainLock);
|
||||
|
||||
PoolBlock* block = nullptr;
|
||||
const PoolBlock* block = nullptr;
|
||||
|
||||
// Empty hash means we return current sidechain tip
|
||||
if (id == hash()) {
|
||||
if (id.empty()) {
|
||||
block = m_chainTip;
|
||||
|
||||
// Don't return stale chain tip
|
||||
if (block && (block->m_txinGenHeight + 2 < m_pool->miner_data().height)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
auto it = m_blocksById.find(id);
|
||||
@@ -562,63 +614,94 @@ bool SideChain::get_block_blob(const hash& id, std::vector<uint8_t>& blob)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SideChain::get_outputs_blob(PoolBlock* block, uint64_t total_reward, std::vector<uint8_t>& blob)
|
||||
bool SideChain::get_outputs_blob(PoolBlock* block, uint64_t total_reward, std::vector<uint8_t>& blob) const
|
||||
{
|
||||
std::vector<MinerShare> shares;
|
||||
std::vector<uint64_t> rewards;
|
||||
blob.clear();
|
||||
|
||||
shares.reserve(m_chainWindowSize * 2);
|
||||
rewards.reserve(m_chainWindowSize * 2);
|
||||
ReadLock lock(m_sidechainLock);
|
||||
|
||||
MutexLock lock(m_sidechainLock);
|
||||
auto it = m_blocksById.find(block->m_sidechainId);
|
||||
if (it != m_blocksById.end()) {
|
||||
PoolBlock* b = it->second;
|
||||
const size_t n = b->m_outputs.size();
|
||||
|
||||
if (!get_shares(block, shares) || !split_reward(total_reward, shares, rewards) || (rewards.size() != shares.size())) {
|
||||
blob.reserve(n * 39 + 64);
|
||||
writeVarint(n, blob);
|
||||
|
||||
for (const PoolBlock::TxOutput& output : b->m_outputs) {
|
||||
writeVarint(output.m_reward, blob);
|
||||
blob.emplace_back(output.m_txType);
|
||||
blob.insert(blob.end(), output.m_ephPublicKey.h, output.m_ephPublicKey.h + HASH_SIZE);
|
||||
|
||||
if (output.m_txType == TXOUT_TO_TAGGED_KEY) {
|
||||
blob.emplace_back(output.m_viewTag);
|
||||
}
|
||||
}
|
||||
|
||||
block->m_outputs = b->m_outputs;
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<MinerShare> tmpShares;
|
||||
std::vector<uint64_t> tmpRewards;
|
||||
|
||||
if (!get_shares(block, tmpShares) || !split_reward(total_reward, tmpShares, tmpRewards) || (tmpRewards.size() != tmpShares.size())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t n = shares.size();
|
||||
const size_t n = tmpShares.size();
|
||||
|
||||
blob.clear();
|
||||
blob.reserve(n * 38 + 64);
|
||||
blob.reserve(n * 39 + 64);
|
||||
|
||||
writeVarint(n, blob);
|
||||
|
||||
block->m_outputs.clear();
|
||||
block->m_outputs.reserve(n);
|
||||
|
||||
const uint8_t tx_type = block->get_tx_type();
|
||||
|
||||
hash eph_public_key;
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
writeVarint(rewards[i], blob);
|
||||
writeVarint(tmpRewards[i], blob);
|
||||
|
||||
blob.emplace_back(TXOUT_TO_KEY);
|
||||
blob.emplace_back(tx_type);
|
||||
|
||||
if (!shares[i].m_wallet->get_eph_public_key(block->m_txkeySec, i, eph_public_key)) {
|
||||
uint8_t view_tag;
|
||||
if (!tmpShares[i].m_wallet->get_eph_public_key(block->m_txkeySec, i, eph_public_key, view_tag)) {
|
||||
LOGWARN(6, "get_eph_public_key failed at index " << i);
|
||||
}
|
||||
blob.insert(blob.end(), eph_public_key.h, eph_public_key.h + HASH_SIZE);
|
||||
|
||||
block->m_outputs.emplace_back(rewards[i], eph_public_key);
|
||||
if (tx_type == TXOUT_TO_TAGGED_KEY) {
|
||||
blob.emplace_back(view_tag);
|
||||
}
|
||||
|
||||
block->m_outputs.emplace_back(tmpRewards[i], eph_public_key, tx_type, view_tag);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SideChain::print_status()
|
||||
void SideChain::print_status() const
|
||||
{
|
||||
std::vector<hash> blocks_in_window;
|
||||
blocks_in_window.reserve(m_chainWindowSize * 9 / 8);
|
||||
|
||||
MutexLock lock(m_sidechainLock);
|
||||
const difficulty_type diff = difficulty();
|
||||
|
||||
ReadLock lock(m_sidechainLock);
|
||||
|
||||
uint64_t rem;
|
||||
uint64_t pool_hashrate = udiv128(m_curDifficulty.hi, m_curDifficulty.lo, m_targetBlockTime, &rem);
|
||||
uint64_t pool_hashrate = udiv128(diff.hi, diff.lo, m_targetBlockTime, &rem);
|
||||
|
||||
const difficulty_type& network_diff = m_pool->miner_data().difficulty;
|
||||
difficulty_type network_diff = m_pool->miner_data().difficulty;
|
||||
uint64_t network_hashrate = udiv128(network_diff.hi, network_diff.lo, 120, &rem);
|
||||
|
||||
const PoolBlock* tip = m_chainTip;
|
||||
|
||||
uint64_t block_depth = 0;
|
||||
PoolBlock* cur = m_chainTip;
|
||||
const uint64_t tip_height = m_chainTip ? m_chainTip->m_sidechainHeight : 0;
|
||||
const PoolBlock* cur = tip;
|
||||
const uint64_t tip_height = tip ? tip->m_sidechainHeight : 0;
|
||||
|
||||
uint32_t total_blocks_in_window = 0;
|
||||
uint32_t total_uncles_in_window = 0;
|
||||
@@ -667,10 +750,14 @@ void SideChain::print_status()
|
||||
uint64_t your_reward = 0;
|
||||
uint64_t total_reward = 0;
|
||||
|
||||
if (m_chainTip) {
|
||||
if (tip) {
|
||||
std::sort(blocks_in_window.begin(), blocks_in_window.end());
|
||||
for (uint64_t i = 0; (i < m_chainWindowSize) && (i <= tip_height); ++i) {
|
||||
for (PoolBlock* block : m_blocksByHeight[tip_height - i]) {
|
||||
auto it = m_blocksByHeight.find(tip_height - i);
|
||||
if (it == m_blocksByHeight.end()) {
|
||||
continue;
|
||||
}
|
||||
for (const PoolBlock* block : it->second) {
|
||||
if (!std::binary_search(blocks_in_window.begin(), blocks_in_window.end(), block->m_sidechainId)) {
|
||||
LOGINFO(4, "orphan block at height " << log::Gray() << block->m_sidechainHeight << log::NoColor() << ": " << log::Gray() << block->m_sidechainId);
|
||||
++total_orphans;
|
||||
@@ -681,15 +768,25 @@ void SideChain::print_status()
|
||||
}
|
||||
}
|
||||
|
||||
Wallet w = m_pool->params().m_wallet;
|
||||
const std::vector<PoolBlock::TxOutput>& outs = m_chainTip->m_outputs;
|
||||
const Wallet& w = m_pool->params().m_wallet;
|
||||
|
||||
hash eph_public_key;
|
||||
for (size_t i = 0, n = outs.size(); i < n; ++i) {
|
||||
if (w.get_eph_public_key(m_chainTip->m_txkeySec, i, eph_public_key) && (outs[i].m_ephPublicKey == eph_public_key)) {
|
||||
your_reward = outs[i].m_reward;
|
||||
for (size_t i = 0, n = tip->m_outputs.size(); i < n; ++i) {
|
||||
const PoolBlock::TxOutput& out = tip->m_outputs[i];
|
||||
if (!your_reward) {
|
||||
if (out.m_txType == TXOUT_TO_TAGGED_KEY) {
|
||||
if (w.get_eph_public_key_with_view_tag(tip->m_txkeySec, i, eph_public_key, out.m_viewTag) && (out.m_ephPublicKey == eph_public_key)) {
|
||||
your_reward = out.m_reward;
|
||||
}
|
||||
}
|
||||
else {
|
||||
uint8_t view_tag;
|
||||
if (w.get_eph_public_key(tip->m_txkeySec, i, eph_public_key, view_tag) && (out.m_ephPublicKey == eph_public_key)) {
|
||||
your_reward = out.m_reward;
|
||||
}
|
||||
}
|
||||
}
|
||||
total_reward += outs[i].m_reward;
|
||||
total_reward += out.m_reward;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -698,62 +795,107 @@ void SideChain::print_status()
|
||||
const uint64_t hashrate_est = total_reward ? udiv128(product[1], product[0], total_reward, &rem) : 0;
|
||||
const double block_share = total_reward ? ((static_cast<double>(your_reward) * 100.0) / static_cast<double>(total_reward)) : 0.0;
|
||||
|
||||
uint32_t our_blocks_in_window_total = std::accumulate(our_blocks_in_window.begin(), our_blocks_in_window.end(), decltype(our_blocks_in_window)::value_type(0));
|
||||
uint32_t our_uncles_in_window_total = std::accumulate(our_uncles_in_window.begin(), our_uncles_in_window.end(), decltype(our_uncles_in_window)::value_type(0));
|
||||
const uint32_t our_blocks_in_window_total = std::accumulate(our_blocks_in_window.begin(), our_blocks_in_window.end(), 0U);
|
||||
const uint32_t our_uncles_in_window_total = std::accumulate(our_uncles_in_window.begin(), our_uncles_in_window.end(), 0U);
|
||||
|
||||
std::string our_blocks_in_window_chart;
|
||||
our_blocks_in_window_chart.reserve(our_blocks_in_window.size());
|
||||
for(const auto& p : our_blocks_in_window){
|
||||
our_blocks_in_window_chart += (p > 0 ? (p > 9 ? "+" : std::to_string(p)) : ".");
|
||||
if (our_blocks_in_window_total) {
|
||||
our_blocks_in_window_chart.reserve(our_blocks_in_window.size() + 32);
|
||||
our_blocks_in_window_chart = "\nYour shares position = [";
|
||||
for (uint32_t p : our_blocks_in_window) {
|
||||
our_blocks_in_window_chart += (p ? ((p > 9) ? '+' : static_cast<char>('0' + p)) : '.');
|
||||
}
|
||||
our_blocks_in_window_chart += ']';
|
||||
}
|
||||
|
||||
std::string our_uncles_in_window_chart;
|
||||
our_uncles_in_window_chart.reserve(our_uncles_in_window.size());
|
||||
for(const auto& p : our_uncles_in_window){
|
||||
our_uncles_in_window_chart += (p > 0 ? (p > 9 ? "+" : std::to_string(p)) : ".");
|
||||
if (our_uncles_in_window_total) {
|
||||
our_uncles_in_window_chart.reserve(our_uncles_in_window.size() + 32);
|
||||
our_uncles_in_window_chart = "\nYour uncles position = [";
|
||||
for (uint32_t p : our_uncles_in_window) {
|
||||
our_uncles_in_window_chart += (p ? ((p > 9) ? '+' : static_cast<char>('0' + p)) : '.');
|
||||
}
|
||||
our_uncles_in_window_chart += ']';
|
||||
}
|
||||
|
||||
LOGINFO(0, "status" <<
|
||||
"\nMain chain height = " << m_pool->block_template().height() <<
|
||||
"\nMain chain hashrate = " << log::Hashrate(network_hashrate) <<
|
||||
"\nSide chain ID = " << (is_default() ? "default" : (is_mini() ? "mini" : m_consensusIdDisplayStr.c_str())) <<
|
||||
"\nSide chain height = " << tip_height + 1 <<
|
||||
"\nSide chain hashrate = " << log::Hashrate(pool_hashrate) <<
|
||||
(hashrate_est ? "\nYour hashrate (pool-side) = " : "") << (hashrate_est ? log::Hashrate(hashrate_est) : log::Hashrate()) <<
|
||||
"\nPPLNS window = " << total_blocks_in_window << " blocks (+" << total_uncles_in_window << " uncles, " << total_orphans << " orphans)" <<
|
||||
"\nYour shares = " << our_blocks_in_window_total << " blocks (+" << our_uncles_in_window_total << " uncles, " << our_orphans << " orphans)" <<
|
||||
(our_blocks_in_window_total > 0 ? "\nYour shares position = " : "") << (our_blocks_in_window_total > 0 ? "[" + our_blocks_in_window_chart + "]" : "") <<
|
||||
(our_uncles_in_window_total > 0 ? "\nYour uncles position = " : "") << (our_uncles_in_window_total > 0 ? "[" + our_uncles_in_window_chart + "]" : "") <<
|
||||
"\nYour shares = " << our_blocks_in_window_total << " blocks (+" << our_uncles_in_window_total << " uncles, " << our_orphans << " orphans)"
|
||||
<< our_blocks_in_window_chart << our_uncles_in_window_chart <<
|
||||
"\nBlock reward share = " << block_share << "% (" << log::XMRAmount(your_reward) << ')'
|
||||
);
|
||||
}
|
||||
|
||||
double SideChain::get_reward_share(const Wallet& w) const
|
||||
{
|
||||
uint64_t reward = 0;
|
||||
uint64_t total_reward = 0;
|
||||
{
|
||||
ReadLock lock(m_sidechainLock);
|
||||
|
||||
const PoolBlock* tip = m_chainTip;
|
||||
if (tip) {
|
||||
hash eph_public_key;
|
||||
for (size_t i = 0, n = tip->m_outputs.size(); i < n; ++i) {
|
||||
const PoolBlock::TxOutput& out = tip->m_outputs[i];
|
||||
if (!reward) {
|
||||
if (out.m_txType == TXOUT_TO_TAGGED_KEY) {
|
||||
if (w.get_eph_public_key_with_view_tag(tip->m_txkeySec, i, eph_public_key, out.m_viewTag) && (out.m_ephPublicKey == eph_public_key)) {
|
||||
reward = out.m_reward;
|
||||
}
|
||||
}
|
||||
else {
|
||||
uint8_t view_tag;
|
||||
if (w.get_eph_public_key(tip->m_txkeySec, i, eph_public_key, view_tag) && (out.m_ephPublicKey == eph_public_key)) {
|
||||
reward = out.m_reward;
|
||||
}
|
||||
}
|
||||
}
|
||||
total_reward += out.m_reward;
|
||||
}
|
||||
}
|
||||
}
|
||||
return total_reward ? (static_cast<double>(reward) / static_cast<double>(total_reward)) : 0.0;
|
||||
}
|
||||
|
||||
difficulty_type SideChain::total_hashes() const
|
||||
{
|
||||
return m_chainTip ? m_chainTip->m_cumulativeDifficulty : difficulty_type();
|
||||
const PoolBlock* tip = m_chainTip;
|
||||
return tip ? tip->m_cumulativeDifficulty : difficulty_type();
|
||||
}
|
||||
|
||||
uint64_t SideChain::miner_count()
|
||||
{
|
||||
const time_t cur_time = time(nullptr);
|
||||
const uint64_t cur_time = seconds_since_epoch();
|
||||
|
||||
MutexLock lock(m_sidechainLock);
|
||||
MutexLock lock(m_seenWalletsLock);
|
||||
|
||||
// Delete wallets that weren't seen for more than 24 hours and return how many remain
|
||||
for (auto it = m_seenWallets.begin(); it != m_seenWallets.end();) {
|
||||
if (it->second + 24 * 60 * 60 <= cur_time) {
|
||||
it = m_seenWallets.erase(it);
|
||||
}
|
||||
else {
|
||||
++it;
|
||||
// Every 5 minutes, delete wallets that weren't seen for more than 72 hours
|
||||
if (m_seenWalletsLastPruneTime + 5 * 60 <= cur_time) {
|
||||
for (auto it = m_seenWallets.begin(); it != m_seenWallets.end();) {
|
||||
if (it->second + 72 * 60 * 60 < cur_time) {
|
||||
it = m_seenWallets.erase(it);
|
||||
}
|
||||
else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
m_seenWalletsLastPruneTime = cur_time;
|
||||
}
|
||||
|
||||
return m_seenWallets.size();
|
||||
}
|
||||
|
||||
time_t SideChain::last_updated() const
|
||||
uint64_t SideChain::last_updated() const
|
||||
{
|
||||
return m_chainTip ? m_chainTip->m_localTimestamp : 0;
|
||||
const PoolBlock* tip = m_chainTip;
|
||||
return tip ? tip->m_localTimestamp : 0;
|
||||
}
|
||||
|
||||
bool SideChain::is_default() const
|
||||
@@ -761,6 +903,11 @@ bool SideChain::is_default() const
|
||||
return (memcmp(m_consensusId.data(), default_consensus_id, HASH_SIZE) == 0);
|
||||
}
|
||||
|
||||
bool SideChain::is_mini() const
|
||||
{
|
||||
return (memcmp(m_consensusId.data(), mini_consensus_id, HASH_SIZE) == 0);
|
||||
}
|
||||
|
||||
bool SideChain::split_reward(uint64_t reward, const std::vector<MinerShare>& shares, std::vector<uint64_t>& rewards)
|
||||
{
|
||||
const size_t num_shares = shares.size();
|
||||
@@ -941,7 +1088,8 @@ void SideChain::verify_loop(PoolBlock* block)
|
||||
|
||||
// This block is now verified
|
||||
|
||||
if (is_longer_chain(highest_block, block)) {
|
||||
bool is_alternative;
|
||||
if (is_longer_chain(highest_block, block, is_alternative)) {
|
||||
highest_block = block;
|
||||
}
|
||||
else if (highest_block && (highest_block->m_sidechainHeight > block->m_sidechainHeight)) {
|
||||
@@ -951,17 +1099,19 @@ void SideChain::verify_loop(PoolBlock* block)
|
||||
", height " << block->m_sidechainHeight);
|
||||
}
|
||||
|
||||
P2PServer* server = p2pServer();
|
||||
|
||||
// If it came through a broadcast, send it to our peers
|
||||
if (block->m_wantBroadcast && !block->m_broadcasted) {
|
||||
block->m_broadcasted = true;
|
||||
if (m_pool->p2p_server() && (block->m_depth < UNCLE_BLOCK_DEPTH)) {
|
||||
m_pool->p2p_server()->broadcast(*block);
|
||||
if (server && (block->m_depth < UNCLE_BLOCK_DEPTH)) {
|
||||
server->broadcast(*block);
|
||||
}
|
||||
}
|
||||
|
||||
// Save it for faster syncing on the next p2pool start
|
||||
if (m_pool->p2p_server()) {
|
||||
m_pool->p2p_server()->store_in_cache(*block);
|
||||
if (server) {
|
||||
server->store_in_cache(*block);
|
||||
}
|
||||
|
||||
// Try to verify blocks on top of this one
|
||||
@@ -1229,7 +1379,13 @@ void SideChain::verify(PoolBlock* block)
|
||||
});
|
||||
|
||||
std::vector<uint64_t> rewards;
|
||||
split_reward(total_reward, shares, rewards);
|
||||
if (!split_reward(total_reward, shares, rewards)) {
|
||||
LOGWARN(3, "block at height = " << block->m_sidechainHeight <<
|
||||
", id = " << block->m_sidechainId <<
|
||||
", mainchain height = " << block->m_txinGenHeight << ": split_reward failed");
|
||||
block->m_invalid = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (rewards.size() != block->m_outputs.size()) {
|
||||
LOGWARN(3, "block at height = " << block->m_sidechainHeight <<
|
||||
@@ -1241,17 +1397,20 @@ void SideChain::verify(PoolBlock* block)
|
||||
}
|
||||
|
||||
for (size_t i = 0, n = rewards.size(); i < n; ++i) {
|
||||
if (rewards[i] != block->m_outputs[i].m_reward) {
|
||||
const PoolBlock::TxOutput& out = block->m_outputs[i];
|
||||
|
||||
if (rewards[i] != out.m_reward) {
|
||||
LOGWARN(3, "block at height = " << block->m_sidechainHeight <<
|
||||
", id = " << block->m_sidechainId <<
|
||||
", mainchain height = " << block->m_txinGenHeight <<
|
||||
" has invalid reward at index " << i << ": got " << block->m_outputs[i].m_reward << ", expected " << rewards[i]);
|
||||
" has invalid reward at index " << i << ": got " << out.m_reward << ", expected " << rewards[i]);
|
||||
block->m_invalid = true;
|
||||
return;
|
||||
}
|
||||
|
||||
hash eph_public_key;
|
||||
if (!shares[i].m_wallet->get_eph_public_key(block->m_txkeySec, i, eph_public_key)) {
|
||||
uint8_t view_tag;
|
||||
if (!shares[i].m_wallet->get_eph_public_key(block->m_txkeySec, i, eph_public_key, view_tag)) {
|
||||
LOGWARN(3, "block at height = " << block->m_sidechainHeight <<
|
||||
", id = " << block->m_sidechainId <<
|
||||
", mainchain height = " << block->m_txinGenHeight <<
|
||||
@@ -1260,7 +1419,16 @@ void SideChain::verify(PoolBlock* block)
|
||||
return;
|
||||
}
|
||||
|
||||
if (eph_public_key != block->m_outputs[i].m_ephPublicKey) {
|
||||
if ((out.m_txType == TXOUT_TO_TAGGED_KEY) && (out.m_viewTag != view_tag)) {
|
||||
LOGWARN(3, "block at height = " << block->m_sidechainHeight <<
|
||||
", id = " << block->m_sidechainId <<
|
||||
", mainchain height = " << block->m_txinGenHeight <<
|
||||
" has an incorrect view tag at index " << i);
|
||||
block->m_invalid = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (eph_public_key != out.m_ephPublicKey) {
|
||||
LOGWARN(3, "block at height = " << block->m_sidechainHeight <<
|
||||
", id = " << block->m_sidechainId <<
|
||||
", mainchain height = " << block->m_txinGenHeight <<
|
||||
@@ -1286,34 +1454,49 @@ void SideChain::update_chain_tip(PoolBlock* block)
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_longer_chain(m_chainTip, block)) {
|
||||
const PoolBlock* tip = m_chainTip;
|
||||
|
||||
bool is_alternative;
|
||||
if (is_longer_chain(tip, block, is_alternative)) {
|
||||
difficulty_type diff;
|
||||
if (get_difficulty(block, m_difficultyData, diff)) {
|
||||
m_chainTip = block;
|
||||
m_curDifficulty = diff;
|
||||
{
|
||||
WriteLock lock(m_curDifficultyLock);
|
||||
m_curDifficulty = diff;
|
||||
}
|
||||
|
||||
LOGINFO(2, "new chain tip: next height = " << log::Gray() << block->m_sidechainHeight + 1 << log::NoColor() <<
|
||||
", next difficulty = " << log::Gray() << m_curDifficulty << log::NoColor() <<
|
||||
", main chain height = " << log::Gray() << m_chainTip->m_txinGenHeight);
|
||||
", next difficulty = " << log::Gray() << diff << log::NoColor() <<
|
||||
", main chain height = " << log::Gray() << block->m_txinGenHeight);
|
||||
|
||||
block->m_wantBroadcast = true;
|
||||
m_pool->update_block_template_async();
|
||||
if (m_pool) {
|
||||
m_pool->update_block_template_async();
|
||||
|
||||
// Reset stratum share counters when switching to an alternative chain to avoid confusion
|
||||
StratumServer* s = m_pool->stratum_server();
|
||||
if (s && is_alternative) {
|
||||
s->reset_share_counters();
|
||||
LOGINFO(0, log::LightCyan() << "SYNCHRONIZED");
|
||||
}
|
||||
}
|
||||
prune_old_blocks();
|
||||
}
|
||||
}
|
||||
else if (block->m_sidechainHeight > m_chainTip->m_sidechainHeight) {
|
||||
else if (block->m_sidechainHeight > tip->m_sidechainHeight) {
|
||||
LOGINFO(4, "block " << block->m_sidechainId <<
|
||||
", height = " << block->m_sidechainHeight <<
|
||||
" is not a longer chain than " << m_chainTip->m_sidechainId <<
|
||||
", height " << m_chainTip->m_sidechainHeight);
|
||||
" is not a longer chain than " << tip->m_sidechainId <<
|
||||
", height " << tip->m_sidechainHeight);
|
||||
}
|
||||
else if (block->m_sidechainHeight + UNCLE_BLOCK_DEPTH > m_chainTip->m_sidechainHeight) {
|
||||
else if (block->m_sidechainHeight + UNCLE_BLOCK_DEPTH > tip->m_sidechainHeight) {
|
||||
LOGINFO(4, "possible uncle block: id = " << log::Gray() << block->m_sidechainId << log::NoColor() <<
|
||||
", height = " << log::Gray() << block->m_sidechainHeight);
|
||||
m_pool->update_block_template_async();
|
||||
}
|
||||
|
||||
if (m_pool->p2p_server() && block->m_wantBroadcast && !block->m_broadcasted) {
|
||||
if (p2pServer() && block->m_wantBroadcast && !block->m_broadcasted) {
|
||||
block->m_broadcasted = true;
|
||||
#ifdef DEBUG_BROADCAST_DELAY_MS
|
||||
struct Work
|
||||
@@ -1324,7 +1507,7 @@ void SideChain::update_chain_tip(PoolBlock* block)
|
||||
};
|
||||
Work* work = new Work{};
|
||||
work->req.data = work;
|
||||
work->server = m_pool->p2p_server();
|
||||
work->server = p2pServer();
|
||||
work->block = block;
|
||||
const int err = uv_queue_work(uv_default_loop(), &work->req,
|
||||
[](uv_work_t*)
|
||||
@@ -1343,12 +1526,12 @@ void SideChain::update_chain_tip(PoolBlock* block)
|
||||
LOGERR(1, "update_chain_tip: uv_queue_work failed, error " << uv_err_name(err));
|
||||
}
|
||||
#else
|
||||
m_pool->p2p_server()->broadcast(*block);
|
||||
p2pServer()->broadcast(*block);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
PoolBlock* SideChain::get_parent(const PoolBlock* block)
|
||||
PoolBlock* SideChain::get_parent(const PoolBlock* block) const
|
||||
{
|
||||
if (block) {
|
||||
auto it = m_blocksById.find(block->m_parent);
|
||||
@@ -1359,20 +1542,24 @@ PoolBlock* SideChain::get_parent(const PoolBlock* block)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool SideChain::is_longer_chain(const PoolBlock* block, const PoolBlock* candidate)
|
||||
bool SideChain::is_longer_chain(const PoolBlock* block, const PoolBlock* candidate, bool& is_alternative)
|
||||
{
|
||||
is_alternative = false;
|
||||
|
||||
if (!candidate || !candidate->m_verified || candidate->m_invalid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!block) {
|
||||
// Switching from an empty to a non-empty chain
|
||||
is_alternative = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
// If these two blocks are on the same chain, they must have a common ancestor
|
||||
|
||||
const PoolBlock* block_ancestor = block;
|
||||
while (block_ancestor->m_sidechainHeight > candidate->m_sidechainHeight) {
|
||||
while (block_ancestor && (block_ancestor->m_sidechainHeight > candidate->m_sidechainHeight)) {
|
||||
const hash& id = block_ancestor->m_parent;
|
||||
block_ancestor = get_parent(block_ancestor);
|
||||
if (!block_ancestor) {
|
||||
@@ -1403,6 +1590,8 @@ bool SideChain::is_longer_chain(const PoolBlock* block, const PoolBlock* candida
|
||||
}
|
||||
|
||||
// They're on totally different chains. Compare total difficulties over the last m_chainWindowSize blocks
|
||||
is_alternative = true;
|
||||
|
||||
difficulty_type block_total_diff;
|
||||
difficulty_type candidate_total_diff;
|
||||
|
||||
@@ -1436,8 +1625,9 @@ bool SideChain::is_longer_chain(const PoolBlock* block, const PoolBlock* candida
|
||||
}
|
||||
|
||||
// Final check: candidate chain must be built on top of recent mainchain blocks
|
||||
if (candidate_mainchain_height + 10 < m_pool->miner_data().height) {
|
||||
LOGWARN(3, "received a longer alternative chain but it's stale: height " << candidate_mainchain_height << ", current height " << m_pool->miner_data().height);
|
||||
MinerData data = m_pool->miner_data();
|
||||
if (candidate_mainchain_height + 10 < data.height) {
|
||||
LOGWARN(3, "received a longer alternative chain but it's stale: height " << candidate_mainchain_height << ", current height " << data.height);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1518,13 +1708,16 @@ void SideChain::prune_old_blocks()
|
||||
const uint64_t prune_distance = m_chainWindowSize * 2 + 120 / m_targetBlockTime;
|
||||
|
||||
// Remove old blocks from alternative unconnected chains after long enough time
|
||||
const time_t prune_time = time(nullptr) - m_chainWindowSize * 4 * m_targetBlockTime;
|
||||
const uint64_t cur_time = seconds_since_epoch();
|
||||
const uint64_t prune_delay = m_chainWindowSize * 4 * m_targetBlockTime;
|
||||
|
||||
if (m_chainTip->m_sidechainHeight < prune_distance) {
|
||||
const PoolBlock* tip = m_chainTip;
|
||||
|
||||
if (tip->m_sidechainHeight < prune_distance) {
|
||||
return;
|
||||
}
|
||||
|
||||
const uint64_t h = m_chainTip->m_sidechainHeight - prune_distance;
|
||||
const uint64_t h = tip->m_sidechainHeight - prune_distance;
|
||||
|
||||
uint64_t num_blocks_pruned = 0;
|
||||
|
||||
@@ -1533,12 +1726,13 @@ void SideChain::prune_old_blocks()
|
||||
std::vector<PoolBlock*>& v = it->second;
|
||||
|
||||
v.erase(std::remove_if(v.begin(), v.end(),
|
||||
[this, prune_distance, prune_time, &num_blocks_pruned, height](PoolBlock* block)
|
||||
[this, prune_distance, cur_time, prune_delay, &num_blocks_pruned, height](PoolBlock* block)
|
||||
{
|
||||
if ((block->m_depth >= prune_distance) || (block->m_localTimestamp <= prune_time)) {
|
||||
if ((block->m_depth >= prune_distance) || (cur_time >= block->m_localTimestamp + prune_delay)) {
|
||||
auto it2 = m_blocksById.find(block->m_sidechainId);
|
||||
if (it2 != m_blocksById.end()) {
|
||||
m_blocksById.erase(it2);
|
||||
unsee_block(*block);
|
||||
delete block;
|
||||
++num_blocks_pruned;
|
||||
}
|
||||
@@ -1563,15 +1757,17 @@ void SideChain::prune_old_blocks()
|
||||
|
||||
// If side-chain started pruning blocks it means the initial sync is complete
|
||||
// It's now safe to delete cached blocks
|
||||
m_pool->p2p_server()->clear_cached_blocks();
|
||||
if (p2pServer()) {
|
||||
p2pServer()->clear_cached_blocks();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SideChain::get_missing_blocks(std::vector<hash>& missing_blocks)
|
||||
void SideChain::get_missing_blocks(std::vector<hash>& missing_blocks) const
|
||||
{
|
||||
missing_blocks.clear();
|
||||
|
||||
MutexLock lock(m_sidechainLock);
|
||||
ReadLock lock(m_sidechainLock);
|
||||
|
||||
for (auto& b : m_blocksById) {
|
||||
if (b.second->m_verified) {
|
||||
|
||||
+29
-18
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -19,12 +19,11 @@
|
||||
|
||||
#include "uv_util.h"
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace p2pool {
|
||||
|
||||
class p2pool;
|
||||
class P2PServer;
|
||||
struct DifficultyData;
|
||||
struct PoolBlock;
|
||||
class Wallet;
|
||||
@@ -44,21 +43,22 @@ public:
|
||||
SideChain(p2pool* pool, NetworkType type, const char* pool_name = nullptr);
|
||||
~SideChain();
|
||||
|
||||
void fill_sidechain_data(PoolBlock& block, Wallet* w, const hash& txkeySec, std::vector<MinerShare>& shares);
|
||||
void fill_sidechain_data(PoolBlock& block, const Wallet* w, const hash& txkeySec, std::vector<MinerShare>& shares) const;
|
||||
|
||||
bool block_seen(const PoolBlock& block);
|
||||
void unsee_block(const PoolBlock& block);
|
||||
bool add_external_block(PoolBlock& block, std::vector<hash>& missing_blocks);
|
||||
void add_block(const PoolBlock& block);
|
||||
void get_missing_blocks(std::vector<hash>& missing_blocks);
|
||||
void get_missing_blocks(std::vector<hash>& missing_blocks) const;
|
||||
|
||||
bool has_block(const hash& id);
|
||||
PoolBlock* find_block(const hash& id) const;
|
||||
void watch_mainchain_block(const ChainMain& data, const hash& possible_id);
|
||||
|
||||
bool get_block_blob(const hash& id, std::vector<uint8_t>& blob);
|
||||
bool get_outputs_blob(PoolBlock* block, uint64_t total_reward, std::vector<uint8_t>& blob);
|
||||
bool get_block_blob(const hash& id, std::vector<uint8_t>& blob) const;
|
||||
bool get_outputs_blob(PoolBlock* block, uint64_t total_reward, std::vector<uint8_t>& blob) const;
|
||||
|
||||
void print_status();
|
||||
void print_status() const;
|
||||
double get_reward_share(const Wallet& w) const;
|
||||
|
||||
// Consensus ID can be used to spawn independent P2Pools with their own sidechains
|
||||
// It's never sent over the network to avoid revealing it to the possible man in the middle
|
||||
@@ -66,17 +66,21 @@ public:
|
||||
const std::vector<uint8_t>& consensus_id() const { return m_consensusId; }
|
||||
uint64_t chain_window_size() const { return m_chainWindowSize; }
|
||||
NetworkType network_type() const { return m_networkType; }
|
||||
const difficulty_type& difficulty() const { return m_curDifficulty; }
|
||||
FORCEINLINE difficulty_type difficulty() const { ReadLock lock(m_curDifficultyLock); return m_curDifficulty; }
|
||||
difficulty_type total_hashes() const;
|
||||
uint64_t block_time() const { return m_targetBlockTime; }
|
||||
uint64_t miner_count();
|
||||
time_t last_updated() const;
|
||||
uint64_t last_updated() const;
|
||||
bool is_default() const;
|
||||
bool is_mini() const;
|
||||
|
||||
const PoolBlock* chainTip() const { return m_chainTip; }
|
||||
|
||||
static bool split_reward(uint64_t reward, const std::vector<MinerShare>& shares, std::vector<uint64_t>& rewards);
|
||||
|
||||
private:
|
||||
p2pool* m_pool;
|
||||
P2PServer* p2pServer() const;
|
||||
NetworkType m_networkType;
|
||||
|
||||
private:
|
||||
@@ -85,22 +89,27 @@ private:
|
||||
void verify_loop(PoolBlock* block);
|
||||
void verify(PoolBlock* block);
|
||||
void update_chain_tip(PoolBlock* block);
|
||||
PoolBlock* get_parent(const PoolBlock* block);
|
||||
PoolBlock* get_parent(const PoolBlock* block) const;
|
||||
|
||||
// Checks if "candidate" has longer (higher difficulty) chain than "block"
|
||||
bool is_longer_chain(const PoolBlock* block, const PoolBlock* candidate);
|
||||
bool is_longer_chain(const PoolBlock* block, const PoolBlock* candidate, bool& is_alternative);
|
||||
void update_depths(PoolBlock* block);
|
||||
void prune_old_blocks();
|
||||
|
||||
bool load_config(const std::string& filename);
|
||||
bool check_config();
|
||||
|
||||
mutable uv_mutex_t m_sidechainLock;
|
||||
PoolBlock* m_chainTip;
|
||||
mutable uv_rwlock_t m_sidechainLock;
|
||||
std::atomic<PoolBlock*> m_chainTip;
|
||||
std::map<uint64_t, std::vector<PoolBlock*>> m_blocksByHeight;
|
||||
std::unordered_map<hash, PoolBlock*> m_blocksById;
|
||||
std::unordered_set<hash> m_seenBlocks;
|
||||
std::unordered_map<hash, time_t> m_seenWallets;
|
||||
unordered_map<hash, PoolBlock*> m_blocksById;
|
||||
|
||||
uv_mutex_t m_seenWalletsLock;
|
||||
unordered_map<hash, uint64_t> m_seenWallets;
|
||||
uint64_t m_seenWalletsLastPruneTime;
|
||||
|
||||
uv_mutex_t m_seenBlocksLock;
|
||||
unordered_set<hash> m_seenBlocks;
|
||||
|
||||
std::vector<DifficultyData> m_difficultyData;
|
||||
|
||||
@@ -112,7 +121,9 @@ private:
|
||||
uint64_t m_unclePenalty;
|
||||
|
||||
std::vector<uint8_t> m_consensusId;
|
||||
std::string m_consensusIdDisplayStr;
|
||||
|
||||
mutable uv_rwlock_t m_curDifficultyLock;
|
||||
difficulty_type m_curDifficulty;
|
||||
|
||||
ChainMain m_watchBlock;
|
||||
|
||||
+300
-89
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -27,9 +27,11 @@ static constexpr char log_category_prefix[] = "StratumServer ";
|
||||
|
||||
static constexpr int DEFAULT_BACKLOG = 128;
|
||||
static constexpr uint64_t DEFAULT_BAN_TIME = 600;
|
||||
static constexpr uint64_t MIN_DIFF = 1000;
|
||||
static constexpr uint64_t AUTO_DIFF_TARGET_TIME = 30;
|
||||
|
||||
// Use short target format (4 bytes) for diff <= 4 million
|
||||
static constexpr uint64_t TARGET_4_BYTES_LIMIT = std::numeric_limits<uint64_t>::max() / 4000000;
|
||||
static constexpr uint64_t TARGET_4_BYTES_LIMIT = std::numeric_limits<uint64_t>::max() / 4000000 + 1;
|
||||
|
||||
#include "tcp_server.inl"
|
||||
|
||||
@@ -38,9 +40,9 @@ namespace p2pool {
|
||||
StratumServer::StratumServer(p2pool* pool)
|
||||
: TCPServer(StratumClient::allocate)
|
||||
, m_pool(pool)
|
||||
, m_autoDiff(pool->params().m_autoDiff)
|
||||
, m_extraNonce(0)
|
||||
, m_rd{}
|
||||
, m_rng(m_rd())
|
||||
, m_rng(RandomDeviceSeed::instance)
|
||||
, m_cumulativeHashes(0)
|
||||
, m_cumulativeHashesAtLastShare(0)
|
||||
, m_hashrateDataHead(0)
|
||||
@@ -51,11 +53,13 @@ StratumServer::StratumServer(p2pool* pool)
|
||||
, m_totalFoundShares(0)
|
||||
, m_apiLastUpdateTime(0)
|
||||
{
|
||||
m_hashrateData[0] = { time(nullptr), 0 };
|
||||
// Diffuse the initial state in case it has low quality
|
||||
m_rng.discard(10000);
|
||||
|
||||
m_hashrateData[0] = { seconds_since_epoch(), 0 };
|
||||
|
||||
uv_mutex_init_checked(&m_blobsQueueLock);
|
||||
uv_mutex_init_checked(&m_rngLock);
|
||||
uv_mutex_init_checked(&m_submittedSharesPoolLock);
|
||||
uv_rwlock_init_checked(&m_hashrateDataLock);
|
||||
|
||||
m_submittedSharesPool.resize(10);
|
||||
@@ -66,7 +70,7 @@ StratumServer::StratumServer(p2pool* pool)
|
||||
const int err = uv_async_init(&m_loop, &m_blobsAsync, on_blobs_ready);
|
||||
if (err) {
|
||||
LOGERR(1, "uv_async_init failed, error " << uv_err_name(err));
|
||||
return;
|
||||
panic();
|
||||
}
|
||||
m_blobsAsync.data = this;
|
||||
m_blobsQueue.reserve(2);
|
||||
@@ -82,7 +86,6 @@ StratumServer::~StratumServer()
|
||||
|
||||
uv_mutex_destroy(&m_blobsQueueLock);
|
||||
uv_mutex_destroy(&m_rngLock);
|
||||
uv_mutex_destroy(&m_submittedSharesPoolLock);
|
||||
uv_rwlock_destroy(&m_hashrateDataLock);
|
||||
|
||||
for (SubmittedShare* share : m_submittedSharesPool) {
|
||||
@@ -99,6 +102,7 @@ void StratumServer::on_block(const BlockTemplate& block)
|
||||
LOGINFO(4, "no clients connected");
|
||||
return;
|
||||
}
|
||||
m_extraNonce.exchange(num_connections);
|
||||
|
||||
BlobsData* blobs_data = new BlobsData{};
|
||||
|
||||
@@ -110,9 +114,7 @@ void StratumServer::on_block(const BlockTemplate& block)
|
||||
// Even if they do, they'll be added to the beginning of the list and will get their block template in on_login()
|
||||
// We'll iterate through the list backwards so when we get to the beginning and run out of extra_nonce values, it'll be only new clients left
|
||||
blobs_data->m_numClientsExpected = num_connections;
|
||||
m_extraNonce.exchange(blobs_data->m_numClientsExpected);
|
||||
|
||||
blobs_data->m_blobSize = block.get_hashing_blobs(0, blobs_data->m_numClientsExpected, blobs_data->m_blobs, blobs_data->m_height, difficulty, sidechain_difficulty, blobs_data->m_seedHash, nonce_offset, blobs_data->m_templateId);
|
||||
blobs_data->m_blobSize = block.get_hashing_blobs(0, num_connections, blobs_data->m_blobs, blobs_data->m_height, difficulty, sidechain_difficulty, blobs_data->m_seedHash, nonce_offset, blobs_data->m_templateId);
|
||||
|
||||
// Integrity checks
|
||||
if (blobs_data->m_blobSize < 76) {
|
||||
@@ -176,7 +178,32 @@ void StratumServer::on_block(const BlockTemplate& block)
|
||||
}
|
||||
}
|
||||
|
||||
bool StratumServer::get_custom_diff(const char* s, difficulty_type& diff)
|
||||
template<size_t N>
|
||||
static bool get_custom_user(const char* s, char (&user)[N])
|
||||
{
|
||||
size_t len = 0;
|
||||
|
||||
// Find first of '+' or '.', drop non-printable characters
|
||||
while (s && (len < N - 1)) {
|
||||
const char c = *s;
|
||||
if (!c) {
|
||||
break;
|
||||
}
|
||||
if ((c == '+') || (c == '.')) {
|
||||
break;
|
||||
}
|
||||
// Limit to printable ASCII characters
|
||||
if (c >= ' ' && c <= '~') {
|
||||
user[len++] = c;
|
||||
}
|
||||
++s;
|
||||
}
|
||||
user[len] = '\0';
|
||||
|
||||
return (len > 0);
|
||||
}
|
||||
|
||||
static bool get_custom_diff(const char* s, difficulty_type& diff)
|
||||
{
|
||||
const char* diff_str = nullptr;
|
||||
|
||||
@@ -195,8 +222,8 @@ bool StratumServer::get_custom_diff(const char* s, difficulty_type& diff)
|
||||
if (diff_str) {
|
||||
const uint64_t t = strtoull(diff_str + 1, nullptr, 10);
|
||||
if (t) {
|
||||
// Don't let clients set difficulty less than 1000
|
||||
diff = { std::max<uint64_t>(t + 1, 1000), 0 };
|
||||
// Don't let clients set difficulty less than MIN_DIFF
|
||||
diff = { std::max<uint64_t>(t + 1, MIN_DIFF), 0 };
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -225,13 +252,16 @@ bool StratumServer::on_login(StratumClient* client, uint32_t id, const char* log
|
||||
target = std::max(target, client->m_customDiff.target());
|
||||
}
|
||||
|
||||
if (get_custom_user(login, client->m_customUser)) {
|
||||
const char* s = client->m_customUser;
|
||||
LOGINFO(5, "client " << log::Gray() << static_cast<char*>(client->m_addrString) << " set custom user " << s);
|
||||
}
|
||||
|
||||
uint32_t job_id;
|
||||
{
|
||||
MutexLock lock(client->m_jobsLock);
|
||||
job_id = ++client->m_perConnectionJobId;
|
||||
|
||||
job_id = client->m_perConnectionJobId++;
|
||||
|
||||
StratumClient::SavedJob& saved_job = client->m_jobs[job_id % array_size(&StratumClient::m_jobs)];
|
||||
StratumClient::SavedJob& saved_job = client->m_jobs[job_id % StratumClient::JOBS_SIZE];
|
||||
saved_job.job_id = job_id;
|
||||
saved_job.extra_nonce = extra_nonce;
|
||||
saved_job.template_id = template_id;
|
||||
@@ -239,7 +269,7 @@ bool StratumServer::on_login(StratumClient* client, uint32_t id, const char* log
|
||||
}
|
||||
|
||||
const bool result = send(client,
|
||||
[client, id, &hashing_blob, job_id, blob_size, target, height, &seed_hash](void* buf)
|
||||
[client, id, &hashing_blob, job_id, blob_size, target, height, &seed_hash](void* buf, size_t buf_size)
|
||||
{
|
||||
do {
|
||||
client->m_rpcId = static_cast<uint32_t>(static_cast<StratumServer*>(client->m_owner)->get_random64());
|
||||
@@ -252,7 +282,7 @@ bool StratumServer::on_login(StratumClient* client, uint32_t id, const char* log
|
||||
target_hex.m_size -= sizeof(uint32_t);
|
||||
}
|
||||
|
||||
log::Stream s(reinterpret_cast<char*>(buf));
|
||||
log::Stream s(buf, buf_size);
|
||||
s << "{\"id\":" << id << ",\"jsonrpc\":\"2.0\",\"result\":{\"id\":\"";
|
||||
s << log::Hex(client->m_rpcId) << "\",\"job\":{\"blob\":\"";
|
||||
s << log::hex_buf(hashing_blob, blob_size) << "\",\"job_id\":\"";
|
||||
@@ -279,6 +309,11 @@ bool StratumServer::on_submit(StratumClient* client, uint32_t id, const char* jo
|
||||
job_id = (job_id << 4) + d;
|
||||
}
|
||||
|
||||
if (!job_id) {
|
||||
LOGWARN(4, "client " << static_cast<char*>(client->m_addrString) << " invalid params ('job_id' can't be 0)");
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t nonce = 0;
|
||||
|
||||
for (int i = static_cast<int>(sizeof(uint32_t)) - 1; i >= 0; --i) {
|
||||
@@ -307,9 +342,7 @@ bool StratumServer::on_submit(StratumClient* client, uint32_t id, const char* jo
|
||||
|
||||
bool found = false;
|
||||
{
|
||||
MutexLock lock(client->m_jobsLock);
|
||||
|
||||
const StratumClient::SavedJob& saved_job = client->m_jobs[job_id % array_size(&StratumClient::m_jobs)];
|
||||
const StratumClient::SavedJob& saved_job = client->m_jobs[job_id % StratumClient::JOBS_SIZE];
|
||||
if (saved_job.job_id == job_id) {
|
||||
template_id = saved_job.template_id;
|
||||
extra_nonce = saved_job.extra_nonce;
|
||||
@@ -325,32 +358,33 @@ bool StratumServer::on_submit(StratumClient* client, uint32_t id, const char* jo
|
||||
if (!block.get_difficulties(template_id, mainchain_diff, sidechain_diff)) {
|
||||
LOGWARN(4, "client " << static_cast<char*>(client->m_addrString) << " got a stale share");
|
||||
return send(client,
|
||||
[id](void* buf)
|
||||
[id](void* buf, size_t buf_size)
|
||||
{
|
||||
log::Stream s(reinterpret_cast<char*>(buf));
|
||||
log::Stream s(buf, buf_size);
|
||||
s << "{\"id\":" << id << ",\"jsonrpc\":\"2.0\",\"error\":{\"message\":\"Stale share\"}}\n";
|
||||
return s.m_pos;
|
||||
});
|
||||
}
|
||||
|
||||
if (mainchain_diff.check_pow(resultHash)) {
|
||||
LOGINFO(0, log::Green() << "client " << static_cast<char*>(client->m_addrString) << " found a mainchain block, submitting it");
|
||||
const char* s = client->m_customUser;
|
||||
LOGINFO(0, log::Green() << "client " << static_cast<char*>(client->m_addrString) << (*s ? " user " : "") << s << " found a mainchain block, submitting it");
|
||||
m_pool->submit_block_async(template_id, nonce, extra_nonce);
|
||||
block.update_tx_keys();
|
||||
}
|
||||
|
||||
SubmittedShare* share;
|
||||
|
||||
{
|
||||
MutexLock lock(m_submittedSharesPoolLock);
|
||||
if (!m_submittedSharesPool.empty()) {
|
||||
share = m_submittedSharesPool.back();
|
||||
m_submittedSharesPool.pop_back();
|
||||
}
|
||||
else {
|
||||
share = new SubmittedShare{};
|
||||
}
|
||||
|
||||
if (!m_submittedSharesPool.empty()) {
|
||||
share = m_submittedSharesPool.back();
|
||||
m_submittedSharesPool.pop_back();
|
||||
}
|
||||
else {
|
||||
share = new SubmittedShare{};
|
||||
}
|
||||
if (target >= TARGET_4_BYTES_LIMIT) {
|
||||
target = (target >> 32) << 32;
|
||||
}
|
||||
|
||||
share->m_req.data = share;
|
||||
@@ -366,13 +400,29 @@ bool StratumServer::on_submit(StratumClient* client, uint32_t id, const char* jo
|
||||
share->m_target = target;
|
||||
share->m_resultHash = resultHash;
|
||||
share->m_sidechainDifficulty = sidechain_diff;
|
||||
share->m_timestamp = seconds_since_epoch();
|
||||
|
||||
uint64_t rem;
|
||||
share->m_hashes = (target > 1) ? udiv128(1, 0, target, &rem) : 1;
|
||||
share->m_highEnoughDifficulty = sidechain_diff.check_pow(resultHash);
|
||||
|
||||
update_auto_diff(client, share->m_timestamp, share->m_hashes);
|
||||
|
||||
// If this share is below sidechain difficulty, process it in this thread because it'll be quick
|
||||
if (!share->m_highEnoughDifficulty) {
|
||||
on_share_found(&share->m_req);
|
||||
on_after_share_found(&share->m_req, 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Else switch to a worker thread to check PoW which can take a long time
|
||||
const int err = uv_queue_work(&m_loop, &share->m_req, on_share_found, on_after_share_found);
|
||||
if (err) {
|
||||
LOGERR(1, "uv_queue_work failed, error " << uv_err_name(err));
|
||||
MutexLock lock(m_submittedSharesPoolLock);
|
||||
m_submittedSharesPool.push_back(share);
|
||||
return false;
|
||||
|
||||
// If uv_queue_work failed, process this share here anyway
|
||||
on_share_found(&share->m_req);
|
||||
on_after_share_found(&share->m_req, 0);
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -381,9 +431,9 @@ bool StratumServer::on_submit(StratumClient* client, uint32_t id, const char* jo
|
||||
LOGWARN(4, "client " << static_cast<char*>(client->m_addrString) << " got a share with invalid job id");
|
||||
|
||||
const bool result = send(client,
|
||||
[id](void* buf)
|
||||
[id](void* buf, size_t buf_size)
|
||||
{
|
||||
log::Stream s(reinterpret_cast<char*>(buf));
|
||||
log::Stream s(buf, buf_size);
|
||||
s << "{\"id\":" << id << ",\"jsonrpc\":\"2.0\",\"error\":{\"message\":\"Invalid job id\"}}\n";
|
||||
return s.m_pos;
|
||||
});
|
||||
@@ -399,10 +449,62 @@ uint64_t StratumServer::get_random64()
|
||||
|
||||
void StratumServer::print_status()
|
||||
{
|
||||
update_hashrate_data(0, time(nullptr));
|
||||
update_hashrate_data(0, seconds_since_epoch());
|
||||
print_stratum_status();
|
||||
}
|
||||
|
||||
void StratumServer::show_workers()
|
||||
{
|
||||
const uint64_t cur_time = seconds_since_epoch();
|
||||
const difficulty_type pool_diff = m_pool->side_chain().difficulty();
|
||||
|
||||
MutexLock lock(m_clientsListLock);
|
||||
|
||||
int addr_len = 0;
|
||||
for (const StratumClient* c = static_cast<StratumClient*>(m_connectedClientsList->m_next); c != m_connectedClientsList; c = static_cast<StratumClient*>(c->m_next)) {
|
||||
addr_len = std::max(addr_len, static_cast<int>(strlen(c->m_addrString)));
|
||||
}
|
||||
|
||||
size_t n = 0;
|
||||
|
||||
LOGINFO(0, log::pad_right("IP:port", addr_len + 8)
|
||||
<< log::pad_right("uptime", 20)
|
||||
<< log::pad_right("difficulty", 20)
|
||||
<< log::pad_right("hashrate", 15)
|
||||
<< "name"
|
||||
);
|
||||
|
||||
for (const StratumClient* c = static_cast<StratumClient*>(m_connectedClientsList->m_next); c != m_connectedClientsList; c = static_cast<StratumClient*>(c->m_next)) {
|
||||
difficulty_type diff;
|
||||
if (c->m_customDiff != 0) {
|
||||
diff = c->m_customDiff;
|
||||
}
|
||||
else if (m_autoDiff && (c->m_autoDiff != 0)) {
|
||||
diff = c->m_autoDiff;
|
||||
}
|
||||
else {
|
||||
diff = pool_diff;
|
||||
}
|
||||
LOGINFO(0, log::pad_right(static_cast<const char*>(c->m_addrString), addr_len + 8)
|
||||
<< log::pad_right(log::Duration(cur_time - c->m_connectedTime), 20)
|
||||
<< log::pad_right(diff, 20)
|
||||
<< log::pad_right(log::Hashrate(c->m_autoDiff.lo / AUTO_DIFF_TARGET_TIME, m_autoDiff && (c->m_autoDiff != 0)), 15)
|
||||
<< (c->m_rpcId ? c->m_customUser : "not logged in")
|
||||
);
|
||||
++n;
|
||||
}
|
||||
|
||||
LOGINFO(0, "Total: " << n << " workers");
|
||||
}
|
||||
|
||||
void StratumServer::reset_share_counters()
|
||||
{
|
||||
m_cumulativeHashes = 0;
|
||||
m_cumulativeHashesAtLastShare = 0;
|
||||
m_cumulativeFoundSharesDiff = 0.0;
|
||||
m_totalFoundShares = 0;
|
||||
}
|
||||
|
||||
void StratumServer::print_stratum_status() const
|
||||
{
|
||||
uint64_t hashes_15m, hashes_1h, hashes_24h, total_hashes;
|
||||
@@ -437,8 +539,9 @@ void StratumServer::print_stratum_status() const
|
||||
const uint64_t hashrate_24h = (dt_24h > 0) ? (hashes_24h / dt_24h) : 0;
|
||||
|
||||
double average_effort = 0.0;
|
||||
if (m_cumulativeFoundSharesDiff > 0.0) {
|
||||
average_effort = static_cast<double>(m_cumulativeHashesAtLastShare) * 100.0 / m_cumulativeFoundSharesDiff;
|
||||
const double diff = m_cumulativeFoundSharesDiff;
|
||||
if (diff > 0.0) {
|
||||
average_effort = static_cast<double>(m_cumulativeHashesAtLastShare) * 100.0 / diff;
|
||||
}
|
||||
|
||||
LOGINFO(0, "status" <<
|
||||
@@ -449,10 +552,83 @@ void StratumServer::print_stratum_status() const
|
||||
"\nShares found = " << m_totalFoundShares <<
|
||||
"\nAverage effort = " << average_effort << '%' <<
|
||||
"\nCurrent effort = " << static_cast<double>(hashes_since_last_share) * 100.0 / m_pool->side_chain().difficulty().to_double() << '%' <<
|
||||
"\nConnections = " << m_numConnections << " (" << m_numIncomingConnections << " incoming)"
|
||||
"\nConnections = " << m_numConnections.load() << " (" << m_numIncomingConnections.load() << " incoming)"
|
||||
);
|
||||
}
|
||||
|
||||
// Compresses 64-bit hashes value into 16-bit value (5 bits for shift, 11 bits for data)
|
||||
namespace {
|
||||
|
||||
enum HashValue : uint64_t {
|
||||
bits = 11,
|
||||
mask = (1 << bits) - 1,
|
||||
};
|
||||
|
||||
static constexpr FORCEINLINE uint64_t hash_uncompress(uint64_t h)
|
||||
{
|
||||
return (h & HashValue::mask) << (h >> HashValue::bits);
|
||||
};
|
||||
|
||||
enum HashMaxValue : uint64_t {
|
||||
value = hash_uncompress(std::numeric_limits<uint16_t>::max())
|
||||
};
|
||||
|
||||
static FORCEINLINE uint16_t hash_compress(uint64_t h)
|
||||
{
|
||||
if (h <= HashValue::mask) {
|
||||
return static_cast<uint16_t>(h);
|
||||
}
|
||||
|
||||
if (h >= HashMaxValue::value) {
|
||||
return std::numeric_limits<uint16_t>::max();
|
||||
}
|
||||
|
||||
const uint64_t shift = bsr(h) - (HashValue::bits - 1);
|
||||
return static_cast<uint16_t>((shift << HashValue::bits) | (h >> shift));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void StratumServer::update_auto_diff(StratumClient* client, const uint64_t timestamp, const uint64_t hashes)
|
||||
{
|
||||
const uint16_t hashes_compressed = hash_compress(hashes);
|
||||
client->m_autoDiffWindowHashes += hash_uncompress(hashes_compressed);
|
||||
|
||||
const uint32_t k = client->m_autoDiffIndex++;
|
||||
constexpr uint32_t N = StratumClient::AUTO_DIFF_SIZE;
|
||||
|
||||
StratumClient::AutoDiffData& auto_diff_data = client->m_autoDiffData[k % N];
|
||||
|
||||
if (k >= N) {
|
||||
client->m_autoDiffWindowHashes -= hash_uncompress(auto_diff_data.m_hashes);
|
||||
}
|
||||
|
||||
const uint16_t t1 = auto_diff_data.m_timestamp;
|
||||
const uint16_t t2 = static_cast<uint16_t>(timestamp);
|
||||
auto_diff_data.m_timestamp = t2;
|
||||
auto_diff_data.m_hashes = hashes_compressed;
|
||||
|
||||
if (k >= N) {
|
||||
// Full window
|
||||
const uint64_t dt = t2 - t1;
|
||||
client->m_autoDiff.lo = std::max<uint64_t>((client->m_autoDiffWindowHashes * AUTO_DIFF_TARGET_TIME) / (dt ? dt : 1), MIN_DIFF);
|
||||
client->m_autoDiff.hi = 0;
|
||||
}
|
||||
else if (k >= 10) {
|
||||
// Partial window
|
||||
const uint64_t h0 = hash_uncompress(client->m_autoDiffData[0].m_hashes);
|
||||
const uint64_t dt = client->m_autoDiffData[k].m_timestamp - client->m_autoDiffData[0].m_timestamp;
|
||||
|
||||
client->m_autoDiff.lo = std::max<uint64_t>(((client->m_autoDiffWindowHashes - h0) * AUTO_DIFF_TARGET_TIME) / (dt ? dt : 1), MIN_DIFF);
|
||||
client->m_autoDiff.hi = 0;
|
||||
}
|
||||
else if (k == 0) {
|
||||
// First share, fix auto diff to current difficulty until we have at least 10 shares
|
||||
client->m_autoDiff.lo = hashes;
|
||||
client->m_autoDiff.hi = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void StratumServer::on_blobs_ready()
|
||||
{
|
||||
std::vector<BlobsData*> blobs_queue;
|
||||
@@ -480,6 +656,8 @@ void StratumServer::on_blobs_ready()
|
||||
|
||||
size_t numClientsProcessed = 0;
|
||||
uint32_t extra_nonce = 0;
|
||||
|
||||
const uint64_t cur_time = seconds_since_epoch();
|
||||
{
|
||||
MutexLock lock2(m_clientsListLock);
|
||||
|
||||
@@ -487,7 +665,12 @@ void StratumServer::on_blobs_ready()
|
||||
++numClientsProcessed;
|
||||
|
||||
if (!client->m_rpcId) {
|
||||
// Not logged in yet, on_login() will send the job to this client
|
||||
// Not logged in yet, on_login() will send the job to this client. Also close inactive connections.
|
||||
if (cur_time >= client->m_connectedTime + 10) {
|
||||
LOGWARN(4, "client " << static_cast<char*>(client->m_addrString) << " didn't send login data");
|
||||
client->ban(DEFAULT_BAN_TIME);
|
||||
client->close();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -502,14 +685,32 @@ void StratumServer::on_blobs_ready()
|
||||
if (client->m_customDiff.lo) {
|
||||
target = std::max(target, client->m_customDiff.target());
|
||||
}
|
||||
else if (m_autoDiff) {
|
||||
if (client->m_autoDiff.lo) {
|
||||
const uint32_t k = client->m_autoDiffIndex;
|
||||
const uint16_t elapsed_time = static_cast<uint16_t>(cur_time) - client->m_autoDiffData[(k - 1) % StratumClient::AUTO_DIFF_SIZE].m_timestamp;
|
||||
if (elapsed_time > AUTO_DIFF_TARGET_TIME * 5) {
|
||||
// More than 500% effort, reduce the auto diff by 1/8 every time until the share is found
|
||||
client->m_autoDiff.lo = std::max<uint64_t>(client->m_autoDiff.lo - client->m_autoDiff.lo / 8, MIN_DIFF);
|
||||
}
|
||||
target = std::max(target, client->m_autoDiff.target());
|
||||
}
|
||||
else {
|
||||
// Not enough shares from the client yet, cut diff in half every 16 seconds
|
||||
const uint64_t num_halvings = (cur_time - client->m_connectedTime) / 16;
|
||||
constexpr uint64_t max_target = (std::numeric_limits<uint64_t>::max() / MIN_DIFF) + 1;
|
||||
for (uint64_t i = 0; (i < num_halvings) && (target < max_target); ++i) {
|
||||
target *= 2;
|
||||
}
|
||||
target = std::min<uint64_t>(target, max_target);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t job_id;
|
||||
{
|
||||
MutexLock lock3(client->m_jobsLock);
|
||||
job_id = ++client->m_perConnectionJobId;
|
||||
|
||||
job_id = client->m_perConnectionJobId++;
|
||||
|
||||
StratumClient::SavedJob& saved_job = client->m_jobs[job_id % array_size(&StratumClient::m_jobs)];
|
||||
StratumClient::SavedJob& saved_job = client->m_jobs[job_id % StratumClient::JOBS_SIZE];
|
||||
saved_job.job_id = job_id;
|
||||
saved_job.extra_nonce = extra_nonce;
|
||||
saved_job.template_id = data->m_templateId;
|
||||
@@ -517,7 +718,7 @@ void StratumServer::on_blobs_ready()
|
||||
}
|
||||
|
||||
const bool result = send(client,
|
||||
[data, target, hashing_blob, &job_id](void* buf)
|
||||
[data, target, hashing_blob, &job_id](void* buf, size_t buf_size)
|
||||
{
|
||||
log::hex_buf target_hex(reinterpret_cast<const uint8_t*>(&target), sizeof(uint64_t));
|
||||
|
||||
@@ -526,7 +727,7 @@ void StratumServer::on_blobs_ready()
|
||||
target_hex.m_size -= sizeof(uint32_t);
|
||||
}
|
||||
|
||||
log::Stream s(reinterpret_cast<char*>(buf));
|
||||
log::Stream s(buf, buf_size);
|
||||
s << "{\"jsonrpc\":\"2.0\",\"method\":\"job\",\"params\":{\"blob\":\"";
|
||||
s << log::hex_buf(hashing_blob, data->m_blobSize) << "\",\"job_id\":\"";
|
||||
s << log::Hex(job_id) << "\",\"target\":\"";
|
||||
@@ -544,15 +745,16 @@ void StratumServer::on_blobs_ready()
|
||||
}
|
||||
}
|
||||
|
||||
if (numClientsProcessed != m_numConnections) {
|
||||
LOGWARN(1, "client list is broken, expected " << m_numConnections << ", got " << numClientsProcessed << " clients");
|
||||
const uint32_t num_connections = m_numConnections;
|
||||
if (numClientsProcessed != num_connections) {
|
||||
LOGWARN(1, "client list is broken, expected " << num_connections << ", got " << numClientsProcessed << " clients");
|
||||
}
|
||||
}
|
||||
|
||||
LOGINFO(3, "sent new job to " << extra_nonce << '/' << numClientsProcessed << " clients");
|
||||
}
|
||||
|
||||
void StratumServer::update_hashrate_data(uint64_t hashes, time_t timestamp)
|
||||
void StratumServer::update_hashrate_data(uint64_t hashes, uint64_t timestamp)
|
||||
{
|
||||
constexpr size_t N = array_size(&StratumServer::m_hashrateData);
|
||||
|
||||
@@ -585,26 +787,23 @@ void StratumServer::update_hashrate_data(uint64_t hashes, time_t timestamp)
|
||||
|
||||
void StratumServer::on_share_found(uv_work_t* req)
|
||||
{
|
||||
bkg_jobs_tracker.start("StratumServer::on_share_found");
|
||||
|
||||
SubmittedShare* share = reinterpret_cast<SubmittedShare*>(req->data);
|
||||
if (share->m_highEnoughDifficulty) {
|
||||
bkg_jobs_tracker.start("StratumServer::on_share_found");
|
||||
}
|
||||
|
||||
StratumClient* client = share->m_client;
|
||||
StratumServer* server = share->m_server;
|
||||
p2pool* pool = server->m_pool;
|
||||
|
||||
uint64_t target = share->m_target;
|
||||
if (target >= TARGET_4_BYTES_LIMIT) {
|
||||
target = (target >> 32) << 32;
|
||||
}
|
||||
|
||||
uint64_t rem;
|
||||
const uint64_t hashes = (target > 1) ? udiv128(1, 0, target, &rem) : 0;
|
||||
const uint64_t target = share->m_target;
|
||||
const uint64_t hashes = share->m_hashes;
|
||||
|
||||
if (pool->stopped()) {
|
||||
LOGWARN(0, "p2pool is shutting down, but a share was found. Trying to process it anyway!");
|
||||
}
|
||||
|
||||
if (share->m_sidechainDifficulty.check_pow(share->m_resultHash)) {
|
||||
if (share->m_highEnoughDifficulty) {
|
||||
uint8_t blob[128];
|
||||
uint64_t height;
|
||||
difficulty_type difficulty;
|
||||
@@ -625,7 +824,7 @@ void StratumServer::on_share_found(uv_work_t* req)
|
||||
}
|
||||
|
||||
hash pow_hash;
|
||||
if (!pool->calculate_hash(blob, blob_size, seed_hash, pow_hash)) {
|
||||
if (!pool->calculate_hash(blob, blob_size, height, seed_hash, pow_hash)) {
|
||||
LOGWARN(3, "client " << static_cast<char*>(client->m_addrString) << " couldn't check share PoW");
|
||||
share->m_result = SubmittedShare::Result::COULDNT_CHECK_POW;
|
||||
return;
|
||||
@@ -645,7 +844,8 @@ void StratumServer::on_share_found(uv_work_t* req)
|
||||
server->m_cumulativeFoundSharesDiff += diff;
|
||||
++server->m_totalFoundShares;
|
||||
|
||||
LOGINFO(0, log::Green() << "SHARE FOUND: mainchain height " << height << ", diff " << sidechain_difficulty << ", effort " << effort << '%');
|
||||
const char* s = client->m_customUser;
|
||||
LOGINFO(0, log::Green() << "SHARE FOUND: mainchain height " << height << ", diff " << sidechain_difficulty << ", client " << static_cast<char*>(client->m_addrString) << (*s ? " user " : "") << s << ", effort " << effort << '%');
|
||||
pool->submit_sidechain_block(share->m_templateId, share->m_nonce, share->m_extraNonce);
|
||||
}
|
||||
|
||||
@@ -653,7 +853,7 @@ void StratumServer::on_share_found(uv_work_t* req)
|
||||
const uint64_t value = *reinterpret_cast<uint64_t*>(share->m_resultHash.h + HASH_SIZE - sizeof(uint64_t));
|
||||
|
||||
if (LIKELY(value < target)) {
|
||||
const time_t timestamp = time(nullptr);
|
||||
const uint64_t timestamp = share->m_timestamp;
|
||||
server->update_hashrate_data(hashes, timestamp);
|
||||
server->api_update_local_stats(timestamp);
|
||||
share->m_result = SubmittedShare::Result::OK;
|
||||
@@ -667,16 +867,11 @@ void StratumServer::on_share_found(uv_work_t* req)
|
||||
void StratumServer::on_after_share_found(uv_work_t* req, int /*status*/)
|
||||
{
|
||||
SubmittedShare* share = reinterpret_cast<SubmittedShare*>(req->data);
|
||||
if (share->m_highEnoughDifficulty) {
|
||||
bkg_jobs_tracker.stop("StratumServer::on_share_found");
|
||||
}
|
||||
|
||||
ON_SCOPE_LEAVE(
|
||||
[share]()
|
||||
{
|
||||
{
|
||||
MutexLock lock(share->m_server->m_submittedSharesPoolLock);
|
||||
share->m_server->m_submittedSharesPool.push_back(share);
|
||||
}
|
||||
bkg_jobs_tracker.stop("StratumServer::on_share_found");
|
||||
});
|
||||
ON_SCOPE_LEAVE([share]() { share->m_server->m_submittedSharesPool.push_back(share); });
|
||||
|
||||
StratumClient* client = share->m_client;
|
||||
StratumServer* server = share->m_server;
|
||||
@@ -685,9 +880,9 @@ void StratumServer::on_after_share_found(uv_work_t* req, int /*status*/)
|
||||
|
||||
if ((client->m_resetCounter.load() == share->m_clientResetCounter) && (client->m_rpcId == share->m_rpcId)) {
|
||||
const bool result = server->send(client,
|
||||
[share](void* buf)
|
||||
[share](void* buf, size_t buf_size)
|
||||
{
|
||||
log::Stream s(reinterpret_cast<char*>(buf));
|
||||
log::Stream s(buf, buf_size);
|
||||
switch (share->m_result) {
|
||||
case SubmittedShare::Result::STALE:
|
||||
s << "{\"id\":" << share->m_id << ",\"jsonrpc\":\"2.0\",\"error\":{\"message\":\"Stale share\"}}\n";
|
||||
@@ -723,25 +918,40 @@ void StratumServer::on_after_share_found(uv_work_t* req, int /*status*/)
|
||||
|
||||
StratumServer::StratumClient::StratumClient()
|
||||
: m_rpcId(0)
|
||||
, m_jobs{}
|
||||
, m_perConnectionJobId(0)
|
||||
, m_connectedTime(0)
|
||||
, m_jobs{}
|
||||
, m_autoDiffData{}
|
||||
, m_autoDiffWindowHashes(0)
|
||||
, m_autoDiffIndex(0)
|
||||
, m_customDiff{}
|
||||
, m_autoDiff{}
|
||||
, m_customUser{}
|
||||
{
|
||||
uv_mutex_init_checked(&m_jobsLock);
|
||||
}
|
||||
|
||||
StratumServer::StratumClient::~StratumClient()
|
||||
{
|
||||
uv_mutex_destroy(&m_jobsLock);
|
||||
}
|
||||
|
||||
void StratumServer::StratumClient::reset()
|
||||
{
|
||||
Client::reset();
|
||||
m_rpcId = 0;
|
||||
memset(m_jobs, 0, sizeof(m_jobs));
|
||||
m_perConnectionJobId = 0;
|
||||
m_connectedTime = 0;
|
||||
|
||||
for (int i = 0; i < JOBS_SIZE; ++i) {
|
||||
m_jobs[i].job_id = 0;
|
||||
}
|
||||
|
||||
m_autoDiffWindowHashes = 0;
|
||||
m_autoDiffIndex = 0;
|
||||
m_customDiff = {};
|
||||
m_autoDiff = {};
|
||||
m_customUser[0] = '\0';
|
||||
}
|
||||
|
||||
bool StratumServer::StratumClient::on_connect()
|
||||
{
|
||||
m_connectedTime = seconds_since_epoch();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool StratumServer::StratumClient::on_read(char* data, uint32_t size)
|
||||
@@ -930,7 +1140,7 @@ bool StratumServer::StratumClient::process_submit(rapidjson::Document& doc, uint
|
||||
return static_cast<StratumServer*>(m_owner)->on_submit(this, id, job_id.GetString(), nonce.GetString(), result.GetString());
|
||||
}
|
||||
|
||||
void StratumServer::api_update_local_stats(time_t timestamp)
|
||||
void StratumServer::api_update_local_stats(uint64_t timestamp)
|
||||
{
|
||||
if (!m_pool->api() || !m_pool->params().m_localStats) {
|
||||
return;
|
||||
@@ -975,8 +1185,9 @@ void StratumServer::api_update_local_stats(time_t timestamp)
|
||||
const uint64_t hashrate_24h = (dt_24h > 0) ? (hashes_24h / dt_24h) : 0;
|
||||
|
||||
double average_effort = 0.0;
|
||||
if (m_cumulativeFoundSharesDiff > 0.0) {
|
||||
average_effort = static_cast<double>(m_cumulativeHashesAtLastShare) * 100.0 / m_cumulativeFoundSharesDiff;
|
||||
const double diff = m_cumulativeFoundSharesDiff;
|
||||
if (diff > 0.0) {
|
||||
average_effort = static_cast<double>(m_cumulativeHashesAtLastShare) * 100.0 / diff;
|
||||
}
|
||||
|
||||
int shares_found = m_totalFoundShares;
|
||||
|
||||
+32
-15
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -19,7 +19,6 @@
|
||||
|
||||
#include "tcp_server.h"
|
||||
#include <rapidjson/document.h>
|
||||
#include <random>
|
||||
|
||||
namespace p2pool {
|
||||
|
||||
@@ -40,12 +39,12 @@ public:
|
||||
struct StratumClient : public Client
|
||||
{
|
||||
StratumClient();
|
||||
~StratumClient();
|
||||
FORCEINLINE ~StratumClient() {}
|
||||
|
||||
static Client* allocate() { return new StratumClient(); }
|
||||
|
||||
void reset() override;
|
||||
bool on_connect() override { return true; }
|
||||
bool on_connect() override;
|
||||
bool on_read(char* data, uint32_t size) override;
|
||||
|
||||
bool process_request(char* data, uint32_t size);
|
||||
@@ -53,18 +52,32 @@ public:
|
||||
bool process_submit(rapidjson::Document& doc, uint32_t id);
|
||||
|
||||
uint32_t m_rpcId;
|
||||
uint32_t m_perConnectionJobId;
|
||||
uint64_t m_connectedTime;
|
||||
|
||||
uv_mutex_t m_jobsLock;
|
||||
enum {
|
||||
JOBS_SIZE = 4,
|
||||
AUTO_DIFF_SIZE = 64,
|
||||
};
|
||||
|
||||
struct SavedJob {
|
||||
uint32_t job_id;
|
||||
uint32_t extra_nonce;
|
||||
uint32_t template_id;
|
||||
uint64_t target;
|
||||
} m_jobs[4];
|
||||
} m_jobs[JOBS_SIZE];
|
||||
|
||||
struct AutoDiffData {
|
||||
uint16_t m_timestamp;
|
||||
uint16_t m_hashes;
|
||||
} m_autoDiffData[AUTO_DIFF_SIZE];
|
||||
|
||||
uint64_t m_autoDiffWindowHashes;
|
||||
uint32_t m_autoDiffIndex;
|
||||
|
||||
uint32_t m_perConnectionJobId;
|
||||
difficulty_type m_customDiff;
|
||||
difficulty_type m_autoDiff;
|
||||
char m_customUser[32];
|
||||
};
|
||||
|
||||
bool on_login(StratumClient* client, uint32_t id, const char* login);
|
||||
@@ -72,16 +85,19 @@ public:
|
||||
uint64_t get_random64();
|
||||
|
||||
void print_status() override;
|
||||
void show_workers();
|
||||
|
||||
void reset_share_counters();
|
||||
|
||||
private:
|
||||
void print_stratum_status() const;
|
||||
|
||||
static bool get_custom_diff(const char* s, difficulty_type& diff);
|
||||
void update_auto_diff(StratumClient* client, const uint64_t timestamp, const uint64_t hashes);
|
||||
|
||||
static void on_share_found(uv_work_t* req);
|
||||
static void on_after_share_found(uv_work_t* req, int status);
|
||||
|
||||
p2pool* m_pool;
|
||||
bool m_autoDiff;
|
||||
|
||||
struct BlobsData
|
||||
{
|
||||
@@ -104,7 +120,6 @@ private:
|
||||
std::atomic<uint32_t> m_extraNonce;
|
||||
|
||||
uv_mutex_t m_rngLock;
|
||||
std::random_device m_rd;
|
||||
std::mt19937_64 m_rng;
|
||||
|
||||
struct SubmittedShare
|
||||
@@ -122,6 +137,9 @@ private:
|
||||
uint64_t m_target;
|
||||
hash m_resultHash;
|
||||
difficulty_type m_sidechainDifficulty;
|
||||
uint64_t m_timestamp;
|
||||
uint64_t m_hashes;
|
||||
bool m_highEnoughDifficulty;
|
||||
|
||||
enum class Result {
|
||||
STALE,
|
||||
@@ -132,12 +150,11 @@ private:
|
||||
} m_result;
|
||||
};
|
||||
|
||||
uv_mutex_t m_submittedSharesPoolLock;
|
||||
std::vector<SubmittedShare*> m_submittedSharesPool;
|
||||
|
||||
struct HashrateData
|
||||
{
|
||||
time_t m_timestamp;
|
||||
uint64_t m_timestamp;
|
||||
uint64_t m_cumulativeHashes;
|
||||
};
|
||||
|
||||
@@ -154,10 +171,10 @@ private:
|
||||
double m_cumulativeFoundSharesDiff;
|
||||
uint32_t m_totalFoundShares;
|
||||
|
||||
time_t m_apiLastUpdateTime;
|
||||
uint64_t m_apiLastUpdateTime;
|
||||
|
||||
void update_hashrate_data(uint64_t hashes, time_t timestamp);
|
||||
void api_update_local_stats(time_t timestamp);
|
||||
void update_hashrate_data(uint64_t hashes, uint64_t timestamp);
|
||||
void api_update_local_stats(uint64_t timestamp);
|
||||
};
|
||||
|
||||
} // namespace p2pool
|
||||
|
||||
+26
-60
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -18,8 +18,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "uv_util.h"
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
namespace p2pool {
|
||||
|
||||
@@ -46,45 +44,16 @@ public:
|
||||
|
||||
int listen_port() const { return m_listenPort; }
|
||||
|
||||
struct raw_ip
|
||||
{
|
||||
alignas(8) uint8_t data[16];
|
||||
|
||||
FORCEINLINE bool operator<(const raw_ip& other) const
|
||||
{
|
||||
const uint64_t* a = reinterpret_cast<const uint64_t*>(data);
|
||||
const uint64_t* b = reinterpret_cast<const uint64_t*>(other.data);
|
||||
|
||||
if (a[1] < b[1]) return true;
|
||||
if (a[1] > b[1]) return false;
|
||||
|
||||
return a[0] < b[0];
|
||||
}
|
||||
|
||||
FORCEINLINE bool operator==(const raw_ip& other) const
|
||||
{
|
||||
const uint64_t* a = reinterpret_cast<const uint64_t*>(data);
|
||||
const uint64_t* b = reinterpret_cast<const uint64_t*>(other.data);
|
||||
|
||||
return (a[0] == b[0]) && (a[1] == b[1]);
|
||||
}
|
||||
|
||||
FORCEINLINE bool operator!=(const raw_ip& other) const { return !operator==(other); }
|
||||
};
|
||||
|
||||
static_assert(sizeof(raw_ip) == 16, "struct raw_ip has invalid size");
|
||||
static_assert(sizeof(in6_addr) == 16, "struct in6_addr has invalid size");
|
||||
static_assert(sizeof(in_addr) == 4, "struct in_addr has invalid size");
|
||||
|
||||
bool connect_to_peer(bool is_v6, const raw_ip& ip, int port);
|
||||
virtual void on_connect_failed(bool is_v6, const raw_ip& ip, int port);
|
||||
|
||||
void ban(const raw_ip& ip, uint64_t seconds);
|
||||
virtual void print_bans();
|
||||
|
||||
struct Client
|
||||
{
|
||||
Client();
|
||||
virtual ~Client();
|
||||
virtual ~Client() {}
|
||||
|
||||
virtual void reset();
|
||||
virtual bool on_connect() = 0;
|
||||
@@ -101,6 +70,8 @@ public:
|
||||
|
||||
void init_addr_string(bool is_v6, const sockaddr_storage* peer_addr);
|
||||
|
||||
alignas(8) char m_readBuf[READ_BUF_SIZE];
|
||||
|
||||
TCPServer* m_owner;
|
||||
|
||||
// Used to maintain connected clients list
|
||||
@@ -108,45 +79,40 @@ public:
|
||||
Client* m_next;
|
||||
|
||||
uv_tcp_t m_socket;
|
||||
uv_write_t m_write;
|
||||
uv_connect_t m_connectRequest;
|
||||
|
||||
bool m_isV6;
|
||||
bool m_isIncoming;
|
||||
bool m_readBufInUse;
|
||||
uint32_t m_numRead;
|
||||
|
||||
raw_ip m_addr;
|
||||
int m_port;
|
||||
char m_addrString[64];
|
||||
|
||||
bool m_readBufInUse;
|
||||
char m_readBuf[READ_BUF_SIZE];
|
||||
uint32_t m_numRead;
|
||||
|
||||
struct WriteBuf
|
||||
{
|
||||
Client* m_client;
|
||||
uv_write_t m_write;
|
||||
char m_data[WRITE_BUF_SIZE];
|
||||
};
|
||||
|
||||
uv_mutex_t m_writeBuffersLock;
|
||||
std::vector<WriteBuf*> m_writeBuffers;
|
||||
|
||||
std::atomic<uint32_t> m_resetCounter{ 0 };
|
||||
|
||||
uv_mutex_t m_sendLock;
|
||||
std::atomic<uint32_t> m_resetCounter;
|
||||
};
|
||||
|
||||
struct WriteBuf
|
||||
{
|
||||
Client* m_client = nullptr;
|
||||
uv_write_t m_write = {};
|
||||
std::vector<uint8_t> m_data;
|
||||
};
|
||||
|
||||
uv_mutex_t m_writeBuffersLock;
|
||||
std::vector<WriteBuf*> m_writeBuffers;
|
||||
|
||||
struct SendCallbackBase
|
||||
{
|
||||
virtual ~SendCallbackBase() {}
|
||||
virtual size_t operator()(void*) = 0;
|
||||
virtual size_t operator()(void*, size_t) = 0;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct SendCallback : public SendCallbackBase
|
||||
{
|
||||
explicit FORCEINLINE SendCallback(T&& callback) : m_callback(std::move(callback)) {}
|
||||
size_t operator()(void* buf) override { return m_callback(buf); }
|
||||
size_t operator()(void* buf, size_t buf_size) override { return m_callback(buf, buf_size); }
|
||||
|
||||
private:
|
||||
SendCallback& operator=(SendCallback&&) = delete;
|
||||
@@ -184,21 +150,21 @@ protected:
|
||||
int m_listenPort;
|
||||
|
||||
uv_loop_t m_loop;
|
||||
volatile bool m_loopStopped;
|
||||
std::atomic<bool> m_loopStopped;
|
||||
|
||||
uv_mutex_t m_clientsListLock;
|
||||
std::vector<Client*> m_preallocatedClients;
|
||||
Client* m_connectedClientsList;
|
||||
uint32_t m_numConnections;
|
||||
uint32_t m_numIncomingConnections;
|
||||
std::atomic<uint32_t> m_numConnections;
|
||||
std::atomic<uint32_t> m_numIncomingConnections;
|
||||
|
||||
uv_mutex_t m_bansLock;
|
||||
std::map<raw_ip, time_t> m_bans;
|
||||
unordered_map<raw_ip, std::chrono::steady_clock::time_point> m_bans;
|
||||
|
||||
bool is_banned(const raw_ip& ip);
|
||||
|
||||
uv_mutex_t m_pendingConnectionsLock;
|
||||
std::set<raw_ip> m_pendingConnections;
|
||||
unordered_set<raw_ip> m_pendingConnections;
|
||||
|
||||
uv_async_t m_dropConnectionsAsync;
|
||||
static void on_drop_connections(uv_async_t* async) { reinterpret_cast<TCPServer*>(async->data)->close_sockets(false); }
|
||||
|
||||
+123
-64
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -27,9 +27,9 @@ TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::TCPServer(allocate_client_callback all
|
||||
, m_loopThread{}
|
||||
, m_finished(0)
|
||||
, m_listenPort(-1)
|
||||
, m_loopStopped(false)
|
||||
, m_numConnections(0)
|
||||
, m_numIncomingConnections(0)
|
||||
, m_loopStopped{false}
|
||||
, m_numConnections{ 0 }
|
||||
, m_numIncomingConnections{ 0 }
|
||||
{
|
||||
int err = uv_loop_init(&m_loop);
|
||||
if (err) {
|
||||
@@ -37,15 +37,29 @@ TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::TCPServer(allocate_client_callback all
|
||||
panic();
|
||||
}
|
||||
|
||||
uv_async_init(&m_loop, &m_dropConnectionsAsync, on_drop_connections);
|
||||
err = uv_async_init(&m_loop, &m_dropConnectionsAsync, on_drop_connections);
|
||||
if (err) {
|
||||
LOGERR(1, "uv_async_init failed, error " << uv_err_name(err));
|
||||
panic();
|
||||
}
|
||||
m_dropConnectionsAsync.data = this;
|
||||
|
||||
uv_async_init(&m_loop, &m_shutdownAsync, on_shutdown);
|
||||
err = uv_async_init(&m_loop, &m_shutdownAsync, on_shutdown);
|
||||
if (err) {
|
||||
LOGERR(1, "uv_async_init failed, error " << uv_err_name(err));
|
||||
panic();
|
||||
}
|
||||
m_shutdownAsync.data = this;
|
||||
|
||||
uv_mutex_init_checked(&m_clientsListLock);
|
||||
uv_mutex_init_checked(&m_bansLock);
|
||||
uv_mutex_init_checked(&m_pendingConnectionsLock);
|
||||
uv_mutex_init_checked(&m_writeBuffersLock);
|
||||
|
||||
m_writeBuffers.resize(DEFAULT_BACKLOG);
|
||||
for (size_t i = 0; i < m_writeBuffers.size(); ++i) {
|
||||
m_writeBuffers[i] = new WriteBuf();
|
||||
}
|
||||
|
||||
m_preallocatedClients.reserve(DEFAULT_BACKLOG);
|
||||
for (int i = 0; i < DEFAULT_BACKLOG; ++i) {
|
||||
@@ -58,6 +72,7 @@ TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::TCPServer(allocate_client_callback all
|
||||
}
|
||||
|
||||
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
|
||||
// cppcheck-suppress functionStatic
|
||||
TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::~TCPServer()
|
||||
{
|
||||
if (m_finished.load() == 0) {
|
||||
@@ -308,7 +323,6 @@ bool TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::connect_to_peer(bool is_v6, const
|
||||
return connect_to_peer_nolock(client, is_v6, reinterpret_cast<sockaddr*>(&addr));
|
||||
}
|
||||
|
||||
|
||||
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
|
||||
void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::on_connect_failed(bool, const raw_ip&, int)
|
||||
{
|
||||
@@ -317,11 +331,21 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::on_connect_failed(bool, const raw
|
||||
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
|
||||
bool TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::is_banned(const raw_ip& ip)
|
||||
{
|
||||
if (ip.is_localhost()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto cur_time = std::chrono::steady_clock::now();
|
||||
|
||||
MutexLock lock(m_bansLock);
|
||||
|
||||
auto it = m_bans.find(ip);
|
||||
if ((it != m_bans.end()) && (time(nullptr) < it->second)) {
|
||||
return true;
|
||||
if (it != m_bans.end()) {
|
||||
const bool banned = (cur_time < it->second);
|
||||
if (!banned) {
|
||||
m_bans.erase(it);
|
||||
}
|
||||
return banned;
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -361,8 +385,11 @@ bool TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::connect_to_peer_nolock(Client* cl
|
||||
return false;
|
||||
}
|
||||
|
||||
client->m_connectRequest.data = client;
|
||||
err = uv_tcp_connect(&client->m_connectRequest, &client->m_socket, addr, on_connect);
|
||||
uv_connect_t* connect_request = reinterpret_cast<uv_connect_t*>(client->m_readBuf);
|
||||
memset(connect_request, 0, sizeof(uv_connect_t));
|
||||
|
||||
connect_request->data = client;
|
||||
err = uv_tcp_connect(connect_request, &client->m_socket, addr, on_connect);
|
||||
if (err) {
|
||||
LOGERR(1, "failed to initiate tcp connection, error " << uv_err_name(err));
|
||||
m_pendingConnections.erase(client->m_addr);
|
||||
@@ -426,14 +453,14 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::shutdown_tcp()
|
||||
|
||||
using namespace std::chrono;
|
||||
|
||||
const system_clock::time_point start_time = system_clock::now();
|
||||
const auto start_time = steady_clock::now();
|
||||
int64_t counter = 0;
|
||||
uv_async_t asy;
|
||||
|
||||
constexpr uint32_t timeout_seconds = 30;
|
||||
|
||||
while (!m_loopStopped) {
|
||||
const int64_t elapsed_time = duration_cast<milliseconds>(system_clock::now() - start_time).count();
|
||||
const int64_t elapsed_time = duration_cast<milliseconds>(steady_clock::now() - start_time).count();
|
||||
|
||||
if (elapsed_time >= (counter + 1) * 1000) {
|
||||
++counter;
|
||||
@@ -462,6 +489,14 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::shutdown_tcp()
|
||||
uv_mutex_destroy(&m_bansLock);
|
||||
uv_mutex_destroy(&m_pendingConnectionsLock);
|
||||
|
||||
{
|
||||
MutexLock lock(m_writeBuffersLock);
|
||||
for (WriteBuf* buf : m_writeBuffers) {
|
||||
delete buf;
|
||||
}
|
||||
}
|
||||
uv_mutex_destroy(&m_writeBuffersLock);
|
||||
|
||||
LOGINFO(1, "stopped");
|
||||
}
|
||||
|
||||
@@ -469,15 +504,45 @@ template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
|
||||
void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::print_status()
|
||||
{
|
||||
LOGINFO(0, "status" <<
|
||||
"\nConnections = " << m_numConnections << " (" << m_numIncomingConnections << " incoming)"
|
||||
"\nConnections = " << m_numConnections.load() << " (" << m_numIncomingConnections.load() << " incoming)"
|
||||
);
|
||||
}
|
||||
|
||||
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
|
||||
void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::ban(const raw_ip& ip, uint64_t seconds)
|
||||
{
|
||||
if (ip.is_localhost()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto ban_time = std::chrono::steady_clock::now() + std::chrono::seconds(seconds);
|
||||
|
||||
MutexLock lock(m_bansLock);
|
||||
m_bans[ip] = time(nullptr) + seconds;
|
||||
m_bans[ip] = ban_time;
|
||||
}
|
||||
|
||||
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
|
||||
void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::print_bans()
|
||||
{
|
||||
using namespace std::chrono;
|
||||
const auto cur_time = steady_clock::now();
|
||||
|
||||
std::vector<std::pair<raw_ip, std::chrono::steady_clock::time_point>> bans;
|
||||
{
|
||||
MutexLock lock(m_bansLock);
|
||||
|
||||
bans.reserve(m_bans.size());
|
||||
for (const auto& b : m_bans) {
|
||||
bans.emplace_back(std::make_pair(b.first, b.second));
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& b : bans) {
|
||||
if (cur_time < b.second) {
|
||||
const uint64_t t = duration_cast<seconds>(b.second - cur_time).count();
|
||||
LOGINFO(0, b.first << " is banned (" << t << " seconds left)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
|
||||
@@ -487,50 +552,52 @@ bool TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::send_internal(Client* client, Sen
|
||||
LOGERR(1, "sending data from another thread, this is not thread safe");
|
||||
}
|
||||
|
||||
MutexLock lock0(client->m_sendLock);
|
||||
|
||||
typename Client::WriteBuf* buf = nullptr;
|
||||
WriteBuf* buf = nullptr;
|
||||
|
||||
{
|
||||
MutexLock lock(client->m_writeBuffersLock);
|
||||
if (!client->m_writeBuffers.empty()) {
|
||||
buf = client->m_writeBuffers.back();
|
||||
client->m_writeBuffers.pop_back();
|
||||
MutexLock lock(m_writeBuffersLock);
|
||||
if (!m_writeBuffers.empty()) {
|
||||
buf = m_writeBuffers.back();
|
||||
m_writeBuffers.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
if (!buf) {
|
||||
buf = new typename Client::WriteBuf();
|
||||
buf = new WriteBuf();
|
||||
}
|
||||
|
||||
const size_t bytes_written = callback(buf->m_data);
|
||||
// callback_buf is used in only 1 thread, so it's safe
|
||||
static uint8_t callback_buf[WRITE_BUF_SIZE];
|
||||
const size_t bytes_written = callback(callback_buf, sizeof(callback_buf));
|
||||
|
||||
if (bytes_written > sizeof(buf->m_data)) {
|
||||
LOGERR(0, "send callback wrote " << bytes_written << " bytes, expected no more than " << sizeof(buf->m_data) << " bytes");
|
||||
if (bytes_written > WRITE_BUF_SIZE) {
|
||||
LOGERR(0, "send callback wrote " << bytes_written << " bytes, expected no more than " << WRITE_BUF_SIZE << " bytes");
|
||||
panic();
|
||||
}
|
||||
|
||||
if (bytes_written == 0) {
|
||||
LOGWARN(1, "send callback wrote 0 bytes, nothing to do");
|
||||
{
|
||||
MutexLock lock(client->m_writeBuffersLock);
|
||||
client->m_writeBuffers.push_back(buf);
|
||||
MutexLock lock(m_writeBuffersLock);
|
||||
m_writeBuffers.push_back(buf);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
buf->m_client = client;
|
||||
buf->m_write.data = buf;
|
||||
buf->m_data.reserve(round_up(bytes_written, 64));
|
||||
buf->m_data.assign(callback_buf, callback_buf + bytes_written);
|
||||
|
||||
uv_buf_t bufs[1];
|
||||
bufs[0].base = buf->m_data;
|
||||
bufs[0].base = reinterpret_cast<char*>(buf->m_data.data());
|
||||
bufs[0].len = static_cast<int>(bytes_written);
|
||||
|
||||
const int err = uv_write(&buf->m_write, reinterpret_cast<uv_stream_t*>(&client->m_socket), bufs, 1, Client::on_write);
|
||||
if (err) {
|
||||
{
|
||||
MutexLock lock(client->m_writeBuffersLock);
|
||||
client->m_writeBuffers.push_back(buf);
|
||||
MutexLock lock(m_writeBuffersLock);
|
||||
m_writeBuffers.push_back(buf);
|
||||
}
|
||||
LOGWARN(1, "failed to start writing data to client connection " << static_cast<const char*>(client->m_addrString) << ", error " << uv_err_name(err));
|
||||
return false;
|
||||
@@ -547,6 +614,7 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::loop(void* data)
|
||||
TCPServer* server = static_cast<TCPServer*>(data);
|
||||
uv_run(&server->m_loop, UV_RUN_DEFAULT);
|
||||
uv_loop_close(&server->m_loop);
|
||||
LOGINFO(1, "event loop stopped");
|
||||
server->m_loopStopped = true;
|
||||
}
|
||||
|
||||
@@ -570,9 +638,11 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::on_new_connection(uv_stream_t* se
|
||||
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
|
||||
void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::on_connection_close(uv_handle_t* handle)
|
||||
{
|
||||
Client* client = static_cast<Client*>(handle->data);
|
||||
MutexLock lock0(client->m_sendLock);
|
||||
if (!server_event_loop_thread) {
|
||||
LOGERR(1, "on_connection_close called from another thread, this is not thread safe");
|
||||
}
|
||||
|
||||
Client* client = static_cast<Client*>(handle->data);
|
||||
TCPServer* owner = client->m_owner;
|
||||
|
||||
LOGINFO(5, "peer " << log::Gray() << static_cast<char*>(client->m_addrString) << log::NoColor() << " disconnected");
|
||||
@@ -758,31 +828,21 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::on_new_client_nolock(uv_stream_t*
|
||||
|
||||
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
|
||||
TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::Client::Client()
|
||||
: m_owner(nullptr)
|
||||
, m_prev(nullptr)
|
||||
, m_next(nullptr)
|
||||
, m_socket{}
|
||||
, m_isV6(false)
|
||||
, m_isIncoming(false)
|
||||
, m_readBufInUse(false)
|
||||
, m_numRead(0)
|
||||
, m_addr{}
|
||||
, m_port(0)
|
||||
, m_addrString{}
|
||||
, m_resetCounter{ 0 }
|
||||
{
|
||||
Client::reset();
|
||||
|
||||
uv_mutex_init_checked(&m_writeBuffersLock);
|
||||
uv_mutex_init_checked(&m_sendLock);
|
||||
|
||||
m_readBuf[0] = '\0';
|
||||
|
||||
m_writeBuffers.resize(2);
|
||||
for (size_t i = 0; i < m_writeBuffers.size(); ++i) {
|
||||
m_writeBuffers[i] = new WriteBuf();
|
||||
}
|
||||
}
|
||||
|
||||
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
|
||||
TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::Client::~Client()
|
||||
{
|
||||
{
|
||||
MutexLock lock(m_writeBuffersLock);
|
||||
for (WriteBuf* buf : m_writeBuffers) {
|
||||
delete buf;
|
||||
}
|
||||
}
|
||||
uv_mutex_destroy(&m_writeBuffersLock);
|
||||
uv_mutex_destroy(&m_sendLock);
|
||||
m_readBuf[READ_BUF_SIZE - 1] = '\0';
|
||||
}
|
||||
|
||||
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
|
||||
@@ -794,15 +854,13 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::Client::reset()
|
||||
m_prev = nullptr;
|
||||
m_next = nullptr;
|
||||
memset(&m_socket, 0, sizeof(m_socket));
|
||||
memset(&m_write, 0, sizeof(m_write));
|
||||
memset(&m_connectRequest, 0, sizeof(m_connectRequest));
|
||||
m_isV6 = false;
|
||||
m_isIncoming = false;
|
||||
m_readBufInUse = false;
|
||||
m_numRead = 0;
|
||||
m_addr = {};
|
||||
m_port = -1;
|
||||
m_addrString[0] = '\0';
|
||||
m_readBufInUse = false;
|
||||
m_numRead = 0;
|
||||
}
|
||||
|
||||
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
|
||||
@@ -858,12 +916,13 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::Client::on_read(uv_stream_t* stre
|
||||
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
|
||||
void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::Client::on_write(uv_write_t* req, int status)
|
||||
{
|
||||
Client::WriteBuf* buf = static_cast<Client::WriteBuf*>(req->data);
|
||||
WriteBuf* buf = static_cast<WriteBuf*>(req->data);
|
||||
Client* client = buf->m_client;
|
||||
TCPServer* server = client->m_owner;
|
||||
|
||||
{
|
||||
MutexLock lock(client->m_writeBuffersLock);
|
||||
client->m_writeBuffers.push_back(buf);
|
||||
if (server) {
|
||||
MutexLock lock(server->m_writeBuffersLock);
|
||||
server->m_writeBuffers.push_back(buf);
|
||||
}
|
||||
|
||||
if (status != 0) {
|
||||
|
||||
+50
-6
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -32,7 +32,7 @@ namespace p2pool {
|
||||
#define STR2(X) STR(X)
|
||||
#define STR(X) #X
|
||||
|
||||
const char* VERSION = "v1.2 (built"
|
||||
const char* VERSION = "v2.1 (built"
|
||||
#if defined(__clang__)
|
||||
" with clang/" __clang_version__
|
||||
#elif defined(__GNUC__)
|
||||
@@ -66,7 +66,7 @@ void make_thread_background()
|
||||
SetThreadPriorityBoost(GetCurrentThread(), true);
|
||||
SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_BEGIN);
|
||||
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE);
|
||||
#elif !defined(__APPLE__)
|
||||
#elif !defined(__APPLE__) && !defined(__FreeBSD__)
|
||||
sched_param param;
|
||||
param.sched_priority = 0;
|
||||
if (sched_setscheduler(0, SCHED_IDLE, ¶m) != 0) {
|
||||
@@ -225,7 +225,7 @@ void uv_rwlock_init_checked(uv_rwlock_t* lock)
|
||||
|
||||
uv_loop_t* uv_default_loop_checked()
|
||||
{
|
||||
if (!is_main_thread) {
|
||||
if (!is_main_thread()) {
|
||||
LOGERR(1, "uv_default_loop() can only be used by the main thread. Fix the code!");
|
||||
#ifdef _WIN32
|
||||
if (IsDebuggerPresent()) {
|
||||
@@ -339,14 +339,17 @@ void BackgroundJobTracker::print_status()
|
||||
}
|
||||
|
||||
BackgroundJobTracker bkg_jobs_tracker;
|
||||
thread_local bool is_main_thread = false;
|
||||
|
||||
static thread_local bool main_thread = false;
|
||||
void set_main_thread() { main_thread = true; }
|
||||
bool is_main_thread() { return main_thread; }
|
||||
|
||||
bool resolve_host(std::string& host, bool& is_v6)
|
||||
{
|
||||
addrinfo hints{};
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG;
|
||||
hints.ai_flags = AI_ADDRCONFIG;
|
||||
|
||||
addrinfo* r = nullptr;
|
||||
const int err = getaddrinfo(host.c_str(), nullptr, &hints, &r);
|
||||
@@ -379,4 +382,45 @@ bool resolve_host(std::string& host, bool& is_v6)
|
||||
return true;
|
||||
}
|
||||
|
||||
RandomDeviceSeed RandomDeviceSeed::instance;
|
||||
|
||||
struct BSR8
|
||||
{
|
||||
uint8_t data[256];
|
||||
|
||||
static constexpr BSR8 init() {
|
||||
BSR8 result = { 55 };
|
||||
|
||||
for (int i = 1; i < 256; ++i) {
|
||||
int x = i;
|
||||
result.data[i] = 63;
|
||||
while (x < 0x80) {
|
||||
--result.data[i];
|
||||
x <<= 1;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
static constexpr BSR8 bsr8_table = BSR8::init();
|
||||
|
||||
NOINLINE uint64_t bsr_reference(uint64_t x)
|
||||
{
|
||||
uint32_t y = static_cast<uint32_t>(x);
|
||||
|
||||
uint64_t n0 = (x == y) ? 0 : 32;
|
||||
y = static_cast<uint32_t>(x >> n0);
|
||||
n0 ^= 32;
|
||||
|
||||
const uint64_t n1 = (y & 0xFFFF0000UL) ? 0 : 16;
|
||||
y <<= n1;
|
||||
|
||||
const uint64_t n2 = (y & 0xFF000000UL) ? 0 : 8;
|
||||
y <<= n2;
|
||||
|
||||
return bsr8_table.data[y >> 24] - n0 - n1 - n2;
|
||||
}
|
||||
|
||||
} // namespace p2pool
|
||||
|
||||
+122
-14
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -17,6 +17,21 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4623 5026 5027)
|
||||
#endif
|
||||
|
||||
#define ROBIN_HOOD_MALLOC(size) p2pool::malloc_hook(size)
|
||||
#define ROBIN_HOOD_CALLOC(count, size) p2pool::calloc_hook((count), (size))
|
||||
#define ROBIN_HOOD_FREE(ptr) p2pool::free_hook(ptr)
|
||||
|
||||
#include "robin_hood.h"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
namespace p2pool {
|
||||
|
||||
extern const char* VERSION;
|
||||
@@ -94,6 +109,40 @@ FORCEINLINE void writeVarint(T value, std::vector<uint8_t>& out)
|
||||
writeVarint(value, [&out](uint8_t b) { out.emplace_back(b); });
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
const uint8_t* readVarint(const uint8_t* data, const uint8_t* data_end, T& b)
|
||||
{
|
||||
uint64_t result = 0;
|
||||
int k = 0;
|
||||
|
||||
while (data < data_end) {
|
||||
if (k >= static_cast<int>(sizeof(T)) * 8) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const uint64_t cur_byte = *(data++);
|
||||
result |= (cur_byte & 0x7F) << k;
|
||||
k += 7;
|
||||
|
||||
if ((cur_byte & 0x80) == 0) {
|
||||
b = result;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
FORCEINLINE T read_unaligned(const T* p)
|
||||
{
|
||||
static_assert(std::is_integral<T>::value, "T must be an integer type");
|
||||
|
||||
T result;
|
||||
memcpy(&result, p, sizeof(T));
|
||||
return result;
|
||||
}
|
||||
|
||||
template<typename T, size_t N> FORCEINLINE constexpr size_t array_size(T(&)[N]) { return N; }
|
||||
template<typename T, typename U, size_t N> FORCEINLINE constexpr size_t array_size(T(U::*)[N]) { return N; }
|
||||
|
||||
@@ -118,24 +167,68 @@ private:
|
||||
};
|
||||
|
||||
extern BackgroundJobTracker bkg_jobs_tracker;
|
||||
extern thread_local bool is_main_thread;
|
||||
|
||||
void set_main_thread();
|
||||
bool is_main_thread();
|
||||
|
||||
bool resolve_host(std::string& host, bool& is_v6);
|
||||
|
||||
template <typename Key, typename T>
|
||||
using unordered_map = robin_hood::detail::Table<false, 80, Key, T, robin_hood::hash<Key>, std::equal_to<Key>>;
|
||||
|
||||
template <typename Key>
|
||||
using unordered_set = robin_hood::detail::Table<false, 80, Key, void, robin_hood::hash<Key>, std::equal_to<Key>>;
|
||||
|
||||
// Fills the whole initial MT19937-64 state with non-deterministic random numbers
|
||||
struct RandomDeviceSeed
|
||||
{
|
||||
using result_type = std::random_device::result_type;
|
||||
static_assert(sizeof(result_type) >= 4, "result_type must have at least 32 bits");
|
||||
|
||||
template<typename T>
|
||||
static void generate(T begin, T end)
|
||||
{
|
||||
std::random_device rd;
|
||||
for (T i = begin; i != end; ++i) {
|
||||
*i = rd();
|
||||
}
|
||||
}
|
||||
|
||||
static RandomDeviceSeed instance;
|
||||
};
|
||||
|
||||
FORCEINLINE uint64_t seconds_since_epoch()
|
||||
{
|
||||
using namespace std::chrono;
|
||||
return duration_cast<seconds>(steady_clock::now().time_since_epoch()).count();
|
||||
}
|
||||
|
||||
uint64_t bsr_reference(uint64_t x);
|
||||
|
||||
#ifdef HAVE_BUILTIN_CLZLL
|
||||
#define bsr(x) (63 - __builtin_clzll(x))
|
||||
#elif defined HAVE_BITSCANREVERSE64
|
||||
#pragma intrinsic(_BitScanReverse64)
|
||||
FORCEINLINE uint64_t bsr(uint64_t x)
|
||||
{
|
||||
unsigned long index;
|
||||
_BitScanReverse64(&index, x);
|
||||
return index;
|
||||
}
|
||||
#else
|
||||
#define bsr bsr_reference
|
||||
#endif
|
||||
|
||||
} // namespace p2pool
|
||||
|
||||
namespace std {
|
||||
namespace robin_hood {
|
||||
|
||||
template<>
|
||||
struct hash<p2pool::hash>
|
||||
{
|
||||
FORCEINLINE size_t operator()(const p2pool::hash& value) const noexcept
|
||||
{
|
||||
uint64_t result = 0xcbf29ce484222325ull;
|
||||
for (size_t i = 0; i < p2pool::HASH_SIZE; ++i) {
|
||||
result = (result ^ value.h[i]) * 0x100000001b3ull;
|
||||
}
|
||||
return static_cast<size_t>(result);
|
||||
return hash_bytes(value.h, p2pool::HASH_SIZE);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -144,12 +237,27 @@ struct hash<std::array<uint8_t, N>>
|
||||
{
|
||||
FORCEINLINE size_t operator()(const std::array<uint8_t, N>& value) const noexcept
|
||||
{
|
||||
uint64_t result = 0xcbf29ce484222325ull;
|
||||
for (size_t i = 0; i < N; ++i) {
|
||||
result = (result ^ value[i]) * 0x100000001b3ull;
|
||||
}
|
||||
return static_cast<size_t>(result);
|
||||
return hash_bytes(value.data(), N);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace std
|
||||
template<>
|
||||
struct hash<p2pool::raw_ip>
|
||||
{
|
||||
FORCEINLINE size_t operator()(const p2pool::raw_ip& value) const noexcept
|
||||
{
|
||||
return hash_bytes(value.data, sizeof(value.data));
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct hash<std::pair<uint64_t, uint64_t>>
|
||||
{
|
||||
FORCEINLINE size_t operator()(const std::pair<uint64_t, uint64_t>& value) const noexcept
|
||||
{
|
||||
static_assert(sizeof(value) == sizeof(uint64_t) * 2, "Invalid std::pair<uint64_t, uint64_t> size");
|
||||
return hash_bytes(&value, sizeof(value));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace robin_hood
|
||||
|
||||
+4
-1
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -19,6 +19,9 @@
|
||||
|
||||
#include <uv.h>
|
||||
|
||||
static_assert(sizeof(in6_addr) == 16, "struct in6_addr has invalid size");
|
||||
static_assert(sizeof(in_addr) == 4, "struct in_addr has invalid size");
|
||||
|
||||
namespace p2pool {
|
||||
|
||||
struct MutexLock : public nocopy_nomove
|
||||
|
||||
+20
-10
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
* Portions Copyright (c) 2012-2013 The Cryptonote developers
|
||||
* Portions Copyright (c) 2014-2021 The Monero Project
|
||||
* Portions Copyright (c) 2021 XMRig <https://github.com/xmrig>
|
||||
@@ -79,21 +79,16 @@ static_assert(rev_alphabet.num_symbols == 58, "Check alphabet");
|
||||
|
||||
namespace p2pool {
|
||||
|
||||
Wallet::Wallet(const char* address)
|
||||
Wallet::Wallet(const char* address) : m_prefix(0), m_checksum(0), m_type(NetworkType::Invalid)
|
||||
{
|
||||
decode(address);
|
||||
}
|
||||
|
||||
Wallet::~Wallet()
|
||||
{
|
||||
}
|
||||
|
||||
Wallet::Wallet(const Wallet& w)
|
||||
{
|
||||
operator=(w);
|
||||
}
|
||||
|
||||
// cppcheck-suppress operatorEqVarError
|
||||
Wallet& Wallet::operator=(const Wallet& w)
|
||||
{
|
||||
if (this == &w) {
|
||||
@@ -123,7 +118,7 @@ bool Wallet::decode(const char* address)
|
||||
|
||||
static_assert(last_block_size_index >= 0, "Check ADDRESS_LENGTH");
|
||||
|
||||
uint8_t data[static_cast<size_t>(num_full_blocks) * sizeof(uint64_t) + last_block_size_index];
|
||||
uint8_t data[static_cast<size_t>(num_full_blocks) * sizeof(uint64_t) + last_block_size_index] = {};
|
||||
int data_index = 0;
|
||||
|
||||
for (int i = 0; i <= num_full_blocks; ++i) {
|
||||
@@ -199,10 +194,25 @@ bool Wallet::assign(const hash& spend_pub_key, const hash& view_pub_key, Network
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Wallet::get_eph_public_key(const hash& txkey_sec, size_t output_index, hash& eph_public_key) const
|
||||
bool Wallet::get_eph_public_key(const hash& txkey_sec, size_t output_index, hash& eph_public_key, uint8_t& view_tag) const
|
||||
{
|
||||
hash derivation;
|
||||
if (!generate_key_derivation(m_viewPublicKey, txkey_sec, derivation)) {
|
||||
if (!generate_key_derivation(m_viewPublicKey, txkey_sec, output_index, derivation, view_tag)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!derive_public_key(derivation, output_index, m_spendPublicKey, eph_public_key)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Wallet::get_eph_public_key_with_view_tag(const hash& txkey_sec, size_t output_index, hash& eph_public_key, uint8_t expected_view_tag) const
|
||||
{
|
||||
hash derivation;
|
||||
uint8_t view_tag;
|
||||
if (!generate_key_derivation(m_viewPublicKey, txkey_sec, output_index, derivation, view_tag) || (view_tag != expected_view_tag)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -25,7 +25,6 @@ class Wallet
|
||||
{
|
||||
public:
|
||||
explicit Wallet(const char* address);
|
||||
~Wallet();
|
||||
|
||||
Wallet(const Wallet& w);
|
||||
Wallet& operator=(const Wallet& w);
|
||||
@@ -39,7 +38,8 @@ public:
|
||||
FORCEINLINE const hash& spend_public_key() const { return m_spendPublicKey; }
|
||||
FORCEINLINE const hash& view_public_key() const { return m_viewPublicKey; }
|
||||
|
||||
bool get_eph_public_key(const hash& txkey_sec, size_t output_index, hash& eph_public_key) const;
|
||||
bool get_eph_public_key(const hash& txkey_sec, size_t output_index, hash& eph_public_key, uint8_t& view_tag) const;
|
||||
bool get_eph_public_key_with_view_tag(const hash& txkey_sec, size_t output_index, hash& eph_public_key, uint8_t expected_view_tag) const;
|
||||
|
||||
FORCEINLINE bool operator<(const Wallet& w) const { return m_spendPublicKey < w.m_spendPublicKey; }
|
||||
FORCEINLINE bool operator==(const Wallet& w) const { return m_spendPublicKey == w.m_spendPublicKey; }
|
||||
|
||||
+39
-22
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -49,13 +49,13 @@ ZMQReader::ZMQReader(const char* address, uint32_t zmq_port, MinerCallbackHandle
|
||||
|
||||
if (!m_publisherPort) {
|
||||
LOGERR(1, "failed to to bind ZMQ publisher port, aborting");
|
||||
panic();
|
||||
throw zmq::error_t(EFSM);
|
||||
}
|
||||
|
||||
const int err = uv_thread_create(&m_worker, run_wrapper, this);
|
||||
if (err) {
|
||||
LOGERR(1, "failed to start ZMQ thread, error " << uv_err_name(err));
|
||||
panic();
|
||||
throw zmq::error_t(EFSM);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,24 +71,29 @@ ZMQReader::~ZMQReader()
|
||||
uv_thread_join(&m_worker);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
LOGERR(1, "exception " << e.what() << ", aborting");
|
||||
panic();
|
||||
LOGERR(1, "exception " << e.what());
|
||||
}
|
||||
}
|
||||
|
||||
void ZMQReader::run_wrapper(void* arg)
|
||||
{
|
||||
reinterpret_cast<ZMQReader*>(arg)->run();
|
||||
LOGINFO(1, "worker thread stopped");
|
||||
}
|
||||
|
||||
void ZMQReader::run()
|
||||
{
|
||||
try {
|
||||
char addr[32];
|
||||
|
||||
snprintf(addr, sizeof(addr), "tcp://%s:%u", m_address, m_zmqPort);
|
||||
if (!connect(addr, m_zmqPort)) {
|
||||
throw zmq::error_t();
|
||||
if (!connect(addr)) {
|
||||
return;
|
||||
}
|
||||
|
||||
snprintf(addr, sizeof(addr), "tcp://127.0.0.1:%u", m_publisherPort);
|
||||
if (!connect(addr, m_publisherPort)) {
|
||||
throw zmq::error_t();
|
||||
if (!connect(addr)) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_subscriber.set(zmq::sockopt::subscribe, "json-full-chain_main");
|
||||
@@ -98,7 +103,7 @@ void ZMQReader::run()
|
||||
zmq_msg_t message;
|
||||
int rc = zmq_msg_init(&message);
|
||||
if (rc != 0) {
|
||||
throw zmq::error_t();
|
||||
throw zmq::error_t(errno);
|
||||
}
|
||||
|
||||
LOGINFO(1, "worker thread ready");
|
||||
@@ -106,7 +111,7 @@ void ZMQReader::run()
|
||||
do {
|
||||
rc = zmq_msg_recv(&message, m_subscriber, 0);
|
||||
if (rc < 0) {
|
||||
throw zmq::error_t();
|
||||
throw zmq::error_t(errno);
|
||||
}
|
||||
|
||||
if (m_finished.load()) {
|
||||
@@ -119,13 +124,11 @@ void ZMQReader::run()
|
||||
zmq_msg_close(&message);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
LOGERR(1, "exception " << e.what() << ", aborting");
|
||||
panic();
|
||||
LOGERR(1, "exception " << e.what());
|
||||
}
|
||||
LOGINFO(1, "worker thread stopped");
|
||||
}
|
||||
|
||||
bool ZMQReader::connect(const char* address, uint32_t id)
|
||||
bool ZMQReader::connect(const char* address)
|
||||
{
|
||||
struct ConnectMonitor : public zmq::monitor_t
|
||||
{
|
||||
@@ -138,19 +141,33 @@ bool ZMQReader::connect(const char* address, uint32_t id)
|
||||
bool connected = false;
|
||||
} monitor;
|
||||
|
||||
char buf[32];
|
||||
snprintf(buf, sizeof(buf), "inproc://connect-mon-%u", id);
|
||||
static uint64_t id = 0;
|
||||
|
||||
if (!id) {
|
||||
std::random_device rd;
|
||||
id = (static_cast<uint64_t>(rd()) << 32) | static_cast<uint32_t>(rd());
|
||||
}
|
||||
|
||||
char buf[log::Stream::BUF_SIZE + 1];
|
||||
log::Stream s(buf);
|
||||
s << "inproc://p2pool-connect-mon-" << id << '\0';
|
||||
++id;
|
||||
|
||||
monitor.init(m_subscriber, buf);
|
||||
m_subscriber.connect(address);
|
||||
|
||||
using namespace std::chrono;
|
||||
const system_clock::time_point start_time = system_clock::now();
|
||||
steady_clock::time_point start_time = steady_clock::now();
|
||||
|
||||
while (!monitor.connected && monitor.check_event(-1)) {
|
||||
const int64_t elapsed_time = duration_cast<milliseconds>(system_clock::now() - start_time).count();
|
||||
const steady_clock::time_point cur_time = steady_clock::now();
|
||||
const int64_t elapsed_time = duration_cast<milliseconds>(cur_time - start_time).count();
|
||||
if (elapsed_time >= 3000) {
|
||||
LOGERR(1, "failed to connect to " << address);
|
||||
return false;
|
||||
if (m_finished.load()) {
|
||||
return false;
|
||||
}
|
||||
start_time = cur_time;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,7 +194,7 @@ void ZMQReader::parse(char* data, size_t size)
|
||||
using namespace rapidjson;
|
||||
|
||||
Document doc;
|
||||
if (doc.Parse<rapidjson::kParseCommentsFlag | rapidjson::kParseTrailingCommasFlag>(value, end - value).HasParseError()) {
|
||||
if (doc.Parse<kParseCommentsFlag | kParseTrailingCommasFlag>(value, end - value).HasParseError()) {
|
||||
LOGWARN(1, "ZeroMQ message failed to parse, skipping it");
|
||||
return;
|
||||
}
|
||||
@@ -188,7 +205,7 @@ void ZMQReader::parse(char* data, size_t size)
|
||||
return;
|
||||
}
|
||||
|
||||
m_tx.time_received = time(nullptr);
|
||||
m_tx.time_received = seconds_since_epoch();
|
||||
|
||||
for (SizeType i = 0, n = doc.Size(); i < n; ++i) {
|
||||
const auto& v = doc[i];
|
||||
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -28,9 +28,9 @@ public:
|
||||
~ZMQReader();
|
||||
|
||||
private:
|
||||
static void run_wrapper(void* arg) { reinterpret_cast<ZMQReader*>(arg)->run(); }
|
||||
static void run_wrapper(void* arg);
|
||||
void run();
|
||||
bool connect(const char* address, uint32_t id);
|
||||
bool connect(const char* address);
|
||||
|
||||
void parse(char* data, size_t size);
|
||||
|
||||
|
||||
+43
-61
@@ -1,12 +1,9 @@
|
||||
cmake_minimum_required(VERSION 2.8.12)
|
||||
project(p2pool_tests)
|
||||
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
option(STATIC_LIBS "Use locally built libuv and libzmq static libs" OFF)
|
||||
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
|
||||
|
||||
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.6.0")
|
||||
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT p2pool_tests)
|
||||
@@ -17,49 +14,9 @@ set(LIBS gtest)
|
||||
|
||||
add_subdirectory(../external/src/RandomX RandomX)
|
||||
set(LIBS ${LIBS} randomx)
|
||||
add_definitions(-DWITH_RANDOMX)
|
||||
|
||||
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
|
||||
set(WARNING_FLAGS "")
|
||||
set(OPTIMIZATION_FLAGS "-Ofast -s")
|
||||
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WARNING_FLAGS}")
|
||||
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WARNING_FLAGS}")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
|
||||
if (WIN32)
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
|
||||
else()
|
||||
if (STATIC_LINUX_BINARY)
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
|
||||
else()
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++")
|
||||
endif()
|
||||
endif()
|
||||
elseif (CMAKE_CXX_COMPILER_ID MATCHES MSVC)
|
||||
set(WARNING_FLAGS "")
|
||||
set(SECURITY_FLAGS "/GS /guard:cf")
|
||||
set(OPTIMIZATION_FLAGS "/O2 /Oi /Ob2 /Ot /DNDEBUG /GL")
|
||||
|
||||
set(CMAKE_C_FLAGS_DEBUG "${WARNING_FLAGS} ${SECURITY_FLAGS} /Od /Ob0 /Zi /MTd /fsanitize=address")
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${WARNING_FLAGS} ${SECURITY_FLAGS} /Od /Ob0 /Zi /MTd /fsanitize=address")
|
||||
|
||||
set(CMAKE_C_FLAGS_RELEASE "${WARNING_FLAGS} ${SECURITY_FLAGS} ${OPTIMIZATION_FLAGS} /MT")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${WARNING_FLAGS} ${SECURITY_FLAGS} ${OPTIMIZATION_FLAGS} /MT")
|
||||
|
||||
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${WARNING_FLAGS} ${SECURITY_FLAGS} /Ob1 /Ot /Zi /MT")
|
||||
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${WARNING_FLAGS} ${SECURITY_FLAGS} /Ob1 /Ot /Zi /MT")
|
||||
elseif (CMAKE_CXX_COMPILER_ID MATCHES Clang)
|
||||
set(WARNING_FLAGS "")
|
||||
set(OPTIMIZATION_FLAGS "-Ofast -funroll-loops -fmerge-all-constants")
|
||||
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WARNING_FLAGS}")
|
||||
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WARNING_FLAGS}")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
endif()
|
||||
include(cmake/flags.cmake)
|
||||
|
||||
set(HEADERS
|
||||
)
|
||||
@@ -71,6 +28,7 @@ set(SOURCES
|
||||
src/keccak_tests.cpp
|
||||
src/main.cpp
|
||||
src/pool_block_tests.cpp
|
||||
src/util_tests.cpp
|
||||
src/wallet_tests.cpp
|
||||
../external/src/cryptonote/crypto-ops-data.c
|
||||
../external/src/cryptonote/crypto-ops.c
|
||||
@@ -84,7 +42,9 @@ set(SOURCES
|
||||
../src/json_rpc_request.cpp
|
||||
../src/keccak.cpp
|
||||
../src/log.cpp
|
||||
../src/memory_leak_debug.cpp
|
||||
../src/mempool.cpp
|
||||
../src/miner.cpp
|
||||
../src/p2p_server.cpp
|
||||
../src/p2pool.cpp
|
||||
../src/p2pool_api.cpp
|
||||
@@ -107,6 +67,7 @@ include_directories(../external/src/libzmq/include)
|
||||
include_directories(../external/src/llhttp)
|
||||
include_directories(../external/src/RandomX/src)
|
||||
include_directories(../external/src/rapidjson/include)
|
||||
include_directories(../external/src/robin-hood-hashing/src/include)
|
||||
include_directories(src)
|
||||
include_directories(googletest/googletest/include)
|
||||
|
||||
@@ -116,17 +77,28 @@ elseif (NOT APPLE)
|
||||
set(LIBS ${LIBS} pthread gss dl)
|
||||
endif()
|
||||
|
||||
if (CMAKE_CXX_COMPILER_ID MATCHES MSVC)
|
||||
find_library(ZMQ_LIBRARY_DEBUG NAMES libzmq-v142-mt-sgd-4_3_5 PATHS "../external/lib/libzmq/Debug")
|
||||
find_library(ZMQ_LIBRARY NAMES libzmq-v142-mt-s-4_3_5 PATHS "../external/lib/libzmq/Release")
|
||||
find_library(UV_LIBRARY_DEBUG NAMES uv_a PATHS "../external/lib/libuv/Debug")
|
||||
find_library(UV_LIBRARY NAMES uv_a PATHS "../external/lib/libuv/Release")
|
||||
elseif (CMAKE_CXX_COMPILER_ID MATCHES GNU OR CMAKE_CXX_COMPILER_ID MATCHES Clang)
|
||||
find_library(ZMQ_LIBRARY_DEBUG NAMES zmq libzmq.a)
|
||||
find_library(ZMQ_LIBRARY NAMES zmq libzmq.a)
|
||||
find_library(UV_LIBRARY_DEBUG NAMES uv libuv.a)
|
||||
find_library(UV_LIBRARY NAMES uv libuv.a)
|
||||
find_library(SODIUM_LIBRARY sodium)
|
||||
if (STATIC_LIBS)
|
||||
set(ZMQ_LIBRARY "${CMAKE_SOURCE_DIR}/../external/src/libzmq/build/lib/libzmq.a")
|
||||
set(ZMQ_LIBRARY_DEBUG "${CMAKE_SOURCE_DIR}/../external/src/libzmq/build/lib/libzmq.a")
|
||||
set(UV_LIBRARY_DEBUG "${CMAKE_SOURCE_DIR}/../external/src/libuv/build/libuv_a.a")
|
||||
set(UV_LIBRARY "${CMAKE_SOURCE_DIR}/../external/src/libuv/build/libuv_a.a")
|
||||
else()
|
||||
if (CMAKE_CXX_COMPILER_ID MATCHES MSVC)
|
||||
find_library(ZMQ_LIBRARY_DEBUG NAMES libzmq-v142-mt-sgd-4_3_5 PATHS "../external/lib/libzmq/Debug")
|
||||
find_library(ZMQ_LIBRARY NAMES libzmq-v142-mt-s-4_3_5 PATHS "../external/lib/libzmq/Release")
|
||||
find_library(UV_LIBRARY_DEBUG NAMES uv_a PATHS "../external/lib/libuv/Debug")
|
||||
find_library(UV_LIBRARY NAMES uv_a PATHS "../external/lib/libuv/Release")
|
||||
elseif (CMAKE_CXX_COMPILER_ID MATCHES GNU OR CMAKE_CXX_COMPILER_ID MATCHES Clang)
|
||||
find_library(ZMQ_LIBRARY_DEBUG NAMES zmq libzmq.a)
|
||||
find_library(ZMQ_LIBRARY NAMES zmq libzmq.a)
|
||||
find_library(UV_LIBRARY_DEBUG NAMES uv libuv.a)
|
||||
find_library(UV_LIBRARY NAMES uv libuv.a)
|
||||
find_library(SODIUM_LIBRARY sodium)
|
||||
endif()
|
||||
|
||||
if (SODIUM_LIBRARY)
|
||||
set(LIBS ${LIBS} ${SODIUM_LIBRARY})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
find_library(PGM_LIBRARY pgm)
|
||||
@@ -140,13 +112,23 @@ if (NORM_LIBRARY)
|
||||
set(LIBS ${LIBS} ${NORM_LIBRARY})
|
||||
endif()
|
||||
|
||||
if (SODIUM_LIBRARY)
|
||||
set(LIBS ${LIBS} ${SODIUM_LIBRARY})
|
||||
add_definitions(/DZMQ_STATIC /DP2POOL_LOG_DISABLE)
|
||||
|
||||
include(CheckCXXSourceCompiles)
|
||||
|
||||
check_cxx_source_compiles("int main(){ return __builtin_clzll(1);}" HAVE_BUILTIN_CLZLL)
|
||||
check_cxx_source_compiles("#include <intrin.h>\n#pragma intrinsic(_BitScanReverse64)\nint main(){unsigned long r;_BitScanReverse64(&r,1);return r;}" HAVE_BITSCANREVERSE64)
|
||||
|
||||
if (HAVE_BUILTIN_CLZLL)
|
||||
add_definitions(/DHAVE_BUILTIN_CLZLL)
|
||||
endif()
|
||||
|
||||
add_definitions(/DZMQ_STATIC /DP2POOL_LOG_DISABLE)
|
||||
if (HAVE_BITSCANREVERSE64)
|
||||
add_definitions(/DHAVE_BITSCANREVERSE64)
|
||||
endif()
|
||||
|
||||
add_executable(${CMAKE_PROJECT_NAME} ${HEADERS} ${SOURCES})
|
||||
target_link_libraries(${CMAKE_PROJECT_NAME} debug ${ZMQ_LIBRARY_DEBUG} debug ${UV_LIBRARY_DEBUG} optimized ${ZMQ_LIBRARY} optimized ${UV_LIBRARY} ${LIBS})
|
||||
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_SOURCE_DIR}/src/crypto_tests.txt" $<TARGET_FILE_DIR:${CMAKE_PROJECT_NAME}>)
|
||||
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_SOURCE_DIR}/src/mainnet_test2_block.dat" $<TARGET_FILE_DIR:${CMAKE_PROJECT_NAME}>)
|
||||
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_SOURCE_DIR}/src/sidechain_dump.dat" $<TARGET_FILE_DIR:${CMAKE_PROJECT_NAME}>)
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||
|
||||
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
|
||||
set(GENERAL_FLAGS "-pthread")
|
||||
set(WARNING_FLAGS "-Wall -Wextra")
|
||||
set(OPTIMIZATION_FLAGS "-Ofast -s")
|
||||
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GENERAL_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${GENERAL_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GENERAL_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${GENERAL_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
|
||||
if (WIN32)
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
|
||||
else()
|
||||
if (STATIC_BINARY)
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
|
||||
else()
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++")
|
||||
endif()
|
||||
endif()
|
||||
elseif (CMAKE_CXX_COMPILER_ID MATCHES MSVC)
|
||||
set(GENERAL_FLAGS "")
|
||||
set(WARNING_FLAGS "/Wall /sdl")
|
||||
set(SECURITY_FLAGS "/GS /guard:cf")
|
||||
set(OPTIMIZATION_FLAGS "/O2 /Oi /Ob2 /Ot /DNDEBUG /GL")
|
||||
|
||||
set(CMAKE_C_FLAGS_DEBUG "${GENERAL_FLAGS} ${WARNING_FLAGS} ${SECURITY_FLAGS} /Od /Ob0 /Zi /MTd /fsanitize=address")
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${GENERAL_FLAGS} ${WARNING_FLAGS} ${SECURITY_FLAGS} /Od /Ob0 /Zi /MTd /fsanitize=address")
|
||||
|
||||
set(CMAKE_C_FLAGS_RELEASE "${GENERAL_FLAGS} ${WARNING_FLAGS} ${SECURITY_FLAGS} ${OPTIMIZATION_FLAGS} /MT")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${GENERAL_FLAGS} ${WARNING_FLAGS} ${SECURITY_FLAGS} ${OPTIMIZATION_FLAGS} /MT")
|
||||
|
||||
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${GENERAL_FLAGS} ${WARNING_FLAGS} ${SECURITY_FLAGS} /Ob1 /Ot /Zi /MT")
|
||||
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${GENERAL_FLAGS} ${WARNING_FLAGS} ${SECURITY_FLAGS} /Ob1 /Ot /Zi /MT")
|
||||
|
||||
elseif (CMAKE_CXX_COMPILER_ID MATCHES Clang)
|
||||
set(GENERAL_FLAGS "-pthread")
|
||||
set(WARNING_FLAGS "-Wall -Wextra -Wno-undefined-internal")
|
||||
set(OPTIMIZATION_FLAGS "-Ofast -funroll-loops -fmerge-all-constants")
|
||||
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GENERAL_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${GENERAL_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GENERAL_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${GENERAL_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
|
||||
endif()
|
||||
@@ -42,7 +42,8 @@ TEST(crypto, derivation)
|
||||
if (result) {
|
||||
f >> expected_derivation;
|
||||
}
|
||||
ASSERT_EQ(p2pool::generate_key_derivation(key1, key2, derivation), result);
|
||||
uint8_t view_tag;
|
||||
ASSERT_EQ(p2pool::generate_key_derivation(key1, key2, 0, derivation, view_tag), result);
|
||||
if (result) {
|
||||
ASSERT_EQ(derivation, expected_derivation);
|
||||
}
|
||||
@@ -61,6 +62,20 @@ TEST(crypto, derivation)
|
||||
ASSERT_EQ(derived_key, expected_derived_key);
|
||||
}
|
||||
}
|
||||
else if (name == "derive_view_tag") {
|
||||
hash derivation;
|
||||
uint64_t output_index;
|
||||
std::string result_str;
|
||||
f >> derivation >> output_index >> result_str;
|
||||
uint8_t view_tag;
|
||||
p2pool::derive_view_tag(derivation, output_index, view_tag);
|
||||
|
||||
char buf[log::Stream::BUF_SIZE + 1];
|
||||
log::Stream s(buf);
|
||||
s << log::hex_buf(&view_tag, 1) << '\0';
|
||||
|
||||
ASSERT_EQ(buf, result_str);
|
||||
}
|
||||
} while (!f.eof());
|
||||
}
|
||||
|
||||
|
||||
@@ -542,3 +542,73 @@ derive_public_key 5ea95a51ab11b80c7d09d0c8f8952b70c67e81d0fd421bbed43ab77c1b7b30
|
||||
derive_public_key 2c312ef971def53361274c37a90bfde86f959d877a636ea641a9c976ee80c7e3 121 b611ebd2bcfefc81cb772e35e3dd0204575cb0da644f68d4f9828a2683861e6c false
|
||||
derive_public_key 6fa161dd958022caf185faf873dd9adbc5578352cda505e84fff7cc99a8762a7 333934910 c2b56e207862958751d49643f23079009092c32bf82179a1295e3b85a385c1c3 false
|
||||
derive_public_key d85725562544e1984048391413a6112eb221bd217db3baaa9843bee331000e8e 21880446 f3148c3041e634d829e5df463ba3b1d64df282d620d63485e1f10024e003b939 false
|
||||
derive_view_tag 0fc47054f355ced4d67de73bfa12e4c78ff19089548fffa7d07a674741860f97 0 76
|
||||
derive_view_tag 0fc47054f355ced4d67de73bfa12e4c78ff19089548fffa7d07a674741860f97 1 d6
|
||||
derive_view_tag 0fc47054f355ced4d67de73bfa12e4c78ff19089548fffa7d07a674741860f97 2 87
|
||||
derive_view_tag 0fc47054f355ced4d67de73bfa12e4c78ff19089548fffa7d07a674741860f97 3 1b
|
||||
derive_view_tag 0fc47054f355ced4d67de73bfa12e4c78ff19089548fffa7d07a674741860f97 12 d6
|
||||
derive_view_tag 0fc47054f355ced4d67de73bfa12e4c78ff19089548fffa7d07a674741860f97 13 e9
|
||||
derive_view_tag 0fc47054f355ced4d67de73bfa12e4c78ff19089548fffa7d07a674741860f97 14 12
|
||||
derive_view_tag 0fc47054f355ced4d67de73bfa12e4c78ff19089548fffa7d07a674741860f97 15 26
|
||||
derive_view_tag 0fc47054f355ced4d67de73bfa12e4c78ff19089548fffa7d07a674741860f97 127 2c
|
||||
derive_view_tag 0fc47054f355ced4d67de73bfa12e4c78ff19089548fffa7d07a674741860f97 128 11
|
||||
derive_view_tag a36ba7b4d31349ad278a6df8f77adb76748b59f4929348e67dd92adb9fa174dc 0 70
|
||||
derive_view_tag a36ba7b4d31349ad278a6df8f77adb76748b59f4929348e67dd92adb9fa174dc 1 81
|
||||
derive_view_tag a36ba7b4d31349ad278a6df8f77adb76748b59f4929348e67dd92adb9fa174dc 2 a0
|
||||
derive_view_tag a36ba7b4d31349ad278a6df8f77adb76748b59f4929348e67dd92adb9fa174dc 3 ec
|
||||
derive_view_tag a36ba7b4d31349ad278a6df8f77adb76748b59f4929348e67dd92adb9fa174dc 12 22
|
||||
derive_view_tag a36ba7b4d31349ad278a6df8f77adb76748b59f4929348e67dd92adb9fa174dc 13 0a
|
||||
derive_view_tag a36ba7b4d31349ad278a6df8f77adb76748b59f4929348e67dd92adb9fa174dc 14 87
|
||||
derive_view_tag a36ba7b4d31349ad278a6df8f77adb76748b59f4929348e67dd92adb9fa174dc 15 76
|
||||
derive_view_tag a36ba7b4d31349ad278a6df8f77adb76748b59f4929348e67dd92adb9fa174dc 127 4b
|
||||
derive_view_tag a36ba7b4d31349ad278a6df8f77adb76748b59f4929348e67dd92adb9fa174dc 128 39
|
||||
derive_view_tag 7498d5bf0b69e08653f6d420a17f866dd2bd490ab43074f46065cb501fe7e2d8 0 93
|
||||
derive_view_tag 7498d5bf0b69e08653f6d420a17f866dd2bd490ab43074f46065cb501fe7e2d8 1 67
|
||||
derive_view_tag 7498d5bf0b69e08653f6d420a17f866dd2bd490ab43074f46065cb501fe7e2d8 2 9d
|
||||
derive_view_tag 7498d5bf0b69e08653f6d420a17f866dd2bd490ab43074f46065cb501fe7e2d8 3 2d
|
||||
derive_view_tag 7498d5bf0b69e08653f6d420a17f866dd2bd490ab43074f46065cb501fe7e2d8 12 63
|
||||
derive_view_tag 7498d5bf0b69e08653f6d420a17f866dd2bd490ab43074f46065cb501fe7e2d8 13 cf
|
||||
derive_view_tag 7498d5bf0b69e08653f6d420a17f866dd2bd490ab43074f46065cb501fe7e2d8 14 ef
|
||||
derive_view_tag 7498d5bf0b69e08653f6d420a17f866dd2bd490ab43074f46065cb501fe7e2d8 15 10
|
||||
derive_view_tag 7498d5bf0b69e08653f6d420a17f866dd2bd490ab43074f46065cb501fe7e2d8 127 fc
|
||||
derive_view_tag 7498d5bf0b69e08653f6d420a17f866dd2bd490ab43074f46065cb501fe7e2d8 128 73
|
||||
derive_view_tag fe7770c4b076e95ddb8026affcfab39d31c7c4a2266e0e25e343bc4badc907d0 0 90
|
||||
derive_view_tag fe7770c4b076e95ddb8026affcfab39d31c7c4a2266e0e25e343bc4badc907d0 1 5a
|
||||
derive_view_tag fe7770c4b076e95ddb8026affcfab39d31c7c4a2266e0e25e343bc4badc907d0 2 de
|
||||
derive_view_tag fe7770c4b076e95ddb8026affcfab39d31c7c4a2266e0e25e343bc4badc907d0 3 21
|
||||
derive_view_tag fe7770c4b076e95ddb8026affcfab39d31c7c4a2266e0e25e343bc4badc907d0 12 57
|
||||
derive_view_tag fe7770c4b076e95ddb8026affcfab39d31c7c4a2266e0e25e343bc4badc907d0 13 52
|
||||
derive_view_tag fe7770c4b076e95ddb8026affcfab39d31c7c4a2266e0e25e343bc4badc907d0 14 6f
|
||||
derive_view_tag fe7770c4b076e95ddb8026affcfab39d31c7c4a2266e0e25e343bc4badc907d0 15 eb
|
||||
derive_view_tag fe7770c4b076e95ddb8026affcfab39d31c7c4a2266e0e25e343bc4badc907d0 127 93
|
||||
derive_view_tag fe7770c4b076e95ddb8026affcfab39d31c7c4a2266e0e25e343bc4badc907d0 128 dc
|
||||
derive_view_tag ea9337d0ddf480abdc4fc56a0cb223702729cb230ae7b9de50243ad25ce90e8d 0 c6
|
||||
derive_view_tag ea9337d0ddf480abdc4fc56a0cb223702729cb230ae7b9de50243ad25ce90e8d 1 60
|
||||
derive_view_tag ea9337d0ddf480abdc4fc56a0cb223702729cb230ae7b9de50243ad25ce90e8d 2 f0
|
||||
derive_view_tag ea9337d0ddf480abdc4fc56a0cb223702729cb230ae7b9de50243ad25ce90e8d 3 71
|
||||
derive_view_tag ea9337d0ddf480abdc4fc56a0cb223702729cb230ae7b9de50243ad25ce90e8d 12 0e
|
||||
derive_view_tag ea9337d0ddf480abdc4fc56a0cb223702729cb230ae7b9de50243ad25ce90e8d 13 42
|
||||
derive_view_tag ea9337d0ddf480abdc4fc56a0cb223702729cb230ae7b9de50243ad25ce90e8d 14 b2
|
||||
derive_view_tag ea9337d0ddf480abdc4fc56a0cb223702729cb230ae7b9de50243ad25ce90e8d 15 61
|
||||
derive_view_tag ea9337d0ddf480abdc4fc56a0cb223702729cb230ae7b9de50243ad25ce90e8d 127 c3
|
||||
derive_view_tag ea9337d0ddf480abdc4fc56a0cb223702729cb230ae7b9de50243ad25ce90e8d 128 0a
|
||||
derive_view_tag 25d538315bcb81aff9574189ea65f418aeb0392f5cbbc84cd8a33c7ade31ef0a 0 4c
|
||||
derive_view_tag 25d538315bcb81aff9574189ea65f418aeb0392f5cbbc84cd8a33c7ade31ef0a 1 9b
|
||||
derive_view_tag 25d538315bcb81aff9574189ea65f418aeb0392f5cbbc84cd8a33c7ade31ef0a 2 64
|
||||
derive_view_tag 25d538315bcb81aff9574189ea65f418aeb0392f5cbbc84cd8a33c7ade31ef0a 3 ff
|
||||
derive_view_tag 25d538315bcb81aff9574189ea65f418aeb0392f5cbbc84cd8a33c7ade31ef0a 12 e3
|
||||
derive_view_tag 25d538315bcb81aff9574189ea65f418aeb0392f5cbbc84cd8a33c7ade31ef0a 13 24
|
||||
derive_view_tag 25d538315bcb81aff9574189ea65f418aeb0392f5cbbc84cd8a33c7ade31ef0a 14 ea
|
||||
derive_view_tag 25d538315bcb81aff9574189ea65f418aeb0392f5cbbc84cd8a33c7ade31ef0a 15 3b
|
||||
derive_view_tag 25d538315bcb81aff9574189ea65f418aeb0392f5cbbc84cd8a33c7ade31ef0a 127 47
|
||||
derive_view_tag 25d538315bcb81aff9574189ea65f418aeb0392f5cbbc84cd8a33c7ade31ef0a 128 5a
|
||||
derive_view_tag 8edfabada2b24ef4d8d915826c9ff0245910e4b835b59c2cf8ed8fc991b2e1e8 0 74
|
||||
derive_view_tag 8edfabada2b24ef4d8d915826c9ff0245910e4b835b59c2cf8ed8fc991b2e1e8 1 77
|
||||
derive_view_tag 8edfabada2b24ef4d8d915826c9ff0245910e4b835b59c2cf8ed8fc991b2e1e8 2 a9
|
||||
derive_view_tag 8edfabada2b24ef4d8d915826c9ff0245910e4b835b59c2cf8ed8fc991b2e1e8 3 44
|
||||
derive_view_tag 8edfabada2b24ef4d8d915826c9ff0245910e4b835b59c2cf8ed8fc991b2e1e8 12 75
|
||||
derive_view_tag 8edfabada2b24ef4d8d915826c9ff0245910e4b835b59c2cf8ed8fc991b2e1e8 13 05
|
||||
derive_view_tag 8edfabada2b24ef4d8d915826c9ff0245910e4b835b59c2cf8ed8fc991b2e1e8 14 ca
|
||||
derive_view_tag 8edfabada2b24ef4d8d915826c9ff0245910e4b835b59c2cf8ed8fc991b2e1e8 15 00
|
||||
derive_view_tag 8edfabada2b24ef4d8d915826c9ff0245910e4b835b59c2cf8ed8fc991b2e1e8 127 a6
|
||||
derive_view_tag 8edfabada2b24ef4d8d915826c9ff0245910e4b835b59c2cf8ed8fc991b2e1e8 128 0d
|
||||
|
||||
@@ -254,7 +254,7 @@ TEST(difficulty_type, check_pow)
|
||||
|
||||
// Max hash value that passes this difficulty
|
||||
memcpy(h.h, data, HASH_SIZE);
|
||||
EXPECT_EQ(diff.check_pow(h), true);
|
||||
ASSERT_EQ(diff.check_pow(h), true);
|
||||
|
||||
// Add 1 to data (256-bit number)
|
||||
for (int j = 0; j <= 3; ++j) {
|
||||
@@ -267,7 +267,7 @@ TEST(difficulty_type, check_pow)
|
||||
|
||||
// Min hash value that fails this difficulty
|
||||
memcpy(h.h, data, HASH_SIZE);
|
||||
EXPECT_EQ(diff.check_pow(h), false);
|
||||
ASSERT_EQ(diff.check_pow(h), false);
|
||||
}
|
||||
|
||||
const uint64_t target = diff.target();
|
||||
@@ -276,14 +276,14 @@ TEST(difficulty_type, check_pow)
|
||||
for (int j = 0; j < 10000; ++j) {
|
||||
const uint64_t data[4] = { r(), r(), r(), r() % target };
|
||||
memcpy(h.h, data, HASH_SIZE);
|
||||
EXPECT_EQ(diff.check_pow(h), true);
|
||||
ASSERT_EQ(diff.check_pow(h), true);
|
||||
}
|
||||
|
||||
// Random values that fail
|
||||
for (int j = 0; j < 10000; ++j) {
|
||||
const uint64_t data[4] = { r(), r(), r(), target + (r() % (std::numeric_limits<uint64_t>::max() - target + 1)) };
|
||||
memcpy(h.h, data, HASH_SIZE);
|
||||
EXPECT_EQ(diff.check_pow(h), false);
|
||||
ASSERT_EQ(diff.check_pow(h), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ TEST(hash, empty)
|
||||
hash h;
|
||||
ASSERT_EQ(h.empty(), true);
|
||||
|
||||
for (int i = 0; i < HASH_SIZE; ++i) {
|
||||
for (size_t i = 0; i < HASH_SIZE; ++i) {
|
||||
hash h2;
|
||||
h2.h[i] = 1;
|
||||
ASSERT_EQ(h2.empty(), false);
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
void p2pool_usage() {}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
#include "crypto.h"
|
||||
#include "pool_block.h"
|
||||
#include "pow_hash.h"
|
||||
#include "side_chain.h"
|
||||
@@ -72,7 +73,6 @@ TEST(pool_block, deserialize)
|
||||
ASSERT_EQ(b.m_cumulativeDifficulty.lo, 12544665764606ull);
|
||||
ASSERT_EQ(b.m_cumulativeDifficulty.hi, 0);
|
||||
ASSERT_EQ(b.m_tmpTxExtra.size(), 0);
|
||||
ASSERT_EQ(b.m_tmpInts.size(), 0);
|
||||
ASSERT_EQ(b.m_depth, 0);
|
||||
ASSERT_EQ(b.m_verified, false);
|
||||
ASSERT_EQ(b.m_invalid, false);
|
||||
@@ -91,7 +91,7 @@ TEST(pool_block, deserialize)
|
||||
hasher.set_seed(seed);
|
||||
|
||||
hash pow_hash;
|
||||
ASSERT_EQ(b.get_pow_hash(&hasher, seed, pow_hash), true);
|
||||
ASSERT_EQ(b.get_pow_hash(&hasher, 0, seed, pow_hash), true);
|
||||
|
||||
std::stringstream s;
|
||||
s << pow_hash;
|
||||
@@ -100,4 +100,43 @@ TEST(pool_block, deserialize)
|
||||
ASSERT_EQ(b.m_difficulty.check_pow(pow_hash), true);
|
||||
}
|
||||
|
||||
TEST(pool_block, verify)
|
||||
{
|
||||
init_crypto_cache();
|
||||
|
||||
PoolBlock b;
|
||||
SideChain sidechain(nullptr, NetworkType::Mainnet);
|
||||
|
||||
std::ifstream f("sidechain_dump.dat", std::ios::binary | std::ios::ate);
|
||||
ASSERT_EQ(f.good() && f.is_open(), true);
|
||||
|
||||
std::vector<uint8_t> buf(f.tellg());
|
||||
f.seekg(0);
|
||||
f.read(reinterpret_cast<char*>(buf.data()), buf.size());
|
||||
ASSERT_EQ(f.good(), true);
|
||||
|
||||
for (const uint8_t *p = buf.data(), *e = buf.data() + buf.size(); p < e;) {
|
||||
ASSERT_TRUE(p + sizeof(uint32_t) <= e);
|
||||
const uint32_t n = *reinterpret_cast<const uint32_t*>(p);
|
||||
p += sizeof(uint32_t);
|
||||
|
||||
ASSERT_TRUE(p + n <= e);
|
||||
ASSERT_EQ(b.deserialize(p, n, sidechain), 0);
|
||||
p += n;
|
||||
|
||||
sidechain.add_block(b);
|
||||
ASSERT_TRUE(sidechain.find_block(b.m_sidechainId) != nullptr);
|
||||
}
|
||||
|
||||
const PoolBlock* tip = sidechain.chainTip();
|
||||
ASSERT_TRUE(tip != nullptr);
|
||||
ASSERT_TRUE(tip->m_verified);
|
||||
ASSERT_FALSE(tip->m_invalid);
|
||||
|
||||
ASSERT_EQ(tip->m_txinGenHeight, 2483901);
|
||||
ASSERT_EQ(tip->m_sidechainHeight, 522805);
|
||||
|
||||
destroy_crypto_cache();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program 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, version 3.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
#include "util.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace p2pool {
|
||||
|
||||
TEST(util, varint)
|
||||
{
|
||||
std::vector<uint8_t> v;
|
||||
v.reserve(16);
|
||||
|
||||
uint64_t check;
|
||||
|
||||
// 0...2^7 - 1
|
||||
for (uint64_t value = 0; value < 0x80; ++value) {
|
||||
v.clear();
|
||||
writeVarint(value, v);
|
||||
ASSERT_EQ(v.size(), 1);
|
||||
ASSERT_EQ(v[0], value);
|
||||
ASSERT_EQ(readVarint(v.data(), v.data() + v.size(), check), v.data() + v.size());
|
||||
ASSERT_EQ(check, value);
|
||||
}
|
||||
|
||||
// 2^7...2^14 - 1
|
||||
for (uint64_t value = 0x80; value < 0x4000; ++value) {
|
||||
v.clear();
|
||||
writeVarint(value, v);
|
||||
ASSERT_EQ(v.size(), 2);
|
||||
ASSERT_EQ(v[0], (value & 0x7F) | 0x80);
|
||||
ASSERT_EQ(v[1], value >> 7);
|
||||
ASSERT_EQ(readVarint(v.data(), v.data() + v.size(), check), v.data() + v.size());
|
||||
ASSERT_EQ(check, value);
|
||||
}
|
||||
|
||||
// 2^14...2^21 - 1
|
||||
for (uint64_t value = 0x4000; value < 0x200000; ++value) {
|
||||
v.clear();
|
||||
writeVarint(value, v);
|
||||
ASSERT_EQ(v.size(), 3);
|
||||
ASSERT_EQ(v[0], (value & 0x7F) | 0x80);
|
||||
ASSERT_EQ(v[1], ((value >> 7) & 0x7F) | 0x80);
|
||||
ASSERT_EQ(v[2], value >> 14);
|
||||
ASSERT_EQ(readVarint(v.data(), v.data() + v.size(), check), v.data() + v.size());
|
||||
ASSERT_EQ(check, value);
|
||||
}
|
||||
|
||||
// 2^64 - 1
|
||||
v.clear();
|
||||
writeVarint(std::numeric_limits<uint64_t>::max(), v);
|
||||
ASSERT_EQ(v.size(), 10);
|
||||
for (int i = 0; i < 9; ++i) {
|
||||
ASSERT_EQ(v[i], 0xFF);
|
||||
}
|
||||
ASSERT_EQ(v[9], 1);
|
||||
ASSERT_EQ(readVarint(v.data(), v.data() + v.size(), check), v.data() + v.size());
|
||||
ASSERT_EQ(check, std::numeric_limits<uint64_t>::max());
|
||||
|
||||
// Invalid value 1
|
||||
uint8_t buf[16];
|
||||
memset(buf, -1, sizeof(buf));
|
||||
ASSERT_EQ(readVarint(buf, buf + sizeof(buf), check), nullptr);
|
||||
|
||||
// Invalid value 2
|
||||
uint8_t buf2[1] = { 0x80 };
|
||||
ASSERT_EQ(readVarint(buf2, buf2 + 1, check), nullptr);
|
||||
}
|
||||
|
||||
TEST(util, bsr)
|
||||
{
|
||||
for (uint64_t i = 0, x = 1; i <= 63; ++i, x <<= 1) {
|
||||
ASSERT_EQ(bsr(x), i);
|
||||
ASSERT_EQ(bsr_reference(x), i);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user