Skip to content

Commit d9c2f1c

Browse files
authored
Merge pull request #1598 from carver/humanize-hash
Pretty hash display utility
2 parents e655371 + a11649f commit d9c2f1c

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

tests/trinity/core/humanize-utils/test_humanize_elapsed.py renamed to tests/trinity/core/utils/test_humanize.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from trinity.utils.humanize import humanize_elapsed
3+
from trinity.utils.humanize import humanize_elapsed, humanize_hash
44

55

66
SECOND = 1
@@ -39,3 +39,13 @@
3939
def test_humanize_elapsed(seconds, expected):
4040
actual = humanize_elapsed(seconds)
4141
assert actual == expected
42+
43+
44+
@pytest.mark.parametrize(
45+
'hash32,expected',
46+
(
47+
(bytes(range(32)), '0001..1e1f'),
48+
)
49+
)
50+
def test_humanize_hash(hash32, expected):
51+
assert humanize_hash(hash32) == expected

trinity/sync/full/chain.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
TaskQueue,
6666
)
6767
from trinity.utils.ema import EMA
68-
from trinity.utils.humanize import humanize_elapsed
68+
from trinity.utils.humanize import humanize_elapsed, humanize_hash
6969
from trinity.utils.timer import Timer
7070

7171
# (ReceiptBundle, (Receipt, (root_hash, receipt_trie_data))
@@ -537,7 +537,7 @@ async def _display_stats(self) -> None:
537537
"bps=%-3d "
538538
"tps=%-4d "
539539
"elapsed=%0.1f "
540-
"head=#%d (%s...%s) "
540+
"head=#%d %s "
541541
"age=%s"
542542
),
543543
stats.num_blocks,
@@ -546,8 +546,7 @@ async def _display_stats(self) -> None:
546546
stats.transactions_per_second,
547547
stats.elapsed,
548548
stats.latest_head.block_number,
549-
stats.latest_head.hex_hash[2:6],
550-
stats.latest_head.hex_hash[-4:],
549+
humanize_hash(stats.latest_head.hash),
551550
humanize_elapsed(head_age),
552551
)
553552

trinity/utils/humanize.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from typing import Iterator
22

3+
from eth_typing import Hash32
4+
35

46
def humanize_elapsed(seconds: int) -> str:
57
return ''.join(_humanize_elapsed(seconds))
@@ -44,3 +46,12 @@ def _humanize_elapsed(seconds: int) -> Iterator[str]:
4446
return
4547

4648
remainder %= duration
49+
50+
51+
DISPLAY_HASH_BYTES = 2
52+
53+
54+
def humanize_hash(value: Hash32) -> str:
55+
head = value[:DISPLAY_HASH_BYTES]
56+
tail = value[-1 * DISPLAY_HASH_BYTES:]
57+
return f"{head.hex()}..{tail.hex()}"

0 commit comments

Comments
 (0)