From 2f4e95f22dcf89ab1147bd008b39eb37589bc1e8 Mon Sep 17 00:00:00 2001 From: pxgamer Date: Tue, 29 May 2018 15:50:30 +0100 Subject: [PATCH] Update files to use class constructor parentheses --- api.php | 8 +- include/account.inc.php | 2 +- include/block.inc.php | 20 ++--- include/config.inc.php | 4 +- include/transaction.inc.php | 158 +++++++++++++++++++----------------- peer.php | 33 ++++++-- propagate.php | 2 +- sanity.php | 104 ++++++++++++++++++------ 8 files changed, 201 insertions(+), 130 deletions(-) diff --git a/api.php b/api.php index 7dc63c6..921d314 100755 --- a/api.php +++ b/api.php @@ -276,7 +276,7 @@ if ($q == "getAddress") { * @apiSuccess {string} private_key Private key */ - $acc = new Account; + $acc = new Account(); $res = $acc->generate_account(); api_echo($res); } elseif ($q == "currentBlock") { @@ -391,10 +391,10 @@ if ($q == "getAddress") { api_err("Hard fork in progress. Please retry the transaction later!"); //10800 } - $acc = new Account; - $block = new Block; + $acc = new Account(); + $block = new Block(); - $trx = new Transaction; + $trx = new Transaction(); $dst = san($data['dst']); diff --git a/include/account.inc.php b/include/account.inc.php index d777b0e..ee90df8 100755 --- a/include/account.inc.php +++ b/include/account.inc.php @@ -145,7 +145,7 @@ class Account public function get_transactions($id, $limit = 100) { global $db; - $block = new Block; + $block = new Block(); $current = $block->current(); $public_key = $this->public_key($id); $limit = intval($limit); diff --git a/include/block.inc.php b/include/block.inc.php index 31a0622..57b87e2 100755 --- a/include/block.inc.php +++ b/include/block.inc.php @@ -5,8 +5,8 @@ class Block public function add($height, $public_key, $nonce, $data, $date, $signature, $difficulty, $reward_signature, $argon) { global $db; - $acc = new Account; - $trx = new Transaction; + $acc = new Account(); + $trx = new Transaction(); $generator = $acc->get_address($public_key); @@ -232,7 +232,7 @@ class Block _log("Invalid block argon - $data[argon]"); return false; } - $acc = new Account; + $acc = new Account(); // generator's public key must be valid if (!$acc->valid_key($data['public_key'])) { @@ -274,12 +274,12 @@ class Block } // get the mempool transactions - $txn = new Transaction; + $txn = new Transaction(); $data = $txn->mempool($this->max_transactions()); $difficulty = $this->difficulty(); - $acc = new Account; + $acc = new Account(); $generator = $acc->get_address($public_key); // always sort the transactions in the same way @@ -397,8 +397,8 @@ class Block if ($data === false) { return false; } - $acc = new Account; - $trx = new Transaction; + $acc = new Account(); + $trx = new Transaction(); // no transactions means all are valid if (count($data) == 0) { return true; @@ -503,7 +503,7 @@ class Block $height = 2; } global $db; - $trx = new Transaction; + $trx = new Transaction(); $r = $db->run("SELECT * FROM blocks WHERE height>=:height ORDER by height DESC", [":height" => $height]); @@ -538,7 +538,7 @@ class Block { global $db; - $trx = new Transaction; + $trx = new Transaction(); $x = $db->row("SELECT * FROM blocks WHERE id=:id", [":id" => $id]); @@ -599,7 +599,7 @@ class Block } global $db; - $trx = new Transaction; + $trx = new Transaction(); if (!empty($height)) { $block = $db->row("SELECT * FROM blocks WHERE height=:height", [":height" => $height]); } else { diff --git a/include/config.inc.php b/include/config.inc.php index 5588957..ce26506 100755 --- a/include/config.inc.php +++ b/include/config.inc.php @@ -4,7 +4,7 @@ $_config['db_connect']="mysql:host=localhost;dbname=ENTER-DB-NAME"; $_config['db_user']="ENTER-DB-USER"; $_config['db_pass']="ENTER-DB-PASS"; -// Maximum number of connected peers +// Maximum number of connected peers $_config['max_peers']=30; // Testnet, used for development $_config['testnet']=false; @@ -36,5 +36,3 @@ $_config['sanity_rebroadcast_locals']=true; $_config['enable_logging']=false; // log file, should not be publicly viewable $_config['log_file']="/var/log/aro.log"; - -?> diff --git a/include/transaction.inc.php b/include/transaction.inc.php index 9f3bf02..1663307 100755 --- a/include/transaction.inc.php +++ b/include/transaction.inc.php @@ -6,7 +6,7 @@ class Transaction public function reverse($block) { global $db; - $acc = new Account; + $acc = new Account(); $r = $db->run("SELECT * FROM transactions WHERE block=:block", [":block" => $block]); foreach ($r as $x) { if (empty($x['src'])) { @@ -40,7 +40,7 @@ class Transaction public function clean_mempool() { global $db; - $block = new Block; + $block = new Block(); $current = $block->current(); $height = $current['height']; $limit = $height - 1000; @@ -51,7 +51,7 @@ class Transaction public function mempool($max) { global $db; - $block = new Block; + $block = new Block(); $current = $block->current(); $height = $current['height'] + 1; // only get the transactions that are not locked with a future height @@ -64,15 +64,16 @@ class Transaction $i = 0; $balance = []; foreach ($r as $x) { - $trans = ["id" => $x['id'], - "dst" => $x['dst'], - "val" => $x['val'], - "fee" => $x['fee'], - "signature" => $x['signature'], - "message" => $x['message'], - "version" => $x['version'], - "date" => $x['date'], - "public_key" => $x['public_key'], + $trans = [ + "id" => $x['id'], + "dst" => $x['dst'], + "val" => $x['val'], + "fee" => $x['fee'], + "signature" => $x['signature'], + "message" => $x['message'], + "version" => $x['version'], + "date" => $x['date'], + "public_key" => $x['public_key'], ]; if ($i >= $max) { @@ -122,22 +123,23 @@ class Transaction public function add_mempool($x, $peer = "") { global $db; - $block = new Block; + $block = new Block(); $current = $block->current(); $height = $current['height']; $x['id'] = san($x['id']); - $bind = [":peer" => $peer, - ":id" => $x['id'], - "public_key" => $x['public_key'], - ":height" => $height, - ":src" => $x['src'], - ":dst" => $x['dst'], - ":val" => $x['val'], - ":fee" => $x['fee'], - ":signature" => $x['signature'], - ":version" => $x['version'], - ":date" => $x['date'], - ":message" => $x['message'], + $bind = [ + ":peer" => $peer, + ":id" => $x['id'], + "public_key" => $x['public_key'], + ":height" => $height, + ":src" => $x['src'], + ":dst" => $x['dst'], + ":val" => $x['val'], + ":fee" => $x['fee'], + ":signature" => $x['signature'], + ":version" => $x['version'], + ":date" => $x['date'], + ":message" => $x['message'], ]; $db->run( "INSERT into mempool SET peer=:peer, id=:id, public_key=:public_key, height=:height, src=:src, dst=:dst, val=:val, fee=:fee, signature=:signature, version=:version, message=:message, `date`=:date", @@ -150,21 +152,22 @@ class Transaction public function add($block, $height, $x) { global $db; - $acc = new Account; + $acc = new Account(); $acc->add($x['public_key'], $block); $acc->add_id($x['dst'], $block); $x['id'] = san($x['id']); - $bind = [":id" => $x['id'], - ":public_key" => $x['public_key'], - ":height" => $height, - ":block" => $block, - ":dst" => $x['dst'], - ":val" => $x['val'], - ":fee" => $x['fee'], - ":signature" => $x['signature'], - ":version" => $x['version'], - ":date" => $x['date'], - ":message" => $x['message'], + $bind = [ + ":id" => $x['id'], + ":public_key" => $x['public_key'], + ":height" => $height, + ":block" => $block, + ":dst" => $x['dst'], + ":val" => $x['val'], + ":fee" => $x['fee'], + ":signature" => $x['signature'], + ":version" => $x['version'], + ":date" => $x['date'], + ":message" => $x['message'], ]; $res = $db->run( "INSERT into transactions SET id=:id, public_key=:public_key, block=:block, height=:height, dst=:dst, val=:val, fee=:fee, signature=:signature, version=:version, message=:message, `date`=:date", @@ -198,11 +201,11 @@ class Transaction { // if no specific block, use current if ($height === 0) { - $block = new Block; + $block = new Block(); $current = $block->current(); $height = $current['height']; } - $acc = new Account; + $acc = new Account(); $info = $x['val']."-".$x['fee']."-".$x['dst']."-".$x['message']."-".$x['version']."-".$x['public_key']."-".$x['date']; // the value must be >=0 @@ -309,8 +312,8 @@ class Transaction public function get_transaction($id) { global $db; - $acc = new Account; - $block = new Block; + $acc = new Account(); + $block = new Block(); $current = $block->current(); $x = $db->row("SELECT * FROM transactions WHERE id=:id", [":id" => $id]); @@ -318,17 +321,18 @@ class Transaction if (!$x) { return false; } - $trans = ["block" => $x['block'], - "height" => $x['height'], - "id" => $x['id'], - "dst" => $x['dst'], - "val" => $x['val'], - "fee" => $x['fee'], - "signature" => $x['signature'], - "message" => $x['message'], - "version" => $x['version'], - "date" => $x['date'], - "public_key" => $x['public_key'], + $trans = [ + "block" => $x['block'], + "height" => $x['height'], + "id" => $x['id'], + "dst" => $x['dst'], + "val" => $x['val'], + "fee" => $x['fee'], + "signature" => $x['signature'], + "message" => $x['message'], + "version" => $x['version'], + "date" => $x['date'], + "public_key" => $x['public_key'], ]; $trans['src'] = $acc->get_address($x['public_key']); $trans['confirmations'] = $current['height'] - $x['height']; @@ -352,9 +356,9 @@ class Transaction public function get_transactions($height = "", $id = "") { global $db; - $block = new Block; + $block = new Block(); $current = $block->current(); - $acc = new Account; + $acc = new Account(); $height = san($height); $id = san($id); if (empty($id) && empty($height)) { @@ -367,17 +371,18 @@ class Transaction } $res = []; foreach ($r as $x) { - $trans = ["block" => $x['block'], - "height" => $x['height'], - "id" => $x['id'], - "dst" => $x['dst'], - "val" => $x['val'], - "fee" => $x['fee'], - "signature" => $x['signature'], - "message" => $x['message'], - "version" => $x['version'], - "date" => $x['date'], - "public_key" => $x['public_key'], + $trans = [ + "block" => $x['block'], + "height" => $x['height'], + "id" => $x['id'], + "dst" => $x['dst'], + "val" => $x['val'], + "fee" => $x['fee'], + "signature" => $x['signature'], + "message" => $x['message'], + "version" => $x['version'], + "date" => $x['date'], + "public_key" => $x['public_key'], ]; $trans['src'] = $acc->get_address($x['public_key']); $trans['confirmations'] = $current['height'] - $x['height']; @@ -407,17 +412,18 @@ class Transaction if (!$x) { return false; } - $trans = ["block" => $x['block'], - "height" => $x['height'], - "id" => $x['id'], - "dst" => $x['dst'], - "val" => $x['val'], - "fee" => $x['fee'], - "signature" => $x['signature'], - "message" => $x['message'], - "version" => $x['version'], - "date" => $x['date'], - "public_key" => $x['public_key'], + $trans = [ + "block" => $x['block'], + "height" => $x['height'], + "id" => $x['id'], + "dst" => $x['dst'], + "val" => $x['val'], + "fee" => $x['fee'], + "signature" => $x['signature'], + "message" => $x['message'], + "version" => $x['version'], + "date" => $x['date'], + "public_key" => $x['public_key'], ]; $trans['src'] = $x['src']; diff --git a/peer.php b/peer.php index 56fd8ca..5d70627 100755 --- a/peer.php +++ b/peer.php @@ -49,8 +49,10 @@ if ($q == "peer") { } $hostname = san_host($hostname); // if it's already peered, only repeer on request - $res = $db->single("SELECT COUNT(1) FROM peers WHERE hostname=:hostname AND ip=:ip", - [":hostname" => $hostname, ":ip" => $ip]); + $res = $db->single( + "SELECT COUNT(1) FROM peers WHERE hostname=:hostname AND ip=:ip", + [":hostname" => $hostname, ":ip" => $ip] + ); if ($res == 1) { if ($data['repeer'] == 1) { $res = peer_post($hostname."/peer.php?q=peer", ["hostname" => $_config['hostname']]); @@ -68,8 +70,10 @@ if ($q == "peer") { if ($res < $_config['max_peers']) { $reserve = 0; } - $db->run("INSERT ignore INTO peers SET hostname=:hostname, reserve=:reserve, ping=UNIX_TIMESTAMP(), ip=:ip ON DUPLICATE KEY UPDATE hostname=:hostname2", - [":ip" => $ip, ":hostname2" => $hostname, ":hostname" => $hostname, ":reserve" => $reserve]); + $db->run( + "INSERT ignore INTO peers SET hostname=:hostname, reserve=:reserve, ping=UNIX_TIMESTAMP(), ip=:ip ON DUPLICATE KEY UPDATE hostname=:hostname2", + [":ip" => $ip, ":hostname2" => $hostname, ":hostname" => $hostname, ":reserve" => $reserve] + ); // re-peer to make sure the peer is valid $res = peer_post($hostname."/peer.php?q=peer", ["hostname" => $_config['hostname']]); if ($res !== false) { @@ -118,7 +122,7 @@ if ($q == "peer") { if ($res != 0) { api_err("The transaction is already in a block"); } - $acc = new Account; + $acc = new Account(); $src = $acc->get_address($data['public_key']); // make sure the sender has enough balance $balance = $db->single("SELECT balance FROM accounts WHERE id=:id", [":id" => $src]); @@ -214,8 +218,17 @@ if ($q == "peer") { } $b = $data; // add the block to the blockchain - $res = $block->add($b['height'], $b['public_key'], $b['nonce'], $b['data'], $b['date'], $b['signature'], - $b['difficulty'], $b['reward_signature'], $b['argon']); + $res = $block->add( + $b['height'], + $b['public_key'], + $b['nonce'], + $b['data'], + $b['date'], + $b['signature'], + $b['difficulty'], + $b['reward_signature'], + $b['argon'] + ); if (!$res) { _log('['.$ip."] invalid block data - $data[height]"); @@ -245,8 +258,10 @@ elseif ($q == "getBlock") { $height = intval($data['height']); - $r = $db->run("SELECT id,height FROM blocks WHERE height>=:height ORDER by height ASC LIMIT 100", - [":height" => $height]); + $r = $db->run( + "SELECT id,height FROM blocks WHERE height>=:height ORDER by height ASC LIMIT 100", + [":height" => $height] + ); foreach ($r as $x) { $blocks[$x['height']] = $block->export($x['id']); } diff --git a/propagate.php b/propagate.php index 999b072..d54a3c9 100755 --- a/propagate.php +++ b/propagate.php @@ -152,7 +152,7 @@ if ($type == "block") { } // broadcast a transaction to some peers if ($type == "transaction") { - $trx = new Transaction; + $trx = new Transaction(); // get the transaction data $data = $trx->export($id); diff --git a/sanity.php b/sanity.php index 65102e3..9f15639 100755 --- a/sanity.php +++ b/sanity.php @@ -79,8 +79,10 @@ $microsanity = false; if ($arg == "microsanity" && !empty($arg2)) { do { // the microsanity runs only against 1 specific peer - $x = $db->row("SELECT id,hostname FROM peers WHERE reserve=0 AND blacklisted $arg2]); + $x = $db->row( + "SELECT id,hostname FROM peers WHERE reserve=0 AND blacklisted $arg2] + ); if (!$x) { echo "Invalid node - $arg2\n"; @@ -118,8 +120,14 @@ if ($arg == "microsanity" && !empty($arg2)) { // make sure the block is valid $prev = $block->get($current['height'] - 1); $public = $acc->public_key($data['generator']); - if (!$block->mine($public, $data['nonce'], $data['argon'], $block->difficulty($current['height'] - 1), - $prev['id'], $prev['height'])) { + if (!$block->mine( + $public, + $data['nonce'], + $data['argon'], + $block->difficulty($current['height'] - 1), + $prev['id'], + $prev['height'] + )) { echo "Invalid prev-block\n"; break; } @@ -134,8 +142,17 @@ if ($arg == "microsanity" && !empty($arg2)) { // add the new block echo "Starting to sync last block from $x[hostname]\n"; $b = $data; - $res = $block->add($b['height'], $b['public_key'], $b['nonce'], $b['data'], $b['date'], $b['signature'], - $b['difficulty'], $b['reward_signature'], $b['argon']); + $res = $block->add( + $b['height'], + $b['public_key'], + $b['nonce'], + $b['data'], + $b['date'], + $b['signature'], + $b['difficulty'], + $b['reward_signature'], + $b['argon'] + ); if (!$res) { _log("Block add: could not add block - $b[id] - $b[height]"); @@ -234,8 +251,10 @@ foreach ($r as $x) { if ($data === false) { _log("Peer $x[hostname] unresponsive"); // if the peer is unresponsive, mark it as failed and blacklist it for a while - $db->run("UPDATE peers SET fails=fails+1, blacklisted=UNIX_TIMESTAMP()+((fails+1)*3600) WHERE id=:id", - [":id" => $x['id']]); + $db->run( + "UPDATE peers SET fails=fails+1, blacklisted=UNIX_TIMESTAMP()+((fails+1)*3600) WHERE id=:id", + [":id" => $x['id']] + ); continue; } @@ -263,8 +282,10 @@ foreach ($r as $x) { continue; } // make sure there's no peer in db with this ip or hostname - if (!$db->single("SELECT COUNT(1) FROM peers WHERE ip=:ip or hostname=:hostname", - [":ip" => $peer['ip'], ":hostname" => $peer['hostname']])) { + if (!$db->single( + "SELECT COUNT(1) FROM peers WHERE ip=:ip or hostname=:hostname", + [":ip" => $peer['ip'], ":hostname" => $peer['hostname']] + )) { $i++; // check a max_test_peers number of peers from each peer if ($i > $_config['max_test_peers']) { @@ -295,8 +316,10 @@ foreach ($r as $x) { $data['height'] = san($data['height']); if ($data['height'] < $current['height'] - 500) { - $db->run("UPDATE peers SET stuckfail=stuckfail+1, blacklisted=UNIX_TIMESTAMP()+7200 WHERE id=:id", - [":id" => $x['id']]); + $db->run( + "UPDATE peers SET stuckfail=stuckfail+1, blacklisted=UNIX_TIMESTAMP()+7200 WHERE id=:id", + [":id" => $x['id']] + ); continue; } else { if ($x['stuckfail'] > 0) { @@ -418,8 +441,14 @@ if ($current['height'] < $largest_height && $largest_height > 1) { } // check if the block mining data is correct for ($i = $last_good + 1; $i <= $largest_height; $i++) { - if (!$block->mine($cblock[$i]['public_key'], $cblock[$i]['nonce'], $cblock[$i]['argon'], - $cblock[$i]['difficulty'], $cblock[$i - 1]['id'], $cblock[$i - 1]['height'])) { + if (!$block->mine( + $cblock[$i]['public_key'], + $cblock[$i]['nonce'], + $cblock[$i]['argon'], + $cblock[$i]['difficulty'], + $cblock[$i - 1]['id'], + $cblock[$i - 1]['height'] + )) { $invalid = true; break; } @@ -454,8 +483,17 @@ if ($current['height'] < $largest_height && $largest_height > 1) { $good_peer = false; break; } - $res = $block->add($b['height'], $b['public_key'], $b['nonce'], $b['data'], $b['date'], $b['signature'], - $b['difficulty'], $b['reward_signature'], $b['argon']); + $res = $block->add( + $b['height'], + $b['public_key'], + $b['nonce'], + $b['data'], + $b['date'], + $b['signature'], + $b['difficulty'], + $b['reward_signature'], + $b['argon'] + ); if (!$res) { _log("Block add: could not add block - $b[id] - $b[height]"); $good_peer = false; @@ -483,21 +521,27 @@ $db->run("DELETE FROM `mempool` WHERE `date` < UNIX_TIMESTAMP()-(3600*24*14)"); //rebroadcasting local transactions if ($_config['sanity_rebroadcast_locals'] == true) { - $r = $db->run("SELECT id FROM mempool WHERE height>=:current and peer='local' order by `height` asc LIMIT 20", - [":current" => $current['height']]); + $r = $db->run( + "SELECT id FROM mempool WHERE height>=:current and peer='local' order by `height` asc LIMIT 20", + [":current" => $current['height']] + ); _log("Rebroadcasting local transactions - ".count($r)); foreach ($r as $x) { $x['id'] = san($x['id']); system("php propagate.php transaction $x[id] > /dev/null 2>&1 &"); - $db->run("UPDATE mempool SET height=:current WHERE id=:id", - [":id" => $x['id'], ":current" => $current['height']]); + $db->run( + "UPDATE mempool SET height=:current WHERE id=:id", + [":id" => $x['id'], ":current" => $current['height']] + ); } } //rebroadcasting transactions $forgotten = $current['height'] - $_config['sanity_rebroadcast_height']; -$r = $db->run("SELECT id FROM mempool WHERE height<:forgotten ORDER by val DESC LIMIT 10", - [":forgotten" => $forgotten]); +$r = $db->run( + "SELECT id FROM mempool WHERE height<:forgotten ORDER by val DESC LIMIT 10", + [":forgotten" => $forgotten] +); _log("Rebroadcasting external transactions - ".count($r)); @@ -520,8 +564,10 @@ foreach ($r as $x) { $url = $x['hostname']."/peer.php?q="; $data = peer_post($url."ping", [], 5); if ($data === false) { - $db->run("UPDATE peers SET fails=fails+1, blacklisted=UNIX_TIMESTAMP()+((fails+1)*60) WHERE id=:id", - [":id" => $x['id']]); + $db->run( + "UPDATE peers SET fails=fails+1, blacklisted=UNIX_TIMESTAMP()+((fails+1)*60) WHERE id=:id", + [":id" => $x['id']] + ); _log("Random reserve peer test $x[hostname] -> FAILED"); } else { _log("Random reserve peer test $x[hostname] -> OK"); @@ -564,8 +610,14 @@ if ($_config['sanity_recheck_blocks'] > 0) { $key = $db->single("SELECT public_key FROM accounts WHERE id=:id", [":id" => $data['generator']]); - if (!$block->mine($key, $data['nonce'], $data['argon'], $data['difficulty'], $blocks[$i - 1]['id'], - $blocks[$i - 1]['height'])) { + if (!$block->mine( + $key, + $data['nonce'], + $data['argon'], + $data['difficulty'], + $blocks[$i - 1]['id'], + $blocks[$i - 1]['height'] + )) { $db->run("UPDATE config SET val=1 WHERE cfg='sanity_sync'"); _log("Invalid block detected. Deleting everything after $data[height] - $data[id]"); sleep(10);