Changing upstream share finding function

* Fetch all blocks unaccounted for in ASC oder (low to high height)
* Use lowest height block to find lowest ID upstream accepted share
* Use this share as the finding share for a block
* Set share as last found upstream share for further blocks
 * This only applies if shares are not deleted at all which they should!
This commit is contained in:
Sebastian Grewe 2013-05-31 12:11:56 +02:00
parent 969be407c4
commit 8a1dc20ec8
3 changed files with 38 additions and 31 deletions

View File

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

View File

@ -32,11 +32,7 @@ if (empty($aAllBlocks)) {
$count = 0;
foreach ($aAllBlocks as $iIndex => $aBlock) {
if (!$aBlock['accounted']) {
if ($share->setUpstream(@$aAllBlocks[$iIndex - 1]['time'])) {
$share->setLastUpstreamId();
}
if ($share->setUpstream($aBlock['time'])) {
if ($share->setUpstream($share->getLastUpstreamId())) {
$iCurrentUpstreamId = $share->getUpstreamId();
} else {
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']))
$strStatus = "Donation Failed";
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
$share->moveArchive($share->getLastUpstreamId(), $iCurrentUpstreamId, $aBlock['id']);
$block->setAccounted($aBlock['id']);
if ($config['archive_shares']) $share->moveArchive($iCurrentUpstreamId, $aBlock['id'], $share->getLastUpstreamId());
// 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
* @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)
SELECT id, username, our_result, upstream_result, ?, time
FROM $this->table
WHERE id BETWEEN ? AND ?");
$delete_stmt = $this->mysqli->prepare("DELETE FROM $this->table WHERE id BETWEEN ? AND ?");
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();
if ($this->checkStmt($archive_stmt) && $archive_stmt->bind_param('iii', $block_id, $previous_upstream, $current_upstream) && $archive_stmt->execute()) {
$archive_stmt->close();
return true;
}
// Catchall
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
**/
@ -144,7 +146,7 @@ class Share {
$this->iLastUpstreamId = @$this->oUpstream->id ? $this->oUpstream->id : 0;
}
public function getLastUpstreamId() {
return @$this->iLastUpstreamId;
return @$this->iLastUpstreamId ? @$this->iLastUpstreamId : 0;
}
public function getUpstreamFinder() {
return @$this->oUpstream->account;
@ -154,28 +156,25 @@ class Share {
}
/**
* Find upstream accepted share that should be valid for a specific block
* We allow for +/- 5 seconds here to ensure we find a share
* @param time timestamp Time of when the block was found
* Assumptions:
* * 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
**/
public function setUpstream($time='') {
$stmt = $this->mysqli->prepare("SELECT
public function setUpstream($last=0) {
$stmt = $this->mysqli->prepare("
SELECT
SUBSTRING_INDEX( `username` , '.', 1 ) AS account, id
FROM $this->table
WHERE upstream_result = 'Y'
AND UNIX_TIMESTAMP(time) BETWEEN (? - 5) AND (? + 5)
AND id > ?
ORDER BY id ASC LIMIT 1");
if ($this->checkStmt($stmt)) {
$stmt->bind_param('ii', $time, $time);
$stmt->execute();
if (! $result = $stmt->get_result()) {
$this->setErrorMessage("No result returned from query");
$stmt->close();
}
$stmt->close();
if ($this->checkStmt($stmt) && $stmt->bind_param('i', $last) && $stmt->execute() && $result = $stmt->get_result()) {
$this->oUpstream = $result->fetch_object();
return true;
}
// Catchall
return false;
}