reverse microsync

This commit is contained in:
Arionum
2018-01-12 12:48:24 +02:00
parent bb94575f47
commit 967d38b5cd
4 changed files with 37 additions and 18 deletions

View File

@@ -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; global $_config;
if($debug) echo "\nPeer post: $url\n";
$postdata = http_build_query( $postdata = http_build_query(
array( array(
'data' => json_encode($data), 'data' => json_encode($data),
@@ -192,7 +193,7 @@ function peer_post($url, $data=array(),$timeout=60){
$context = stream_context_create($opts); $context = stream_context_create($opts);
$result = file_get_contents($url, false, $context); $result = file_get_contents($url, false, $context);
if($debug) echo "\nPeer response: $result\n";
$res=json_decode($result,true); $res=json_decode($result,true);
if($res['status']!="ok"||$res['coin']!=$_config['coin']) return false; if($res['status']!="ok"||$res['coin']!=$_config['coin']) return false;
return $res['data']; return $res['data'];

View File

@@ -99,7 +99,7 @@ elseif($q=="submitTransaction"){
$trx->add_mempool($data, $_SERVER['REMOTE_ADDR']); $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'])); $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"); api_echo("transaction-ok");
} }
elseif($q=="submitBlock"){ elseif($q=="submitBlock"){
@@ -119,14 +119,14 @@ elseif($q=="submitBlock"){
} }
} }
if($accept_new){ if($accept_new){
system("php sanity.php microsanity $ip &>/dev/null &"); system("php sanity.php microsanity '$ip' &>/dev/null &");
api_echo("microsanity"); api_echo("microsanity");
} } else api_echo("reverse-microsanity");
} }
if($current['height']!=$data['height']-1) { if($current['height']!=$data['height']-1) {
if($data['height']<$current['height']) api_err("block-too-old"); 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'])); 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"); if(!$res) api_err("invalid-block-data");
api_echo("block-ok"); api_echo("block-ok");
system("php propagate.php block $data[id] &>/dev/null &"); system("php propagate.php block '$data[id]' &>/dev/null &");
} }

View File

@@ -29,11 +29,16 @@ $block= new Block();
$type=san($argv[1]); $type=san($argv[1]);
$id=san($argv[2]); $id=san($argv[2]);
$debug=false;
if(trim($argv[5])=='debug') $debug=true;
$peer=san(trim($argv[3])); $peer=san(trim($argv[3]));
if(empty($peer)&&$type=="block"){ if((empty($peer)||$peer=='all')&&$type=="block"){
$whr=""; $whr="";
if($id=="current") {
$current=$block->current();
$id=$current['id'];
}
$data=$block->export($id); $data=$block->export($id);
if($data===false||empty($data)) die("Could not export block"); 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"); $r=$db->run("SELECT * FROM peers WHERE blacklisted < UNIX_TIMESTAMP() AND reserve=0");
foreach($r as $x) { foreach($r as $x) {
$host=base58_encode($x['hostname']); $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; exit;
} }
@@ -54,14 +60,19 @@ if(empty($peer)&&$type=="block"){
if($type=="block"){ if($type=="block"){
if($id=="current"){
$data=file_get_contents("tmp/$id"); $current=$block->current();
if(empty($data)) { echo "Invalid Block data"; exit; } $data=$block->export($current['id']);
$data=json_decode($data,true); 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); $hostname=base58_decode($peer);
echo "Peer response - $hostname:\n"; echo "Block sent to $hostname:\n";
$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 accepted. Exiting.\n"; exit;} if($response=="block-ok") { echo "Block $i accepted. Exiting.\n"; exit;}
elseif($response['request']=="microsync"){ elseif($response['request']=="microsync"){
echo "Microsync request\n"; echo "Microsync request\n";
@@ -76,12 +87,16 @@ if($type=="block"){
for($i=$height+1;$i<=$current['height'];$i++){ for($i=$height+1;$i<=$current['height'];$i++){
$data=$block->export("",$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;} if($response!="block-ok") { echo "Block $i not accepted. Exiting.\n"; exit;}
echo "Block\t$i\t accepted\n"; 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"; else echo "Block not accepted!\n";

View File

@@ -125,7 +125,10 @@ echo "Mempool size: $res\n";
$r=$db->run("SELECT * FROM peers"); $r=$db->run("SELECT * FROM peers");
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']);
if($argv[2]=="debug") echo "$enc\t";
echo "$x[hostname]\t$a[height]\n"; echo "$x[hostname]\t$a[height]\n";
} }