Browse Source

Fix fs_cache tx count

master
Neil Booth 8 years ago
parent
commit
cdbb6b093a
  1. 19
      server/cache.py
  2. 2
      server/controller.py

19
server/cache.py

@ -276,7 +276,6 @@ class FSCache(LoggedClass):
# On-disk values, updated by a flush # On-disk values, updated by a flush
self.height = height self.height = height
self.tx_count = tx_count
# Unflushed items # Unflushed items
self.headers = [] self.headers = []
@ -289,6 +288,10 @@ class FSCache(LoggedClass):
self.tx_counts = array.array('I') self.tx_counts = array.array('I')
self.txcount_file.seek(0) self.txcount_file.seek(0)
self.tx_counts.fromfile(self.txcount_file, self.height + 1) self.tx_counts.fromfile(self.txcount_file, self.height + 1)
if self.tx_counts:
assert tx_count == self.tx_counts[-1]
else:
assert tx_count == 0
def open_file(self, filename, create=False): def open_file(self, filename, create=False):
'''Open the file name. Return its handle.''' '''Open the file name. Return its handle.'''
@ -299,6 +302,8 @@ class FSCache(LoggedClass):
return open(filename, 'wb+') return open(filename, 'wb+')
raise raise
return self.tx_counts[self.height] if self.tx_counts else 0
def process_block(self, block): def process_block(self, block):
'''Process a new block and return (header, tx_hashes, txs)''' '''Process a new block and return (header, tx_hashes, txs)'''
assert len(self.tx_counts) == self.height + 1 + len(self.headers) assert len(self.tx_counts) == self.height + 1 + len(self.headers)
@ -308,7 +313,8 @@ class FSCache(LoggedClass):
# Cache the new header, tx hashes and cumulative tx count # Cache the new header, tx hashes and cumulative tx count
self.headers.append(header) self.headers.append(header)
self.tx_hashes.append(tx_hashes) self.tx_hashes.append(tx_hashes)
self.tx_counts.append(self.tx_count + len(txs)) prior_tx_count = self.tx_counts[-1] if self.tx_counts else 0
self.tx_counts.append(prior_tx_count + len(txs))
return triple return triple
@ -320,6 +326,9 @@ class FSCache(LoggedClass):
assert self.height + block_count == new_height assert self.height + block_count == new_height
assert len(self.tx_hashes) == block_count assert len(self.tx_hashes) == block_count
assert len(self.tx_counts) == self.height + 1 + block_count assert len(self.tx_counts) == self.height + 1 + block_count
assert new_tx_count == self.tx_counts[-1] if self.tx_counts else 0
prior_tx_count = self.tx_counts[self.height] if self.height >= 0 else 0
tx_diff = new_tx_count - prior_tx_count
# First the headers # First the headers
headers = b''.join(self.headers) headers = b''.join(self.headers)
@ -336,9 +345,9 @@ class FSCache(LoggedClass):
# Finally the hashes # Finally the hashes
hashes = memoryview(b''.join(itertools.chain(*self.tx_hashes))) hashes = memoryview(b''.join(itertools.chain(*self.tx_hashes)))
assert len(hashes) % 32 == 0 assert len(hashes) % 32 == 0
assert self.tx_count + len(hashes) // 32 == new_tx_count assert len(hashes) // 32 == tx_diff
cursor = 0 cursor = 0
file_pos = self.tx_count * 32 file_pos = prior_tx_count * 32
while cursor < len(hashes): while cursor < len(hashes):
file_num, offset = divmod(file_pos, self.tx_hash_file_size) file_num, offset = divmod(file_pos, self.tx_hash_file_size)
size = min(len(hashes) - cursor, self.tx_hash_file_size - offset) size = min(len(hashes) - cursor, self.tx_hash_file_size - offset)
@ -351,11 +360,9 @@ class FSCache(LoggedClass):
os.sync() os.sync()
tx_diff = new_tx_count - self.tx_count
self.tx_hashes = [] self.tx_hashes = []
self.headers = [] self.headers = []
self.height += block_count self.height += block_count
self.tx_count = new_tx_count
return tx_diff return tx_diff

2
server/controller.py

@ -46,7 +46,7 @@ class Controller(LoggedClass):
self.logger.info('RPC server listening on {}:{:d}' self.logger.info('RPC server listening on {}:{:d}'
.format(host, env.rpc_port)) .format(host, env.rpc_port))
protocol = partial(ElectrumX, self, self.db, self.daemon, env) protocol = partial(ElectrumX, self, self.daemon, env)
if env.tcp_port is not None: if env.tcp_port is not None:
tcp_server = loop.create_server(protocol, env.host, env.tcp_port) tcp_server = loop.create_server(protocol, env.host, env.tcp_port)
self.servers.append(loop.run_until_complete(tcp_server)) self.servers.append(loop.run_until_complete(tcp_server))

Loading…
Cancel
Save