-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_sync.py
574 lines (468 loc) · 16.4 KB
/
test_sync.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
import asyncio
import logging
import os
from pathlib import Path
from tempfile import NamedTemporaryFile, TemporaryDirectory
import pytest
import requests
import ujson as json
from solana.publickey import PublicKey
from program_admin import ProgramAdmin
from program_admin.keys import load_keypair
from program_admin.parsing import (
parse_authority_permissions_json,
parse_permissions_with_overrides,
parse_products_json,
parse_publishers_json,
)
from program_admin.types import Network, ReferenceOverrides, ReferencePermissions
from program_admin.util import apply_overrides
LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.getLevelName(os.getenv("LOG_LEVEL", "INFO").upper()))
BTC_USD = {
"account": "",
"attr_dict": {
"symbol": "Crypto.BTC/USD",
"asset_type": "Crypto",
"base": "BTC",
"quote_currency": "USD",
"generic_symbol": "BTCUSD",
"description": "BTC/USD",
},
"metadata": {
"jump_id": "78876709",
"jump_symbol": "BTCUSD",
"price_exp": -8,
"min_publishers": 7,
},
}
AAPL_USD = {
"account": "",
"attr_dict": {
"asset_type": "Equity",
"country": "US",
"description": "APPLE INC",
"quote_currency": "USD",
"cms_symbol": "AAPL",
"cqs_symbol": "AAPL",
"nasdaq_symbol": "AAPL",
"symbol": "Equity.US.AAPL/USD",
"base": "AAPL",
},
"metadata": {"jump_id": "186", "jump_symbol": "AAPL", "price_exp": -5},
}
ETH_USD = {
"account": "",
"attr_dict": {
"symbol": "Crypto.ETH/USD",
"asset_type": "Crypto",
"base": "ETH",
"quote_currency": "USD",
"generic_symbol": "ETHUSD",
"description": "ETH/USD",
},
"metadata": {"jump_id": "12345", "jump_symbol": "ETHUSD", "price_exp": -8},
}
@pytest.fixture
def set_test_env_var():
"""
Sets an env required for program-admin sync() testing
"""
os.environ["TEST_MODE"] = "1"
@pytest.fixture
async def oracle():
"""
Downloads the latest version of the oracle from the pyth-client repo
"""
api_url = "https://api.github.com/repos/pyth-network/pyth-client/releases/latest"
filename = "pyth_oracle_pythnet.so"
outfile = "tests/pyth_oracle.so"
try:
response = requests.get(api_url, timeout=300)
response.raise_for_status()
release_info = response.json()
# Find the desired asset in the release assets
asset_url = None
for asset in release_info["assets"]:
if asset["name"] == filename:
asset_url = asset["browser_download_url"]
break
if not asset_url:
raise Exception(f"Unable to find asset URL for {filename}")
# Download the asset
download_response = requests.get(asset_url, timeout=300)
download_response.raise_for_status()
# Save the file to the specified path
with open(outfile, "wb") as file:
file.write(download_response.content)
LOGGER.debug(f"File {filename} downloaded successfully to {outfile}.")
except requests.exceptions.RequestException as error:
LOGGER.error(f"An error occurred: {error}")
raise error
yield outfile
@pytest.fixture
def key_dir():
with TemporaryDirectory() as directory:
yield directory
@pytest.fixture
def products_json():
with NamedTemporaryFile(delete=False) as jsonfile:
jsonfile.write(json.dumps([BTC_USD, AAPL_USD]).encode())
jsonfile.flush()
yield jsonfile.name
@pytest.fixture
def products2_json():
with NamedTemporaryFile(delete=False) as jsonfile:
jsonfile.write(json.dumps([BTC_USD, AAPL_USD, ETH_USD]).encode())
jsonfile.flush()
yield jsonfile.name
@pytest.fixture
def publishers_json():
with NamedTemporaryFile() as jsonfile:
jsonfile.write(
json.dumps(
{
"random": "23CGbZq2AAzZcHk1vVBs9Zq4AkNJhjxRbjMiCFTy8vJP", # random key
},
).encode()
)
jsonfile.flush()
yield jsonfile.name
@pytest.fixture
def permissions_json():
with NamedTemporaryFile() as jsonfile:
jsonfile.write(
json.dumps(
{
"AAPL": {"price": ["random"]},
"BTCUSD": {"price": ["random"]},
},
).encode()
)
jsonfile.flush()
yield jsonfile.name
@pytest.fixture
def permissions2_json():
with NamedTemporaryFile() as jsonfile:
jsonfile.write(
json.dumps(
{
"AAPL": {"price": ["random"]},
"BTCUSD": {"price": ["random"]},
"ETHUSD": {"price": ["random"]},
},
).encode()
)
jsonfile.flush()
yield jsonfile.name
@pytest.fixture
def empty_overrides_json():
with NamedTemporaryFile() as jsonfile:
jsonfile.write(
json.dumps(
{},
).encode()
)
jsonfile.flush()
yield jsonfile.name
@pytest.fixture
def localhost_overrides_json():
with NamedTemporaryFile() as jsonfile:
jsonfile.write(
json.dumps(
{
"pythnet": {"AAPL": True, "BTCUSD": False},
"localhost": {"AAPL": False},
},
).encode()
)
jsonfile.flush()
yield jsonfile.name
@pytest.fixture
async def validator():
process = await asyncio.create_subprocess_shell(
"solana-test-validator --reset",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
await asyncio.sleep(5.0)
yield process.pid
process.terminate()
await process.wait()
# pylint: disable=redefined-outer-name,unused-argument
@pytest.fixture
async def pyth_keypair(key_dir, validator):
process = await asyncio.create_subprocess_shell(
f"solana-keygen new --no-bip39-passphrase -o {key_dir}/funding.json",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
await process.wait()
if process.returncode != 0:
stdout, stderr = await process.communicate()
if stdout:
LOGGER.debug(f"[stdout]\n{stdout.decode()}")
if stderr:
LOGGER.debug(f"[stderr]\n{stderr.decode()}")
raise RuntimeError("Failed to generate funding key")
yield f"{key_dir}/funding.json"
@pytest.fixture
def authority_permissions_json(key_dir, pyth_keypair):
funding_keypair = load_keypair("funding", key_dir=key_dir)
funding_key = funding_keypair.public_key
with NamedTemporaryFile() as jsonfile:
value = {
"master_authority": str(funding_key),
"data_curation_authority": str(funding_key),
"security_authority": str(funding_key),
}
LOGGER.debug("Writing authority permissions JSON:\n%s", value)
jsonfile.write(json.dumps(value).encode())
jsonfile.flush()
yield jsonfile.name
@pytest.fixture
async def upgrade_authority_keypair(key_dir, validator):
keypair_path = f"{key_dir}/upgrade_authority.json"
process = await asyncio.create_subprocess_shell(
f"solana-keygen new --no-bip39-passphrase -o {keypair_path}",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
await process.wait()
if process.returncode != 0:
stdout, stderr = await process.communicate()
if stdout:
LOGGER.debug(f"[stdout]\n{stdout.decode()}")
if stderr:
LOGGER.debug(f"[stderr]\n{stderr.decode()}")
raise RuntimeError("Failed to generate upgrade authority key")
# Fund the keypair
process = await asyncio.create_subprocess_shell(
f"solana airdrop 100 -k {keypair_path} -u localhost",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
await process.wait()
stdout, stderr = await process.communicate()
LOGGER.debug(f"[cmd exited with {process.returncode}]")
if stdout:
LOGGER.debug(f"[stdout]\n{stdout.decode()}")
if stderr:
LOGGER.debug(f"[stderr]\n{stderr.decode()}")
yield keypair_path
# pylint: disable=redefined-outer-name,unused-argument
@pytest.fixture
async def pyth_program(pyth_keypair, upgrade_authority_keypair, oracle):
process = await asyncio.create_subprocess_shell(
f"solana airdrop 100 -k {pyth_keypair} -u localhost",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
await process.wait()
stdout, stderr = await process.communicate()
LOGGER.debug(f"[cmd exited with {process.returncode}]")
if stdout:
LOGGER.debug(f"[stdout]\n{stdout.decode()}")
if stderr:
LOGGER.debug(f"[stderr]\n{stderr.decode()}")
process = await asyncio.create_subprocess_shell(
f"solana program deploy \
-k {pyth_keypair} \
-u localhost \
--upgrade-authority {upgrade_authority_keypair} \
{oracle} \
&& sleep 10",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
await process.wait()
stdout, stderr = await process.communicate()
LOGGER.debug(f"[cmd exited with {process.returncode}]")
if stdout:
LOGGER.debug(f"[stdout]\n{stdout.decode()}")
if stderr:
LOGGER.debug(f"[stderr]\n{stderr.decode()}")
_, _, program_id = stdout.decode("ascii").split()
# FIXME: This is so the mapping account kludge can work (we are bypassing
# the input args and using env. variables directly).
os.environ["PROGRAM_KEY"] = program_id
yield program_id
def test_apply_overrides():
permissions: ReferencePermissions = {
"AAPL": {"price": ["random"]},
"BTCUSD": {"price": ["random"]},
"ETHUSD": {"price": ["random"]},
}
overrides: ReferenceOverrides = {}
result = apply_overrides(permissions, overrides, "localhost")
assert result == permissions
overrides = {
"localhost": {"BTCUSD": False, "AAPL": True},
"pythnet": {"ETHUSD": False},
}
result = apply_overrides(permissions, overrides, "mainnet-beta")
assert result == permissions
result = apply_overrides(permissions, overrides, "localhost")
expected = {
"AAPL": {"price": ["random"]},
"BTCUSD": {"price": []},
"ETHUSD": {"price": ["random"]},
}
assert result == expected
result = apply_overrides(permissions, overrides, "pythnet")
expected = {
"AAPL": {"price": ["random"]},
"BTCUSD": {"price": ["random"]},
"ETHUSD": {"price": []},
}
assert result == expected
# pylint: disable=protected-access,redefined-outer-name
async def test_sync(
set_test_env_var,
key_dir,
pyth_program,
products_json,
products2_json,
publishers_json,
permissions_json,
permissions2_json,
authority_permissions_json,
empty_overrides_json,
localhost_overrides_json,
pyth_keypair,
):
network = "localhost"
program_admin = ProgramAdmin(
network=network,
key_dir=key_dir,
program_key=pyth_program,
commitment="confirmed",
)
await sync_from_files(
program_admin,
products_path=products_json,
publishers_path=publishers_json,
permissions_path=permissions_json,
authority_permissions_path=authority_permissions_json,
overrides_path=empty_overrides_json,
network=network,
allocate_price_v2=True,
generate_keys=True,
)
await program_admin.refresh_program_accounts()
product_accounts = list(program_admin._product_accounts.values())
price_accounts = list(program_admin._price_accounts.values())
authority_permission_account = program_admin.authority_permission_account
if authority_permission_account:
authority_permissions = authority_permission_account.data
else:
raise Exception("Authority permissions not found")
reference_symbols = ["Crypto.BTC/USD", "Equity.US.AAPL/USD"]
with open(publishers_json, encoding="utf8") as file:
random_publisher = PublicKey(json.load(file)["random"])
assert product_accounts[0].data.metadata["symbol"] in reference_symbols
assert product_accounts[1].data.metadata["symbol"] in reference_symbols
assert price_accounts[0].data.price_components[0].publisher_key == random_publisher
assert price_accounts[1].data.price_components[0].publisher_key == random_publisher
funding_keypair = load_keypair("funding", key_dir=key_dir)
funding_key = funding_keypair.public_key
assert str(authority_permissions.master_authority) == str(funding_key)
assert str(authority_permissions.data_curation_authority) == str(funding_key)
assert str(authority_permissions.security_authority) == str(funding_key)
# Map from symbol names to the corresponding price account
symbol_price_account_map = {}
for product_account in product_accounts:
symbol_price_account_map[product_account.data.metadata["symbol"]] = [
acc
for acc in price_accounts
if acc.public_key == product_account.data.first_price_account_key
][0]
assert symbol_price_account_map["Crypto.BTC/USD"].data.min_publishers == 7
# Warning: this test hardcodes the default minimum number of publishers for the account.
# This default may change if you upgrade the oracle program.
assert symbol_price_account_map["Equity.US.AAPL/USD"].data.min_publishers == 20
# Syncing again with generate_keys=False should succeed
await sync_from_files(
program_admin,
products_path=products_json,
publishers_path=publishers_json,
permissions_path=permissions_json,
authority_permissions_path=authority_permissions_json,
overrides_path=empty_overrides_json,
network=network,
allocate_price_v2=True,
generate_keys=False,
)
# Syncing a different product list should fail
threw_error = False
try:
await sync_from_files(
program_admin,
products_path=products2_json,
publishers_path=publishers_json,
permissions_path=permissions2_json,
authority_permissions_path=authority_permissions_json,
overrides_path=empty_overrides_json,
network=network,
allocate_price_v2=True,
generate_keys=False,
)
except RuntimeError:
threw_error = True
assert threw_error
# Test overriding network configurations
await sync_from_files(
program_admin,
products_path=products_json,
publishers_path=publishers_json,
permissions_path=permissions_json,
authority_permissions_path=authority_permissions_json,
overrides_path=localhost_overrides_json,
network=network,
allocate_price_v2=True,
generate_keys=False,
)
# Test override configuration
await program_admin.refresh_program_accounts()
product_accounts = list(program_admin._product_accounts.values())
is_enabled = {"Crypto.BTC/USD": True, "Equity.US.AAPL/USD": False}
for product_account in product_accounts:
symbol = product_account.data.metadata["symbol"]
price_account = program_admin.get_price_account(
product_account.data.first_price_account_key
)
if is_enabled[symbol]:
assert (
price_account.data.price_components[0].publisher_key == random_publisher
)
else:
assert len(price_account.data.price_components) == 0
async def sync_from_files(
program_admin,
products_path: str,
publishers_path: str,
permissions_path: str,
authority_permissions_path: str,
overrides_path: str,
network: Network,
allocate_price_v2: bool,
send_transactions: bool = True,
generate_keys: bool = False,
):
ref_products = parse_products_json(Path(products_path))
ref_publishers = parse_publishers_json(Path(publishers_path))
ref_permissions = parse_permissions_with_overrides(
Path(permissions_path), Path(overrides_path), network
)
ref_authority_permissions = parse_authority_permissions_json(
Path(authority_permissions_path)
)
return await program_admin.sync(
ref_products,
ref_publishers,
ref_permissions,
ref_authority_permissions,
send_transactions,
generate_keys,
allocate_price_v2=allocate_price_v2,
)