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
):
20 self
.enable_mocktime()
23 # Simple send, 0 to 1:
24 txid
= self
.nodes
[0].sendtoaddress(self
.nodes
[1].getnewaddress(), 0.1)
26 assert_array_result(self
.nodes
[0].listtransactions(),
28 {"category":"send","account":"","amount":Decimal("-0.1"),"confirmations":0})
29 assert_array_result(self
.nodes
[1].listtransactions(),
31 {"category":"receive","account":"","amount":Decimal("0.1"),"confirmations":0})
32 # mine a block, confirmations should change:
33 self
.nodes
[0].generate(1)
35 assert_array_result(self
.nodes
[0].listtransactions(),
37 {"category":"send","account":"","amount":Decimal("-0.1"),"confirmations":1})
38 assert_array_result(self
.nodes
[1].listtransactions(),
40 {"category":"receive","account":"","amount":Decimal("0.1"),"confirmations":1})
43 txid
= self
.nodes
[0].sendtoaddress(self
.nodes
[0].getnewaddress(), 0.2)
44 assert_array_result(self
.nodes
[0].listtransactions(),
45 {"txid":txid
, "category":"send"},
46 {"amount":Decimal("-0.2")})
47 assert_array_result(self
.nodes
[0].listtransactions(),
48 {"txid":txid
, "category":"receive"},
49 {"amount":Decimal("0.2")})
51 # sendmany from node1: twice to self, twice to node2:
52 send_to
= { self
.nodes
[0].getnewaddress() : 0.11,
53 self
.nodes
[1].getnewaddress() : 0.22,
54 self
.nodes
[0].getaccountaddress("from1") : 0.33,
55 self
.nodes
[1].getaccountaddress("toself") : 0.44 }
56 txid
= self
.nodes
[1].sendmany("", send_to
)
58 assert_array_result(self
.nodes
[1].listtransactions(),
59 {"category":"send","amount":Decimal("-0.11")},
61 assert_array_result(self
.nodes
[0].listtransactions(),
62 {"category":"receive","amount":Decimal("0.11")},
64 assert_array_result(self
.nodes
[1].listtransactions(),
65 {"category":"send","amount":Decimal("-0.22")},
67 assert_array_result(self
.nodes
[1].listtransactions(),
68 {"category":"receive","amount":Decimal("0.22")},
70 assert_array_result(self
.nodes
[1].listtransactions(),
71 {"category":"send","amount":Decimal("-0.33")},
73 assert_array_result(self
.nodes
[0].listtransactions(),
74 {"category":"receive","amount":Decimal("0.33")},
75 {"txid":txid
, "account" : "from1"} )
76 assert_array_result(self
.nodes
[1].listtransactions(),
77 {"category":"send","amount":Decimal("-0.44")},
78 {"txid":txid
, "account" : ""} )
79 assert_array_result(self
.nodes
[1].listtransactions(),
80 {"category":"receive","amount":Decimal("0.44")},
81 {"txid":txid
, "account" : "toself"} )
83 multisig
= self
.nodes
[1].createmultisig(1, [self
.nodes
[1].getnewaddress()])
84 self
.nodes
[0].importaddress(multisig
["redeemScript"], "watchonly", False, True)
85 txid
= self
.nodes
[1].sendtoaddress(multisig
["address"], 0.1)
86 self
.nodes
[1].generate(1)
88 assert(len(self
.nodes
[0].listtransactions("watchonly", 100, 0, False)) == 0)
89 assert_array_result(self
.nodes
[0].listtransactions("watchonly", 100, 0, True),
90 {"category":"receive","amount":Decimal("0.1")},
91 {"txid":txid
, "account" : "watchonly"} )
93 self
.run_rbf_opt_in_test()
95 # Check that the opt-in-rbf flag works properly, for sent and received
97 def run_rbf_opt_in_test(self
):
98 # Check whether a transaction signals opt-in RBF itself
99 def is_opt_in(node
, txid
):
100 rawtx
= node
.getrawtransaction(txid
, 1)
101 for x
in rawtx
["vin"]:
102 if x
["sequence"] < 0xfffffffe:
106 # Find an unconfirmed output matching a certain txid
107 def get_unconfirmed_utxo_entry(node
, txid_to_match
):
108 utxo
= node
.listunspent(0, 0)
110 if i
["txid"] == txid_to_match
:
114 # 1. Chain a few transactions that don't opt-in.
115 txid_1
= self
.nodes
[0].sendtoaddress(self
.nodes
[1].getnewaddress(), 1)
116 assert(not is_opt_in(self
.nodes
[0], txid_1
))
117 assert_array_result(self
.nodes
[0].listtransactions(), {"txid": txid_1
}, {"bip125-replaceable":"no"})
118 sync_mempools(self
.nodes
)
119 assert_array_result(self
.nodes
[1].listtransactions(), {"txid": txid_1
}, {"bip125-replaceable":"no"})
121 # Tx2 will build off txid_1, still not opting in to RBF.
122 utxo_to_use
= get_unconfirmed_utxo_entry(self
.nodes
[0], txid_1
)
123 assert_equal(utxo_to_use
["safe"], True)
124 utxo_to_use
= get_unconfirmed_utxo_entry(self
.nodes
[1], txid_1
)
125 utxo_to_use
= get_unconfirmed_utxo_entry(self
.nodes
[1], txid_1
)
126 assert_equal(utxo_to_use
["safe"], False)
128 # Create tx2 using createrawtransaction
129 inputs
= [{"txid":utxo_to_use
["txid"], "vout":utxo_to_use
["vout"]}]
130 outputs
= {self
.nodes
[0].getnewaddress(): 0.999}
131 tx2
= self
.nodes
[1].createrawtransaction(inputs
, outputs
)
132 tx2_signed
= self
.nodes
[1].signrawtransaction(tx2
)["hex"]
133 txid_2
= self
.nodes
[1].sendrawtransaction(tx2_signed
)
135 # ...and check the result
136 assert(not is_opt_in(self
.nodes
[1], txid_2
))
137 assert_array_result(self
.nodes
[1].listtransactions(), {"txid": txid_2
}, {"bip125-replaceable":"no"})
138 sync_mempools(self
.nodes
)
139 assert_array_result(self
.nodes
[0].listtransactions(), {"txid": txid_2
}, {"bip125-replaceable":"no"})
141 # Tx3 will opt-in to RBF
142 utxo_to_use
= get_unconfirmed_utxo_entry(self
.nodes
[0], txid_2
)
143 inputs
= [{"txid": txid_2
, "vout":utxo_to_use
["vout"]}]
144 outputs
= {self
.nodes
[1].getnewaddress(): 0.998}
145 tx3
= self
.nodes
[0].createrawtransaction(inputs
, outputs
)
146 tx3_modified
= txFromHex(tx3
)
147 tx3_modified
.vin
[0].nSequence
= 0
148 tx3
= bytes_to_hex_str(tx3_modified
.serialize())
149 tx3_signed
= self
.nodes
[0].signrawtransaction(tx3
)['hex']
150 txid_3
= self
.nodes
[0].sendrawtransaction(tx3_signed
)
152 assert(is_opt_in(self
.nodes
[0], txid_3
))
153 assert_array_result(self
.nodes
[0].listtransactions(), {"txid": txid_3
}, {"bip125-replaceable":"yes"})
154 sync_mempools(self
.nodes
)
155 assert_array_result(self
.nodes
[1].listtransactions(), {"txid": txid_3
}, {"bip125-replaceable":"yes"})
157 # Tx4 will chain off tx3. Doesn't signal itself, but depends on one
159 utxo_to_use
= get_unconfirmed_utxo_entry(self
.nodes
[1], txid_3
)
160 inputs
= [{"txid": txid_3
, "vout":utxo_to_use
["vout"]}]
161 outputs
= {self
.nodes
[0].getnewaddress(): 0.997}
162 tx4
= self
.nodes
[1].createrawtransaction(inputs
, outputs
)
163 tx4_signed
= self
.nodes
[1].signrawtransaction(tx4
)["hex"]
164 txid_4
= self
.nodes
[1].sendrawtransaction(tx4_signed
)
166 assert(not is_opt_in(self
.nodes
[1], txid_4
))
167 assert_array_result(self
.nodes
[1].listtransactions(), {"txid": txid_4
}, {"bip125-replaceable":"yes"})
168 sync_mempools(self
.nodes
)
169 assert_array_result(self
.nodes
[0].listtransactions(), {"txid": txid_4
}, {"bip125-replaceable":"yes"})
171 # Replace tx3, and check that tx4 becomes unknown
173 tx3_b
.vout
[0].nValue
-= int(Decimal("0.004") * COIN
) # bump the fee
174 tx3_b
= bytes_to_hex_str(tx3_b
.serialize())
175 tx3_b_signed
= self
.nodes
[0].signrawtransaction(tx3_b
)['hex']
176 txid_3b
= self
.nodes
[0].sendrawtransaction(tx3_b_signed
, True)
177 assert(is_opt_in(self
.nodes
[0], txid_3b
))
179 assert_array_result(self
.nodes
[0].listtransactions(), {"txid": txid_4
}, {"bip125-replaceable":"unknown"})
180 sync_mempools(self
.nodes
)
181 assert_array_result(self
.nodes
[1].listtransactions(), {"txid": txid_4
}, {"bip125-replaceable":"unknown"})
183 # Check gettransaction as well:
184 for n
in self
.nodes
[0:2]:
185 assert_equal(n
.gettransaction(txid_1
)["bip125-replaceable"], "no")
186 assert_equal(n
.gettransaction(txid_2
)["bip125-replaceable"], "no")
187 assert_equal(n
.gettransaction(txid_3
)["bip125-replaceable"], "yes")
188 assert_equal(n
.gettransaction(txid_3b
)["bip125-replaceable"], "yes")
189 assert_equal(n
.gettransaction(txid_4
)["bip125-replaceable"], "unknown")
191 # After mining a transaction, it's no longer BIP125-replaceable
192 self
.nodes
[0].generate(1)
193 assert(txid_3b
not in self
.nodes
[0].getrawmempool())
194 assert_equal(self
.nodes
[0].gettransaction(txid_3b
)["bip125-replaceable"], "no")
195 assert_equal(self
.nodes
[0].gettransaction(txid_4
)["bip125-replaceable"], "unknown")
198 if __name__
== '__main__':
199 ListTransactionsTest().main()