debug output on malformed merkle tree branch

This commit is contained in:
shufps 2024-09-13 15:50:34 +02:00
parent 901be540f5
commit c772ac4a91
No known key found for this signature in database
GPG Key ID: 371CB8C24D8CB455
4 changed files with 53 additions and 10 deletions

View File

@ -195,7 +195,7 @@ void BM1397_send_hash_frequency(float frequency)
vTaskDelay(10 / portTICK_PERIOD_MS); 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) { while (true) {
int received = SERIAL_rx(asic_response_buffer, 11, 1000); int received = SERIAL_rx(asic_response_buffer, 11, 1000);
if (received > 0) { if (received > 0) {
ESP_LOG_BUFFER_HEX(TAG, asic_response_buffer, received); //ESP_LOG_BUFFER_HEX(TAG, asic_response_buffer, received);
chip_counter++; chip_counter++;
} else { } else {
break; 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 // send serial data
vTaskDelay(SLEEP_TIME / portTICK_PERIOD_MS); 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) 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)); 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) int BM1397_set_max_baud(void)
{ {
// divider of 0 for 3,125,000 // 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}; unsigned char baudrate[9] = {0x00, MISC_CONTROL, 0x00, 0x00, 0b01100000, 0b00110001};
; // baudrate - misc_control ; // baudrate - misc_control
_send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), baudrate, 6, BM1397_SERIALTX_DEBUG); _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); 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); _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) if (received < 0)
{ {
ESP_LOGI(TAG, "Error in serial RX"); Serial.println("Error in serial RX");
return NULL; return NULL;
} }
else if (received == 0) 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) if (received != 9 || asic_response_buffer[0] != 0xAA || asic_response_buffer[1] != 0x55)
{ {
ESP_LOGI(TAG, "Serial RX invalid %i", received); Serial.println("Serial RX invalid. Resetting receive buffer ...");
ESP_LOG_BUFFER_HEX(TAG, asic_response_buffer, received); //ESP_LOG_BUFFER_HEX(TAG, asic_response_buffer, received);
SERIAL_clear_buffer();
return NULL; return NULL;
} }

View File

@ -51,6 +51,21 @@ double nerdnos_test_nonce_value(const bm_job_t *job, const uint32_t nonce, const
return ds; 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]) 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; 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); memcpy(both_merkles, new_root, 32);
for (size_t i = 0; i < job->merkle_branch.size(); i++) { 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); double_sha256_bin(both_merkles, 64, new_root);
memcpy(both_merkles, new_root, 32); memcpy(both_merkles, new_root, 32);
} }

View File

@ -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 hex2bin(const char *hex, uint8_t *bin, size_t bin_len)
{ {
size_t len = 0; size_t len = 0;

View File

@ -2,6 +2,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
int hex2char(uint8_t x, char *c); 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); int largest_power_of_two(int num);
uint32_t increment_bitmask(const uint32_t value, const uint32_t mask); 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);