示例#1
0
 public function __construct()
 {
     parent::__construct(function () {
         $consensus = new ConsensusFactory(Bitcoin::getEcAdapter());
         $loop = LoopFactory::create();
         $context = new ZmqContext($loop);
         $control = $context->getSocket(\ZMQ::SOCKET_SUB);
         $control->connect('tcp://127.0.0.1:5594');
         $control->subscribe('control');
         $control->on('messages', function ($msg) use($loop) {
             if ($msg[1] == 'shutdown') {
                 $loop->stop();
             }
         });
         $results = $context->getSocket(\ZMQ::SOCKET_PUSH);
         $results->connect("tcp://127.0.0.1:5593");
         $workers = $context->getSocket(\ZMQ::SOCKET_PULL);
         $workers->connect('tcp://127.0.0.1:5592');
         $workers->on('message', function ($message) use($consensus, $results) {
             $details = json_decode($message, true);
             $txid = $details['txid'];
             $flags = $details['flags'];
             $vin = $details['vin'];
             $scriptPubKey = new Script(Buffer::hex($details['scriptPubKey']));
             $tx = TransactionFactory::fromHex($details['tx']);
             $results->send(json_encode(['txid' => $txid, 'vin' => $vin, 'result' => $consensus->getConsensus(new Flags($flags))->verify($tx, $scriptPubKey, $vin)]));
         });
         $loop->run();
         exit(0);
     });
 }
示例#2
0
 public function __construct()
 {
     parent::__construct(function () {
         $loop = LoopFactory::create();
         $context = new ZmqContext($loop);
         $socket = $context->getSocket(\ZMQ::SOCKET_REP);
         $socket->bind("tcp://127.0.0.1:5591");
         $workers = $context->getSocket(\ZMQ::SOCKET_PUSH);
         $workers->bind('tcp://127.0.0.1:5592');
         $sub = $context->getSocket(\ZMQ::SOCKET_SUB);
         $sub->connect('tcp://127.0.0.1:5594');
         $sub->subscribe('control');
         $sub->on('messages', function ($msg) use($loop) {
             if ($msg[1] == 'shutdown') {
                 $loop->stop();
             }
         });
         /**
          * @var \React\Promise\Deferred[]
          */
         $deferredSet = [];
         $results = [];
         $socket->on('message', function ($message) use($socket, $workers, &$results, &$deferredSet) {
             // Incoming work. Distribute.
             $payload = json_decode($message, true);
             $batch = [];
             $work = ["txid" => $payload['txid'], "tx" => $payload['tx'], "flags" => $payload['flags'], "vin" => null, "scriptPubKey" => null];
             // Send to workers, and create a Promise for each result.
             foreach ($payload['scripts'] as $vin => $scriptPubKey) {
                 $work['vin'] = $vin;
                 $work['scriptPubKey'] = $scriptPubKey;
                 $deferred = new \React\Promise\Deferred();
                 $deferredSet[$payload['txid'] . $vin] = $deferred;
                 $batch[] = $deferred->promise();
                 $workers->send(json_encode($work));
             }
             // Once all promises have resolved, return outcome to socket.
             \React\Promise\all($batch)->then(function ($results) use($socket) {
                 $final = true;
                 foreach ($results as $result) {
                     $final &= $result['result'];
                 }
                 $socket->send($final);
             });
         });
         $results = $context->getSocket(\ZMQ::SOCKET_PULL);
         $results->bind("tcp://127.0.0.1:5593");
         $results->on('message', function ($message) use(&$deferredSet) {
             $payload = json_decode($message, true);
             $deferredSet[$payload['txid'] . $payload['vin']]->resolve($payload);
         });
         $loop->run();
         exit(0);
     });
 }