Skip to content

Commit

Permalink
minimal test for the vector logic
Browse files Browse the repository at this point in the history
  • Loading branch information
grooviegermanikus committed Aug 7, 2024
1 parent 1a66598 commit e34c443
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion connector/src/chain_data.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use solana_sdk::clock::Slot;

Check warning on line 1 in connector/src/chain_data.rs

View workflow job for this annotation

GitHub Actions / test

Diff in /home/runner/work/mango-feeds/mango-feeds/connector/src/chain_data.rs
use std::str::FromStr;
use log::info;
use {
solana_sdk::account::{AccountSharedData, ReadableAccount},
solana_sdk::pubkey::Pubkey,
std::collections::HashMap,

Check warning on line 7 in connector/src/chain_data.rs

View workflow job for this annotation

GitHub Actions / test

Diff in /home/runner/work/mango-feeds/mango-feeds/connector/src/chain_data.rs
};
use crate::chain_data::WhatToDo::*;

use crate::metrics::*;

Expand Down Expand Up @@ -164,25 +166,35 @@ impl ChainData {
use std::collections::hash_map::Entry;

Check warning on line 166 in connector/src/chain_data.rs

View workflow job for this annotation

GitHub Actions / test

Diff in /home/runner/work/mango-feeds/mango-feeds/connector/src/chain_data.rs
match self.accounts.entry(pubkey) {
Entry::Vacant(v) => {
println!("update_account_vacant: slot={}, write_version={}", account.slot, account.write_version);
self.account_versions_stored += 1;
self.account_bytes_stored += account.account.data().len();
v.insert(vec![account]);
}
Entry::Occupied(o) => {
println!("update_account_occupied: slot={}, write_version={}", account.slot, account.write_version);
let v = o.into_mut();

for el in v.iter() {
println!("> el.slot={}, el.write_version={}", el.slot, el.write_version);
}

// v is ordered by slot ascending. find the right position
// overwrite if an entry for the slot already exists, otherwise insert
let rev_pos = v
.iter()
.rev()
.position(|d| d.slot <= account.slot)
.unwrap_or(v.len());
println!("rev_pos={}", rev_pos);
let pos = v.len() - rev_pos;
if pos < v.len() && v[pos].slot == account.slot {
println!("now check write version {} <= {}", v[pos].write_version, account.write_version);
if v[pos].write_version <= account.write_version {
v[pos] = account;
}
} else {
println!("insert it");
self.account_versions_stored += 1;
self.account_bytes_stored += account.account.data().len();
v.insert(pos, account);
Expand Down Expand Up @@ -456,7 +468,6 @@ pub fn test_must_not_overwrite_with_older_by_slot() {
}

#[test]
#[ignore]
pub fn test_overwrite_with_older_by_write_version() {
const SLOT: Slot = 42_000_000;

Expand Down Expand Up @@ -501,3 +512,57 @@ pub fn test_overwrite_with_older_by_write_version() {
"should not overwrite if write_version is older"
);
}

enum WhatToDo {
Overwrite(usize),
Insert,
DoNothing,
}


#[test]
fn asdf() {
let mut v = vec![
AccountData {
slot: 1,
write_version: 1,
account: AccountSharedData::new(1, 1, &Pubkey::new_unique()),
},
AccountData {
slot: 2,
write_version: 1,
account: AccountSharedData::new(1, 1, &Pubkey::new_unique()),
},
AccountData {
slot: 3,
write_version: 1,
account: AccountSharedData::new(1, 1, &Pubkey::new_unique()),
},
];

the_logic(&mut v, 2, 1);

}

fn the_logic(v: &mut Vec<AccountData>, update_slot: Slot, update_write_version: u64) -> WhatToDo {
let rev_pos = v
.iter()
.rev()
.position(|d| d.slot <= update_slot)
.unwrap_or(v.len());
println!("rev_pos={}", rev_pos);
let pos = v.len() - rev_pos;
if pos < v.len() && v[pos].slot == update_slot {
println!("now check write version {} <= {}", v[pos].write_version, update_write_version);
if v[pos].write_version <= update_write_version {
// v[pos] = account;
return Overwrite(pos);
} else {
return DoNothing;
}
} else {
println!("insert it");
// self.accoxpos, account);
return Insert;
}
}

0 comments on commit e34c443

Please sign in to comment.