3 from json import dumps, loads
6 from Queue import Queue
14 from backends.bitcoind import deserialize
15 from processor import Processor, print_log
19 class BlockchainProcessor(Processor):
21 def __init__(self, config, shared):
22 Processor.__init__(self)
26 self.up_to_date = False
27 self.watched_addresses = []
28 self.history_cache = {}
30 self.cache_lock = threading.Lock()
31 self.headers_data = ''
33 self.mempool_addresses = {}
34 self.mempool_hist = {}
35 self.mempool_hashes = []
36 self.mempool_lock = threading.Lock()
38 self.address_queue = Queue()
39 self.dbpath = config.get('leveldb', 'path')
41 self.dblock = threading.Lock()
43 self.db = leveldb.LevelDB(self.dbpath)
45 traceback.print_exc(file=sys.stdout)
48 self.bitcoind_url = 'http://%s:%s@%s:%s/' % (
49 config.get('bitcoind', 'user'),
50 config.get('bitcoind', 'password'),
51 config.get('bitcoind', 'host'),
52 config.get('bitcoind', 'port'))
57 self.sent_header = None
60 hist = self.deserialize(self.db.Get('height'))
61 self.last_hash, self.height, _ = hist[0]
62 print_log("hist", hist)
64 #traceback.print_exc(file=sys.stdout)
65 print_log('initializing database')
67 self.last_hash = '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'
70 self.init_headers(self.height)
72 threading.Timer(0, lambda: self.catch_up(sync=False)).start()
73 while not shared.stopped() and not self.up_to_date:
77 print "keyboard interrupt: stopping threads"
81 print_log("blockchain is up to date.")
83 threading.Timer(10, self.main_iteration).start()
85 def bitcoind(self, method, params=[]):
86 postdata = dumps({"method": method, 'params': params, 'id': 'jsonrpc'})
88 respdata = urllib.urlopen(self.bitcoind_url, postdata).read()
90 traceback.print_exc(file=sys.stdout)
94 if r['error'] is not None:
95 raise BaseException(r['error'])
96 return r.get('result')
98 def serialize(self, h):
100 for txid, txpos, height in h:
101 s += txid + int_to_hex(txpos, 4) + int_to_hex(height, 4)
102 return s.decode('hex')
104 def deserialize(self, s):
107 txid = s[0:32].encode('hex')
108 txpos = int(rev_hex(s[32:36].encode('hex')), 16)
109 height = int(rev_hex(s[36:40].encode('hex')), 16)
110 h.append((txid, txpos, height))
114 def block2header(self, b):
116 "block_height": b.get('height'),
117 "version": b.get('version'),
118 "prev_block_hash": b.get('previousblockhash'),
119 "merkle_root": b.get('merkleroot'),
120 "timestamp": b.get('time'),
121 "bits": int(b.get('bits'), 16),
122 "nonce": b.get('nonce'),
125 def get_header(self, height):
126 block_hash = self.bitcoind('getblockhash', [height])
127 b = self.bitcoind('getblock', [block_hash])
128 return self.block2header(b)
130 def init_headers(self, db_height):
131 self.chunk_cache = {}
132 self.headers_filename = os.path.join(self.dbpath, 'blockchain_headers')
134 if os.path.exists(self.headers_filename):
135 height = os.path.getsize(self.headers_filename)/80 - 1 # the current height
137 prev_hash = self.hash_header(self.read_header(height))
141 open(self.headers_filename, 'wb').close()
145 if height < db_height:
146 print_log("catching up missing headers:", height, db_height)
149 while height < db_height:
151 header = self.get_header(height)
153 assert prev_hash == header.get('prev_block_hash')
154 self.write_header(header, sync=False)
155 prev_hash = self.hash_header(header)
156 if (height % 1000) == 0:
157 print_log("headers file:", height)
158 except KeyboardInterrupt:
164 def hash_header(self, header):
165 return rev_hex(Hash(header_to_string(header).decode('hex')).encode('hex'))
167 def read_header(self, block_height):
168 if os.path.exists(self.headers_filename):
169 with open(self.headers_filename, 'rb') as f:
170 f.seek(block_height * 80)
173 h = header_from_string(h)
176 def read_chunk(self, index):
177 with open(self.headers_filename, 'rb') as f:
178 f.seek(index*2016*80)
179 chunk = f.read(2016*80)
180 return chunk.encode('hex')
182 def write_header(self, header, sync=True):
183 if not self.headers_data:
184 self.headers_offset = header.get('block_height')
186 self.headers_data += header_to_string(header).decode('hex')
187 if sync or len(self.headers_data) > 40*100:
190 with self.cache_lock:
191 chunk_index = header.get('block_height')/2016
192 if self.chunk_cache.get(chunk_index):
193 self.chunk_cache.pop(chunk_index)
195 def pop_header(self):
196 # we need to do this only if we have not flushed
197 if self.headers_data:
198 self.headers_data = self.headers_data[:-40]
200 def flush_headers(self):
201 if not self.headers_data:
203 with open(self.headers_filename, 'rb+') as f:
204 f.seek(self.headers_offset*80)
205 f.write(self.headers_data)
206 self.headers_data = ''
208 def get_chunk(self, i):
209 # store them on disk; store the current chunk in memory
210 with self.cache_lock:
211 chunk = self.chunk_cache.get(i)
213 chunk = self.read_chunk(i)
214 self.chunk_cache[i] = chunk
218 def get_mempool_transaction(self, txid):
220 raw_tx = self.bitcoind('getrawtransaction', [txid, 0, -1])
224 vds = deserialize.BCDataStream()
225 vds.write(raw_tx.decode('hex'))
227 return deserialize.parse_Transaction(vds, is_coinbase=False)
229 def get_history(self, addr, cache_only=False):
230 with self.cache_lock:
231 hist = self.history_cache.get(addr)
239 hash_160 = bc_address_to_hash_160(addr)
240 hist = self.deserialize(self.db.Get(hash_160))
246 # should not be necessary
247 hist.sort(key=lambda tup: tup[2])
248 # check uniqueness too...
251 with self.mempool_lock:
252 for txid in self.mempool_hist.get(addr, []):
253 hist.append((txid, 0, 0))
255 hist = map(lambda x: {'tx_hash': x[0], 'height': x[2]}, hist)
256 # add something to distinguish between unused and empty addresses
257 if hist == [] and is_known:
260 with self.cache_lock:
261 self.history_cache[addr] = hist
264 def get_status(self, addr, cache_only=False):
265 tx_points = self.get_history(addr, cache_only)
266 if cache_only and tx_points == -1:
271 if tx_points == ['*']:
275 status += tx.get('tx_hash') + ':%d:' % tx.get('height')
276 return hashlib.sha256(status).digest().encode('hex')
278 def get_merkle(self, tx_hash, height):
280 block_hash = self.bitcoind('getblockhash', [height])
281 b = self.bitcoind('getblock', [block_hash])
282 tx_list = b.get('tx')
283 tx_pos = tx_list.index(tx_hash)
285 merkle = map(hash_decode, tx_list)
286 target_hash = hash_decode(tx_hash)
288 while len(merkle) != 1:
290 merkle.append(merkle[-1])
293 new_hash = Hash(merkle[0] + merkle[1])
294 if merkle[0] == target_hash:
295 s.append(hash_encode(merkle[1]))
296 target_hash = new_hash
297 elif merkle[1] == target_hash:
298 s.append(hash_encode(merkle[0]))
299 target_hash = new_hash
304 return {"block_height": height, "merkle": s, "pos": tx_pos}
306 def add_to_history(self, addr, tx_hash, tx_pos, tx_height):
308 s = (tx_hash + int_to_hex(tx_pos, 4) + int_to_hex(tx_height, 4)).decode('hex')
310 serialized_hist = self.batch_list[addr]
312 l = len(serialized_hist)/40
313 for i in range(l-1, -1, -1):
314 item = serialized_hist[40*i:40*(i+1)]
315 item_height = int(rev_hex(item[36:40].encode('hex')), 16)
316 if item_height < tx_height:
317 serialized_hist = serialized_hist[0:40*(i+1)] + s + serialized_hist[40*(i+1):]
320 serialized_hist = s + serialized_hist
322 self.batch_list[addr] = serialized_hist
325 txo = (tx_hash + int_to_hex(tx_pos, 4)).decode('hex')
326 self.batch_txio[txo] = addr
328 def remove_from_history(self, addr, tx_hash, tx_pos):
329 txi = (tx_hash + int_to_hex(tx_pos, 4)).decode('hex')
333 addr = self.batch_txio[txi]
335 raise BaseException(tx_hash, tx_pos)
337 serialized_hist = self.batch_list[addr]
339 l = len(serialized_hist)/40
341 item = serialized_hist[40*i:40*(i+1)]
342 if item[0:36] == txi:
343 height = int(rev_hex(item[36:40].encode('hex')), 16)
344 serialized_hist = serialized_hist[0:40*i] + serialized_hist[40*(i+1):]
347 hist = self.deserialize(serialized_hist)
348 raise BaseException("prevout not found", addr, hist, tx_hash, tx_pos)
350 self.batch_list[addr] = serialized_hist
353 def deserialize_block(self, block):
354 txlist = block.get('tx')
355 tx_hashes = [] # ordered txids
356 txdict = {} # deserialized tx
358 for raw_tx in txlist:
359 tx_hash = hash_encode(Hash(raw_tx.decode('hex')))
360 tx_hashes.append(tx_hash)
361 vds = deserialize.BCDataStream()
362 vds.write(raw_tx.decode('hex'))
363 tx = deserialize.parse_Transaction(vds, is_coinbase)
366 return tx_hashes, txdict
368 def get_undo_info(self, height):
369 s = self.db.Get("undo%d" % (height % 100))
372 def write_undo_info(self, batch, height, undo_info):
373 if self.is_test or height > self.bitcoind_height - 100:
374 batch.Put("undo%d" % (height % 100), repr(undo_info))
376 def import_block(self, block, block_hash, block_height, sync, revert=False):
378 self.batch_list = {} # address -> history
379 self.batch_txio = {} # transaction i/o -> address
385 # deserialize transactions
387 tx_hashes, txdict = self.deserialize_block(block)
392 # read addresses of tx inputs
393 for tx in txdict.values():
394 for x in tx.get('inputs'):
395 txi = (x.get('prevout_hash') + int_to_hex(x.get('prevout_n'), 4)).decode('hex')
396 block_inputs.append(txi)
399 for txi in block_inputs:
401 addr = self.db.Get(txi)
403 # the input could come from the same block
405 self.batch_txio[txi] = addr
406 addr_to_read.append(addr)
409 for txid, tx in txdict.items():
410 for x in tx.get('outputs'):
411 txo = (txid + int_to_hex(x.get('index'), 4)).decode('hex')
412 block_outputs.append(txo)
414 # read histories of addresses
415 for txid, tx in txdict.items():
416 for x in tx.get('outputs'):
417 hash_160 = bc_address_to_hash_160(x.get('address'))
418 addr_to_read.append(hash_160)
421 for addr in addr_to_read:
423 self.batch_list[addr] = self.db.Get(addr)
425 self.batch_list[addr] = ''
428 undo_info = self.get_undo_info(block_height)
429 # print "undo", block_height, undo_info
437 tx_hashes = tx_hashes[::-1]
438 for txid in tx_hashes: # must be ordered
443 for x in tx.get('inputs'):
444 prevout_height, prevout_addr = self.remove_from_history(None, x.get('prevout_hash'), x.get('prevout_n'))
445 undo.append((prevout_height, prevout_addr))
446 undo_info[txid] = undo
448 for x in tx.get('outputs'):
449 hash_160 = bc_address_to_hash_160(x.get('address'))
450 self.add_to_history(hash_160, txid, x.get('index'), block_height)
453 for x in tx.get('outputs'):
454 hash_160 = bc_address_to_hash_160(x.get('address'))
455 self.remove_from_history(hash_160, txid, x.get('index'))
458 for x in tx.get('inputs'):
459 prevout_height, prevout_addr = undo_info.get(txid)[i]
462 # read the history into batch list
463 if self.batch_list.get(prevout_addr) is None:
464 self.batch_list[prevout_addr] = self.db.Get(prevout_addr)
466 # re-add them to the history
467 self.add_to_history(prevout_addr, x.get('prevout_hash'), x.get('prevout_n'), prevout_height)
468 # print_log("new hist for", hash_160_to_bc_address(prevout_addr), self.deserialize(self.batch_list[prevout_addr]) )
475 batch = leveldb.WriteBatch()
476 for addr, serialized_hist in self.batch_list.items():
477 batch.Put(addr, serialized_hist)
478 l = len(serialized_hist)
484 # add new created outputs
485 for txio, addr in self.batch_txio.items():
486 batch.Put(txio, addr)
487 # delete spent inputs
488 for txi in block_inputs:
491 self.write_undo_info(batch, block_height, undo_info)
493 # restore spent inputs
494 for txio, addr in self.batch_txio.items():
495 batch.Put(txio, addr)
496 # delete spent outputs
497 for txo in block_outputs:
501 batch.Put('height', self.serialize([(block_hash, block_height, 0)]))
504 self.db.Write(batch, sync=sync)
507 if t3 - t0 > 10 and not sync:
508 print_log("block", block_height,
509 "parse:%0.2f " % (t00 - t0),
510 "read:%0.2f " % (t1 - t00),
511 "proc:%.2f " % (t2-t1),
512 "write:%.2f " % (t3-t2),
513 "max:", max_len, hash_160_to_bc_address(max_addr))
515 for h160 in self.batch_list.keys():
516 addr = hash_160_to_bc_address(h160)
517 self.invalidate_cache(addr)
519 def add_request(self, request):
520 # see if we can get if from cache. if not, add to queue
521 if self.process(request, cache_only=True) == -1:
522 self.queue.put(request)
524 def process(self, request, cache_only=False):
525 #print "abe process", request
527 message_id = request['id']
528 method = request['method']
529 params = request.get('params', [])
533 if method == 'blockchain.numblocks.subscribe':
536 elif method == 'blockchain.headers.subscribe':
539 elif method == 'blockchain.address.subscribe':
542 result = self.get_status(address, cache_only)
543 self.watch_address(address)
544 except BaseException, e:
545 error = str(e) + ': ' + address
546 print_log("error:", error)
548 elif method == 'blockchain.address.unsubscribe':
552 if password == self.config.get('server', 'password'):
553 self.watched_addresses.remove(address)
554 print_log('unsubscribed', address)
557 print_log('incorrect password')
558 result = "authentication error"
559 except BaseException, e:
560 error = str(e) + ': ' + address
561 print_log("error:", error)
563 elif method == 'blockchain.address.get_history':
566 result = self.get_history(address, cache_only)
567 except BaseException, e:
568 error = str(e) + ': ' + address
569 print_log("error:", error)
571 elif method == 'blockchain.block.get_header':
577 result = self.get_header(height)
578 except BaseException, e:
579 error = str(e) + ': %d' % height
580 print_log("error:", error)
582 elif method == 'blockchain.block.get_chunk':
588 result = self.get_chunk(index)
589 except BaseException, e:
590 error = str(e) + ': %d' % index
591 print_log("error:", error)
593 elif method == 'blockchain.transaction.broadcast':
595 txo = self.bitcoind('sendrawtransaction', params)
596 print_log("sent tx:", txo)
598 except BaseException, e:
599 result = str(e) # do not send an error
600 print_log("error:", result, params)
602 elif method == 'blockchain.transaction.get_merkle':
608 tx_height = params[1]
609 result = self.get_merkle(tx_hash, tx_height)
610 except BaseException, e:
611 error = str(e) + ': ' + repr(params)
612 print_log("get_merkle error:", error)
614 elif method == 'blockchain.transaction.get':
618 result = self.bitcoind('getrawtransaction', [tx_hash, 0, height])
619 except BaseException, e:
620 error = str(e) + ': ' + repr(params)
621 print_log("tx get error:", error)
624 error = "unknown method:%s" % method
626 if cache_only and result == -1:
630 self.push_response({'id': message_id, 'error': error})
632 self.push_response({'id': message_id, 'result': result})
634 def watch_address(self, addr):
635 if addr not in self.watched_addresses:
636 self.watched_addresses.append(addr)
638 def catch_up(self, sync=True):
641 while not self.shared.stopped():
643 info = self.bitcoind('getinfo')
644 self.bitcoind_height = info.get('blocks')
645 bitcoind_block_hash = self.bitcoind('getblockhash', [self.bitcoind_height])
646 if self.last_hash == bitcoind_block_hash:
647 self.up_to_date = True
651 self.up_to_date = False
652 next_block_hash = self.bitcoind('getblockhash', [self.height + 1])
653 next_block = self.bitcoind('getblock', [next_block_hash, 1])
655 # fixme: this is unsafe, if we revert when the undo info is not yet written
656 revert = (random.randint(1, 100) == 1) if self.is_test else False
658 if (next_block.get('previousblockhash') == self.last_hash) and not revert:
660 self.import_block(next_block, next_block_hash, self.height+1, sync)
661 self.height = self.height + 1
662 self.write_header(self.block2header(next_block), sync)
663 self.last_hash = next_block_hash
665 if self.height % 100 == 0 and not sync:
667 print_log("catch_up: block %d (%.3fs)" % (self.height, t2 - t1))
671 # revert current block
672 block = self.bitcoind('getblock', [self.last_hash, 1])
673 print_log("blockchain reorg", self.height, block.get('previousblockhash'), self.last_hash)
674 self.import_block(block, self.last_hash, self.height, sync, revert=True)
680 # read previous header from disk
681 self.header = self.read_header(self.height)
682 self.last_hash = self.hash_header(self.header)
684 self.header = self.block2header(self.bitcoind('getblock', [self.last_hash]))
686 def memorypool_update(self):
687 mempool_hashes = self.bitcoind('getrawmempool')
689 for tx_hash in mempool_hashes:
690 if tx_hash in self.mempool_hashes:
693 tx = self.get_mempool_transaction(tx_hash)
697 for x in tx.get('inputs'):
698 txi = (x.get('prevout_hash') + int_to_hex(x.get('prevout_n'), 4)).decode('hex')
700 h160 = self.db.Get(txi)
701 addr = hash_160_to_bc_address(h160)
704 l = self.mempool_addresses.get(tx_hash, [])
707 self.mempool_addresses[tx_hash] = l
709 for x in tx.get('outputs'):
710 addr = x.get('address')
711 l = self.mempool_addresses.get(tx_hash, [])
714 self.mempool_addresses[tx_hash] = l
716 self.mempool_hashes.append(tx_hash)
718 # remove older entries from mempool_hashes
719 self.mempool_hashes = mempool_hashes
721 # remove deprecated entries from mempool_addresses
722 for tx_hash, addresses in self.mempool_addresses.items():
723 if tx_hash not in self.mempool_hashes:
724 self.mempool_addresses.pop(tx_hash)
727 new_mempool_hist = {}
728 for tx_hash, addresses in self.mempool_addresses.items():
729 for addr in addresses:
730 h = new_mempool_hist.get(addr, [])
733 new_mempool_hist[addr] = h
735 for addr in new_mempool_hist.keys():
736 if addr in self.mempool_hist.keys():
737 if self.mempool_hist[addr] != new_mempool_hist[addr]:
738 self.invalidate_cache(addr)
740 self.invalidate_cache(addr)
742 with self.mempool_lock:
743 self.mempool_hist = new_mempool_hist
745 def invalidate_cache(self, address):
746 with self.cache_lock:
747 if address in self.history_cache:
748 print_log("cache: invalidating", address)
749 self.history_cache.pop(address)
751 if address in self.watched_addresses:
752 self.address_queue.put(address)
754 def main_iteration(self):
755 if self.shared.stopped():
756 print_log("blockchain processor terminating")
764 self.memorypool_update()
766 # print "mempool:", len(self.mempool_addresses), len(self.mempool_hist), "%.3fs"%(t3 - t2)
768 if self.sent_height != self.height:
769 self.sent_height = self.height
772 'method': 'blockchain.numblocks.subscribe',
773 'params': [self.height],
776 if self.sent_header != self.header:
777 print_log("blockchain: %d (%.3fs)" % (self.height, t2 - t1))
778 self.sent_header = self.header
781 'method': 'blockchain.headers.subscribe',
782 'params': [self.header],
787 addr = self.address_queue.get(False)
790 if addr in self.watched_addresses:
791 status = self.get_status(addr)
794 'method': 'blockchain.address.subscribe',
795 'params': [addr, status],
798 if not self.shared.stopped():
799 threading.Timer(10, self.main_iteration).start()
801 print_log("blockchain processor terminating")