From 61ed3c1c97b23dfb4c984ebc854d47d8ad3a09e0 Mon Sep 17 00:00:00 2001 From: Skot Croshere Date: Thu, 25 May 2023 00:16:29 -0400 Subject: [PATCH] cleaned up debug messages and hex printing a bit. still not checking nonces right --- components/stratum/include/utils.h | 9 ++++++- components/stratum/mining.c | 24 +++++++----------- components/stratum/utils.c | 40 ++++++++---------------------- main/bm1397.c | 1 - main/miner.c | 5 ++-- main/pretty.c | 6 ++--- main/serial.c | 2 -- 7 files changed, 33 insertions(+), 54 deletions(-) diff --git a/components/stratum/include/utils.h b/components/stratum/include/utils.h index a370a13f..cd5c9616 100644 --- a/components/stratum/include/utils.h +++ b/components/stratum/include/utils.h @@ -4,12 +4,19 @@ #include #include +/* + * General byte order swapping functions. + */ +#define bswap16(x) __bswap16(x) +#define bswap32(x) __bswap32(x) +#define bswap64(x) __bswap64(x) + int hex2char(uint8_t x, char * c); size_t bin2hex(const uint8_t * buf, size_t buflen, char * hex, size_t hexlen); uint8_t hex2val(char c); -void flip80bytes(void *dest_p, const void *src_p); +void flip_bytes(void *dest_p, const void *src_p, uint8_t num_bytes); size_t hex2bin(const char * hex, uint8_t * bin, size_t bin_len); diff --git a/components/stratum/mining.c b/components/stratum/mining.c index 6e6808ad..03c5bb4d 100644 --- a/components/stratum/mining.c +++ b/components/stratum/mining.c @@ -78,13 +78,11 @@ bm_job construct_bm_job(mining_notify * params, const char * merkle_root) { printf("midstate_data: "); prettyHex(midstate_data, 64); - printf("\n"); midstate_sha256_bin(midstate_data, 64, new_job.midstate); //make the midstate hash printf("midstate_hash: "); prettyHex(new_job.midstate, 32); - printf("\n"); reverse_bytes(new_job.midstate, 32); //reverse the midstate bytes for the BM job packet @@ -107,6 +105,7 @@ static const double truediffone = 2695953529101130949315647634472399133601089873 double test_nonce_value(bm_job * job, uint32_t nonce) { double d64, s64, ds; unsigned char header[80]; + uint32_t flipped_nonce; //copy data from job to header memcpy(header, &job->version, 4); @@ -114,23 +113,19 @@ double test_nonce_value(bm_job * job, uint32_t nonce) { memcpy(header + 36, job->merkle_root, 32); memcpy(header + 68, &job->ntime, 4); memcpy(header + 72, &job->target, 4); - memcpy(header + 76, &nonce, 4); + flipped_nonce = bswap32(nonce); + memcpy(header + 76, &flipped_nonce, 4); - // unsigned char swapped_header[80]; + //unsigned char swapped_header[80]; // unsigned char hash_buffer[32]; unsigned char hash_result[32]; - // printf("data32: "); - // prettyHex(header, 80); - // printf("\n"); + //endian flip 80 bytes. + //version (4 bytes), prevhash (32 bytes), merkle (32 bytes), time (4 bytes), bits (4 bytes), nonce (4 bytes) = 80 bytes + // flip_bytes((uint32_t *)swapped_header, header, 80); - // //endian flip the first 80 bytes. - // //version (4 bytes), prevhash (32 bytes), merkle (32 bytes), time (4 bytes), bits (4 bytes), nonce (4 bytes) = 80 bytes - // flip80bytes((uint32_t *)swapped_header, header); - - // //double hash the header - // mbedtls_sha256(swapped_header, 80, hash_buffer, 0); - // mbedtls_sha256(hash_buffer, 32, hash_result, 0); + printf("data32: "); + prettyHex(header, 80); mbedtls_sha256_context midstate, ctx; @@ -152,7 +147,6 @@ double test_nonce_value(bm_job * job, uint32_t nonce) { printf("hash: "); prettyHex(hash_result, 32); - printf("\n"); // //check that the last 4 bytes are 0 // if (*hash_32 != 0) { // return 0.0; diff --git a/components/stratum/utils.c b/components/stratum/utils.c index 0ce1469d..34942893 100644 --- a/components/stratum/utils.c +++ b/components/stratum/utils.c @@ -16,45 +16,27 @@ (((uint32_t) (a) >> 24) & 0xff)) #endif -/* - * General byte order swapping functions. - */ -#define bswap16(x) __bswap16(x) -#define bswap32(x) __bswap32(x) -#define bswap64(x) __bswap64(x) - uint32_t swab32(uint32_t v) { return bswap_32(v); } -//takes 80 bytes and flips every 4 bytes -void flip80bytes(void *dest_p, const void *src_p) { +//takes num_bytes bytes and flips every 4 bytes +void flip_bytes(void *dest_p, const void *src_p, uint8_t num_bytes) { uint32_t *dest = dest_p; const uint32_t *src = src_p; int i; + uint8_t num_words = num_bytes / 4; - for (i = 0; i < 20; i++) + //make sure num_bytes is a multiple of 4 + if (num_bytes % 4 != 0) { + fprintf(stderr, "num_bytes must be a multiple of 4\n"); + exit(EXIT_FAILURE); + } + + for (i = 0; i < num_words; i++) dest[i] = swab32(src[i]); } -void flip64bytes(void *dest_p, const void *src_p) { - uint32_t *dest = dest_p; - const uint32_t *src = src_p; - int i; - - for (i = 0; i < 16; i++) - dest[i] = swab32(src[i]); -} - -void flip32bytes(void *dest_p, const void *src_p) { - uint32_t *dest = dest_p; - const uint32_t *src = src_p; - int i; - - for (i = 0; i < 8; i++) - dest[i] = swab32(src[i]); -} - int hex2char(uint8_t x, char *c) { @@ -202,7 +184,7 @@ void midstate_sha256_bin( const uint8_t * data, const size_t data_len, uint8_t * mbedtls_sha256_update_ret(&midstate, data, 64); //memcpy(dest, midstate.state, 32); - flip32bytes(dest, midstate.state); + flip_bytes(dest, midstate.state, 32); } diff --git a/main/bm1397.c b/main/bm1397.c index c5edd191..64e38677 100644 --- a/main/bm1397.c +++ b/main/bm1397.c @@ -83,7 +83,6 @@ void parse_packet(unsigned char *buf, int len) { //debug the packet printf("<-"); prettyHex(buf, len); - printf("\n"); //determine response type diff --git a/main/miner.c b/main/miner.c index 53113172..585796ce 100755 --- a/main/miner.c +++ b/main/miner.c @@ -111,7 +111,7 @@ static void mining_task(void * pvParameters) while (1) { char * next_notify_json_str = (char *) queue_dequeue(&g_queue, &termination_flag); ESP_LOGI(TAG, "New Work Dequeued"); - ESP_LOGI(TAG, "Notify json: %s", next_notify_json_str); + //ESP_LOGI(TAG, "Notify json: %s", next_notify_json_str); uint32_t free_heap_size = esp_get_free_heap_size(); ESP_LOGI(TAG, "miner heap free size: %u bytes", free_heap_size); @@ -224,8 +224,7 @@ static void admin_task(void *pvParameters) while (1) { char * line = receive_jsonrpc_line(sock); - ESP_LOGI(TAG, "Received line: %s", line); - ESP_LOGI(TAG, "Received line length: %d", strlen(line)); + ESP_LOGI(TAG, "Received line [%d]: %s", strlen(line), line); stratum_method method = parse_stratum_method(line); if (method == MINING_NOTIFY) { diff --git a/main/pretty.c b/main/pretty.c index c8f8f777..117393ac 100644 --- a/main/pretty.c +++ b/main/pretty.c @@ -9,11 +9,11 @@ void prettyHex(unsigned char * buf, int len) { int i; - for (i = 0; i < len; i++) { - if ((i > 0) && (buf[i] == 0xAA) && (buf[i+1] == 0x55)) - printf("\n"); + printf("["); + for (i = 0; i < len-1; i++) { printf("%02X ", buf[i]); } + printf("%02X]\n", buf[len-1]); } //flip byte order of a 32 bit integer diff --git a/main/serial.c b/main/serial.c index cd9179b5..bfa29bff 100644 --- a/main/serial.c +++ b/main/serial.c @@ -44,7 +44,6 @@ int send_serial(uint8_t *data, int len, bool debug) { if (debug) { printf("->"); prettyHex((unsigned char*)data, len); - printf("\n"); } return uart_write_bytes(UART_NUM_1, (const char *) data, len); @@ -58,7 +57,6 @@ int16_t serial_rx(uint8_t * buf) { if (bytes_read > 0) { printf("bm rx\n"); prettyHex((unsigned char*) buf, bytes_read); - printf("\n"); } return bytes_read; }