future block
This commit is contained in:
@@ -21,7 +21,7 @@ function api_echo($data){
|
|||||||
}
|
}
|
||||||
// log function, shows only in cli atm
|
// log function, shows only in cli atm
|
||||||
function _log($data){
|
function _log($data){
|
||||||
$date=date("[Y-m-d H:s:]");
|
$date=date("[Y-m-d H:i:s]");
|
||||||
$trace=debug_backtrace();
|
$trace=debug_backtrace();
|
||||||
$loc=count($trace)-1;
|
$loc=count($trace)-1;
|
||||||
$file=substr($trace[$loc]['file'],strrpos($trace[$loc]['file'],"/")+1);
|
$file=substr($trace[$loc]['file'],strrpos($trace[$loc]['file'],"/")+1);
|
||||||
|
|||||||
@@ -136,6 +136,11 @@ if($dbversion==5){
|
|||||||
$db->run("ALTER TABLE `peers` ADD `fails` TINYINT NOT NULL DEFAULT '0' AFTER `ip`; ");
|
$db->run("ALTER TABLE `peers` ADD `fails` TINYINT NOT NULL DEFAULT '0' AFTER `ip`; ");
|
||||||
$dbversion++;
|
$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
|
// 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));
|
if($dbversion!=$_config['dbversion']) $db->run("UPDATE config SET val=:val WHERE cfg='dbversion'",array(":val"=>$dbversion));
|
||||||
$db->commit();
|
$db->commit();
|
||||||
|
|||||||
4
peer.php
4
peer.php
@@ -122,6 +122,8 @@ elseif($q=="submitBlock"){
|
|||||||
$current=$block->current();
|
$current=$block->current();
|
||||||
// block already in the blockchain
|
// block already in the blockchain
|
||||||
if($current['id']==$data['id']) api_echo("block-ok");
|
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']){
|
if($current['height']==$data['height']&&$current['id']!=$data['id']){
|
||||||
// different forks, same height
|
// different forks, same height
|
||||||
$accept_new=false;
|
$accept_new=false;
|
||||||
@@ -186,7 +188,7 @@ elseif($q=="submitBlock"){
|
|||||||
_log('['.$_SERVER['REMOTE_ADDR']."] block ok, repropagating - $data[height]");
|
_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");
|
api_echo("block-ok");
|
||||||
}
|
}
|
||||||
// return the current block, used in syncing
|
// return the current block, used in syncing
|
||||||
|
|||||||
66
sanity.php
66
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
|
// make sure there's only a single sanity process running at the same time
|
||||||
if(file_exists("tmp/sanity-lock")){
|
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");
|
$pid_time=filemtime("tmp/sanity-lock");
|
||||||
// if the process died, restart after 1day
|
// if the process died, restart after 1day
|
||||||
if(time()-$pid_time>86400){
|
if(time()-$pid_time>86400){
|
||||||
@unlink("tmp/sanity-lock");
|
@unlink("tmp/sanity-lock");
|
||||||
}
|
}
|
||||||
die("Sanity lock in place");
|
if(!$ignore_lock) die("Sanity lock in place");
|
||||||
}
|
}
|
||||||
// set the new sanity lock
|
// set the new sanity lock
|
||||||
$lock = fopen("tmp/sanity-lock", "w");
|
$lock = fopen("tmp/sanity-lock", "w");
|
||||||
fclose($lock);
|
fclose($lock);
|
||||||
$arg=trim($argv[1]);
|
$arg=trim($argv[1]);
|
||||||
$arg2=trim($argv[2]);
|
$arg2=trim($argv[2]);
|
||||||
|
echo "Sleeping for 3 seconds\n";
|
||||||
// sleep for 10 seconds to make sure there's a delay between starting the sanity and other processes
|
// sleep for 3 seconds to make sure there's a delay between starting the sanity and other processes
|
||||||
if($arg!="microsanity") sleep(10);
|
if($arg!="microsanity") sleep(3);
|
||||||
|
|
||||||
|
|
||||||
require_once("include/init.inc.php");
|
require_once("include/init.inc.php");
|
||||||
@@ -128,6 +134,8 @@ exit;
|
|||||||
$t=time();
|
$t=time();
|
||||||
//if($t-$_config['sanity_last']<300) {@unlink("tmp/sanity-lock"); die("The sanity cron was already run recently"); }
|
//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
|
// 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));
|
$db->run("UPDATE config SET val=:time WHERE cfg='sanity_last'",array(":time"=>$t));
|
||||||
$block_peers=array();
|
$block_peers=array();
|
||||||
@@ -142,9 +150,8 @@ $total_active_peers=0;
|
|||||||
// checking peers
|
// checking peers
|
||||||
|
|
||||||
// delete the dead peers
|
// delete the dead peers
|
||||||
$db->run("DELETE from peers WHERE fails>100");
|
$db->run("DELETE from peers WHERE fails>100 OR stuckfail>200");
|
||||||
|
$r=$db->run("SELECT id,hostname,stuckfail,fails FROM peers WHERE reserve=0 AND blacklisted<UNIX_TIMESTAMP()");
|
||||||
$r=$db->run("SELECT id,hostname FROM peers WHERE reserve=0 AND blacklisted<UNIX_TIMESTAMP()");
|
|
||||||
|
|
||||||
$total_peers=count($r);
|
$total_peers=count($r);
|
||||||
|
|
||||||
@@ -186,12 +193,14 @@ if($total_peers==0&&$_config['testnet']==false){
|
|||||||
|
|
||||||
// contact all the active peers
|
// contact all the active peers
|
||||||
foreach($r as $x){
|
foreach($r as $x){
|
||||||
|
_log("Contacting peer $x[hostname]");
|
||||||
$url=$x['hostname']."/peer.php?q=";
|
$url=$x['hostname']."/peer.php?q=";
|
||||||
// get their peers list
|
// get their peers list
|
||||||
$data=peer_post($url."getPeers");
|
$data=peer_post($url."getPeers",array(),5);
|
||||||
if($data===false) {
|
if($data===false) {
|
||||||
|
_log("Peer $x[hostname] unresponsive");
|
||||||
// if the peer is unresponsive, mark it as failed and blacklist it for a while
|
// 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)*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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -202,6 +211,8 @@ foreach($r as $x){
|
|||||||
// do not peer if we are already peered
|
// do not peer if we are already peered
|
||||||
if($peered[$pid]==1) continue;
|
if($peered[$pid]==1) continue;
|
||||||
$peered[$pid]=1;
|
$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 it's our hostname, ignore
|
||||||
if($peer['hostname']==$_config['hostname']) continue;
|
if($peer['hostname']==$_config['hostname']) continue;
|
||||||
// if invalid hostname, ignore
|
// if invalid hostname, ignore
|
||||||
@@ -213,7 +224,8 @@ foreach($r as $x){
|
|||||||
if($i>$_config['max_test_peers']) break;
|
if($i>$_config['max_test_peers']) break;
|
||||||
$peer['hostname'] = filter_var($peer['hostname'], FILTER_SANITIZE_URL);
|
$peer['hostname'] = filter_var($peer['hostname'], FILTER_SANITIZE_URL);
|
||||||
// peer with each one
|
// 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){
|
if($test!==false){
|
||||||
$total_peers++;
|
$total_peers++;
|
||||||
echo "Peered with: $peer[hostname]\n";
|
echo "Peered with: $peer[hostname]\n";
|
||||||
@@ -226,11 +238,16 @@ foreach($r as $x){
|
|||||||
|
|
||||||
|
|
||||||
// get the current block and check it's blockchain
|
// 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;
|
if($data===false) continue;
|
||||||
// peer was responsive, mark it as good
|
// 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++;
|
$total_active_peers++;
|
||||||
// add the hostname and block relationship to an array
|
// add the hostname and block relationship to an array
|
||||||
$block_peers[$data['id']][]=$x['hostname'];
|
$block_peers[$data['id']][]=$x['hostname'];
|
||||||
@@ -298,7 +315,7 @@ if($current['height']<$largest_height&&$largest_height>1){
|
|||||||
foreach($peers as $host){
|
foreach($peers as $host){
|
||||||
_log("Starting to sync from $host");
|
_log("Starting to sync from $host");
|
||||||
$url=$host."/peer.php?q=";
|
$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
|
// invalid data
|
||||||
if($data===false){ _log("Could not get block from $host - $current[height]"); continue; }
|
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
|
// 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
|
//rebroadcasting local transactions
|
||||||
if($_config['sanity_rebroadcast_locals']==true){
|
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']));
|
$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){
|
foreach($r as $x){
|
||||||
$x['id']=san($x['id']);
|
$x['id']=san($x['id']);
|
||||||
system("php propagate.php transaction $x[id] > /dev/null 2>&1 &");
|
system("php propagate.php transaction $x[id] > /dev/null 2>&1 &");
|
||||||
@@ -394,6 +412,9 @@ foreach($r as $x){
|
|||||||
//rebroadcasting transactions
|
//rebroadcasting transactions
|
||||||
$forgotten=$current['height']-$_config['sanity_rebroadcast_height'];
|
$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));
|
$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){
|
foreach($r as $x){
|
||||||
$x['id']=san($x['id']);
|
$x['id']=san($x['id']);
|
||||||
system("php propagate.php transaction $x[id] > /dev/null 2>&1 &");
|
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
|
//random peer check
|
||||||
$r=$db->run("SELECT * FROM peers WHERE blacklisted<UNIX_TIMESTAMP() and reserve=1 LIMIT ".$_config['max_test_peers']);
|
$r=$db->run("SELECT * FROM peers WHERE blacklisted<UNIX_TIMESTAMP() and reserve=1 LIMIT ".$_config['max_test_peers']);
|
||||||
foreach($r as $x){
|
foreach($r as $x){
|
||||||
|
|
||||||
$url=$x['hostname']."/peer.php?q=";
|
$url=$x['hostname']."/peer.php?q=";
|
||||||
$data=peer_post($url."ping");
|
$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']));
|
if($data===false){
|
||||||
else $db->run("UPDATE peers SET fails=0 WHERE id=:id",array(":id"=>$x['id']));
|
$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
|
//clean tmp files
|
||||||
|
_log("Cleaning tmp files");
|
||||||
$f=scandir("tmp/");
|
$f=scandir("tmp/");
|
||||||
$time=time();
|
$time=time();
|
||||||
foreach($f as $x){
|
foreach($f as $x){
|
||||||
@@ -428,6 +456,7 @@ foreach($f as $x){
|
|||||||
|
|
||||||
//recheck the last blocks
|
//recheck the last blocks
|
||||||
if($_config['sanity_recheck_blocks']>0){
|
if($_config['sanity_recheck_blocks']>0){
|
||||||
|
_log("Rechecking blocks");
|
||||||
$blocks=array();
|
$blocks=array();
|
||||||
$all_blocks_ok=true;
|
$all_blocks_ok=true;
|
||||||
$start=$current['height']-$_config['sanity_recheck_blocks'];
|
$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";
|
if($all_blocks_ok) echo "All checked blocks are ok\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_log("Finishing sanity");
|
||||||
|
|
||||||
@unlink("tmp/sanity-lock");
|
@unlink("tmp/sanity-lock");
|
||||||
?>
|
?>
|
||||||
|
|||||||
2
util.php
2
util.php
@@ -303,7 +303,7 @@ elseif($cmd=="delete-peer"){
|
|||||||
* http://peer10.arionum.com 16849
|
* http://peer10.arionum.com 16849
|
||||||
*/
|
*/
|
||||||
elseif($cmd=="peers-block"){
|
elseif($cmd=="peers-block"){
|
||||||
$r=$db->run("SELECT * FROM peers");
|
$r=$db->run("SELECT * FROM peers WHERE blacklisted<UNIX_TIMESTAMP()");
|
||||||
foreach($r as $x){
|
foreach($r as $x){
|
||||||
$a=peer_post($x['hostname']."/peer.php?q=currentBlock",array(),5);
|
$a=peer_post($x['hostname']."/peer.php?q=currentBlock",array(),5);
|
||||||
$enc=base58_encode($x['hostname']);
|
$enc=base58_encode($x['hostname']);
|
||||||
|
|||||||
Reference in New Issue
Block a user