Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
ledger-tool: Add print-accounts command
Browse files Browse the repository at this point in the history
(cherry picked from commit 1bf2285)
  • Loading branch information
mvines committed Feb 15, 2020
1 parent 1dbcd5c commit 370716e
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 10 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.

2 changes: 1 addition & 1 deletion core/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl JsonRpcRequestProcessor {
) -> Result<Vec<RpcKeyedAccount>> {
Ok(self
.bank(commitment)
.get_program_accounts(&program_id)
.get_program_accounts(Some(&program_id))
.into_iter()
.map(|(pubkey, account)| RpcKeyedAccount {
pubkey: pubkey.to_string(),
Expand Down
1 change: 1 addition & 0 deletions ledger-tool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ homepage = "https://solana.com/"

[dependencies]
bincode = "1.2.1"
bs58 = "0.3.0"
clap = "2.33.0"
histogram = "*"
serde = "1.0.104"
Expand Down
52 changes: 52 additions & 0 deletions ledger-tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,13 @@ fn main() {
.takes_value(true)
.help("Output directory for the snapshot"),
)
).subcommand(
SubCommand::with_name("print-accounts")
.about("Print account contents after processing in the ledger")
.arg(&no_snapshot_arg)
.arg(&account_paths_arg)
.arg(&halt_at_slot_arg)
.arg(&hard_forks_arg)
).subcommand(
SubCommand::with_name("prune")
.about("Prune the ledger at the block height")
Expand Down Expand Up @@ -933,6 +940,51 @@ fn main() {
}
}
}
("print-accounts", Some(arg_matches)) => {
let dev_halt_at_slot = value_t!(arg_matches, "halt_at_slot", Slot).ok();
let process_options = ProcessOptions {
dev_halt_at_slot,
new_hard_forks: hardforks_of(arg_matches, "hard_forks"),
poh_verify: false,
..ProcessOptions::default()
};
let genesis_config = open_genesis_config(&ledger_path);
match load_bank_forks(arg_matches, &ledger_path, &genesis_config, process_options) {
Ok((bank_forks, bank_forks_info, _leader_schedule_cache)) => {
let slot = dev_halt_at_slot.unwrap_or_else(|| {
if bank_forks_info.len() > 1 {
eprintln!("Error: multiple forks present");
exit(1);
}
bank_forks_info[0].bank_slot
});

let bank = bank_forks.get(slot).unwrap_or_else(|| {
eprintln!("Error: Slot {} is not available", slot);
exit(1);
});

let accounts: Vec<_> = bank
.get_program_accounts(None)
.into_iter()
.filter(|(pubkey, _account)| !solana_sdk::sysvar::is_sysvar_id(pubkey))
.collect();

println!("---");
for (pubkey, account) in accounts.into_iter() {
println!("{}:", pubkey);
println!(" - lamports: {}", account.lamports);
println!(" - owner: '{}'", account.owner);
println!(" - executable: {}", account.executable);
println!(" - data: '{}'", bs58::encode(account.data).into_string());
}
}
Err(err) => {
eprintln!("Failed to load ledger: {:?}", err);
exit(1);
}
}
}
("prune", Some(arg_matches)) => {
if let Some(prune_file_path) = arg_matches.value_of("slot_list") {
let blockstore = open_blockstore(&ledger_path);
Expand Down
7 changes: 5 additions & 2 deletions runtime/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,16 @@ impl Accounts {
pub fn load_by_program(
&self,
ancestors: &HashMap<Slot, usize>,
program_id: &Pubkey,
program_id: Option<&Pubkey>,
) -> Vec<(Pubkey, Account)> {
self.accounts_db.scan_accounts(
ancestors,
|collector: &mut Vec<(Pubkey, Account)>, option| {
if let Some(data) = option
.filter(|(_, account, _)| account.owner == *program_id && account.lamports != 0)
.filter(|(_, account, _)| {
(program_id.is_none() || Some(&account.owner) == program_id)
&& account.lamports != 0
})
.map(|(pubkey, account, _slot)| (*pubkey, account))
{
collector.push(data)
Expand Down
27 changes: 20 additions & 7 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,7 @@ impl Bank {
.map(|(acc, _slot)| acc)
}

pub fn get_program_accounts(&self, program_id: &Pubkey) -> Vec<(Pubkey, Account)> {
pub fn get_program_accounts(&self, program_id: Option<&Pubkey>) -> Vec<(Pubkey, Account)> {
self.rc
.accounts
.load_by_program(&self.ancestors, program_id)
Expand Down Expand Up @@ -4702,11 +4702,24 @@ mod tests {

#[test]
fn test_bank_get_program_accounts() {
let (genesis_config, _mint_keypair) = create_genesis_config(500);
let (genesis_config, mint_keypair) = create_genesis_config(500);
let parent = Arc::new(Bank::new(&genesis_config));

let bank0 = Arc::new(new_from_parent(&parent));
let genesis_accounts: Vec<_> = parent.get_program_accounts(None);
assert!(
genesis_accounts
.iter()
.any(|(pubkey, _)| *pubkey == mint_keypair.pubkey()),
"mint pubkey not found"
);
assert!(
genesis_accounts
.iter()
.any(|(pubkey, _)| solana_sdk::sysvar::is_sysvar_id(pubkey)),
"no sysvars found"
);

let bank0 = Arc::new(new_from_parent(&parent));
let pubkey0 = Pubkey::new_rand();
let program_id = Pubkey::new(&[2; 32]);
let account0 = Account::new(1, 0, &program_id);
Expand All @@ -4720,11 +4733,11 @@ mod tests {
let bank1 = Arc::new(new_from_parent(&bank0));
bank1.squash();
assert_eq!(
bank0.get_program_accounts(&program_id),
bank0.get_program_accounts(Some(&program_id)),
vec![(pubkey0, account0.clone())]
);
assert_eq!(
bank1.get_program_accounts(&program_id),
bank1.get_program_accounts(Some(&program_id)),
vec![(pubkey0, account0.clone())]
);
assert_eq!(
Expand All @@ -4743,8 +4756,8 @@ mod tests {

let bank3 = Arc::new(new_from_parent(&bank2));
bank3.squash();
assert_eq!(bank1.get_program_accounts(&program_id).len(), 2);
assert_eq!(bank3.get_program_accounts(&program_id).len(), 2);
assert_eq!(bank1.get_program_accounts(Some(&program_id)).len(), 2);
assert_eq!(bank3.get_program_accounts(Some(&program_id)).len(), 2);
}

#[test]
Expand Down

0 comments on commit 370716e

Please sign in to comment.