mirror of
https://github.com/skot/ESP-Miner.git
synced 2025-04-11 05:19:28 +02:00
fix some warnings. trying to check nonce diff -- it's not working.
This commit is contained in:
parent
9927e2995b
commit
ccb094a916
@ -4,10 +4,13 @@
|
||||
#include "stratum_api.h"
|
||||
|
||||
typedef struct {
|
||||
uint32_t starting_nonce;
|
||||
uint32_t target; // aka difficulty, aka nbits
|
||||
uint32_t version;
|
||||
uint8_t prev_block_hash[32];
|
||||
uint8_t merkle_root[32];
|
||||
uint32_t ntime;
|
||||
uint32_t merkle_root_end;
|
||||
uint32_t target; // aka difficulty, aka nbits
|
||||
uint32_t starting_nonce;
|
||||
|
||||
uint8_t midstate[32];
|
||||
char * jobid;
|
||||
char * extranonce2;
|
||||
@ -20,10 +23,10 @@ char * construct_coinbase_tx(const char * coinbase_1, const char * coinbase_2,
|
||||
|
||||
char * calculate_merkle_root_hash(const char * coinbase_tx, const uint8_t merkle_branches[][32], const int num_merkle_branches);
|
||||
|
||||
bm_job construct_bm_job(uint32_t version, const char * prev_block_hash, const char * merkle_root,
|
||||
uint32_t ntime, uint32_t target);
|
||||
bm_job construct_bm_job(mining_notify * params, const char * merkle_root);
|
||||
|
||||
void init_extranonce_2_generation(uint32_t extranonce_2_length, uint64_t starting_nonce);
|
||||
double test_nonce_value(bm_job * job, uint32_t nonce);
|
||||
|
||||
char * extranonce_2_generate();
|
||||
|
||||
|
@ -9,6 +9,7 @@ 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);
|
||||
|
||||
size_t hex2bin(const char * hex, uint8_t * bin, size_t bin_len);
|
||||
|
||||
@ -26,4 +27,6 @@ void swap_endian_words(const char * hex, uint8_t * output);
|
||||
|
||||
void reverse_bytes(uint8_t * data, size_t len);
|
||||
|
||||
double le256todouble(const void *target);
|
||||
|
||||
#endif // STRATUM_UTILS_H
|
@ -4,6 +4,7 @@
|
||||
#include "mining.h"
|
||||
#include "utils.h"
|
||||
#include "../../main/pretty.h"
|
||||
#include "mbedtls/sha256.h"
|
||||
|
||||
void free_bm_job(bm_job * job)
|
||||
{
|
||||
@ -52,36 +53,37 @@ char * calculate_merkle_root_hash(const char * coinbase_tx, const uint8_t merkle
|
||||
return merkle_root_hash;
|
||||
}
|
||||
|
||||
bm_job construct_bm_job(uint32_t version, const char * prev_block_hash, const char * merkle_root,
|
||||
uint32_t ntime, uint32_t target)
|
||||
{
|
||||
//take a mining_notify struct with ascii hex strings and convert it to a bm_job struct
|
||||
bm_job construct_bm_job(mining_notify * params, const char * merkle_root) {
|
||||
bm_job new_job;
|
||||
|
||||
new_job.version = params->version;
|
||||
new_job.starting_nonce = 0;
|
||||
new_job.target = target;
|
||||
new_job.ntime = ntime;
|
||||
new_job.target = params->target;
|
||||
new_job.ntime = params->ntime;
|
||||
|
||||
uint8_t merkle_root_bin[32];
|
||||
uint8_t prev_block_bin[32];
|
||||
hex2bin(merkle_root, merkle_root_bin, 32);
|
||||
hex2bin(prev_block_hash, prev_block_bin, 32);
|
||||
hex2bin(merkle_root, new_job.merkle_root, 32);
|
||||
hex2bin(params->prev_block_hash, new_job.prev_block_hash, 32);
|
||||
|
||||
////make the midstate hash
|
||||
uint8_t midstate_data[64];
|
||||
|
||||
//print the header
|
||||
//printf("header: %08x%s%s%08x%08x000000000000008000000000000000000000000000000000000000000000000000000000\n", version, prev_block_hash, merkle_root, ntime, target);
|
||||
printf("header: %08x%s%s%08x%08x000000000000008000000000000000000000000000000000000000000000000000000000\n", params->version, params->prev_block_hash, merkle_root, params->ntime, params->target);
|
||||
|
||||
memcpy(midstate_data, &version, 4); //copy version
|
||||
swap_endian_words(prev_block_hash, midstate_data + 4); //copy prev_block_hash
|
||||
memcpy(midstate_data + 36, merkle_root_bin, 28); //copy merkle_root
|
||||
// printf("midstate_data: ");
|
||||
// prettyHex(midstate_data, 64);
|
||||
// printf("\n");
|
||||
//copy 68 bytes header data into midstate (and deal with endianess)
|
||||
memcpy(midstate_data, &new_job.version, 4); //copy version
|
||||
swap_endian_words(params->prev_block_hash, midstate_data + 4); //copy prev_block_hash
|
||||
swap_endian_words(merkle_root, midstate_data + 36); //copy merkle_root
|
||||
|
||||
|
||||
printf("midstate_data: ");
|
||||
prettyHex(midstate_data, 64);
|
||||
printf("\n");
|
||||
|
||||
midstate_sha256_bin(midstate_data, 64, new_job.midstate); //make the midstate hash
|
||||
reverse_bytes(new_job.midstate, 32); //reverse the midstate bytes for the BM job packet
|
||||
|
||||
memcpy(&new_job.merkle_root_end, merkle_root_bin + 28, 4);
|
||||
|
||||
return new_job;
|
||||
}
|
||||
|
||||
@ -90,4 +92,51 @@ char * extranonce_2_generate(uint32_t extranonce_2, uint32_t length)
|
||||
char * extranonce_2_str = malloc(length * 2 + 1);
|
||||
bin2hex((uint8_t *) &extranonce_2, length, extranonce_2_str, length * 2 + 1);
|
||||
return extranonce_2_str;
|
||||
}
|
||||
|
||||
///////cgminer nonce testing
|
||||
/* truediffone == 0x00000000FFFF0000000000000000000000000000000000000000000000000000
|
||||
*/
|
||||
static const double truediffone = 26959535291011309493156476344723991336010898738574164086137773096960.0;
|
||||
|
||||
/* testing a nonce and return the diff - 0 means invalid */
|
||||
double test_nonce_value(bm_job * job, uint32_t nonce) {
|
||||
double d64, s64, ds;
|
||||
unsigned char header[80];
|
||||
|
||||
//copy data from job to header
|
||||
memcpy(header, &job->version, 4);
|
||||
memcpy(header + 4, job->prev_block_hash, 32);
|
||||
memcpy(header + 36, job->merkle_root, 32);
|
||||
memcpy(header + 68, &job->ntime, 4);
|
||||
memcpy(header + 72, &job->target, 4);
|
||||
memcpy(header + 76, &nonce, 4);
|
||||
|
||||
unsigned char swapped_header[80];
|
||||
unsigned char hash_buffer[32];
|
||||
unsigned char hash_result[32];
|
||||
|
||||
printf("data32: ");
|
||||
prettyHex(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("hash: ");
|
||||
prettyHex(hash_result, 32);
|
||||
// //check that the last 4 bytes are 0
|
||||
// if (*hash_32 != 0) {
|
||||
// return 0.0;
|
||||
// }
|
||||
|
||||
d64 = truediffone;
|
||||
s64 = le256todouble(hash_result);
|
||||
ds = d64 / s64;
|
||||
|
||||
return ds;
|
||||
}
|
@ -16,10 +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) {
|
||||
uint32_t *dest = dest_p;
|
||||
const uint32_t *src = src_p;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 20; 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;
|
||||
@ -214,3 +231,24 @@ void reverse_bytes(uint8_t * data, size_t len) {
|
||||
data[len - 1 - i] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
/* Converts a little endian 256 bit value to a double */
|
||||
double le256todouble(const void *target)
|
||||
{
|
||||
uint64_t *data64;
|
||||
double dcut64;
|
||||
|
||||
data64 = (uint64_t *)(target + 24);
|
||||
dcut64 = bswap64(*data64) * 6277101735386680763835789423207666416102355444464034512896.0;
|
||||
|
||||
data64 = (uint64_t *)(target + 16);
|
||||
dcut64 += bswap64(*data64) * 340282366920938463463374607431768211456.0;
|
||||
|
||||
data64 = (uint64_t *)(target + 8);
|
||||
dcut64 += bswap64(*data64) * 18446744073709551616.0;
|
||||
|
||||
data64 = (uint64_t *)(target);
|
||||
dcut64 += bswap64(*data64);
|
||||
|
||||
return dcut64;
|
||||
}
|
||||
|
@ -12,7 +12,7 @@
|
||||
#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
|
||||
#define I2C_MASTER_TIMEOUT_MS 1000
|
||||
|
||||
static const char *TAG = "EMC2101.c";
|
||||
//static const char *TAG = "EMC2101.c";
|
||||
|
||||
/**
|
||||
* @brief Read a sequence of I2C bytes
|
||||
|
@ -12,7 +12,7 @@
|
||||
#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
|
||||
#define I2C_MASTER_TIMEOUT_MS 1000
|
||||
|
||||
static const char *TAG = "INA260.c";
|
||||
//static const char *TAG = "INA260.c";
|
||||
|
||||
/**
|
||||
* @brief Read a sequence of I2C bytes
|
||||
@ -24,14 +24,14 @@ static esp_err_t register_read(uint8_t reg_addr, uint8_t *data, size_t len) {
|
||||
/**
|
||||
* @brief Write a byte to a I2C register
|
||||
*/
|
||||
static esp_err_t register_write_byte(uint8_t reg_addr, uint8_t data) {
|
||||
int ret;
|
||||
uint8_t write_buf[2] = {reg_addr, data};
|
||||
// static esp_err_t register_write_byte(uint8_t reg_addr, uint8_t data) {
|
||||
// int ret;
|
||||
// uint8_t write_buf[2] = {reg_addr, data};
|
||||
|
||||
ret = i2c_master_write_to_device(I2C_MASTER_NUM, INA260_I2CADDR_DEFAULT, write_buf, sizeof(write_buf), I2C_MASTER_TIMEOUT_MS / portTICK_RATE_MS);
|
||||
// ret = i2c_master_write_to_device(I2C_MASTER_NUM, INA260_I2CADDR_DEFAULT, write_buf, sizeof(write_buf), I2C_MASTER_TIMEOUT_MS / portTICK_RATE_MS);
|
||||
|
||||
return ret;
|
||||
}
|
||||
// return ret;
|
||||
// }
|
||||
|
||||
float INA260_read_current(void) {
|
||||
uint8_t data[2];
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include "driver/adc.h"
|
||||
#include "esp_adc_cal.h"
|
||||
|
||||
static const char *TAG = "adc.c";
|
||||
//static const char *TAG = "adc.c";
|
||||
static esp_adc_cal_characteristics_t adc1_chars;
|
||||
|
||||
//Sets up the ADC to read Vcore. Run this before ADC_get_vcore()
|
||||
|
@ -78,7 +78,7 @@ void split_response(unsigned char *buf, int len) {
|
||||
|
||||
//parse incoming packets
|
||||
void parse_packet(unsigned char *buf, int len) {
|
||||
response_type_t response_type;
|
||||
//response_type_t response_type;
|
||||
|
||||
//debug the packet
|
||||
printf("<-");
|
||||
@ -88,10 +88,10 @@ void parse_packet(unsigned char *buf, int len) {
|
||||
|
||||
//determine response type
|
||||
if (buf[len-1] & RESPONSE_JOB) {
|
||||
response_type = JOB_RESP;
|
||||
//response_type = JOB_RESP;
|
||||
parse_job_response(buf, len);
|
||||
} else {
|
||||
response_type = CMD_RESP;
|
||||
//response_type = CMD_RESP;
|
||||
parse_cmd_packet(buf, len);
|
||||
}
|
||||
|
||||
|
24
main/miner.c
24
main/miner.c
@ -79,7 +79,7 @@ static void AsicTask(void * pvParameters)
|
||||
memcpy(&job.starting_nonce, &next_bm_job->starting_nonce, 4);
|
||||
memcpy(&job.nbits, &next_bm_job->target, 4);
|
||||
memcpy(&job.ntime, &next_bm_job->ntime, 4);
|
||||
memcpy(&job.merkle4, &next_bm_job->merkle_root_end, 4);
|
||||
memcpy(&job.merkle4, &next_bm_job->merkle_root + 28, 4);
|
||||
memcpy(&job.midstate, &next_bm_job->midstate, 32);
|
||||
|
||||
send_work(&job);
|
||||
@ -92,13 +92,13 @@ static void AsicTask(void * pvParameters)
|
||||
print_hex((uint8_t *) &nonce.nonce, 4, 4, "nonce: ");
|
||||
memset(buf, 0, 1024);
|
||||
//check the nonce difficulty
|
||||
if (nonce.nonce < next_bm_job->target) {
|
||||
ESP_LOGI(TAG, "Nonce is valid");
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Nonce is invalid");
|
||||
double nonce_diff = test_nonce_value(next_bm_job, nonce.nonce);
|
||||
|
||||
ESP_LOGI(TAG, "Nonce difficulty: %f", nonce_diff);
|
||||
|
||||
if (nonce_diff > 100) {
|
||||
submit_share(sock, STRATUM_USER, next_bm_job->jobid, next_bm_job->ntime, next_bm_job->extranonce2, nonce.nonce);
|
||||
}
|
||||
submit_share(sock, STRATUM_USER, next_bm_job->jobid,
|
||||
next_bm_job->ntime, next_bm_job->extranonce2, nonce.nonce);
|
||||
}
|
||||
free_bm_job(next_bm_job);
|
||||
}
|
||||
@ -122,17 +122,13 @@ static void mining_task(void * pvParameters)
|
||||
|
||||
char * extranonce_2_str = extranonce_2_generate(extranonce_2, extranonce_2_len);
|
||||
|
||||
char *coinbase_tx = construct_coinbase_tx(params.coinbase_1, params.coinbase_2,
|
||||
extranonce_str, extranonce_2_str);
|
||||
char *coinbase_tx = construct_coinbase_tx(params.coinbase_1, params.coinbase_2, extranonce_str, extranonce_2_str);
|
||||
//ESP_LOGI(TAG, "Coinbase tx: %s", coinbase_tx);
|
||||
|
||||
char *merkle_root = calculate_merkle_root_hash(coinbase_tx,
|
||||
(uint8_t(*)[32])params.merkle_branches,
|
||||
params.n_merkle_branches);
|
||||
char *merkle_root = calculate_merkle_root_hash(coinbase_tx, (uint8_t(*)[32])params.merkle_branches, params.n_merkle_branches);
|
||||
//ESP_LOGI(TAG, "Merkle root: %s", merkle_root);
|
||||
|
||||
bm_job next_job = construct_bm_job(params.version, params.prev_block_hash, merkle_root,
|
||||
params.ntime, params.target);
|
||||
bm_job next_job = construct_bm_job(¶ms, merkle_root);
|
||||
|
||||
//ESP_LOGI(TAG, "bm_job: ");
|
||||
// print_hex((uint8_t *) &next_job.target, 4, 4, "nbits: ");
|
||||
|
@ -54,10 +54,10 @@ void get_stats(void) {
|
||||
|
||||
uint16_t fan_speed = EMC2101_get_fan_speed();
|
||||
float chip_temp = EMC2101_get_chip_temp();
|
||||
float current = INA260_read_current();
|
||||
float voltage = INA260_read_voltage();
|
||||
//float current = INA260_read_current();
|
||||
//float voltage = INA260_read_voltage();
|
||||
float power = INA260_read_power();
|
||||
uint16_t vcore = ADC_get_vcore();
|
||||
//uint16_t vcore = ADC_get_vcore();
|
||||
|
||||
// ESP_LOGI(TAG, "Fan Speed: %d RPM", fan_speed);
|
||||
// ESP_LOGI(TAG, "Chip Temp: %.2f C", chip_temp);
|
||||
|
Loading…
x
Reference in New Issue
Block a user