Skip to content

Commit

Permalink
Update mining protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
Lbqds committed Aug 16, 2024
1 parent 46e0c40 commit 1715dbf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
#define parallel_mining_works_per_gpu 4
#define max_worker_num (max_gpu_num * parallel_mining_works_per_gpu)
#define mining_steps 5000
#define mining_protocol_version 1

#endif // ALEPHIUM_CONSTANTS_H
10 changes: 9 additions & 1 deletion src/main.cu
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,15 @@ void on_read(uv_stream_t *server, ssize_t nread, const uv_buf_t *buf)
break;

case SUBMIT_RESULT:
LOG("submitted: %d -> %d: %d \n", message->submit_result->from_group, message->submit_result->to_group, message->submit_result->status);
char *block_hash_hex = bytes_to_hex(message->submit_result->block_hash, 32);
LOG(
"submitted: %d -> %d, %s: %d \n",
message->submit_result->from_group,
message->submit_result->to_group,
block_hash_hex,
message->submit_result->status
);
free(block_hash_hex);
break;
}
free_server_message_except_jobs(message);
Expand Down
17 changes: 17 additions & 0 deletions src/messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <stdlib.h>

#include "log.h"
#include "constants.h"

typedef struct blob_t {
uint8_t *blob;
Expand Down Expand Up @@ -79,6 +80,7 @@ typedef struct job_t {
blob_t header_blob;
blob_t txs_blob;
blob_t target;
int height;
} job_t;

void free_job(job_t *job) {
Expand All @@ -104,6 +106,7 @@ void free_jobs(jobs_t *jobs)
typedef struct submit_result_t {
int from_group;
int to_group;
uint8_t block_hash[32];
bool status;
} submit_result_t;

Expand Down Expand Up @@ -218,6 +221,7 @@ void extract_job(uint8_t **bytes, job_t *job)
extract_blob(bytes, &job->header_blob);
extract_blob(bytes, &job->txs_blob);
extract_blob(bytes, &job->target);
job->height = extract_size(bytes);
}

void extract_jobs(uint8_t **bytes, jobs_t *jobs)
Expand All @@ -234,10 +238,17 @@ void extract_jobs(uint8_t **bytes, jobs_t *jobs)
}
}

void extract_block_hash(uint8_t **bytes, uint8_t *block_hash)
{
memcpy(block_hash, *bytes, 32);
*bytes = *bytes + 32;
}

void extract_submit_result(uint8_t **bytes, submit_result_t *result)
{
result->from_group = extract_size(bytes);
result->to_group = extract_size(bytes);
extract_block_hash(bytes, result->block_hash);
result->status = extract_bool(bytes);
}

Expand All @@ -259,6 +270,12 @@ server_message_t *decode_server_message(blob_t *blob)
return NULL; // not enough bytes for decoding
}

uint8_t version = extract_byte(&pos);
if (version != mining_protocol_version) {
LOG("Invalid protocol version %d, expect %d\n", version, mining_protocol_version);
exit(1);
}

server_message_t *server_message = (server_message_t *)malloc(sizeof(server_message_t));
switch (extract_byte(&pos))
{
Expand Down
3 changes: 2 additions & 1 deletion src/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,10 @@ ssize_t write_new_block(mining_worker_t *worker, uint8_t *write_buf) {
uint8_t *write_pos = write_buf;

ssize_t block_size = 24 + job->header_blob.len + job->txs_blob.len;
ssize_t message_size = 1 + 4 + block_size;
ssize_t message_size = 1 + 1 + 4 + block_size;

write_size(&write_pos, message_size);
write_byte(&write_pos, mining_protocol_version); // version
write_byte(&write_pos, 0); // message type
write_size(&write_pos, block_size);
write_bytes(&write_pos, nonce, 24);
Expand Down

0 comments on commit 1715dbf

Please sign in to comment.