|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\RpcTest; |
|
4
|
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Network\NetworkFactory; |
|
6
|
|
|
use BitWasp\Bitcoin\Network\NetworkInterface; |
|
7
|
|
|
use BitWasp\Bitcoin\Script\Interpreter\Interpreter; |
|
8
|
|
|
use Nbobtc\Command\Command; |
|
9
|
|
|
|
|
10
|
|
|
abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $rpcHost; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $rpcPort; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $rpcUser; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $rpcPass; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var \Nbobtc\Http\Client |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $client; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
private $scriptFlagNames; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var NetworkInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $network; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* AbstractTestCase constructor. |
|
49
|
|
|
* @param null $name |
|
50
|
|
|
* @param array $data |
|
51
|
|
|
* @param string $dataName |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct($name = null, array $data = [], $dataName = '') |
|
54
|
|
|
{ |
|
55
|
|
|
$this->initRpcConfig(); |
|
56
|
|
|
parent::__construct($name, $data, $dataName); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
protected function initRpcConfig() |
|
60
|
|
|
{ |
|
61
|
|
|
if (!($host = getenv("RPC_TEST_HOST"))) { |
|
62
|
|
|
throw new \RuntimeException("Missing RPC_TEST_HOST from environment"); |
|
63
|
|
|
} |
|
64
|
|
|
if (!($port = getenv("RPC_TEST_PORT"))) { |
|
65
|
|
|
throw new \RuntimeException("Missing RPC_TEST_PORT from environment"); |
|
66
|
|
|
} |
|
67
|
|
|
if (!($user = getenv("RPC_TEST_USERNAME"))) { |
|
68
|
|
|
throw new \RuntimeException("Missing RPC_TEST_USERNAME from environment"); |
|
69
|
|
|
} |
|
70
|
|
|
if (!($pass = getenv("RPC_TEST_PASSWORD"))) { |
|
71
|
|
|
throw new \RuntimeException("Missing RPC_TEST_PASSWORD from environment"); |
|
72
|
|
|
} |
|
73
|
|
|
if (!($networkName = getenv("RPC_NETWORK"))) { |
|
74
|
|
|
throw new \RuntimeException("Missing RPC_NETWORK from environment"); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if ($networkName === "bitcoin") { |
|
78
|
|
|
$this->network = NetworkFactory::bitcoin(); |
|
79
|
|
|
} else if ($networkName === "bitcoinTestnet") { |
|
80
|
|
|
$this->network = NetworkFactory::bitcoinTestnet(); |
|
81
|
|
|
} else { |
|
82
|
|
|
throw new \RuntimeException("Unconfigured network"); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$this->rpcHost = $host; |
|
86
|
|
|
$this->rpcPort = $port; |
|
87
|
|
|
$this->rpcUser = $user; |
|
88
|
|
|
$this->rpcPass = $pass; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function getRpcDsn() |
|
95
|
|
|
{ |
|
96
|
|
|
return "http://{$this->rpcUser}:{$this->rpcPass}@{$this->rpcHost}:{$this->rpcPort}"; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return \Nbobtc\Http\Client |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function getRpcClient() |
|
103
|
|
|
{ |
|
104
|
|
|
if (null === $this->client) { |
|
105
|
|
|
$this->client = new \Nbobtc\Http\Client($this->getRpcDsn()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $this->client; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param string $command |
|
113
|
|
|
* @param array $params |
|
114
|
|
|
* @return mixed |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function makeRpcRequest($command, array $params = []) |
|
117
|
|
|
{ |
|
118
|
|
|
$unsorted = $this->getRpcClient()->sendCommand(new Command($command, $params)); |
|
119
|
|
|
$jsonResult = $unsorted->getBody()->getContents(); |
|
120
|
|
|
$json = json_decode($jsonResult, true); |
|
121
|
|
|
if (false === $json) { |
|
122
|
|
|
throw new \RuntimeException("Invalid JSON from server"); |
|
123
|
|
|
} |
|
124
|
|
|
return $json; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param string $name |
|
130
|
|
|
* @return array |
|
131
|
|
|
*/ |
|
132
|
|
|
public function jsonDataFile($name) |
|
133
|
|
|
{ |
|
134
|
|
|
$contents = $this->dataFile($name); |
|
135
|
|
|
$decoded = json_decode($contents, true); |
|
136
|
|
|
if (false === $decoded || json_last_error() !== JSON_ERROR_NONE) { |
|
137
|
|
|
throw new \RuntimeException('Invalid JSON file ' . $name); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $decoded; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param string $filename |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
|
|
public function dataFile($filename) |
|
148
|
|
|
{ |
|
149
|
|
|
$contents = file_get_contents($this->dataPath($filename)); |
|
150
|
|
|
if (false === $contents) { |
|
151
|
|
|
throw new \RuntimeException('Failed to data file ' . $filename); |
|
152
|
|
|
} |
|
153
|
|
|
return $contents; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param string $file |
|
158
|
|
|
* @return string |
|
159
|
|
|
*/ |
|
160
|
|
|
public function dataPath($file) |
|
161
|
|
|
{ |
|
162
|
|
|
return __DIR__ . '/../tests/Data/' . $file; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return array |
|
168
|
|
|
*/ |
|
169
|
|
|
public function calcMapScriptFlags() |
|
170
|
|
|
{ |
|
171
|
|
|
if (null === $this->scriptFlagNames) { |
|
172
|
|
|
$this->scriptFlagNames = [ |
|
173
|
|
|
"NONE" => Interpreter::VERIFY_NONE, |
|
174
|
|
|
"P2SH" => Interpreter::VERIFY_P2SH, |
|
175
|
|
|
"STRICTENC" => Interpreter::VERIFY_STRICTENC, |
|
176
|
|
|
"DERSIG" => Interpreter::VERIFY_DERSIG, |
|
177
|
|
|
"LOW_S" => Interpreter::VERIFY_LOW_S, |
|
178
|
|
|
"SIGPUSHONLY" => Interpreter::VERIFY_SIGPUSHONLY, |
|
179
|
|
|
"MINIMALDATA" => Interpreter::VERIFY_MINIMALDATA, |
|
180
|
|
|
"NULLDUMMY" => Interpreter::VERIFY_NULL_DUMMY, |
|
181
|
|
|
"DISCOURAGE_UPGRADABLE_NOPS" => Interpreter::VERIFY_DISCOURAGE_UPGRADABLE_NOPS, |
|
182
|
|
|
"CLEANSTACK" => Interpreter::VERIFY_CLEAN_STACK, |
|
183
|
|
|
"CHECKLOCKTIMEVERIFY" => Interpreter::VERIFY_CHECKLOCKTIMEVERIFY, |
|
184
|
|
|
"CHECKSEQUENCEVERIFY" => Interpreter::VERIFY_CHECKSEQUENCEVERIFY, |
|
185
|
|
|
"WITNESS" => Interpreter::VERIFY_WITNESS, |
|
186
|
|
|
"DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM" => Interpreter::VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM, |
|
187
|
|
|
"MINIMALIF" => Interpreter::VERIFY_MINIMALIF, |
|
188
|
|
|
"NULLFAIL" => Interpreter::VERIFY_NULLFAIL, |
|
189
|
|
|
]; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return $this->scriptFlagNames; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @param string $string |
|
197
|
|
|
* @return int |
|
198
|
|
|
*/ |
|
199
|
|
|
public function getScriptFlagsFromString($string) |
|
200
|
|
|
{ |
|
201
|
|
|
$mapFlagNames = $this->calcMapScriptFlags(); |
|
202
|
|
|
if (strlen($string) === 0) { |
|
203
|
|
|
return Interpreter::VERIFY_NONE; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
$flags = 0; |
|
207
|
|
|
$words = explode(",", $string); |
|
208
|
|
|
foreach ($words as $word) { |
|
209
|
|
|
if (!isset($mapFlagNames[$word])) { |
|
210
|
|
|
throw new \RuntimeException('Unknown verification flag: ' . $word); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
$flags |= $mapFlagNames[$word]; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
return $flags; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
} |
|
220
|
|
|
|