Update propagate file to PSR-2

This commit is contained in:
pxgamer
2018-05-29 15:47:18 +01:00
parent ffd3a55144
commit b68f316029

View File

@@ -1,7 +1,7 @@
<?php <?php
/* /*
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2018 AroDev Copyright (c) 2018 AroDev
www.arionum.com www.arionum.com
@@ -25,116 +25,153 @@ OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
set_time_limit(360); set_time_limit(360);
require_once("include/init.inc.php"); require_once("include/init.inc.php");
$block= new Block(); $block = new Block();
$type=san($argv[1]); $type = san($argv[1]);
$id=san($argv[2]); $id = san($argv[2]);
$debug=false; $debug = false;
$linear=false; $linear = false;
// if debug mode, all data is printed to console, no background processes // if debug mode, all data is printed to console, no background processes
if(trim($argv[5])=='debug') $debug=true; if (trim($argv[5]) == 'debug') {
if(trim($argv[5])=='linear') $linear=true; $debug = true;
$peer=san(trim($argv[3])); }
if (trim($argv[5]) == 'linear') {
$linear = true;
}
$peer = san(trim($argv[3]));
// broadcasting a block to all peers // broadcasting a block to all peers
if((empty($peer)||$peer=='all')&&$type=="block"){ if ((empty($peer) || $peer == 'all') && $type == "block") {
$whr=""; $whr = "";
if($id=="current") { if ($id == "current") {
$current=$block->current(); $current = $block->current();
$id=$current['id']; $id = $current['id'];
} }
$data=$block->export($id); $data = $block->export($id);
$id=san($id); $id = san($id);
if($data===false||empty($data)) die("Could not export block"); if ($data === false || empty($data)) {
$data=json_encode($data); die("Could not export block");
// cache it to reduce the load }
$res=file_put_contents("tmp/$id",$data); $data = json_encode($data);
if($res===false) die("Could not write the cache file"); // cache it to reduce the load
// broadcasting to all peers $res = file_put_contents("tmp/$id", $data);
$ewhr=""; if ($res === false) {
// boradcasting to only certain peers die("Could not write the cache file");
if($linear==true) $ewhr=" ORDER by RAND() LIMIT 5"; }
$r=$db->run("SELECT * FROM peers WHERE blacklisted < UNIX_TIMESTAMP() AND reserve=0 $ewhr"); // broadcasting to all peers
foreach($r as $x) { $ewhr = "";
// encode the hostname in base58 and sanitize the IP to avoid any second order shell injections // boradcasting to only certain peers
$host=base58_encode($x['hostname']); if ($linear == true) {
$ip=filter_var($x['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE); $ewhr = " ORDER by RAND() LIMIT 5";
// fork a new process to send the blocks async }
if($debug) system("php propagate.php '$type' '$id' '$host' '$ip' debug"); $r = $db->run("SELECT * FROM peers WHERE blacklisted < UNIX_TIMESTAMP() AND reserve=0 $ewhr");
elseif($linear) system("php propagate.php '$type' '$id' '$host' '$ip' linear"); foreach ($r as $x) {
else system("php propagate.php '$type' '$id' '$host' 'ip' > /dev/null 2>&1 &"); // encode the hostname in base58 and sanitize the IP to avoid any second order shell injections
} $host = base58_encode($x['hostname']);
exit; $ip = filter_var($x['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
// fork a new process to send the blocks async
if ($debug) {
system("php propagate.php '$type' '$id' '$host' '$ip' debug");
} elseif ($linear) {
system("php propagate.php '$type' '$id' '$host' '$ip' linear");
} else {
system("php propagate.php '$type' '$id' '$host' 'ip' > /dev/null 2>&1 &");
}
}
exit;
} }
// broadcast a block to a single peer (usually a forked process from above)
// broadcast a block to a single peer (usually a forked process from above) if ($type == "block") {
if($type=="block"){ // current block or read cache
// current block or read cache if ($id == "current") {
if($id=="current"){ $current = $block->current();
$current=$block->current(); $data = $block->export($current['id']);
$data=$block->export($current['id']); if (!$data) {
if(!$data) { echo "Invalid Block data"; exit; } echo "Invalid Block data";
} else { exit;
$data=file_get_contents("tmp/$id"); }
if(empty($data)) { echo "Invalid Block data"; exit; } } else {
$data=json_decode($data,true); $data = file_get_contents("tmp/$id");
} if (empty($data)) {
$hostname=base58_decode($peer); echo "Invalid Block data";
// send the block as POST to the peer exit;
echo "Block sent to $hostname:\n"; }
$response= peer_post($hostname."/peer.php?q=submitBlock",$data,60, $debug); $data = json_decode($data, true);
if($response=="block-ok") { echo "Block $i accepted. Exiting.\n"; exit;} }
elseif($response['request']=="microsync"){ $hostname = base58_decode($peer);
// the peer requested us to send more blocks, as it's behind // send the block as POST to the peer
echo "Microsync request\n"; echo "Block sent to $hostname:\n";
$height=intval($response['height']); $response = peer_post($hostname."/peer.php?q=submitBlock", $data, 60, $debug);
$bl=san($response['block']); if ($response == "block-ok") {
$current=$block->current(); echo "Block $i accepted. Exiting.\n";
// maximum microsync is 10 blocks, for more, the peer should sync by sanity exit;
if($current['height']-$height>10) { echo "Height Differece too high\n"; exit; } } elseif ($response['request'] == "microsync") {
$last_block=$block->get($height); // the peer requested us to send more blocks, as it's behind
// if their last block does not match our blockchain/fork, ignore the request echo "Microsync request\n";
if ($last_block['id'] != $bl ) { echo "Last block does not match\n"; exit; } $height = intval($response['height']);
echo "Sending the requested blocks\n"; $bl = san($response['block']);
//start sending the requested block $current = $block->current();
for($i=$height+1;$i<=$current['height'];$i++){ // maximum microsync is 10 blocks, for more, the peer should sync by sanity
$data=$block->export("",$i); if ($current['height'] - $height > 10) {
$response = peer_post($hostname."/peer.php?q=submitBlock",$data,60,$debug); echo "Height Differece too high\n";
if($response!="block-ok") { echo "Block $i not accepted. Exiting.\n"; exit;} exit;
echo "Block\t$i\t accepted\n"; }
} $last_block = $block->get($height);
// if their last block does not match our blockchain/fork, ignore the request
} elseif($response=="reverse-microsanity"){ if ($last_block['id'] != $bl) {
// the peer informe us that we should run a microsanity echo "Last block does not match\n";
echo "Running microsanity\n"; exit;
$ip=trim($argv[4]); }
$ip=filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE); echo "Sending the requested blocks\n";
if(empty($ip)) die("Invalid IP"); //start sending the requested block
// fork a microsanity in a new process for ($i = $height + 1; $i <= $current['height']; $i++) {
system("php sanity.php microsanity '$ip' > /dev/null 2>&1 &"); $data = $block->export("", $i);
} $response = peer_post($hostname."/peer.php?q=submitBlock", $data, 60, $debug);
else echo "Block not accepted!\n"; if ($response != "block-ok") {
echo "Block $i not accepted. Exiting.\n";
exit;
}
echo "Block\t$i\t accepted\n";
}
} elseif ($response == "reverse-microsanity") {
// the peer informe us that we should run a microsanity
echo "Running microsanity\n";
$ip = trim($argv[4]);
$ip = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
if (empty($ip)) {
die("Invalid IP");
}
// fork a microsanity in a new process
system("php sanity.php microsanity '$ip' > /dev/null 2>&1 &");
} else {
echo "Block not accepted!\n";
}
} }
// broadcast a transaction to some peers // broadcast a transaction to some peers
if($type=="transaction"){ if ($type == "transaction") {
$trx = new Transaction;
// get the transaction data
$data = $trx->export($id);
$trx=new Transaction; if (!$data) {
// get the transaction data echo "Invalid transaction id\n";
$data=$trx->export($id); exit;
}
if(!$data){ echo "Invalid transaction id\n"; exit; } // if the transaction was first sent locally, we will send it to all our peers, otherwise to just a few
// if the transaction was first sent locally, we will send it to all our peers, otherwise to just a few if ($data['peer'] == "local") {
if($data['peer']=="local") $r=$db->run("SELECT hostname FROM peers WHERE blacklisted < UNIX_TIMESTAMP()"); $r = $db->run("SELECT hostname FROM peers WHERE blacklisted < UNIX_TIMESTAMP()");
else $r=$db->run("SELECT hostname FROM peers WHERE blacklisted < UNIX_TIMESTAMP() AND reserve=0 ORDER by RAND() LIMIT ".intval($_config['transaction_propagation_peers'])); } else {
foreach($r as $x){ $r = $db->run("SELECT hostname FROM peers WHERE blacklisted < UNIX_TIMESTAMP() AND reserve=0 ORDER by RAND() LIMIT ".intval($_config['transaction_propagation_peers']));
$res= peer_post($x['hostname']."/peer.php?q=submitTransaction",$data); }
if(!$res) echo "Transaction not accepted\n"; foreach ($r as $x) {
else echo "Transaction accepted\n"; $res = peer_post($x['hostname']."/peer.php?q=submitTransaction", $data);
} if (!$res) {
echo "Transaction not accepted\n";
} else {
echo "Transaction accepted\n";
}
}
} }
?>