Skip to content

Commit

Permalink
phi2 changes to handle lux smart contracts
Browse files Browse the repository at this point in the history
adds a smart contract roots field (stateroot+utxoroot) to mining.notify

like lbry do with their claimtrie.

These fields are optional, means there are 2 variants of the phi2 algo

Signed-off-by: Tanguy Pruvot <[email protected]>
  • Loading branch information
tpruvot committed Jun 21, 2018
1 parent 9038f09 commit 27176b2
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 24 deletions.
8 changes: 4 additions & 4 deletions stratum/algos/phi2.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

void phi2_hash(const char* input, char* output, uint32_t len)
{
unsigned char _ALIGN(128) hash[128] = { 0 };
unsigned char _ALIGN(128) hashA[64] = { 0 };
unsigned char _ALIGN(128) hashB[64] = { 0 };
unsigned char _ALIGN(128) hash[64];
unsigned char _ALIGN(128) hashA[64];
unsigned char _ALIGN(128) hashB[64];

sph_cubehash512_context ctx_cubehash;
sph_jh512_context ctx_jh;
Expand All @@ -27,7 +27,7 @@ void phi2_hash(const char* input, char* output, uint32_t len)
sph_skein512_context ctx_skein;

sph_cubehash512_init(&ctx_cubehash);
sph_cubehash512(&ctx_cubehash, input, 80);
sph_cubehash512(&ctx_cubehash, input, len);
sph_cubehash512_close(&ctx_cubehash, (void*)hashB);

LYRA2(&hashA[ 0], 32, &hashB[ 0], 32, &hashB[ 0], 32, 1, 8, 8);
Expand Down
45 changes: 29 additions & 16 deletions stratum/client_submit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ void build_submit_values(YAAMP_JOB_VALUES *submitvalues, YAAMP_JOB_TEMPLATE *tem
#ifdef MERKLE_DEBUGLOG
printf("merkle root %s\n", merkleroot.c_str());
#endif
if (!strcmp(g_current_algo->name, "lbry")) {
if (!strcmp(g_stratum_algo, "lbry")) {
sprintf(submitvalues->header, "%s%s%s%s%s%s%s", templ->version, templ->prevhash_be, submitvalues->merkleroot_be,
templ->claim_be, ntime, templ->nbits, nonce);
ser_string_be(submitvalues->header, submitvalues->header_be, 32 + 20);
ser_string_be(submitvalues->header, submitvalues->header_be, 112/4);
} else if (strlen(templ->extradata_be) == 128) { // LUX SC
sprintf(submitvalues->header, "%s%s%s%s%s%s%s", templ->version, templ->prevhash_be, submitvalues->merkleroot_be,
ntime, templ->nbits, nonce, templ->extradata_be);
ser_string_be(submitvalues->header, submitvalues->header_be, 36); // 80+64 / sizeof(u32)
} else {
sprintf(submitvalues->header, "%s%s%s%s%s%s", templ->version, templ->prevhash_be, submitvalues->merkleroot_be,
ntime, templ->nbits, nonce);
Expand Down Expand Up @@ -355,15 +359,11 @@ bool client_submit(YAAMP_CLIENT *client, json_value *json_params)
return false;
}

char extranonce2[32];
char ntime[32];
char nonce[32];
char vote[8];

memset(extranonce2, 0, 32);
memset(ntime, 0, 32);
memset(nonce, 0, 32);
memset(vote, 0, 8);
char extranonce2[32] = { 0 };
char extra[160] = { 0 };
char nonce[80] = { 0 };
char ntime[32] = { 0 };
char vote[8] = { 0 };

if (!json_params->u.array.values[1]->u.string.ptr || strlen(json_params->u.array.values[1]->u.string.ptr) > 32) {
clientlog(client, "bad json, wrong jobid len");
Expand All @@ -378,15 +378,28 @@ bool client_submit(YAAMP_CLIENT *client, json_value *json_params)
if (json_params->u.array.length == 6)
strncpy(vote, json_params->u.array.values[5]->u.string.ptr, 7);

if (g_debuglog_hash) {
debuglog("submit %s (uid %d) %d, %s, %s, %s\n", client->sock->ip, client->userid, jobid, extranonce2, ntime, nonce);
}

string_lower(extranonce2);
string_lower(ntime);
string_lower(nonce);
string_lower(vote);

if (json_params->u.array.length == 6) {
if (strstr(g_stratum_algo, "phi")) {
// lux optional field, smart contral root hashes (not mandatory on shares submit)
strncpy(extra, json_params->u.array.values[5]->u.string.ptr, 128);
string_lower(extra);
} else {
// heavycoin vote
strncpy(vote, json_params->u.array.values[5]->u.string.ptr, 7);
string_lower(vote);
}
}

if (g_debuglog_hash) {
debuglog("submit %s (uid %d) %d, %s, t=%s, n=%s, extra=%s\n", client->sock->ip, client->userid,
jobid, extranonce2, ntime, nonce, extra);
}

YAAMP_JOB *job = (YAAMP_JOB *)object_find(&g_list_job, jobid, true);
if(!job)
{
Expand All @@ -413,7 +426,7 @@ bool client_submit(YAAMP_CLIENT *client, json_value *json_params)

if(strcmp(ntime, templ->ntime))
{
if (!ntime_valid_range(ntime)) {
if (!ishexa(ntime, 8) || !ntime_valid_range(ntime)) {
client_submit_error(client, job, 23, "Invalid time rolling", extranonce2, ntime, nonce);
return true;
}
Expand Down
12 changes: 12 additions & 0 deletions stratum/coind_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,18 @@ YAAMP_JOB_TEMPLATE *coind_create_template(YAAMP_COIND *coind)
}
}

const char *sc_root = json_get_string(json_result, "stateroot");
const char *sc_utxo = json_get_string(json_result, "utxoroot");
if (sc_root && sc_utxo) {
// LUX Smart Contracts, 144-bytes block headers
strcpy(&templ->extradata_hex[ 0], sc_root); // 32-bytes hash (64 in hexa)
strcpy(&templ->extradata_hex[64], sc_utxo); // 32-bytes hash too

// same weird byte order as previousblockhash field
ser_string_be2(sc_root, &templ->extradata_be[ 0], 8);
ser_string_be2(sc_utxo, &templ->extradata_be[64], 8);
}

if (strcmp(coind->rpcencoding, "DCR") == 0) {
decred_fix_template(coind, templ, json_result);
}
Expand Down
8 changes: 6 additions & 2 deletions stratum/job.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ struct YAAMP_JOB_TEMPLATE
int created;
char flags[64];

char prevhash_hex[1024];
char prevhash_be[1024];
char prevhash_hex[512];
char prevhash_be[512];

char extradata_hex[512];
char extradata_be[512];

// todo: can use extra field
char claim_hex[128];
char claim_be[128];

Expand Down
10 changes: 9 additions & 1 deletion stratum/job_send.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@ static void job_mining_notify_buffer(YAAMP_JOB *job, char *buffer)
{
YAAMP_JOB_TEMPLATE *templ = job->templ;

if (!strcmp(g_current_algo->name, "lbry")) {
if (!strcmp(g_stratum_algo, "lbry")) {
sprintf(buffer, "{\"id\":null,\"method\":\"mining.notify\",\"params\":["
"\"%x\",\"%s\",\"%s\",\"%s\",\"%s\",[%s],\"%s\",\"%s\",\"%s\",true]}\n",
job->id, templ->prevhash_be, templ->claim_be, templ->coinb1, templ->coinb2,
templ->txmerkles, templ->version, templ->nbits, templ->ntime);
return;
} else if (strlen(templ->extradata_hex) == 128) {
// LUX smart contract state hashes (like lbry extra field, here the 2 root hashes in one)
sprintf(buffer, "{\"id\":null,\"method\":\"mining.notify\",\"params\":["
"\"%x\",\"%s\",\"%s\",\"%s\",\"%s\",[%s],\"%s\",\"%s\",\"%s\",true]}\n",
job->id, templ->prevhash_be, templ->extradata_be, templ->coinb1, templ->coinb2,
templ->txmerkles, templ->version, templ->nbits, templ->ntime);
return;
}

// standard stratum
sprintf(buffer, "{\"id\":null,\"method\":\"mining.notify\",\"params\":[\"%x\",\"%s\",\"%s\",\"%s\",[%s],\"%s\",\"%s\",\"%s\",true]}\n",
job->id, templ->prevhash_be, templ->coinb1, templ->coinb2, templ->txmerkles, templ->version, templ->nbits, templ->ntime);
}
Expand Down
2 changes: 1 addition & 1 deletion stratum/remote_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void remote_create_job(YAAMP_REMOTE *remote, json_value *json_params)
YAAMP_JOB_TEMPLATE *templ = new YAAMP_JOB_TEMPLATE;
memset(templ, 0, sizeof(YAAMP_JOB_TEMPLATE));

strncpy(templ->prevhash_be, json_params->u.array.values[1]->u.string.ptr, 1023);
strncpy(templ->prevhash_be, json_params->u.array.values[1]->u.string.ptr, sizeof(templ->prevhash_be)-1);
strncpy(templ->coinb1, json_params->u.array.values[2]->u.string.ptr, 1023);
strncpy(templ->coinb2, json_params->u.array.values[3]->u.string.ptr, 1023);

Expand Down

0 comments on commit 27176b2

Please sign in to comment.