Browse Source

ckdb - auto flag blocks when mature

master
kanoi 10 years ago
parent
commit
2530c8a028
  1. 2
      src/ckdb.h
  2. 122
      src/ckdb_btc.c

2
src/ckdb.h

@ -52,7 +52,7 @@
#define DB_VLOCK "1" #define DB_VLOCK "1"
#define DB_VERSION "0.9.2" #define DB_VERSION "0.9.2"
#define CKDB_VERSION DB_VERSION"-0.511" #define CKDB_VERSION DB_VERSION"-0.512"
#define WHERE_FFL " - from %s %s() line %d" #define WHERE_FFL " - from %s %s() line %d"
#define WHERE_FFL_HERE __FILE__, __func__, __LINE__ #define WHERE_FFL_HERE __FILE__, __func__, __LINE__

122
src/ckdb_btc.c

@ -10,11 +10,11 @@
#include "ckdb.h" #include "ckdb.h"
//#include <curl/curl.h> #define BTCKEY ((const char *)"result")
#define GETBLOCKHASHCMD "getblockhash" #define GETBLOCKHASHCMD "getblockhash"
#define GETBLOCKHASH "{\"method\":\"" GETBLOCKHASHCMD "\",\"params\":[%d],\"id\":1}" #define GETBLOCKHASH "{\"method\":\"" GETBLOCKHASHCMD "\",\"params\":[%d],\"id\":1}"
#define GETBLOCKHASHKEY ((const char *)"result") #define GETBLOCKHASHKEY NULL
#define GETBLOCKCMD "getblock" #define GETBLOCKCMD "getblock"
#define GETBLOCK "{\"method\":\"" GETBLOCKCMD "\",\"params\":[\"%s\"],\"id\":1}" #define GETBLOCK "{\"method\":\"" GETBLOCKCMD "\",\"params\":[\"%s\"],\"id\":1}"
@ -145,13 +145,12 @@ static char *_btc_io(__maybe_unused const char *cmd, char *json, WHERE_FFL_ARGS)
return res; return res;
} }
static char *single_decode_str(char *ans, const char *cmd, const char *key) static json_t *single_decode(char *ans, const char *cmd, const char *key)
{ {
json_t *json_data, *json_ob; json_t *json_data, *btc_ob, *json_ob = NULL;
json_error_t err_val; json_error_t err_val;
const char *json_str;
if (ans) { if (ans && *ans) {
json_data = json_loads(ans, JSON_DISABLE_EOF_CHECK, &err_val); json_data = json_loads(ans, JSON_DISABLE_EOF_CHECK, &err_val);
if (!json_data) { if (!json_data) {
char *text = safe_text(ans); char *text = safe_text(ans);
@ -163,16 +162,44 @@ static char *single_decode_str(char *ans, const char *cmd, const char *key)
err_val.text, text); err_val.text, text);
free(text); free(text);
} else { } else {
json_ob = json_object_get(json_data, key); btc_ob = json_object_get(json_data, BTCKEY);
if (!json_ob) { if (!btc_ob) {
char *text = safe_text(ans); char *text = safe_text(ans);
LOGERR("%s() Json %s reply missing key %s " LOGERR("%s() Json %s reply missing main key %s "
"ans='%s'", "ans='%s'",
__func__, cmd, key, text); __func__, cmd, key, text);
free(text); free(text);
} else { } else {
if (key == NULL)
json_ob = btc_ob;
else {
json_ob = json_object_get(btc_ob, key);
if (!json_ob) {
char *text = safe_text(ans);
LOGERR("%s() Json %s reply missing "
"sub-key %s ans='%s'",
__func__, cmd, key, text);
free(text);
}
}
}
}
}
return json_ob;
}
static char *single_decode_str(char *ans, const char *cmd, const char *key)
{
const char *json_str;
char *str = NULL;
json_t *json_ob;
json_ob = single_decode(ans, cmd, key);
if (json_ob) {
if (!json_is_string(json_ob)) { if (!json_is_string(json_ob)) {
char *text = safe_text(ans); char *text = safe_text(ans);
if (!key)
key = BTCKEY;
LOGERR("%s() Json %s key %s " LOGERR("%s() Json %s key %s "
"not a string ans='%s'", "not a string ans='%s'",
__func__, cmd, key, text); __func__, cmd, key, text);
@ -180,45 +207,31 @@ static char *single_decode_str(char *ans, const char *cmd, const char *key)
} else { } else {
json_str = json_string_value(json_ob); json_str = json_string_value(json_ob);
if (json_str) if (json_str)
return strdup(json_str); str = strdup(json_str);
else
return strdup(EMPTY);
}
} }
} }
} return str;
return NULL;
} }
static int64_t single_decode_int(char *ans, const char *cmd, const char *key) static int64_t single_decode_int(char *ans, const char *cmd, const char *key)
{ {
json_t *json_data, *json_ob; json_t *json_ob;
json_error_t err_val; int64_t val = 0;
if (ans) { json_ob = single_decode(ans, cmd, key);
json_data = json_loads(ans, JSON_DISABLE_EOF_CHECK, &err_val); if (json_ob) {
if (!json_data) { if (!json_is_integer(json_ob)) {
char *text = safe_text(ans);
LOGERR("%s() Json %s decode error "
"json_err=(%d:%d:%d)%s:%s ans='%s'",
__func__, cmd,
err_val.line, err_val.column,
err_val.position, err_val.source,
err_val.text, text);
free(text);
} else {
json_ob = json_object_get(json_data, key);
if (!json_ob) {
char *text = safe_text(ans); char *text = safe_text(ans);
LOGERR("%s() Json %s reply missing key %s " if (!key)
"ans='%s'", key = BTCKEY;
LOGERR("%s() Json %s key %s "
"not an int ans='%s'",
__func__, cmd, key, text); __func__, cmd, key, text);
free(text); free(text);
} else } else
return (int64_t)json_integer_value(json_ob); val = (int64_t)json_integer_value(json_ob);
}
} }
return 0; return val;
} }
static char *btc_blockhash(int32_t height) static char *btc_blockhash(int32_t height)
@ -234,14 +247,14 @@ static char *btc_blockhash(int32_t height)
return hash; return hash;
} }
static __maybe_unused int32_t btc_confirms(int32_t height) static int32_t btc_confirms(char *hash)
{ {
char buf[1024]; char buf[1024];
char *ans; char *ans;
int32_t conf; int32_t conf;
snprintf(buf, sizeof(buf), GETBLOCKHASH, height); snprintf(buf, sizeof(buf), GETBLOCK, hash);
ans = btc_io(GETBLOCKHASHCMD, buf); ans = btc_io(GETBLOCKCMD, buf);
conf = (int32_t)single_decode_int(ans, GETBLOCKCMD, GETBLOCKCONFKEY); conf = (int32_t)single_decode_int(ans, GETBLOCKCMD, GETBLOCKCONFKEY);
free(ans); free(ans);
return conf; return conf;
@ -251,7 +264,9 @@ static __maybe_unused int32_t btc_confirms(int32_t height)
void btc_blockstatus(BLOCKS *blocks) void btc_blockstatus(BLOCKS *blocks)
{ {
char hash[TXT_BIG+1]; char hash[TXT_BIG+1];
char height_str[32];
char *blockhash; char *blockhash;
int32_t confirms;
size_t len; size_t len;
tv_t now; tv_t now;
bool ok; bool ok;
@ -289,16 +304,14 @@ void btc_blockstatus(BLOCKS *blocks)
return; return;
if (strcmp(blockhash, hash) != 0) { if (strcmp(blockhash, hash) != 0) {
char height_tmp[32]; snprintf(height_str, sizeof(height_str), "%d", blocks->height);
snprintf(height_tmp, sizeof(height_tmp), "%d", blocks->height);
LOGERR("%s() flagging block %d(%s) as %s pool=%s btc=%s", LOGERR("%s() flagging block %d(%s) as %s pool=%s btc=%s",
__func__, __func__,
blocks->height, height_tmp, blocks->height, height_str,
blocks_confirmed(BLOCKS_ORPHAN_STR), blocks_confirmed(BLOCKS_ORPHAN_STR),
hash, blockhash); hash, blockhash);
ok = blocks_add(NULL, height_tmp, ok = blocks_add(NULL, height_str,
blocks->blockhash, blocks->blockhash,
BLOCKS_ORPHAN_STR, BLOCKS_ORPHAN_STR,
EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
@ -312,5 +325,24 @@ void btc_blockstatus(BLOCKS *blocks)
return; return;
} }
// confirms = btc_confirms(hash); 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,
blocks_confirmed(BLOCKS_42_STR),
confirms, BLOCKS_42_VALUE);
ok = blocks_add(NULL, height_str,
blocks->blockhash,
BLOCKS_42_STR,
EMPTY, EMPTY, EMPTY, EMPTY,
EMPTY, EMPTY, EMPTY, EMPTY,
by_default, (char *)__func__, inet_default,
&now, false, id_default, NULL);
if (!ok)
blocks->ignore = true;
}
} }

Loading…
Cancel
Save