From a13c7285fdc32b0f32a393db715f6dff308f018c Mon Sep 17 00:00:00 2001 From: Arionum Date: Sun, 1 Apr 2018 16:28:08 +0300 Subject: [PATCH] future block --- include/functions.inc.php | 2 +- include/schema.inc.php | 5 +++ peer.php | 4 ++- sanity.php | 66 ++++++++++++++++++++++++++++----------- util.php | 2 +- 5 files changed, 58 insertions(+), 21 deletions(-) diff --git a/include/functions.inc.php b/include/functions.inc.php index 11907e8..fc3ad0e 100755 --- a/include/functions.inc.php +++ b/include/functions.inc.php @@ -21,7 +21,7 @@ function api_echo($data){ } // log function, shows only in cli atm function _log($data){ - $date=date("[Y-m-d H:s:]"); + $date=date("[Y-m-d H:i:s]"); $trace=debug_backtrace(); $loc=count($trace)-1; $file=substr($trace[$loc]['file'],strrpos($trace[$loc]['file'],"/")+1); diff --git a/include/schema.inc.php b/include/schema.inc.php index 5315a00..bf5d6cc 100755 --- a/include/schema.inc.php +++ b/include/schema.inc.php @@ -136,6 +136,11 @@ if($dbversion==5){ $db->run("ALTER TABLE `peers` ADD `fails` TINYINT NOT NULL DEFAULT '0' AFTER `ip`; "); $dbversion++; } +if($dbversion==6){ + $db->run("ALTER TABLE `peers` ADD `stuckfail` TINYINT(4) NOT NULL DEFAULT '0' AFTER `fails`, ADD INDEX (`stuckfail`); "); + $db->run("ALTER TABLE `accounts` ADD `alias` VARCHAR(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL AFTER `balance`; "); + $dbversion++; +} // update the db version to the latest one if($dbversion!=$_config['dbversion']) $db->run("UPDATE config SET val=:val WHERE cfg='dbversion'",array(":val"=>$dbversion)); $db->commit(); diff --git a/peer.php b/peer.php index 36ddb05..7c8ad41 100755 --- a/peer.php +++ b/peer.php @@ -122,6 +122,8 @@ elseif($q=="submitBlock"){ $current=$block->current(); // block already in the blockchain if($current['id']==$data['id']) api_echo("block-ok"); + if($data['date']>time()+30) api_err("block in the future"); + if($current['height']==$data['height']&&$current['id']!=$data['id']){ // different forks, same height $accept_new=false; @@ -186,7 +188,7 @@ elseif($q=="submitBlock"){ _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 &"); +// system("php propagate.php block '$data[id]' > /dev/null 2>&1 &"); api_echo("block-ok"); } // return the current block, used in syncing diff --git a/sanity.php b/sanity.php index eaaa559..65f0940 100755 --- a/sanity.php +++ b/sanity.php @@ -31,22 +31,28 @@ if(php_sapi_name() !== 'cli') die("This should only be run as cli"); // make sure there's only a single sanity process running at the same time if(file_exists("tmp/sanity-lock")){ - + $ignore_lock=false; + if($argv[1]=="force"){ + $res=intval(shell_exec("ps aux|grep sanity.php|grep -v grep|wc -l")); + if($res==1){ + $ignore_lock=true; + } + } $pid_time=filemtime("tmp/sanity-lock"); // if the process died, restart after 1day if(time()-$pid_time>86400){ @unlink("tmp/sanity-lock"); } - die("Sanity lock in place"); + if(!$ignore_lock) die("Sanity lock in place"); } // set the new sanity lock $lock = fopen("tmp/sanity-lock", "w"); fclose($lock); $arg=trim($argv[1]); $arg2=trim($argv[2]); - -// sleep for 10 seconds to make sure there's a delay between starting the sanity and other processes -if($arg!="microsanity") sleep(10); +echo "Sleeping for 3 seconds\n"; +// sleep for 3 seconds to make sure there's a delay between starting the sanity and other processes +if($arg!="microsanity") sleep(3); require_once("include/init.inc.php"); @@ -128,6 +134,8 @@ exit; $t=time(); //if($t-$_config['sanity_last']<300) {@unlink("tmp/sanity-lock"); die("The sanity cron was already run recently"); } +_log("Starting sanity"); + // update the last time sanity ran, to set the execution of the next run $db->run("UPDATE config SET val=:time WHERE cfg='sanity_last'",array(":time"=>$t)); $block_peers=array(); @@ -142,9 +150,8 @@ $total_active_peers=0; // checking peers // delete the dead peers -$db->run("DELETE from peers WHERE fails>100"); - -$r=$db->run("SELECT id,hostname FROM peers WHERE reserve=0 AND blacklistedrun("DELETE from peers WHERE fails>100 OR stuckfail>200"); +$r=$db->run("SELECT id,hostname,stuckfail,fails FROM peers WHERE reserve=0 AND blacklistedrun("UPDATE peers SET fails=fails+1, blacklisted=UNIX_TIMESTAMP()+((fails+1)*60) WHERE id=:id",array(":id"=>$x['id'])); + $db->run("UPDATE peers SET fails=fails+1, blacklisted=UNIX_TIMESTAMP()+((fails+1)*3600) WHERE id=:id",array(":id"=>$x['id'])); continue; } @@ -202,6 +211,8 @@ foreach($r as $x){ // do not peer if we are already peered if($peered[$pid]==1) continue; $peered[$pid]=1; + $bad_peers=array("127.0.0.1","localhost"); + if(str_replace($bad_peers,"",$peer['hostname'])!=$peer['hostname']) continue; // if it's our hostname, ignore if($peer['hostname']==$_config['hostname']) continue; // if invalid hostname, ignore @@ -213,7 +224,8 @@ foreach($r as $x){ if($i>$_config['max_test_peers']) break; $peer['hostname'] = filter_var($peer['hostname'], FILTER_SANITIZE_URL); // peer with each one - $test=peer_post($peer['hostname']."/peer.php?q=peer",array("hostname"=>$_config['hostname']),20); + _log("Trying to peer with recommended peer: $peer[hostname]"); + $test=peer_post($peer['hostname']."/peer.php?q=peer",array("hostname"=>$_config['hostname']),5); if($test!==false){ $total_peers++; echo "Peered with: $peer[hostname]\n"; @@ -226,11 +238,16 @@ foreach($r as $x){ // get the current block and check it's blockchain - $data=peer_post($url."currentBlock"); + $data=peer_post($url."currentBlock",array(),5); if($data===false) continue; // peer was responsive, mark it as good - $db->run("UPDATE peers SET fails=0 WHERE id=:id",array(":id"=>$x['id'])); - + if($x['fails']>0) $db->run("UPDATE peers SET fails=0 WHERE id=:id",array(":id"=>$x['id'])); + if($data['height']<$current['height']-500) { + $db->run("UPDATE peers SET stuckfail=stuckfail+1, blacklisted=UNIX_TIMESTAMP()+7200 WHERE id=:id",array(":id"=>$x['id'])); + continue; + } else { + if($x['stuckfail']>0) $db->run("UPDATE peers SET stuckfail=0 WHERE id=:id",array(":id"=>$x['id'])); + } $total_active_peers++; // add the hostname and block relationship to an array $block_peers[$data['id']][]=$x['hostname']; @@ -298,7 +315,7 @@ if($current['height']<$largest_height&&$largest_height>1){ foreach($peers as $host){ _log("Starting to sync from $host"); $url=$host."/peer.php?q="; - $data=peer_post($url."getBlock",array("height"=>$current['height'])); + $data=peer_post($url."getBlock",array("height"=>$current['height']),60); // invalid data if($data===false){ _log("Could not get block from $host - $current[height]"); continue; } // if we're not on the same blockchain but the blockchain is most common with over 90% of the peers, delete the last 3 blocks and retry @@ -384,6 +401,7 @@ if($current['height']<$largest_height&&$largest_height>1){ //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",array(":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 &"); @@ -394,6 +412,9 @@ foreach($r as $x){ //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",array(":forgotten"=>$forgotten)); + +_log("Rebroadcasting external transactions - ".count($r)); + foreach($r as $x){ $x['id']=san($x['id']); system("php propagate.php transaction $x[id] > /dev/null 2>&1 &"); @@ -410,13 +431,20 @@ if($total_peers<$_config['max_peers']*0.7){ //random peer check $r=$db->run("SELECT * FROM peers WHERE blacklistedrun("UPDATE peers SET fails=fails+1, blacklisted=UNIX_TIMESTAMP()+((fails+1)*60) WHERE id=:id",array(":id"=>$x['id'])); - else $db->run("UPDATE peers SET fails=0 WHERE id=:id",array(":id"=>$x['id'])); + $data=peer_post($url."ping",array(),5); + if($data===false){ + $db->run("UPDATE peers SET fails=fails+1, blacklisted=UNIX_TIMESTAMP()+((fails+1)*60) WHERE id=:id",array(":id"=>$x['id'])); + _log("Random reserve peer test $x[hostname] -> FAILED"); + }else{ + _log("Random reserve peer test $x[hostname] -> OK"); + $db->run("UPDATE peers SET fails=0 WHERE id=:id",array(":id"=>$x['id'])); + } } //clean tmp files +_log("Cleaning tmp files"); $f=scandir("tmp/"); $time=time(); foreach($f as $x){ @@ -428,6 +456,7 @@ foreach($f as $x){ //recheck the last blocks if($_config['sanity_recheck_blocks']>0){ +_log("Rechecking blocks"); $blocks=array(); $all_blocks_ok=true; $start=$current['height']-$_config['sanity_recheck_blocks']; @@ -457,6 +486,7 @@ if($_config['sanity_recheck_blocks']>0){ if($all_blocks_ok) echo "All checked blocks are ok\n"; } +_log("Finishing sanity"); @unlink("tmp/sanity-lock"); ?> diff --git a/util.php b/util.php index aa00fc9..47118a5 100755 --- a/util.php +++ b/util.php @@ -303,7 +303,7 @@ elseif($cmd=="delete-peer"){ * http://peer10.arionum.com 16849 */ elseif($cmd=="peers-block"){ - $r=$db->run("SELECT * FROM peers"); + $r=$db->run("SELECT * FROM peers WHERE blacklisted