Skip to content
Merged
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
5 changes: 5 additions & 0 deletions dlp-api/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ pub enum DlpError {
#[error("Account cannot be delegated to the system program")]
DelegationToSystemProgramNotAllowed = 42,

#[error(
"The account lamports is too small to make the account rent-exempt"
)]
InsufficientRent = 43,
Comment thread
snawaz marked this conversation as resolved.

#[error("An infallible error is encountered possibly due to logic error")]
InfallibleError = 100,
}
Expand Down
4 changes: 2 additions & 2 deletions dlp-api/src/instruction_builder/commit_finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ pub fn commit_finalize(
accounts: vec![
AccountMeta::new(validator, true),
AccountMeta::new(delegated_account, false),
AccountMeta::new_readonly(delegation_record.0, false),
AccountMeta::new(delegation_record.0, false),
AccountMeta::new(delegation_metadata.0, false),
AccountMeta::new_readonly(validator_fees_vault.0, false),
AccountMeta::new(validator_fees_vault.0, false),
AccountMeta::new_readonly(system_program::id(), false),
],
data: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ pub fn commit_finalize_from_buffer(
accounts: vec![
AccountMeta::new(validator, true),
AccountMeta::new(delegated_account, false),
AccountMeta::new_readonly(delegation_record.0, false),
AccountMeta::new(delegation_record.0, false),
AccountMeta::new(delegation_metadata.0, false),
AccountMeta::new_readonly(data_buffer, false),
AccountMeta::new_readonly(validator_fees_vault.0, false),
AccountMeta::new(validator_fees_vault.0, false),
AccountMeta::new_readonly(system_program::id(), false),
],
data: [
Expand Down
2 changes: 1 addition & 1 deletion dlp-api/src/instruction_builder/finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn finalize(validator: Pubkey, delegated_account: Pubkey) -> Instruction {
Instruction {
program_id: dlp::id(),
accounts: vec![
AccountMeta::new_readonly(validator, true),
AccountMeta::new(validator, true),
AccountMeta::new(delegated_account, false),
AccountMeta::new(commit_state_pda, false),
AccountMeta::new(commit_record_pda, false),
Expand Down
18 changes: 9 additions & 9 deletions dlp-api/src/requires.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ macro_rules! require {
macro_rules! require_signer {
($info: expr) => {{
if !$info.is_signer() {
log!("require_signer!({}): ", stringify!($info));
pinocchio_log::log!("require_signer!({}): ", stringify!($info));
$info.address().log();
return Err(ProgramError::MissingRequiredSignature);
}
Expand Down Expand Up @@ -243,7 +243,7 @@ macro_rules! require_initialized_pda {
let pda = match pinocchio::Address::create_program_address($seeds, $program_id) {
Ok(pda) => pda,
Err(_) => {
log!(
pinocchio_log::log!(
"require_initialized_pda!({}, {}, {}, {}); create_program_address failed",
stringify!($info),
stringify!($seeds),
Expand All @@ -254,7 +254,7 @@ macro_rules! require_initialized_pda {
}
};
if !address_eq($info.address(), &pda) {
log!(
pinocchio_log::log!(
"require_initialized_pda!({}, {}, {}, {}); address_eq failed",
stringify!($info),
stringify!($seeds),
Expand All @@ -269,7 +269,7 @@ macro_rules! require_initialized_pda {
require_owned_by!($info, $program_id);

if $is_writable && !$info.is_writable() {
log!(
pinocchio_log::log!(
"require_initialized_pda!({}, {}, {}, {}); is_writable expectation failed",
stringify!($info),
stringify!($seeds),
Expand All @@ -287,7 +287,7 @@ macro_rules! require_initialized_pda_fast {
($info:expr, $seeds: expr, $is_writable: expr) => {{
let pda = solana_sha256_hasher::hashv($seeds).to_bytes();
if !address_eq($info.address(), &pda.into()) {
log!(
pinocchio_log::log!(
"require_initialized_pda!({}, {}, {}); address_eq failed",
stringify!($info),
stringify!($seeds),
Expand All @@ -300,7 +300,7 @@ macro_rules! require_initialized_pda_fast {
require_owned_by!($info, &$crate::fast::ID);

if $is_writable && !$info.is_writable() {
log!(
pinocchio_log::log!(
"require_initialized_pda!({}, {}, {}); is_writable expectation failed",
stringify!($info),
stringify!($seeds),
Expand All @@ -318,7 +318,7 @@ macro_rules! require_pda {
let pda = match pinocchio::Address::create_program_address($seeds, $program_id) {
Ok(pda) => pda,
Err(_) => {
log!(
pinocchio_log::log!(
"require_pda!({}, {}, {}, {}); create_program_address failed",
stringify!($info),
stringify!($seeds),
Expand All @@ -329,7 +329,7 @@ macro_rules! require_pda {
}
};
if !address_eq($info.address(), &pda) {
log!(
pinocchio_log::log!(
"require_pda!({}, {}, {}, {}); address_eq failed",
stringify!($info),
stringify!($seeds),
Expand All @@ -342,7 +342,7 @@ macro_rules! require_pda {
}

if $is_writable && !$info.is_writable() {
log!(
pinocchio_log::log!(
"require_pda!({}, {}, {}, {}); is_writable expectation failed",
stringify!($info),
stringify!($seeds),
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ pub(crate) use dlp_api::{
undelegate_buffer_seeds_from_delegated_account,
validator_fees_vault_seeds_from_validator,
};
#[allow(unused_imports)]
pub(crate) use dlp_api::{id, ID};
pub use dlp_api::{id, ID};
#[allow(unused_imports)]
pub(crate) use dlp_api::{
require, require_eq, require_eq_keys, require_ge, require_gt,
Expand Down
7 changes: 4 additions & 3 deletions src/processor/fast/commit_finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ use crate::{
/// Accounts:
///
/// 0: `[signer]` the validator requesting the commit
/// 1: `[]` the delegated account
/// 2: `[]` the delegation record
/// 1: `[writable]` the delegated account
/// 2: `[writable]` the delegation record
/// 3: `[writable]` the delegation metadata
/// 4: `[]` the validator fees vault
/// 4: `[writable]` the validator fees vault
/// 5: `[]` system program
Comment thread
snawaz marked this conversation as resolved.
///
/// Instruction Data: CommitFinalizeArgsWithBuffer
Expand Down Expand Up @@ -54,6 +54,7 @@ pub fn process_commit_finalize(
} else {
NewState::FullBytes(args.buffer)
},
commit_lamports: args.lamports,
commit_id: args.commit_id,
allow_undelegation: args.allow_undelegation.is_true(),
validator,
Expand Down
13 changes: 7 additions & 6 deletions src/processor/fast/commit_finalize_from_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ use crate::{
/// Accounts:
///
/// 0: `[signer]` the validator requesting the commit
/// 1: `[]` the delegated account
/// 2: `[writable]` the PDA storing the new state
/// 3: `[writable]` the PDA storing the commit record
/// 4: `[]` the delegation record
/// 5: `[writable]` the delegation metadata
/// 6: `[]` the validator fees vault
/// 1: `[writable]` the delegated account
/// 2: `[writable]` the delegation record
/// 3: `[writable]` the delegation metadata
/// 4: `[]` the data buffer
/// 5: `[writable]` the validator fees vault
/// 6: `[]` system program
Comment thread
snawaz marked this conversation as resolved.
///
/// Instruction Data: CommitFinalizeArgs
///
Expand Down Expand Up @@ -72,6 +72,7 @@ pub fn process_commit_finalize_from_buffer(
} else {
NewState::FullBytes(&data)
},
commit_lamports: args.lamports,
commit_id: args.commit_id,
allow_undelegation: args.allow_undelegation.is_true(),
validator,
Expand Down
2 changes: 1 addition & 1 deletion src/processor/fast/finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub fn process_finalize(
.map_err(to_pinocchio_program_error)?;

// Update the delegation record
delegation_record.lamports = delegated_account.lamports();
delegation_record.lamports = commit_record.lamports;
Comment thread
snawaz marked this conversation as resolved.

// Load commit state
let commit_state_data = commit_state_account.try_borrow()?;
Expand Down
65 changes: 49 additions & 16 deletions src/processor/fast/internal/commit_finalize_internal.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use pinocchio::{
address::{address_eq, PDA_MARKER},
error::ProgramError,
sysvars::{rent::Rent, Sysvar},
AccountView, Address,
};
use pinocchio_log::log;
use pinocchio_system::instructions as system;

use crate::{
apply_diff_in_place,
args::CommitBumps,
error::DlpError,
pda,
pod_view::PodView,
processor::fast::NewState,
processor::fast::{utils::LamportsOperation, NewState},
require, require_eq, require_eq_keys, require_ge,
require_initialized_pda_fast, require_owned_by, require_signer,
state::{DelegationMetadataFast, DelegationRecord},
Expand All @@ -21,6 +22,7 @@ use crate::{
pub(crate) struct CommitFinalizeInternalArgs<'a> {
pub(crate) bumps: &'a CommitBumps,
pub(crate) new_state: NewState<'a>,
pub(crate) commit_lamports: u64,
pub(crate) commit_id: u64,
pub(crate) allow_undelegation: bool,
pub(crate) validator: &'a AccountView,
Expand Down Expand Up @@ -48,7 +50,7 @@ pub(crate) fn process_commit_finalize_internal(
crate::fast::ID.as_ref(),
PDA_MARKER
],
false
true
);

require_initialized_pda_fast!(
Expand All @@ -72,7 +74,7 @@ pub(crate) fn process_commit_finalize_internal(
crate::fast::ID.as_ref(),
PDA_MARKER
],
false
true
);

// validate and update metadata
Expand All @@ -91,9 +93,11 @@ pub(crate) fn process_commit_finalize_internal(
);
}

let delegation_record_data = args.delegation_record_account.try_borrow()?;
let delegation_record =
DelegationRecord::try_view_from(&delegation_record_data.as_ref()[8..])?;
let mut delegation_record_data =
args.delegation_record_account.try_borrow_mut()?;
let delegation_record = DelegationRecord::try_view_from_mut(
&mut delegation_record_data.as_mut()[8..],
)?;

// Check that the authority is allowed to commit
require_eq_keys!(
Expand All @@ -109,22 +113,51 @@ pub(crate) fn process_commit_finalize_internal(
DlpError::InvalidDelegatedState
);

// if args.commit_record_lamports > delegation_record.lamports {
// system::Transfer {
// from: args.validator,
// to: args.commit_state_account,
// lamports: args.commit_record_lamports - delegation_record.lamports,
// }
// .invoke()?;
// }
let mut check_minimum_balance =
args.new_state.data_len() > args.delegated_account.data_len();

args.delegated_account.resize(args.new_state.data_len())?;

match args.commit_lamports.cmp(&delegation_record.lamports) {
std::cmp::Ordering::Greater => {
require!(args.validator.is_writable(), ProgramError::Immutable);

system::Transfer {
from: args.validator,
to: args.delegated_account,
lamports: args.commit_lamports - delegation_record.lamports,
}
.invoke()?;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
std::cmp::Ordering::Less => {
let amount = delegation_record.lamports - args.commit_lamports;

args.delegated_account.lamports_decrement_by(amount)?;
args.validator_fees_vault.lamports_increment_by(amount)?;

check_minimum_balance = true;
}
std::cmp::Ordering::Equal => {}
}

// Update the delegation record lamports after settling.
delegation_record.lamports = args.commit_lamports;

// require the account is still rent-exempted even after decrementing lamports
if check_minimum_balance {
require_ge!(
args.delegated_account.lamports(),
Rent::get()?
.try_minimum_balance(args.delegated_account.data_len())?,
DlpError::InsufficientRent
);
}
Comment thread
snawaz marked this conversation as resolved.

// copy the new state to the delegated account
let mut delegated_account_data = args.delegated_account.try_borrow_mut()?;
match args.new_state {
NewState::FullBytes(bytes) => {
(*delegated_account_data).copy_from_slice(bytes)
(*delegated_account_data).copy_from_slice(bytes);
}
NewState::Diff(diff) => {
apply_diff_in_place(&mut delegated_account_data, &diff)?;
Expand Down
28 changes: 28 additions & 0 deletions src/processor/fast/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
use pinocchio::{AccountView, ProgramResult};

use crate::error::DlpError;

pub(crate) mod pda;

pub trait LamportsOperation {
fn lamports_increment_by(&self, value: u64) -> ProgramResult;
fn lamports_decrement_by(&self, value: u64) -> ProgramResult;
}

impl LamportsOperation for AccountView {
fn lamports_increment_by(&self, value: u64) -> ProgramResult {
self.set_lamports(
self.lamports()
.checked_add(value)
.ok_or(DlpError::Overflow)?,
);
Ok(())
}
fn lamports_decrement_by(&self, value: u64) -> ProgramResult {
self.set_lamports(
self.lamports()
.checked_sub(value)
.ok_or(DlpError::Overflow)?,
);
Ok(())
}
}
Loading
Loading