forked from namecoin/namecoin-core
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathname_sendcoins.py
executable file
·100 lines (81 loc) · 3.88 KB
/
name_sendcoins.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
#!/usr/bin/env python3
# Copyright (c) 2018-2020 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 "sendCoins" option with name operations."""
from test_framework.names import NameTestFramework
from test_framework.util import *
class NameSendCoinsTest (NameTestFramework):
def set_test_params (self):
self.setup_name_test ([[]] * 1)
def verifyTx (self, txid, expected):
"""Verify that the given tx sends currency to the expected recipients."""
txData = self.nodes[0].gettransaction (txid)
assert_greater_than (txData["confirmations"], 0)
tx = self.nodes[0].decoderawtransaction (txData["hex"])
vout = tx['vout']
# There should be two additional outputs: name and change
assert_equal (len (vout), len (expected) + 2)
actual = {}
for out in vout:
if 'nameOp' in out['scriptPubKey']:
continue
addr = out['scriptPubKey']['addresses']
assert_equal (len (addr), 1)
addr = addr[0]
if not addr in expected:
# This must be the change address. Through the assertion above about
# the expected sizes, we make sure that the test fails if there is
# not exactly one key with this property.
continue
actual[addr] = out['value']
assert_equal (actual, expected)
def run_test (self):
# Send name_new with sendCoins option and verify it worked as expected.
addr1 = self.nodes[0].getnewaddress ()
addr2 = self.nodes[0].getnewaddress ()
sendCoins = {addr1: 1, addr2: 2}
new = self.nodes[0].name_new ("testname", {"sendCoins": sendCoins})
self.nodes[0].generate (10)
self.verifyTx (new[0], sendCoins)
# Check that it also works with first_update.
txid = self.firstupdateName (0, "testname", new, "value",
{"sendCoins": sendCoins})
self.nodes[0].generate (5)
self.verifyTx (txid, sendCoins)
# Test different variations (numbers of target addresses) with name_update.
for n in range (5):
sendCoins = {self.nodes[0].getnewaddress (): 42 + i for i in range (n)}
txid = self.nodes[0].name_update ("testname", "value",
{"sendCoins": sendCoins})
self.nodes[0].generate (1)
self.verifyTx (txid, sendCoins)
# Verify the range check for amount and the address validation.
assert_raises_rpc_error (-3, 'Invalid amount for send',
self.nodes[0].name_update,
"testname", "value", {"sendCoins": {addr1: 0}})
assert_raises_rpc_error (-5, 'Invalid address',
self.nodes[0].name_update,
"testname", "value", {"sendCoins": {"x": 1}})
# Verify the insufficient funds check, both where it fails a priori
# and where we just don't have enough for the fee.
balance = self.nodes[0].getbalance ()
assert_raises_rpc_error (-6, 'Insufficient funds',
self.nodes[0].name_update,
"testname", "value",
{"sendCoins": {addr1: balance + 1}})
assert_raises_rpc_error (-6, 'Insufficient funds',
self.nodes[0].name_update,
"testname", "value",
{"sendCoins": {addr1: balance}})
# Check that we can send a name_update that spends almost all funds in
# the wallet. We only need to keep a tiny amount to pay for the fee
# (but less than the locked amount of 0.01).
keep = Decimal ("0.009")
sendCoins = {addr1: balance - keep}
txid = self.nodes[0].name_update ("testname", "value",
{"sendCoins": sendCoins})
self.nodes[0].generate (1)
self.verifyTx (txid, sendCoins)
if __name__ == '__main__':
NameSendCoinsTest ().main ()