Merge pull request #83 from TheSerapher/refactor-backend
Changed backend logics for block finding and payout
This commit is contained in:
commit
ac2a9b30c5
@ -40,28 +40,63 @@ if ( $bitcoin->can_connect() === true ){
|
||||
// Nothing to do so bail out
|
||||
if (empty($aTransactions['transactions'])) {
|
||||
verbose("No new transactions since last block\n");
|
||||
exit(0);
|
||||
}
|
||||
} else {
|
||||
|
||||
// Table header
|
||||
verbose("Blockhash\t\tHeight\tAmount\tConfirmations\tDiff\t\tTime\t\t\tStatus\n");
|
||||
// Table header
|
||||
verbose("Blockhash\t\tHeight\tAmount\tConfirmations\tDiff\t\tTime\t\t\tStatus\n");
|
||||
|
||||
foreach ($aTransactions['transactions'] as $iIndex => $aData) {
|
||||
if ( $aData['category'] == 'generate' || $aData['category'] == 'immature' ) {
|
||||
$aBlockInfo = $bitcoin->query('getblock', $aData['blockhash']);
|
||||
$aData['height'] = $aBlockInfo['height'];
|
||||
$aData['difficulty'] = $aBlockInfo['difficulty'];
|
||||
verbose(substr($aData['blockhash'], 0, 15) . "...\t" .
|
||||
$aData['height'] . "\t" .
|
||||
$aData['amount'] . "\t" .
|
||||
$aData['confirmations'] . "\t\t" .
|
||||
$aData['difficulty'] . "\t" .
|
||||
strftime("%Y-%m-%d %H:%M:%S", $aData['time']) . "\t");
|
||||
if ( $block->addBlock($aData) ) {
|
||||
verbose("Added\n");
|
||||
} else {
|
||||
verbose("Failed" . "\n");
|
||||
// Let us add those blocks as unaccounted
|
||||
foreach ($aTransactions['transactions'] as $iIndex => $aData) {
|
||||
if ( $aData['category'] == 'generate' || $aData['category'] == 'immature' ) {
|
||||
$aBlockInfo = $bitcoin->query('getblock', $aData['blockhash']);
|
||||
$aData['height'] = $aBlockInfo['height'];
|
||||
$aData['difficulty'] = $aBlockInfo['difficulty'];
|
||||
verbose(substr($aData['blockhash'], 0, 15) . "...\t" .
|
||||
$aData['height'] . "\t" .
|
||||
$aData['amount'] . "\t" .
|
||||
$aData['confirmations'] . "\t\t" .
|
||||
$aData['difficulty'] . "\t" .
|
||||
strftime("%Y-%m-%d %H:%M:%S", $aData['time']) . "\t");
|
||||
if ( $block->addBlock($aData) ) {
|
||||
verbose("Added\n");
|
||||
} else {
|
||||
verbose("Failed" . "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Now with our blocks added we can scan for their upstream shares
|
||||
$aAllBlocks = $block->getAllUnaccounted('ASC');
|
||||
|
||||
// Loop through our unaccounted blocks
|
||||
verbose("Block ID\tBlock Height\tShare ID\tFinder\t\t\tStatus\n");
|
||||
foreach ($aAllBlocks as $iIndex => $aBlock) {
|
||||
if (empty($aBlock['share_id'])) {
|
||||
// Fetch this blocks upstream ID
|
||||
if ($share->setUpstream($block->getLastUpstreamId())) {
|
||||
$iCurrentUpstreamId = $share->getUpstreamId();
|
||||
$iAccountId = $user->getUserId($share->getUpstreamFinder());
|
||||
} else {
|
||||
verbose("Unable to fetch blocks upstream share\n");
|
||||
verbose($share->getError() . "\n");
|
||||
continue;
|
||||
}
|
||||
// Store new information
|
||||
$strStatus = "OK";
|
||||
if (!$block->setShareId($aBlock['id'], $iCurrentUpstreamId))
|
||||
$strStatus = "Share ID Failed";
|
||||
if (!$block->setFinder($aBlock['id'], $iAccountId))
|
||||
$strStatus = "Finder Failed";
|
||||
verbose(
|
||||
$aBlock['id'] . "\t\t"
|
||||
. $aBlock['height'] . "\t\t"
|
||||
. $iCurrentUpstreamId . "\t\t"
|
||||
. "[$iAccountId] " . $user->getUserName($iAccountId) . "\t\t"
|
||||
. $strStatus
|
||||
. "\n"
|
||||
);
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
|
||||
@ -32,29 +32,30 @@ if (empty($aAllBlocks)) {
|
||||
$count = 0;
|
||||
foreach ($aAllBlocks as $iIndex => $aBlock) {
|
||||
if (!$aBlock['accounted']) {
|
||||
if ($share->setUpstream($share->getLastUpstreamId())) {
|
||||
$iCurrentUpstreamId = $share->getUpstreamId();
|
||||
} else {
|
||||
verbose("Unable to fetch blocks upstream share\n");
|
||||
verbose($share->getError() . "\n");
|
||||
continue;
|
||||
}
|
||||
$aAccountShares = $share->getSharesForAccounts($share->getLastUpstreamId(), $iCurrentUpstreamId);
|
||||
$iRoundShares = $share->getRoundShares($share->getLastUpstreamId(), $iCurrentUpstreamId);
|
||||
$iPreviousShareId = $aAllBlocks[$iIndex - 1]['share_id'] ? $aAllBlocks[$iIndex - 1]['share_id'] : 0;
|
||||
$iCurrentUpstreamId = $aBlock['share_id'];
|
||||
$aAccountShares = $share->getSharesForAccounts($iPreviousShareId, $aBlock['share_id']);
|
||||
$iRoundShares = $share->getRoundShares($iPreviousShareId, $aBlock['share_id']);
|
||||
|
||||
// Table header for block details
|
||||
verbose("ID\tHeight\tTime\t\tShares\tFinder\t\tShare ID\tPrev Share\t\tStatus\n");
|
||||
verbose($aBlock['id'] . "\t" . $aBlock['height'] . "\t" . $aBlock['time'] . "\t" . $iRoundShares . "\t" . $share->getUpstreamFinder() . "\t" . $share->getUpstreamId() . "\t\t" . $share->getLastUpstreamId());
|
||||
verbose($aBlock['id'] . "\t" . $aBlock['height'] . "\t" . $aBlock['time'] . "\t" . $iRoundShares . "\t" . $user->getUserName($aBlock['account_id']) . "\t" . $iCurrentUpstreamId . "\t\t" . $iPreviousShareId);
|
||||
|
||||
if (empty($aAccountShares)) {
|
||||
verbose("\nNo shares found for this block\n\n");
|
||||
sleep(2);
|
||||
continue;
|
||||
}
|
||||
$strStatus = "OK";
|
||||
if (!$block->setFinder($aBlock['id'], $user->getUserId($share->getUpstreamFinder())))
|
||||
$strStatus = "Finder Failed";
|
||||
// Store share information for this block
|
||||
if (!$block->setShares($aBlock['id'], $iRoundShares))
|
||||
$strStatus = "Shares Failed";
|
||||
verbose("\t\t$strStatus\n\n");
|
||||
|
||||
// Table header for account shares
|
||||
verbose("ID\tUsername\tValid\tInvalid\tPercentage\tPayout\t\tDonation\tFee\t\tStatus\n");
|
||||
|
||||
// Loop through all accounts that have found shares for this round
|
||||
foreach ($aAccountShares as $key => $aData) {
|
||||
// Payout based on shares, PPS system
|
||||
$aData['percentage'] = number_format(round(( 100 / $iRoundShares ) * $aData['valid'], 8), 8);
|
||||
@ -94,14 +95,13 @@ 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
|
||||
}
|
||||
|
||||
// Move counted shares to archive before this blockhash upstream share
|
||||
if ($config['archive_shares']) $share->moveArchive($iCurrentUpstreamId, $aBlock['id'], $share->getLastUpstreamId());
|
||||
if ($config['archive_shares']) $share->moveArchive($iCurrentUpstreamId, $aBlock['id'], $iPreviousShareId);
|
||||
// Delete all accounted shares
|
||||
if (!$share->deleteAccountedShares($iCurrentUpstreamId, $share->getLastUpstreamId())) {
|
||||
verbose("\nERROR : Failed to delete accounted shares from " . $share->getLastUpstreamId() . " to $iCurrentUpstreamId, aborting!\n");
|
||||
if (!$share->deleteAccountedShares($iCurrentUpstreamId, $iPreviousShareId)) {
|
||||
verbose("\nERROR : Failed to delete accounted shares from $iPreviousShareId to $iCurrentUpstreamId, aborting!\n");
|
||||
exit(1);
|
||||
}
|
||||
// Mark this block as accounted for
|
||||
@ -110,6 +110,5 @@ foreach ($aAllBlocks as $iIndex => $aBlock) {
|
||||
}
|
||||
|
||||
verbose("------------------------------------------------------------------------\n\n");
|
||||
$share->setLastUpstreamId();
|
||||
}
|
||||
}
|
||||
@ -130,6 +130,16 @@ class Block {
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getLastUpstreamId() {
|
||||
$stmt = $this->mysqli->prepare("
|
||||
SELECT MAX(share_id) AS share_id FROM $this->table
|
||||
");
|
||||
if ($this->checkStmt($stmt) && $stmt->execute() && $stmt->bind_result($share_id) && $stmt->fetch())
|
||||
return $share_id ? $share_id : 0;
|
||||
// Catchall
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single column within a single row
|
||||
* @param block_id int Block ID to update
|
||||
@ -162,6 +172,16 @@ class Block {
|
||||
return $this->updateSingle($block_id, 'account_id', $account_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set finding share for a block
|
||||
* @param block_id int Block ID
|
||||
* @param share_id int Upstream valid share ID
|
||||
* @return bool
|
||||
**/
|
||||
public function setShareId($block_id, $share_id) {
|
||||
return $this->updateSingle($block_id, 'share_id', $share_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set counted shares for a block
|
||||
* @param block_id int Block ID
|
||||
|
||||
Loading…
Reference in New Issue
Block a user