From e684a987cc430164c289b151144be09c5d75d352 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 20 Mar 2014 12:11:35 +0100 Subject: [PATCH 01/10] [ADDED] Calc Coin PPS Value Method --- cronjobs/pps_payout.php | 2 +- public/include/classes/coins/coin_base.class.php | 8 ++++++++ public/include/classes/statistics.class.php | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index d7036ee0..cc1cbad1 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -63,7 +63,7 @@ if ($config['pps']['reward']['type'] == 'blockavg' && $block->getBlockCount() > } // Per-share value to be paid out to users -$pps_value = round($pps_reward / (pow(2, $coin->getTargetBits()) * $dDifficulty), 12); +$pps_value = round($coin->calcPPSValue($pps_reward, $dDifficulty), 12); // Find our last share accounted and last inserted share for PPS calculations if (!$iPreviousShareId = $setting->getValue('pps_last_share_id')) { diff --git a/public/include/classes/coins/coin_base.class.php b/public/include/classes/coins/coin_base.class.php index 79e74c58..64e412e2 100644 --- a/public/include/classes/coins/coin_base.class.php +++ b/public/include/classes/coins/coin_base.class.php @@ -19,6 +19,14 @@ class CoinBase extends Base { return $this->target_bits; } + /** + * Calculate the PPS value for this coin + * WARNING: Get this wrong and you will over- or underpay your miners! + **/ + public function calcPPSValue($pps_reward, $dDifficulty) { + return ($pps_reward / (pow(2, $this->target_bits) * $dDifficulty)); + } + /** * Calculate our hashrate based on shares inserted to DB * We use diff1 share values, not a baseline one diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index b04bff4a..d368afe4 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -892,7 +892,7 @@ class Statistics extends Base { $pps_reward = $this->config['pps']['reward']['default']; } } - return round($pps_reward / (pow(2, $this->coin->getTargetBits()) * $dDifficulty), 12); + return round($this->coin->calcPPSValue($pps_reward, $dDifficulty), 12); } /** From 0bdc124eb2725594eca0a9bca1b3e434e3af55fb Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 20 Mar 2014 14:00:38 +0100 Subject: [PATCH 02/10] [FIX] Spelling mistake Thanks @szoyj --- public/include/classes/user.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/include/classes/user.class.php b/public/include/classes/user.class.php index 81fefd45..498ac584 100644 --- a/public/include/classes/user.class.php +++ b/public/include/classes/user.class.php @@ -863,7 +863,7 @@ class User extends Base { $this->debug->append("STA " . __METHOD__, 4); // Fetch the users mail address if (empty($username)) { - $this->serErrorMessage("Username must not be empty"); + $this->setErrorMessage("Username must not be empty"); return false; } if (filter_var($username, FILTER_VALIDATE_EMAIL)) { From 67a4713cf025afc419ae7309ada53a52c7009334 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 21 Mar 2014 11:51:34 +0100 Subject: [PATCH 03/10] [REMOVED] Baseline Share calculations * We now track diff1 shares instead of converting into a baselined value * PPS has been adjusted to payout shares directly * Estimated Shares in coin_base adjusted --- cronjobs/pps_payout.php | 7 +++---- public/include/classes/coins/coin_base.class.php | 2 +- public/include/classes/share.class.php | 10 +++++----- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index cc1cbad1..509176d2 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -105,9 +105,8 @@ foreach ($aAccountShares as $aData) { continue; } - // MPOS uses a base difficulty setting to avoid showing weightened shares - // Since we need weightened shares here, we go back to the proper value for payouts - $aData['payout'] = round($aData['valid'] * pow(2, ($config['difficulty'] - 16)) * $pps_value, 12); + // Payout for this user + $aData['payout'] = round($aData['valid'] * $pps_value, 12); // Defaults $aData['fee' ] = 0; @@ -120,7 +119,7 @@ foreach ($aAccountShares as $aData) { $aData['donation'] = round($user->getDonatePercent($user->getUserId($aData['username'])) / 100 * ( $aData['payout'] - $aData['fee']), 12); $log->logInfo(sprintf( - $strLogMask, $aData['id'], $aData['username'], $aData['invalid'], $aData['valid'] * pow(2, ($config['difficulty'] - 16)), + $strLogMask, $aData['id'], $aData['username'], $aData['invalid'], $aData['valid'], number_format($pps_value, 12), number_format($aData['payout'], 12), number_format($aData['donation'], 12), number_format($aData['fee'], 12) )); diff --git a/public/include/classes/coins/coin_base.class.php b/public/include/classes/coins/coin_base.class.php index 64e412e2..1eb5470c 100644 --- a/public/include/classes/coins/coin_base.class.php +++ b/public/include/classes/coins/coin_base.class.php @@ -40,7 +40,7 @@ class CoinBase extends Base { * according to our configuration difficulty **/ public function calcEstaimtedShares($dDifficulty) { - return (int)round((pow(2, (32 - $this->target_bits)) * $dDifficulty) / pow(2, ($this->config['difficulty'] - 16))); + return (int)round(pow(2, (32 - $this->target_bits)) * $dDifficulty, 0); } /** diff --git a/public/include/classes/share.class.php b/public/include/classes/share.class.php index 0d11c4d5..3dd0e573 100644 --- a/public/include/classes/share.class.php +++ b/public/include/classes/share.class.php @@ -75,7 +75,7 @@ class Share Extends Base { **/ public function getRoundShares($previous_upstream=0, $current_upstream) { $stmt = $this->mysqli->prepare("SELECT - ROUND(IFNULL(SUM(IF(s.difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 8) AS total + IFNULL(SUM(IF(s.difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty)), 0) AS total FROM $this->table AS s LEFT JOIN " . $this->user->getTableName() . " AS a ON a.username = SUBSTRING_INDEX( s.username , '.', 1 ) @@ -99,8 +99,8 @@ class Share Extends Base { a.id, SUBSTRING_INDEX( s.username , '.', 1 ) as username, a.no_fees AS no_fees, - ROUND(IFNULL(SUM(IF(our_result='Y', IF(s.difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty), 0)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 8) AS valid, - ROUND(IFNULL(SUM(IF(our_result='N', IF(s.difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty), 0)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 8) AS invalid + IFNULL(SUM(IF(our_result='Y', IF(s.difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty), 0)), 0) AS valid, + IFNULL(SUM(IF(our_result='N', IF(s.difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty), 0)), 0) AS invalid FROM $this->table AS s LEFT JOIN " . $this->user->getTableName() . " AS a ON a.username = SUBSTRING_INDEX( s.username , '.', 1 ) @@ -146,8 +146,8 @@ class Share Extends Base { a.id, SUBSTRING_INDEX( s.username , '.', 1 ) as account, a.no_fees AS no_fees, - ROUND(IFNULL(SUM(IF(our_result='Y', IF(s.difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty), 0)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 8) AS valid, - ROUND(IFNULL(SUM(IF(our_result='N', IF(s.difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty), 0)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 8) AS invalid + IFNULL(SUM(IF(our_result='Y', IF(s.difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty), 0)), 0) AS valid, + IFNULL(SUM(IF(our_result='N', IF(s.difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty), 0)), 0) AS invalid FROM $this->tableArchive AS s LEFT JOIN " . $this->user->getTableName() . " AS a ON a.username = SUBSTRING_INDEX( s.username , '.', 1 ) From 1f6ef81b9c3f690b180bf088d00a66840bec235f Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 21 Mar 2014 12:47:24 +0100 Subject: [PATCH 04/10] [ADDED] SQL Upgrade Script --- public/include/version.inc.php | 2 +- upgrade/db_version_0.0.8.php | 36 ++++++++++++++++++++++++ upgrade/shared.inc.php | 51 ++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100755 upgrade/db_version_0.0.8.php create mode 100644 upgrade/shared.inc.php diff --git a/public/include/version.inc.php b/public/include/version.inc.php index 7244eb91..a9065e8c 100644 --- a/public/include/version.inc.php +++ b/public/include/version.inc.php @@ -2,7 +2,7 @@ $defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1; define('MPOS_VERSION', '0.0.4'); -define('DB_VERSION', '0.0.7'); +define('DB_VERSION', '0.0.8'); define('CONFIG_VERSION', '0.0.7'); // Fetch installed database version diff --git a/upgrade/db_version_0.0.8.php b/upgrade/db_version_0.0.8.php new file mode 100755 index 00000000..de58dd30 --- /dev/null +++ b/upgrade/db_version_0.0.8.php @@ -0,0 +1,36 @@ +#!/usr/bin/php +getValue('DB_VERSION'); // Our actual version installed + +// Upgrade specific variables +$dDifficulty = POW(2, ($config['difficulty'] - 16)); +$aSql[] = "UPDATE " . $statistics->getTableName() . " SET valid = valid * $dDifficulty, invalid = invalid * $dDifficulty, pplns_valid = pplns_valid * $dDifficulty, pplns_invalid = pplns_invalid * $dDifficulty"; +$aSql[] = "UPDATE " . $block->getTableName() . " SET shares = shares * $dDifficulty"; +$aSql[] = "UPDATE " . $setting->getTableName() . " SET value = '0.0.8' WHERE name = 'DB_VERSION'"; + +if ($db_version_now == $db_version_old && version_compare($db_version_now, DB_VERSION, '<')) { + // Run the upgrade + echo 'Starting database migration to version ' . $db_version_new . PHP_EOL; + foreach ($aSql as $sql) { + echo ' Preparing: ' . $sql . PHP_EOL; + $stmt = $mysqli->prepare($sql); + if ($stmt && $stmt->execute()) { + echo ' success' . PHP_EOL; + } else { + echo ' failed: ' . $mysqli->error . PHP_EOL; + exit(1); + } + } +} +?> diff --git a/upgrade/shared.inc.php b/upgrade/shared.inc.php new file mode 100644 index 00000000..0f0638cb --- /dev/null +++ b/upgrade/shared.inc.php @@ -0,0 +1,51 @@ + From 055e4259e15516466a57e95249df7208b34a4373 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 21 Mar 2014 12:54:29 +0100 Subject: [PATCH 05/10] [REMOVED] Baseline shares from statistics --- public/include/classes/statistics.class.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index d368afe4..c9de9d24 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -104,7 +104,7 @@ class Statistics extends Base { b.*, a.username AS finder, a.is_anonymous AS is_anonymous, - ROUND((difficulty * POW(2, 32 - " . $this->coin->getTargetBits() . ")) / POW(2, (" . $this->config['difficulty'] . " -16)), 0) AS estshares + ROUND(difficulty * POW(2, 32 - " . $this->coin->getTargetBits() . "), 0) AS estshares FROM " . $this->block->getTableName() . " AS b LEFT JOIN " . $this->user->getTableName() . " AS a ON b.account_id = a.id @@ -127,7 +127,7 @@ class Statistics extends Base { b.*, a.username AS finder, a.is_anonymous AS is_anonymous, - ROUND((difficulty * POW(2, 32 - " . $this->coin->getTargetBits() . ")) / POW(2, (" . $this->config['difficulty'] . " -16)), 0) AS estshares + ROUND(difficulty * POW(2, 32 - " . $this->coin->getTargetBits() . "), 0) AS estshares FROM " . $this->block->getTableName() . " AS b LEFT JOIN " . $this->user->getTableName() . " AS a ON b.account_id = a.id @@ -293,8 +293,8 @@ class Statistics extends Base { } $stmt = $this->mysqli->prepare(" SELECT - ROUND(IFNULL(SUM(IF(our_result='Y', IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty), 0)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 0) AS valid, - ROUND(IFNULL(SUM(IF(our_result='N', IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty), 0)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 0) AS invalid + ROUND(IFNULL(SUM(IF(our_result='Y', IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty), 0)), 0), 0) AS valid, + ROUND(IFNULL(SUM(IF(our_result='N', IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty), 0)), 0), 0) AS invalid FROM " . $this->share->getTableName() . " WHERE UNIX_TIMESTAMP(time) > IFNULL((SELECT MAX(time) FROM " . $this->block->getTableName() . "), 0)"); if ( $this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) @@ -316,8 +316,8 @@ class Statistics extends Base { } $stmt = $this->mysqli->prepare(" SELECT - ROUND(IFNULL(SUM(IF(our_result='Y', IF(s.difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty), 0)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 0) AS valid, - ROUND(IFNULL(SUM(IF(our_result='N', IF(s.difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty), 0)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 0) AS invalid, + ROUND(IFNULL(SUM(IF(our_result='Y', IF(s.difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty), 0)), 0), 0) AS valid, + ROUND(IFNULL(SUM(IF(our_result='N', IF(s.difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty), 0)), 0), 0) AS invalid, u.id AS id, u.donate_percent AS donate_percent, u.is_anonymous AS is_anonymous, @@ -368,8 +368,8 @@ class Statistics extends Base { if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; $stmt = $this->mysqli->prepare(" SELECT - ROUND(IFNULL(SUM(IF(our_result='Y', IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty), 0)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 0) AS valid, - ROUND(IFNULL(SUM(IF(our_result='N', IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty), 0)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 0) AS invalid + ROUND(IFNULL(SUM(IF(our_result='Y', IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty), 0)), 0), 0) AS valid, + ROUND(IFNULL(SUM(IF(our_result='N', IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty), 0)), 0), 0) AS invalid FROM " . $this->share->getTableName() . " WHERE username LIKE ? AND UNIX_TIMESTAMP(time) >IFNULL((SELECT MAX(b.time) FROM " . $this->block->getTableName() . " AS b),0)"); @@ -500,7 +500,7 @@ class Statistics extends Base { if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; $stmt = $this->mysqli->prepare(" SELECT - ROUND(IFNULL(SUM(IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 0) AS total + ROUND(IFNULL(SUM(IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty)), 0), 0) AS total FROM " . $this->share->getTableName() . " WHERE username LIKE ? AND id > ? @@ -603,7 +603,7 @@ class Statistics extends Base { a.username AS account, a.donate_percent AS donate_percent, a.is_anonymous AS is_anonymous, - ROUND(IFNULL(SUM(IF(s.difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 0) AS shares + ROUND(IFNULL(SUM(IF(s.difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty)), 0), 0) AS shares FROM " . $this->share->getTableName() . " AS s LEFT JOIN " . $this->user->getTableName() . " AS a ON SUBSTRING_INDEX( s.username, '.', 1 ) = a.username From d72b4f6a51656c97347736aee940d89adc027308 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 21 Mar 2014 12:56:31 +0100 Subject: [PATCH 06/10] [REMOVED] Baseline from roundstats --- public/include/classes/roundstats.class.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/public/include/classes/roundstats.class.php b/public/include/classes/roundstats.class.php index 81c8f1d1..8c3f0315 100644 --- a/public/include/classes/roundstats.class.php +++ b/public/include/classes/roundstats.class.php @@ -78,7 +78,7 @@ class RoundStats extends Base { SELECT b.id, height, blockhash, amount, confirmations, difficulty, FROM_UNIXTIME(time) as time, shares, IF(a.is_anonymous, 'anonymous', a.username) AS finder, - ROUND((difficulty * 65535) / POW(2, (" . $this->config['difficulty'] . " -16)), 0) AS estshares, + ROUND(difficulty * POW(2, 32 - " . $this->coin->getTargetBits() . "), 0) AS estshares, (time - (SELECT time FROM $this->tableBlocks WHERE height < ? ORDER BY height DESC LIMIT 1)) AS round_time FROM " . $this->block->getTableName() . " as b LEFT JOIN " . $this->user->getTableName() . " AS a ON b.account_id = a.id @@ -288,3 +288,4 @@ $roundstats->setUser($user); $roundstats->setStatistics($statistics); $roundstats->setBlock($block); $roundstats->setTransaction($transaction); +$roundstats->setCoin($coin); From 10ad428575792fbc701003156bdaabf39f51ab75 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 21 Mar 2014 14:06:31 +0100 Subject: [PATCH 07/10] [UPDATED] Logrotate conf --- cronjobs/etc/logrotate.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cronjobs/etc/logrotate.conf b/cronjobs/etc/logrotate.conf index 341b89e7..5ec8786e 100644 --- a/cronjobs/etc/logrotate.conf +++ b/cronjobs/etc/logrotate.conf @@ -1,4 +1,4 @@ -"./logs/*.txt" { +"./logs/*/*.txt" { copytruncate rotate 7 compress From bc42963c755c695e9b69c6cd5a925a2b560a8b84 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 21 Mar 2014 14:06:46 +0100 Subject: [PATCH 08/10] [DEPRECATED] run-crons throws a warning now --- cronjobs/run-crons.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cronjobs/run-crons.sh b/cronjobs/run-crons.sh index d1cca24b..90fe718f 100755 --- a/cronjobs/run-crons.sh +++ b/cronjobs/run-crons.sh @@ -1,5 +1,7 @@ #!/bin/bash +echo "Please be aware: This cron is deprecated and will be removed. Please read: https://github.com/MPOS/php-mpos/wiki/Cronjobs#setup" +sleep 2 ######################### # # From f9cba03c689cd8af2f085daffc327bd3eda71bd4 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 21 Mar 2014 15:12:13 +0100 Subject: [PATCH 09/10] [IMPROVED] Advanced upgrade structure --- upgrade/db_version_0.0.8.php | 36 ---------------------- upgrade/definitions/0.0.7_to_0.0.8.inc.php | 32 +++++++++++++++++++ upgrade/run_upgrades.php | 33 ++++++++++++++++++++ 3 files changed, 65 insertions(+), 36 deletions(-) delete mode 100755 upgrade/db_version_0.0.8.php create mode 100755 upgrade/definitions/0.0.7_to_0.0.8.inc.php create mode 100755 upgrade/run_upgrades.php diff --git a/upgrade/db_version_0.0.8.php b/upgrade/db_version_0.0.8.php deleted file mode 100755 index de58dd30..00000000 --- a/upgrade/db_version_0.0.8.php +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/php -getValue('DB_VERSION'); // Our actual version installed - -// Upgrade specific variables -$dDifficulty = POW(2, ($config['difficulty'] - 16)); -$aSql[] = "UPDATE " . $statistics->getTableName() . " SET valid = valid * $dDifficulty, invalid = invalid * $dDifficulty, pplns_valid = pplns_valid * $dDifficulty, pplns_invalid = pplns_invalid * $dDifficulty"; -$aSql[] = "UPDATE " . $block->getTableName() . " SET shares = shares * $dDifficulty"; -$aSql[] = "UPDATE " . $setting->getTableName() . " SET value = '0.0.8' WHERE name = 'DB_VERSION'"; - -if ($db_version_now == $db_version_old && version_compare($db_version_now, DB_VERSION, '<')) { - // Run the upgrade - echo 'Starting database migration to version ' . $db_version_new . PHP_EOL; - foreach ($aSql as $sql) { - echo ' Preparing: ' . $sql . PHP_EOL; - $stmt = $mysqli->prepare($sql); - if ($stmt && $stmt->execute()) { - echo ' success' . PHP_EOL; - } else { - echo ' failed: ' . $mysqli->error . PHP_EOL; - exit(1); - } - } -} -?> diff --git a/upgrade/definitions/0.0.7_to_0.0.8.inc.php b/upgrade/definitions/0.0.7_to_0.0.8.inc.php new file mode 100755 index 00000000..623af7e1 --- /dev/null +++ b/upgrade/definitions/0.0.7_to_0.0.8.inc.php @@ -0,0 +1,32 @@ +getValue('DB_VERSION'); // Our actual version installed + + // Upgrade specific variables + $dDifficulty = POW(2, ($config['difficulty'] - 16)); + $aSql[] = "UPDATE " . $statistics->getTableName() . " SET valid = valid / $dDifficulty, invalid = invalid / $dDifficulty, pplns_valid = pplns_valid / $dDifficulty, pplns_invalid = pplns_invalid / $dDifficulty"; + $aSql[] = "UPDATE " . $block->getTableName() . " SET shares = shares / $dDifficulty"; + $aSql[] = "UPDATE " . $setting->getTableName() . " SET value = '0.0.8' WHERE name = 'DB_VERSION'"; + + if ($db_version_now == $db_version_old && version_compare($db_version_now, DB_VERSION, '<')) { + // Run the upgrade + echo '- Starting database migration to version ' . $db_version_new . PHP_EOL; + foreach ($aSql as $sql) { + echo '- Preparing: ' . $sql . PHP_EOL; + $stmt = $mysqli->prepare($sql); + if ($stmt && $stmt->execute()) { + echo '- success' . PHP_EOL; + } else { + echo '- failed: ' . $mysqli->error . PHP_EOL; + exit(1); + } + } + } +} +?> diff --git a/upgrade/run_upgrades.php b/upgrade/run_upgrades.php new file mode 100755 index 00000000..dd949c8a --- /dev/null +++ b/upgrade/run_upgrades.php @@ -0,0 +1,33 @@ +#!/usr/bin/php +getValue('DB_VERSION'); + +// Helper function +function run_db_upgrade($db_version_now) { + // Find upgrades + $files = glob(dirname(__FILE__) . "/definitions/${db_version_now}_to_*"); + if (count($files) == 0) die('No upgrade definitions found for ' . $db_version_now . PHP_EOL); + foreach ($files as $file) { + $db_version_to = preg_replace("/(.*)\/definitions\/${db_version_now}_to_/", '', $file); + $db_version_to = preg_replace("/.inc.php/", '', $db_version_to); + $run_string = preg_replace("/\./", '', $db_version_to); + if (!require_once($file)) die('Failed to load upgrade definition: ' . $file); + echo "+ Running upgrade from $db_version_now to $db_version_to" . PHP_EOL; + $run = "run_$run_string"; + $run(); + run_db_upgrade($db_version_to); + } +} + +// Initial caller +run_db_upgrade($db_version_now); +?> From 8b28b25e1812d2dcd6dcc147042a77f4115ca6eb Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 21 Mar 2014 15:49:30 +0100 Subject: [PATCH 10/10] [CHANGE] Moved SQL files into upgrade script --- sql/012_accounts_update.sql | 3 --- sql/013_tokentype_update.sql | 6 ------ sql/014_accounts_update.sql | 5 ----- sql/015_accounts_update.sql | 3 --- sql/016_transactions_update.sql | 2 -- sql/017_blocks_update.sql | 2 -- upgrade/definitions/0.0.1_to_0.0.2.inc.php | 19 ++++++++++++++++++ upgrade/definitions/0.0.2_to_0.0.3.inc.php | 23 ++++++++++++++++++++++ upgrade/definitions/0.0.3_to_0.0.4.inc.php | 22 +++++++++++++++++++++ upgrade/definitions/0.0.4_to_0.0.5.inc.php | 21 ++++++++++++++++++++ upgrade/definitions/0.0.5_to_0.0.6.inc.php | 19 ++++++++++++++++++ upgrade/definitions/0.0.6_to_0.0.7.inc.php | 19 ++++++++++++++++++ upgrade/run_upgrades.php | 17 +++++++++++++++- 13 files changed, 139 insertions(+), 22 deletions(-) delete mode 100644 sql/012_accounts_update.sql delete mode 100644 sql/013_tokentype_update.sql delete mode 100644 sql/014_accounts_update.sql delete mode 100644 sql/015_accounts_update.sql delete mode 100644 sql/016_transactions_update.sql delete mode 100644 sql/017_blocks_update.sql create mode 100755 upgrade/definitions/0.0.1_to_0.0.2.inc.php create mode 100755 upgrade/definitions/0.0.2_to_0.0.3.inc.php create mode 100755 upgrade/definitions/0.0.3_to_0.0.4.inc.php create mode 100755 upgrade/definitions/0.0.4_to_0.0.5.inc.php create mode 100755 upgrade/definitions/0.0.5_to_0.0.6.inc.php create mode 100755 upgrade/definitions/0.0.6_to_0.0.7.inc.php diff --git a/sql/012_accounts_update.sql b/sql/012_accounts_update.sql deleted file mode 100644 index 2ba7cbbe..00000000 --- a/sql/012_accounts_update.sql +++ /dev/null @@ -1,3 +0,0 @@ -ALTER TABLE `accounts` CHANGE `sessionTimeoutStamp` `last_login` INT( 10 ) NULL DEFAULT NULL ; -INSERT INTO `settings` (`name`, `value`) VALUES ('DB_VERSION', '0.0.2') ON DUPLICATE KEY UPDATE `value` = '0.0.2'; -INSERT INTO `settings` (`name`, `value`) VALUES ('db_upgrade_required', 0) ON DUPLICATE KEY UPDATE `value` = 0; diff --git a/sql/013_tokentype_update.sql b/sql/013_tokentype_update.sql deleted file mode 100644 index 8445e2f7..00000000 --- a/sql/013_tokentype_update.sql +++ /dev/null @@ -1,6 +0,0 @@ -INSERT INTO `token_types` (`name`, `expiration`) VALUES ('account_edit', 360); -INSERT INTO `token_types` (`name`, `expiration`) VALUES ('change_pw', 360); -INSERT INTO `token_types` (`name`, `expiration`) VALUES ('withdraw_funds', 360); -CREATE INDEX `account_id` ON `notification_settings` (`account_id`); -CREATE UNIQUE INDEX `account_id_type` ON `notification_settings` (`account_id`,`type`); -INSERT INTO `settings` (`name`, `value`) VALUES ('DB_VERSION', '0.0.3') ON DUPLICATE KEY UPDATE `value` = '0.0.3'; diff --git a/sql/014_accounts_update.sql b/sql/014_accounts_update.sql deleted file mode 100644 index ed7802c0..00000000 --- a/sql/014_accounts_update.sql +++ /dev/null @@ -1,5 +0,0 @@ -ALTER TABLE `accounts` ADD COLUMN `signup_timestamp` INT( 10 ) NOT NULL DEFAULT '0' AFTER `failed_pins`; -ALTER TABLE `accounts` ADD COLUMN `notify_email` VARCHAR( 255 ) NULL DEFAULT NULL AFTER `email`; -TRUNCATE TABLE `token_types`; -INSERT INTO `token_types` (`id`, `name`, `expiration`) VALUES (1, 'password_reset', 3600), (2, 'confirm_email', 0), (3, 'invitation', 0), (4, 'account_unlock', 0), (5, 'account_edit', 3600), (6, 'change_pw', 3600), (7, 'withdraw_funds', 3600); -INSERT INTO `settings` (`name`, `value`) VALUES ('DB_VERSION', '0.0.4') ON DUPLICATE KEY UPDATE `value` = '0.0.4'; diff --git a/sql/015_accounts_update.sql b/sql/015_accounts_update.sql deleted file mode 100644 index 3ff16668..00000000 --- a/sql/015_accounts_update.sql +++ /dev/null @@ -1,3 +0,0 @@ -UPDATE `accounts` SET `coin_address` = NULL WHERE `coin_address` = ''; -ALTER TABLE `accounts` ADD UNIQUE INDEX ( `coin_address` ) ; -INSERT INTO `settings` (`name`, `value`) VALUES ('DB_VERSION', '0.0.5') ON DUPLICATE KEY UPDATE `value` = '0.0.5'; diff --git a/sql/016_transactions_update.sql b/sql/016_transactions_update.sql deleted file mode 100644 index 6458aab8..00000000 --- a/sql/016_transactions_update.sql +++ /dev/null @@ -1,2 +0,0 @@ -CREATE INDEX `account_id_archived` ON `transactions` (`account_id`,`archived`); -INSERT INTO `settings` (`name`, `value`) VALUES ('DB_VERSION', '0.0.6') ON DUPLICATE KEY UPDATE `value` = '0.0.6'; diff --git a/sql/017_blocks_update.sql b/sql/017_blocks_update.sql deleted file mode 100644 index 0e87da9b..00000000 --- a/sql/017_blocks_update.sql +++ /dev/null @@ -1,2 +0,0 @@ -ALTER TABLE `blocks` CHANGE `share_id` `share_id` BIGINT( 30 ) NULL DEFAULT NULL ; -INSERT INTO `settings` (`name`, `value`) VALUES ('DB_VERSION', '0.0.7') ON DUPLICATE KEY UPDATE `value` = '0.0.7'; diff --git a/upgrade/definitions/0.0.1_to_0.0.2.inc.php b/upgrade/definitions/0.0.1_to_0.0.2.inc.php new file mode 100755 index 00000000..1b24ca56 --- /dev/null +++ b/upgrade/definitions/0.0.1_to_0.0.2.inc.php @@ -0,0 +1,19 @@ +getValue('DB_VERSION'); // Our actual version installed + + // Upgrade specific variables + $aSql[] = "ALTER TABLE `accounts` CHANGE `sessionTimeoutStamp` `last_login` INT( 10 ) NULL DEFAULT NULL"; + $aSql[] = "INSERT INTO `settings` (`name`, `value`) VALUES ('DB_VERSION', '0.0.2') ON DUPLICATE KEY UPDATE `value` = '0.0.2'"; + + if ($db_version_now == $db_version_old && version_compare($db_version_now, DB_VERSION, '<')) { + execute_db_upgrade($aSql); + } +} +?> diff --git a/upgrade/definitions/0.0.2_to_0.0.3.inc.php b/upgrade/definitions/0.0.2_to_0.0.3.inc.php new file mode 100755 index 00000000..c7025fcb --- /dev/null +++ b/upgrade/definitions/0.0.2_to_0.0.3.inc.php @@ -0,0 +1,23 @@ +getValue('DB_VERSION'); // Our actual version installed + + // Upgrade specific variables + $aSql[] = "INSERT INTO `token_types` (`name`, `expiration`) VALUES ('account_edit', 360)"; + $aSql[] = "INSERT INTO `token_types` (`name`, `expiration`) VALUES ('change_pw', 360)"; + $aSql[] = "INSERT INTO `token_types` (`name`, `expiration`) VALUES ('withdraw_funds', 360)"; + $aSql[] = "CREATE INDEX `account_id` ON `notification_settings` (`account_id`)"; + $aSql[] = "CREATE UNIQUE INDEX `account_id_type` ON `notification_settings` (`account_id`,`type`)"; + $aSql[] = "INSERT INTO `settings` (`name`, `value`) VALUES ('DB_VERSION', '0.0.3') ON DUPLICATE KEY UPDATE `value` = '0.0.3'"; + + if ($db_version_now == $db_version_old && version_compare($db_version_now, DB_VERSION, '<')) { + execute_db_upgrade($aSql); + } +} +?> diff --git a/upgrade/definitions/0.0.3_to_0.0.4.inc.php b/upgrade/definitions/0.0.3_to_0.0.4.inc.php new file mode 100755 index 00000000..d843a4aa --- /dev/null +++ b/upgrade/definitions/0.0.3_to_0.0.4.inc.php @@ -0,0 +1,22 @@ +getValue('DB_VERSION'); // Our actual version installed + + // Upgrade specific variables + $aSql[] = "ALTER TABLE `accounts` ADD COLUMN `signup_timestamp` INT( 10 ) NOT NULL DEFAULT '0' AFTER `failed_pins`"; + $aSql[] = "ALTER TABLE `accounts` ADD COLUMN `notify_email` VARCHAR( 255 ) NULL DEFAULT NULL AFTER `email`"; + $aSql[] = "TRUNCATE TABLE `token_types`"; + $aSql[] = "INSERT INTO `token_types` (`id`, `name`, `expiration`) VALUES (1, 'password_reset', 3600), (2, 'confirm_email', 0), (3, 'invitation', 0), (4, 'account_unlock', 0), (5, 'account_edit', 3600), (6, 'change_pw', 3600), (7, 'withdraw_funds', 3600)"; + $aSql[] = "INSERT INTO `settings` (`name`, `value`) VALUES ('DB_VERSION', '0.0.4') ON DUPLICATE KEY UPDATE `value` = '0.0.4'"; + + if ($db_version_now == $db_version_old && version_compare($db_version_now, DB_VERSION, '<')) { + execute_db_upgrade($aSql); + } +} +?> diff --git a/upgrade/definitions/0.0.4_to_0.0.5.inc.php b/upgrade/definitions/0.0.4_to_0.0.5.inc.php new file mode 100755 index 00000000..fb5d87fb --- /dev/null +++ b/upgrade/definitions/0.0.4_to_0.0.5.inc.php @@ -0,0 +1,21 @@ +getValue('DB_VERSION'); // Our actual version installed + + // Upgrade specific variables + $aSql[] = "ALTER TABLE `accounts` ADD COLUMN `signup_timestamp` INT( 10 ) NOT NULL DEFAULT '0' AFTER `failed_pins`"; + $aSql[] = "UPDATE `accounts` SET `coin_address` = NULL WHERE `coin_address` = ''"; + $aSql[] = "ALTER TABLE `accounts` ADD UNIQUE INDEX ( `coin_address` )"; + $aSql[] = "INSERT INTO `settings` (`name`, `value`) VALUES ('DB_VERSION', '0.0.5') ON DUPLICATE KEY UPDATE `value` = '0.0.5'"; + + if ($db_version_now == $db_version_old && version_compare($db_version_now, DB_VERSION, '<')) { + execute_db_upgrade($aSql); + } +} +?> diff --git a/upgrade/definitions/0.0.5_to_0.0.6.inc.php b/upgrade/definitions/0.0.5_to_0.0.6.inc.php new file mode 100755 index 00000000..a87658c5 --- /dev/null +++ b/upgrade/definitions/0.0.5_to_0.0.6.inc.php @@ -0,0 +1,19 @@ +getValue('DB_VERSION'); // Our actual version installed + + // Upgrade specific variables + $aSql[] = "CREATE INDEX `account_id_archived` ON `transactions` (`account_id`,`archived`)"; + $aSql[] = "INSERT INTO `settings` (`name`, `value`) VALUES ('DB_VERSION', '0.0.6') ON DUPLICATE KEY UPDATE `value` = '0.0.6'"; + + if ($db_version_now == $db_version_old && version_compare($db_version_now, DB_VERSION, '<')) { + execute_db_upgrade($aSql); + } +} +?> diff --git a/upgrade/definitions/0.0.6_to_0.0.7.inc.php b/upgrade/definitions/0.0.6_to_0.0.7.inc.php new file mode 100755 index 00000000..2b5bebfe --- /dev/null +++ b/upgrade/definitions/0.0.6_to_0.0.7.inc.php @@ -0,0 +1,19 @@ +getValue('DB_VERSION'); // Our actual version installed + + // Upgrade specific variables + $aSql[] = "ALTER TABLE `blocks` CHANGE `share_id` `share_id` BIGINT( 30 ) NULL DEFAULT NULL"; + $aSql[] = "INSERT INTO `settings` (`name`, `value`) VALUES ('DB_VERSION', '0.0.7') ON DUPLICATE KEY UPDATE `value` = '0.0.7'"; + + if ($db_version_now == $db_version_old && version_compare($db_version_now, DB_VERSION, '<')) { + execute_db_upgrade($aSql); + } +} +?> diff --git a/upgrade/run_upgrades.php b/upgrade/run_upgrades.php index dd949c8a..c0d02298 100755 --- a/upgrade/run_upgrades.php +++ b/upgrade/run_upgrades.php @@ -11,7 +11,7 @@ require_once('shared.inc.php'); // Fetch current version $db_version_now = $setting->getValue('DB_VERSION'); -// Helper function +// Helper functions function run_db_upgrade($db_version_now) { // Find upgrades $files = glob(dirname(__FILE__) . "/definitions/${db_version_now}_to_*"); @@ -27,6 +27,21 @@ function run_db_upgrade($db_version_now) { run_db_upgrade($db_version_to); } } +function execute_db_upgrade($aSql) { + global $mysqli; + // Run the upgrade + echo '- Starting database migration to version ' . $db_version_new . PHP_EOL; + foreach ($aSql as $sql) { + echo '- Preparing: ' . $sql . PHP_EOL; + $stmt = $mysqli->prepare($sql); + if ($stmt && $stmt->execute()) { + echo '- success' . PHP_EOL; + } else { + echo '- failed: ' . $mysqli->error . PHP_EOL; + exit(1); + } + } +} // Initial caller run_db_upgrade($db_version_now);