Skip to content

Commit

Permalink
[tools] debugger tools account state dump (#189)
Browse files Browse the repository at this point in the history
Co-authored-by: 0o-de-lally <1364012+0o-de-lally@users.noreply.github.com>
  • Loading branch information
nonast and 0o-de-lally committed Aug 8, 2024
1 parent 46484e9 commit 9450d0e
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 44 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion tools/genesis/src/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub fn compare_recovery_vec_to_genesis_tx(
supply: &Supply,
) -> Result<Vec<CompareError>, anyhow::Error> {
let mut err_list: Vec<CompareError> = vec![];
let mut user_supply = 0u64;

recovery
.iter_mut()
Expand Down Expand Up @@ -100,6 +101,8 @@ pub fn compare_recovery_vec_to_genesis_tx(
});
}

user_supply += on_chain_balance.coin();

// Check Slow Wallet Balance was migrated as expected
if let Some(old_slow) = &old.slow_wallet {
let new_slow = account_state_view
Expand Down Expand Up @@ -129,7 +132,6 @@ pub fn compare_recovery_vec_to_genesis_tx(
}
}
});

Ok(err_list)
}

Expand Down
32 changes: 26 additions & 6 deletions tools/genesis/src/supply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@ pub struct Supply {
pub slow_locked: f64,
pub slow_validator_locked: f64,
pub slow_unlocked: f64,
pub donor_directed: f64,
pub donor_voice: f64,
pub make_whole: f64,
// which will compute later
pub split_factor: f64,
pub escrow_pct: f64,
pub epoch_reward_base_case: f64,
pub expected_user_balance: f64,
pub expected_user_ratio: f64,
pub expected_circulating: f64,
pub expected_circulating_ratio: f64,
}

