adding future ledger cronjob for transaction confirmations

This commit is contained in:
Sebastian Grewe 2013-05-10 23:29:42 +02:00
parent a995ab640d
commit cbfcff3ba1
3 changed files with 83 additions and 0 deletions

25
cronjobs/ledger.php Normal file
View File

@ -0,0 +1,25 @@
<?php
/*
Copyright:: 2013, Sebastian Grewe
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Include all settings and classes
require_once('shared.inc.php');
// UPDATE ledger AS l INNER JOIN blocks as b ON l.assocBlock = b.height SET l.confirmed = 1 WHERE b.confirmations > 120 AND l.confirmed = 0;
$ledger->confirmTransactions();

View File

@ -9,4 +9,5 @@ require_once(CLASS_DIR . '/user.class.php');
require_once(CLASS_DIR . '/block.class.php');
require_once(CLASS_DIR . '/share.class.php');
require_once(CLASS_DIR . '/statistics.class.php');
require_once(CLASS_DIR . '/ledger.class.php');
require_once(CLASS_DIR . '/settings.class.php');

View File

@ -0,0 +1,57 @@
<?php
// Make sure we are called from index.php
if (!defined('SECURITY'))
die('Hacking attempt');
class Ledger {
private $sError = '';
private $table = 'blocks';
// This defines each block
public $height, $blockhash, $confirmations, $difficulty, $time;
public function __construct($debug, $mysqli, $salt) {
$this->debug = $debug;
$this->mysqli = $mysqli;
$this->debug->append("Instantiated Ledger class", 2);
}
// get and set methods
private function setErrorMessage($msg) {
$this->sError = $msg;
}
public function getError() {
return $this->sError;
}
public function confirmTransactions() {
// Confirm all outstanding transactions
$stmt = $this->mysqli->prepare("UPDATE
ledger AS l
INNER JOIN blocks as b ON l.assocBlock = b.height
SET l.confirmed = 1
WHERE b.confirmations > 120
AND l.confirmed = 0");
if ($this->checkStmt($stmt)) {
if (!$stmt->execute()) {
$this->debug->append("Failed to execute statement: " . $stmt->error);
$stmt->close();
return false;
}
$stmt->close();
return true;
}
return false;
}
private function checkStmt($bState) {
if ($bState ===! true) {
$this->debug->append("Failed to prepare statement: " . $this->mysqli->error);
$this->setErrorMessage('Internal application Error');
return false;
}
return true;
}
}
$ledger = new Ledger($debug, $mysqli, SALT);