Skip to content

Commit

Permalink
CU for VoteInstruction::InitializeAccount (#4342)
Browse files Browse the repository at this point in the history
Vote cu
  • Loading branch information
ksolana authored Jan 24, 2025
1 parent d12cfde commit 981c4c1
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

11 changes: 11 additions & 0 deletions programs/vote/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,22 @@ thiserror = { workspace = true }

[dev-dependencies]
assert_matches = { workspace = true }
criterion = { workspace = true }
solana-account = { workspace = true }
solana-clock = { workspace = true }
solana-instruction = { workspace = true }
solana-logger = { workspace = true }
solana-pubkey = { workspace = true, features = ["rand"] }
solana-rent = { workspace = true }
solana-sdk = { workspace = true }
solana-sdk-ids = { workspace = true }
solana-sha256-hasher = { workspace = true }
test-case = { workspace = true }

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

[lib]
crate-type = ["lib"]
name = "solana_vote_program"
Expand Down
189 changes: 189 additions & 0 deletions programs/vote/benches/vote_instructions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
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::{create_account, VoteAuthorize, VoteInit, VoteState},
},
};

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())
}

fn create_test_account() -> (Pubkey, AccountSharedData) {
let rent = Rent::default();
let balance = VoteState::get_rent_exempt_reserve(&rent);
let vote_pubkey = solana_pubkey::new_rand();
(
vote_pubkey,
create_account(&vote_pubkey, &solana_pubkey::new_rand(), 0, balance),
)
}

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| {},
)
}

struct BenchAuthorize {
instruction_data: Vec<u8>,
transaction_accounts: Vec<(Pubkey, AccountSharedData)>,
instruction_accounts: Vec<AccountMeta>,
}

impl BenchAuthorize {
fn new() -> Self {
let (vote_pubkey, vote_account) = create_test_account();
let authorized_voter_pubkey = solana_pubkey::new_rand();
let clock = Clock {
epoch: 1,
leader_schedule_epoch: 2,
..Clock::default()
};
let clock_account = account::create_account_shared_data_for_test(&clock);
let instruction_data = serialize(&VoteInstruction::Authorize(
authorized_voter_pubkey,
VoteAuthorize::Voter,
))
.unwrap();
let transaction_accounts = vec![
(vote_pubkey, vote_account),
(sysvar::clock::id(), clock_account),
(authorized_voter_pubkey, AccountSharedData::default()),
];
let instruction_accounts = vec![
AccountMeta {
pubkey: vote_pubkey,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: sysvar::clock::id(),
is_signer: false,
is_writable: false,
},
];
Self {
instruction_data,
transaction_accounts,
instruction_accounts,
}
}

fn run(&self) {
let _accounts = process_instruction(
&self.instruction_data,
self.transaction_accounts.clone(),
self.instruction_accounts.clone(),
Ok(()),
);
}
}

struct BenchInitializeAccount {
instruction_data: Vec<u8>,
transaction_accounts: Vec<(Pubkey, AccountSharedData)>,
instruction_accounts: Vec<AccountMeta>,
}

impl BenchInitializeAccount {
fn new() -> 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,
},
];
let transaction_accounts = vec![
(vote_pubkey, vote_account),
(sysvar::rent::id(), create_default_rent_account()),
(sysvar::clock::id(), create_default_clock_account()),
(node_pubkey, node_account),
];
Self {
instruction_data,
transaction_accounts,
instruction_accounts,
}
}
pub fn run(&self) {
let _accounts = process_instruction(
&self.instruction_data,
self.transaction_accounts.clone(),
self.instruction_accounts.clone(),
Ok(()),
);
}
}

fn bench_initialize_account(c: &mut Criterion) {
let test_setup = BenchInitializeAccount::new();
c.bench_function("vote_instruction_initialize_account", |bencher| {
bencher.iter(|| test_setup.run())
});
}

fn bench_authorize(c: &mut Criterion) {
let test_setup = BenchAuthorize::new();
c.bench_function("vote_instruction_authorize", |bencher| {
bencher.iter(|| test_setup.run())
});
}

criterion_group!(benches, bench_initialize_account, bench_authorize);
criterion_main!(benches);

0 comments on commit 981c4c1

Please sign in to comment.