Improved POW

This commit is contained in:
2025-08-15 07:47:16 -04:00
parent 3d2537603c
commit c569c0c346
8 changed files with 155 additions and 188 deletions

View File

@@ -1376,12 +1376,16 @@ static void replace_event_content(cJSON* target_event, cJSON* source_event) {
* @param event The event to add proof of work to
* @param private_key The private key for re-signing the event
* @param target_difficulty Target number of leading zero bits (default: 4 if 0)
* @param max_attempts Maximum number of mining attempts (default: 10,000,000 if <= 0)
* @param progress_report_interval How often to call progress callback (default: 10,000 if <= 0)
* @param timestamp_update_interval How often to update timestamp (default: 10,000 if <= 0)
* @param progress_callback Optional callback for mining progress
* @param user_data User data for progress callback
* @return NOSTR_SUCCESS on success, error code on failure
*/
int nostr_add_proof_of_work(cJSON* event, const unsigned char* private_key,
int target_difficulty,
int target_difficulty, int max_attempts,
int progress_report_interval, int timestamp_update_interval,
void (*progress_callback)(int current_difficulty, uint64_t nonce, void* user_data),
void* user_data) {
if (!event || !private_key) {
@@ -1398,6 +1402,17 @@ int nostr_add_proof_of_work(cJSON* event, const unsigned char* private_key,
return NOSTR_SUCCESS;
}
// Set default values for parameters
if (max_attempts <= 0) {
max_attempts = 10000000; // 10 million default
}
if (progress_report_interval <= 0) {
progress_report_interval = 10000; // Every 10,000 attempts
}
if (timestamp_update_interval <= 0) {
timestamp_update_interval = 10000; // Every 10,000 attempts
}
// Extract event data for reconstruction
cJSON* kind_item = cJSON_GetObjectItem(event, "kind");
cJSON* content_item = cJSON_GetObjectItem(event, "content");
@@ -1414,7 +1429,6 @@ int nostr_add_proof_of_work(cJSON* event, const unsigned char* private_key,
uint64_t nonce = 0;
int attempts = 0;
int max_attempts = 10000000;
time_t current_timestamp = original_timestamp;
// PoW difficulty tracking variables
@@ -1423,8 +1437,8 @@ int nostr_add_proof_of_work(cJSON* event, const unsigned char* private_key,
// Mining loop
while (attempts < max_attempts) {
// Update timestamp every 10,000 iterations
if (attempts % 10000 == 0) {
// Update timestamp based on timestamp_update_interval
if (attempts % timestamp_update_interval == 0) {
current_timestamp = time(NULL);
#ifdef ENABLE_DEBUG_LOGGING
FILE* f = fopen("debug.log", "a");
@@ -1438,6 +1452,11 @@ int nostr_add_proof_of_work(cJSON* event, const unsigned char* private_key,
best_difficulty_this_round = 0;
}
// Call progress callback at specified intervals
if (progress_callback && (attempts % progress_report_interval == 0)) {
progress_callback(best_difficulty_overall, nonce, user_data);
}
// Create working copy of tags and add nonce
cJSON* working_tags = cJSON_Duplicate(tags_item, 1);
if (!working_tags) {
@@ -1483,11 +1502,6 @@ int nostr_add_proof_of_work(cJSON* event, const unsigned char* private_key,
best_difficulty_overall = current_difficulty;
}
// Call progress callback if provided
if (progress_callback) {
progress_callback(current_difficulty, nonce, user_data);
}
// Check if we've reached the target
if (current_difficulty >= target_difficulty) {
#ifdef ENABLE_DEBUG_LOGGING

Binary file not shown.

View File

@@ -485,13 +485,17 @@ int nostr_decode_npub(const char* input, unsigned char* private_key);
*
* @param event cJSON event object to add PoW to
* @param private_key Private key for re-signing the event during mining
* @param target_difficulty Target number of leading zero bits (default: 2 if 0)
* @param progress_callback Optional callback for progress updates (nonce, difficulty, user_data)
* @param target_difficulty Target number of leading zero bits (default: 4 if 0)
* @param max_attempts Maximum number of mining attempts (default: 10,000,000 if <= 0)
* @param progress_report_interval How often to call progress callback (default: 10,000 if <= 0)
* @param timestamp_update_interval How often to update timestamp (default: 10,000 if <= 0)
* @param progress_callback Optional callback for progress updates (current_difficulty, nonce, user_data)
* @param user_data User data passed to progress callback
* @return NOSTR_SUCCESS on success, error code on failure
*/
int nostr_add_proof_of_work(cJSON* event, const unsigned char* private_key,
int target_difficulty,
int target_difficulty, int max_attempts,
int progress_report_interval, int timestamp_update_interval,
void (*progress_callback)(int current_difficulty, uint64_t nonce, void* user_data),
void* user_data);