From 16a81271e42087128455e17ff258a6f74d6789c7 Mon Sep 17 00:00:00 2001 From: thomasv Date: Wed, 5 Dec 2012 15:16:52 +0100 Subject: [PATCH] store timestamps in verifier --- lib/verifier.py | 32 +++++++++++++++++++------------- lib/wallet.py | 28 +++++----------------------- 2 files changed, 24 insertions(+), 36 deletions(-) diff --git a/lib/verifier.py b/lib/verifier.py index 646b64670..1801edc33 100644 --- a/lib/verifier.py +++ b/lib/verifier.py @@ -35,7 +35,7 @@ class WalletVerifier(threading.Thread): self.transactions = {} # requested verifications (with height sent by the requestor) self.interface.register_channel('verifier') - self.verified_tx = config.get('verified_tx',{}) # height of verified tx + self.verified_tx = config.get('verified_tx2',{}) # height, timestamp of verified transactions self.merkle_roots = config.get('merkle_roots',{}) # hashed by me self.targets = config.get('targets',{}) # compute targets @@ -51,10 +51,20 @@ class WalletVerifier(threading.Thread): """ return the number of confirmations of a monitored transaction. """ with self.lock: if tx in self.transactions.keys(): - return (self.local_height - self.verified_tx[tx] + 1) if tx in self.verified_tx else -1 + if tx in self.verified_tx: + height, timestamp = self.verified_tx[tx] + conf = (self.local_height - height + 1) + else: + conf = -1 else: #print "verifier: tx not in list", tx - return 0 + conf = 0 + + if conf <= 0: + timestamp = None + + return conf, timestamp + def add(self, tx_hash, tx_height): """ add a transaction to the list of monitored transactions. """ @@ -167,9 +177,11 @@ class WalletVerifier(threading.Thread): if not header: return assert header.get('merkle_root') == self.merkle_roots[tx_hash] # we passed all the tests - self.verified_tx[tx_hash] = tx_height + header = self.read_header(tx_height) + timestamp = header.get('timestamp') + self.verified_tx[tx_hash] = (tx_height, timestamp) print_error("verified %s"%tx_hash) - self.config.set_key('verified_tx', self.verified_tx, True) + self.config.set_key('verified_tx2', self.verified_tx, True) self.interface.trigger_callback('updated') @@ -225,7 +237,8 @@ class WalletVerifier(threading.Thread): # this can be caused by a reorg. print_error("verify header failed"+ repr(header)) # undo verifications - for tx_hash, tx_height in self.verified_tx.items(): + for tx_hash, item in self.verified_tx.items(): + tx_height, timestamp = item if tx_height >= height: print_error("redoing", tx_hash) self.verified_tx.pop(tx_hash) @@ -368,10 +381,3 @@ class WalletVerifier(threading.Thread): new_bits = c + MM * i return new_bits, new_target - def get_timestamp(self, tx_height): - if tx_height>0: - header = self.read_header(tx_height) - if header: - return header.get('timestamp') - - diff --git a/lib/wallet.py b/lib/wallet.py index 43124bac3..c5f948a9e 100644 --- a/lib/wallet.py +++ b/lib/wallet.py @@ -430,9 +430,9 @@ class Wallet: if not tx_hash: return '' tx = self.transactions.get(tx_hash) is_mine, v, fee = self.get_tx_value(tx_hash) - conf = self.verifier.get_confirmations(tx_hash) - timestamp = tx.get('timestamp') - if conf and timestamp: + conf, timestamp = self.verifier.get_confirmations(tx_hash) + + if conf: time_str = datetime.datetime.fromtimestamp(timestamp).isoformat(' ')[:-3] else: time_str = 'pending' @@ -683,7 +683,7 @@ class Wallet: def get_tx_history(self): with self.lock: history = self.transactions.values() - history.sort(key = lambda x: x.get('timestamp') if x.get('timestamp') else 1e12) + history.sort(key = lambda x: x.get('height') if x.get('height') else 1e12) result = [] balance = 0 @@ -699,8 +699,7 @@ class Wallet: balance = c + u - balance for tx in history: tx_hash = tx['tx_hash'] - timestamp = tx.get('timestamp') - conf = self.verifier.get_confirmations(tx_hash) if self.verifier else None + conf, timestamp = self.verifier.get_confirmations(tx_hash) if self.verifier else None is_mine, value, fee = self.get_tx_value(tx_hash) if value is not None: balance += value @@ -1042,11 +1041,6 @@ class Wallet: if tx_height>0: self.verifier.add(tx_hash, tx_height) - # set the timestamp for transactions that need it - if tx and not tx.get('timestamp'): - timestamp = self.verifier.get_timestamp(tx_height) - self.set_tx_timestamp(tx_hash, timestamp) - # review transactions that are in the history for addr, hist in self.history.items(): if hist == ['*']: continue @@ -1062,13 +1056,6 @@ class Wallet: tx['height'] = tx_height - - def set_tx_timestamp(self, tx_hash, timestamp): - with self.lock: - self.transactions[tx_hash]['timestamp'] = timestamp - - - def is_addr_in_tx(self, addr, tx): found = False for txin in tx.get('inputs'): @@ -1300,10 +1287,6 @@ class WalletSynchronizer(threading.Thread): if self.wallet.transactions.get(tx_hash) is None: if (tx_hash, tx_height) not in requested_tx and (tx_hash, tx_height) not in missing_tx: missing_tx.append( (tx_hash, tx_height) ) - else: - if self.wallet.verifier: - timestamp = self.wallet.verifier.get_timestamp(tx_height) - self.wallet.set_tx_timestamp(tx_hash, timestamp) elif method == 'blockchain.transaction.get': tx_hash = params[0] @@ -1339,6 +1322,5 @@ class WalletSynchronizer(threading.Thread): d = deserialize.parse_Transaction(vds) d['height'] = tx_height d['tx_hash'] = tx_hash - if self.wallet.verifier: d['timestamp'] = self.wallet.verifier.get_timestamp(tx_height) return d