From 6f1838536ca02fd8306a9dfe6176a6beb3c17714 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 21 Apr 2014 15:58:07 +1000 Subject: [PATCH] Create a stratifier thread that updates the data from the gbt template regularly and receives other requests --- src/Makefile.am | 3 +- src/ckpool.c | 12 +++++- src/libckpool.h | 1 + src/stratifier.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ src/stratifier.h | 15 ++++++++ 5 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 src/stratifier.c create mode 100644 src/stratifier.h diff --git a/src/Makefile.am b/src/Makefile.am index 7c810063..f6eb8744 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -5,5 +5,6 @@ libckpool_la_SOURCES = libckpool.c libckpool.h sha2.c sha2.h libckpool_la_LIBADD = @PTHREAD_LIBS@ @MATH_LIBS@ @RT_LIBS@ @JANSSON_LIBS@ bin_PROGRAMS = ckpool -ckpool_SOURCES = ckpool.c ckpool.h generator.c generator.h bitcoin.c bitcoin.h +ckpool_SOURCES = ckpool.c ckpool.h generator.c generator.h bitcoin.c bitcoin.h \ + stratifier.c stratifier.h ckpool_LDADD = libckpool.la diff --git a/src/ckpool.c b/src/ckpool.c index 3b17556a..04cacba7 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -20,6 +20,7 @@ #include "ckpool.h" #include "libckpool.h" #include "generator.h" +#include "stratifier.h" /* Only global variable, to be used only by sighandler */ static ckpool_t *global_ckp; @@ -149,6 +150,7 @@ static void clean_up(ckpool_t *ckp) static void shutdown_children(ckpool_t *ckp, int sig) { kill(ckp->generator.pid, sig); + kill(ckp->stratifier.pid, sig); } static void sighandler(int sig) @@ -206,15 +208,15 @@ static void test_functions(ckpool_t *ckp) } send_unix_msg(genfd, "getbase"); buf = recv_unix_msg(genfd); - LOGWARNING("getbase response: %s", buf); dealloc(buf); - +#if 0 genfd = open_unix_client(ckp->generator.us.path); if (genfd < 0) { LOGWARNING("Failed to open generator socket %s", path); return; } send_unix_msg(genfd, "shutdown"); +#endif } int main(int argc, char **argv) @@ -276,6 +278,12 @@ int main(int argc, char **argv) ckp.generator.process = &generator; launch_process(&ckp.generator); + ckp.stratifier.ckp = &ckp; + ckp.stratifier.processname = strdup("stratifier"); + ckp.stratifier.sockname = ckp.stratifier.processname; + ckp.stratifier.process = &stratifier; + launch_process(&ckp.stratifier); + /* Install signal handlers only for the master process to be able to * shut down all child processes */ handler.sa_handler = &sighandler; diff --git a/src/libckpool.h b/src/libckpool.h index e3443582..d19e5ee9 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -148,6 +148,7 @@ struct ckpool_instance { /* Process instance data of parent/child processes */ proc_instance_t main; proc_instance_t generator; + proc_instance_t stratifier; /* Bitcoind data */ char *btcdurl; diff --git a/src/stratifier.c b/src/stratifier.c new file mode 100644 index 00000000..fe49b8b2 --- /dev/null +++ b/src/stratifier.c @@ -0,0 +1,98 @@ +/* + * Copyright 2014 Con Kolivas + * + * 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 + * Software Foundation; either version 3 of the License, or (at your option) + * any later version. See COPYING for more details. + */ + +#include "config.h" + +#include +#include +#include + +#include "ckpool.h" +#include "libckpool.h" +#include "bitcoin.h" + +static void update_base(ckpool_t *ckp) +{ + char *path = ckp->generator.us.path, *buf; + int genfd; + + genfd = open_unix_client(ckp->generator.us.path); + if (genfd < 0) { + LOGWARNING("Failed to open generator socket %s", path); + return; + } + send_unix_msg(genfd, "getbase"); + buf = recv_unix_msg(genfd); + /* Do something with this buffer here */ + dealloc(buf); +} + +static int strat_loop(ckpool_t *ckp, proc_instance_t *pi) +{ + int sockd, ret = 0, selret; + unixsock_t *us = &pi->us; + char *buf = NULL; + fd_set readfds; + tv_t timeout; + +reset: + timeout.tv_sec = 60; +retry: + FD_ZERO(&readfds); + FD_SET(us->sockd, &readfds); + selret = select(us->sockd + 1, &readfds, NULL, NULL, &timeout); + if (selret < 0) { + if (interrupted()) + goto retry; + LOGERR("Select failed in strat_loop"); + ret = 1; + goto out; + } + if (!selret) { + LOGDEBUG("60s elapsed in strat_loop, updating gbt base"); + update_base(ckp); + goto reset; + } + sockd = accept(us->sockd, NULL, NULL); + if (sockd < 0) { + if (interrupted()) + goto retry; + LOGERR("Failed to accept on stratifier socket"); + ret = 1; + goto out; + } + + dealloc(buf); + buf = recv_unix_msg(sockd); + if (!buf) { + LOGWARNING("Failed to get message in strat_loop"); + close(sockd); + goto retry; + } + LOGDEBUG("Stratifier received request: %s", buf); + if (!strncasecmp(buf, "shutdown", 8)) + goto out; + if (!strncasecmp(buf, "update", 6)) { + update_base(ckp); + goto reset; + } + +out: + dealloc(buf); + return ret; +} + +int stratifier(proc_instance_t *pi) +{ + ckpool_t *ckp = pi->ckp; + int ret = 0; + + strat_loop(ckp, pi); + return ret; +} diff --git a/src/stratifier.h b/src/stratifier.h new file mode 100644 index 00000000..0f41b91c --- /dev/null +++ b/src/stratifier.h @@ -0,0 +1,15 @@ +/* + * Copyright 2014 Con Kolivas + * + * 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 + * Software Foundation; either version 3 of the License, or (at your option) + * any later version. See COPYING for more details. + */ + +#ifndef STRATIFIER_H +#define STRATIFIER_H + +int stratifier(proc_instance_t *pi); + +#endif /* STRATIFIER_H */