forked from neonevm/neon-evm
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NDEV-3112. Optimize getting of deactivated features (#566)
* NDEV-3112. Optimize getting of deactivated features * NDEV-3112. Fix incorrect logic * NDEV-3112. Make unique cache for deactivated features * NDEV-3112. Fix bug, add test for deactivated_features --------- Co-authored-by: Dzmitry Zdanovich <[email protected]> Co-authored-by: Semen Medvedev <[email protected]>
- Loading branch information
1 parent
9c9a415
commit d55ea02
Showing
9 changed files
with
270 additions
and
73 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
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,130 @@ | ||
mod deactivated_features_tests { | ||
use std::collections::HashMap; | ||
|
||
use async_trait::async_trait; | ||
use solana_account_decoder::UiDataSliceConfig as SliceConfig; | ||
use solana_client::client_error::Result as ClientResult; | ||
use solana_sdk::{ | ||
account::{Account, AccountSharedData}, | ||
clock::{Slot, UnixTimestamp}, | ||
feature::Feature, | ||
feature_set, | ||
pubkey::Pubkey, | ||
}; | ||
|
||
use crate::{ | ||
rpc::Rpc, | ||
types::deactivated_features::{ | ||
get_deactivated_features_at_slot, set_deactivated_features_rpc, | ||
}, | ||
}; | ||
|
||
#[derive(Clone)] | ||
struct RpcMockFeatures { | ||
pub features: HashMap<Pubkey, Option<u64>>, | ||
} | ||
|
||
impl Default for RpcMockFeatures { | ||
fn default() -> Self { | ||
let accounts: Vec<_> = feature_set::FEATURE_NAMES.keys().copied().collect(); | ||
|
||
let mut features = HashMap::<Pubkey, Option<u64>>::new(); | ||
|
||
for (slot, account) in accounts.iter().enumerate() { | ||
let slot = if slot == 0 { None } else { Some(slot as u64) }; | ||
features.insert(*account, slot); | ||
} | ||
|
||
Self { features } | ||
} | ||
} | ||
|
||
#[async_trait(?Send)] | ||
impl Rpc for RpcMockFeatures { | ||
async fn get_account_slice( | ||
&self, | ||
_key: &Pubkey, | ||
_slice: Option<SliceConfig>, | ||
) -> ClientResult<Option<Account>> { | ||
todo!() | ||
} | ||
|
||
async fn get_multiple_accounts( | ||
&self, | ||
pubkeys: &[Pubkey], | ||
) -> ClientResult<Vec<Option<Account>>> { | ||
let mut result: Vec<Option<Account>> = vec![]; | ||
|
||
for pubkey in pubkeys.iter() { | ||
let feature = self.features.get(pubkey); | ||
|
||
match feature { | ||
Some(slot) => { | ||
let mut data = | ||
AccountSharedData::new(1_000_000_000, 100, &solana_sdk::feature::ID); | ||
if solana_sdk::feature::to_account( | ||
&Feature { | ||
activated_at: *slot, | ||
}, | ||
&mut data, | ||
) | ||
.is_some() | ||
{ | ||
result.push(Some(data.into())); | ||
} else { | ||
result.push(None) | ||
} | ||
} | ||
None => result.push(None), | ||
} | ||
} | ||
|
||
Ok(result) | ||
} | ||
|
||
async fn get_block_time(&self, _slot: Slot) -> ClientResult<UnixTimestamp> { | ||
todo!() | ||
} | ||
|
||
async fn get_slot(&self) -> ClientResult<Slot> { | ||
todo!() | ||
} | ||
|
||
async fn get_deactivated_solana_features(&self) -> ClientResult<Vec<Pubkey>> { | ||
todo!() | ||
} | ||
} | ||
|
||
async fn get_deactivated_features_and_wait(slot: Option<u64>) -> Vec<Pubkey> { | ||
get_deactivated_features_at_slot(slot) | ||
.await | ||
.expect("get deactivated features failed at slot unexpectedly") | ||
} | ||
|
||
// by default? mock rpc contains vector of features: RpcMockFeatures::features | ||
// features[0] is not activated | ||
// features[1] is activated at slot 1 | ||
// features[i] is activated at slot i | ||
// let we have N features overall, then... | ||
// only 1 feature will be deactivated on slot N (features[0]) | ||
// only 2 features will be deactivated on slot N-1 (features[0], features[n-1] (last one) which should be activated at slot N) | ||
// ... | ||
// all features will be deactivated on slot 0 | ||
#[tokio::test] | ||
async fn test_deactivated_features_cache() { | ||
let rpc = RpcMockFeatures::default(); | ||
let features_cnt = rpc.features.len() as u64; | ||
set_deactivated_features_rpc(rpc.clone()).await; | ||
|
||
for i in 0..features_cnt { | ||
let features = get_deactivated_features_and_wait(Some(i)).await; | ||
|
||
assert_eq!(features_cnt - i, features.len() as u64); | ||
} | ||
{ | ||
// current slot | ||
let features = get_deactivated_features_and_wait(None).await; | ||
assert_eq!(features.len(), 1); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.