Skip to content

Commit f534e30

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

File tree

4 files changed

+109
-3
lines changed

4 files changed

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

0 commit comments

Comments
 (0)