X-Git-Url: https://git.novaco.in/?p=electrum-server.git;a=blobdiff_plain;f=backends%2Fbitcoind%2Fblockchain_processor.py;h=0a133db4af792e7d24f02562cfb49d68a88a91b9;hp=cf5f60964b244eb82c52e53ad5c91e147677cdbb;hb=b781bb56313255e668fffe45a09ed330a9887c13;hpb=f80e689bf86e7ca5998884bca0c7160e6ec81ce5 diff --git a/backends/bitcoind/blockchain_processor.py b/backends/bitcoind/blockchain_processor.py index cf5f609..0a133db 100644 --- a/backends/bitcoind/blockchain_processor.py +++ b/backends/bitcoind/blockchain_processor.py @@ -87,7 +87,8 @@ class BlockchainProcessor(Processor): self.memorypool_update() print_log("Memory pool initialized.") - threading.Timer(10, self.main_iteration).start() + self.timer = threading.Timer(10, self.main_iteration) + self.timer.start() @@ -490,10 +491,10 @@ class BlockchainProcessor(Processor): error = str(e) + ': ' + address print_log("error:", error) - elif method == 'blockchain.address.get_path': + elif method == 'blockchain.address.get_proof': try: address = str(params[0]) - result = self.storage.get_address_path(address) + result = self.storage.get_proof(address) except BaseException, e: error = str(e) + ': ' + address print_log("error:", error) @@ -506,12 +507,22 @@ class BlockchainProcessor(Processor): error = str(e) + ': ' + address print_log("error:", error) + elif method == 'blockchain.utxo.get_address': + try: + txid = str(params[0]) + pos = int(params[1]) + txi = (txid + int_to_hex(pos, 4)).decode('hex') + result = self.storage.get_address(txi) + except BaseException, e: + error = str(e) + print_log("error:", error, params) + elif method == 'blockchain.block.get_header': if cache_only: result = -1 else: try: - height = params[0] + height = int(params[0]) result = self.get_header(height) except BaseException, e: error = str(e) + ': %d' % height @@ -522,7 +533,7 @@ class BlockchainProcessor(Processor): result = -1 else: try: - index = params[0] + index = int(params[0]) result = self.get_chunk(index) except BaseException, e: error = str(e) + ': %d' % index @@ -602,7 +613,7 @@ class BlockchainProcessor(Processor): def catch_up(self, sync=True): - prh = None + prev_root_hash = None while not self.shared.stopped(): self.mtime('') @@ -626,6 +637,8 @@ class BlockchainProcessor(Processor): if (next_block.get('previousblockhash') == self.storage.last_hash) and not revert: + prev_root_hash = self.storage.get_root_hash() + self.import_block(next_block, next_block_hash, self.storage.height+1, sync) self.storage.height = self.storage.height + 1 self.write_header(self.block2header(next_block), sync) @@ -639,12 +652,7 @@ class BlockchainProcessor(Processor): self.mtimes['daemon'] = 0 self.mtimes['import'] = 0 - if prh: - assert prh == self.storage.get_root_hash().encode('hex') - prh = None - else: - prh = self.storage.get_root_hash().encode('hex') # revert current block block = self.getfullblock(self.storage.last_hash) @@ -659,8 +667,13 @@ class BlockchainProcessor(Processor): self.header = self.read_header(self.storage.height) self.storage.last_hash = self.hash_header(self.header) + if prev_root_hash: + assert prev_root_hash == self.storage.get_root_hash() + prev_root_hash = None + self.header = self.block2header(self.bitcoind('getblock', [self.storage.last_hash])) + self.header['utxo_root'] = self.storage.get_root_hash().encode('hex') if self.shared.stopped(): print_log( "closing database" ) @@ -736,10 +749,17 @@ class BlockchainProcessor(Processor): # TODO: update cache here. if new value equals cached value, do not send notification self.address_queue.put((address,sessions)) + + def close(self): + self.timer.join() + print_log("Closing database...") + self.storage.close() + print_log("Database is closed") + + def main_iteration(self): if self.shared.stopped(): - print_log("blockchain processor terminating") - self.storage.close() + print_log("Stopping timer") return with self.dblock: @@ -782,7 +802,7 @@ class BlockchainProcessor(Processor): 'params': [addr, status], }) - if not self.shared.stopped(): - threading.Timer(10, self.main_iteration).start() - else: - print_log("blockchain processor terminating") + # next iteration + self.timer = threading.Timer(10, self.main_iteration) + self.timer.start() +