Compare commits

...

53 Commits

Author SHA1 Message Date
SChernykh 2071a24a9f Update version 2021-10-27 17:27:48 +02:00
SChernykh 1c0da4513f Added --no-color command line option
`--no-color` disables colors in console output
2021-10-27 16:48:17 +02:00
SChernykh 6a002cda46 Create msvc-analysis.yml 2021-10-27 16:06:36 +02:00
SChernykh c21c9615a3 Fixed optimization flags for gcc/clang 2021-10-26 20:13:51 +02:00
SChernykh c0c210664a Show uptime in status 2021-10-26 17:55:47 +02:00
SChernykh 6859d11445 Disable console quick-edit mode on Windows
Prevent user from accidentally selecting text in console and freezing all output
2021-10-26 17:42:00 +02:00
SChernykh 111324b6e0 Added console command to show connected peers 2021-10-24 16:04:30 +02:00
SChernykh 148b9dd294 Switch to faster unordered_map/set 2021-10-22 18:34:20 +02:00
SChernykh 04d18cdf1d P2PServer: fixed m_timer leak 2021-10-21 19:19:22 +02:00
SChernykh 352ad81a0a StratumServer: faster processing of regular shares 2021-10-21 19:09:36 +02:00
SChernykh d26902468d P2PServer: peer list tweaks
- Increase outgoing connections to 10
- Stricter handling of peer list responses
2021-10-21 15:55:00 +02:00
SChernykh 0382027d70 Fixed wrong error message 2021-10-21 10:12:57 +02:00
SChernykh 966b499e5c ZMQReader: keep trying to connect on startup 2021-10-21 00:22:53 +02:00
SChernykh 9885f82e9e Limit user name length to 64 characters
Also avoid creating temporary strings (`" user " + client->m_customUser`) when logging user name
2021-10-19 21:09:08 +02:00
SChernykh 32a58d1534 Merge pull request #58 from WeebDataHoarder/master
Display stratum user on SHARE FOUND and "client found mainchain block" log entries
2021-10-19 20:10:53 +02:00
WeebDataHoarder bc341918f6 Decode custom user from stratum client, display stratum client+user on SHARE FOUND and client mainchain found message 2021-10-19 19:33:15 +02:00
SChernykh f17c23528a Update version 2021-10-17 11:00:51 +02:00
SChernykh a06f4dcdcd P2PServer: fixed ever growing memory usage 2021-10-17 10:57:33 +02:00
SChernykh 3bc03e4801 CI: added Visual Studio 2022 build 2021-10-16 15:01:29 +02:00
SChernykh 816a29c5ab Added --no-cache command line parameter 2021-10-16 13:45:28 +02:00
SChernykh 285560e120 Fixed possible sidechain split during RandomX epoch change 2021-10-16 09:57:13 +02:00
SChernykh 39ba5c4131 P2PServer: check for messages that shouldn't be sent 2021-10-15 17:09:51 +02:00
SChernykh 9b86f8e81f Peer list refactoring
- Remove peers that weren't seen for > 1 hour
- Better peer selection algorithm for PEER_LIST_RESPONSE
2021-10-15 11:32:01 +02:00
SChernykh 9e90e988fa Refactored array_size to make it always constexpr 2021-10-14 16:46:49 +02:00
SChernykh 39b2167c98 Show correct max log level in usage 2021-10-14 13:59:11 +02:00
SChernykh f3024d3556 StratumServer: submit Monero block as soon as possible
1-3 ms saved when a block is found
2021-10-13 18:57:21 +02:00
SChernykh 61ac90f1d5 Fixed typo 2021-10-13 14:56:27 +02:00
SChernykh 60a8538e0c P2PServer: show an error when there are no ZMQ messages 2021-10-09 11:01:26 +02:00
SChernykh 77a3a5857c P2PServer: show an error when there are no connections 2021-10-08 18:21:31 +02:00
SChernykh af2a8eeb08 Tweaked warning level for lagging nodes 2021-10-06 15:22:03 +02:00
SChernykh 54d0ab3658 Move most block broadcasts to log level 6 2021-10-06 09:47:41 +02:00
SChernykh c9faa92098 StratumServer: fixed "bad share" ban logic 2021-10-04 15:51:28 +02:00
SChernykh 52e5008bf4 Refactoring
- Skip calling time(nullptr) twice
- Use getter method for m_api instead of exposing it
- Moved api_udate_*() back to private since they're not used anywhere else
2021-10-04 10:28:56 +02:00
SChernykh a0a31b0f3b Merge pull request #51 from Tonux599/stratumServerJson-staging
API for stratum server statistics.
2021-10-04 11:16:37 +03:00
Thomas Clarke 7742d163f7 API for stratum server statistics.
Users can pass `--stratum-api` to command line to enable a file called `local/stats` in `--data-api` directory. `local/stats` contains statistics that `StratumServer status` provides but in JSON format. It is currently setup that `local/stats` is updated each time a new job is sent to a worker.
2021-10-03 22:32:13 +01:00
SChernykh 8106cc1c1c Update rapidjson 2021-10-02 17:16:30 +02:00
SChernykh 7d66864b01 Print P2Pool version 2021-10-02 17:06:48 +02:00
SChernykh 2a3cd13b19 P2PServer: delete old cached blocks after initial sync
Saves ~20 MB of memory
2021-10-01 15:31:47 +02:00
SChernykh 00281fb006 Updated monerod command line for optimal mining 2021-09-28 17:32:30 +02:00
SChernykh 4bcaa41c8e Clear crypto derivation/key cache when a block is found 2021-09-27 13:47:27 +02:00
SChernykh 4fce76576f ZMQ reader: abort if connect to monerod failed 2021-09-27 13:47:27 +02:00
SChernykh 19043ace90 Merge pull request #38 from QuickBASIC/fix-batch-2-electric-boogaloo
fix run as admin and add pauses
2021-09-26 19:47:22 +03:00
QuickBASIC f9d75360ba fix run as admin and add pauses 2021-09-26 12:29:04 -04:00
SChernykh f05f7c4268 Fixed wrong nonce values in submit_block error log 2021-09-26 16:50:24 +02:00
SChernykh d8d84f08c4 Update llhttp.c 2021-09-26 14:35:16 +02:00
SChernykh ae8733c187 Update RandomX 2021-09-22 17:59:58 +02:00
SChernykh 3d1db2fa22 Merge pull request #28 from QuickBASIC/readme-updates
update how to mine on p2pool section
2021-09-20 09:38:36 +03:00
Mike Justman 3adc0b8ecd fixed typo 2021-09-20 01:38:53 -04:00
QuickBASIC 08dff2a63a update how to mine on p2pool section 2021-09-19 21:59:53 -04:00
SChernykh c29f211ebf Update README.md 2021-09-19 09:21:51 +02:00
SChernykh 75b629c64c Merge pull request #27 from QuickBASIC/post-release-readme-cleanup
remove testing verbiage
2021-09-19 03:16:38 +03:00
QuickBASIC 516e7196a4 remove testing verbiage 2021-09-18 20:08:41 -04:00
SChernykh 7d1372ff05 Update README.md 2021-09-18 23:47:59 +02:00
44 changed files with 842 additions and 221 deletions
+12 -9
View File
@@ -96,7 +96,13 @@ jobs:
build-windows-msbuild:
runs-on: windows-latest
runs-on: windows-${{ matrix.config.os }}
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\\"}
steps:
- name: Checkout repository
@@ -107,23 +113,20 @@ jobs:
- name: Setup cmake
uses: lukka/get-cmake@latest
- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1.0.3
- name: Build p2pool
run: |
mkdir build
cd build
cmake .. -G "Visual Studio 16 2019"
msbuild /m /p:Configuration=Release p2pool.vcxproj
cmake .. -G "${{ matrix.config.vs }}"
& "${{ matrix.config.msbuild }}msbuild" /m /p:Configuration=Release p2pool.vcxproj
- name: Build tests
run: |
cd tests
mkdir build
cd build
cmake .. -G "Visual Studio 16 2019"
msbuild /m /p:Configuration=Release p2pool_tests.vcxproj
cmake .. -G "${{ matrix.config.vs }}"
& "${{ matrix.config.msbuild }}msbuild" /m /p:Configuration=Release p2pool_tests.vcxproj
- name: Run tests
run: |
@@ -133,7 +136,7 @@ jobs:
- name: Archive binary
uses: actions/upload-artifact@v2
with:
name: p2pool-msbuild.exe
name: p2pool-msbuild-${{ matrix.config.os }}.exe
path: build/Release/p2pool.exe
build-macos:
+61
View File
@@ -0,0 +1,61 @@
# 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'
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 }}
# 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@04825f6d9e00f87422d6bf04e1a38b1f3ed60d99
# Provide a unique ID to access the sarif output path
id: run-analysis
with:
cmakeBuildDirectory: ${{ env.build }}
# 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
View File
@@ -19,3 +19,6 @@
[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
+1
View File
@@ -82,6 +82,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)
if (WIN32)
set(LIBS ${LIBS} ws2_32 iphlpapi userenv psapi)
+53 -17
View File
@@ -2,9 +2,7 @@
Decentralized pool for Monero mining.
**NOTE** This is a highly experimental and untested software. I did some extensive testing locally, but there's zero guarantee it will work for you! It requires a custom monerod version (for now). No binaries are provided yet. Testing on mainnet has started! You can check the pool status at https://p2pool.io/
No ETA on the official release date, but hopefully before the end of September 2021.
Mining on the mainnet has started! You can check the pool status at https://p2pool.io/
### Build status
@@ -126,6 +124,7 @@ make -j$(sysctl -n hw.logicalcpu)
### Windows
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
cd p2pool
@@ -135,6 +134,8 @@ cmake .. -G "Visual Studio 16 2019"
```
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:
```
git clone --recursive https://github.com/SChernykh/monero
@@ -144,28 +145,35 @@ git submodule sync && git submodule update --init --force --recursive
```
then follow the instructions from https://github.com/monero-project/monero/#on-windows
## How to test
## How to mine on P2Pool
Mainnet test has started! **PPLNS window = 2160 blocks, block time = 10 seconds**. 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 testing because **wallet addresses are public on p2pool**.
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
The purpose of this test is to bring as much hashrate as possible and check if stratum server works fine!
**General Considerations**
- 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)
- 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`
- Create a separate restricted user account for testing. p2pool is untested and can have serious bugs/vulnerabilities!
- 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
- Create a new mainnet wallet
- You have to use the primary wallet address for mining. Subaddresses and integrated addresses are not supported, just like with monerod solo mining
- Run `./monerod --zmq-pub tcp://127.0.0.1:18083` **don't forget --zmq-pub parameter in the command line**
- Double check that it shows **Monero 'Oxygen Orion' (v0.17.2.3-04bfd948a)** on startup. Wait until it's synchronized.
- 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`
- Keep both monerod and p2pool running for the whole duration of your test
- 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:
```
@@ -178,15 +186,43 @@ The purpose of this test is to bring as much hashrate as possible and check if s
nocreate
}
```
- Wait until initial p2pool sync is finished, it shouldn't take more than 5-10 minutes. Of course it depends on your connection speed!
- p2pool has a stratum server listening on port 3333, you can connect xmrig to it now
- 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
- you can connect multiple miners to the same p2pool node. The more the better!
- From now on, watch your wallet to see if it gets anything
- 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.
```
@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
```
## Donations
If you'd like to support further development of Monero P2Pool, you're welcome to send any amount of XMR to the following address:
+4 -4
View File
@@ -9,10 +9,10 @@ if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
set(WARNING_FLAGS "-Wall -Wextra -Werror")
set(OPTIMIZATION_FLAGS "-Ofast -s")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WARNING_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_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 "${CMAKE_CXX_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
if (WIN32)
@@ -42,9 +42,9 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES Clang)
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 "${CMAKE_C_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_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 "${CMAKE_CXX_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
endif()
+1
View File
@@ -7,3 +7,4 @@
../external/src/llhttp/
../external/src/RandomX/src/
../external/src/rapidjson/include
../external/src/robin-hood-hashing/src/include
+1 -1
View File
@@ -1,5 +1,5 @@
#!/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
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 -I ../external/src/robin-hood-hashing/src/include --suppressions-list=suppressions.txt --output-file=errors_full.txt
grep -v 'external' errors_full.txt | grep -v 'unmatchedSuppression' > errors_filtered.txt
if [ -s errors_filtered.txt ]; then
cat errors_filtered.txt
+1
View File
@@ -2,6 +2,7 @@
#pragma warning(disable : 4061 4065 4100 4152 4242 4244 4668 4702 4710 4711 4820 5045)
#elif defined __GNUC__
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-variable"
#endif
#if LLHTTP_STRICT_MODE
+25 -6
View File
@@ -66,7 +66,7 @@ BlockTemplate::BlockTemplate(p2pool* pool)
m_mempoolTxsOrder.reserve(1024);
m_shares.reserve(m_pool->side_chain().chain_window_size() * 2);
for (size_t i = 0; i < array_size(m_oldTemplates); ++i) {
for (size_t i = 0; i < array_size(&BlockTemplate::m_oldTemplates); ++i) {
m_oldTemplates[i] = new BlockTemplate(*this);
}
@@ -79,7 +79,7 @@ BlockTemplate::BlockTemplate(p2pool* pool)
BlockTemplate::~BlockTemplate()
{
for (size_t i = 0; i < array_size(m_oldTemplates); ++i) {
for (size_t i = 0; i < array_size(&BlockTemplate::m_oldTemplates); ++i) {
delete m_oldTemplates[i];
}
@@ -185,7 +185,7 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet
WriteLock lock(m_lock);
if (m_templateId > 0) {
*m_oldTemplates[m_templateId % array_size(m_oldTemplates)] = *this;
*m_oldTemplates[m_templateId % array_size(&BlockTemplate::m_oldTemplates)] = *this;
}
++m_templateId;
@@ -838,6 +838,25 @@ void BlockTemplate::calc_merkle_tree_main_branch()
}
}
bool BlockTemplate::get_difficulties(const uint32_t template_id, difficulty_type& mainchain_difficulty, difficulty_type& sidechain_difficulty) const
{
ReadLock lock(m_lock);
if (template_id == m_templateId) {
mainchain_difficulty = m_difficulty;
sidechain_difficulty = m_poolBlockTemplate->m_difficulty;
return true;
}
const BlockTemplate* old = m_oldTemplates[template_id % array_size(&BlockTemplate::m_oldTemplates)];
if (old && (template_id == old->m_templateId)) {
return old->get_difficulties(template_id, mainchain_difficulty, sidechain_difficulty);
}
return false;
}
uint32_t BlockTemplate::get_hashing_blob(const uint32_t template_id, uint32_t extra_nonce, uint8_t (&blob)[128], uint64_t& height, difficulty_type& difficulty, difficulty_type& sidechain_difficulty, hash& seed_hash, size_t& nonce_offset) const
{
ReadLock lock(m_lock);
@@ -852,7 +871,7 @@ uint32_t BlockTemplate::get_hashing_blob(const uint32_t template_id, uint32_t ex
return get_hashing_blob_nolock(extra_nonce, blob);
}
const BlockTemplate* old = m_oldTemplates[template_id % array_size(m_oldTemplates)];
const BlockTemplate* old = m_oldTemplates[template_id % array_size(&BlockTemplate::m_oldTemplates)];
if (old && (template_id == old->m_templateId)) {
return old->get_hashing_blob(template_id, extra_nonce, blob, height, difficulty, sidechain_difficulty, seed_hash, nonce_offset);
@@ -953,7 +972,7 @@ std::vector<uint8_t> BlockTemplate::get_block_template_blob(uint32_t template_id
ReadLock lock(m_lock);
if (template_id != m_templateId) {
const BlockTemplate* old = m_oldTemplates[template_id % array_size(m_oldTemplates)];
const BlockTemplate* old = m_oldTemplates[template_id % array_size(&BlockTemplate::m_oldTemplates)];
if (old && (template_id == old->m_templateId)) {
return old->get_block_template_blob(template_id, nonce_offset, extra_nonce_offset);
}
@@ -1016,7 +1035,7 @@ void BlockTemplate::submit_sidechain_block(uint32_t template_id, uint32_t nonce,
return;
}
BlockTemplate* old = m_oldTemplates[template_id % array_size(m_oldTemplates)];
BlockTemplate* old = m_oldTemplates[template_id % array_size(&BlockTemplate::m_oldTemplates)];
if (old && (template_id == old->m_templateId)) {
old->submit_sidechain_block(template_id, nonce, extra_nonce);
+1
View File
@@ -40,6 +40,7 @@ public:
void update(const MinerData& data, const Mempool& mempool, Wallet* miner_wallet);
bool get_difficulties(const uint32_t template_id, difficulty_type& mainchain_difficulty, difficulty_type& sidechain_difficulty) const;
uint32_t get_hashing_blob(const uint32_t template_id, uint32_t extra_nonce, uint8_t (&blob)[128], uint64_t& height, difficulty_type& difficulty, difficulty_type& sidechain_difficulty, hash& seed_hash, size_t& nonce_offset) const;
uint32_t get_hashing_blob(uint32_t extra_nonce, uint8_t (&blob)[128], uint64_t& height, difficulty_type& difficulty, difficulty_type& sidechain_difficulty, hash& seed_hash, size_t& nonce_offset, uint32_t& template_id) const;
+5
View File
@@ -279,6 +279,11 @@ enum class NetworkType {
Stagenet,
};
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"
+10 -1
View File
@@ -69,7 +69,7 @@ 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_exit;
static cmd cmds[] = {
{ STRCONST("help"), "", "display list of commands", do_help },
@@ -77,6 +77,7 @@ 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("exit"), "", "terminate p2pool", do_exit },
{ STRCNULL, NULL, NULL, NULL }
};
@@ -129,6 +130,14 @@ 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_exit(p2pool *m_pool, const char * /* args */)
{
bkg_jobs_tracker.wait();
+33 -6
View File
@@ -20,7 +20,6 @@
#include "keccak.h"
#include "uv_util.h"
#include <random>
#include <unordered_map>
extern "C" {
#include "crypto-ops.h"
@@ -227,22 +226,50 @@ public:
return true;
}
void clear()
{
MutexLock lock(m);
derivations.clear();
public_keys.clear();
}
private:
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>, hash> derivations;
unordered_map<std::array<uint8_t, HASH_SIZE * 2 + sizeof(size_t)>, hash> public_keys;
};
static Cache cache;
static Cache* cache = nullptr;
bool generate_key_derivation(const hash& key1, const hash& key2, hash& derivation)
{
return cache.get_derivation(key1, key2, derivation);
return cache->get_derivation(key1, key2, derivation);
}
bool derive_public_key(const hash& derivation, size_t output_index, const hash& base, hash& derived_key)
{
return cache.get_public_key(derivation, output_index, base, derived_key);
return cache->get_public_key(derivation, output_index, base, derived_key);
}
void init_crypto_cache()
{
if (!cache) {
cache = new Cache();
}
}
void destroy_crypto_cache()
{
if (cache) {
delete cache;
cache = nullptr;
}
}
void clear_crypto_cache()
{
cache->clear();
}
} // namespace p2pool
+4
View File
@@ -24,4 +24,8 @@ bool check_keys(const hash& pub, const hash& sec);
bool generate_key_derivation(const hash& key1, const hash& key2, hash& derivation);
bool derive_public_key(const hash& derivation, size_t output_index, const hash& base, hash& derived_key);
void init_crypto_cache();
void destroy_crypto_cache();
void clear_crypto_cache();
} // namespace p2pool
+12 -1
View File
@@ -28,6 +28,7 @@ namespace p2pool {
namespace log {
int GLOBAL_LOG_LEVEL = 3;
bool CONSOLE_COLORS = true;
#ifndef P2POOL_LOG_DISABLE
@@ -35,6 +36,7 @@ static volatile 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
@@ -69,6 +71,9 @@ public:
#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);
}
@@ -155,6 +160,10 @@ private:
p += 3;
size -= 3;
if (!CONSOLE_COLORS) {
strip_colors(p, size);
}
#ifdef _WIN32
DWORD k;
WriteConsole((severity == 1) ? hStdOut : hStdErr, p, size, &k, nullptr);
@@ -172,7 +181,9 @@ private:
}
if (m_logFile.is_open()) {
strip_colors(p, size);
if (CONSOLE_COLORS) {
strip_colors(p, size);
}
if (severity == 1) {
m_logFile.write("NOTICE ", 8);
+2
View File
@@ -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 {
@@ -371,6 +372,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)
{
switch (value) {
+12 -2
View File
@@ -16,13 +16,14 @@
*/
#include "common.h"
#include "crypto.h"
#include "p2pool.h"
#include "stratum_server.h"
#include "p2p_server.h"
static void usage()
{
printf(
printf("P2Pool %s\n"
"\nUsage:\n\n" \
"--wallet Wallet address to mine to. Subaddresses and integrated addresses are not supported!\n"
"--host IP address of your Monero node, default is 127.0.0.1\n"
@@ -32,12 +33,17 @@ static void usage()
"--p2p Comma-separated list of IP:port for p2p server to listen on\n"
"--addpeers Comma-separated list of IP:port of other p2pool nodes to connect to\n"
"--light-mode Don't allocate RandomX dataset, saves 2GB of RAM\n"
"--loglevel Verbosity of the log, integer number between 0 and 5\n"
"--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"
"--no-cache Disable p2pool.cache\n"
"--no-color Disable colors in console output\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,
#ifdef _WIN32
"p2pool.exe"
#else
@@ -68,10 +74,14 @@ int main(int argc, char* argv[])
int result;
memory_tracking_start();
p2pool::init_crypto_cache();
{
p2pool::p2pool pool(argc, argv);
result = pool.run();
}
p2pool::destroy_crypto_cache();
memory_tracking_stop();
return result;
+40 -10
View File
@@ -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,23 @@ 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
void memory_tracking_start() {}
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
+1 -2
View File
@@ -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
+186 -57
View File
@@ -42,13 +42,14 @@ namespace p2pool {
P2PServer::P2PServer(p2pool* pool)
: TCPServer(P2PClient::allocate)
, m_pool(pool)
, m_cache(new BlockCache())
, m_cache(pool->params().m_blockCache ? new BlockCache() : nullptr)
, m_cacheLoaded(false)
, m_initialPeerList(pool->params().m_p2pPeerList)
, m_rd{}
, m_rng(m_rd())
, m_block(new PoolBlock())
, m_timer{}
, m_timerCounter(0)
, m_peerId(m_rng())
, m_peerListLastSaved(0)
{
@@ -93,6 +94,7 @@ P2PServer::P2PServer(p2pool* pool)
P2PServer::~P2PServer()
{
uv_timer_stop(&m_timer);
uv_close(reinterpret_cast<uv_handle_t*>(&m_timer), nullptr);
uv_close(reinterpret_cast<uv_handle_t*>(&m_broadcastAsync), nullptr);
shutdown_tcp();
@@ -102,14 +104,11 @@ P2PServer::~P2PServer()
uv_mutex_destroy(&m_peerListLock);
uv_mutex_destroy(&m_broadcastLock);
uv_mutex_destroy(&m_missingBlockRequestsLock);
clear_cached_blocks();
uv_rwlock_destroy(&m_cachedBlocksLock);
delete m_block;
for (auto it : m_cachedBlocks) {
delete it.second;
}
delete m_cache;
}
@@ -124,6 +123,16 @@ void P2PServer::add_cached_block(const PoolBlock& block)
m_cachedBlocks.insert({ new_block->m_sidechainId, new_block });
}
void P2PServer::clear_cached_blocks()
{
WriteLock lock(m_cachedBlocksLock);
for (auto it : m_cachedBlocks) {
delete it.second;
}
m_cachedBlocks.clear();
}
void P2PServer::store_in_cache(const PoolBlock& block)
{
if (m_cache && block.m_verified && !block.m_invalid) {
@@ -159,12 +168,6 @@ void P2PServer::on_connect_failed(bool is_v6, const raw_ip& ip, int port)
void P2PServer::update_peer_connections()
{
std::vector<Peer> peer_list;
{
MutexLock lock(m_peerListLock);
peer_list = m_peerList;
}
const time_t cur_time = time(nullptr);
const time_t last_updated = m_pool->side_chain().last_updated();
@@ -173,13 +176,14 @@ void P2PServer::update_peer_connections()
MutexLock lock(m_clientsListLock);
connected_clients.reserve(m_numConnections);
for (P2PClient* client = static_cast<P2PClient*>(m_connectedClientsList->m_next); client != m_connectedClientsList; client = static_cast<P2PClient*>(client->m_next)) {
connected_clients.emplace_back(client->m_addr);
bool disconnected = false;
const int timeout = client->m_handshakeComplete ? 300 : 10;
if (cur_time >= client->m_lastAlive + timeout) {
const uint64_t idle_time = static_cast<uint64_t>(cur_time - client->m_lastAlive);
LOGWARN(5, "peer " << static_cast<char*>(client->m_addrString) << " has been idle for " << idle_time << " seconds, disconnecting");
client->close();
disconnected = true;
}
if (client->m_handshakeComplete && client->m_lastBroadcastTimestamp) {
@@ -193,13 +197,37 @@ void P2PServer::update_peer_connections()
client->ban(DEFAULT_BAN_TIME);
remove_peer_from_list(client);
client->close();
disconnected = true;
}
}
if (!disconnected) {
connected_clients.emplace_back(client->m_addr);
}
}
}
// Try to have at least 8 outgoing connections
for (uint32_t i = m_numConnections - m_numIncomingConnections; (i < 8) && !peer_list.empty();) {
std::vector<Peer> peer_list;
{
MutexLock lock(m_peerListLock);
if ((m_timerCounter % 30) == 1) {
// Update last seen time for currently connected peers
for (Peer& p : m_peerList) {
if (std::find_if(connected_clients.begin(), connected_clients.end(), [&p](const raw_ip& addr) { return p.m_addr == addr; }) != connected_clients.end()) {
p.m_lastSeen = cur_time;
}
}
// Remove all peers that weren't seen for more than 1 hour
m_peerList.erase(std::remove_if(m_peerList.begin(), m_peerList.end(), [cur_time](const Peer& p) { return p.m_lastSeen + 3600 < cur_time; }), m_peerList.end());
}
peer_list = m_peerList;
}
// Try to have at least 10 outgoing connections
for (uint32_t i = m_numConnections - m_numIncomingConnections; (i < 10) && !peer_list.empty();) {
const uint64_t k = get_random64() % peer_list.size();
const Peer& peer = peer_list[k];
@@ -220,6 +248,10 @@ void P2PServer::update_peer_connections()
}
peer_list.pop_back();
}
if ((m_numConnections == 0) && ((m_timerCounter % 30) == 0)) {
LOGERR(1, "no connections to other p2pool nodes, check your monerod/p2pool/network/firewall setup!!!");
}
}
void P2PServer::update_peer_list()
@@ -237,13 +269,18 @@ void P2PServer::update_peer_list()
// Send peer list requests at random intervals (60-120 seconds)
client->m_nextPeerListRequest = cur_time + 60 + (get_random64() % 61);
send(client,
const bool result = send(client,
[](void* buf)
{
LOGINFO(5, "sending PEER_LIST_REQUEST");
*reinterpret_cast<uint8_t*>(buf) = static_cast<uint8_t>(MessageId::PEER_LIST_REQUEST);
return 1;
});
if (result) {
client->m_lastPeerListRequestTime = std::chrono::system_clock::now();
++client->m_peerListPendingRequests;
}
}
}
}
@@ -279,7 +316,7 @@ void P2PServer::save_peer_list_async()
});
if (err) {
LOGERR(1, "update_peer_list: uv_queue_work failed, error " << uv_err_name(err));
LOGERR(1, "save_peer_list_async: uv_queue_work failed, error " << uv_err_name(err));
delete work;
}
}
@@ -417,8 +454,6 @@ void P2PServer::load_peer_list()
}
p.m_isV6 = true;
memcpy(p.m_addr.data, &addr6.sin6_addr, sizeof(in6_addr));
p.m_port = port;
p.m_numFailedConnections = 0;
}
else {
sockaddr_in addr4;
@@ -432,8 +467,6 @@ void P2PServer::load_peer_list()
p.m_addr.data[10] = 0xFF;
p.m_addr.data[11] = 0xFF;
memcpy(p.m_addr.data + 12, &addr4.sin_addr, sizeof(in_addr));
p.m_port = port;
p.m_numFailedConnections = 0;
}
bool already_added = false;
@@ -444,6 +477,10 @@ void P2PServer::load_peer_list()
}
}
p.m_port = port;
p.m_numFailedConnections = 0;
p.m_lastSeen = time(nullptr);
if (!already_added && !is_banned(p.m_addr)) {
m_peerList.push_back(p);
}
@@ -454,18 +491,21 @@ void P2PServer::load_peer_list()
void P2PServer::update_peer_in_list(bool is_v6, const raw_ip& ip, int port)
{
const time_t cur_time = time(nullptr);
MutexLock lock(m_peerListLock);
for (Peer& p : m_peerList) {
if ((p.m_isV6 == is_v6) && (p.m_addr == ip)) {
p.m_port = port;
p.m_numFailedConnections = 0;
p.m_lastSeen = cur_time;
return;
}
}
if (!is_banned(ip)) {
m_peerList.emplace_back(Peer{ is_v6, ip, port, 0 });
m_peerList.emplace_back(Peer{ is_v6, ip, port, 0, cur_time });
}
}
@@ -602,18 +642,19 @@ void P2PServer::on_broadcast()
uint8_t* p = p0;
bool send_pruned = true;
{
ReadLock lock(client->m_broadcastedHashesLock);
for (const hash& id : data->ancestor_hashes) {
if (client->m_broadcastedHashes.find(id) == client->m_broadcastedHashes.end()) {
send_pruned = false;
break;
}
const hash* a = client->m_broadcastedHashes;
const hash* b = client->m_broadcastedHashes + array_size(&P2PClient::m_broadcastedHashes);
for (const hash& id : data->ancestor_hashes) {
if (std::find(a, b, id) == b) {
send_pruned = false;
break;
}
}
if (send_pruned) {
LOGINFO(5, "sending BLOCK_BROADCAST (pruned) to " << log::Gray() << static_cast<char*>(client->m_addrString));
LOGINFO(6, "sending BLOCK_BROADCAST (pruned) to " << log::Gray() << static_cast<char*>(client->m_addrString));
*(p++) = static_cast<uint8_t>(MessageId::BLOCK_BROADCAST);
*reinterpret_cast<uint32_t*>(p) = static_cast<uint32_t>(data->pruned_blob.size());
@@ -647,14 +688,43 @@ uint64_t P2PServer::get_random64()
void P2PServer::print_status()
{
const int64_t uptime = time(nullptr) - m_pool->start_time();
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;
char buf[log::Stream::BUF_SIZE + 1];
log::Stream s1(buf);
if (d > 0) {
s1 << d << "d ";
}
s1 << h << "h " << m << "m " << s << 's';
LOGINFO(0, "status" <<
"\nConnections = " << m_numConnections << " (" << m_numIncomingConnections << " incoming)" <<
"\nPeer list size = " << m_peerList.size()
"\nPeer list size = " << m_peerList.size() <<
"\nUptime = " << log::const_buf(buf, s1.m_pos)
);
}
void P2PServer::show_peers()
{
MutexLock lock(m_clientsListLock);
for (P2PClient* client = static_cast<P2PClient*>(m_connectedClientsList->m_next); client != m_connectedClientsList; client = static_cast<P2PClient*>(client->m_next)) {
if (client->m_listenPort >= 0) {
LOGINFO(0, (client->m_isIncoming ? "I " : "O ") << client->m_pingTime << " ms\t" << static_cast<char*>(client->m_addrString));
}
}
}
void P2PServer::on_timer()
{
++m_timerCounter;
if (!m_initialPeerList.empty()) {
connect_to_peers(m_initialPeerList);
m_initialPeerList.clear();
@@ -665,6 +735,7 @@ void P2PServer::on_timer()
update_peer_list();
save_peer_list_async();
update_peer_connections();
check_zmq();
}
void P2PServer::flush_cache()
@@ -765,6 +836,21 @@ void P2PServer::download_missing_blocks()
}
}
void P2PServer::check_zmq()
{
if ((m_timerCounter % 30) != 0) {
return;
}
const time_t cur_time = time(nullptr);
const time_t last_active = m_pool->zmq_last_active();
if (cur_time >= last_active + 300) {
const uint64_t dt = static_cast<uint64_t>(cur_time - last_active);
LOGERR(1, "no ZMQ messages received from monerod in the last " << dt << " seconds, check your monerod/p2pool/network/firewall setup!!!");
}
}
P2PServer::P2PClient::P2PClient()
: m_peerId(0)
, m_expectedMessage(MessageId::HANDSHAKE_CHALLENGE)
@@ -774,16 +860,18 @@ P2PServer::P2PClient::P2PClient()
, m_handshakeInvalid(false)
, m_listenPort(-1)
, m_nextPeerListRequest(0)
, m_lastPeerListRequestTime{}
, m_peerListPendingRequests(0)
, m_pingTime(0)
, m_lastAlive(0)
, m_lastBroadcastTimestamp(0)
, m_lastBlockrequestTimestamp(0)
, m_broadcastedHashes{}
{
uv_rwlock_init_checked(&m_broadcastedHashesLock);
}
P2PServer::P2PClient::~P2PClient()
{
uv_rwlock_destroy(&m_broadcastedHashesLock);
}
void P2PServer::P2PClient::reset()
@@ -798,12 +886,17 @@ void P2PServer::P2PClient::reset()
m_handshakeInvalid = false;
m_listenPort = -1;
m_nextPeerListRequest = 0;
m_lastPeerListRequestTime = {};
m_peerListPendingRequests = 0;
m_pingTime = 0;
m_lastAlive = 0;
m_lastBroadcastTimestamp = 0;
m_lastBlockrequestTimestamp = 0;
WriteLock lock(m_broadcastedHashesLock);
m_broadcastedHashes.clear();
for (hash& h : m_broadcastedHashes) {
h = {};
}
m_broadcastedHashesIndex = 0;
}
bool P2PServer::P2PClient::on_connect()
@@ -859,6 +952,13 @@ bool P2PServer::P2PClient::on_read(char* data, uint32_t size)
switch (id)
{
case MessageId::HANDSHAKE_CHALLENGE:
if (m_handshakeComplete) {
LOGWARN(4, "peer " << log::Gray() << static_cast<char*>(m_addrString) << log::NoColor() << " sent an unexpected HANDSHAKE_CHALLENGE");
ban(DEFAULT_BAN_TIME);
server->remove_peer_from_list(this);
return false;
}
LOGINFO(5, "peer " << log::Gray() << static_cast<char*>(m_addrString) << log::NoColor() << " sent HANDSHAKE_CHALLENGE");
if (bytes_left >= 1 + CHALLENGE_SIZE + sizeof(uint64_t)) {
@@ -873,6 +973,13 @@ bool P2PServer::P2PClient::on_read(char* data, uint32_t size)
break;
case MessageId::HANDSHAKE_SOLUTION:
if (m_handshakeComplete) {
LOGWARN(4, "peer " << log::Gray() << static_cast<char*>(m_addrString) << log::NoColor() << " sent an unexpected HANDSHAKE_SOLUTION");
ban(DEFAULT_BAN_TIME);
server->remove_peer_from_list(this);
return false;
}
LOGINFO(5, "peer " << log::Gray() << static_cast<char*>(m_addrString) << log::NoColor() << " sent HANDSHAKE_SOLUTION");
if (bytes_left >= 1 + HASH_SIZE + CHALLENGE_SIZE) {
@@ -928,7 +1035,7 @@ bool P2PServer::P2PClient::on_read(char* data, uint32_t size)
break;
case MessageId::BLOCK_BROADCAST:
LOGINFO(5, "peer " << log::Gray() << static_cast<char*>(m_addrString) << log::NoColor() << " sent BLOCK_BROADCAST");
LOGINFO(6, "peer " << log::Gray() << static_cast<char*>(m_addrString) << log::NoColor() << " sent BLOCK_BROADCAST");
if (bytes_left >= 1 + sizeof(uint32_t)) {
const uint32_t block_size = *reinterpret_cast<uint32_t*>(buf + 1);
@@ -957,10 +1064,17 @@ bool P2PServer::P2PClient::on_read(char* data, uint32_t size)
break;
case MessageId::PEER_LIST_RESPONSE:
if (m_peerListPendingRequests <= 0) {
LOGWARN(4, "peer " << log::Gray() << static_cast<char*>(m_addrString) << log::NoColor() << " sent an unexpected PEER_LIST_RESPONSE");
ban(DEFAULT_BAN_TIME);
server->remove_peer_from_list(this);
return false;
}
LOGINFO(5, "peer " << log::Gray() << static_cast<char*>(m_addrString) << log::NoColor() << " sent PEER_LIST_RESPONSE");
if (bytes_left >= 2) {
const uint8_t num_peers = buf[1];
const uint32_t num_peers = buf[1];
if (num_peers > PEER_LIST_RESPONSE_MAX_PEERS) {
LOGWARN(5, "peer " << log::Gray() << static_cast<char*>(m_addrString) << log::NoColor() << " sent too long peer list (" << num_peers << ')');
ban(DEFAULT_BAN_TIME);
@@ -968,8 +1082,13 @@ bool P2PServer::P2PClient::on_read(char* data, uint32_t size)
return false;
}
if (bytes_left >= 2u + num_peers * 19) {
bytes_read = 2u + num_peers * 19;
if (bytes_left >= 2u + num_peers * 19u) {
bytes_read = 2u + num_peers * 19u;
using namespace std::chrono;
m_pingTime = duration_cast<milliseconds>(system_clock::now() - m_lastPeerListRequestTime).count();
--m_peerListPendingRequests;
if (!on_peer_list_response(buf + 1)) {
ban(DEFAULT_BAN_TIME);
server->remove_peer_from_list(this);
@@ -1406,10 +1525,7 @@ bool P2PServer::P2PClient::on_block_broadcast(const uint8_t* buf, uint32_t size)
return false;
}
{
WriteLock lock2(m_broadcastedHashesLock);
m_broadcastedHashes.insert(server->m_block->m_sidechainId);
}
m_broadcastedHashes[m_broadcastedHashesIndex.fetch_add(1) % array_size(&P2PClient::m_broadcastedHashes)] = server->m_block->m_sidechainId;
const MinerData& miner_data = server->m_pool->miner_data();
@@ -1437,8 +1553,7 @@ bool P2PServer::P2PClient::on_block_broadcast(const uint8_t* buf, uint32_t size)
}
else if (peer_height > our_height) {
if (peer_height >= our_height + 2) {
const int level = (peer_height >= our_height + 3) ? 3 : 4;
LOGWARN(level , "peer " << static_cast<char*>(m_addrString) << " is ahead on mainchain (height " << peer_height << ", your height " << our_height << "). Is your monerod stuck or lagging?");
LOGWARN(3, "peer " << static_cast<char*>(m_addrString) << " is ahead on mainchain (height " << peer_height << ", your height " << our_height << "). Is your monerod stuck or lagging?");
}
}
else {
@@ -1463,23 +1578,28 @@ bool P2PServer::P2PClient::on_peer_list_request(const uint8_t*)
MutexLock lock(server->m_clientsListLock);
// Send every 4th peer on average, selected at random
const uint32_t n = server->m_numConnections;
const uint32_t peers_to_send_target = std::min<uint32_t>(PEER_LIST_RESPONSE_MAX_PEERS, std::max<uint32_t>(1, n / 4));
const uint32_t peers_to_send_target = std::min<uint32_t>(PEER_LIST_RESPONSE_MAX_PEERS, std::max<uint32_t>(1, server->m_numConnections / 4));
uint32_t n = 0;
for (P2PClient* client = static_cast<P2PClient*>(server->m_connectedClientsList->m_next); client != server->m_connectedClientsList; client = static_cast<P2PClient*>(client->m_next)) {
if (client->m_listenPort < 0) {
if ((client->m_listenPort < 0) || (client->m_addr == m_addr)) {
continue;
}
uint64_t hi;
umul128(server->get_random64(), n, &hi);
const Peer p{ client->m_isV6, client->m_addr, client->m_listenPort, 0, 0 };
++n;
if ((hi < peers_to_send_target) && (client->m_addr != m_addr)) {
peers[num_selected_peers++] = { client->m_isV6, client->m_addr, client->m_listenPort, 0 };
// Use https://en.wikipedia.org/wiki/Reservoir_sampling algorithm
if (num_selected_peers < peers_to_send_target) {
peers[num_selected_peers++] = p;
continue;
}
if (num_selected_peers >= PEER_LIST_RESPONSE_MAX_PEERS) {
break;
}
uint64_t k;
umul128(server->get_random64(), n, &k);
if (k < peers_to_send_target) {
peers[k] = p;
}
}
}
@@ -1513,6 +1633,7 @@ bool P2PServer::P2PClient::on_peer_list_request(const uint8_t*)
bool P2PServer::P2PClient::on_peer_list_response(const uint8_t* buf) const
{
P2PServer* server = static_cast<P2PServer*>(m_owner);
const time_t cur_time = time(nullptr);
MutexLock lock(server->m_peerListLock);
@@ -1524,20 +1645,28 @@ bool P2PServer::P2PClient::on_peer_list_response(const uint8_t* buf) const
memcpy(ip.data, buf, sizeof(ip.data));
buf += sizeof(ip.data);
// Fill in default bytes for IPv4 addresses
if (!is_v6) {
memset(ip.data, 0, 10);
ip.data[10] = 0xFF;
ip.data[11] = 0xFF;
}
int port = 0;
memcpy(&port, buf, 2);
buf += 2;
bool already_added = false;
for (const Peer& p : server->m_peerList) {
for (Peer& p : server->m_peerList) {
if ((p.m_isV6 == is_v6) && (p.m_addr == ip)) {
already_added = true;
p.m_lastSeen = cur_time;
break;
}
}
if (!already_added && !server->is_banned(ip)) {
server->m_peerList.emplace_back(Peer{ is_v6, ip, port, 0 });
server->m_peerList.emplace_back(Peer{ is_v6, ip, port, 0, cur_time });
}
}
+13 -4
View File
@@ -19,7 +19,6 @@
#include "tcp_server.h"
#include <random>
#include <unordered_map>
namespace p2pool {
@@ -49,6 +48,7 @@ public:
~P2PServer();
void add_cached_block(const PoolBlock& block);
void clear_cached_blocks();
void store_in_cache(const PoolBlock& block);
void connect_to_peers(const std::string& peer_list);
@@ -108,13 +108,18 @@ public:
bool m_handshakeComplete;
bool m_handshakeInvalid;
int m_listenPort;
time_t m_nextPeerListRequest;
std::chrono::system_clock::time_point m_lastPeerListRequestTime;
int m_peerListPendingRequests;
int64_t m_pingTime;
time_t m_lastAlive;
time_t m_lastBroadcastTimestamp;
time_t m_lastBlockrequestTimestamp;
uv_rwlock_t m_broadcastedHashesLock;
std::set<hash> m_broadcastedHashes;
hash m_broadcastedHashes[8];
std::atomic<uint32_t> m_broadcastedHashesIndex{ 0 };
};
void broadcast(const PoolBlock& block);
@@ -122,6 +127,7 @@ public:
uint64_t get_peerId() const { return m_peerId; }
void print_status() override;
void show_peers();
private:
p2pool* m_pool;
@@ -130,7 +136,7 @@ private:
std::string m_initialPeerList;
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(); }
@@ -138,6 +144,7 @@ private:
void flush_cache();
void download_missing_blocks();
void check_zmq();
void update_peer_connections();
void update_peer_list();
void save_peer_list_async();
@@ -155,6 +162,7 @@ private:
PoolBlock* m_block;
uv_timer_t m_timer;
uint32_t m_timerCounter;
uint64_t m_peerId;
@@ -166,6 +174,7 @@ private:
raw_ip m_addr;
int m_port;
uint32_t m_numFailedConnections;
time_t m_lastSeen;
};
std::vector<Peer> m_peerList;
+17 -5
View File
@@ -49,7 +49,11 @@ p2pool::p2pool(int argc, char* argv[])
, m_params(new Params(argc, argv))
, m_updateSeed(true)
, m_submitBlockData{}
, m_zmqLastActive(0)
, m_startTime(time(nullptr))
{
LOGINFO(1, log::LightCyan() << VERSION);
if (!m_params->m_wallet.valid()) {
LOGERR(1, "Invalid wallet address. Try \"p2pool --help\".");
panic();
@@ -103,7 +107,7 @@ p2pool::p2pool(int argc, char* argv[])
uv_mutex_init_checked(&m_foundBlocksLock);
uv_mutex_init_checked(&m_submitBlockDataLock);
m_api = m_params->m_apiPath.empty() ? nullptr : new p2pool_api(m_params->m_apiPath);
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);
@@ -171,6 +175,8 @@ void p2pool::handle_tx(TxMempoolData& tx)
#if TEST_MEMPOOL_PICKING_ALGORITHM
m_blockTemplate->update(m_minerData, *m_mempool, &m_params->m_wallet);
#endif
m_zmqLastActive = time(nullptr);
}
void p2pool::handle_miner_data(MinerData& data)
@@ -224,6 +230,8 @@ void p2pool::handle_miner_data(MinerData& data)
else {
update_block_template();
}
m_zmqLastActive = time(nullptr);
}
const char* BLOCK_FOUND = "\n\
@@ -286,6 +294,8 @@ void p2pool::handle_chain_main(ChainMain& data, const char* extra)
}
api_update_network_stats();
m_zmqLastActive = time(nullptr);
}
void p2pool::submit_block_async(uint32_t template_id, uint32_t nonce, uint32_t extra_nonce)
@@ -370,6 +380,10 @@ void p2pool::submit_block() const
request = "{\"jsonrpc\":\"2.0\",\"id\":\"0\",\"method\":\"submit_block\",\"params\":[\"";
const uint32_t template_id = submit_data.template_id;
const uint32_t nonce = submit_data.nonce;
const uint32_t extra_nonce = submit_data.extra_nonce;
for (size_t i = 0; i < submit_data.blob.size(); ++i) {
char buf[16];
@@ -389,10 +403,6 @@ void p2pool::submit_block() const
}
request.append("\"]}");
const uint32_t template_id = submit_data.template_id;
const uint32_t nonce = submit_data.nonce;
const uint32_t extra_nonce = submit_data.extra_nonce;
JSONRPCRequest::call(m_params->m_host.c_str(), m_params->m_rpcPort, request.c_str(),
[height, diff, template_id, nonce, extra_nonce, is_external](const char* data, size_t size)
{
@@ -1055,6 +1065,8 @@ void p2pool::api_update_stats_mod()
void p2pool::api_update_block_found(const ChainMain* data)
{
clear_crypto_cache();
if (!m_api) {
return;
}
+12 -5
View File
@@ -19,7 +19,6 @@
#include "uv_util.h"
#include <map>
#include <unordered_map>
namespace p2pool {
@@ -49,6 +48,8 @@ public:
SideChain& side_chain() { return *m_sideChain; }
const MinerData& miner_data() const { 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);
static uint64_t get_seed_height(uint64_t height);
@@ -76,6 +77,9 @@ public:
bool get_difficulty_at_height(uint64_t height, difficulty_type& diff);
time_t zmq_last_active() const { return m_zmqLastActive; }
time_t start_time() const { return m_startTime; }
private:
p2pool(const p2pool&) = delete;
p2pool(p2pool&&) = delete;
@@ -100,7 +104,7 @@ private:
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;
enum { TIMESTAMP_WINDOW = 60 };
bool get_timestamps(uint64_t (&timestamps)[TIMESTAMP_WINDOW]) const;
@@ -153,9 +157,9 @@ private:
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;
};
@@ -165,6 +169,9 @@ private:
uv_async_t m_submitBlockAsync;
uv_async_t m_blockTemplateAsync;
uv_async_t m_stopAsync;
time_t m_zmqLastActive;
time_t m_startTime;
};
} // namespace p2pool
+8 -2
View File
@@ -28,7 +28,7 @@ static constexpr char log_category_prefix[] = "P2Pool API ";
namespace p2pool {
p2pool_api::p2pool_api(const std::string& api_path) : m_apiPath(api_path)
p2pool_api::p2pool_api(const std::string& api_path, const bool local_stats): m_apiPath(api_path)
{
if (m_apiPath.empty()) {
LOGERR(1, "api path is empty");
@@ -60,9 +60,14 @@ p2pool_api::p2pool_api(const std::string& api_path) : m_apiPath(api_path)
m_networkPath = m_apiPath + "network/";
m_poolPath = m_apiPath + "pool/";
m_localPath = m_apiPath + "local/";
create_dir(m_networkPath);
create_dir(m_poolPath);
if (local_stats) {
create_dir(m_localPath);
}
}
p2pool_api::~p2pool_api()
@@ -109,6 +114,7 @@ void p2pool_api::dump_to_file_async_internal(const Category& category, const cha
case Category::GLOBAL: path = m_apiPath + filename; break;
case Category::NETWORK: path = m_networkPath + filename; break;
case Category::POOL: path = m_poolPath + filename; break;
case Category::LOCAL: path = m_localPath + filename; break;
}
{
@@ -121,7 +127,7 @@ 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);
+4 -3
View File
@@ -18,20 +18,20 @@
#pragma once
#include "uv_util.h"
#include <unordered_map>
namespace p2pool {
class p2pool_api
{
public:
explicit p2pool_api(const std::string& api_path);
p2pool_api(const std::string& api_path, const bool local_stats);
~p2pool_api();
enum class Category {
GLOBAL,
NETWORK,
POOL,
LOCAL,
};
void on_stop();
@@ -80,9 +80,10 @@ private:
std::string m_apiPath;
std::string m_networkPath;
std::string m_poolPath;
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;
};
+12
View File
@@ -69,6 +69,18 @@ Params::Params(int argc, char* argv[])
if ((strcmp(argv[i], "--data-api") == 0) && (i + 1 < argc)) {
m_apiPath = argv[++i];
}
if (strcmp(argv[i], "--stratum-api") == 0) {
m_localStats = true;
}
if (strcmp(argv[i], "--no-cache") == 0) {
m_blockCache = false;
}
if (strcmp(argv[i], "--no-color") == 0) {
log::CONSOLE_COLORS = false;
}
}
if (m_stratumAddresses.empty()) {
+2
View File
@@ -37,6 +37,8 @@ struct Params
std::string m_p2pPeerList;
std::string m_config;
std::string m_apiPath;
bool m_localStats = false;
bool m_blockCache = true;
};
} // namespace p2pool
+4 -4
View File
@@ -54,7 +54,7 @@ RandomX_Hasher::RandomX_Hasher(p2pool* pool)
const randomx_flags flags = randomx_get_flags();
for (size_t i = 0; i < array_size(m_cache); ++i) {
for (size_t i = 0; i < array_size(&RandomX_Hasher::m_cache); ++i) {
m_cache[i] = randomx_alloc_cache(flags | RANDOMX_FLAG_LARGE_PAGES);
if (!m_cache[i]) {
LOGWARN(1, "couldn't allocate RandomX cache using large pages");
@@ -70,7 +70,7 @@ RandomX_Hasher::RandomX_Hasher(p2pool* pool)
uv_rwlock_init_checked(&m_datasetLock);
uv_rwlock_init_checked(&m_cacheLock);
for (size_t i = 0; i < array_size(m_vm); ++i) {
for (size_t i = 0; i < array_size(&RandomX_Hasher::m_vm); ++i) {
uv_mutex_init_checked(&m_vm[i].mutex);
m_vm[i].vm = nullptr;
}
@@ -91,7 +91,7 @@ RandomX_Hasher::~RandomX_Hasher()
uv_rwlock_destroy(&m_datasetLock);
uv_rwlock_destroy(&m_cacheLock);
for (size_t i = 0; i < array_size(m_vm); ++i) {
for (size_t i = 0; i < array_size(&RandomX_Hasher::m_vm); ++i) {
{
MutexLock lock(m_vm[i].mutex);
if (m_vm[i].vm) {
@@ -105,7 +105,7 @@ RandomX_Hasher::~RandomX_Hasher()
randomx_release_dataset(m_dataset);
}
for (size_t i = 0; i < array_size(m_cache); ++i) {
for (size_t i = 0; i < array_size(&RandomX_Hasher::m_cache); ++i) {
if (m_cache[i]) {
randomx_release_cache(m_cache[i]);
}
+3 -3
View File
@@ -44,8 +44,8 @@ private:
struct ThreadSafeVM
{
uv_mutex_t mutex;
randomx_vm* vm;
uv_mutex_t mutex{};
randomx_vm* vm = nullptr;
};
p2pool* m_pool;
@@ -62,7 +62,7 @@ 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;
+14 -2
View File
@@ -355,6 +355,12 @@ bool SideChain::block_seen(const PoolBlock& block)
return !m_seenBlocks.insert(block.m_sidechainId).second;
}
void SideChain::unsee_block(const PoolBlock& block)
{
MutexLock lock(m_sidechainLock);
m_seenBlocks.erase(block.m_sidechainId);
}
extern const char* BLOCK_FOUND;
bool SideChain::add_external_block(PoolBlock& block, std::vector<hash>& missing_blocks)
@@ -407,13 +413,15 @@ bool SideChain::add_external_block(PoolBlock& block, std::vector<hash>& missing_
hash seed;
if (!m_pool->get_seed(block.m_txinGenHeight, seed)) {
LOGWARN(3, "add_external_block: couldn't get seed hash for mainchain height " << block.m_txinGenHeight);
unsee_block(block);
return false;
}
hash pow_hash;
if (!block.get_pow_hash(m_pool->hasher(), seed, pow_hash)) {
LOGWARN(3, "add_external_block: couldn't get PoW hash for height = " << block.m_sidechainHeight << ", mainchain height " << block.m_txinGenHeight);
return false;
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
@@ -1552,6 +1560,10 @@ void SideChain::prune_old_blocks()
if (num_blocks_pruned) {
LOGINFO(4, "pruned " << num_blocks_pruned << " old blocks at heights <= " << h);
// 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();
}
}
+4 -5
View File
@@ -19,8 +19,6 @@
#include "uv_util.h"
#include <map>
#include <unordered_map>
#include <unordered_set>
namespace p2pool {
@@ -47,6 +45,7 @@ public:
void fill_sidechain_data(PoolBlock& block, Wallet* w, const hash& txkeySec, std::vector<MinerShare>& shares);
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);
@@ -97,9 +96,9 @@ private:
mutable uv_mutex_t m_sidechainLock;
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;
unordered_set<hash> m_seenBlocks;
unordered_map<hash, time_t> m_seenWallets;
std::vector<DifficultyData> m_difficultyData;
+174 -48
View File
@@ -21,6 +21,7 @@
#include "p2pool.h"
#include "side_chain.h"
#include "params.h"
#include "p2pool_api.h"
static constexpr char log_category_prefix[] = "StratumServer ";
@@ -48,6 +49,7 @@ StratumServer::StratumServer(p2pool* pool)
, m_hashrateDataTail_24h(0)
, m_cumulativeFoundSharesDiff(0.0)
, m_totalFoundShares(0)
, m_apiLastUpdateTime(0)
{
m_hashrateData[0] = { time(nullptr), 0 };
@@ -174,6 +176,28 @@ void StratumServer::on_block(const BlockTemplate& block)
}
}
bool StratumServer::get_custom_user(const char* s, std::string& user)
{
user.clear();
// Find first of '+' or '.', drop non-printable characters
while (s && (user.length() < 64)) {
const char c = *s;
if (!c) {
break;
}
if ((c == '+') || (c == '.')) {
break;
}
// Limit to printable ASCII characters
if (c >= ' ' && c <= '~') {
user += c;
}
++s;
}
return !user.empty();
}
bool StratumServer::get_custom_diff(const char* s, difficulty_type& diff)
{
const char* diff_str = nullptr;
@@ -223,13 +247,17 @@ 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)) {
LOGINFO(5, "client " << log::Gray() << static_cast<char*>(client->m_addrString) << " set custom user " << client->m_customUser);
}
uint32_t job_id;
{
MutexLock lock(client->m_jobsLock);
job_id = client->m_perConnectionJobId++;
StratumClient::SavedJob& saved_job = client->m_jobs[job_id % array_size(client->m_jobs)];
StratumClient::SavedJob& saved_job = client->m_jobs[job_id % array_size(&StratumClient::m_jobs)];
saved_job.job_id = job_id;
saved_job.extra_nonce = extra_nonce;
saved_job.template_id = template_id;
@@ -307,7 +335,7 @@ bool StratumServer::on_submit(StratumClient* client, uint32_t id, const char* jo
{
MutexLock lock(client->m_jobsLock);
const StratumClient::SavedJob& saved_job = client->m_jobs[job_id % array_size(client->m_jobs)];
const StratumClient::SavedJob& saved_job = client->m_jobs[job_id % array_size(&StratumClient::m_jobs)];
if (saved_job.job_id == job_id) {
template_id = saved_job.template_id;
extra_nonce = saved_job.extra_nonce;
@@ -317,6 +345,27 @@ bool StratumServer::on_submit(StratumClient* client, uint32_t id, const char* jo
}
if (found) {
BlockTemplate& block = m_pool->block_template();
difficulty_type mainchain_diff, sidechain_diff;
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)
{
log::Stream s(reinterpret_cast<char*>(buf));
s << "{\"id\":" << id << ",\"jsonrpc\":\"2.0\",\"error\":{\"message\":\"Stale share\"}}\n";
return s.m_pos;
});
}
if (mainchain_diff.check_pow(resultHash)) {
const std::string& s = client->m_customUser;
LOGINFO(0, log::Green() << "client " << static_cast<char*>(client->m_addrString) << (!s.empty() ? " user " : "") << s << " found a mainchain block, submitting it");
m_pool->submit_block_async(template_id, nonce, extra_nonce);
block.update_tx_keys();
}
SubmittedShare* share;
{
@@ -334,6 +383,7 @@ bool StratumServer::on_submit(StratumClient* client, uint32_t id, const char* jo
share->m_req.data = share;
share->m_server = this;
share->m_client = client;
share->m_clientAddr = client->m_addr;
share->m_clientResetCounter = client->m_resetCounter.load();
share->m_rpcId = client->m_rpcId;
share->m_id = id;
@@ -342,13 +392,23 @@ bool StratumServer::on_submit(StratumClient* client, uint32_t id, const char* jo
share->m_extraNonce = extra_nonce;
share->m_target = target;
share->m_resultHash = resultHash;
share->m_sidechainDifficulty = sidechain_diff;
// If this share is below sidechain difficulty, process it in this thread because it'll be quick
if (!share->m_sidechainDifficulty.check_pow(share->m_resultHash)) {
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;
@@ -375,7 +435,7 @@ uint64_t StratumServer::get_random64()
void StratumServer::print_status()
{
update_hashrate_data(0);
update_hashrate_data(0, time(nullptr));
print_stratum_status();
}
@@ -485,7 +545,7 @@ void StratumServer::on_blobs_ready()
job_id = client->m_perConnectionJobId++;
StratumClient::SavedJob& saved_job = client->m_jobs[job_id % array_size(client->m_jobs)];
StratumClient::SavedJob& saved_job = client->m_jobs[job_id % array_size(&StratumClient::m_jobs)];
saved_job.job_id = job_id;
saved_job.extra_nonce = extra_nonce;
saved_job.template_id = data->m_templateId;
@@ -528,12 +588,9 @@ void StratumServer::on_blobs_ready()
LOGINFO(3, "sent new job to " << extra_nonce << '/' << numClientsProcessed << " clients");
}
void StratumServer::update_hashrate_data(uint64_t target)
void StratumServer::update_hashrate_data(uint64_t hashes, time_t timestamp)
{
const time_t timestamp = time(nullptr);
uint64_t rem;
const uint64_t hashes = (target > 1) ? udiv128(1, 0, target, &rem) : 0;
constexpr size_t N = array_size(&StratumServer::m_hashrateData);
WriteLock lock(m_hashrateDataLock);
@@ -545,20 +602,20 @@ void StratumServer::update_hashrate_data(uint64_t target)
head.m_cumulativeHashes = m_cumulativeHashes;
}
else {
m_hashrateDataHead = (m_hashrateDataHead + 1) % array_size(m_hashrateData);
m_hashrateDataHead = (m_hashrateDataHead + 1) % N;
data[m_hashrateDataHead] = { timestamp, m_cumulativeHashes };
}
while (data[m_hashrateDataTail_15m].m_timestamp + 15 * 60 < timestamp) {
m_hashrateDataTail_15m = (m_hashrateDataTail_15m + 1) % array_size(m_hashrateData);
m_hashrateDataTail_15m = (m_hashrateDataTail_15m + 1) % N;
}
while (data[m_hashrateDataTail_1h].m_timestamp + 60 * 60 < timestamp) {
m_hashrateDataTail_1h = (m_hashrateDataTail_1h + 1) % array_size(m_hashrateData);
m_hashrateDataTail_1h = (m_hashrateDataTail_1h + 1) % N;
}
while (data[m_hashrateDataTail_24h].m_timestamp + 60 * 60 * 24 < timestamp) {
m_hashrateDataTail_24h = (m_hashrateDataTail_24h + 1) % array_size(m_hashrateData);
m_hashrateDataTail_24h = (m_hashrateDataTail_24h + 1) % N;
}
}
@@ -570,35 +627,34 @@ void StratumServer::on_share_found(uv_work_t* req)
StratumClient* client = share->m_client;
StratumServer* server = share->m_server;
p2pool* pool = server->m_pool;
BlockTemplate& block = pool->block_template();
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;
if (pool->stopped()) {
LOGWARN(0, "p2pool is shutting down, but a share was found. Trying to process it anyway!");
}
uint8_t blob[128];
uint64_t height;
difficulty_type difficulty;
difficulty_type sidechain_difficulty;
hash seed_hash;
size_t nonce_offset;
if (share->m_sidechainDifficulty.check_pow(share->m_resultHash)) {
uint8_t blob[128];
uint64_t height;
difficulty_type difficulty;
difficulty_type sidechain_difficulty;
hash seed_hash;
size_t nonce_offset;
const uint32_t blob_size = block.get_hashing_blob(share->m_templateId, share->m_extraNonce, blob, height, difficulty, sidechain_difficulty, seed_hash, nonce_offset);
if (!blob_size) {
LOGWARN(4, "client " << static_cast<char*>(client->m_addrString) << " got a stale share");
share->m_result = SubmittedShare::Result::STALE;
return;
}
const uint32_t blob_size = pool->block_template().get_hashing_blob(share->m_templateId, share->m_extraNonce, blob, height, difficulty, sidechain_difficulty, seed_hash, nonce_offset);
if (!blob_size) {
LOGWARN(4, "client " << static_cast<char*>(client->m_addrString) << " got a stale share");
share->m_result = SubmittedShare::Result::STALE;
return;
}
const bool mainchain_solution = difficulty.check_pow(share->m_resultHash);
const bool sidechain_solution = sidechain_difficulty.check_pow(share->m_resultHash);
if (mainchain_solution || sidechain_solution) {
for (uint32_t i = 0, nonce = share->m_nonce; i < sizeof(share->m_nonce); ++i) {
blob[nonce_offset + i] = nonce & 255;
nonce >>= 8;
@@ -617,9 +673,6 @@ void StratumServer::on_share_found(uv_work_t* req)
return;
}
uint64_t rem;
const uint64_t hashes = (target > 1) ? udiv128(1, 0, target, &rem) : 0;
const uint64_t n = server->m_cumulativeHashes + hashes;
const double diff = sidechain_difficulty.to_double();
const double effort = static_cast<double>(n - server->m_cumulativeHashesAtLastShare) * 100.0 / diff;
@@ -628,23 +681,18 @@ 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 << '%');
if (mainchain_solution) {
pool->submit_block_async(share->m_templateId, share->m_nonce, share->m_extraNonce);
block.update_tx_keys();
}
if (sidechain_solution) {
pool->submit_sidechain_block(share->m_templateId, share->m_nonce, share->m_extraNonce);
}
const std::string& s = client->m_customUser;
LOGINFO(0, log::Green() << "SHARE FOUND: mainchain height " << height << ", diff " << sidechain_difficulty << ", client " << static_cast<char*>(client->m_addrString) << (!s.empty() ? " user " : "") << s << ", effort " << effort << '%');
pool->submit_sidechain_block(share->m_templateId, share->m_nonce, share->m_extraNonce);
}
// Send the response to miner
const uint64_t value = *reinterpret_cast<uint64_t*>(share->m_resultHash.h + HASH_SIZE - sizeof(uint64_t));
if (LIKELY(value < target)) {
server->update_hashrate_data(target);
const time_t timestamp = time(nullptr);
server->update_hashrate_data(hashes, timestamp);
server->api_update_local_stats(timestamp);
share->m_result = SubmittedShare::Result::OK;
}
else {
@@ -670,6 +718,8 @@ void StratumServer::on_after_share_found(uv_work_t* req, int /*status*/)
StratumClient* client = share->m_client;
StratumServer* server = share->m_server;
const bool bad_share = (share->m_result == SubmittedShare::Result::LOW_DIFF) || (share->m_result == SubmittedShare::Result::INVALID_POW);
if ((client->m_resetCounter.load() == share->m_clientResetCounter) && (client->m_rpcId == share->m_rpcId)) {
const bool result = server->send(client,
[share](void* buf)
@@ -695,7 +745,7 @@ void StratumServer::on_after_share_found(uv_work_t* req, int /*status*/)
return s.m_pos;
});
if ((share->m_result == SubmittedShare::Result::LOW_DIFF) || (share->m_result == SubmittedShare::Result::INVALID_POW)) {
if (bad_share) {
client->ban(DEFAULT_BAN_TIME);
client->close();
}
@@ -703,6 +753,9 @@ void StratumServer::on_after_share_found(uv_work_t* req, int /*status*/)
client->close();
}
}
else if (bad_share) {
server->ban(share->m_clientAddr, DEFAULT_BAN_TIME);
}
}
StratumServer::StratumClient::StratumClient()
@@ -726,6 +779,7 @@ void StratumServer::StratumClient::reset()
memset(m_jobs, 0, sizeof(m_jobs));
m_perConnectionJobId = 0;
m_customDiff = {};
m_customUser.clear();
}
bool StratumServer::StratumClient::on_read(char* data, uint32_t size)
@@ -914,4 +968,76 @@ 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)
{
if (!m_pool->api() || !m_pool->params().m_localStats) {
return;
}
// Rate limit to no more than once in 60 seconds.
if (timestamp < m_apiLastUpdateTime + 60) {
return;
}
m_apiLastUpdateTime = timestamp;
uint64_t hashes_15m, hashes_1h, hashes_24h, total_hashes;
int64_t dt_15m, dt_1h, dt_24h;
uint64_t hashes_since_last_share;
{
ReadLock lock(m_hashrateDataLock);
total_hashes = m_cumulativeHashes;
hashes_since_last_share = m_cumulativeHashes - m_cumulativeHashesAtLastShare;
const HashrateData* data = m_hashrateData;
const HashrateData& head = data[m_hashrateDataHead];
const HashrateData& tail_15m = data[m_hashrateDataTail_15m];
const HashrateData& tail_1h = data[m_hashrateDataTail_1h];
const HashrateData& tail_24h = data[m_hashrateDataTail_24h];
hashes_15m = head.m_cumulativeHashes - tail_15m.m_cumulativeHashes;
dt_15m = static_cast<int64_t>(head.m_timestamp - tail_15m.m_timestamp);
hashes_1h = head.m_cumulativeHashes - tail_1h.m_cumulativeHashes;
dt_1h = static_cast<int64_t>(head.m_timestamp - tail_1h.m_timestamp);
hashes_24h = head.m_cumulativeHashes - tail_24h.m_cumulativeHashes;
dt_24h = static_cast<int64_t>(head.m_timestamp - tail_24h.m_timestamp);
}
const uint64_t hashrate_15m = (dt_15m > 0) ? (hashes_15m / dt_15m) : 0;
const uint64_t hashrate_1h = (dt_1h > 0) ? (hashes_1h / dt_1h ) : 0;
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;
}
int shares_found = m_totalFoundShares;
double current_effort = static_cast<double>(hashes_since_last_share) * 100.0 / m_pool->side_chain().difficulty().to_double();
int connections = m_numConnections;
int incoming_connections = m_numIncomingConnections;
m_pool->api()->set(p2pool_api::Category::LOCAL, "stats",
[hashrate_15m, hashrate_1h, hashrate_24h, total_hashes, shares_found, average_effort, current_effort, connections, incoming_connections](log::Stream& s)
{
s << "{\"hashrate_15m\":" << hashrate_15m
<< ",\"hashrate_1h\":" << hashrate_1h
<< ",\"hashrate_24h\":" << hashrate_24h
<< ",\"total_hashes\":" << total_hashes
<< ",\"shares_found\":" << shares_found
<< ",\"average_effort\":" << average_effort
<< ",\"current_effort\":" << current_effort
<< ",\"connections\":" << connections
<< ",\"incoming_connections\":" << incoming_connections
<< "}";
});
}
} // namespace p2pool
+8 -1
View File
@@ -65,6 +65,7 @@ public:
uint32_t m_perConnectionJobId;
difficulty_type m_customDiff;
std::string m_customUser;
};
bool on_login(StratumClient* client, uint32_t id, const char* login);
@@ -77,6 +78,7 @@ private:
void print_stratum_status() const;
static bool get_custom_diff(const char* s, difficulty_type& diff);
static bool get_custom_user(const char* s, std::string& user);
static void on_share_found(uv_work_t* req);
static void on_after_share_found(uv_work_t* req, int status);
@@ -112,6 +114,7 @@ private:
uv_work_t m_req;
StratumServer* m_server;
StratumClient* m_client;
raw_ip m_clientAddr;
uint32_t m_clientResetCounter;
uint32_t m_rpcId;
uint32_t m_id;
@@ -120,6 +123,7 @@ private:
uint32_t m_extraNonce;
uint64_t m_target;
hash m_resultHash;
difficulty_type m_sidechainDifficulty;
enum class Result {
STALE,
@@ -152,7 +156,10 @@ private:
double m_cumulativeFoundSharesDiff;
uint32_t m_totalFoundShares;
void update_hashrate_data(uint64_t target);
time_t m_apiLastUpdateTime;
void update_hashrate_data(uint64_t hashes, time_t timestamp);
void api_update_local_stats(time_t timestamp);
};
} // namespace p2pool
+1 -2
View File
@@ -101,7 +101,7 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::parse_address_list(const std::str
}
const int port = atoi(address.substr(k2 + 1).c_str());
if ((port > 0) && (port < 655356)) {
if ((port > 0) && (port < 65536)) {
callback(is_v6, address, ip, port);
}
else {
@@ -308,7 +308,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)
{
+17 -1
View File
@@ -29,6 +29,22 @@ static constexpr char log_category_prefix[] = "Util ";
namespace p2pool {
#define STR2(X) STR(X)
#define STR(X) #X
const char* VERSION = "v1.3 (built"
#if defined(__clang__)
" with clang/" __clang_version__
#elif defined(__GNUC__)
" with GCC/" STR2(__GNUC__) "." STR2(__GNUC_MINOR__) "." STR2(__GNUC_PATCHLEVEL__)
#elif defined(_MSC_VER)
" with MSVC/" STR2(_MSC_VER)
#endif
" on " __DATE__ ")";
#undef STR2
#undef STR
MinerCallbackHandler::~MinerCallbackHandler() {}
void panic()
@@ -40,7 +56,7 @@ void panic()
__debugbreak();
}
#endif
exit(1);
abort();
} while (true);
}
+30 -14
View File
@@ -17,8 +17,25 @@
#pragma once
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 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;
template<typename T> struct not_implemented { enum { value = 0 }; };
struct nocopy_nomove
@@ -93,6 +110,7 @@ FORCEINLINE void writeVarint(T value, std::vector<uint8_t>& out)
}
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; }
[[noreturn]] void panic();
@@ -119,34 +137,32 @@ extern thread_local 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>>;
} // namespace p2pool
namespace std {
namespace robin_hood {
template<>
struct hash<p2pool::hash>
{
FORCEINLINE size_t operator()(const p2pool::hash& value) const
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);
}
};
template<size_t N>
struct hash<std::array<uint8_t, N>>
{
FORCEINLINE size_t operator()(const std::array<uint8_t, N>& value) const
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
} // namespace robin_hood
+40 -3
View File
@@ -76,16 +76,22 @@ ZMQReader::~ZMQReader()
}
}
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);
m_subscriber.connect(addr);
while (!connect(addr, m_zmqPort)) { if (m_finished.load()) return; }
snprintf(addr, sizeof(addr), "tcp://127.0.0.1:%u", m_publisherPort);
m_subscriber.connect(addr);
while (!connect(addr, m_publisherPort)) { if (m_finished.load()) return; }
m_subscriber.set(zmq::sockopt::subscribe, "json-full-chain_main");
m_subscriber.set(zmq::sockopt::subscribe, "json-full-miner_data");
@@ -118,7 +124,38 @@ void ZMQReader::run()
LOGERR(1, "exception " << e.what() << ", aborting");
panic();
}
LOGINFO(1, "worker thread stopped");
}
bool ZMQReader::connect(const char* address, uint32_t id)
{
struct ConnectMonitor : public zmq::monitor_t
{
void on_event_connected(const zmq_event_t&, const char* address) ZMQ_OVERRIDE
{
LOGINFO(1, "connected to " << address);
connected = true;
}
bool connected = false;
} monitor;
char buf[32];
snprintf(buf, sizeof(buf), "inproc://connect-mon-%u", id);
monitor.init(m_subscriber, buf);
m_subscriber.connect(address);
using namespace std::chrono;
const system_clock::time_point start_time = system_clock::now();
while (!monitor.connected && monitor.check_event(-1)) {
const int64_t elapsed_time = duration_cast<milliseconds>(system_clock::now() - start_time).count();
if (elapsed_time >= 3000) {
LOGERR(1, "failed to connect to " << address);
return false;
}
}
return true;
}
void ZMQReader::parse(char* data, size_t size)
+2 -1
View File
@@ -28,8 +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);
void parse(char* data, size_t size);
+2
View File
@@ -84,6 +84,7 @@ set(SOURCES
../src/json_rpc_request.cpp
../src/keccak.cpp
../src/log.cpp
../src/memory_leak_debug.cpp
../src/mempool.cpp
../src/p2p_server.cpp
../src/p2pool.cpp
@@ -107,6 +108,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)
+4
View File
@@ -25,6 +25,8 @@ namespace p2pool {
TEST(crypto, derivation)
{
init_crypto_cache();
// Run the tests twice to check how crypto cache works
for (int i = 0; i < 2; ++i) {
std::ifstream f("crypto_tests.txt");
@@ -61,6 +63,8 @@ TEST(crypto, derivation)
}
} while (!f.eof());
}
destroy_crypto_cache();
}
}