-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrouter.php
More file actions
69 lines (55 loc) · 1.9 KB
/
Copy pathrouter.php
File metadata and controls
69 lines (55 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
require "vendor/autoload.php";
use React\EventLoop\Factory as LoopFactory;
use \React\ZMQ\Context as ZmqContext;
$loop = LoopFactory::create();
$context = new ZmqContext($loop);
$deferredSet = [];
$results = [];
$workers = $context->getSocket(\ZMQ::SOCKET_REQ);
$workers->bind('tcp://127.0.0.1:5592');
$workers->on('message', function ($message) use (&$deferredSet) {
$payload = json_decode($message, true);
$deferredSet[$payload['txid'].$payload['vin']]->resolve($payload['result']);
});
$socket = $context->getSocket(\ZMQ::SOCKET_REP);
$socket->bind("tcp://127.0.0.1:6661");
/**
* @var \React\Promise\Deferred[]
*/
$socket->on('message', function ($message) use ($socket, $workers, &$results, &$deferredSet) {
// Incoming work. Distribute.
$payload = json_decode($message, true);
$reqid = $payload['id'];
$batch = [];
$work = [
"txid" => null,
"tx" => null,
"flags" => $payload['flags'],
"vin" => null,
"scriptPubKey" => null
];
// Send to workers, and create a Promise for each result.
foreach ($payload['txs'] as $t) {
$work['txid'] = $t['txid'];
$work['tx'] = $t['tx'];
foreach ($t['scripts'] as $vin => $scriptPubKey) {
$work['vin'] = $vin;
$work['scriptPubKey'] = $scriptPubKey;
$deferred = new \React\Promise\Deferred();
$deferredSet[$t['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, $reqid) {
$final = true;
foreach ($results as $result) {
$final &= $result;
}
$socket->send(json_encode(['txid'=>$reqid, 'result' => $final]));
});
});
$loop->run();