Skip to content

Commit 912aba3

Browse files
authored
feat: add CommitFinalizeFromBuffer (#137)
* feat: add CommitFinalizeFromBuffer * Cleanup
1 parent 413a173 commit 912aba3

10 files changed

Lines changed: 414 additions & 2 deletions

src/discriminator.rs

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

5151
/// See [crate::processor::process_commit_finalize] for docs.
5252
CommitFinalize = 21,
53+
54+
/// See [crate::processor::process_commit_finalize_from_buffer] for docs.
55+
CommitFinalizeFromBuffer = 22,
5356
}
5457

5558
impl DlpDiscriminator {
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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, total_size_budget,
11+
validator_fees_vault_seeds_from_validator, AccountSizeClass, DLP_PROGRAM_DATA_SIZE_CLASS,
12+
};
13+
14+
/// Builds a commit state from buffer instruction.
15+
/// See [crate::processor::process_commit_diff_from_buffer] for docs.
16+
pub fn commit_finalize_from_buffer(
17+
validator: Pubkey,
18+
delegated_account: Pubkey,
19+
data_buffer: Pubkey,
20+
commit_args: &mut CommitFinalizeArgs,
21+
) -> (Instruction, super::CommitPDAs) {
22+
let delegation_record = Pubkey::find_program_address(
23+
delegation_record_seeds_from_delegated_account!(delegated_account),
24+
&crate::id(),
25+
);
26+
27+
let validator_fees_vault = Pubkey::find_program_address(
28+
validator_fees_vault_seeds_from_validator!(validator),
29+
&crate::id(),
30+
);
31+
32+
let delegation_metadata = Pubkey::find_program_address(
33+
delegation_metadata_seeds_from_delegated_account!(delegated_account),
34+
&crate::id(),
35+
);
36+
37+
// save the bumps in the args
38+
commit_args.bumps = CommitBumps {
39+
delegation_record: delegation_record.1,
40+
delegation_metadata: delegation_metadata.1,
41+
validator_fees_vault: validator_fees_vault.1,
42+
};
43+
44+
(
45+
Instruction {
46+
program_id: crate::id(),
47+
accounts: vec![
48+
AccountMeta::new_readonly(validator, true),
49+
AccountMeta::new(delegated_account, false),
50+
AccountMeta::new_readonly(delegation_record.0, false),
51+
AccountMeta::new(delegation_metadata.0, false),
52+
AccountMeta::new_readonly(data_buffer, false),
53+
AccountMeta::new_readonly(validator_fees_vault.0, false),
54+
AccountMeta::new_readonly(system_program::id(), false),
55+
],
56+
data: [
57+
DlpDiscriminator::CommitFinalizeFromBuffer.to_vec(),
58+
commit_args.to_bytes(),
59+
]
60+
.concat(),
61+
},
62+
super::CommitPDAs {
63+
delegation_record: delegation_record.0,
64+
delegation_metadata: delegation_metadata.0,
65+
validator_fees_vault: validator_fees_vault.0,
66+
},
67+
)
68+
}
69+
70+
///
71+
/// Returns accounts-data-size budget for commit_diff_from_buffer instruction.
72+
///
73+
/// This value can be used with ComputeBudgetInstruction::SetLoadedAccountsDataSizeLimit
74+
///
75+
pub fn commit_finalize_from_buffer_size_budget(delegated_account: AccountSizeClass) -> u32 {
76+
total_size_budget(&[
77+
DLP_PROGRAM_DATA_SIZE_CLASS,
78+
AccountSizeClass::Tiny, // validator
79+
delegated_account, // delegated_account
80+
AccountSizeClass::Tiny, // delegation_record_pda
81+
AccountSizeClass::Tiny, // delegation_metadata_pda
82+
delegated_account, // data_buffer
83+
AccountSizeClass::Tiny, // validator_fees_vault_pda
84+
AccountSizeClass::Tiny, // program_config_pda
85+
AccountSizeClass::Tiny, // system_program
86+
])
87+
}

src/instruction_builder/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod close_validator_fees_vault;
55
mod commit_diff;
66
mod commit_diff_from_buffer;
77
mod commit_finalize;
8+
mod commit_finalize_from_buffer;
89
mod commit_state;
910
mod commit_state_from_buffer;
1011
mod delegate;
@@ -26,6 +27,7 @@ pub use close_validator_fees_vault::*;
2627
pub use commit_diff::*;
2728
pub use commit_diff_from_buffer::*;
2829
pub use commit_finalize::*;
30+
pub use commit_finalize_from_buffer::*;
2931
pub use commit_state::*;
3032
pub use commit_state_from_buffer::*;
3133
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: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
///
24+
/// Instruction Data: CommitFinalizeArgs
25+
///
26+
/// Requirements:
27+
///
28+
/// - delegation record is initialized
29+
/// - delegation metadata is initialized
30+
/// - validator fees vault is initialized
31+
/// - program config is initialized
32+
/// - commit state is uninitialized
33+
/// - commit record is uninitialized
34+
/// - delegated account holds at least the lamports indicated in the delegation record
35+
/// - account was not committed at a later slot
36+
///
37+
/// Steps:
38+
/// 1. Check that the pda is delegated
39+
/// 2. Init a new PDA to store the new state
40+
/// 3. Copy the new state to the new PDA
41+
/// 4. Init a new PDA to store the record of the new state commitment
42+
pub fn process_commit_finalize_from_buffer(
43+
_program_id: &Pubkey,
44+
accounts: &[AccountInfo],
45+
data: &[u8],
46+
) -> ProgramResult {
47+
let [
48+
validator, // force multi-line
49+
delegated_account,
50+
delegation_record_account,
51+
delegation_metadata_account,
52+
data_account, // full bytes or diff
53+
validator_fees_vault,
54+
_system_program,
55+
] = require_n_accounts!(accounts, 7);
56+
57+
let args = CommitFinalizeArgs::try_view_from(data)?;
58+
59+
let data = data_account.try_borrow_data()?;
60+
let commit_args = CommitFinalizeInternalArgs {
61+
bumps: &args.bumps,
62+
new_state: if args.data_is_diff.is_true() {
63+
let diffset = DiffSet::try_new(data.as_ref())?;
64+
if diffset.segments_count() == 0 {
65+
log!("WARN: noop; empty diff sent");
66+
}
67+
NewState::Diff(diffset)
68+
} else {
69+
NewState::FullBytes(&data)
70+
},
71+
commit_id: args.commit_id,
72+
allow_undelegation: args.allow_undelegation.is_true(),
73+
validator,
74+
delegated_account,
75+
delegation_record_account,
76+
delegation_metadata_account,
77+
validator_fees_vault,
78+
};
79+
80+
process_commit_finalize_internal(commit_args)
81+
}

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
@@ -367,6 +367,8 @@ pub fn require_owned_pda(
367367
if !pubkey_eq(info.owner(), owner) {
368368
log!("Invalid account owner for {}:", label);
369369
pubkey::log(info.key());
370+
pubkey::log(info.owner());
371+
pubkey::log(owner);
370372
return Err(ProgramError::InvalidAccountOwner);
371373
}
372374
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
@@ -22,7 +22,7 @@ mod fixtures;
2222

2323
#[tokio::test]
2424
async fn test_commit_finalize_data_perf() {
25-
run_test_commit_finalize(vec![0; 10240], vec![1; 10240], false, 1100).await;
25+
run_test_commit_finalize(vec![0; 10240], vec![1; 10240], false, 1150).await;
2626
}
2727

2828
#[tokio::test]

0 commit comments

Comments
 (0)