forked from namecoin/namecoin-core
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathname_listunspent.py
executable file
·139 lines (117 loc) · 5.22 KB
/
name_listunspent.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
#!/usr/bin/env python3
# Copyright (c) 2018-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 correct handling of names in "listunspent".
from test_framework.names import NameTestFramework
from test_framework.util import *
class NameListUnspentTest (NameTestFramework):
def set_test_params (self):
self.setup_name_test ([["-allowexpired"]] * 2)
def lookupName (self, ind, name, **kwargs):
"""Wrapper around lookup that gets txid and vout from the name."""
pending = self.nodes[ind].name_pending (name)
if len (pending) > 0:
data = pending[0]
else:
data = self.nodes[ind].name_show (name)
return self.lookup (ind, data['txid'], data['vout'], **kwargs)
def lookup (self, ind, txid, vout,
addressFilter=None,
allowUnconfirmed=False,
includeNames=False):
"""
Runs listunspent on the given node and returns the unspent result matching
the txid:vout combination or None.
"""
minConf = 1
if allowUnconfirmed:
minConf = 0
opt = {"includeNames": includeNames}
res = self.nodes[ind].listunspent (minConf, None, addressFilter, None, opt)
for out in res:
if (out['txid'], out['vout']) == (txid, vout):
return out
return None
def run_test (self):
# Create a name new for testing. Figure out the vout by looking at the
# raw transaction.
addrA = self.nodes[0].getnewaddress ()
new = self.nodes[0].name_new ("testname", {"destAddress": addrA})
txid = new[0]
raw = self.nodes[0].getrawtransaction (txid, 1)
vout = None
for i in range (len (raw['vout'])):
if 'nameOp' in raw['vout'][i]['scriptPubKey']:
vout = i
break
assert vout is not None
# Check expected behaviour for listunspent with the unconfirmed name_new.
assert self.lookup (0, txid, vout, allowUnconfirmed=True) is None
assert self.lookup (0, txid, vout, includeNames=True) is None
unspent = self.lookup (0, txid, vout,
addressFilter=[addrA],
allowUnconfirmed=True,
includeNames=True)
assert unspent is not None
assert_equal (unspent['confirmations'], 0)
assert 'nameOp' in unspent
assert_equal (unspent['nameOp']['op'], 'name_new')
# Name new's don't expire. Verify that we get it after confirmation
# correctly as well.
self.nodes[0].generate (50)
unspent = self.lookup (0, txid, vout, includeNames=True)
assert unspent is not None
assert_equal (unspent['confirmations'], 50)
assert 'nameOp' in unspent
# Firstupdate the name and check that briefly.
self.firstupdateName (0, "testname", new, "value")
self.nodes[0].generate (1)
unspent = self.lookupName (0, "testname", includeNames=True)
assert unspent is not None
assert 'nameOp' in unspent
assert_equal (unspent['nameOp']['op'], 'name_firstupdate')
assert_equal (unspent['nameOp']['name'], 'testname')
# Update the name, sending to another node.
addrB = self.nodes[1].getnewaddress ()
self.nodes[0].name_update ("testname", "value", {"destAddress": addrB})
self.sync_mempools ()
# Node 0 should no longer have the unspent output.
assert self.lookupName (0, "testname",
allowUnconfirmed=True,
includeNames=True) is None
assert self.lookupName (0, "testname",
allowUnconfirmed=True,
includeNames=True) is None
# Node 1 should now see the output as unconfirmed.
assert self.lookupName (1, "testname", allowUnconfirmed=True) is None
assert self.lookupName (1, "testname", includeNames=True) is None
unspent = self.lookupName (1, "testname",
addressFilter=[addrB],
allowUnconfirmed=True,
includeNames=True)
assert unspent is not None
assert_equal (unspent['confirmations'], 0)
assert 'nameOp' in unspent
assert_equal (unspent['nameOp']['op'], 'name_update')
assert_equal (unspent['nameOp']['name'], 'testname')
# Mine blocks and verify node 1 seeing the name correctly.
self.nodes[1].generate (30)
assert_equal (self.nodes[1].name_show ("testname")['expired'], False)
assert_equal (self.nodes[1].name_show ("testname")['expires_in'], 1)
assert self.lookupName (1, "testname") is None
assert self.lookupName (1, "testname",
addressFilter=[addrA],
includeNames=True) is None
unspent = self.lookupName (1, "testname", includeNames=True)
assert unspent is not None
assert_equal (unspent['confirmations'], 30)
assert 'nameOp' in unspent
assert_equal (unspent['nameOp']['op'], 'name_update')
assert_equal (unspent['nameOp']['name'], 'testname')
# One more block and the name expires. Then it should no longer show up.
self.nodes[1].generate (1)
assert_equal (self.nodes[1].name_show ("testname")['expired'], True)
assert self.lookupName (1, "testname", includeNames=True) is None
if __name__ == '__main__':
NameListUnspentTest ().main ()