Browse Source

autosign: improve summary output, ts_autosign: refactor mnemonic entry code

MMGen 5 years ago
parent
commit
85236cd
3 changed files with 54 additions and 50 deletions
  1. 16 16
      mmgen/main_autosign.py
  2. 26 1
      test/test_py_d/common.py
  3. 12 33
      test/test_py_d/ts_autosign.py

+ 16 - 16
mmgen/main_autosign.py

@@ -166,7 +166,7 @@ def do_umount():
 		msg('Unmounting '+mountpoint)
 		subprocess.call(['umount',mountpoint])
 
-def sign_tx_file(txfile):
+def sign_tx_file(txfile,signed_txs):
 	try:
 		g.testnet = False
 		g.coin = 'BTC'
@@ -194,7 +194,7 @@ def sign_tx_file(txfile):
 
 		if txsign(tx,wfs,None,None):
 			tx.write_to_file(ask_write=False)
-			txlist.append(tx)
+			signed_txs.append(tx)
 			return True
 		else:
 			return False
@@ -211,17 +211,21 @@ def sign():
 	unsigned = [os.path.join(tx_dir,f) for f in raw if f[:-6] not in signed]
 
 	if unsigned:
-		fails = 0
+		signed_txs,fails = [],[]
 		for txfile in unsigned:
-			ret = sign_tx_file(txfile)
+			ret = sign_tx_file(txfile,signed_txs)
 			if not ret:
-				fails += 1
+				fails.append(txfile)
 			qmsg('')
 		time.sleep(0.3)
-		n_ok = len(unsigned) - fails
-		msg('{} transaction{} signed'.format(n_ok,suf(n_ok)))
+		msg('{} transaction{} signed'.format(len(signed_txs),suf(signed_txs)))
+		if fails:
+			rmsg('{} transaction{} failed to sign'.format(len(fails),suf(fails)))
+		if signed_txs:
+			print_summary(signed_txs)
 		if fails:
-			ymsg('{} transaction{} failed to sign'.format(fails,suf(fails)))
+			rmsg('{}Failed transactions:'.format('' if opt.full_summary else '\n'))
+			rmsg('  ' + '\n  '.join(sorted(fails)) + '\n')
 		return False if fails else True
 	else:
 		msg('No unsigned transactions')
@@ -246,17 +250,17 @@ def decrypt_wallets():
 	return False if fails else True
 
 
-def print_summary():
+def print_summary(signed_txs):
 
 	if opt.full_summary:
-		bmsg('\nAutosign summary:')
-		for tx in txlist:
+		bmsg('\nAutosign summary:\n')
+		for tx in signed_txs:
 			init_coin(tx.coin,tx.chain == 'testnet')
 			msg_r(tx.format_view(terse=True))
 		return
 
 	body = []
-	for tx in txlist:
+	for tx in signed_txs:
 		non_mmgen = [o for o in tx.outputs if not o.mmid]
 		if non_mmgen:
 			body.append((tx,non_mmgen))
@@ -282,11 +286,7 @@ def do_sign():
 	key_ok = decrypt_wallets()
 	if key_ok:
 		if opt.stealth_led: set_led('busy')
-		global txlist
-		txlist = []
 		ret = sign()
-		print_summary()
-		txlist = []
 		do_umount()
 		set_led(('standby','off','error')[(not ret)*2 or bool(opt.stealth_led)])
 		return ret

+ 26 - 1
test/test_py_d/common.py

