Dash Core  0.12.2.1
P2P Digital Currency
listtransactions.py
Go to the documentation of this file.
1 #!/usr/bin/env python2
2 # Copyright (c) 2014-2015 The Bitcoin Core developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 # Exercise the listtransactions API
7 
8 from test_framework.test_framework import BitcoinTestFramework
9 from test_framework.util import *
10 from test_framework.mininode import CTransaction, COIN
11 from io import BytesIO
12 
13 def txFromHex(hexstring):
14  tx = CTransaction()
15  f = BytesIO(hex_str_to_bytes(hexstring))
16  tx.deserialize(f)
17  return tx
18 
20 
21  def setup_nodes(self):
22  #This test requires mocktime
24  return start_nodes(4, self.options.tmpdir)
25 
26  def run_test(self):
27  # Simple send, 0 to 1:
28  txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1)
29  self.sync_all()
31  {"txid":txid},
32  {"category":"send","account":"","amount":Decimal("-0.1"),"confirmations":0})
34  {"txid":txid},
35  {"category":"receive","account":"","amount":Decimal("0.1"),"confirmations":0})
36  # mine a block, confirmations should change:
37  self.nodes[0].generate(1)
38  self.sync_all()
40  {"txid":txid},
41  {"category":"send","account":"","amount":Decimal("-0.1"),"confirmations":1})
43  {"txid":txid},
44  {"category":"receive","account":"","amount":Decimal("0.1"),"confirmations":1})
45 
46  # send-to-self:
47  txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 0.2)
49  {"txid":txid, "category":"send"},
50  {"amount":Decimal("-0.2")})
52  {"txid":txid, "category":"receive"},
53  {"amount":Decimal("0.2")})
54 
55  # sendmany from node1: twice to self, twice to node2:
56  send_to = { self.nodes[0].getnewaddress() : 0.11,
57  self.nodes[1].getnewaddress() : 0.22,
58  self.nodes[0].getaccountaddress("from1") : 0.33,
59  self.nodes[1].getaccountaddress("toself") : 0.44 }
60  txid = self.nodes[1].sendmany("", send_to)
61  self.sync_all()
63  {"category":"send","amount":Decimal("-0.11")},
64  {"txid":txid} )
66  {"category":"receive","amount":Decimal("0.11")},
67  {"txid":txid} )
69  {"category":"send","amount":Decimal("-0.22")},
70  {"txid":txid} )
72  {"category":"receive","amount":Decimal("0.22")},
73  {"txid":txid} )
75  {"category":"send","amount":Decimal("-0.33")},
76  {"txid":txid} )
78  {"category":"receive","amount":Decimal("0.33")},
79  {"txid":txid, "account" : "from1"} )
81  {"category":"send","amount":Decimal("-0.44")},
82  {"txid":txid, "account" : ""} )
84  {"category":"receive","amount":Decimal("0.44")},
85  {"txid":txid, "account" : "toself"} )
86 
87  multisig = self.nodes[1].createmultisig(1, [self.nodes[1].getnewaddress()])
88  self.nodes[0].importaddress(multisig["redeemScript"], "watchonly", False, True)
89  txid = self.nodes[1].sendtoaddress(multisig["address"], 0.1)
90  self.nodes[1].generate(1)
91  self.sync_all()
92  assert(len(self.nodes[0].listtransactions("watchonly", 100, 0, False)) == 0)
93  assert_array_result(self.nodes[0].listtransactions("watchonly", 100, 0, True),
94  {"category":"receive","amount":Decimal("0.1")},
95  {"txid":txid, "account" : "watchonly"} )
96 
97  # rbf is disabled in Dash Core
98  # self.run_rbf_opt_in_test()
99 
100  # Check that the opt-in-rbf flag works properly, for sent and received
101  # transactions.
103  # Check whether a transaction signals opt-in RBF itself
104  def is_opt_in(node, txid):
105  rawtx = node.getrawtransaction(txid, 1)
106  for x in rawtx["vin"]:
107  if x["sequence"] < 0xfffffffe:
108  return True
109  return False
110 
111  # Find an unconfirmed output matching a certain txid
112  def get_unconfirmed_utxo_entry(node, txid_to_match):
113  utxo = node.listunspent(0, 0)
114  for i in utxo:
115  if i["txid"] == txid_to_match:
116  return i
117  return None
118 
119  # 1. Chain a few transactions that don't opt-in.
120  txid_1 = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 1)
121  assert(not is_opt_in(self.nodes[0], txid_1))
122  assert_array_result(self.nodes[0].listtransactions(), {"txid": txid_1}, {"bip125-replaceable":"no"})
123  sync_mempools(self.nodes)
124  assert_array_result(self.nodes[1].listtransactions(), {"txid": txid_1}, {"bip125-replaceable":"no"})
125 
126  # Tx2 will build off txid_1, still not opting in to RBF.
127  utxo_to_use = get_unconfirmed_utxo_entry(self.nodes[1], txid_1)
128 
129  # Create tx2 using createrawtransaction
130  inputs = [{"txid":utxo_to_use["txid"], "vout":utxo_to_use["vout"]}]
131  outputs = {self.nodes[0].getnewaddress(): 0.999}
132  tx2 = self.nodes[1].createrawtransaction(inputs, outputs)
133  tx2_signed = self.nodes[1].signrawtransaction(tx2)["hex"]
134  txid_2 = self.nodes[1].sendrawtransaction(tx2_signed)
135 
136  # ...and check the result
137  assert(not is_opt_in(self.nodes[1], txid_2))
138  assert_array_result(self.nodes[1].listtransactions(), {"txid": txid_2}, {"bip125-replaceable":"no"})
139  sync_mempools(self.nodes)
140  assert_array_result(self.nodes[0].listtransactions(), {"txid": txid_2}, {"bip125-replaceable":"no"})
141 
142  # Tx3 will opt-in to RBF
143  utxo_to_use = get_unconfirmed_utxo_entry(self.nodes[0], txid_2)
144  inputs = [{"txid": txid_2, "vout":utxo_to_use["vout"]}]
145  outputs = {self.nodes[1].getnewaddress(): 0.998}
146  tx3 = self.nodes[0].createrawtransaction(inputs, outputs)
147  tx3_modified = txFromHex(tx3)
148  tx3_modified.vin[0].nSequence = 0
149  tx3 = bytes_to_hex_str(tx3_modified.serialize())
150  tx3_signed = self.nodes[0].signrawtransaction(tx3)['hex']
151  txid_3 = self.nodes[0].sendrawtransaction(tx3_signed)
152 
153  assert(is_opt_in(self.nodes[0], txid_3))
154  assert_array_result(self.nodes[0].listtransactions(), {"txid": txid_3}, {"bip125-replaceable":"yes"})
155  sync_mempools(self.nodes)
156  assert_array_result(self.nodes[1].listtransactions(), {"txid": txid_3}, {"bip125-replaceable":"yes"})
157 
158  # Tx4 will chain off tx3. Doesn't signal itself, but depends on one
159  # that does.
160  utxo_to_use = get_unconfirmed_utxo_entry(self.nodes[1], txid_3)
161  inputs = [{"txid": txid_3, "vout":utxo_to_use["vout"]}]
162  outputs = {self.nodes[0].getnewaddress(): 0.997}
163  tx4 = self.nodes[1].createrawtransaction(inputs, outputs)
164  tx4_signed = self.nodes[1].signrawtransaction(tx4)["hex"]
165  txid_4 = self.nodes[1].sendrawtransaction(tx4_signed)
166 
167  assert(not is_opt_in(self.nodes[1], txid_4))
168  assert_array_result(self.nodes[1].listtransactions(), {"txid": txid_4}, {"bip125-replaceable":"yes"})
169  sync_mempools(self.nodes)
170  assert_array_result(self.nodes[0].listtransactions(), {"txid": txid_4}, {"bip125-replaceable":"yes"})
171 
172  # Replace tx3, and check that tx4 becomes unknown
173  tx3_b = tx3_modified
174  tx3_b.vout[0].nValue -= int(Decimal("0.004") * COIN) # bump the fee
175  tx3_b = bytes_to_hex_str(tx3_b.serialize())
176  tx3_b_signed = self.nodes[0].signrawtransaction(tx3_b)['hex']
177  txid_3b = self.nodes[0].sendrawtransaction(tx3_b_signed, True)
178  assert(is_opt_in(self.nodes[0], txid_3b))
179 
180  assert_array_result(self.nodes[0].listtransactions(), {"txid": txid_4}, {"bip125-replaceable":"unknown"})
181  sync_mempools(self.nodes)
182  assert_array_result(self.nodes[1].listtransactions(), {"txid": txid_4}, {"bip125-replaceable":"unknown"})
183 
184  # Check gettransaction as well:
185  for n in self.nodes[0:2]:
186  assert_equal(n.gettransaction(txid_1)["bip125-replaceable"], "no")
187  assert_equal(n.gettransaction(txid_2)["bip125-replaceable"], "no")
188  assert_equal(n.gettransaction(txid_3)["bip125-replaceable"], "yes")
189  assert_equal(n.gettransaction(txid_3b)["bip125-replaceable"], "yes")
190  assert_equal(n.gettransaction(txid_4)["bip125-replaceable"], "unknown")
191 
192  # After mining a transaction, it's no longer BIP125-replaceable
193  self.nodes[0].generate(1)
194  assert(txid_3b not in self.nodes[0].getrawmempool())
195  assert_equal(self.nodes[0].gettransaction(txid_3b)["bip125-replaceable"], "no")
196  assert_equal(self.nodes[0].gettransaction(txid_4)["bip125-replaceable"], "unknown")
197 
198 
199 if __name__ == '__main__':
201 
def hex_str_to_bytes(hex_str)
Definition: util.py:111
def assert_array_result(object_array, to_match, expected, should_not_find=False)
Definition: util.py:496
def start_nodes(num_nodes, dirname, extra_args=None, rpchost=None, binary=None)
Definition: util.py:305
UniValue sendmany(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:1024
UniValue getaccountaddress(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:192
UniValue sendtoaddress(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:409
UniValue getnewaddress(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:113
def sync_mempools(rpc_connections, wait=1)
Definition: util.py:127
def enable_mocktime()
Definition: util.py:38
UniValue signrawtransaction(const UniValue &params, bool fHelp)
UniValue createrawtransaction(const UniValue &params, bool fHelp)
UniValue createmultisig(const UniValue &params, bool fHelp)
Definition: misc.cpp:401
UniValue importaddress(const UniValue &params, bool fHelp)
Definition: rpcdump.cpp:181
UniValue generate(const UniValue &params, bool fHelp)
Definition: mining.cpp:122
UniValue sendrawtransaction(const UniValue &params, bool fHelp)
UniValue getrawmempool(const UniValue &params, bool fHelp)
Definition: blockchain.cpp:234
def txFromHex(hexstring)
def assert_equal(thing1, thing2)
Definition: util.py:461
UniValue gettransaction(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:1821
def bytes_to_hex_str(byte_str)
Definition: util.py:108