Skip to content

Commit

Permalink
♻️ Bpf_loader changed to bpf_upgradable_loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Ikrk committed Feb 22, 2024
1 parent b7a44a5 commit 03019b9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 28 deletions.
80 changes: 53 additions & 27 deletions crates/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ use anchor_client::{
solana_client::rpc_config::RpcTransactionConfig,
solana_sdk::{
account::Account,
bpf_loader,
bpf_loader_upgradeable,
commitment_config::CommitmentConfig,
instruction::Instruction,
loader_instruction,
pubkey::Pubkey,
signature::read_keypair_file,
signer::{keypair::Keypair, Signer},
Expand All @@ -20,6 +19,7 @@ use anchor_client::{
Client as AnchorClient, ClientError as Error, Cluster, Program,
};

use anchor_lang::prelude::UpgradeableLoaderState;
use borsh::BorshDeserialize;
use fehler::{throw, throws};
use futures::stream::{self, StreamExt};
Expand All @@ -46,7 +46,6 @@ pub struct Client {
payer: Keypair,
anchor_client: AnchorClient<Payer>,
}

/// Implement Default trait for Client, which reads keypair from default path for `solana-keygen new`
impl Default for Client {
fn default() -> Self {
Expand Down Expand Up @@ -406,7 +405,7 @@ impl Client {

// TODO: This will fail on devnet where airdrops are limited to 1 SOL

self.airdrop(self.payer().pubkey(), 5_000_000_000)
self.airdrop(self.payer().pubkey(), 5_000_000_000_000)
.await
.expect("airdropping for deployment failed");

Expand Down Expand Up @@ -442,8 +441,10 @@ impl Client {
#[throws]
async fn deploy(&self, program_keypair: Keypair, program_data: Vec<u8>) {
const PROGRAM_DATA_CHUNK_SIZE: usize = 900;
let buffer_account = Keypair::new();

let program_data_len = program_data.len();
let size_of_buffer = UpgradeableLoaderState::size_of_buffer(program_data_len);

let rpc_client = self
.anchor_client
Expand All @@ -462,17 +463,30 @@ impl Client {
.await
.unwrap();

let create_account_ix: Instruction = system_instruction::create_account(
let min_balance_for_rent_exemption_buffer = rpc_client
.get_minimum_balance_for_rent_exemption(size_of_buffer)
.await
.unwrap();

let create_account_ixs = bpf_loader_upgradeable::create_buffer(
&self.payer.pubkey(),
&program_keypair.pubkey(),
min_balance_for_rent_exemption,
program_data_len as u64,
&bpf_loader::id(),
);
system_program
.request()
.instruction(create_account_ix)
.signer(&program_keypair)
&buffer_account.pubkey(),
&self.payer.pubkey(),
min_balance_for_rent_exemption_buffer,
program_data_len,
)
.unwrap();

debug!("number of ixs = {}", create_account_ixs.len());

let mut ix_builder = system_program.request();
for ix in create_account_ixs {
ix_builder = ix_builder.instruction(ix);
}

ix_builder
.signer(&buffer_account)
.signer(&self.payer)
.send()
.await
.unwrap();
Expand All @@ -483,9 +497,9 @@ impl Client {
let mut futures_vec = Vec::new();

for chunk in program_data.chunks(PROGRAM_DATA_CHUNK_SIZE) {
let loader_write_ix = loader_instruction::write(
&program_keypair.pubkey(),
&bpf_loader::id(),
let loader_write_ix = bpf_loader_upgradeable::write(
&buffer_account.pubkey(),
&self.payer.pubkey(),
offset as u32,
chunk.to_vec(),
);
Expand All @@ -494,7 +508,7 @@ impl Client {
system_program
.request()
.instruction(loader_write_ix)
.signer(&program_keypair)
.signer(&self.payer)
.send()
.await
.unwrap();
Expand All @@ -506,13 +520,25 @@ impl Client {
.collect::<Vec<_>>()
.await;

debug!("finalize program");
debug!("deploy program");

let deploy_ixs = bpf_loader_upgradeable::deploy_with_max_program_len(
&self.payer.pubkey(),
&program_keypair.pubkey(),
&buffer_account.pubkey(),
&self.payer.pubkey(),
min_balance_for_rent_exemption,
program_data_len,
)
.unwrap();

let mut ix_builder = system_program.request();
for ix in deploy_ixs {
ix_builder = ix_builder.instruction(ix);
}

let loader_finalize_ix =
loader_instruction::finalize(&program_keypair.pubkey(), &bpf_loader::id());
system_program
.request()
.instruction(loader_finalize_ix)
ix_builder
.signer(&self.payer)
.signer(&program_keypair)
.send()
.await
Expand Down Expand Up @@ -691,7 +717,7 @@ impl Client {
.get_minimum_balance_for_rent_exemption(data.len())
.await?,
data.len() as u64,
&bpf_loader::id(),
&bpf_loader_upgradeable::id(),
)],
[account],
)
Expand All @@ -701,9 +727,9 @@ impl Client {
for chunk in data.chunks(DATA_CHUNK_SIZE) {
debug!("writing bytes {} to {}", offset, offset + chunk.len());
self.send_transaction(
&[loader_instruction::write(
&[bpf_loader_upgradeable::write(
&account.pubkey(),
&bpf_loader::id(),
&bpf_loader_upgradeable::id(),
offset as u32,
chunk.to_vec(),
)],
Expand Down
2 changes: 1 addition & 1 deletion examples/escrow/trdelnik-tests/poc_tests/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl Fixture {
#[throws]
async fn deploy(&mut self) {
self.client
.airdrop(self.alice_wallet.pubkey(), 5_000_000_000)
.airdrop(self.alice_wallet.pubkey(), 5_000_000_000_000)
.await?;
self.client
.deploy_by_name(&self.program.clone(), "escrow")
Expand Down

0 comments on commit 03019b9

Please sign in to comment.