Skip to content

Commit

Permalink
Namecoin / AuxPoW: Truncate AuxPoW when covered by a checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyRand committed Jan 25, 2019
1 parent 7172596 commit 9838d0e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
14 changes: 9 additions & 5 deletions electrum/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def deserialize_header(s: bytes, height: int, expect_trailing_data=False, start_
h['nonce'] = hex_to_int(s[start_position+76:start_position+80])
h['block_height'] = height

if auxpow.auxpow_active(h):
if auxpow.auxpow_active(h) and height > constants.net.max_checkpoint():
if expect_trailing_data:
h['auxpow'], start_position = auxpow.deserialize_auxpow_header(h, s, expect_trailing_data=True, start_position=start_position+HEADER_SIZE)
else:
Expand Down Expand Up @@ -271,7 +271,9 @@ def update_size(self) -> None:
@classmethod
def verify_header(cls, header: dict, prev_hash: str, target: int, expected_header_hash: str=None) -> None:
_hash = hash_header(header)
_pow_hash = auxpow.hash_parent_header(header)
# Don't verify AuxPoW when covered by a checkpoint
if header.get('block_height') > constants.net.max_checkpoint():
_pow_hash = auxpow.hash_parent_header(header)
if expected_header_hash and expected_header_hash != _hash:
raise Exception("hash mismatches with expected: {} vs {}".format(expected_header_hash, _hash))
if prev_hash != header.get('prev_block_hash'):
Expand All @@ -281,9 +283,11 @@ def verify_header(cls, header: dict, prev_hash: str, target: int, expected_heade
bits = cls.target_to_bits(target)
if bits != header.get('bits'):
raise Exception("bits mismatch: %s vs %s" % (bits, header.get('bits')))
block_hash_as_num = int.from_bytes(bfh(_pow_hash), byteorder='big')
if block_hash_as_num > target:
raise Exception(f"insufficient proof of work: {block_hash_as_num} vs target {target}")
# Don't verify AuxPoW when covered by a checkpoint
if header.get('block_height') > constants.net.max_checkpoint():
block_hash_as_num = int.from_bytes(bfh(_pow_hash), byteorder='big')
if block_hash_as_num > target:
raise Exception(f"insufficient proof of work: {block_hash_as_num} vs target {target}")

def verify_chunk(self, index: int, data: bytes) -> bytes:
stripped = bytearray()
Expand Down
12 changes: 10 additions & 2 deletions electrum/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,12 @@ async def get_block_header(self, height, assert_mode):
self.print_error('requesting block header {} in mode {}'.format(height, assert_mode))
# use lower timeout as we usually have network.bhi_lock here
timeout = self.network.get_network_timeout_seconds(NetworkTimeout.Urgent)
res = await self.session.send_request('blockchain.block.header', [height], timeout=timeout)
cp_height = constants.net.max_checkpoint()
if height > cp_height:
cp_height = 0
res = await self.session.send_request('blockchain.block.header', [height, cp_height], timeout=timeout)
if cp_height != 0:
res = res["header"]
return blockchain.deserialize_header(bytes.fromhex(res), height)

async def request_chunk(self, height, tip=None, *, can_return_early=False):
Expand All @@ -361,8 +366,11 @@ async def request_chunk(self, height, tip=None, *, can_return_early=False):
size = min(size, tip - index * 2016 + 1)
size = max(size, 0)
try:
cp_height = constants.net.max_checkpoint()
if index * 2016 + size - 1 > cp_height:
cp_height = 0
self._requested_chunks.add(index)
res = await self.session.send_request('blockchain.block.headers', [index * 2016, size])
res = await self.session.send_request('blockchain.block.headers', [index * 2016, size, cp_height])
finally:
try: self._requested_chunks.remove(index)
except KeyError: pass
Expand Down
2 changes: 1 addition & 1 deletion electrum/version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ELECTRUM_VERSION = '3.3.2' # version of the client package
APK_VERSION = '3.3.2.0' # read by buildozer.spec

PROTOCOL_VERSION = '1.4' # protocol version requested
PROTOCOL_VERSION = '1.4.1' # protocol version requested

# The hash of the mnemonic seed must begin with this
SEED_PREFIX = '01' # Standard wallet
Expand Down

0 comments on commit 9838d0e

Please sign in to comment.