-
Notifications
You must be signed in to change notification settings - Fork 19
feat: RPC redundancy #157
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
feat: RPC redundancy #157
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
39850f3
feat: RPC redundancy
merolish ccd4a13
Merge branch 'main' of github.com:pyth-network/pyth-agent into mike/r…
merolish 7ed93c6
Refactor into rpc_multi_client module
merolish f4e871e
clippy
merolish 062bc3b
Round robin through wss_urls
merolish 07b4569
Log wss url on disconnect
merolish 6da0188
cargo update
merolish df7e8ee
Merge branch 'main' of github.com:pyth-network/pyth-agent into mike/r…
merolish 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -111,7 +111,7 @@ pub struct NetworkState { | |
/// fetching the blockhash and slot number. | ||
struct NetworkStateQuerier { | ||
/// The RPC client | ||
rpc_client: RpcClient, | ||
rpc_clients: Vec<RpcClient>, | ||
|
||
/// The interval with which to query the network state | ||
query_interval: Interval, | ||
|
@@ -122,20 +122,24 @@ struct NetworkStateQuerier { | |
|
||
impl NetworkStateQuerier { | ||
#[instrument( | ||
skip(rpc_endpoint, rpc_timeout, query_interval), | ||
skip(rpc_urls, rpc_timeout, query_interval), | ||
fields( | ||
rpc_timeout = rpc_timeout.as_millis(), | ||
query_interval = query_interval.period().as_millis(), | ||
) | ||
)] | ||
pub fn new( | ||
rpc_endpoint: &str, | ||
rpc_urls: &Vec<String>, | ||
rpc_timeout: Duration, | ||
query_interval: Interval, | ||
network_state_tx: watch::Sender<NetworkState>, | ||
) -> Self { | ||
let rpc_clients = rpc_urls | ||
.iter() | ||
.map(|rpc_url| RpcClient::new_with_timeout(rpc_url.clone(), rpc_timeout)) | ||
.collect(); | ||
NetworkStateQuerier { | ||
rpc_client: RpcClient::new_with_timeout(rpc_endpoint.to_string(), rpc_timeout), | ||
rpc_clients, | ||
query_interval, | ||
network_state_tx, | ||
} | ||
|
@@ -152,11 +156,12 @@ impl NetworkStateQuerier { | |
|
||
#[instrument(skip(self))] | ||
async fn query_network_state(&mut self) -> Result<()> { | ||
// TODO: These are polled every 200ms and errors are simply logged. | ||
// TODO: Should we retry/fallback on failure? | ||
// Fetch the blockhash and current slot in parallel | ||
let current_slot_future = self | ||
.rpc_client | ||
.get_slot_with_commitment(CommitmentConfig::confirmed()); | ||
let latest_blockhash_future = self.rpc_client.get_latest_blockhash(); | ||
let current_slot_future = | ||
self.rpc_clients[0].get_slot_with_commitment(CommitmentConfig::confirmed()); | ||
let latest_blockhash_future = self.rpc_clients[0].get_latest_blockhash(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's important for this to work properly as the data from these two are used in the transactions. Please loop through urls |
||
|
||
let (current_slot_result, latest_blockhash_result) = | ||
future::join(current_slot_future, latest_blockhash_future).await; | ||
|
@@ -183,7 +188,7 @@ where | |
// Create and spawn the network state querier | ||
let (network_state_tx, network_state_rx) = watch::channel(Default::default()); | ||
let mut network_state_querier = NetworkStateQuerier::new( | ||
&config.rpc_url, | ||
&config.rpc_urls, | ||
config.rpc_timeout, | ||
tokio::time::interval(config.exporter.refresh_network_state_interval_duration), | ||
network_state_tx, | ||
|
@@ -226,6 +231,7 @@ mod exporter { | |
}, | ||
}, | ||
solana_client::nonblocking::rpc_client::RpcClient, | ||
solana_sdk::commitment_config::CommitmentConfig, | ||
std::sync::Arc, | ||
tokio::sync::watch, | ||
}; | ||
|
@@ -243,10 +249,21 @@ mod exporter { | |
let mut dynamic_compute_unit_price_update_interval = | ||
tokio::time::interval(config.exporter.publish_interval_duration); | ||
|
||
let client = Arc::new(RpcClient::new_with_timeout( | ||
config.rpc_url.to_string(), | ||
config.rpc_timeout, | ||
)); | ||
let clients: Arc<Vec<RpcClient>> = Arc::new( | ||
config | ||
.rpc_urls | ||
.iter() | ||
.map(|rpc_url| { | ||
RpcClient::new_with_timeout_and_commitment( | ||
rpc_url.clone(), | ||
config.rpc_timeout, | ||
CommitmentConfig { | ||
commitment: config.oracle.commitment, | ||
}, | ||
) | ||
}) | ||
.collect(), | ||
); | ||
let Ok(key_store) = KeyStore::new(config.key_store.clone()) else { | ||
tracing::warn!("Key store not available, Exporter won't start."); | ||
return; | ||
|
@@ -265,7 +282,7 @@ mod exporter { | |
let publisher_buffer_key = Exporter::get_publisher_buffer_key(&*state).await; | ||
if let Err(err) = publish_batches( | ||
state.clone(), | ||
client.clone(), | ||
clients.clone(), | ||
network, | ||
&network_state_rx, | ||
key_store.accumulator_key, | ||
|
@@ -293,7 +310,7 @@ mod exporter { | |
if let Err(err) = Exporter::update_recent_compute_unit_price( | ||
&*state, | ||
&publish_keypair, | ||
&client, | ||
&clients, | ||
config.exporter.staleness_threshold, | ||
config.exporter.unchanged_publish_threshold, | ||
).await { | ||
|
@@ -352,13 +369,17 @@ mod transaction_monitor { | |
where | ||
S: Transactions, | ||
{ | ||
let client = RpcClient::new_with_timeout(config.rpc_url.to_string(), config.rpc_timeout); | ||
let rpc_clients = config | ||
.rpc_urls | ||
.iter() | ||
.map(|rpc_url| RpcClient::new_with_timeout(rpc_url.clone(), config.rpc_timeout)) | ||
.collect(); | ||
let mut poll_interval = | ||
tokio::time::interval(config.exporter.transaction_monitor.poll_interval_duration); | ||
|
||
loop { | ||
poll_interval.tick().await; | ||
if let Err(err) = Transactions::poll_transactions_status(&*state, &client).await { | ||
if let Err(err) = Transactions::poll_transactions_status(&*state, &rpc_clients).await { | ||
tracing::error!(err = ?err, "Transaction monitor failed."); | ||
} | ||
} | ||
|
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This breaks backward compatibility, let's bump the version as a major one. (we can ship it with lazer in one go)