@@ -72,62 +72,135 @@ def run_test(self):
72
72
# otherwise we're off by exactly the fee amount as that's mined
73
73
# and matures in the next 100 blocks
74
74
node .sendfrom ("" , common_address , fee )
75
- accounts = ["a" , "b" , "c" , "d" , "e" ]
76
75
amount_to_send = 1.0
77
- account_addresses = dict ()
76
+
77
+ # Create accounts and make sure subsequent account API calls
78
+ # recognize the account/address associations.
79
+ accounts = [Account (name ) for name in ("a" , "b" , "c" , "d" , "e" )]
78
80
for account in accounts :
79
- address = node .getaccountaddress (account )
80
- account_addresses [account ] = address
81
-
82
- node .getnewaddress (account )
83
- assert_equal (node .getaccount (address ), account )
84
- assert (address in node .getaddressesbyaccount (account ))
85
-
86
- node .sendfrom ("" , address , amount_to_send )
87
-
81
+ account .add_receive_address (node .getaccountaddress (account .name ))
82
+ account .verify (node )
83
+
84
+ # Send a transaction to each account, and make sure this forces
85
+ # getaccountaddress to generate a new receiving address.
86
+ for account in accounts :
87
+ node .sendtoaddress (account .receive_address , amount_to_send )
88
+ account .add_receive_address (node .getaccountaddress (account .name ))
89
+ account .verify (node )
90
+
91
+ # Check the amounts received.
88
92
node .generate (1 )
93
+ for account in accounts :
94
+ assert_equal (
95
+ node .getreceivedbyaddress (account .addresses [0 ]), amount_to_send )
96
+ assert_equal (node .getreceivedbyaccount (account .name ), amount_to_send )
89
97
90
- for i in range ( len ( accounts )):
91
- from_account = accounts [ i ]
98
+ # Check that sendfrom account reduces listaccounts balances.
99
+ for i , account in enumerate ( accounts ):
92
100
to_account = accounts [(i + 1 ) % len (accounts )]
93
- to_address = account_addresses [to_account ]
94
- node .sendfrom (from_account , to_address , amount_to_send )
95
-
101
+ node .sendfrom (account .name , to_account .receive_address , amount_to_send )
96
102
node .generate (1 )
97
-
98
103
for account in accounts :
99
- address = node .getaccountaddress (account )
100
- assert ( address != account_addresses [ account ] )
101
- assert_equal (node .getreceivedbyaccount (account ), 2 )
102
- node .move (account , "" , node .getbalance (account ))
103
-
104
+ account . add_receive_address ( node .getaccountaddress (account . name ) )
105
+ account . verify ( node )
106
+ assert_equal (node .getreceivedbyaccount (account . name ), 2 )
107
+ node .move (account . name , "" , node .getbalance (account . name ))
108
+ account . verify ( node )
104
109
node .generate (101 )
105
-
106
110
expected_account_balances = {"" : 5200 }
107
111
for account in accounts :
108
- expected_account_balances [account ] = 0
109
-
112
+ expected_account_balances [account .name ] = 0
110
113
assert_equal (node .listaccounts (), expected_account_balances )
111
-
112
114
assert_equal (node .getbalance ("" ), 5200 )
113
115
116
+ # Check that setaccount can assign an account to a new unused address.
114
117
for account in accounts :
115
118
address = node .getaccountaddress ("" )
116
- node .setaccount (address , account )
117
- assert (address in node .getaddressesbyaccount (account ))
119
+ node .setaccount (address , account .name )
120
+ account .add_address (address )
121
+ account .verify (node )
118
122
assert (address not in node .getaddressesbyaccount ("" ))
119
123
124
+ # Check that addmultisigaddress can assign accounts.
120
125
for account in accounts :
121
126
addresses = []
122
127
for x in range (10 ):
123
128
addresses .append (node .getnewaddress ())
124
- multisig_address = node .addmultisigaddress (5 , addresses , account )
129
+ multisig_address = node .addmultisigaddress (5 , addresses , account .name )
130
+ account .add_address (multisig_address )
131
+ account .verify (node )
125
132
node .sendfrom ("" , multisig_address , 50 )
126
-
127
133
node .generate (101 )
128
-
129
134
for account in accounts :
130
- assert_equal (node .getbalance (account ), 50 )
135
+ assert_equal (node .getbalance (account .name ), 50 )
136
+
137
+ # Check that setaccount can change the account of an address from a
138
+ # different account.
139
+ change_account (node , accounts [0 ].addresses [0 ], accounts [0 ], accounts [1 ])
140
+
141
+ # Check that setaccount can change the account of an address which
142
+ # is the receiving address of a different account.
143
+ change_account (node , accounts [0 ].receive_address , accounts [0 ], accounts [1 ])
144
+
145
+ # Check that setaccount can set the account of an address already
146
+ # in the account. This is a no-op.
147
+ change_account (node , accounts [2 ].addresses [0 ], accounts [2 ], accounts [2 ])
148
+
149
+ # Check that setaccount can set the account of an address which is
150
+ # already the receiving address of the account. It would probably make
151
+ # sense for this to be a no-op, but right now it resets the receiving
152
+ # address, causing getaccountaddress to return a brand new address.
153
+ change_account (node , accounts [2 ].receive_address , accounts [2 ], accounts [2 ])
154
+
155
+ class Account :
156
+ def __init__ (self , name ):
157
+ # Account name
158
+ self .name = name
159
+ # Current receiving address associated with this account.
160
+ self .receive_address = None
161
+ # List of all addresses assigned with this account
162
+ self .addresses = []
163
+
164
+ def add_address (self , address ):
165
+ assert_equal (address not in self .addresses , True )
166
+ self .addresses .append (address )
167
+
168
+ def add_receive_address (self , address ):
169
+ self .add_address (address )
170
+ self .receive_address = address
171
+
172
+ def verify (self , node ):
173
+ if self .receive_address is not None :
174
+ assert self .receive_address in self .addresses
175
+ assert_equal (node .getaccountaddress (self .name ), self .receive_address )
176
+
177
+ for address in self .addresses :
178
+ assert_equal (node .getaccount (address ), self .name )
179
+
180
+ assert_equal (
181
+ set (node .getaddressesbyaccount (self .name )), set (self .addresses ))
182
+
183
+
184
+ def change_account (node , address , old_account , new_account ):
185
+ assert_equal (address in old_account .addresses , True )
186
+ node .setaccount (address , new_account .name )
187
+
188
+ old_account .addresses .remove (address )
189
+ new_account .add_address (address )
190
+
191
+ # Calling setaccount on an address which was previously the receiving
192
+ # address of a different account should reset the receiving address of
193
+ # the old account, causing getaccountaddress to return a brand new
194
+ # address.
195
+ if address == old_account .receive_address :
196
+ new_address = node .getaccountaddress (old_account .name )
197
+ assert_equal (new_address not in old_account .addresses , True )
198
+ assert_equal (new_address not in new_account .addresses , True )
199
+ old_account .add_receive_address (new_address )
200
+
201
+ old_account .verify (node )
202
+ new_account .verify (node )
203
+
131
204
132
205
if __name__ == '__main__' :
133
206
WalletAccountsTest ().main ()
0 commit comments