Browse Source

MoneroWalletOps.submit: move error handling to autosign mod

The MMGen Project 1 year ago
parent
commit
86863c72d5
2 changed files with 39 additions and 23 deletions
  1. 32 0
      mmgen/autosign.py
  2. 7 23
      mmgen/xmrwallet.py

+ 32 - 0
mmgen/autosign.py

@@ -39,6 +39,15 @@ class Signable:
 			self.long_desc = getattr(self, 'xmr_desc', self.desc) if 'XMR' in self.parent.coins else self.desc
 			self.name = type(self).__name__
 
+		@property
+		def submitted(self):
+			return self._processed('_submitted', self.subext)
+
+		def _processed(self, attrname, ext):
+			if not hasattr(self, attrname):
+				setattr(self, attrname, tuple(f for f in sorted(self.dir.iterdir()) if f.name.endswith('.'+ext)))
+			return getattr(self, attrname)
+
 		@property
 		def unsigned(self):
 			return self._unprocessed( '_unsigned', self.rawext, self.sigext )
@@ -65,6 +74,29 @@ class Signable:
 				b = '  {}\n'.format('\n  '.join(self.gen_bad_list(sorted(bad_files,key=lambda f: f.name))))
 			))
 
+		def die_wrong_num_txs(self, desc, msg=None, show_dir=False):
+			num_txs = len(getattr(self, desc))
+			die('AutosignTXError', "{m}{a} {b} transaction{c} {d} {e}!".format(
+				m = msg + '\n' if msg else '',
+				a = 'One' if num_txs == 1 else 'More than one' if num_txs else 'No',
+				b = desc,
+				c = suf(num_txs),
+				d = 'already present' if num_txs else 'present',
+				e = f'in ‘{getattr(self.parent, self.dir_name)}’' if show_dir else 'on removable device',
+			))
+
+		def get_unsubmitted(self, desc='unsubmitted'):
+			if len(self.unsubmitted) == 1:
+				return self.unsubmitted[0]
+			else:
+				self.die_wrong_num_txs(desc)
+
+		def get_submitted(self):
+			if len(self.submitted) == 0:
+				self.die_wrong_num_txs('submitted')
+			else:
+				return self.submitted
+
 	class transaction(base):
 		desc = 'transaction'
 		xmr_desc = 'non-Monero transaction'

+ 7 - 23
mmgen/xmrwallet.py

@@ -1761,14 +1761,6 @@ class MoneroWalletOps:
 		def post_mount_action(self):
 			self.tx # trigger an exit if no suitable transaction present
 
-		def die_no_tx(self,desc,num_txs,tx_dir):
-			die('AutosignTXError', "{a} {b} transaction{c} in '{d}'!".format(
-				a = 'More than one' if num_txs else 'No',
-				b = desc,
-				c = suf(num_txs),
-				d = tx_dir,
-			))
-
 		@property
 		def tx(self):
 			if not hasattr(self,'_tx'):
@@ -1780,12 +1772,8 @@ class MoneroWalletOps:
 				fn = Path(uarg.infile)
 			else:
 				from .autosign import Signable
-				t = Signable.xmr_transaction(self.asi)
-				if len(t.unsubmitted) == 1:
-					fn = t.unsubmitted[0]
-				else:
-					self.die_no_tx( 'unsubmitted', len(t.unsubmitted), t.parent.xmr_tx_dir )
-			return MoneroMMGenTX.ColdSigned( cfg=self.cfg, fn=fn )
+				fn = Signable.xmr_transaction(self.asi).get_unsubmitted()
+			return MoneroMMGenTX.ColdSigned(cfg=self.cfg, fn=fn)
 
 		def get_relay_rpc(self):
 
@@ -1859,16 +1847,12 @@ class MoneroWalletOps:
 				die(1,'--autosign is required for this operation')
 
 		def get_tx(self):
-			files = [f for f in self.asi.xmr_tx_dir.iterdir()
-						if f.name.endswith('.'+MoneroMMGenTX.Submitted.ext)]
-			txs = sorted(
-				(MoneroMMGenTX.Submitted( self.cfg, Path(fn) ) for fn in files),
+			from .autosign import Signable
+			fns = Signable.xmr_transaction(self.asi).get_submitted()
+			return sorted(
+				(MoneroMMGenTX.Submitted(self.cfg, Path(fn)) for fn in fns),
 					key = lambda x: getattr(x.data,'submit_time',None) or x.data.create_time
-			)
-			if txs:
-				return txs[-1]
-			else:
-				self.die_no_tx('submitted', 0, self.asi.xmr_tx_dir)
+			)[-1]
 
 	class dump(wallet):
 		wallet_offline = True