From 9cefb4ce4006b2d983e7f32f445ca43198193d0e Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Sun, 29 Nov 2026 17:04:40 +0000 Subject: [PATCH] tx.util: new `check_fee()`, `fee_too_high_msg()` functions --- mmgen/proto/btc/tx/new.py | 8 +++----- mmgen/proto/btc/tx/online.py | 9 ++------- mmgen/proto/eth/tx/new.py | 7 ++----- mmgen/proto/eth/tx/online.py | 8 ++------ mmgen/tx/new.py | 4 ++-- mmgen/tx/util.py | 24 ++++++++++++++++++++++++ 6 files changed, 35 insertions(+), 25 deletions(-) create mode 100644 mmgen/tx/util.py diff --git a/mmgen/proto/btc/tx/new.py b/mmgen/proto/btc/tx/new.py index 2c8748e2..455b0465 100644 --- a/mmgen/proto/btc/tx/new.py +++ b/mmgen/proto/btc/tx/new.py @@ -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) diff --git a/mmgen/proto/btc/tx/online.py b/mmgen/proto/btc/tx/online.py index 54e0e169..424c990c 100644 --- a/mmgen/proto/btc/tx/online.py +++ b/mmgen/proto/btc/tx/online.py @@ -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): diff --git a/mmgen/proto/eth/tx/new.py b/mmgen/proto/eth/tx/new.py index f30da918..43c83dca 100644 --- a/mmgen/proto/eth/tx/new.py +++ b/mmgen/proto/eth/tx/new.py @@ -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 diff --git a/mmgen/proto/eth/tx/online.py b/mmgen/proto/eth/tx/online.py index 74fd6275..3f774215 100644 --- a/mmgen/proto/eth/tx/online.py +++ b/mmgen/proto/eth/tx/online.py @@ -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): diff --git a/mmgen/tx/new.py b/mmgen/tx/new.py index 2f19a27f..0e89465a 100644 --- a/mmgen/tx/new.py +++ b/mmgen/tx/new.py @@ -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): diff --git a/mmgen/tx/util.py b/mmgen/tx/util.py new file mode 100644 index 00000000..b1a2d6c3 --- /dev/null +++ b/mmgen/tx/util.py @@ -0,0 +1,24 @@ +# MMGen Wallet, a terminal-based cryptocurrency wallet +# Copyright (C)2013-2026 The MMGen Project +# 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)