From f4db3e14bd739f2780f2004ce366283a21efa841 Mon Sep 17 00:00:00 2001 From: kanoi Date: Thu, 1 Oct 2015 22:10:32 +1000 Subject: [PATCH] ckdb/sql/php - add an optional address idname for users with multiple payment addressess --- pool/db.php | 4 ++++ pool/page_addrmgt.php | 39 ++++++++++++++++++++++++++------------- pool/page_percent.php | 6 +++++- sql/ckdb.sql | 4 +++- sql/v1.0.3-v1.0.4.sql | 26 ++++++++++++++++++++++++++ src/ckdb.c | 18 ++++++++++++------ src/ckdb.h | 3 ++- src/ckdb_cmd.c | 24 ++++++++++++++++++++---- src/ckdb_dbio.c | 26 ++++++++++++++++++-------- 9 files changed, 116 insertions(+), 34 deletions(-) create mode 100644 sql/v1.0.3-v1.0.4.sql diff --git a/pool/db.php b/pool/db.php index 2b9d4c5e..c845d5e9 100644 --- a/pool/db.php +++ b/pool/db.php @@ -242,6 +242,7 @@ function userReg($user, $email, $pass) # function userSettings($user, $email = null, $addr = null, $pass = null, $twofa = null) { + global $fld_sep; $tmo = false; $flds = array('username' => $user); if ($email != null) @@ -253,6 +254,9 @@ function userSettings($user, $email = null, $addr = null, $pass = null, $twofa = foreach ($addr as $ar) { $flds['address:'.$i] = $ar['addr']; + // optional - missing = blank + if (isset($ar['payname'])) + $flds['payname:'.$i] = str_replace($fld_sep, ' ', trim($ar['payname'])); // optional - missing = use default if (isset($ar['ratio'])) $flds['ratio:'.$i] = $ar['ratio']; diff --git a/pool/page_addrmgt.php b/pool/page_addrmgt.php index d0300796..fe886b44 100644 --- a/pool/page_addrmgt.php +++ b/pool/page_addrmgt.php @@ -11,6 +11,7 @@ function addrmgtuser($data, $user, $err) $pg .= "\n"; $pg .= ''; $pg .= ''; + $pg .= ''; $pg .= ''; $pg .= ''; $pg .= ''; @@ -18,7 +19,10 @@ function addrmgtuser($data, $user, $err) # new row template for '+' $pg .= ''; $pg .= ''; + $pg .= ''; $pg .= '"; $addr = $ans['addr:'.$i]; $pg .= ''; + $nam = htmlspecialchars($ans['payname:'.$i]); + $pg .= ''; $ratio = intval($ans['ratio:'.$i]); $pg .= '"; - $pg .= ''; @@ -72,20 +80,21 @@ function addrmgtuser($data, $user, $err) else $row = 'odd'; $pg .= ""; - $pg .= ''; + $pg .= ''; + $pg .= ''; + $pg .= ''; if ((($offset++) % 2) == 0) $row = 'even'; else $row = 'odd'; $pg .= ""; - $pg .= ''; + $pg .= ''; + $pg .= ''; + $pg .= ''; - $pg .= "'; @@ -97,8 +106,9 @@ function addrmgtuser($data, $user, $err) $pg .= "var r=document.getElementById('rows');var c=parseInt(r.value);"; $pg .= "var bs=document.getElementById('bs');var n=bs.cloneNode(true);n.id='z';"; $pg .= "n.className='odd';var ia=n.childNodes[0].firstChild;ia.name='addr:'+c;ia.value='';"; - $pg .= "var ir=n.childNodes[1].firstChild;ir.id='rat'+c;ir.name='ratio:'+c;ir.value='0';"; - $pg .= "var ip=n.childNodes[2].firstChild;ip.id='per'+c;ip.innerHTML='0.00%';"; + $pg .= "var ipn=n.childNodes[1].firstChild;ipn.name='payname:'+c;ipn.value='';"; + $pg .= "var ir=n.childNodes[2].firstChild;ir.id='rat'+c;ir.name='ratio:'+c;ir.value='0';"; + $pg .= "var ip=n.childNodes[3].firstChild;ip.id='per'+c;ip.innerHTML='0.00%';"; $pg .= "p.parentNode.insertBefore(n, p);"; $pg .= "c++;r.value=c;return true}\n"; $pg .= "function repc(){var c=parseInt(document.getElementById('rows').value);"; @@ -127,9 +137,12 @@ function doaddrmgt($data, $user) for ($i = 0; $i < $count; $i++) { $addr = getparam('addr:'.$i, false); + $nam = getparam('payname:'.$i, false); + if (nuem($nam)) + $nam = ''; $ratio = getparam('ratio:'.$i, false); if (!nuem($addr) && !nuem($ratio)) - $addrarr[] = array('addr' => $addr, 'ratio' => $ratio); + $addrarr[] = array('addr' => $addr, 'payname' => $nam, 'ratio' => $ratio); } $ans = userSettings($user, null, $addrarr, $pass, $twofa); if ($ans['STATUS'] != 'ok') diff --git a/pool/page_percent.php b/pool/page_percent.php index 2cd5b887..1e303691 100644 --- a/pool/page_percent.php +++ b/pool/page_percent.php @@ -4,6 +4,7 @@ function pertitle($data, $user) { $pg = ''; $pg .= ''; + $pg .= ''; $pg .= ''; $pg .= ''; $pg .= ''; @@ -40,6 +41,7 @@ function peruser($data, $user, &$offset, &$totshare, &$totdiff, $all[] = array('payaddress' => $ans['payaddress:'.$i], 'payratio' => $ans['payratio:'.$i], 'paypercent' => $ans['paypercent:'.$i], + 'payname' => $ans['payname:'.$i], 'p_shareacc' => $ans['p_shareacc:'.$i], 'p_diffacc' => $ans['p_diffacc:'.$i], 'p_diffinv' => $ans['p_diffinv:'.$i], @@ -58,6 +60,7 @@ function peruser($data, $user, &$offset, &$totshare, &$totdiff, $pg .= ""; $pg .= ''; + $pg .= ''; $shareacc = number_format($all[$i]['p_shareacc'], 0); $totshare += $all[$i]['p_shareacc']; @@ -113,6 +116,7 @@ function pertotal($offset, $totshare, $totdiff, $totinvalid, $totrate, $blockacc else $row = 'odd'; $pg .= ""; + $pg .= ""; $shareacc = number_format($totshare, 0); $pg .= ""; $diffacc = number_format($totdiff, 0); @@ -156,7 +160,7 @@ function dopercent($data, $user) if ($blockacc > 0 && $blockreward > 0) { $btc = btcfmt($totdiff / $blockacc * $blockreward); - $pg .= ''; } diff --git a/sql/ckdb.sql b/sql/ckdb.sql index 4f39ba26..614990ea 100644 --- a/sql/ckdb.sql +++ b/sql/ckdb.sql @@ -75,6 +75,8 @@ CREATE TABLE paymentaddresses ( userid bigint NOT NULL, payaddress character varying(256) DEFAULT ''::character varying NOT NULL, payratio integer DEFAULT 1000000 NOT NULL, + payname character varying(64) DEFAULT ''::character varying NOT NULL, + status char DEFAULT ' ' NOT NULL, createdate timestamp with time zone NOT NULL, createby character varying(64) DEFAULT ''::character varying NOT NULL, createcode character varying(128) DEFAULT ''::character varying NOT NULL, @@ -468,4 +470,4 @@ CREATE TABLE version ( PRIMARY KEY (vlock) ); -insert into version (vlock,version) values (1,'1.0.3'); +insert into version (vlock,version) values (1,'1.0.4'); diff --git a/sql/v1.0.3-v1.0.4.sql b/sql/v1.0.3-v1.0.4.sql new file mode 100644 index 00000000..ba49dc7f --- /dev/null +++ b/sql/v1.0.3-v1.0.4.sql @@ -0,0 +1,26 @@ +SET SESSION AUTHORIZATION 'postgres'; + +BEGIN transaction; + +DO $$ +DECLARE ver TEXT; +BEGIN + + UPDATE version set version='1.0.4' where vlock=1 and version='1.0.3'; + + IF found THEN + RETURN; + END IF; + + SELECT version into ver from version + WHERE vlock=1; + + RAISE EXCEPTION 'Wrong DB version - expect "1.0.3" - found "%"', ver; + +END $$; + +ALTER TABLE ONLY paymentaddresses + ADD COLUMN payname character varying(64) DEFAULT ''::character varying NOT NULL, + ADD COLUMN status char DEFAULT ' ' NOT NULL; + +END transaction; diff --git a/src/ckdb.c b/src/ckdb.c index 909e5b41..041ec653 100644 --- a/src/ckdb.c +++ b/src/ckdb.c @@ -3112,8 +3112,10 @@ static void *summariser(__maybe_unused void *arg) while (!everyone_die && !reload_queue_complete) cksleep_ms(42); - LOGWARNING("%s() Start processing...", __func__); - summariser_using_data = true; + if (!everyone_die) { + LOGWARNING("%s() Start processing...", __func__); + summariser_using_data = true; + } while (!everyone_die) { for (i = 0; i < 5; i++) { @@ -3670,8 +3672,10 @@ static void *marker(__maybe_unused void *arg) return NULL; } - LOGWARNING("%s() Start processing...", __func__); - marker_using_data = true; + if (!everyone_die) { + LOGWARNING("%s() Start processing...", __func__); + marker_using_data = true; + } while (!everyone_die) { for (i = 0; i < 5; i++) { @@ -3844,8 +3848,10 @@ static void *socketer(__maybe_unused void *arg) while (!everyone_die && !db_users_complete) cksem_mswait(&socketer_sem, 420); - LOGWARNING("%s() Start processing...", __func__); - socketer_using_data = true; + if (!everyone_die) { + LOGWARNING("%s() Start processing...", __func__); + socketer_using_data = true; + } want_first = true; while (!everyone_die) { diff --git a/src/ckdb.h b/src/ckdb.h index 2dd2d5a7..3c0ef443 100644 --- a/src/ckdb.h +++ b/src/ckdb.h @@ -54,7 +54,7 @@ */ #define DB_VLOCK "1" -#define DB_VERSION "1.0.3" +#define DB_VERSION "1.0.4" #define CKDB_VERSION DB_VERSION"-1.402" #define WHERE_FFL " - from %s %s() line %d" @@ -1186,6 +1186,7 @@ typedef struct paymentaddresses { int64_t userid; char payaddress[TXT_BIG+1]; int32_t payratio; + char payname[TXT_SML+1]; HISTORYDATECONTROLFIELDS; bool match; // non-DB field } PAYMENTADDRESSES; diff --git a/src/ckdb_cmd.c b/src/ckdb_cmd.c index f5669b95..4f2bce33 100644 --- a/src/ckdb_cmd.c +++ b/src/ckdb_cmd.c @@ -445,8 +445,8 @@ static char *cmd_userset(PGconn *conn, char *cmd, char *id, __maybe_unused tv_t *notcd, K_TREE *trf_root) { K_ITEM *i_username, *i_passwordhash, *i_2fa, *i_rows, *i_address; - K_ITEM *i_ratio, *i_email, *u_item, *pa_item, *old_pa_item; - char *email, *address; + K_ITEM *i_ratio, *i_payname, *i_email, *u_item, *pa_item, *old_pa_item; + char *email, *address, *payname; char reply[1024] = ""; size_t siz = sizeof(reply); char tmp[1024]; @@ -507,6 +507,9 @@ static char *cmd_userset(PGconn *conn, char *cmd, char *id, snprintf(tmp, sizeof(tmp), "ratio:%d=%d%c", rows, row->payratio, FLDSEP); APPEND_REALLOC(answer, off, len, tmp); + snprintf(tmp, sizeof(tmp), "payname:%d=%s%c", + rows, row->payname, FLDSEP); + APPEND_REALLOC(answer, off, len, tmp); rows++; pa_item = prev_in_ktree(ctx); @@ -517,7 +520,7 @@ static char *cmd_userset(PGconn *conn, char *cmd, char *id, snprintf(tmp, sizeof(tmp), "rows=%d%cflds=%s%c", rows, FLDSEP, - "addr,ratio", FLDSEP); + "addr,ratio,payname", FLDSEP); APPEND_REALLOC(answer, off, len, tmp); snprintf(tmp, sizeof(tmp), "arn=%s%carp=%s", "PaymentAddresses", FLDSEP, ""); @@ -615,11 +618,20 @@ static char *cmd_userset(PGconn *conn, char *cmd, char *id, } pa_item = pa_item->next; } + snprintf(tmp, sizeof(tmp), "payname:%d", i); + i_payname = optional_name(trf_root, tmp, + 0, NULL, + reply, siz); + if (i_payname) + payname = transfer_data(i_payname); + else + payname = EMPTY; pa_item = k_unlink_head(paymentaddresses_free); DATA_PAYMENTADDRESSES(row, pa_item); bzero(row, sizeof(*row)); STRNCPY(row->payaddress, address); row->payratio = ratio; + STRNCPY(row->payname, payname); k_add_head(pa_store, pa_item); } K_WUNLOCK(paymentaddresses_free); @@ -1808,6 +1820,10 @@ static char *cmd_percent(char *cmd, char *id, tv_t *now, USERS *users) rows, ratio * 100.0, FLDSEP); APPEND_REALLOC(buf, off, len, tmp); + snprintf(tmp, sizeof(tmp), "payname:%d=%s%c", + rows, pa->payname, FLDSEP); + APPEND_REALLOC(buf, off, len, tmp); + snprintf(tmp, sizeof(tmp), "p_hashrate5m:%d=%.1f%c", rows, (double)t_hashrate5m * ratio, FLDSEP); @@ -1893,7 +1909,7 @@ static char *cmd_percent(char *cmd, char *id, tv_t *now, USERS *users) snprintf(tmp, sizeof(tmp), "rows=%d%cflds=%s%c", rows, FLDSEP, - "payaddress,payratio,paypercent," + "payaddress,payratio,paypercent,payname," "p_hashrate5m,p_hashrate1hr,p_hashrate24hr," "p_diffacc,p_diffinv," "p_diffsta,p_diffdup,p_diffhi,p_diffrej," diff --git a/src/ckdb_dbio.c b/src/ckdb_dbio.c index 60940a30..90f34033 100644 --- a/src/ckdb_dbio.c +++ b/src/ckdb_dbio.c @@ -1723,7 +1723,9 @@ bool paymentaddresses_set(PGconn *conn, int64_t userid, K_STORE *pa_store, /* Since we are merging the changes in rather than just * replacing the db contents, lock the data for the duration - * of the update to ensure nothing else changes it */ + * of the update to ensure nothing else changes it + * N.B. 'payname' isn't the start of the key + * thus 2 different addresses can have the same 'payname' */ K_WLOCK(paymentaddresses_free); locked = true; @@ -1741,7 +1743,8 @@ bool paymentaddresses_set(PGconn *conn, int64_t userid, K_STORE *pa_store, while (match) { DATA_PAYMENTADDRESSES(pa, match); if (strcmp(pa->payaddress, row->payaddress) == 0 && - pa->payratio == row->payratio) { + pa->payratio == row->payratio && + strcmp(pa->payname, row->payname) == 0) { pa->match = true; // Don't store it matches++; break; @@ -1791,8 +1794,8 @@ bool paymentaddresses_set(PGconn *conn, int64_t userid, K_STORE *pa_store, // Second step - add the non-matching records to the DB LOGDEBUG("%s(): Step 2", __func__); ins = "insert into paymentaddresses " - "(paymentaddressid,userid,payaddress,payratio" - HISTORYDATECONTROL ") values (" PQPARAM9 ")"; + "(paymentaddressid,userid,payaddress,payratio,payname" + HISTORYDATECONTROL ") values (" PQPARAM10 ")"; count = 0; match = pa_store->head; @@ -1814,8 +1817,9 @@ bool paymentaddresses_set(PGconn *conn, int64_t userid, K_STORE *pa_store, params[par++] = bigint_to_buf(row->userid, NULL, 0); params[par++] = str_to_buf(row->payaddress, NULL, 0); params[par++] = int_to_buf(row->payratio, NULL, 0); + params[par++] = str_to_buf(row->payname, NULL, 0); HISTORYDATEPARAMS(params, par, row); - PARCHKVAL(par, 9, params); // As per PQPARAM9 above + PARCHKVAL(par, 10, params); // As per PQPARAM10 above res = PQexecParams(conn, ins, par, NULL, (const char **)params, NULL, NULL, 0, CKPQ_WRITE); @@ -1864,7 +1868,8 @@ unparam: while (match) { DATA_PAYMENTADDRESSES(pa, match); if (strcmp(pa->payaddress, row->payaddress) == 0 && - pa->payratio == row->payratio) { + pa->payratio == row->payratio && + strcmp(pa->payname, row->payname) == 0) { break; } match = match->next; @@ -1917,13 +1922,13 @@ bool paymentaddresses_fill(PGconn *conn) int n, i; char *field; char *sel; - int fields = 4; + int fields = 5; bool ok; LOGDEBUG("%s(): select", __func__); sel = "select " - "paymentaddressid,userid,payaddress,payratio" + "paymentaddressid,userid,payaddress,payratio,payname" HISTORYDATECONTROL " from paymentaddresses"; res = PQexec(conn, sel, CKPQ_READ); @@ -1976,6 +1981,11 @@ bool paymentaddresses_fill(PGconn *conn) break; TXT_TO_INT("payratio", field, row->payratio); + PQ_GET_FLD(res, i, "payname", field, ok); + if (!ok) + break; + TXT_TO_STR("payname", field, row->payname); + HISTORYDATEFLDS(res, i, row, ok); if (!ok) break;
AddressIDRatio%
'; - $pg .= ""; + $pg .= ""; + $pg .= ''; + $pg .= ""; $pg .= ''; $pg .= ""; @@ -45,7 +49,11 @@ function addrmgtuser($data, $user, $err) $pg .= "
'; - $pg .= ""; + $pg .= ""; + $pg .= ''; + $pg .= ""; $pg .= ''; @@ -63,7 +71,7 @@ function addrmgtuser($data, $user, $err) else $row = 'odd'; $pg .= "
'; + $pg .= ''; $pg .= ""; $pg .= '
'; - $pg .= 'Password: '; - $pg .= ' 
Password: 
'; - $pg .= '*2nd Authentication: '; - $pg .= '
*2nd Authentication:'; + $pg .= '  
"; + $pg .= "
"; $pg .= "*Leave blank if you haven't enabled it
"; $pg .= 'You must enter your password to save changes
'; $pg .= 'A ratio of 0, will remove the address from the payouts
AddressIDSharesDiffInvalid
'.$all[$i]['payaddress'].''.$all[$i]['payname'].'
Total: $shareacc
'; + $pg .= '
'; $pg .= "
Payout est if block found at 100%: ~$btc BTC"; $pg .= '