diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml index 80e600e..2b0bdae 100644 --- a/.github/workflows/c-cpp.yml +++ b/.github/workflows/c-cpp.yml @@ -76,6 +76,7 @@ jobs: - name: Run RandomX tests shell: alpine.sh {0} run: | + p2pool/build/p2pool --test p2pool/build/external/src/RandomX/randomx-tests - name: Build tests @@ -138,6 +139,7 @@ jobs: - name: Run RandomX tests run: | + build/p2pool --test build/external/src/RandomX/randomx-tests - name: Build tests @@ -215,6 +217,7 @@ jobs: - name: Run RandomX tests run: | + build/p2pool --test build/external/src/RandomX/randomx-tests - name: Build tests @@ -291,6 +294,7 @@ jobs: - name: Run RandomX tests run: | + qemu-aarch64 -L /usr/aarch64-linux-gnu build/p2pool --test qemu-aarch64 -L /usr/aarch64-linux-gnu build/external/src/RandomX/randomx-tests - name: Build tests @@ -372,6 +376,7 @@ jobs: - name: Run RandomX tests run: | + build/p2pool.exe --test build/external/src/RandomX/randomx-tests.exe - name: Build tests @@ -425,6 +430,7 @@ jobs: - name: Run RandomX tests if: matrix.config.rx == 'ON' run: | + build/Release/p2pool.exe --test cd build/external/src/RandomX & "${{ matrix.config.msbuild }}msbuild" -v:m /m /p:Configuration=Release randomx-tests.vcxproj Release/randomx-tests.exe @@ -504,6 +510,7 @@ jobs: - name: Run RandomX tests run: | + build/p2pool --test build/external/src/RandomX/randomx-tests - name: Build tests @@ -536,7 +543,7 @@ jobs: config: - {os: "macos-11", target: "arm64-apple-macos-11", xcode: "12.5.1"} - {os: "macos-12", target: "arm64-apple-macos-12", xcode: "14.2"} - - {os: "macos-13", target: "arm64-apple-macos-13.5", xcode: "15.0"} + - {os: "macos-13", target: "arm64-apple-macos", xcode: "15.0"} steps: - name: Checkout repository @@ -632,6 +639,7 @@ jobs: cd build cmake .. -DCMAKE_C_FLAGS='-flto' -DCMAKE_CXX_FLAGS='-flto' -DSTATIC_LIBS=ON make -j2 + ./p2pool --test external/src/RandomX/randomx-tests cd ../tests mkdir build diff --git a/src/main.cpp b/src/main.cpp index 3544bf8..2ecaebf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,6 +22,10 @@ #include "p2p_server.h" #include +#ifdef WITH_RANDOMX +#include "randomx.h" +#endif + void p2pool_usage() { printf("P2Pool %s\n" @@ -81,6 +85,55 @@ void p2pool_version() printf("P2Pool %s\n", p2pool::VERSION); } +int p2pool_test() +{ +#ifdef WITH_RANDOMX + const char myKey[] = "test key 000"; + const char myInput[] = "This is a test"; + char hash[RANDOMX_HASH_SIZE]; + + const randomx_flags flags = randomx_get_flags() | RANDOMX_FLAG_FULL_MEM; + randomx_cache* myCache = randomx_alloc_cache(flags); + if (!myCache) { + printf("Cache allocation failed\n"); + return 1; + } + randomx_init_cache(myCache, myKey, sizeof(myKey) - 1); + + randomx_dataset* myDataset = randomx_alloc_dataset(flags); + if (!myDataset) { + printf("Dataset allocation failed\n"); + return 1; + } + + randomx_init_dataset(myDataset, myCache, 0, randomx_dataset_item_count()); + randomx_release_cache(myCache); + + randomx_vm* myMachine = randomx_create_vm(flags, nullptr, myDataset); + if (!myMachine) { + printf("Failed to create a virtual machine"); + return 1; + } + + randomx_calculate_hash(myMachine, &myInput, sizeof(myInput) - 1, hash); + + char buf[RANDOMX_HASH_SIZE * 2 + 1] = {}; + p2pool::log::Stream s(buf); + s << p2pool::log::hex_buf(hash, RANDOMX_HASH_SIZE); + + if (memcmp(buf, "639183aae1bf4c9a35884cb46b09cad9175f04efd7684e7262a0ac1c2f0b4e3f", RANDOMX_HASH_SIZE * 2) != 0) { + printf("Invalid hash calculated\n"); + return 1; + } + + randomx_destroy_vm(myMachine); + randomx_release_dataset(myDataset); +#endif + + printf("Self-test passed\n"); + return 0; +} + int main(int argc, char* argv[]) { if (argc == 1) { @@ -98,6 +151,10 @@ int main(int argc, char* argv[]) p2pool_version(); return 0; } + + if (!strcmp(argv[i], "--test")) { + return p2pool_test(); + } } memory_tracking_start(); diff --git a/src/util.h b/src/util.h index 8a20bce..bb6effc 100644 --- a/src/util.h +++ b/src/util.h @@ -311,6 +311,7 @@ void memory_tracking_start(); bool memory_tracking_stop(); void p2pool_usage(); void p2pool_version(); +int p2pool_test(); namespace robin_hood {