fix repair and tests

This commit is contained in:
Victor Shyba 2019-07-23 16:02:05 -03:00 committed by Lex Berezhny
parent 6f5a595975
commit afeff61268
2 changed files with 5 additions and 4 deletions

View file

@ -109,7 +109,6 @@ class BasicHeadersTests(BitcoinHeadersTestCase):
# corrupt by appending # corrupt by appending
headers.io.seek(block_bytes(len(headers))) headers.io.seek(block_bytes(len(headers)))
headers.io.write(b"appending") headers.io.write(b"appending")
headers._size = None
await headers.repair() await headers.repair()
self.assertEqual(headers.height, 1499) self.assertEqual(headers.height, 1499)
await headers.connect(len(headers), self.get_bytes(block_bytes(3001 - 1500), after=block_bytes(1500))) await headers.connect(len(headers), self.get_bytes(block_bytes(3001 - 1500), after=block_bytes(1500)))

View file

@ -170,10 +170,12 @@ class BaseHeaders:
async def repair(self): async def repair(self):
previous_header_hash = fail = None previous_header_hash = fail = None
self.io.seek(0) batch_size = 36
batch_size = 10000
for start_height in range(0, self.height, batch_size): for start_height in range(0, self.height, batch_size):
self.io.seek(self.header_size * start_height)
headers = self.io.read(self.header_size*batch_size) headers = self.io.read(self.header_size*batch_size)
if len(headers) % self.header_size != 0:
headers = headers[:(len(headers) // self.header_size) * self.header_size]
for header_hash, header in self._iterate_headers(start_height, headers): for header_hash, header in self._iterate_headers(start_height, headers):
height = header['block_height'] height = header['block_height']
if height: if height:
@ -205,7 +207,7 @@ class BaseHeaders:
end = min(len(headers), end + self.chunk_size * self.header_size) end = min(len(headers), end + self.chunk_size * self.header_size)
def _iterate_headers(self, height: int, headers: bytes) -> Iterator[Tuple[bytes, dict]]: def _iterate_headers(self, height: int, headers: bytes) -> Iterator[Tuple[bytes, dict]]:
assert len(headers) % self.header_size == 0 assert len(headers) % self.header_size == 0, len(headers)
for idx in range(len(headers) // self.header_size): for idx in range(len(headers) // self.header_size):
start, end = idx * self.header_size, (idx + 1) * self.header_size start, end = idx * self.header_size, (idx + 1) * self.header_size
header = headers[start:end] header = headers[start:end]