@@ -20,7 +20,7 @@
 common.py: Shared routines and data for the test.py test suite
 """
 
-import os,subprocess
+import os,time,subprocess
 from mmgen.common import *
 
 log_file = 'test.py.log'
@@ -168,3 +168,28 @@ def get_label(do_shuffle=False):
 		if do_shuffle: shuffle(labels)
 		label_iter = iter(labels)
 		return next(label_iter)
+
+def stealth_mnemonic_entry(t,mn):
+	wnum = 1
+	max_wordlen = 12
+
+	def get_pad_chars(n):
+		ret = ''
+		for i in range(n):
+			m = int(os.urandom(1).hex(),16) % 32
+			ret += r'123579!@#$%^&*()_+-=[]{}"?/,.<>|'[m]
+		return ret
+
+	for i in range(len(mn)):
+		w = mn[i]
+		if len(w) > 5:
+			w = w + '\n'
+		else:
+			w = get_pad_chars(3 if randbool() else 0) + w[0] + get_pad_chars(3) + w[1:] + get_pad_chars(7)
+			w = w[:max_wordlen+1]
+		em,rm = 'Enter word #{}: ','Repeat word #{}: '
+		ret = t.expect((em.format(wnum),rm.format(wnum-1)))
+		if ret == 0: wnum += 1
+		for j in range(len(w)):
+			t.send(w[j])
+			time.sleep(0.005)

+ 12 - 33
test/test_py_d/ts_autosign.py

@@ -67,29 +67,7 @@ class TestSuiteAutosign(TestSuiteBase):
 			mn_file = dfl_words_file
 			mn = read_from_file(mn_file).strip().split()
 			mn = ['foo'] + mn[:5] + ['realiz','realized'] + mn[5:]
-			wnum = 1
-			max_wordlen = 12
-
-			def get_pad_chars(n):
-				ret = ''
-				for i in range(n):
-					m = int(os.urandom(1).hex(),16) % 32
-					ret += r'123579!@#$%^&*()_+-=[]{}"?/,.<>|'[m]
-				return ret
-
-			for i in range(len(mn)):
-				w = mn[i]
-				if len(w) > 5:
-					w = w + '\n'
-				else:
-					w = get_pad_chars(3 if randbool() else 0) + w[0] + get_pad_chars(3) + w[1:] + get_pad_chars(7)
-					w = w[:max_wordlen+1]
-				em,rm = 'Enter word #{}: ','Repeat word #{}: '
-				ret = t.expect((em.format(wnum),rm.format(wnum-1)))
-				if ret == 0: wnum += 1
-				for j in range(len(w)):
-					t.send(w[j])
-					time.sleep(0.005)
+			stealth_mnemonic_entry(t,mn)
 			wf = t.written_to_file('Autosign wallet')
 			t.ok()
 
@@ -114,13 +92,14 @@ class TestSuiteAutosign(TestSuiteBase):
 					try: os.unlink(target.replace('.rawtx','.sigtx'))
 					except: pass
 
-			# make a bad tx file
-			bad_tx = joinpath(mountpoint,'tx','bad.rawtx')
-			if include_bad_tx and not remove_signed_only:
-				open(bad_tx,'w').write('bad tx data')
-			if not include_bad_tx:
-				try: os.unlink(bad_tx)
-				except: pass
+			# make 2 bad tx files
+			for n in (1,2):
+				bad_tx = joinpath(mountpoint,'tx','bad{}.rawtx'.format(n))
+				if include_bad_tx and not remove_signed_only:
+					open(bad_tx,'w').write('bad tx data')
+				if not include_bad_tx:
+					try: os.unlink(bad_tx)
+					except: pass
 
 		def do_autosign_live(opts,mountpoint,led_opts=[],gen_wallet=True):
 
@@ -155,7 +134,7 @@ class TestSuiteAutosign(TestSuiteBase):
 				omsg(blue(m2))
 				t.expect('{} transactions signed'.format(txcount))
 				if not led_opts:
-					t.expect('1 transaction failed to sign')
+					t.expect('2 transactions failed to sign')
 				t.expect('Waiting')
 
 			do_unmount()
@@ -177,7 +156,7 @@ class TestSuiteAutosign(TestSuiteBase):
 			copy_files(mountpoint,include_bad_tx=True)
 			t = self.spawn('mmgen-autosign',opts+['--full-summary','wait'],extra_desc='(sign - full summary)')
 			t.expect('{} transactions signed'.format(txcount))
-			t.expect('1 transaction failed to sign')
+			t.expect('2 transactions failed to sign')
 			t.expect('Waiting')
 			t.kill(2)
 			t.req_exit_val = 1
@@ -187,7 +166,7 @@ class TestSuiteAutosign(TestSuiteBase):
 			copy_files(mountpoint,remove_signed_only=True)
 			t = self.spawn('mmgen-autosign',opts+['wait'],extra_desc='(sign)')
 			t.expect('{} transactions signed'.format(txcount))
-			t.expect('1 transaction failed to sign')
+			t.expect('2 transactions failed to sign')
 			t.expect('Waiting')
 			t.kill(2)
 			t.req_exit_val = 1