-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(backfiller): add retries to rpc calls (#118)
- Loading branch information
Showing
6 changed files
with
206 additions
and
91 deletions.
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 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 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 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 |
---|---|---|
|
@@ -2,6 +2,7 @@ mod backfiller; | |
mod db; | ||
mod metrics; | ||
mod queue; | ||
mod rpc; | ||
mod tree; | ||
|
||
use anyhow::Result; | ||
|
This file contains 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,136 @@ | ||
use anyhow::Result; | ||
use backon::ExponentialBuilder; | ||
use backon::Retryable; | ||
use clap::Parser; | ||
use solana_account_decoder::UiAccountEncoding; | ||
use solana_client::rpc_response::RpcConfirmedTransactionStatusWithSignature; | ||
use solana_client::{ | ||
client_error::ClientError, | ||
nonblocking::rpc_client::RpcClient, | ||
rpc_client::GetConfirmedSignaturesForAddress2Config, | ||
rpc_config::{RpcAccountInfoConfig, RpcProgramAccountsConfig, RpcTransactionConfig}, | ||
rpc_filter::RpcFilterType, | ||
}; | ||
use solana_sdk::{ | ||
account::Account, | ||
commitment_config::{CommitmentConfig, CommitmentLevel}, | ||
pubkey::Pubkey, | ||
signature::Signature, | ||
}; | ||
use solana_transaction_status::EncodedConfirmedTransactionWithStatusMeta; | ||
use solana_transaction_status::UiTransactionEncoding; | ||
use std::sync::Arc; | ||
|
||
#[derive(Clone, Parser, Debug)] | ||
pub struct SolanaRpcArgs { | ||
#[arg(long, env)] | ||
pub solana_rpc_url: String, | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct Rpc(Arc<RpcClient>); | ||
|
||
impl Rpc { | ||
pub fn from_config(config: SolanaRpcArgs) -> Self { | ||
Rpc(Arc::new(RpcClient::new(config.solana_rpc_url))) | ||
} | ||
|
||
pub async fn get_transaction( | ||
&self, | ||
signature: &Signature, | ||
) -> Result<EncodedConfirmedTransactionWithStatusMeta, ClientError> { | ||
(|| async { | ||
self.0 | ||
.get_transaction_with_config( | ||
signature, | ||
RpcTransactionConfig { | ||
encoding: Some(UiTransactionEncoding::Base58), | ||
max_supported_transaction_version: Some(0), | ||
commitment: Some(CommitmentConfig { | ||
commitment: CommitmentLevel::Finalized, | ||
}), | ||
..RpcTransactionConfig::default() | ||
}, | ||
) | ||
.await | ||
}) | ||
.retry(&ExponentialBuilder::default()) | ||
.await | ||
} | ||
|
||
pub async fn get_signatures_for_address( | ||
&self, | ||
pubkey: &Pubkey, | ||
before: Option<Signature>, | ||
until: Option<Signature>, | ||
) -> Result<Vec<RpcConfirmedTransactionStatusWithSignature>, ClientError> { | ||
(|| async { | ||
self.0 | ||
.get_signatures_for_address_with_config( | ||
pubkey, | ||
GetConfirmedSignaturesForAddress2Config { | ||
before, | ||
until, | ||
commitment: Some(CommitmentConfig { | ||
commitment: CommitmentLevel::Finalized, | ||
}), | ||
..GetConfirmedSignaturesForAddress2Config::default() | ||
}, | ||
) | ||
.await | ||
}) | ||
.retry(&ExponentialBuilder::default()) | ||
.await | ||
} | ||
|
||
pub async fn get_program_accounts( | ||
&self, | ||
program: &Pubkey, | ||
filters: Option<Vec<RpcFilterType>>, | ||
) -> Result<Vec<(Pubkey, Account)>, ClientError> { | ||
(|| async { | ||
let filters = filters.clone(); | ||
|
||
self.0 | ||
.get_program_accounts_with_config( | ||
program, | ||
RpcProgramAccountsConfig { | ||
filters, | ||
account_config: RpcAccountInfoConfig { | ||
encoding: Some(UiAccountEncoding::Base64), | ||
commitment: Some(CommitmentConfig { | ||
commitment: CommitmentLevel::Finalized, | ||
}), | ||
..RpcAccountInfoConfig::default() | ||
}, | ||
..RpcProgramAccountsConfig::default() | ||
}, | ||
) | ||
.await | ||
}) | ||
.retry(&ExponentialBuilder::default()) | ||
.await | ||
} | ||
|
||
pub async fn get_multiple_accounts( | ||
&self, | ||
pubkeys: &[Pubkey], | ||
) -> Result<Vec<Option<Account>>, ClientError> { | ||
Ok((|| async { | ||
self.0 | ||
.get_multiple_accounts_with_config( | ||
pubkeys, | ||
RpcAccountInfoConfig { | ||
commitment: Some(CommitmentConfig { | ||
commitment: CommitmentLevel::Finalized, | ||
}), | ||
..RpcAccountInfoConfig::default() | ||
}, | ||
) | ||
.await | ||
}) | ||
.retry(&ExponentialBuilder::default()) | ||
.await? | ||
.value) | ||
} | ||
} |
Oops, something went wrong.