coin protocol: minor cleanups
This commit is contained in:
parent
43432327c7
commit
4204a32311
8 changed files with 36 additions and 32 deletions
|
|
@ -678,10 +678,10 @@ Removed {{}} duplicate WIF key{{}} from keylist (also in {pnm} key-address file
|
|||
al_coin,al_mmtype = None,None
|
||||
tn = lbl[-8:] == ':TESTNET'
|
||||
if tn:
|
||||
assert g.proto.testnet,'{} file is testnet but protocol is mainnet!'.format(self.data_desc)
|
||||
assert g.proto.testnet, f'{self.data_desc} file is testnet but protocol is mainnet!'
|
||||
lbl = lbl[:-8]
|
||||
else:
|
||||
assert not g.proto.testnet,'{} file is mainnet but protocol is testnet!'.format(self.data_desc)
|
||||
assert not g.proto.testnet, f'{self.data_desc} file is mainnet but protocol is testnet!'
|
||||
lbl = lbl.split(':',1)
|
||||
if len(lbl) == 2:
|
||||
al_coin,al_mmtype = lbl[0],lbl[1].lower()
|
||||
|
|
|
|||
14
mmgen/obj.py
14
mmgen/obj.py
|
|
@ -529,7 +529,7 @@ class CoinAddr(str,Hilite,InitErrors,MMGenObject):
|
|||
me.hex = ap.bytes.hex()
|
||||
return me
|
||||
except Exception as e:
|
||||
return cls.init_fail(e,s,objname='{} address'.format(type(g.proto).__name__))
|
||||
return cls.init_fail(e,s,objname=f'{g.proto.cls_name} address')
|
||||
|
||||
@classmethod
|
||||
def fmtc(cls,s,**kwargs):
|
||||
|
|
@ -538,7 +538,7 @@ class CoinAddr(str,Hilite,InitErrors,MMGenObject):
|
|||
|
||||
def is_for_chain(self,chain):
|
||||
|
||||
if type(g.proto).__name__.startswith('Ethereum'):
|
||||
if g.proto.name.startswith('Ethereum'):
|
||||
return True
|
||||
|
||||
from mmgen.protocol import init_proto
|
||||
|
|
@ -621,7 +621,7 @@ class MMGenID(str,Hilite,InitErrors,MMGenObject):
|
|||
me.sid = SeedID(sid=ss[0],on_fail='raise')
|
||||
me.idx = AddrIdx(ss[-1],on_fail='raise')
|
||||
me.mmtype = t
|
||||
assert t in g.proto.mmtypes,'{}: invalid address type for {}'.format(t,type(g.proto).__name__)
|
||||
assert t in g.proto.mmtypes, f'{t}: invalid address type for {g.proto.cls_name}'
|
||||
me.al_id = str.__new__(AddrListID,me.sid+':'+me.mmtype) # checks already done
|
||||
me.sort_key = '{}:{}:{:0{w}}'.format(me.sid,me.mmtype,me.idx,w=me.idx.max_digits)
|
||||
return me
|
||||
|
|
@ -755,8 +755,8 @@ class PrivKey(str,Hilite,InitErrors,MMGenObject):
|
|||
me.wif = str.__new__(WifKey,wif) # check has been done
|
||||
me.orig_hex = None
|
||||
if k.sec != g.proto.preprocess_key(k.sec,k.pubkey_type):
|
||||
m = '{} WIF key {!r} encodes private key with unacceptable value {}'
|
||||
raise PrivateKeyError(m.format(type(g.proto).__name__,me.wif,me))
|
||||
raise PrivateKeyError(
|
||||
f'{g.proto.cls_name} WIF key {me.wif!r} encodes private key with invalid value {me}')
|
||||
return me
|
||||
except Exception as e:
|
||||
return cls.init_fail(e,s,objname='{} WIF key'.format(g.coin))
|
||||
|
|
@ -914,8 +914,8 @@ class MMGenAddrType(str,Hilite,InitErrors,MMGenObject):
|
|||
me = str.__new__(cls,s)
|
||||
for k in v._fields:
|
||||
setattr(me,k,getattr(v,k))
|
||||
assert me in g.proto.mmtypes + ('P',), (
|
||||
"'{}': invalid address type for {}".format(me.name,type(g.proto).__name__))
|
||||
if me not in g.proto.mmtypes + ('P',):
|
||||
raise ValueError(f'{me.name!r}: invalid address type for {g.proto.cls_name}')
|
||||
return me
|
||||
raise ValueError('unrecognized address type')
|
||||
except Exception as e:
|
||||
|
|
|
|||
|
|
@ -88,18 +88,19 @@ class CoinProtocol(MMGenObject):
|
|||
networks = ('mainnet','testnet','regtest')
|
||||
|
||||
def __init__(self,coin,name,network):
|
||||
self.coin = coin.upper()
|
||||
self.dcoin = self.coin # display coin - for Ethereum, is set to ERC20 token name
|
||||
self.name = name
|
||||
self.network = network
|
||||
self.testnet = network in ('testnet','regtest')
|
||||
self.regtest = network == 'regtest'
|
||||
self.coin = coin.upper()
|
||||
self.dcoin = self.coin # display coin - for Ethereum, is set to ERC20 token name
|
||||
self.name = name
|
||||
self.cls_name = type(self).__name__
|
||||
self.network = network
|
||||
self.testnet = network in ('testnet','regtest')
|
||||
self.regtest = network == 'regtest'
|
||||
|
||||
def cap(self,s):
|
||||
return s in self.caps
|
||||
|
||||
class Bitcoin(Common): # chainparams.cpp
|
||||
mod_clsname = 'bitcoin'
|
||||
mod_clsname = 'Bitcoin'
|
||||
daemon_name = 'bitcoind'
|
||||
daemon_family = 'bitcoind'
|
||||
addr_ver_bytes = { '00': 'p2pkh', '05': 'p2sh' }
|
||||
|
|
@ -342,7 +343,7 @@ class CoinProtocol(MMGenObject):
|
|||
addr_len = 20
|
||||
mmtypes = ('E',)
|
||||
dfl_mmtype = 'E'
|
||||
mod_clsname = 'ethereum'
|
||||
mod_clsname = 'Ethereum'
|
||||
base_coin = 'ETH'
|
||||
pubkey_type = 'std' # required by DummyWIF
|
||||
|
||||
|
|
|
|||
|
|
@ -330,7 +330,8 @@ class BitcoinRPCClient(RPCClient,metaclass=aInitMeta):
|
|||
|
||||
async def check_chainfork_mismatch(block0):
|
||||
try:
|
||||
assert block0 == self.proto.block0,'Incorrect Genesis block for {}'.format(type(self.proto).__name__)
|
||||
if block0 != self.proto.block0:
|
||||
raise ValueError(f'Invalid Genesis block for {self.proto.cls_name} protocol')
|
||||
for fork in self.proto.forks:
|
||||
if fork.height == None or self.blockcount < fork.height:
|
||||
break
|
||||
|
|
@ -555,7 +556,7 @@ async def rpc_init(proto=None,backend=None):
|
|||
backend = backend or opt.rpc_backend
|
||||
|
||||
if not 'rpc' in proto.mmcaps:
|
||||
die(1,'Coin daemon operations not supported for {}!'.format(type(proto).__name__))
|
||||
die(1,f'Coin daemon operations not supported for {proto.name} protocol!')
|
||||
|
||||
g.rpc = await {
|
||||
'bitcoind': BitcoinRPCClient,
|
||||
|
|
|
|||
|
|
@ -860,12 +860,13 @@ def format_par(s,indent=0,width=80,as_list=False):
|
|||
|
||||
# module loading magic for tx.py and tw.py
|
||||
def altcoin_subclass(cls,mod_id,cls_name):
|
||||
if cls.__name__ != cls_name: return cls
|
||||
if cls.__name__ != cls_name:
|
||||
return cls
|
||||
mod_dir = g.proto.base_coin.lower()
|
||||
tname = 'Token' if g.token else ''
|
||||
import importlib
|
||||
modname = 'mmgen.altcoins.{}.{}'.format(mod_dir,mod_id)
|
||||
clsname = '{}{}{}'.format(capfirst(g.proto.mod_clsname),tname,cls_name)
|
||||
modname = f'mmgen.altcoins.{mod_dir}.{mod_id}'
|
||||
clsname = g.proto.mod_clsname + tname + cls_name
|
||||
try:
|
||||
return getattr(importlib.import_module(modname),clsname)
|
||||
except ImportError:
|
||||
|
|
|
|||
15
test/test.py
15
test/test.py
|
|
@ -23,7 +23,7 @@ test/test.py: Test suite for the MMGen wallet system
|
|||
def check_segwit_opts():
|
||||
for k,m in (('segwit','S'),('segwit_random','S'),('bech32','B')):
|
||||
if getattr(opt,k) and m not in g.proto.mmtypes:
|
||||
die(1,'--{} option incompatible with {}'.format(k.replace('_','-'),type(g.proto).__name__))
|
||||
die(1,f'--{k.replace("_","-")} option incompatible with {g.proto.cls_name}')
|
||||
|
||||
def create_shm_dir(data_dir,trash_dir):
|
||||
# Laggy flash media can cause pexpect to fail, so create a temporary directory
|
||||
|
|
@ -696,11 +696,14 @@ class TestSuiteRunner(object):
|
|||
else:
|
||||
segwit_opt = None
|
||||
|
||||
m1 = ('test group {g!r}','{g}:{c}')[bool(cmd)].format(g=gname,c=cmd)
|
||||
m2 = ' for {} {}net'.format(g.coin.lower(),'test' if g.proto.testnet else 'main') \
|
||||
if len(ts_cls.networks) != 1 else ''
|
||||
m3 = ' (--{})'.format(segwit_opt.replace('_','-')) if segwit_opt else ''
|
||||
m = m1 + m2 + m3
|
||||
def gen_msg():
|
||||
yield ('{g}:{c}' if cmd else 'test group {g!r}').format(g=gname,c=cmd)
|
||||
if len(ts_cls.networks) != 1:
|
||||
yield ' for {} {}'.format(g.proto.coin,g.proto.network)
|
||||
if segwit_opt:
|
||||
yield ' (--{})'.format(segwit_opt.replace('_','-'))
|
||||
|
||||
m = ''.join(gen_msg())
|
||||
|
||||
if segwit_opt and not ts_cls.segwit_opts_ok:
|
||||
iqmsg('INFO → skipping ' + m)
|
||||
|
|
|
|||
|
|
@ -147,8 +147,6 @@ class TestSuiteMain(TestSuiteBase,TestSuiteShared):
|
|||
if g.coin.lower() not in self.networks:
|
||||
return
|
||||
from mmgen.rpc import rpc_init
|
||||
if g.proto.regtest: # rpc_init hack
|
||||
g.proto = init_proto(g.coin,network='testnet')
|
||||
self.rpc = run_session(rpc_init())
|
||||
self.lbl_id = ('account','label')['label_api' in self.rpc.caps]
|
||||
if g.coin in ('BTC','BCH','LTC'):
|
||||
|
|
|
|||
|
|
@ -231,12 +231,12 @@ class TestSuiteRef(TestSuiteBase,TestSuiteShared):
|
|||
|
||||
def ref_segwitaddrfile_chk(self):
|
||||
if not 'S' in g.proto.mmtypes:
|
||||
return skip('not supported by {}'.format(type(g.proto).__name__))
|
||||
return skip(f'not supported by {g.protocol.cls_name} protocol')
|
||||
return self.ref_addrfile_chk(ftype='segwitaddr',pat='{}.*Segwit'.format(nw_name))
|
||||
|
||||
def ref_bech32addrfile_chk(self):
|
||||
if not 'B' in g.proto.mmtypes:
|
||||
return skip('not supported by {}'.format(type(g.proto).__name__))
|
||||
return skip(f'not supported by {g.protocol.cls_name} protocol')
|
||||
return self.ref_addrfile_chk(ftype='bech32addr',pat='{}.*Bech32'.format(nw_name))
|
||||
|
||||
def ref_keyaddrfile_chk(self):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue