Example #1
0
 public function testConnectingToPeerRequestingRelay()
 {
     $loop = new \React\EventLoop\StreamSelectLoop();
     $factory = new \BitWasp\Bitcoin\Networking\Factory($loop);
     $peerFactory = $factory->getPeerFactory($factory->getDns());
     $locator = $peerFactory->getLocator();
     $manager = $peerFactory->getManager(true);
     $deferred = new Deferred();
     $onInv = function (Peer $peer, Inv $inv) use($deferred, $loop) {
         foreach ($inv->getItems() as $item) {
             if ($item->isTx()) {
                 $peer->close();
                 $deferred->resolve($item);
             }
         }
     };
     $onSeeds = function (Locator $locator) use($manager, $deferred, $onInv, $loop) {
         for ($i = 0; $i < 8; $i++) {
             $manager->connectNextPeer($locator)->then(function (Peer $peer) use($onInv) {
                 $peer->on('inv', $onInv);
             }, function ($err) use($loop) {
                 $loop->stop();
             });
         }
     };
     $locator->queryDnsSeeds(1)->then($onSeeds, function ($err) use($loop) {
         $loop->stop();
     });
     $receivedTx = false;
     $deferred->promise()->then(function () use(&$receivedTx) {
         $receivedTx = true;
     })->always(function () use($loop) {
         $loop->stop();
     });
     $loop->run();
     $this->assertTrue($receivedTx);
 }