-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathBitcoind.php
More file actions
208 lines (174 loc) · 4.81 KB
/
Bitcoind.php
File metadata and controls
208 lines (174 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
namespace RpcTests\Nbobtc;
use Nbobtc\Command\Command;
use Nbobtc\Http\Client;
class Bitcoind
{
const ERROR_STARTUP = -28;
const ERROR_TX_MEMPOOL_CONFLICT = -26;
const ERROR_UNKNOWN_COMMAND = -32601;
/**
* @var string
*/
private $dataDir;
/**
* @var string
*/
private $bitcoind;
/**
* @var Credential
*/
private $credential;
/**
* @var Client
*/
private $client;
private $defaultOptions = [
"daemon" => 1,
"server" => 1,
"regtest" => 1,
];
private $options = [];
/**
* RpcServer constructor.
* @param $bitcoind
* @param string $dataDir
* @param Credential $credential
* @param array $options
*/
public function __construct($bitcoind, $dataDir, Credential $credential, array $options = [])
{
$this->bitcoind = $bitcoind;
$this->dataDir = $dataDir;
$this->credential = $credential;
$this->options = array_merge($options, $this->defaultOptions);
}
/**
* @return string
*/
private function getPidFile()
{
return "{$this->dataDir}/regtest/bitcoind.pid";
}
/**
* @return string
*/
private function getConfigFile()
{
return "{$this->dataDir}/bitcoin.conf";
}
/**
* @param Credential $rpcCredential
*/
private function writeConfigToFile(Credential $rpcCredential)
{
$fd = fopen($this->getConfigFile(), "w");
if (!$fd) {
throw new \RuntimeException("Failed to open bitcoin.conf for writing");
}
$config = array_merge(
$this->options,
$rpcCredential->getConfigArray()
);
$iniConfig = implode("\n", array_map(function ($value, $key) {
return "{$key}={$value}";
}, $config, array_keys($config)));
if (!fwrite($fd, $iniConfig)) {
throw new \RuntimeException("Failed to write to bitcoin.conf");
}
fclose($fd);
}
/**
* Start bitcoind and
* @return void
*/
public function start()
{
if ($this->isRunning()) {
return;
}
$this->writeConfigToFile($this->credential);
$res = 0;
$out = '';
exec(sprintf("%s -datadir=%s", $this->bitcoind, $this->dataDir), $out, $res);
if ($res !== 0) {
throw new \RuntimeException("Failed to start bitcoind: {$this->dataDir}\n");
}
$start = microtime(true);
$limit = 10;
$connected = false;
$conn = new Client($this->credential->getDsn());
do {
try {
$result = json_decode($conn->sendCommand(new Command("getchaintips"))->getBody()->getContents(), true);
if ($result['error'] === null) {
$connected = true;
} else {
if ($result['error']['code'] !== self::ERROR_STARTUP && $result['error']['code'] !== self::ERROR_UNKNOWN_COMMAND) {
throw new \RuntimeException("Unexpected error code during startup: {$result['error']['code']}");
}
sleep(0.2);
}
} catch (\Exception $e) {
sleep(0.2);
}
if (microtime(true) > $start + $limit) {
throw new \RuntimeException("Timeout elapsed, never made connection to bitcoind");
}
} while (!$connected);
}
/**
* Recursive delete of datadir.
* @param string $src
*/
private function recursiveDelete($src)
{
$dir = opendir($src);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
$full = $src . '/' . $file;
if ( is_dir($full) ) {
$this->recursiveDelete($full);
}
else {
unlink($full);
}
}
}
closedir($dir);
rmdir($src);
}
/**
* @return void
*/
public function destroy()
{
if ($this->isRunning()) {
$this->makeClient()->sendCommand(new Command("stop"));
do {
sleep(0.2);
} while($this->isRunning());
$this->recursiveDelete($this->dataDir);
}
}
/**
* @return bool
*/
public function isRunning()
{
return file_exists($this->getPidFile());
}
/**
* @return Client
*/
public function makeClient()
{
if (!$this->isRunning()) {
throw new \RuntimeException("No client, server not running");
}
if (null === $this->client) {
$this->client = new Client($this->credential->getDsn());
}
return $this->client;
}
}