From 967d38b5cdd486b42beb7cc908d051562e513a96 Mon Sep 17 00:00:00 2001 From: Arionum Date: Fri, 12 Jan 2018 12:48:24 +0200 Subject: [PATCH] reverse microsync --- include/functions.inc.php | 5 +++-- peer.php | 10 +++++----- propagate.php | 37 ++++++++++++++++++++++++++----------- util.php | 3 +++ 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/include/functions.inc.php b/include/functions.inc.php index b84ae2b..aa0f222 100755 --- a/include/functions.inc.php +++ b/include/functions.inc.php @@ -171,8 +171,9 @@ function ec_verify($data, $signature, $key){ } -function peer_post($url, $data=array(),$timeout=60){ +function peer_post($url, $data=array(),$timeout=60,$debug=false){ global $_config; + if($debug) echo "\nPeer post: $url\n"; $postdata = http_build_query( array( 'data' => json_encode($data), @@ -192,7 +193,7 @@ function peer_post($url, $data=array(),$timeout=60){ $context = stream_context_create($opts); $result = file_get_contents($url, false, $context); - + if($debug) echo "\nPeer response: $result\n"; $res=json_decode($result,true); if($res['status']!="ok"||$res['coin']!=$_config['coin']) return false; return $res['data']; diff --git a/peer.php b/peer.php index a4dd2cc..2d73c21 100755 --- a/peer.php +++ b/peer.php @@ -99,7 +99,7 @@ elseif($q=="submitTransaction"){ $trx->add_mempool($data, $_SERVER['REMOTE_ADDR']); $res=$db->row("SELECT COUNT(1) as c, sum(val) as v FROM mempool ",array(":src"=>$data['src'])); - if($res['c']<$_config['max_mempool_rebroadcast']&&$res['v']/$res['c']<$data['val']) system("php propagate.php transaction $data[id] &>/dev/null &"); + if($res['c']<$_config['max_mempool_rebroadcast']&&$res['v']/$res['c']<$data['val']) system("php propagate.php transaction '$data[id]' &>/dev/null &"); api_echo("transaction-ok"); } elseif($q=="submitBlock"){ @@ -119,14 +119,14 @@ elseif($q=="submitBlock"){ } } if($accept_new){ - system("php sanity.php microsanity $ip &>/dev/null &"); + system("php sanity.php microsanity '$ip' &>/dev/null &"); api_echo("microsanity"); - } + } else api_echo("reverse-microsanity"); } if($current['height']!=$data['height']-1) { if($data['height']<$current['height']) api_err("block-too-old"); - if($data['height']-$current['height']>30) api_err("block-out-of-sync"); + if($data['height']-$current['height']>150) api_err("block-out-of-sync"); api_echo(array("request"=>"microsync","height"=>$current['height'], "block"=>$current['id'])); } @@ -137,7 +137,7 @@ elseif($q=="submitBlock"){ if(!$res) api_err("invalid-block-data"); api_echo("block-ok"); - system("php propagate.php block $data[id] &>/dev/null &"); + system("php propagate.php block '$data[id]' &>/dev/null &"); } diff --git a/propagate.php b/propagate.php index 515c3ed..2b87fdc 100755 --- a/propagate.php +++ b/propagate.php @@ -29,11 +29,16 @@ $block= new Block(); $type=san($argv[1]); $id=san($argv[2]); +$debug=false; +if(trim($argv[5])=='debug') $debug=true; $peer=san(trim($argv[3])); -if(empty($peer)&&$type=="block"){ +if((empty($peer)||$peer=='all')&&$type=="block"){ $whr=""; - + if($id=="current") { + $current=$block->current(); + $id=$current['id']; + } $data=$block->export($id); if($data===false||empty($data)) die("Could not export block"); @@ -44,7 +49,8 @@ if(empty($peer)&&$type=="block"){ $r=$db->run("SELECT * FROM peers WHERE blacklisted < UNIX_TIMESTAMP() AND reserve=0"); foreach($r as $x) { $host=base58_encode($x['hostname']); - system("php propagate.php $type $id $host &>/dev/null &"); + if($debug) system("php propagate.php '$type' '$id' '$host' '$x[ip]' debug"); + else system("php propagate.php '$type' '$id' '$host' '$x[ip]' &>/dev/null &"); } exit; } @@ -54,14 +60,19 @@ if(empty($peer)&&$type=="block"){ if($type=="block"){ - - $data=file_get_contents("tmp/$id"); - if(empty($data)) { echo "Invalid Block data"; exit; } - $data=json_decode($data,true); + if($id=="current"){ + $current=$block->current(); + $data=$block->export($current['id']); + if(!$data) { echo "Invalid Block data"; exit; } + } else { + $data=file_get_contents("tmp/$id"); + if(empty($data)) { echo "Invalid Block data"; exit; } + $data=json_decode($data,true); + } $hostname=base58_decode($peer); - echo "Peer response - $hostname:\n"; - $response= peer_post($hostname."/peer.php?q=submitBlock",$data); + echo "Block sent to $hostname:\n"; + $response= peer_post($hostname."/peer.php?q=submitBlock",$data,60,$debug); if($response=="block-ok") { echo "Block $i accepted. Exiting.\n"; exit;} elseif($response['request']=="microsync"){ echo "Microsync request\n"; @@ -76,12 +87,16 @@ if($type=="block"){ for($i=$height+1;$i<=$current['height'];$i++){ $data=$block->export("",$i); - $response = peer_post($hostname."/peer.php?q=submitBlock",$data); - + $response = peer_post($hostname."/peer.php?q=submitBlock",$data,60,$debug); if($response!="block-ok") { echo "Block $i not accepted. Exiting.\n"; exit;} echo "Block\t$i\t accepted\n"; } + } elseif($response=="reverse-microsanity"){ + echo "Running microsanity\n"; + $ip=trim($argv[4]); + if(empty($ip)) die("Invalid IP"); + system("php sanity.php microsanity '$ip' &>/dev/null &"); } else echo "Block not accepted!\n"; diff --git a/util.php b/util.php index 488a997..91eb5ae 100755 --- a/util.php +++ b/util.php @@ -125,7 +125,10 @@ echo "Mempool size: $res\n"; $r=$db->run("SELECT * FROM peers"); foreach($r as $x){ $a=peer_post($x['hostname']."/peer.php?q=currentBlock",array(),5); + $enc=base58_encode($x['hostname']); + if($argv[2]=="debug") echo "$enc\t"; echo "$x[hostname]\t$a[height]\n"; + }