Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save "Best Difficulty" scores into NVS #20 #47

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions main/nvs_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,43 @@ void nvs_config_set_u16(const char * key, const uint16_t value)
nvs_close(handle);
return;
}

uint64_t nvs_config_get_u64(const char * key, const uint64_t default_value)
{
nvs_handle handle;
esp_err_t err;
err = nvs_open(NVS_CONFIG_NAMESPACE, NVS_READONLY, &handle);
if (err != ESP_OK) {
return default_value;
}

uint64_t out;
err = nvs_get_u64(handle, key, &out);

if (err != ESP_OK) {
return default_value;
}

nvs_close(handle);
return out;
}

void nvs_config_set_u64(const char * key, const uint64_t value)
{

nvs_handle handle;
esp_err_t err;
err = nvs_open(NVS_CONFIG_NAMESPACE, NVS_READWRITE, &handle);
if (err != ESP_OK) {
ESP_LOGW(TAG, "Could not open nvs");
return;
}

err = nvs_set_u64(handle, key, value);
if (err != ESP_OK) {
ESP_LOGW(TAG, "Could not write nvs key: %s, value: %llu", key, value);
return;
}
nvs_close(handle);
return;
}
3 changes: 3 additions & 0 deletions main/nvs_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
#define NVS_CONFIG_BOARD_VERSION "boardversion"
#define NVS_CONFIG_FLIP_SCREEN "flipscreen"
#define NVS_CONFIG_INVERT_SCREEN "invertscreen"
#define NVS_CONFIG_BEST_DIFF "bestdiff"

char * nvs_config_get_string(const char * key, const char * default_value);
void nvs_config_set_string(const char * key, const char * default_value);
uint16_t nvs_config_get_u16(const char * key, const uint16_t default_value);
void nvs_config_set_u16(const char * key, const uint16_t value);
uint64_t nvs_config_get_u64(const char * key, const uint64_t default_value);
void nvs_config_set_u64(const char * key, const uint64_t value);

#endif // MAIN_NVS_CONFIG_H
12 changes: 9 additions & 3 deletions main/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ static void _init_system(GlobalState * global_state, SystemModule * module)
module->screen_page = 0;
module->shares_accepted = 0;
module->shares_rejected = 0;
module->best_nonce_diff = 0;
module->best_nonce_diff = nvs_config_get_u64(NVS_CONFIG_BEST_DIFF, 0);
module->start_time = esp_timer_get_time();
module->lastClockSync = 0;
module->FOUND_BLOCK = false;
module->startup_done = false;

// set the best diff string to 0
_suffix_string(0, module->best_diff_string, DIFF_STRING_SIZE, 0);
// set the best diff string
_suffix_string(module->best_nonce_diff, module->best_diff_string, DIFF_STRING_SIZE, 0);

// set the ssid string to blank
memset(module->ssid, 0, 20);
Expand Down Expand Up @@ -275,7 +275,13 @@ static void _check_for_best_diff(SystemModule * module, double diff, uint32_t nb
if (diff < module->best_nonce_diff) {
return;
}

uint64_t old = module->best_nonce_diff;
module->best_nonce_diff = diff;
// only write to flash if new > old
if(module->best_nonce_diff > old) {
nvs_config_set_u64(NVS_CONFIG_BEST_DIFF, module->best_nonce_diff);
}
// make the best_nonce_diff into a string
_suffix_string((uint64_t) diff, module->best_diff_string, DIFF_STRING_SIZE, 0);
double network_diff = _calculate_network_difficulty(nbits);
Expand Down