From 6aca0f29865a745d6e208f08c73475df45d12309 Mon Sep 17 00:00:00 2001 From: MMGen Date: Thu, 22 Feb 2018 23:13:20 +0300 Subject: [PATCH] use estimatesmartfee if the daemon supports it --- mmgen/rpc.py | 1 + mmgen/tx.py | 19 ++++++++++++++----- test/test.py | 12 ++++++++++-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/mmgen/rpc.py b/mmgen/rpc.py index 9cabb831..02a2d1f5 100755 --- a/mmgen/rpc.py +++ b/mmgen/rpc.py @@ -168,6 +168,7 @@ class CoinDaemonRPCConnection(object): 'decoderawtransaction', 'disconnectnode', 'estimatefee', + 'estimatesmartfee', 'getaddressesbyaccount', 'getbalance', 'getblock', diff --git a/mmgen/tx.py b/mmgen/tx.py index 4e5c4f7e..1e6271ce 100755 --- a/mmgen/tx.py +++ b/mmgen/tx.py @@ -1049,16 +1049,25 @@ class MMGenTX(MMGenObject): start_fee = opt.tx_fee else: desc = 'Network-estimated' - ret = g.rpch.estimatefee(opt.tx_confs) - if ret == -1: + try: + ret = g.rpch.estimatesmartfee(opt.tx_confs,on_fail='raise') + except: + fetype = 'estimatefee' + fee_per_kb = g.rpch.estimatefee(opt.tx_confs) + else: + fetype = 'estimatesmartfee' + fee_per_kb = ret['feerate'] if 'feerate' in ret else -2 + + if fee_per_kb < 0: if not have_estimate_fail: - msg('Network fee estimation for {} confirmations failed'.format(opt.tx_confs)) + msg('Network fee estimation for {} confirmations failed ({})'.format(opt.tx_confs,fetype)) have_estimate_fail.append(True) start_fee = None else: - start_fee = g.proto.coin_amt(ret) * opt.tx_fee_adj * self.estimate_size() / 1024 + start_fee = g.proto.coin_amt(fee_per_kb) * opt.tx_fee_adj * self.estimate_size() / 1024 if opt.verbose: - msg('{} fee ({} confs): {} {}/kB'.format(desc,opt.tx_confs,ret,g.coin)) + msg('{} fee for {} confirmations: {} {}/kB'.format( + fetype.upper(),opt.tx_confs,fee_per_kb,g.coin)) msg('TX size (estimated): {}'.format(self.estimate_size())) return self.get_usr_fee_interactive(start_fee,desc=desc) diff --git a/test/test.py b/test/test.py index 28e1faef..48c33aff 100755 --- a/test/test.py +++ b/test/test.py @@ -685,6 +685,7 @@ cmd_group['regtest'] = ( ('regtest_alice_chk_label3', 'the label'), ('regtest_alice_remove_label1','removing a label'), ('regtest_alice_chk_label4', 'the label'), + ('regtest_alice_send_estimatefee','tx creation with no fee on command line'), ('regtest_stop', 'stopping regtest daemon'), ) @@ -2326,13 +2327,16 @@ class MMGenTestSuite(object): bad_locktime=False): os.environ['MMGEN_BOGUS_SEND'] = '' t = MMGenExpect(name,'mmgen-txdo', - ['-d',cfg['tmpdir'],'-B','--'+user,'--tx-fee='+fee] - + extra_args + ([],[wf])[bool(wf)] + outputs_cl) + ['-d',cfg['tmpdir'],'-B','--'+user] + + (['--tx-fee='+fee] if fee else []) + + extra_args + ([],[wf])[bool(wf)] + outputs_cl) os.environ['MMGEN_BOGUS_SEND'] = '1' t.expect(r"'q'=quit view, .*?:.",'M',regex=True) # sort by mmid t.expect(r"'q'=quit view, .*?:.",'q',regex=True) t.expect('outputs to spend: ',outputs_prompt+'\n') + if not fee: + t.expect('Enter transaction fee: ','{}\n'.format(tx_fee)) t.expect('OK? (Y/n): ','y') # fee OK? t.expect('OK? (Y/n): ','y') # change OK? if do_label: @@ -2389,6 +2393,10 @@ class MMGenTestSuite(object): fn = os.path.join(cfg['tmpdir'],'non-mmgen.keys') return self.regtest_user_txdo(name,'bob',rtFee[3],outputs_cl,'3-9',extra_args=['--keys-from-file='+fn]) + def regtest_alice_send_estimatefee(self,name): + outputs_cl = self.create_tx_outputs('bob',(('L',1,''),)) # bob_sid:L:1 + return self.regtest_user_txdo(name,'alice',None,outputs_cl,'1') # fee=None + def regtest_user_txbump(self,name,user,txfile,fee,red_op,no_send=False): if not g.proto.cap('rbf'): msg('Skipping RBF'); return True