|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Networking; |
|
6
|
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Bitcoin; |
|
8
|
|
|
use BitWasp\Bitcoin\Crypto\Random\Random; |
|
9
|
|
|
use BitWasp\Bitcoin\Network\NetworkInterface; |
|
10
|
|
|
use BitWasp\Bitcoin\Networking\Ip\IpInterface; |
|
11
|
|
|
use BitWasp\Bitcoin\Networking\Settings\NetworkSettings; |
|
12
|
|
|
use React\Dns\Resolver\Resolver; |
|
13
|
|
|
use React\EventLoop\LoopInterface; |
|
14
|
|
|
|
|
15
|
|
|
class Factory |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var NetworkInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $network; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var LoopInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $loop; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var NetworkSettings |
|
29
|
18 |
|
*/ |
|
30
|
|
|
private $settings; |
|
31
|
18 |
|
|
|
32
|
18 |
|
/** |
|
33
|
18 |
|
* @var Messages\Factory |
|
34
|
|
|
*/ |
|
35
|
|
|
private $messages = null; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param NetworkInterface $network |
|
39
|
18 |
|
* @param LoopInterface $loop |
|
40
|
|
|
*/ |
|
41
|
18 |
|
public function __construct( |
|
42
|
|
|
LoopInterface $loop, |
|
43
|
|
|
NetworkInterface $network = null |
|
44
|
|
|
) { |
|
45
|
|
|
$this->loop = $loop; |
|
46
|
|
|
$this->network = $network ?: Bitcoin::getNetwork(); |
|
47
|
|
|
$this->settings = new Settings\MainnetSettings(); |
|
48
|
12 |
|
} |
|
49
|
|
|
|
|
50
|
12 |
|
/** |
|
51
|
12 |
|
* @return Resolver |
|
52
|
12 |
|
*/ |
|
53
|
8 |
|
public function getDns(): Resolver |
|
54
|
|
|
{ |
|
55
|
|
|
return (new \React\Dns\Resolver\Factory())->create($this->settings->getDnsServer(), $this->loop); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param Random|null $random |
|
60
|
|
|
* @return Messages\Factory |
|
61
|
|
|
*/ |
|
62
|
6 |
|
public function getMessages(Random $random = null): Messages\Factory |
|
63
|
|
|
{ |
|
64
|
6 |
|
if (null === $this->messages) { |
|
65
|
4 |
|
$this->messages = new Messages\Factory( |
|
66
|
4 |
|
$this->network, |
|
67
|
|
|
$random ?: new Random() |
|
68
|
4 |
|
); |
|
69
|
|
|
} |
|
70
|
|
|
return $this->messages; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param Settings\NetworkSettings $settings |
|
75
|
|
|
*/ |
|
76
|
|
|
public function setSettings(Settings\NetworkSettings $settings) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->settings = $settings; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return Settings\NetworkSettings |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getSettings(): Settings\NetworkSettings |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->settings; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param Peer\ConnectionParams $params |
|
91
|
|
|
* @return Peer\Connector |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getConnector( |
|
94
|
|
|
Peer\ConnectionParams $params |
|
95
|
|
|
): Peer\Connector { |
|
96
|
|
|
return new Peer\Connector( |
|
97
|
|
|
$this->getMessages(), |
|
98
|
|
|
$params, |
|
99
|
|
|
$this->loop, |
|
100
|
|
|
$this->settings->getSocketParams() |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param Peer\Connector $connector |
|
106
|
|
|
* @return Peer\Manager |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getManager(Peer\Connector $connector): Peer\Manager |
|
109
|
|
|
{ |
|
110
|
|
|
return new Peer\Manager($connector, $this->settings); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return Peer\Locator |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getLocator(): Peer\Locator |
|
117
|
|
|
{ |
|
118
|
|
|
return new Peer\Locator($this->getDns(), $this->settings); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param Peer\ConnectionParams $params |
|
123
|
|
|
* @param Structure\NetworkAddressInterface $serverAddress |
|
124
|
|
|
* @return Peer\Listener |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getListener( |
|
127
|
|
|
Peer\ConnectionParams $params, |
|
128
|
|
|
Structure\NetworkAddressInterface $serverAddress |
|
129
|
|
|
): Peer\Listener { |
|
130
|
|
|
return new Peer\Listener( |
|
131
|
|
|
$params, |
|
132
|
|
|
$this->getMessages(), |
|
133
|
|
|
$serverAddress, |
|
134
|
|
|
$this->loop |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param IpInterface $ipAddress |
|
140
|
|
|
* @param int $port |
|
141
|
|
|
* @param int $services |
|
142
|
|
|
* @return Structure\NetworkAddress |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getAddress( |
|
145
|
|
|
IpInterface $ipAddress, |
|
146
|
|
|
int $port = null, |
|
147
|
|
|
int $services = Services::NONE |
|
148
|
|
|
): Structure\NetworkAddress { |
|
149
|
|
|
if (null === $port) { |
|
150
|
|
|
$port = $this->settings->getDefaultP2PPort(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return new Structure\NetworkAddress($services, $ipAddress, $port); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|