|
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\Structure\NetworkAddress; |
|
8
|
|
|
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressInterface; |
|
9
|
|
|
use BitWasp\Buffertools\Buffer; |
|
10
|
|
|
use BitWasp\Buffertools\BufferInterface; |
|
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 BufferInterface |
|
58
|
|
|
*/ |
|
59
|
|
|
private $localServices; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
private $userAgent; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param bool $optRelay |
|
68
|
|
|
* @return $this |
|
69
|
|
|
*/ |
|
70
|
|
|
public function requestTxRelay($optRelay = true) |
|
71
|
|
|
{ |
|
72
|
|
|
if (!is_bool($optRelay)) { |
|
73
|
|
|
throw new \InvalidArgumentException('Invalid txrelay setting, must be a boolean'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$this->txRelay = $optRelay; |
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param int $blockHeight |
|
82
|
|
|
* @return $this |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setBestBlockHeight($blockHeight) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->bestBlockHeight = $blockHeight; |
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param callable $callable |
|
92
|
|
|
* @return $this |
|
93
|
|
|
*/ |
|
94
|
|
|
public function setBestBlockHeightCallback(callable $callable) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->bestBlockHeightCallback = $callable; |
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param int $version |
|
102
|
|
|
* @return $this |
|
103
|
|
|
*/ |
|
104
|
|
|
public function setProtocolVersion($version) |
|
105
|
|
|
{ |
|
106
|
|
|
$this->protocolVersion = $version; |
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param string $ip |
|
112
|
|
|
* @return $this |
|
113
|
|
|
*/ |
|
114
|
|
|
public function setLocalIp($ip) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->localIp = $ip; |
|
117
|
|
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param int $port |
|
122
|
|
|
* @return $this |
|
123
|
|
|
*/ |
|
124
|
|
|
public function setLocalPort($port) |
|
125
|
|
|
{ |
|
126
|
|
|
$this->localPort = $port; |
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param BufferInterface $services |
|
132
|
|
|
* @return $this |
|
133
|
|
|
*/ |
|
134
|
|
|
public function setLocalServices(BufferInterface $services) |
|
135
|
|
|
{ |
|
136
|
|
|
$this->localServices = $services; |
|
137
|
|
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param NetworkAddressInterface $networkAddress |
|
142
|
|
|
* @return $this |
|
143
|
|
|
*/ |
|
144
|
|
|
public function setLocalNetAddr(NetworkAddressInterface $networkAddress) |
|
145
|
|
|
{ |
|
146
|
|
|
return $this |
|
147
|
|
|
->setLocalIp($networkAddress->getIp()) |
|
148
|
|
|
->setLocalPort($networkAddress->getPort()) |
|
149
|
|
|
->setLocalServices($networkAddress->getServices()); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param int $timestamp |
|
154
|
|
|
* @return $this |
|
155
|
|
|
*/ |
|
156
|
|
|
public function setTimestamp($timestamp) |
|
157
|
|
|
{ |
|
158
|
|
|
$this->timestamp = $timestamp; |
|
159
|
|
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param $string |
|
164
|
|
|
* @return $this |
|
165
|
|
|
*/ |
|
166
|
|
|
public function setUserAgent($string) |
|
167
|
|
|
{ |
|
168
|
|
|
if (!is_string($string)) { |
|
169
|
|
|
throw new \InvalidArgumentException('User agent must be a string'); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
$this->userAgent = new Buffer($string); |
|
|
|
|
|
|
173
|
|
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @param MsgFactory $messageFactory |
|
178
|
|
|
* @param NetworkAddressInterface $remoteAddress |
|
179
|
|
|
* @return Version |
|
180
|
|
|
*/ |
|
181
|
|
|
public function produceVersion(MsgFactory $messageFactory, NetworkAddressInterface $remoteAddress) |
|
182
|
|
|
{ |
|
183
|
|
|
$protocolVersion = is_null($this->protocolVersion) ? $this->defaultProtocolVersion : $this->protocolVersion; |
|
184
|
|
|
$localServices = is_null($this->localServices) ? new Buffer('', 8) : $this->localServices; |
|
185
|
|
|
$timestamp = is_null($this->timestamp) ? time() : $this->timestamp; |
|
186
|
|
|
$localAddr = new NetworkAddress( |
|
187
|
|
|
$localServices, |
|
188
|
|
|
is_null($this->localIp) ? $this->defaultLocalIp : $this->localIp, |
|
189
|
|
|
is_null($this->localPort) ? $this->defaultLocalPort : $this->localPort |
|
190
|
|
|
); |
|
191
|
|
|
|
|
192
|
|
|
$userAgent = new Buffer(is_null($this->userAgent) ? $this->defaultUserAgent : $this->userAgent); |
|
193
|
|
|
|
|
194
|
|
|
if (is_callable($this->bestBlockHeightCallback)) { |
|
195
|
|
|
$cb = $this->bestBlockHeightCallback; |
|
196
|
|
|
$bestHeight = $cb(); |
|
197
|
|
|
} elseif (!is_null($this->bestBlockHeight)) { |
|
198
|
|
|
$bestHeight = $this->bestBlockHeight; |
|
199
|
|
|
} else { |
|
200
|
|
|
$bestHeight = $this->defaultBlockHeight; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$relay = is_null($this->txRelay) ? $this->defaultTxRelay : $this->txRelay; |
|
204
|
|
|
|
|
205
|
|
|
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..