diff --git a/src/ckdb.c b/src/ckdb.c index 2be37ab4..78045855 100644 --- a/src/ckdb.c +++ b/src/ckdb.c @@ -826,7 +826,8 @@ static bool reload() tv_to_buf(&(dbstatus.newest_createdate_poolstats), buf, sizeof(buf)); LOGWARNING("%s(): %s newest DB poolstats (ignored)", __func__, buf); tv_to_buf(&(dbstatus.newest_createdate_blocks), buf, sizeof(buf)); - LOGWARNING("%s(): %s newest DB blocks (ignored)", __func__, buf); + LOGWARNING("%s(): %"PRId32"/%s newest DB blocks (ignored)", + __func__, dbstatus.newest_height_blocks, buf); copy_tv(&start, &(dbstatus.newest_createdate_workmarker_workinfo)); reason = "workmarkers"; diff --git a/src/ckdb.h b/src/ckdb.h index 17bd6b70..403acf0c 100644 --- a/src/ckdb.h +++ b/src/ckdb.h @@ -55,7 +55,7 @@ #define DB_VLOCK "1" #define DB_VERSION "1.0.3" -#define CKDB_VERSION DB_VERSION"-1.340" +#define CKDB_VERSION DB_VERSION"-1.350" #define WHERE_FFL " - from %s %s() line %d" #define WHERE_FFL_HERE __FILE__, __func__, __LINE__ @@ -138,6 +138,7 @@ typedef struct loadstatus { tv_t newest_createdate_workinfo; tv_t newest_createdate_poolstats; tv_t newest_createdate_blocks; + int32_t newest_height_blocks; } LOADSTATUS; extern LOADSTATUS dbstatus; @@ -2545,7 +2546,7 @@ extern bool blocks_stats(PGconn *conn, int32_t height, char *blockhash, double diffacc, double diffinv, double shareacc, double shareinv, int64_t elapsed, char *by, char *code, char *inet, tv_t *cd); -extern bool blocks_add(PGconn *conn, char *height, char *blockhash, +extern bool blocks_add(PGconn *conn, int32_t height, char *blockhash, char *confirmed, char *info, char *workinfoid, char *username, char *workername, char *clientid, char *enonce1, char *nonce2, char *nonce, char *reward, diff --git a/src/ckdb_btc.c b/src/ckdb_btc.c index 93759bc2..1e258800 100644 --- a/src/ckdb_btc.c +++ b/src/ckdb_btc.c @@ -308,7 +308,6 @@ bool btc_valid_address(char *addr) void btc_blockstatus(BLOCKS *blocks) { char hash[TXT_BIG+1]; - char height_str[32]; char *blockhash; int32_t confirms; size_t len; @@ -348,14 +347,12 @@ void btc_blockstatus(BLOCKS *blocks) return; if (strcmp(blockhash, hash) != 0) { - snprintf(height_str, sizeof(height_str), "%d", blocks->height); - LOGERR("%s() flagging block %d(%s) as %s pool=%s btc=%s", - __func__, - blocks->height, height_str, + LOGERR("%s() flagging block %d as %s pool=%s btc=%s", + __func__, blocks->height, blocks_confirmed(BLOCKS_ORPHAN_STR), hash, blockhash); - ok = blocks_add(NULL, height_str, + ok = blocks_add(NULL, blocks->height, blocks->blockhash, BLOCKS_ORPHAN_STR, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, @@ -371,14 +368,12 @@ void btc_blockstatus(BLOCKS *blocks) confirms = btc_confirms(hash); if (confirms >= BLOCKS_42_VALUE) { - snprintf(height_str, sizeof(height_str), "%d", blocks->height); - LOGERR("%s() flagging block %d(%s) as %s confirms=%d(%d)", - __func__, - blocks->height, height_str, + LOGERR("%s() flagging block %d as %s confirms=%d(%d)", + __func__, blocks->height, blocks_confirmed(BLOCKS_42_STR), confirms, BLOCKS_42_VALUE); - ok = blocks_add(NULL, height_str, + ok = blocks_add(NULL, blocks->height, blocks->blockhash, BLOCKS_42_STR, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, diff --git a/src/ckdb_cmd.c b/src/ckdb_cmd.c index 8dccf664..2f8b396f 100644 --- a/src/ckdb_cmd.c +++ b/src/ckdb_cmd.c @@ -1387,8 +1387,8 @@ redo: return buf; } -static char *cmd_blockstatus(__maybe_unused PGconn *conn, char *cmd, char *id, - tv_t *now, char *by, char *code, char *inet, +static char *cmd_blockstatus(PGconn *conn, char *cmd, char *id, tv_t *now, + char *by, char *code, char *inet, __maybe_unused tv_t *cd, K_TREE *trf_root) { K_ITEM *i_height, *i_blockhash, *i_action, *i_info; @@ -1437,7 +1437,7 @@ static char *cmd_blockstatus(__maybe_unused PGconn *conn, char *cmd, char *id, switch (blocks->confirmed[0]) { case BLOCKS_NEW: case BLOCKS_CONFIRM: - ok = blocks_add(conn, transfer_data(i_height), + ok = blocks_add(conn, height, blocks->blockhash, BLOCKS_ORPHAN_STR, info, EMPTY, EMPTY, EMPTY, EMPTY, @@ -1478,7 +1478,7 @@ static char *cmd_blockstatus(__maybe_unused PGconn *conn, char *cmd, char *id, case BLOCKS_CONFIRM: case BLOCKS_ORPHAN: case BLOCKS_REJECT: - ok = blocks_add(conn, transfer_data(i_height), + ok = blocks_add(conn, height, blocks->blockhash, BLOCKS_REJECT_STR, info, EMPTY, EMPTY, EMPTY, EMPTY, @@ -1507,7 +1507,7 @@ static char *cmd_blockstatus(__maybe_unused PGconn *conn, char *cmd, char *id, // Confirm a new block that wasn't confirmed due to some bug switch (blocks->confirmed[0]) { case BLOCKS_NEW: - ok = blocks_add(conn, transfer_data(i_height), + ok = blocks_add(conn, height, blocks->blockhash, BLOCKS_CONFIRM_STR, info, EMPTY, EMPTY, EMPTY, EMPTY, @@ -2644,13 +2644,13 @@ awconf: } // TODO: the confirm update: identify block changes from workinfo height? -static char *cmd_blocks_do(PGconn *conn, char *cmd, char *id, char *by, - char *code, char *inet, tv_t *cd, bool igndup, - K_TREE *trf_root) +static char *cmd_blocks_do(PGconn *conn, char *cmd, int32_t height, char *id, + char *by, char *code, char *inet, tv_t *cd, + bool igndup, K_TREE *trf_root) { char reply[1024] = ""; size_t siz = sizeof(reply); - K_ITEM *i_height, *i_blockhash, *i_confirmed, *i_workinfoid, *i_username; + K_ITEM *i_blockhash, *i_confirmed, *i_workinfoid, *i_username; K_ITEM *i_workername, *i_clientid, *i_enonce1, *i_nonce2, *i_nonce, *i_reward; TRANSFER *transfer; char *msg; @@ -2658,10 +2658,6 @@ static char *cmd_blocks_do(PGconn *conn, char *cmd, char *id, char *by, LOGDEBUG("%s(): cmd '%s'", __func__, cmd); - i_height = require_name(trf_root, "height", 1, NULL, reply, siz); - if (!i_height) - return strdup(reply); - i_blockhash = require_name(trf_root, "blockhash", 1, NULL, reply, siz); if (!i_blockhash) return strdup(reply); @@ -2707,7 +2703,7 @@ static char *cmd_blocks_do(PGconn *conn, char *cmd, char *id, char *by, return strdup(reply); msg = "added"; - ok = blocks_add(conn, transfer_data(i_height), + ok = blocks_add(conn, height, transfer_data(i_blockhash), transfer_data(i_confirmed), EMPTY, @@ -2724,7 +2720,7 @@ static char *cmd_blocks_do(PGconn *conn, char *cmd, char *id, char *by, break; case BLOCKS_CONFIRM: msg = "confirmed"; - ok = blocks_add(conn, transfer_data(i_height), + ok = blocks_add(conn, height, transfer_data(i_blockhash), transfer_data(i_confirmed), EMPTY, @@ -2757,17 +2753,28 @@ static char *cmd_blocks(PGconn *conn, char *cmd, char *id, char *code, char *inet, tv_t *cd, K_TREE *trf_root) { + char reply[1024] = ""; + size_t siz = sizeof(reply); bool igndup = false; + K_ITEM *i_height; + int32_t height; + + LOGDEBUG("%s(): cmd '%s'", __func__, cmd); + + i_height = require_name(trf_root, "height", 1, NULL, reply, siz); + if (!i_height) + return strdup(reply); + + TXT_TO_INT("height", transfer_data(i_height), height); // confirm_summaries() doesn't call this if (reloading) { - if (tv_equal(cd, &(dbstatus.newest_createdate_blocks))) + // Since they're blocks, just try them all + if (height <= dbstatus.newest_height_blocks) igndup = true; - else if (tv_newer(cd, &(dbstatus.newest_createdate_blocks))) - return NULL; } - return cmd_blocks_do(conn, cmd, id, by, code, inet, cd, igndup, trf_root); + return cmd_blocks_do(conn, cmd, height, id, by, code, inet, cd, igndup, trf_root); } static char *cmd_auth_do(PGconn *conn, char *cmd, char *id, char *by, diff --git a/src/ckdb_dbio.c b/src/ckdb_dbio.c index fd790a13..fd839ad7 100644 --- a/src/ckdb_dbio.c +++ b/src/ckdb_dbio.c @@ -4434,7 +4434,7 @@ unparam: return ok; } -bool blocks_add(PGconn *conn, char *height, char *blockhash, +bool blocks_add(PGconn *conn, int32_t height, char *blockhash, char *confirmed, char *info, char *workinfoid, char *username, char *workername, char *clientid, char *enonce1, char *nonce2, char *nonce, char *reward, @@ -4465,7 +4465,7 @@ bool blocks_add(PGconn *conn, char *height, char *blockhash, DATA_BLOCKS(row, b_item); bzero(row, sizeof(*row)); - TXT_TO_INT("height", height, row->height); + row->height = height; STRNCPY(row->blockhash, blockhash); dsp_hash(blockhash, hash_dsp, sizeof(hash_dsp)); @@ -4486,7 +4486,7 @@ bool blocks_add(PGconn *conn, char *height, char *blockhash, if (!igndup) { tv_to_buf(cd, cd_buf, sizeof(cd_buf)); LOGERR("%s(): Duplicate (%s) blocks ignored, Status: " - "%s, Block: %s/...%s/%s", + "%s, Block: %"PRId32"/...%s/%s", __func__, blocks_confirmed(oldblocks->confirmed), blocks_confirmed(confirmed), @@ -4578,7 +4578,7 @@ bool blocks_add(PGconn *conn, char *height, char *blockhash, if (!startup_complete) { tv_to_buf(cd, cd_buf, sizeof(cd_buf)); LOGERR("%s(): Status: %s invalid during startup. " - "Ignored: Block: %s/...%s/%s", + "Ignored: Block: %"PRId32"/...%s/%s", __func__, blocks_confirmed(confirmed), height, hash_dsp, cd_buf); @@ -4587,7 +4587,7 @@ bool blocks_add(PGconn *conn, char *height, char *blockhash, case BLOCKS_CONFIRM: if (!old_b_item) { tv_to_buf(cd, cd_buf, sizeof(cd_buf)); - LOGERR("%s(): Can't %s a non-existent Block: %s/...%s/%s", + LOGERR("%s(): Can't %s a non-existent Block: %"PRId32"/...%s/%s", __func__, blocks_confirmed(confirmed), height, hash_dsp, cd_buf); goto flail; @@ -4619,7 +4619,7 @@ bool blocks_add(PGconn *conn, char *height, char *blockhash, if (startup_complete) { tv_to_buf(cd, cd_buf, sizeof(cd_buf)); LOGERR("%s(): New Status: (%s)%s requires Status: %s. " - "Ignored: Status: (%s)%s, Block: %s/...%s/%s", + "Ignored: Status: (%s)%s, Block: %"PRId32"/...%s/%s", __func__, confirmed, blocks_confirmed(confirmed), want, oldblocks->confirmed, @@ -4870,7 +4870,7 @@ flail: break; } - LOGWARNING("%s(): %sStatus: %s, Block: %s/...%s Diff %s%s", + LOGWARNING("%s(): %sStatus: %s, Block: %"PRId32"/...%s Diff %s%s", __func__, blk ? "BLOCK! " : "", blocks_confirmed(confirmed), height, hash_dsp, diff, tmp); @@ -5030,6 +5030,9 @@ bool blocks_fill(PGconn *conn) if (tv_newer(&(dbstatus.newest_createdate_blocks), &(row->createdate))) copy_tv(&(dbstatus.newest_createdate_blocks), &(row->createdate)); + if (dbstatus.newest_height_blocks < row->height) + dbstatus.newest_height_blocks = row->height; + if (pool.workinfoid < row->workinfoid) { pool.workinfoid = row->workinfoid; pool.height = row->height;