Move submission block assembly code to base libblkmaker

This commit is contained in:
Luke Dashjr
2014-08-24 19:35:36 +00:00
parent cd1b78f508
commit 75d4da0d9b
3 changed files with 73 additions and 61 deletions

View File

@@ -13,6 +13,12 @@
#include <time.h>
#include <unistd.h>
#ifndef WIN32
#include <arpa/inet.h>
#else
#include <winsock2.h>
#endif
#include <blkmaker.h>
#include <blktemplate.h>
@@ -416,3 +422,61 @@ unsigned long blkmk_work_left(const blktemplate_t *tmpl) {
return UINT_MAX - tmpl->next_dataid;
return BLKMK_UNLIMITED_WORK_COUNT;
}
static
char varintEncode(unsigned char *out, uint64_t n) {
if (n < 0xfd)
{
out[0] = n;
return 1;
}
char L;
if (n <= 0xffff)
{
out[0] = '\xfd';
L = 3;
}
else
if (n <= 0xffffffff)
{
out[0] = '\xfe';
L = 5;
}
else
{
out[0] = '\xff';
L = 9;
}
for (unsigned char i = 1; i < L; ++i)
out[i] = (n >> ((i - 1) * 8)) % 256;
return L;
}
char *blkmk_assemble_submission_(blktemplate_t * const tmpl, const unsigned char * const data, const unsigned int dataid, blknonce_t nonce, const bool foreign)
{
unsigned char blk[80 + 8 + 1000000];
memcpy(blk, data, 76);
nonce = htonl(nonce);
memcpy(&blk[76], &nonce, 4);
size_t offs = 80;
if (foreign || (!(tmpl->mutations & BMAb_TRUNCATE && !dataid)))
{
offs += varintEncode(&blk[offs], 1 + tmpl->txncount);
if (!_blkmk_extranonce(tmpl, &blk[offs], dataid, &offs))
return NULL;
if (foreign || !(tmpl->mutations & BMAb_COINBASE))
for (unsigned long i = 0; i < tmpl->txncount; ++i)
{
memcpy(&blk[offs], tmpl->txns[i].data, tmpl->txns[i].datasz);
offs += tmpl->txns[i].datasz;
}
}
char *blkhex = malloc((offs * 2) + 1);
_blkmk_bin2hex(blkhex, blk, offs);
return blkhex;
}

View File

@@ -10,12 +10,6 @@
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include <arpa/inet.h>
#else
#include <winsock2.h>
#endif
#include <jansson.h>
#include <blkmaker.h>
@@ -253,66 +247,17 @@ const char *blktmpl_add_jansson(blktemplate_t *tmpl, const json_t *json, time_t
return NULL;
}
static
char varintEncode(unsigned char *out, uint64_t n) {
if (n < 0xfd)
{
out[0] = n;
return 1;
}
char L;
if (n <= 0xffff)
{
out[0] = '\xfd';
L = 3;
}
else
if (n <= 0xffffffff)
{
out[0] = '\xfe';
L = 5;
}
else
{
out[0] = '\xff';
L = 9;
}
for (unsigned char i = 1; i < L; ++i)
out[i] = (n >> ((i - 1) * 8)) % 256;
return L;
}
#define my_bin2hex _blkmk_bin2hex
static
json_t *_blkmk_submit_jansson(blktemplate_t *tmpl, const unsigned char *data, unsigned int dataid, blknonce_t nonce, bool foreign) {
unsigned char blk[80 + 8 + 1000000];
memcpy(blk, data, 76);
nonce = htonl(nonce);
memcpy(&blk[76], &nonce, 4);
size_t offs = 80;
if (foreign || (!(tmpl->mutations & BMAb_TRUNCATE && !dataid)))
{
offs += varintEncode(&blk[offs], 1 + tmpl->txncount);
if (!_blkmk_extranonce(tmpl, &blk[offs], dataid, &offs))
return NULL;
if (foreign || !(tmpl->mutations & BMAb_COINBASE))
for (unsigned long i = 0; i < tmpl->txncount; ++i)
{
memcpy(&blk[offs], tmpl->txns[i].data, tmpl->txns[i].datasz);
offs += tmpl->txns[i].datasz;
}
}
char blkhex[(offs * 2) + 1];
my_bin2hex(blkhex, blk, offs);
char *blkhex = blkmk_assemble_submission_(tmpl, data, dataid, nonce, foreign);
if (!blkhex)
return NULL;
json_t *rv = json_array(), *ja, *jb;
jb = NULL;
if (!(ja = json_string(blkhex)))
ja = json_string(blkhex);
free(blkhex);
if (!ja)
goto err;
if (json_array_append_new(rv, ja))
goto err;

View File

@@ -4,8 +4,11 @@
#include <stdbool.h>
#include <string.h>
#include <blktemplate.h>
// blkmaker.c
extern bool _blkmk_dblsha256(void *hash, const void *data, size_t datasz);
extern char *blkmk_assemble_submission_(blktemplate_t *, const unsigned char *data, unsigned int dataid, blknonce_t nonce, bool foreign);
// blktemplate.c
extern void _blktxn_free(struct blktxn_t *);