1
|
|
|
<?php |
2
|
|
|
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* We use a wrapper class around BitcoinClient to add |
6
|
|
|
* some basic caching functionality and some debugging |
7
|
|
|
**/ |
8
|
|
|
class BitcoinWrapper extends BitcoinClient { |
9
|
|
|
public function __construct($type, $username, $password, $host, $debug_level, $debug_object, $memcache) { |
10
|
|
|
$this->type = $type; |
11
|
|
|
$this->username = $username; |
12
|
|
|
$this->password = $password; |
13
|
|
|
$this->host = $host; |
14
|
|
|
// $this->debug is already used |
15
|
|
|
$this->oDebug = $debug_object; |
16
|
|
|
$this->memcache = $memcache; |
17
|
|
|
$debug_level > 0 ? $debug_level = true : $debug_level = false; |
18
|
|
|
return parent::__construct($this->type, $this->username, $this->password, $this->host, '', $debug_level); |
|
|
|
|
19
|
|
|
} |
20
|
|
|
/** |
21
|
|
|
* Wrap variouns methods to add caching |
22
|
|
|
**/ |
23
|
|
|
// Caching this, used for each can_connect call |
24
|
|
|
public function getinfo() { |
25
|
|
|
$this->oDebug->append("STA " . __METHOD__, 4); |
26
|
|
|
if ($data = $this->memcache->get(__FUNCTION__)) return $data; |
27
|
|
|
try { |
28
|
|
|
return $this->memcache->setCache(__FUNCTION__, parent::getnetworkinfo()+parent::getmininginfo()+parent::getwalletinfo(), 30); |
29
|
|
|
} catch (Exception $e) { |
30
|
|
|
$this->oDebug->append("DEPRECATED : RPC version < 0.16, fallback to `getinfo` RPC call", 2); |
31
|
|
|
return $this->memcache->setCache(__FUNCTION__, parent::getinfo(), 30); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function is_testnet() { |
36
|
|
|
$this->oDebug->append("STA " . __METHOD__, 4); |
37
|
|
|
if ($data = $this->memcache->get(__FUNCTION__)) return $data; |
38
|
|
|
if (!(parent::getblockchaininfo())) |
39
|
|
|
return $this->memcache->setCache(__FUNCTION__, parent::is_testnet(), 30); |
40
|
|
|
else |
41
|
|
|
return $this->memcache->setCache(__FUNCTION__, parent::getblockchaininfo()['chain'] == 'test', 30); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getmininginfo() { |
45
|
|
|
$this->oDebug->append("STA " . __METHOD__, 4); |
46
|
|
|
if ($data = $this->memcache->get(__FUNCTION__)) return $data; |
47
|
|
|
return $this->memcache->setCache(__FUNCTION__, parent::getmininginfo(), 30); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getblockcount() { |
51
|
|
|
$this->oDebug->append("STA " . __METHOD__, 4); |
52
|
|
|
if ($data = $this->memcache->get(__FUNCTION__)) return $data; |
53
|
|
|
return $this->memcache->setCache(__FUNCTION__, parent::getblockcount(), 30); |
54
|
|
|
} |
55
|
|
|
// Wrapper method to get the real main account balance |
56
|
|
|
public function getrealbalance() { |
57
|
|
|
$this->oDebug->append("STA " . __METHOD__, 4); |
58
|
|
|
$aAccounts = []; |
59
|
|
|
|
60
|
|
|
try { |
61
|
|
|
$aAccounts = parent::listaccounts(); |
|
|
|
|
62
|
|
|
} catch (Exception $e) { |
63
|
|
|
if ($e->getCode() == 404) |
64
|
|
|
$aAccounts = array( '*' => parent::getbalance("*") ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Account checks |
68
|
|
|
if (count($aAccounts) == 1) { |
69
|
|
|
// We only have a single account so getbalance will be fine |
70
|
|
|
return parent::getbalance("*"); |
71
|
|
|
} else { |
72
|
|
|
return $aAccounts[0]; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
public function getdifficulty() { |
76
|
|
|
$this->oDebug->append("STA " . __METHOD__, 4); |
77
|
|
|
if ($data = $this->memcache->get(__FUNCTION__)) return $data; |
78
|
|
|
$data = parent::getdifficulty(); |
79
|
|
|
// Check for PoS/PoW coins |
80
|
|
|
if (is_array($data) && array_key_exists('proof-of-work', $data)) |
81
|
|
|
$data = $data['proof-of-work']; |
82
|
|
|
return $this->memcache->setCache(__FUNCTION__, $data, 30); |
83
|
|
|
} |
84
|
|
|
public function getestimatedtime($iCurrentPoolHashrate) { |
85
|
|
|
$this->oDebug->append("STA " . __METHOD__, 4); |
86
|
|
|
if ($iCurrentPoolHashrate == 0) return 0; |
87
|
|
|
if ($data = $this->memcache->get(__FUNCTION__)) return $data; |
88
|
|
|
$dDifficulty = $this->getdifficulty(); |
89
|
|
|
return $this->memcache->setCache(__FUNCTION__, $dDifficulty * pow(2,32) / $iCurrentPoolHashrate, 30); |
90
|
|
|
} |
91
|
|
|
public function getblockchaindownload() { |
92
|
|
|
$aPeerInfo = $this->getpeerinfo(); |
93
|
|
|
$aInfo = $this->getinfo(); |
94
|
|
|
$iStartingHeight = 0; |
95
|
|
|
foreach ($aPeerInfo as $aPeerData) { |
96
|
|
|
if ($iStartingHeight < $aPeerData['startingheight']) $iStartingHeight = $aPeerData['startingheight']; |
97
|
|
|
} |
98
|
|
|
if ($iStartingHeight > $aInfo['blocks']) { |
99
|
|
|
return number_format(round($aInfo['blocks'] / $iStartingHeight * 100, 2), 2); |
100
|
|
|
} else { |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
public function getnetworkhashps() { |
105
|
|
|
$this->oDebug->append("STA " . __METHOD__, 4); |
106
|
|
|
if ($data = $this->memcache->get(__FUNCTION__)) return $data; |
107
|
|
|
try { |
108
|
|
|
$dNetworkHashrate = $this->getmininginfo(); |
109
|
|
|
if (is_array($dNetworkHashrate)) { |
110
|
|
|
if (array_key_exists('networkhashps', $dNetworkHashrate)) { |
111
|
|
|
$dNetworkHashrate = $dNetworkHashrate['networkhashps']; |
112
|
|
|
} else if (array_key_exists('networkmhps', $dNetworkHashrate)) { |
113
|
|
|
$dNetworkHashrate = $dNetworkHashrate['networkmhps'] * 1000 * 1000; |
114
|
|
|
} else if (array_key_exists('networkghps', $dNetworkHashrate)) { |
115
|
|
|
$dNetworkHashrate = $dNetworkHashrate['networkghps'] * 1000 * 1000 * 1000; |
116
|
|
|
} else if (array_key_exists('hashespersec', $dNetworkHashrate)) { |
117
|
|
|
$dNetworkHashrate = $dNetworkHashrate['hashespersec']; |
118
|
|
|
} else if (array_key_exists('netmhashps', $dNetworkHashrate)) { |
119
|
|
|
$dNetworkHashrate = $dNetworkHashrate['netmhashps'] * 1000 * 1000; |
120
|
|
|
} else { |
121
|
|
|
// Unsupported implementation |
122
|
|
|
$dNetworkHashrate = 0; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} catch (Exception $e) { |
126
|
|
|
// getmininginfo does not exist, cache for an hour |
127
|
|
|
return $this->memcache->setCache(__FUNCTION__, 0, 3600); |
128
|
|
|
} |
129
|
|
|
return $this->memcache->setCache(__FUNCTION__, $dNetworkHashrate, 30); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// Load this wrapper |
134
|
|
|
$bitcoin = new BitcoinWrapper($config['wallet']['type'], $config['wallet']['username'], $config['wallet']['password'], $config['wallet']['host'], $config['DEBUG'], $debug, $memcache); |
135
|
|
|
|