Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions api.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,15 +452,15 @@



$private_key = san($data['private_key']);
$private_key = san($data['private_key'] ?? "");
if (!$acc->valid_key($private_key)) {
api_err("Invalid private key");
}
$signature = san($data['signature']);
$signature = san($data['signature'] ?? "");
if (!$acc->valid_key($signature)) {
api_err("Invalid signature");
}
$date = $data['date'] + 0;
$date = ($data['date'] ?? 0) + 0;

if ($date == 0) {
$date = time();
Expand Down
63 changes: 53 additions & 10 deletions include/block.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,11 @@ public function add($height, $public_key, $nonce, $data, $date, $signature, $dif
if ($res != 1) {
// rollback and exit if it fails
_log("Block DB insert failed");
$db->rollback();
try {
$db->rollback();
} Catch (Exception $ex){
// no transactions made
}
$db->exec("UNLOCK TABLES");
return false;
}
Expand All @@ -190,7 +194,11 @@ public function add($height, $public_key, $nonce, $data, $date, $signature, $dif
if ($res == false) {
// rollback and exit if it fails
_log("Reward DB insert failed");
$db->rollback();
try {
$db->rollback();
} Catch (Exception $ex){
// no transactions made
}
$db->exec("UNLOCK TABLES");
return false;
}
Expand Down Expand Up @@ -222,7 +230,11 @@ public function add($height, $public_key, $nonce, $data, $date, $signature, $dif
if ($res != 1) {
// rollback and exit if it fails
_log("Masternode Cold reward DB insert failed");
$db->rollback();
try {
$db->rollback();
} Catch (Exception $ex){
// no transactions made
}
$db->exec("UNLOCK TABLES");
return false;
}
Expand Down Expand Up @@ -256,7 +268,11 @@ public function add($height, $public_key, $nonce, $data, $date, $signature, $dif
if ($res != 1) {
// rollback and exit if it fails
_log("Masternode reward DB insert failed");
$db->rollback();
try {
$db->rollback();
} Catch (Exception $ex){
// no transactions made
}
$db->exec("UNLOCK TABLES");
return false;
}
Expand All @@ -265,7 +281,11 @@ public function add($height, $public_key, $nonce, $data, $date, $signature, $dif

// rollback and exit if it fails
_log("Masternode log DB insert failed");
$db->rollback();
try {
$db->rollback();
} Catch (Exception $ex){
// no transactions made
}
$db->exec("UNLOCK TABLES");
return false;
}
Expand Down Expand Up @@ -299,7 +319,11 @@ public function add($height, $public_key, $nonce, $data, $date, $signature, $dif
// if any fails, rollback
if ($res == false) {
_log("Rollback block", 3);
$db->rollback();
try {
$db->rollback();
} Catch (Exception $ex){
// no transactions made
}
} else {
_log("Commiting block", 3);
$db->commit();
Expand Down Expand Up @@ -1242,6 +1266,9 @@ public function parse_block($block, $height, $data, $test = true, $bootstrapping
}

// prepare total balance
if(!isset($balance[$x['src']])){
$balance[$x['src']]=0;
}
$balance[$x['src']] += $x['val'] + $x['fee'];

// check if the transaction is already on the blockchain
Expand Down Expand Up @@ -1346,7 +1373,11 @@ public function delete($height)
$res = $trx->reverse($x['id']);
if ($res === false) {
_log("A transaction could not be reversed. Delete block failed.");
$db->rollback();
try {
$db->rollback();
} Catch (Exception $ex){
// no transactions made
}
// the blockchain has some flaw, we should resync from scratch

$current = $this->current();
Expand All @@ -1372,7 +1403,11 @@ public function delete($height)
$res = $db->run("DELETE FROM blocks WHERE id=:id", [":id" => $x['id']]);
if ($res != 1) {
_log("Delete block failed.");
$db->rollback();
try {
$db->rollback();
} Catch (Exception $ex){
// no transactions made
}
$db->exec("UNLOCK TABLES");
return false;
}
Expand Down Expand Up @@ -1406,15 +1441,23 @@ public function delete_id($id)
$res = $trx->reverse($x['id']);
if ($res === false) {
// rollback if you can't reverse the transactions
$db->rollback();
try {
$db->rollback();
} Catch (Exception $ex){
// no transactions made
}
$db->exec("UNLOCK TABLES");
return false;
}
// remove the actual block
$res = $db->run("DELETE FROM blocks WHERE id=:id", [":id" => $x['id']]);
if ($res != 1) {
//rollback if you can't delete the block
$db->rollback();
try {
$db->rollback();
} Catch (Exception $ex){
// no transactions made
}
$db->exec("UNLOCK TABLES");
return false;
}
Expand Down
5 changes: 4 additions & 1 deletion include/config-sample.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@
// Log verbosity (default 0, maximum 3)
$_config['log_verbosity'] = 0;

// Log E_ALL php errors
$_config['log_php_errors'] = false;

/*
|--------------------------------------------------------------------------
| Masternode Configuration
Expand All @@ -159,4 +162,4 @@
// The public key for the masternode
$_config['masternode_public_key'] = '';
$_config['masternode_voting_public_key'] = '';
$_config['masternode_voting_private_key'] = '';
$_config['masternode_voting_private_key'] = '';
7 changes: 5 additions & 2 deletions include/functions.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
// simple santization function to accept only alphanumeric characters
function san($a, $b = "")
{
if(empty($a)) return "";
$a = preg_replace("/[^a-zA-Z0-9".$b."]/", "", $a);

return $a;
}

function san_ip($a)
{
if(empty($a)) return "";
$a = preg_replace("/[^a-fA-F0-9\[\]\.\:]/", "", $a);
return $a;
}

function san_host($a)
{
if(empty($a)) return "";
$a = preg_replace("/[^a-zA-Z0-9\.\-\:\/]/", "", $a);
return $a;
}
Expand Down Expand Up @@ -285,14 +288,14 @@ function peer_post($url, $data = [], $timeout = 60, $debug = false)

$context = stream_context_create($opts);

$result = file_get_contents($url, false, $context);
$result = @file_get_contents($url, false, $context);
if ($debug) {
echo "\nPeer response: $result\n";
}
$res = json_decode($result, true);

// the function will return false if something goes wrong
if ($res['status'] != "ok" || $res['coin'] != $_config['coin']) {
if ($res==false || is_null($res) || $res['status'] != "ok" || $res['coin'] != $_config['coin']) {
return false;
}
return $res['data'];
Expand Down
23 changes: 15 additions & 8 deletions include/init.inc.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
<?php
// ARO version
define("VERSION", "1.0.0-alpha.7");
define("VERSION", "1.0.0-alpha.8");
// UTC timezone by default
date_default_timezone_set("UTC");

// error_reporting(E_ALL & ~E_NOTICE);
error_reporting(0);
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
// not accessible directly
if (php_sapi_name() !== 'cli' && substr_count($_SERVER['PHP_SELF'], "/") > 1) {
die("This application should only be run in the main directory /");
}

require_once __DIR__.'/Exception.php';
require_once __DIR__.'/config.inc.php';
if(isset($_config['log_php_errors']) && $_config['log_php_errors'] == true){
error_reporting(E_ALL);
} else {
error_reporting(0);
}
require_once __DIR__.'/Exception.php';

require_once __DIR__.'/db.inc.php';
require_once __DIR__.'/functions.inc.php';
require_once __DIR__.'/Blacklist.php';
Expand Down Expand Up @@ -60,7 +63,7 @@
}

// nothing is allowed while in maintenance
if ($_config['maintenance'] == 1) {
if (isset($_config['maintenance']) && $_config['maintenance'] == 1) {
api_err("under-maintenance");
}

Expand Down Expand Up @@ -94,9 +97,13 @@
}

// current hostname
$hostname = (!empty($_SERVER['HTTPS']) ? 'https' : 'http')."://".san_host($_SERVER['HTTP_HOST']);
$http_host=san_host($_SERVER['HTTP_HOST'] ?? "");
$hostname = (!empty($_SERVER['HTTPS']) ? 'https' : 'http')."://".$http_host;
// set the hostname to the current one
if ($hostname != $_config['hostname'] && $_SERVER['HTTP_HOST'] != "localhost" && $_SERVER['HTTP_HOST'] != "127.0.0.1" && $_SERVER['hostname'] != '::1' && php_sapi_name() !== 'cli' && ($_config['allow_hostname_change'] != false || empty($_config['hostname']))) {
if(!isset($_config['hostname'])){
$_config['hostname']="";
}
if ($hostname != $_config['hostname'] && $http_host != "localhost" && $http_host != "127.0.0.1" && $http_host != '::1' && php_sapi_name() !== 'cli' && ($_config['allow_hostname_change'] != false || empty($_config['hostname']))) {
$db->run("UPDATE config SET val=:hostname WHERE cfg='hostname' LIMIT 1", [":hostname" => $hostname]);
$_config['hostname'] = $hostname;
}
Expand Down
4 changes: 3 additions & 1 deletion include/transaction.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,9 @@ public function mempool($max)
_log("$x[id] - Transaction Check Failed");
continue;
}

if(!isset($balance[$x['src']])){
$balance[$x['src']]=0;
}
$balance[$x['src']] += $x['val'] + $x['fee'];
if ($db->single("SELECT COUNT(1) FROM transactions WHERE id=:id", [":id" => $x['id']]) > 0) {
_log("$x[id] - Duplicate transaction");
Expand Down
4 changes: 3 additions & 1 deletion peer.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
[":hostname" => $hostname, ":ip" => $ip]
);
if ($res == 1) {
if ($data['repeer'] == 1) {
if (isset($data['repeer']) && $data['repeer'] == 1) {
$res = peer_post($hostname."/peer.php?q=peer", ["hostname" => $_config['hostname']]);
if ($res !== false) {
api_echo("re-peer-ok");
Expand Down Expand Up @@ -141,6 +141,8 @@
}
$acc = new Account();
$src = $acc->get_address($data['public_key']);
$val = $data['val'] ?? 0;
$fee = $data['fee'] ?? 0;
// make sure the sender has enough balance
$balance = $db->single("SELECT balance FROM accounts WHERE id=:id", [":id" => $src]);
if ($balance < $val + $fee) {
Expand Down
20 changes: 10 additions & 10 deletions propagate.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@
require_once __DIR__.'/include/init.inc.php';
$block = new Block();

$type = san($argv[1]);
$id = san($argv[2]);
$type = san($argv[1] ?? "");
$id = san($argv[2] ?? "");
$debug = false;
$linear = false;
// if debug mode, all data is printed to console, no background processes
if (trim($argv[5]) == 'debug') {
if (trim($argv[5] ?? "") == 'debug') {
$debug = true;
}
if (trim($argv[5]) == 'linear') {
if (trim($argv[5] ?? "") == 'linear') {
$linear = true;
}
$peer = san(trim($argv[3]));
$peer = san(trim($argv[3] ?? ""));


// broadcasting a block to all peers
Expand Down Expand Up @@ -110,11 +110,11 @@
// send the block as POST to the peer
echo "Block sent to $hostname:\n";
$response = peer_post($hostname."/peer.php?q=submitBlock", $data, 60, $debug);
_log("Propagating block to $hostname - [result: $response] $data[height] - $data[id]",2);
if ($response == "block-ok") {
echo "Block $i accepted. Exiting.\n";
_log("Propagating block to $hostname - $data[height] - $data[id]",2);
if (is_string($response) && $response == "block-ok") {
echo "Block $data[height] accepted. Exiting.\n";
exit;
} elseif ($response['request'] == "microsync") {
} elseif (isset($response['request']) && $response['request'] == "microsync") {
// the peer requested us to send more blocks, as it's behind
echo "Microsync request\n";
$height = intval($response['height']);
Expand Down Expand Up @@ -142,7 +142,7 @@
}
echo "Block\t$i\t accepted\n";
}
} elseif ($response == "reverse-microsanity") {
} elseif (is_string($response) && $response == "reverse-microsanity") {
// the peer informe us that we should run a microsanity
echo "Running microsanity\n";
$ip = trim($argv[4]);
Expand Down
Loading