Browse Source

ckdb - separate out the block orphan check and call it more often

master
kanoi 9 years ago
parent
commit
ac7e41c570
  1. 57
      src/ckdb.c
  2. 3
      src/ckdb.h
  3. 83
      src/ckdb_btc.c
  4. 2
      src/ckdb_crypt.c
  5. 2
      src/klist.h
  6. 2
      src/ktree.h

57
src/ckdb.c

@ -3774,6 +3774,46 @@ static void *breaker(void *arg)
return NULL; return NULL;
} }
static void check_orphans()
{
K_TREE_CTX ctx[1];
K_ITEM *b_item;
BLOCKS *blocks = NULL;
uint32_t currhi = 0;
K_RLOCK(blocks_free);
// Find the most recent block BLOCKS_NEW or BLOCKS_CONFIRM
b_item = last_in_ktree(blocks_root, ctx);
while (b_item) {
DATA_BLOCKS(blocks, b_item);
if (!blocks->ignore &&
CURRENT(&(blocks->expirydate)) &&
(blocks->confirmed[0] == BLOCKS_NEW ||
blocks->confirmed[0] == BLOCKS_CONFIRM))
break;
b_item = prev_in_ktree(ctx);
}
K_RUNLOCK(blocks_free);
// None
if (!b_item)
return;
K_RLOCK(workinfo_free);
if (workinfo_current) {
WORKINFO *wic;
DATA_WORKINFO(wic, workinfo_current);
currhi = wic->height - 1;
}
K_RUNLOCK(workinfo_free);
LOGDEBUG("%s() currhi=%"PRIu32" block=%"PRIu32,
__func__, currhi, blocks->height);
// Keep checking for 6 blocks
if (currhi && (currhi - blocks->height) < 6)
btc_orphancheck(blocks);
}
static void check_blocks() static void check_blocks()
{ {
K_TREE_CTX ctx[1]; K_TREE_CTX ctx[1];
@ -3800,7 +3840,8 @@ static void check_blocks()
if (!b_item) if (!b_item)
return; return;
btc_blockstatus(blocks); if (btc_orphancheck(blocks))
btc_blockstatus(blocks);
} }
static void pplns_block(BLOCKS *blocks) static void pplns_block(BLOCKS *blocks)
@ -4043,6 +4084,7 @@ static void summarise_blocks()
static void *summariser(__maybe_unused void *arg) static void *summariser(__maybe_unused void *arg)
{ {
bool orphan_check = false;
int i; int i;
pthread_detach(pthread_self()); pthread_detach(pthread_self());
@ -4073,6 +4115,19 @@ static void *summariser(__maybe_unused void *arg)
if (!everyone_die) if (!everyone_die)
sleep(1); sleep(1);
} }
if (everyone_die)
break;
else {
orphan_check = !orphan_check;
// Check every 2nd time
if (orphan_check)
check_orphans();
}
if (!everyone_die)
sleep(1);
if (everyone_die) if (everyone_die)
break; break;
else else

3
src/ckdb.h

@ -52,7 +52,7 @@
#define DB_VLOCK "1" #define DB_VLOCK "1"
#define DB_VERSION "1.0.5" #define DB_VERSION "1.0.5"
#define CKDB_VERSION DB_VERSION"-2.105" #define CKDB_VERSION DB_VERSION"-2.106"
#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__
@ -3377,6 +3377,7 @@ extern struct CMDS ckdb_cmds[];
// *** // ***
extern bool btc_valid_address(char *addr); extern bool btc_valid_address(char *addr);
extern bool btc_orphancheck(BLOCKS *blocks);
extern void btc_blockstatus(BLOCKS *blocks); extern void btc_blockstatus(BLOCKS *blocks);
// *** // ***

83
src/ckdb_btc.c

