Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make coinbaseaux flags optional #12

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ cgminer_SOURCES += klist.h klist.c

cgminer_SOURCES += noncedup.c

# Bech32 support
cgminer_SOURCES += segwit_addr.c segwit_addr.h

if NEED_FPGAUTILS
cgminer_SOURCES += fpgautils.c fpgautils.h
endif
Expand Down
71 changes: 54 additions & 17 deletions cgminer.c
Original file line number Diff line number Diff line change
Expand Up @@ -464,14 +464,18 @@ bool curses_active;
char current_hash[68];
static char prev_block[12];
static char current_block[32];
static char last_found_block[12] = "\0";

static char datestamp[40];
static char blocktime[32];
struct timeval block_timeval;
static char best_share[8] = "0";
static char best_share_ever[8] = "0";
static char best_device[12] = "n/a";
double current_diff = 0xFFFFFFFFFFFFFFFFULL;
static char block_diff[8];
uint64_t best_diff = 0;
uint64_t best_diff_ever = 0;

struct block {
char hash[68];
Expand Down Expand Up @@ -2960,7 +2964,7 @@ static bool gbt_solo_decode(struct pool *pool, json_t *res_val)
flags = json_string_value(json_object_get(coinbase_aux, "flags"));
default_witness_commitment = json_string_value(json_object_get(res_val, "default_witness_commitment"));

if (!previousblockhash || !target || !version || !curtime || !bits || !coinbase_aux || !flags) {
if (!previousblockhash || !target || !version || !curtime || !bits) {
applog(LOG_ERR, "Pool %d JSON failed to decode GBT", pool->pool_no);
return false;
}
Expand Down Expand Up @@ -2990,7 +2994,8 @@ static bool gbt_solo_decode(struct pool *pool, json_t *res_val)
applog(LOG_DEBUG, "curtime: %d", curtime);
applog(LOG_DEBUG, "bits: %s", bits);
applog(LOG_DEBUG, "height: %d", height);
applog(LOG_DEBUG, "flags: %s", flags);
if (flags)
applog(LOG_DEBUG, "flags: %s", flags);

cg_wlock(&pool->gbt_lock);
hex2bin(hash_swap, previousblockhash, 32);
Expand Down Expand Up @@ -3039,10 +3044,12 @@ static bool gbt_solo_decode(struct pool *pool, json_t *res_val)
ofs += ser_number(pool->scriptsig_base + ofs, height); // max 5

/* Followed by flags */
len = strlen(flags) / 2;
pool->scriptsig_base[ofs++] = len;
hex2bin(pool->scriptsig_base + ofs, flags, len);
ofs += len;
if (flags) {
len = strlen(flags) / 2;
pool->scriptsig_base[ofs++] = len;
hex2bin(pool->scriptsig_base + ofs, flags, len);
ofs += len;
}

/* Followed by timestamp */
cgtime(&now);
Expand Down Expand Up @@ -3080,7 +3087,7 @@ static bool gbt_solo_decode(struct pool *pool, json_t *res_val)
+ 4 // txin sequence no
+ 1 // txouts
+ 8 // value
+ 1 + 25 // txout
+ 1 + pool->script_pubkey_len // txout
+ 4; // lock

if (insert_witness) {
Expand All @@ -3097,7 +3104,7 @@ static bool gbt_solo_decode(struct pool *pool, json_t *res_val)
*u64 = htole64(coinbasevalue);

if (insert_witness) {
unsigned char *witness = &pool->coinbase[41 + ofs + 4 + 1 + 8 + 1 + 25];
unsigned char *witness = &pool->coinbase[41 + ofs + 4 + 1 + 8 + 1 + pool->script_pubkey_len];

memset(witness, 0, 8);
witness_txout_len += 8;
Expand All @@ -3110,7 +3117,7 @@ static bool gbt_solo_decode(struct pool *pool, json_t *res_val)

pool->nonce2 = 0;
pool->n2size = 4;
pool->coinbase_len = 41 + ofs + 4 + 1 + 8 + 1 + 25 + witness_txout_len + 4;
pool->coinbase_len = 41 + ofs + 4 + 1 + 8 + 1 + pool->script_pubkey_len + witness_txout_len + 4;
cg_wunlock(&pool->gbt_lock);

snprintf(header, 257, "%s%s%s%s%s%s%s",
Expand Down Expand Up @@ -3403,13 +3410,17 @@ static void curses_print_status(void)
pool->sockaddr_url, pool->diff, pool->rpc_user);
} else {
cg_mvwprintw(statuswin, 4, 0, " Connected to %s diff %s with%s %s as user %s",
pool->sockaddr_url, pool->diff, have_longpoll ? "": "out",
pool->rpc_url, pool->diff, have_longpoll ? "": "out",
pool->has_gbt ? "GBT" : "LP", pool->rpc_user);
}
wclrtoeol(statuswin);
cg_mvwprintw(statuswin, 5, 0, " Block: %s... Diff:%s Started: %s Best share: %s ",
prev_block, block_diff, blocktime, best_share);
mvwhline(statuswin, 6, 0, '-', linewidth);
cg_mvwprintw(statuswin, 5, 0, " Block: %s... Height: %u Diff: %s Started: %s",
prev_block, pool->current_height, block_diff, blocktime);
wclrtoeol(statuswin);
cg_mvwprintw(statuswin, 6, 0, " Last block found: %s... Best share (ever): %s [%s] (%s)",
last_found_block, best_share, best_device, best_share_ever);
wclrtoeol(statuswin);
mvwhline(statuswin, 7, 0, '-', linewidth);
mvwhline(statuswin, statusy - 1, 0, '-', linewidth);
#ifdef USE_USBUTILS
cg_mvwprintw(statuswin, devcursor - 1, 1, "[U]SB management [P]ool management [S]ettings [D]isplay options [Q]uit");
Expand Down Expand Up @@ -3854,6 +3865,9 @@ static void show_hash(struct work *work, char *hashshow)
suffix_string(work->share_diff, diffdisp, sizeof (diffdisp), 0);
snprintf(hashshow, 64, "%08lx Diff %s/%"PRIu64"%s", h32, diffdisp, uintdiff,
work->block? " BLOCK!" : "");
// update last found block
if (work->block)
snprintf(last_found_block, 12, "%08lx", h32);
}

#ifdef HAVE_LIBCURL
Expand Down Expand Up @@ -5021,6 +5035,9 @@ uint64_t share_diff(const struct work *work)
double d64, s64;
uint64_t ret;

int thr_id;
struct cgpu_info *cgpu;

d64 = truediffone;
s64 = le256todouble(work->hash);
if (unlikely(!s64))
Expand All @@ -5033,6 +5050,14 @@ uint64_t share_diff(const struct work *work)
new_best = true;
best_diff = ret;
suffix_string(best_diff, best_share, sizeof(best_share), 0);
if (unlikely(best_diff > best_diff_ever)) {
best_diff_ever = best_diff;
suffix_string(best_diff, best_share_ever, sizeof(best_share), 0);
}

thr_id = work->thr_id;
cgpu = get_thr_cgpu(thr_id);
snprintf(best_device, sizeof(best_device), "%s %u", cgpu->drv->name, cgpu->device_id);
}
if (unlikely(ret > work->pool->best_diff))
work->pool->best_diff = ret;
Expand Down Expand Up @@ -5747,6 +5772,8 @@ void zero_bestshare(void)
best_diff = 0;
memset(best_share, 0, 8);
suffix_string(best_diff, best_share, sizeof(best_share), 0);
memset(best_device, 0, 12);
snprintf(best_device, sizeof(best_device), "n/a");

for (i = 0; i < total_pools; i++) {
struct pool *pool = pools[i];
Expand Down Expand Up @@ -7101,8 +7128,9 @@ static void __setup_gbt_solo(struct pool *pool)
{
cg_wlock(&pool->gbt_lock);
cg_memcpy(pool->coinbase, scriptsig_header_bin, 41);
pool->coinbase[41 + pool->n1_len + 4 + 1 + 8] = 25;
cg_memcpy(pool->coinbase + 41 + pool->n1_len + 4 + 1 + 8 + 1, pool->script_pubkey, 25);
applog(LOG_DEBUG, "script_pubkey_len=%d", pool->script_pubkey_len);
pool->coinbase[41 + pool->n1_len + 4 + 1 + 8] = pool->script_pubkey_len;
cg_memcpy(pool->coinbase + 41 + pool->n1_len + 4 + 1 + 8 + 1, pool->script_pubkey, pool->script_pubkey_len);
cg_wunlock(&pool->gbt_lock);
}

Expand All @@ -7120,6 +7148,11 @@ static bool setup_gbt_solo(CURL *curl, struct pool *pool)
}
goto out;
}
// check for P2PKH or BECH32 address format
if ((strncmp(opt_btc_address, "1", 1) != 0) && (!addr_format_is_bech32(opt_btc_address))) {
applog(LOG_ERR, "Invalid Bitcoin address %s, only P2PKH (1...) or BECH32 (bc1...) format is supported for solo mining", opt_btc_address);
goto out;
}
snprintf(s, 256, "{\"id\": 1, \"method\": \"validateaddress\", \"params\": [\"%s\"]}\n", opt_btc_address);
val = json_rpc_call(curl, pool->rpc_url, pool->rpc_userpass, s, true,
false, &rolltime, pool, false);
Expand All @@ -7137,7 +7170,8 @@ static bool setup_gbt_solo(CURL *curl, struct pool *pool)
}
applog(LOG_NOTICE, "Solo mining to valid address: %s", opt_btc_address);
ret = true;
address_to_pubkeyhash(pool->script_pubkey, opt_btc_address);
address_to_pubkeyhash(pool->script_pubkey, &pool->script_pubkey_len, opt_btc_address);
applog(LOG_DEBUG, "script_pubkey: %s", bin2hex(pool->script_pubkey, pool->script_pubkey_len));
hex2bin(scriptsig_header_bin, scriptsig_header, 41);
__setup_gbt_solo(pool);

