diff --git a/include/functions.inc.php b/include/functions.inc.php index 1e5ab78..b84ae2b 100755 --- a/include/functions.inc.php +++ b/include/functions.inc.php @@ -171,7 +171,7 @@ function ec_verify($data, $signature, $key){ } -function peer_post($url, $data=array()){ +function peer_post($url, $data=array(),$timeout=60){ global $_config; $postdata = http_build_query( array( @@ -182,7 +182,7 @@ function peer_post($url, $data=array()){ $opts = array('http' => array( - 'timeout' => "60", + 'timeout' => $timeout, 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata diff --git a/sanity.php b/sanity.php index 5f97aa9..ffb38c3 100755 --- a/sanity.php +++ b/sanity.php @@ -24,6 +24,7 @@ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ set_time_limit(0); +error_reporting(0); if(php_sapi_name() !== 'cli') die("This should only be run as cli"); if(file_exists("tmp/sanity-lock")){ @@ -44,7 +45,6 @@ if($arg!="microsanity") sleep(10); require_once("include/init.inc.php"); - if($_config['dbversion']<2){ die("DB schema not created"); @unlink("tmp/sanity-lock"); @@ -132,6 +132,8 @@ $r=$db->run("SELECT id,hostname FROM peers WHERE reserve=0 AND blacklisted$_config['hostname'], "repeer"=>1)); if($res!==false) {$i++; echo "Peering OK - $peer\n"; } else echo "Peering FAIL - $peer\n"; @@ -168,6 +172,10 @@ foreach($r as $x){ $i=0; foreach($data as $peer){ + $pid=md5($peer); + if($peered[$pid]==1) continue; + $peered[$pid]=1; + if($peer['hostname']==$_config['hostname']) continue; if (!filter_var($peer['hostname'], FILTER_VALIDATE_URL)) continue; @@ -176,7 +184,7 @@ foreach($r as $x){ if($i>$_config['max_test_peers']) break; $peer['hostname'] = filter_var($peer['hostname'], FILTER_SANITIZE_URL); - $test=peer_post($peer['hostname']."/peer.php?q=peer",array("hostname"=>$_config['hostname'])); + $test=peer_post($peer['hostname']."/peer.php?q=peer",array("hostname"=>$_config['hostname']),20); if($test!==false){ $total_peers++; echo "Peered with: $peer[hostname]\n"; @@ -248,14 +256,43 @@ if($current['height']<$largest_height&&$largest_height>1){ $data=peer_post($url."getBlock",array("height"=>$current['height'])); if($data===false){ _log("Could not get block from $host - $current[height]"); continue; } - if($data['id']==$most_common&&($most_common_size/$total_active_peers)>0.90){ - if($data['id']!=$current['id']){ + if($data['id']!=$current['id']&&$data['id']==$most_common&&($most_common_size/$total_active_peers)>0.90){ $block->delete($current['height']-3); $current=$block->current(); $data=peer_post($url."getBlock",array("height"=>$current['height'])); if($data===false){_log("Could not get block from $host - $current[height]"); break; } - } + + }elseif($data['id']!=$current['id']&&$data['id']!=$most_common){ + $invalid=false; + $last_good=$current['height']; + for($i=$current['height']-10;$i<$current['height'];$i++){ + $data=peer_post($url."getBlock",array("height"=>$i)); + if($data===false){ $invalid=true; break; } + $ext=$block->get($i); + if($i==$current['height']-10&&$ext['id']!=$data['id']){ $invalid=true; break; } + + if($ext['id']==$data['id']) $last_good=$i; + + } + if($invalid==false) { + $cblock=array(); + for($i=$last_good;$i<=$largest_height;$i++){ + $data=peer_post($url."getBlock",array("height"=>$i)); + if($data===false){ $invalid=true; break; } + $cblock[$i]=$data; + } + + for($i=$last_good+1;$i<=$largest_height;$i++){ + if(!$block->mine($cblock[$i]['public_key'], $cblock[$i]['nonce'], $cblock[$i]['argon'], $cblock[$i]['difficulty'], $cblock[$i-1]['id'])) {$invalid=true; break; } + } + } + if($invalid==false){ + $block->delete($last_good); + $current=$block->current(); + $data=$current; + } + } if($data['id']!=$current['id']) continue; while($current['height']<$largest_height){ diff --git a/util.php b/util.php index 05a0c88..488a997 100755 --- a/util.php +++ b/util.php @@ -121,6 +121,12 @@ echo "Mempool size: $res\n"; } else echo "$x[hostname] ->ok \n"; } +}elseif($cmd=="peers-block"){ + $r=$db->run("SELECT * FROM peers"); + foreach($r as $x){ + $a=peer_post($x['hostname']."/peer.php?q=currentBlock",array(),5); + echo "$x[hostname]\t$a[height]\n"; + } } else {