Skip to content

Commit bfb7079

Browse files
ahmedbodiSomberNight
authored andcommitted
[AuxPow] Add Support for individual block headers instead of a combined hex string
Array headers: fix type error Array headers: move variable initialization
1 parent 68b1728 commit bfb7079

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

src/electrumx/server/session.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,8 @@ async def block_headers(self, start_height, count, cp_height=0):
13571357
start_height and count must be non-negative integers. At most
13581358
MAX_CHUNK_SIZE headers will be returned.
13591359
'''
1360+
if self.protocol_tuple >= (1, 6):
1361+
return await self.block_headers_array(start_height, count, cp_height)
13601362
start_height = non_negative_integer(start_height)
13611363
count = non_negative_integer(count)
13621364
cp_height = non_negative_integer(cp_height)
@@ -1373,6 +1375,38 @@ async def block_headers(self, start_height, count, cp_height=0):
13731375
self.bump_cost(cost)
13741376
return result
13751377

1378+
async def block_headers_array(self, start_height, count, cp_height=0):
1379+
'''Return block headers in an array for the main chain;
1380+
starting at start_height.
1381+
start_height and count must be non-negative integers. At most
1382+
MAX_CHUNK_SIZE headers will be returned.
1383+
'''
1384+
start_height = non_negative_integer(start_height)
1385+
count = non_negative_integer(count)
1386+
cp_height = non_negative_integer(cp_height)
1387+
cost = count / 50
1388+
1389+
max_size = self.MAX_CHUNK_SIZE
1390+
count = min(count, max_size)
1391+
headers, count = await self.db.read_headers(start_height, count)
1392+
result = {'count': count, 'max': max_size, 'headers': []}
1393+
if count and cp_height:
1394+
cost += 1.0
1395+
last_height = start_height + count - 1
1396+
result.update(await self._merkle_proof(cp_height, last_height))
1397+
1398+
cursor = 0
1399+
height = 0
1400+
while cursor < len(headers):
1401+
next_cursor = self.db.header_offset(height + 1)
1402+
header = headers[cursor:next_cursor]
1403+
result['headers'].append(header.hex())
1404+
cursor = next_cursor
1405+
height += 1
1406+
1407+
self.bump_cost(cost)
1408+
return result
1409+
13761410
def is_tor(self):
13771411
'''Try to detect if the connection is to a tor hidden service we are
13781412
running.'''
@@ -1966,9 +2000,19 @@ async def block_headers(self, start_height, count, cp_height=0):
19662000
return result
19672001

19682002
# Covered by a checkpoint; truncate AuxPoW data
2003+
if self.protocol_tuple >= (1, 6):
2004+
result['headers'] = self.truncate_auxpow_headers(result['headers'])
2005+
return
2006+
19692007
result['hex'] = self.truncate_auxpow(result['hex'], start_height)
19702008
return result
19712009

2010+
def truncate_auxpow_headers(self, headers):
2011+
result = []
2012+
for header in headers:
2013+
result.append(header[:self.coin.TRUNCATED_HEADER_SIZE])
2014+
return result
2015+
19722016
def truncate_auxpow(self, headers_full_hex, start_height):
19732017
height = start_height
19742018
headers_full = util.hex_to_bytes(headers_full_hex)

0 commit comments

Comments
 (0)