tx.util: new check_fee(), fee_too_high_msg() functions

This commit is contained in:
The MMGen Project 2026-11-29 17:04:40 +00:00
commit 9cefb4ce40
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
6 changed files with 35 additions and 25 deletions

View file

@ -12,7 +12,7 @@ proto.btc.tx.new: Bitcoin new transaction class
from ....tx.new import New as TxNew
from ....obj import MMGenTxID
from ....util import msg, fmt, make_chksum_6, die, suf
from ....util import msg, fmt, make_chksum_6, suf
from ....color import pink
from .base import Base
@ -113,10 +113,8 @@ class New(Base, TxNew):
self.update_output_amt(self.chg_idx, funds_left)
def check_fee(self):
fee = self.sum_inputs() - self.sum_outputs()
if fee > self.proto.max_tx_fee:
c = self.proto.coin
die('MaxFeeExceeded', f'Transaction fee of {fee} {c} too high! (> {self.proto.max_tx_fee} {c})')
from ....tx.util import check_fee
check_fee(self.proto, self.sum_inputs() - self.sum_outputs())
def final_inputs_ok_msg(self, funds_left):
return 'Transaction produces {} {} in change'.format(funds_left.hl(), self.coin)

View file

@ -19,20 +19,15 @@ class OnlineSigned(Signed, TxBase.OnlineSigned):
async def send_checks(self):
from ....tx.util import check_fee
self.check_correct_chain()
check_fee(self.proto, self.fee)
if not self.cfg.test_suite_bogus_send:
if self.has_segwit_outputs() and not self.rpc.info('segwit_is_active'):
die(2, 'Transaction has Segwit outputs, but this blockchain does not support Segwit'
+ ' at the current height')
if self.fee > self.proto.max_tx_fee:
die(2, 'Transaction fee ({}) greater than {} max_tx_fee ({} {})!'.format(
self.fee,
self.proto.name,
self.proto.max_tx_fee,
self.proto.coin))
await self.status.display()
async def test_sendable(self, txhex):

View file

@ -112,11 +112,8 @@ class New(VmNew, Base, TxBase.New):
if (abs_fee := self.feespec2abs(fee, None)) is False:
return False
if not self.disable_fee_check and (abs_fee > self.proto.max_tx_fee):
msg('{} {c}: {} fee too large (maximum fee: {} {c})'.format(
abs_fee.hl(),
desc,
self.proto.max_tx_fee.hl(),
c = self.proto.coin))
from ....tx.util import fee_too_high_msg
msg(fee_too_high_msg(self.proto, abs_fee, desc=desc))
return False
else:
return abs_fee

View file

@ -27,13 +27,9 @@ class OnlineSigned(Signed, TxBase.OnlineSigned):
ymsg('Warning: two transactions (approval and router) will be broadcast to the network')
async def send_checks(self):
from ....tx.util import check_fee
self.check_correct_chain()
if not self.disable_fee_check and (self.fee > self.proto.max_tx_fee):
die(2, 'Transaction fee ({}) greater than {} max_tx_fee ({} {})!'.format(
self.fee,
self.proto.name,
self.proto.max_tx_fee,
self.proto.coin))
check_fee(self.proto, self.fee)
await self.status.display()
async def rpc_send(self, txhex):

View file

@ -158,8 +158,8 @@ class New(Base):
def check_fee(self):
if not self.disable_fee_check:
if self.usr_fee and self.usr_fee > self.proto.max_tx_fee:
die('MaxFeeExceeded', f'{self.usr_fee} > max fee ({self.proto.max_tx_fee})')
from .util import check_fee
check_fee(self.proto, self.usr_fee)
# we don't know fee yet, so perform preliminary check with fee == 0
async def precheck_sufficient_funds(self, inputs_sum, sel_unspent, outputs_sum):

24
mmgen/tx/util.py Normal file
View file

@ -0,0 +1,24 @@
# MMGen Wallet, a terminal-based cryptocurrency wallet
# Copyright (C)2013-2026 The MMGen Project <mmgen@tuta.io>
# Licensed under the GNU General Public License, Version 3:
# https://www.gnu.org/licenses
# Public project repositories:
# https://github.com/mmgen/mmgen-wallet
# https://gitlab.com/mmgen/mmgen-wallet
"""
tx.util: transaction-related utilities
"""
from ..util import die
def check_fee(proto, fee):
if fee and fee > proto.max_tx_fee:
die('MaxFeeExceeded', fee_too_high_msg(proto, fee))
def fee_too_high_msg(proto, fee, *, desc='transaction'):
return '{a} {c}: {d} fee too high! (> {b} {c})'.format(
a = fee.hl(color=None),
b = proto.max_tx_fee,
c = proto.dcoin,
d = desc)