From 48b1947778a82f54e4be1e76b414e89e8fb69847 Mon Sep 17 00:00:00 2001 From: kanoi Date: Sat, 31 Oct 2015 20:16:59 +1100 Subject: [PATCH] ckdb - store and use workinfo diff and height in the ram structrue --- src/ckdb.c | 8 +++---- src/ckdb.h | 10 ++++---- src/ckdb_cmd.c | 38 ++++++++++------------------- src/ckdb_data.c | 64 ++++++++++++++++++++----------------------------- src/ckdb_dbio.c | 10 ++++++-- 5 files changed, 55 insertions(+), 75 deletions(-) diff --git a/src/ckdb.c b/src/ckdb.c index 041ec653..d13997c7 100644 --- a/src/ckdb.c +++ b/src/ckdb.c @@ -2915,7 +2915,7 @@ static void summarise_blocks() if (workinfo_current) { WORKINFO *wic; DATA_WORKINFO(wic, workinfo_current); - hi = coinbase1height(wic->coinbase1); + hi = wic->height; } K_RUNLOCK(workinfo_free); @@ -3202,7 +3202,6 @@ static void make_a_shift_mark() MARKS *marks = NULL, *sh_marks = NULL; int64_t ss_age_wid, last_marks_wid, marks_wid, prev_wid; int64_t shiftdiffblock = SHIFT_DIFF_BLOCK; - int32_t height; char wi_bits[TXT_SML+1]; bool was_block = false, ok, oc_look = true; char cd_buf[DATE_BUFSIZ], cd_buf2[DATE_BUFSIZ], cd_buf3[DATE_BUFSIZ]; @@ -3452,8 +3451,7 @@ static void make_a_shift_mark() * Stop at the last workinfo, before the diff * changed */ if (strcmp(wi_bits, workinfo->bits) != 0) { - height = coinbase1height(workinfo->coinbase1); - if (height > (int32_t)shiftdiffblock) { + if (workinfo->height > (int32_t)shiftdiffblock) { LOGDEBUG("%s() OK shift stops at diff" " change '%s->%s' %"PRId64 "->%"PRId64" height %"PRId32 @@ -3461,7 +3459,7 @@ static void make_a_shift_mark() __func__, wi_bits, workinfo->bits, prev_wid, workinfo->workinfoid, - height, + workinfo->height, shiftdiffblock); marks_wid = prev_wid; break; diff --git a/src/ckdb.h b/src/ckdb.h index ab48fccb..779b70c2 100644 --- a/src/ckdb.h +++ b/src/ckdb.h @@ -55,7 +55,7 @@ #define DB_VLOCK "1" #define DB_VERSION "1.0.4" -#define CKDB_VERSION DB_VERSION"-1.500" +#define CKDB_VERSION DB_VERSION"-1.501" #define WHERE_FFL " - from %s %s() line %d" #define WHERE_FFL_HERE __FILE__, __func__, __LINE__ @@ -1336,6 +1336,8 @@ typedef struct workinfo { char bits[TXT_SML+1]; char ntime[TXT_SML+1]; int64_t reward; + int32_t height; // non-DB field + double diff_target; // non-DB field HISTORYDATECONTROLFIELDS; } WORKINFO; @@ -2299,10 +2301,8 @@ extern K_ITEM *find_optioncontrol(char *optionname, const tv_t *now, int32_t hei extern int64_t user_sys_setting(int64_t userid, char *setting_name, int64_t setting_default, const tv_t *now); extern cmp_t cmp_workinfo(K_ITEM *a, K_ITEM *b); -#define coinbase1height(_cb1) _coinbase1height(_cb1, WHERE_FFL_HERE) -extern int32_t _coinbase1height(char *coinbase1, WHERE_FFL_ARGS); -#define cmp_height(_cb1a, _cb1b) _cmp_height(_cb1a, _cb1b, WHERE_FFL_HERE) -extern cmp_t _cmp_height(char *coinbase1a, char *coinbase1b, WHERE_FFL_ARGS); +#define coinbase1height(_wi) _coinbase1height(_wi, WHERE_FFL_HERE) +extern int32_t _coinbase1height(WORKINFO *wi, WHERE_FFL_ARGS); extern cmp_t cmp_workinfo_height(K_ITEM *a, K_ITEM *b); extern K_ITEM *find_workinfo(int64_t workinfoid, K_TREE_CTX *ctx); extern K_ITEM *next_workinfo(int64_t workinfoid, K_TREE_CTX *ctx); diff --git a/src/ckdb_cmd.c b/src/ckdb_cmd.c index 5ad59a5a..9322d1ae 100644 --- a/src/ckdb_cmd.c +++ b/src/ckdb_cmd.c @@ -3187,11 +3187,9 @@ static char *cmd_homepage(__maybe_unused PGconn *conn, char *cmd, char *id, K_RLOCK(workinfo_free); if (workinfo_current) { WORKINFO *wic; - int32_t hi; DATA_WORKINFO(wic, workinfo_current); - hi = coinbase1height(wic->coinbase1); snprintf(tmp, sizeof(tmp), "lastheight=%d%c", - hi-1, FLDSEP); + wic->height-1, FLDSEP); APPEND_REALLOC(buf, off, len, tmp); } else { snprintf(tmp, sizeof(tmp), "lastheight=?%c", FLDSEP); @@ -4066,7 +4064,6 @@ static char *cmd_pplns(__maybe_unused PGconn *conn, char *cmd, char *id, double diff_add = 0.0; double diff_want; bool allow_aged = false, countbacklimit; - char ndiffbin[TXT_SML+1]; size_t len, off; int rows; @@ -4156,8 +4153,7 @@ static char *cmd_pplns(__maybe_unused PGconn *conn, char *cmd, char *id, } DATA_WORKINFO(workinfo, w_item); - hex2bin(ndiffbin, workinfo->bits, 4); - ndiff = diff_from_nbits(ndiffbin); + ndiff = workinfo->diff_target; diff_want = ndiff * diff_times + diff_add; if (diff_want < 1.0) { snprintf(reply, siz, @@ -4527,8 +4523,6 @@ static char *cmd_pplns2(__maybe_unused PGconn *conn, char *cmd, char *id, PAYOUTS *payouts; BLOCKS lookblocks, *blocks; WORKINFO *bworkinfo, *workinfo; - char ndiffbin[TXT_SML+1]; - double ndiff; USERS *users; int32_t height; K_TREE_CTX b_ctx[1], mp_ctx[1], pay_ctx[1]; @@ -4759,9 +4753,8 @@ static char *cmd_pplns2(__maybe_unused PGconn *conn, char *cmd, char *id, APPEND_REALLOC(buf, off, len, tmp); snprintf(tmp, sizeof(tmp), "%s%c", payouts->stats, FLDSEP); APPEND_REALLOC(buf, off, len, tmp); - hex2bin(ndiffbin, bworkinfo->bits, 4); - ndiff = diff_from_nbits(ndiffbin); - snprintf(tmp, sizeof(tmp), "block_ndiff=%f%c", ndiff, FLDSEP); + snprintf(tmp, sizeof(tmp), "block_ndiff=%f%c", + bworkinfo->diff_target, FLDSEP); APPEND_REALLOC(buf, off, len, tmp); snprintf(tmp, sizeof(tmp), "diff_want=%.1f%c", payouts->diffwanted, FLDSEP); @@ -6738,7 +6731,6 @@ static char *cmd_query(__maybe_unused PGconn *conn, char *cmd, char *id, * pgsql scripts would do that to the DB, then ckdb would * load them the next time it (re)starts */ K_ITEM *i_wid, *i_expired, *wi_item, *wm_item; - char ndiffbin[TXT_SML+1]; bool expired = false; WORKINFO *workinfo; WORKMARKERS *wm; @@ -6776,7 +6768,7 @@ static char *cmd_query(__maybe_unused PGconn *conn, char *cmd, char *id, "workinfoid:%d=%s%c", rows, reply, FLDSEP); APPEND_REALLOC(buf, off, len, tmp); - int_to_buf(coinbase1height(workinfo->coinbase1), + int_to_buf(workinfo->height, reply, sizeof(reply)); snprintf(tmp, sizeof(tmp), "height:%d=%s%c", @@ -6798,10 +6790,9 @@ static char *cmd_query(__maybe_unused PGconn *conn, char *cmd, char *id, CDDB"_str:%d=%s%c", rows, cd_buf, FLDSEP); APPEND_REALLOC(buf, off, len, tmp); - hex2bin(ndiffbin, workinfo->bits, 4); snprintf(tmp, sizeof(tmp), "ndiff:%d=%.1f%c", rows, - diff_from_nbits(ndiffbin), + workinfo->diff_target, FLDSEP); APPEND_REALLOC(buf, off, len, tmp); snprintf(tmp, sizeof(tmp), @@ -6899,7 +6890,7 @@ static char *cmd_query(__maybe_unused PGconn *conn, char *cmd, char *id, wi_item = last_in_ktree(workinfo_root, ctx); DATA_WORKINFO_NULL(workinfo, wi_item); while (wi_item) { - this_height = coinbase1height(workinfo->coinbase1); + this_height = workinfo->height; if (this_height < height) break; if (CURRENT(&(workinfo->expirydate)) && @@ -6946,7 +6937,6 @@ static char *cmd_query(__maybe_unused PGconn *conn, char *cmd, char *id, K_ITEM *i_height, *wi_item; WORKINFO *workinfo = NULL; int32_t height, this_height; - char ndiffbin[TXT_SML+1]; char bits[TXT_SML+1]; bool got = false; @@ -6971,7 +6961,7 @@ static char *cmd_query(__maybe_unused PGconn *conn, char *cmd, char *id, DATA_WORKINFO_NULL(workinfo, wi_item); while (wi_item) { if (CURRENT(&(workinfo->expirydate))) { - this_height = coinbase1height(workinfo->coinbase1); + this_height = workinfo->height; if (this_height < height) break; } @@ -6984,7 +6974,7 @@ static char *cmd_query(__maybe_unused PGconn *conn, char *cmd, char *id, DATA_WORKINFO_NULL(workinfo, wi_item); while (wi_item) { if (CURRENT(&(workinfo->expirydate))) { - this_height = coinbase1height(workinfo->coinbase1); + this_height = workinfo->height; if (this_height >= height) break; } @@ -6993,7 +6983,7 @@ static char *cmd_query(__maybe_unused PGconn *conn, char *cmd, char *id, } if (wi_item) { DATA_WORKINFO(workinfo, wi_item); - this_height = coinbase1height(workinfo->coinbase1); + this_height = workinfo->height; if (this_height == height) { // We have our starting point STRNCPY(bits, workinfo->bits); @@ -7005,10 +6995,9 @@ static char *cmd_query(__maybe_unused PGconn *conn, char *cmd, char *id, "workinfoid0:%d=%s%c", rows, reply, FLDSEP); APPEND_REALLOC(buf, off, len, tmp); - hex2bin(ndiffbin, workinfo->bits, 4); snprintf(tmp, sizeof(tmp), "ndiff0:%d=%.1f%c", rows, - diff_from_nbits(ndiffbin), + workinfo->diff_target, FLDSEP); APPEND_REALLOC(buf, off, len, tmp); @@ -7045,7 +7034,7 @@ static char *cmd_query(__maybe_unused PGconn *conn, char *cmd, char *id, rows, FLDSEP); APPEND_REALLOC(buf, off, len, tmp); } else { - this_height = coinbase1height(workinfo->coinbase1); + this_height = workinfo->height; int_to_buf(this_height, reply, sizeof(reply)); snprintf(tmp, sizeof(tmp), "height:%d=%s%c", rows, reply, FLDSEP); @@ -7055,10 +7044,9 @@ static char *cmd_query(__maybe_unused PGconn *conn, char *cmd, char *id, snprintf(tmp, sizeof(tmp), "workinfoid:%d=%s%c", rows, reply, FLDSEP); APPEND_REALLOC(buf, off, len, tmp); - hex2bin(ndiffbin, workinfo->bits, 4); snprintf(tmp, sizeof(tmp), "ndiff:%d=%.1f%c", rows, - diff_from_nbits(ndiffbin), + workinfo->diff_target, FLDSEP); APPEND_REALLOC(buf, off, len, tmp); } diff --git a/src/ckdb_data.c b/src/ckdb_data.c index ff6f102c..30347485 100644 --- a/src/ckdb_data.c +++ b/src/ckdb_data.c @@ -1965,20 +1965,36 @@ cmp_t cmp_workinfo(K_ITEM *a, K_ITEM *b) return c; } -int32_t _coinbase1height(char *coinbase1, WHERE_FFL_ARGS) +int32_t _coinbase1height(WORKINFO *wi, WHERE_FFL_ARGS) { int32_t height = 0; + char *st = NULL; uchar *cb1; + size_t len; int siz; - cb1 = ((uchar *)coinbase1) + 84; + len = strlen(wi->coinbase1); + if (len < (BLOCKNUM_OFFSET * 2 + 4) || (len & 1)) { + LOGERR("ERR %s(): Invalid coinbase1 len %d - " + "should be >= %d and even - wid %"PRId64 + " (cb1 %.10s%s)", + __func__, (int)len, (BLOCKNUM_OFFSET * 2 + 4), + wi->workinfoid, st = safe_text_nonull(wi->coinbase1), + len <= 10 ? "" : "..."); + FREENULL(st); + return height; + } + + cb1 = ((uchar *)(wi->coinbase1)) + (BLOCKNUM_OFFSET * 2); siz = ((hex2bin_tbl[*cb1]) << 4) + (hex2bin_tbl[*(cb1+1)]); // limit to 4 for int32_t and since ... that should last a while :) if (siz < 1 || siz > 4) { - LOGERR("%s(): Invalid coinbase1 block height size (%d)" - " require: 1..4 (cb1 %s)" WHERE_FFL, - __func__, siz, coinbase1, WHERE_FFL_PASS); + LOGERR("ERR %s(): Invalid coinbase1 block height size (%d)" + " require: 1..4 - wid %"PRId64" (cb1 %.10s...)" + WHERE_FFL, + __func__, siz, wi->workinfoid, wi->coinbase1, + WHERE_FFL_PASS); return height; } @@ -1991,19 +2007,13 @@ int32_t _coinbase1height(char *coinbase1, WHERE_FFL_ARGS) return height; } -cmp_t _cmp_height(char *coinbase1a, char *coinbase1b, WHERE_FFL_ARGS) -{ - return CMP_INT(_coinbase1height(coinbase1a, WHERE_FFL_PASS), - _coinbase1height(coinbase1b, WHERE_FFL_PASS)); -} - // order by height asc,createdate asc cmp_t cmp_workinfo_height(K_ITEM *a, K_ITEM *b) { WORKINFO *wa, *wb; DATA_WORKINFO(wa, a); DATA_WORKINFO(wb, b); - cmp_t c = cmp_height(wa->coinbase1, wb->coinbase1); + cmp_t c = CMP_INT(wa->height, wb->height); if (c == 0) c = CMP_TV(wa->createdate, wb->createdate); return c; @@ -2251,12 +2261,7 @@ double workinfo_pps(K_ITEM *w_item, int64_t workinfoid, bool lock) OPTIONCONTROL *optioncontrol; K_ITEM *oc_item; char oc_name[TXT_SML+1]; - char coinbase1bin[TXT_SML+1]; - char ndiffbin[TXT_SML+1]; WORKINFO *workinfo; - double w_diff; - int32_t w_blocknum; - size_t len; // Allow optioncontrol override for a given workinfoid snprintf(oc_name, sizeof(oc_name), PPSOVERRIDE"_%"PRId64, workinfoid); @@ -2280,23 +2285,9 @@ double workinfo_pps(K_ITEM *w_item, int64_t workinfoid, bool lock) } DATA_WORKINFO(workinfo, w_item); - len = strlen(workinfo->coinbase1); - if (len < (BLOCKNUM_OFFSET * 2 + 4) || (len & 1)) { - LOGERR("%s(): Invalid coinbase1 len %d - " - "should be >= %d and even - for wid %"PRId64, - __func__, (int)len, (BLOCKNUM_OFFSET * 2 + 4), - workinfoid); - return 0.0; - } - - hex2bin(ndiffbin, workinfo->bits, 4); - w_diff = diff_from_nbits(ndiffbin); - hex2bin(coinbase1bin, workinfo->coinbase1 + (BLOCKNUM_OFFSET * 2), - (len - (BLOCKNUM_OFFSET * 2)) >> 1); - w_blocknum = (int32_t)get_sernumber((uchar *)coinbase1bin); // PPS 1diff is worth coinbase reward divided by difficulty - return(coinbase_reward(w_blocknum) / w_diff); + return(coinbase_reward(workinfo->height) / workinfo->diff_target); } // order by workinfoid asc,userid asc,workername asc,createdate asc,nonce asc,expirydate desc @@ -2973,7 +2964,6 @@ bool check_update_blocks_stats(tv_t *stats) K_ITEM *b_item, *w_item; WORKINFO *workinfo; BLOCKS *blocks; - char ndiffbin[TXT_SML+1]; double ok, diffacc, netsumm, diffmean, pending, txmean, cr; tv_t now; @@ -3028,8 +3018,7 @@ bool check_update_blocks_stats(tv_t *stats) return false; } DATA_WORKINFO(workinfo, w_item); - hex2bin(ndiffbin, workinfo->bits, 4); - blocks->netdiff = diff_from_nbits(ndiffbin); + blocks->netdiff = workinfo->diff_target; } /* Stats for each blocks are independent of * if they are orphans or not */ @@ -3599,7 +3588,7 @@ bool process_pplns(int32_t height, char *blockhash, tv_t *addr_cd) K_TREE *mu_root = NULL; int usercount; double ndiff, total_diff, diff_want, elapsed; - char ndiffbin[TXT_SML+1], rewardbuf[32]; + char rewardbuf[32]; double diff_times, diff_add; char cd_buf[CDATE_BUFSIZ]; tv_t end_tv = { 0L, 0L }; @@ -3699,8 +3688,7 @@ bool process_pplns(int32_t height, char *blockhash, tv_t *addr_cd) DATA_OPTIONCONTROL(optioncontrol, oc_item); diff_add = atof(optioncontrol->optionvalue); - hex2bin(ndiffbin, workinfo->bits, 4); - ndiff = diff_from_nbits(ndiffbin); + ndiff = workinfo->diff_target; diff_want = ndiff * diff_times + diff_add; if (diff_want < 1.0) { LOGERR("%s(): invalid diff_want %.1f, block %"PRId32"/%" diff --git a/src/ckdb_dbio.c b/src/ckdb_dbio.c index 6319aed4..843d3e8d 100644 --- a/src/ckdb_dbio.c +++ b/src/ckdb_dbio.c @@ -2808,8 +2808,9 @@ unparam: LIST_MEM_SUB(workinfo_free, row->transactiontree); FREENULL(row->transactiontree); + row->height = coinbase1height(row); hex2bin(ndiffbin, row->bits, 4); - current_ndiff = diff_from_nbits(ndiffbin); + row->diff_target = current_ndiff = diff_from_nbits(ndiffbin); add_to_ktree(workinfo_root, item); k_add_head(workinfo_store, item); @@ -2818,7 +2819,7 @@ unparam: if (workinfo_current) { WORKINFO *wic; DATA_WORKINFO(wic, workinfo_current); - if (cmp_height(wic->coinbase1, row->coinbase1) != 0) { + if (wic->height != row->height) { copy_tv(&last_bc, cd); zero_active = true; } @@ -2836,6 +2837,7 @@ unparam: bool workinfo_fill(PGconn *conn) { + char ndiffbin[TXT_SML+1]; ExecStatusType rescode; PGresult *res; K_ITEM *item; @@ -3003,6 +3005,10 @@ bool workinfo_fill(PGconn *conn) if (!ok) break; + row->height = coinbase1height(row); + hex2bin(ndiffbin, row->bits, 4); + row->diff_target = diff_from_nbits(ndiffbin); + add_to_ktree(workinfo_root, item); if (!confirm_sharesummary) add_to_ktree(workinfo_height_root, item);