Révision 9a2a8138
bitcoind_: return support for older Bitcoin Core versions
Refactor to take Bitcoin Core minor version into consideration,
and use that to implement legacy version support.
| plugins/currency/bitcoin/bitcoind_ | ||
|---|---|---|
| 63 | 63 |
DEBUG = os.getenv('MUNIN_DEBUG') == '1'
|
| 64 | 64 |
|
| 65 | 65 |
|
| 66 |
def _get_version(info): |
|
| 67 |
# v0.15.2 version is represented as 150200 |
|
| 68 |
return info['version'] // 10000 |
|
| 69 |
|
|
| 70 |
|
|
| 71 |
def _rpc_get_initial_info(connection): |
|
| 72 |
(info, connect_error) = connection.getnetworkinfo() |
|
| 73 |
if connect_error: |
|
| 74 |
if isinstance(connect_error, urllib.error.HTTPError) and connect_error.code == 404: |
|
| 75 |
# getinfo RPC exists in version <= 0.15 |
|
| 76 |
(info, connect_error) = connection.getinfo() |
|
| 77 |
if connect_error: |
|
| 78 |
return (None, None, connect_error) |
|
| 79 |
else: |
|
| 80 |
return (None, None, connect_error) # pass all other not-404 errors |
|
| 81 |
|
|
| 82 |
return (info, _get_version(info), None) |
|
| 83 |
|
|
| 84 |
|
|
| 85 |
def _rpc_get_balance(info, minor_version, connection): |
|
| 86 |
# see https://github.com/bitcoin/bitcoin/blob/239d199667888e5d60309f15a38eed4d3afe56c4/ |
|
| 87 |
# doc/release-notes/release-notes-0.19.0.1.md#new-rpcs |
|
| 88 |
if minor_version >= 19: |
|
| 89 |
# we use getbalance*s* (plural) as old getbalance is being deprecated, |
|
| 90 |
# and we have to calculate total balance (owned and watch-only) manually now. |
|
| 91 |
(result, error) = connection.getbalances() |
|
| 92 |
|
|
| 93 |
total = sum(result[wallet_mode]['trusted'] |
|
| 94 |
for wallet_mode in ('mine', 'watchonly')
|
|
| 95 |
if wallet_mode in result) |
|
| 96 |
|
|
| 97 |
info['balance'] = total |
|
| 98 |
return info |
|
| 99 |
else: |
|
| 100 |
(result, error) = connection.getbalance() |
|
| 101 |
info['balance'] = result |
|
| 102 |
return info |
|
| 103 |
|
|
| 104 |
|
|
| 66 | 105 |
def main(): |
| 67 | 106 |
# getinfo variable is read from command name - probably the sym-link name. |
| 68 | 107 |
request_var = sys.argv[0].split('_', 1)[1] or 'balance'
|
| ... | ... | |
| 119 | 158 |
bitcoin_options.rpcport), |
| 120 | 159 |
username=bitcoin_options.rpcuser, |
| 121 | 160 |
password=bitcoin_options.rpcpassword) |
| 122 |
(info, connect_error) = bitcoin.getnetworkinfo()
|
|
| 161 |
(info, minor_version, connect_error) = _rpc_get_initial_info(bitcoin)
|
|
| 123 | 162 |
if connect_error: |
| 124 | 163 |
error = "Could not connect to Bitcoin server: {}".format(connect_error)
|
| 125 | 164 |
|
| ... | ... | |
| 135 | 174 |
return False |
| 136 | 175 |
|
| 137 | 176 |
if request_var == 'balance': |
| 138 |
# we use getbalance*s* (plural) as old getbalance is being deprecated, |
|
| 139 |
# and we have to calculate total balance (owned and watch-only) manually now. |
|
| 140 |
(info, error) = bitcoin.getbalances() |
|
| 141 |
|
|
| 142 |
total = sum(info[wallet_mode]['trusted'] |
|
| 143 |
for wallet_mode in ('mine', 'watchonly')
|
|
| 144 |
if wallet_mode in info) |
|
| 145 |
|
|
| 146 |
info['balance'] = total |
|
| 177 |
info = _rpc_get_balance(info, minor_version, bitcoin) |
|
| 147 | 178 |
|
| 148 | 179 |
if request_var in ('transactions', 'block_age'):
|
| 149 | 180 |
(info, error) = bitcoin.getblockchaininfo() |
Formats disponibles : Unified diff