From 59bffb59b2293996072717323c299594d3914a74 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Sat, 29 Jan 2022 11:25:03 +0000 Subject: [PATCH] CoinProtocol.Base: skip loading of .amt module via 'need_amt=False' --- examples/halving-calculator.py | 2 +- mmgen/main_autosign.py | 2 +- mmgen/main_txcreate.py | 2 +- mmgen/main_txdo.py | 2 +- mmgen/opts.py | 4 +-- mmgen/protocol.py | 42 +++++++++++++++--------- mmgen/regtest.py | 2 +- mmgen/tool/api.py | 2 +- mmgen/tool/common.py | 3 +- mmgen/tool/file.py | 1 + mmgen/tool/rpc.py | 1 + mmgen/txfile.py | 2 +- mmgen/xmrwallet.py | 6 ++-- test/misc/cfg.py | 2 +- test/objattrtest.py | 2 +- test/objattrtest_py_d/oat_btc_mainnet.py | 3 +- test/objtest.py | 2 +- test/objtest_py_d/ot_btc_mainnet.py | 2 +- test/objtest_py_d/ot_btc_testnet.py | 2 +- test/objtest_py_d/ot_ltc_mainnet.py | 2 +- test/objtest_py_d/ot_ltc_testnet.py | 2 +- test/test_py_d/ts_base.py | 2 +- test/test_py_d/ts_ethdev.py | 2 +- test/test_py_d/ts_regtest.py | 2 +- test/tooltest2.py | 2 +- test/unit_tests_d/ut_rpc.py | 2 +- test/unit_tests_d/ut_tx.py | 2 +- test/unit_tests_d/ut_tx_deserialize.py | 2 +- 28 files changed, 58 insertions(+), 44 deletions(-) diff --git a/examples/halving-calculator.py b/examples/halving-calculator.py index 0824a159..0d2c6be2 100755 --- a/examples/halving-calculator.py +++ b/examples/halving-calculator.py @@ -45,7 +45,7 @@ def time_diff_warning(t_diff): async def main(): from mmgen.protocol import init_proto_from_opts - proto = init_proto_from_opts() + proto = init_proto_from_opts(need_amt=True) from mmgen.rpc import rpc_init c = await rpc_init(proto) diff --git a/mmgen/main_autosign.py b/mmgen/main_autosign.py index f2037838..e2dfd10f 100755 --- a/mmgen/main_autosign.py +++ b/mmgen/main_autosign.py @@ -157,7 +157,7 @@ async def check_daemons_running(): coins = ['BTC'] for coin in coins: - proto = init_proto(coin,testnet=g.testnet) + proto = init_proto(coin,testnet=g.testnet,need_amt=True) if proto.sign_mode == 'daemon': vmsg(f'Checking {coin} daemon') try: diff --git a/mmgen/main_txcreate.py b/mmgen/main_txcreate.py index 76cd986a..29250dd0 100755 --- a/mmgen/main_txcreate.py +++ b/mmgen/main_txcreate.py @@ -79,7 +79,7 @@ cmd_args = opts.init(opts_data) async def main(): from .protocol import init_proto_from_opts - proto = init_proto_from_opts() + proto = init_proto_from_opts(need_amt=True) from .tx import MMGenTX from .twctl import TrackingWallet diff --git a/mmgen/main_txdo.py b/mmgen/main_txdo.py index ddaf82a7..0af1b086 100755 --- a/mmgen/main_txdo.py +++ b/mmgen/main_txdo.py @@ -125,7 +125,7 @@ async def main(): from .twctl import TrackingWallet from .protocol import init_proto_from_opts - proto = init_proto_from_opts() + proto = init_proto_from_opts(need_amt=True) tx1 = MMGenTX.New( proto = proto, diff --git a/mmgen/opts.py b/mmgen/opts.py index e2f48274..a2eb726e 100755 --- a/mmgen/opts.py +++ b/mmgen/opts.py @@ -52,7 +52,7 @@ def print_help(po,opts_data,opt_filter): opts_data['code'] = {} from .protocol import init_proto_from_opts - proto = init_proto_from_opts() + proto = init_proto_from_opts(need_amt=True) if getattr(opt,'longhelp',None): opts_data['code']['long_options'] = common_opts_data['code'] @@ -155,7 +155,7 @@ def override_globals_from_cfg_file(ucfg,need_proto): (ns[2:],ns[1]=='testnet') if len(ns) > 2 and ns[1] in ('mainnet','testnet') else (ns[1:],False) ) - cls = type(init_proto(ns[0],tn)) # no instance yet, so override _class_ attr + cls = type(init_proto( ns[0], tn, need_amt=True )) # no instance yet, so override _class_ attr attr = '_'.join(nse) else: cls = g # g is "singleton" instance, so override _instance_ attr diff --git a/mmgen/protocol.py b/mmgen/protocol.py index d72f017e..6677b21d 100755 --- a/mmgen/protocol.py +++ b/mmgen/protocol.py @@ -25,7 +25,6 @@ from collections import namedtuple from .devtools import * from .globalvars import g -from .amt import BTCAmt,LTCAmt,BCHAmt,XMRAmt,ETHAmt parsed_wif = namedtuple('parsed_wif',['sec','pubkey_type','compressed']) parsed_addr = namedtuple('parsed_addr',['bytes','fmt']) @@ -85,7 +84,7 @@ class CoinProtocol(MMGenObject): is_fork_of = None networks = ('mainnet','testnet','regtest') - def __init__(self,coin,name,network,tokensym=None): + def __init__(self,coin,name,network,tokensym=None,need_amt=False): self.coin = coin.upper() self.coin_id = self.coin self.name = name @@ -114,6 +113,15 @@ class CoinProtocol(MMGenObject): from .util import get_keccak self.keccak_256 = get_keccak() + if need_amt: + import mmgen.amt + setattr( self, 'coin_amt', getattr(mmgen.amt,self.coin_amt) ) + fee = getattr(self,'max_tx_fee',None) + setattr( self, 'max_tx_fee', (self.coin_amt(fee) if fee else None) ) + else: + setattr( self, 'coin_amt', None ) + setattr( self, 'max_tx_fee', None ) + @property def dcoin(self): return self.coin @@ -215,8 +223,8 @@ class CoinProtocol(MMGenObject): wif_ver_num = { 'std': '80' } mmtypes = ('L','C','S','B') dfl_mmtype = 'L' - coin_amt = BTCAmt - max_tx_fee = BTCAmt('0.003') + coin_amt = 'BTCAmt' + max_tx_fee = '0.003' sighash_type = 'ALL' block0 = '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f' forks = [ @@ -326,8 +334,8 @@ class CoinProtocol(MMGenObject): _finfo(478559,'000000000000000000651ef99cb9fcbe0dadde1d424bd9f15ff20136191a5eec','BTC',False) ] caps = () - coin_amt = BCHAmt - max_tx_fee = BCHAmt('0.1') + coin_amt = 'BCHAmt' + max_tx_fee = '0.1' ignore_daemon_version = False def pubkey2redeem_script(self,pubkey): raise NotImplementedError @@ -345,8 +353,8 @@ class CoinProtocol(MMGenObject): addr_ver_bytes = { '30': 'p2pkh', '32': 'p2sh', '05': 'p2sh' } # new p2sh ver 0x32 must come first wif_ver_num = { 'std': 'b0' } mmtypes = ('L','C','S','B') - coin_amt = LTCAmt - max_tx_fee = LTCAmt('0.3') + coin_amt = 'LTCAmt' + max_tx_fee = '0.3' base_coin = 'LTC' forks = [] bech32_hrp = 'ltc' @@ -387,8 +395,8 @@ class CoinProtocol(MMGenObject): base_coin = 'ETH' pubkey_type = 'std' # required by DummyWIF - coin_amt = ETHAmt - max_tx_fee = ETHAmt('0.005') + coin_amt = 'ETHAmt' + max_tx_fee = '0.005' chain_names = ['ethereum','foundation'] sign_mode = 'standalone' caps = ('token',) @@ -441,7 +449,7 @@ class CoinProtocol(MMGenObject): class EthereumClassic(Ethereum): chain_names = ['classic','ethereum_classic'] - max_tx_fee = ETHAmt('0.005') + max_tx_fee = '0.005' ignore_daemon_version = False class EthereumClassicTestnet(EthereumClassic): @@ -503,7 +511,7 @@ class CoinProtocol(MMGenObject): privkey_len = 32 mmcaps = ('key','addr') ignore_daemon_version = False - coin_amt = XMRAmt + coin_amt = 'XMRAmt' def preprocess_key(self,sec,pubkey_type): # reduce key from .ed25519 import l @@ -534,7 +542,7 @@ class CoinProtocol(MMGenObject): class MoneroTestnet(Monero): # use stagenet for testnet addr_ver_bytes = { '18': 'monero', '24': 'monero_sub' } # testnet is ('35','3f') -def init_proto(coin=None,testnet=False,regtest=False,network=None,network_id=None,tokensym=None): +def init_proto(coin=None,testnet=False,regtest=False,network=None,network_id=None,tokensym=None,need_amt=False): assert type(testnet) == bool, 'init_proto_chk1' assert type(regtest) == bool, 'init_proto_chk2' @@ -563,14 +571,16 @@ def init_proto(coin=None,testnet=False,regtest=False,network=None,network_id=Non coin = coin, name = name, network = network, - tokensym = tokensym ) + tokensym = tokensym, + need_amt = need_amt ) -def init_proto_from_opts(): +def init_proto_from_opts(need_amt=False): return init_proto( coin = g.coin, testnet = g.testnet, regtest = g.regtest, - tokensym = g.token ) + tokensym = g.token, + need_amt = need_amt ) def warn_trustlevel(coinsym): diff --git a/mmgen/regtest.py b/mmgen/regtest.py index 9d24d008..d4e9edd2 100755 --- a/mmgen/regtest.py +++ b/mmgen/regtest.py @@ -75,7 +75,7 @@ class MMGenRegtest(MMGenObject): assert self.coin in self.coins, f'{coin!r}: invalid coin for regtest' from .daemon import CoinDaemon - self.proto = init_proto(self.coin,regtest=True) + self.proto = init_proto(self.coin,regtest=True,need_amt=True) self.d = CoinDaemon(self.coin+'_rt',test_suite=g.test_suite) async def generate(self,blocks=1,silent=False): diff --git a/mmgen/tool/api.py b/mmgen/tool/api.py index 162c595f..ae5ee5d9 100755 --- a/mmgen/tool/api.py +++ b/mmgen/tool/api.py @@ -82,7 +82,7 @@ class tool_api( """ from ..protocol import init_proto,warn_trustlevel warn_trustlevel(coinsym) - self.proto = init_proto(coinsym,network=network) + self.proto = init_proto(coinsym,network=network,need_amt=True) return self.proto @property diff --git a/mmgen/tool/common.py b/mmgen/tool/common.py index 53476220..9f2d38d0 100755 --- a/mmgen/tool/common.py +++ b/mmgen/tool/common.py @@ -27,6 +27,7 @@ class tool_cmd_base: need_proto = False need_addrtype = False + need_amt = False def __init__(self,cmdname=None,proto=None,mmtype=None): @@ -35,7 +36,7 @@ class tool_cmd_base: self.proto = proto else: from ..protocol import init_proto_from_opts - self.proto = init_proto_from_opts() + self.proto = init_proto_from_opts(need_amt=self.need_amt) from ..globalvars import g if g.token: self.proto.tokensym = g.token.upper() diff --git a/mmgen/tool/file.py b/mmgen/tool/file.py index 48ffc37c..4a9e14c5 100755 --- a/mmgen/tool/file.py +++ b/mmgen/tool/file.py @@ -26,6 +26,7 @@ class tool_cmd(tool_cmd_base): "utilities for viewing/checking MMGen address and transaction files" need_proto = True + need_amt = True # for txview def _file_chksum(self,mmgen_addrfile,objname): from ..opts import opt diff --git a/mmgen/tool/rpc.py b/mmgen/tool/rpc.py index 1334129f..2c5a81a5 100755 --- a/mmgen/tool/rpc.py +++ b/mmgen/tool/rpc.py @@ -27,6 +27,7 @@ class tool_cmd(tool_cmd_base): "tracking wallet commands using the JSON-RPC interface" need_proto = True + need_amt = True async def daemon_version(self): "print coin daemon version" diff --git a/mmgen/txfile.py b/mmgen/txfile.py index 3e6c87a4..c1ffe321 100755 --- a/mmgen/txfile.py +++ b/mmgen/txfile.py @@ -106,7 +106,7 @@ class MMGenTxFile: network = CoinProtocol.Base.chain_name_to_network(coin,tx.chain) desc = 'initialization of protocol' - tx.proto = init_proto(coin,network=network) + tx.proto = init_proto(coin,network=network,need_amt=True) if tokensym: tx.proto.tokensym = tokensym diff --git a/mmgen/xmrwallet.py b/mmgen/xmrwallet.py index 24fdf79e..d8b67903 100755 --- a/mmgen/xmrwallet.py +++ b/mmgen/xmrwallet.py @@ -171,7 +171,7 @@ class MoneroMMGenTX: def __init__(self,*args,**kwargs): assert not args, 'Non-keyword args not permitted' d = namedtuple('kwargs_tuple',kwargs)(**kwargs) - proto = init_proto('xmr',network=d.network) + proto = init_proto( 'xmr', network=d.network, need_amt=True ) now = int(time.time()) self.data = self.xmrwallet_tx_data( op = d.op, @@ -196,7 +196,7 @@ class MoneroMMGenTX: self.fn = fn d_wrap = json.loads(get_data_from_file(fn))['MoneroMMGenTX'] d = self.xmrwallet_tx_data(**d_wrap['data']) - proto = init_proto('xmr',network=d.network) + proto = init_proto( 'xmr', network=d.network, need_amt=True ) self.data = self.xmrwallet_tx_data( op = d.op, create_time = d.create_time, @@ -265,7 +265,7 @@ class MoneroWalletOps: cls.check_uopts(self) id_cur = id(cls.check_uopts) - self.proto = init_proto('xmr',testnet=g.testnet) + self.proto = init_proto( 'xmr', testnet=g.testnet, need_amt=True ) def check_uopts(self): diff --git a/test/misc/cfg.py b/test/misc/cfg.py index 806e07ca..57e5d0a3 100755 --- a/test/misc/cfg.py +++ b/test/misc/cfg.py @@ -22,7 +22,7 @@ if cmd_args: msg('usr cfg: {}'.format( ' '.join(f'{i.name}={i.value}' for i in pu) )) elif cmd_args[0] == 'coin_specific_vars': from mmgen.protocol import init_proto_from_opts - proto = init_proto_from_opts() + proto = init_proto_from_opts(need_amt=True) for varname in cmd_args[1:]: msg('{}.{}: {}'.format( type(proto).__name__, diff --git a/test/objattrtest.py b/test/objattrtest.py index fca3de7a..dcafc40c 100755 --- a/test/objattrtest.py +++ b/test/objattrtest.py @@ -161,5 +161,5 @@ def do_loop(): test_object(test_data,obj) from mmgen.protocol import init_proto_from_opts -proto = init_proto_from_opts() +proto = init_proto_from_opts(need_amt=True) do_loop() diff --git a/test/objattrtest_py_d/oat_btc_mainnet.py b/test/objattrtest_py_d/oat_btc_mainnet.py index 365dc407..08704f06 100755 --- a/test/objattrtest_py_d/oat_btc_mainnet.py +++ b/test/objattrtest_py_d/oat_btc_mainnet.py @@ -10,8 +10,9 @@ test.objattrtest_py_d.oat_btc_mainnet: from .oat_common import * from mmgen.protocol import init_proto +from mmgen.amt import BTCAmt -proto = init_proto('btc') +proto = init_proto('btc',need_amt=True) sample_objs.update({ 'PrivKey': PrivKey(proto,seed_bin,compressed=True,pubkey_type='std'), diff --git a/test/objtest.py b/test/objtest.py index 27309568..004a641a 100755 --- a/test/objtest.py +++ b/test/objtest.py @@ -201,5 +201,5 @@ def do_loop(): ) from mmgen.protocol import init_proto_from_opts -proto = init_proto_from_opts() +proto = init_proto_from_opts(need_amt=True) do_loop() diff --git a/test/objtest_py_d/ot_btc_mainnet.py b/test/objtest_py_d/ot_btc_mainnet.py index 0b77c1d4..789cb689 100755 --- a/test/objtest_py_d/ot_btc_mainnet.py +++ b/test/objtest_py_d/ot_btc_mainnet.py @@ -14,7 +14,7 @@ from mmgen.key import * from .ot_common import * from mmgen.protocol import init_proto -proto = init_proto('btc') +proto = init_proto('btc',need_amt=True) tw_pfx = proto.base_coin.lower() + ':' ssm = str(SeedShareCount.max_val) diff --git a/test/objtest_py_d/ot_btc_testnet.py b/test/objtest_py_d/ot_btc_testnet.py index 40d2fe87..22d8ff7d 100755 --- a/test/objtest_py_d/ot_btc_testnet.py +++ b/test/objtest_py_d/ot_btc_testnet.py @@ -11,7 +11,7 @@ from mmgen.obj import * from .ot_common import * from mmgen.protocol import init_proto -proto = init_proto('btc',network='testnet') +proto = init_proto('btc',network='testnet',need_amt=True) tests = { 'CoinAddr': { diff --git a/test/objtest_py_d/ot_ltc_mainnet.py b/test/objtest_py_d/ot_ltc_mainnet.py index 22e34750..1fefb87f 100755 --- a/test/objtest_py_d/ot_ltc_mainnet.py +++ b/test/objtest_py_d/ot_ltc_mainnet.py @@ -11,7 +11,7 @@ from mmgen.obj import * from .ot_common import * from mmgen.protocol import init_proto -proto = init_proto('ltc') +proto = init_proto('ltc',need_amt=True) tests = { 'LTCAmt': { diff --git a/test/objtest_py_d/ot_ltc_testnet.py b/test/objtest_py_d/ot_ltc_testnet.py index d815553d..9eface1d 100755 --- a/test/objtest_py_d/ot_ltc_testnet.py +++ b/test/objtest_py_d/ot_ltc_testnet.py @@ -11,7 +11,7 @@ from mmgen.obj import * from .ot_common import * from mmgen.protocol import init_proto -proto = init_proto('ltc',network='testnet') +proto = init_proto('ltc',network='testnet',need_amt=True) tests = { 'CoinAddr': { diff --git a/test/test_py_d/ts_base.py b/test/test_py_d/ts_base.py index 24801055..5d1c4873 100755 --- a/test/test_py_d/ts_base.py +++ b/test/test_py_d/ts_base.py @@ -37,7 +37,7 @@ class TestSuiteBase(object): def __init__(self,trunner,cfgs,spawn): from mmgen.protocol import init_proto_from_opts - self.proto = init_proto_from_opts() + self.proto = init_proto_from_opts(need_amt=True) self.tr = trunner self.cfgs = cfgs self.spawn = spawn diff --git a/test/test_py_d/ts_ethdev.py b/test/test_py_d/ts_ethdev.py index a883bcfb..c5c1a569 100755 --- a/test/test_py_d/ts_ethdev.py +++ b/test/test_py_d/ts_ethdev.py @@ -324,7 +324,7 @@ class TestSuiteEthdev(TestSuiteBase,TestSuiteShared): self.erase_input = Ctrl_U if opt.pexpect_spawn else '' from mmgen.protocol import init_proto - self.proto = init_proto(g.coin,network='regtest') + self.proto = init_proto(g.coin,network='regtest',need_amt=True) from mmgen.daemon import CoinDaemon self.rpc_port = CoinDaemon(proto=self.proto,test_suite=True).rpc_port self.using_solc = check_solc_ver() diff --git a/test/test_py_d/ts_regtest.py b/test/test_py_d/ts_regtest.py index 0cbb1a95..5c6b2bb3 100755 --- a/test/test_py_d/ts_regtest.py +++ b/test/test_py_d/ts_regtest.py @@ -265,7 +265,7 @@ class TestSuiteRegtest(TestSuiteBase,TestSuiteShared): return if self.proto.testnet: die(2,'--testnet and --regtest options incompatible with regtest test suite') - self.proto = init_proto(self.proto.coin,network='regtest') + self.proto = init_proto(self.proto.coin,network='regtest',need_amt=True) coin = self.proto.coin.lower() for k in rt_data: globals()[k] = rt_data[k][coin] if coin in rt_data[k] else None diff --git a/test/tooltest2.py b/test/tooltest2.py index 4922557c..f5db4cc1 100755 --- a/test/tooltest2.py +++ b/test/tooltest2.py @@ -971,7 +971,7 @@ cmd_args = opts.init( }) from mmgen.protocol import init_proto_from_opts -proto = init_proto_from_opts() +proto = init_proto_from_opts(need_amt=True) if opt.tool_api: del tests['Wallet'] diff --git a/test/unit_tests_d/ut_rpc.py b/test/unit_tests_d/ut_rpc.py index 74119aa6..cb36fb5b 100755 --- a/test/unit_tests_d/ut_rpc.py +++ b/test/unit_tests_d/ut_rpc.py @@ -126,7 +126,7 @@ class unit_tests: test_suite = True, wallet_dir = 'test/trash2', passwd = 'ut_rpc_passw0rd' ) - ) for proto in (init_proto('xmr',network=network) for network in networks) ] + ) for proto in (init_proto( 'xmr', network=network ) for network in networks) ] for md,wd in daemons: if not opt.no_daemon_autostart: diff --git a/test/unit_tests_d/ut_tx.py b/test/unit_tests_d/ut_tx.py index 411df5ce..58be5dd4 100755 --- a/test/unit_tests_d/ut_tx.py +++ b/test/unit_tests_d/ut_tx.py @@ -19,7 +19,7 @@ class unit_tests: d.start() async def do(): - proto = init_proto('btc') + proto = init_proto('btc',need_amt=True) tx = MMGenTX.New(proto=proto) tx.rpc = await rpc_init(proto=proto) diff --git a/test/unit_tests_d/ut_tx_deserialize.py b/test/unit_tests_d/ut_tx_deserialize.py index 26fbed17..e5fcefbe 100755 --- a/test/unit_tests_d/ut_tx_deserialize.py +++ b/test/unit_tests_d/ut_tx_deserialize.py @@ -105,7 +105,7 @@ class unit_test(object): for e in data: if type(e[0]) == list: await test_tx( - tx_proto = init_proto('btc'), + tx_proto = init_proto('btc',need_amt=True), tx_hex = e[1], desc = desc, n = n )