impl Supply {
Expand All @@ -67,11 +71,23 @@ impl Supply {
// get the coin amount of chosen future uses
let target_future_uses = settings.target_future_uses * self.total;
// excluding donor directed, how many coins are remaining to fund
let remaining_to_fund = target_future_uses - self.donor_directed;
let remaining_to_fund = target_future_uses - self.donor_voice;
// what the ratio of remaining to fund, compared to validator_slow_locked
self.escrow_pct = remaining_to_fund / self.slow_validator_locked;
self.epoch_reward_base_case =
remaining_to_fund / (365 * 100 * settings.years_escrow) as f64; // one hundred validators over 7 years every day. Note: discussed elsewhere: if this is an over estimate, the capital gets returned to community by the daily excess burn.
self.expected_user_balance = self.split_factor
* (
self.normal +
(self.slow_total - self.slow_validator_locked ) + // remove vals
(self.slow_validator_locked * (1.0 - self.escrow_pct))
// add back vals after escrow
);
let total_scaled = self.total * self.split_factor;
self.expected_user_ratio = self.expected_user_balance / total_scaled;

self.expected_circulating = self.split_factor * (self.normal + self.slow_unlocked);
self.expected_circulating_ratio = self.expected_circulating / total_scaled;

Ok(())
}
Expand Down Expand Up @@ -105,7 +121,7 @@ fn inc_supply(
// sum all accounts
if dd_wallet_list.contains(&r.account.unwrap()) {
// is this categorized as a donor voice account?
acc.donor_directed += user_total;
acc.donor_voice += user_total;
} else if let Some(sl) = &r.slow_wallet {
// is it a slow wallet?
acc.slow_total += user_total;
Expand Down Expand Up @@ -155,11 +171,15 @@ pub fn populate_supply_stats_from_legacy(
slow_locked: 0.0,
slow_validator_locked: 0.0,
slow_unlocked: 0.0,
donor_directed: 0.0,
donor_voice: 0.0,
make_whole: 0.0,
split_factor: 0.0,
escrow_pct: 0.0,
epoch_reward_base_case: 0.0,
expected_user_balance: 0.0,
expected_user_ratio: 0.0,
expected_circulating: 0.0,
expected_circulating_ratio: 0.0,
};

let dd_wallets = rec
Expand Down Expand Up @@ -215,7 +235,7 @@ fn test_genesis_math() {

println!("before");
let pct_normal = supply.normal / supply.total;
let pct_dd = supply.donor_directed / supply.total;
let pct_dd = supply.donor_voice / supply.total;
let pct_slow = supply.slow_total / supply.total;
let pct_mk_whole = supply.make_whole / supply.total;
let _pct_val_locked = supply.slow_validator_locked / supply.total;
Expand All @@ -238,6 +258,6 @@ fn test_genesis_math() {
// dbg!(new_slow / supply.total);
// dbg!(to_escrow / supply.total);

let sum_all = to_escrow + new_slow + supply.normal + supply.donor_directed + supply.make_whole;
let sum_all = to_escrow + new_slow + supply.normal + supply.donor_voice + supply.make_whole;
assert!(supply.total == sum_all);
}

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions tools/genesis/tests/json_to_genesis_audit_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use libra_types::exports::ChainId;
use libra_types::legacy_types::legacy_address::LegacyAddress;
use support::{path_utils::json_path, test_vals};

#[ignore]
#[test]
// test that a genesis blob created from struct, will actually contain the data
fn test_correct_supply_arithmetic_all() {
Expand All @@ -20,7 +19,7 @@ fn test_correct_supply_arithmetic_all() {
let path = json_path()
.parent()
.unwrap()
.join("v5_recovery_epoch_690.json");
.join("v5_recovery_actual.json"); // the actual file used on 6.9.0 upgrade

let mut user_accounts = recovery_file_parse(path).unwrap();
let map_dd_to_slow = vec![
Expand Down Expand Up @@ -61,8 +60,8 @@ fn test_correct_supply_arithmetic_all() {
let (db_rw, _) = genesis_reader::bootstrap_db_reader_from_gen_tx(&gen_tx).unwrap();

// test dump balances
compare::export_account_balances(&user_accounts, &db_rw.reader, json_path().parent().unwrap())
.unwrap();
// compare::export_account_balances(&user_accounts, &db_rw.reader, json_path().parent().unwrap())
// .unwrap();

// audit
match compare::compare_recovery_vec_to_genesis_tx(
Expand Down
1 change: 1 addition & 0 deletions tools/query/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ repository = { workspace = true }
[dependencies]
anyhow = { workspace = true }
clap = { workspace = true }
diem-debugger = { workspace = true }
diem-sdk = { workspace = true }
indoc = { workspace = true }
libra-types = { workspace = true }
Expand Down
7 changes: 4 additions & 3 deletions tools/query/src/query_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ pub struct QueryCli {

impl QueryCli {
pub async fn run(&self) -> Result<()> {
// let client = Client::default()?;
// TODO: get client from configs

let res = self.subcommand.query_to_json(None).await?;

println!("{}", res);
match res {
serde_json::Value::String(a) => println!("{:#}", a.as_str()),
_ => println!("{:#}", res),
}

Ok(())
}
Expand Down
74 changes: 46 additions & 28 deletions tools/query/src/query_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
query_view::get_view,
};
use anyhow::{bail, Context, Result};
use diem_debugger::DiemDebugger;
use diem_sdk::{rest_client::Client, types::account_address::AccountAddress};
use indoc::indoc;
use libra_types::exports::AuthenticationKey;
Expand Down Expand Up @@ -86,33 +87,36 @@ pub enum QueryType {
},
/// How far behind the local is from the upstream nodes
SyncDelay,
// TODO:
// /// Network block height
// BlockHeight,
// /// Get transaction history
// Txs {
// #[clap(short, long)]
// /// account to query txs of
// account: AccountAddress,
// #[clap(long)]
// /// get transactions after this height
// txs_height: Option<u64>,
// #[clap(long)]
// /// limit how many txs
// txs_count: Option<u64>,
// #[clap(long)]
// /// filter by type
// txs_type: Option<String>,
// },
// /// Get events
// Events {
// /// account to query events
// account: AccountAddress,
// /// switch for sent or received events.
// sent_or_received: bool,
// /// what event sequence number to start querying from, if DB does not have all.
// seq_start: Option<u64>,
// },

Annotate {
account: AccountAddress,
}, // TODO:
// /// Network block height
// BlockHeight,
// /// Get transaction history
// Txs {
// #[clap(short, long)]
// /// account to query txs of
// account: AccountAddress,
// #[clap(long)]
// /// get transactions after this height
// txs_height: Option<u64>,
// #[clap(long)]
// /// limit how many txs
// txs_count: Option<u64>,
// #[clap(long)]
// /// filter by type
// txs_type: Option<String>,
// },
// /// Get events
// Events {
// /// account to query events
// account: AccountAddress,
// /// switch for sent or received events.
// sent_or_received: bool,
// /// what event sequence number to start querying from, if DB does not have all.
// seq_start: Option<u64>,
// },
}

impl QueryType {
Expand Down Expand Up @@ -180,9 +184,23 @@ impl QueryType {
"validator_index": res.validator_index,
}))
}
QueryType::Annotate { account } => {
let dbgger = DiemDebugger::rest_client(client)?;
let version = dbgger.get_latest_version().await?;
let blob = dbgger
.annotate_account_state_at_version(account.to_owned(), version)
.await?;
if blob.is_none() {
bail!("cannot find account state at {}", account)
};
// dbg!(&blob.unwrap());
// blob.unwrap().to_string();
let pretty = format!("{:#}", blob.unwrap().to_string());
Ok(json!(pretty))
}
_ => {
bail!(
"Not implemented for type: {:?}\n Ground control to major tom.",
"Not implemented for type: {:?}\n Ground control to Major Tom.",
self
)
}
Expand Down
32 changes: 31 additions & 1 deletion tools/query/tests/query.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use libra_query::query_type::QueryType;
use libra_smoke_tests::libra_smoke::LibraSmoke;
use libra_types::move_resource::gas_coin::LibraBalanceDisplay;

use libra_types::move_resource::gas_coin::LibraBalanceDisplay;
/// Testing the query library
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn libra_query_test() {
Expand All @@ -24,3 +24,33 @@ async fn libra_query_test() {
}
}
}

/// test account struct annotation
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn account_annotate_test() {
let mut s = LibraSmoke::new(None).await.expect("could not start swarm");
let val_acct = s.first_account.address();

let c = s.client();

let q = QueryType::Annotate { account: val_acct };
let res = q.query_to_json(Some(c)).await.unwrap();
println!("{:#}", &res.as_str().unwrap());
assert!(res.as_str().unwrap().contains("drop"));

// let q = QueryType::Annotate{ account: AccountAddress::ZERO };
// let res = q.query_to_json(Some(c)).await.unwrap();
// println!("{:#}", &res.as_str().unwrap());
// match q.query_to_json(Some(c.to_owned())).await {
// Ok(v) => {
// println!("v: {:?}", v);
// let b: LibraBalanceDisplay = serde_json::from_value(v).unwrap();
// assert!(b.unlocked == 1000.0);
// assert!(b.total == 1000.0);
// }
// Err(e) => {
// println!("e: {:?}", e);
// panic!("nothing returned from query");
// }
// }
}

0 comments on commit 9450d0e

Please sign in to comment.