Browse Source

mmgen-txsend: support transaction selection with `--status`

Examples (assumes --autosign):

    # View status of last sent transaction:
    $ mmgen-txsend --status

    # Same as above:
    $ mmgen-txsend --status 0

    # View status of next-to-last sent transaction:
    $ mmgen-txsend --status 1

Testing/demo:

    $ test/cmdtest.py -e -X alice_txstatus8 autosign_automount
The MMGen Project 1 month ago
parent
commit
4f1b16f489
4 changed files with 40 additions and 12 deletions
  1. 7 4
      mmgen/autosign.py
  2. 1 1
      mmgen/data/version
  3. 17 5
      mmgen/main_txsend.py
  4. 15 2
      test/cmdtest_d/automount.py

+ 7 - 4
mmgen/autosign.py

@@ -334,19 +334,22 @@ class Signable:
 				shred_file(self.cfg, fn, iterations=15)
 				shred_file(self.cfg, fn, iterations=15)
 			sys.exit(0)
 			sys.exit(0)
 
 
-		async def get_last_sent(self):
+		async def get_last_sent(self, *, idx=0):
 			return await self.get_last_created(
 			return await self.get_last_created(
 				# compat fallback - ‘sent_timestamp’ attr is missing in some old TX files:
 				# compat fallback - ‘sent_timestamp’ attr is missing in some old TX files:
-				sort_key = lambda x: x.sent_timestamp or x.timestamp)
+				sort_key = lambda x: x.sent_timestamp or x.timestamp,
+				idx = idx)
 
 
-		async def get_last_created(self, *, sort_key=lambda x: x.timestamp):
+		async def get_last_created(self, *, sort_key=lambda x: x.timestamp, idx=0):
 			from .tx import CompletedTX
 			from .tx import CompletedTX
 			fns = [f for f in self.dir.iterdir() if f.name.endswith(self.subext)]
 			fns = [f for f in self.dir.iterdir() if f.name.endswith(self.subext)]
 			files = sorted(
 			files = sorted(
 				[await CompletedTX(cfg=self.cfg, filename=str(txfile), quiet_open=True)
 				[await CompletedTX(cfg=self.cfg, filename=str(txfile), quiet_open=True)
 					for txfile in fns],
 					for txfile in fns],
 				key = sort_key)
 				key = sort_key)
-			return files[-1]
+			if not (0 <= idx < len(files)):
+				die(2, f'{idx}: invalid transaction index (must be less than {len(files)})')
+			return files[-1 - idx]
 
 
 	class xmr_signable: # mixin class
 	class xmr_signable: # mixin class
 		automount = True
 		automount = True

+ 1 - 1
mmgen/data/version

@@ -1 +1 @@
-16.1.dev27
+16.1.dev28

+ 17 - 5
mmgen/main_txsend.py

@@ -23,7 +23,7 @@ mmgen-txsend: Broadcast a transaction signed by 'mmgen-txsign' to the network
 import sys
 import sys
 
 
 from .cfg import gc, Config
 from .cfg import gc, Config
