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