2 # Copyright (c) 2014-2016 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 """Test the listtransactions API."""
7 from test_framework
.test_framework
import BitcoinTestFramework
8 from test_framework
.util
import *
9 from test_framework
.mininode
import CTransaction
, COIN
10 from io
import BytesIO
12 def txFromHex(hexstring
):
14 f
= BytesIO(hex_str_to_bytes(hexstring
))
18 class ListTransactionsTest(BitcoinTestFramework
):
22 self
.setup_clean_chain
= False
24 def setup_nodes(self
):
25 #This test requires mocktime
26 self
.enable_mocktime()
27 self
.nodes
= self
.start_nodes(self
.num_nodes
, self
.options
.tmpdir
)
30 # Simple send, 0 to 1:
31 txid
= self
.nodes
[0].sendtoaddress(self
.nodes
[1].getnewaddress(), 0.1)
33 assert_array_result(self
.nodes
[0].listtransactions(),
35 {"category":"send","account":"","amount":Decimal("-0.1"),"confirmations":0})
36 assert_array_result(self
.nodes
[1].listtransactions(),
38 {"category":"receive","account":"","amount":Decimal("0.1"),"confirmations":0})
39 # mine a block, confirmations should change:
40 self
.nodes
[0].generate(1)
42 assert_array_result(self
.nodes
[0].listtransactions(),
44 {"category":"send","account":"","amount":Decimal("-0.1"),"confirmations":1})
45 assert_array_result(self
.nodes
[1].listtransactions(),
47 {"category":"receive","account":"","amount":Decimal("0.1"),"confirmations":1})
50 txid
= self
.nodes
[0].sendtoaddress(self
.nodes
[0].getnewaddress(), 0.2)
51 assert_array_result(self
.nodes
[0].listtransactions(),
52 {"txid":txid
, "category":"send"},
53 {"amount":Decimal("-0.2")})
54 assert_array_result(self
.nodes
[0].listtransactions(),
55 {"txid":txid
, "category":"receive"},
56 {"amount":Decimal("0.2")})
58 # sendmany from node1: twice to self, twice to node2:
59 send_to
= { self
.nodes
[0].getnewaddress() : 0.11,
60 self
.nodes
[1].getnewaddress() : 0.22,
61 self
.nodes
[0].getaccountaddress("from1") : 0.33,
62 self
.nodes
[1].getaccountaddress("toself") : 0.44 }
63 txid
= self
.nodes
[1].sendmany("", send_to
)
65 assert_array_result(self
.nodes
[1].listtransactions(),
66 {"category":"send","amount":Decimal("-0.11")},
68 assert_array_result(self
.nodes
[0].listtransactions(),
69 {"category":"receive","amount":Decimal("0.11")},
71 assert_array_result(self
.nodes
[1].listtransactions(),
72 {"category":"send","amount":Decimal("-0.22")},
74 assert_array_result(self
.nodes
[1].listtransactions(),
75 {"category":"receive","amount":Decimal("0.22")},
77 assert_array_result(self
.nodes
[1].listtransactions(),
78 {"category":"send","amount":Decimal("-0.33")},
80 assert_array_result(self
.nodes
[0].listtransactions(),
81 {"category":"receive","amount":Decimal("0.33")},
82 {"txid":txid
, "account" : "from1"} )
83 assert_array_result(self
.nodes
[1].listtransactions(),
84 {"category":"send","amount":Decimal("-0.44")},
85 {"txid":txid
, "account" : ""} )
86 assert_array_result(self
.nodes
[1].listtransactions(),
87 {"category":"receive","amount":Decimal("0.44")},
88 {"txid":txid
, "account" : "toself"} )
90 multisig
= self
.nodes
[1].createmultisig(1, [self
.nodes
[1].getnewaddress()])
91 self
.nodes
[0].importaddress(multisig
["redeemScript"], "watchonly", False, True)
92 txid
= self
.nodes
[1].sendtoaddress(multisig
["address"], 0.1)
93 self
.nodes
[1].generate(1)
95 assert(len(self
.nodes
[0].listtransactions("watchonly", 100, 0, False)) == 0)
96 assert_array_result(self
.nodes
[0].listtransactions("watchonly", 100, 0, True),
97 {"category":"receive","amount":Decimal("0.1")},
98 {"txid":txid
, "account" : "watchonly"} )
100 self
.run_rbf_opt_in_test()
102 # Check that the opt-in-rbf flag works properly, for sent and received
104 def run_rbf_opt_in_test(self
):
105 # Check whether a transaction signals opt-in RBF itself
106 def is_opt_in(node
, txid
):
107 rawtx
= node
.getrawtransaction(txid
, 1)
108 for x
in rawtx
["vin"]:
109 if x
["sequence"] < 0xfffffffe:
113 # Find an unconfirmed output matching a certain txid
114 def get_unconfirmed_utxo_entry(node
, txid_to_match
):
115 utxo
= node
.listunspent(0, 0)
117 if i
["txid"] == txid_to_match
:
121 # 1. Chain a few transactions that don't opt-in.
122 txid_1
= self
.nodes
[0].sendtoaddress(self
.nodes
[1].getnewaddress(), 1)
123 assert(not is_opt_in(self
.nodes
[0], txid_1
))
124 assert_array_result(self
.nodes
[0].listtransactions(), {"txid": txid_1
}, {"bip125-replaceable":"no"})
125 sync_mempools(self
.nodes
)
126 assert_array_result(self
.nodes
[1].listtransactions(), {"txid": txid_1
}, {"bip125-replaceable":"no"})
128 # Tx2 will build off txid_1, still not opting in to RBF.
129 utxo_to_use
= get_unconfirmed_utxo_entry(self
.nodes
[0], txid_1
)
130 assert_equal(utxo_to_use
["safe"], True)
131 utxo_to_use
= get_unconfirmed_utxo_entry(self
.nodes
[1], txid_1
)
132 utxo_to_use
= get_unconfirmed_utxo_entry(self
.nodes
[1], txid_1
)
133 assert_equal(utxo_to_use
["safe"], False)
135 # Create tx2 using createrawtransaction
136 inputs
= [{"txid":utxo_to_use
["txid"], "vout":utxo_to_use
["vout"]}]
137 outputs
= {self
.nodes
[0].getnewaddress(): 0.999}
138 tx2
= self
.nodes
[1].createrawtransaction(inputs
, outputs
)
139 tx2_signed
= self
.nodes
[1].signrawtransaction(tx2
)["hex"]
140 txid_2
= self
.nodes
[1].sendrawtransaction(tx2_signed
)
142 # ...and check the result
143 assert(not is_opt_in(self
.nodes
[1], txid_2
))
144 assert_array_result(self
.nodes
[1].listtransactions(), {"txid": txid_2
}, {"bip125-replaceable":"no"})
145 sync_mempools(self
.nodes
)
146 assert_array_result(self
.nodes
[0].listtransactions(), {"txid": txid_2
}, {"bip125-replaceable":"no"})
148 # Tx3 will opt-in to RBF
149 utxo_to_use
= get_unconfirmed_utxo_entry(self
.nodes
[0], txid_2
)
150 inputs
= [{"txid": txid_2
, "vout":utxo_to_use
["vout"]}]
151 outputs
= {self
.nodes
[1].getnewaddress(): 0.998}
152 tx3
= self
.nodes
[0].createrawtransaction(inputs
, outputs
)
153 tx3_modified
= txFromHex(tx3
)
154 tx3_modified
.vin
[0].nSequence
= 0
155 tx3
= bytes_to_hex_str(tx3_modified
.serialize())
156 tx3_signed
= self
.nodes
[0].signrawtransaction(tx3
)['hex']
157 txid_3
= self
.nodes
[0].sendrawtransaction(tx3_signed
)
159 assert(is_opt_in(self
.nodes
[0], txid_3
))
160 assert_array_result(self
.nodes
[0].listtransactions(), {"txid": txid_3
}, {"bip125-replaceable":"yes"})
161 sync_mempools(self
.nodes
)
162 assert_array_result(self
.nodes
[1].listtransactions(), {"txid": txid_3
}, {"bip125-replaceable":"yes"})
164 # Tx4 will chain off tx3. Doesn't signal itself, but depends on one
166 utxo_to_use
= get_unconfirmed_utxo_entry(self
.nodes
[1], txid_3
)
167 inputs
= [{"txid": txid_3
, "vout":utxo_to_use
["vout"]}]
168 outputs
= {self
.nodes
[0].getnewaddress(): 0.997}
169 tx4
= self
.nodes
[1].createrawtransaction(inputs
, outputs
)
170 tx4_signed
= self
.nodes
[1].signrawtransaction(tx4
)["hex"]
171 txid_4
= self
.nodes
[1].sendrawtransaction(tx4_signed
)
173 assert(not is_opt_in(self
.nodes
[1], txid_4
))
174 assert_array_result(self
.nodes
[1].listtransactions(), {"txid": txid_4
}, {"bip125-replaceable":"yes"})
175 sync_mempools(self
.nodes
)
176 assert_array_result(self
.nodes
[0].listtransactions(), {"txid": txid_4
}, {"bip125-replaceable":"yes"})
178 # Replace tx3, and check that tx4 becomes unknown
180 tx3_b
.vout
[0].nValue
-= int(Decimal("0.004") * COIN
) # bump the fee
181 tx3_b
= bytes_to_hex_str(tx3_b
.serialize())
182 tx3_b_signed
= self
.nodes
[0].signrawtransaction(tx3_b
)['hex']
183 txid_3b
= self
.nodes
[0].sendrawtransaction(tx3_b_signed
, True)
184 assert(is_opt_in(self
.nodes
[0], txid_3b
))
186 assert_array_result(self
.nodes
[0].listtransactions(), {"txid": txid_4
}, {"bip125-replaceable":"unknown"})
187 sync_mempools(self
.nodes
)
188 assert_array_result(self
.nodes
[1].listtransactions(), {"txid": txid_4
}, {"bip125-replaceable":"unknown"})
190 # Check gettransaction as well:
191 for n
in self
.nodes
[0:2]:
192 assert_equal(n
.gettransaction(txid_1
)["bip125-replaceable"], "no")
193 assert_equal(n
.gettransaction(txid_2
)["bip125-replaceable"], "no")
194 assert_equal(n
.gettransaction(txid_3
)["bip125-replaceable"], "yes")
195 assert_equal(n
.gettransaction(txid_3b
)["bip125-replaceable"], "yes")
196 assert_equal(n
.gettransaction(txid_4
)["bip125-replaceable"], "unknown")
198 # After mining a transaction, it's no longer BIP125-replaceable
199 self
.nodes
[0].generate(1)
200 assert(txid_3b
not in self
.nodes
[0].getrawmempool())
201 assert_equal(self
.nodes
[0].gettransaction(txid_3b
)["bip125-replaceable"], "no")
202 assert_equal(self
.nodes
[0].gettransaction(txid_4
)["bip125-replaceable"], "unknown")
205 if __name__
== '__main__':
206 ListTransactionsTest().main()