Config API, Part II
This patch completes the implementation of the API, making the entire MMGen
code base usable as a library for external projects. A usage example can be
found in the script `examples/coin-daemon-info.py`.
Testing:
$ test/test.py -e coin_daemon_info
This commit is contained in:
parent
bac47a6a0a
commit
e90e25b2f0
5 changed files with 693 additions and 615 deletions
94
examples/coin-daemon-info.py
Executable file
94
examples/coin-daemon-info.py
Executable file
|
|
@ -0,0 +1,94 @@
|
|||
#!/usr/bin/env python3
|
||||
#
|
||||
# mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
|
||||
# Copyright (C)2013-2023 The MMGen Project <mmgen@tuta.io>
|
||||
# Licensed under the GNU General Public License, Version 3:
|
||||
# https://www.gnu.org/licenses
|
||||
# Public project repositories:
|
||||
# https://github.com/mmgen/mmgen
|
||||
# https://gitlab.com/mmgen/mmgen
|
||||
|
||||
"""
|
||||
examples/coin-daemon-info.py:
|
||||
Get info about multiple running coin daemons.
|
||||
Demonstrates use of the MMGen Config API.
|
||||
"""
|
||||
|
||||
# Instructions
|
||||
#
|
||||
# Testing mode:
|
||||
#
|
||||
# 1) From the MMGen repository root, start the mainnet test suite daemons as follows
|
||||
# (note that openethereum is the default testing daemon for ETH):
|
||||
#
|
||||
# test/start-coin-daemons.py btc ltc eth
|
||||
#
|
||||
# 2) Then run the script as follows:
|
||||
#
|
||||
# PYTHONPATH=. MMGEN_TEST_SUITE=1 examples/coin-daemon-info.py btc ltc eth
|
||||
#
|
||||
# Live mode:
|
||||
#
|
||||
# 1) Start up one or more of bitcoind, litecoind or geth with the standard mainnet ports
|
||||
# and datadirs. For geth, the options ‘--http --http.api=eth,web3’ are required.
|
||||
#
|
||||
# 2) Then run the script as follows:
|
||||
#
|
||||
# PYTHONPATH=. examples/coin-daemon-info.py btc ltc eth
|
||||
|
||||
import sys,os,asyncio
|
||||
|
||||
from mmgen.exception import SocketError
|
||||
from mmgen.cfg import Config
|
||||
from mmgen.rpc import rpc_init
|
||||
from mmgen.util import make_timestr
|
||||
|
||||
async def get_rpc(cfg):
|
||||
try:
|
||||
return await rpc_init(cfg)
|
||||
except SocketError as e:
|
||||
return False
|
||||
|
||||
async def main(coins):
|
||||
|
||||
rpcs = {}
|
||||
cfgs = {}
|
||||
test_suite = os.getenv('MMGEN_TEST_SUITE')
|
||||
base_cfg = Config({'pager':True})
|
||||
|
||||
for coin in coins:
|
||||
cfg_in = {
|
||||
'coin': coin,
|
||||
'test_suite': test_suite,
|
||||
}
|
||||
if coin == 'eth' and not test_suite:
|
||||
cfg_in.update({'daemon_id': 'geth'})
|
||||
cfgs[coin] = Config(cfg_in)
|
||||
rpcs[coin] = await get_rpc(cfgs[coin])
|
||||
|
||||
def gen_output():
|
||||
fs = '{:4} {:7} {:6} {:<5} {:<8} {:30} {:13} {:23} {}'
|
||||
yield fs.format('Coin','Network','Status','Port','Chain','Latest Block','Daemon','Version','Datadir')
|
||||
for coin,rpc in rpcs.items():
|
||||
info = ('Down','-','-','-','-','-','-') if rpc is False else (
|
||||
'Up',
|
||||
rpc.port,
|
||||
rpc.chain,
|
||||
'{:<8} [{}]'.format(rpc.blockcount, make_timestr(rpc.cur_date)),
|
||||
rpc.daemon.coind_name,
|
||||
rpc.daemon_version_str,
|
||||
rpc.daemon.datadir
|
||||
)
|
||||
yield fs.format( coin.upper(), cfgs[coin].network, *info )
|
||||
|
||||
base_cfg._util.stdout_or_pager('\n'.join(gen_output()))
|
||||
|
||||
all_coins = ('btc', 'ltc', 'eth')
|
||||
|
||||
coins = sys.argv[1:]
|
||||
|
||||
if coins and all(coin in all_coins for coin in coins):
|
||||
asyncio.run(main(coins))
|
||||
else:
|
||||
print(f'You must supply one or more of the following coins on the command line:\n {all_coins}')
|
||||
sys.exit(1)
|
||||
Loading…
Add table
Add a link
Reference in a new issue