Merge pull request #82 from TheSerapher/upstream-share-by-order

Changing upstream share finding function
This commit is contained in:
Sebastian Grewe 2013-05-31 03:13:52 -07:00
commit 4917376908
3 changed files with 38 additions and 31 deletions

View File

@ -23,7 +23,8 @@ limitations under the License.
require_once('shared.inc.php'); require_once('shared.inc.php');
// Fetch our last block found from the DB as a starting point // Fetch our last block found from the DB as a starting point
$strLastBlockHash = @$block->getLast()->blockhash; $aLastBlock = @$block->getLast();
$strLastBlockHash = $aLastBlock['blockhash'];
if (!$strLastBlockHash) { if (!$strLastBlockHash) {
$strLastBlockHash = ''; $strLastBlockHash = '';
} }

View File

@ -32,11 +32,7 @@ if (empty($aAllBlocks)) {
$count = 0; $count = 0;
foreach ($aAllBlocks as $iIndex => $aBlock) { foreach ($aAllBlocks as $iIndex => $aBlock) {
if (!$aBlock['accounted']) { if (!$aBlock['accounted']) {
if ($share->setUpstream(@$aAllBlocks[$iIndex - 1]['time'])) { if ($share->setUpstream($share->getLastUpstreamId())) {
$share->setLastUpstreamId();
}
if ($share->setUpstream($aBlock['time'])) {
$iCurrentUpstreamId = $share->getUpstreamId(); $iCurrentUpstreamId = $share->getUpstreamId();
} else { } else {
verbose("Unable to fetch blocks upstream share\n"); verbose("Unable to fetch blocks upstream share\n");
@ -98,11 +94,22 @@ foreach ($aAllBlocks as $iIndex => $aBlock) {
if (!$transaction->addTransaction($aData['id'], $aData['donation'], 'Donation', $aBlock['id'])) if (!$transaction->addTransaction($aData['id'], $aData['donation'], 'Donation', $aBlock['id']))
$strStatus = "Donation Failed"; $strStatus = "Donation Failed";
verbose("\t$strStatus\n"); verbose("\t$strStatus\n");
// Set this blocks share ID as the last found upstream ID
} }
verbose("------------------------------------------------------------------------\n\n");
// Move counted shares to archive before this blockhash upstream share // Move counted shares to archive before this blockhash upstream share
$share->moveArchive($share->getLastUpstreamId(), $iCurrentUpstreamId, $aBlock['id']); if ($config['archive_shares']) $share->moveArchive($iCurrentUpstreamId, $aBlock['id'], $share->getLastUpstreamId());
$block->setAccounted($aBlock['id']); // Delete all accounted shares
if (!$share->deleteAccountedShares($iCurrentUpstreamId, $share->getLastUpstreamId())) {
verbose("\nERROR : Failed to delete accounted shares from " . $share->getLastUpstreamId() . " to $iCurrentUpstreamId, aborting!\n");
exit(1);
}
// Mark this block as accounted for
if (!$block->setAccounted($aBlock['id'])) {
verbose("\nERROR : Failed to mark block as accounted! Aborting!\n");
}
verbose("------------------------------------------------------------------------\n\n");
$share->setLastUpstreamId();
} }
} }

View File

@ -119,24 +119,26 @@ class Share {
* @param block_id int Block ID to assign shares to a specific block * @param block_id int Block ID to assign shares to a specific block
* @return bool * @return bool
**/ **/
public function moveArchive($previous_upstream=0, $current_upstream,$block_id) { public function moveArchive($current_upstream, $block_id, $previous_upstream=0) {
$archive_stmt = $this->mysqli->prepare("INSERT INTO $this->tableArchive (share_id, username, our_result, upstream_result, block_id, time) $archive_stmt = $this->mysqli->prepare("INSERT INTO $this->tableArchive (share_id, username, our_result, upstream_result, block_id, time)
SELECT id, username, our_result, upstream_result, ?, time SELECT id, username, our_result, upstream_result, ?, time
FROM $this->table FROM $this->table
WHERE id BETWEEN ? AND ?"); WHERE id BETWEEN ? AND ?");
$delete_stmt = $this->mysqli->prepare("DELETE FROM $this->table WHERE id BETWEEN ? AND ?"); if ($this->checkStmt($archive_stmt) && $archive_stmt->bind_param('iii', $block_id, $previous_upstream, $current_upstream) && $archive_stmt->execute()) {
if ($this->checkStmt($archive_stmt) && $this->checkStmt($delete_stmt)) {
$archive_stmt->bind_param('iii', $block_id, $previous_upstream, $current_upstream);
$archive_stmt->execute();
$delete_stmt->bind_param('ii', $previous_upstream, $current_upstream);
$delete_stmt->execute();
$delete_stmt->close();
$archive_stmt->close(); $archive_stmt->close();
return true; return true;
} }
// Catchall
return false; return false;
} }
public function deleteAccountedShares($current_upstream, $previous_upstream=0) {
$stmt = $this->mysqli->prepare("DELETE FROM $this->table WHERE id BETWEEN ? AND ?");
if ($this->checkStmt($stmt) && $stmt->bind_param('ii', $previous_upstream, $current_upstream) && $stmt->execute())
return true;
// Catchall
return false;
}
/** /**
* Set/get last found share accepted by upstream: id and accounts * Set/get last found share accepted by upstream: id and accounts
**/ **/
@ -144,7 +146,7 @@ class Share {
$this->iLastUpstreamId = @$this->oUpstream->id ? $this->oUpstream->id : 0; $this->iLastUpstreamId = @$this->oUpstream->id ? $this->oUpstream->id : 0;
} }
public function getLastUpstreamId() { public function getLastUpstreamId() {
return @$this->iLastUpstreamId; return @$this->iLastUpstreamId ? @$this->iLastUpstreamId : 0;
} }
public function getUpstreamFinder() { public function getUpstreamFinder() {
return @$this->oUpstream->account; return @$this->oUpstream->account;
@ -154,28 +156,25 @@ class Share {
} }
/** /**
* Find upstream accepted share that should be valid for a specific block * Find upstream accepted share that should be valid for a specific block
* We allow for +/- 5 seconds here to ensure we find a share * Assumptions:
* @param time timestamp Time of when the block was found * * Shares are matching blocks in ASC order
* * We can skip all upstream shares of previously found ones used in a block
* @param last int Skips all shares up to last to find new share
* @return bool * @return bool
**/ **/
public function setUpstream($time='') { public function setUpstream($last=0) {
$stmt = $this->mysqli->prepare("SELECT $stmt = $this->mysqli->prepare("
SELECT
SUBSTRING_INDEX( `username` , '.', 1 ) AS account, id SUBSTRING_INDEX( `username` , '.', 1 ) AS account, id
FROM $this->table FROM $this->table
WHERE upstream_result = 'Y' WHERE upstream_result = 'Y'
AND UNIX_TIMESTAMP(time) BETWEEN (? - 5) AND (? + 5) AND id > ?
ORDER BY id ASC LIMIT 1"); ORDER BY id ASC LIMIT 1");
if ($this->checkStmt($stmt)) { if ($this->checkStmt($stmt) && $stmt->bind_param('i', $last) && $stmt->execute() && $result = $stmt->get_result()) {
$stmt->bind_param('ii', $time, $time);
$stmt->execute();
if (! $result = $stmt->get_result()) {
$this->setErrorMessage("No result returned from query");
$stmt->close();
}
$stmt->close();
$this->oUpstream = $result->fetch_object(); $this->oUpstream = $result->fetch_object();
return true; return true;
} }
// Catchall
return false; return false;
} }