Skip to content

Commit 0983082

Browse files
committed
Pass bumps
1 parent dc704b3 commit 0983082

15 files changed

Lines changed: 869 additions & 170 deletions

Cargo.lock

Lines changed: 100 additions & 60 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ logging = []
4848

4949
[dependencies]
5050
borsh = { version = "1.5.3", features = [ "derive" ] }
51+
const-crypto = "0.3.0"
52+
solana-sha256-hasher = "3.1.0"
5153
solana-program = { version = ">=1.16, <3.0.0" }
5254
bytemuck = { version = ">=1", features = [ "derive" ] }
5355
num_enum = "^0.7.2"

src/args/args_with_buffer.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use std::ops::Deref;
2+
3+
use bytemuck::Pod;
4+
use pinocchio::program_error::ProgramError;
5+
6+
pub struct ArgsWithBuffer<'a, H> {
7+
header: &'a H,
8+
pub buffer: &'a [u8],
9+
}
10+
11+
impl<'a, H: Pod> ArgsWithBuffer<'a, H> {
12+
pub fn from_bytes(input: &'a [u8]) -> Result<Self, ProgramError> {
13+
let header_size = size_of::<H>();
14+
15+
if input.len() < header_size {
16+
return Err(ProgramError::InvalidInstructionData);
17+
}
18+
19+
let (header_bytes, buffer) = input.split_at(header_size);
20+
let header = bytemuck::from_bytes::<H>(header_bytes);
21+
22+
Ok(Self { header, buffer })
23+
}
24+
}
25+
26+
impl<'a, H> Deref for ArgsWithBuffer<'a, H> {
27+
type Target = H;
28+
fn deref(&self) -> &Self::Target {
29+
self.header
30+
}
31+
}

src/args/commit_state.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
1+
use bytemuck::{Pod, Zeroable};
12
use std::mem::size_of;
23

34
use borsh::{BorshDeserialize, BorshSerialize};
45

5-
#[derive(Default, Debug, BorshSerialize, BorshDeserialize)]
6+
use crate::args::ArgsWithBuffer;
7+
8+
#[repr(C)]
9+
#[derive(Copy, Clone, Pod, Zeroable)]
610
pub struct CommitFinalizeArgs {
7-
/// "Nonce" of an account. Updates are submitted historically and nonce incremented by 1
8-
/// Deprecated: The ephemeral slot at which the account data is committed
9-
pub nonce: u64,
11+
pub commit_id: u64,
12+
1013
/// The lamports that the account holds in the ephemeral validator
1114
pub lamports: u64,
15+
1216
/// Whether the account can be undelegated after the commit completes
1317
pub allow_undelegation: u8,
18+
1419
/// Whether the account can be undelegated after the commit completes
1520
pub data_is_diff: u8,
16-
/// The account data
17-
pub data: Vec<u8>,
21+
22+
/// bumps of the PDA accounts to be validated by the ix
23+
pub delegation_record_bump: u8,
24+
pub delegation_metadata_bump: u8,
25+
pub validator_fees_vault_bump: u8,
26+
pub program_config_bump: u8,
27+
28+
pub reserved_padding: [u8; 2],
1829
}
1930

