|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "lib/platform_common/qstdint.h" |
| 4 | + |
| 5 | +#include "platform/memory.h" |
| 6 | +#include "platform/memory_util.h" |
| 7 | +#include "platform/concurrency.h" |
| 8 | +#include "network_messages/custom_mining.h" |
| 9 | +#include "contract_core/qpi_hash_map_impl.h" |
| 10 | +#include "kangaroo_twelve.h" |
| 11 | + |
| 12 | + |
| 13 | +class CustomQubicMiningStorage |
| 14 | +{ |
| 15 | +public: |
| 16 | + static constexpr unsigned int maxNumTasks = 32; |
| 17 | + |
| 18 | + // A struct for storing an active doge mining task on the node. |
| 19 | + struct StoredDogeMiningTask |
| 20 | + { |
| 21 | + uint8_t dispatcherTarget[32]; // dispatcher target, usually easier than pool and network difficulty, full 32-byte representation |
| 22 | + |
| 23 | + // Full header can be constructed via concatenating version + prevHash + merkleRoot + miner's nTime + nBits + miner's nonce. |
| 24 | + uint8_t version[4]; // 4 bytes version |
| 25 | + uint8_t prevHash[32]; // 32 bytes prevBlockHash |
| 26 | + uint8_t nBits[4]; // 4 bytes network difficulty (nBits) |
| 27 | + }; |
| 28 | + |
| 29 | +private: |
| 30 | + static constexpr unsigned int maxNumSolutionsPerTask = 256; |
| 31 | + |
| 32 | + uint64_t activeTasks[CustomMiningType::TOTAL_NUM_TYPES][maxNumTasks]; |
| 33 | + unsigned int nextTaskIndex[CustomMiningType::TOTAL_NUM_TYPES]; |
| 34 | + |
| 35 | + // For each task, we store a set of received solution hashes to prevent duplicate solutions. |
| 36 | + // Two-dimensional array [CustomMiningType::TOTAL_NUM_TYPES][maxNumTasks] indexed by mining type and type-specific task index. |
| 37 | + QPI::HashSet<m256i, maxNumSolutionsPerTask>* receivedSolutions; |
| 38 | + static constexpr unsigned long long receivedSolutionsSize = CustomMiningType::TOTAL_NUM_TYPES * maxNumTasks * sizeof(QPI::HashSet<m256i, maxNumSolutionsPerTask>); |
| 39 | + |
| 40 | + // Storage for type-specific task descriptions. |
| 41 | + StoredDogeMiningTask dogeTasks[maxNumTasks]; |
| 42 | + |
| 43 | + inline static volatile char lock = 0; |
| 44 | + |
| 45 | + // Return the mining-type-specific index of the task with the given jobId and customMiningType, or -1 if not found. |
| 46 | + // The returned index can then be used to access the type-specific task descriptions and the solution hash set for this task. |
| 47 | + int findTask(uint8_t customMiningType, uint64_t jobId) |
| 48 | + { |
| 49 | + for (unsigned int i = 0; i < maxNumTasks; ++i) |
| 50 | + { |
| 51 | + if (activeTasks[customMiningType][i] == jobId) |
| 52 | + return i; |
| 53 | + } |
| 54 | + return -1; |
| 55 | + } |
| 56 | + |
| 57 | + // Converts the compact representation of the target (4 bytes) to the full 32-byte representation, and writes it to the provided output pointer. |
| 58 | + // Expected input byte order: bytes 0 - 2 mantissa in little endian, byte 3 exponent. |
| 59 | + // Output byte order: little endian, i.e. least-significant byte in index 0. |
| 60 | + void convertTargetCompactToFull(const uint8_t* compact, uint8_t* full) |
| 61 | + { |
| 62 | + setMem(full, 32, 0); |
| 63 | + |
| 64 | + uint8_t exponent = compact[3]; |
| 65 | + |
| 66 | + // The target is mantissa * 256^(exponent - 3). |
| 67 | + // This means the mantissa starts at byte index (exponent - 3). |
| 68 | + int start_index = exponent - 3; |
| 69 | + |
| 70 | + for (int i = 0; i < 3; ++i) |
| 71 | + { |
| 72 | + int target_idx = start_index + i; |
| 73 | + if (target_idx >= 0 && target_idx < 32) |
| 74 | + { |
| 75 | + full[target_idx] = compact[i]; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | +public: |
| 81 | + |
| 82 | + // Initialize the storage. Return true if successful, false if initialization failed (e.g. due to memory allocation failure). |
| 83 | + bool init() |
| 84 | + { |
| 85 | + ASSERT(lock == 0); |
| 86 | + |
| 87 | + if (!allocPoolWithErrorLog(L"CustomQubicMiningStorage::receivedSolutions ", receivedSolutionsSize, (void**)&receivedSolutions, __LINE__)) |
| 88 | + return false; |
| 89 | + |
| 90 | + setMem(activeTasks, sizeof(activeTasks), 0); |
| 91 | + setMem(nextTaskIndex, sizeof(nextTaskIndex), 0); |
| 92 | + setMem(dogeTasks, sizeof(dogeTasks), 0); |
| 93 | + |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + // Deinitialize the storage and free any allocated memory. |
| 98 | + void deinit() |
| 99 | + { |
| 100 | + if (receivedSolutions) |
| 101 | + freePool(receivedSolutions); |
| 102 | + } |
| 103 | + |
| 104 | + // Add a new mining task. Return false if a task with the same jobId and customMiningType already exists, true if added successfully. |
| 105 | + // The task description is expected to be behind the CustomQubicMiningTask struct in memory. The provided size should specify the |
| 106 | + // full size of the CustomQubicMiningTask struct and the task description. |
| 107 | + // For DOGE mining: Adding a task with cleanJobQueue = true will clear all existing tasks and solutions before adding the new task. |
| 108 | + // If maxNumTasks is reached and a job with cleanJobQueue = false is added, it will override the oldest task and its solutions. |
| 109 | + bool addTask(const CustomQubicMiningTask* task, unsigned int size) |
| 110 | + { |
| 111 | + LockGuard guard(lock); |
| 112 | + |
| 113 | + if (findTask(task->customMiningType, task->jobId) < 0) |
| 114 | + { |
| 115 | + unsigned int typeSpecificTaskIndex = 0; |
| 116 | + if (task->customMiningType == CustomMiningType::DOGE) |
| 117 | + { |
| 118 | + // Type-specific task info is stored behind general CustomQubicMiningTask struct. |
| 119 | + if (size < sizeof(CustomQubicMiningTask) + sizeof(QubicDogeMiningTask)) |
| 120 | + return false; |
| 121 | + unsigned int& nextDogeTaskId = nextTaskIndex[CustomMiningType::DOGE]; |
| 122 | + const QubicDogeMiningTask* dogeTask = reinterpret_cast<const QubicDogeMiningTask*>(reinterpret_cast<const char*>(task) + sizeof(CustomQubicMiningTask)); |
| 123 | + if (dogeTask->cleanJobQueue) |
| 124 | + { |
| 125 | + setMem(activeTasks[CustomMiningType::DOGE], maxNumTasks * sizeof(uint64_t), 0); |
| 126 | + for (int t = 0; t < maxNumTasks; ++t) |
| 127 | + receivedSolutions[CustomMiningType::DOGE * maxNumTasks + t].reset(); |
| 128 | + setMem(dogeTasks, sizeof(dogeTasks), 0); |
| 129 | + nextDogeTaskId = 0; |
| 130 | + } |
| 131 | + else |
| 132 | + { |
| 133 | + // If not cleaning job queue, we will override the oldest task. Clean the corresponding solution hash set. |
| 134 | + receivedSolutions[CustomMiningType::DOGE * maxNumTasks + nextDogeTaskId].reset(); |
| 135 | + } |
| 136 | + convertTargetCompactToFull(dogeTask->dispatcherDifficulty, dogeTasks[nextDogeTaskId].dispatcherTarget); |
| 137 | + copyMem(dogeTasks[nextDogeTaskId].nBits, dogeTask->nBits, 4); |
| 138 | + copyMem(dogeTasks[nextDogeTaskId].version, dogeTask->version, 4); |
| 139 | + copyMem(dogeTasks[nextDogeTaskId].prevHash, dogeTask->prevHash, 32); |
| 140 | + typeSpecificTaskIndex = nextDogeTaskId; |
| 141 | + nextDogeTaskId = (nextDogeTaskId + 1) % maxNumTasks; |
| 142 | + } |
| 143 | + else |
| 144 | + { |
| 145 | + return false; |
| 146 | + } |
| 147 | + activeTasks[task->customMiningType][typeSpecificTaskIndex] = task->jobId; |
| 148 | + return true; |
| 149 | + } |
| 150 | + |
| 151 | + return false; |
| 152 | + } |
| 153 | + |
| 154 | + // Return -1 if the solution is invalid (stale or duplicate), 0 if the solution is valid but not added due to storage limit, |
| 155 | + // and 1 if the solution is valid and added successfully. If taskDescription is not nullptr and the solution corresponds to an active task, |
| 156 | + // the task description is written into the provided pointer. |
| 157 | + int addSolution(const CustomQubicMiningSolution* solution, unsigned int size, unsigned char* taskDescription = nullptr) |
| 158 | + { |
| 159 | + if (size <= sizeof(CustomQubicMiningSolution)) |
| 160 | + return -1; |
| 161 | + |
| 162 | + LockGuard guard(lock); |
| 163 | + |
| 164 | + // Check if the solution corresponds to an active task. |
| 165 | + int typeSpecificTaskIndex = findTask(solution->customMiningType, solution->jobId); |
| 166 | + if (typeSpecificTaskIndex < 0) |
| 167 | + return -1; |
| 168 | + |
| 169 | + if (taskDescription) |
| 170 | + { |
| 171 | + if (solution->customMiningType == CustomMiningType::DOGE) |
| 172 | + { |
| 173 | + StoredDogeMiningTask* taskOut = reinterpret_cast<StoredDogeMiningTask*>(taskDescription); |
| 174 | + *taskOut = dogeTasks[typeSpecificTaskIndex]; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + // Check if the solution is duplicate. |
| 179 | + m256i digest; |
| 180 | + KangarooTwelve(reinterpret_cast<const char*>(solution) + sizeof(CustomQubicMiningSolution), |
| 181 | + size - sizeof(CustomQubicMiningSolution), &digest, sizeof(digest)); |
| 182 | + |
| 183 | + if (receivedSolutions[solution->customMiningType * maxNumTasks + typeSpecificTaskIndex].contains(digest)) |
| 184 | + return -1; |
| 185 | + |
| 186 | + // Try to add the solution hash to the set of received solutions for this task. May return NULL_INDEX if the set is full. |
| 187 | + QPI::sint64 indexAdded = receivedSolutions[solution->customMiningType * maxNumTasks + typeSpecificTaskIndex].add(digest); |
| 188 | + |
| 189 | + return (indexAdded == QPI::NULL_INDEX) ? 0 : 1; |
| 190 | + } |
| 191 | + |
| 192 | + // Return true if there is an active task with the given jobId and customMiningType, false otherwise. |
| 193 | + bool containsTask(uint8_t customMiningType, uint64_t jobId) |
| 194 | + { |
| 195 | + LockGuard guard(lock); |
| 196 | + return findTask(customMiningType, jobId) >= 0; |
| 197 | + } |
| 198 | +}; |
0 commit comments