proper job management

This commit is contained in:
shufps 2024-09-09 07:38:02 +02:00
parent 62503f4af8
commit 0931960ac1
No known key found for this signature in database
GPG Key ID: 371CB8C24D8CB455
7 changed files with 56 additions and 33 deletions

View File

@ -337,17 +337,15 @@ void BM1397_set_job_difficulty_mask(int difficulty)
_send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), job_difficulty_mask, 6, BM1937_SERIALTX_DEBUG);
}
static uint8_t id = 0;
uint8_t BM1397_send_work(bm_job_t *next_bm_job)
void BM1397_send_work(bm_job_t *next_bm_job, uint8_t job_id)
{
job_packet job;
// max job number is 128
// there is still some really weird logic with the job id bits for the asic to sort out
// so we have it limited to 128 and it has to increment by 4
id = (id + 4) % 128;
job.job_id = id;
job.job_id = (job_id * 4) % 128;
job.num_midstates = next_bm_job->num_midstates;
memcpy(&job.starting_nonce, &next_bm_job->starting_nonce, 4);
memcpy(&job.nbits, &next_bm_job->target, 4);
@ -363,8 +361,6 @@ uint8_t BM1397_send_work(bm_job_t *next_bm_job)
}
_send_BM1397((TYPE_JOB | GROUP_SINGLE | CMD_WRITE), (uint8_t*) &job, sizeof(job_packet), BM1397_DEBUG_WORK);
return id;
}
asic_result *BM1397_receive_work(uint16_t timeout)
@ -394,7 +390,7 @@ asic_result *BM1397_receive_work(uint16_t timeout)
return (asic_result *)asic_response_buffer;
}
task_result *BM1397_proccess_work(bm_job_t *job, uint16_t timeout)
task_result *BM1397_proccess_work(uint32_t version, uint16_t timeout)
{
asic_result *asic_result = BM1397_receive_work(timeout);
@ -408,10 +404,10 @@ task_result *BM1397_proccess_work(bm_job_t *job, uint16_t timeout)
uint8_t nonce_found = 0;
uint32_t first_nonce = 0;
uint8_t rx_job_id = asic_result->job_id & 0xfc;
uint8_t rx_job_id = (asic_result->job_id & 0xfc) >> 2;
uint8_t rx_midstate_index = asic_result->job_id & 0x03;
uint32_t rolled_version = job->version;
uint32_t rolled_version = version;
for (int i = 0; i < rx_midstate_index; i++)
{
rolled_version = increment_bitmask(rolled_version, 0x1fffe000);
@ -446,3 +442,4 @@ task_result *BM1397_proccess_work(bm_job_t *job, uint16_t timeout)
return &result;
}

View File

@ -46,10 +46,10 @@ typedef struct __attribute__((__packed__))
uint8_t BM1397_init(uint64_t frequency, uint16_t asic_count);
uint8_t BM1397_send_work(bm_job_t * next_bm_job);
void BM1397_send_work(bm_job_t * next_bm_job, uint8_t job_id);
void BM1397_set_job_difficulty_mask(int);
int BM1397_set_max_baud(void);
int BM1397_set_default_baud(void);
void BM1397_send_hash_frequency(float frequency);
task_result *BM1397_proccess_work(bm_job_t *job, uint16_t timeout);
task_result *BM1397_proccess_work(uint32_t version, uint16_t timeout);

View File

@ -159,10 +159,17 @@ void asic_create_job(mining_subscribe *mWorker, mining_job *job, bm_job_t *next_
//next_job->pool_diff = stratum_difficulty;
}
uint8_t asic_send_work(bm_job_t *next_bm_job) {
return BM1397_send_work(next_bm_job);
void asic_send_work(bm_job_t *next_bm_job, uint8_t job_id) {
BM1397_send_work(next_bm_job, job_id);
}
task_result *asic_proccess_work(bm_job_t *job, uint16_t timeout) {
return BM1397_proccess_work(job, timeout);
task_result *asic_proccess_work(uint32_t version, uint16_t timeout) {
return BM1397_proccess_work(version, timeout);
}
void asic_free_bm_job(bm_job_t *job) {
free(job->jobid);
free(job->extranonce2);
// mark as free
job->ntime = 0;
}

View File

@ -26,7 +26,8 @@ typedef struct
} bm_job_t;
uint8_t asic_send_work(bm_job_t *next_bm_job);
task_result *asic_proccess_work(bm_job_t *job, uint16_t timeout);
void asic_send_work(bm_job_t *next_bm_job, uint8_t job_id);
task_result *asic_proccess_work(uint32_t version, uint16_t timeout);
double asic_test_nonce_value(const bm_job_t *job, const uint32_t nonce, const uint32_t rolled_version);
void asic_free_bm_job(bm_job_t *job);

View File

@ -374,11 +374,13 @@ void runMiner(void * task_id) {
void asic_create_job(mining_subscribe *mWorker, mining_job *job, bm_job_t *next_job, uint32_t extranonce_2);
// we can have 32 different job ids
bm_job_t asic_jobs[32];
void runASIC(void * task_id) {
Serial.printf("[MINER] Started runASIC Task!\n");
uint32_t extranonce_2 = 0;
bm_job_t asic_job;
while(1) {
// wait for new job
while(1) {
@ -396,33 +398,49 @@ void runASIC(void * task_id) {
Serial.println(">>> STARTING TO HASH NONCES");
uint32_t startT = micros();
while (mMiner.inRun) {
memset(asic_jobs, 0, sizeof(asic_jobs));
// we are assuming the version doesn't change from job to job
uint32_t version = strtoul(mJob.version.c_str(), NULL, 16);
mMonitor.NerdStatus = NM_hashing;
while (mMiner.inRun) {
extranonce_2++;
// use extranonce2 as job id
uint8_t asic_job_id = (uint8_t) (extranonce_2 % 32);
// if it was used before, we have to free the pointers
if (asic_jobs[asic_job_id].ntime) {
asic_free_bm_job(&asic_jobs[asic_job_id]);
}
// create the next asic job
asic_create_job(&mWorker, &mJob, &asic_job, extranonce_2);
asic_create_job(&mWorker, &mJob, &asic_jobs[asic_job_id], extranonce_2);
// send the job and
uint8_t asic_job_id = asic_send_work(&asic_job);
asic_send_work(&asic_jobs[asic_job_id], asic_job_id);
// wait 30ms for the response
task_result *result = asic_proccess_work(&asic_job, 30);
// the pointer returned is the RS232 receive buffer :shushing-face:
// but we only have a single thread so it should be okay
task_result *result = asic_proccess_work(version, 30);
// if we haven't received anything in time, so send a new job
if (!result) {
// we haven't received anything in time, so send a new job
continue;
}
if (result->job_id != asic_job_id) {
// job id mismatch
Serial.printf("ID mismatch, expected %02x, got %02x\n", asic_job_id, result->job_id);
// if we have received a job we don't know
if (!asic_jobs[result->job_id].ntime) {
Serial.printf("No Job found for received ID %02x\n", result->job_id);
continue;
}
// check the nonce difficulty
double diff_hash = asic_test_nonce_value(
&asic_job,
&asic_jobs[result->job_id],
result->nonce,
result->rolled_version);
@ -435,7 +453,7 @@ void runASIC(void * task_id) {
if(diff_hash > mMiner.poolDifficulty)
{
tx_mining_submit_with_version(client, mWorker, &asic_job, extranonce_2, result->nonce, result->rolled_version);
tx_mining_submit_asic(client, mWorker, &asic_jobs[result->job_id], result);
Serial.println("valid share!");
/*
Serial.print(" - Current diff share: "); Serial.println(diff_hash,12);

View File

@ -230,7 +230,7 @@ bool tx_mining_submit(WiFiClient& client, mining_subscribe mWorker, mining_job m
return true;
}
bool tx_mining_submit_with_version(WiFiClient& client, mining_subscribe mWorker, const bm_job_t* asic_job, uint32_t extranonce2, unsigned long nonce, uint32_t version)
bool tx_mining_submit_asic(WiFiClient& client, mining_subscribe mWorker, const bm_job_t* asic_job, task_result *result)
{
char payload[BUFFER] = {0};
@ -242,8 +242,8 @@ bool tx_mining_submit_with_version(WiFiClient& client, mining_subscribe mWorker,
asic_job->jobid,
asic_job->extranonce2,
asic_job->ntime,
nonce,
version ^ asic_job->version
result->nonce,
result->rolled_version ^ asic_job->version
);
Serial.print(" Sending : "); Serial.print(payload);
client.print(payload);

View File

@ -62,7 +62,7 @@ bool parse_mining_notify(String line, mining_job& mJob);
//Method Mining.submit
bool tx_mining_submit(WiFiClient& client, mining_subscribe mWorker, mining_job mJob, unsigned long nonce);
bool tx_mining_submit_with_version(WiFiClient& client, mining_subscribe mWorker, const bm_job_t* asic_job, uint32_t extranonce2, unsigned long nonce, uint32_t version);
bool tx_mining_submit_asic(WiFiClient& client, mining_subscribe mWorker, const bm_job_t* asic_job, task_result *result);
//Difficulty Methods
bool tx_suggest_difficulty(WiFiClient& client, double difficulty);