-
Notifications
You must be signed in to change notification settings - Fork 119
Batch updates in update_price JRPC call #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3ebbdd9
Change semantics of update_price JRPC call to buffer updates internal…
tompntn 85b3758
Send pending updates when a new slot is received
tompntn d22783d
Add additional logging to batch sending
tompntn 62a3111
Don't add a price to the list of pending updates if it is already pre…
tompntn 566a904
Add test for update_price JPRC call
tompntn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import json | ||
| from subprocess import check_output | ||
| import pytest | ||
| import websockets | ||
| import time | ||
| import itertools | ||
| import random | ||
|
|
||
| from pyth.tests.conftest import PRODUCTS | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_batch_update_price(solana_test_validator, pythd, pyth_dir, pyth_init_product, pyth_init_price): | ||
|
|
||
| messageIds = itertools.count() | ||
|
|
||
| # Use a single websocket connection for the entire test, as batching is done per-user | ||
| async with websockets.connect('ws://localhost:8910/') as ws: | ||
|
|
||
| async def update_price(account, price, conf, status): | ||
| msg = jrpc_req( | ||
| method='update_price', | ||
| params={ | ||
| 'account': account, | ||
| 'price': price, | ||
| 'conf': conf, | ||
| 'status': status, | ||
| }) | ||
|
|
||
| await send(msg) | ||
| resp = await recv() | ||
| assert resp['result'] == 0 | ||
|
|
||
| async def get_product(account): | ||
| output = check_output([ | ||
| 'pyth', 'get_product', | ||
| account, | ||
| '-r', 'localhost', | ||
| '-k', pyth_dir, | ||
| '-c', 'finalized', | ||
| '-j', | ||
| ]).decode('ascii') | ||
| result = json.loads(output) | ||
|
|
||
| return result | ||
|
|
||
| def jrpc_req(method=None, params=None): | ||
| return { | ||
| 'jsonrpc': '2.0', | ||
| 'method': method, | ||
| 'params': params, | ||
| 'id': next(messageIds) | ||
| } | ||
|
|
||
| async def send(msg): | ||
| print("--- sending message ---") | ||
| print(msg) | ||
| await ws.send(json.dumps(msg)) | ||
|
|
||
| async def recv(): | ||
| data = await ws.recv() | ||
| msg = json.loads(data) | ||
| print("----- received message -----") | ||
| print(msg) | ||
| return msg | ||
|
|
||
| def get_publisher_acc(product_acc): | ||
| assert len(product_acc['price_accounts']) == 1 | ||
| price_acc = product_acc['price_accounts'][0] | ||
|
|
||
| assert len(price_acc['publisher_accounts']) == 1 | ||
| return price_acc['publisher_accounts'][0] | ||
|
|
||
| # Check that the prices are 0 initially | ||
| for product in PRODUCTS.keys(): | ||
| product_acc = await get_product(pyth_init_product[product]) | ||
| publisher_acc = get_publisher_acc(product_acc) | ||
|
|
||
| assert publisher_acc['price'] == 0 | ||
| assert publisher_acc['conf'] == 0 | ||
| assert publisher_acc['status'] == 'unknown' | ||
|
|
||
| # Generate new values for this test | ||
| new_values = { | ||
| product: { | ||
| 'price':random.randint(1, 150), | ||
| 'conf': random.randint(1, 20), | ||
| 'status': 'trading', | ||
| } for product in PRODUCTS.keys() | ||
| } | ||
|
|
||
| # Update the values of the products | ||
| for product in PRODUCTS.keys(): | ||
| await update_price( | ||
| pyth_init_price[product], | ||
| new_values[product]['price'], | ||
| new_values[product]['conf'], | ||
| new_values[product]['status']) | ||
|
|
||
| time.sleep(80) | ||
|
|
||
| # Crank the products | ||
| for product in PRODUCTS.keys(): | ||
| await update_price( | ||
| pyth_init_price[product], | ||
| 1, | ||
| 1, | ||
| 'trading') | ||
|
|
||
| time.sleep(80) | ||
|
|
||
| # Check that the price has been updated | ||
| for product in PRODUCTS.keys(): | ||
| product_acc = await get_product(pyth_init_product[product]) | ||
| publisher_acc = get_publisher_acc(product_acc) | ||
|
|
||
| assert publisher_acc['price'] == new_values[product]['price'] | ||
tompntn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert publisher_acc['conf'] == new_values[product]['conf'] | ||
| assert publisher_acc['status'] == new_values[product]['status'] | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.