diff --git a/src/generator.c b/src/generator.c index 34f89671..52f9c605 100644 --- a/src/generator.c +++ b/src/generator.c @@ -827,6 +827,37 @@ out: return val; } +int generator_getbest(ckpool_t *ckp, char *hash) +{ + gdata_t *gdata = ckp->gdata; + int ret = GETBEST_FAILED; + server_instance_t *si; + connsock_t *cs; + + /* Use temporary variables to prevent deref while accessing */ + si = gdata->current_si; + if (unlikely(!si)) { + LOGWARNING("No live current server in generator_getbest"); + goto out; + } + if (si->notify) { + ret = GETBEST_NOTIFY; + goto out; + } + cs = &si->cs; + if (unlikely(!cs)) { + LOGWARNING("No live connsock for current server in generator_getbest"); + goto out; + } + if (unlikely(!get_bestblockhash(cs, hash))) { + LOGWARNING("Failed to get best block hash from %s:%s", cs->url, cs->port); + goto out; + } + ret = GETBEST_SUCCESS; +out: + return ret; +} + static bool parse_notify(ckpool_t *ckp, proxy_instance_t *proxi, json_t *val) { const char *prev_hash, *bbversion, *nbit, *ntime; diff --git a/src/generator.h b/src/generator.h index 8c0fdd89..b5c25f6a 100644 --- a/src/generator.h +++ b/src/generator.h @@ -12,8 +12,13 @@ #include "config.h" +#define GETBEST_FAILED -1 +#define GETBEST_NOTIFY 0 +#define GETBEST_SUCCESS 1 + void generator_add_send(ckpool_t *ckp, json_t *val); json_t *generator_genbase(ckpool_t *ckp); +int generator_getbest(ckpool_t *ckp, char *hash); void *generator(void *arg); #endif /* GENERATOR_H */ diff --git a/src/stratifier.c b/src/stratifier.c index dcffe6a8..11be7963 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -1143,19 +1143,6 @@ static char *__send_recv_generator(ckpool_t *ckp, const char *msg, const int pri return buf; } -/* Conditionally send_recv a message only if it's equal or higher priority than - * any currently being serviced. NULL is returned if the request is not - * processed for priority reasons, "failed" for an actual failure. */ -static char *send_recv_generator(ckpool_t *ckp, const char *msg, const int prio) -{ - sdata_t *sdata = ckp->sdata; - char *buf = NULL; - - if (prio >= sdata->gen_priority) - buf = __send_recv_generator(ckp, msg, prio); - return buf; -} - static void send_generator(ckpool_t *ckp, const char *msg, const int prio) { sdata_t *sdata = ckp->sdata; @@ -4390,26 +4377,28 @@ static void *blockupdate(void *arg) { ckpool_t *ckp = (ckpool_t *)arg; sdata_t *sdata = ckp->sdata; - char *buf = NULL; - char request[8]; + char hash[68]; pthread_detach(pthread_self()); rename_proc("blockupdate"); - buf = send_recv_proc(ckp->generator, "getbest"); - if (!cmdmatch(buf, "failed")) - sprintf(request, "getbest"); - else - sprintf(request, "getlast"); while (42) { - dealloc(buf); - buf = send_recv_generator(ckp, request, GEN_LAX); - if (buf && cmdmatch(buf, "notify")) - cksleep_ms(5000); - else if (buf && strcmp(buf, sdata->lastswaphash) && !cmdmatch(buf, "failed")) - update_base(sdata, GEN_PRIORITY); - else - cksleep_ms(ckp->blockpoll); + int ret; + + ret = generator_getbest(ckp, hash); + switch (ret) { + case GETBEST_NOTIFY: + cksleep_ms(5000); + break; + case GETBEST_SUCCESS: + if (strcmp(hash, sdata->lastswaphash)) { + update_base(sdata, GEN_PRIORITY); + break; + } + case GETBEST_FAILED: + default: + cksleep_ms(ckp->blockpoll); + } } return NULL; }