Browse Source

CoinProtocol.Base: skip loading of .amt module via 'need_amt=False'

The MMGen Project 3 years ago
parent
commit
59bffb59b2

+ 1 - 1
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)

+ 1 - 1
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:

+ 1 - 1
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

+ 1 - 1
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,

+ 2 - 2
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

+ 26 - 16
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):
 

+ 1 - 1
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):

+ 1 - 1
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

+ 2 - 1
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()

+ 1 - 0
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

+ 1 - 0
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"

+ 1 - 1
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
 

+ 3 - 3
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):
 

+ 1 - 1
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__,

+ 1 - 1
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()

+ 2 - 1
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'),

+ 1 - 1
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()

+ 1 - 1
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)

+ 1 - 1
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': {

+ 1 - 1
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': {

+ 1 - 1
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': {

+ 1 - 1
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

+ 1 - 1
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()

+ 1 - 1
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

+ 1 - 1
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']

+ 1 - 1
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:

+ 1 - 1
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)
 

+ 1 - 1
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 )