|
| 1 | +// (c) 2021 Pttn (https://github.com/Pttn/rieMiner) |
| 2 | + |
| 3 | +#include <chrono> |
| 4 | +#include <cstdio> |
| 5 | +#include <cstdlib> |
| 6 | +#include <iomanip> |
| 7 | +#include <iostream> |
| 8 | +#include <netinet/in.h> |
| 9 | +#include <sstream> |
| 10 | +#include <thread> |
| 11 | +#include <unistd.h> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +constexpr uint16_t port(3004); |
| 15 | + |
| 16 | +int serverFd, clientFd; |
| 17 | +sockaddr_in address; |
| 18 | +socklen_t addressLength(sizeof(address)); |
| 19 | + |
| 20 | +constexpr uint16_t maxMessageSize(256); |
| 21 | +char buffer[256] = {0}; |
| 22 | +std::string receiveMessage() { |
| 23 | + /*int bytesRead(0); |
| 24 | + bytesRead = */read(clientFd, buffer, 256); |
| 25 | + // std::cout << "Got " << bytesRead << " bytes from rieMiner: " << buffer << std::endl; |
| 26 | + return buffer; |
| 27 | +} |
| 28 | + |
| 29 | +void sendMessage(const std::string &message) { |
| 30 | + // std::cout << "Sending " << message.size() << " bytes: " << message << std::endl; |
| 31 | + send(clientFd , message.c_str(), message.size(), 0); |
| 32 | +} |
| 33 | + |
| 34 | +void acceptMiner() { |
| 35 | + if ((clientFd = accept(serverFd, reinterpret_cast<sockaddr*>(&address), &addressLength)) < 0) { |
| 36 | + std::cerr << "Could not accept connection" << std::endl; |
| 37 | + exit(-1); |
| 38 | + } |
| 39 | + receiveMessage(); |
| 40 | + sendMessage("{\"id\": 0, \"result\": [[[\"mining.notify\", \"00\"]], \"00\", 1], \"error\": null}\n"); |
| 41 | + receiveMessage(); |
| 42 | + sendMessage("{\"id\": 0, \"result\": true, \"error\": null}\n"); |
| 43 | +} |
| 44 | + |
| 45 | +template <class C> std::string formatContainer(const C& container) { |
| 46 | + std::ostringstream oss; |
| 47 | + for (auto it(container.begin()) ; it < container.end() ; it++) { |
| 48 | + oss << *it; |
| 49 | + if (it != container.end() - 1) oss << ", "; |
| 50 | + } |
| 51 | + return oss.str(); |
| 52 | +} |
| 53 | + |
| 54 | +uint64_t timestampNow() { |
| 55 | + const auto now(std::chrono::system_clock::now()); |
| 56 | + return std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count(); |
| 57 | +} |
| 58 | + |
| 59 | +std::string formattedHeight(const uint32_t height) { |
| 60 | + std::ostringstream oss; |
| 61 | + if (height < 128) |
| 62 | + oss << "01" << std::setfill('0') << std::setw(2) << std::hex << height; |
| 63 | + else if (height < 32768) |
| 64 | + oss << "02" << std::setfill('0') << std::setw(2) << std::hex << height % 256 << std::setfill('0') << std::setw(2) << std::hex << (height/256) % 256; |
| 65 | + else |
| 66 | + oss << "03" << std::setfill('0') << std::setw(2) << std::hex << height % 256 << std::setfill('0') << std::setw(2) << std::hex << (height/256) % 256 << std::setfill('0') << std::setw(2) << std::hex << (height/65536) % 256; |
| 67 | + return oss.str(); |
| 68 | +} |
| 69 | +std::string generateMiningNotify(const uint32_t height, const uint32_t nBits, const std::vector<std::vector<uint64_t>> acceptedPatterns, const bool cleanJobs) { |
| 70 | + uint64_t currentJobId(0); |
| 71 | + std::ostringstream oss; |
| 72 | + oss << "{\"id\": null, \"method\": \"mining.notify\", \"params\": [\""; |
| 73 | + oss << std::hex << currentJobId++ << "\""; |
| 74 | + oss << ", \"0000000000000000000000000000000000000000000000000000000000000000\""; |
| 75 | + oss << ", \"000000000000000000000000000000000000000000000000000000000000000000000000000000000000" << formattedHeight(height) << "\""; |
| 76 | + oss << ", \"\""; |
| 77 | + oss << ", []"; |
| 78 | + oss << ", \"20000000\""; |
| 79 | + oss << ", \"" << std::setfill('0') << std::setw(8) << std::hex << nBits << "\""; |
| 80 | + oss << ", \"" << std::setfill('0') << std::setw(8) << std::hex << timestampNow() << "\""; |
| 81 | + oss << ", " << (cleanJobs ? "true" : "false"); |
| 82 | + oss << ", 1"; |
| 83 | + oss << ", ["; |
| 84 | + for (uint64_t i(0) ; i < acceptedPatterns.size() ; i++) { |
| 85 | + oss << "[" << formatContainer(acceptedPatterns[i]) << "]"; |
| 86 | + if (i + 1 != acceptedPatterns.size()) |
| 87 | + oss << ", "; |
| 88 | + } |
| 89 | + oss << "]]}\n"; |
| 90 | + return oss.str(); |
| 91 | +} |
| 92 | + |
| 93 | +int main() { |
| 94 | + std::cout << "rieMiner Test Server" << std::endl; |
| 95 | + std::cout << "-----------------------------------------------------------" << std::endl; |
| 96 | + if ((serverFd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { |
| 97 | + std::cerr << "Could not get a File Descriptor for the Server" << std::endl; |
| 98 | + exit(-1); |
| 99 | + } |
| 100 | + int optval(1); |
| 101 | + if (setsockopt(serverFd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &optval, sizeof(decltype(optval))) != 0) { |
| 102 | + std::cerr << "Setsockopt could not set SO_REUSEADDR | SO_REUSEPORT" << std::endl; |
| 103 | + exit(-1); |
| 104 | + } |
| 105 | + address.sin_family = AF_INET; |
| 106 | + address.sin_addr.s_addr = INADDR_ANY; |
| 107 | + address.sin_port = htons(port); |
| 108 | + if (bind(serverFd, reinterpret_cast<sockaddr*>(&address), sizeof(address)) < 0) { |
| 109 | + std::cerr << "Could not bind" << std::endl; |
| 110 | + exit(-1); |
| 111 | + } |
| 112 | + if (listen(serverFd, 1) < 0) { |
| 113 | + std::cerr << "Could not listen" << std::endl; |
| 114 | + exit(-1); |
| 115 | + } |
| 116 | + |
| 117 | + std::cout << "Please start rieMiner in Pool Mode with Port " << port << std::endl; |
| 118 | + uint32_t height(1U); |
| 119 | + while (true) { |
| 120 | + std::cout << "Test 1: connection to a Pool" << std::endl; |
| 121 | + std::cout << "Expected behavior: rieMiner initializes the miner and starts mining" << std::endl; |
| 122 | + acceptMiner(); |
| 123 | + uint32_t difficulty(768U); |
| 124 | + std::cout << "-----------------------------------------------------------" << std::endl; |
| 125 | + std::cout << "Test 2: Varying Difficulty" << std::endl; |
| 126 | + std::cout << "Expected behavior: rieMiner finds shares and restarts correctly in reaction to the increasing and decreasing Difficulty" << std::endl; |
| 127 | + for ( ; difficulty < 896 ; difficulty += 8) { |
| 128 | + std::cout << "Current Difficulty: " << difficulty << " (Block " << height << ")" << std::endl; |
| 129 | + sendMessage(generateMiningNotify(height++, difficulty*256, {{0, 4, 2, 4, 2}, {0, 2, 4, 2, 4}}, true)); |
| 130 | + for (uint16_t i(0) ; i < 8 ; i++) { |
| 131 | + receiveMessage(); |
| 132 | + sendMessage("{\"id\": 0, \"result\": true, \"error\": null}\n"); |
| 133 | + } |
| 134 | + } |
| 135 | + for ( ; difficulty > 640 ; difficulty -= 16) { |
| 136 | + std::cout << "Current Difficulty: " << difficulty << " (Block " << height << ")" << std::endl; |
| 137 | + sendMessage(generateMiningNotify(height++, difficulty*256, {{0, 4, 2, 4, 2}, {0, 2, 4, 2, 4}}, true)); |
| 138 | + for (uint16_t i(0) ; i < 8 ; i++) { |
| 139 | + receiveMessage(); |
| 140 | + sendMessage("{\"id\": 0, \"result\": true, \"error\": null}\n"); |
| 141 | + } |
| 142 | + } |
| 143 | + std::cout << "-----------------------------------------------------------" << std::endl; |
| 144 | + std::cout << "Test 3: Disconnects" << std::endl; |
| 145 | + std::cout << "Expected behavior: rieMiner gets disconnected several times, but always reconnects and resumes mining properly" << std::endl; |
| 146 | + std::cout << "8 disconnections after 5 s" << std::endl; |
| 147 | + for (uint16_t i(0) ; i < 8 ; i++) { |
| 148 | + std::this_thread::sleep_for(std::chrono::seconds(5)); |
| 149 | + std::cout << "Disconnect " << i << std::endl; |
| 150 | + close(clientFd); |
| 151 | + acceptMiner(); |
| 152 | + std::cout << "Reconnected" << std::endl; |
| 153 | + sendMessage(generateMiningNotify(height, difficulty*256, {{0, 4, 2, 4, 2}, {0, 2, 4, 2, 4}}, true)); |
| 154 | + } |
| 155 | + std::cout << "4 disconnections after 15 s" << std::endl; |
| 156 | + for (uint16_t i(0) ; i < 4 ; i++) { |
| 157 | + std::this_thread::sleep_for(std::chrono::seconds(15)); |
| 158 | + std::cout << "Disconnect " << i << std::endl; |
| 159 | + close(clientFd); |
| 160 | + acceptMiner(); |
| 161 | + std::cout << "Reconnected" << std::endl; |
| 162 | + sendMessage(generateMiningNotify(height, difficulty*256, {{0, 4, 2, 4, 2}, {0, 2, 4, 2, 4}}, true)); |
| 163 | + } |
| 164 | + std::cout << "2 disconnections after 30 s" << std::endl; |
| 165 | + for (uint16_t i(0) ; i < 2 ; i++) { |
| 166 | + std::this_thread::sleep_for(std::chrono::seconds(30)); |
| 167 | + std::cout << "Disconnect " << i << std::endl; |
| 168 | + close(clientFd); |
| 169 | + acceptMiner(); |
| 170 | + std::cout << "Reconnected" << std::endl; |
| 171 | + sendMessage(generateMiningNotify(height, difficulty*256, {{0, 4, 2, 4, 2}, {0, 2, 4, 2, 4}}, true)); |
| 172 | + } |
| 173 | + std::cout << "-----------------------------------------------------------" << std::endl; |
| 174 | + std::cout << "Test 4: Constellation Pattern Change/Hard Fork Simulation" << std::endl; |
| 175 | + std::cout << "Expected behavior: rieMiner finds shares and restarts correctly in reaction to the Pattern or Difficulty Change" << std::endl; |
| 176 | + difficulty = 540; |
| 177 | + sendMessage(generateMiningNotify(height++, difficulty*256, {{0, 2, 4, 2, 4, 6, 2}, {0, 2, 6, 4, 2, 4, 2}}, true)); |
| 178 | + std::cout << "rieMiner should now mine 7-tuples. Current Difficulty: " << difficulty << " (Block " << height << ")" << std::endl; |
| 179 | + for (uint16_t i(0) ; i < 64 ; i++) { |
| 180 | + receiveMessage(); |
| 181 | + sendMessage("{\"id\": 0, \"result\": true, \"error\": null}\n"); |
| 182 | + } |
| 183 | + difficulty = 480; |
| 184 | + sendMessage(generateMiningNotify(height++, difficulty*256, {{0, 2, 4, 2, 4, 6, 2, 6}, {0, 2, 4, 6, 2, 6, 4, 2}, {0, 6, 2, 6, 4, 2, 4, 2}}, true)); |
| 185 | + std::cout << "rieMiner should now mine 8-tuples. Current Difficulty: " << difficulty << " (Block " << height << ")" << std::endl; |
| 186 | + for (uint16_t i(0) ; i < 8 ; i++) { |
| 187 | + receiveMessage(); |
| 188 | + sendMessage("{\"id\": 0, \"result\": true, \"error\": null}\n"); |
| 189 | + } |
| 190 | + std::cout << "Test loop finished." << std::endl; |
| 191 | + close(clientFd); |
| 192 | + std::cout << "-----------------------------------------------------------" << std::endl; |
| 193 | + } |
| 194 | + return 0; |
| 195 | +} |
0 commit comments