From 7573e640346494d98424acb06f10c74fa74bcf6a Mon Sep 17 00:00:00 2001 From: Arionum Date: Sat, 31 Mar 2018 12:17:23 +0300 Subject: [PATCH] Improved log handling --- include/config.inc.php | 5 +++++ include/functions.inc.php | 19 +++++++++++++++---- peer.php | 35 ++++++++++++++++++++++++++--------- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/include/config.inc.php b/include/config.inc.php index 67a1f71..5588957 100755 --- a/include/config.inc.php +++ b/include/config.inc.php @@ -32,4 +32,9 @@ $_config['sanity_interval']=900; $_config['allow_hostname_change']=false; // rebroadcast local transactions on each sanity $_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"; + ?> diff --git a/include/functions.inc.php b/include/functions.inc.php index 75cd941..11907e8 100755 --- a/include/functions.inc.php +++ b/include/functions.inc.php @@ -21,10 +21,21 @@ function api_echo($data){ } // log function, shows only in cli atm function _log($data){ - $date=date("[Y-m-d H:s:]"); - $trace=debug_backtrace(); - $location=$trace[1]['class'].'->'.$trace[1]['function'].'()'; - if(php_sapi_name() === 'cli') echo "$date [$location] $data\n"; + $date=date("[Y-m-d H:s:]"); + $trace=debug_backtrace(); + $loc=count($trace)-1; + $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 diff --git a/peer.php b/peer.php index fd4a560..36ddb05 100755 --- a/peer.php +++ b/peer.php @@ -27,7 +27,6 @@ require_once("include/init.inc.php"); $trx = new Transaction; $block=new Block; $q=$_GET['q']; - // the data is sent as json, in $_POST['data'] if(!empty($_POST['data'])){ $data=json_decode(trim($_POST['data']),true); @@ -118,7 +117,7 @@ elseif($q=="submitBlock"){ // receive a new block from a peer // 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']); $current=$block->current(); // block already in the blockchain @@ -139,10 +138,14 @@ elseif($q=="submitBlock"){ } if($accept_new){ // 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 &"); 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($current['height']!=$data['height']-1) { @@ -151,26 +154,40 @@ elseif($q=="submitBlock"){ $pr=$db->row("SELECT * FROM peers WHERE ip=:ip",array(":ip"=>$ip)); if(!$pr) api_err("block-too-old"); $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"); } // 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 + _log('['.$_SERVER['REMOTE_ADDR']."] requesting microsync - $current[height] - $data[height]"); api_echo(array("request"=>"microsync","height"=>$current['height'], "block"=>$current['id'])); } // 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; // 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']); - if(!$res) api_err("invalid-block-data"); - api_echo("block-ok"); + if(!$res) { + _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 system("php propagate.php block '$data[id]' > /dev/null 2>&1 &"); - + api_echo("block-ok"); } // return the current block, used in syncing elseif($q=="currentBlock"){