mirror of
https://github.com/skot/ESP-Miner.git
synced 2025-04-11 05:19:28 +02:00
miner: generate more bm_jobs using extranonce 2
This commit is contained in:
parent
9c9c1ad540
commit
9cf8ee9b3b
@ -75,31 +75,19 @@ bm_job construct_bm_job(uint32_t version, const char * prev_block_hash, const ch
|
||||
memcpy(midstate_data + 36, merkle_root_bin, 28); //copy merkle_root
|
||||
// printf("midstate_data: ");
|
||||
// prettyHex(midstate_data, 64);
|
||||
// printf("\n");
|
||||
// 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;
|
||||
}
|
||||
|
||||
static uint32_t extranonce_2;
|
||||
static uint32_t extranonce_2_max_number;
|
||||
static uint32_t extranonce_2_len;
|
||||
|
||||
void init_extranonce_2_generation(uint32_t length, uint64_t starting_nonce)
|
||||
char * extranonce_2_generate(uint32_t extranonce_2, uint32_t length)
|
||||
{
|
||||
extranonce_2_len = length;
|
||||
extranonce_2 = starting_nonce;
|
||||
extranonce_2_max_number = UINT_MAX;
|
||||
}
|
||||
|
||||
char * extranonce_2_generate()
|
||||
{
|
||||
char * extranonce_2_str = malloc(extranonce_2_len * 2 + 1);
|
||||
bin2hex((uint8_t *) &extranonce_2, extranonce_2_len, extranonce_2_str, extranonce_2_len * 2 + 1);
|
||||
extranonce_2 = (extranonce_2 + 1) % (extranonce_2_max_number);
|
||||
char * extranonce_2_str = malloc(length * 2 + 1);
|
||||
bin2hex((uint8_t *) &extranonce_2, length, extranonce_2_str, length * 2 + 1);
|
||||
return extranonce_2_str;
|
||||
}
|
@ -77,24 +77,23 @@ TEST_CASE("Validate bm job construction", "[mining]")
|
||||
|
||||
TEST_CASE("Test extranonce 2 generation", "[mining extranonce2]")
|
||||
{
|
||||
init_extranonce_2_generation(4, 0);
|
||||
char * first = extranonce_2_generate();
|
||||
char * first = extranonce_2_generate(0, 4);
|
||||
TEST_ASSERT_EQUAL_STRING("00000000", first);
|
||||
free(first);
|
||||
|
||||
char * second = extranonce_2_generate();
|
||||
char * second = extranonce_2_generate(1, 4);
|
||||
TEST_ASSERT_EQUAL_STRING("01000000", second);
|
||||
free(second);
|
||||
|
||||
char * third = extranonce_2_generate();
|
||||
char * third = extranonce_2_generate(2, 4);
|
||||
TEST_ASSERT_EQUAL_STRING("02000000", third);
|
||||
free(third);
|
||||
|
||||
init_extranonce_2_generation(4, UINT_MAX - 1);
|
||||
char * fourth = extranonce_2_generate();
|
||||
char * fourth = extranonce_2_generate(UINT_MAX - 1, 4);
|
||||
TEST_ASSERT_EQUAL_STRING("feffffff", fourth);
|
||||
free(fourth);
|
||||
|
||||
char * fifth = extranonce_2_generate();
|
||||
TEST_ASSERT_EQUAL_STRING("00000000", fifth);
|
||||
|
||||
init_extranonce_2_generation(6, UINT_MAX / 2);
|
||||
char * sixth = extranonce_2_generate();
|
||||
TEST_ASSERT_EQUAL_STRING("ffffff7f0000", sixth);
|
||||
char * fifth = extranonce_2_generate(UINT_MAX / 2, 6);
|
||||
TEST_ASSERT_EQUAL_STRING("ffffff7f0000", fifth);
|
||||
free(fifth);
|
||||
}
|
64
main/miner.c
64
main/miner.c
@ -46,6 +46,8 @@ static int extranonce_2_len = 0;
|
||||
|
||||
static int sock;
|
||||
|
||||
static int abandon_work = 0;
|
||||
|
||||
static void AsicTask(void * pvParameters)
|
||||
{
|
||||
init_serial();
|
||||
@ -114,37 +116,48 @@ static void mining_task(void * pvParameters)
|
||||
|
||||
mining_notify params = parse_mining_notify_message(next_notify_json_str);
|
||||
|
||||
char extranonce_2[extranonce_2_len * 2 + 1];
|
||||
memset(extranonce_2, '9', extranonce_2_len * 2);
|
||||
extranonce_2[extranonce_2_len * 2] = '\0';
|
||||
uint32_t extranonce_2 = 0;
|
||||
while (extranonce_2 < UINT_MAX || abandon_work == 1)
|
||||
{
|
||||
|
||||
char * coinbase_tx = construct_coinbase_tx(params.coinbase_1, params.coinbase_2,
|
||||
extranonce_str, extranonce_2);
|
||||
ESP_LOGI(TAG, "Coinbase tx: %s", coinbase_tx);
|
||||
char * extranonce_2_str = extranonce_2_generate(extranonce_2, extranonce_2_len);
|
||||
|
||||
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);
|
||||
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);
|
||||
|
||||
bm_job next_job = construct_bm_job(params.version, params.prev_block_hash, merkle_root,
|
||||
params.ntime, params.target);
|
||||
|
||||
ESP_LOGI(TAG, "bm_job: ");
|
||||
// print_hex((uint8_t *) &next_job.target, 4, 4, "nbits: ");
|
||||
// print_hex((uint8_t *) &next_job.ntime, 4, 4, "ntime: ");
|
||||
// print_hex((uint8_t *) &next_job.merkle_root_end, 4, 4, "merkle root end: ");
|
||||
print_hex(next_job.midstate, 32, 32, "midstate: ");
|
||||
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 * queued_next_job = malloc(sizeof(bm_job));
|
||||
memcpy(queued_next_job, &next_job, sizeof(bm_job));
|
||||
queued_next_job->extranonce2 = strdup(extranonce_2);
|
||||
queued_next_job->jobid = strdup(params.job_id);
|
||||
queue_enqueue(&g_bm_queue, queued_next_job);
|
||||
bm_job next_job = construct_bm_job(params.version, params.prev_block_hash, merkle_root,
|
||||
params.ntime, params.target);
|
||||
|
||||
//ESP_LOGI(TAG, "bm_job: ");
|
||||
// print_hex((uint8_t *) &next_job.target, 4, 4, "nbits: ");
|
||||
// print_hex((uint8_t *) &next_job.ntime, 4, 4, "ntime: ");
|
||||
// print_hex((uint8_t *) &next_job.merkle_root_end, 4, 4, "merkle root end: ");
|
||||
//print_hex(next_job.midstate, 32, 32, "midstate: ");
|
||||
|
||||
bm_job * queued_next_job = malloc(sizeof(bm_job));
|
||||
memcpy(queued_next_job, &next_job, sizeof(bm_job));
|
||||
queued_next_job->extranonce2 = strdup(extranonce_2_str);
|
||||
queued_next_job->jobid = strdup(params.job_id);
|
||||
queue_enqueue(&g_bm_queue, queued_next_job);
|
||||
|
||||
free(coinbase_tx);
|
||||
free(merkle_root);
|
||||
free(extranonce_2_str);
|
||||
extranonce_2++;
|
||||
}
|
||||
|
||||
if (abandon_work == 1) {
|
||||
abandon_work = 0;
|
||||
queue_clear(&g_bm_queue);
|
||||
}
|
||||
|
||||
free_mining_notify(params);
|
||||
free(coinbase_tx);
|
||||
free(merkle_root);
|
||||
free(next_notify_json_str);
|
||||
}
|
||||
}
|
||||
@ -226,6 +239,7 @@ static void admin_task(void *pvParameters)
|
||||
if (method == MINING_NOTIFY) {
|
||||
if (should_abandon_work(line)) {
|
||||
queue_clear(&g_queue);
|
||||
abandon_work = 1;
|
||||
}
|
||||
queue_enqueue(&g_queue, line);
|
||||
} else {
|
||||
|
Loading…
x
Reference in New Issue
Block a user