Skip to content

Commit

Permalink
Vote cu
Browse files Browse the repository at this point in the history
  • Loading branch information
ksolana committed Jan 18, 2025
1 parent 91d0d0c commit 6da5204
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

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

10 changes: 10 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,23 @@ tiny-bip39 = { workspace = true }

[dev-dependencies]
assert_matches = { workspace = true }
criterion = { workspace = true }
solana-account = { workspace = true }
solana-clock = { workspace = true }
solana-faucet = { workspace = true }
solana-instruction = { workspace = true }
solana-rent = { workspace = true }
solana-rpc = { workspace = true }
solana-sdk-ids = { workspace = true }
solana-streamer = { workspace = true }
solana-test-validator = { workspace = true }
tempfile = { workspace = true }
test-case = { workspace = true }

[[bench]]
name = "vote"
harness = false

[[bin]]
name = "solana"
path = "src/main.rs"
Expand Down
108 changes: 108 additions & 0 deletions cli/benches/vote.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use {
bincode::serialize,
criterion::{criterion_group, criterion_main, Criterion},
solana_account::{self as account, AccountSharedData},
solana_clock::Clock,
solana_instruction::{error::InstructionError, AccountMeta},
solana_program_runtime::invoke_context::mock_process_instruction,
solana_pubkey::Pubkey,
solana_rent::Rent,
solana_sdk::sysvar,
solana_sdk_ids::vote::id,
solana_vote_program::{vote_instruction::VoteInstruction, vote_processor::Entrypoint, vote_state::{VoteState, VoteInit}},
};

fn create_default_rent_account() -> AccountSharedData {
account::create_account_shared_data_for_test(&Rent::free())
}

fn create_default_clock_account() -> AccountSharedData {
account::create_account_shared_data_for_test(&Clock::default())
}

struct TestSetup {
}

impl TestSetup {
fn new(p: u64) -> TestSetup {
Self {
}
}

fn process_instruction(
instruction_data: &[u8],
transaction_accounts: Vec<(Pubkey, AccountSharedData)>,
instruction_accounts: Vec<AccountMeta>,
expected_result: Result<(), InstructionError>,
) -> Vec<AccountSharedData> {
mock_process_instruction(
&id(),
Vec::new(),
instruction_data,
transaction_accounts,
instruction_accounts,
expected_result,
Entrypoint::vm,
|_invoke_context| {},
|_invoke_context| {},
)
}
pub fn run(&self) {
let vote_pubkey = solana_pubkey::new_rand();
let vote_account = AccountSharedData::new(100, VoteState::size_of(), &id());
let node_pubkey = solana_pubkey::new_rand();
let node_account = AccountSharedData::default();
let instruction_data = serialize(&VoteInstruction::InitializeAccount(VoteInit {
node_pubkey,
authorized_voter: vote_pubkey,
authorized_withdrawer: vote_pubkey,
commission: 0,
}))
.unwrap();
let instruction_accounts = vec![
AccountMeta {
pubkey: vote_pubkey,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: sysvar::rent::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: sysvar::clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: node_pubkey,
is_signer: true,
is_writable: false,
},
];

// init should pass
let _accounts = Self::process_instruction(
&instruction_data,
vec![
(vote_pubkey, vote_account.clone()),
(sysvar::rent::id(), create_default_rent_account()),
(sysvar::clock::id(), create_default_clock_account()),
(node_pubkey, node_account.clone()),
],
instruction_accounts.clone(),
Ok(()),
);
}
}

fn bench_create_vote_account(c: &mut Criterion) {
let test_setup = TestSetup::new();
c.bench_function("create_vote_account", |bencher| {
bencher.iter(|| test_setup.run())
});
}

criterion_group!(benches, bench_create_vote_account,);
criterion_main!(benches);

0 comments on commit 6da5204

Please sign in to comment.