Skip to content

Commit

Permalink
move bpf_loader helpers to tests (#69)
Browse files Browse the repository at this point in the history
* move bpf_loader helpers to tests

* lint

* update changelog
  • Loading branch information
kevinheavey authored Apr 22, 2024
1 parent 3ce9d1a commit 30d67c5
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 152 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Change `set_account` to return `Result<(), LiteSVMError>` ([#62](https://github.com/LiteSVM/litesvm/pull/62)).
- Replace `&mut self` with `&self` in `simulate_transaction`. ([#64](https://github.com/LiteSVM/litesvm/pull/64)).
- Remove `set_compute_budget` as it duplicates `with_compute_budget`. ([#68](https://github.com/LiteSVM/litesvm/pull/68)).
- Remove `set_upgrade_authority` and `deploy_upgradeable_program` ([#69](https://github.com/LiteSVM/litesvm/pull/69)).

## [0.1.0] - 2024-04-02

Expand Down
41 changes: 1 addition & 40 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ use solana_sdk::{
rent::Rent,
signature::{Keypair, Signature},
signer::Signer,
signers::Signers,
slot_hashes::SlotHashes,
slot_history::SlotHistory,
stake_history::StakeHistory,
Expand All @@ -55,11 +54,7 @@ use crate::{
history::TransactionHistory,
spl::load_spl_programs,
types::{ExecutionResult, FailedTransactionMetadata, TransactionMetadata, TransactionResult},
utils::{
create_blockhash,
loader::{deploy_upgradeable_program, set_upgrade_authority},
rent::RentState,
},
utils::{create_blockhash, rent::RentState},
};

pub mod error;
Expand Down Expand Up @@ -340,31 +335,6 @@ impl LiteSVM {
.replenish(program_id, Arc::new(loaded_program));
}

pub fn deploy_upgradeable_program(
&mut self,
payer_kp: &Keypair,
program_kp: &Keypair,
program_bytes: &[u8],
) -> Result<(), FailedTransactionMetadata> {
deploy_upgradeable_program(self, payer_kp, program_kp, program_bytes)
}

pub fn set_upgrade_authority(
&mut self,
from_keypair: &Keypair,
program_pubkey: &Pubkey,
current_authority_keypair: &Keypair,
new_authority_pubkey: Option<&Pubkey>,
) -> Result<(), FailedTransactionMetadata> {
set_upgrade_authority(
self,
from_keypair,
program_pubkey,
current_authority_keypair,
new_authority_pubkey,
)
}

fn create_transaction_context(
&self,
compute_budget: ComputeBudget,
Expand Down Expand Up @@ -772,15 +742,6 @@ impl LiteSVM {
})
}

pub(crate) fn send_message<T: Signers>(
&mut self,
message: Message,
signers: &T,
) -> TransactionResult {
let tx = VersionedTransaction::try_new(VersionedMessage::Legacy(message), signers).unwrap();
self.send_transaction(tx)
}

pub fn send_transaction(&mut self, tx: impl Into<VersionedTransaction>) -> TransactionResult {
let vtx: VersionedTransaction = tx.into();
let ExecutionResult {
Expand Down
107 changes: 0 additions & 107 deletions src/utils/loader.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use solana_sdk::{
sysvar::{self, instructions::construct_instructions_data},
};

pub mod loader;
pub mod rent;

/// Create a blockhash from the given bytes
Expand Down
132 changes: 128 additions & 4 deletions tests/loaders.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,130 @@
use litesvm::LiteSVM;
#![allow(clippy::result_large_err)]
use litesvm::{types::FailedTransactionMetadata, LiteSVM};
use solana_program::{
instruction::{AccountMeta, Instruction},
message::Message,
};
use solana_sdk::{signature::Keypair, signer::Signer, transaction::Transaction};
use solana_sdk::{
bpf_loader_upgradeable::{self, UpgradeableLoaderState},
message::VersionedMessage,
pubkey::Pubkey,
signature::Keypair,
signer::Signer,
transaction::{Transaction, VersionedTransaction},
};

use crate::programs_bytes::HELLO_WORLD_BYTES;

mod programs_bytes;

const CHUNK_SIZE: usize = 512;

fn set_upgrade_authority(
svm: &mut LiteSVM,
from_keypair: &Keypair,
program_pubkey: &Pubkey,
current_authority_keypair: &Keypair,
new_authority_pubkey: Option<&Pubkey>,
) -> Result<(), FailedTransactionMetadata> {
let message = Message::new_with_blockhash(
&[bpf_loader_upgradeable::set_upgrade_authority(
program_pubkey,
&current_authority_keypair.pubkey(),
new_authority_pubkey,
)],
Some(&from_keypair.pubkey()),
&svm.latest_blockhash(),
);
let tx =
VersionedTransaction::try_new(VersionedMessage::Legacy(message), &[from_keypair]).unwrap();
svm.send_transaction(tx)?;

Ok(())
}

fn load_upgradeable_buffer(
svm: &mut LiteSVM,
payer_kp: &Keypair,
program_bytes: &[u8],
) -> Result<Pubkey, FailedTransactionMetadata> {
let payer_pk = payer_kp.pubkey();
let buffer_kp = Keypair::new();
let buffer_pk = buffer_kp.pubkey();
// loader
let buffer_len = UpgradeableLoaderState::size_of_buffer(program_bytes.len());
let lamports = svm.minimum_balance_for_rent_exemption(buffer_len);

let message = Message::new_with_blockhash(
&bpf_loader_upgradeable::create_buffer(
&payer_pk,
&buffer_pk,
&payer_pk,
lamports,
program_bytes.len(),
)
.unwrap(),
Some(&payer_pk),
&svm.latest_blockhash(),
);
let tx =
VersionedTransaction::try_new(VersionedMessage::Legacy(message), &[payer_kp, &buffer_kp])
.unwrap();
svm.send_transaction(tx)?;

let chunk_size = CHUNK_SIZE;
let mut offset = 0;
for chunk in program_bytes.chunks(chunk_size) {
let message = Message::new_with_blockhash(
&[bpf_loader_upgradeable::write(
&buffer_pk,
&payer_pk,
offset,
chunk.to_vec(),
)],
Some(&payer_pk),
&svm.latest_blockhash(),
);
let tx =
VersionedTransaction::try_new(VersionedMessage::Legacy(message), &[payer_kp]).unwrap();
svm.send_transaction(tx)?;
offset += chunk_size as u32;
}

Ok(buffer_pk)
}

fn deploy_upgradeable_program(
svm: &mut LiteSVM,
payer_kp: &Keypair,
program_kp: &Keypair,
program_bytes: &[u8],
) -> Result<(), FailedTransactionMetadata> {
let program_pk = program_kp.pubkey();
let payer_pk = payer_kp.pubkey();
let buffer_pk = load_upgradeable_buffer(svm, payer_kp, program_bytes)?;

let lamports = svm.minimum_balance_for_rent_exemption(program_bytes.len());
let message = Message::new_with_blockhash(
&bpf_loader_upgradeable::deploy_with_max_program_len(
&payer_pk,
&program_pk,
&buffer_pk,
&payer_pk,
lamports,
program_bytes.len() * 2,
)
.unwrap(),
Some(&payer_pk),
&svm.latest_blockhash(),
);
let tx =
VersionedTransaction::try_new(VersionedMessage::Legacy(message), &[payer_kp, &program_kp])
.unwrap();
svm.send_transaction(tx)?;

Ok(())
}

#[test]
fn hello_world_with_store() {
let mut svm = LiteSVM::new();
Expand Down Expand Up @@ -49,8 +165,7 @@ fn hello_world_with_deploy_upgradeable() {
svm.airdrop(&payer_pk, 10000000000).unwrap();

let program_keypair = Keypair::new();
svm.deploy_upgradeable_program(&payer_kp, &program_keypair, program_bytes)
.unwrap();
deploy_upgradeable_program(&mut svm, &payer_kp, &program_keypair, program_bytes).unwrap();
let program_id = program_keypair.pubkey();
let instruction =
Instruction::new_with_bytes(program_id, &[], vec![AccountMeta::new(payer_pk, true)]);
Expand All @@ -61,4 +176,13 @@ fn hello_world_with_deploy_upgradeable() {
.unwrap()
.logs
.contains(&"Program log: Hello world!".to_string()));
let new_authority = Keypair::new();
set_upgrade_authority(
&mut svm,
&payer_kp,
&program_id,
&payer_kp,
Some(&new_authority.pubkey()),
)
.unwrap();
}

0 comments on commit 30d67c5

Please sign in to comment.