Abstract out tests for whether work has come from a block that has been seen before and whether a string is from a previously seen block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
diff --git a/cgminer.c b/cgminer.c
index eba8f4c..04879b5 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -2013,9 +2013,36 @@ static void set_curblock(char *hexstr, unsigned char *hash)
free(old_hash);
}
-static void test_work_current(struct work *work, bool longpoll)
+/* Search to see if this string is from a block that has been seen before */
+static bool block_exists(char *hexstr)
{
struct block *s;
+
+ rd_lock(&blk_lock);
+ HASH_FIND_STR(blocks, hexstr, s);
+ rd_unlock(&blk_lock);
+ if (s)
+ return true;
+ return false;
+}
+
+/* Tests if this work is from a block that has been seen before */
+static inline bool from_existing_block(struct work *work)
+{
+ char *hexstr = bin2hex(work->data, 18);
+ bool ret;
+
+ if (unlikely(!hexstr)) {
+ applog(LOG_ERR, "from_existing_block OOM");
+ return true;
+ }
+ ret = block_exists(hexstr);
+ free(hexstr);
+ return ret;
+}
+
+static void test_work_current(struct work *work, bool longpoll)
+{
char *hexstr;
/* Allow donation to not set current work, so it will work even if
@@ -2031,11 +2058,9 @@ static void test_work_current(struct work *work, bool longpoll)
/* Search to see if this block exists yet and if not, consider it a
* new block and set the current block details to this one */
- rd_lock(&blk_lock);
- HASH_FIND_STR(blocks, hexstr, s);
- rd_unlock(&blk_lock);
- if (!s) {
- s = calloc(sizeof(struct block), 1);
+ if (!block_exists(hexstr)) {
+ struct block *s = calloc(sizeof(struct block), 1);
+
if (unlikely(!s))
quit (1, "test_work_current OOM");
strcpy(s->hash, hexstr);