From be2a0f3abf02d649a1f9c05320b11b2009755505 Mon Sep 17 00:00:00 2001 From: DcruBro Date: Sun, 3 May 2026 16:37:26 +0200 Subject: [PATCH] throttling cli arg --- include/autolykos2/autolykos2.h | 4 ++++ src/autolykos2/autolykos2.c | 25 ++++++++++++++++++++++++- src/main.c | 24 ++++++++++++++++++++++-- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/include/autolykos2/autolykos2.h b/include/autolykos2/autolykos2.h index fe60b6e..f73f3a7 100644 --- a/include/autolykos2/autolykos2.h +++ b/include/autolykos2/autolykos2.h @@ -12,6 +12,8 @@ extern "C" { typedef struct Autolykos2Context Autolykos2Context; +extern uint64_t autolykos2_sleepBetweenHashOperationsMicroseconds; + Autolykos2Context* Autolykos2_Create(void); void Autolykos2_Destroy(Autolykos2Context* ctx); @@ -61,6 +63,8 @@ bool Autolykos2_FindNonceSingleCore( uint8_t outHash[32] ); +void Autolykos2_SetSleepBetweenHashOperations(uint64_t sleepMicroseconds); + #ifdef __cplusplus } #endif diff --git a/src/autolykos2/autolykos2.c b/src/autolykos2/autolykos2.c index a99f0b8..830dc3e 100644 --- a/src/autolykos2/autolykos2.c +++ b/src/autolykos2/autolykos2.c @@ -4,6 +4,13 @@ #include #include +#if defined(_WIN32) || defined(_WIN64) +#include +#else +#include +#endif + +uint64_t autolykos2_sleepBetweenHashOperationsMicroseconds = 0; typedef struct { uint8_t* buf; @@ -239,7 +246,19 @@ static bool Autolykos2_HashCore( memcpy(finalInput + 32, accHash, 32); memcpy(finalInput + 64, nonceBytes, 8); memcpy(finalInput + 72, heightBytes, 8); - return Blake2b_Hash(finalInput, sizeof(finalInput), outHash, 32); + bool ok = Blake2b_Hash(finalInput, sizeof(finalInput), outHash, 32); + + // Throttle between hash operations if configured (applies to all hash paths). + if (autolykos2_sleepBetweenHashOperationsMicroseconds > 0) { +#if defined(_WIN32) || defined(_WIN64) + DWORD ms = (DWORD)((autolykos2_sleepBetweenHashOperationsMicroseconds + 999) / 1000); + Sleep(ms); +#else + usleep((useconds_t)autolykos2_sleepBetweenHashOperationsMicroseconds); +#endif + } + + return ok; } Autolykos2Context* Autolykos2_Create(void) { @@ -510,3 +529,7 @@ bool Autolykos2_FindNonceSingleCore( return false; } + +void Autolykos2_SetSleepBetweenHashOperations(uint64_t sleepMicroseconds) { + autolykos2_sleepBetweenHashOperationsMicroseconds = sleepMicroseconds; +} diff --git a/src/main.c b/src/main.c index acf840d..de99ab3 100644 --- a/src/main.c +++ b/src/main.c @@ -444,8 +444,28 @@ static bool VerifyChainFully(blockchain_t* chain) { } int main(int argc, char* argv[]) { - (void)argc; - (void)argv; + //(void)argc; + //(void)argv; + if (argc > 1) { + // Check for potential startup args. + if (strcmp(argv[1], "--throttle") == 0) { + // Get throttle value in microseconds if provided, otherwise default to 1000 microseconds (1ms) between hash operations. + uint64_t throttleUs = 1000; + if (argc > 2) { + char* endptr = NULL; + throttleUs = strtoull(argv[2], &endptr, 10); + if (*argv[2] == '\0' || argv[2][0] == '-' || (endptr && *endptr != '\0')) { + printf("invalid throttle value\n"); + return 1; + } + } + Autolykos2_SetSleepBetweenHashOperations(throttleUs); + printf("Throttling hash operations with a sleep of %llu microseconds\n", (unsigned long long)throttleUs); + } else { + printf("Unknown argument: %s\n", argv[1]); + return 1; + } + } signal(SIGINT, handle_sigint); srand((unsigned int)time(NULL));