Skip to content

Commit e3dcef1

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 7f7e301 commit e3dcef1

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/electrumx/server/session.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,8 @@ async def block_headers(self, start_height, count, cp_height=0):
13461346
start_height and count must be non-negative integers. At most
13471347
MAX_CHUNK_SIZE headers will be returned.
13481348
'''
1349+
if self.protocol_tuple >= (1, 6):
1350+
return await self.block_headers_array(start_height, count, cp_height)
13491351
start_height = non_negative_integer(start_height)
13501352
count = non_negative_integer(count)
13511353
cp_height = non_negative_integer(cp_height)
@@ -1362,6 +1364,38 @@ async def block_headers(self, start_height, count, cp_height=0):
13621364
self.bump_cost(cost)
13631365
return result
13641366

1367+
async def block_headers_array(self, start_height, count, cp_height=0):
1368+
'''Return block headers in an array for the main chain;
1369+
starting at start_height.
1370+
start_height and count must be non-negative integers. At most
1371+
MAX_CHUNK_SIZE headers will be returned.
1372+
'''
1373+
start_height = non_negative_integer(start_height)
1374+
count = non_negative_integer(count)
1375+
cp_height = non_negative_integer(cp_height)
1376+
cost = count / 50
1377+
1378+
max_size = self.MAX_CHUNK_SIZE
1379+
count = min(count, max_size)
1380+
headers, count = await self.db.read_headers(start_height, count)
1381+
result = {'count': count, 'max': max_size, 'headers': []}
1382+
if count and cp_height:
1383+
cost += 1.0
1384+
last_height = start_height + count - 1
1385+
result.update(await self._merkle_proof(cp_height, last_height))
1386+
1387+
cursor = 0
1388+
height = 0
1389+
while cursor < len(headers):
1390+
next_cursor = self.db.header_offset(height + 1)
1391+
header = headers[cursor:next_cursor]
1392+
result['headers'].append(header.hex())
1393+
cursor = next_cursor
1394+
height += 1
1395+
1396+
self.bump_cost(cost)
1397+
return result
1398+
13651399
def is_tor(self):
13661400
'''Try to detect if the connection is to a tor hidden service we are
13671401
running.'''
@@ -1957,9 +1991,19 @@ async def block_headers(self, start_height, count, cp_height=0):
19571991
return result
19581992

19591993
# Covered by a checkpoint; truncate AuxPoW data
1994+
if self.protocol_tuple >= (1, 6):
1995+
result['headers'] = self.truncate_auxpow_headers(result['headers'])
1996+
return
1997+
19601998
result['hex'] = self.truncate_auxpow(result['hex'], start_height)
19611999
return result
19622000

2001+
def truncate_auxpow_headers(self, headers):
2002+
result = []
2003+
for header in headers:
2004+
result.append(header[:self.coin.TRUNCATED_HEADER_SIZE])
2005+
return result
2006+
19632007
def truncate_auxpow(self, headers_full_hex, start_height):
19642008
height = start_height
19652009
headers_full = util.hex_to_bytes(headers_full_hex)

0 commit comments

Comments
 (0)