Skip to content

Commit 2c0c6f4

Browse files
committed
test: dedup file hashing using sha256sum_file helper
Rather than doing the open/read/hash-steps manually in the affected functional tests, we can just use the `sha256sum_file` helper from the utils module instead. Note that for the tool_wallet.py test, the used hash is changed from sha1 to sha256, but as the only purpose is to detect file content changes, this doesn't matter. Also, the optimization using `memoryview` is overkill here, as the opened file has only a size of 24KiB and determining the hash doesn't take longer than a few hundred micro-seconds on my machine.
1 parent aebcd18 commit 2c0c6f4

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

test/functional/rpc_dumptxoutset.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test the generation of UTXO snapshots using `dumptxoutset`.
66
"""
7+
from pathlib import Path
78

89
from test_framework.blocktools import COINBASE_MATURITY
910
from test_framework.test_framework import BitcoinTestFramework
10-
from test_framework.util import assert_equal, assert_raises_rpc_error
11-
12-
import hashlib
13-
from pathlib import Path
11+
from test_framework.util import (
12+
assert_equal,
13+
assert_raises_rpc_error,
14+
sha256sum_file,
15+
)
1416

1517

1618
class DumptxoutsetTest(BitcoinTestFramework):
@@ -39,11 +41,10 @@ def run_test(self):
3941
out['base_hash'],
4042
'09abf0e7b510f61ca6cf33bab104e9ee99b3528b371d27a2d4b39abb800fba7e')
4143

42-
with open(str(expected_path), 'rb') as f:
43-
digest = hashlib.sha256(f.read()).hexdigest()
44-
# UTXO snapshot hash should be deterministic based on mocked time.
45-
assert_equal(
46-
digest, 'b1bacb602eacf5fbc9a7c2ef6eeb0d229c04e98bdf0c2ea5929012cd0eae3830')
44+
# UTXO snapshot hash should be deterministic based on mocked time.
45+
assert_equal(
46+
sha256sum_file(str(expected_path)).hex(),
47+
'b1bacb602eacf5fbc9a7c2ef6eeb0d229c04e98bdf0c2ea5929012cd0eae3830')
4748

4849
assert_equal(
4950
out['txoutset_hash'], '1f7e3befd45dc13ae198dfbb22869a9c5c4196f8e9ef9735831af1288033f890')

test/functional/tool_wallet.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test bitcoin-wallet."""
66

7-
import hashlib
87
import os
98
import stat
109
import subprocess
@@ -13,9 +12,10 @@
1312
from collections import OrderedDict
1413

1514
from test_framework.test_framework import BitcoinTestFramework
16-
from test_framework.util import assert_equal
17-
18-
BUFFER_SIZE = 16 * 1024
15+
from test_framework.util import (
16+
assert_equal,
17+
sha256sum_file,
18+
)
1919

2020

2121
class ToolWalletTest(BitcoinTestFramework):
@@ -54,12 +54,7 @@ def assert_tool_output(self, output, *args):
5454
assert_equal(p.poll(), 0)
5555

5656
def wallet_shasum(self):
57-
h = hashlib.sha1()
58-
mv = memoryview(bytearray(BUFFER_SIZE))
59-
with open(self.wallet_path, 'rb', buffering=0) as f:
60-
for n in iter(lambda: f.readinto(mv), 0):
61-
h.update(mv[:n])
62-
return h.hexdigest()
57+
return sha256sum_file(self.wallet_path).hex()
6358

6459
def wallet_timestamp(self):
6560
return os.path.getmtime(self.wallet_path)

0 commit comments

Comments
 (0)