3 from json import dumps, loads
5 from Queue import Queue
13 from backends.bitcoind import deserialize
14 from processor import Processor, print_log
17 from storage import Storage
20 class BlockchainProcessor(Processor):
22 def __init__(self, config, shared):
23 Processor.__init__(self)
25 self.mtimes = {} # monitoring
28 self.up_to_date = False
30 self.watch_lock = threading.Lock()
31 self.watch_blocks = []
32 self.watch_headers = []
33 self.watched_addresses = {}
35 self.history_cache = {}
37 self.cache_lock = threading.Lock()
38 self.headers_data = ''
39 self.headers_path = config.get('leveldb', 'path_fulltree')
41 self.mempool_addresses = {}
42 self.mempool_hist = {}
43 self.mempool_hashes = set([])
44 self.mempool_lock = threading.Lock()
46 self.address_queue = Queue()
49 self.test_reorgs = config.getboolean('leveldb', 'test_reorgs') # simulate random blockchain reorgs
51 self.test_reorgs = False
52 self.storage = Storage(config, shared, self.test_reorgs)
54 self.dblock = threading.Lock()
56 self.bitcoind_url = 'http://%s:%s@%s:%s/' % (
57 config.get('bitcoind', 'user'),
58 config.get('bitcoind', 'password'),
59 config.get('bitcoind', 'host'),
60 config.get('bitcoind', 'port'))
64 self.bitcoind('getinfo')
67 print_log('cannot contact bitcoind...')
72 self.sent_header = None
75 self.init_headers(self.storage.height)
77 threading.Timer(0, lambda: self.catch_up(sync=False)).start()
78 while not shared.stopped() and not self.up_to_date:
82 print "keyboard interrupt: stopping threads"
86 print_log("Blockchain is up to date.")
87 self.memorypool_update()
88 print_log("Memory pool initialized.")
90 threading.Timer(10, self.main_iteration).start()
94 def mtime(self, name):
97 delta = now - self.now
98 t = self.mtimes.get(name, 0)
99 self.mtimes[name] = t + delta
102 def print_mtime(self):
104 for k, v in self.mtimes.items():
105 s += k+':'+"%.2f"%v+' '
109 def bitcoind(self, method, params=[]):
110 postdata = dumps({"method": method, 'params': params, 'id': 'jsonrpc'})
112 respdata = urllib.urlopen(self.bitcoind_url, postdata).read()
114 traceback.print_exc(file=sys.stdout)
118 if r['error'] is not None:
119 raise BaseException(r['error'])
120 return r.get('result')
123 def block2header(self, b):
125 "block_height": b.get('height'),
126 "version": b.get('version'),
127 "prev_block_hash": b.get('previousblockhash'),
128 "merkle_root": b.get('merkleroot'),
129 "timestamp": b.get('time'),
130 "bits": int(b.get('bits'), 16),
131 "nonce": b.get('nonce'),
134 def get_header(self, height):
135 block_hash = self.bitcoind('getblockhash', [height])
136 b = self.bitcoind('getblock', [block_hash])
137 return self.block2header(b)
139 def init_headers(self, db_height):
140 self.chunk_cache = {}
141 self.headers_filename = os.path.join(self.headers_path, 'blockchain_headers')
143 if os.path.exists(self.headers_filename):
144 height = os.path.getsize(self.headers_filename)/80 - 1 # the current height
146 prev_hash = self.hash_header(self.read_header(height))
150 open(self.headers_filename, 'wb').close()
154 if height < db_height:
155 print_log("catching up missing headers:", height, db_height)
158 while height < db_height:
160 header = self.get_header(height)
162 assert prev_hash == header.get('prev_block_hash')
163 self.write_header(header, sync=False)
164 prev_hash = self.hash_header(header)
165 if (height % 1000) == 0:
166 print_log("headers file:", height)
167 except KeyboardInterrupt:
173 def hash_header(self, header):
174 return rev_hex(Hash(header_to_string(header).decode('hex')).encode('hex'))
176 def read_header(self, block_height):
177 if os.path.exists(self.headers_filename):
178 with open(self.headers_filename, 'rb') as f:
179 f.seek(block_height * 80)
182 h = header_from_string(h)
185 def read_chunk(self, index):
186 with open(self.headers_filename, 'rb') as f:
187 f.seek(index*2016*80)
188 chunk = f.read(2016*80)
189 return chunk.encode('hex')
191 def write_header(self, header, sync=True):
192 if not self.headers_data:
193 self.headers_offset = header.get('block_height')
195 self.headers_data += header_to_string(header).decode('hex')
196 if sync or len(self.headers_data) > 40*100:
199 with self.cache_lock:
200 chunk_index = header.get('block_height')/2016
201 if self.chunk_cache.get(chunk_index):
202 self.chunk_cache.pop(chunk_index)
204 def pop_header(self):
205 # we need to do this only if we have not flushed
206 if self.headers_data:
207 self.headers_data = self.headers_data[:-40]
209 def flush_headers(self):
210 if not self.headers_data:
212 with open(self.headers_filename, 'rb+') as f:
213 f.seek(self.headers_offset*80)
214 f.write(self.headers_data)
215 self.headers_data = ''
217 def get_chunk(self, i):
218 # store them on disk; store the current chunk in memory
219 with self.cache_lock:
220 chunk = self.chunk_cache.get(i)
222 chunk = self.read_chunk(i)
223 self.chunk_cache[i] = chunk
227 def get_mempool_transaction(self, txid):
229 raw_tx = self.bitcoind('getrawtransaction', [txid, 0])
233 vds = deserialize.BCDataStream()
234 vds.write(raw_tx.decode('hex'))
236 return deserialize.parse_Transaction(vds, is_coinbase=False)
238 print_log("ERROR: cannot parse", txid)
242 def get_history(self, addr, cache_only=False):
243 with self.cache_lock:
244 hist = self.history_cache.get(addr)
252 hist = self.storage.get_history(addr)
264 with self.mempool_lock:
265 for txid in self.mempool_hist.get(addr, []):
266 hist.append({'tx_hash':txid, 'height':0})
268 # add something to distinguish between unused and empty addresses
269 if hist == [] and is_known:
272 with self.cache_lock:
273 self.history_cache[addr] = hist
277 def get_status(self, addr, cache_only=False):
278 tx_points = self.get_history(addr, cache_only)
279 if cache_only and tx_points == -1:
284 if tx_points == ['*']:
288 status += tx.get('tx_hash') + ':%d:' % tx.get('height')
289 return hashlib.sha256(status).digest().encode('hex')
291 def get_merkle(self, tx_hash, height):
293 block_hash = self.bitcoind('getblockhash', [height])
294 b = self.bitcoind('getblock', [block_hash])
295 tx_list = b.get('tx')
296 tx_pos = tx_list.index(tx_hash)
298 merkle = map(hash_decode, tx_list)
299 target_hash = hash_decode(tx_hash)
301 while len(merkle) != 1:
303 merkle.append(merkle[-1])
306 new_hash = Hash(merkle[0] + merkle[1])
307 if merkle[0] == target_hash:
308 s.append(hash_encode(merkle[1]))
309 target_hash = new_hash
310 elif merkle[1] == target_hash:
311 s.append(hash_encode(merkle[0]))
312 target_hash = new_hash
317 return {"block_height": height, "merkle": s, "pos": tx_pos}
320 def add_to_history(self, addr, tx_hash, tx_pos, tx_height):
322 s = self.serialize_item(tx_hash, tx_pos, tx_height) + 40*chr(0)
325 serialized_hist = self.batch_list[addr]
327 l = len(serialized_hist)/80
328 for i in range(l-1, -1, -1):
329 item = serialized_hist[80*i:80*(i+1)]
330 item_height = int(rev_hex(item[36:39].encode('hex')), 16)
331 if item_height <= tx_height:
332 serialized_hist = serialized_hist[0:80*(i+1)] + s + serialized_hist[80*(i+1):]
335 serialized_hist = s + serialized_hist
337 self.batch_list[addr] = serialized_hist
340 txo = (tx_hash + int_to_hex(tx_pos, 4)).decode('hex')
341 self.batch_txio[txo] = addr
348 def deserialize_block(self, block):
349 txlist = block.get('tx')
350 tx_hashes = [] # ordered txids
351 txdict = {} # deserialized tx
353 for raw_tx in txlist:
354 tx_hash = hash_encode(Hash(raw_tx.decode('hex')))
355 vds = deserialize.BCDataStream()
356 vds.write(raw_tx.decode('hex'))
358 tx = deserialize.parse_Transaction(vds, is_coinbase)
360 print_log("ERROR: cannot parse", tx_hash)
362 tx_hashes.append(tx_hash)
365 return tx_hashes, txdict
369 def import_block(self, block, block_hash, block_height, sync, revert=False):
371 touched_addr = set([])
373 # deserialize transactions
374 tx_hashes, txdict = self.deserialize_block(block)
378 undo_info = self.storage.get_undo_info(block_height)
383 for txid in tx_hashes: # must be ordered
386 undo = self.storage.import_transaction(txid, tx, block_height, touched_addr)
387 undo_info[txid] = undo
389 undo = undo_info.pop(txid)
390 self.storage.revert_transaction(txid, tx, block_height, touched_addr, undo)
393 assert undo_info == {}
397 self.storage.write_undo_info(block_height, self.bitcoind_height, undo_info)
400 self.storage.db_undo.put('height', repr( (block_hash, block_height, self.storage.db_version) ))
402 for addr in touched_addr:
403 self.invalidate_cache(addr)
405 self.storage.update_hashes()
408 def add_request(self, session, request):
409 # see if we can get if from cache. if not, add to queue
410 if self.process(session, request, cache_only=True) == -1:
411 self.queue.put((session, request))
414 def do_subscribe(self, method, params, session):
415 with self.watch_lock:
416 if method == 'blockchain.numblocks.subscribe':
417 if session not in self.watch_blocks:
418 self.watch_blocks.append(session)
420 elif method == 'blockchain.headers.subscribe':
421 if session not in self.watch_headers:
422 self.watch_headers.append(session)
424 elif method == 'blockchain.address.subscribe':
426 l = self.watched_addresses.get(address)
428 self.watched_addresses[address] = [session]
429 elif session not in l:
433 def do_unsubscribe(self, method, params, session):
434 with self.watch_lock:
435 if method == 'blockchain.numblocks.subscribe':
436 if session in self.watch_blocks:
437 self.watch_blocks.remove(session)
438 elif method == 'blockchain.headers.subscribe':
439 if session in self.watch_headers:
440 self.watch_headers.remove(session)
441 elif method == "blockchain.address.subscribe":
443 l = self.watched_addresses.get(addr)
452 self.watched_addresses.pop(addr)
455 def process(self, session, request, cache_only=False):
457 message_id = request['id']
458 method = request['method']
459 params = request.get('params', [])
463 if method == 'blockchain.numblocks.subscribe':
464 result = self.storage.height
466 elif method == 'blockchain.headers.subscribe':
469 elif method == 'blockchain.address.subscribe':
471 address = str(params[0])
472 result = self.get_status(address, cache_only)
473 except BaseException, e:
474 error = str(e) + ': ' + address
475 print_log("error:", error)
477 elif method == 'blockchain.address.get_history':
479 address = str(params[0])
480 result = self.get_history(address, cache_only)
481 except BaseException, e:
482 error = str(e) + ': ' + address
483 print_log("error:", error)
485 elif method == 'blockchain.address.get_balance':
487 address = str(params[0])
488 result = self.storage.get_balance(address)
489 except BaseException, e:
490 error = str(e) + ': ' + address
491 print_log("error:", error)
493 elif method == 'blockchain.address.get_path':
495 address = str(params[0])
496 result = self.storage.get_address_path(address)
497 except BaseException, e:
498 error = str(e) + ': ' + address
499 print_log("error:", error)
501 elif method == 'blockchain.address.listunspent':
503 address = str(params[0])
504 result = self.storage.listunspent(address)
505 except BaseException, e:
506 error = str(e) + ': ' + address
507 print_log("error:", error)
509 elif method == 'blockchain.block.get_header':
515 result = self.get_header(height)
516 except BaseException, e:
517 error = str(e) + ': %d' % height
518 print_log("error:", error)
520 elif method == 'blockchain.block.get_chunk':
526 result = self.get_chunk(index)
527 except BaseException, e:
528 error = str(e) + ': %d' % index
529 print_log("error:", error)
531 elif method == 'blockchain.transaction.broadcast':
533 txo = self.bitcoind('sendrawtransaction', params)
534 print_log("sent tx:", txo)
536 except BaseException, e:
537 result = str(e) # do not send an error
538 print_log("error:", result, params)
540 elif method == 'blockchain.transaction.get_merkle':
546 tx_height = params[1]
547 result = self.get_merkle(tx_hash, tx_height)
548 except BaseException, e:
549 error = str(e) + ': ' + repr(params)
550 print_log("get_merkle error:", error)
552 elif method == 'blockchain.transaction.get':
555 result = self.bitcoind('getrawtransaction', [tx_hash, 0])
556 except BaseException, e:
557 error = str(e) + ': ' + repr(params)
558 print_log("tx get error:", error)
561 error = "unknown method:%s" % method
563 if cache_only and result == -1:
567 self.push_response(session, {'id': message_id, 'error': error})
569 self.push_response(session, {'id': message_id, 'result': result})
572 def getfullblock(self, block_hash):
573 block = self.bitcoind('getblock', [block_hash])
577 for txid in block['tx']:
579 "method": "getrawtransaction",
585 postdata = dumps(rawtxreq)
587 respdata = urllib.urlopen(self.bitcoind_url, postdata).read()
589 traceback.print_exc(file=sys.stdout)
595 if ir['error'] is not None:
597 print_log("Error: make sure you run bitcoind with txindex=1; use -reindex if needed.")
598 raise BaseException(ir['error'])
599 rawtxdata.append(ir['result'])
600 block['tx'] = rawtxdata
603 def catch_up(self, sync=True):
606 while not self.shared.stopped():
611 info = self.bitcoind('getinfo')
612 self.bitcoind_height = info.get('blocks')
613 bitcoind_block_hash = self.bitcoind('getblockhash', [self.bitcoind_height])
614 if self.storage.last_hash == bitcoind_block_hash:
615 self.up_to_date = True
619 self.up_to_date = False
620 next_block_hash = self.bitcoind('getblockhash', [self.storage.height + 1])
621 next_block = self.getfullblock(next_block_hash)
624 # fixme: this is unsafe, if we revert when the undo info is not yet written
625 revert = (random.randint(1, 100) == 1) if self.test_reorgs else False
627 if (next_block.get('previousblockhash') == self.storage.last_hash) and not revert:
629 self.import_block(next_block, next_block_hash, self.storage.height+1, sync)
630 self.storage.height = self.storage.height + 1
631 self.write_header(self.block2header(next_block), sync)
632 self.storage.last_hash = next_block_hash
635 if self.storage.height % 1000 == 0 and not sync:
636 t_daemon = self.mtimes.get('daemon')
637 t_import = self.mtimes.get('import')
638 print_log("catch_up: block %d (%.3fs %.3fs)" % (self.storage.height, t_daemon, t_import), self.storage.get_root_hash().encode('hex'))
639 self.mtimes['daemon'] = 0
640 self.mtimes['import'] = 0
643 rh = self.storage.get_root_hash().encode('hex')
645 print_log("root hash error", prh, rh)
651 prh = self.storage.get_root_hash().encode('hex')
653 # revert current block
654 block = self.getfullblock(self.storage.last_hash)
655 print_log("blockchain reorg", self.storage.height, block.get('previousblockhash'), self.storage.last_hash)
656 self.import_block(block, self.storage.last_hash, self.storage.height, sync, revert=True)
660 self.storage.height -= 1
662 # read previous header from disk
663 self.header = self.read_header(self.storage.height)
664 self.storage.last_hash = self.hash_header(self.header)
667 self.header = self.block2header(self.bitcoind('getblock', [self.storage.last_hash]))
669 if self.shared.stopped():
670 print_log( "closing database" )
674 def memorypool_update(self):
675 mempool_hashes = set(self.bitcoind('getrawmempool'))
676 touched_addresses = set([])
678 for tx_hash in mempool_hashes:
679 if tx_hash in self.mempool_hashes:
682 tx = self.get_mempool_transaction(tx_hash)
686 mpa = self.mempool_addresses.get(tx_hash, [])
687 for x in tx.get('inputs'):
688 # we assume that the input address can be parsed by deserialize(); this is true for Electrum transactions
689 addr = x.get('address')
690 if addr and addr not in mpa:
692 touched_addresses.add(addr)
694 for x in tx.get('outputs'):
695 addr = x.get('address')
696 if addr and addr not in mpa:
698 touched_addresses.add(addr)
700 self.mempool_addresses[tx_hash] = mpa
701 self.mempool_hashes.add(tx_hash)
703 # remove older entries from mempool_hashes
704 self.mempool_hashes = mempool_hashes
706 # remove deprecated entries from mempool_addresses
707 for tx_hash, addresses in self.mempool_addresses.items():
708 if tx_hash not in self.mempool_hashes:
709 self.mempool_addresses.pop(tx_hash)
710 for addr in addresses:
711 touched_addresses.add(addr)
713 # rebuild mempool histories
714 new_mempool_hist = {}
715 for tx_hash, addresses in self.mempool_addresses.items():
716 for addr in addresses:
717 h = new_mempool_hist.get(addr, [])
720 new_mempool_hist[addr] = h
722 with self.mempool_lock:
723 self.mempool_hist = new_mempool_hist
725 # invalidate cache for touched addresses
726 for addr in touched_addresses:
727 self.invalidate_cache(addr)
730 def invalidate_cache(self, address):
731 with self.cache_lock:
732 if address in self.history_cache:
733 print_log("cache: invalidating", address)
734 self.history_cache.pop(address)
736 with self.watch_lock:
737 sessions = self.watched_addresses.get(address)
740 # TODO: update cache here. if new value equals cached value, do not send notification
741 self.address_queue.put((address,sessions))
743 def main_iteration(self):
744 if self.shared.stopped():
745 print_log("blockchain processor terminating")
754 self.memorypool_update()
756 if self.sent_height != self.storage.height:
757 self.sent_height = self.storage.height
758 for session in self.watch_blocks:
759 self.push_response(session, {
761 'method': 'blockchain.numblocks.subscribe',
762 'params': [self.storage.height],
765 if self.sent_header != self.header:
766 print_log("blockchain: %d (%.3fs)" % (self.storage.height, t2 - t1))
767 self.sent_header = self.header
768 for session in self.watch_headers:
769 self.push_response(session, {
771 'method': 'blockchain.headers.subscribe',
772 'params': [self.header],
777 addr, sessions = self.address_queue.get(False)
781 status = self.get_status(addr)
782 for session in sessions:
783 self.push_response(session, {
785 'method': 'blockchain.address.subscribe',
786 'params': [addr, status],
789 if not self.shared.stopped():
790 threading.Timer(10, self.main_iteration).start()
792 print_log("blockchain processor terminating")