Browse Source

tx.get_chg_output_idx() -> tx.chg_idx

The MMGen Project 2 years ago
parent
commit
f45c140350
5 changed files with 31 additions and 17 deletions
  1. 1 1
      mmgen/proto/btc/tx/completed.py
  2. 5 4
      mmgen/proto/btc/tx/new.py
  3. 17 5
      mmgen/tx/base.py
  4. 3 2
      mmgen/tx/bump.py
  5. 5 5
      mmgen/tx/new.py

+ 1 - 1
mmgen/proto/btc/tx/completed.py

@@ -65,7 +65,7 @@ class Completed(Base,TxBase.Completed):
 	@property
 	def send_amt(self):
 		return self.sum_outputs(
-			exclude = None if len(self.outputs) == 1 else self.get_chg_output_idx()
+			exclude = None if len(self.outputs) == 1 else self.chg_idx
 		)
 
 	def check_txfile_hex_data(self):

+ 5 - 4
mmgen/proto/btc/tx/new.py

@@ -86,12 +86,13 @@ class New(Base,TxBase.New):
 		return []
 
 	def update_change_output(self,funds_left):
-		chg_idx = self.get_chg_output_idx()
-		if funds_left == 0:
+		if funds_left == 0: # TODO: test
 			msg(self.no_chg_msg)
-			self.outputs.pop(chg_idx)
+			self.outputs.pop(self.chg_idx)
 		else:
-			self.update_output_amt(chg_idx,self.proto.coin_amt(funds_left))
+			self.update_output_amt(
+				self.chg_idx,
+				self.proto.coin_amt(funds_left) )
 
 	def check_fee(self):
 		fee = self.sum_inputs() - self.sum_outputs()

+ 17 - 5
mmgen/tx/base.py

@@ -142,12 +142,24 @@ class Base(MMGenObject):
 			return self.proto.coin_amt('0')
 		return self.proto.coin_amt(sum(e.amt for e in olist))
 
-	def get_chg_output_idx(self):
-		ch_ops = [x.is_chg for x in self.outputs]
-		try:
-			return ch_ops.index(True)
-		except ValueError:
+	def _chg_output_ops(self,op):
+		is_chgs = [x.is_chg for x in self.outputs]
+		if is_chgs.count(True) == 1:
+			return (
+				is_chgs.index(True) if op == 'idx' else
+				self.outputs[is_chgs.index(True)] )
+		elif is_chgs.count(True) == 0:
 			return None
+		else:
+			raise ValueError('more than one change output!')
+
+	@property
+	def chg_idx(self):
+		return self._chg_output_ops('idx')
+
+	@property
+	def chg_output(self):
+		return self._chg_output_ops('output')
 
 	def add_timestamp(self):
 		self.timestamp = make_timestamp()

+ 3 - 2
mmgen/tx/bump.py

@@ -42,8 +42,6 @@ class Bump(Completed,New):
 				f'All outputs contain less than the minimum fee ({self.min_fee} {self.coin})')
 
 	def choose_output(self):
-		chg_idx = self.get_chg_output_idx()
-		init_reply = opt.output_to_reduce
 
 		def check_sufficient_funds(o_amt):
 			if o_amt < self.min_fee:
@@ -58,6 +56,9 @@ class Bump(Completed,New):
 			else:
 				die(1,'Insufficient funds to bump transaction')
 
+		init_reply = opt.output_to_reduce
+		chg_idx = self.chg_idx
+
 		while True:
 			if init_reply == None:
 				from ..ui import line_input

+ 5 - 5
mmgen/tx/new.py

@@ -183,7 +183,7 @@ class New(Base):
 		else:
 			die(2,f'{addr}: invalid {err_desc} {{!r}}'.format(f'{addr},{amt}' if amt else addr))
 
-		if not amt and self.get_chg_output_idx() is not None:
+		if not (amt or self.chg_idx is None):
 			die(2,'ERROR: More than one change address {} on command line'.format(
 				'requested' if self.chg_autoselected else 'listed'))
 
@@ -194,7 +194,7 @@ class New(Base):
 		for a in cmd_args:
 			await self.process_cmd_arg(a,ad_f,ad_w)
 
-		if self.get_chg_output_idx() == None:
+		if self.chg_idx is None:
 			die(2,(
 				fmt( self.msg_no_change_output.format(self.dcoin) ).strip()
 					if len(self.outputs) == 1 else
@@ -235,9 +235,9 @@ class New(Base):
 		self.add_mmaddrs_to_outputs(ad_w,ad_f)
 		self.check_dup_addrs('outputs')
 
-		chg_idx = self.get_chg_output_idx()
-		if chg_idx is not None:
-			await self.warn_chg_addr_used(self.outputs[chg_idx])
+		if self.chg_output is not None:
+			if len(self.outputs) > 1:
+				await self.warn_chg_addr_used(self.chg_output)
 
 	async def warn_chg_addr_used(self,chg):
 		from ..tw.addresses import TwAddresses