cleaned up debug messages and hex printing a bit. still not checking nonces right

This commit is contained in:
Skot Croshere 2023-05-25 00:16:29 -04:00 committed by johnny9
parent 3f79ff615e
commit 61ed3c1c97
7 changed files with 33 additions and 54 deletions

View File

@ -4,12 +4,19 @@
#include <stddef.h>
#include <stdint.h>
/*
* 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);

View File

@ -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;

View File

@ -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);
}

View File

@ -83,7 +83,6 @@ void parse_packet(unsigned char *buf, int len) {
//debug the packet
printf("<-");
prettyHex(buf, len);
printf("\n");
//determine response type

View File

@ -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) {

View File

@ -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

View File

@ -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;
}