1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Networking\Peer; |
6
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Networking\Ip\IpInterface; |
8
|
|
|
use BitWasp\Bitcoin\Networking\Ip\Ipv4; |
9
|
|
|
use BitWasp\Bitcoin\Networking\Messages\Factory as MsgFactory; |
10
|
|
|
use BitWasp\Bitcoin\Networking\Messages\Version; |
11
|
|
|
use BitWasp\Bitcoin\Networking\Services; |
12
|
|
|
use BitWasp\Bitcoin\Networking\Structure\NetworkAddress; |
13
|
|
|
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressInterface; |
14
|
|
|
use BitWasp\Buffertools\Buffer; |
15
|
|
|
use BitWasp\Buffertools\BufferInterface; |
16
|
|
|
|
17
|
|
|
class ConnectionParams |
18
|
|
|
{ |
19
|
|
|
protected $defaultUserAgent = 'bitcoin-php'; |
20
|
|
|
protected $defaultProtocolVersion = 70000; |
21
|
|
|
protected $defaultTxRelay = false; |
22
|
|
|
protected $defaultBlockHeight = 0; |
23
|
|
|
protected $defaultLocalIp = '0.0.0.0'; |
24
|
|
|
protected $defaultLocalPort = 0; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int|null |
28
|
|
|
*/ |
29
|
|
|
private $protocolVersion; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int|null |
33
|
|
|
*/ |
34
|
|
|
private $timestamp; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var bool|null |
38
|
|
|
*/ |
39
|
|
|
private $txRelay; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var callable|null |
43
|
|
|
*/ |
44
|
|
|
private $bestBlockHeightCallback; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var int|null |
48
|
|
|
*/ |
49
|
|
|
private $bestBlockHeight; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var IpInterface|null |
53
|
|
|
*/ |
54
|
|
|
private $localIp; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var int|null |
58
|
|
|
*/ |
59
|
|
|
private $localPort; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var int|null |
63
|
|
|
*/ |
64
|
|
|
private $localServices; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var BufferInterface|null |
68
|
|
|
*/ |
69
|
|
|
private $userAgent; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var int |
73
|
|
|
*/ |
74
|
|
|
private $requiredServices = 0; |
75
|
|
|
|
76
|
|
|
/** |
77
|
6 |
|
* @param bool $optRelay |
78
|
|
|
* @return $this |
79
|
6 |
|
*/ |
80
|
3 |
|
public function requestTxRelay(bool $optRelay = true) |
81
|
|
|
{ |
82
|
|
|
$this->txRelay = $optRelay; |
83
|
3 |
|
return $this; |
84
|
3 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param int $blockHeight |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function setBestBlockHeight(int $blockHeight) |
91
|
6 |
|
{ |
92
|
|
|
$this->bestBlockHeight = $blockHeight; |
93
|
6 |
|
return $this; |
94
|
6 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param callable $callable |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function setBestBlockHeightCallback(callable $callable) |
101
|
3 |
|
{ |
102
|
|
|
$this->bestBlockHeightCallback = $callable; |
103
|
3 |
|
return $this; |
104
|
3 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param int $version |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function setProtocolVersion(int $version) |
111
|
3 |
|
{ |
112
|
|
|
$this->protocolVersion = $version; |
113
|
3 |
|
return $this; |
114
|
3 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param IpInterface $ip |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
|
|
public function setLocalIp(IpInterface $ip) |
121
|
9 |
|
{ |
122
|
|
|
$this->localIp = $ip; |
123
|
9 |
|
return $this; |
124
|
9 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param int $port |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
|
|
public function setLocalPort(int $port) |
131
|
6 |
|
{ |
132
|
|
|
$this->localPort = $port; |
133
|
6 |
|
return $this; |
134
|
6 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param int $services |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
|
|
public function setLocalServices(int $services) |
141
|
6 |
|
{ |
142
|
|
|
$this->localServices = $services; |
143
|
6 |
|
return $this; |
144
|
6 |
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param NetworkAddressInterface $networkAddress |
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
|
|
public function setLocalNetAddr(NetworkAddressInterface $networkAddress) |
151
|
3 |
|
{ |
152
|
|
|
// @todo: just set net addr? |
153
|
2 |
|
$this->setLocalIp($networkAddress->getIp()); |
154
|
3 |
|
$this->setLocalPort($networkAddress->getPort()); |
155
|
3 |
|
$this->setLocalServices($networkAddress->getServices()); |
156
|
3 |
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param int $timestamp |
161
|
|
|
* @return $this |
162
|
|
|
*/ |
163
|
3 |
|
public function setTimestamp(int $timestamp) |
164
|
|
|
{ |
165
|
3 |
|
$this->timestamp = $timestamp; |
166
|
3 |
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param int $services |
171
|
|
|
* @return $this |
172
|
|
|
*/ |
173
|
|
|
public function setRequiredServices(int $services) |
174
|
|
|
{ |
175
|
|
|
$this->requiredServices = $services; |
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return int |
181
|
|
|
*/ |
182
|
9 |
|
public function getRequiredServices(): int |
183
|
|
|
{ |
184
|
9 |
|
return $this->requiredServices; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param string $string |
189
|
|
|
* @return $this |
190
|
|
|
*/ |
191
|
6 |
|
public function setUserAgent(string $string) |
192
|
|
|
{ |
193
|
6 |
|
$this->userAgent = new Buffer($string); |
194
|
3 |
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
3 |
|
/** |
198
|
3 |
|
* @param MsgFactory $messageFactory |
199
|
|
|
* @param NetworkAddress $remoteAddress |
200
|
|
|
* @return Version |
201
|
|
|
*/ |
202
|
|
|
public function produceVersion(MsgFactory $messageFactory, NetworkAddress $remoteAddress): Version |
203
|
|
|
{ |
204
|
|
|
$protocolVersion = is_null($this->protocolVersion) ? $this->defaultProtocolVersion : $this->protocolVersion; |
205
|
|
|
$localServices = is_null($this->localServices) ? Services::NONE : $this->localServices; |
206
|
45 |
|
$timestamp = is_null($this->timestamp) ? time() : $this->timestamp; |
207
|
|
|
$localAddr = new NetworkAddress( |
208
|
45 |
|
$localServices, |
209
|
45 |
|
is_null($this->localIp) ? new Ipv4($this->defaultLocalIp) : $this->localIp, |
210
|
45 |
|
is_null($this->localPort) ? $this->defaultLocalPort : $this->localPort |
211
|
45 |
|
); |
212
|
30 |
|
|
213
|
45 |
|
$userAgent = is_null($this->userAgent) ? new Buffer($this->defaultUserAgent) : $this->userAgent; |
214
|
45 |
|
|
215
|
30 |
|
if (is_callable($this->bestBlockHeightCallback)) { |
216
|
|
|
$cb = $this->bestBlockHeightCallback; |
217
|
45 |
|
$bestHeight = $cb(); |
218
|
|
|
} elseif (!is_null($this->bestBlockHeight)) { |
219
|
45 |
|
$bestHeight = $this->bestBlockHeight; |
220
|
3 |
|
} else { |
221
|
3 |
|
$bestHeight = $this->defaultBlockHeight; |
222
|
44 |
|
} |
223
|
6 |
|
|
224
|
4 |
|
$relay = is_null($this->txRelay) ? $this->defaultTxRelay : $this->txRelay; |
225
|
36 |
|
|
226
|
|
|
return $messageFactory->version($protocolVersion, $localServices, $timestamp, $remoteAddress, $localAddr, $userAgent, $bestHeight, $relay); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|