-from .util import async_run, die
+from .util import async_run, die, is_int
 
 
 opts_data = {
 opts_data = {
 	'sets': [
 	'sets': [
@@ -35,6 +35,7 @@ opts_data = {
 		'usage2': [
 		'usage2': [
 			'[opts] <signed transaction file>',
 			'[opts] <signed transaction file>',
 			'[opts] --autosign',
 			'[opts] --autosign',
+			'[opts] --autosign (--status | --receipt) [IDX]',
 		],
 		],
 		'options': """
 		'options': """
 -h, --help       Print this help message
 -h, --help       Print this help message
@@ -68,6 +69,12 @@ opts_data = {
                  Use special value ‘env’ to honor *_PROXY environment vars
                  Use special value ‘env’ to honor *_PROXY environment vars
                  instead.
                  instead.
 -y, --yes        Answer 'yes' to prompts, suppress non-essential output
 -y, --yes        Answer 'yes' to prompts, suppress non-essential output
+""",
+		'notes': """
+With --autosign, combined with --status or --receipt, the optional IDX arg
+represents an index into the list of sent transaction files on the removable
+device, in reverse chronological order.  ‘0’ (the default) specifies the
+last sent transaction, ‘1’ the next-to-last, and so on.
 """
 """
 	},
 	},
 	'code': {
 	'code': {
@@ -95,8 +102,8 @@ post_send_op = cfg.status or cfg.receipt
 
 
 asi = None
 asi = None
 
 
-def init_autosign():
-	global asi, si, infile
+def init_autosign(arg):
+	global asi, si, infile, tx_idx
 	from .tx.util import mount_removable_device
 	from .tx.util import mount_removable_device
 	from .autosign import Signable
 	from .autosign import Signable
 	asi = mount_removable_device(cfg)
 	asi = mount_removable_device(cfg)
@@ -108,13 +115,18 @@ def init_autosign():
 			die(1, 'Transaction is unsent')
 			die(1, 'Transaction is unsent')
 		if si.unsigned:
 		if si.unsigned:
 			die(1, 'Transaction is unsigned')
 			die(1, 'Transaction is unsigned')
+		if not is_int(arg):
+			die(2, f'{arg}: invalid transaction index (must be a non-negative integer)')
+		tx_idx = int(arg)
 	else:
 	else:
 		infile = si.get_unsent()
 		infile = si.get_unsent()
 		cfg._util.qmsg(f'Got signed transaction file ‘{infile}’')
 		cfg._util.qmsg(f'Got signed transaction file ‘{infile}’')
 
 
 match cfg._args:
 match cfg._args:
+	case [arg] if cfg.autosign and post_send_op:
+		init_autosign(arg)
 	case [] if cfg.autosign:
 	case [] if cfg.autosign:
-		init_autosign()
+		init_autosign(0)
 	case [infile]:
 	case [infile]:
 		from .fileutil import check_infile
 		from .fileutil import check_infile
 		check_infile(infile)
 		check_infile(infile)
@@ -132,7 +144,7 @@ async def main():
 	global cfg
 	global cfg
 
 
 	if cfg.autosign and post_send_op:
 	if cfg.autosign and post_send_op:
-		tx = await si.get_last_sent()
+		tx = await si.get_last_sent(idx=tx_idx)
 	else:
 	else:
 		tx = await OnlineSignedTX(
 		tx = await OnlineSignedTX(
 			cfg        = cfg,
 			cfg        = cfg,

+ 15 - 2
test/cmdtest_d/automount.py

@@ -82,6 +82,9 @@ class CmdTestAutosignAutomount(CmdTestAutosignThreaded, CmdTestRegtest):
 		('alice_txbump5',                    'bumping the transaction (new outputs)'),
 		('alice_txbump5',                    'bumping the transaction (new outputs)'),
 		('alice_txsend5',                    'sending the bumped transaction'),
 		('alice_txsend5',                    'sending the bumped transaction'),
 		('alice_txstatus5',                  'getting transaction status (in mempool)'),
 		('alice_txstatus5',                  'getting transaction status (in mempool)'),
+		('alice_txstatus6',                  'getting transaction status (idx=0, in mempool)'),
+		('alice_txstatus7',                  'getting transaction status (idx=1, replaced)'),
+		('alice_txstatus8',                  'getting transaction status (idx=3, 2 confirmations)'),
 		('generate',                         'mining a block'),
 		('generate',                         'mining a block'),
 		('alice_bal2',                       'checking Alice’s balance'),
 		('alice_bal2',                       'checking Alice’s balance'),
 		('wait_loop_kill',                   'stopping autosign wait loop'),
 		('wait_loop_kill',                   'stopping autosign wait loop'),
@@ -221,7 +224,7 @@ class CmdTestAutosignAutomount(CmdTestAutosignThreaded, CmdTestRegtest):
 	def alice_txsend5(self):
 	def alice_txsend5(self):
 		return self._user_txsend('alice', need_rbf=True)
 		return self._user_txsend('alice', need_rbf=True)
 
 
-	def _alice_txstatus(self, expect, exit_val=None, need_rbf=False):
+	def _alice_txstatus(self, expect, exit_val=None, need_rbf=False, idx=None):
 
 
 		if need_rbf and not self.proto.cap('rbf'):
 		if need_rbf and not self.proto.cap('rbf'):
 			return 'skip'
 			return 'skip'
@@ -229,7 +232,8 @@ class CmdTestAutosignAutomount(CmdTestAutosignThreaded, CmdTestRegtest):
 		self.insert_device_online()
 		self.insert_device_online()
 		t = self.spawn(
 		t = self.spawn(
 				'mmgen-txsend',
 				'mmgen-txsend',
-				['--alice', '--autosign', '--status', '--verbose'],
+				['--alice', '--autosign', '--status', '--verbose']
+				+ ([] if idx is None else [str(idx)]),
 				no_passthru_opts = ['coin'],
 				no_passthru_opts = ['coin'],
 				exit_val = exit_val)
 				exit_val = exit_val)
 		t.expect(expect)
 		t.expect(expect)
@@ -255,6 +259,15 @@ class CmdTestAutosignAutomount(CmdTestAutosignThreaded, CmdTestRegtest):
 	def alice_txstatus5(self):
 	def alice_txstatus5(self):
 		return self._alice_txstatus('in mempool', need_rbf=True)
 		return self._alice_txstatus('in mempool', need_rbf=True)
 
 
+	def alice_txstatus6(self):
+		return self._alice_txstatus('in mempool', need_rbf=True, idx=0)
+
+	def alice_txstatus7(self):
+		return self._alice_txstatus('replaced', need_rbf=True, idx=1)
+
+	def alice_txstatus8(self):
+		return self._alice_txstatus('2 confirmations', need_rbf=True, idx=3)
+
 	def alice_txsend_bad_no_unsent(self):
 	def alice_txsend_bad_no_unsent(self):
 		self.insert_device_online()
 		self.insert_device_online()
 		t = self.spawn('mmgen-txsend', ['--quiet', '--autosign'], exit_val=2, no_passthru_opts=['coin'])
 		t = self.spawn('mmgen-txsend', ['--quiet', '--autosign'], exit_val=2, no_passthru_opts=['coin'])