From 3aab5cf25c3856dadeb06eea6278fb4ab65750c6 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Sat, 29 Jan 2022 11:25:02 +0000 Subject: [PATCH] protocol.py: add warn_trustlevel(), create altcoin classes from init_proto() --- mmgen/altcoin.py | 4 ---- mmgen/opts.py | 14 ++------------ mmgen/protocol.py | 49 +++++++++++++++++++++++++++++++++++++++++++---- mmgen/tool/api.py | 7 ++----- mmgen/util.py | 26 ------------------------- 5 files changed, 49 insertions(+), 51 deletions(-) diff --git a/mmgen/altcoin.py b/mmgen/altcoin.py index 6058e9d0..984b5a19 100755 --- a/mmgen/altcoin.py +++ b/mmgen/altcoin.py @@ -731,12 +731,8 @@ def init_genonly_altcoins(usr_coin=None,testnet=False): if cinfo.trust_level == -1: raise ValueError(f'{usr_coin.upper()!r}: unsupported (disabled) coin for network {network.upper()}') - trust_level = cinfo.trust_level - create_altcoin_protos(data) - return trust_level - def create_altcoin_protos(data): from .protocol import CoinProtocol diff --git a/mmgen/opts.py b/mmgen/opts.py index b6801a2b..e2f48274 100755 --- a/mmgen/opts.py +++ b/mmgen/opts.py @@ -374,12 +374,6 @@ def init( g.regtest = True g.data_dir = os.path.join(g.data_dir_root,'regtest',g.coin.lower(),('alice','bob')[g.bob]) - if need_proto: - from .altcoin import init_genonly_altcoins - altcoin_trust_level = init_genonly_altcoins( - g.coin, - testnet = g.testnet or g.regtest ) - # === end global var initialization === # # print help screen only after global vars are initialized: @@ -391,8 +385,8 @@ def init( del mmgen.share.Opts.parse_opts if need_proto: - from .util import warn_altcoins - warn_altcoins(g.coin,altcoin_trust_level) + from .protocol import warn_trustlevel + warn_trustlevel(g.coin) die_on_incompatible_opts(g.incompatible_opts) @@ -601,10 +595,6 @@ def check_usr_opts(usr_opts): # Raises an exception if any check fails from .util import ymsg ymsg(f'Adjusting transaction vsize by a factor of {float(val):1.2f}') - def chk_coin(key,val,desc): - from .protocol import CoinProtocol - opt_is_in_list(val.lower(),CoinProtocol.coins,'coin') - # TODO: move this check elsewhere # def chk_rbf(key,val,desc): # if not proto.cap('rbf'): diff --git a/mmgen/protocol.py b/mmgen/protocol.py index f87e9c25..d72f017e 100755 --- a/mmgen/protocol.py +++ b/mmgen/protocol.py @@ -551,11 +551,10 @@ def init_proto(coin=None,testnet=False,regtest=False,network=None,network_id=Non network = 'regtest' if regtest else 'testnet' if testnet else 'mainnet' coin = coin.lower() + if coin not in CoinProtocol.coins: - raise ValueError( - f'{coin.upper()}: not a valid coin for network {network.upper()}\n' - + 'Supported coins: ' - + ' '.join(c.upper() for c in CoinProtocol.coins) ) + from .altcoin import init_genonly_altcoins + init_genonly_altcoins( coin, testnet=testnet ) # raises exception on failure name = CoinProtocol.coins[coin].name proto_name = name + ('' if network == 'mainnet' else network.capitalize()) @@ -572,3 +571,45 @@ def init_proto_from_opts(): testnet = g.testnet, regtest = g.regtest, tokensym = g.token ) + +def warn_trustlevel(coinsym): + + if coinsym.lower() in CoinProtocol.coins: + trust_level = CoinProtocol.coins[coinsym.lower()].trust_level + else: + from .altcoin import CoinInfo + e = CoinInfo.get_entry(coinsym,'mainnet') + trust_level = e.trust_level if e else None + if trust_level in (None,-1): + from .util import die + die(1,f'Coin {coinsym} is not supported by {g.proj_name}') + + if trust_level > 3: + return + + m = """ + Support for coin {c!r} is EXPERIMENTAL. The {p} project + assumes no responsibility for any loss of funds you may incur. + This coin’s {p} testing status: {t} + Are you sure you want to continue? + """ + + from .util import qmsg,fmt,keypress_confirm + from .color import red,yellow,green + + warning = fmt(m).strip().format( + c = coinsym.upper(), + t = { + 0: red('COMPLETELY UNTESTED'), + 1: red('LOW'), + 2: yellow('MEDIUM'), + 3: green('OK'), + }[trust_level], + p = g.proj_name ) + + if g.test_suite: + qmsg(warning) + return + + if not keypress_confirm(warning,default_yes=True): + sys.exit(0) diff --git a/mmgen/tool/api.py b/mmgen/tool/api.py index 354fcd45..162c595f 100755 --- a/mmgen/tool/api.py +++ b/mmgen/tool/api.py @@ -80,11 +80,8 @@ class tool_api( Valid choices for coins: one of the symbols returned by the 'coins' attribute Valid choices for network: 'mainnet','testnet','regtest' """ - from ..protocol import init_proto - from ..altcoin import init_genonly_altcoins - from ..util import warn_altcoins - altcoin_trust_level = init_genonly_altcoins(coinsym,testnet=network in ('testnet','regtest')) - warn_altcoins(coinsym,altcoin_trust_level) + from ..protocol import init_proto,warn_trustlevel + warn_trustlevel(coinsym) self.proto = init_proto(coinsym,network=network) return self.proto diff --git a/mmgen/util.py b/mmgen/util.py index c79d7bbc..bb5deffe 100755 --- a/mmgen/util.py +++ b/mmgen/util.py @@ -207,32 +207,6 @@ def exit_if_mswin(feature): m = capfirst(feature) + ' not supported on the MSWin / MSYS2 platform' ydie(1,m) -def warn_altcoins(coinsym,trust_level): - if trust_level > 3: - return - - tl_str = ( - red('COMPLETELY UNTESTED'), - red('LOW'), - yellow('MEDIUM'), - green('HIGH'), - )[trust_level] - - m = """ - Support for coin {!r} is EXPERIMENTAL. The {pn} project - assumes no responsibility for any loss of funds you may incur. - This coin’s {pn} testing status: {} - Are you sure you want to continue? - """ - m = fmt(m).strip().format(coinsym.upper(),tl_str,pn=g.proj_name) - - if g.test_suite: - qmsg(m) - return - - if not keypress_confirm(m,default_yes=True): - sys.exit(0) - def get_keccak(): from .opts import opt