forked from namecoin/namecoin-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
name_scanning.py
executable file
·141 lines (116 loc) · 5.44 KB
/
name_scanning.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
# Copyright (c) 2014-2018 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 the name_scan RPC method.
from test_framework.names import NameTestFramework
from test_framework.util import *
class NameScanningTest (NameTestFramework):
def set_test_params (self):
self.setup_name_test ([[]] * 1)
def run_test (self):
self.node = self.nodes[0]
# Mine a block so that we're no longer in initial download.
self.node.generate (1)
# Initially, all should be empty.
assert_equal (self.node.name_scan (), [])
assert_equal (self.node.name_scan ("foo", 10), [])
# Register some names with various data, heights and expiration status.
# Using both "aa" and "b" ensures that we can also check for the expected
# comparison order between string length and lexicographic ordering.
newA = self.node.name_new ("d/a")
newAA = self.node.name_new ("d/aa")
newB = self.node.name_new ("d/b")
newC = self.node.name_new ("d/c")
self.node.generate (15)
self.firstupdateName (0, "d/a", newA, "wrong value")
self.firstupdateName (0, "d/aa", newAA, "value aa")
self.firstupdateName (0, "d/b", newB, "value b")
self.node.generate (15)
self.firstupdateName (0, "d/c", newC, "value c")
self.node.name_update ("d/a", "value a")
self.node.generate (20)
# Check the expected name_scan data values.
scan = self.node.name_scan ()
assert_equal (len (scan), 4)
self.checkNameData (scan[0], "d/a", "value a", 11, False)
self.checkNameData (scan[1], "d/b", "value b", -4, True)
self.checkNameData (scan[2], "d/c", "value c", 11, False)
self.checkNameData (scan[3], "d/aa", "value aa", -4, True)
# Check for expected names in various basic name_scan calls.
self.checkList (self.node.name_scan (), ["d/a", "d/b", "d/c", "d/aa"])
self.checkList (self.node.name_scan ("", 0), [])
self.checkList (self.node.name_scan ("", -1), [])
self.checkList (self.node.name_scan ("d/b"), ["d/b", "d/c", "d/aa"])
self.checkList (self.node.name_scan ("d/zz"), [])
self.checkList (self.node.name_scan ("", 2), ["d/a", "d/b"])
self.checkList (self.node.name_scan ("d/b", 1), ["d/b"])
# Verify encoding for start argument.
self.checkList (self.node.name_scan ("642f63", 10, {"nameEncoding": "hex"}),
["642f63", "642f6161"])
# Verify filtering based on number of confirmations.
self.checkList (self.node.name_scan ("", 100, {"minConf": 35}),
["d/b", "d/aa"])
self.checkList (self.node.name_scan ("", 100, {"minConf": 36}), [])
self.checkList (self.node.name_scan ("", 100, {"maxConf": 19}), [])
self.checkList (self.node.name_scan ("", 100, {"maxConf": 20}),
["d/a", "d/c"])
# Verify interaction with filtering and count.
self.checkList (self.node.name_scan ("", 1, {"maxConf": 20}), ["d/a"])
self.checkList (self.node.name_scan ("", 2, {"maxConf": 20}),
["d/a", "d/c"])
# Error checks for confirmation options.
assert_raises_rpc_error (-8, "minConf must be >= 1",
self.node.name_scan, "", 100, {"minConf": 0})
assert_raises_rpc_error (-8, "minConf must be >= 1",
self.node.name_scan, "", 100, {"minConf": -42})
self.node.name_scan ("", 100, {"minConf": 1})
assert_raises_rpc_error (-8, "maxConf must not be negative",
self.node.name_scan, "", 100, {"maxConf": -1})
self.node.name_scan ("", 100, {"maxConf": 0})
# Verify filtering based on prefix.
self.checkList (self.node.name_scan ("", 100, {"prefix": ""}),
["d/a", "d/b", "d/c", "d/aa"])
self.checkList (self.node.name_scan ("", 100, {"prefix": "d/a"}),
["d/a", "d/aa"])
# Check prefix and nameEncoding.
options = {
"prefix": "642f61",
"nameEncoding": "hex",
}
self.checkList (self.node.name_scan ("", 100, options),
["642f61", "642f6161"])
assert_raises_rpc_error (-1000, "Name/value is invalid for encoding ascii",
self.node.name_scan, "", 100, {"prefix": "äöü"})
# Verify filtering based on regexp.
self.checkList (self.node.name_scan ("", 100, {"regexp": "[ac]"}),
["d/a", "d/c", "d/aa"])
# Multiple filters are combined using "and".
options = {
"prefix": "d/a",
"maxConf": 20,
}
self.checkList (self.node.name_scan ("", 100, options), ["d/a"])
# Include a name with invalid UTF-8 to make sure it doesn't break
# the regexp check.
self.restart_node (0, extra_args=["-nameencoding=hex"])
hexName = "642f00ff"
new = self.node.name_new (hexName)
self.node.generate (10)
self.firstupdateName (0, hexName, new, "{}")
self.node.generate (5)
fullHexList = ['642f61', '642f62', '642f63', hexName, '642f6161']
self.checkList (self.node.name_scan (), fullHexList)
self.checkList (self.node.name_scan ("", 100, {"regexp": "a"}),
['642f61', '642f6161'])
def checkList (self, data, names):
"""
Check that the result in 'data' contains the names
given in the array 'names'.
"""
def walker (e):
return e['name']
dataNames = list (map (walker, data))
assert_equal (dataNames, names)
if __name__ == '__main__':
NameScanningTest ().main ()