This repository was archived by the owner on Aug 31, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathname_byhash.py
More file actions
executable file
·79 lines (62 loc) · 2.7 KB
/
Copy pathname_byhash.py
File metadata and controls
executable file
·79 lines (62 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
# Copyright (c) 2019 Daniel Kraft
# Distributed under the MIT/X11 software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
# RPC test for lookups of names by hash rather than preimage.
from test_framework.names import NameTestFramework
from test_framework.util import *
import hashlib
class NameByHashTest (NameTestFramework):
def set_test_params (self):
# We start without -namehashindex initially so that we can test the
# "not enabled" error first.
self.setup_name_test ([["-namehistory"]] * 1)
self.setup_clean_chain = True
def run_test (self):
node = self.nodes[0]
node.generate (200)
name = "testname"
nameHex = name.encode ("ascii").hex ()
singleHash = hashlib.new ("sha256", name.encode ("ascii")).digest ()
doubleHashHex = hashlib.new ("sha256", singleHash).hexdigest ()
byHashOptions = {"nameEncoding": "hex", "byHash": "sha256d"}
# Start by setting up a test name.
new = node.name_new (name)
node.generate (10)
self.firstupdateName (0, name, new, "value")
node.generate (5)
# Check looking up "direct".
res = node.name_show (name, {"byHash": "direct"})
assert_equal (res["name"], name)
assert_equal (res["value"], "value")
# -namehashindex is not enabled yet.
assert_raises_rpc_error (-1, 'namehashindex is not enabled',
node.name_show, doubleHashHex, byHashOptions)
assert_equal (node.getindexinfo ("namehash"), {})
# Restart the node and enable indexing.
self.restart_node (0, extra_args=["-namehashindex", "-namehistory"])
self.wait_until (
lambda: all (i["synced"] for i in node.getindexinfo ().values ()))
assert_equal (node.getindexinfo ("namehash"), {
"namehash": {
"synced": True,
"best_block_height": node.getblockcount (),
}
})
# Now the lookup by hash should work.
res = node.name_show (doubleHashHex, byHashOptions)
assert_equal (res["name"], nameHex)
assert_equal (res["value"], "value")
res = node.name_history (doubleHashHex, byHashOptions)
assert_equal (len (res), 1)
assert_equal (res[0]["name"], nameHex)
# Unknown name by hash.
assert_raises_rpc_error (-4, "name hash not found",
node.name_show, "42" * 32, byHashOptions)
# General errors with the parameters.
assert_raises_rpc_error (-8, "Invalid value for byHash",
node.name_show, doubleHashHex, {"byHash": "foo"})
assert_raises_rpc_error (-8, "must be 32 bytes long",
node.name_show, "abcd", {"byHash": "sha256d"})
if __name__ == '__main__':
NameByHashTest ().main ()