31+
pub type CommitFinalizeArgsWithBuffer<'a> = ArgsWithBuffer<'a, CommitFinalizeArgs>;
32+
2033
#[derive(Default, Debug, BorshSerialize, BorshDeserialize)]
2134
pub struct CommitStateArgs {
2235
/// "Nonce" of an account. Updates are submitted historically and nonce incremented by 1

src/args/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod args_with_buffer;
12
mod call_handler;
23
mod commit_state;
34
mod delegate;
@@ -6,6 +7,7 @@ mod top_up_ephemeral_balance;
67
mod validator_claim_fees;
78
mod whitelist_validator_for_program;
89

10+
pub use args_with_buffer::*;
911
pub use call_handler::*;
1012
pub use commit_state::*;
1113
pub use delegate::*;

src/entrypoint.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use crate::{fast_process_instruction, slow_process_instruction};
1+
use crate::{error::DlpError, fast_process_instruction, slow_process_instruction};
22

3+
use pinocchio::program_error::ToStr;
34
use solana_program::entrypoint;
45

56
entrypoint::custom_heap_default!();
@@ -17,13 +18,17 @@ pub unsafe extern "C" fn entrypoint(input: *mut u8) -> u64 {
1718

1819
let (program_id, count, data) =
1920
pinocchio::entrypoint::deserialize::<{ pinocchio::MAX_TX_ACCOUNTS }>(input, &mut accounts);
21+
2022
match fast_process_instruction(
2123
program_id,
2224
core::slice::from_raw_parts(accounts.as_ptr() as _, count),
2325
data,
2426
) {
2527
Some(Ok(())) => pinocchio::SUCCESS,
26-
Some(Err(error)) => error.into(),
28+
Some(Err(error)) => {
29+
pinocchio_log::log!("fast_process_instruction: {}", error.to_str::<DlpError>());
30+
error.into()
31+
}
2732

2833
// Fallback to the slow path that does not use pinocchio SDK.
2934
None => slow_entrypoint(input),
@@ -39,6 +44,9 @@ pub unsafe fn slow_entrypoint(input: *mut u8) -> u64 {
3944
let (program_id, accounts, instruction_data) = unsafe { entrypoint::deserialize(input) };
4045
match slow_process_instruction(program_id, &accounts, instruction_data) {
4146
Ok(()) => entrypoint::SUCCESS,
42-
Err(error) => error.into(),
47+
Err(error) => {
48+
solana_program::msg!("slow_process_instruction: {}", error);
49+
error.into()
50+
}
4351
}
4452
}

src/error.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
use num_enum::IntoPrimitive;
1+
use num_enum::{IntoPrimitive, TryFromPrimitive};
22
use solana_program::program_error::ProgramError;
3+
use strum::IntoStaticStr;
34
use thiserror::Error;
45

5-
#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, IntoPrimitive)]
6+
#[derive(
7+
Debug, Error, Clone, Copy, PartialEq, Eq, IntoPrimitive, TryFromPrimitive, IntoStaticStr,
8+
)]
69
#[repr(u32)]
710
pub enum DlpError {
811
#[error("Invalid Authority")]
@@ -147,3 +150,12 @@ impl From<DlpError> for pinocchio::program_error::ProgramError {
147150
pinocchio::program_error::ProgramError::Custom(e as u32)
148151
}
149152
}
153+
154+
impl pinocchio::program_error::ToStr for DlpError {
155+
fn to_str<E>(&self) -> &'static str
156+
where
157+
E: 'static + pinocchio::program_error::ToStr + TryFrom<u32>,
158+
{
159+
self.into()
160+
}
161+
}

src/instruction_builder/commit_finalize.rs

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,69 @@
1-
use borsh::to_vec;
21
use solana_program::instruction::Instruction;
32
use solana_program::system_program;
43
use solana_program::{instruction::AccountMeta, pubkey::Pubkey};
54

65
use crate::args::CommitFinalizeArgs;
76
use crate::discriminator::DlpDiscriminator;
8-
use crate::pda::{
9-
delegation_metadata_pda_from_delegated_account, delegation_record_pda_from_delegated_account,
10-
program_config_from_program_id, validator_fees_vault_pda_from_validator,
7+
use crate::pod_view::PodView;
8+
use crate::{
9+
delegation_metadata_seeds_from_delegated_account,
10+
delegation_record_seeds_from_delegated_account, program_config_seeds_from_program_id,
11+
total_size_budget, validator_fees_vault_seeds_from_validator, AccountSizeClass,
12+
DLP_PROGRAM_DATA_SIZE_CLASS,
1113
};
12-
use crate::{total_size_budget, AccountSizeClass, DLP_PROGRAM_DATA_SIZE_CLASS};
1314

1415
/// Builds a commit finalize instruction.
1516
/// See [crate::processor::process_commit_finalize] for docs.
1617
pub fn commit_finalize(
1718
validator: Pubkey,
1819
delegated_account: Pubkey,
1920
delegated_account_owner: Pubkey,
20-
commit_args: CommitFinalizeArgs,
21+
commit_args: &mut CommitFinalizeArgs,
22+
data: &[u8],
2123
) -> Instruction {
22-
let commit_args = to_vec(&commit_args).unwrap();
23-
let delegation_record_pda = delegation_record_pda_from_delegated_account(&delegated_account);
24-
let validator_fees_vault_pda = validator_fees_vault_pda_from_validator(&validator);
25-
let delegation_metadata_pda =
26-
delegation_metadata_pda_from_delegated_account(&delegated_account);
27-
let program_config_pda = program_config_from_program_id(&delegated_account_owner);
24+
let delegation_record = Pubkey::find_program_address(
25+
delegation_record_seeds_from_delegated_account!(delegated_account),
26+
&crate::id(),
27+
);
28+
29+
let validator_fees_vault = Pubkey::find_program_address(
30+
validator_fees_vault_seeds_from_validator!(validator),
31+
&crate::id(),
32+
);
33+
34+
let delegation_metadata = Pubkey::find_program_address(
35+
delegation_metadata_seeds_from_delegated_account!(delegated_account),
36+
&crate::id(),
37+
);
38+
39+
let program_config = Pubkey::find_program_address(
40+
program_config_seeds_from_program_id!(delegated_account_owner),
41+
&crate::id(),
42+
);
43+
44+
// save the bumps in the args
45+
commit_args.delegation_record_bump = delegation_record.1;
46+
commit_args.delegation_metadata_bump = delegation_metadata.1;
47+
commit_args.validator_fees_vault_bump = validator_fees_vault.1;
48+
commit_args.program_config_bump = program_config.1;
49+
2850
Instruction {
2951
program_id: crate::id(),
3052
accounts: vec![
3153
AccountMeta::new_readonly(validator, true),
3254
AccountMeta::new(delegated_account, false),
33-
AccountMeta::new_readonly(delegation_record_pda, false),
34-
AccountMeta::new(delegation_metadata_pda, false),
35-
AccountMeta::new_readonly(validator_fees_vault_pda, false),
36-
AccountMeta::new_readonly(program_config_pda, false),
55+
AccountMeta::new_readonly(delegation_record.0, false),
56+
AccountMeta::new(delegation_metadata.0, false),
57+
AccountMeta::new_readonly(validator_fees_vault.0, false),
58+
AccountMeta::new_readonly(program_config.0, false),
3759
AccountMeta::new_readonly(system_program::id(), false),
3860
],
39-
data: [DlpDiscriminator::CommitFinalize.to_vec(), commit_args].concat(),
61+
data: [
62+
DlpDiscriminator::CommitFinalize.to_vec(),
63+
commit_args.to_bytes(),
64+
data.to_vec(),
65+
]
66+
.concat(),
4067
}
4168
}
4269

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ mod discriminator;
2929
pub mod error;
3030
pub mod instruction_builder;
3131
pub mod pda;
32+
pub mod pod_view;
3233
pub mod state;
3334

3435
mod account_size_class;
@@ -37,6 +38,7 @@ pub use account_size_class::*;
3738

3839
#[cfg(not(feature = "sdk"))]
3940
mod diff;
41+
4042
#[cfg(not(feature = "sdk"))]
4143
mod processor;
4244

0 commit comments

Comments
 (0)