protocol.py: add warn_trustlevel(), create altcoin classes from init_proto()

This commit is contained in:
The MMGen Project 2022-01-29 11:25:02 +00:00
commit 3aab5cf25c
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
5 changed files with 49 additions and 51 deletions

View file

@ -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

View file

@ -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'):

View file

@ -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 coins {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)

View file

@ -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

View file

@ -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 coins {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