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
|
// Nothing to do so bail out
|
||||||
if (empty($aTransactions['transactions'])) {
|
if (empty($aTransactions['transactions'])) {
|
||||||
verbose("No new transactions since last block\n");
|
verbose("No new transactions since last block\n");
|
||||||
exit(0);
|
} else {
|
||||||
}
|
|
||||||
|
|
||||||
// Table header
|
// Table header
|
||||||
verbose("Blockhash\t\tHeight\tAmount\tConfirmations\tDiff\t\tTime\t\t\tStatus\n");
|
verbose("Blockhash\t\tHeight\tAmount\tConfirmations\tDiff\t\tTime\t\t\tStatus\n");
|
||||||
|
|
||||||
foreach ($aTransactions['transactions'] as $iIndex => $aData) {
|
// Let us add those blocks as unaccounted
|
||||||
if ( $aData['category'] == 'generate' || $aData['category'] == 'immature' ) {
|
foreach ($aTransactions['transactions'] as $iIndex => $aData) {
|
||||||
$aBlockInfo = $bitcoin->query('getblock', $aData['blockhash']);
|
if ( $aData['category'] == 'generate' || $aData['category'] == 'immature' ) {
|
||||||
$aData['height'] = $aBlockInfo['height'];
|
$aBlockInfo = $bitcoin->query('getblock', $aData['blockhash']);
|
||||||
$aData['difficulty'] = $aBlockInfo['difficulty'];
|
$aData['height'] = $aBlockInfo['height'];
|
||||||
verbose(substr($aData['blockhash'], 0, 15) . "...\t" .
|
$aData['difficulty'] = $aBlockInfo['difficulty'];
|
||||||
$aData['height'] . "\t" .
|
verbose(substr($aData['blockhash'], 0, 15) . "...\t" .
|
||||||
$aData['amount'] . "\t" .
|
$aData['height'] . "\t" .
|
||||||
$aData['confirmations'] . "\t\t" .
|
$aData['amount'] . "\t" .
|
||||||
$aData['difficulty'] . "\t" .
|
$aData['confirmations'] . "\t\t" .
|
||||||
strftime("%Y-%m-%d %H:%M:%S", $aData['time']) . "\t");
|
$aData['difficulty'] . "\t" .
|
||||||
if ( $block->addBlock($aData) ) {
|
strftime("%Y-%m-%d %H:%M:%S", $aData['time']) . "\t");
|
||||||
verbose("Added\n");
|
if ( $block->addBlock($aData) ) {
|
||||||
} else {
|
verbose("Added\n");
|
||||||
verbose("Failed" . "\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;
|
$count = 0;
|
||||||
foreach ($aAllBlocks as $iIndex => $aBlock) {
|
foreach ($aAllBlocks as $iIndex => $aBlock) {
|
||||||
if (!$aBlock['accounted']) {
|
if (!$aBlock['accounted']) {
|
||||||
if ($share->setUpstream($share->getLastUpstreamId())) {
|
$iPreviousShareId = $aAllBlocks[$iIndex - 1]['share_id'] ? $aAllBlocks[$iIndex - 1]['share_id'] : 0;
|
||||||
$iCurrentUpstreamId = $share->getUpstreamId();
|
$iCurrentUpstreamId = $aBlock['share_id'];
|
||||||
} else {
|
$aAccountShares = $share->getSharesForAccounts($iPreviousShareId, $aBlock['share_id']);
|
||||||
verbose("Unable to fetch blocks upstream share\n");
|
$iRoundShares = $share->getRoundShares($iPreviousShareId, $aBlock['share_id']);
|
||||||
verbose($share->getError() . "\n");
|
|
||||||
continue;
|
// Table header for block details
|
||||||
}
|
|
||||||
$aAccountShares = $share->getSharesForAccounts($share->getLastUpstreamId(), $iCurrentUpstreamId);
|
|
||||||
$iRoundShares = $share->getRoundShares($share->getLastUpstreamId(), $iCurrentUpstreamId);
|
|
||||||
verbose("ID\tHeight\tTime\t\tShares\tFinder\t\tShare ID\tPrev Share\t\tStatus\n");
|
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)) {
|
if (empty($aAccountShares)) {
|
||||||
verbose("\nNo shares found for this block\n\n");
|
verbose("\nNo shares found for this block\n\n");
|
||||||
sleep(2);
|
sleep(2);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$strStatus = "OK";
|
$strStatus = "OK";
|
||||||
if (!$block->setFinder($aBlock['id'], $user->getUserId($share->getUpstreamFinder())))
|
// Store share information for this block
|
||||||
$strStatus = "Finder Failed";
|
|
||||||
if (!$block->setShares($aBlock['id'], $iRoundShares))
|
if (!$block->setShares($aBlock['id'], $iRoundShares))
|
||||||
$strStatus = "Shares Failed";
|
$strStatus = "Shares Failed";
|
||||||
verbose("\t\t$strStatus\n\n");
|
verbose("\t\t$strStatus\n\n");
|
||||||
|
|
||||||
|
// Table header for account shares
|
||||||
verbose("ID\tUsername\tValid\tInvalid\tPercentage\tPayout\t\tDonation\tFee\t\tStatus\n");
|
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) {
|
foreach ($aAccountShares as $key => $aData) {
|
||||||
// Payout based on shares, PPS system
|
// Payout based on shares, PPS system
|
||||||
$aData['percentage'] = number_format(round(( 100 / $iRoundShares ) * $aData['valid'], 8), 8);
|
$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']))
|
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move counted shares to archive before this blockhash upstream share
|
// 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
|
// Delete all accounted shares
|
||||||
if (!$share->deleteAccountedShares($iCurrentUpstreamId, $share->getLastUpstreamId())) {
|
if (!$share->deleteAccountedShares($iCurrentUpstreamId, $iPreviousShareId)) {
|
||||||
verbose("\nERROR : Failed to delete accounted shares from " . $share->getLastUpstreamId() . " to $iCurrentUpstreamId, aborting!\n");
|
verbose("\nERROR : Failed to delete accounted shares from $iPreviousShareId to $iCurrentUpstreamId, aborting!\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
// Mark this block as accounted for
|
// Mark this block as accounted for
|
||||||
@ -110,6 +110,5 @@ foreach ($aAllBlocks as $iIndex => $aBlock) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
verbose("------------------------------------------------------------------------\n\n");
|
verbose("------------------------------------------------------------------------\n\n");
|
||||||
$share->setLastUpstreamId();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -130,6 +130,16 @@ class Block {
|
|||||||
return false;
|
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
|
* Update a single column within a single row
|
||||||
* @param block_id int Block ID to update
|
* @param block_id int Block ID to update
|
||||||
@ -162,6 +172,16 @@ class Block {
|
|||||||
return $this->updateSingle($block_id, 'account_id', $account_id);
|
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
|
* Set counted shares for a block
|
||||||
* @param block_id int Block ID
|
* @param block_id int Block ID
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user