3 from json import dumps, loads
5 from Queue import Queue
13 from backends.bitcoind import deserialize
14 from processor import Processor, print_log
18 class BlockchainProcessor(Processor):
20 def __init__(self, config, shared):
21 Processor.__init__(self)
25 self.up_to_date = False
27 self.watch_lock = threading.Lock()
28 self.watch_blocks = []
29 self.watch_headers = []
30 self.watched_addresses = {}
32 self.history_cache = {}
34 self.cache_lock = threading.Lock()
35 self.headers_data = ''
37 self.mempool_addresses = {}
38 self.mempool_hist = {}
39 self.mempool_hashes = set([])
40 self.mempool_lock = threading.Lock()
42 self.address_queue = Queue()
45 self.use_plyvel = config.getboolean('leveldb', 'use_plyvel')
47 self.use_plyvel = False
48 print_log('use_plyvel:', self.use_plyvel)
50 # don't use the same database for plyvel, because python-leveldb uses snappy compression
51 self.dbpath = config.get('leveldb', 'path_plyvel' if self.use_plyvel else 'path')
53 self.pruning_limit = config.getint('leveldb', 'pruning_limit')
54 self.db_version = 1 # increase this when database needs to be updated
56 self.dblock = threading.Lock()
60 self.db = plyvel.DB(self.dbpath, create_if_missing=True, paranoid_checks=None, compression=None)
63 self.db = leveldb.LevelDB(self.dbpath, paranoid_checks=False)
65 traceback.print_exc(file=sys.stdout)
68 self.bitcoind_url = 'http://%s:%s@%s:%s/' % (
69 config.get('bitcoind', 'user'),
70 config.get('bitcoind', 'password'),
71 config.get('bitcoind', 'host'),
72 config.get('bitcoind', 'port'))
76 self.bitcoind('getinfo')
79 print_log('cannot contact bitcoind...')
86 self.sent_header = None
89 hist = self.deserialize(self.db_get('height'))
90 self.last_hash, self.height, db_version = hist[0]
91 print_log("Database version", self.db_version)
92 print_log("Blockchain height", self.height)
94 traceback.print_exc(file=sys.stdout)
95 print_log('initializing database')
97 self.last_hash = '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'
98 db_version = self.db_version
101 if self.db_version != db_version:
102 print_log("Your database '%s' is deprecated. Please create a new database"%self.dbpath)
107 self.init_headers(self.height)
109 threading.Timer(0, lambda: self.catch_up(sync=False)).start()
110 while not shared.stopped() and not self.up_to_date:
114 print "keyboard interrupt: stopping threads"
118 print_log("Blockchain is up to date.")
119 self.memorypool_update()
120 print_log("Memory pool initialized.")
122 threading.Timer(10, self.main_iteration).start()
125 def db_get(self, key):
127 return self.db.get(key)
130 return self.db.Get(key)
134 def batch_put(self, batch, key, value):
136 batch.put(key, value)
138 batch.Put(key, value)
140 def batch_delete(self, batch, key):
146 def batch_write(self, batch, sync):
148 batch.write()#, sync=sync)
150 self.db.Write(batch, sync=sync)
153 def bitcoind(self, method, params=[]):
154 postdata = dumps({"method": method, 'params': params, 'id': 'jsonrpc'})
156 respdata = urllib.urlopen(self.bitcoind_url, postdata).read()
158 traceback.print_exc(file=sys.stdout)
162 if r['error'] is not None:
163 raise BaseException(r['error'])
164 return r.get('result')
166 def serialize(self, h):
168 for txid, txpos, height in h:
169 s += self.serialize_item(txid, txpos, height)
172 def serialize_item(self, txid, txpos, height, spent=chr(0)):
173 s = (txid + int_to_hex(txpos, 4) + int_to_hex(height, 3)).decode('hex') + spent
176 def deserialize_item(self,s):
177 txid = s[0:32].encode('hex')
178 txpos = int(rev_hex(s[32:36].encode('hex')), 16)
179 height = int(rev_hex(s[36:39].encode('hex')), 16)
181 return (txid, txpos, height, spent)
183 def deserialize(self, s):
186 txid, txpos, height, spent = self.deserialize_item(s[0:40])
187 h.append((txid, txpos, height))
189 txid, txpos, height, spent = self.deserialize_item(s[40:80])
190 h.append((txid, txpos, height))
194 def block2header(self, b):
196 "block_height": b.get('height'),
197 "version": b.get('version'),
198 "prev_block_hash": b.get('previousblockhash'),
199 "merkle_root": b.get('merkleroot'),
200 "timestamp": b.get('time'),
201 "bits": int(b.get('bits'), 16),
202 "nonce": b.get('nonce'),
205 def get_header(self, height):
206 block_hash = self.bitcoind('getblockhash', [height])
207 b = self.bitcoind('getblock', [block_hash])
208 return self.block2header(b)
210 def init_headers(self, db_height):
211 self.chunk_cache = {}
212 self.headers_filename = os.path.join(self.dbpath, 'blockchain_headers')
214 if os.path.exists(self.headers_filename):
215 height = os.path.getsize(self.headers_filename)/80 - 1 # the current height
217 prev_hash = self.hash_header(self.read_header(height))
221 open(self.headers_filename, 'wb').close()
225 if height < db_height:
226 print_log("catching up missing headers:", height, db_height)
229 while height < db_height:
231 header = self.get_header(height)
233 assert prev_hash == header.get('prev_block_hash')
234 self.write_header(header, sync=False)
235 prev_hash = self.hash_header(header)
236 if (height % 1000) == 0:
237 print_log("headers file:", height)
238 except KeyboardInterrupt:
244 def hash_header(self, header):
245 return rev_hex(Hash(header_to_string(header).decode('hex')).encode('hex'))
247 def read_header(self, block_height):
248 if os.path.exists(self.headers_filename):
249 with open(self.headers_filename, 'rb') as f:
250 f.seek(block_height * 80)
253 h = header_from_string(h)
256 def read_chunk(self, index):
257 with open(self.headers_filename, 'rb') as f:
258 f.seek(index*2016*80)
259 chunk = f.read(2016*80)
260 return chunk.encode('hex')
262 def write_header(self, header, sync=True):
263 if not self.headers_data:
264 self.headers_offset = header.get('block_height')
266 self.headers_data += header_to_string(header).decode('hex')
267 if sync or len(self.headers_data) > 40*100:
270 with self.cache_lock:
271 chunk_index = header.get('block_height')/2016
272 if self.chunk_cache.get(chunk_index):
273 self.chunk_cache.pop(chunk_index)
275 def pop_header(self):
276 # we need to do this only if we have not flushed
277 if self.headers_data:
278 self.headers_data = self.headers_data[:-40]
280 def flush_headers(self):
281 if not self.headers_data:
283 with open(self.headers_filename, 'rb+') as f:
284 f.seek(self.headers_offset*80)
285 f.write(self.headers_data)
286 self.headers_data = ''
288 def get_chunk(self, i):
289 # store them on disk; store the current chunk in memory
290 with self.cache_lock:
291 chunk = self.chunk_cache.get(i)
293 chunk = self.read_chunk(i)
294 self.chunk_cache[i] = chunk
298 def get_mempool_transaction(self, txid):
300 raw_tx = self.bitcoind('getrawtransaction', [txid, 0])
304 vds = deserialize.BCDataStream()
305 vds.write(raw_tx.decode('hex'))
307 return deserialize.parse_Transaction(vds, is_coinbase=False)
309 print_log("ERROR: cannot parse", txid)
312 def get_history(self, addr, cache_only=False):
313 with self.cache_lock:
314 hist = self.history_cache.get(addr)
322 hist = self.deserialize(self.db_get(str((addr))))
333 # sort history, because redeeming transactions are next to the corresponding txout
334 hist.sort(key=lambda tup: tup[2])
337 with self.mempool_lock:
338 for txid in self.mempool_hist.get(addr, []):
339 hist.append((txid, 0, 0))
342 hist = set(map(lambda x: (x[0], x[2]), hist))
345 hist = map(lambda x: {'tx_hash': x[0], 'height': x[1]}, hist)
347 # add something to distinguish between unused and empty addresses
348 if hist == [] and is_known:
351 with self.cache_lock:
352 self.history_cache[addr] = hist
355 def get_status(self, addr, cache_only=False):
356 tx_points = self.get_history(addr, cache_only)
357 if cache_only and tx_points == -1:
362 if tx_points == ['*']:
366 status += tx.get('tx_hash') + ':%d:' % tx.get('height')
367 return hashlib.sha256(status).digest().encode('hex')
369 def get_merkle(self, tx_hash, height):
371 block_hash = self.bitcoind('getblockhash', [height])
372 b = self.bitcoind('getblock', [block_hash])
373 tx_list = b.get('tx')
374 tx_pos = tx_list.index(tx_hash)
376 merkle = map(hash_decode, tx_list)
377 target_hash = hash_decode(tx_hash)
379 while len(merkle) != 1:
381 merkle.append(merkle[-1])
384 new_hash = Hash(merkle[0] + merkle[1])
385 if merkle[0] == target_hash:
386 s.append(hash_encode(merkle[1]))
387 target_hash = new_hash
388 elif merkle[1] == target_hash:
389 s.append(hash_encode(merkle[0]))
390 target_hash = new_hash
395 return {"block_height": height, "merkle": s, "pos": tx_pos}
398 def add_to_history(self, addr, tx_hash, tx_pos, tx_height):
400 s = self.serialize_item(tx_hash, tx_pos, tx_height) + 40*chr(0)
403 serialized_hist = self.batch_list[addr]
405 l = len(serialized_hist)/80
406 for i in range(l-1, -1, -1):
407 item = serialized_hist[80*i:80*(i+1)]
408 item_height = int(rev_hex(item[36:39].encode('hex')), 16)
409 if item_height <= tx_height:
410 serialized_hist = serialized_hist[0:80*(i+1)] + s + serialized_hist[80*(i+1):]
413 serialized_hist = s + serialized_hist
415 self.batch_list[addr] = serialized_hist
418 txo = (tx_hash + int_to_hex(tx_pos, 4)).decode('hex')
419 self.batch_txio[txo] = addr
423 def revert_add_to_history(self, addr, tx_hash, tx_pos, tx_height):
425 serialized_hist = self.batch_list[addr]
426 s = self.serialize_item(tx_hash, tx_pos, tx_height) + 40*chr(0)
427 if serialized_hist.find(s) == -1: raise
428 serialized_hist = serialized_hist.replace(s, '')
429 self.batch_list[addr] = serialized_hist
433 def prune_history(self, addr, undo):
434 # remove items that have bit set to one
435 if undo.get(addr) is None: undo[addr] = []
437 serialized_hist = self.batch_list[addr]
438 l = len(serialized_hist)/80
440 if len(serialized_hist)/80 < self.pruning_limit: break
441 item = serialized_hist[80*i:80*(i+1)]
442 if item[39:40] == chr(1):
443 assert item[79:80] == chr(2)
444 serialized_hist = serialized_hist[0:80*i] + serialized_hist[80*(i+1):]
445 undo[addr].append(item) # items are ordered
446 self.batch_list[addr] = serialized_hist
449 def revert_prune_history(self, addr, undo):
450 # restore removed items
451 serialized_hist = self.batch_list[addr]
453 if undo.get(addr) is not None:
454 itemlist = undo.pop(addr)
458 if not itemlist: return
460 l = len(serialized_hist)/80
462 for i in range(l-1, -1, -1):
467 tx_item = itemlist.pop(-1) # get the last element
468 tx_height = int(rev_hex(tx_item[36:39].encode('hex')), 16)
470 item = serialized_hist[80*i:80*(i+1)]
471 item_height = int(rev_hex(item[36:39].encode('hex')), 16)
473 if item_height < tx_height:
474 serialized_hist = serialized_hist[0:80*(i+1)] + tx_item + serialized_hist[80*(i+1):]
478 serialized_hist = ''.join(itemlist) + tx_item + serialized_hist
480 self.batch_list[addr] = serialized_hist
483 def set_spent_bit(self, addr, txi, is_spent, txid=None, index=None, height=None):
484 serialized_hist = self.batch_list[addr]
485 l = len(serialized_hist)/80
487 item = serialized_hist[80*i:80*(i+1)]
488 if item[0:36] == txi:
490 new_item = item[0:39] + chr(1) + self.serialize_item(txid, index, height, chr(2))
492 new_item = item[0:39] + chr(0) + chr(0)*40
493 serialized_hist = serialized_hist[0:80*i] + new_item + serialized_hist[80*(i+1):]
497 hist = self.deserialize(serialized_hist)
498 raise BaseException("prevout not found", addr, hist, txi.encode('hex'))
500 self.batch_list[addr] = serialized_hist
503 def unset_spent_bit(self, addr, txi):
504 self.set_spent_bit(addr, txi, False)
505 self.batch_txio[txi] = addr
508 def deserialize_block(self, block):
509 txlist = block.get('tx')
510 tx_hashes = [] # ordered txids
511 txdict = {} # deserialized tx
513 for raw_tx in txlist:
514 tx_hash = hash_encode(Hash(raw_tx.decode('hex')))
515 vds = deserialize.BCDataStream()
516 vds.write(raw_tx.decode('hex'))
518 tx = deserialize.parse_Transaction(vds, is_coinbase)
520 print_log("ERROR: cannot parse", tx_hash)
522 tx_hashes.append(tx_hash)
525 return tx_hashes, txdict
527 def get_undo_info(self, height):
528 s = self.db_get("undo%d" % (height % 100))
531 def write_undo_info(self, batch, height, undo_info):
532 if self.is_test or height > self.bitcoind_height - 100:
533 self.batch_put(batch, "undo%d" % (height % 100), repr(undo_info))
535 def import_block(self, block, block_hash, block_height, sync, revert=False):
537 self.batch_list = {} # address -> history
538 self.batch_txio = {} # transaction i/o -> address
540 block_inputs = set([])
541 block_outputs = set([])
542 addr_to_read = set([])
544 # deserialize transactions
546 tx_hashes, txdict = self.deserialize_block(block)
552 undo_info = self.get_undo_info(block_height)
558 # read addresses of tx inputs
559 for tx in txdict.values():
560 for x in tx.get('inputs'):
561 txi = (x.get('prevout_hash') + int_to_hex(x.get('prevout_n'), 4)).decode('hex')
562 block_inputs.add(txi)
565 for txi in sorted(block_inputs):
567 addr = self.db_get(txi)
569 # the input could come from the same block
572 traceback.print_exc(file=sys.stdout)
576 self.batch_txio[txi] = addr
577 addr_to_read.add(addr)
580 for txid, tx in txdict.items():
581 for x in tx.get('outputs'):
582 txo = (txid + int_to_hex(x.get('index'), 4)).decode('hex')
583 block_outputs.add(txo)
584 addr_to_read.add( x.get('address') )
586 undo = undo_info.get(txid)
587 for i, x in enumerate(tx.get('inputs')):
588 addr = undo['prev_addr'][i]
589 addr_to_read.add(addr)
592 #time spent reading txio
595 # read histories of addresses
596 for txid, tx in txdict.items():
597 for x in tx.get('outputs'):
598 addr_to_read.add(x.get('address'))
601 for addr in sorted(addr_to_read):
603 h = self.db_get(addr)
604 self.batch_list[addr] = '' if h is None else h
606 print "db get error", addr
607 traceback.print_exc(file=sys.stdout)
616 tx_hashes = tx_hashes[::-1]
619 for txid in tx_hashes: # must be ordered
623 undo = { 'prev_addr':[] } # contains the list of pruned items for each address in the tx; also, 'prev_addr' is a list of prev addresses
626 for i, x in enumerate(tx.get('inputs')):
627 txi = (x.get('prevout_hash') + int_to_hex(x.get('prevout_n'), 4)).decode('hex')
628 addr = self.batch_txio[txi]
630 # add redeem item to the history.
631 # add it right next to the input txi? this will break history sorting, but it's ok if I neglect tx inputs during search
632 self.set_spent_bit(addr, txi, True, txid, i, block_height)
634 # when I prune, prune a pair
635 self.prune_history(addr, undo)
636 prev_addr.append(addr)
638 undo['prev_addr'] = prev_addr
640 # here I add only the outputs to history; maybe I want to add inputs too (that's in the other loop)
641 for x in tx.get('outputs'):
642 addr = x.get('address')
643 self.add_to_history(addr, txid, x.get('index'), block_height)
644 self.prune_history(addr, undo) # prune here because we increased the length of the history
646 undo_info[txid] = undo
650 undo = undo_info.pop(txid)
652 for x in tx.get('outputs'):
653 addr = x.get('address')
654 self.revert_prune_history(addr, undo)
655 self.revert_add_to_history(addr, txid, x.get('index'), block_height)
657 prev_addr = undo.pop('prev_addr')
658 for i, x in enumerate(tx.get('inputs')):
660 self.revert_prune_history(addr, undo)
661 txi = (x.get('prevout_hash') + int_to_hex(x.get('prevout_n'), 4)).decode('hex')
662 self.unset_spent_bit(addr, txi)
667 assert undo_info == {}
676 batch = self.db.write_batch()
679 batch = leveldb.WriteBatch()
681 for addr, serialized_hist in self.batch_list.items():
682 self.batch_put(batch, addr, serialized_hist)
683 l = len(serialized_hist)/80
689 # add new created outputs
690 for txio, addr in self.batch_txio.items():
691 self.batch_put(batch, txio, addr)
692 # delete spent inputs
693 for txi in block_inputs:
694 self.batch_delete(batch, txi)
696 self.write_undo_info(batch, block_height, undo_info)
698 # restore spent inputs
699 for txio, addr in self.batch_txio.items():
700 # print "restoring spent input", repr(txio)
701 self.batch_put(batch, txio, addr)
702 # delete spent outputs
703 for txo in block_outputs:
704 self.batch_delete(batch, txo)
707 self.batch_put(batch, 'height', self.serialize([(block_hash, block_height, self.db_version)]))
710 self.batch_write(batch, sync)
713 if t3 - t0 > 10 and not sync:
714 print_log("block %d "%block_height,
715 "total:%0.2f " % (t3 - t0),
716 #"parse:%0.2f " % (t00 - t0),
717 "read_txio[%4d]:%0.2f " % (len(block_inputs), t000 - t00),
718 "read_addr[%4d]:%0.2f " % (len(addr_to_read), t1 - t000),
719 #"proc:%.2f " % (t2-t1),
720 "write:%.2f " % (t3-t2),
721 "max:", max_len, max_addr)
723 for addr in self.batch_list.keys():
724 self.invalidate_cache(addr)
726 def add_request(self, session, request):
727 # see if we can get if from cache. if not, add to queue
728 if self.process(session, request, cache_only=True) == -1:
729 self.queue.put((session, request))
732 def do_subscribe(self, method, params, session):
733 with self.watch_lock:
734 if method == 'blockchain.numblocks.subscribe':
735 if session not in self.watch_blocks:
736 self.watch_blocks.append(session)
738 elif method == 'blockchain.headers.subscribe':
739 if session not in self.watch_headers:
740 self.watch_headers.append(session)
742 elif method == 'blockchain.address.subscribe':
744 l = self.watched_addresses.get(address)
746 self.watched_addresses[address] = [session]
747 elif session not in l:
751 def do_unsubscribe(self, method, params, session):
752 with self.watch_lock:
753 if method == 'blockchain.numblocks.subscribe':
754 if session in self.watch_blocks:
755 self.watch_blocks.remove(session)
756 elif method == 'blockchain.headers.subscribe':
757 if session in self.watch_headers:
758 self.watch_headers.remove(session)
759 elif method == "blockchain.address.subscribe":
761 l = self.watched_addresses.get(addr)
770 self.watched_addresses.pop(addr)
773 def process(self, session, request, cache_only=False):
775 message_id = request['id']
776 method = request['method']
777 params = request.get('params', [])
781 if method == 'blockchain.numblocks.subscribe':
784 elif method == 'blockchain.headers.subscribe':
787 elif method == 'blockchain.address.subscribe':
790 result = self.get_status(address, cache_only)
791 except BaseException, e:
792 error = str(e) + ': ' + address
793 print_log("error:", error)
795 elif method == 'blockchain.address.get_history':
798 result = self.get_history(address, cache_only)
799 except BaseException, e:
800 error = str(e) + ': ' + address
801 print_log("error:", error)
803 elif method == 'blockchain.block.get_header':
809 result = self.get_header(height)
810 except BaseException, e:
811 error = str(e) + ': %d' % height
812 print_log("error:", error)
814 elif method == 'blockchain.block.get_chunk':
820 result = self.get_chunk(index)
821 except BaseException, e:
822 error = str(e) + ': %d' % index
823 print_log("error:", error)
825 elif method == 'blockchain.transaction.broadcast':
827 txo = self.bitcoind('sendrawtransaction', params)
828 print_log("sent tx:", txo)
830 except BaseException, e:
831 result = str(e) # do not send an error
832 print_log("error:", result, params)
834 elif method == 'blockchain.transaction.get_merkle':
840 tx_height = params[1]
841 result = self.get_merkle(tx_hash, tx_height)
842 except BaseException, e:
843 error = str(e) + ': ' + repr(params)
844 print_log("get_merkle error:", error)
846 elif method == 'blockchain.transaction.get':
849 result = self.bitcoind('getrawtransaction', [tx_hash, 0])
850 except BaseException, e:
851 error = str(e) + ': ' + repr(params)
852 print_log("tx get error:", error)
855 error = "unknown method:%s" % method
857 if cache_only and result == -1:
861 self.push_response(session, {'id': message_id, 'error': error})
863 self.push_response(session, {'id': message_id, 'result': result})
866 def getfullblock(self, block_hash):
867 block = self.bitcoind('getblock', [block_hash])
871 for txid in block['tx']:
873 "method": "getrawtransaction",
879 postdata = dumps(rawtxreq)
881 respdata = urllib.urlopen(self.bitcoind_url, postdata).read()
883 traceback.print_exc(file=sys.stdout)
889 if ir['error'] is not None:
891 print_log("Error: make sure you run bitcoind with txindex=1; use -reindex if needed.")
892 raise BaseException(ir['error'])
893 rawtxdata.append(ir['result'])
894 block['tx'] = rawtxdata
897 def catch_up(self, sync=True):
900 while not self.shared.stopped():
902 info = self.bitcoind('getinfo')
903 self.bitcoind_height = info.get('blocks')
904 bitcoind_block_hash = self.bitcoind('getblockhash', [self.bitcoind_height])
905 if self.last_hash == bitcoind_block_hash:
906 self.up_to_date = True
910 self.up_to_date = False
911 next_block_hash = self.bitcoind('getblockhash', [self.height + 1])
912 next_block = self.getfullblock(next_block_hash)
914 # fixme: this is unsafe, if we revert when the undo info is not yet written
915 revert = (random.randint(1, 100) == 1) if self.is_test else False
917 if (next_block.get('previousblockhash') == self.last_hash) and not revert:
919 self.import_block(next_block, next_block_hash, self.height+1, sync)
920 self.height = self.height + 1
921 self.write_header(self.block2header(next_block), sync)
922 self.last_hash = next_block_hash
924 if self.height % 100 == 0 and not sync:
926 print_log("catch_up: block %d (%.3fs)" % (self.height, t2 - t1))
930 # revert current block
931 block = self.getfullblock(self.last_hash)
932 print_log("blockchain reorg", self.height, block.get('previousblockhash'), self.last_hash)
933 self.import_block(block, self.last_hash, self.height, sync, revert=True)
939 # read previous header from disk
940 self.header = self.read_header(self.height)
941 self.last_hash = self.hash_header(self.header)
943 self.header = self.block2header(self.bitcoind('getblock', [self.last_hash]))
945 if self.shared.stopped() and self.use_plyvel:
946 print_log( "closing database" )
950 def memorypool_update(self):
951 mempool_hashes = set(self.bitcoind('getrawmempool'))
952 touched_addresses = set([])
954 for tx_hash in mempool_hashes:
955 if tx_hash in self.mempool_hashes:
958 tx = self.get_mempool_transaction(tx_hash)
962 mpa = self.mempool_addresses.get(tx_hash, [])
963 for x in tx.get('inputs'):
964 # we assume that the input address can be parsed by deserialize(); this is true for Electrum transactions
965 addr = x.get('address')
966 if addr and addr not in mpa:
968 touched_addresses.add(addr)
970 for x in tx.get('outputs'):
971 addr = x.get('address')
972 if addr and addr not in mpa:
974 touched_addresses.add(addr)
976 self.mempool_addresses[tx_hash] = mpa
977 self.mempool_hashes.add(tx_hash)
979 # remove older entries from mempool_hashes
980 self.mempool_hashes = mempool_hashes
982 # remove deprecated entries from mempool_addresses
983 for tx_hash, addresses in self.mempool_addresses.items():
984 if tx_hash not in self.mempool_hashes:
985 self.mempool_addresses.pop(tx_hash)
986 for addr in addresses:
987 touched_addresses.add(addr)
989 # rebuild mempool histories
990 new_mempool_hist = {}
991 for tx_hash, addresses in self.mempool_addresses.items():
992 for addr in addresses:
993 h = new_mempool_hist.get(addr, [])
996 new_mempool_hist[addr] = h
998 with self.mempool_lock:
999 self.mempool_hist = new_mempool_hist
1001 # invalidate cache for touched addresses
1002 for addr in touched_addresses:
1003 self.invalidate_cache(addr)
1006 def invalidate_cache(self, address):
1007 with self.cache_lock:
1008 if address in self.history_cache:
1009 print_log("cache: invalidating", address)
1010 self.history_cache.pop(address)
1012 with self.watch_lock:
1013 sessions = self.watched_addresses.get(address)
1016 # TODO: update cache here. if new value equals cached value, do not send notification
1017 self.address_queue.put((address,sessions))
1019 def main_iteration(self):
1020 if self.shared.stopped():
1021 print_log("blockchain processor terminating")
1031 self.memorypool_update()
1033 if self.sent_height != self.height:
1034 self.sent_height = self.height
1035 for session in self.watch_blocks:
1036 self.push_response(session, {
1038 'method': 'blockchain.numblocks.subscribe',
1039 'params': [self.height],
1042 if self.sent_header != self.header:
1043 print_log("blockchain: %d (%.3fs)" % (self.height, t2 - t1))
1044 self.sent_header = self.header
1045 for session in self.watch_headers:
1046 self.push_response(session, {
1048 'method': 'blockchain.headers.subscribe',
1049 'params': [self.header],
1054 addr, sessions = self.address_queue.get(False)
1058 status = self.get_status(addr)
1059 for session in sessions:
1060 self.push_response(session, {
1062 'method': 'blockchain.address.subscribe',
1063 'params': [addr, status],
1066 if not self.shared.stopped():
1067 threading.Timer(10, self.main_iteration).start()
1069 print_log("blockchain processor terminating")