public function testRequestFactory() { $factory = new RequestFactory(); $id = 909; $result = ['a', 'b', 'c']; $response = $factory->response(json_encode(['id' => 909, 'result' => $result])); $this->assertInstanceOf('BitWasp\\Stratum\\Request\\Response', $response); $this->assertEquals($id, $response->getId()); $this->assertEquals($result, $response->getResult()); }
public function testRequestFactoryParse() { $factory = new RequestFactory(); $id = 909; $error = 'Unknown method'; $e = $factory->response(json_encode(['id' => 909, 'error' => $error])); $this->assertEquals($id, $e->getId()); $this->assertEquals($error, $e->getMessage()); $written = json_encode(['id' => $id, 'error' => $error]) . "\n"; $this->assertEquals($written, $e->write()); }
public function testRequestFactoryParse() { $factory = new RequestFactory(); $id = 909; $method = 'this.method'; $params = ['a', 'b', 'c']; /** @var Request $request */ $request = $factory->response(json_encode(['id' => 909, 'method' => $method, 'params' => $params])); $this->assertInstanceOf('BitWasp\\Stratum\\Request\\Request', $request); $this->assertEquals($id, $request->getId()); $this->assertEquals($method, $request->getMethod()); $this->assertEquals($params, $request->getParams()); }
/** * @param string $data * @throws \BitWasp\Stratum\Exception\ApiError * @throws \Exception */ public function onMessage($data) { $response = $this->factory->response($data); if (isset($this->deferred[$response->getId()])) { $this->deferred[$response->getId()]->resolve($response); } else { $this->emit('message', [$response]); if ($response instanceof Request) { $params = $response->getParams(); switch ($response->getMethod()) { case ElectrumClient::HEADERS_SUBSCRIBE: if (!isset($params[0])) { throw new \RuntimeException('Headers notification missing body'); } $header = $params[0]; if (count($header) !== 8) { throw new \RuntimeException('Headers notification missing parameter'); } $this->emit(ElectrumClient::HEADERS_SUBSCRIBE, [new HeadersNotification($header[0], $header[1], $header[2], $header[3], $header[4], $header[5], $header[6], $header[7])]); break; case ElectrumClient::ADDRESS_SUBSCRIBE: if (!isset($params[0]) || !isset($params[1])) { throw new \RuntimeException('Address notification missing address/txid'); } $this->emit(ElectrumClient::ADDRESS_SUBSCRIBE, [new AddressNotification($params[0], $params[1])]); break; case ElectrumClient::NUMBLOCKS_SUBSCRIBE: if (!isset($params[0])) { throw new \RuntimeException('Missing notification parameter: height'); } $this->emit(ElectrumClient::NUMBLOCKS_SUBSCRIBE, [new NumBlocksNotification($params[0])]); break; case MiningClient::SET_DIFFICULTY: if (!isset($params[0])) { throw new \RuntimeException('Missing mining difficulty notification parameter'); } $this->emit(MiningClient::SET_DIFFICULTY, [new SetDifficultyNotification($params[0])]); break; case MiningClient::NOTIFY: if (count($params) !== 9) { throw new \RuntimeException('Missing mining notification parameter'); } $this->emit(MiningClient::NOTIFY, [new MiningNotification($params[0], $params[1], $params[2], $params[3], $params[4], $params[5], $params[6], $params[7], $params[8])]); break; } } } }
public function testReturnsResponse() { $loop = new StreamSelectLoop(); $request = new RequestFactory(); $server = new Server($loop); $server->on('connection', function (SocketConnection $connection) use($server, $request) { $connection->on('data', function ($data) use($connection, $request) { $req = $request->response($data); $response = new Response($req->getId(), ['1.0']); $connection->write($response->write()); }); $connection->on('close', function () use($server) { $server->shutdown(); }); }); $server->listen(54323, '127.0.0.1'); $tcp = new TcpConnector($loop); $client = new Client($tcp, $request); $client->connect('127.0.0.1', 54323)->then(function (Connection $connection) use($loop) { $deferred = new Deferred(); $deferred->promise()->then(function ($value) { $this->assertEquals(1, $value); }); $electrum = new ElectrumClient($connection); $electrum->getServerVersion('1.9.6', ' 0.6')->then(function () use($deferred, $connection) { $deferred->resolve(1); $connection->close(); }, function () use($loop) { $loop->stop(); $this->fail(); }); }); $loop->run(); }
/** * @expectedException \Exception * @expectedExceptionMessage Response missing error/params/result */ public function testMalformedResponse() { $factory = new RequestFactory(); $factory->response('{}'); }