Skip to content

Commit 566a904

Browse files
committed
Add test for update_price JPRC call
1 parent 62a3111 commit 566a904

File tree

4 files changed

+122
-3
lines changed

4 files changed

+122
-3
lines changed

docker/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ RUN apt-get install -qq \
1616
libzstd1 \
1717
libzstd-dev \
1818
python3-pytest \
19+
python3-pytest-asyncio \
1920
python3-websockets \
2021
sudo \
2122
zlib1g \

pc/request.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ bool price::send( price *prices[], const unsigned n )
773773
std::string( 100, '\0' ), p1->preq_->get_sent_time()
774774
);
775775
p1->preq_->get_signature()->enc_base58( p1->tvec_.back().first );
776-
PC_LOG_DBG( "sent price update transaction" )
776+
PC_LOG_DBG( "sent price update" )
777777
.add( "price_account", *p1->get_account() )
778778
.add( "product_account", *p1->prod_->get_account() )
779779
.add( "symbol", p1->get_symbol() )

pyth/tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,12 @@ def pythd(solana_test_validator, pyth_dir):
347347
'-x',
348348
'-m', 'finalized',
349349
'-d',
350+
'-l', 'pyth_logs.txt',
350351
]
351352
kwargs = {
352353
'stdin': DEVNULL,
353-
'stdout': DEVNULL,
354-
'stderr': DEVNULL,
355354
}
355+
356356
with Popen(cmd, **kwargs) as p:
357357
time.sleep(3)
358358
yield

pyth/tests/test_update_price.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import json
2+
from subprocess import check_output
3+
import pytest
4+
import websockets
5+
import time
6+
import itertools
7+
import random
8+
9+
from pyth.tests.conftest import PRODUCTS
10+
11+
@pytest.mark.asyncio
12+
async def test_batch_update_price(solana_test_validator, pythd, pyth_dir, pyth_init_product, pyth_init_price):
13+
14+
messageIds = itertools.count()
15+
16+
# Use a single websocket connection for the entire test, as batching is done per-user
17+
async with websockets.connect('ws://localhost:8910/') as ws:
18+
19+
async def update_price(account, price, conf, status):
20+
msg = jrpc_req(
21+
method='update_price',
22+
params={
23+
'account': account,
24+
'price': price,
25+
'conf': conf,
26+
'status': status,
27+
})
28+
29+
await send(msg)
30+
resp = await recv()
31+
assert resp['result'] == 0
32+
33+
async def get_product(account):
34+
output = check_output([
35+
'pyth', 'get_product',
36+
account,
37+
'-r', 'localhost',
38+
'-k', pyth_dir,
39+
'-c', 'finalized',
40+
'-j',
41+
]).decode('ascii')
42+
result = json.loads(output)
43+
44+
return result
45+
46+
def jrpc_req(method=None, params=None):
47+
return {
48+
'jsonrpc': '2.0',
49+
'method': method,
50+
'params': params,
51+
'id': next(messageIds)
52+
}
53+
54+
async def send(msg):
55+
print("--- sending message ---")
56+
print(msg)
57+
await ws.send(json.dumps(msg))
58+
59+
async def recv():
60+
data = await ws.recv()
61+
msg = json.loads(data)
62+
print("----- received message -----")
63+
print(msg)
64+
return msg
65+
66+
def get_publisher_acc(product_acc):
67+
assert len(product_acc['price_accounts']) == 1
68+
price_acc = product_acc['price_accounts'][0]
69+
70+
assert len(price_acc['publisher_accounts']) == 1
71+
return price_acc['publisher_accounts'][0]
72+
73+
# Check that the prices are 0 initially
74+
for product in PRODUCTS.keys():
75+
product_acc = await get_product(pyth_init_product[product])
76+
publisher_acc = get_publisher_acc(product_acc)
77+
78+
assert publisher_acc['price'] == 0
79+
assert publisher_acc['conf'] == 0
80+
assert publisher_acc['status'] == 'unknown'
81+
82+
# Generate new values for this test
83+
new_values = {
84+
product: {
85+
'price':random.randint(1, 150),
86+
'conf': random.randint(1, 20),
87+
'status': 'trading',
88+
} for product in PRODUCTS.keys()
89+
}
90+
91+
# Update the values of the products
92+
for product in PRODUCTS.keys():
93+
await update_price(
94+
pyth_init_price[product],
95+
new_values[product]['price'],
96+
new_values[product]['conf'],
97+
new_values[product]['status'])
98+
99+
time.sleep(80)
100+
101+
# Crank the products
102+
for product in PRODUCTS.keys():
103+
await update_price(
104+
pyth_init_price[product],
105+
1,
106+
1,
107+
'trading')
108+
109+
time.sleep(80)
110+
111+
# Check that the price has been updated
112+
for product in PRODUCTS.keys():
113+
product_acc = await get_product(pyth_init_product[product])
114+
publisher_acc = get_publisher_acc(product_acc)
115+
116+
assert publisher_acc['price'] == new_values[product]['price']
117+
assert publisher_acc['conf'] == new_values[product]['conf']
118+
assert publisher_acc['status'] == new_values[product]['status']

0 commit comments

Comments
 (0)