From c772ac4a9121f8bf7c4c67311be145b6666db380 Mon Sep 17 00:00:00 2001 From: shufps Date: Fri, 13 Sep 2024 15:50:34 +0200 Subject: [PATCH] debug output on malformed merkle tree branch --- src/drivers/nerd-nos/bm1397.cpp | 19 ++++++++++--------- src/drivers/nerd-nos/mining.cpp | 22 +++++++++++++++++++++- src/drivers/nerd-nos/utils.cpp | 18 ++++++++++++++++++ src/drivers/nerd-nos/utils.h | 4 ++++ 4 files changed, 53 insertions(+), 10 deletions(-) diff --git a/src/drivers/nerd-nos/bm1397.cpp b/src/drivers/nerd-nos/bm1397.cpp index 4f2fffa..a45a826 100644 --- a/src/drivers/nerd-nos/bm1397.cpp +++ b/src/drivers/nerd-nos/bm1397.cpp @@ -195,7 +195,7 @@ void BM1397_send_hash_frequency(float frequency) vTaskDelay(10 / portTICK_PERIOD_MS); - ESP_LOGI(TAG, "Setting Frequency to %.2fMHz (%.2f)", frequency, newf); + Serial.printf("Setting Frequency to %.2fMHz (%.2f)\n", frequency, newf); } @@ -208,13 +208,13 @@ static uint8_t _send_init(uint64_t frequency, uint16_t asic_count) while (true) { int received = SERIAL_rx(asic_response_buffer, 11, 1000); if (received > 0) { - ESP_LOG_BUFFER_HEX(TAG, asic_response_buffer, received); + //ESP_LOG_BUFFER_HEX(TAG, asic_response_buffer, received); chip_counter++; } else { break; } } - ESP_LOGI(TAG, "%i chip(s) detected on the chain, expected %i", chip_counter, asic_count); + Serial.printf("%i chip(s) detected on the chain, expected %i\n", chip_counter, asic_count); // send serial data vTaskDelay(SLEEP_TIME / portTICK_PERIOD_MS); @@ -269,7 +269,7 @@ static void _reset(void) uint8_t BM1397_init(uint64_t frequency, uint16_t asic_count) { - ESP_LOGI(TAG, "Initializing BM1397"); + Serial.println("Initializing BM1397"); memset(asic_response_buffer, 0, sizeof(asic_response_buffer)); @@ -299,7 +299,7 @@ int BM1397_set_default_baud(void) int BM1397_set_max_baud(void) { // divider of 0 for 3,125,000 - ESP_LOGI(TAG, "Setting max baud of 3125000"); + Serial.println("Setting max baud of 3125000"); unsigned char baudrate[9] = {0x00, MISC_CONTROL, 0x00, 0x00, 0b01100000, 0b00110001}; ; // baudrate - misc_control _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), baudrate, 6, BM1397_SERIALTX_DEBUG); @@ -330,7 +330,7 @@ void BM1397_set_job_difficulty_mask(int difficulty) job_difficulty_mask[5 - i] = reverse_bits(value); } - ESP_LOGI(TAG, "Setting job ASIC mask to %d", difficulty); + Serial.printf("Setting job ASIC mask to %d\n", difficulty); _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), job_difficulty_mask, 6, BM1397_SERIALTX_DEBUG); } @@ -369,7 +369,7 @@ asic_result *BM1397_receive_work(uint16_t timeout) if (received < 0) { - ESP_LOGI(TAG, "Error in serial RX"); + Serial.println("Error in serial RX"); return NULL; } else if (received == 0) @@ -380,8 +380,9 @@ asic_result *BM1397_receive_work(uint16_t timeout) if (received != 9 || asic_response_buffer[0] != 0xAA || asic_response_buffer[1] != 0x55) { - ESP_LOGI(TAG, "Serial RX invalid %i", received); - ESP_LOG_BUFFER_HEX(TAG, asic_response_buffer, received); + Serial.println("Serial RX invalid. Resetting receive buffer ..."); + //ESP_LOG_BUFFER_HEX(TAG, asic_response_buffer, received); + SERIAL_clear_buffer(); return NULL; } diff --git a/src/drivers/nerd-nos/mining.cpp b/src/drivers/nerd-nos/mining.cpp index 929b072..f8cda28 100644 --- a/src/drivers/nerd-nos/mining.cpp +++ b/src/drivers/nerd-nos/mining.cpp @@ -51,6 +51,21 @@ double nerdnos_test_nonce_value(const bm_job_t *job, const uint32_t nonce, const return ds; } +static void dump(mining_job *job) { + Serial.printf("job_id: %s\n", job->job_id.c_str()); + Serial.printf("prev_block_hash: %s\n", job->prev_block_hash.c_str()); + Serial.printf("coinb1: %s\n", job->coinb1.c_str()); + Serial.printf("coinb2: %s\n", job->coinb2.c_str()); + Serial.printf("nbits: %s\n", job->nbits.c_str()); + Serial.printf("version: %s\n", job->version.c_str()); + Serial.printf("ntime: %s\n", job->ntime.c_str()); + Serial.printf("taget: %lu\n", job->target); + Serial.printf("clean_jobs: %s\n", job->clean_jobs ? "true" : "false"); + for (size_t i = 0; i < job->merkle_branch.size(); i++) { + const char* m = job->merkle_branch[i]; + Serial.printf("merkle_branch[%d]: %s\n", i, m); + } +} static void calculate_merkle_root_hash(const char *coinbase_tx, mining_job* job, char merkle_root_hash[65]) { size_t coinbase_tx_bin_len = strlen(coinbase_tx) / 2; @@ -63,7 +78,12 @@ static void calculate_merkle_root_hash(const char *coinbase_tx, mining_job* job, memcpy(both_merkles, new_root, 32); for (size_t i = 0; i < job->merkle_branch.size(); i++) { - hex2bin((const char*) job->merkle_branch[i], &both_merkles[32], 32); + const char* m = job->merkle_branch[i]; + // if merkle branch is not what we expect, dump the job + if (!is_hex_string(m)) { + dump(job); + } + hex2bin(m, &both_merkles[32], 32); double_sha256_bin(both_merkles, 64, new_root); memcpy(both_merkles, new_root, 32); } diff --git a/src/drivers/nerd-nos/utils.cpp b/src/drivers/nerd-nos/utils.cpp index 01a47e1..ce409d3 100644 --- a/src/drivers/nerd-nos/utils.cpp +++ b/src/drivers/nerd-nos/utils.cpp @@ -127,6 +127,24 @@ uint8_t hex2val(char c) } } +bool is_hex_digit(char c) { + return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')); +} + +bool is_hex_string(const char* str) { + // Check if the string is exactly 64 characters long + if (strlen(str) != 64) { + return false; + } + // Check if each character is a valid hexadecimal digit + for (size_t i = 0; i < 64; i++) { + if (!is_hex_digit(str[i])) { + return false; + } + } + return true; +} + size_t hex2bin(const char *hex, uint8_t *bin, size_t bin_len) { size_t len = 0; diff --git a/src/drivers/nerd-nos/utils.h b/src/drivers/nerd-nos/utils.h index 78b6852..273a965 100644 --- a/src/drivers/nerd-nos/utils.h +++ b/src/drivers/nerd-nos/utils.h @@ -2,6 +2,7 @@ #include #include +#include int hex2char(uint8_t x, char *c); @@ -38,3 +39,6 @@ unsigned char reverse_bits(unsigned char num); int largest_power_of_two(int num); uint32_t increment_bitmask(const uint32_t value, const uint32_t mask); + +bool is_hex_digit(char c); +bool is_hex_string(const char* str); \ No newline at end of file