Skip to content

Commit 45be7bb

Browse files
committed
feat: add CommitFinalizeFromBuffer
1 parent aebbf1c commit 45be7bb

10 files changed

Lines changed: 430 additions & 2 deletions

src/discriminator.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ pub enum DlpDiscriminator {
4747

4848
/// See [crate::processor::process_commit_finalize] for docs.
4949
CommitFinalize = 20,
50+
51+
/// See [crate::processor::process_commit_finalize_from_buffer] for docs.
52+
CommitFinalizeFromBuffer = 21,
5053
}
5154

5255
impl DlpDiscriminator {
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
use solana_program::instruction::Instruction;
2+
use solana_program::system_program;
3+
use solana_program::{instruction::AccountMeta, pubkey::Pubkey};
4+
5+
use crate::args::{CommitBumps, CommitFinalizeArgs};
6+
use crate::discriminator::DlpDiscriminator;
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,
13+
};
14+
15+
/// Builds a commit state from buffer instruction.
16+
/// See [crate::processor::process_commit_diff_from_buffer] for docs.
17+
pub fn commit_finalize_from_buffer(
18+
validator: Pubkey,
19+
delegated_account: Pubkey,
20+
delegated_account_owner: Pubkey,
21+
data_buffer: Pubkey,
22+
commit_args: &mut CommitFinalizeArgs,
23+
) -> (Instruction, super::CommitPDAs) {
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.bumps = CommitBumps {
46+
delegation_record: delegation_record.1,
47+
delegation_metadata: delegation_metadata.1,
48+
validator_fees_vault: validator_fees_vault.1,
49+
program_config: program_config.1,
50+
};
51+
52+
(
53+
Instruction {
54+
program_id: crate::id(),
55+
accounts: vec![
56+
AccountMeta::new_readonly(validator, true),
57+
AccountMeta::new(delegated_account, false),
58+
AccountMeta::new_readonly(delegation_record.0, false),
59+
AccountMeta::new(delegation_metadata.0, false),
60+
AccountMeta::new_readonly(data_buffer, false),
61+
AccountMeta::new_readonly(validator_fees_vault.0, false),
62+
AccountMeta::new_readonly(program_config.0, false),
63+
AccountMeta::new_readonly(system_program::id(), false),
64+
],
65+
data: [
66+
DlpDiscriminator::CommitFinalizeFromBuffer.to_vec(),
67+
commit_args.to_bytes(),
68+
]
69+
.concat(),
70+
},
71+
super::CommitPDAs {
72+
delegation_record: delegation_record.0,
73+
delegation_metadata: delegation_metadata.0,
74+
validator_fees_vault: validator_fees_vault.0,
75+
program_config: program_config.0,
76+
},
77+
)
78+
}
79+
80+
///
81+
/// Returns accounts-data-size budget for commit_diff_from_buffer instruction.
82+
///
83+
/// This value can be used with ComputeBudgetInstruction::SetLoadedAccountsDataSizeLimit
84+
///
85+
pub fn commit_finalize_from_buffer_size_budget(delegated_account: AccountSizeClass) -> u32 {
86+
total_size_budget(&[
87+
DLP_PROGRAM_DATA_SIZE_CLASS,
88+
AccountSizeClass::Tiny, // validator
89+
delegated_account, // delegated_account
90+
AccountSizeClass::Tiny, // delegation_record_pda
91+
AccountSizeClass::Tiny, // delegation_metadata_pda
92+
delegated_account, // data_buffer
93+
AccountSizeClass::Tiny, // validator_fees_vault_pda
94+
AccountSizeClass::Tiny, // program_config_pda
95+
AccountSizeClass::Tiny, // system_program
96+
])
97+
}

