blockchain: move init_headers_file from network.py to blockchain.py

and don't run it every time the network restarts
This commit is contained in:
SomberNight 2020-02-27 20:45:29 +01:00
parent b21bcf5977
commit ce81957d25
No known key found for this signature in database
GPG key ID: B33B5F232C6271E9
2 changed files with 15 additions and 14 deletions

View file

@ -159,6 +159,20 @@ _CHAINWORK_CACHE = {
} # type: Dict[str, int] } # type: Dict[str, int]
def init_headers_file_for_best_chain():
b = get_best_chain()
filename = b.path()
length = HEADER_SIZE * len(constants.net.CHECKPOINTS) * 2016
if not os.path.exists(filename) or os.path.getsize(filename) < length:
with open(filename, 'wb') as f:
if length > 0:
f.seek(length - 1)
f.write(b'\x00')
util.ensure_sparse_file(filename)
with b.lock:
b.update_size()
class Blockchain(Logger): class Blockchain(Logger):
""" """
Manages blockchain headers and their verification Manages blockchain headers and their verification

View file

@ -253,6 +253,7 @@ class Network(Logger):
self.daemon = daemon self.daemon = daemon
blockchain.read_blockchains(self.config) blockchain.read_blockchains(self.config)
blockchain.init_headers_file_for_best_chain()
self.logger.info(f"blockchains {list(map(lambda b: b.forkpoint, blockchain.blockchains.values()))}") self.logger.info(f"blockchains {list(map(lambda b: b.forkpoint, blockchain.blockchains.values()))}")
self._blockchain_preferred_block = self.config.get('blockchain_preferred_block', None) # type: Optional[Dict] self._blockchain_preferred_block = self.config.get('blockchain_preferred_block', None) # type: Optional[Dict]
self._blockchain = blockchain.get_best_chain() self._blockchain = blockchain.get_best_chain()
@ -776,19 +777,6 @@ class Network(Logger):
return False return False
return True return True
async def _init_headers_file(self):
b = blockchain.get_best_chain()
filename = b.path()
length = HEADER_SIZE * len(constants.net.CHECKPOINTS) * 2016
if not os.path.exists(filename) or os.path.getsize(filename) < length:
with open(filename, 'wb') as f:
if length > 0:
f.seek(length-1)
f.write(b'\x00')
util.ensure_sparse_file(filename)
with b.lock:
b.update_size()
def best_effort_reliable(func): def best_effort_reliable(func):
async def make_reliable_wrapper(self: 'Network', *args, **kwargs): async def make_reliable_wrapper(self: 'Network', *args, **kwargs):
for i in range(10): for i in range(10):
@ -1133,7 +1121,6 @@ class Network(Logger):
async def main(): async def main():
self.logger.info("starting taskgroup.") self.logger.info("starting taskgroup.")
try: try:
await self._init_headers_file()
# note: if a task finishes with CancelledError, that # note: if a task finishes with CancelledError, that
# will NOT raise, and the group will keep the other tasks running # will NOT raise, and the group will keep the other tasks running
async with taskgroup as group: async with taskgroup as group: