Skip to content

Commit

Permalink
Strip auxpow headers from on-disk storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyRand committed Jun 30, 2018
1 parent 57ddd12 commit 4fa905e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
16 changes: 16 additions & 0 deletions lib/auxpow.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ def get_chain_id(base_header):
return base_header['version'] >> 16

def deserialize_auxpow_header(base_header, s, expect_trailing_data=False):
if len(s) == 0 and not expect_trailing_data:
return None

auxpow_header = {}

# Chain ID is the top 16 bits of the 32-bit version.
Expand Down Expand Up @@ -118,6 +121,19 @@ def deserialize_merkle_branch(s):
index = vds.read_int32()
return hashes, index, s[vds.read_cursor:]

# TODO: This is dead code that will probably be removed.
def strip_auxpow_headers(index, chunk):
result = bytearray()
trailing_data = chunk

i = 0
while len(trailing_data) > 0:
header, trailing_data = electrum_nmc.blockchain.deserialize_header(trailing_data, index*2016 + i, expect_trailing_data=True)
result.extend(bfh(electrum_nmc.blockchain.serialize_header(header)))
i = i + 1

return bytes(result)

def hash_parent_header(header):
if not auxpow_active(header):
return electrum_nmc.blockchain.hash_header(header)
Expand Down
10 changes: 9 additions & 1 deletion lib/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ def verify_header(self, header, prev_hash, target):
raise Exception("insufficient proof of work: %s vs target %s" % (int('0x' + _hash, 16), target))

def verify_chunk(self, index, data):
stripped = bytearray()
trailing_data = data
prev_hash = self.get_hash(index * 2016 - 1)
target = self.get_target(index-1)
Expand All @@ -195,8 +196,14 @@ def verify_chunk(self, index, data):
header, trailing_data = deserialize_header(trailing_data, index*2016 + i, expect_trailing_data=True)
self.verify_header(header, prev_hash, target)
prev_hash = hash_header(header)

# Strip auxpow header for disk
stripped.extend(bfh(serialize_header(header)))

i = i + 1

return bytes(stripped)

def path(self):
d = util.get_headers_dir(self.config)
filename = 'blockchain_headers' if self.parent_id is None else os.path.join('forks', 'fork_%d_%d'%(self.parent_id, self.checkpoint))
Expand Down Expand Up @@ -381,7 +388,8 @@ def can_connect(self, header, check_height=True):
def connect_chunk(self, idx, hexdata):
try:
data = bfh(hexdata)
self.verify_chunk(idx, data)
# verify_chunk also strips the AuxPoW headers
data = self.verify_chunk(idx, data)
#self.print_error("validated chunk %d" % idx)
self.save_chunk(idx, data)
return True
Expand Down

0 comments on commit 4fa905e

Please sign in to comment.