Skip to content

Commit

Permalink
ancestry tests passing.
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Aug 31, 2023
1 parent ef5ab6f commit 9e4b68c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 89 deletions.
1 change: 1 addition & 0 deletions ol/fixtures/rescue/test_ancestry_recovery_post.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions ol/fixtures/rescue/test_ancestry_recovery_pre.json

Large diffs are not rendered by default.

109 changes: 26 additions & 83 deletions ol/genesis-tools/src/ancestry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! functions for parsing ancestry file and updating a recovery file with new ancestry data
use std::path::PathBuf;
use ol_types::legacy_recovery::LegacyRecovery;
use ol_types::ancestry::AncestryResource;
// use ol_types::ancestry::AncestryResource;
use diem_types::account_address::AccountAddress;
use serde::Deserialize;

Expand All @@ -14,105 +14,42 @@ pub struct Ancestry {
pub parent_tree: Vec<AccountAddress>
}

// #[derive(Debug, Clone, Deserialize)]
// /// format of file for recovery
// pub struct JsonAncestry {
// address: AccountAddress,
// tree: JsonTree,
// }

// #[derive(Debug, Clone, Deserialize)]
// /// just the relevant fields from the exported file
// pub struct JsonTree {
// #[serde(default = "default_addr")]
// parent: AccountAddress,
// }

// fn default_addr() -> AccountAddress {
// AccountAddress::from_hex_literal("0x666").unwrap()
// }

/// parse the ancestry json file
pub fn parse_ancestry_json(path: PathBuf) -> anyhow::Result<Vec<Ancestry>>{
let json_str = std::fs::read_to_string(path)?;
Ok(serde_json::from_str(&json_str)?)
}

// /// function for searching all ancestry data and compiling list.
// pub fn find_all_ancestors(my_account: AccountAddress, list: &Vec<Ancestry>) -> anyhow::Result<Vec<AccountAddress>>{
// let mut my_ancestors: Vec<AccountAddress> = vec![];
// let mut i = 0;

// let mut parent_to_find_next = my_account;

// while i < 100 {
// let parent_struct = list.iter()
// .find(|el|{
// el.address == parent_to_find_next
// });
// if let Some(p) = parent_struct {
// if p.tree.parent == AccountAddress::ZERO { break };
// my_ancestors.push(p.tree.parent); // starts with my_address, which we do not include in list
// parent_to_find_next = p.tree.parent;
// } else {
// break;
// }
// i+=1;
// }
// // need to reverse such that oldest is 0th.
// my_ancestors.reverse();
// Ok(my_ancestors)
// }


// /// maps json struct into the same format as the chain struct
// pub fn map_ancestry(list: &Vec<JsonAncestry>) -> anyhow::Result<Vec<Ancestry>>{
// list.iter()
// .map(|el| {
// let tree = find_all_ancestors(el.address, list).unwrap_or(vec![]);
// Ok(Ancestry {
// address: el.address,
// tree,
// })
// })
// .collect()
// }

