Increase merkle cache test coverage to 100%

This commit is contained in:
Neil Booth 2018-07-16 09:53:43 +08:00
parent 4168341857
commit 26d8026938
2 changed files with 12 additions and 15 deletions

View File

@ -187,24 +187,20 @@ class MerkleCache(object):
# Retain the value of depth_higher; in practice this is fine # Retain the value of depth_higher; in practice this is fine
start = self._leaf_start(self.length) start = self._leaf_start(self.length)
hashes = self.source.hashes(start, length - start) hashes = self.source.hashes(start, length - start)
self.level[start >> self.depth_higher] = self._level(hashes) self.level[start >> self.depth_higher:] = self._level(hashes)
self.length = length self.length = length
def _level_for(self, length): def _level_for(self, length):
'''Return a (level_length, final_hash) pair for a truncation '''Return a (level_length, final_hash) pair for a truncation
of the hashes to the given length. Length may be an extension, of the hashes to the given length.'''
in which case extra hashes are requested from the source.''' if length == self.length:
if length > self.length: return self.level
hashes = self.source.hashes(self.length, length - self.length) level = self.level[:length >> self.depth_higher]
return self.level + self._level(hashes) leaf_start = self._leaf_start(length)
if length < self.length: count = min(self._segment_length(), length - leaf_start)
level = self.level[:length >> self.depth_higher] hashes = self.source.hashes(leaf_start, count)
leaf_start = self._leaf_start(length) level += self._level(hashes)
count = min(self._segment_length(), length - leaf_start) return level
hashes = self.source.hashes(leaf_start, count)
level += self._level(hashes)
return level
return self.level
def branch_and_root(self, length, index): def branch_and_root(self, length, index):
'''Return a merkle branch and root. Length is the number of '''Return a merkle branch and root. Length is the number of

View File

@ -172,7 +172,7 @@ def test_merkle_cache():
assert root == root2 assert root == root2
def merkle_cache_extension(): def test_merkle_cache_extension():
source = Source(64) source = Source(64)
for length in range(14, 18): for length in range(14, 18):
for cp_length in range(30, 36): for cp_length in range(30, 36):
@ -201,6 +201,7 @@ def test_markle_cache_bad():
with pytest.raises(ValueError): with pytest.raises(ValueError):
cache.branch_and_root(3, 3) cache.branch_and_root(3, 3)
def test_bad_extension(): def test_bad_extension():
length = 5 length = 5
source = Source(length) source = Source(length)