Improved log handling
This commit is contained in:
@@ -32,4 +32,9 @@ $_config['sanity_interval']=900;
|
|||||||
$_config['allow_hostname_change']=false;
|
$_config['allow_hostname_change']=false;
|
||||||
// rebroadcast local transactions on each sanity
|
// rebroadcast local transactions on each sanity
|
||||||
$_config['sanity_rebroadcast_locals']=true;
|
$_config['sanity_rebroadcast_locals']=true;
|
||||||
|
// write logs to file
|
||||||
|
$_config['enable_logging']=false;
|
||||||
|
// log file, should not be publicly viewable
|
||||||
|
$_config['log_file']="/var/log/aro.log";
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -23,8 +23,19 @@ function api_echo($data){
|
|||||||
function _log($data){
|
function _log($data){
|
||||||
$date=date("[Y-m-d H:s:]");
|
$date=date("[Y-m-d H:s:]");
|
||||||
$trace=debug_backtrace();
|
$trace=debug_backtrace();
|
||||||
$location=$trace[1]['class'].'->'.$trace[1]['function'].'()';
|
$loc=count($trace)-1;
|
||||||
if(php_sapi_name() === 'cli') echo "$date [$location] $data\n";
|
$file=substr($trace[$loc]['file'],strrpos($trace[$loc]['file'],"/")+1);
|
||||||
|
|
||||||
|
$res="$date ".$file.":".$trace[$loc]['line'];
|
||||||
|
|
||||||
|
if(!empty($trace[$loc]['class'])) $res.="---".$trace[$loc]['class'];
|
||||||
|
if(!empty($trace[$loc]['function'])&&$trace[$loc]['function']!='_log') $res.='->'.$trace[$loc]['function'].'()';
|
||||||
|
$res.=" $data \n";
|
||||||
|
if(php_sapi_name() === 'cli') echo $res;
|
||||||
|
global $_config;
|
||||||
|
if($_config['enable_logging']==true){
|
||||||
|
@file_put_contents($_config['log_file'],$res, FILE_APPEND);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// converts PEM key to hex
|
// converts PEM key to hex
|
||||||
|
|||||||
33
peer.php
33
peer.php
@@ -27,7 +27,6 @@ require_once("include/init.inc.php");
|
|||||||
$trx = new Transaction;
|
$trx = new Transaction;
|
||||||
$block=new Block;
|
$block=new Block;
|
||||||
$q=$_GET['q'];
|
$q=$_GET['q'];
|
||||||
|
|
||||||
// the data is sent as json, in $_POST['data']
|
// the data is sent as json, in $_POST['data']
|
||||||
if(!empty($_POST['data'])){
|
if(!empty($_POST['data'])){
|
||||||
$data=json_decode(trim($_POST['data']),true);
|
$data=json_decode(trim($_POST['data']),true);
|
||||||
@@ -118,7 +117,7 @@ elseif($q=="submitBlock"){
|
|||||||
// receive a new block from a peer
|
// receive a new block from a peer
|
||||||
|
|
||||||
// if sanity sync, refuse all
|
// if sanity sync, refuse all
|
||||||
if($_config['sanity_sync']==1) api_err("sanity-sync");
|
if($_config['sanity_sync']==1){ _log('['.$_SERVER['REMOTE_ADDR']."] Block rejected due to sanity sync"); api_err("sanity-sync"); }
|
||||||
$data['id']=san($data['id']);
|
$data['id']=san($data['id']);
|
||||||
$current=$block->current();
|
$current=$block->current();
|
||||||
// block already in the blockchain
|
// block already in the blockchain
|
||||||
@@ -139,10 +138,14 @@ elseif($q=="submitBlock"){
|
|||||||
}
|
}
|
||||||
if($accept_new){
|
if($accept_new){
|
||||||
// if the new block is accepted, run a microsanity to sync it
|
// if the new block is accepted, run a microsanity to sync it
|
||||||
|
_log('['.$_SERVER['REMOTE_ADDR']."] Starting microsanity - $data[height]");
|
||||||
system("php sanity.php microsanity '$ip' > /dev/null 2>&1 &");
|
system("php sanity.php microsanity '$ip' > /dev/null 2>&1 &");
|
||||||
api_echo("microsanity");
|
api_echo("microsanity");
|
||||||
|
|
||||||
} else api_echo("reverse-microsanity"); // if it's not, suggest to the peer to get the block from us
|
} else {
|
||||||
|
_log('['.$_SERVER['REMOTE_ADDR']."] suggesting reverse-microsanity - $data[height]");
|
||||||
|
api_echo("reverse-microsanity"); // if it's not, suggest to the peer to get the block from us
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// if it's not the next block
|
// if it's not the next block
|
||||||
if($current['height']!=$data['height']-1) {
|
if($current['height']!=$data['height']-1) {
|
||||||
@@ -152,25 +155,39 @@ elseif($q=="submitBlock"){
|
|||||||
if(!$pr) api_err("block-too-old");
|
if(!$pr) api_err("block-too-old");
|
||||||
$peer_host=base58_encode($pr['hostname']);
|
$peer_host=base58_encode($pr['hostname']);
|
||||||
system("php propagate.php block current '$peer_host' '$pr[ip]' > /dev/null 2>&1 &");
|
system("php propagate.php block current '$peer_host' '$pr[ip]' > /dev/null 2>&1 &");
|
||||||
|
_log('['.$_SERVER['REMOTE_ADDR']."] block too old, sending our current block - $data[height]");
|
||||||
|
|
||||||
api_err("block-too-old");
|
api_err("block-too-old");
|
||||||
}
|
}
|
||||||
// if the block difference is bigger than 150, nothing should be done. They should sync via sanity
|
// if the block difference is bigger than 150, nothing should be done. They should sync via sanity
|
||||||
if($data['height']-$current['height']>150) api_err("block-out-of-sync");
|
if($data['height']-$current['height']>150) {
|
||||||
|
_log('['.$_SERVER['REMOTE_ADDR']."] block-out-of-sync - $data[height]");
|
||||||
|
api_err("block-out-of-sync");
|
||||||
|
}
|
||||||
// request them to send us a microsync with the latest blocks
|
// request them to send us a microsync with the latest blocks
|
||||||
|
_log('['.$_SERVER['REMOTE_ADDR']."] requesting microsync - $current[height] - $data[height]");
|
||||||
api_echo(array("request"=>"microsync","height"=>$current['height'], "block"=>$current['id']));
|
api_echo(array("request"=>"microsync","height"=>$current['height'], "block"=>$current['id']));
|
||||||
|
|
||||||
}
|
}
|
||||||
// check block data
|
// check block data
|
||||||
if(!$block->check($data)) api_err("invalid-block");
|
if(!$block->check($data)){
|
||||||
|
_log('['.$_SERVER['REMOTE_ADDR']."] invalid block - $data[height]");
|
||||||
|
api_err("invalid-block");
|
||||||
|
}
|
||||||
$b=$data;
|
$b=$data;
|
||||||
// add the block to the blockchain
|
// 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) api_err("invalid-block-data");
|
if(!$res) {
|
||||||
api_echo("block-ok");
|
_log('['.$_SERVER['REMOTE_ADDR']."] invalid block data - $data[height]");
|
||||||
|
api_err("invalid-block-data");
|
||||||
|
}
|
||||||
|
|
||||||
|
_log('['.$_SERVER['REMOTE_ADDR']."] block ok, repropagating - $data[height]");
|
||||||
|
|
||||||
// send it to all our peers
|
// send it to all our peers
|
||||||
system("php propagate.php block '$data[id]' > /dev/null 2>&1 &");
|
system("php propagate.php block '$data[id]' > /dev/null 2>&1 &");
|
||||||
|
api_echo("block-ok");
|
||||||
}
|
}
|
||||||
// return the current block, used in syncing
|
// return the current block, used in syncing
|
||||||
elseif($q=="currentBlock"){
|
elseif($q=="currentBlock"){
|
||||||
|
|||||||
Reference in New Issue
Block a user