55
66# Exercise the listtransactions API
77
8- # Add python-bitcoinrpc to module search path:
9- import os
10- import sys
11- sys .path .append (os .path .join (os .path .dirname (os .path .abspath (__file__ )), "python-bitcoinrpc" ))
12-
13- import json
14- import shutil
15- import subprocess
16- import tempfile
17- import traceback
18-
8+ from test_framework import BitcoinTestFramework
199from bitcoinrpc .authproxy import AuthServiceProxy , JSONRPCException
2010from util import *
2111
@@ -41,116 +31,67 @@ def check_array_result(object_array, to_match, expected):
4131 if num_matched == 0 :
4232 raise AssertionError ("No objects matched %s" % (str (to_match )))
4333
44- def run_test (nodes ):
45- # Simple send, 0 to 1:
46- txid = nodes [0 ].sendtoaddress (nodes [1 ].getnewaddress (), 0.1 )
47- sync_mempools (nodes )
48- check_array_result (nodes [0 ].listtransactions (),
49- {"txid" :txid },
50- {"category" :"send" ,"account" :"" ,"amount" :Decimal ("-0.1" ),"confirmations" :0 })
51- check_array_result (nodes [1 ].listtransactions (),
52- {"txid" :txid },
53- {"category" :"receive" ,"account" :"" ,"amount" :Decimal ("0.1" ),"confirmations" :0 })
54- # mine a block, confirmations should change:
55- nodes [0 ].setgenerate (True , 1 )
56- sync_blocks (nodes )
57- check_array_result (nodes [0 ].listtransactions (),
58- {"txid" :txid },
59- {"category" :"send" ,"account" :"" ,"amount" :Decimal ("-0.1" ),"confirmations" :1 })
60- check_array_result (nodes [1 ].listtransactions (),
61- {"txid" :txid },
62- {"category" :"receive" ,"account" :"" ,"amount" :Decimal ("0.1" ),"confirmations" :1 })
63-
64- # send-to-self:
65- txid = nodes [0 ].sendtoaddress (nodes [0 ].getnewaddress (), 0.2 )
66- check_array_result (nodes [0 ].listtransactions (),
67- {"txid" :txid , "category" :"send" },
68- {"amount" :Decimal ("-0.2" )})
69- check_array_result (nodes [0 ].listtransactions (),
70- {"txid" :txid , "category" :"receive" },
71- {"amount" :Decimal ("0.2" )})
72-
73- # sendmany from node1: twice to self, twice to node2:
74- send_to = { nodes [0 ].getnewaddress () : 0.11 , nodes [1 ].getnewaddress () : 0.22 ,
75- nodes [0 ].getaccountaddress ("from1" ) : 0.33 , nodes [1 ].getaccountaddress ("toself" ) : 0.44 }
76- txid = nodes [1 ].sendmany ("" , send_to )
77- sync_mempools (nodes )
78- check_array_result (nodes [1 ].listtransactions (),
79- {"category" :"send" ,"amount" :Decimal ("-0.11" )},
80- {"txid" :txid } )
81- check_array_result (nodes [0 ].listtransactions (),
82- {"category" :"receive" ,"amount" :Decimal ("0.11" )},
83- {"txid" :txid } )
84- check_array_result (nodes [1 ].listtransactions (),
85- {"category" :"send" ,"amount" :Decimal ("-0.22" )},
86- {"txid" :txid } )
87- check_array_result (nodes [1 ].listtransactions (),
88- {"category" :"receive" ,"amount" :Decimal ("0.22" )},
89- {"txid" :txid } )
90- check_array_result (nodes [1 ].listtransactions (),
91- {"category" :"send" ,"amount" :Decimal ("-0.33" )},
92- {"txid" :txid } )
93- check_array_result (nodes [0 ].listtransactions (),
94- {"category" :"receive" ,"amount" :Decimal ("0.33" )},
95- {"txid" :txid , "account" : "from1" } )
96- check_array_result (nodes [1 ].listtransactions (),
97- {"category" :"send" ,"amount" :Decimal ("-0.44" )},
98- {"txid" :txid , "account" : "" } )
99- check_array_result (nodes [1 ].listtransactions (),
100- {"category" :"receive" ,"amount" :Decimal ("0.44" )},
101- {"txid" :txid , "account" : "toself" } )
102-
103-
104- def main ():
105- import optparse
106-
107- parser = optparse .OptionParser (usage = "%prog [options]" )
108- parser .add_option ("--nocleanup" , dest = "nocleanup" , default = False , action = "store_true" ,
109- help = "Leave bitcoinds and test.* datadir on exit or error" )
110- parser .add_option ("--srcdir" , dest = "srcdir" , default = "../../src" ,
111- help = "Source directory containing bitcoind/bitcoin-cli (default: %default%)" )
112- parser .add_option ("--tmpdir" , dest = "tmpdir" , default = tempfile .mkdtemp (prefix = "test" ),
113- help = "Root directory for datadirs" )
114- (options , args ) = parser .parse_args ()
115-
116- os .environ ['PATH' ] = options .srcdir + ":" + os .environ ['PATH' ]
117-
118- check_json_precision ()
119-
120- success = False
121- nodes = []
122- try :
123- print ("Initializing test directory " + options .tmpdir )
124- if not os .path .isdir (options .tmpdir ):
125- os .makedirs (options .tmpdir )
126- initialize_chain (options .tmpdir )
127-
128- nodes = start_nodes (2 , options .tmpdir )
129- connect_nodes (nodes [1 ], 0 )
34+ class ListTransactionsTest (BitcoinTestFramework ):
35+
36+ def run_test (self , nodes ):
37+ # Simple send, 0 to 1:
38+ txid = nodes [0 ].sendtoaddress (nodes [1 ].getnewaddress (), 0.1 )
39+ sync_mempools (nodes )
40+ check_array_result (nodes [0 ].listtransactions (),
41+ {"txid" :txid },
42+ {"category" :"send" ,"account" :"" ,"amount" :Decimal ("-0.1" ),"confirmations" :0 })
43+ check_array_result (nodes [1 ].listtransactions (),
44+ {"txid" :txid },
45+ {"category" :"receive" ,"account" :"" ,"amount" :Decimal ("0.1" ),"confirmations" :0 })
46+ # mine a block, confirmations should change:
47+ nodes [0 ].setgenerate (True , 1 )
13048 sync_blocks (nodes )
131-
132- run_test (nodes )
133-
134- success = True
135-
136- except AssertionError as e :
137- print ("Assertion failed: " + e .message )
138- except Exception as e :
139- print ("Unexpected exception caught during testing: " + str (e ))
140- traceback .print_tb (sys .exc_info ()[2 ])
141-
142- if not options .nocleanup :
143- print ("Cleaning up" )
144- stop_nodes (nodes )
145- wait_bitcoinds ()
146- shutil .rmtree (options .tmpdir )
147-
148- if success :
149- print ("Tests successful" )
150- sys .exit (0 )
151- else :
152- print ("Failed" )
153- sys .exit (1 )
49+ check_array_result (nodes [0 ].listtransactions (),
50+ {"txid" :txid },
51+ {"category" :"send" ,"account" :"" ,"amount" :Decimal ("-0.1" ),"confirmations" :1 })
52+ check_array_result (nodes [1 ].listtransactions (),
53+ {"txid" :txid },
54+ {"category" :"receive" ,"account" :"" ,"amount" :Decimal ("0.1" ),"confirmations" :1 })
55+
56+ # send-to-self:
57+ txid = nodes [0 ].sendtoaddress (nodes [0 ].getnewaddress (), 0.2 )
58+ check_array_result (nodes [0 ].listtransactions (),
59+ {"txid" :txid , "category" :"send" },
60+ {"amount" :Decimal ("-0.2" )})
61+ check_array_result (nodes [0 ].listtransactions (),
62+ {"txid" :txid , "category" :"receive" },
63+ {"amount" :Decimal ("0.2" )})
64+
65+ # sendmany from node1: twice to self, twice to node2:
66+ send_to = { nodes [0 ].getnewaddress () : 0.11 , nodes [1 ].getnewaddress () : 0.22 ,
67+ nodes [0 ].getaccountaddress ("from1" ) : 0.33 , nodes [1 ].getaccountaddress ("toself" ) : 0.44 }
68+ txid = nodes [1 ].sendmany ("" , send_to )
69+ sync_mempools (nodes )
70+ check_array_result (nodes [1 ].listtransactions (),
71+ {"category" :"send" ,"amount" :Decimal ("-0.11" )},
72+ {"txid" :txid } )
73+ check_array_result (nodes [0 ].listtransactions (),
74+ {"category" :"receive" ,"amount" :Decimal ("0.11" )},
75+ {"txid" :txid } )
76+ check_array_result (nodes [1 ].listtransactions (),
77+ {"category" :"send" ,"amount" :Decimal ("-0.22" )},
78+ {"txid" :txid } )
79+ check_array_result (nodes [1 ].listtransactions (),
80+ {"category" :"receive" ,"amount" :Decimal ("0.22" )},
81+ {"txid" :txid } )
82+ check_array_result (nodes [1 ].listtransactions (),
83+ {"category" :"send" ,"amount" :Decimal ("-0.33" )},
84+ {"txid" :txid } )
85+ check_array_result (nodes [0 ].listtransactions (),
86+ {"category" :"receive" ,"amount" :Decimal ("0.33" )},
87+ {"txid" :txid , "account" : "from1" } )
88+ check_array_result (nodes [1 ].listtransactions (),
89+ {"category" :"send" ,"amount" :Decimal ("-0.44" )},
90+ {"txid" :txid , "account" : "" } )
91+ check_array_result (nodes [1 ].listtransactions (),
92+ {"category" :"receive" ,"amount" :Decimal ("0.44" )},
93+ {"txid" :txid , "account" : "toself" } )
15494
15595if __name__ == '__main__' :
156- main ()
96+ ListTransactionsTest ().main ()
97+
0 commit comments