From 330c3d82a067c820a7fe0d22586b724315215239 Mon Sep 17 00:00:00 2001 From: philemon Date: Sat, 19 Jul 2014 11:25:33 +0400 Subject: [PATCH] Make blockchain rescanning optional for 'mmgen-addrimport' --- mmgen-addrimport | 73 ++++++++++++++++++++++++++--------------- mmgen-txcreate | 2 +- mmgen/rpc/connection.py | 4 +-- 3 files changed, 50 insertions(+), 29 deletions(-) diff --git a/mmgen-addrimport b/mmgen-addrimport index 13835fbb..54d5f891 100755 --- a/mmgen-addrimport +++ b/mmgen-addrimport @@ -34,12 +34,14 @@ help_data = { 'options': """ -h, --help Print this help message -l, --addrlist f Import the non-mmgen Bitcoin addresses listed in file 'f' +-r, --rescan Rescan the blockchain. Required if address to import is + on the blockchain and has a balance. Rescanning is slow. -q, --quiet Suppress warnings """ } -short_opts = "hl:q" -long_opts = "help", "addrlist=", "quiet" +short_opts = "hl:qr" +long_opts = "help", "addrlist=", "quiet", "rescan" opts,cmd_args = process_opts(sys.argv,help_data,"".join(short_opts),long_opts) if 'quiet' in opts: g.quiet = True @@ -75,18 +77,23 @@ g.http_timeout = 3600 c = connect_to_bitcoind() m = """ -Importing addresses can take a long time (>30 min.) on a low-powered computer. +WARNING: You've chosen the '--rescan' option. Rescanning the block chain is +necessary only if an address you're importing is already on the block chain +and has a balance. Note that the rescanning process is very slow (>30 min. +for each imported address on a low-powered computer). + """.strip() if "rescan" in opts else """ +WARNING: If any of the addresses you're importing is already on the block chain +and has a balance, you must exit the program now and rerun it using the +'--rescan' option. Otherwise you may ignore this message and continue. """.strip() -confirm_or_exit(m, "continue", expect="YES") -import threading -import time +confirm_or_exit(m, "continue", expect="YES") err_flag = False -def import_address(addr,label): +def import_address(addr,label,rescan): try: - c.importaddress(addr,label) + c.importaddress(addr,label,rescan) except: global err_flag err_flag = True @@ -94,7 +101,13 @@ def import_address(addr,label): w1 = len(str(len(addr_data))) * 2 + 2 w2 = len(str(max([i[0] for i in addr_data if i[0]]))) + 12 -msg_fmt = "\r%s %-" + str(w1) + "s %-34s %-" + str(w2) + "s" + +if "rescan" in opts: + import threading + import time + msg_fmt = "\r%s %-" + str(w1) + "s %-34s %-" + str(w2) + "s" +else: + msg_fmt = "\r%-" + str(w1) + "s %-34s %-" + str(w2) + "s" msg("Importing addresses") for n,i in enumerate(addr_data): @@ -103,25 +116,33 @@ for n,i in enumerate(addr_data): label = "%s:%s%s" % (seed_id,i[0],comment) else: label = "non-mmgen" - t = threading.Thread(target=import_address, args = (i[1],label)) - t.daemon = True - t.start() + if "rescan" in opts: + t = threading.Thread(target=import_address, args=(i[1],label,True)) + t.daemon = True + t.start() - start = int(time.time()) + start = int(time.time()) - while True: - if t.is_alive(): - elapsed = int(time.time() - start) - msg_r(msg_fmt % ( - secs_to_hms(elapsed), + while True: + if t.is_alive(): + elapsed = int(time.time() - start) + msg_r(msg_fmt % ( + secs_to_hms(elapsed), + ("%s/%s:" % (n+1,len(addr_data))), + i[1], "(" + label + ")" + ) + ) + time.sleep(1) + else: + if err_flag: msg("\nImport failed"); sys.exit(2) + msg("\nOK") + break + else: + import_address(i[1],label,rescan=False) + msg_r(msg_fmt % ( ("%s/%s:" % (n+1,len(addr_data))), i[1], "(" + label + ")" - ) ) - time.sleep(1) - else: - if err_flag: - msg("\nImport failed") - sys.exit(2) - msg("\nOK") - break + ) + if err_flag: msg("\nImport failed"); sys.exit(2) + msg(" - OK") diff --git a/mmgen-txcreate b/mmgen-txcreate index 5d0fce2b..ae237279 100755 --- a/mmgen-txcreate +++ b/mmgen-txcreate @@ -34,7 +34,7 @@ prog_name = sys.argv[0].split("/")[-1] help_data = { 'prog_name': prog_name, 'desc': "Create a BTC transaction with outputs to specified addresses", - 'usage': "[opts] ... [change addr] [tx fee] [addr file] ...", + 'usage': "[opts] ... [change addr] [addr file] ...", 'options': """ -h, --help Print this help message -d, --outdir d Specify an alternate directory 'd' for output diff --git a/mmgen/rpc/connection.py b/mmgen/rpc/connection.py index dea66045..8c445d23 100755 --- a/mmgen/rpc/connection.py +++ b/mmgen/rpc/connection.py @@ -58,12 +58,12 @@ class BitcoinConnection(object): raise _wrap_exception(e.error) # importaddress
[label] [rescan=true] - def importaddress(self,address,label=None): + def importaddress(self,address,label=None,rescan=True): """ """ try: # return self.proxy.badmethod(address,label) # DEBUG - return self.proxy.importaddress(address,label) + return self.proxy.importaddress(address,label,rescan) except JSONRPCException as e: if e.error['message'] == "Method not found": from mmgen.util import msg_r