Skip to content

Commit

Permalink
revert get_chains logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rnovikov committed Dec 10, 2024
1 parent 823a92a commit 4161c2e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
3 changes: 3 additions & 0 deletions evm_loader/lib/src/account_storage_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ mod mock_rpc_client {

#[async_trait(?Send)]
impl BuildConfigSimulator for MockRpcClient {
fn use_cache_for_chains(&self) -> bool {
false
}
async fn build_config_simulator(&self, _program_id: Pubkey) -> NeonResult<ConfigSimulator> {
unimplemented!();
}
Expand Down
23 changes: 21 additions & 2 deletions evm_loader/lib/src/commands/get_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use solana_sdk::signer::Signer;
use solana_sdk::{instruction::Instruction, pubkey::Pubkey, transaction::Transaction};

use crate::NeonResult;

use tokio::sync::OnceCell;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Status {
Ok,
Expand Down Expand Up @@ -60,6 +60,7 @@ pub enum ConfigSimulator<'r> {
#[async_trait(?Send)]
#[enum_dispatch]
pub trait BuildConfigSimulator: Rpc {
fn use_cache_for_chains(&self) -> bool;
async fn get_config(&self, program_id: Pubkey) -> NeonResult<GetConfigResponse> {
let maybe_slot = self.get_last_deployed_slot(&program_id).await?;
if let Some(slot) = maybe_slot {
Expand Down Expand Up @@ -101,6 +102,9 @@ pub trait BuildConfigSimulator: Rpc {

#[async_trait(?Send)]
impl BuildConfigSimulator for CloneRpcClient {
fn use_cache_for_chains(&self) -> bool {
true
}
async fn build_config_simulator(&self, program_id: Pubkey) -> NeonResult<ConfigSimulator> {
Ok(ConfigSimulator::CloneRpcClient {
program_id,
Expand All @@ -111,6 +115,9 @@ impl BuildConfigSimulator for CloneRpcClient {

#[async_trait(?Send)]
impl BuildConfigSimulator for CallDbClient {
fn use_cache_for_chains(&self) -> bool {
false
}
async fn build_config_simulator(&self, program_id: Pubkey) -> NeonResult<ConfigSimulator> {
let mut simulator = SolanaSimulator::new_without_sync(self).await?;
simulator.sync_accounts(self, &[program_id]).await?;
Expand Down Expand Up @@ -289,7 +296,7 @@ impl ConfigSimulator<'_> {
Ok(result)
}
}

static CHAINS_CACHE: OnceCell<Vec<ChainInfo>> = OnceCell::const_new();
pub async fn execute(
rpc: &impl BuildConfigSimulator,
program_id: Pubkey,
Expand All @@ -301,6 +308,18 @@ pub async fn read_chains(
rpc: &impl BuildConfigSimulator,
program_id: Pubkey,
) -> NeonResult<Vec<ChainInfo>> {
if rpc.use_cache_for_chains() {
let result = CHAINS_CACHE
.get_or_init(|| async {
rpc.get_config(program_id)
.await
.expect(" get config error for chain info")
.chains
})
.await
.clone();
return Ok(result);
}
Ok(rpc.get_config(program_id).await?.chains)
}

Expand Down

0 comments on commit 4161c2e

Please sign in to comment.