@ -1,5 +1,5 @@
/* /*
* Copyright 2014 Andrew Smith * Copyright 2014-2016 Andrew Smith
* Copyright 2014 Con Kolivas * Copyright 2014 Con Kolivas
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
@ -304,28 +304,20 @@ bool btc_valid_address(char *addr)
return valid; return valid;
} }
// Check for orphan or update confirm count // Check orphan, returns true only if all OK and not an orphan
void btc_blockstatus(BLOCKS *blocks) bool btc_orphancheck(BLOCKS *blocks)
{ {
char hash[TXT_BIG+1]; char hash[TXT_BIG+1];
char *blockhash; char *blockhash;
int32_t confirms;
size_t len; size_t len;
tv_t now; tv_t now;
bool ok; bool ok;
setnow(&now);
LOGDEBUG("%s() checking %d %s", LOGDEBUG("%s() checking %d %s",
__func__, __func__, blocks->height, blocks->blockhash);
blocks->height, blocks->blockhash);
// Caller must check this to avoid resending it every time if (blocks->ignore)
if (blocks->ignore) { return false;
LOGERR("%s() ignored block %d passed",
__func__, blocks->height);
return;
}
len = strlen(blocks->blockhash); len = strlen(blocks->blockhash);
if (len != SHA256SIZHEX) { if (len != SHA256SIZHEX) {
@ -336,7 +328,7 @@ void btc_blockstatus(BLOCKS *blocks)
* This should never happen */ * This should never happen */
blocks->ignore = true; blocks->ignore = true;
return; return false;
} }
dbhash2btchash(blocks->blockhash, hash, sizeof(hash)); dbhash2btchash(blocks->blockhash, hash, sizeof(hash));
@ -344,7 +336,12 @@ void btc_blockstatus(BLOCKS *blocks)
blockhash = btc_blockhash(blocks->height); blockhash = btc_blockhash(blocks->height);
// Something's amiss - let it try again later // Something's amiss - let it try again later
if (!blockhash) if (!blockhash)
return; return false;
if (strlen(blockhash) != SHA256SIZHEX) {
free(blockhash);
return false;
}
if (strcmp(blockhash, hash) != 0) { if (strcmp(blockhash, hash) != 0) {
LOGERR("%s() flagging block %d as %s pool=%s btc=%s", LOGERR("%s() flagging block %d as %s pool=%s btc=%s",
@ -352,6 +349,8 @@ void btc_blockstatus(BLOCKS *blocks)
blocks_confirmed(BLOCKS_ORPHAN_STR), blocks_confirmed(BLOCKS_ORPHAN_STR),
hash, blockhash); hash, blockhash);
setnow(&now);
ok = blocks_add(NULL, blocks->height, ok = blocks_add(NULL, blocks->height,
blocks->blockhash, blocks->blockhash,
BLOCKS_ORPHAN_STR, EMPTY, BLOCKS_ORPHAN_STR, EMPTY,
@ -363,6 +362,55 @@ void btc_blockstatus(BLOCKS *blocks)
if (!ok) if (!ok)
blocks->ignore = true; blocks->ignore = true;
free(blockhash);
return false;
}
free(blockhash);
return true;
}
// Check to update confirm count
void btc_blockstatus(BLOCKS *blocks)
{
char hash[TXT_BIG+1];
char *blockhash;
int32_t confirms;
size_t len;
tv_t now;
bool ok;
LOGDEBUG("%s() checking %d %s",
__func__, blocks->height, blocks->blockhash);
// Caller must check this to avoid resending it every time
if (blocks->ignore) {
LOGERR("%s() ignored block %d passed",
__func__, blocks->height);
return;
}
len = strlen(blocks->blockhash);
if (len != SHA256SIZHEX) {
LOGERR("%s() invalid blockhash size %d (%d) for block %d",
__func__, (int)len, SHA256SIZHEX, blocks->height);
/* So we don't keep repeating the message
* This should never happen */
blocks->ignore = true;
return;
}
dbhash2btchash(blocks->blockhash, hash, sizeof(hash));
blockhash = btc_blockhash(blocks->height);
// Something's amiss - let it try again later
if (!blockhash)
return;
if (strlen(blockhash) != SHA256SIZHEX) {
free(blockhash);
return; return;
} }
@ -373,6 +421,8 @@ void btc_blockstatus(BLOCKS *blocks)
blocks_confirmed(BLOCKS_42_STR), blocks_confirmed(BLOCKS_42_STR),
confirms, BLOCKS_42_VALUE); confirms, BLOCKS_42_VALUE);
setnow(&now);
ok = blocks_add(NULL, blocks->height, ok = blocks_add(NULL, blocks->height,
blocks->blockhash, blocks->blockhash,
BLOCKS_42_STR, EMPTY, BLOCKS_42_STR, EMPTY,
@ -384,4 +434,5 @@ void btc_blockstatus(BLOCKS *blocks)
if (!ok) if (!ok)
blocks->ignore = true; blocks->ignore = true;
} }
free(blockhash);
} }

2
src/ckdb_crypt.c

@ -1,5 +1,5 @@
/* /*
* Copyright 2015 Andrew Smith * Copyright 2015-2016 Andrew Smith
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free * under the terms of the GNU General Public License as published by the Free

2
src/klist.h

@ -1,6 +1,6 @@
/* /*
* Copyright 2013-2014 Andrew Smith - BlackArrow Ltd * Copyright 2013-2014 Andrew Smith - BlackArrow Ltd
* Copyright 2015 Andrew Smith * Copyright 2015-2016 Andrew Smith
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free * under the terms of the GNU General Public License as published by the Free

2
src/ktree.h

@ -1,5 +1,5 @@
/* /*
* Copyright 1995-2015 Andrew Smith * Copyright 1995-2016 Andrew Smith
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free * under the terms of the GNU General Public License as published by the Free

Loading…
Cancel
Save