diff --git a/include/Exception.php b/include/Exception.php new file mode 100644 index 0000000..68b9b8e --- /dev/null +++ b/include/Exception.php @@ -0,0 +1,11 @@ +peerList = $peerList; + } + + /** + * Retrieve a peer from the initial peer list. + * @return string + * @throws Exception + */ + public function get(): string + { + if (!$this->peerList || count($this->peerList) < self::MINIMUM_PEERS_REQUIRED) { + $this->retrieveFromPreloadList(); + } + + return $this->selectPeer(); + } + + /** + * Retrieve all available initial peers. + * @return array + * @throws Exception + */ + public function getAll(): array + { + if (!$this->peerList || count($this->peerList) < self::MINIMUM_PEERS_REQUIRED) { + $this->retrieveFromPreloadList(); + } + + return $this->peerList; + } + + /** + * @return string + */ + private function selectPeer(): string + { + return $this->peerList[array_rand($this->peerList)]; + } + + /** + * Retrieve a peer from + * + * @return void + * @throws Exception + */ + private function retrieveFromPreloadList(): void + { + $peerList = file(self::PRELOAD_LIST, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); + + if (!$peerList || count($peerList) < self::MINIMUM_PEERS_REQUIRED) { + throw new Exception(self::PRELOAD_ERROR); + } + + $this->peerList = $peerList; + } +} diff --git a/include/config-sample.inc.php b/include/config-sample.inc.php index b0b467c..63f13d4 100755 --- a/include/config-sample.inc.php +++ b/include/config-sample.inc.php @@ -54,6 +54,37 @@ $_config['transaction_propagation_peers'] = 5; // How many new peers to check from each peer $_config['max_test_peers'] = 5; +// The initial peers to sync from in sanity +$_config['initial_peer_list'] = [ + 'http://peer1.arionum.com', + 'http://peer2.arionum.com', + 'http://peer3.arionum.com', + 'http://peer4.arionum.com', + 'http://peer5.arionum.com', + 'http://peer6.arionum.com', + 'http://peer7.arionum.com', + 'http://peer8.arionum.com', + 'http://peer9.arionum.com', + 'http://peer10.arionum.com', + 'http://peer11.arionum.com', + 'http://peer12.arionum.com', + 'http://peer13.arionum.com', + 'http://peer14.arionum.com', + 'http://peer15.arionum.com', + 'http://peer16.arionum.com', + 'http://peer17.arionum.com', + 'http://peer18.arionum.com', + 'http://peer19.arionum.com', + 'http://peer20.arionum.com', + 'http://peer21.arionum.com', + 'http://peer22.arionum.com', + 'http://peer23.arionum.com', + 'http://peer24.arionum.com', + 'http://peer25.arionum.com', + 'http://peer26.arionum.com', + 'http://peer27.arionum.com', +]; + /* |-------------------------------------------------------------------------- | Mempool Configuration @@ -69,7 +100,7 @@ $_config['max_mempool_rebroadcast'] = 5000; // The number of blocks between rebroadcasting transactions $_config['sanity_rebroadcast_height'] = 30; -// Block accepting transfers from addresses blacklisted by the Arionum devs +// Block accepting transfers from addresses blacklisted by the Arionum devs $_config['use_official_blacklist'] = true; /* diff --git a/include/init.inc.php b/include/init.inc.php index a0171eb..b833864 100755 --- a/include/init.inc.php +++ b/include/init.inc.php @@ -13,10 +13,12 @@ if (php_sapi_name() !== 'cli' && substr_count($_SERVER['PHP_SELF'], "/") > 1) { die("This application should only be run in the main directory /"); } +require_once __DIR__.'/Exception.php'; require_once("include/config.inc.php"); require_once("include/db.inc.php"); require_once("include/functions.inc.php"); require_once __DIR__.'/Blacklist.php'; +require_once __DIR__.'/InitialPeers.php'; require_once("include/block.inc.php"); require_once("include/account.inc.php"); require_once("include/transaction.inc.php"); diff --git a/sanity.php b/sanity.php index 736e0cc..ba3fc75 100755 --- a/sanity.php +++ b/sanity.php @@ -60,8 +60,7 @@ if ($arg != "microsanity") { sleep(3); } - -require_once("include/init.inc.php"); +require_once __DIR__.'/include/init.inc.php'; if ($argv[1]=="dev") { error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE); @@ -267,16 +266,20 @@ $peered = []; // if we have no peers, get the seed list from the official site if ($total_peers == 0 && $_config['testnet'] == false) { $i = 0; - echo "No peers found. Attempting to get peers from arionum.com\n"; - $f = file("https://www.arionum.com/peers.txt"); - shuffle($f); - // we can't connect to arionum.com - if (count($f) < 2) { - @unlink("tmp/sanity-lock"); - die("Could not connect to arionum.com! Will try later!\n"); + echo 'No peers found. Attempting to get peers from the initial list.'.PHP_EOL; + + $initialPeers = new \Arionum\Node\InitialPeers($_config['initial_peer_list'] ?? []); + + try { + $peers = $initialPeers->getAll(); + } catch (\Arionum\Node\Exception $e) { + @unlink('tmp/sanity-lock'); + die($e->getMessage().PHP_EOL); } - foreach ($f as $peer) { - //peer with all until max_peers, this will ask them to send a peering request to our peer.php where we add their peer to the db. + + foreach ($peers as $peer) { + // Peer with all until max_peers + // This will ask them to send a peering request to our peer.php where we add their peer to the db. $peer = trim(san_host($peer)); $bad_peers = ["127.", "localhost", "10.", "192.168.","172.16.","172.17.","172.18.","172.19.","172.20.","172.21.","172.22.","172.23.","172.24.","172.25.","172.26.","172.27.","172.28.","172.29.","172.30.","172.31."];