1 from json import dumps, loads
4 import ast, time, threading, hashlib
5 from Queue import Queue
6 import traceback, sys, os, random
9 from util import Hash, hash_encode, hash_decode, rev_hex, int_to_hex
10 from util import bc_address_to_hash_160, hash_160_to_bc_address, header_to_string, header_from_string
11 from processor import Processor, print_log
13 class BlockchainProcessor(Processor):
15 def __init__(self, config, shared):
16 Processor.__init__(self)
20 self.up_to_date = False
21 self.watched_addresses = []
22 self.history_cache = {}
24 self.cache_lock = threading.Lock()
25 self.headers_data = ''
27 self.mempool_addresses = {}
28 self.mempool_hist = {}
29 self.mempool_hashes = []
30 self.mempool_lock = threading.Lock()
32 self.address_queue = Queue()
33 self.dbpath = config.get('leveldb', 'path')
35 self.dblock = threading.Lock()
37 self.db = leveldb.LevelDB(self.dbpath)
39 traceback.print_exc(file=sys.stdout)
42 self.bitcoind_url = 'http://%s:%s@%s:%s/' % (
43 config.get('bitcoind','user'),
44 config.get('bitcoind','password'),
45 config.get('bitcoind','host'),
46 config.get('bitcoind','port'))
51 self.sent_header = None
55 hist = self.deserialize(self.db.Get('height'))
56 self.last_hash, self.height, _ = hist[0]
57 print_log( "hist", hist )
59 #traceback.print_exc(file=sys.stdout)
60 print_log('initializing database')
62 self.last_hash = '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'
65 self.init_headers(self.height)
67 threading.Timer(0, lambda: self.catch_up(sync=False)).start()
68 while not shared.stopped() and not self.up_to_date:
72 print "keyboard interrupt: stopping threads"
76 print_log( "blockchain is up to date." )
78 threading.Timer(10, self.main_iteration).start()
82 def bitcoind(self, method, params=[]):
83 postdata = dumps({"method": method, 'params': params, 'id':'jsonrpc'})
85 respdata = urllib.urlopen(self.bitcoind_url, postdata).read()
87 traceback.print_exc(file=sys.stdout)
91 if r['error'] != None:
92 raise BaseException(r['error'])
93 return r.get('result')
96 def serialize(self, h):
98 for txid, txpos, height in h:
99 s += txid + int_to_hex(txpos, 4) + int_to_hex(height, 4)
100 return s.decode('hex')
103 def deserialize(self, s):
106 txid = s[0:32].encode('hex')
107 txpos = int( rev_hex( s[32:36].encode('hex') ), 16 )
108 height = int( rev_hex( s[36:40].encode('hex') ), 16 )
109 h.append( ( txid, txpos, height ) )
114 def block2header(self, b):
115 return {"block_height":b.get('height'), "version":b.get('version'), "prev_block_hash":b.get('previousblockhash'),
116 "merkle_root":b.get('merkleroot'), "timestamp":b.get('time'), "bits":int(b.get('bits'),16), "nonce":b.get('nonce')}
119 def get_header(self, height):
120 block_hash = self.bitcoind('getblockhash', [height])
121 b = self.bitcoind('getblock', [block_hash])
122 return self.block2header(b)
125 def init_headers(self, db_height):
126 self.chunk_cache = {}
127 self.headers_filename = os.path.join( self.dbpath, 'blockchain_headers')
129 if os.path.exists(self.headers_filename):
130 height = os.path.getsize(self.headers_filename)/80 - 1 # the current height
132 prev_hash = self.hash_header(self.read_header(height))
136 open(self.headers_filename,'wb').close()
140 if height < db_height:
141 print_log( "catching up missing headers:", height, db_height)
144 while height < db_height:
146 header = self.get_header(height)
148 assert prev_hash == header.get('prev_block_hash')
149 self.write_header(header, sync=False)
150 prev_hash = self.hash_header(header)
151 if height%1000==0: print_log("headers file:",height)
152 except KeyboardInterrupt:
159 def hash_header(self, header):
160 return rev_hex(Hash(header_to_string(header).decode('hex')).encode('hex'))
163 def read_header(self, block_height):
164 if os.path.exists(self.headers_filename):
165 f = open(self.headers_filename,'rb')
166 f.seek(block_height*80)
170 h = header_from_string(h)
174 def read_chunk(self, index):
175 f = open(self.headers_filename,'rb')
176 f.seek(index*2016*80)
177 chunk = f.read(2016*80)
179 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: return
202 f = open(self.headers_filename,'rb+')
203 f.seek(self.headers_offset*80)
204 f.write(self.headers_data)
206 self.headers_data = ''
209 def get_chunk(self, i):
210 # store them on disk; store the current chunk in memory
211 with self.cache_lock:
212 chunk = self.chunk_cache.get(i)
214 chunk = self.read_chunk(i)
215 self.chunk_cache[i] = chunk
220 def get_mempool_transaction(self, txid):
222 raw_tx = self.bitcoind('getrawtransaction', [txid, 0, -1])
226 vds = deserialize.BCDataStream()
227 vds.write(raw_tx.decode('hex'))
228 out = deserialize.parse_Transaction(vds, is_coinbase = False)
232 def get_history(self, addr, cache_only=False):
233 with self.cache_lock: hist = self.history_cache.get( addr )
234 if hist is not None: return hist
235 if cache_only: return -1
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[1])
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: hist = ['*']
259 with self.cache_lock: self.history_cache[addr] = hist
263 def get_status(self, addr, cache_only=False):
264 tx_points = self.get_history(addr, cache_only)
265 if cache_only and tx_points == -1: return -1
267 if not tx_points: return None
268 if tx_points == ['*']: return '*'
271 status += tx.get('tx_hash') + ':%d:' % tx.get('height')
272 return hashlib.sha256( status ).digest().encode('hex')
275 def get_merkle(self, tx_hash, height):
277 block_hash = self.bitcoind('getblockhash', [height])
278 b = self.bitcoind('getblock', [block_hash])
279 tx_list = b.get('tx')
280 tx_pos = tx_list.index(tx_hash)
282 merkle = map(hash_decode, tx_list)
283 target_hash = hash_decode(tx_hash)
285 while len(merkle) != 1:
286 if len(merkle)%2: merkle.append( merkle[-1] )
289 new_hash = Hash( merkle[0] + merkle[1] )
290 if merkle[0] == target_hash:
291 s.append( hash_encode( merkle[1]))
292 target_hash = new_hash
293 elif merkle[1] == target_hash:
294 s.append( hash_encode( merkle[0]))
295 target_hash = new_hash
300 return {"block_height":height, "merkle":s, "pos":tx_pos}
305 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
329 def remove_from_history(self, addr, tx_hash, tx_pos):
331 txi = (tx_hash + int_to_hex(tx_pos, 4)).decode('hex')
335 addr = self.batch_txio[txi]
337 raise BaseException(tx_hash, tx_pos)
339 serialized_hist = self.batch_list[addr]
341 l = len(serialized_hist)/40
343 item = serialized_hist[40*i:40*(i+1)]
344 if item[0:36] == txi:
345 height = int( rev_hex( item[36:40].encode('hex') ), 16 )
346 serialized_hist = serialized_hist[0:40*i] + serialized_hist[40*(i+1):]
349 hist = self.deserialize(serialized_hist)
350 raise BaseException("prevout not found", addr, hist, tx_hash, tx_pos)
352 self.batch_list[addr] = serialized_hist
356 def deserialize_block(self, block):
357 txlist = block.get('tx')
358 tx_hashes = [] # ordered txids
359 txdict = {} # deserialized tx
361 for raw_tx in txlist:
362 tx_hash = hash_encode(Hash(raw_tx.decode('hex')))
363 tx_hashes.append(tx_hash)
364 vds = deserialize.BCDataStream()
365 vds.write(raw_tx.decode('hex'))
366 tx = deserialize.parse_Transaction(vds, is_coinbase)
369 return tx_hashes, txdict
371 def get_undo_info(self, height):
372 s = self.db.Get("undo%d"%(height%100))
375 def write_undo_info(self, batch, height, undo_info):
376 if self.is_test or height > self.bitcoind_height - 100:
377 batch.Put("undo%d"%(height%100), repr(undo_info))
380 def import_block(self, block, block_hash, block_height, sync, revert=False):
382 self.batch_list = {} # address -> history
383 self.batch_txio = {} # transaction i/o -> address
389 # deserialize transactions
391 tx_hashes, txdict = self.deserialize_block(block)
397 # read addresses of tx inputs
398 for tx in txdict.values():
399 for x in tx.get('inputs'):
400 txi = (x.get('prevout_hash') + int_to_hex(x.get('prevout_n'), 4)).decode('hex')
401 block_inputs.append(txi)
404 for txi in block_inputs:
406 addr = self.db.Get(txi)
408 # the input could come from the same block
410 self.batch_txio[txi] = addr
411 addr_to_read.append(addr)
414 for txid, tx in txdict.items():
415 for x in tx.get('outputs'):
416 txo = (txid + int_to_hex(x.get('index'), 4)).decode('hex')
417 block_outputs.append(txo)
419 # read histories of addresses
420 for txid, tx in txdict.items():
421 for x in tx.get('outputs'):
422 hash_160 = bc_address_to_hash_160(x.get('address'))
423 addr_to_read.append(hash_160)
426 for addr in addr_to_read:
428 self.batch_list[addr] = self.db.Get(addr)
430 self.batch_list[addr] = ''
434 undo_info = self.get_undo_info(block_height)
435 # print "undo", block_height, undo_info
441 if revert: tx_hashes = tx_hashes[::-1]
442 for txid in tx_hashes: # must be ordered
447 for x in tx.get('inputs'):
448 prevout_height, prevout_addr = self.remove_from_history( None, x.get('prevout_hash'), x.get('prevout_n'))
449 undo.append( (prevout_height, prevout_addr) )
450 undo_info[txid] = undo
452 for x in tx.get('outputs'):
453 hash_160 = bc_address_to_hash_160(x.get('address'))
454 self.add_to_history( hash_160, txid, x.get('index'), block_height)
457 for x in tx.get('outputs'):
458 hash_160 = bc_address_to_hash_160(x.get('address'))
459 self.remove_from_history( hash_160, txid, x.get('index'))
462 for x in tx.get('inputs'):
463 prevout_height, prevout_addr = undo_info.get(txid)[i]
466 # read the history into batch list
467 if self.batch_list.get(prevout_addr) is None:
468 self.batch_list[prevout_addr] = self.db.Get(prevout_addr)
470 # re-add them to the history
471 self.add_to_history( prevout_addr, x.get('prevout_hash'), x.get('prevout_n'), prevout_height)
472 # print_log( "new hist for", hash_160_to_bc_address(prevout_addr), self.deserialize(self.batch_list[prevout_addr]) )
479 batch = leveldb.WriteBatch()
480 for addr, serialized_hist in self.batch_list.items():
481 batch.Put(addr, serialized_hist)
482 l = len(serialized_hist)
488 # add new created outputs
489 for txio, addr in self.batch_txio.items():
490 batch.Put(txio, addr)
491 # delete spent inputs
492 for txi in block_inputs:
495 self.write_undo_info(batch, block_height, undo_info)
497 # restore spent inputs
498 for txio, addr in self.batch_txio.items():
499 batch.Put(txio, addr)
500 # delete spent outputs
501 for txo in block_outputs:
506 batch.Put('height', self.serialize( [(block_hash, block_height, 0)] ) )
509 self.db.Write(batch, sync = sync)
512 if t3 - t0 > 10 and not sync:
513 print_log("block", block_height,
514 "parse:%0.2f "%(t00 - t0),
515 "read:%0.2f "%(t1 - t00),
516 "proc:%.2f "%(t2-t1),
517 "write:%.2f "%(t3-t2),
518 "max:", max_len, hash_160_to_bc_address(max_addr))
520 for h160 in self.batch_list.keys():
521 addr = hash_160_to_bc_address(h160)
522 self.invalidate_cache(addr)
526 def add_request(self, request):
527 # see if we can get if from cache. if not, add to queue
528 if self.process( request, cache_only = True) == -1:
529 self.queue.put(request)
533 def process(self, request, cache_only = False):
534 #print "abe process", request
536 message_id = request['id']
537 method = request['method']
538 params = request.get('params',[])
542 if method == 'blockchain.numblocks.subscribe':
545 elif method == 'blockchain.headers.subscribe':
548 elif method == 'blockchain.address.subscribe':
551 result = self.get_status(address, cache_only)
552 self.watch_address(address)
553 except BaseException, e:
554 error = str(e) + ': ' + address
555 print_log( "error:", error )
557 elif method == 'blockchain.address.unsubscribe':
561 if password == self.config.get('server','password'):
562 self.watched_addresses.remove(address)
563 print_log('unsubscribed', address)
566 print_log('incorrect password')
567 result = "authentication error"
568 except BaseException, e:
569 error = str(e) + ': ' + address
570 print_log( "error:", error )
572 elif method == 'blockchain.address.get_history':
575 result = self.get_history( address, cache_only )
576 except BaseException, e:
577 error = str(e) + ': ' + address
578 print_log( "error:", error )
580 elif method == 'blockchain.block.get_header':
586 result = self.get_header( height )
587 except BaseException, e:
588 error = str(e) + ': %d'% height
589 print_log( "error:", error )
591 elif method == 'blockchain.block.get_chunk':
597 result = self.get_chunk( index )
598 except BaseException, e:
599 error = str(e) + ': %d'% index
600 print_log( "error:", error)
602 elif method == 'blockchain.transaction.broadcast':
604 txo = self.bitcoind('sendrawtransaction', params)
605 print_log( "sent tx:", txo )
607 except BaseException, e:
608 result = str(e) # do not send an error
609 print_log( "error:", str(e), params )
611 elif method == 'blockchain.transaction.get_merkle':
617 tx_height = params[1]
618 result = self.get_merkle(tx_hash, tx_height)
619 except BaseException, e:
620 error = str(e) + ': ' + tx_hash
621 print_log( "error:", error )
623 elif method == 'blockchain.transaction.get':
627 result = self.bitcoind('getrawtransaction', [tx_hash, 0, height] )
628 except BaseException, e:
629 error = str(e) + ': ' + tx_hash
630 print_log( "error:", error )
633 error = "unknown method:%s"%method
635 if cache_only and result == -1: return -1
638 response = { 'id':message_id, 'error':error }
639 self.push_response(response)
641 response = { 'id':message_id, 'result':result }
642 self.push_response(response)
645 def watch_address(self, addr):
646 if addr not in self.watched_addresses:
647 self.watched_addresses.append(addr)
651 def catch_up(self, sync = True):
655 while not self.shared.stopped():
658 info = self.bitcoind('getinfo')
659 self.bitcoind_height = info.get('blocks')
660 bitcoind_block_hash = self.bitcoind('getblockhash', [self.bitcoind_height])
661 if self.last_hash == bitcoind_block_hash:
662 self.up_to_date = True
666 self.up_to_date = False
667 next_block_hash = self.bitcoind('getblockhash', [self.height+1])
668 next_block = self.bitcoind('getblock', [next_block_hash, 1])
670 # fixme: this is unsafe, if we revert when the undo info is not yet written
671 revert = (random.randint(1, 100)==1) if self.is_test else False
673 if (next_block.get('previousblockhash') == self.last_hash) and not revert:
675 self.import_block(next_block, next_block_hash, self.height+1, sync)
676 self.height = self.height + 1
677 self.write_header(self.block2header(next_block), sync)
678 self.last_hash = next_block_hash
680 if (self.height)%100 == 0 and not sync:
682 print_log( "catch_up: block %d (%.3fs)"%( self.height, t2 - t1 ) )
686 # revert current block
687 block = self.bitcoind('getblock', [self.last_hash, 1])
688 print_log( "blockchain reorg", self.height, block.get('previousblockhash'), self.last_hash )
689 self.import_block(block, self.last_hash, self.height, sync, revert=True)
693 self.height = self.height -1
695 # read previous header from disk
696 self.header = self.read_header(self.height)
697 self.last_hash = self.hash_header(self.header)
700 self.header = self.block2header(self.bitcoind('getblock', [self.last_hash]))
705 def memorypool_update(self):
707 mempool_hashes = self.bitcoind('getrawmempool')
709 for tx_hash in mempool_hashes:
710 if tx_hash in self.mempool_hashes: continue
712 tx = self.get_mempool_transaction(tx_hash)
715 for x in tx.get('inputs'):
716 txi = (x.get('prevout_hash') + int_to_hex(x.get('prevout_n'), 4)).decode('hex')
718 h160 = self.db.Get(txi)
719 addr = hash_160_to_bc_address(h160)
722 l = self.mempool_addresses.get(tx_hash, [])
725 self.mempool_addresses[tx_hash] = l
727 for x in tx.get('outputs'):
728 addr = x.get('address')
729 l = self.mempool_addresses.get(tx_hash, [])
732 self.mempool_addresses[tx_hash] = l
734 self.mempool_hashes.append(tx_hash)
736 # remove older entries from mempool_hashes
737 self.mempool_hashes = mempool_hashes
739 # remove deprecated entries from mempool_addresses
740 for tx_hash, addresses in self.mempool_addresses.items():
741 if tx_hash not in self.mempool_hashes:
742 self.mempool_addresses.pop(tx_hash)
745 new_mempool_hist = {}
746 for tx_hash, addresses in self.mempool_addresses.items():
747 for addr in addresses:
748 h = new_mempool_hist.get(addr, [])
751 new_mempool_hist[addr] = h
753 for addr in new_mempool_hist.keys():
754 if addr in self.mempool_hist.keys():
755 if self.mempool_hist[addr] != new_mempool_hist[addr]:
756 self.invalidate_cache(addr)
758 self.invalidate_cache(addr)
760 with self.mempool_lock:
761 self.mempool_hist = new_mempool_hist
765 def invalidate_cache(self, address):
766 with self.cache_lock:
767 if self.history_cache.has_key(address):
768 print_log( "cache: invalidating", address )
769 self.history_cache.pop(address)
771 if address in self.watched_addresses:
772 self.address_queue.put(address)
776 def main_iteration(self):
778 if self.shared.stopped():
779 print_log( "blockchain processor terminating" )
787 self.memorypool_update()
789 # print "mempool:", len(self.mempool_addresses), len(self.mempool_hist), "%.3fs"%(t3 - t2)
792 if self.sent_height != self.height:
793 self.sent_height = self.height
794 self.push_response({ 'id': None, 'method':'blockchain.numblocks.subscribe', 'params':[self.height] })
796 if self.sent_header != self.header:
797 print_log( "blockchain: %d (%.3fs)"%( self.height, t2 - t1 ) )
798 self.sent_header = self.header
799 self.push_response({ 'id': None, 'method':'blockchain.headers.subscribe', 'params':[self.header] })
803 addr = self.address_queue.get(False)
806 if addr in self.watched_addresses:
807 status = self.get_status( addr )
808 self.push_response({ 'id': None, 'method':'blockchain.address.subscribe', 'params':[addr, status] })
810 if not self.shared.stopped():
811 threading.Timer(10, self.main_iteration).start()
813 print_log( "blockchain processor terminating" )