Skip to content

Commit

Permalink
rust: Bump Solana crates to v2.1
Browse files Browse the repository at this point in the history
#### Problem

Version 2.1 of the Solana crates is out, but all of the Rust programs
are still on v2.0.

#### Summary of changes

Bump crates to v2.1, Rust to 1.81, and use component crates wherever
possible. The CPI program has a higher CU usage also, so update the
README to reflect that.
  • Loading branch information
joncinque committed Nov 6, 2024
1 parent 7d00568 commit c37e4b5
Show file tree
Hide file tree
Showing 13 changed files with 1,365 additions and 775 deletions.
2,009 changes: 1,279 additions & 730 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ members = [
]
resolver = "2"

[workspace.lints.rust.unexpected_cfgs]
level = "warn"
check-cfg = [
'cfg(target_os, values("solana"))',
'cfg(feature, values("frozen-abi", "no-entrypoint"))',
]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ the address and `invoke_signed` to CPI to the system program.

| Language | CU Usage | CU Usage (minus syscalls) |
| --- | --- | --- |
| Rust | 3662 | 1162 |
| Rust | 3698 | 1198 |
| Zig | 2825 | 325 |
| C | 3122 | 622 |
| Rust (pinocchio) | 2816 | 316 |
Expand Down
14 changes: 11 additions & 3 deletions cpi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ edition = "2021"
test-sbf = []

[dependencies]
solana-program = "2.0.3"
solana-account-info = "2.1.0"
solana-cpi = "2.1.0"
solana-program = "2.1.0"
solana-program-entrypoint = "2.1.0"
solana-program-error = "2.1.0"
solana-pubkey = "2.1.0"

[dev-dependencies]
solana-program-test = "2.0.3"
solana-sdk = "2.0.3"
solana-program-test = "2.1.0"
solana-sdk = "2.1.0"

[lib]
crate-type = ["cdylib", "lib"]

[lints]
workspace = true
16 changes: 8 additions & 8 deletions cpi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
#![deny(missing_docs)]
#![forbid(unsafe_code)]

use solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint::ProgramResult,
program::invoke_signed,
program_error::ProgramError,
pubkey::Pubkey,
system_instruction,
use {
solana_account_info::{next_account_info, AccountInfo},
solana_cpi::invoke_signed,
solana_program::system_instruction, // replace with solana_system_interface
solana_program_entrypoint::ProgramResult,
solana_program_error::ProgramError,
solana_pubkey::Pubkey,
};

solana_program::entrypoint!(process_instruction);
solana_program_entrypoint::entrypoint!(process_instruction);

/// Amount of bytes of account data to allocate
pub const SIZE: usize = 42;
Expand Down
2 changes: 1 addition & 1 deletion cpi/tests/functional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async fn test_cross_program_invocation() {
},
);

let (mut banks_client, payer, recent_blockhash) = program_test.start().await;
let (banks_client, payer, recent_blockhash) = program_test.start().await;

