deployd/client/pow.c
Arija A. 90724afb2d
Add multithreading to pow.py
Signed-off-by: Arija A. <ari@ari.lt>
2025-07-27 02:05:49 +03:00

79 lines
2.4 KiB
C

#include "include/conf.h"
#include "include/def.h"
#include "include/pow.h"
#include "server/include/log.h"
#include "server/include/pow.h"
#include "server/include/def.h"
#include "server/include/blake2s.h"
/* TODO: Add multithreading */
int dc_pow_solve_batch(dp_Logger *logger,
uint8_t difficulty,
uint8_t ones,
const uint8_t challenge[DP_POW_CHALLENGE_SIZE],
uint64_t *noncep,
size_t batch) {
if (!logger || difficulty == 0 || ones == 0 || !challenge || !noncep ||
batch == 0) {
return -1;
}
uint64_t nonce = *noncep;
char strnum[21] = {0};
uint8_t digest[DP_BLAKE2S_OUTPUT_SIZE_MAX] = {0};
for (size_t num = 0; num < batch; ++num) {
if (nonce == UINT64_MAX) {
dp_log(
logger, DP_LOG_FATAL, DC_CLIENT_POW_LOG,
"Could not find a valid nonce solution in 64 bit search space");
return -1;
}
const int size = dp_u642str(strnum, nonce);
if (size < 1) {
dp_log(logger, DP_LOG_FATAL, DC_CLIENT_POW_LOG,
"Could not convert nonce to number");
return -1;
}
dp_Blake2sCtx ctx = {0};
if (!dp_Blake2sCtx_init(&ctx, DP_BLAKE2S_OUTPUT_SIZE_MAX, challenge,
DP_POW_CHALLENGE_SIZE)) {
dp_log(logger, DP_LOG_FATAL, DC_CLIENT_POW_LOG,
"Could not initialise PoW BLAKE2s context");
return -1;
}
if (!dp_Blake2sCtx_update(&ctx, strnum, (size_t)size)) {
dp_log(logger, DP_LOG_FATAL, DC_CLIENT_POW_LOG,
"Could not update PoW BLAKE2s context");
return -1;
}
if (!dp_Blake2sCtx_final(&ctx)) {
dp_log(logger, DP_LOG_FATAL, DC_CLIENT_POW_LOG,
"Could not finalise BLAKE2s context");
return -1;
}
if (!dp_Blake2sCtx_to_digest(&ctx, digest)) {
dp_log(logger, DP_LOG_FATAL, DC_CLIENT_POW_LOG,
"Could not digest PoW solution");
return -1;
}
if (dp_pow_has_leading_zero_bits(digest, difficulty) &&
dp_pow_has_xor_ones(digest, challenge, ones)) {
*noncep = nonce;
return 1;
}
++nonce;
}
*noncep = nonce;
return 0;
}