Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion dlp-api/src/instruction_builder/commit_finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ use dlp::{
delegation_metadata_seeds_from_delegated_account,
delegation_record_seeds_from_delegated_account,
discriminator::DlpDiscriminator,
pda::{
fees_vault_pda, undelegate_buffer_pda_from_delegated_account,
undelegation_request_pda_from_delegated_account,
},
pod_view::PodView,
total_size_budget, validator_fees_vault_seeds_from_validator,
AccountSizeClass, DLP_PROGRAM_DATA_SIZE_CLASS,
Expand All @@ -13,7 +17,7 @@ use solana_program::{
};
use solana_sdk_ids::system_program;

use crate::compat::Modernize;
use crate::compat::{Compatize, Modernize};

pub struct CommitPDAs {
pub delegation_record: Pubkey,
Expand All @@ -26,9 +30,12 @@ pub struct CommitPDAs {
pub fn commit_finalize(
validator: Pubkey,
delegated_account: Pubkey,
owner_program: Pubkey,
rent_reimbursement: Pubkey,
args: &mut CommitFinalizeArgs,
state_or_diff: &[u8],
) -> (Instruction, CommitPDAs) {
let delegated_account_compat = delegated_account.compatize();
let delegation_record = Pubkey::find_program_address(
delegation_record_seeds_from_delegated_account!(delegated_account),
&dlp::id().modernize(),
Expand All @@ -43,6 +50,15 @@ pub fn commit_finalize(
validator_fees_vault_seeds_from_validator!(validator),
&dlp::id().modernize(),
);
let undelegate_buffer_pda =
undelegate_buffer_pda_from_delegated_account(&delegated_account_compat)
.modernize();
let undelegation_request_pda =
undelegation_request_pda_from_delegated_account(
&delegated_account_compat,
)
.modernize();
let fees_vault_pda = fees_vault_pda().modernize();

// save the bumps in the args
args.bumps = CommitBumps {
Expand All @@ -61,6 +77,11 @@ pub fn commit_finalize(
AccountMeta::new(delegation_metadata.0, false),
AccountMeta::new(validator_fees_vault.0, false),
AccountMeta::new_readonly(system_program::id(), false),
AccountMeta::new_readonly(owner_program, false),
AccountMeta::new(undelegate_buffer_pda, false),
AccountMeta::new(rent_reimbursement, false),
AccountMeta::new(fees_vault_pda, false),
AccountMeta::new(undelegation_request_pda, false),
],
data: [
DlpDiscriminator::CommitFinalize.to_vec(),
Expand Down Expand Up @@ -91,5 +112,10 @@ pub fn commit_finalize_size_budget(delegated_account: AccountSizeClass) -> u32 {
AccountSizeClass::Tiny, // delegation_metadata_pda
AccountSizeClass::Tiny, // validator_fees_vault_pda
AccountSizeClass::Tiny, // system_program
AccountSizeClass::Tiny, // owner_program
delegated_account, // undelegate_buffer_pda
AccountSizeClass::Tiny, // rent_reimbursement
AccountSizeClass::Tiny, // fees_vault_pda
AccountSizeClass::Tiny, // undelegation_request_pda
])
}
29 changes: 27 additions & 2 deletions dlp-api/src/instruction_builder/commit_finalize_from_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ use dlp::{
delegation_metadata_seeds_from_delegated_account,
delegation_record_seeds_from_delegated_account,
discriminator::DlpDiscriminator,
pda::{
fees_vault_pda, undelegate_buffer_pda_from_delegated_account,
undelegation_request_pda_from_delegated_account,
},
pod_view::PodView,
total_size_budget, validator_fees_vault_seeds_from_validator,
AccountSizeClass, DLP_PROGRAM_DATA_SIZE_CLASS,
Expand All @@ -13,16 +17,19 @@ use solana_program::{
};
use solana_sdk_ids::system_program;

use crate::compat::Modernize;
use crate::compat::{Compatize, Modernize};

/// Builds a commit state from buffer instruction.
/// See [dlp::processor::process_commit_diff_from_buffer] for docs.
pub fn commit_finalize_from_buffer(
validator: Pubkey,
delegated_account: Pubkey,
data_buffer: Pubkey,
owner_program: Pubkey,
rent_reimbursement: Pubkey,
commit_args: &mut CommitFinalizeArgs,
) -> (Instruction, super::CommitPDAs) {
let delegated_account_compat = delegated_account.compatize();
let delegation_record = Pubkey::find_program_address(
delegation_record_seeds_from_delegated_account!(delegated_account),
&dlp::id().modernize(),
Expand All @@ -37,6 +44,15 @@ pub fn commit_finalize_from_buffer(
delegation_metadata_seeds_from_delegated_account!(delegated_account),
&dlp::id().modernize(),
);
let undelegate_buffer_pda =
undelegate_buffer_pda_from_delegated_account(&delegated_account_compat)
.modernize();
let undelegation_request_pda =
undelegation_request_pda_from_delegated_account(
&delegated_account_compat,
)
.modernize();
let fees_vault_pda = fees_vault_pda().modernize();

// save the bumps in the args
commit_args.bumps = CommitBumps {
Expand All @@ -56,6 +72,11 @@ pub fn commit_finalize_from_buffer(
AccountMeta::new_readonly(data_buffer, false),
AccountMeta::new(validator_fees_vault.0, false),
AccountMeta::new_readonly(system_program::id(), false),
AccountMeta::new_readonly(owner_program, false),
AccountMeta::new(undelegate_buffer_pda, false),
AccountMeta::new(rent_reimbursement, false),
AccountMeta::new(fees_vault_pda, false),
AccountMeta::new(undelegation_request_pda, false),
],
data: [
DlpDiscriminator::CommitFinalizeFromBuffer.to_vec(),
Expand Down Expand Up @@ -87,7 +108,11 @@ pub fn commit_finalize_from_buffer_size_budget(
AccountSizeClass::Tiny, // delegation_metadata_pda
delegated_account, // data_buffer
AccountSizeClass::Tiny, // validator_fees_vault_pda
AccountSizeClass::Tiny, // program_config_pda
AccountSizeClass::Tiny, // system_program
AccountSizeClass::Tiny, // owner_program
delegated_account, // undelegate_buffer_pda
AccountSizeClass::Tiny, // rent_reimbursement
AccountSizeClass::Tiny, // fees_vault_pda
AccountSizeClass::Tiny, // undelegation_request_pda
])
}
30 changes: 28 additions & 2 deletions dlp-api/src/instruction_builder/finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use dlp::{
commit_record_pda_from_delegated_account,
commit_state_pda_from_delegated_account,
delegation_metadata_pda_from_delegated_account,
delegation_record_pda_from_delegated_account,
delegation_record_pda_from_delegated_account, fees_vault_pda,
undelegate_buffer_pda_from_delegated_account,
undelegation_request_pda_from_delegated_account,
validator_fees_vault_pda_from_validator,
},
total_size_budget, AccountSizeClass, DLP_PROGRAM_DATA_SIZE_CLASS,
Expand All @@ -19,7 +21,12 @@ use crate::compat::{Compatize, Modernize};

/// Builds a finalize state instruction.
/// See [dlp::processor::process_finalize] for docs.
pub fn finalize(validator: Pubkey, delegated_account: Pubkey) -> Instruction {
pub fn finalize(
validator: Pubkey,
delegated_account: Pubkey,
owner_program: Pubkey,
rent_reimbursement: Pubkey,
) -> Instruction {
let validator_compat = validator.compatize();
let delegated_account_compat = delegated_account.compatize();
let commit_state_pda =
Expand All @@ -36,6 +43,15 @@ pub fn finalize(validator: Pubkey, delegated_account: Pubkey) -> Instruction {
&delegated_account_compat,
)
.modernize();
let fees_vault_pda = fees_vault_pda().modernize();
let undelegate_buffer_pda =
undelegate_buffer_pda_from_delegated_account(&delegated_account_compat)
.modernize();
let undelegation_request_pda =
undelegation_request_pda_from_delegated_account(
&delegated_account_compat,
)
.modernize();
let validator_fees_vault_pda =
validator_fees_vault_pda_from_validator(&validator_compat).modernize();
Instruction {
Expand All @@ -49,6 +65,11 @@ pub fn finalize(validator: Pubkey, delegated_account: Pubkey) -> Instruction {
AccountMeta::new(delegation_metadata_pda, false),
AccountMeta::new(validator_fees_vault_pda, false),
AccountMeta::new_readonly(system_program::id(), false),
AccountMeta::new_readonly(owner_program, false),
AccountMeta::new(undelegate_buffer_pda, false),
AccountMeta::new(rent_reimbursement, false),
AccountMeta::new(fees_vault_pda, false),
AccountMeta::new(undelegation_request_pda, false),
],
data: DlpDiscriminator::Finalize.to_vec(),
}
Expand All @@ -70,5 +91,10 @@ pub fn finalize_size_budget(delegated_account: AccountSizeClass) -> u32 {
AccountSizeClass::Tiny, // delegation_metadata_pda
AccountSizeClass::Tiny, // validator_fees_vault_pda
AccountSizeClass::Tiny, // system_program
AccountSizeClass::Tiny, // owner_program
delegated_account, // undelegate_buffer_pda
AccountSizeClass::Tiny, // rent_reimbursement
AccountSizeClass::Tiny, // fees_vault_pda
AccountSizeClass::Tiny, // undelegation_request_pda
])
}
8 changes: 8 additions & 0 deletions dlp-api/src/state/undelegation_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ pub struct UndelegationRequest {
pub owner_program: Pubkey,

/// The account that paid rent for this request PDA.
///
/// Owner-program requests intentionally use the same account as
/// `DelegationMetadata::rent_payer`. That matches the common case where
/// the delegation payer also requests undelegation, and it lets validators
/// process auto-undelegation by fetching one account (delegation metadata only)
/// instead of two accounts and it also keeps the tx-size smaller. The request
/// PDA can be derived and passed without fetching it just to discover
/// a second close reimbursement account.
pub rent_payer: Pubkey,

/// The slot at which the request was created.
Expand Down
36 changes: 26 additions & 10 deletions src/processor/fast/commit_finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ use crate::{
args::CommitFinalizeArgsWithBuffer,
processor::fast::{
internal::{
parse_auto_undelegation_accounts,
process_auto_undelegation_if_requested,
process_commit_finalize_internal, CommitFinalizeInternalArgs,
},
NewState,
},
require_n_accounts, DiffSet,
require_n_accounts_with_optionals, DiffSet,
};

/// Commit a new state, or a diff, directly to the delegated account. Unlike, CommitState and
Expand All @@ -32,14 +34,18 @@ pub fn process_commit_finalize(
accounts: &[AccountView],
data: &[u8],
) -> ProgramResult {
let [
validator, // force multi-line
delegated_account,
delegation_record_account,
delegation_metadata_account,
validator_fees_vault,
_system_program,
] = require_n_accounts!(accounts, 6);
let (
[
validator, // force multi-line
delegated_account,
delegation_record_account,
delegation_metadata_account,
validator_fees_vault,
system_program,
],
optional_accounts,
) = require_n_accounts_with_optionals!(accounts, 6);
let auto_accounts = parse_auto_undelegation_accounts(optional_accounts)?;

let args = CommitFinalizeArgsWithBuffer::from_bytes(data)?;

Expand All @@ -64,5 +70,15 @@ pub fn process_commit_finalize(
validator_fees_vault,
};

process_commit_finalize_internal(commit_args)
let requester = process_commit_finalize_internal(commit_args)?;
process_auto_undelegation_if_requested(
requester,
validator,
delegated_account,
delegation_record_account,
delegation_metadata_account,
validator_fees_vault,
system_program,
auto_accounts,
)
}
38 changes: 27 additions & 11 deletions src/processor/fast/commit_finalize_from_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ use crate::{
pod_view::PodView,
processor::fast::{
internal::{
parse_auto_undelegation_accounts,
process_auto_undelegation_if_requested,
process_commit_finalize_internal, CommitFinalizeInternalArgs,
},
NewState,
},
require_n_accounts, DiffSet,
require_n_accounts_with_optionals, DiffSet,
};

/// Commit a new state of a delegated PDA
Expand Down Expand Up @@ -48,15 +50,19 @@ pub fn process_commit_finalize_from_buffer(
accounts: &[AccountView],
data: &[u8],
) -> ProgramResult {
let [
validator, // force multi-line
delegated_account,
delegation_record_account,
delegation_metadata_account,
data_account, // full bytes or diff
validator_fees_vault,
_system_program,
] = require_n_accounts!(accounts, 7);
let (
[
validator, // force multi-line
delegated_account,
delegation_record_account,
delegation_metadata_account,
data_account, // full bytes or diff
validator_fees_vault,
system_program,
],
optional_accounts,
) = require_n_accounts_with_optionals!(accounts, 7);
let auto_accounts = parse_auto_undelegation_accounts(optional_accounts)?;

let args = CommitFinalizeArgs::try_view_from(data)?;

Expand All @@ -82,5 +88,15 @@ pub fn process_commit_finalize_from_buffer(
validator_fees_vault,
};

process_commit_finalize_internal(commit_args)
let requester = process_commit_finalize_internal(commit_args)?;
process_auto_undelegation_if_requested(
requester,
validator,
delegated_account,
delegation_record_account,
delegation_metadata_account,
validator_fees_vault,
system_program,
auto_accounts,
)
}
24 changes: 13 additions & 11 deletions src/processor/fast/commit_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,20 @@ pub(crate) fn process_commit_state_internal(
return Err(DlpError::NonceOutOfOrder.into());
}

// Once undelegation has been requested, any subsequent commit should fail.
if delegation_metadata.undelegation_requester
== UndelegationRequester::Validator
{
log!("delegation metadata already has an undelegation requester: ");
args.delegation_metadata_account.address().log();
return Err(DlpError::AlreadyUndelegated.into());
match delegation_metadata.undelegation_requester {
UndelegationRequester::None => {
delegation_metadata.undelegation_requester =
UndelegationRequester::from_allow_undelegation(
args.allow_undelegation,
);
}
UndelegationRequester::OwnerProgram => {}
UndelegationRequester::Validator => {
log!("delegation metadata already has an undelegation requester: ");
args.delegation_metadata_account.address().log();
return Err(DlpError::AlreadyUndelegated.into());
}
}

// Record whether this commit requested undelegation.
delegation_metadata.undelegation_requester =
UndelegationRequester::from_allow_undelegation(args.allow_undelegation);
delegation_metadata
.to_bytes_with_discriminator(&mut delegation_metadata_data.as_mut())
.map_err(to_pinocchio_program_error)?;
Expand Down
Loading
Loading