-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_proposal.py
359 lines (310 loc) · 15.6 KB
/
test_proposal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# coding=utf-8
'''
Created on 2018-10-10
@author: holly
Project: test proposal with one operation and two operations
'''
from modules import *
import bitsharesbase.operations as btsops
def test_proposal_one_operation_with_multisign(INSTANCE, cleartxpool):
instance = INSTANCE
reset_wallet(instance)
# create two accounts
logging.info('create new accounts')
accounts = create_accounts(instance, num=4)
if accounts == False:
logging.info('create account error')
assert 0
# get accounts information
accountName1 = accounts[0]['account']
accountName2 = accounts[1]['account']
accountName3 = accounts[2]['account']
accountName4 = accounts[3]['account']
activeKey1 = accounts[0]['active']['wif_priv_key']
activeKey2 = accounts[1]['active']['wif_priv_key']
activeKey3 = accounts[2]['active']['wif_priv_key']
activeKey4 = accounts[3]['active']['wif_priv_key']
activePub1 = accounts[0]['active']['pub_key']
activePub2 = accounts[1]['active']['pub_key']
activePub3 = accounts[2]['active']['pub_key']
account1 = cybex.Account(accountName1, cybex_instance=instance)
account3 = cybex.Account(accountName3, cybex_instance=instance)
account4 = cybex.Account(accountName4, cybex_instance=instance)
# add active key
instance.wallet.addPrivateKey(activeKey1)
instance.wallet.addPrivateKey(activeKey2)
# transfer 3 CYB to account1 and account2 from nathan
acc = instance.const['master_account']
valu = 3
instance.transfer(accountName1, valu, 'CYB', '', acc)
instance.transfer(accountName2, valu, 'CYB', '', acc)
instance.transfer(accountName3, valu, 'CYB', '', acc)
instance.transfer(accountName4, valu, 'CYB', '', acc)
logging.info('transfer %s CYB from %s to %s, %s, %s and %s' % (valu, acc, account1, cybex.Account(accountName2, cybex_instance=instance), account3, account4))
account1.refresh()
account3.refresh()
account4.refresh()
assert int(account1.balances[0]) == valu * 10 ** account1.balances[0].asset.precision
assert int(account3.balances[0]) == valu * 10 ** account3.balances[0].asset.precision
assert int(account4.balances[0]) == valu * 10 ** account4.balances[0].asset.precision
# make account2 a multisign account, controled by account3 and account4
obj = {'weight_threshold':2, 'account_auths':[[account3['id'], 1], [account4['id'], 1]], 'key_auths':[[activePub2, 1]], 'address_auths':[]}
update_active_keys(instance, obj, account=accountName2)
account2 = cybex.Account(accountName2, cybex_instance=instance)
account2.refresh()
assert account2['active']['weight_threshold'] == 2
# proposal with one transaction
instance.proposer = accountName1
instance.proposal_expiration = int(60*60*24)
fee = instance.fee[0]['fee']['fee']/100000
logging.info('fee: %s' % fee)
amount = 200000
transfer = btsops.Transfer(**{
"fee": {"amount": fee, "asset_id":"1.3.0"},
"from": dict(account2)["id"],
"to": dict(account1)["id"],
"amount": {"amount": amount, "asset_id": "1.3.0"},
"extensions": [],
})
instance.finalizeOp([transfer], account1, "active")
logging.info('%s create a proposal' % account1)
account2.refresh()
# logging.info(dict(account2.proposals[0]))
p_id = dict(account2.proposals[0])['id']
logging.info('proposal id: %s' % p_id)
logging.info('proposal: %s transfer %s CYB to %s' % (account2, amount/100000, account1))
instance.proposer = None
logging.info('after %s proposal, %s balance cyb: %s' % (account1, account1, account1.balance('CYB')))
logging.info('after %s proposal, %s balance cyb: %s' % (account1, account2, account2.balance('CYB')))
logging.info('after %s proposal, %s balance cyb: %s' % (account3, account3, account3.balance('CYB')))
logging.info('after %s proposal, %s balance cyb: %s' % (account3, account4, account4.balance('CYB')))
# check proposal
assert len(dict(account2.proposals[0])['required_active_approvals']) == 1
assert len(dict(account2.proposals[0])['available_active_approvals']) == 0
assert account1.balance('CYB') == valu - fee
assert account2.balance('CYB') == valu
logging.info('this proposal has not been executed')
# time.sleep(60)
# add account3 sign
# instance.wallet.removePrivateKeyFromPublicKey(activePub1)
# instance.wallet.removePrivateKeyFromPublicKey(activePub2)
instance.wallet.addPrivateKey(activeKey3)
logging.info('%s accept the proposal' % (account3))
instance.approveproposal(p_id, account=accountName3, approver=accountName3)
account1.refresh()
account2.refresh()
account3.refresh()
account4.refresh()
logging.info('after %s sign, %s balance cyb: %s' % (account3, account1, account1.balance('CYB')))
logging.info('after %s sign, %s balance cyb: %s' % (account3, account2, account2.balance('CYB')))
logging.info('after %s sign, %s balance cyb: %s' % (account3, account3, account3.balance('CYB')))
logging.info('after %s sign, %s balance cyb: %s' % (account3, account4, account4.balance('CYB')))
assert len(dict(account2.proposals[0])['required_active_approvals']) == 1
assert len(dict(account2.proposals[0])['available_active_approvals']) == 1
assert account1.balance('CYB') == valu - fee
assert account2.balance('CYB') == valu
assert account3.balance('CYB') == valu - fee
assert account4.balance('CYB') == valu
logging.info('this proposal has not been executed')
# time.sleep(60)
# add account4 sign
# instance.wallet.removePrivateKeyFromPublicKey(activePub3)
instance.wallet.addPrivateKey(activeKey4)
logging.info('%s accept the proposal' % (account4))
instance.approveproposal(p_id, account=accountName4, approver=accountName4)
account1.refresh()
account2.refresh()
account3.refresh()
account4.refresh()
logging.info('after %s sign, %s balance cyb: %s' % (account4, account1, account1.balance('CYB')))
logging.info('after %s sign, %s balance cyb: %s' % (account4, account2, account2.balance('CYB')))
logging.info('after %s sign, %s balance cyb: %s' % (account4, account3, account3.balance('CYB')))
logging.info('after %s sign, %s balance cyb: %s' % (account4, account4, account4.balance('CYB')))
assert account1.balance('CYB') == valu - fee + amount/100000
assert account2.balance('CYB') == valu - fee - amount/100000
assert account3.balance('CYB') == valu - fee
assert account4.balance('CYB') == valu - fee
logging.info('this proposal has been executed successfully')
def test_proposal_two_accounts(INSTANCE, cleartxpool):
instance = INSTANCE
reset_wallet(instance)
# create two accounts
logging.info('create new accounts')
accounts = create_accounts(instance, num=2)
if accounts == False:
logging.info('create account error')
assert 0
# get accounts information
accountName1 = accounts[0]['account']
accountName2 = accounts[1]['account']
activeKey1 = accounts[0]['active']['wif_priv_key']
activeKey2 = accounts[1]['active']['wif_priv_key']
ownerKey1 = accounts[0]['owner']['wif_priv_key']
ownerKey2 = accounts[1]['owner']['wif_priv_key']
account1 = cybex.Account(accountName1, cybex_instance=instance)
account2 = cybex.Account(accountName2, cybex_instance=instance)
# add active key
instance.wallet.addPrivateKey(activeKey1)
instance.wallet.addPrivateKey(activeKey2)
# transfer 3 CYB to account1 and account2 from nathan
acc = instance.const['master_account']
valu = 3
instance.transfer(accountName1, valu, 'CYB', '', acc)
instance.transfer(accountName2, valu, 'CYB', '', acc)
logging.info('transfer %s CYB from %s to %s and %s' % (valu, acc, account1, account2))
account1.refresh()
account2.refresh()
assert int(account1.balances[0]) == valu * 10 ** account1.balances[0].asset.precision
assert int(account2.balances[0]) == valu * 10 ** account2.balances[0].asset.precision
# proposal with two transaction
instance.proposer = accountName1
instance.proposal_expiration = int(60*60*24)
fee = instance.fee[0]['fee']['fee']/100000
logging.info('fee: %s' % fee)
amount1 = 100000
amount2 = 200000
transfer1 = btsops.Transfer(**{
"fee": {"amount": fee, "asset_id":"1.3.0"},
"from": dict(account1)["id"],
"to": dict(account2)["id"],
"amount": {"amount": amount1, "asset_id": "1.3.0"},
"extensions": [],
})
transfer2 = btsops.Transfer(**{
"fee": {"amount": fee, "asset_id":"1.3.0"},
"from": dict(account2)["id"],
"to": dict(account1)["id"],
"amount": {"amount": amount2, "asset_id": "1.3.0"},
"extensions": [],
})
instance.finalizeOp([transfer1, transfer2], account1, "active")
logging.info('%s create a proposal' % account1)
account1.refresh()
p_id = dict(account1.proposals[0])['id']
# logging.info(dict(account1.proposals[0]))
logging.info('proposal id: %s' % p_id)
logging.info('proposal: %s transfer %s CYB to %s, %s transfer %s CYB to %s' % (account1, amount1/100000, account2, account2, amount2/100000, account1))
instance.proposer = None
logging.info('after %s proposal, %s balance cyb: %s' % (account1, account1, account1.balance('CYB')))
logging.info('after %s proposal, %s balance cyb: %s' % (account1, account2, account2.balance('CYB')))
# check proposal
assert len(dict(account1.proposals[0])['required_active_approvals']) == 2
assert len(dict(account1.proposals[0])['available_active_approvals']) == 0
assert account1.balance('CYB') == valu - fee
assert account2.balance('CYB') == valu
logging.info('this proposal has not been executed')
# add account1 sign
logging.info('%s accept the proposal' % (account1))
instance.approveproposal([p_id], account=accountName1, approver=accountName1)
account1.refresh()
account2.refresh()
assert len(dict(account1.proposals[0])['required_active_approvals']) == 2
assert len(dict(account1.proposals[0])['available_active_approvals']) == 1
assert account1.balance('CYB') == valu - 2*fee
assert account2.balance('CYB') == valu
logging.info('after %s sign, %s balance cyb: %s' % (account1, account1, account1.balance('CYB')))
logging.info('after %s sign, %s balance cyb: %s' % (account1, account2, account2.balance('CYB')))
logging.info('this proposal is not executed yet')
# add account2 sign
logging.info('%s accept the proposal' % (account2))
instance.approveproposal(p_id, account=accountName2, approver=accountName2)
account1.refresh()
account2.refresh()
logging.info('after %s sign, %s balance cyb: %s' % (account2, account1, account1.balance('CYB')))
logging.info('after %s sign, %s balance cyb: %s' % (account2, account2, account2.balance('CYB')))
assert account1.balance('CYB') == valu - 3*fee - amount1/100000 + amount2/100000
assert account2.balance('CYB') == valu - 2*fee - amount2/100000 + amount1/100000
logging.info('this proposal has been executed successfully')
def test_proposal_delete_operation(INSTANCE, cleartxpool):
instance = INSTANCE
reset_wallet(instance)
# create two accounts
logging.info('create new accounts')
accounts = create_accounts(instance, num=2)
if accounts == False:
logging.info('create account error')
assert 0
# get accounts information
accountName1 = accounts[0]['account']
accountName2 = accounts[1]['account']
activeKey1 = accounts[0]['active']['wif_priv_key']
activeKey2 = accounts[1]['active']['wif_priv_key']
account1 = cybex.Account(accountName1, cybex_instance=instance)
account2 = cybex.Account(accountName2, cybex_instance=instance)
# add active key
instance.wallet.addPrivateKey(activeKey1)
instance.wallet.addPrivateKey(activeKey2)
# transfer 3 CYB to account1 and account2 from nathan
acc = instance.const['master_account']
valu = 3
instance.transfer(accountName1, valu, 'CYB', '', acc)
instance.transfer(accountName2, valu, 'CYB', '', acc)
logging.info('transfer %s CYB from %s to %s and %s' % (valu, acc, account1, account2))
account1.refresh()
account2.refresh()
assert int(account1.balances[0]) == valu * 10 ** account1.balances[0].asset.precision
assert int(account2.balances[0]) == valu * 10 ** account2.balances[0].asset.precision
# proposal with two transaction
instance.proposer = accountName1
instance.proposal_expiration = int(60*60*24)
fee = instance.fee[0]['fee']['fee']/100000
logging.info('fee: %s' % fee)
amount1 = 100000
amount2 = 200000
transfer1 = btsops.Transfer(**{
"fee": {"amount": fee, "asset_id":"1.3.0"},
"from": dict(account1)["id"],
"to": dict(account2)["id"],
"amount": {"amount": amount1, "asset_id": "1.3.0"},
"extensions": [],
})
transfer2 = btsops.Transfer(**{
"fee": {"amount": fee, "asset_id":"1.3.0"},
"from": dict(account2)["id"],
"to": dict(account1)["id"],
"amount": {"amount": amount2, "asset_id": "1.3.0"},
"extensions": [],
})
instance.finalizeOp([transfer1, transfer2], account1, "active")
logging.info('%s create a proposal' % account1)
account1.refresh()
p_id = dict(account1.proposals[0])['id']
# logging.info(dict(account1.proposals[0]))
logging.info('proposal id: %s' % p_id)
logging.info('proposal: %s transfer %s CYB to %s, %s transfer %s CYB to %s' % (account1, amount1/100000, account2, account2, amount2/100000, account1))
instance.proposer = None
logging.info('after %s proposal, %s balance cyb: %s' % (account1, account1, account1.balance('CYB')))
logging.info('after %s proposal, %s balance cyb: %s' % (account1, account2, account2.balance('CYB')))
# check proposal
assert len(dict(account1.proposals[0])['required_active_approvals']) == 2
assert len(dict(account1.proposals[0])['available_active_approvals']) == 0
assert account1.balance('CYB') == valu - fee
assert account2.balance('CYB') == valu
logging.info('this proposal has not been executed')
# add account1 sign
logging.info('%s accept the proposal' % (account1))
instance.approveproposal([p_id], account=accountName1, approver=accountName1)
account1.refresh()
account2.refresh()
assert len(dict(account1.proposals[0])['required_active_approvals']) == 2
assert len(dict(account1.proposals[0])['available_active_approvals']) == 1
assert account1.balance('CYB') == valu - 2*fee
assert account2.balance('CYB') == valu
logging.info('after %s sign, %s balance cyb: %s' % (account1, account1, account1.balance('CYB')))
logging.info('after %s sign, %s balance cyb: %s' % (account1, account2, account2.balance('CYB')))
logging.info('this proposal is not executed yet')
logging.info('%s dissapprove proposal' % (account1))
instance.disapproveproposal([p_id], account=accountName1, approver=accountName1)
account1.refresh()
account2.refresh()
logging.info('after %s disapprove proposal, %s balance: %s' % (account1, account1, account1.balance('CYB')))
logging.info('after %s disapprove proposal, %s balance: %s' % (account1, account2, account2.balance('CYB')))
logging.info('%s delete this proposal' % (account1))
instance.proposal_delete(proposal_id=p_id, account=accountName1)
# check account1 balance
account1.refresh()
account2.refresh()
logging.info('%s balance: %s' % (account1, account1.balance('CYB')))
logging.info('%s balance: %s' % (account2, account2.balance('CYB')))
assert account2.balance('CYB') == valu
assert account1.balance('CYB') == valu -3*fee - 0.001