Repository navigation
Expand file tree
/
Copy pathconftest.py
More file actions
377 lines (274 loc) · 8.37 KB
/
Copy pathconftest.py
File metadata and controls
377 lines (274 loc) · 8.37 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
from collections import namedtuple
import eth_account
import pytest
from eth_utils.address import to_checksum_address
from web3 import HTTPProvider, Web3
from web3.middleware import geth_poa_middleware
from ..channel import Channel, ChannelState, Payment
from ..contracts.dappsys import DSToken
from ..contracts.PreimageManager import PreimageManager
from ..contracts.SpritesRegistry import SpritesRegistry
from ..util import (
GAS,
check_tx,
deploy_contract,
fund_account,
fund_token,
generate_preimage,
mint,
)
GETH_URL = "http://localhost:8545"
PARTY_NAMES = ["alice", "bob"]
DEPLOYER = "0xd124b979f746be85706daa1180227e716eafcc5c"
ALICE = "0xa49aad37c34e92236690b93e291ae5f10daf7cbe"
BOB = "0xb357fc3dbd4cdb7cbd96aa0a0bd905dbe56cab77"
CHARLIE = "0xcBE431FF3fdcd4d735df5706e755D0f8726549f0"
DEPLOYER_PK = "0xe33292da27178504b848586dcee3011a7e21ee6ed96f9df17487fd6518a128c7"
ALICE_PK = "0xd8ae722d3a6876fd27907c434968e7373c6fbb985242e545a427531132ef3a71"
BOB_PK = "0x28e58f2f6a924d381e243ec1ca4a2239d2b35ebd9a44cec11aead6848a52630b"
CHARLIE_PK = "0x8e1733c6774268aee3db54901086b1f642f51e60300674ae3b33f1e1217ec7f5"
TOKEN_NAMES = ["WETH", "OAX"]
THIRD_PARTY_NAME = "charlie"
FUND_TOKEN_AMOUNT = 100
DEPOSIT_AMOUNTS = {"alice": 9, "bob": 10}
SEND_AMOUNT = 7
# two parties are equivalent / symmetric in many tests
Account = namedtuple("Account", "address privateKey")
ACCOUNTS = {
name: Account(to_checksum_address(address), private_key)
for name, address, private_key in zip(
["deployer", "alice", "bob", "charlie"],
[DEPLOYER, ALICE, BOB, CHARLIE],
[DEPLOYER_PK, ALICE_PK, BOB_PK, CHARLIE_PK],
)
}
@pytest.fixture(scope="session")
def web3():
w3 = Web3(HTTPProvider(GETH_URL))
# enable eth.account
w3.eth.enable_unaudited_features()
# for POA dev chain, see
# https://web3py.readthedocs.io/en/latest/middleware.html#geth-style-proof-of-authority
w3.middleware_stack.inject(geth_poa_middleware, layer=0)
return w3
# Account generation is slow, on the order of a second.
# We reuse the accounts for the entire session but use function scoped
# token contracts to make sure balances are zero initially.
@pytest.fixture
def mock_address():
return eth_account.Account.create().address
@pytest.fixture(scope="session")
def guy(web3):
return web3.eth.accounts[0]
@pytest.fixture(scope="session")
def tx_args(guy):
return {"from": guy, "gas": GAS}
def _get_account(web3, guy, name):
account = ACCOUNTS[name]
# web3.personal.unlockAccount(account.address, None)
fund_account(web3, guy, account)
return account
@pytest.fixture(scope="session")
def alice(web3, guy):
return _get_account(web3, guy, "alice")
@pytest.fixture(scope="session")
def bob(web3, guy):
return _get_account(web3, guy, "bob")
@pytest.fixture(scope="session")
def charlie(web3, guy):
return _get_account(web3, guy, "charlie")
@pytest.fixture(scope="session")
def deployer(web3):
return web3.eth.accounts[0]
@pytest.fixture(scope="session")
def registry(web3, deployer, preimage_manager):
return deploy_contract(
web3,
deployer,
"SpritesRegistry.sol",
"SpritesRegistry",
SpritesRegistry,
args=[preimage_manager._contract.address],
)
@pytest.fixture(scope="session")
def preimage_manager(web3, deployer):
return deploy_contract(
web3, deployer, "PreimageManager.sol", "PreimageManager", PreimageManager
)
@pytest.fixture(scope="function")
def token(web3, deployer):
token = deploy_contract(
web3, deployer, "dappsys.sol", "DSToken", DSToken, args=[deployer]
)
mint(web3, token, deployer)
return token
@pytest.fixture(scope="function")
def other_token(web3, deployer):
return token(web3, deployer)
@pytest.fixture
def mock_channel(
web3, mock_address, registry, preimage_manager, acting_party, other_party
):
tx_hash = registry.createChannel(other_party.address, mock_address).transact(
{"from": acting_party.address, "gas": GAS}
)
receipt = check_tx(web3, tx_hash)
channel_id = web3.toInt(hexstr=receipt.logs[0].data)
return Channel(
web3,
registry,
preimage_manager,
mock_address,
channel_id,
acting_party,
other_party,
)
@pytest.fixture
def channel(
web3,
token: DSToken,
registry: SpritesRegistry,
preimage_manager: PreimageManager,
acting_party,
other_party,
):
tx_hash = registry.createChannel(
other_party.address, token._contract.address
).transact({"from": acting_party.address, "gas": GAS})
receipt = check_tx(web3, tx_hash)
channel_id = web3.toInt(hexstr=receipt.logs[0].data)
return Channel(
web3, registry, preimage_manager, token, channel_id, acting_party, other_party
)
@pytest.fixture
def other_channel(
web3,
other_token: DSToken,
registry: SpritesRegistry,
preimage_manager: PreimageManager,
acting_party,
other_party,
):
tx_hash = registry.createChannel(
other_party.address, other_token._contract.address
).transact({"from": acting_party.address, "gas": GAS})
receipt = check_tx(web3, tx_hash)
channel_id = web3.toInt(hexstr=receipt.logs[0].data)
return Channel(
web3,
registry,
preimage_manager,
other_token,
channel_id,
acting_party,
other_party,
)
@pytest.fixture(params=["alice", "bob"])
def acting_party_name(request):
return request.param
@pytest.fixture
def acting_party(acting_party_name, alice, bob):
return alice if acting_party_name == "alice" else bob
@pytest.fixture
def other_party_name(acting_party_name):
return next(name for name in PARTY_NAMES if name != acting_party_name)
@pytest.fixture
def other_party(other_party_name, alice, bob):
return alice if other_party_name == "alice" else bob
@pytest.fixture
def third_party(charlie):
return charlie
@pytest.fixture
def preimage():
return generate_preimage()
@pytest.fixture
def round():
return 0
@pytest.fixture
def new_round():
return 1
@pytest.fixture
def amount():
return 0
@pytest.fixture
def new_amount():
return 0
@pytest.fixture
def payment(amount):
return Payment(amount=amount)
@pytest.fixture
def new_payment(new_amount):
return Payment(amount=new_amount)
@pytest.fixture
def deposits():
return [0, 0]
@pytest.fixture
def new_deposits(deposits):
return deposits
@pytest.fixture
def credits():
return [0, 0]
@pytest.fixture
def new_credits(credits):
return credits
@pytest.fixture
def withdrawals():
return [0, 0]
@pytest.fixture
def new_withdrawals(withdrawals):
return withdrawals
@pytest.fixture
def new_state(new_deposits, new_credits, new_withdrawals, new_round, new_payment):
return ChannelState(
deposits=new_deposits,
credits=new_credits,
withdrawals=new_withdrawals,
round=new_round,
payment=new_payment,
)
@pytest.fixture
def last_state(deposits, credits, withdrawals, round, payment):
return ChannelState(
deposits=deposits,
credits=credits,
withdrawals=withdrawals,
round=round,
payment=payment,
)
@pytest.fixture
def deposit_amount(acting_party_name):
return DEPOSIT_AMOUNTS[acting_party_name]
@pytest.fixture
def send_amount():
return SEND_AMOUNT
def test_send_tokens(web3, token, acting_party, guy):
fund_token(
web3, token=token, sender=guy, to=acting_party.address, amount=FUND_TOKEN_AMOUNT
)
assert token.balanceOf(acting_party.address).call() == FUND_TOKEN_AMOUNT
@pytest.fixture
def with_tokens(web3, token, acting_party, guy):
fund_token(
web3, token=token, sender=guy, to=acting_party.address, amount=FUND_TOKEN_AMOUNT
)
return acting_party
@pytest.fixture
def with_other_tokens(web3, other_token, other_party, guy):
fund_token(
web3,
token=other_token,
sender=guy,
to=other_party.address,
amount=FUND_TOKEN_AMOUNT,
)
return other_party
@pytest.fixture
def channel_with_deposit(channel, acting_party, deposit_amount, with_tokens):
channel.deposit(sender=acting_party, amount=deposit_amount)
return channel
@pytest.fixture
def other_channel_with_deposit(
other_channel, other_party, deposit_amount, with_other_tokens
):
other_channel.deposit(sender=other_party, amount=deposit_amount)
return other_channel