src/instruction_builder/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod close_validator_fees_vault;
44
mod commit_diff;
55
mod commit_diff_from_buffer;
66
mod commit_finalize;
7+
mod commit_finalize_from_buffer;
78
mod commit_state;
89
mod commit_state_from_buffer;
910
mod delegate;
@@ -24,6 +25,7 @@ pub use close_validator_fees_vault::*;
2425
pub use commit_diff::*;
2526
pub use commit_diff_from_buffer::*;
2627
pub use commit_finalize::*;
28+
pub use commit_finalize_from_buffer::*;
2729
pub use commit_state::*;
2830
pub use commit_state_from_buffer::*;
2931
pub use delegate::*;

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ pub fn fast_process_instruction(
121121
DlpDiscriminator::CommitFinalize => Some(processor::fast::process_commit_finalize(
122122
program_id, accounts, data,
123123
)),
124+
DlpDiscriminator::CommitFinalizeFromBuffer => Some(
125+
processor::fast::process_commit_finalize_from_buffer(program_id, accounts, data),
126+
),
124127
DlpDiscriminator::Finalize => Some(processor::fast::process_finalize(
125128
program_id, accounts, data,
126129
)),
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
use pinocchio::{account_info::AccountInfo, pubkey::Pubkey, ProgramResult};
2+
use pinocchio_log::log;
3+
4+
use crate::args::CommitFinalizeArgs;
5+
use crate::pod_view::PodView;
6+
use crate::processor::fast::internal::{
7+
process_commit_finalize_internal, CommitFinalizeInternalArgs,
8+
};
9+
use crate::processor::fast::NewState;
10+
use crate::{require_n_accounts, DiffSet};
11+
12+
/// Commit a new state of a delegated PDA
13+
///
14+
/// Accounts:
15+
///
16+
/// 0: `[signer]` the validator requesting the commit
17+
/// 1: `[]` the delegated account
18+
/// 2: `[writable]` the PDA storing the new state
19+
/// 3: `[writable]` the PDA storing the commit record
20+
/// 4: `[]` the delegation record
21+
/// 5: `[writable]` the delegation metadata
22+
/// 6: `[]` the validator fees vault
23+
/// 7: `[]` the program config account
24+
///
25+
/// Instruction Data: CommitFinalizeArgs
26+
///
27+
/// Requirements:
28+
///
29+
/// - delegation record is initialized
30+
/// - delegation metadata is initialized
31+
/// - validator fees vault is initialized
32+
/// - program config is initialized
33+
/// - commit state is uninitialized
34+
/// - commit record is uninitialized
35+
/// - delegated account holds at least the lamports indicated in the delegation record
36+
/// - account was not committed at a later slot
37+
///
38+
/// Steps:
39+
/// 1. Check that the pda is delegated
40+
/// 2. Init a new PDA to store the new state
41+
/// 3. Copy the new state to the new PDA
42+
/// 4. Init a new PDA to store the record of the new state commitment
43+
pub fn process_commit_finalize_from_buffer(
44+
_program_id: &Pubkey,
45+
accounts: &[AccountInfo],
46+
data: &[u8],
47+
) -> ProgramResult {
48+
let [
49+
validator, // force multi-line
50+
delegated_account,
51+
delegation_record_account,
52+
delegation_metadata_account,
53+
data_account, // full bytes or diff
54+
validator_fees_vault,
55+
program_config_account,
56+
_system_program,
57+
] = require_n_accounts!(accounts, 8);
58+
59+
let args = CommitFinalizeArgs::try_view_from(data)?;
60+
61+
let data = data_account.try_borrow_data()?;
62+
let commit_args = CommitFinalizeInternalArgs {
63+
bumps: &args.bumps,
64+
new_state: if args.data_is_diff.is_true() {
65+
let diffset = DiffSet::try_new(data.as_ref())?;
66+
if diffset.segments_count() == 0 {
67+
log!("WARN: noop; empty diff sent");
68+
}
69+
NewState::Diff(diffset)
70+
} else {
71+
NewState::FullBytes(&data)
72+
},
73+
commit_id: args.commit_id,
74+
allow_undelegation: args.allow_undelegation.is_true(),
75+
validator,
76+
delegated_account,
77+
delegation_record_account,
78+
delegation_metadata_account,
79+
validator_fees_vault,
80+
program_config_account,
81+
};
82+
83+
process_commit_finalize_internal(commit_args)
84+
}

src/processor/fast/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod commit_diff;
22
mod commit_diff_from_buffer;
33
mod commit_finalize;
4+
mod commit_finalize_from_buffer;
45
mod commit_state;
56
mod commit_state_from_buffer;
67
mod delegate;
@@ -14,6 +15,7 @@ pub(crate) mod internal;
1415
pub use commit_diff::*;
1516
pub use commit_diff_from_buffer::*;
1617
pub use commit_finalize::*;
18+
pub use commit_finalize_from_buffer::*;
1719
pub use commit_state::*;
1820
pub use commit_state_from_buffer::*;
1921
pub use delegate::*;

src/processor/fast/utils/requires.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ pub fn require_owned_pda(
414414
if !pubkey_eq(info.owner(), owner) {
415415
log!("Invalid account owner for {}:", label);
416416
pubkey::log(info.key());
417+
pubkey::log(info.owner());
418+
pubkey::log(owner);
417419
return Err(ProgramError::InvalidAccountOwner);
418420
}
419421
Ok(())

src/processor/init_validator_fees_vault.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::validator_fees_vault_seeds_from_validator;
1616
/// Accounts:
1717
///
1818
/// 0; `[signer]` payer
19-
/// 1; `[signer]` admin that controls the vault
19+
/// 1; `[signer]` magicblock admin that controls the vault
2020
/// 2; `[]` validator_identity
2121
/// 3; `[]` validator_fees_vault_pda
2222
/// 4; `[]` system_program

tests/test_commit_finalize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ async fn test_commit_finalize_perf() {
5858

5959
let metadata = metadata.unwrap();
6060

61-
assertables::assert_lt!(metadata.compute_units_consumed, 1100);
61+
assertables::assert_lt!(metadata.compute_units_consumed, 1150);
6262

6363
assert_eq!(
6464
metadata.log_messages.len(),

0 commit comments

Comments
 (0)