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
):
19 def set_test_params(self
):
21 self
.enable_mocktime()
24 # Simple send, 0 to 1:
25 txid
= self
.nodes
[0].sendtoaddress(self
.nodes
[1].getnewaddress(), 0.1)
27 assert_array_result(self
.nodes
[0].listtransactions(),
29 {"category":"send","account":"","amount":Decimal("-0.1"),"confirmations":0})
30 assert_array_result(self
.nodes
[1].listtransactions(),
32 {"category":"receive","account":"","amount":Decimal("0.1"),"confirmations":0})
33 # mine a block, confirmations should change:
34 self
.nodes
[0].generate(1)
36 assert_array_result(self
.nodes
[0].listtransactions(),
38 {"category":"send","account":"","amount":Decimal("-0.1"),"confirmations":1})
39 assert_array_result(self
.nodes
[1].listtransactions(),
41 {"category":"receive","account":"","amount":Decimal("0.1"),"confirmations":1})
44 txid
= self
.nodes
[0].sendtoaddress(self
.nodes
[0].getnewaddress(), 0.2)
45 assert_array_result(self
.nodes
[0].listtransactions(),
46 {"txid":txid
, "category":"send"},
47 {"amount":Decimal("-0.2")})
48 assert_array_result(self
.nodes
[0].listtransactions(),
49 {"txid":txid
, "category":"receive"},
50 {"amount":Decimal("0.2")})
52 # sendmany from node1: twice to self, twice to node2:
53 send_to
= { self
.nodes
[0].getnewaddress() : 0.11,
54 self
.nodes
[1].getnewaddress() : 0.22,
55 self
.nodes
[0].getaccountaddress("from1") : 0.33,
56 self
.nodes
[1].getaccountaddress("toself") : 0.44 }
57 txid
= self
.nodes
[1].sendmany("", send_to
)
59 assert_array_result(self
.nodes
[1].listtransactions(),
60 {"category":"send","amount":Decimal("-0.11")},
62 assert_array_result(self
.nodes
[0].listtransactions(),
63 {"category":"receive","amount":Decimal("0.11")},
65 assert_array_result(self
.nodes
[1].listtransactions(),
66 {"category":"send","amount":Decimal("-0.22")},
68 assert_array_result(self
.nodes
[1].listtransactions(),
69 {"category":"receive","amount":Decimal("0.22")},
71 assert_array_result(self
.nodes
[1].listtransactions(),
72 {"category":"send","amount":Decimal("-0.33")},
74 assert_array_result(self
.nodes
[0].listtransactions(),
75 {"category":"receive","amount":Decimal("0.33")},
76 {"txid":txid
, "account" : "from1"} )
77 assert_array_result(self
.nodes
[1].listtransactions(),
78 {"category":"send","amount":Decimal("-0.44")},
79 {"txid":txid
, "account" : ""} )
80 assert_array_result(self
.nodes
[1].listtransactions(),
81 {"category":"receive","amount":Decimal("0.44")},
82 {"txid":txid
, "account" : "toself"} )
84 multisig
= self
.nodes
[1].createmultisig(1, [self
.nodes
[1].getnewaddress()])
85 self
.nodes
[0].importaddress(multisig
["redeemScript"], "watchonly", False, True)
86 txid
= self
.nodes
[1].sendtoaddress(multisig
["address"], 0.1)
87 self
.nodes
[1].generate(1)
89 assert(len(self
.nodes
[0].listtransactions("watchonly", 100, 0, False)) == 0)
90 assert_array_result(self
.nodes
[0].listtransactions("watchonly", 100, 0, True),
91 {"category":"receive","amount":Decimal("0.1")},
92 {"txid":txid
, "account" : "watchonly"} )
94 self
.run_rbf_opt_in_test()
96 # Check that the opt-in-rbf flag works properly, for sent and received
98 def run_rbf_opt_in_test(self
):
99 # Check whether a transaction signals opt-in RBF itself
100 def is_opt_in(node
, txid
):
101 rawtx
= node
.getrawtransaction(txid
, 1)
102 for x
in rawtx
["vin"]:
103 if x
["sequence"] < 0xfffffffe:
107 # Find an unconfirmed output matching a certain txid
108 def get_unconfirmed_utxo_entry(node
, txid_to_match
):
109 utxo
= node
.listunspent(0, 0)
111 if i
["txid"] == txid_to_match
:
115 # 1. Chain a few transactions that don't opt-in.
116 txid_1
= self
.nodes
[0].sendtoaddress(self
.nodes
[1].getnewaddress(), 1)
117 assert(not is_opt_in(self
.nodes
[0], txid_1
))
118 assert_array_result(self
.nodes
[0].listtransactions(), {"txid": txid_1
}, {"bip125-replaceable":"no"})
119 sync_mempools(self
.nodes
)
120 assert_array_result(self
.nodes
[1].listtransactions(), {"txid": txid_1
}, {"bip125-replaceable":"no"})
122 # Tx2 will build off txid_1, still not opting in to RBF.
123 utxo_to_use
= get_unconfirmed_utxo_entry(self
.nodes
[0], txid_1
)
124 assert_equal(utxo_to_use
["safe"], True)
125 utxo_to_use
= get_unconfirmed_utxo_entry(self
.nodes
[1], txid_1
)
126 utxo_to_use
= get_unconfirmed_utxo_entry(self
.nodes
[1], txid_1
)
127 assert_equal(utxo_to_use
["safe"], False)
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
)
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"})
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
)
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"})
158 # Tx4 will chain off tx3. Doesn't signal itself, but depends on one
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
)
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"})
172 # Replace tx3, and check that tx4 becomes unknown
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
))
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"})
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")
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")
199 if __name__
== '__main__':
200 ListTransactionsTest().main()