/// patch the recovery data structure with updated ancestry information
pub fn fix_legacy_recovery_data(legacy: &mut [LegacyRecovery], ancestry: &mut [Ancestry]) {
let mut corrections_made = 0u64;
ancestry.iter_mut().for_each(|a| {
ancestry.iter_mut().for_each(|correct| {
// for every record in ancestry, find the corresponding in
// legacy data struct
let legacy_data = legacy.iter_mut().find(|l| {
if let Some(acc) = l.account {
acc == a.account
} else { false }
l.account == Some(correct.account)
});

if let Some(l) = legacy_data {
let resource_type = AncestryResource {
tree: a.parent_tree.clone()
};

if let Some(anc) = &l.ancestry {
a.parent_tree.retain(|el| {
el != &AccountAddress::ZERO
});
if anc.tree != a.parent_tree {
l.ancestry = Some(resource_type);
corrections_made += 1;
}
}

correct.parent_tree.retain(|el| {
el != &AccountAddress::ZERO
});

l.ancestry = Some(ol_types::ancestry::AncestryResource { tree: correct.parent_tree.clone() });
corrections_made += 1;
}
});
println!("corrections made: {corrections_made}");
println!("ancestry corrections made: {corrections_made}");
}

#[test]
fn test_fix() {
let a = Ancestry {
account: "02A892A449874E2BE18B7EA814688B04".parse().unwrap(),
parent_tree: vec![
"00000000000000000000000000000000".parse().unwrap(),
"C0A1F4D49658CF2FE5402E10F496BB80".parse().unwrap(),
"B080A6E0464CCA28ED6C7E116FECB837".parse().unwrap(),
"1EE5432BD3C6374E33798C4C9EDCD0CF".parse().unwrap(),
Expand All @@ -127,9 +64,14 @@ fn test_fix() {
assert!(l.ancestry.is_none());
let mut vec = vec![l];

fix_legacy_recovery_data(&mut vec, &mut [a] );
fix_legacy_recovery_data(&mut vec, &mut [a.clone()] );
dbg!(&vec);
assert!(&vec.iter().next().unwrap().ancestry.is_some());
let anc = vec.get(0).unwrap().ancestry.as_ref().expect("should have an ancestry struct here");
// assert!(&anc.is_some());

// check the 0x0 address is dropped
assert!(anc.tree.len() == (a.parent_tree.len() - 1));


}

Expand Down Expand Up @@ -160,12 +102,13 @@ fn test_find() {
let json_ancestry = parse_ancestry_json(p).unwrap();
let my_account = AccountAddress::from_hex_literal("0x242a49d3c5e141e9ca59b42ed45b917c").unwrap();
let all = &json_ancestry.iter().find(|el|{el.account == my_account}).unwrap().parent_tree;
// let my_account = json_ancestry.iter().next().unwrap().address;
// let all = find_all_ancestors(my_account, &json_ancestry).unwrap();
// dbg!(&all);

assert!(all.len() == 5);
assert!(all[0] == AccountAddress::from_hex_literal("0x00000000000000000000000000000000").unwrap());
assert!(all[4] == AccountAddress::from_hex_literal("64D54A14BA2F83C14DE003FAC6E8F6AD").unwrap());
assert!(all[0] == AccountAddress::ZERO);
assert!(all[1] == AccountAddress::from_hex_literal("0xBDB8AD37341CEC0817FD8E2474E25031").unwrap());
assert!(all[2] == AccountAddress::from_hex_literal("0xCD7C59C9D7CA50FE417E3083771FA7E8").unwrap());
assert!(all[3] == AccountAddress::from_hex_literal("0x763A077E0EFA9A5CE86CD5C9FADDE32B").unwrap());
assert!(all[4] == AccountAddress::from_hex_literal("0x64D54A14BA2F83C14DE003FAC6E8F6AD").unwrap());

}

Expand Down
31 changes: 25 additions & 6 deletions ol/genesis-tools/tests/fix_ancestry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,43 @@ async fn fix_ancestry() {
let mut recovery = db_backup_into_recovery_struct(&backup)
.await
.expect("could not export backup into json file");
let output = backup.parent().unwrap().parent().unwrap().join("test_recovery_pre.json");
save_recovery_file(&recovery, &output)

// This specific account is an example of the 4,000
// accounts which do not have ancestry metadata.
let record = recovery.iter().find(|a| {
a.account == AccountAddress::from_hex_literal("0x242a49d3c5e141e9ca59b42ed45b917c").ok()
}).unwrap();

// MISSING DATA!
assert!(record.ancestry.is_none());


let output_pre = backup.parent().unwrap().parent().unwrap().join("test_ancestry_recovery_pre.json");
save_recovery_file(&recovery, &output_pre)
.expect("ERROR: failed to create recovery from snapshot,");

let p = json_path().parent().unwrap().join("ancestry_v7.json");
let mut proper_ancestry = ancestry::parse_ancestry_json(p).unwrap();

ancestry::fix_legacy_recovery_data(&mut recovery, &mut proper_ancestry);

let output = backup.parent().unwrap().parent().unwrap().join("test_recovery_post.json");
let output_post = backup.parent().unwrap().parent().unwrap().join("test_ancestry_recovery_post.json");

let record = recovery.iter().find(|a| {
a.account == AccountAddress::from_hex_literal("0x242a49d3c5e141e9ca59b42ed45b917c").ok()
}).unwrap();
assert!(record.ancestry.is_some());
let tree = &record.ancestry.as_ref().expect("should definitly have fixed ancestry").tree;

assert!(tree.len() == 4);
assert!(tree[0] != AccountAddress::ZERO); // should not have the 0x0 address
assert!(tree[0] == AccountAddress::from_hex_literal("0xBDB8AD37341CEC0817FD8E2474E25031").unwrap());
assert!(tree[1] == AccountAddress::from_hex_literal("0xCD7C59C9D7CA50FE417E3083771FA7E8").unwrap());
assert!(tree[2] == AccountAddress::from_hex_literal("0x763A077E0EFA9A5CE86CD5C9FADDE32B").unwrap());
assert!(tree[3] == AccountAddress::from_hex_literal("0x64D54A14BA2F83C14DE003FAC6E8F6AD").unwrap());

save_recovery_file(&recovery, &output)
save_recovery_file(&recovery, &output_post)
.expect("ERROR: failed to create recovery from snapshot,");
// fs::remove_file(output).unwrap();
// fs::remove_file(output_pre).unwrap();
// fs::remove_file(output_post).unwrap();
}

0 comments on commit 9e4b68c

Please sign in to comment.