Expand Down Expand Up @@ -7939,6 +7973,7 @@ bool test_nonce_diff(struct work *work, uint32_t nonce, double diff)
static void update_work_stats(struct thr_info *thr, struct work *work)
{
double test_diff = current_diff;
char *work_hash;

work->share_diff = share_diff(work);

Expand All @@ -7948,6 +7983,8 @@ static void update_work_stats(struct thr_info *thr, struct work *work)
found_blocks++;
work->mandatory = true;
applog(LOG_NOTICE, "Found block for pool %d!", work->pool->pool_no);
// reset best_share stats when block was found
zero_bestshare();
}

mutex_lock(&stats_lock);
Expand Down Expand Up @@ -10128,7 +10165,7 @@ int main(int argc, char *argv[])
free(s);
strcat(cgminer_path, "/");

devcursor = 8;
devcursor = 9;
logstart = devcursor + 1;
logcursor = logstart + 1;

Expand Down
5 changes: 4 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##
m4_define([v_maj], [4])
m4_define([v_min], [11])
m4_define([v_mic], [1])
m4_define([v_mic], [2])
##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##
m4_define([v_ver], [v_maj.v_min.v_mic])
m4_define([lt_rev], m4_eval(v_maj + v_min))
Expand Down Expand Up @@ -85,6 +85,9 @@ esac
#CFLAGS="$CFLAGS -Wimplicit-fallthrough=0"
#^ causes problem on gcc 4.9 - pass in from CFLAG if needed

# Explicitly set -fcommon for gcc 10+
CFLAGS="$CFLAGS -fcommon"

case $target in
*-*-linux-gnu*)
have_linux=true
Expand Down
2 changes: 1 addition & 1 deletion logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern bool want_per_device_stats;
/* global log_level, messages with lower or equal prio are logged */
extern int opt_log_level;

#define LOGBUFSIZ 256
#define LOGBUFSIZ 320

extern void _applog(int prio, const char *str, bool force);
extern void _simplelog(int prio, const char *str, bool force);
Expand Down
3 changes: 2 additions & 1 deletion miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,8 @@ struct pool {
int transactions;
char *txn_data;
unsigned char scriptsig_base[100];
unsigned char script_pubkey[25 + 3];
unsigned char script_pubkey[42 + 3];
int script_pubkey_len;
int nValue;
CURL *gbt_curl;
bool gbt_curl_inuse;
Expand Down
Loading