let mut transaction = Transaction::new_with_payer(
&[Instruction::new_with_bincode(
Expand Down
12 changes: 9 additions & 3 deletions helloworld/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ version = "0.1.0"
edition = "2021"

[dependencies]
solana-program = "2"
solana-msg = "2.1"
solana-program-entrypoint = "2.1"

[dev-dependencies]
solana-program-test = "2"
solana-sdk = "2"
solana-instruction = "2.1"
solana-program-test = "2.1"
solana-pubkey = "2.1"
solana-sdk = "2.1"

[lib]
crate-type = ["cdylib", "lib"]

[lints]
workspace = true
6 changes: 3 additions & 3 deletions helloworld/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use solana_program::msg;
use solana_msg::msg;

#[no_mangle]
pub extern "C" fn entrypoint(_: *mut u8) -> u64 {
Expand All @@ -7,5 +7,5 @@ pub extern "C" fn entrypoint(_: *mut u8) -> u64 {
}
#[cfg(target_os = "solana")]
#[no_mangle]
fn custom_panic(info: &core::panic::PanicInfo<'_>) {}
solana_program::custom_heap_default!();
fn custom_panic(_info: &core::panic::PanicInfo<'_>) {}
solana_program_entrypoint::custom_heap_default!();
6 changes: 3 additions & 3 deletions helloworld/tests/functional.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use {
solana_program::instruction::Instruction,
solana_instruction::Instruction,
solana_program_test::{tokio, ProgramTest},
solana_sdk::{signature::Signer, transaction::Transaction},
};
mod program {
solana_program::declare_id!("1og1111111111111111111111111111111111111111");
solana_pubkey::declare_id!("1og1111111111111111111111111111111111111111");
}
fn program_test() -> ProgramTest {
ProgramTest::new("solana_program_rosetta_helloworld", program::id(), None)
}
#[tokio::test]
async fn call() {
let pt = program_test();
let mut context = pt.start_with_context().await;
let context = pt.start_with_context().await;
let blockhash = context.banks_client.get_latest_blockhash().await.unwrap();
let transaction = Transaction::new_signed_with_payer(
&[Instruction {
Expand Down
14 changes: 10 additions & 4 deletions transfer-lamports/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ version = "1.0.0"
edition = "2021"

[features]
no-entrypoint = []
test-sbf = []

[dependencies]
solana-program = "2.0.3"
solana-account-info = "2.1.0"
solana-program-entrypoint = "2.1.0"
solana-program-error = "2.1.0"
solana-pubkey = "2.1.0"

[dev-dependencies]
solana-program-test = "2.0.3"
solana-sdk = "2.0.3"
solana-instruction = "2.1.0"
solana-program-test = "2.1.0"
solana-sdk = "2.1.0"

[lib]
crate-type = ["cdylib", "lib"]

[lints]
workspace = true
12 changes: 0 additions & 12 deletions transfer-lamports/src/entrypoint.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,2 @@
//! Program entrypoint
#![cfg(not(feature = "no-entrypoint"))]

use solana_program::{account_info::AccountInfo, entrypoint::ProgramResult, pubkey::Pubkey};

solana_program::entrypoint!(process_instruction);
fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
crate::processor::process_instruction(program_id, accounts, instruction_data)
}
33 changes: 31 additions & 2 deletions transfer-lamports/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
//! A program demonstrating the transfer of lamports
#![deny(missing_docs)]
#![forbid(unsafe_code)]
#![allow(clippy::arithmetic_side_effects)]

mod entrypoint;
pub mod processor;
use {
solana_account_info::{next_account_info, AccountInfo},
solana_program_error::ProgramResult,
solana_pubkey::Pubkey,
};

solana_program_entrypoint::entrypoint!(process_instruction);

/// Instruction processor
pub fn process_instruction(
_program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
// Create an iterator to safely reference accounts in the slice
let account_info_iter = &mut accounts.iter();
let transfer_amount = u64::from_le_bytes(instruction_data.try_into().unwrap());

// As part of the program specification the first account is the source
// account and the second is the destination account
let source_info = next_account_info(account_info_iter)?;
let destination_info = next_account_info(account_info_iter)?;

// Withdraw five lamports from the source
**source_info.try_borrow_mut_lamports()? -= transfer_amount;
// Deposit five lamports into the destination
**destination_info.try_borrow_mut_lamports()? += transfer_amount;

Ok(())
}
8 changes: 3 additions & 5 deletions transfer-lamports/tests/functional.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use {
solana_program::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
},
solana_instruction::{AccountMeta, Instruction},
solana_program_test::*,
solana_pubkey::Pubkey,
solana_sdk::{account::Account, signature::Signer, transaction::Transaction},
std::str::FromStr,
};
Expand Down Expand Up @@ -38,7 +36,7 @@ async fn test_lamport_transfer() {
..Account::default()
},
);
let (mut banks_client, payer, recent_blockhash) = program_test.start().await;
let (banks_client, payer, recent_blockhash) = program_test.start().await;

let mut transaction = Transaction::new_with_payer(
&[Instruction::new_with_bincode(
Expand Down

0 comments on commit c37e4b5

Please sign in to comment.